<p><em>La Lune De Idees</em>提供的答案是一个非常好的起点。多亏了这个答案,我才能够通过使用numpy和scipy将代码速度提高30倍</p>
<p>给定</p>
<p><code>vertice_points</code>作为M x 3 numpy数组的网格</p>
<p>及</p>
<p><code>point_cloud</code>一个点云作为nx 3 numpy数组,用于计算到的距离</p>
<p>我提出了以下完整的(我工作时)示例:</p>
<pre><code># make efficient search tree
tree = cKDTree(vertice_points)
# get indices of closest three points to use as vetice to calculate distance to
d, idx_of_point_in_mesh = tree.query(point_cloud, 3)
# anchor point to span a plane from
anchor_points = vertice_points[idx_of_point_in_mesh[:,0],:]
# use next two nearest points to span a plane with two vectors
# from anchor point
plane_points = vertice_points[idx_of_point_in_mesh[:,1:],:]
plane_vecs = np.array(plane_points)
plane_vecs[:,0,:] -= anchor_points
plane_vecs[:,1,:] -= anchor_points
# calculate normal vectors of the planes
normals = np.cross(plane_vecs[:,0,:], plane_vecs[:,1,:], axis=1)
# distance from each point to its anchor point for spanning a plane
PQ = anchor_points - point_cloud
# distance is dot product between normal and PQ vector
# since normals and PQ are arrays of vectors
# use einsum to calc dot product along first dimension
dists = np.einsum('ij,ij->i', PQ, normals)
</code></pre>
<p><strong>注意</strong>:上述代码假设网格中三个最近点跨越的垂直度也是到该点的最近垂直度,这是一个合理的假设,前提是网格相对平坦,且点靠近网格中心,且与网格延伸距离不远。这就是我的用例的情况</p>