Skip to content

Commit a61efff

Browse files
authored
Merge pull request #31 from diffCheckOrg/ktree_impl
Adding API functions for compute distance (mesh-cloud cloud-mesh)
2 parents 2864e54 + 2a86f97 commit a61efff

File tree

13 files changed

+1766
-225
lines changed

13 files changed

+1766
-225
lines changed

src/diffCheck.hh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#pragma once
22

33
#include <open3d/Open3D.h>
4+
#include <open3d/t/geometry/RaycastingScene.h>
5+
46
#include <loguru.hpp>
57

68
// diffCheck includes

src/diffCheck/geometry/DFMesh.cc

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#include "diffCheck/geometry/DFMesh.hh"
22
#include "diffCheck/IOManager.hh"
33

4+
#include <open3d/t/geometry/RaycastingScene.h>
5+
46

57
namespace diffCheck::geometry
68
{
@@ -89,4 +91,39 @@ namespace diffCheck::geometry
8991
this->NormalsFace = tempMesh_ptr->NormalsFace;
9092
this->ColorsFace = tempMesh_ptr->ColorsFace;
9193
}
94+
95+
std::vector<float> DFMesh::ComputeDistance(const diffCheck::geometry::DFPointCloud &targetCloud, bool useAbs)
96+
{
97+
auto rayCastingScene = open3d::t::geometry::RaycastingScene();
98+
99+
std::vector<Eigen::Vector3d> vertices = this->Vertices;
100+
std::vector<float> verticesPosition;
101+
for (const auto& vertex : vertices) {
102+
verticesPosition.insert(verticesPosition.end(), vertex.data(), vertex.data() + 3);
103+
}
104+
open3d::core::Tensor verticesPositionTensor(verticesPosition.data(), {static_cast<int64_t>(vertices.size()), 3}, open3d::core::Dtype::Float32);
105+
std::vector<uint32_t> triangles;
106+
for (int i = 0; i < this->Faces.size(); i++) {
107+
triangles.push_back(static_cast<uint32_t>(this->Faces[i].x()));
108+
triangles.push_back(static_cast<uint32_t>(this->Faces[i].y()));
109+
triangles.push_back(static_cast<uint32_t>(this->Faces[i].z()));
110+
}
111+
open3d::core::Tensor trianglesTensor(triangles.data(), {static_cast<int64_t>(this->Faces.size()), 3}, open3d::core::Dtype::UInt32);
112+
rayCastingScene.AddTriangles(verticesPositionTensor, trianglesTensor);
113+
114+
auto pointCloudO3dCopy = targetCloud;
115+
std::shared_ptr<open3d::geometry::PointCloud> pointCloudO3d_ptr = pointCloudO3dCopy.Cvt2O3DPointCloud();
116+
std::vector<float> cloudPoints;
117+
for (const auto& point : pointCloudO3d_ptr->points_) {
118+
cloudPoints.insert(cloudPoints.end(), point.data(), point.data() + 3);
119+
}
120+
open3d::core::Tensor cloudPointsTensor(cloudPoints.data(), {static_cast<int64_t>(pointCloudO3d_ptr->points_.size()), 3}, open3d::core::Dtype::Float32);
121+
122+
open3d::core::Tensor sdf = rayCastingScene.ComputeSignedDistance(cloudPointsTensor);
123+
if (useAbs)
124+
sdf = sdf.Abs();
125+
std::vector<float> sdfVector(sdf.GetDataPtr<float>(), sdf.GetDataPtr<float>() + sdf.NumElements());
126+
127+
return sdfVector;
128+
}
92129
} // namespace diffCheck::geometry

src/diffCheck/geometry/DFMesh.hh

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
#include <Eigen/Core>
44
#include <open3d/Open3D.h>
55

6-
#include <diffCheck/geometry/DFPointCloud.hh>
7-
#include <diffCheck/transformation/DFTransformation.hh>
6+
#include "diffCheck/geometry/DFPointCloud.hh"
7+
#include "diffCheck/transformation/DFTransformation.hh"
88

