From f699bd905de101de2feb752d32cd9ceb8f15007c Mon Sep 17 00:00:00 2001 From: Ismail Date: Mon, 11 May 2026 21:06:47 +0200 Subject: [PATCH] Fix getFitnessScore to respect user-provided indices in Registration --- .../pcl/registration/impl/registration.hpp | 7 +++- .../include/pcl/registration/registration.h | 3 +- test/registration/test_registration.cpp | 38 +++++++++++++++++++ 3 files changed, 45 insertions(+), 3 deletions(-) diff --git a/registration/include/pcl/registration/impl/registration.hpp b/registration/include/pcl/registration/impl/registration.hpp index b246958927e..e55bc344590 100644 --- a/registration/include/pcl/registration/impl/registration.hpp +++ b/registration/include/pcl/registration/impl/registration.hpp @@ -131,13 +131,16 @@ Registration::getFitnessScore( template inline double -Registration::getFitnessScore(double max_range) +Registration::getFitnessScore (double max_range, bool use_indices) { double fitness_score = 0.0; // Transform the input dataset using the final transformation PointCloudSource input_transformed; - transformPointCloud(*input_, input_transformed, final_transformation_); + if (use_indices && indices_ && indices_->size () != input_->size ()) + transformPointCloud (*input_, *indices_, input_transformed, final_transformation_); + else + transformPointCloud (*input_, input_transformed, final_transformation_); pcl::Indices nn_indices(1); std::vector nn_dists(1); diff --git a/registration/include/pcl/registration/registration.h b/registration/include/pcl/registration/registration.h index 171b0854fdd..9ae0e55c4d1 100644 --- a/registration/include/pcl/registration/registration.h +++ b/registration/include/pcl/registration/registration.h @@ -444,9 +444,10 @@ class Registration : public PCLBase { /** \brief Obtain the Euclidean fitness score (e.g., mean of squared distances from * the source to the target) \param[in] max_range maximum allowable distance between a * point and its correspondence in the target (default: double::max) + * \param[in] use_indices whether to use the registered indices or all points (default: false) */ inline double - getFitnessScore(double max_range = std::numeric_limits::max()); + getFitnessScore (double max_range = std::numeric_limits::max (), bool use_indices = false); /** \brief Obtain the Euclidean fitness score (e.g., mean of squared distances from * the source to the target) from two sets of correspondence distances (distances diff --git a/test/registration/test_registration.cpp b/test/registration/test_registration.cpp index 0eda177b8a2..494ec323a70 100644 --- a/test/registration/test_registration.cpp +++ b/test/registration/test_registration.cpp @@ -194,6 +194,44 @@ TEST(PCL, ICP_translated) EXPECT_NEAR(icp.getFinalTransformation()(2, 3), 0.2, 2e-3); } +////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +TEST (PCL, Registration_getFitnessScore_Indices) +{ + pcl::PointCloud::Ptr cloud_in (new pcl::PointCloud); + pcl::PointCloud::Ptr cloud_out (new pcl::PointCloud); + + cloud_in->push_back (pcl::PointXYZ (0, 0, 0)); + cloud_in->push_back (pcl::PointXYZ (0, 1, 0)); + cloud_in->push_back (pcl::PointXYZ (0, 0, 1)); + cloud_in->push_back (pcl::PointXYZ (10, 0, 0)); + + cloud_out->push_back (pcl::PointXYZ (0, 0, 0)); + cloud_out->push_back (pcl::PointXYZ (0, 1, 0)); + cloud_out->push_back (pcl::PointXYZ (0, 0, 1)); + cloud_out->push_back (pcl::PointXYZ (10, 0, 0.5)); // Dist squared = 0.25 + + RegistrationWrapper reg; + reg.setInputSource (cloud_in); + reg.setInputTarget (cloud_out); + + pcl::IndicesPtr indices (new pcl::Indices ()); + indices->push_back (0); + indices->push_back (1); + indices->push_back (2); + reg.setIndices (indices); + + pcl::PointCloud final_cloud; + reg.align (final_cloud); + + // With use_indices = false (default), should calculate score using all points + // mean of squared distances: (0 + 0 + 0 + 0.25) / 4 = 0.0625 + EXPECT_NEAR (reg.getFitnessScore (1.0, false), 0.0625, 1e-4); + + // With use_indices = true, should calculate score using only indices (points 0, 1, 2) + // mean of squared distances: (0 + 0 + 0) / 3 = 0.0 + EXPECT_NEAR (reg.getFitnessScore (1.0, true), 0.0, 1e-4); +} + ////////////////////////////////////////////////////////////////////////////////////////////////////////////////// TEST (PCL, IterativeClosestPoint) {