From c26c525ddaf2627b2fb69611004c291fb2b202d3 Mon Sep 17 00:00:00 2001 From: Franck HOUSSEN Date: Sat, 22 Jun 2024 19:56:39 +0200 Subject: [PATCH 1/3] [BUG FIX] getCorrespondenceScore must not go out-of-bounds. --- .../registration/correspondence_rejection.h | 18 ++++++++++++++++++ .../correspondence_rejection_features.h | 6 ++++++ 2 files changed, 24 insertions(+) diff --git a/registration/include/pcl/registration/correspondence_rejection.h b/registration/include/pcl/registration/correspondence_rejection.h index f68882ab665..123f77ad40d 100644 --- a/registration/include/pcl/registration/correspondence_rejection.h +++ b/registration/include/pcl/registration/correspondence_rejection.h @@ -347,6 +347,12 @@ class DataContainer : public DataContainerInterface { inline double getCorrespondenceScore(int index) override { + // Check correspondence is valid + if (index >= (*input_).size()) + { + return (std::numeric_limits::max ()); + } + if (target_cloud_updated_ && !force_no_recompute_) { tree_->setInputCloud(target_); } @@ -363,6 +369,12 @@ class DataContainer : public DataContainerInterface { inline double getCorrespondenceScore(const pcl::Correspondence& corr) override { + // Check correspondence is valid + if (corr.index_query >= (*input_).size() || corr.index_match >= (*target_).size()) + { + return (std::numeric_limits::max ()); + } + // Get the source and the target feature from the list const PointT& src = (*input_)[corr.index_query]; const PointT& tgt = (*target_)[corr.index_match]; @@ -377,6 +389,12 @@ class DataContainer : public DataContainerInterface { inline double getCorrespondenceScoreFromNormals(const pcl::Correspondence& corr) override { + // Check correspondence is valid + if (corr.index_query >= (*input_normals_).size() || corr.index_match >= (*target_normals_).size()) + { + return (std::numeric_limits::max ()); + } + // assert ( (input_normals_->size () != 0) && (target_normals_->size () != 0) && // "Normals are not set for the input and target point clouds"); assert(input_normals_ && target_normals_ && diff --git a/registration/include/pcl/registration/correspondence_rejection_features.h b/registration/include/pcl/registration/correspondence_rejection_features.h index 44835c379fc..65577d85261 100644 --- a/registration/include/pcl/registration/correspondence_rejection_features.h +++ b/registration/include/pcl/registration/correspondence_rejection_features.h @@ -257,6 +257,12 @@ class PCL_EXPORTS CorrespondenceRejectorFeatures : public CorrespondenceRejector inline double getCorrespondenceScore(int index) override { + // Check correspondence is valid + if (index >= (*source_features_).size() || index >= (*target_features_).size()) + { + return (std::numeric_limits::max ()); + } + // If no feature representation was given, reset to the default implementation for // FeatureT if (!feature_representation_) From c73cca5f290e194a359dae15e050fe09e0292f0c Mon Sep 17 00:00:00 2001 From: Franck HOUSSEN Date: Sun, 23 Jun 2024 10:10:28 +0200 Subject: [PATCH 2/3] Fix compilation warnings [-Wsign-compare]. --- .../include/pcl/registration/correspondence_rejection.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/registration/include/pcl/registration/correspondence_rejection.h b/registration/include/pcl/registration/correspondence_rejection.h index 123f77ad40d..54d0f11df8f 100644 --- a/registration/include/pcl/registration/correspondence_rejection.h +++ b/registration/include/pcl/registration/correspondence_rejection.h @@ -348,7 +348,7 @@ class DataContainer : public DataContainerInterface { getCorrespondenceScore(int index) override { // Check correspondence is valid - if (index >= (*input_).size()) + if ((size_t) index >= (*input_).size()) { return (std::numeric_limits::max ()); } @@ -370,7 +370,7 @@ class DataContainer : public DataContainerInterface { getCorrespondenceScore(const pcl::Correspondence& corr) override { // Check correspondence is valid - if (corr.index_query >= (*input_).size() || corr.index_match >= (*target_).size()) + if ((size_t) corr.index_query >= (*input_).size() || (size_t) corr.index_match >= (*target_).size()) { return (std::numeric_limits::max ()); } @@ -390,7 +390,7 @@ class DataContainer : public DataContainerInterface { getCorrespondenceScoreFromNormals(const pcl::Correspondence& corr) override { // Check correspondence is valid - if (corr.index_query >= (*input_normals_).size() || corr.index_match >= (*target_normals_).size()) + if ((size_t) corr.index_query >= (*input_normals_).size() || (size_t) corr.index_match >= (*target_normals_).size()) { return (std::numeric_limits::max ()); } From d16669557083257d3da2e1f7b25f79d55ae855e1 Mon Sep 17 00:00:00 2001 From: Franck HOUSSEN Date: Sun, 23 Jun 2024 10:14:51 +0200 Subject: [PATCH 3/3] [BUG FIX] Do not access nth[i] if nth is empty. --- ...orrespondence_rejection_median_distance.cpp | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/registration/src/correspondence_rejection_median_distance.cpp b/registration/src/correspondence_rejection_median_distance.cpp index 11913157bab..4fe4977d819 100644 --- a/registration/src/correspondence_rejection_median_distance.cpp +++ b/registration/src/correspondence_rejection_median_distance.cpp @@ -55,16 +55,18 @@ pcl::registration::CorrespondenceRejectorMedianDistance::getRemainingCorresponde dists[i] = original_correspondences[i].distance; } + unsigned int number_valid_correspondences = 0; std::vector nth(dists); - nth_element(nth.begin(), nth.begin() + (nth.size() / 2), nth.end()); - median_distance_ = nth[nth.size() / 2]; + if (!nth.empty()) { + nth_element(nth.begin(), nth.begin() + (nth.size() / 2), nth.end()); + median_distance_ = nth[nth.size() / 2]; - unsigned int number_valid_correspondences = 0; - remaining_correspondences.resize(original_correspondences.size()); + remaining_correspondences.resize(original_correspondences.size()); - for (std::size_t i = 0; i < original_correspondences.size(); ++i) - if (dists[i] <= median_distance_ * factor_) - remaining_correspondences[number_valid_correspondences++] = - original_correspondences[i]; + for (std::size_t i = 0; i < original_correspondences.size(); ++i) + if (dists[i] <= median_distance_ * factor_) + remaining_correspondences[number_valid_correspondences++] = + original_correspondences[i]; + } remaining_correspondences.resize(number_valid_correspondences); }