diff --git a/src/diffCheck/geometry/DFPointCloud.cc b/src/diffCheck/geometry/DFPointCloud.cc index db3bcf76..2ed6934c 100644 --- a/src/diffCheck/geometry/DFPointCloud.cc +++ b/src/diffCheck/geometry/DFPointCloud.cc @@ -116,7 +116,7 @@ namespace diffCheck::geometry return cilantroPointCloud; } - std::vector DFPointCloud::ComputeBoundingBox() + std::vector DFPointCloud::GetAxixAlignedBoundingBox() { auto O3DPointCloud = this->Cvt2O3DPointCloud(); auto boundingBox = O3DPointCloud->GetAxisAlignedBoundingBox(); diff --git a/src/diffCheck/geometry/DFPointCloud.hh b/src/diffCheck/geometry/DFPointCloud.hh index b66a78d3..5a172385 100644 --- a/src/diffCheck/geometry/DFPointCloud.hh +++ b/src/diffCheck/geometry/DFPointCloud.hh @@ -54,7 +54,7 @@ namespace diffCheck::geometry * @return std::vector A vector of two Eigen::Vector3d, with the first one being the minimum * point and the second one the maximum point of the bounding box. */ - std::vector ComputeBoundingBox(); + std::vector GetAxixAlignedBoundingBox(); /** * @brief Estimate the normals of the point cloud by either knn or if the radius diff --git a/src/diffCheck/registrations/DFRefinedRegistration.cc b/src/diffCheck/registrations/DFRefinedRegistration.cc index 503382d6..83d64f5a 100644 --- a/src/diffCheck/registrations/DFRefinedRegistration.cc +++ b/src/diffCheck/registrations/DFRefinedRegistration.cc @@ -13,7 +13,7 @@ namespace diffCheck::registrations int maxIteration, bool usePointToPlane) { - std::vector minMax = source->ComputeBoundingBox(); + std::vector minMax = source->GetAxixAlignedBoundingBox(); std::shared_ptr O3Dsource = source->Cvt2O3DPointCloud(); std::shared_ptr O3Dtarget = target->Cvt2O3DPointCloud(); @@ -65,7 +65,7 @@ namespace diffCheck::registrations double relativeFitness, double relativeRMSE) { - std::vector minMax = source->ComputeBoundingBox(); + std::vector minMax = source->GetAxixAlignedBoundingBox(); std::shared_ptr O3Dsource = source->Cvt2O3DPointCloud(); std::shared_ptr O3Dtarget = target->Cvt2O3DPointCloud(); diff --git a/src/diffCheckBindings.cc b/src/diffCheckBindings.cc index 9b7d2af0..55e2db7e 100644 --- a/src/diffCheckBindings.cc +++ b/src/diffCheckBindings.cc @@ -31,7 +31,6 @@ PYBIND11_MODULE(diffcheck_bindings, m) { .def("compute_distance", &diffCheck::geometry::DFPointCloud::ComputeDistance, py::arg("target_cloud")) - .def("compute_BoundingBox", &diffCheck::geometry::DFPointCloud::ComputeBoundingBox) .def("voxel_downsample", &diffCheck::geometry::DFPointCloud::VoxelDownsample, py::arg("voxel_size")) @@ -40,6 +39,9 @@ PYBIND11_MODULE(diffcheck_bindings, m) { .def("downsample_by_size", &diffCheck::geometry::DFPointCloud::DownsampleBySize, py::arg("target_size")) + .def("apply_transformation", &diffCheck::geometry::DFPointCloud::ApplyTransformation, + py::arg("transformation")) + .def("estimate_normals", &diffCheck::geometry::DFPointCloud::EstimateNormals, py::arg("use_cilantro_evaluator") = false, py::arg("knn") = 100, @@ -52,6 +54,7 @@ PYBIND11_MODULE(diffcheck_bindings, m) { .def("add_points", &diffCheck::geometry::DFPointCloud::AddPoints) .def("get_tight_bounding_box", &diffCheck::geometry::DFPointCloud::GetTightBoundingBox) + .def("get_axis_aligned_bounding_box", &diffCheck::geometry::DFPointCloud::GetAxixAlignedBoundingBox) .def("get_num_points", &diffCheck::geometry::DFPointCloud::GetNumPoints) .def("get_num_colors", &diffCheck::geometry::DFPointCloud::GetNumColors) diff --git a/tests/integration_tests/pybinds_tests/test_pybind_units.py b/tests/integration_tests/pybinds_tests/test_pybind_units.py index f906c561..3149c70d 100644 --- a/tests/integration_tests/pybinds_tests/test_pybind_units.py +++ b/tests/integration_tests/pybinds_tests/test_pybind_units.py @@ -27,6 +27,41 @@ def get_ply_cloud_roof_quarter_path(): raise FileNotFoundError(f"PLY file not found at: {ply_file_path}") return ply_file_path +def get_ply_cloud_sphere_path(): + base_test_data_dir = os.getenv('DF_TEST_DATA_DIR', os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..', 'test_data'))) + ply_file_path = os.path.join(base_test_data_dir, "sphere_5kpts_with_normals.ply") + if not os.path.exists(ply_file_path): + raise FileNotFoundError(f"PLY file not found at: {ply_file_path}") + return ply_file_path + +def get_ply_cloud_bunny_path(): + base_test_data_dir = os.getenv('DF_TEST_DATA_DIR', os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..', 'test_data'))) + ply_file_path = os.path.join(base_test_data_dir, "stanford_bunny_50kpts_with_normals.ply") + if not os.path.exists(ply_file_path): + raise FileNotFoundError(f"PLY file not found at: {ply_file_path}") + return ply_file_path + +def get_ply_mesh_cube_path(): + base_test_data_dir = os.getenv('DF_TEST_DATA_DIR', os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..', 'test_data'))) + ply_file_path = os.path.join(base_test_data_dir, "cube_mesh.ply") + if not os.path.exists(ply_file_path): + raise FileNotFoundError(f"PLY file not found at: {ply_file_path}") + return ply_file_path + +def get_two_separate_planes_ply_path(): + base_test_data_dir = os.getenv('DF_TEST_DATA_DIR', os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..', 'test_data'))) + ply_file_path = os.path.join(base_test_data_dir, "two_separate_planes_with_normals.ply") + if not os.path.exists(ply_file_path): + raise FileNotFoundError(f"PLY file not found at: {ply_file_path}") + return ply_file_path + +def get_two_connected_planes_ply_path(): + base_test_data_dir = os.getenv('DF_TEST_DATA_DIR', os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..', 'test_data'))) + ply_file_path = os.path.join(base_test_data_dir, "two_connected_planes_with_normals.ply") + if not os.path.exists(ply_file_path): + raise FileNotFoundError(f"PLY file not found at: {ply_file_path}") + return ply_file_path + #------------------------------------------------------------------------------ # dfb_geometry namespace #------------------------------------------------------------------------------ @@ -54,6 +89,42 @@ def create_DFPointCloudSampleRoof(): df_pcd.load_from_PLY(get_ply_cloud_roof_quarter_path()) yield df_pcd +@pytest.fixture +def create_two_DFPointCloudSphere(): + df_pcd_1 = dfb.dfb_geometry.DFPointCloud() + df_pcd_2 = dfb.dfb_geometry.DFPointCloud() + df_pcd_1.load_from_PLY(get_ply_cloud_sphere_path()) + df_pcd_2.load_from_PLY(get_ply_cloud_sphere_path()) + yield df_pcd_1, df_pcd_2 + +@pytest.fixture +def create_two_DFPointCloudBunny(): + df_pcd_1 = dfb.dfb_geometry.DFPointCloud() + df_pcd_2 = dfb.dfb_geometry.DFPointCloud() + df_pcd_1.load_from_PLY(get_ply_cloud_bunny_path()) + df_pcd_2.load_from_PLY(get_ply_cloud_bunny_path()) + yield df_pcd_1, df_pcd_2 + +@pytest.fixture +def create_DFPointCloudTwoSeparatePlanes(): + df_pcd = dfb.dfb_geometry.DFPointCloud() + df_pcd.load_from_PLY(get_two_separate_planes_ply_path()) + yield df_pcd + +@pytest.fixture +def create_DFPointCloudTwoConnectedPlanes(): + df_pcd = dfb.dfb_geometry.DFPointCloud() + df_pcd.load_from_PLY(get_two_connected_planes_ply_path()) + yield df_pcd + +@pytest.fixture +def create_DFMeshCube(): + df_mesh = dfb.dfb_geometry.DFMesh() + df_mesh.load_from_PLY(get_ply_mesh_cube_path()) + yield df_mesh + +# point cloud tests + def test_DFPointCloud_properties(create_DFPointCloudSampleRoof): pc = create_DFPointCloudSampleRoof assert pc.points.__len__() == 7379, "DFPointCloud should have 7379 points" @@ -114,9 +185,82 @@ def test_DFPointCloud_get_tight_bounding_box(create_DFPointCloudSampleRoof): # round to the 3 decimal places assert round(obb[0][0], 3) == 0.196, "The min x of the OBB should be 0.196" -# TODO: to implement DFMesh tests +def test_DFPointCloud_get_axis_aligned_bounding_box(create_DFPointCloudSampleRoof): + pc = create_DFPointCloudSampleRoof + aabb = pc.get_axis_aligned_bounding_box() + # round to the 3 decimal places + assert round(aabb[0][0], 3) == -2.339, "The min x of the AABB should be 0.196" + +def test_DFPointCloud_compute_distance(): + point_pc_1 = [(0, 0, 0)] + point_pc_2 = [(1, 0, 0)] + normal_pc_1 = [(0, 0, 1)] + normal_pc_2 = [(0, 0, 1)] + color_pc_1 = [(0, 0, 0)] + color_pc_2 = [(0, 0, 0)] + + pc_1 = dfb.dfb_geometry.DFPointCloud(point_pc_1, normal_pc_1, color_pc_1) + pc_2 = dfb.dfb_geometry.DFPointCloud(point_pc_2, normal_pc_2, color_pc_2) + + distance = pc_1.compute_distance(pc_2)[0] + + assert distance == 1. , "The distance between the two points should be 1." + +# mesh tests + def test_DFMesh_init(): - pass + mesh = dfb.dfb_geometry.DFMesh() + assert mesh is not None, "DFMesh should be initialized successfully" + +def test_DFMesh_load_from_PLY(create_DFMeshCube): + mesh = create_DFMeshCube + assert mesh.vertices.__len__() == 726, "DFMesh should have 726 vertices" + assert mesh.faces.__len__() == 1200, "DFMesh should have 800 faces" + +def test_DFMesh_properties(): + vertices = [[0, 0, 0], [1, 0, 0], [1, 1, 0], [0, 1, 0], [0, 1, -1], [0, 0, -1]] + faces = [[0, 1, 2], [0, 2, 3], [0, 3, 4], [0, 4, 5]] + mesh = dfb.dfb_geometry.DFMesh(vertices, faces, [], [], []) + assert mesh.get_num_vertices() == 6, "get_num_vertices() should return 6" + assert mesh.get_num_faces() == 4, "get_num_faces() should return 4" + assert isinstance(mesh.vertices, list), "vertices should be a list" + assert isinstance(mesh.faces, list), "faces should be a list" + assert isinstance(mesh.normals_face, list), "normals_faces should be a list" + assert isinstance(mesh.normals_vertex, list), "normals_vertex should be a list" + assert isinstance(mesh.colors_face, list), "colors_faces should be a list" + assert isinstance(mesh.colors_vertex, list), "colors_vertex should be a list" + assert len(mesh.vertices[0]) == 3, "vertices should be a list of 3 coordinates" + assert len(mesh.faces[0]) == 3, "faces should be a list of 3 indexes" + +def test_DFMesh_compute_distance(): + vertices = [[0, 0, 0], [1, 0, 0], [0, 1, 0]] + faces = [[0, 1, 2]] + mesh = dfb.dfb_geometry.DFMesh(vertices, faces, [], [], []) + point = [(0, 0, 1)] + normal = [(0, 0, 1)] + color = [(0, 0, 0)] + pc = dfb.dfb_geometry.DFPointCloud(point, normal, color) + distance = mesh.compute_distance(pc)[0] + assert distance == 1.0, "The distance between the point and the mesh should be 1.0" + +def test_DFMesh_sample_points(create_DFMeshCube): + mesh = create_DFMeshCube + pc = mesh.sample_points_uniformly(1000) + assert pc.points.__len__() == 1000, "DFPointCloud should have 1000 points" + +def test_DFMesh_compute_bounding_box(create_DFMeshCube): + mesh = create_DFMeshCube + obb = mesh.get_tight_bounding_box() + assert obb[0][0] == 0, "The x coordinate of the first corner of the OBB should be 0" + assert obb[1][0] == 100, "The y coordinate of the second corner of the OBB should be 100" + assert obb[2][0] == 0, "The y coordinate of the third corner of the OBB should be 0" + assert obb[6][2] == 100, "The z coordinate of the second to last corner of the OBB should be 100" + +def test_DFMesh_getters(create_DFMeshCube): + mesh = create_DFMeshCube + assert mesh.get_num_vertices() == 726, "get_num_vertices() should return 726" + assert mesh.get_num_faces() == 1200, "get_num_faces() should return 1200" + #------------------------------------------------------------------------------ # dfb_transformation namespace @@ -146,13 +290,118 @@ def test_DFTransform_read_write(create_DFPointCloudSampleRoof): #------------------------------------------------------------------------------ # dfb_registrations namespace #------------------------------------------------------------------------------ -# TODO: to be implemented +def test_DFRegistration_pure_translation(create_two_DFPointCloudSphere): + + def make_assertions(df_transformation_result): + assert df_transformation_result is not None, "DFRegistration should return a transformation matrix" + assert abs(df_transformation_result.transformation_matrix[0][3] - 20) < 0.5, "The translation in x should be around 20" + assert abs(df_transformation_result.transformation_matrix[1][3] - 20) < 0.5, "The translation in y should be around 20" + assert abs(df_transformation_result.transformation_matrix[2][3] - 20) < 0.5, "The translation in z should be around 20" + sphere_1, sphere_2 = create_two_DFPointCloudSphere + + t = dfb.dfb_transformation.DFTransformation() + t.transformation_matrix = [[1.0, 0.0, 0.0, 20], + [0.0, 1.0, 0.0, 20], + [0.0, 0.0, 1.0, 20], + [0.0, 0.0, 0.0, 1.0]] + + sphere_2.apply_transformation(t) + + df_transformation_result_o3dfgrfm = dfb.dfb_registrations.DFGlobalRegistrations.O3DFastGlobalRegistrationFeatureMatching(sphere_1, sphere_2) + df_transformation_result_o3drfm = dfb.dfb_registrations.DFGlobalRegistrations.O3DRansacOnFeatureMatching(sphere_1, sphere_2) + df_transformation_result_o3dicp = dfb.dfb_registrations.DFRefinedRegistration.O3DICP(sphere_1, sphere_2, max_correspondence_distance=20) + df_transformation_result_o3dgicp = dfb.dfb_registrations.DFRefinedRegistration.O3DGeneralizedICP(sphere_1, sphere_2, max_correspondence_distance=20) + + make_assertions(df_transformation_result_o3dfgrfm) + make_assertions(df_transformation_result_o3drfm) + make_assertions(df_transformation_result_o3dicp) + make_assertions(df_transformation_result_o3dgicp) + + +def test_DFRegistration_rotation_bunny(create_two_DFPointCloudBunny): + + def make_assertions(df_transformation_result): + assert df_transformation_result is not None, "DFRegistration should return a transformation matrix" + assert abs(df_transformation_result.transformation_matrix[0][0] - 0.866) < 0.2, "The rotation part of transformation matrix should be close to the transposed rotation matrix initially applied" + assert abs(df_transformation_result.transformation_matrix[0][1]) < 0.2, "The rotation part of transformation matrix should be close to the transposed rotation matrix initially applied" + assert abs(df_transformation_result.transformation_matrix[0][2] - 0.5) < 0.2, "The rotation part of transformation matrix should be close to the transposed rotation matrix initially applied" + + bunny_1, bunny_2 =create_two_DFPointCloudBunny + + r = dfb.dfb_transformation.DFTransformation() + r.transformation_matrix = [[0.866, 0.0, 0.5, 0.0], + [0.0, 1.0, 0.0, 0.0], + [-0.5, 0.0, 0.866, 0.0], + [0.0, 0.0, 0.0, 1.0]] # 30 degree rotation around y-axis + bunny_2.apply_transformation(r) + + df_transformation_result_o3dfgrfm = dfb.dfb_registrations.DFGlobalRegistrations.O3DFastGlobalRegistrationFeatureMatching(bunny_1, bunny_2) + df_transformation_result_o3drfm = dfb.dfb_registrations.DFGlobalRegistrations.O3DRansacOnFeatureMatching(bunny_1, bunny_2) + df_transformation_result_o3dicp = dfb.dfb_registrations.DFRefinedRegistration.O3DICP(bunny_1, bunny_2, max_correspondence_distance=1.0) + df_transformation_result_o3dgicp = dfb.dfb_registrations.DFRefinedRegistration.O3DGeneralizedICP(bunny_1, bunny_2, max_correspondence_distance=15.0) + + make_assertions(df_transformation_result_o3dfgrfm) + make_assertions(df_transformation_result_o3drfm) + make_assertions(df_transformation_result_o3dicp) + make_assertions(df_transformation_result_o3dgicp) + + +def test_DFRegistration_composite_bunny(create_two_DFPointCloudBunny): + + def make_assertions(df_transformation_result): + assert df_transformation_result is not None, "DFRegistration should return a transformation matrix" + assert abs(df_transformation_result.transformation_matrix[0][3] - 0.1) < 0.02, "The translation in x should be around -0.05" + assert abs(df_transformation_result.transformation_matrix[1][3] - 0.1) < 0.02, "The translation in y should be around -0.05" + assert abs(df_transformation_result.transformation_matrix[2][3] - 0.1) < 0.02, "The translation in z should be around 0.05" + assert abs(df_transformation_result.transformation_matrix[0][0] - 0.866) < 0.2, "The rotation part of transformation matrix should be close to the transposed rotation matrix initially applied" + assert abs(df_transformation_result.transformation_matrix[0][1]) < 0.2, "The rotation part of transformation matrix should be close to the transposed rotation matrix initially applied" + assert abs(df_transformation_result.transformation_matrix[0][2] - 0.5) < 0.2, "The rotation part of transformation matrix should be close to the transposed rotation matrix initially applied" + + bunny_1 ,bunny_2 = create_two_DFPointCloudBunny + + transform = dfb.dfb_transformation.DFTransformation() + transform.transformation_matrix = [[0.866, 0.0, 0.5, 0.1], + [0.0, 1.0, 0.0, 0.1], + [-0.5, 0.0, 0.866, 0.1], + [0.0, 0.0, 0.0, 1.0]] # 30 degree rotation around y-axis + translation + + bunny_2.apply_transformation(transform) + + df_transformation_result_o3dfgrfm = dfb.dfb_registrations.DFGlobalRegistrations.O3DFastGlobalRegistrationFeatureMatching(bunny_1, bunny_2) + df_transformation_result_o3drfm = dfb.dfb_registrations.DFGlobalRegistrations.O3DRansacOnFeatureMatching(bunny_1, bunny_2) + df_transformation_result_o3dicp = dfb.dfb_registrations.DFRefinedRegistration.O3DICP(bunny_1, bunny_2) + df_transformation_result_o3dgicp = dfb.dfb_registrations.DFRefinedRegistration.O3DGeneralizedICP(bunny_1, bunny_2) + + make_assertions(df_transformation_result_o3dfgrfm) + make_assertions(df_transformation_result_o3drfm) + make_assertions(df_transformation_result_o3dicp) + make_assertions(df_transformation_result_o3dgicp) + + #------------------------------------------------------------------------------ # dfb_segmentation namespace #------------------------------------------------------------------------------ -# TODO: to be implemented +def test_DFPlaneSegmentation_separate_plans(create_DFPointCloudTwoSeparatePlanes): + pc = create_DFPointCloudTwoSeparatePlanes + + segments = dfb.dfb_segmentation.DFSegmentation.segment_by_normal(pc, + normal_threshold_degree=5, + min_cluster_size=100, + knn_neighborhood_size=20) + + assert len(segments) == 2, "DFPlaneSegmentation should return 2 segments" + +def test_DFPlaneSegmentation_connected_plans(create_DFPointCloudTwoConnectedPlanes): + pc = create_DFPointCloudTwoConnectedPlanes + + segments = dfb.dfb_segmentation.DFSegmentation.segment_by_normal(pc, + normal_threshold_degree=5, + min_cluster_size=100, + knn_neighborhood_size=20) + + assert len(segments) == 2, "DFPlaneSegmentation should return 2 segments" if __name__ == "__main__": pytest.main() \ No newline at end of file diff --git a/tests/test_data/cube_mesh.ply b/tests/test_data/cube_mesh.ply new file mode 100644 index 00000000..38ad7a06 --- /dev/null +++ b/tests/test_data/cube_mesh.ply @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:8a7804c4bfc0fbebb68ffb9a7d616c6dc21199d1e43a0b9434168ee47ce5a09e +size 36134 diff --git a/tests/test_data/sphere_5kpts_with_normals.ply b/tests/test_data/sphere_5kpts_with_normals.ply new file mode 100644 index 00000000..57659d60 --- /dev/null +++ b/tests/test_data/sphere_5kpts_with_normals.ply @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:0374e977d8f8a23e99eb19682d1429a572b8a1f4db297d06afe8aeafdf389db9 +size 120891 diff --git a/tests/test_data/stanford_bunny_50kpts_with_normals.ply b/tests/test_data/stanford_bunny_50kpts_with_normals.ply new file mode 100644 index 00000000..5dc2156d --- /dev/null +++ b/tests/test_data/stanford_bunny_50kpts_with_normals.ply @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:c5af80113153bfc7e52ba6d0966a020f4a0b96ef4021b2704ebdbedbef40994a +size 1195348 diff --git a/tests/test_data/two_connected_planes_with_normals.ply b/tests/test_data/two_connected_planes_with_normals.ply new file mode 100644 index 00000000..0a0406e0 --- /dev/null +++ b/tests/test_data/two_connected_planes_with_normals.ply @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:878ac619b461a9e1385a74cdaab7e9a9e553221da8bf59c407c1130eb16faff5 +size 48291 diff --git a/tests/test_data/two_separate_planes_with_normals.ply b/tests/test_data/two_separate_planes_with_normals.ply new file mode 100644 index 00000000..1e41e554 --- /dev/null +++ b/tests/test_data/two_separate_planes_with_normals.ply @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:94ca289b7e9eedeabae77b8ab764e95d3817877a060b44eb2375b048f576514c +size 48291 diff --git a/tests/unit_tests/DFPointCloudTest.cc b/tests/unit_tests/DFPointCloudTest.cc index d2ea167b..2e392e3c 100644 --- a/tests/unit_tests/DFPointCloudTest.cc +++ b/tests/unit_tests/DFPointCloudTest.cc @@ -125,7 +125,7 @@ TEST_F(DFPointCloudTestFixture, AddPoints) { } TEST_F(DFPointCloudTestFixture, ComputeAABB) { - std::vector bbox = dfPointCloud.ComputeBoundingBox(); + std::vector bbox = dfPointCloud.GetAxixAlignedBoundingBox(); EXPECT_EQ(bbox.size(), 2); }