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