Point Cloud Library (PCL)  1.11.1-dev
sac_model_registration.h
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Point Cloud Library (PCL) - www.pointclouds.org
5  * Copyright (c) 2010-2011, 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/memory.h>
44 #include <pcl/pcl_macros.h>
45 #include <pcl/pcl_base.h>
46 #include <pcl/sample_consensus/eigen.h>
47 #include <pcl/sample_consensus/sac_model.h>
48 #include <pcl/sample_consensus/model_types.h>
49 #include <pcl/common/eigen.h>
50 #include <pcl/common/centroid.h>
51 #include <map>
52 #include <numeric> // for std::iota
53 
54 namespace pcl
55 {
56  /** \brief SampleConsensusModelRegistration defines a model for Point-To-Point registration outlier rejection.
57  * \author Radu Bogdan Rusu
58  * \ingroup sample_consensus
59  */
60  template <typename PointT>
62  {
63  public:
69 
73 
74  using Ptr = shared_ptr<SampleConsensusModelRegistration<PointT> >;
75  using ConstPtr = shared_ptr<const SampleConsensusModelRegistration<PointT>>;
76 
77  /** \brief Constructor for base SampleConsensusModelRegistration.
78  * \param[in] cloud the input point cloud dataset
79  * \param[in] random if true set the random seed to the current time, else set to 12345 (default: false)
80  */
82  bool random = false)
83  : SampleConsensusModel<PointT> (cloud, random)
84  , target_ ()
86  {
87  // Call our own setInputCloud
88  setInputCloud (cloud);
89  model_name_ = "SampleConsensusModelRegistration";
90  sample_size_ = 3;
91  model_size_ = 16;
92  }
93 
94  /** \brief Constructor for base SampleConsensusModelRegistration.
95  * \param[in] cloud the input point cloud dataset
96  * \param[in] indices a vector of point indices to be used from \a cloud
97  * \param[in] random if true set the random seed to the current time, else set to 12345 (default: false)
98  */
100  const Indices &indices,
101  bool random = false)
102  : SampleConsensusModel<PointT> (cloud, indices, random)
103  , target_ ()
104  , sample_dist_thresh_ (0)
105  {
107  computeSampleDistanceThreshold (cloud, indices);
108  model_name_ = "SampleConsensusModelRegistration";
109  sample_size_ = 3;
110  model_size_ = 16;
111  }
112 
113  /** \brief Empty destructor */
115 
116  /** \brief Provide a pointer to the input dataset
117  * \param[in] cloud the const boost shared pointer to a PointCloud message
118  */
119  inline void
120  setInputCloud (const PointCloudConstPtr &cloud) override
121  {
125  }
126 
127  /** \brief Set the input point cloud target.
128  * \param[in] target the input point cloud target
129  */
130  inline void
132  {
133  target_ = target;
134  // Cache the size and fill the target indices
135  const index_t target_size = static_cast<index_t> (target->size ());
136  indices_tgt_.reset (new Indices (target_size));
137  std::iota (indices_tgt_->begin (), indices_tgt_->end (), 0);
139  }
140 
141  /** \brief Set the input point cloud target.
142  * \param[in] target the input point cloud target
143  * \param[in] indices_tgt a vector of point indices to be used from \a target
144  */
145  inline void
146  setInputTarget (const PointCloudConstPtr &target, const Indices &indices_tgt)
147  {
148  target_ = target;
149  indices_tgt_.reset (new Indices (indices_tgt));
151  }
152 
153  /** \brief Compute a 4x4 rigid transformation matrix from the samples given
154  * \param[in] samples the indices found as good candidates for creating a valid model
155  * \param[out] model_coefficients the resultant model coefficients
156  */
157  bool
158  computeModelCoefficients (const Indices &samples,
159  Eigen::VectorXf &model_coefficients) const override;
160 
161  /** \brief Compute all distances from the transformed points to their correspondences
162  * \param[in] model_coefficients the 4x4 transformation matrix
163  * \param[out] distances the resultant estimated distances
164  */
165  void
166  getDistancesToModel (const Eigen::VectorXf &model_coefficients,
167  std::vector<double> &distances) const override;
168 
169  /** \brief Select all the points which respect the given model coefficients as inliers.
170  * \param[in] model_coefficients the 4x4 transformation matrix
171  * \param[in] threshold a maximum admissible distance threshold for determining the inliers from the outliers
172  * \param[out] inliers the resultant model inliers
173  */
174  void
175  selectWithinDistance (const Eigen::VectorXf &model_coefficients,
176  const double threshold,
177  Indices &inliers) override;
178 
179  /** \brief Count all the points which respect the given model coefficients as inliers.
180  *
181  * \param[in] model_coefficients the coefficients of a model that we need to compute distances to
182  * \param[in] threshold maximum admissible distance threshold for determining the inliers from the outliers
183  * \return the resultant number of inliers
184  */
185  std::size_t
186  countWithinDistance (const Eigen::VectorXf &model_coefficients,
187  const double threshold) const override;
188 
189  /** \brief Recompute the 4x4 transformation using the given inlier set
190  * \param[in] inliers the data inliers found as supporting the model
191  * \param[in] model_coefficients the initial guess for the optimization
192  * \param[out] optimized_coefficients the resultant recomputed transformation
193  */
194  void
195  optimizeModelCoefficients (const Indices &inliers,
196  const Eigen::VectorXf &model_coefficients,
197  Eigen::VectorXf &optimized_coefficients) const override;
198 
199  void
201  const Eigen::VectorXf &,
202  PointCloud &, bool = true) const override
203  {
204  };
205 
206  bool
207  doSamplesVerifyModel (const std::set<index_t> &,
208  const Eigen::VectorXf &,
209  const double) const override
210  {
211  return (false);
212  }
213 
214  /** \brief Return a unique id for this model (SACMODEL_REGISTRATION). */
215  inline pcl::SacModel
216  getModelType () const override { return (SACMODEL_REGISTRATION); }
217 
218  protected:
221 
222  /** \brief Check if a sample of indices results in a good sample of points
223  * indices.
224  * \param[in] samples the resultant index samples
225  */
226  bool
227  isSampleGood (const Indices &samples) const override;
228 
229  /** \brief Computes an "optimal" sample distance threshold based on the
230  * principal directions of the input cloud.
231  * \param[in] cloud the const boost shared pointer to a PointCloud message
232  */
233  inline void
235  {
236  // Compute the principal directions via PCA
237  Eigen::Vector4f xyz_centroid;
238  Eigen::Matrix3f covariance_matrix = Eigen::Matrix3f::Zero ();
239 
240  computeMeanAndCovarianceMatrix (*cloud, covariance_matrix, xyz_centroid);
241 
242  // Check if the covariance matrix is finite or not.
243  for (int i = 0; i < 3; ++i)
244  for (int j = 0; j < 3; ++j)
245  if (!std::isfinite (covariance_matrix.coeffRef (i, j)))
246  PCL_ERROR ("[pcl::SampleConsensusModelRegistration::computeSampleDistanceThreshold] Covariance matrix has NaN values! Is the input cloud finite?\n");
247 
248  Eigen::Vector3f eigen_values;
249  pcl::eigen33 (covariance_matrix, eigen_values);
250 
251  // Compute the distance threshold for sample selection
252  sample_dist_thresh_ = eigen_values.array ().sqrt ().sum () / 3.0;
254  PCL_DEBUG ("[pcl::SampleConsensusModelRegistration::setInputCloud] Estimated a sample selection distance threshold of: %f\n", sample_dist_thresh_);
255  }
256 
257  /** \brief Computes an "optimal" sample distance threshold based on the
258  * principal directions of the input cloud.
259  * \param[in] cloud the const boost shared pointer to a PointCloud message
260  * \param indices
261  */
262  inline void
264  const Indices &indices)
265  {
266  // Compute the principal directions via PCA
267  Eigen::Vector4f xyz_centroid;
268  Eigen::Matrix3f covariance_matrix;
269  computeMeanAndCovarianceMatrix (*cloud, indices, covariance_matrix, xyz_centroid);
270 
271  // Check if the covariance matrix is finite or not.
272  for (int i = 0; i < 3; ++i)
273  for (int j = 0; j < 3; ++j)
274  if (!std::isfinite (covariance_matrix.coeffRef (i, j)))
275  PCL_ERROR ("[pcl::SampleConsensusModelRegistration::computeSampleDistanceThreshold] Covariance matrix has NaN values! Is the input cloud finite?\n");
276 
277  Eigen::Vector3f eigen_values;
278  pcl::eigen33 (covariance_matrix, eigen_values);
279 
280  // Compute the distance threshold for sample selection
281  sample_dist_thresh_ = eigen_values.array ().sqrt ().sum () / 3.0;
283  PCL_DEBUG ("[pcl::SampleConsensusModelRegistration::setInputCloud] Estimated a sample selection distance threshold of: %f\n", sample_dist_thresh_);
284  }
285 
286  /** \brief Estimate a rigid transformation between a source and a target point cloud using an SVD closed-form
287  * solution of absolute orientation using unit quaternions
288  * \param[in] cloud_src the source point cloud dataset
289  * \param[in] indices_src the vector of indices describing the points of interest in cloud_src
290  * \param[in] cloud_tgt the target point cloud dataset
291  * \param[in] indices_tgt the vector of indices describing the correspondences of the interest points from
292  * indices_src
293  * \param[out] transform the resultant transformation matrix (as model coefficients)
294  *
295  * This method is an implementation of: Horn, B. “Closed-Form Solution of Absolute Orientation Using Unit Quaternions,” JOSA A, Vol. 4, No. 4, 1987
296  */
297  void
299  const Indices &indices_src,
300  const pcl::PointCloud<PointT> &cloud_tgt,
301  const Indices &indices_tgt,
302  Eigen::VectorXf &transform) const;
303 
304  /** \brief Compute mappings between original indices of the input_/target_ clouds. */
305  void
307  {
308  if (!indices_tgt_)
309  {
310  PCL_DEBUG ("[pcl::SampleConsensusModelRegistration::computeOriginalIndexMapping] Cannot compute mapping: indices_tgt_ is null.\n");
311  return;
312  }
313  if (!indices_)
314  {
315  PCL_DEBUG ("[pcl::SampleConsensusModelRegistration::computeOriginalIndexMapping] Cannot compute mapping: indices_ is null.\n");
316  return;
317  }
318  if (indices_->empty ())
319  {
320  PCL_DEBUG ("[pcl::SampleConsensusModelRegistration::computeOriginalIndexMapping] Cannot compute mapping: indices_ is empty.\n");
321  return;
322  }
323  if (indices_->size () != indices_tgt_->size ())
324  {
325  PCL_DEBUG ("[pcl::SampleConsensusModelRegistration::computeOriginalIndexMapping] Cannot compute mapping: indices_ and indices_tgt_ are not the same size (%zu vs %zu).\n",
326  indices_->size (), indices_tgt_->size ());
327  return;
328  }
329  for (std::size_t i = 0; i < indices_->size (); ++i)
330  correspondences_[(*indices_)[i]] = (*indices_tgt_)[i];
331  PCL_DEBUG ("[pcl::SampleConsensusModelRegistration::computeOriginalIndexMapping] Successfully computed mapping.\n");
332  }
333 
334  /** \brief A boost shared pointer to the target point cloud data array. */
336 
337  /** \brief A pointer to the vector of target point indices to use. */
339 
340  /** \brief Given the index in the original point cloud, give the matching original index in the target cloud */
341  std::map<index_t, index_t> correspondences_;
342 
343  /** \brief Internal distance threshold used for the sample selection step. */
345  public:
347  };
348 }
349 
350 #include <pcl/sample_consensus/impl/sac_model_registration.hpp>
pcl::computeMeanAndCovarianceMatrix
unsigned int computeMeanAndCovarianceMatrix(const pcl::PointCloud< PointT > &cloud, Eigen::Matrix< Scalar, 3, 3 > &covariance_matrix, Eigen::Matrix< Scalar, 4, 1 > &centroid)
Compute the normalized 3x3 covariance matrix and the centroid of a given set of points in a single lo...
Definition: centroid.hpp:485
pcl::SampleConsensusModelRegistration::sample_dist_thresh_
double sample_dist_thresh_
Internal distance threshold used for the sample selection step.
Definition: sac_model_registration.h:344
pcl_macros.h
Defines all the PCL and non-PCL macros used.
pcl
Definition: convolution.h:46
pcl::IndicesPtr
shared_ptr< Indices > IndicesPtr
Definition: pcl_base.h:58
pcl::SampleConsensusModelRegistration::estimateRigidTransformationSVD
void estimateRigidTransformationSVD(const pcl::PointCloud< PointT > &cloud_src, const Indices &indices_src, const pcl::PointCloud< PointT > &cloud_tgt, const Indices &indices_tgt, Eigen::VectorXf &transform) const
Estimate a rigid transformation between a source and a target point cloud using an SVD closed-form so...
Definition: sac_model_registration.hpp:289
pcl::SampleConsensusModelRegistration::setInputCloud
void setInputCloud(const PointCloudConstPtr &cloud) override
Provide a pointer to the input dataset.
Definition: sac_model_registration.h:120
pcl::SampleConsensusModelRegistration::getModelType
pcl::SacModel getModelType() const override
Return a unique id for this model (SACMODEL_REGISTRATION).
Definition: sac_model_registration.h:216
pcl::SampleConsensusModelRegistration::computeSampleDistanceThreshold
void computeSampleDistanceThreshold(const PointCloudConstPtr &cloud)
Computes an "optimal" sample distance threshold based on the principal directions of the input cloud.
Definition: sac_model_registration.h:234
pcl::SampleConsensusModel::sample_size_
unsigned int sample_size_
The size of a sample from which the model is computed.
Definition: sac_model.h:588
pcl::SampleConsensusModel::model_size_
unsigned int model_size_
The number of coefficients in the model.
Definition: sac_model.h:591
pcl::PointCloud
PointCloud represents the base class in PCL for storing collections of 3D points.
Definition: distances.h:55
pcl::index_t
detail::int_type_t< detail::index_type_size, detail::index_type_signed > index_t
Type used for an index in PCL.
Definition: types.h:110
pcl::eigen33
void eigen33(const Matrix &mat, typename Matrix::Scalar &eigenvalue, Vector &eigenvector)
determines the eigenvector and eigenvalue of the smallest eigenvalue of the symmetric positive semi d...
Definition: eigen.hpp:296
pcl::PointXYZRGB
A point structure representing Euclidean xyz coordinates, and the RGB color.
Definition: point_types.hpp:628
pcl::SampleConsensusModelRegistration::doSamplesVerifyModel
bool doSamplesVerifyModel(const std::set< index_t > &, const Eigen::VectorXf &, const double) const override
Verify whether a subset of indices verifies a given set of model coefficients.
Definition: sac_model_registration.h:207
pcl::SampleConsensusModelRegistration::PointCloudConstPtr
typename SampleConsensusModel< PointT >::PointCloudConstPtr PointCloudConstPtr
Definition: sac_model_registration.h:72
pcl::SampleConsensusModelRegistration::SampleConsensusModelRegistration
SampleConsensusModelRegistration(const PointCloudConstPtr &cloud, const Indices &indices, bool random=false)
Constructor for base SampleConsensusModelRegistration.
Definition: sac_model_registration.h:99
pcl::SampleConsensusModelRegistration::getDistancesToModel
void getDistancesToModel(const Eigen::VectorXf &model_coefficients, std::vector< double > &distances) const override
Compute all distances from the transformed points to their correspondences.
Definition: sac_model_registration.hpp:101
pcl::SampleConsensusModelRegistration::computeSampleDistanceThreshold
void computeSampleDistanceThreshold(const PointCloudConstPtr &cloud, const Indices &indices)
Computes an "optimal" sample distance threshold based on the principal directions of the input cloud.
Definition: sac_model_registration.h:263
pcl::SampleConsensusModelRegistration::correspondences_
std::map< index_t, index_t > correspondences_
Given the index in the original point cloud, give the matching original index in the target cloud.
Definition: sac_model_registration.h:341
pcl::SampleConsensusModelRegistration::indices_tgt_
IndicesPtr indices_tgt_
A pointer to the vector of target point indices to use.
Definition: sac_model_registration.h:338
pcl::SacModel
SacModel
Definition: model_types.h:45
pcl::SampleConsensusModelRegistration::isSampleGood
bool isSampleGood(const Indices &samples) const override
Check if a sample of indices results in a good sample of points indices.
Definition: sac_model_registration.hpp:48
pcl::SampleConsensusModel::indices_
IndicesPtr indices_
A pointer to the vector of point indices to use.
Definition: sac_model.h:556
pcl::SampleConsensusModel::ConstPtr
shared_ptr< const SampleConsensusModel< PointT > > ConstPtr
Definition: sac_model.h:78
pcl::SampleConsensusModel::setInputCloud
virtual void setInputCloud(const PointCloudConstPtr &cloud)
Provide a pointer to the input dataset.
Definition: sac_model.h:300
pcl::SampleConsensusModelRegistration::countWithinDistance
std::size_t countWithinDistance(const Eigen::VectorXf &model_coefficients, const double threshold) const override
Count all the points which respect the given model coefficients as inliers.
Definition: sac_model_registration.hpp:204
PCL_MAKE_ALIGNED_OPERATOR_NEW
#define PCL_MAKE_ALIGNED_OPERATOR_NEW
Macro to signal a class requires a custom allocator.
Definition: memory.h:63
pcl::SampleConsensusModelRegistration::computeOriginalIndexMapping
void computeOriginalIndexMapping()
Compute mappings between original indices of the input_/target_ clouds.
Definition: sac_model_registration.h:306
pcl::SampleConsensusModel::Ptr
shared_ptr< SampleConsensusModel< PointT > > Ptr
Definition: sac_model.h:77
pcl::SampleConsensusModel::model_name_
std::string model_name_
The model name.
Definition: sac_model.h:550
pcl::SampleConsensusModelRegistration::selectWithinDistance
void selectWithinDistance(const Eigen::VectorXf &model_coefficients, const double threshold, Indices &inliers) override
Select all the points which respect the given model coefficients as inliers.
Definition: sac_model_registration.hpp:147
pcl::Indices
IndicesAllocator<> Indices
Type used for indices in PCL.
Definition: types.h:131
pcl::SampleConsensusModel::PointCloudConstPtr
typename PointCloud::ConstPtr PointCloudConstPtr
Definition: sac_model.h:73
pcl::SampleConsensusModelRegistration::optimizeModelCoefficients
void optimizeModelCoefficients(const Indices &inliers, const Eigen::VectorXf &model_coefficients, Eigen::VectorXf &optimized_coefficients) const override
Recompute the 4x4 transformation using the given inlier set.
Definition: sac_model_registration.hpp:252
pcl::SampleConsensusModelRegistration::computeModelCoefficients
bool computeModelCoefficients(const Indices &samples, Eigen::VectorXf &model_coefficients) const override
Compute a 4x4 rigid transformation matrix from the samples given.
Definition: sac_model_registration.hpp:69
pcl::SampleConsensusModelRegistration::setInputTarget
void setInputTarget(const PointCloudConstPtr &target)
Set the input point cloud target.
Definition: sac_model_registration.h:131
pcl::SampleConsensusModelRegistration::target_
PointCloudConstPtr target_
A boost shared pointer to the target point cloud data array.
Definition: sac_model_registration.h:335
pcl::SampleConsensusModel::PointCloudPtr
typename PointCloud::Ptr PointCloudPtr
Definition: sac_model.h:74
pcl::SampleConsensusModel
SampleConsensusModel represents the base model class.
Definition: sac_model.h:69
pcl::SampleConsensusModelRegistration
SampleConsensusModelRegistration defines a model for Point-To-Point registration outlier rejection.
Definition: sac_model_registration.h:61
pcl::SACMODEL_REGISTRATION
@ SACMODEL_REGISTRATION
Definition: model_types.h:60
memory.h
Defines functions, macros and traits for allocating and using memory.
centroid.h
pcl::SampleConsensusModelRegistration::projectPoints
void projectPoints(const Indices &, const Eigen::VectorXf &, PointCloud &, bool=true) const override
Create a new point cloud with inliers projected onto the model.
Definition: sac_model_registration.h:200
pcl::SampleConsensusModelRegistration::SampleConsensusModelRegistration
SampleConsensusModelRegistration(const PointCloudConstPtr &cloud, bool random=false)
Constructor for base SampleConsensusModelRegistration.
Definition: sac_model_registration.h:81
pcl::SampleConsensusModelRegistration::~SampleConsensusModelRegistration
~SampleConsensusModelRegistration()
Empty destructor.
Definition: sac_model_registration.h:114
pcl::SampleConsensusModelRegistration::setInputTarget
void setInputTarget(const PointCloudConstPtr &target, const Indices &indices_tgt)
Set the input point cloud target.
Definition: sac_model_registration.h:146