99
namespace diffCheck::geometry
1010
{
@@ -85,6 +85,18 @@ namespace diffCheck::geometry
8585
/// @brief Number of faces in the mesh
8686
int GetNumFaces() const { return this->Faces.size(); }
8787

88+
public: ///< Distance calculations
89+
/**
90+
* @brief Compute the distance between the df mesh and a df point cloud. It
91+
* can be considered as a point to plane distance.
92+
*
93+
* @param target the target cloud in format df
94+
* @param useAbs if true, the absolute value of the distance is returned
95+
* @return std::vector<float> the distance between the point cloud and the mesh
96+
*/
97+
std::vector<float> ComputeDistance(const diffCheck::geometry::DFPointCloud &targetMesh, bool useAbs = true);
98+
99+
88100
public: ///< Basic mesh data
89101
/// @brief Eigen vector of 3D vertices
90102
std::vector<Eigen::Vector3d> Vertices;

src/diffCheck/geometry/DFPointCloud.cc

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
#include "diffCheck/IOManager.hh"
44

5-
65
namespace diffCheck::geometry
76
{
87
void DFPointCloud::Cvt2DFPointCloud(const std::shared_ptr<open3d::geometry::PointCloud> &O3DPointCloud)
@@ -33,18 +32,6 @@ namespace diffCheck::geometry
3332
return O3DPointCloud;
3433
}
3534

36-
std::vector<double> DFPointCloud::ComputeP2PDistance(std::shared_ptr<geometry::DFPointCloud> target)
37-
{
38-
std::vector<double> errors;
39-
auto O3DSourcePointCloud = this->Cvt2O3DPointCloud();
40-
auto O3DTargetPointCloud = target->Cvt2O3DPointCloud();
41-
42-
std::vector<double> distances;
43-
44-
distances = O3DSourcePointCloud->ComputePointCloudDistance(*O3DTargetPointCloud);
45-
return distances;
46-
}
47-
4835
std::vector<Eigen::Vector3d> DFPointCloud::ComputeBoundingBox()
4936
{
5037
auto O3DPointCloud = this->Cvt2O3DPointCloud();
@@ -132,4 +119,17 @@ namespace diffCheck::geometry
132119
this->Colors = cloud->Colors;
133120
this->Normals = cloud->Normals;
134121
}
122+
123+
std::vector<double> DFPointCloud::ComputeDistance(const diffCheck::geometry::DFPointCloud &targetCloud, bool useAbs)
124+
{
125+
auto O3DSourcePointCloud = this->Cvt2O3DPointCloud();
126+
auto targetCloudCopy = targetCloud;
127+
auto O3DTargetPointCloud = targetCloudCopy.Cvt2O3DPointCloud();
128+
std::vector<double> distances;
129+
distances = O3DSourcePointCloud->ComputePointCloudDistance(*O3DTargetPointCloud);
130+
if (useAbs)
131+
for (auto &dist : distances)
132+
dist = std::abs(dist);
133+
return distances;
134+
}
135135
}

src/diffCheck/geometry/DFPointCloud.hh

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#include <Eigen/Core>
44
#include <open3d/Open3D.h>
55

6-
#include <diffCheck/transformation/DFTransformation.hh>
6+
#include "diffCheck/transformation/DFTransformation.hh"
77

88

99
namespace diffCheck::geometry
@@ -37,19 +37,6 @@ namespace diffCheck::geometry
3737
std::shared_ptr<open3d::geometry::PointCloud> Cvt2O3DPointCloud();
3838

3939
public: ///< Utilities
40-
/**
41-
* @brief Compute the "point to point" distance between this and another point clouds.
42-
*
43-
* For every point in the source point cloud, it looks in the KDTree of the target point cloud and finds the closest point.
44-
* It returns a vector of distances, one for each point in the source point cloud.
45-
*
46-
* @param target The target diffCheck point cloud
47-
* @return std::vector<double> A vector of distances, one for each point in the source point cloud.
48-
*
49-
* @see https://github.com/isl-org/Open3D/blob/main/cpp/open3d/geometry/PointCloud.cpp
50-
*/
51-
std::vector<double> ComputeP2PDistance(std::shared_ptr<geometry::DFPointCloud> target);
52-
5340
/**
5441
* @brief Compute the bounding box of the point cloud and stores it as member of the DFPointCloud object
5542
*
@@ -122,6 +109,20 @@ namespace diffCheck::geometry
122109
*/
123110
void LoadFromPLY(const std::string &path);
124111

112+
public: ///< Distance calculations
113+
/**
114+
* @brief Compute the distance between two point clouds.
115+
* For every point in the source point cloud, it looks in the KDTree of the target point cloud and finds the closest point.
116+
* It returns a vector of distances, one for each point in the source point cloud.
117+
*
118+
* @param target the target point cloud in format df
119+
* @param useAbs if true, the absolute value of the distance is returned
120+
* @return std::vector<double> the distance between the two point clouds
121+
*
122+
* @see https://github.com/isl-org/Open3D/blob/main/cpp/open3d/geometry/PointCloud.cpp
123+
*/
124+
std::vector<double> ComputeDistance(const DFPointCloud &targetCloud, bool useAbs = true);
125+
125126
public: ///< Getters
126127
/// @brief Number of points in the point cloud
127128
int GetNumPoints() const { return this->Points.size(); }

src/diffCheck/registrations/DFGlobalRegistrations.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ namespace diffCheck::registrations
1717

