Point Cloud Library (PCL) 1.15.1-dev
Loading...
Searching...
No Matches
ia_ransac.h
1/*
2 * Software License Agreement (BSD License)
3 *
4 * Point Cloud Library (PCL) - www.pointclouds.org
5 * Copyright (c) 2010-2012, Willow Garage, Inc.
6 * Copyright (c) 2012-, Open Perception, Inc.
7 *
8 * All rights reserved.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 *
14 * * Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * * Redistributions in binary form must reproduce the above
17 * copyright notice, this list of conditions and the following
18 * disclaimer in the documentation and/or other materials provided
19 * with the distribution.
20 * * Neither the name of the copyright holder(s) nor the names of its
21 * contributors may be used to endorse or promote products derived
22 * from this software without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
28 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
29 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
30 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
34 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 * POSSIBILITY OF SUCH DAMAGE.
36 *
37 * $Id$
38 *
39 */
40
41#pragma once
42
43#include <pcl/registration/registration.h>
44#include <pcl/registration/transformation_estimation_svd.h>
45#include <pcl/memory.h>
46
47namespace pcl {
48/** \brief @b SampleConsensusInitialAlignment is an implementation of the initial
49 * alignment algorithm described in section IV of "Fast Point Feature Histograms (FPFH)
50 * for 3D Registration," Rusu et al. \author Michael Dixon, Radu B. Rusu
51 * \ingroup registration
52 */
53template <typename PointSource, typename PointTarget, typename FeatureT>
54class SampleConsensusInitialAlignment : public Registration<PointSource, PointTarget> {
55public:
56 using Registration<PointSource, PointTarget>::reg_name_;
57 using Registration<PointSource, PointTarget>::input_;
58 using Registration<PointSource, PointTarget>::indices_;
59 using Registration<PointSource, PointTarget>::target_;
60 using Registration<PointSource, PointTarget>::final_transformation_;
61 using Registration<PointSource, PointTarget>::transformation_;
62 using Registration<PointSource, PointTarget>::corr_dist_threshold_;
63 using Registration<PointSource, PointTarget>::min_number_correspondences_;
64 using Registration<PointSource, PointTarget>::max_iterations_;
65 using Registration<PointSource, PointTarget>::tree_;
66 using Registration<PointSource, PointTarget>::transformation_estimation_;
67 using Registration<PointSource, PointTarget>::converged_;
68 using Registration<PointSource, PointTarget>::getClassName;
69
72 using PointCloudSourcePtr = typename PointCloudSource::Ptr;
73 using PointCloudSourceConstPtr = typename PointCloudSource::ConstPtr;
74
77
80
84
85 using Ptr =
86 shared_ptr<SampleConsensusInitialAlignment<PointSource, PointTarget, FeatureT>>;
87 using ConstPtr = shared_ptr<
89
91 public:
92 using Ptr = shared_ptr<ErrorFunctor>;
93 using ConstPtr = shared_ptr<const ErrorFunctor>;
94
95 virtual ~ErrorFunctor() = default;
96 virtual float
97 operator()(float d) const = 0;
98 };
99
100 class HuberPenalty : public ErrorFunctor {
101 private:
102 HuberPenalty() = default;
103
104 public:
105 HuberPenalty(float threshold) : threshold_(threshold) {}
106 float
107 operator()(float e) const override
108 {
109 if (e <= threshold_)
110 return (0.5 * e * e);
111 return (0.5 * threshold_ * (2.0 * std::fabs(e) - threshold_));
112 }
113
114 protected:
115 float threshold_{0.0f};
116 };
117
119 private:
120 TruncatedError() = default;
121
122 public:
123 ~TruncatedError() override = default;
124
125 TruncatedError(float threshold) : threshold_(threshold) {}
126 float
127 operator()(float e) const override
128 {
129 if (e <= threshold_)
130 return (e / threshold_);
131 return (1.0);
132 }
133
134 protected:
135 float threshold_{0.0f};
136 };
137
139
141 /** \brief Constructor. */
144 {
145 reg_name_ = "SampleConsensusInitialAlignment";
146 max_iterations_ = 1000;
147
148 // Setting a non-std::numeric_limits<double>::max () value to corr_dist_threshold_
149 // to make it play nicely with TruncatedError
150 corr_dist_threshold_ = 100.0f;
154 };
155
156 /** \brief Provide a shared pointer to the source point cloud's feature descriptors
157 * \param features the source point cloud's features
158 */
159 void
161
162 /** \brief Get a pointer to the source point cloud's features */
163 inline FeatureCloudConstPtr const
165 {
166 return (input_features_);
167 }
168
169 /** \brief Provide a shared pointer to the target point cloud's feature descriptors
170 * \param features the target point cloud's features
171 */
172 void
174
175 /** \brief Get a pointer to the target point cloud's features */
176 inline FeatureCloudConstPtr const
178 {
179 return (target_features_);
180 }
181
182 /** \brief Set the minimum distances between samples
183 * \param min_sample_distance the minimum distances between samples
184 */
185 void
186 setMinSampleDistance(float min_sample_distance)
187 {
188 min_sample_distance_ = min_sample_distance;
189 }
190
191 /** \brief Get the minimum distances between samples, as set by the user */
192 float
194 {
195 return (min_sample_distance_);
196 }
197
198 /** \brief Set the number of samples to use during each iteration
199 * \param nr_samples the number of samples to use during each iteration
200 */
201 void
202 setNumberOfSamples(int nr_samples)
203 {
204 nr_samples_ = nr_samples;
205 }
206
207 /** \brief Get the number of samples to use during each iteration, as set by the user
208 */
209 int
211 {
212 return (nr_samples_);
213 }
214
215 /** \brief Set the number of neighbors to use when selecting a random feature
216 * correspondence. A higher value will add more randomness to the feature matching.
217 * \param k the number of neighbors to use when selecting a random feature
218 * correspondence.
219 */
220 void
225
226 /** \brief Get the number of neighbors used when selecting a random feature
227 * correspondence, as set by the user */
228 int
233
234 /** \brief Initialize the scheduler and set the number of threads to use.
235 * \param nr_threads the number of hardware threads to use (0 sets the value back to
236 * automatic)
237 */
238 void
239 setNumberOfThreads(unsigned int nr_threads = 0);
240
241 /** \brief Specify the error function to minimize
242 * \note This call is optional. TruncatedError will be used by default
243 * \param[in] error_functor a shared pointer to a subclass of
244 * SampleConsensusInitialAlignment::ErrorFunctor
245 */
246 void
247 setErrorFunction(const ErrorFunctorPtr& error_functor)
248 {
249 error_functor_ = error_functor;
250 }
251
252 /** \brief Get a shared pointer to the ErrorFunctor that is to be minimized
253 * \return A shared pointer to a subclass of
254 * SampleConsensusInitialAlignment::ErrorFunctor
255 */
258 {
259 return (error_functor_);
260 }
261
262protected:
263 /** \brief Choose a random index between 0 and n-1
264 * \param n the number of possible indices to choose from
265 */
266 inline pcl::index_t
268 {
269 return (static_cast<pcl::index_t>(n * (rand() / (RAND_MAX + 1.0))));
270 };
271
272 /** \brief Select \a nr_samples sample points from cloud while making sure that their
273 * pairwise distances are greater than a user-defined minimum distance, \a
274 * min_sample_distance. \param cloud the input point cloud \param nr_samples the
275 * number of samples to select \param min_sample_distance the minimum distance between
276 * any two samples \param sample_indices the resulting sample indices
277 */
278 void
279 selectSamples(const PointCloudSource& cloud,
280 unsigned int nr_samples,
281 float min_sample_distance,
282 pcl::Indices& sample_indices);
283
284 /** \brief For each of the sample points, find a list of points in the target cloud
285 * whose features are similar to the sample points' features. From these, select one
286 * randomly which will be considered that sample point's correspondence. \param
287 * input_features a cloud of feature descriptors \param sample_indices the indices of
288 * each sample point \param corresponding_indices the resulting indices of each
289 * sample's corresponding point in the target cloud
290 */
291 void
292 findSimilarFeatures(const FeatureCloud& input_features,
293 const pcl::Indices& sample_indices,
294 pcl::Indices& corresponding_indices);
295
296 /** \brief An error metric for that computes the quality of the alignment between the
297 * given cloud and the target. \param cloud the input cloud \param threshold distances
298 * greater than this value are capped
299 */
300 float
301 computeErrorMetric(const PointCloudSource& cloud, float threshold);
302
303 /** \brief Rigid transformation computation method.
304 * \param output the transformed input point cloud dataset using the rigid
305 * transformation found \param guess The computed transforamtion
306 */
307 void
309 const Eigen::Matrix4f& guess) override;
310
311 /** \brief The source point cloud's feature descriptors. */
313
314 /** \brief The target point cloud's feature descriptors. */
316
317 /** \brief The number of samples to use during each iteration. */
319
320 /** \brief The minimum distances between samples. */
322
323 /** \brief The number of neighbors to use when selecting a random feature
324 * correspondence. */
326
327 /** \brief The number of threads the scheduler should use. */
328 unsigned int threads_{1};
329
330 /** \brief The KdTree used to compare feature descriptors. */
332
334
335public:
337};
338} // namespace pcl
339
340#include <pcl/registration/impl/ia_ransac.hpp>
PointCloudConstPtr input_
The input point cloud dataset.
Definition pcl_base.h:147
IndicesPtr indices_
A pointer to the vector of point indices to use.
Definition pcl_base.h:150
shared_ptr< PointCloud< FeatureT > > Ptr
shared_ptr< const PointCloud< FeatureT > > ConstPtr
Registration represents the base registration class for general purpose, ICP-like methods.
Matrix4 final_transformation_
The final transformation matrix estimated by the registration method after N iterations.
double corr_dist_threshold_
The maximum distance threshold between two correspondent points in source <-> target.
std::string reg_name_
The registration method name.
Matrix4 transformation_
The transformation matrix estimated by the registration method.
KdTreePtr tree_
A pointer to the spatial search object.
bool converged_
Holds internal convergence state, given user parameters.
TransformationEstimationPtr transformation_estimation_
A TransformationEstimation object, used to calculate the 4x4 rigid transformation.
int max_iterations_
The maximum number of iterations the internal optimization should run for.
unsigned int min_number_correspondences_
The minimum number of correspondences that the algorithm needs before attempting to estimate the tran...
PointCloudTargetConstPtr target_
The input point cloud dataset target.
const std::string & getClassName() const
Abstract class get name method.
virtual float operator()(float d) const =0
shared_ptr< const ErrorFunctor > ConstPtr
Definition ia_ransac.h:93
SampleConsensusInitialAlignment is an implementation of the initial alignment algorithm described in ...
Definition ia_ransac.h:54
FeatureCloudConstPtr const getTargetFeatures()
Get a pointer to the target point cloud's features.
Definition ia_ransac.h:177
void setNumberOfThreads(unsigned int nr_threads=0)
Initialize the scheduler and set the number of threads to use.
Definition ia_ransac.hpp:51
PointIndices::ConstPtr PointIndicesConstPtr
Definition ia_ransac.h:79
void computeTransformation(PointCloudSource &output, const Eigen::Matrix4f &guess) override
Rigid transformation computation method.
shared_ptr< const SampleConsensusInitialAlignment< PointSource, PointTarget, FeatureT > > ConstPtr
Definition ia_ransac.h:88
int getNumberOfSamples()
Get the number of samples to use during each iteration, as set by the user.
Definition ia_ransac.h:210
float computeErrorMetric(const PointCloudSource &cloud, float threshold)
An error metric for that computes the quality of the alignment between the given cloud and the target...
typename PointCloudSource::ConstPtr PointCloudSourceConstPtr
Definition ia_ransac.h:73
void setMinSampleDistance(float min_sample_distance)
Set the minimum distances between samples.
Definition ia_ransac.h:186
typename Registration< PointSource, PointTarget >::PointCloudSource PointCloudSource
Definition ia_ransac.h:71
FeatureKdTreePtr feature_tree_
The KdTree used to compare feature descriptors.
Definition ia_ransac.h:331
pcl::index_t getRandomIndex(int n)
Choose a random index between 0 and n-1.
Definition ia_ransac.h:267
pcl::PointCloud< FeatureT > FeatureCloud
Definition ia_ransac.h:81
void setTargetFeatures(const FeatureCloudConstPtr &features)
Provide a shared pointer to the target point cloud's feature descriptors.
Definition ia_ransac.hpp:87
void setNumberOfSamples(int nr_samples)
Set the number of samples to use during each iteration.
Definition ia_ransac.h:202
FeatureCloudConstPtr target_features_
The target point cloud's feature descriptors.
Definition ia_ransac.h:315
float getMinSampleDistance()
Get the minimum distances between samples, as set by the user.
Definition ia_ransac.h:193
shared_ptr< SampleConsensusInitialAlignment< PointSource, PointTarget, FeatureT > > Ptr
Definition ia_ransac.h:86
float min_sample_distance_
The minimum distances between samples.
Definition ia_ransac.h:321
typename ErrorFunctor::Ptr ErrorFunctorPtr
Definition ia_ransac.h:138
unsigned int threads_
The number of threads the scheduler should use.
Definition ia_ransac.h:328
typename pcl::search::Search< FeatureT >::Ptr FeatureKdTreePtr
Definition ia_ransac.h:140
typename FeatureCloud::Ptr FeatureCloudPtr
Definition ia_ransac.h:82
void setErrorFunction(const ErrorFunctorPtr &error_functor)
Specify the error function to minimize.
Definition ia_ransac.h:247
FeatureCloudConstPtr input_features_
The source point cloud's feature descriptors.
Definition ia_ransac.h:312
int nr_samples_
The number of samples to use during each iteration.
Definition ia_ransac.h:318
void selectSamples(const PointCloudSource &cloud, unsigned int nr_samples, float min_sample_distance, pcl::Indices &sample_indices)
Select nr_samples sample points from cloud while making sure that their pairwise distances are greate...
typename PointCloudSource::Ptr PointCloudSourcePtr
Definition ia_ransac.h:72
int getCorrespondenceRandomness()
Get the number of neighbors used when selecting a random feature correspondence, as set by the user.
Definition ia_ransac.h:229
void findSimilarFeatures(const FeatureCloud &input_features, const pcl::Indices &sample_indices, pcl::Indices &corresponding_indices)
For each of the sample points, find a list of points in the target cloud whose features are similar t...
void setCorrespondenceRandomness(int k)
Set the number of neighbors to use when selecting a random feature correspondence.
Definition ia_ransac.h:221
typename FeatureCloud::ConstPtr FeatureCloudConstPtr
Definition ia_ransac.h:83
SampleConsensusInitialAlignment()
Constructor.
Definition ia_ransac.h:142
int k_correspondences_
The number of neighbors to use when selecting a random feature correspondence.
Definition ia_ransac.h:325
ErrorFunctorPtr getErrorFunction()
Get a shared pointer to the ErrorFunctor that is to be minimized.
Definition ia_ransac.h:257
FeatureCloudConstPtr const getSourceFeatures()
Get a pointer to the source point cloud's features.
Definition ia_ransac.h:164
typename Registration< PointSource, PointTarget >::PointCloudTarget PointCloudTarget
Definition ia_ransac.h:76
void setSourceFeatures(const FeatureCloudConstPtr &features)
Provide a shared pointer to the source point cloud's feature descriptors.
Definition ia_ransac.hpp:73
TransformationEstimationSVD implements SVD-based estimation of the transformation aligning the given ...
shared_ptr< pcl::search::Search< PointT > > Ptr
Definition search.h:81
#define PCL_MAKE_ALIGNED_OPERATOR_NEW
Macro to signal a class requires a custom allocator.
Definition memory.h:86
Defines functions, macros and traits for allocating and using memory.
detail::int_type_t< detail::index_type_size, detail::index_type_signed > index_t
Type used for an index in PCL.
Definition types.h:112
IndicesAllocator<> Indices
Type used for indices in PCL.
Definition types.h:133
shared_ptr< ::pcl::PointIndices > Ptr
shared_ptr< const ::pcl::PointIndices > ConstPtr