Skip to content

Commit b61aa5f

Browse files
committed
WIP-FIX: cloud2cloud distance
1 parent d1490a6 commit b61aa5f

File tree

5 files changed

+28
-12
lines changed

5 files changed

+28
-12
lines changed

src/gh/diffCheck/diffCheck.egg-info/PKG-INFO

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Classifier: Programming Language :: Python :: 3
1010
Classifier: Programming Language :: Python :: 3.9
1111
Description-Content-Type: text/markdown
1212
Requires-Dist: numpy
13+
Requires-Dist: open3d
1314
Requires-Dist: pybind11>=2.5.0
1415

1516
# DiffCheck Grasshopper Plugin

src/gh/diffCheck/diffCheck.egg-info/SOURCES.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ README.md
22
setup.py
33
diffCheck/__init__.py
44
diffCheck/df_cvt_bindings.py
5+
diffCheck/df_error_estimation.py
56
diffCheck/df_geometries.py
67
diffCheck/df_joint_detector.py
78
diffCheck/df_transformations.py
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
numpy
2+
open3d
23
pybind11>=2.5.0

src/gh/diffCheck/diffCheck/df_error_estimation.py

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,38 @@
66
import numpy as np
77
import 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

1211
def 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

src/gh/diffCheck/setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
packages=find_packages(),
1313
install_requires=[
1414
"numpy",
15+
"open3d",
1516
"pybind11>=2.5.0"
1617
# other dependencies...
1718
],

0 commit comments

Comments
 (0)