使用numpy where查找所有符合条件的点
代码
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.]]