Point Cloud Library (PCL)  1.7.2
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 #ifndef PCL_REGISTRATION_H_
42 #define PCL_REGISTRATION_H_
43 
44 // PCL includes
45 #include <pcl/pcl_base.h>
46 #include <pcl/common/transforms.h>
47 #include <pcl/pcl_macros.h>
48 #include <pcl/search/kdtree.h>
49 #include <pcl/kdtree/kdtree_flann.h>
50 #include <pcl/registration/boost.h>
51 #include <pcl/registration/transformation_estimation.h>
52 #include <pcl/registration/correspondence_estimation.h>
53 #include <pcl/registration/correspondence_rejection.h>
54 
55 namespace pcl
56 {
57  /** \brief @b Registration represents the base registration class for general purpose, ICP-like methods.
58  * \author Radu B. Rusu, Michael Dixon
59  * \ingroup registration
60  */
61  template <typename PointSource, typename PointTarget, typename Scalar = float>
62  class Registration : public PCLBase<PointSource>
63  {
64  public:
65  typedef Eigen::Matrix<Scalar, 4, 4> Matrix4;
66 
67  // using PCLBase<PointSource>::initCompute;
71 
72  typedef boost::shared_ptr< Registration<PointSource, PointTarget, Scalar> > Ptr;
73  typedef boost::shared_ptr< const Registration<PointSource, PointTarget, Scalar> > ConstPtr;
74 
78 
80  typedef typename KdTree::Ptr KdTreeReciprocalPtr;
81 
85 
89 
91 
95 
99 
100  /** \brief Empty constructor. */
102  : reg_name_ ()
103  , tree_ (new KdTree)
105  , nr_iterations_ (0)
106  , max_iterations_ (10)
107  , ransac_iterations_ (0)
108  , target_ ()
109  , final_transformation_ (Matrix4::Identity ())
110  , transformation_ (Matrix4::Identity ())
111  , previous_transformation_ (Matrix4::Identity ())
113  , euclidean_fitness_epsilon_ (-std::numeric_limits<double>::max ())
114  , corr_dist_threshold_ (std::sqrt (std::numeric_limits<double>::max ()))
115  , inlier_threshold_ (0.05)
116  , converged_ (false)
122  , target_cloud_updated_ (true)
123  , source_cloud_updated_ (true)
124  , force_no_recompute_ (false)
126  , update_visualizer_ (NULL)
127  , point_representation_ ()
128  {
129  }
130 
131  /** \brief destructor. */
132  virtual ~Registration () {}
133 
134  /** \brief Provide a pointer to the transformation estimation object.
135  * (e.g., SVD, point to plane etc.)
136  *
137  * \param[in] te is the pointer to the corresponding transformation estimation object
138  *
139  * Code example:
140  *
141  * \code
142  * TransformationEstimationPointToPlaneLLS<PointXYZ, PointXYZ>::Ptr trans_lls (new TransformationEstimationPointToPlaneLLS<PointXYZ, PointXYZ>);
143  * icp.setTransformationEstimation (trans_lls);
144  * // or...
145  * TransformationEstimationSVD<PointXYZ, PointXYZ>::Ptr trans_svd (new TransformationEstimationSVD<PointXYZ, PointXYZ>);
146  * icp.setTransformationEstimation (trans_svd);
147  * \endcode
148  */
149  void
151 
152  /** \brief Provide a pointer to the correspondence estimation object.
153  * (e.g., regular, reciprocal, normal shooting etc.)
154  *
155  * \param[in] ce is the pointer to the corresponding correspondence estimation object
156  *
157  * Code example:
158  *
159  * \code
160  * CorrespondenceEstimation<PointXYZ, PointXYZ>::Ptr ce (new CorrespondenceEstimation<PointXYZ, PointXYZ>);
161  * ce->setInputSource (source);
162  * ce->setInputTarget (target);
163  * icp.setCorrespondenceEstimation (ce);
164  * // or...
165  * CorrespondenceEstimationNormalShooting<PointNormal, PointNormal, PointNormal>::Ptr cens (new CorrespondenceEstimationNormalShooting<PointNormal, PointNormal>);
166  * ce->setInputSource (source);
167  * ce->setInputTarget (target);
168  * ce->setSourceNormals (source);
169  * ce->setTargetNormals (target);
170  * icp.setCorrespondenceEstimation (cens);
171  * \endcode
172  */
173  void
175 
176  /** \brief Provide a pointer to the input source
177  * (e.g., the point cloud that we want to align to the target)
178  *
179  * \param[in] cloud the input point cloud source
180  */
181  PCL_DEPRECATED ("[pcl::registration::Registration::setInputCloud] setInputCloud is deprecated. Please use setInputSource instead.")
182  void
184 
185  /** \brief Get a pointer to the input point cloud dataset target. */
186  PCL_DEPRECATED ("[pcl::registration::Registration::getInputCloud] getInputCloud is deprecated. Please use getInputSource instead.")
188  getInputCloud ();
189 
190  /** \brief Provide a pointer to the input source
191  * (e.g., the point cloud that we want to align to the target)
192  *
193  * \param[in] cloud the input point cloud source
194  */
195  virtual void
197  {
198  source_cloud_updated_ = true;
200  }
201 
202  /** \brief Get a pointer to the input point cloud dataset target. */
203  inline PointCloudSourceConstPtr const
204  getInputSource () { return (input_ ); }
205 
206  /** \brief Provide a pointer to the input target (e.g., the point cloud that we want to align the input source to)
207  * \param[in] cloud the input point cloud target
208  */
209  virtual inline void
211 
212  /** \brief Get a pointer to the input point cloud dataset target. */
213  inline PointCloudTargetConstPtr const
214  getInputTarget () { return (target_ ); }
215 
216 
217  /** \brief Provide a pointer to the search object used to find correspondences in
218  * the target cloud.
219  * \param[in] tree a pointer to the spatial search object.
220  * \param[in] force_no_recompute If set to true, this tree will NEVER be
221  * recomputed, regardless of calls to setInputTarget. Only use if you are
222  * confident that the tree will be set correctly.
223  */
224  inline void
226  bool force_no_recompute = false)
227  {
228  tree_ = tree;
229  if (force_no_recompute)
230  {
231  force_no_recompute_ = true;
232  }
233  // Since we just set a new tree, we need to check for updates
234  target_cloud_updated_ = true;
235  }
236 
237  /** \brief Get a pointer to the search method used to find correspondences in the
238  * target cloud. */
239  inline KdTreePtr
241  {
242  return (tree_);
243  }
244 
245  /** \brief Provide a pointer to the search object used to find correspondences in
246  * the source cloud (usually used by reciprocal correspondence finding).
247  * \param[in] tree a pointer to the spatial search object.
248  * \param[in] force_no_recompute If set to true, this tree will NEVER be
249  * recomputed, regardless of calls to setInputSource. Only use if you are
250  * extremely confident that the tree will be set correctly.
251  */
252  inline void
254  bool force_no_recompute = false)
255  {
256  tree_reciprocal_ = tree;
257  if ( force_no_recompute )
258  {
260  }
261  // Since we just set a new tree, we need to check for updates
262  source_cloud_updated_ = true;
263  }
264 
265  /** \brief Get a pointer to the search method used to find correspondences in the
266  * source cloud. */
267  inline KdTreeReciprocalPtr
269  {
270  return (tree_reciprocal_);
271  }
272 
273  /** \brief Get the final transformation matrix estimated by the registration method. */
274  inline Matrix4
276 
277  /** \brief Get the last incremental transformation matrix estimated by the registration method. */
278  inline Matrix4
280 
281  /** \brief Set the maximum number of iterations the internal optimization should run for.
282  * \param[in] nr_iterations the maximum number of iterations the internal optimization should run for
283  */
284  inline void
285  setMaximumIterations (int nr_iterations) { max_iterations_ = nr_iterations; }
286 
287  /** \brief Get the maximum number of iterations the internal optimization should run for, as set by the user. */
288  inline int
290 
291  /** \brief Set the number of iterations RANSAC should run for.
292  * \param[in] ransac_iterations is the number of iterations RANSAC should run for
293  */
294  inline void
295  setRANSACIterations (int ransac_iterations) { ransac_iterations_ = ransac_iterations; }
296 
297  /** \brief Get the number of iterations RANSAC should run for, as set by the user. */
298  inline double
300 
301  /** \brief Set the inlier distance threshold for the internal RANSAC outlier rejection loop.
302  *
303  * The method considers a point to be an inlier, if the distance between the target data index and the transformed
304  * source index is smaller than the given inlier distance threshold.
305  * The value is set by default to 0.05m.
306  * \param[in] inlier_threshold the inlier distance threshold for the internal RANSAC outlier rejection loop
307  */
308  inline void
309  setRANSACOutlierRejectionThreshold (double inlier_threshold) { inlier_threshold_ = inlier_threshold; }
310 
311  /** \brief Get the inlier distance threshold for the internal outlier rejection loop as set by the user. */
312  inline double
314 
315  /** \brief Set the maximum distance threshold between two correspondent points in source <-> target. If the
316  * distance is larger than this threshold, the points will be ignored in the alignment process.
317  * \param[in] distance_threshold the maximum distance threshold between a point and its nearest neighbor
318  * correspondent in order to be considered in the alignment process
319  */
320  inline void
321  setMaxCorrespondenceDistance (double distance_threshold) { corr_dist_threshold_ = distance_threshold; }
322 
323  /** \brief Get the maximum distance threshold between two correspondent points in source <-> target. If the
324  * distance is larger than this threshold, the points will be ignored in the alignment process.
325  */
326  inline double
328 
329  /** \brief Set the transformation epsilon (maximum allowable difference between two consecutive
330  * transformations) in order for an optimization to be considered as having converged to the final
331  * solution.
332  * \param[in] epsilon the transformation epsilon in order for an optimization to be considered as having
333  * converged to the final solution.
334  */
335  inline void
336  setTransformationEpsilon (double epsilon) { transformation_epsilon_ = epsilon; }
337 
338  /** \brief Get the transformation epsilon (maximum allowable difference between two consecutive
339  * transformations) as set by the user.
340  */
341  inline double
343 
344  /** \brief Set the maximum allowed Euclidean error between two consecutive steps in the ICP loop, before
345  * the algorithm is considered to have converged.
346  * The error is estimated as the sum of the differences between correspondences in an Euclidean sense,
347  * divided by the number of correspondences.
348  * \param[in] epsilon the maximum allowed distance error before the algorithm will be considered to have
349  * converged
350  */
351 
352  inline void
354 
355  /** \brief Get the maximum allowed distance error before the algorithm will be considered to have converged,
356  * as set by the user. See \ref setEuclideanFitnessEpsilon
357  */
358  inline double
360 
361  /** \brief Provide a boost shared pointer to the PointRepresentation to be used when comparing points
362  * \param[in] point_representation the PointRepresentation to be used by the k-D tree
363  */
364  inline void
366  {
367  point_representation_ = point_representation;
368  }
369 
370  /** \brief Register the user callback function which will be called from registration thread
371  * in order to update point cloud obtained after each iteration
372  * \param[in] visualizerCallback reference of the user callback function
373  */
374  template<typename FunctionSignature> inline bool
375  registerVisualizationCallback (boost::function<FunctionSignature> &visualizerCallback)
376  {
377  if (visualizerCallback != NULL)
378  {
379  update_visualizer_ = visualizerCallback;
380  return (true);
381  }
382  else
383  return (false);
384  }
385 
386  /** \brief Obtain the Euclidean fitness score (e.g., sum of squared distances from the source to the target)
387  * \param[in] max_range maximum allowable distance between a point and its correspondence in the target
388  * (default: double::max)
389  */
390  inline double
391  getFitnessScore (double max_range = std::numeric_limits<double>::max ());
392 
393  /** \brief Obtain the Euclidean fitness score (e.g., sum of squared distances from the source to the target)
394  * from two sets of correspondence distances (distances between source and target points)
395  * \param[in] distances_a the first set of distances between correspondences
396  * \param[in] distances_b the second set of distances between correspondences
397  */
398  inline double
399  getFitnessScore (const std::vector<float> &distances_a, const std::vector<float> &distances_b);
400 
401  /** \brief Return the state of convergence after the last align run */
402  inline bool
403  hasConverged () { return (converged_); }
404 
405  /** \brief Call the registration algorithm which estimates the transformation and returns the transformed source
406  * (input) as \a output.
407  * \param[out] output the resultant input transfomed point cloud dataset
408  */
409  inline void
410  align (PointCloudSource &output);
411 
412  /** \brief Call the registration algorithm which estimates the transformation and returns the transformed source
413  * (input) as \a output.
414  * \param[out] output the resultant input transfomed point cloud dataset
415  * \param[in] guess the initial gross estimation of the transformation
416  */
417  inline void
418  align (PointCloudSource &output, const Matrix4& guess);
419 
420  /** \brief Abstract class get name method. */
421  inline const std::string&
422  getClassName () const { return (reg_name_); }
423 
424  /** \brief Internal computation initalization. */
425  bool
426  initCompute ();
427 
428  /** \brief Internal computation when reciprocal lookup is needed */
429  bool
431 
432  /** \brief Add a new correspondence rejector to the list
433  * \param[in] rejector the new correspondence rejector to concatenate
434  *
435  * Code example:
436  *
437  * \code
438  * CorrespondenceRejectorDistance rej;
439  * rej.setInputCloud<PointXYZ> (keypoints_src);
440  * rej.setInputTarget<PointXYZ> (keypoints_tgt);
441  * rej.setMaximumDistance (1);
442  * rej.setInputCorrespondences (all_correspondences);
443  *
444  * // or...
445  *
446  * \endcode
447  */
448  inline void
450  {
451  correspondence_rejectors_.push_back (rejector);
452  }
453 
454  /** \brief Get the list of correspondence rejectors. */
455  inline std::vector<CorrespondenceRejectorPtr>
457  {
458  return (correspondence_rejectors_);
459  }
460 
461  /** \brief Remove the i-th correspondence rejector in the list
462  * \param[in] i the position of the correspondence rejector in the list to remove
463  */
464  inline bool
466  {
467  if (i >= correspondence_rejectors_.size ())
468  return (false);
470  return (true);
471  }
472 
473  /** \brief Clear the list of correspondence rejectors. */
474  inline void
476  {
477  correspondence_rejectors_.clear ();
478  }
479 
480  protected:
481  /** \brief The registration method name. */
482  std::string reg_name_;
483 
484  /** \brief A pointer to the spatial search object. */
486 
487  /** \brief A pointer to the spatial search object of the source. */
489 
490  /** \brief The number of iterations the internal optimization ran for (used internally). */
492 
493  /** \brief The maximum number of iterations the internal optimization should run for.
494  * The default value is 10.
495  */
497 
498  /** \brief The number of iterations RANSAC should run for. */
500 
501  /** \brief The input point cloud dataset target. */
503 
504  /** \brief The final transformation matrix estimated by the registration method after N iterations. */
506 
507  /** \brief The transformation matrix estimated by the registration method. */
509 
510  /** \brief The previous transformation matrix estimated by the registration method (used internally). */
512 
513  /** \brief The maximum difference between two consecutive transformations in order to consider convergence
514  * (user defined).
515  */
517 
518  /** \brief The maximum allowed Euclidean error between two consecutive steps in the ICP loop, before the
519  * algorithm is considered to have converged. The error is estimated as the sum of the differences between
520  * correspondences in an Euclidean sense, divided by the number of correspondences.
521  */
523 
524  /** \brief The maximum distance threshold between two correspondent points in source <-> target. If the
525  * distance is larger than this threshold, the points will be ignored in the alignement process.
526  */
528 
529  /** \brief The inlier distance threshold for the internal RANSAC outlier rejection loop.
530  * The method considers a point to be an inlier, if the distance between the target data index and the transformed
531  * source index is smaller than the given inlier distance threshold. The default value is 0.05.
532  */
534 
535  /** \brief Holds internal convergence state, given user parameters. */
537 
538  /** \brief The minimum number of correspondences that the algorithm needs before attempting to estimate the
539  * transformation. The default value is 3.
540  */
542 
543  /** \brief The set of correspondences determined at this ICP step. */
545 
546  /** \brief A TransformationEstimation object, used to calculate the 4x4 rigid transformation. */
548 
549  /** \brief A CorrespondenceEstimation object, used to estimate correspondences between the source and the target cloud. */
551 
552  /** \brief The list of correspondence rejectors to use. */
553  std::vector<CorrespondenceRejectorPtr> correspondence_rejectors_;
554 
555  /** \brief Variable that stores whether we have a new target cloud, meaning we need to pre-process it again.
556  * This way, we avoid rebuilding the kd-tree for the target cloud every time the determineCorrespondences () method
557  * is called. */
559  /** \brief Variable that stores whether we have a new source cloud, meaning we need to pre-process it again.
560  * This way, we avoid rebuilding the reciprocal kd-tree for the source cloud every time the determineCorrespondences () method
561  * is called. */
563  /** \brief A flag which, if set, means the tree operating on the target cloud
564  * will never be recomputed*/
566 
567  /** \brief A flag which, if set, means the tree operating on the source cloud
568  * will never be recomputed*/
570 
571  /** \brief Callback function to update intermediate source point cloud position during it's registration
572  * to the target point cloud.
573  */
574  boost::function<void(const pcl::PointCloud<PointSource> &cloud_src,
575  const std::vector<int> &indices_src,
576  const pcl::PointCloud<PointTarget> &cloud_tgt,
577  const std::vector<int> &indices_tgt)> update_visualizer_;
578 
579  /** \brief Search for the closest nearest neighbor of a given point.
580  * \param cloud the point cloud dataset to use for nearest neighbor search
581  * \param index the index of the query point
582  * \param indices the resultant vector of indices representing the k-nearest neighbors
583  * \param distances the resultant distances from the query point to the k-nearest neighbors
584  */
585  inline bool
586  searchForNeighbors (const PointCloudSource &cloud, int index,
587  std::vector<int> &indices, std::vector<float> &distances)
588  {
589  int k = tree_->nearestKSearch (cloud, index, 1, indices, distances);
590  if (k == 0)
591  return (false);
592  return (true);
593  }
594 
595  /** \brief Abstract transformation computation method with initial guess */
596  virtual void
597  computeTransformation (PointCloudSource &output, const Matrix4& guess) = 0;
598 
599  private:
600  /** \brief The point representation used (internal). */
601  PointRepresentationConstPtr point_representation_;
602  public:
603  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
604  };
605 }
606 
607 #include <pcl/registration/impl/registration.hpp>
608 
609 #endif //#ifndef PCL_REGISTRATION_H_