I have a question regarding open3d. Is it possible to display two triangles as a rectangle?
I try to explain it with the following code example, what I would like as a result. (in principle I would like to display rectangle elements, and I think this is only possible if I create two rectangles)
import open3d as o3d
import open3d.visualization.gui as gui
import open3d.visualization.rendering as rendering
import numpy as np
node_array = np.zeros(shape=(4, 3))
shell_array = np.zeros(shape=(2, 3))
"""
Nodes
0 0 0 0
1 10 0 0
2 10 10 0
3 0 10 0
Shells
0 0 1 3
1 3 1 2
"""
node_array[0] = [0, 0, 0]
node_array[1] = [10, 0, 0]
node_array[2] = [10, 10, 0]
node_array[3] = [0, 10, 0]
shell_array[0] = [0, 1, 3]
shell_array[1] = [3, 1, 2]
mesh = o3d.geometry.TriangleMesh()
mesh.vertices = o3d.utility.Vector3dVector(node_array)
mesh.triangles = o3d.utility.Vector3iVector(shell_array)
o3d.visualization.draw_geometries([mesh], mesh_show_wireframe=True)
And I would like to have it displayed that way.
Is this even possible with open3d?
Thanks a lot for your help.
Related
I have a row A = [0 1 2 3 4] and an index I = [0 0 1 0 1]. I would like to extract the elements in A indexed by I, i.e. [2, 4].
My attempt:
import numpy as np
A = np.array([0, 1, 2, 3, 4])
index = np.array([0, 0, 1, 0, 1])
print(A[index])
The result is not as I expected:
[0 0 1 0 1]
Could you please elaborate on how to achieve my goal?
I think you want boolean indexing:
A[index.astype(bool)]
# array([2, 4])
A non-numpy way to achieve this, in case its useful - it uses zip to combine each pair of elements, and returns the first if the second is true:
[x[0] for x in zip(a, i) if x[1]]
I have a following data frame,
df1=
mac gw_mac ibeaconMajor ibeaconMinor
ac233f264920 ac233fc015f6 [1, 0, 1] [1, 0]
ac233f26492b ac233fc015f6 [0, 0, 0] [0, 0]
ac233f264933 ac233fc015f6 [0, 1, 1] [0, 2]
If all the values in a list(from the columns "ibeaconMajor" & "ibeaconMinor") is "0" it should return as "0" or else it should return frequently occurred non-zero values from a list as like below,
df1=
mac gw_mac ibeaconMajor ibeaconMinor
ac233f264920 ac233fc015f6 1 1
ac233f26492b ac233fc015f6 0 0
ac233f264933 ac233fc015f6 1 2
Idea is use DataFrame.applymap for elementwise apply lambda function - first remve 0 values in list comprehension, get top values by Counter and add next with iter for possible add 0 if all 0 values - here in tuple for possible select first value of tuples:
from collections import Counter
cols = ['ibeaconMajor','ibeaconMinor']
f = lambda x: next(iter(Counter([y for y in x if y != 0]).most_common(1)), (0,))[0]
#alternative
#f = lambda x: next(iter(Counter(filter(lambda y: y != 0, x)).most_common(1)), (0,))[0]
df[cols] = df[cols].applymap(f)
print (df)
mac gw_mac ibeaconMajor ibeaconMinor
0 ac233f264920 ac233fc015f6 1 1
1 ac233f26492b ac233fc015f6 0 0
2 ac233f264933 ac233fc015f6 1 2
I want to create a matrix 11x3 with 0 and 1 and save it as the variable TIME. Each entry in the matrix will execute the following if u[i] < t then will give 1 as an entry else 0.
My code is the following:
u = [1, 3, 9]
ts = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
time = np.zeros((len(ts), len(u))) # the size of the matrix
for i in range(len(ts)):
for j in range(len(u)):
for t in ts:
if u[j] < t:
time [i,j] += 1
else:
time[i,j] += 0
print(time[i,j])
However I do not get the result I want. If anyone could help I would be grateful. I want to create a function out of it because I want to use it again later on my code but it does not work .If anyone could help I would be grateful.
You can try using nested list comprehensions:
import numpy as np
u = [1, 3, 9]
ts = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
l = [[int(x < y) for x in u] for y in ts]
time = np.array(l)
print(time)
Output will look like this:
[[0 0 0]
[0 0 0]
[1 0 0]
[1 0 0]
[1 1 0]
[1 1 0]
[1 1 0]
[1 1 0]
[1 1 0]
[1 1 0]
[1 1 1]]
Input= 2 2 2 1 2 0 1 0 0 0 0 1
first number is X coordinate in normal XY axis (not list), second Y coordinate, third X and so on; so from this input it will look like:
Y
2 *
1* *
0* * *
0 1 2 X
(first*: 2,2, second*: 2,1, third*:2,0 - going from right side).
I need to get output which would look like:
output=
[[0,0,1],
[1,0,1],
[1,1,1]]
So far I tried this, but don't know how to continue:
inp=[2,2,2,1,2,0,1, 0, 0, 0, 0, 1]
maxx=0
maxy=0
for i in range(1,len(inp),2): #yaxis
if inp[i]>maxy:
maxy=inp[i]
continue
else:
continue
for j in range(0,len(inp),2): #xaxis
if inp[j]>maxx:
maxx=inp[j]
continue
else:
continue
part=[]
for i in range(maxy+1):
part.append([0 for j in range (maxx+1)])
for k in range(0,len(inp),2):
for j in range(1,len(inp),2):
for i in range(len(part)):
part[i][j]=
inp = [2,2,2,1,2,0,1, 0, 0, 0, 0, 1]
tuples = [(inp[i], inp[i+1]) for i in range(0, len(inp), 2)]
print(tuples) # [(2, 2), (2, 1), (2, 0), (1, 0), (0, 0), (0, 1)]
# Define the dimensions of the matrix
max_x_value = max([i[0] for i in tuples])+1
max_y_value = max([i[1] for i in tuples])+1
# Build the matrix - fill all cells with 0 for now
res_matrix = [[0 for _ in range(max_y_value)] for _ in range(max_x_value)]
# Iterate through the tuples and fill the 1's into the matrix
for i in tuples:
res_matrix[i[0]][i[1]]=1
print(res_matrix) # [[1, 1, 0], [1, 0, 0], [1, 1, 1]]
# Rotate the matrix by 90 to get the final answer
res = list(map(list, list(zip(*res_matrix))[::-1]))
print(res) # [[0, 0, 1], [1, 0, 1], [1, 1, 1]]
I have a matrix like below:
[0 0 1 1]
[0 0 1 1]
[0 0 0 0]
[0 0 0 0]
I need to divide it into multiple 3x3 matrices starting from top left through right. It's sort of a 3x3 slide across the matrix. In this example, we would have 4 3x3 matrices like so:
[0 0 1] [0 1 1]
1 = [0 0 1] 2 = [0 1 1]
[0 0 0] [0 0 0]
[0 0 1] [0 1 1]
3 = [0 0 0] 4 = [0 0 0]
[0 0 0] [0 0 0]
I've tried this using tf.extract_image_patch and got the 4 matrices, but I'm still not sure how I can do a sort-of running product for these matrices in Tensorflow. Or, better could achieve the running product without having to pre-calculate the separate matrices.
With running product I mean this: I need to multiple above 1-4 matrices element-wise and need to get 1 3x3 matrix. For example, 1 & 2 matrices would be multiplied, the result would be multiplied with matrix 3, and the result again would be multiplied with matrix 4.This operation should give me start of the patch([[1 1], [1 1]]) in my original matrix, a matrix like below:
[0 0 1]
res = [0 0 0]
[0 0 0]
Once done, I need to make this operation part of my network, a Tensorflow layer perhaps.
I'd appreciate if someone could help me achieve this. Thanks.
EDIT
This seems to be one way to multiply matrices in a list, but I'm still looking for 1) slice matrices into multiple parts and multiply them in a better way and 2) to add this as a layer to a network:
tf.scan(lambda a, b: tf.multiply(tf.squeeze(a), tf.squeeze(b)), original)
you could use tf.nn.conv2d, manipulating a matrix like this is called a convolution
see tensorflow.org/api_docs/python/tf/nn/conv2d
You can use numpy array slicing
import numpy as np
A = np.array([[0, 0, 1, 1],
[0, 0, 1, 1],
[0, 0, 0, 0],
[0, 0, 0, 0]])
res = A[:-1, :-1] * A[:-1, 1:] * A[1:, :-1] * A[1:, 1:]
and then, perhaps, convert the numpy array to a Tensor object by
tf.convert_to_tensor(res)