Search code examples
pythonnumpynumpy-slicingopen3d

finding all the points that belong to a plane using python


I have an mx3 array that is used to create a 3d model. Is there is a fast way to extract all the points that belong to a given plane using numpy or other python functions? The plane will take the Ax+By+Cz+D=0 form. I'm currently looping through all the points in the array to find the points that satisfy this equation.

plane1=[]
for i in pcd_array:
    if (normal_vector[0]*(i[0]-point1[0])+normal_vector[1]*(i[1]-point1[1])+normal_vector[2]*(i[2]-point1[2]))==0:
        plane1.append(i)

I'm wondering is there any numpythonic way to do it to make it faster?


Solution

  • Vectorization will be much faster. In the example below, all points below lie on integer values in the region -100 < x,y,z < 100. The matrix p contains one million points; we calculate all points that lie on a given plane (almost instantaneously):

    # define 1M points at random:
    p = np.random.randint(-100,100, size=(1000000,3))
    
    """
    Points that lie on a plane satisfy Ax + By + Cz + D = 0. 
    Here we set values of A, B, C to arbitrary values and 
    calculate D so the plane intersects the first point in `p`:
    """
    ABC = np.array([3,5,7])
    D = -p[0].dot(ABC) 
    
    # return all points in plane Ax + By + Cz + D = 0
    p_in_plane = p[p.dot(ABC) + D == 0]