Skip to content

Commit b281a67

Browse files
authored
Merge pull request #45 from diffCheckOrg/distance_calculation_visualisation
Distance calculation visualisation
2 parents bcd6802 + 0ef6c3e commit b281a67

File tree

34 files changed

+28302
-95
lines changed

34 files changed

+28302
-95
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,4 +246,4 @@ cython_debug/
246246
#.idea/
247247

248248
# egg-info
249-
*.egg-info/
249+
*.egg-info/

deps/eigen

Submodule eigen updated from d791d48 to 3f06651

deps/pybind11

Submodule pybind11 updated 54 files

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
#include <cilantro/cilantro.hpp>

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
{
@@ -145,4 +147,39 @@ namespace diffCheck::geometry
145147
this->NormalsFace = tempMesh_ptr->NormalsFace;
146148
this->ColorsFace = tempMesh_ptr->ColorsFace;
147149
}
150+
151+
std::vector<float> DFMesh::ComputeDistance(const diffCheck::geometry::DFPointCloud &targetCloud, bool useAbs)
152+
{
153+
auto rayCastingScene = open3d::t::geometry::RaycastingScene();
154+
155+
std::vector<Eigen::Vector3d> vertices = this->Vertices;
156+
std::vector<float> verticesPosition;
157+
for (const auto& vertex : vertices) {
158+
verticesPosition.insert(verticesPosition.end(), vertex.data(), vertex.data() + 3);
159+
}
160+
open3d::core::Tensor verticesPositionTensor(verticesPosition.data(), {static_cast<int64_t>(vertices.size()), 3}, open3d::core::Dtype::Float32);
161+
std::vector<uint32_t> triangles;
162+
for (int i = 0; i < this->Faces.size(); i++) {
163+
triangles.push_back(static_cast<uint32_t>(this->Faces[i].x()));
164+
triangles.push_back(static_cast<uint32_t>(this->Faces[i].y()));
165+
triangles.push_back(static_cast<uint32_t>(this->Faces[i].z()));
166+
}
167+
open3d::core::Tensor trianglesTensor(triangles.data(), {static_cast<int64_t>(this->Faces.size()), 3}, open3d::core::Dtype::UInt32);
168+
rayCastingScene.AddTriangles(verticesPositionTensor, trianglesTensor);
169+
170+
auto pointCloudO3dCopy = targetCloud;
171+
std::shared_ptr<open3d::geometry::PointCloud> pointCloudO3d_ptr = pointCloudO3dCopy.Cvt2O3DPointCloud();
172+
std::vector<float> cloudPoints;
173+
for (const auto& point : pointCloudO3d_ptr->points_) {
174+
cloudPoints.insert(cloudPoints.end(), point.data(), point.data() + 3);
175+
}
176+
open3d::core::Tensor cloudPointsTensor(cloudPoints.data(), {static_cast<int64_t>(pointCloudO3d_ptr->points_.size()), 3}, open3d::core::Dtype::Float32);
177+
178+
open3d::core::Tensor sdf = rayCastingScene.ComputeSignedDistance(cloudPointsTensor);
179+
if (useAbs)
180+
sdf = sdf.Abs();
181+
std::vector<float> sdfVector(sdf.GetDataPtr<float>(), sdf.GetDataPtr<float>() + sdf.NumElements());
182+
183+
return sdfVector;
184+
}
148185
} // 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
{
@@ -100,6 +100,18 @@ namespace diffCheck::geometry
100100
/// @brief Number of faces in the mesh
101101
int GetNumFaces() const { return this->Faces.size(); }
102102

103+
public: ///< Distance calculations
104+
/**
105+
* @brief Compute the distance between the df mesh and a df point cloud. It
106+
* can be considered as a point to plane distance.
107+
*
108+
* @param target the target cloud in format df
109+
* @param useAbs if true, the absolute value of the distance is returned
110+
* @return std::vector<float> the distance between the point cloud and the mesh
111+
*/
112+
std::vector<float> ComputeDistance(const diffCheck::geometry::DFPointCloud &targetMesh, bool useAbs = true);
113+
114+
103115
public: ///< Basic mesh data
104116
/// @brief Eigen vector of 3D vertices
105117
std::vector<Eigen::Vector3d> Vertices;

src/diffCheck/geometry/DFPointCloud.cc

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -116,18 +116,6 @@ namespace diffCheck::geometry
116116
return cilantroPointCloud;
117117
}
118118

