Point Cloud Library (PCL)  1.7.0
gicp.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_GICP_H_
42 #define PCL_GICP_H_
43 
44 #include <pcl/registration/icp.h>
45 #include <pcl/registration/bfgs.h>
46 
47 namespace pcl
48 {
49  /** \brief GeneralizedIterativeClosestPoint is an ICP variant that implements the
50  * generalized iterative closest point algorithm as described by Alex Segal et al. in
51  * http://www.stanford.edu/~avsegal/resources/papers/Generalized_ICP.pdf
52  * The approach is based on using anistropic cost functions to optimize the alignment
53  * after closest point assignments have been made.
54  * The original code uses GSL and ANN while in ours we use an eigen mapped BFGS and
55  * FLANN.
56  * \author Nizar Sallem
57  * \ingroup registration
58  */
59  template <typename PointSource, typename PointTarget>
60  class GeneralizedIterativeClosestPoint : public IterativeClosestPoint<PointSource, PointTarget>
61  {
62  public:
81 
85 
89 
92 
95 
96  typedef boost::shared_ptr< GeneralizedIterativeClosestPoint<PointSource, PointTarget> > Ptr;
97  typedef boost::shared_ptr< const GeneralizedIterativeClosestPoint<PointSource, PointTarget> > ConstPtr;
98 
99 
100  typedef Eigen::Matrix<double, 6, 1> Vector6d;
101 
102  /** \brief Empty constructor. */
104  : k_correspondences_(20)
105  , gicp_epsilon_(0.001)
106  , rotation_epsilon_(2e-3)
107  , input_covariances_(0)
109  , mahalanobis_(0)
111  {
113  reg_name_ = "GeneralizedIterativeClosestPoint";
114  max_iterations_ = 200;
119  this, _1, _2, _3, _4, _5);
120  }
121 
122  /** \brief Provide a pointer to the input dataset
123  * \param cloud the const boost shared pointer to a PointCloud message
124  */
125  PCL_DEPRECATED (void setInputCloud (const PointCloudSourceConstPtr &cloud), "[pcl::registration::GeneralizedIterativeClosestPoint::setInputCloud] setInputCloud is deprecated. Please use setInputSource instead.");
126 
127  /** \brief Provide a pointer to the input dataset
128  * \param cloud the const boost shared pointer to a PointCloud message
129  */
130  inline void
132  {
133 
134  if (cloud->points.empty ())
135  {
136  PCL_ERROR ("[pcl::%s::setInputSource] Invalid or empty point cloud dataset given!\n", getClassName ().c_str ());
137  return;
138  }
139  PointCloudSource input = *cloud;
140  // Set all the point.data[3] values to 1 to aid the rigid transformation
141  for (size_t i = 0; i < input.size (); ++i)
142  input[i].data[3] = 1.0;
143 
145  input_covariances_.reserve (input_->size ());
146  }
147 
148  /** \brief Provide a pointer to the input target (e.g., the point cloud that we want to align the input source to)
149  * \param[in] target the input point cloud target
150  */
151  inline void
153  {
155  target_covariances_.reserve (target_->size ());
156  }
157 
158  /** \brief Estimate a rigid rotation transformation between a source and a target point cloud using an iterative
159  * non-linear Levenberg-Marquardt approach.
160  * \param[in] cloud_src the source point cloud dataset
161  * \param[in] indices_src the vector of indices describing the points of interest in \a cloud_src
162  * \param[in] cloud_tgt the target point cloud dataset
163  * \param[in] indices_tgt the vector of indices describing the correspondences of the interst points from \a indices_src
164  * \param[out] transformation_matrix the resultant transformation matrix
165  */
166  void
168  const std::vector<int> &indices_src,
169  const PointCloudTarget &cloud_tgt,
170  const std::vector<int> &indices_tgt,
171  Eigen::Matrix4f &transformation_matrix);
172 
173  /** \brief \return Mahalanobis distance matrix for the given point index */
174  inline const Eigen::Matrix3d& mahalanobis(size_t index) const
175  {
176  assert(index < mahalanobis_.size());
177  return mahalanobis_[index];
178  }
179 
180  /** \brief Computes rotation matrix derivative.
181  * rotation matrix is obtainded from rotation angles x[3], x[4] and x[5]
182  * \return d/d_rx, d/d_ry and d/d_rz respectively in g[3], g[4] and g[5]
183  * param x array representing 3D transformation
184  * param R rotation matrix
185  * param g gradient vector
186  */
187  void
188  computeRDerivative(const Vector6d &x, const Eigen::Matrix3d &R, Vector6d &g) const;
189 
190  /** \brief Set the rotation epsilon (maximum allowable difference between two
191  * consecutive rotations) in order for an optimization to be considered as having
192  * converged to the final solution.
193  * \param epsilon the rotation epsilon
194  */
195  inline void
196  setRotationEpsilon (double epsilon) { rotation_epsilon_ = epsilon; }
197 
198  /** \brief Get the rotation epsilon (maximum allowable difference between two
199  * consecutive rotations) as set by the user.
200  */
201  inline double
203 
204  /** \brief Set the number of neighbors used when selecting a point neighbourhood
205  * to compute covariances.
206  * A higher value will bring more accurate covariance matrix but will make
207  * covariances computation slower.
208  * \param k the number of neighbors to use when computing covariances
209  */
210  void
212 
213  /** \brief Get the number of neighbors used when computing covariances as set by
214  * the user
215  */
216  int
218 
219  /** set maximum number of iterations at the optimization step
220  * \param[in] max maximum number of iterations for the optimizer
221  */
222  void
224 
225  ///\return maximum number of iterations at the optimization step
226  int
228 
229  protected:
230 
231  /** \brief The number of neighbors used for covariances computation.
232  * default: 20
233  */
235 
236  /** \brief The epsilon constant for gicp paper; this is NOT the convergence
237  * tolerence
238  * default: 0.001
239  */
241 
242  /** The epsilon constant for rotation error. (In GICP the transformation epsilon
243  * is split in rotation part and translation part).
244  * default: 2e-3
245  */
247 
248  /** \brief base transformation */
249  Eigen::Matrix4f base_transformation_;
250 
251  /** \brief Temporary pointer to the source dataset. */
253 
254  /** \brief Temporary pointer to the target dataset. */
256 
257  /** \brief Temporary pointer to the source dataset indices. */
258  const std::vector<int> *tmp_idx_src_;
259 
260  /** \brief Temporary pointer to the target dataset indices. */
261  const std::vector<int> *tmp_idx_tgt_;
262 
263 
264  /** \brief Input cloud points covariances. */
265  std::vector<Eigen::Matrix3d> input_covariances_;
266 
267  /** \brief Target cloud points covariances. */
268  std::vector<Eigen::Matrix3d> target_covariances_;
269 
270  /** \brief Mahalanobis matrices holder. */
271  std::vector<Eigen::Matrix3d> mahalanobis_;
272 
273  /** \brief maximum number of optimizations */
275 
276  /** \brief compute points covariances matrices according to the K nearest
277  * neighbors. K is set via setCorrespondenceRandomness() methode.
278  * \param cloud pointer to point cloud
279  * \param tree KD tree performer for nearest neighbors search
280  * \return cloud_covariance covariances matrices for each point in the cloud
281  */
282  template<typename PointT>
284  const typename pcl::search::KdTree<PointT>::Ptr tree,
285  std::vector<Eigen::Matrix3d>& cloud_covariances);
286 
287  /** \return trace of mat1^t . mat2
288  * \param mat1 matrix of dimension nxm
289  * \param mat2 matrix of dimension nxp
290  */
291  inline double
292  matricesInnerProd(const Eigen::MatrixXd& mat1, const Eigen::MatrixXd& mat2) const
293  {
294  double r = 0.;
295  size_t n = mat1.rows();
296  // tr(mat1^t.mat2)
297  for(size_t i = 0; i < n; i++)
298  for(size_t j = 0; j < n; j++)
299  r += mat1 (j, i) * mat2 (i,j);
300  return r;
301  }
302 
303  /** \brief Rigid transformation computation method with initial guess.
304  * \param output the transformed input point cloud dataset using the rigid transformation found
305  * \param guess the initial guess of the transformation to compute
306  */
307  void
308  computeTransformation (PointCloudSource &output, const Eigen::Matrix4f &guess);
309 
310  /** \brief Search for the closest nearest neighbor of a given point.
311  * \param query the point to search a nearest neighbour for
312  * \param index vector of size 1 to store the index of the nearest neighbour found
313  * \param distance vector of size 1 to store the distance to nearest neighbour found
314  */
315  inline bool
316  searchForNeighbors (const PointSource &query, std::vector<int>& index, std::vector<float>& distance)
317  {
318  int k = tree_->nearestKSearch (query, 1, index, distance);
319  if (k == 0)
320  return (false);
321  return (true);
322  }
323 
324  /// \brief compute transformation matrix from transformation matrix
325  void applyState(Eigen::Matrix4f &t, const Vector6d& x) const;
326 
327  /// \brief optimization functor structure
329  {
331  : BFGSDummyFunctor<double,6> (), gicp_(gicp) {}
332  double operator() (const Vector6d& x);
333  void df(const Vector6d &x, Vector6d &df);
334  void fdf(const Vector6d &x, double &f, Vector6d &df);
335 
337  };
338 
339  boost::function<void(const pcl::PointCloud<PointSource> &cloud_src,
340  const std::vector<int> &src_indices,
341  const pcl::PointCloud<PointTarget> &cloud_tgt,
342  const std::vector<int> &tgt_indices,
343  Eigen::Matrix4f &transformation_matrix)> rigid_transformation_estimation_;
344  };
345 }
346 
347 #include <pcl/registration/impl/gicp.hpp>
348 
349 #endif //#ifndef PCL_GICP_H_