|
4 | 4 | """ |
5 | 5 |
|
6 | 6 | import numpy as np |
| 7 | +import open3d as o3d |
7 | 8 |
|
| 9 | +from diffcheck_bindings.dfb_geometry import DFPointCloud, DFMesh |
| 10 | +# from diffCheck.df_geometries import DFPointCoud, DFMesh, DFBeam, DFAssembly |
8 | 11 |
|
9 | 12 | def cloud_2_cloud_distance(source, target, signed=False): |
10 | 13 | """ |
@@ -39,45 +42,69 @@ def cloud_2_mesh_distance(source, target): |
39 | 42 | return distances |
40 | 43 |
|
41 | 44 |
|
42 | | -def point_2_mesh_distance(mesh, point): |
| 45 | +def point_2_mesh_distance(geo, query_point): |
43 | 46 | """ |
44 | | - Calculate the closest distance between a point and a mesh |
| 47 | + Calculate the closest distance between a point and a target geometry |
45 | 48 | """ |
46 | 49 | # 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 |
49 | 52 | kd_tree = o3d.geometry.KDTreeFlann(pcd) |
50 | 53 |
|
51 | 54 | # 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: |
54 | 57 | nearest_vertex_idx = idx[0] |
55 | 58 | 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] |
58 | 61 | dist = np.linalg.norm(query_point - nearest_vertex) |
59 | 62 |
|
| 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') |
60 | 74 |
|
| 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 | + |
61 | 91 | # create a box centered around the query point with an edge length equal to two times the distance to the nearest vertex |
62 | 92 | search_distance = dist * 2 |
63 | | - if dist > search_distance: |
64 | | - return dist |
65 | 93 |
|
66 | 94 | search_box_min = query_point - search_distance |
67 | 95 | search_box_max = query_point + search_distance |
68 | 96 |
|
| 97 | + # query a kd tree for all the faces that intersect this box |
69 | 98 | def face_in_box(face): |
70 | 99 | v0, v1, v2 = face |
71 | | - vertices = np.asarray(mesh.vertices) |
| 100 | + vertices = np.asarray(geo.vertices) |
72 | 101 | return (np.all(vertices[v0] >= search_box_min) and np.all(vertices[v0] <= search_box_max) or |
73 | 102 | np.all(vertices[v1] >= search_box_min) and np.all(vertices[v1] <= search_box_max) or |
74 | 103 | np.all(vertices[v2] >= search_box_min) and np.all(vertices[v2] <= search_box_max)) |
75 | 104 |
|
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)] |
77 | 106 |
|
78 | 107 |
|
79 | | - # query a kd tree for all the faces that intersect this box |
80 | | - |
81 | 108 | # compute the closest point for the faces that we get back |
82 | 109 | pass |
83 | 110 |
|
|
0 commit comments