Jonas
Jonas

Reputation: 7

Search multiple 1D numpy-arrays in 3D numpy-array

I got a stl-file from a surface scan. From that I got a 3D-array for every triangles with its 3 points and theire x,y,z coordinates.

Now to find all triangles that have 2 points in common with another 2D array with x,y,z coordinates of points (could also be a list, the format would not be that important).

The example data for my 3D-array would be

Triangle= np.array([[[0, 1, 1],
        [1, 0, 1],
        [1, 1, 2]],

       [[0, 0, 1],
        [1, 0, 1],
        [0, 1, 1]],

       [[3, 0, 1],
        [3, 1, 1],
        [2, 0, 2]],

       [[2, 0, 2],
        [3, 1, 1],
        [2, 1, 2]],

       [[1, 1, 2],
        [1, 0, 1],
        [2, 0, 2]],

       [[2, 0, 2],
        [2, 1, 2],
        [1, 1, 2]],

       [[1, 2, 3],
        [0, 1, 1],
        [1, 1, 2]],

       [[1, 2, 3],
        [0, 2, 1],
        [0, 1, 1]],

       [[0, 3, 1],
        [0, 2, 1],
        [1, 3, 2]],

       [[0, 2, 1],
        [1, 2, 3],
        [1, 3, 2]],

       [[1, 1, 2],
        [2, 1, 2],
        [2, 2, 1]],

       [[2, 2, 1],
        [1, 2, 3],
        [1, 1, 2]],

       [[2, 1, 2],
        [3, 1, 1],
        [2, 2, 1]],

       [[3, 1, 1],
        [3, 2, 1],
        [2, 2, 1]],

       [[2, 3, 1],
        [3, 2, 1],
        [3, 3, 1]],

       [[2, 3, 1],
        [2, 2, 1],
        [3, 2, 1]],

       [[2, 3, 1],
        [1, 3, 2],
        [1, 2, 3]],

       [[1, 2, 3],
        [2, 2, 1],
        [2, 3, 1]]])

and for the points

points = np.array([[1, 2, 3],
       [0, 1, 1],
       [0, 2, 1],
       [1, 1, 2],
       [1, 3, 2],
       [2, 2, 1],
       [2, 3, 1]])

Upvotes: 0

Views: 187

Answers (1)

Quang Hoang
Quang Hoang

Reputation: 150735

Let's try broadcasting to compare all the triangle to all the points:

compare = (Triangle[:,:,None,:] == points[None,None,...])

# `all` check if all the coordinates to be equal,
# `sum` counts the equal points 
mask = compare.all(axis=-1).sum(axis=(-2,-1)) >=2

Triangle[mask]

Output:

array([[[0, 1, 1],
        [1, 0, 1],
        [1, 1, 2]],

       [[1, 2, 3],
        [0, 1, 1],
        [1, 1, 2]],

       [[1, 2, 3],
        [0, 2, 1],
        [0, 1, 1]],

       [[0, 3, 1],
        [0, 2, 1],
        [1, 3, 2]],

       [[0, 2, 1],
        [1, 2, 3],
        [1, 3, 2]],

       [[1, 1, 2],
        [2, 1, 2],
        [2, 2, 1]],

       [[2, 2, 1],
        [1, 2, 3],
        [1, 1, 2]],

       [[2, 3, 1],
        [2, 2, 1],
        [3, 2, 1]],

       [[2, 3, 1],
        [1, 3, 2],
        [1, 2, 3]],

       [[1, 2, 3],
        [2, 2, 1],
        [2, 3, 1]]])

Upvotes: 1

Related Questions