Skip to content

Modernise manual memory management in range_image_border_extractor #3865

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 14 additions & 15 deletions features/include/pcl/features/range_image_border_extractor.h
Original file line number Diff line number Diff line change
Expand Up @@ -159,38 +159,37 @@ namespace pcl
getBorderScoresBottom () { extractBorderScoreImages (); return border_scores_bottom_.data (); }

LocalSurface**
getSurfaceStructure () { extractLocalSurfaceStructure (); return surface_structure_; }
getSurfaceStructure () { extractLocalSurfaceStructure (); return surface_structure_.get(); }

PointCloudOut&
getBorderDescriptions () { classifyBorders (); return *border_descriptions_; }
getBorderDescriptions() { classifyBorders(); return *border_descriptions_.get(); }

ShadowBorderIndices**
getShadowBorderInformations () { findAndEvaluateShadowBorders (); return shadow_border_informations_; }
getShadowBorderInformations() { findAndEvaluateShadowBorders(); return shadow_border_informations_.get(); }

Eigen::Vector3f**
getBorderDirections () { calculateBorderDirections (); return border_directions_; }
getBorderDirections() { calculateBorderDirections(); return border_directions_.get(); }

float*
getSurfaceChangeScores () { calculateSurfaceChanges (); return surface_change_scores_; }
getSurfaceChangeScores () { calculateSurfaceChanges (); return surface_change_scores_.data(); }

Eigen::Vector3f*
getSurfaceChangeDirections () { calculateSurfaceChanges (); return surface_change_directions_; }
getSurfaceChangeDirections() { calculateSurfaceChanges(); return surface_change_directions_.get(); }


protected:
// =====PROTECTED MEMBER VARIABLES=====
Parameters parameters_;
const RangeImage* range_image_;
int range_image_size_during_extraction_;
std::vector<float> border_scores_left_, border_scores_right_;
std::vector<float> border_scores_top_, border_scores_bottom_;
LocalSurface** surface_structure_;
PointCloudOut* border_descriptions_;
ShadowBorderIndices** shadow_border_informations_;
Eigen::Vector3f** border_directions_;

float* surface_change_scores_;
Eigen::Vector3f* surface_change_directions_;
std::unique_ptr<LocalSurface*[]> surface_structure_;
std::unique_ptr<PointCloudOut> border_descriptions_;
std::unique_ptr<ShadowBorderIndices*[]> shadow_border_informations_;
std::unique_ptr<Eigen::Vector3f*[]> border_directions_;
std::vector<float> surface_change_scores_;
std::unique_ptr<Eigen::Vector3f[]> surface_change_directions_;


// =====PROTECTED METHODS=====
Expand Down
41 changes: 20 additions & 21 deletions features/src/range_image_border_extractor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ namespace pcl
RangeImageBorderExtractor::RangeImageBorderExtractor(const RangeImage* range_image) :
range_image_(range_image), range_image_size_during_extraction_(0),
surface_structure_(nullptr), border_descriptions_(nullptr), shadow_border_informations_(nullptr), border_directions_(nullptr),
surface_change_scores_(nullptr), surface_change_directions_(nullptr)
surface_change_directions_(nullptr)
{
}

Expand Down Expand Up @@ -88,13 +88,13 @@ RangeImageBorderExtractor::clearData ()
if (border_directions_!=nullptr)
delete border_directions_[i];
}
delete[] surface_structure_; surface_structure_ = nullptr;
delete[] shadow_border_informations_; shadow_border_informations_ = nullptr;
delete[] border_directions_; border_directions_ = nullptr;
delete border_descriptions_; border_descriptions_ = nullptr;
surface_structure_.reset();
shadow_border_informations_.reset();
border_directions_.reset();
border_descriptions_.reset();