1818
std::shared_ptr<geometry::DFPointCloud> dfPointCloudPtrAfterTrans = std::make_shared<geometry::DFPointCloud>();
1919
dfPointCloudPtrAfterTrans->Cvt2DFPointCloud(o3DPointCloudAfterTrans);
20-
std::vector<double> registrationErrors = dfPointCloudPtrAfterTrans->ComputeP2PDistance(target);
20+
std::vector<double> registrationErrors = dfPointCloudPtrAfterTrans->ComputeDistance(*target);
2121
errors.push_back(std::accumulate(registrationErrors.begin(), registrationErrors.end(), 0.0) / registrationErrors.size());
2222
}
2323
return errors;

src/diffCheckApp.cc

Lines changed: 72 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,48 +3,101 @@
33
#include <iostream>
44
#include <fstream>
55

6+
#include <open3d/Open3D.h>
7+
// #include <open3d/t/geometry/RaycastingScene.h>
68

79
int main()
810
{
911
// import clouds
10-
std::shared_ptr<diffCheck::geometry::DFPointCloud> dfPointCloudPtr
12+
std::shared_ptr<diffCheck::geometry::DFPointCloud> dfPointCloudPtr
1113
= std::make_shared<diffCheck::geometry::DFPointCloud>();
1214
std::shared_ptr<diffCheck::geometry::DFMesh> dfMeshPtr
1315
= std::make_shared<diffCheck::geometry::DFMesh>();
1416

1517
// create a sphere from o3d
16-
std::string pathCloud = R"(C:\andre\Downloads\moved_04.ply)";
18+
std::string pathCloud = R"(C:\Users\andre\Downloads\moved_04.ply)";
1719
std::string pathMesh = R"(C:\Users\andre\Downloads\meshtest.ply)";
1820

19-
// dfPointCloudPtr->LoadFromPLY(pathCloud);
21+
dfPointCloudPtr->LoadFromPLY(pathCloud);
2022
dfMeshPtr->LoadFromPLY(pathMesh);
2123

22-
open3d::geometry::TriangleMesh meshO3d = *dfMeshPtr->Cvt2O3DTriangleMesh();
24+
// open3d::geometry::TriangleMesh meshO3d = *dfMeshPtr->Cvt2O3DTriangleMesh();
25+
// open3d::geometry::PointCloud pointCloudO3d = *dfPointCloudPtr->Cvt2O3DPointCloud();
2326

27+
// auto rayCastingScene = open3d::t::geometry::RaycastingScene();
28+
// // Get the vertices of the mesh
29+
// std::vector<Eigen::Vector3d> vertices = meshO3d.vertices_;
2430

25-
// convert the sphere to a diffCheck point cloud
26-
// auto o3dPointCloud = meshO3d.SamplePointsUniformly(1000);
31+
// // Convert the vertices to a tensor
32+
// std::vector<float> verticesPosition;
33+
// for (const auto& vertex : vertices) {
34+
// verticesPosition.insert(verticesPosition.end(), vertex.data(), vertex.data() + 3);
35+
// }
36+
// open3d::core::Tensor verticesPositionTensor(verticesPosition.data(), {static_cast<int64_t>(vertices.size()), 3}, open3d::core::Dtype::Float32);
2737

28-
std::shared_ptr<open3d::geometry::PointCloud> tightBBOX = std::make_shared<open3d::geometry::PointCloud>();
38+
// std::vector<uint32_t> triangles;
39+
// for (int i = 0; i < meshO3d.triangles_.size(); i++) {
40+
// triangles.push_back(static_cast<uint32_t>(meshO3d.triangles_[i].x()));
41+
// triangles.push_back(static_cast<uint32_t>(meshO3d.triangles_[i].y()));
42+
// triangles.push_back(static_cast<uint32_t>(meshO3d.triangles_[i].z()));
43+
// }
44+
// open3d::core::Tensor trianglesTensor(triangles.data(), {static_cast<int64_t>(meshO3d.triangles_.size()), 3}, open3d::core::Dtype::UInt32);
45+
46+
// rayCastingScene.AddTriangles(verticesPositionTensor, trianglesTensor);
2947

30-
// compute the bounding box
31-
open3d::geometry::OrientedBoundingBox bbox = meshO3d.GetMinimalOrientedBoundingBox();
32-
std::vector<Eigen::Vector3d> bboxPts = bbox.GetBoxPoints();
33-
for (auto &pt : bboxPts)
34-
{
35-
tightBBOX->points_.push_back(pt);
36-
}
48+
// // compute the cloud to mesh signed distance
49+
// std::vector<float> cloudPoints;
50+
// for (const auto& point : pointCloudO3d.points_) {
51+
// cloudPoints.insert(cloudPoints.end(), point.data(), point.data() + 3);
52+
// }
53+
// open3d::core::Tensor cloudPointsTensor(cloudPoints.data(), {static_cast<int64_t>(pointCloudO3d.points_.size()), 3}, open3d::core::Dtype::Float32);
3754

3855

39-
dfPointCloudPtr->Cvt2DFPointCloud(tightBBOX);
56+
// open3d::core::Tensor sdf = rayCastingScene.ComputeSignedDistance(cloudPointsTensor);
4057

58+
// // FIXME: replace with bool parameter
59+
// // if true, get the absolute value of the sdf
60+
// if (true) {
61+
// sdf = sdf.Abs();
62+
// }
4163

64+
// convert sdf to a vector
65+
std::vector<float> sdfVector = dfMeshPtr->ComputeDistance(*dfPointCloudPtr);
4266

4367

44-
std::shared_ptr<diffCheck::visualizer::Visualizer> vis = std::make_shared<diffCheck::visualizer::Visualizer>();
45-
vis->AddPointCloud(dfPointCloudPtr);
46-
// vis->AddMesh(dfMeshPtr);
47-
vis->Run();
68+
// >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
69+
// compute point cloud to point cloud distance
70+
std::vector<double> distances = dfPointCloudPtr->ComputeDistance(*dfPointCloudPtr);
71+
// for (auto &dist : distances)
72+
// {
73+
// std::cout << dist << std::endl;
74+
// }
75+
76+
77+
78+
// // convert the sphere to a diffCheck point cloud
79+
// // auto o3dPointCloud = meshO3d.SamplePointsUniformly(1000);
80+
81+
// std::shared_ptr<open3d::geometry::PointCloud> tightBBOX = std::make_shared<open3d::geometry::PointCloud>();
82+
83+
// // compute the bounding box
84+
// open3d::geometry::OrientedBoundingBox bbox = meshO3d.GetMinimalOrientedBoundingBox();
85+
// std::vector<Eigen::Vector3d> bboxPts = bbox.GetBoxPoints();
86+
// for (auto &pt : bboxPts)
87+
// {
88+
// tightBBOX->points_.push_back(pt);
89+
// }
90+
91+
92+
// dfPointCloudPtr->Cvt2DFPointCloud(tightBBOX);
93+
94+
95+
96+
97+
// std::shared_ptr<diffCheck::visualizer::Visualizer> vis = std::make_shared<diffCheck::visualizer::Visualizer>();
98+
// vis->AddPointCloud(dfPointCloudPtr);
99+
// // vis->AddMesh(dfMeshPtr);
100+
// vis->Run();
48101

49102

50103
return 0;

src/diffCheckBindings.cc

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ PYBIND11_MODULE(diffcheck_bindings, m) {
2929
.def(py::init<>())
3030
.def(py::init<std::vector<Eigen::Vector3d>, std::vector<Eigen::Vector3d>, std::vector<Eigen::Vector3d>>())
3131

32-
.def("compute_P2PDistance", &diffCheck::geometry::DFPointCloud::ComputeP2PDistance)
32+
.def("compute_distance", &diffCheck::geometry::DFPointCloud::ComputeDistance,
33+
py::arg("target_cloud"),
34+
py::arg("is_abs") = true)
3335
.def("compute_BoundingBox", &diffCheck::geometry::DFPointCloud::ComputeBoundingBox)
3436

3537
.def("voxel_downsample", &diffCheck::geometry::DFPointCloud::VoxelDownsample,
@@ -65,6 +67,10 @@ PYBIND11_MODULE(diffcheck_bindings, m) {
6567
.def(py::init<>())
6668
.def(py::init<std::vector<Eigen::Vector3d>, std::vector<Eigen::Vector3i>, std::vector<Eigen::Vector3d>, std::vector<Eigen::Vector3d>, std::vector<Eigen::Vector3d>>())
6769

70+
.def("compute_distance", &diffCheck::geometry::DFMesh::ComputeDistance,
71+
py::arg("target_cloud"),
72+
py::arg("is_abs") = true)
73+
6874
.def("load_from_PLY", &diffCheck::geometry::DFMesh::LoadFromPLY)
6975

7076
.def("sample_points_uniformly", &diffCheck::geometry::DFMesh::SampleCloudUniform)

src/gh/components/DF_tester/code.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,8 @@ def RunScript(self):
2525
else:
2626
ghenv.Component.AddRuntimeMessage(RML.Remark, "Bindings imported.")
2727

28-
return is_binding_imported
28+
return is_binding_imported
29+
30+
# if __name__ == "__main__":
31+
# tester = DFTester()
32+
# tester.RunScript()

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ diffCheck/df_geometries.py
77
diffCheck/df_joint_detector.py
88
diffCheck/df_transformations.py
99
diffCheck/df_util.py
10-
diffCheck/diffcheck_bindings.cp39-win_amd64.pyd
1110
diffCheck.egg-info/PKG-INFO
1211
diffCheck.egg-info/SOURCES.txt
1312
diffCheck.egg-info/dependency_links.txt

0 commit comments

Comments
 (0)