Skip to content

Commit bb5f9c8

Browse files
committed
WIP-FIX point_2_mesh_distance
1 parent 9f8069e commit bb5f9c8

File tree

1 file changed

+30
-2
lines changed

1 file changed

+30
-2
lines changed

src/gh/diffCheck/diffCheck/df_error_estimation.py

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,43 @@ def point_2_mesh_distance(mesh, point):
4545
"""
4646
pass
4747
# make a kdtree of the vertices to get the relevant vertices indexes
48+
pcd = o3d.geometry.PointCloud()
49+
pcd.points = mesh.vertices
50+
kd_tree = o3d.geometry.KDTreeFlann(pcd)
4851

4952
# assume smallest distance is the distance to the closest vertex
53+
[_, idx, _] = kd_tree.search_knn_vector_3d(query_point, 1)
54+
if idx>=0:
55+
nearest_vertex_idx = idx[0]
56+
else:
57+
raise ValueError("The mesh has no vertices. Please provide a mesh.")
58+
nearest_vertex = np.asarray(mesh.vertices)[nearest_vertex_idx]
59+
dist = np.linalg.norm(query_point - nearest_vertex)
5060

61+
5162
# create a box centered around the query point with an edge length equal to two times the distance to the nearest vertex
63+
search_distance = dist * 2
64+
if dist > search_distance:
65+
return dist
66+
67+
search_box_min = query_point - search_distance
68+
search_box_max = query_point + search_distance
69+
70+
def face_in_box(face):
71+
v0, v1, v2 = face
72+
vertices = np.asarray(mesh.vertices)
73+
return (np.all(vertices[v0] >= search_box_min) and np.all(vertices[v0] <= search_box_max) or
74+
np.all(vertices[v1] >= search_box_min) and np.all(vertices[v1] <= search_box_max) or
75+
np.all(vertices[v2] >= search_box_min) and np.all(vertices[v2] <= search_box_max))
76+
77+
candidate_faces = [face for face in np.asarray(mesh.triangles) if face_in_box(face)]
78+
79+
5280
# query a kd tree for all the faces that intersect this box
5381

5482
# compute the closest point for the faces that we get back
55-
56-
83+
84+
5785
def point_2_face_distance(face, point):
5886
"""
5987
Calculate the closest distance between a point and a face

0 commit comments

Comments
 (0)