delete[] surface_change_scores_; surface_change_scores_ = nullptr;
delete[] surface_change_directions_; surface_change_directions_ = nullptr;
surface_change_directions_.reset();
surface_change_scores_.clear();
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////
Expand All @@ -110,7 +110,7 @@ RangeImageBorderExtractor::extractLocalSurfaceStructure ()
const auto height = range_image_->height;
range_image_size_during_extraction_ = width*height;
const auto array_size = range_image_size_during_extraction_;
surface_structure_ = new LocalSurface*[array_size];
surface_structure_ = std::make_unique<LocalSurface*[]>(array_size);
const auto step_size = std::max(1, parameters_.pixel_radius_plane_extraction/2);
//std::cout << PVARN(step_size);
const auto sqrt_neighbors = parameters_.pixel_radius_plane_extraction/step_size + 1;
Expand Down Expand Up @@ -257,7 +257,7 @@ RangeImageBorderExtractor::findAndEvaluateShadowBorders ()

int width = range_image_->width,
height = range_image_->height;
shadow_border_informations_ = new ShadowBorderIndices*[width*height];
shadow_border_informations_ = std::make_unique<ShadowBorderIndices*[]>(width * height);
for (int y = 0; y < static_cast<int> (height); ++y)
{
for (int x = 0; x < static_cast<int> (width); ++x)
Expand Down Expand Up @@ -393,7 +393,7 @@ RangeImageBorderExtractor::classifyBorders ()

BorderDescription initial_border_description;
initial_border_description.traits = 0;
border_descriptions_ = new PointCloudOut;
border_descriptions_ = std::make_unique<PointCloudOut>();
border_descriptions_->width = width;
border_descriptions_->height = height;
border_descriptions_->is_dense = true;
Expand Down Expand Up @@ -488,7 +488,7 @@ RangeImageBorderExtractor::calculateBorderDirections ()
int width = range_image_->width,
height = range_image_->height,
size = width*height;
border_directions_ = new Eigen::Vector3f*[size];
border_directions_ = std::make_unique<Eigen::Vector3f*[]>(size);
for (int y=0; y<height; ++y)
{
for (int x=0; x<width; ++x)
Expand All @@ -497,7 +497,7 @@ RangeImageBorderExtractor::calculateBorderDirections ()
}
}

Eigen::Vector3f** average_border_directions = new Eigen::Vector3f*[size];
std::unique_ptr<Eigen::Vector3f*[]> average_border_directions(std::make_unique<Eigen::Vector3f*[]>(size));
int radius = parameters_.pixel_radius_border_direction;
int minimum_weight = radius+1;
float min_cos_angle=std::cos(deg2rad(120.0f));
Expand Down Expand Up @@ -553,15 +553,14 @@ RangeImageBorderExtractor::calculateBorderDirections ()

for (int i=0; i<size; ++i)
delete border_directions_[i];
delete[] border_directions_;
border_directions_ = average_border_directions;
border_directions_ = std::move(average_border_directions);
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////
void
RangeImageBorderExtractor::calculateSurfaceChanges ()
{
if (surface_change_scores_!=nullptr)
if (!surface_change_scores_.empty())
return;

calculateBorderDirections();
Expand All @@ -571,8 +570,8 @@ RangeImageBorderExtractor::calculateSurfaceChanges ()
int width = range_image_->width,
height = range_image_->height,
size = width*height;
surface_change_scores_ = new float[size];
surface_change_directions_ = new Eigen::Vector3f[size];
surface_change_scores_.resize(size);
surface_change_directions_ = std::make_unique<Eigen::Vector3f[]>(size);
#pragma omp parallel for \
default(none) \
shared(height, width) \
Expand Down Expand Up @@ -619,7 +618,7 @@ RangeImageBorderExtractor::blurSurfaceChanges ()
const RangeImage& range_image = *range_image_;

Eigen::Vector3f* blurred_directions = new Eigen::Vector3f[range_image.width*range_image.height];
float* blurred_scores = new float[range_image.width*range_image.height];
std::vector<float> blurred_scores(range_image.width*range_image.height);
for (int y=0; y<int(range_image.height); ++y)
{
for (int x=0; x<int(range_image.width); ++x)
Expand Down Expand Up @@ -665,9 +664,9 @@ RangeImageBorderExtractor::blurSurfaceChanges ()
new_score /= counter;
}
}
delete[] surface_change_directions_;
surface_change_directions_ = blurred_directions;
delete[] surface_change_scores_;
surface_change_directions_.reset();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Swap and reset instead?

