Point Cloud Library (PCL)  1.9.1-dev
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 #pragma once
42 
43 // PCL includes
44 #include <pcl/sample_consensus/ransac.h>
45 #include <pcl/sample_consensus/sac_model_registration.h>
46 #include <pcl/registration/registration.h>
47 #include <pcl/registration/transformation_estimation_svd.h>
48 #include <pcl/registration/transformation_estimation_point_to_plane_lls.h>
49 #include <pcl/registration/transformation_estimation_symmetric_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:
98  using PointCloudSourcePtr = typename PointCloudSource::Ptr;
99  using PointCloudSourceConstPtr = typename PointCloudSource::ConstPtr;
100 
102  using PointCloudTargetPtr = typename PointCloudTarget::Ptr;
103  using PointCloudTargetConstPtr = typename PointCloudTarget::ConstPtr;
104 
107 
108  using Ptr = boost::shared_ptr<IterativeClosestPoint<PointSource, PointTarget, Scalar> >;
109  using ConstPtr = boost::shared_ptr<const IterativeClosestPoint<PointSource, PointTarget, Scalar> >;
110 
133 
136 
137  /** \brief Empty constructor. */
139  : x_idx_offset_ (0)
140  , y_idx_offset_ (0)
141  , z_idx_offset_ (0)
142  , nx_idx_offset_ (0)
143  , ny_idx_offset_ (0)
144  , nz_idx_offset_ (0)
146  , source_has_normals_ (false)
147  , target_has_normals_ (false)
148  {
149  reg_name_ = "IterativeClosestPoint";
153  };
154 
155  /** \brief Empty destructor */
157 
158  /** \brief Returns a pointer to the DefaultConvergenceCriteria used by the IterativeClosestPoint class.
159  * This allows to check the convergence state after the align() method as well as to configure
160  * DefaultConvergenceCriteria's parameters not available through the ICP API before the align()
161  * method is called. Please note that the align method sets max_iterations_,
162  * euclidean_fitness_epsilon_ and transformation_epsilon_ and therefore overrides the default / set
163  * values of the DefaultConvergenceCriteria instance.
164  * \return Pointer to the IterativeClosestPoint's DefaultConvergenceCriteria.
165  */
168  {
169  return convergence_criteria_;
170  }
171 
172  /** \brief Provide a pointer to the input source
173  * (e.g., the point cloud that we want to align to the target)
174  *
175  * \param[in] cloud the input point cloud source
176  */
177  void
178  setInputSource (const PointCloudSourceConstPtr &cloud) override
179  {
181  const auto fields = pcl::getFields<PointSource> ();
182  source_has_normals_ = false;
183  for (const auto &field : fields)
184  {
185  if (field.name == "x") x_idx_offset_ = field.offset;
186  else if (field.name == "y") y_idx_offset_ = field.offset;
187  else if (field.name == "z") z_idx_offset_ = field.offset;
188  else if (field.name == "normal_x")
189  {
190  source_has_normals_ = true;
191  nx_idx_offset_ = field.offset;
192  }
193  else if (field.name == "normal_y")
194  {
195  source_has_normals_ = true;
196  ny_idx_offset_ = field.offset;
197  }
198  else if (field.name == "normal_z")
199  {
200  source_has_normals_ = true;
201  nz_idx_offset_ = field.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  void
212  setInputTarget (const PointCloudTargetConstPtr &cloud) override
213  {
215  const auto fields = pcl::getFields<PointSource> ();
216  target_has_normals_ = false;
217  for (const auto &field : fields)
218  {
219  if (field.name == "normal_x" || field.name == "normal_y" || field.name == "normal_z")
220  {
221  target_has_normals_ = true;
222  break;
223  }
224  }
225  }
226 
227  /** \brief Set whether to use reciprocal correspondence or not
228  *
229  * \param[in] use_reciprocal_correspondence whether to use reciprocal correspondence or not
230  */
231  inline void
232  setUseReciprocalCorrespondences (bool use_reciprocal_correspondence)
233  {
234  use_reciprocal_correspondence_ = use_reciprocal_correspondence;
235  }
236 
237  /** \brief Obtain whether reciprocal correspondence are used or not */
238  inline bool
240  {
242  }
243 
244  protected:
245 
246  /** \brief Apply a rigid transform to a given dataset. Here we check whether whether
247  * the dataset has surface normals in addition to XYZ, and rotate normals as well.
248  * \param[in] input the input point cloud
249  * \param[out] output the resultant output point cloud
250  * \param[in] transform a 4x4 rigid transformation
251  * \note Can be used with cloud_in equal to cloud_out
252  */
253  virtual void
254  transformCloud (const PointCloudSource &input,
255  PointCloudSource &output,
256  const Matrix4 &transform);
257 
258  /** \brief Rigid transformation computation method with initial guess.
259  * \param output the transformed input point cloud dataset using the rigid transformation found
260  * \param guess the initial guess of the transformation to compute
261  */
262  void
263  computeTransformation (PointCloudSource &output, const Matrix4 &guess) override;
264 
265  /** \brief Looks at the Estimators and Rejectors and determines whether their blob-setter methods need to be called */
266  virtual void
268 
269  /** \brief XYZ fields offset. */
271 
272  /** \brief Normal fields offset. */
274 
275  /** \brief The correspondence type used for correspondence estimation. */
277 
278  /** \brief Internal check whether source dataset has normals or not. */
280  /** \brief Internal check whether target dataset has normals or not. */
282 
283  /** \brief Checks for whether estimators and rejectors need various data */
285  };
286 
287  /** \brief @b IterativeClosestPointWithNormals is a special case of
288  * IterativeClosestPoint, that uses a transformation estimated based on
289  * Point to Plane distances by default.
290  *
291  * By default, this implementation uses the traditional point to plane objective
292  * and computes point to plane distances using the normals of the target point
293  * cloud. It also provides the option (through setUseSymmetricObjective) of
294  * using the symmetric objective function of [Rusinkiewicz 2019]. This objective
295  * uses the normals of both the source and target point cloud and has a similar
296  * computational cost to the traditional point to plane objective while also
297  * offering improved convergence speed and a wider basin of convergence.
298  *
299  * Note that this implementation not demean the point clouds which can lead
300  * to increased numerical error. If desired, a user can demean the point cloud,
301  * run iterative closest point, and composite the resulting ICP transformation
302  * with the translations from demeaning to obtain a transformation between
303  * the original point clouds.
304  *
305  * \author Radu B. Rusu, Matthew Cong
306  * \ingroup registration
307  */
308  template <typename PointSource, typename PointTarget, typename Scalar = float>
309  class IterativeClosestPointWithNormals : public IterativeClosestPoint<PointSource, PointTarget, Scalar>
310  {
311  public:
315 
319 
320  using Ptr = boost::shared_ptr<IterativeClosestPoint<PointSource, PointTarget, Scalar> >;
321  using ConstPtr = boost::shared_ptr<const IterativeClosestPoint<PointSource, PointTarget, Scalar> >;
322 
323  /** \brief Empty constructor. */
325  {
326  reg_name_ = "IterativeClosestPointWithNormals";
327  setUseSymmetricObjective (false);
328  setEnforceSameDirectionNormals (true);
329  //correspondence_rejectors_.add
330  };
331 
332  /** \brief Empty destructor */
334 
335  /** \brief Set whether to use a symmetric objective function or not
336  *
337  * \param[in] use_symmetric_objective whether to use a symmetric objective function or not
338  */
339  inline void
340  setUseSymmetricObjective (bool use_symmetric_objective)
341  {
342  use_symmetric_objective_ = use_symmetric_objective;
343  if (use_symmetric_objective_)
344  {
345  auto symmetric_transformation_estimation = std::make_shared<pcl::registration::TransformationEstimationSymmetricPointToPlaneLLS<PointSource, PointTarget, Scalar> > ();
346  symmetric_transformation_estimation->setEnforceSameDirectionNormals (enforce_same_direction_normals_);
347  transformation_estimation_.reset (symmetric_transformation_estimation);
348  }
349  else
350  {
352  }
353  }
354 
355  /** \brief Obtain whether a symmetric objective is used or not */
356  inline bool
358  {
359  return use_symmetric_objective_;
360  }
361 
362  /** \brief Set whether or not to negate source or target normals on a per-point basis such that they point in the same direction. Only applicable to the symmetric objective function.
363  *
364  * \param[in] enforce_same_direction_normals whether to negate source or target normals on a per-point basis such that they point in the same direction.
365  */
366  inline void
367  setEnforceSameDirectionNormals (bool enforce_same_direction_normals)
368  {
369  enforce_same_direction_normals_ = enforce_same_direction_normals;
371  if (symmetric_transformation_estimation)
372  symmetric_transformation_estimation->setEnforceSameDirectionNormals (enforce_same_direction_normals_);
373  }
374 
375  /** \brief Obtain whether source or target normals are negated on a per-point basis such that they point in the same direction or not */
376  inline bool
378  {
379  return enforce_same_direction_normals_;
380  }
381 
382  protected:
383 
384  /** \brief Apply a rigid transform to a given dataset
385  * \param[in] input the input point cloud
386  * \param[out] output the resultant output point cloud
387  * \param[in] transform a 4x4 rigid transformation
388  * \note Can be used with cloud_in equal to cloud_out
389  */
390  virtual void
391  transformCloud (const PointCloudSource &input,
392  PointCloudSource &output,
393  const Matrix4 &transform);
394 
395  /** \brief Type of objective function (asymmetric vs. symmetric) used for transform estimation */
397  /** \brief Whether or not to negate source and/or target normals such that they point in the same direction in the symmetric objective function */
399  };
400 
401 }
402 
403 #include <pcl/registration/impl/icp.hpp>
bool enforce_same_direction_normals_
Whether or not to negate source and/or target normals such that they point in the same direction in t...
Definition: icp.h:398
typename Registration< PointSource, PointTarget, float >::PointCloudTarget PointCloudTarget
Definition: icp.h:101
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:559
IterativeClosestPointWithNormals is a special case of IterativeClosestPoint, that uses a transformati...
Definition: icp.h:309
bool target_has_normals_
Internal check whether target dataset has normals or not.
Definition: icp.h:281
typename Registration< PointSource, PointTarget, float >::Matrix4 Matrix4
Definition: icp.h:135
bool getUseReciprocalCorrespondences() const
Obtain whether reciprocal correspondence are used or not.
Definition: icp.h:239
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:190
CorrespondenceEstimationPtr correspondence_estimation_
A CorrespondenceEstimation object, used to estimate correspondences between the source and the target...
Definition: registration.h:562
This file defines compatibility wrappers for low level I/O functions.
Definition: convolution.h:45
int nr_iterations_
The number of iterations the internal optimization ran for (used internally).
Definition: registration.h:498
boost::shared_ptr< PointIndices > PointIndicesPtr
Definition: pcl_base.h:76
CorrespondencesPtr correspondences_
The set of correspondences determined at this ICP step.
Definition: registration.h:556
TransformationEstimationSymmetricPointToPlaneLLS implements a Linear Least Squares (LLS) approximatio...
virtual void determineRequiredBlobData()
Looks at the Estimators and Rejectors and determines whether their blob-setter methods need to be cal...
Definition: icp.hpp:254
boost::shared_ptr< const IterativeClosestPoint< PointSource, PointTarget, float > > ConstPtr
Definition: icp.h:109
Eigen::Matrix< Scalar, 4, 4 > Matrix4
Definition: registration.h:63
typename PointCloudSource::ConstPtr PointCloudSourceConstPtr
Definition: icp.h:99
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...
virtual void transformCloud(const PointCloudSource &input, PointCloudSource &output, const Matrix4 &transform)
Apply a rigid transform to a given dataset.
Definition: icp.hpp:49
void setEnforceSameDirectionNormals(bool enforce_same_direction_normals)
Set whether or not to negate source or target normals on a per-point basis such that they point in th...
Definition: icp.h:367
Matrix4 transformation_
The transformation matrix estimated by the registration method.
Definition: registration.h:515
IterativeClosestPointWithNormals()
Empty constructor.
Definition: icp.h:324
void setInputSource(const PointCloudSourceConstPtr &cloud) override
Provide a pointer to the input source (e.g., the point cloud that we want to align to the target) ...
Definition: icp.h:178
boost::shared_ptr< ::pcl::PointIndices > Ptr
Definition: PointIndices.h:22
TransformationEstimationSVD implements SVD-based estimation of the transformation aligning the given ...
typename Registration< PointSource, PointTarget, float >::PointCloudSource PointCloudSource
Definition: icp.h:97
boost::shared_ptr< DefaultConvergenceCriteria< Scalar > > Ptr
virtual ~IterativeClosestPointWithNormals()
Empty destructor.
Definition: icp.h:333
~IterativeClosestPoint()
Empty destructor.
Definition: icp.h:156
boost::shared_ptr< PointIndices const > PointIndicesConstPtr
Definition: pcl_base.h:77
IterativeClosestPoint provides a base implementation of the Iterative Closest Point algorithm...
Definition: icp.h:94
boost::shared_ptr< const ::pcl::PointIndices > ConstPtr
Definition: PointIndices.h:23
Registration represents the base registration class for general purpose, ICP-like methods...
Definition: registration.h:60
typename PointCloudTarget::ConstPtr PointCloudTargetConstPtr
Definition: icp.h:103
void setEnforceSameDirectionNormals(bool enforce_same_direction_normals)
Set whether or not to negate source or target normals on a per-point basis such that they point in th...
bool getEnforceSameDirectionNormals() const
Obtain whether source or target normals are negated on a per-point basis such that they point in the ...
Definition: icp.h:377
bool source_has_normals_
Internal check whether source dataset has normals or not.
Definition: icp.h:279
void setInputTarget(const PointCloudTargetConstPtr &cloud) override
Provide a pointer to the input target (e.g., the point cloud that we want to align to the target) ...
Definition: icp.h:212
size_t x_idx_offset_
XYZ fields offset.
Definition: icp.h:270
pcl::registration::DefaultConvergenceCriteria< Scalar >::Ptr getConvergeCriteria()
Returns a pointer to the DefaultConvergenceCriteria used by the IterativeClosestPoint class...
Definition: icp.h:167
pcl::registration::DefaultConvergenceCriteria< Scalar >::Ptr convergence_criteria_
Definition: icp.h:134
TransformationEstimationPointToPlaneLLS implements a Linear Least Squares (LLS) approximation for min...
std::string reg_name_
The registration method name.
Definition: registration.h:489
void computeTransformation(PointCloudSource &output, const Matrix4 &guess) override
Rigid transformation computation method with initial guess.
Definition: icp.hpp:119
bool need_source_blob_
Checks for whether estimators and rejectors need various data.
Definition: icp.h:284
bool use_reciprocal_correspondence_
The correspondence type used for correspondence estimation.
Definition: icp.h:276
void setUseReciprocalCorrespondences(bool use_reciprocal_correspondence)
Set whether to use reciprocal correspondence or not.
Definition: icp.h:232
boost::shared_ptr< IterativeClosestPoint< PointSource, PointTarget, float > > Ptr
Definition: icp.h:108
bool getUseSymmetricObjective() const
Obtain whether a symmetric objective is used or not.
Definition: icp.h:357
IterativeClosestPoint()
Empty constructor.
Definition: icp.h:138
bool use_symmetric_objective_
Type of objective function (asymmetric vs.
Definition: icp.h:396
CorrespondenceEstimation represents the base class for determining correspondences between target and...
typename PointCloudTarget::Ptr PointCloudTargetPtr
Definition: icp.h:102
size_t nx_idx_offset_
Normal fields offset.
Definition: icp.h:273
typename PointCloudSource::Ptr PointCloudSourcePtr
Definition: icp.h:98
void setUseSymmetricObjective(bool use_symmetric_objective)
Set whether to use a symmetric objective function or not.
Definition: icp.h:340