Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/281.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用python查找属于平面的所有点_Python_Numpy_Numpy Slicing_Open3d - Fatal编程技术网

使用python查找属于平面的所有点

使用python查找属于平面的所有点,python,numpy,numpy-slicing,open3d,Python,Numpy,Numpy Slicing,Open3d,我有一个mx3阵列,用于创建3d模型。有没有一种快速的方法可以使用numpy或其他python函数提取属于给定平面的所有点?该平面将采用Ax+By+Cz+D=0形式。我正在循环数组中的所有点,以找到满足这个方程的点 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]))==

我有一个mx3阵列,用于创建3d模型。有没有一种快速的方法可以使用numpy或其他python函数提取属于给定平面的所有点?该平面将采用Ax+By+Cz+D=0形式。我正在循环数组中的所有点,以找到满足这个方程的点

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)
我想知道是否有任何numpythonic方法可以让它更快?

矢量化会快得多。在下面的示例中,下面的所有点都位于-100
以下内容有用吗?我假设它很快,因为它没有使用任何for循环。我的答案是基于

用于查找与条件匹配的所有点

代码

import numpy as np

def get_planer_indexes(pts, plane):
    '''
        :parm pts    - array of 3D points
        :param plane - coefficient of plane (i.e. A, B, C, D)
        :returns     - indexes of points which are in plance
    '''
    # Compute A*pt[0] + B*pt[1] + C*pt[3] + D for each point pt in pts
    # Checks that abs(...) is below threshold (1e-6) to allow for floating point error
    return np.where(np.abs(points.dot(plane[:3]) + plane[3]) <= 1e-6 )
#    Create 3 points which lie in a plane
P1 = [1, -2, 0]
P2 = [3, 1, 4]
P3 = [0, -1, 2]
planar_pts = np.array([P1, P2, P3])

# Plane that P1, P2, P3 lie within
plane = np.array([2, -8, 5, -18]) # i.e. A = 2, B = -8, C = 5, D = -18

# Random 3 D points (100 points)
rand_points = np.random.rand(100, 3)

#    Stack onto planar points
points = np.vstack((planar_pts, rand_points))

#    Shuffle the points (so planar points are in random locations)
np.random.shuffle(points)

#    Find planar points
indexes = get_planer_indexes(points, plane)
print(points[indexes])
输出

[[ 3.  1.  4.]
 [ 0. -1.  2.]
 [ 1. -2.  0.]]

你知道A,B,C,D吗?我假设你在这里做的是计算从一个点到平面的距离?将向量放入numpy数组,将所有点的距离计算为另一个numpy数组,然后过滤距离为0的所有点。如果c0是一个平面将相交的点吗?c0是系数,
[a,B,C]
p[0]
(和其他点可能)与平面相交
#    Create 3 points which lie in a plane
P1 = [1, -2, 0]
P2 = [3, 1, 4]
P3 = [0, -1, 2]
planar_pts = np.array([P1, P2, P3])

# Plane that P1, P2, P3 lie within
plane = np.array([2, -8, 5, -18]) # i.e. A = 2, B = -8, C = 5, D = -18

# Random 3 D points (100 points)
rand_points = np.random.rand(100, 3)

#    Stack onto planar points
points = np.vstack((planar_pts, rand_points))

#    Shuffle the points (so planar points are in random locations)
np.random.shuffle(points)

#    Find planar points
indexes = get_planer_indexes(points, plane)
print(points[indexes])
[[ 3.  1.  4.]
 [ 0. -1.  2.]
 [ 1. -2.  0.]]