119-
std::vector<double> DFPointCloud::ComputeP2PDistance(std::shared_ptr<geometry::DFPointCloud> target)
120-
{
121-
std::vector<double> errors;
122-
auto O3DSourcePointCloud = this->Cvt2O3DPointCloud();
123-
auto O3DTargetPointCloud = target->Cvt2O3DPointCloud();
124-
125-
std::vector<double> distances;
126-
127-
distances = O3DSourcePointCloud->ComputePointCloudDistance(*O3DTargetPointCloud);
128-
return distances;
129-
}
130-
131119
std::vector<Eigen::Vector3d> DFPointCloud::ComputeBoundingBox()
132120
{
133121
auto O3DPointCloud = this->Cvt2O3DPointCloud();
@@ -273,6 +261,18 @@ namespace diffCheck::geometry
273261
this->Normals = cloud->Normals;
274262
}
275263

264+
std::vector<double> DFPointCloud::ComputeDistance(std::shared_ptr<geometry::DFPointCloud> target)
265+
{
266+
std::vector<double> errors;
267+
auto O3DSourcePointCloud = this->Cvt2O3DPointCloud();
268+
auto O3DTargetPointCloud = target->Cvt2O3DPointCloud();
269+
270+
std::vector<double> distances;
271+
272+
distances = O3DSourcePointCloud->ComputePointCloudDistance(*O3DTargetPointCloud);
273+
return distances;
274+
}
275+
276276
void DFPointCloud::AddPoints(const DFPointCloud &pointCloud)
277277
{
278278
this->Points.insert(this->Points.end(), pointCloud.Points.begin(), pointCloud.Points.end());
@@ -290,4 +290,4 @@ namespace diffCheck::geometry
290290
center /= this->Points.size();
291291
return center;
292292
}
293-
}
293+
}

src/diffCheck/geometry/DFPointCloud.hh

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

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

99
#include <cilantro/utilities/point_cloud.hpp>
1010
#include <cilantro/core/nearest_neighbors.hpp>
@@ -48,19 +48,6 @@ namespace diffCheck::geometry
4848
std::shared_ptr<cilantro::PointCloud3f> Cvt2CilantroPointCloud();
4949

5050
public: ///< Utilities
51-
/**
52-
* @brief Compute the "point to point" distance between this and another point clouds.
53-
*
54-
* For every point in the source point cloud, it looks in the KDTree of the target point cloud and finds the closest point.
55-
* It returns a vector of distances, one for each point in the source point cloud.
56-
*
57-
* @param target The target diffCheck point cloud
58-
* @return std::vector<double> A vector of distances, one for each point in the source point cloud.
59-
*
60-
* @see https://github.com/isl-org/Open3D/blob/main/cpp/open3d/geometry/PointCloud.cpp
61-
*/
62-
std::vector<double> ComputeP2PDistance(std::shared_ptr<geometry::DFPointCloud> target);
63-
6451
/**
6552
* @brief Compute the bounding box of the point cloud and stores it as member of the DFPointCloud object
6653
*
@@ -156,6 +143,19 @@ namespace diffCheck::geometry
156143
*/
157144
void LoadFromPLY(const std::string &path);
158145

146+
public: ///< Distance calculations
147+
/**
148+
* @brief Compute the distance between two point clouds.
149+
* For every point in the source point cloud, it looks in the KDTree of the target point cloud and finds the closest point.
150+
* It returns a vector of distances, one for each point in the source point cloud.
151+
*
152+
* @param target the target point cloud in format df
153+
* @return std::vector<double> the distance between the two point clouds
154+
*
155+
* @see https://github.com/isl-org/Open3D/blob/main/cpp/open3d/geometry/PointCloud.cpp
156+
*/
157+
std::vector<double> ComputeDistance(std::shared_ptr<DFPointCloud> target);
158+
159159
/**
160160
* @brief adds the points, colors and normals from another point cloud
161161
*

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/diffCheckBindings.cc

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ 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"))
3334
.def("compute_BoundingBox", &diffCheck::geometry::DFPointCloud::ComputeBoundingBox)
3435

3536
.def("voxel_downsample", &diffCheck::geometry::DFPointCloud::VoxelDownsample,
@@ -75,6 +76,10 @@ PYBIND11_MODULE(diffcheck_bindings, m) {
7576
.def(py::init<>())
7677
.def(py::init<std::vector<Eigen::Vector3d>, std::vector<Eigen::Vector3i>, std::vector<Eigen::Vector3d>, std::vector<Eigen::Vector3d>, std::vector<Eigen::Vector3d>>())
7778

79+
.def("compute_distance", &diffCheck::geometry::DFMesh::ComputeDistance,
80+
py::arg("target_cloud"),
81+
py::arg("is_abs") = true)
82+
7883
.def("load_from_PLY", &diffCheck::geometry::DFMesh::LoadFromPLY)
7984

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

0 commit comments

Comments
 (0)