Skip to content

Commit e0fcce4

Browse files
committed
CAP-WIP: working version basic for registration - problem with scaling and parameters adaptation mechanism
1 parent 52ae0d9 commit e0fcce4

File tree

6 files changed

+137
-65
lines changed

6 files changed

+137
-65
lines changed

src/diffCheck/registrations/DFGlobalRegistrations.hh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ namespace diffCheck::registrations
2020
*
2121
* @param source the source diffCheck point cloud
2222
* @param target the target diffCheck point cloud
23+
* @param voxelize whether to voxelize the point clouds before computing the FPFHFeatures. A higher value will result in a more coarse point cloud (less resulting points).
2324
* @param voxelSize the size of the voxels used to downsample the point clouds. It is expressed relative to the point cloud size (0.01 means voxelSize = 1% of maxSize(pointCloud). A higher value will result in a more coarse point cloud (less resulting points).
2425
* @param radiusKDTreeSearch the radius used to search for neighbors in the KDTree.it is expressed relative to the point cloud size (0.01 means radiusKDTreeSearch = 1% of maxSize(pointCloud). It is used for the calculation of FPFHFeatures
2526
* @param maxNeighborKDTreeSearch the maximum number of neighbors to search for in the KDTree. It is used for the calculation of FPFHFeatures. A higher value will result in heavier computation but potentially more precise.

src/diffCheckApp.cc

Lines changed: 13 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -7,60 +7,27 @@
77
int main()
88
{
99
// import clouds
10-
std::shared_ptr<diffCheck::geometry::DFPointCloud> dfPointCloudPtr = std::make_shared<diffCheck::geometry::DFPointCloud>();
11-
std::shared_ptr<diffCheck::geometry::DFMesh> dfMeshPtr = std::make_shared<diffCheck::geometry::DFMesh>();
12-
std::shared_ptr<diffCheck::geometry::DFPointCloud> dfGroundTruth = std::make_shared<diffCheck::geometry::DFPointCloud>();
10+
std::shared_ptr<diffCheck::geometry::DFPointCloud> pcdSrc = std::make_shared<diffCheck::geometry::DFPointCloud>();
11+
std::shared_ptr<diffCheck::geometry::DFPointCloud> pcdTgt = std::make_shared<diffCheck::geometry::DFPointCloud>();
1312

14-
// std::string pathCloud = R"(C:\Users\andre\Downloads\scan_data_normals.ply\scan_data_normals.ply)";
15-
// std::string pathMesh = R"(F:\diffCheck\assets\dataset\mesh_fromRh_unfixedLength.ply)";
16-
// std::string pathMesh = R"(F:\diffCheck\temp\03_mesh.ply)";
13+
std::string pathPcdSrc = R"(C:\Users\andre\Downloads\04_pt.ply)";
14+
std::string pathPcdTgt = R"(C:\Users\andre\Downloads\moved_04.ply)";
1715

18-
// create a sphere from o3d
16+
pcdSrc->LoadFromPLY(pathPcdSrc);
17+
pcdTgt->LoadFromPLY(pathPcdTgt);
1918

20-
std::string pathCloud = R"(C:\Users\localuser\Downloads\04_pt.ply)";
21-
std::string pathMesh = R"(C:\Users\localuser\Downloads\04_mesh.ply)";
22-
// std::string pathMesh = R"(F:\diffCheck\temp\03_mesh.ply)";
19+
// global registration
20+
diffCheck::transformation::DFTransformation xform =
21+
diffCheck::registrations::DFGlobalRegistrations::O3DFastGlobalRegistrationFeatureMatching(
22+
pcdSrc, pcdTgt);
2323

24-
dfMeshPtr->LoadFromPLY(pathMesh);
25-
dfPointCloudPtr->LoadFromPLY(pathCloud);
24+
pcdTgt->ApplyTransformation(xform);
2625

27-
dfGroundTruth->Cvt2DFPointCloud(dfMeshPtr->Cvt2O3DTriangleMesh()->SamplePointsUniformly(10000));
28-
Eigen::Matrix4d transformation = Eigen::Matrix4d::Identity();
29-
transformation(0, 3) = 0.0;
30-
transformation(1, 3) = -0.02;
31-
transformation(2, 3) = 0.02;
32-
Eigen::Matrix3d rotation;
33-
rotation = Eigen::AngleAxisd(0.3, Eigen::Vector3d::UnitY());
34-
transformation.block<3, 3>(0, 0) = rotation * transformation.block<3, 3>(0, 0);
3526

36-
dfPointCloudPtr->ApplyTransformation(transformation);
37-
38-
diffCheck::transformation::DFTransformation simpleICPTransformation
39-
= diffCheck::registrations::RefinedRegistration::O3DICP(
40-
dfPointCloudPtr,
41-
dfGroundTruth,
42-
0.05);
43-
44-
diffCheck::transformation::DFTransformation generalizedICPTransformation
45-
= diffCheck::registrations::RefinedRegistration::O3DGeneralizedICP(
46-
dfPointCloudPtr,
47-
dfGroundTruth,
48-
0.05);
49-
dfPointCloudPtr->ApplyTransformation(simpleICPTransformation);
50-
51-
std::stringstream ss;
52-
ss << "Simple ICP Transformation:\n";
53-
ss << simpleICPTransformation.TransformationMatrix<<"\n";
54-
ss << "Generalized ICP Transformation:\n";
55-
ss << generalizedICPTransformation.TransformationMatrix;
56-
std::string matrixAsString = ss.str();
57-
DIFFCHECK_INFO(matrixAsString.c_str());
5827

5928
std::shared_ptr<diffCheck::visualizer::Visualizer> vis = std::make_shared<diffCheck::visualizer::Visualizer>();
60-
// vis->AddPointCloud(dfPointCloudPtr);
61-
vis->AddMesh(dfMeshPtr);
62-
vis->AddPointCloud(dfGroundTruth);
63-
vis->AddPointCloud(dfPointCloudPtr);
29+
vis->AddPointCloud(pcdSrc);
30+
vis->AddPointCloud(pcdTgt);
6431
vis->Run();
6532
return 0;
6633
}

src/diffCheckBindings.cc

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ PYBIND11_MODULE(diffcheck_bindings, m) {
2020
submodule_test.def("test", &test, "Simple function testing a vanilla python bindings.");
2121

2222
//#################################################################################################
23-
// df_geometry namespace
23+
// dfb_geometry namespace
2424
//#################################################################################################
2525

2626
py::module_ submodule_geometry = m.def_submodule("dfb_geometry", "A submodule for the geometry classes.");
@@ -83,8 +83,50 @@ PYBIND11_MODULE(diffcheck_bindings, m) {
8383
[](diffCheck::geometry::DFMesh &self, const std::vector<Eigen::Vector3d>& value) { self.ColorsFace = value; });
8484

8585
//#################################################################################################
86-
// df_registration namespace
86+
// dfb_transformation namespace
8787
//#################################################################################################
8888

89-
// py::module_ submodule_geometry = m.def_submodule("df_registration", "A submodule for the registration methods.");
89+
py::module_ submodule_transformation = m.def_submodule("dfb_transformation", "A submodule for the transformation classes.");
90+
91+
py::class_<diffCheck::transformation::DFTransformation>(submodule_transformation, "DFTransformation")
92+
.def(py::init<>())
93+
.def(py::init<const Eigen::Matrix4d&>())
94+
.def(py::init<const Eigen::Matrix3d&, const Eigen::Vector3d&>())
95+
96+
.def_readwrite("transformation_matrix", &diffCheck::transformation::DFTransformation::TransformationMatrix)
97+
.def_readwrite("rotation_matrix", &diffCheck::transformation::DFTransformation::RotationMatrix)
98+
.def_readwrite("translation_vector", &diffCheck::transformation::DFTransformation::TranslationVector);
99+
100+
101+
//#################################################################################################
102+
// dfb_registrations namespace
103+
//#################################################################################################
104+
105+
py::module_ submodule_registrations = m.def_submodule("dfb_registrations", "A submodule for the registration methods.");
106+
107+
py::class_<diffCheck::registrations::DFGlobalRegistrations>(submodule_registrations, "DFGlobalRegistrations")
108+
.def_static("O3DFastGlobalRegistrationFeatureMatching", &diffCheck::registrations::DFGlobalRegistrations::O3DFastGlobalRegistrationFeatureMatching,
109+
py::arg("source"),
110+
py::arg("target"),
111+
py::arg("voxelize") = true,
112+
py::arg("voxel_size") = 0.005,
113+
py::arg("radius_kd_tree_search") = 0.1,
114+
py::arg("max_neighbor_kd_tree_search") = 50,
115+
py::arg("max_correspondence_distance") = 0.05,
116+
py::arg("iteration_number") = 128,
117+
py::arg("max_tuple_count") = 1000)
118+
.def_static("O3DRansacOnFeatureMatching", &diffCheck::registrations::DFGlobalRegistrations::O3DRansacOnFeatureMatching,
119+
py::arg("source"),
120+
py::arg("target"),
121+
py::arg("voxelize") = true,
122+
py::arg("voxel_size") = 0.005,
123+
py::arg("radius_kd_tree_search") = 0.1,
124+
py::arg("max_neighbor_kd_tree_search") = 50,
125+
py::arg("max_correspondence_distance") = 0.05,
126+
py::arg("is_t_estimate_pt2pt") = false,
127+
py::arg("ransac_n") = 3,
128+
py::arg("correspondence_checker_distance") = 0.05,
129+
py::arg("similarity_threshold") = 0.9,
130+
py::arg("ransac_max_iteration") = 100000,
131+
py::arg("ransac_confidence_threshold") = 0.999);
90132
}

src/gh/components/DF_global_registration/code.py

Lines changed: 55 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,34 +12,74 @@
1212

1313
import diffCheck
1414
from diffCheck import diffcheck_bindings
15-
import diffCheck.df_cvt_bindings
15+
from diffCheck import df_cvt_bindings
1616

1717

1818
class DFGlobalRegistration(component):
1919
def RunScript(self,
20-
i_mesh: rg.Mesh,
21-
i_points: int) -> rg.PointCloud:
20+
i_cloud_source: rg.PointCloud = None,
21+
i_cloud_target: rg.PointCloud = None
22+
) -> rg.Transform:
2223
"""
23-
Convert a Rhino mesh to a cloud.
24+
The global registration component aligns two point clouds in a rough way.
2425
25-
:param i_mesh: mesh to convert
26-
:param i_points: number of points to sample
26+
:param i_cloud_source: source point cloud
27+
:param i_cloud_target: target point cloud to align to
2728
28-
:return o_cloud: rhino cloud
29+
:return o_x_form : transformation matrix
2930
"""
3031

31-
diffCheck.df_cvt_bindings.test()
32+
# if i_cloud_source is None or i_cloud_target is None:
33+
# ghenv.Component.AddRuntimeMessage(RML.Error, "Please provide two point clouds to align")
34+
# return o_x_form
3235

33-
df_mesh = diffCheck.df_cvt_bindings.cvt_rhmesh_2_dfmesh(i_mesh)
34-
df_cloud = df_mesh.sample_points_uniformly(i_points)
36+
print(type(i_cloud_source))
3537

36-
# convert the df_cloud to a rhino cloud
37-
rgpoints = [rg.Point3d(pt[0], pt[1], pt[2]) for pt in df_cloud.points]
38-
rh_cloud = rg.PointCloud(rgpoints)
38+
df_cloud_source = df_cvt_bindings.cvt_rhcloud_2_dfcloud(i_cloud_source)
39+
df_cloud_target = df_cvt_bindings.cvt_rhcloud_2_dfcloud(i_cloud_target)
3940

40-
return [rh_cloud]
41+
print(type(df_cloud_source))
42+
# # print all the available registration methods
43+
# print(dir(diffcheck_bindings.dfb_registrations))
44+
# registrations = diffcheck_bindings.dfb_registrations.DFGlobalRegistrations()
45+
46+
47+
48+
df_xform = diffcheck_bindings.dfb_registrations.DFGlobalRegistrations.O3DFastGlobalRegistrationFeatureMatching(
49+
source=df_cloud_source,
50+
target=df_cloud_target,
51+
voxelize=True,
52+
voxel_size=0.1,
53+
radius_kd_tree_search=0.1,
54+
max_neighbor_kd_tree_search=50,
55+
max_correspondence_distance=0.05,
56+
iteration_number=128,
57+
max_tuple_count=1000
58+
)
59+
print(type(df_xform))
60+
61+
print(df_xform.transformation_matrix)
62+
print("-------------------")
63+
print(df_xform.rotation_matrix)
64+
print("-------------------")
65+
print(df_xform.translation_vector)
66+
67+
df_xform_matrix = df_xform.transformation_matrix
68+
69+
70+
rh_form = rg.Transform()
71+
for i in range(4):
72+
for j in range(4):
73+
rh_form[i, j] = df_xform_matrix[i, j]
74+
75+
print(rh_form)
76+
77+
o_x_form = rh_form
78+
79+
80+
return o_x_form
4181

4282

4383
if __name__ == "__main__":
4484
com = DFGlobalRegistration()
45-
com.RunScript(i_mesh, i_points)
85+
o_x_form = com.RunScript(i_cloud_source, i_cloud_target)
-31 Bytes
Binary file not shown.

src/gh/diffCheck/diffCheck/df_cvt_bindings.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,4 +164,26 @@ def cvt_rhmesh_2_dfmesh(rh_mesh: rg.Mesh) -> diffcheck_bindings.dfb_geometry.DFM
164164
colors_vertex.append([color.R, color.G, color.B])
165165
df_mesh.colors_vertex = colors_vertex
166166

167-
return df_mesh
167+
return df_mesh
168+
169+
def cvt_dfxform_2_rhxform(df_xform : diffcheck_bindings.dfb_transformation.DFTransformation) -> rg.Transform:
170+
"""
171+
Convert a diffCheck transformation to a Rhino transformation.
172+
173+
:param df_xform: diffCheck transformation
174+
:return rh_xform: rhino transformation
175+
"""
176+
if not isinstance(df_xform, diffcheck_bindings.dfb_transformation.DFTransformation):
177+
raise ValueError("df_xform should be a DFTransformation")
178+
179+
rh_xform = rg.Transform()
180+
181+
# translation
182+
translation = df_xform.translation_vector
183+
rh_xform = rg.Transform.Translation(translation[0], translation[1], translation[2])
184+
185+
# rotation
186+
rotation = df_xform.rotation_matrix
187+
rh_xform = rh_xform * rg.Transform.Rotation(rotation[0], rg.Vector3d(rotation[1], rotation[2], rotation[3]), rg.Point3d(0, 0, 0))
188+
189+
return rh_xform

0 commit comments

Comments
 (0)