Point Cloud Library (PCL)  1.12.1-dev
transformation_estimation_lm.h
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 #pragma once
42 
43 #include <pcl/registration/transformation_estimation.h>
44 #include <pcl/registration/warp_point_rigid.h>
45 #include <pcl/memory.h>
46 
47 namespace pcl {
48 namespace registration {
49 /** @b TransformationEstimationLM implements Levenberg Marquardt-based
50  * estimation of the transformation aligning the given correspondences.
51  *
52  * \note The class is templated on the source and target point types as well as on the
53  * output scalar of the transformation matrix (i.e., float or double). Default: float.
54  * \author Radu B. Rusu
55  * \ingroup registration
56  */
57 template <typename PointSource, typename PointTarget, typename MatScalar = float>
59 : public TransformationEstimation<PointSource, PointTarget, MatScalar> {
61  using PointCloudSourcePtr = typename PointCloudSource::Ptr;
62  using PointCloudSourceConstPtr = typename PointCloudSource::ConstPtr;
63 
65 
66  using PointIndicesPtr = PointIndices::Ptr;
67  using PointIndicesConstPtr = PointIndices::ConstPtr;
68 
69 public:
70  using Ptr =
71  shared_ptr<TransformationEstimationLM<PointSource, PointTarget, MatScalar>>;
72  using ConstPtr =
73  shared_ptr<const TransformationEstimationLM<PointSource, PointTarget, MatScalar>>;
74 
75  using VectorX = Eigen::Matrix<MatScalar, Eigen::Dynamic, 1>;
76  using Vector4 = Eigen::Matrix<MatScalar, 4, 1>;
77  using Matrix4 =
79 
80  /** \brief Constructor. */
82 
83  /** \brief Copy constructor.
84  * \param[in] src the TransformationEstimationLM object to copy into this
85  */
87  : tmp_src_(src.tmp_src_)
88  , tmp_tgt_(src.tmp_tgt_)
91  , warp_point_(src.warp_point_){};
92 
93  /** \brief Copy operator.
94  * \param[in] src the TransformationEstimationLM object to copy into this
95  */
98  {
99  tmp_src_ = src.tmp_src_;
100  tmp_tgt_ = src.tmp_tgt_;
103  warp_point_ = src.warp_point_;
104  }
105 
106  /** \brief Destructor. */
107  ~TransformationEstimationLM() override = default;
108 
109  /** \brief Estimate a rigid rotation transformation between a source and a target
110  * point cloud using LM. \param[in] cloud_src the source point cloud dataset
111  * \param[in] cloud_tgt the target point cloud dataset
112  * \param[out] transformation_matrix the resultant transformation matrix
113  */
114  inline void
116  const pcl::PointCloud<PointTarget>& cloud_tgt,
117  Matrix4& transformation_matrix) const override;
118 
119  /** \brief Estimate a rigid rotation transformation between a source and a target
120  * point cloud using LM. \param[in] cloud_src the source point cloud dataset
121  * \param[in] indices_src the vector of indices describing the points of interest in
122  * \a cloud_src
123  * \param[in] cloud_tgt the target point cloud dataset
124  * \param[out] transformation_matrix the resultant transformation matrix
125  */
126  inline void
128  const pcl::Indices& indices_src,
129  const pcl::PointCloud<PointTarget>& cloud_tgt,
130  Matrix4& transformation_matrix) const override;
131 
132  /** \brief Estimate a rigid rotation transformation between a source and a target
133  * point cloud using LM. \param[in] cloud_src the source point cloud dataset
134  * \param[in] indices_src the vector of indices describing the points of interest in
135  * \a cloud_src
136  * \param[in] cloud_tgt the target point cloud dataset
137  * \param[in] indices_tgt the vector of indices describing the correspondences of the
138  * interest points from \a indices_src
139  * \param[out] transformation_matrix the resultant transformation matrix
140  */
141  inline void
143  const pcl::Indices& indices_src,
144  const pcl::PointCloud<PointTarget>& cloud_tgt,
145  const pcl::Indices& indices_tgt,
146  Matrix4& transformation_matrix) const override;
147 
148  /** \brief Estimate a rigid rotation transformation between a source and a target
149  * point cloud using LM. \param[in] cloud_src the source point cloud dataset
150  * \param[in] cloud_tgt the target point cloud dataset
151  * \param[in] correspondences the vector of correspondences between source and target
152  * point cloud \param[out] transformation_matrix the resultant transformation matrix
153  */
154  inline void
156  const pcl::PointCloud<PointTarget>& cloud_tgt,
157  const pcl::Correspondences& correspondences,
158  Matrix4& transformation_matrix) const override;
159 
160  /** \brief Set the function we use to warp points. Defaults to rigid 6D warp.
161  * \param[in] warp_fcn a shared pointer to an object that warps points
162  */
163  void
166  {
167  warp_point_ = warp_fcn;
168  }
169 
170 protected:
171  /** \brief Compute the distance between a source point and its corresponding target
172  * point \param[in] p_src The source point \param[in] p_tgt The target point \return
173  * The distance between \a p_src and \a p_tgt
174  *
175  * \note Older versions of PCL used this method internally for calculating the
176  * optimization gradient. Since PCL 1.7, a switch has been made to the
177  * computeDistance method using Vector4 types instead. This method is only
178  * kept for API compatibility reasons.
179  */
180  virtual MatScalar
181  computeDistance(const PointSource& p_src, const PointTarget& p_tgt) const
182  {
183  Vector4 s(p_src.x, p_src.y, p_src.z, 0);
184  Vector4 t(p_tgt.x, p_tgt.y, p_tgt.z, 0);
185  return ((s - t).norm());
186  }
187 
188  /** \brief Compute the distance between a source point and its corresponding target
189  * point \param[in] p_src The source point \param[in] p_tgt The target point \return
190  * The distance between \a p_src and \a p_tgt
191  *
192  * \note A different distance function can be defined by creating a subclass of
193  * TransformationEstimationLM and overriding this method.
194  * (See \a TransformationEstimationPointToPlane)
195  */
196  virtual MatScalar
197  computeDistance(const Vector4& p_src, const PointTarget& p_tgt) const
198  {
199  Vector4 t(p_tgt.x, p_tgt.y, p_tgt.z, 0);
200  return ((p_src - t).norm());
201  }
202 
203  /** \brief Temporary pointer to the source dataset. */
204  mutable const PointCloudSource* tmp_src_;
205 
206  /** \brief Temporary pointer to the target dataset. */
207  mutable const PointCloudTarget* tmp_tgt_;
208 
209  /** \brief Temporary pointer to the source dataset indices. */
210  mutable const pcl::Indices* tmp_idx_src_;
211 
212  /** \brief Temporary pointer to the target dataset indices. */
213  mutable const pcl::Indices* tmp_idx_tgt_;
214 
215  /** \brief The parameterized function used to warp the source to the target. */
218 
219  /** Base functor all the models that need non linear optimization must
220  * define their own one and implement operator() (const Eigen::VectorXd& x,
221  * Eigen::VectorXd& fvec) or operator() (const Eigen::VectorXf& x, Eigen::VectorXf&
222  * fvec) depending on the chosen _Scalar
223  */
224  template <typename _Scalar, int NX = Eigen::Dynamic, int NY = Eigen::Dynamic>
225  struct Functor {
226  using Scalar = _Scalar;
228  using InputType = Eigen::Matrix<_Scalar, InputsAtCompileTime, 1>;
229  using ValueType = Eigen::Matrix<_Scalar, ValuesAtCompileTime, 1>;
230  using JacobianType =
231  Eigen::Matrix<_Scalar, ValuesAtCompileTime, InputsAtCompileTime>;
232 
233  /** \brief Empty Constructor. */
235 
236  /** \brief Constructor
237  * \param[in] m_data_points number of data points to evaluate.
238  */
239  Functor(int m_data_points) : m_data_points_(m_data_points) {}
240 
241  /** \brief Destructor. */
242  virtual ~Functor() = default;
243 
244  /** \brief Get the number of values. */
245  int
246  values() const
247  {
248  return (m_data_points_);
249  }
250 
251  protected:
253  };
254 
255  struct OptimizationFunctor : public Functor<MatScalar> {
257 
258  /** Functor constructor
259  * \param[in] m_data_points the number of data points to evaluate
260  * \param[in,out] estimator pointer to the estimator object
261  */
262  OptimizationFunctor(int m_data_points, const TransformationEstimationLM* estimator)
263  : Functor<MatScalar>(m_data_points), estimator_(estimator)
264  {}
265 
266  /** Copy constructor
267  * \param[in] src the optimization functor to copy into this
268  */
270  : Functor<MatScalar>(src.m_data_points_), estimator_()
271  {
272  *this = src;
273  }
274 
275  /** Copy operator
276  * \param[in] src the optimization functor to copy into this
277  */
278  inline OptimizationFunctor&
280  {
282  estimator_ = src.estimator_;
283  return (*this);
284  }
285 
286  /** \brief Destructor. */
287  ~OptimizationFunctor() override = default;
288 
289  /** Fill fvec from x. For the current state vector x fill the f values
290  * \param[in] x state vector
291  * \param[out] fvec f values vector
292  */
293  int
294  operator()(const VectorX& x, VectorX& fvec) const;
295 
297  };
298 
299  struct OptimizationFunctorWithIndices : public Functor<MatScalar> {
301 
302  /** Functor constructor
303  * \param[in] m_data_points the number of data points to evaluate
304  * \param[in,out] estimator pointer to the estimator object
305  */
307  const TransformationEstimationLM* estimator)
308  : Functor<MatScalar>(m_data_points), estimator_(estimator)
309  {}
310 
311  /** Copy constructor
312  * \param[in] src the optimization functor to copy into this
313  */
315  : Functor<MatScalar>(src.m_data_points_), estimator_()
316  {
317  *this = src;
318  }
319 
320  /** Copy operator
321  * \param[in] src the optimization functor to copy into this
322  */
325  {
327  estimator_ = src.estimator_;
328  return (*this);
329  }
330 
331  /** \brief Destructor. */
332  ~OptimizationFunctorWithIndices() override = default;
333 
334  /** Fill fvec from x. For the current state vector x fill the f values
335  * \param[in] x state vector
336  * \param[out] fvec f values vector
337  */
338  int
339  operator()(const VectorX& x, VectorX& fvec) const;
340 
342  };
343 
344 public:
346 };
347 } // namespace registration
348 } // namespace pcl
349 
350 #include <pcl/registration/impl/transformation_estimation_lm.hpp>
shared_ptr< PointCloud< PointSource > > Ptr
Definition: point_cloud.h:413
shared_ptr< const PointCloud< PointSource > > ConstPtr
Definition: point_cloud.h:414
TransformationEstimation represents the base class for methods for transformation estimation based on...
TransformationEstimationLM implements Levenberg Marquardt-based estimation of the transformation alig...
pcl::registration::WarpPointRigid< PointSource, PointTarget, MatScalar >::Ptr warp_point_
The parameterized function used to warp the source to the target.
TransformationEstimationLM(const TransformationEstimationLM &src)
Copy constructor.
shared_ptr< const TransformationEstimationLM< PointSource, PointTarget, MatScalar > > ConstPtr
virtual MatScalar computeDistance(const PointSource &p_src, const PointTarget &p_tgt) const
Compute the distance between a source point and its corresponding target point.
shared_ptr< TransformationEstimationLM< PointSource, PointTarget, MatScalar > > Ptr
void estimateRigidTransformation(const pcl::PointCloud< PointSource > &cloud_src, const pcl::PointCloud< PointTarget > &cloud_tgt, Matrix4 &transformation_matrix) const override
Estimate a rigid rotation transformation between a source and a target point cloud using LM.
virtual MatScalar computeDistance(const Vector4 &p_src, const PointTarget &p_tgt) const
Compute the distance between a source point and its corresponding target point.
Eigen::Matrix< MatScalar, Eigen::Dynamic, 1 > VectorX
const pcl::Indices * tmp_idx_tgt_
Temporary pointer to the target dataset indices.
const pcl::Indices * tmp_idx_src_
Temporary pointer to the source dataset indices.
const PointCloudSource * tmp_src_
Temporary pointer to the source dataset.
~TransformationEstimationLM() override=default
Destructor.
typename TransformationEstimation< PointSource, PointTarget, MatScalar >::Matrix4 Matrix4
TransformationEstimationLM & operator=(const TransformationEstimationLM &src)
Copy operator.
const PointCloudTarget * tmp_tgt_
Temporary pointer to the target dataset.
void setWarpFunction(const typename WarpPointRigid< PointSource, PointTarget, MatScalar >::Ptr &warp_fcn)
Set the function we use to warp points.
shared_ptr< WarpPointRigid< PointSourceT, PointTargetT, Scalar > > Ptr
#define PCL_MAKE_ALIGNED_OPERATOR_NEW
Macro to signal a class requires a custom allocator.
Definition: memory.h:63
Defines functions, macros and traits for allocating and using memory.
std::vector< pcl::Correspondence, Eigen::aligned_allocator< pcl::Correspondence > > Correspondences
IndicesAllocator<> Indices
Type used for indices in PCL.
Definition: types.h:133
shared_ptr< ::pcl::PointIndices > Ptr
Definition: PointIndices.h:13
shared_ptr< const ::pcl::PointIndices > ConstPtr
Definition: PointIndices.h:14
Base functor all the models that need non linear optimization must define their own one and implement...
Eigen::Matrix< _Scalar, InputsAtCompileTime, 1 > InputType
Eigen::Matrix< _Scalar, ValuesAtCompileTime, 1 > ValueType
Eigen::Matrix< _Scalar, ValuesAtCompileTime, InputsAtCompileTime > JacobianType
OptimizationFunctor & operator=(const OptimizationFunctor &src)
Copy operator.
OptimizationFunctor(int m_data_points, const TransformationEstimationLM *estimator)
Functor constructor.
int operator()(const VectorX &x, VectorX &fvec) const
Fill fvec from x.
const TransformationEstimationLM< PointSource, PointTarget, MatScalar > * estimator_
OptimizationFunctorWithIndices(const OptimizationFunctorWithIndices &src)
Copy constructor.
OptimizationFunctorWithIndices(int m_data_points, const TransformationEstimationLM *estimator)
Functor constructor.
OptimizationFunctorWithIndices & operator=(const OptimizationFunctorWithIndices &src)
Copy operator.
const TransformationEstimationLM< PointSource, PointTarget, MatScalar > * estimator_