Point Cloud Library (PCL)  1.8.0
icp.h
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Point Cloud Library (PCL) - www.pointclouds.org
5  * Copyright (c) 2010, 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_ICP_H_
42 #define PCL_ICP_H_
43 
44 // PCL includes
45 #include <pcl/sample_consensus/ransac.h>
46 #include <pcl/sample_consensus/sac_model_registration.h>
47 #include <pcl/registration/registration.h>
48 #include <pcl/registration/transformation_estimation_svd.h>
49 #include <pcl/registration/transformation_estimation_point_to_plane_lls.h>
50 #include <pcl/registration/correspondence_estimation.h>
51 #include <pcl/registration/default_convergence_criteria.h>
52 
53 namespace pcl
54 {
55  /** \brief @b IterativeClosestPoint provides a base implementation of the Iterative Closest Point algorithm.
56  * The transformation is estimated based on Singular Value Decomposition (SVD).
57  *
58  * The algorithm has several termination criteria:
59  *
60  * <ol>
61  * <li>Number of iterations has reached the maximum user imposed number of iterations (via \ref setMaximumIterations)</li>
62  * <li>The epsilon (difference) between the previous transformation and the current estimated transformation is smaller than an user imposed value (via \ref setTransformationEpsilon)</li>
63  * <li>The sum of Euclidean squared errors is smaller than a user defined threshold (via \ref setEuclideanFitnessEpsilon)</li>
64  * </ol>
65  *
66  *
67  * Usage example:
68  * \code
69  * IterativeClosestPoint<PointXYZ, PointXYZ> icp;
70  * // Set the input source and target
71  * icp.setInputCloud (cloud_source);
72  * icp.setInputTarget (cloud_target);
73  *
74  * // Set the max correspondence distance to 5cm (e.g., correspondences with higher distances will be ignored)
75  * icp.setMaxCorrespondenceDistance (0.05);
76  * // Set the maximum number of iterations (criterion 1)
77  * icp.setMaximumIterations (50);
78  * // Set the transformation epsilon (criterion 2)
79  * icp.setTransformationEpsilon (1e-8);
80  * // Set the euclidean distance difference epsilon (criterion 3)
81  * icp.setEuclideanFitnessEpsilon (1);
82  *
83  * // Perform the alignment
84  * icp.align (cloud_source_registered);
85  *
86  * // Obtain the transformation that aligned cloud_source to cloud_source_registered
87  * Eigen::Matrix4f transformation = icp.getFinalTransformation ();
88  * \endcode
89  *
90  * \author Radu B. Rusu, Michael Dixon
91  * \ingroup registration
92  */
93  template <typename PointSource, typename PointTarget, typename Scalar = float>
94  class IterativeClosestPoint : public Registration<PointSource, PointTarget, Scalar>
95  {
96  public:
100 
104 
107 
108  typedef boost::shared_ptr<IterativeClosestPoint<PointSource, PointTarget, Scalar> > Ptr;
109  typedef boost::shared_ptr<const IterativeClosestPoint<PointSource, PointTarget, Scalar> > ConstPtr;
110 
132 
135 
136  /** \brief Empty constructor. */
138  : x_idx_offset_ (0)
139  , y_idx_offset_ (0)
140  , z_idx_offset_ (0)
141  , nx_idx_offset_ (0)
142  , ny_idx_offset_ (0)
143  , nz_idx_offset_ (0)
145  , source_has_normals_ (false)
146  , target_has_normals_ (false)
147  {
148  reg_name_ = "IterativeClosestPoint";
152  };
153 
154  /** \brief Empty destructor */
156 
157  /** \brief Returns a pointer to the DefaultConvergenceCriteria used by the IterativeClosestPoint class.
158  * This allows to check the convergence state after the align() method as well as to configure
159  * DefaultConvergenceCriteria's parameters not available through the ICP API before the align()
160  * method is called. Please note that the align method sets max_iterations_,
161  * euclidean_fitness_epsilon_ and transformation_epsilon_ and therefore overrides the default / set
162  * values of the DefaultConvergenceCriteria instance.
163  * \return Pointer to the IterativeClosestPoint's DefaultConvergenceCriteria.
164  */
167  {
168  return convergence_criteria_;
169  }
170 
171  /** \brief Provide a pointer to the input source
172  * (e.g., the point cloud that we want to align to the target)
173  *
174  * \param[in] cloud the input point cloud source
175  */
176  virtual void
177  setInputSource (const PointCloudSourceConstPtr &cloud)
178  {
180  std::vector<pcl::PCLPointField> fields;
181  pcl::getFields (*cloud, fields);
182  source_has_normals_ = false;
183  for (size_t i = 0; i < fields.size (); ++i)
184  {
185  if (fields[i].name == "x") x_idx_offset_ = fields[i].offset;
186  else if (fields[i].name == "y") y_idx_offset_ = fields[i].offset;
187  else if (fields[i].name == "z") z_idx_offset_ = fields[i].offset;
188  else if (fields[i].name == "normal_x")
189  {
190  source_has_normals_ = true;
191  nx_idx_offset_ = fields[i].offset;
192  }
193  else if (fields[i].name == "normal_y")
194  {
195  source_has_normals_ = true;
196  ny_idx_offset_ = fields[i].offset;
197  }
198  else if (fields[i].name == "normal_z")
199  {
200  source_has_normals_ = true;
201  nz_idx_offset_ = fields[i].offset;
202  }
203  }
204  }
205 
206  /** \brief Provide a pointer to the input target
207  * (e.g., the point cloud that we want to align to the target)
208  *
209  * \param[in] cloud the input point cloud target
210  */
211  virtual void
212  setInputTarget (const PointCloudTargetConstPtr &cloud)
213  {
215  std::vector<pcl::PCLPointField> fields;
216  pcl::getFields (*cloud, fields);
217  target_has_normals_ = false;
218  for (size_t i = 0; i < fields.size (); ++i)
219  {
220  if (fields[i].name == "normal_x" || fields[i].name == "normal_y" || fields[i].name == "normal_z")
221  {
222  target_has_normals_ = true;
223  break;
224  }
225  }
226  }
227 
228  /** \brief Set whether to use reciprocal correspondence or not
229  *
230  * \param[in] use_reciprocal_correspondence whether to use reciprocal correspondence or not
231  */
232  inline void
233  setUseReciprocalCorrespondences (bool use_reciprocal_correspondence)
234  {
235  use_reciprocal_correspondence_ = use_reciprocal_correspondence;
236  }
237 
238  /** \brief Obtain whether reciprocal correspondence are used or not */
239  inline bool
241  {
243  }
244 
245  protected:
246 
247  /** \brief Apply a rigid transform to a given dataset. Here we check whether whether
248  * the dataset has surface normals in addition to XYZ, and rotate normals as well.
249  * \param[in] input the input point cloud
250  * \param[out] output the resultant output point cloud
251  * \param[in] transform a 4x4 rigid transformation
252  * \note Can be used with cloud_in equal to cloud_out
253  */
254  virtual void
255  transformCloud (const PointCloudSource &input,
256  PointCloudSource &output,
257  const Matrix4 &transform);
258 
259  /** \brief Rigid transformation computation method with initial guess.
260  * \param output the transformed input point cloud dataset using the rigid transformation found
261  * \param guess the initial guess of the transformation to compute
262  */
263  virtual void
264  computeTransformation (PointCloudSource &output, const Matrix4 &guess);
265 
266  /** \brief Looks at the Estimators and Rejectors and determines whether their blob-setter methods need to be called */
267  virtual void
269 
270  /** \brief XYZ fields offset. */
272 
273  /** \brief Normal fields offset. */
275 
276  /** \brief The correspondence type used for correspondence estimation. */
278 
279  /** \brief Internal check whether source dataset has normals or not. */
281  /** \brief Internal check whether target dataset has normals or not. */
283 
284  /** \brief Checks for whether estimators and rejectors need various data */
286  };
287 
288  /** \brief @b IterativeClosestPointWithNormals is a special case of
289  * IterativeClosestPoint, that uses a transformation estimated based on
290  * Point to Plane distances by default.
291  *
292  * \author Radu B. Rusu
293  * \ingroup registration
294  */
295  template <typename PointSource, typename PointTarget, typename Scalar = float>
296  class IterativeClosestPointWithNormals : public IterativeClosestPoint<PointSource, PointTarget, Scalar>
297  {
298  public:
302 
306 
307  typedef boost::shared_ptr<IterativeClosestPoint<PointSource, PointTarget, Scalar> > Ptr;
308  typedef boost::shared_ptr<const IterativeClosestPoint<PointSource, PointTarget, Scalar> > ConstPtr;
309 
310  /** \brief Empty constructor. */
312  {
313  reg_name_ = "IterativeClosestPointWithNormals";
315  //correspondence_rejectors_.add
316  };
317 
318  /** \brief Empty destructor */
320 
321  protected:
322 
323  /** \brief Apply a rigid transform to a given dataset
324  * \param[in] input the input point cloud
325  * \param[out] output the resultant output point cloud
326  * \param[in] transform a 4x4 rigid transformation
327  * \note Can be used with cloud_in equal to cloud_out
328  */
329  virtual void
330  transformCloud (const PointCloudSource &input,
331  PointCloudSource &output,
332  const Matrix4 &transform);
333  };
334 }
335 
336 #include <pcl/registration/impl/icp.hpp>
337 
338 #endif //#ifndef PCL_ICP_H_
PointIndices::Ptr PointIndicesPtr
Definition: icp.h:105
virtual void setInputTarget(const PointCloudTargetConstPtr &cloud)
Provide a pointer to the input target (e.g., the point cloud that we want to align to the target) ...
Definition: icp.h:212
PointCloudTarget::Ptr PointCloudTargetPtr
Definition: icp.h:102
DefaultConvergenceCriteria represents an instantiation of ConvergenceCriteria, and implements the fol...
TransformationEstimationPtr transformation_estimation_
A TransformationEstimation object, used to calculate the 4x4 rigid transformation.
Definition: registration.h:547
IterativeClosestPointWithNormals is a special case of IterativeClosestPoint, that uses a transformati...
Definition: icp.h:296
bool target_has_normals_
Internal check whether target dataset has normals or not.
Definition: icp.h:282
virtual void setInputSource(const PointCloudSourceConstPtr &cloud)
Provide a pointer to the input source (e.g., the point cloud that we want to align to the target) ...
Definition: registration.h:196
CorrespondenceEstimationPtr correspondence_estimation_
A CorrespondenceEstimation object, used to estimate correspondences between the source and the target...
Definition: registration.h:550
int nr_iterations_
The number of iterations the internal optimization ran for (used internally).
Definition: registration.h:491
bool getUseReciprocalCorrespondences() const
Obtain whether reciprocal correspondence are used or not.
Definition: icp.h:240
PointCloudTarget::ConstPtr PointCloudTargetConstPtr
Definition: icp.h:103
CorrespondencesPtr correspondences_
The set of correspondences determined at this ICP step.
Definition: registration.h:544
boost::shared_ptr< const IterativeClosestPoint< PointSource, PointTarget, Scalar > > ConstPtr
Definition: icp.h:308
virtual void determineRequiredBlobData()
Looks at the Estimators and Rejectors and determines whether their blob-setter methods need to be cal...
Definition: icp.hpp:251
virtual void setInputTarget(const PointCloudTargetConstPtr &cloud)
Provide a pointer to the input target (e.g., the point cloud that we want to align the input source t...
Eigen::Matrix< Scalar, 4, 4 > Matrix4
Definition: registration.h:65
boost::shared_ptr< IterativeClosestPoint< PointSource, PointTarget, Scalar > > Ptr
Definition: icp.h:307
boost::shared_ptr< PointCloud< PointSource > > Ptr
Definition: point_cloud.h:428
virtual void transformCloud(const PointCloudSource &input, PointCloudSource &output, const Matrix4 &transform)
Apply a rigid transform to a given dataset.
Definition: icp.hpp:49
Registration< PointSource, PointTarget, Scalar >::PointCloudSource PointCloudSource
Definition: icp.h:97
Matrix4 transformation_
The transformation matrix estimated by the registration method.
Definition: registration.h:508
IterativeClosestPointWithNormals()
Empty constructor.
Definition: icp.h:311
TransformationEstimationSVD implements SVD-based estimation of the transformation aligning the given ...
virtual ~IterativeClosestPoint()
Empty destructor.
Definition: icp.h:155
PointCloudSource::ConstPtr PointCloudSourceConstPtr
Definition: icp.h:99
boost::shared_ptr< IterativeClosestPoint< PointSource, PointTarget, Scalar > > Ptr
Definition: icp.h:108
virtual ~IterativeClosestPointWithNormals()
Empty destructor.
Definition: icp.h:319
boost::shared_ptr< const PointCloud< PointSource > > ConstPtr
Definition: point_cloud.h:429
IterativeClosestPoint provides a base implementation of the Iterative Closest Point algorithm...
Definition: icp.h:94
Registration represents the base registration class for general purpose, ICP-like methods...
Definition: registration.h:62
bool source_has_normals_
Internal check whether source dataset has normals or not.
Definition: icp.h:280
boost::shared_ptr< DefaultConvergenceCriteria< Scalar > > Ptr
Registration< PointSource, PointTarget, Scalar >::Matrix4 Matrix4
Definition: icp.h:134
boost::shared_ptr< ::pcl::PointIndices > Ptr
Definition: PointIndices.h:22
size_t x_idx_offset_
XYZ fields offset.
Definition: icp.h:271
virtual void computeTransformation(PointCloudSource &output, const Matrix4 &guess)
Rigid transformation computation method with initial guess.
Definition: icp.hpp:119
Registration< PointSource, PointTarget, Scalar >::PointCloudTarget PointCloudTarget
Definition: icp.h:101
PointIndices::ConstPtr PointIndicesConstPtr
Definition: icp.h:106
pcl::registration::DefaultConvergenceCriteria< Scalar >::Ptr getConvergeCriteria()
Returns a pointer to the DefaultConvergenceCriteria used by the IterativeClosestPoint class...
Definition: icp.h:166
pcl::registration::DefaultConvergenceCriteria< Scalar >::Ptr convergence_criteria_
Definition: icp.h:133
virtual void setInputSource(const PointCloudSourceConstPtr &cloud)
Provide a pointer to the input source (e.g., the point cloud that we want to align to the target) ...
Definition: icp.h:177
TransformationEstimationPointToPlaneLLS implements a Linear Least Squares (LLS) approximation for min...
std::string reg_name_
The registration method name.
Definition: registration.h:482
bool need_source_blob_
Checks for whether estimators and rejectors need various data.
Definition: icp.h:285
bool use_reciprocal_correspondence_
The correspondence type used for correspondence estimation.
Definition: icp.h:277
void setUseReciprocalCorrespondences(bool use_reciprocal_correspondence)
Set whether to use reciprocal correspondence or not.
Definition: icp.h:233
IterativeClosestPoint< PointSource, PointTarget, Scalar >::PointCloudTarget PointCloudTarget
Definition: icp.h:300
IterativeClosestPoint()
Empty constructor.
Definition: icp.h:137
CorrespondenceEstimation represents the base class for determining correspondences between target and...
IterativeClosestPoint< PointSource, PointTarget, Scalar >::PointCloudSource PointCloudSource
Definition: icp.h:299
size_t nx_idx_offset_
Normal fields offset.
Definition: icp.h:274
IterativeClosestPoint< PointSource, PointTarget, Scalar >::Matrix4 Matrix4
Definition: icp.h:301
boost::shared_ptr< const IterativeClosestPoint< PointSource, PointTarget, Scalar > > ConstPtr
Definition: icp.h:109
void getFields(const pcl::PointCloud< PointT > &cloud, std::vector< pcl::PCLPointField > &fields)
Get the list of available fields (i.e., dimension/channel)
Definition: io.hpp:79
PointCloudSource::Ptr PointCloudSourcePtr
Definition: icp.h:98
boost::shared_ptr< ::pcl::PointIndices const > ConstPtr
Definition: PointIndices.h:23