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