surface_change_directions_ = std::unique_ptr<Eigen::Vector3f[]>(std::move(blurred_directions));
surface_change_scores_.clear();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to clear

surface_change_scores_ = blurred_scores;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

std::move here

}

Expand Down
4 changes: 4 additions & 0 deletions test/features/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ if(BUILD_io)
FILES test_boundary_estimation.cpp
LINK_WITH pcl_gtest pcl_features pcl_io
ARGUMENTS "${PCL_SOURCE_DIR}/test/bun0.pcd")
PCL_ADD_TEST(feature_range_image_border_extraction test_range_image_border_extraction
FILES test_boundary_estimation.cpp
LINK_WITH pcl_gtest pcl_features pcl_io
ARGUMENTS "${PCL_SOURCE_DIR}/test/bun0.pcd")
PCL_ADD_TEST(feature_curvatures_estimation test_curvatures_estimation
FILES test_curvatures_estimation.cpp
LINK_WITH pcl_gtest pcl_features pcl_io
Expand Down
101 changes: 101 additions & 0 deletions test/features/test_range_image_border_extraction.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/*
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Save 40 lines using SPDX license

* Software License Agreement (BSD License)
*
* Point Cloud Library (PCL) - www.pointclouds.org
* Copyright (c) 2010-2012, Willow Garage, Inc.
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
* * Neither the name of the copyright holder(s) nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* $Id$
*
*/

#include <pcl/test/gtest.h>
#include <pcl/point_cloud.h>
#include <pcl/range_image/range_image.h>
#include <pcl/features/range_image_border_extractor.h>
#include <pcl/io/pcd_io.h>

using namespace pcl;
using namespace pcl::io;
using namespace std;

PointCloud<PointXYZ> cloud;
std::vector<int> indices;

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
TEST (PCL, RangeImageBorderExtract)
{
float noise_level = 0.0;
float min_range = 0.0f;
int border_size = 1;
pcl::RangeImage::Ptr range_image_ptr (new pcl::RangeImage);
pcl::RangeImage& range_image = *range_image_ptr;
range_image.createFromPointCloud (cloud, angular_resolution, pcl::deg2rad (360.0f), pcl::deg2rad (180.0f),
scene_sensor_pose, coordinate_frame, noise_level, min_range, border_size);
range_image.integrateFarRanges (far_ranges);
if (setUnseenToMaxRange)
range_image.setUnseenToMaxRange ();


pcl::RangeImageBorderExtractor border_extractor (&range_image);
pcl::PointCloud<pcl::BorderDescription> border_descriptions;
border_extractor.compute (border_descriptions);

printf("RangeImageBorderExtract Test Completed\n");

}

/* ---[ */
int
main (int argc, char** argv)
{
if (argc < 2)
{
std::cerr << "No test file given. Please download `bun0.pcd` and pass its path to the test." << std::endl;
return (-1);
}

if (loadPCDFile<PointXYZ> (argv[1], cloud) < 0)
{
std::cerr << "Failed to read test file. Please download `bun0.pcd` and pass its path to the test." << std::endl;
return (-1);
}

indices.resize (cloud.points.size ());
for (std::size_t i = 0; i < indices.size (); ++i)
indices[i] = static_cast<int> (i);

tree->setInputCloud (cloud.makeShared ());

testing::InitGoogleTest (&argc, argv);
return (RUN_ALL_TESTS ());
}
/* ]--- */