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