From 35fb6248187d922870a05d330151204e31751c2d Mon Sep 17 00:00:00 2001 From: Kunal Tyagi Date: Fri, 17 Jan 2020 01:03:15 +0900 Subject: [PATCH] Replace raw arrays with `std::vector`s in `RangeImageBorderExtractor` (#3533) Replace raw arrays with std::vectors in RangeImageBorderExtractor --- .../features/range_image_border_extractor.h | 15 +++-- features/src/range_image_border_extractor.cpp | 60 +++++++++---------- 2 files changed, 39 insertions(+), 36 deletions(-) diff --git a/features/include/pcl/features/range_image_border_extractor.h b/features/include/pcl/features/range_image_border_extractor.h index 1dd2a18a941..ed4bc0415ce 100644 --- a/features/include/pcl/features/range_image_border_extractor.h +++ b/features/include/pcl/features/range_image_border_extractor.h @@ -147,16 +147,16 @@ namespace pcl getRangeImage () const { return *range_image_; } float* - getBorderScoresLeft () { extractBorderScoreImages (); return border_scores_left_; } + getBorderScoresLeft () { extractBorderScoreImages (); return border_scores_left_.data (); } float* - getBorderScoresRight () { extractBorderScoreImages (); return border_scores_right_; } + getBorderScoresRight () { extractBorderScoreImages (); return border_scores_right_.data (); } float* - getBorderScoresTop () { extractBorderScoreImages (); return border_scores_top_; } + getBorderScoresTop () { extractBorderScoreImages (); return border_scores_top_.data (); } float* - getBorderScoresBottom () { extractBorderScoreImages (); return border_scores_bottom_; } + getBorderScoresBottom () { extractBorderScoreImages (); return border_scores_bottom_.data (); } LocalSurface** getSurfaceStructure () { extractLocalSurfaceStructure (); return surface_structure_; } @@ -182,7 +182,8 @@ namespace pcl Parameters parameters_; const RangeImage* range_image_; int range_image_size_during_extraction_; - float* border_scores_left_, * border_scores_right_, * border_scores_top_, * border_scores_bottom_; + std::vector border_scores_left_, border_scores_right_; + std::vector border_scores_top_, border_scores_bottom_; LocalSurface** surface_structure_; PointCloudOut* border_descriptions_; ShadowBorderIndices** shadow_border_informations_; @@ -347,6 +348,10 @@ namespace pcl /** \brief Implementation of abstract derived function */ void computeFeature (PointCloudOut &output) override; + + private: + std::vector + updatedScoresAccordingToNeighborValues (const std::vector& border_scores) const; }; } // namespace end diff --git a/features/src/range_image_border_extractor.cpp b/features/src/range_image_border_extractor.cpp index a2b9b19770d..287a0146997 100644 --- a/features/src/range_image_border_extractor.cpp +++ b/features/src/range_image_border_extractor.cpp @@ -55,7 +55,6 @@ namespace pcl /////////////////////////////////////////////////////////////////////////////////////////////////////////////// RangeImageBorderExtractor::RangeImageBorderExtractor(const RangeImage* range_image) : range_image_(range_image), range_image_size_during_extraction_(0), - border_scores_left_(nullptr), border_scores_right_(nullptr), border_scores_top_(nullptr), border_scores_bottom_(nullptr), surface_structure_(nullptr), border_descriptions_(nullptr), shadow_border_informations_(nullptr), border_directions_(nullptr), surface_change_scores_(nullptr), surface_change_directions_(nullptr) { @@ -79,10 +78,6 @@ RangeImageBorderExtractor::setRangeImage (const RangeImage* range_image) void RangeImageBorderExtractor::clearData () { - delete[] border_scores_left_; border_scores_left_ = nullptr; - delete[] border_scores_right_; border_scores_right_ = nullptr; - delete[] border_scores_top_; border_scores_top_ = nullptr; - delete[] border_scores_bottom_; border_scores_bottom_ = nullptr; //std::cout << PVARC(range_image_size_during_extraction_)<width, height = range_image_->height, size = width*height; - border_scores_left_ = new float[size]; - border_scores_right_ = new float[size]; - border_scores_top_ = new float[size]; - border_scores_bottom_ = new float[size]; + border_scores_left_.resize (size); + border_scores_right_.resize (size); + border_scores_top_.resize (size); + border_scores_bottom_ .resize (size); # pragma omp parallel for num_threads(parameters_.max_no_of_threads) default(shared) schedule(dynamic, 10) for (int y=0; y +RangeImageBorderExtractor::updatedScoresAccordingToNeighborValues (const std::vector& border_scores) const +{ + std::vector new_border_scores; + new_border_scores.reserve (range_image_->width*range_image_->height); + for (int y=0; y < static_cast (range_image_->height); ++y) + for (int x=0; x < static_cast (range_image_->width); ++x) + new_border_scores.push_back (updatedScoreAccordingToNeighborValues(x, y, border_scores.data ())); + return new_border_scores; +} + /////////////////////////////////////////////////////////////////////////////////////////////////////////////// void RangeImageBorderExtractor::updateScoresAccordingToNeighborValues () @@ -210,18 +216,10 @@ RangeImageBorderExtractor::updateScoresAccordingToNeighborValues () //MEASURE_FUNCTION_TIME; - float* left_with_propagated_neighbors = updatedScoresAccordingToNeighborValues(border_scores_left_); - delete[] border_scores_left_; - border_scores_left_ = left_with_propagated_neighbors; - float* right_with_propagated_neighbors = updatedScoresAccordingToNeighborValues(border_scores_right_); - delete[] border_scores_right_; - border_scores_right_ = right_with_propagated_neighbors; - float* top_with_propagated_neighbors = updatedScoresAccordingToNeighborValues(border_scores_top_); - delete[] border_scores_top_; - border_scores_top_ = top_with_propagated_neighbors; - float* bottom_with_propagated_neighbors = updatedScoresAccordingToNeighborValues(border_scores_bottom_); - delete[] border_scores_bottom_; - border_scores_bottom_ = bottom_with_propagated_neighbors; + border_scores_left_ = updatedScoresAccordingToNeighborValues(border_scores_left_); + border_scores_right_ = updatedScoresAccordingToNeighborValues(border_scores_right_); + border_scores_top_ = updatedScoresAccordingToNeighborValues(border_scores_top_); + border_scores_bottom_ = updatedScoresAccordingToNeighborValues(border_scores_bottom_); } /////////////////////////////////////////////////////////////////////////////////////////////////////////////// @@ -231,7 +229,7 @@ RangeImageBorderExtractor::findAndEvaluateShadowBorders () if (shadow_border_informations_ != nullptr) return; - if (border_scores_left_==nullptr) + if (border_scores_left_.empty ()) { std::cerr << __PRETTY_FUNCTION__<<": border score images not available!\n"; } @@ -250,22 +248,22 @@ RangeImageBorderExtractor::findAndEvaluateShadowBorders () shadow_border_indices = nullptr; int shadow_border_idx; - if (changeScoreAccordingToShadowBorderValue(x, y, -1, 0, border_scores_left_, border_scores_right_, shadow_border_idx)) + if (changeScoreAccordingToShadowBorderValue(x, y, -1, 0, border_scores_left_.data (), border_scores_right_.data (), shadow_border_idx)) { shadow_border_indices = (shadow_border_indices==nullptr ? new ShadowBorderIndices : shadow_border_indices); shadow_border_indices->left = shadow_border_idx; } - if (changeScoreAccordingToShadowBorderValue(x, y, 1, 0, border_scores_right_, border_scores_left_, shadow_border_idx)) + if (changeScoreAccordingToShadowBorderValue(x, y, 1, 0, border_scores_right_.data (), border_scores_left_.data (), shadow_border_idx)) { shadow_border_indices = (shadow_border_indices==nullptr ? new ShadowBorderIndices : shadow_border_indices); shadow_border_indices->right = shadow_border_idx; } - if (changeScoreAccordingToShadowBorderValue(x, y, 0, -1, border_scores_top_, border_scores_bottom_, shadow_border_idx)) + if (changeScoreAccordingToShadowBorderValue(x, y, 0, -1, border_scores_top_.data (), border_scores_bottom_.data (), shadow_border_idx)) { shadow_border_indices = (shadow_border_indices==nullptr ? new ShadowBorderIndices : shadow_border_indices); shadow_border_indices->top = shadow_border_idx; } - if (changeScoreAccordingToShadowBorderValue(x, y, 0, 1, border_scores_bottom_, border_scores_top_, shadow_border_idx)) + if (changeScoreAccordingToShadowBorderValue(x, y, 0, 1, border_scores_bottom_.data (), border_scores_top_.data (), shadow_border_idx)) { shadow_border_indices = (shadow_border_indices==nullptr ? new ShadowBorderIndices : shadow_border_indices); shadow_border_indices->bottom = shadow_border_idx; @@ -397,7 +395,7 @@ RangeImageBorderExtractor::classifyBorders () continue; int shadow_border_index = shadow_border_indices->left; - if (shadow_border_index >= 0 && checkIfMaximum(x, y, -1, 0, border_scores_left_, shadow_border_index)) + if (shadow_border_index >= 0 && checkIfMaximum(x, y, -1, 0, border_scores_left_.data (), shadow_border_index)) { BorderTraits& shadow_traits = border_descriptions_->points[shadow_border_index].traits; border_traits[BORDER_TRAIT__OBSTACLE_BORDER] = border_traits[BORDER_TRAIT__OBSTACLE_BORDER_LEFT] = true; @@ -410,7 +408,7 @@ RangeImageBorderExtractor::classifyBorders () } shadow_border_index = shadow_border_indices->right; - if (shadow_border_index >= 0 && checkIfMaximum(x, y, 1, 0, border_scores_right_, shadow_border_index)) + if (shadow_border_index >= 0 && checkIfMaximum(x, y, 1, 0, border_scores_right_.data (), shadow_border_index)) { BorderTraits& shadow_traits = border_descriptions_->points[shadow_border_index].traits; border_traits[BORDER_TRAIT__OBSTACLE_BORDER] = border_traits[BORDER_TRAIT__OBSTACLE_BORDER_RIGHT] = true; @@ -423,7 +421,7 @@ RangeImageBorderExtractor::classifyBorders () } shadow_border_index = shadow_border_indices->top; - if (shadow_border_index >= 0 && checkIfMaximum(x, y, 0, -1, border_scores_top_, shadow_border_index)) + if (shadow_border_index >= 0 && checkIfMaximum(x, y, 0, -1, border_scores_top_.data (), shadow_border_index)) { BorderTraits& shadow_traits = border_descriptions_->points[shadow_border_index].traits; border_traits[BORDER_TRAIT__OBSTACLE_BORDER] = border_traits[BORDER_TRAIT__OBSTACLE_BORDER_TOP] = true; @@ -437,7 +435,7 @@ RangeImageBorderExtractor::classifyBorders () } shadow_border_index = shadow_border_indices->bottom; - if (shadow_border_index >= 0 && checkIfMaximum(x, y, 0, 1, border_scores_bottom_, shadow_border_index)) + if (shadow_border_index >= 0 && checkIfMaximum(x, y, 0, 1, border_scores_bottom_.data (), shadow_border_index)) { BorderTraits& shadow_traits = border_descriptions_->points[shadow_border_index].traits; border_traits[BORDER_TRAIT__OBSTACLE_BORDER] = border_traits[BORDER_TRAIT__OBSTACLE_BORDER_BOTTOM] = true;