Point Cloud Library (PCL)  1.11.1
icp.hpp
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_IMPL_ICP_HPP_
42 #define PCL_REGISTRATION_IMPL_ICP_HPP_
43 
44 #include <pcl/registration/boost.h>
45 #include <pcl/correspondence.h>
46 
47 
48 namespace pcl
49 {
50 
51 template <typename PointSource, typename PointTarget, typename Scalar> void
53  const PointCloudSource &input,
54  PointCloudSource &output,
55  const Matrix4 &transform)
56 {
57  Eigen::Vector4f pt (0.0f, 0.0f, 0.0f, 1.0f), pt_t;
58  Eigen::Matrix4f tr = transform.template cast<float> ();
59 
60  // XYZ is ALWAYS present due to the templatization, so we only have to check for normals
61  if (source_has_normals_)
62  {
63  Eigen::Vector3f nt, nt_t;
64  Eigen::Matrix3f rot = tr.block<3, 3> (0, 0);
65 
66  for (std::size_t i = 0; i < input.size (); ++i)
67  {
68  const std::uint8_t* data_in = reinterpret_cast<const std::uint8_t*> (&input[i]);
69  std::uint8_t* data_out = reinterpret_cast<std::uint8_t*> (&output[i]);
70  memcpy (&pt[0], data_in + x_idx_offset_, sizeof (float));
71  memcpy (&pt[1], data_in + y_idx_offset_, sizeof (float));
72  memcpy (&pt[2], data_in + z_idx_offset_, sizeof (float));
73 
74  if (!std::isfinite (pt[0]) || !std::isfinite (pt[1]) || !std::isfinite (pt[2]))
75  continue;
76 
77  pt_t = tr * pt;
78 
79  memcpy (data_out + x_idx_offset_, &pt_t[0], sizeof (float));
80  memcpy (data_out + y_idx_offset_, &pt_t[1], sizeof (float));
81  memcpy (data_out + z_idx_offset_, &pt_t[2], sizeof (float));
82 
83  memcpy (&nt[0], data_in + nx_idx_offset_, sizeof (float));
84  memcpy (&nt[1], data_in + ny_idx_offset_, sizeof (float));
85  memcpy (&nt[2], data_in + nz_idx_offset_, sizeof (float));
86 
87  if (!std::isfinite (nt[0]) || !std::isfinite (nt[1]) || !std::isfinite (nt[2]))
88  continue;
89 
90  nt_t = rot * nt;
91 
92  memcpy (data_out + nx_idx_offset_, &nt_t[0], sizeof (float));
93  memcpy (data_out + ny_idx_offset_, &nt_t[1], sizeof (float));
94  memcpy (data_out + nz_idx_offset_, &nt_t[2], sizeof (float));
95  }
96  }
97  else
98  {
99  for (std::size_t i = 0; i < input.size (); ++i)
100  {
101  const std::uint8_t* data_in = reinterpret_cast<const std::uint8_t*> (&input[i]);
102  std::uint8_t* data_out = reinterpret_cast<std::uint8_t*> (&output[i]);
103  memcpy (&pt[0], data_in + x_idx_offset_, sizeof (float));
104  memcpy (&pt[1], data_in + y_idx_offset_, sizeof (float));
105  memcpy (&pt[2], data_in + z_idx_offset_, sizeof (float));
106 
107  if (!std::isfinite (pt[0]) || !std::isfinite (pt[1]) || !std::isfinite (pt[2]))
108  continue;
109 
110  pt_t = tr * pt;
111 
112  memcpy (data_out + x_idx_offset_, &pt_t[0], sizeof (float));
113  memcpy (data_out + y_idx_offset_, &pt_t[1], sizeof (float));
114  memcpy (data_out + z_idx_offset_, &pt_t[2], sizeof (float));
115  }
116  }
117 }
118 
119 
120 template <typename PointSource, typename PointTarget, typename Scalar> void
122  PointCloudSource &output, const Matrix4 &guess)
123 {
124  // Point cloud containing the correspondences of each point in <input, indices>
125  PointCloudSourcePtr input_transformed (new PointCloudSource);
126 
127  nr_iterations_ = 0;
128  converged_ = false;
129 
130  // Initialise final transformation to the guessed one
131  final_transformation_ = guess;
132 
133  // If the guessed transformation is non identity
134  if (guess != Matrix4::Identity ())
135  {
136  input_transformed->resize (input_->size ());
137  // Apply guessed transformation prior to search for neighbours
138  transformCloud (*input_, *input_transformed, guess);
139  }
140  else
141  *input_transformed = *input_;
142 
143  transformation_ = Matrix4::Identity ();
144 
145  // Make blobs if necessary
146  determineRequiredBlobData ();
147  PCLPointCloud2::Ptr target_blob (new PCLPointCloud2);
148  if (need_target_blob_)
149  pcl::toPCLPointCloud2 (*target_, *target_blob);
150 
151  // Pass in the default target for the Correspondence Estimation/Rejection code
152  correspondence_estimation_->setInputTarget (target_);
153  if (correspondence_estimation_->requiresTargetNormals ())
154  correspondence_estimation_->setTargetNormals (target_blob);
155  // Correspondence Rejectors need a binary blob
156  for (std::size_t i = 0; i < correspondence_rejectors_.size (); ++i)
157  {
158  registration::CorrespondenceRejector::Ptr& rej = correspondence_rejectors_[i];
159  if (rej->requiresTargetPoints ())
160  rej->setTargetPoints (target_blob);
161  if (rej->requiresTargetNormals () && target_has_normals_)
162  rej->setTargetNormals (target_blob);
163  }
164 
165  convergence_criteria_->setMaximumIterations (max_iterations_);
166  convergence_criteria_->setRelativeMSE (euclidean_fitness_epsilon_);
167  convergence_criteria_->setTranslationThreshold (transformation_epsilon_);
168  if (transformation_rotation_epsilon_ > 0)
169  convergence_criteria_->setRotationThreshold (transformation_rotation_epsilon_);
170  else
171  convergence_criteria_->setRotationThreshold (1.0 - transformation_epsilon_);
172 
173  // Repeat until convergence
174  do
175  {
176  // Get blob data if needed
177  PCLPointCloud2::Ptr input_transformed_blob;
178  if (need_source_blob_)
179  {
180  input_transformed_blob.reset (new PCLPointCloud2);
181  toPCLPointCloud2 (*input_transformed, *input_transformed_blob);
182  }
183  // Save the previously estimated transformation
184  previous_transformation_ = transformation_;
185 
186  // Set the source each iteration, to ensure the dirty flag is updated
187  correspondence_estimation_->setInputSource (input_transformed);
188  if (correspondence_estimation_->requiresSourceNormals ())
189  correspondence_estimation_->setSourceNormals (input_transformed_blob);
190  // Estimate correspondences
191  if (use_reciprocal_correspondence_)
192  correspondence_estimation_->determineReciprocalCorrespondences (*correspondences_, corr_dist_threshold_);
193  else
194  correspondence_estimation_->determineCorrespondences (*correspondences_, corr_dist_threshold_);
195 
196  //if (correspondence_rejectors_.empty ())
197  CorrespondencesPtr temp_correspondences (new Correspondences (*correspondences_));
198  for (std::size_t i = 0; i < correspondence_rejectors_.size (); ++i)
199  {
200  registration::CorrespondenceRejector::Ptr& rej = correspondence_rejectors_[i];
201  PCL_DEBUG ("Applying a correspondence rejector method: %s.\n", rej->getClassName ().c_str ());
202  if (rej->requiresSourcePoints ())
203  rej->setSourcePoints (input_transformed_blob);
204  if (rej->requiresSourceNormals () && source_has_normals_)
205  rej->setSourceNormals (input_transformed_blob);
206  rej->setInputCorrespondences (temp_correspondences);
207  rej->getCorrespondences (*correspondences_);
208  // Modify input for the next iteration
209  if (i < correspondence_rejectors_.size () - 1)
210  *temp_correspondences = *correspondences_;
211  }
212 
213  std::size_t cnt = correspondences_->size ();
214  // Check whether we have enough correspondences
215  if (static_cast<int> (cnt) < min_number_correspondences_)
216  {
217  PCL_ERROR ("[pcl::%s::computeTransformation] Not enough correspondences found. Relax your threshold parameters.\n", getClassName ().c_str ());
219  converged_ = false;
220  break;
221  }
222 
223  // Estimate the transform
224  transformation_estimation_->estimateRigidTransformation (*input_transformed, *target_, *correspondences_, transformation_);
225 
226  // Transform the data
227  transformCloud (*input_transformed, *input_transformed, transformation_);
228 
229  // Obtain the final transformation
230  final_transformation_ = transformation_ * final_transformation_;
231 
232  ++nr_iterations_;
233 
234  // Update the vizualization of icp convergence
235  //if (update_visualizer_ != 0)
236  // update_visualizer_(output, source_indices_good, *target_, target_indices_good );
237 
238  converged_ = static_cast<bool> ((*convergence_criteria_));
239  }
240  while (convergence_criteria_->getConvergenceState() == pcl::registration::DefaultConvergenceCriteria<Scalar>::CONVERGENCE_CRITERIA_NOT_CONVERGED);
241 
242  // Transform the input cloud using the final transformation
243  PCL_DEBUG ("Transformation is:\n\t%5f\t%5f\t%5f\t%5f\n\t%5f\t%5f\t%5f\t%5f\n\t%5f\t%5f\t%5f\t%5f\n\t%5f\t%5f\t%5f\t%5f\n",
244  final_transformation_ (0, 0), final_transformation_ (0, 1), final_transformation_ (0, 2), final_transformation_ (0, 3),
245  final_transformation_ (1, 0), final_transformation_ (1, 1), final_transformation_ (1, 2), final_transformation_ (1, 3),
246  final_transformation_ (2, 0), final_transformation_ (2, 1), final_transformation_ (2, 2), final_transformation_ (2, 3),
247  final_transformation_ (3, 0), final_transformation_ (3, 1), final_transformation_ (3, 2), final_transformation_ (3, 3));
248 
249  // Copy all the values
250  output = *input_;
251  // Transform the XYZ + normals
252  transformCloud (*input_, output, final_transformation_);
253 }
254 
255 
256 template <typename PointSource, typename PointTarget, typename Scalar> void
258 {
259  need_source_blob_ = false;
260  need_target_blob_ = false;
261  // Check estimator
262  need_source_blob_ |= correspondence_estimation_->requiresSourceNormals ();
263  need_target_blob_ |= correspondence_estimation_->requiresTargetNormals ();
264  // Add warnings if necessary
265  if (correspondence_estimation_->requiresSourceNormals () && !source_has_normals_)
266  {
267  PCL_WARN("[pcl::%s::determineRequiredBlobData] Estimator expects source normals, but we can't provide them.\n", getClassName ().c_str ());
268  }
269  if (correspondence_estimation_->requiresTargetNormals () && !target_has_normals_)
270  {
271  PCL_WARN("[pcl::%s::determineRequiredBlobData] Estimator expects target normals, but we can't provide them.\n", getClassName ().c_str ());
272  }
273  // Check rejectors
274  for (std::size_t i = 0; i < correspondence_rejectors_.size (); i++)
275  {
276  registration::CorrespondenceRejector::Ptr& rej = correspondence_rejectors_[i];
277  need_source_blob_ |= rej->requiresSourcePoints ();
278  need_source_blob_ |= rej->requiresSourceNormals ();
279  need_target_blob_ |= rej->requiresTargetPoints ();
280  need_target_blob_ |= rej->requiresTargetNormals ();
281  if (rej->requiresSourceNormals () && !source_has_normals_)
282  {
283  PCL_WARN("[pcl::%s::determineRequiredBlobData] Rejector %s expects source normals, but we can't provide them.\n", getClassName ().c_str (), rej->getClassName ().c_str ());
284  }
285  if (rej->requiresTargetNormals () && !target_has_normals_)
286  {
287  PCL_WARN("[pcl::%s::determineRequiredBlobData] Rejector %s expects target normals, but we can't provide them.\n", getClassName ().c_str (), rej->getClassName ().c_str ());
288  }
289  }
290 }
291 
292 
293 template <typename PointSource, typename PointTarget, typename Scalar> void
295  const PointCloudSource &input,
296  PointCloudSource &output,
297  const Matrix4 &transform)
298 {
299  pcl::transformPointCloudWithNormals (input, output, transform);
300 }
301 
302 } // namespace pcl
303 
304 #endif /* PCL_REGISTRATION_IMPL_ICP_HPP_ */
305 
shared_ptr< ::pcl::PCLPointCloud2 > Ptr
shared_ptr< CorrespondenceRejector > Ptr
DefaultConvergenceCriteria represents an instantiation of ConvergenceCriteria, and implements the fol...
typename Registration< PointSource, PointTarget, float >::Matrix4 Matrix4
Definition: icp.h:135
void transformPointCloudWithNormals(const pcl::PointCloud< PointT > &cloud_in, pcl::PointCloud< PointT > &cloud_out, const Eigen::Transform< Scalar, 3, Eigen::Affine > &transform, bool copy_all_fields)
Transform a point cloud and rotate its normals using an Eigen transform.
Definition: transforms.hpp:312
virtual void determineRequiredBlobData()
Looks at the Estimators and Rejectors and determines whether their blob-setter methods need to be cal...
Definition: icp.hpp:257
virtual void transformCloud(const PointCloudSource &input, PointCloudSource &output, const Matrix4 &transform)
Apply a rigid transform to a given dataset.
Definition: icp.hpp:52
typename Registration< PointSource, PointTarget, float >::PointCloudSource PointCloudSource
Definition: icp.h:97
void toPCLPointCloud2(const pcl::PointCloud< PointT > &cloud, pcl::PCLPointCloud2 &msg)
Convert a pcl::PointCloud<T> object to a PCLPointCloud2 binary data blob.
Definition: conversions.h:241
std::uint8_t uint8_t
Definition: types.h:54
virtual void transformCloud(const PointCloudSource &input, PointCloudSource &output, const Matrix4 &transform)
Apply a rigid transform to a given dataset.
Definition: icp.hpp:294
void computeTransformation(PointCloudSource &output, const Matrix4 &guess) override
Rigid transformation computation method with initial guess.
Definition: icp.hpp:121
std::vector< pcl::Correspondence, Eigen::aligned_allocator< pcl::Correspondence > > Correspondences
shared_ptr< Correspondences > CorrespondencesPtr
typename PointCloudSource::Ptr PointCloudSourcePtr
Definition: icp.h:98