Skip to content

Commit ad136a4

Browse files
committed
WIP-FIX pt2mesh error method
1 parent 0d94122 commit ad136a4

File tree

1 file changed

+41
-14
lines changed

1 file changed

+41
-14
lines changed

src/gh/diffCheck/diffCheck/df_error_estimation.py

Lines changed: 41 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44
"""
55

66
import numpy as np
7+
import open3d as o3d
78

9+
from diffcheck_bindings.dfb_geometry import DFPointCloud, DFMesh
10+
# from diffCheck.df_geometries import DFPointCoud, DFMesh, DFBeam, DFAssembly
811

912
def cloud_2_cloud_distance(source, target, signed=False):
1013
"""
@@ -39,45 +42,69 @@ def cloud_2_mesh_distance(source, target):
3942
return distances
4043

4144

42-
def point_2_mesh_distance(mesh, point):
45+
def point_2_mesh_distance(geo, query_point):
4346
"""
44-
Calculate the closest distance between a point and a mesh
47+
Calculate the closest distance between a point and a target geometry
4548
"""
4649
# make a kdtree of the vertices to get the relevant vertices indexes
47-
pcd = o3d.geometry.PointCloud()
48-
pcd.points = mesh.vertices
50+
pcd = DFPointCloud()
51+
pcd.points = geo.vertices
4952
kd_tree = o3d.geometry.KDTreeFlann(pcd)
5053

5154
# assume smallest distance is the distance to the closest vertex
52-
[_, idx, _] = kd_tree.search_knn_vector_3d(query_point, 1)
53-
if idx>=0:
55+
_, idx, _ = kd_tree.search_knn_vector_3d(query_point, 1)
56+
if idx:
5457
nearest_vertex_idx = idx[0]
5558
else:
56-
raise ValueError("The mesh has no vertices. Please provide a mesh.")
57-
nearest_vertex = np.asarray(mesh.vertices)[nearest_vertex_idx]
59+
raise ValueError("The mesh or brep has no vertices. Please provide a valid geometry.")
60+
nearest_vertex = np.asarray(geo.vertices)[nearest_vertex_idx]
5861
dist = np.linalg.norm(query_point - nearest_vertex)
5962

63+
# Find its neighbors with distance less than _dist_ multiplied by two.
64+
search_distance = dist * 2
65+
_, v_indices, _ = kd_tree.search_radius_vector_3d(query_point, search_distance)
66+
67+
# Find the faces that belong to these filtered vertices.
68+
geo_triangles = np.asarray(geo.triangles)
69+
candidate_mask = np.isin(geo_triangles, v_indices)
70+
candidate_faces = geo_triangles[np.any(candidate_mask, axis=1)]
71+
72+
# Step 4: Loop over candidate faces
73+
shortest_distance = float('inf')
6074

75+
for face in candidate_faces:
76+
#v0, v1, v2 = np.asarray(geo.vertices)[face]
77+
pt_face_dist = point_2_face_distance(face, query_point)
78+
if pt_face_dist < shortest_distance:
79+
shortest_distance = pt_face_dist
80+
81+
return shortest_distance
82+
83+
# Find the distance to the adjacent faces
84+
if squared_distances:
85+
least_squared_distance = min(squared_distances)
86+
else:
87+
print("No neighbors found within the specified radius.")
88+
89+
90+
6191
# create a box centered around the query point with an edge length equal to two times the distance to the nearest vertex
6292
search_distance = dist * 2
63-
if dist > search_distance:
64-
return dist
6593

6694
search_box_min = query_point - search_distance
6795
search_box_max = query_point + search_distance
6896

97+
# query a kd tree for all the faces that intersect this box
6998
def face_in_box(face):
7099
v0, v1, v2 = face
71-
vertices = np.asarray(mesh.vertices)
100+
vertices = np.asarray(geo.vertices)
72101
return (np.all(vertices[v0] >= search_box_min) and np.all(vertices[v0] <= search_box_max) or
73102
np.all(vertices[v1] >= search_box_min) and np.all(vertices[v1] <= search_box_max) or
74103
np.all(vertices[v2] >= search_box_min) and np.all(vertices[v2] <= search_box_max))
75104

76-
candidate_faces = [face for face in np.asarray(mesh.triangles) if face_in_box(face)]
105+
candidate_faces = [face for face in np.asarray(geo.triangles) if face_in_box(face)]
77106

78107

79-
# query a kd tree for all the faces that intersect this box
80-
81108
# compute the closest point for the faces that we get back
82109
pass
83110

0 commit comments

Comments
 (0)