66import numpy as np
77import open3d as o3d
88
9- from diffcheck_bindings .dfb_geometry import DFPointCloud , DFMesh
10- # from diffCheck.df_geometries import DFPointCoud, DFMesh, DFBeam, DFAssembly
9+ from diffCheck import diffcheck_bindings
1110
1211def cloud_2_cloud_distance (source , target , signed = False ):
1312 """
1413 Compute the Euclidean distance for every point of a source pcd to its closest point on a target pointcloud
1514 """
16- distances = np .full ( len ( source .points ), np . inf )
15+ distances = np .asarray ( source .compute_P2PDistance ( target ) )
1716
18- for i in range (len (source .points )):
17+ if signed :
18+
19+ # Build a KD-tree for the target points
20+ kdtree = o3d .geometry .KDTreeFlann (np .asarray (target .points ))
21+ print ("KD-tree built successfully." )
1922
20- dists = np .linalg .norm (np .asarray (target .points ) - np .asarray (source .points )[i ], axis = 1 )
21- distances [i ] = np .min (dists )
23+ for i in range (len (source .points )):
2224
23- # determine whether the point on the source cloud is in the same direction as the normal of the corresponding point on the target pcd
24- if signed :
25- closest_idx = np .argmin (dists )
26- # direction from target to source
25+ query = np .asarray (source .points [i ], dtype = np .float64 ).reshape (3 )
26+ # Query the KD-tree to find the nearest neighbor
27+ try :
28+ _ , idx , _ = kdtree .search_knn_vector_3d (query , 1 )
29+ except Exception as e :
30+ print (f"Error querying KD-tree for point { i } : { e } " )
31+ continue
32+
33+ closest_idx = idx [0 ]
34+ # Calculate the direction from target to source
2735 direction = source .points [i ] - target .points [closest_idx ]
28- distances [i ] *= np .sign (np .dot (direction , target .normals [closest_idx ]))
36+
37+ # Calculate the signed distance
38+ dot_product = np .dot (direction , target .normals [closest_idx ])
39+ if dot_product < 0 :
40+ distances [i ] = - distances [i ]
2941
3042 return distances
3143
@@ -50,7 +62,7 @@ def point_2_mesh_distance(geo, query_point):
5062 Calculate the closest distance between a point and a target geometry
5163 """
5264 # make a kdtree of the vertices to get the relevant vertices indexes
53- pcd = DFPointCloud ()
65+ pcd = diffcheck_bindings . dfb_geometry . DFPointCloud ()
5466 pcd .points = geo .vertices
5567 kd_tree = o3d .geometry .KDTreeFlann (pcd )
5668
0 commit comments