Point Cloud Library (PCL)  1.7.2
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 (size_t i = 0; i < input.size (); ++i)
64  {
65  const uint8_t* data_in = reinterpret_cast<const uint8_t*> (&input[i]);
66  uint8_t* data_out = reinterpret_cast<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 (!pcl_isfinite (pt[0]) || !pcl_isfinite (pt[1]) || !pcl_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 (!pcl_isfinite (nt[0]) || !pcl_isfinite (nt[1]) || !pcl_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 (size_t i = 0; i < input.size (); ++i)
97  {
98  const uint8_t* data_in = reinterpret_cast<const uint8_t*> (&input[i]);
99  uint8_t* data_out = reinterpret_cast<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 (!pcl_isfinite (pt[0]) || !pcl_isfinite (pt[1]) || !pcl_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 (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  convergence_criteria_->setRotationThreshold (1.0 - transformation_epsilon_);
167 
168  // Repeat until convergence
169  do
170  {
171  // Get blob data if needed
172  PCLPointCloud2::Ptr input_transformed_blob;
173  if (need_source_blob_)
174  {
175  input_transformed_blob.reset (new PCLPointCloud2);
176  toPCLPointCloud2 (*input_transformed, *input_transformed_blob);
177  }
178  // Save the previously estimated transformation
179  previous_transformation_ = transformation_;
180 
181  // Set the source each iteration, to ensure the dirty flag is updated
182  correspondence_estimation_->setInputSource (input_transformed);
183  if (correspondence_estimation_->requiresSourceNormals ())
184  correspondence_estimation_->setSourceNormals (input_transformed_blob);
185  // Estimate correspondences
186  if (use_reciprocal_correspondence_)
187  correspondence_estimation_->determineReciprocalCorrespondences (*correspondences_, corr_dist_threshold_);
188  else
189  correspondence_estimation_->determineCorrespondences (*correspondences_, corr_dist_threshold_);
190 
191  //if (correspondence_rejectors_.empty ())
192  CorrespondencesPtr temp_correspondences (new Correspondences (*correspondences_));
193  for (size_t i = 0; i < correspondence_rejectors_.size (); ++i)
194  {
195  registration::CorrespondenceRejector::Ptr& rej = correspondence_rejectors_[i];
196  PCL_DEBUG ("Applying a correspondence rejector method: %s.\n", rej->getClassName ().c_str ());
197  if (rej->requiresSourcePoints ())
198  rej->setSourcePoints (input_transformed_blob);
199  if (rej->requiresSourceNormals () && source_has_normals_)
200  rej->setSourceNormals (input_transformed_blob);
201  rej->setInputCorrespondences (temp_correspondences);
202  rej->getCorrespondences (*correspondences_);
203  // Modify input for the next iteration
204  if (i < correspondence_rejectors_.size () - 1)
205  *temp_correspondences = *correspondences_;
206  }
207 
208  size_t cnt = correspondences_->size ();
209  // Check whether we have enough correspondences
210  if (static_cast<int> (cnt) < min_number_correspondences_)
211  {
212  PCL_ERROR ("[pcl::%s::computeTransformation] Not enough correspondences found. Relax your threshold parameters.\n", getClassName ().c_str ());
214  converged_ = false;
215  break;
216  }
217 
218  // Estimate the transform
219  transformation_estimation_->estimateRigidTransformation (*input_transformed, *target_, *correspondences_, transformation_);
220 
221  // Tranform the data
222  transformCloud (*input_transformed, *input_transformed, transformation_);
223 
224  // Obtain the final transformation
225  final_transformation_ = transformation_ * final_transformation_;
226 
227  ++nr_iterations_;
228 
229  // Update the vizualization of icp convergence
230  //if (update_visualizer_ != 0)
231  // update_visualizer_(output, source_indices_good, *target_, target_indices_good );
232 
233  converged_ = static_cast<bool> ((*convergence_criteria_));
234  }
235  while (!converged_);
236 
237  // Transform the input cloud using the final transformation
238  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",
239  final_transformation_ (0, 0), final_transformation_ (0, 1), final_transformation_ (0, 2), final_transformation_ (0, 3),
240  final_transformation_ (1, 0), final_transformation_ (1, 1), final_transformation_ (1, 2), final_transformation_ (1, 3),
241  final_transformation_ (2, 0), final_transformation_ (2, 1), final_transformation_ (2, 2), final_transformation_ (2, 3),
242  final_transformation_ (3, 0), final_transformation_ (3, 1), final_transformation_ (3, 2), final_transformation_ (3, 3));
243 
244  // Copy all the values
245  output = *input_;
246  // Transform the XYZ + normals
247  transformCloud (*input_, output, final_transformation_);
248 }
249 
250 template <typename PointSource, typename PointTarget, typename Scalar> void
252 {
253  need_source_blob_ = false;
254  need_target_blob_ = false;
255  // Check estimator
256  need_source_blob_ |= correspondence_estimation_->requiresSourceNormals ();
257  need_target_blob_ |= correspondence_estimation_->requiresTargetNormals ();
258  // Add warnings if necessary
259  if (correspondence_estimation_->requiresSourceNormals () && !source_has_normals_)
260  {
261  PCL_WARN("[pcl::%s::determineRequiredBlobData] Estimator expects source normals, but we can't provide them.\n", getClassName ().c_str ());
262  }
263  if (correspondence_estimation_->requiresTargetNormals () && !target_has_normals_)
264  {
265  PCL_WARN("[pcl::%s::determineRequiredBlobData] Estimator expects target normals, but we can't provide them.\n", getClassName ().c_str ());
266  }
267  // Check rejectors
268  for (size_t i = 0; i < correspondence_rejectors_.size (); i++)
269  {
270  registration::CorrespondenceRejector::Ptr& rej = correspondence_rejectors_[i];
271  need_source_blob_ |= rej->requiresSourcePoints ();
272  need_source_blob_ |= rej->requiresSourceNormals ();
273  need_target_blob_ |= rej->requiresTargetPoints ();
274  need_target_blob_ |= rej->requiresTargetNormals ();
275  if (rej->requiresSourceNormals () && !source_has_normals_)
276  {
277  PCL_WARN("[pcl::%s::determineRequiredBlobData] Rejector %s expects source normals, but we can't provide them.\n", getClassName ().c_str (), rej->getClassName ().c_str ());
278  }
279  if (rej->requiresTargetNormals () && !target_has_normals_)
280  {
281  PCL_WARN("[pcl::%s::determineRequiredBlobData] Rejector %s expects target normals, but we can't provide them.\n", getClassName ().c_str (), rej->getClassName ().c_str ());
282  }
283  }
284 }
285 
286 ///////////////////////////////////////////////////////////////////////////////////////////
287 template <typename PointSource, typename PointTarget, typename Scalar> void
289  const PointCloudSource &input,
290  PointCloudSource &output,
291  const Matrix4 &transform)
292 {
293  pcl::transformPointCloudWithNormals (input, output, transform);
294 }
295 
296 
297 #endif /* PCL_REGISTRATION_IMPL_ICP_HPP_ */
DefaultConvergenceCriteria represents an instantiation of ConvergenceCriteria, and implements the fol...
size_t size() const
Definition: point_cloud.h:440
virtual void determineRequiredBlobData()
Looks at the Estimators and Rejectors and determines whether their blob-setter methods need to be cal...
Definition: icp.hpp:251
void transformPointCloudWithNormals(const pcl::PointCloud< PointT > &cloud_in, pcl::PointCloud< PointT > &cloud_out, const Eigen::Transform< Scalar, 3, Eigen::Affine > &transform)
Transform a point cloud and rotate its normals using an Eigen transform.
Definition: transforms.hpp:143
boost::shared_ptr< ::pcl::PCLPointCloud2 > Ptr
virtual void transformCloud(const PointCloudSource &input, PointCloudSource &output, const Matrix4 &transform)
Apply a rigid transform to a given dataset.
Definition: icp.hpp:49
std::vector< pcl::Correspondence, Eigen::aligned_allocator< pcl::Correspondence > > Correspondences
boost::shared_ptr< Correspondences > CorrespondencesPtr
Registration< PointSource, PointTarget, Scalar >::Matrix4 Matrix4
Definition: icp.h:135
virtual void computeTransformation(PointCloudSource &output, const Matrix4 &guess)
Rigid transformation computation method with initial guess.
Definition: icp.hpp:119
void toPCLPointCloud2(const pcl::PointCloud< PointT > &cloud, pcl::PCLPointCloud2 &msg)
Convert a pcl::PointCloud object to a PCLPointCloud2 binary data blob.
Definition: conversions.h:237
virtual void transformCloud(const PointCloudSource &input, PointCloudSource &output, const Matrix4 &transform)
Apply a rigid transform to a given dataset.
Definition: icp.hpp:288
boost::shared_ptr< CorrespondenceRejector > Ptr
PointCloudSource::Ptr PointCloudSourcePtr
Definition: icp.h:98