Point Cloud Library (PCL)  1.11.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. */
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 \param[in] cloud_tgt the target point cloud dataset \param[out]
123  * transformation_matrix the resultant transformation matrix
124  */
125  inline void
127  const std::vector<int>& indices_src,
128  const pcl::PointCloud<PointTarget>& cloud_tgt,
129  Matrix4& transformation_matrix) const override;
130 
131  /** \brief Estimate a rigid rotation transformation between a source and a target
132  * point cloud using LM. \param[in] cloud_src the source point cloud dataset
133  * \param[in] indices_src the vector of indices describing the points of interest in
134  * \a cloud_src \param[in] cloud_tgt the target point cloud dataset \param[in]
135  * indices_tgt the vector of indices describing the correspondences of the interest
136  * points from \a indices_src \param[out] transformation_matrix the resultant
137  * transformation matrix
138  */
139  inline void
141  const std::vector<int>& indices_src,
142  const pcl::PointCloud<PointTarget>& cloud_tgt,
143  const std::vector<int>& indices_tgt,
144  Matrix4& transformation_matrix) const override;
145 
146  /** \brief Estimate a rigid rotation transformation between a source and a target
147  * point cloud using LM. \param[in] cloud_src the source point cloud dataset
148  * \param[in] cloud_tgt the target point cloud dataset
149  * \param[in] correspondences the vector of correspondences between source and target
150  * point cloud \param[out] transformation_matrix the resultant transformation matrix
151  */
152  inline void
154  const pcl::PointCloud<PointTarget>& cloud_tgt,
155  const pcl::Correspondences& correspondences,
156  Matrix4& transformation_matrix) const override;
157 
158  /** \brief Set the function we use to warp points. Defaults to rigid 6D warp.
159  * \param[in] warp_fcn a shared pointer to an object that warps points
160  */
161  void
164  {
165  warp_point_ = warp_fcn;
166  }
167 
168 protected:
169  /** \brief Compute the distance between a source point and its corresponding target
170  * point \param[in] p_src The source point \param[in] p_tgt The target point \return
171  * The distance between \a p_src and \a p_tgt
172  *
173  * \note Older versions of PCL used this method internally for calculating the
174  * optimization gradient. Since PCL 1.7, a switch has been made to the
175  * computeDistance method using Vector4 types instead. This method is only
176  * kept for API compatibility reasons.
177  */
178  virtual MatScalar
179  computeDistance(const PointSource& p_src, const PointTarget& p_tgt) const
180  {
181  Vector4 s(p_src.x, p_src.y, p_src.z, 0);
182  Vector4 t(p_tgt.x, p_tgt.y, p_tgt.z, 0);
183  return ((s - t).norm());
184  }
185 
186  /** \brief Compute the distance between a source point and its corresponding target
187  * point \param[in] p_src The source point \param[in] p_tgt The target point \return
188  * The distance between \a p_src and \a p_tgt
189  *
190  * \note A different distance function can be defined by creating a subclass of
191  * TransformationEstimationLM and overriding this method.
192  * (See \a TransformationEstimationPointToPlane)
193  */
194  virtual MatScalar
195  computeDistance(const Vector4& p_src, const PointTarget& p_tgt) const
196  {
197  Vector4 t(p_tgt.x, p_tgt.y, p_tgt.z, 0);
198  return ((p_src - t).norm());
199  }
200 
201  /** \brief Temporary pointer to the source dataset. */
202  mutable const PointCloudSource* tmp_src_;
203 
204  /** \brief Temporary pointer to the target dataset. */
205  mutable const PointCloudTarget* tmp_tgt_;
206 
207  /** \brief Temporary pointer to the source dataset indices. */
208  mutable const std::vector<int>* tmp_idx_src_;
209 
210  /** \brief Temporary pointer to the target dataset indices. */
211  mutable const std::vector<int>* tmp_idx_tgt_;
212 
213  /** \brief The parameterized function used to warp the source to the target. */
216 
217  /** Base functor all the models that need non linear optimization must
218  * define their own one and implement operator() (const Eigen::VectorXd& x,
219  * Eigen::VectorXd& fvec) or operator() (const Eigen::VectorXf& x, Eigen::VectorXf&
220  * fvec) depending on the chosen _Scalar
221  */
222  template <typename _Scalar, int NX = Eigen::Dynamic, int NY = Eigen::Dynamic>
223  struct Functor {
224  using Scalar = _Scalar;
226  using InputType = Eigen::Matrix<_Scalar, InputsAtCompileTime, 1>;
227  using ValueType = Eigen::Matrix<_Scalar, ValuesAtCompileTime, 1>;
228  using JacobianType =
229  Eigen::Matrix<_Scalar, ValuesAtCompileTime, InputsAtCompileTime>;
230 
231  /** \brief Empty Constructor. */
233 
234  /** \brief Constructor
235  * \param[in] m_data_points number of data points to evaluate.
236  */
237  Functor(int m_data_points) : m_data_points_(m_data_points) {}
238 
239  /** \brief Destructor. */
240  virtual ~Functor() {}
241 
242  /** \brief Get the number of values. */
243  int
244  values() const
245  {
246  return (m_data_points_);
247  }
248 
249  protected:
251  };
252 
253  struct OptimizationFunctor : public Functor<MatScalar> {
255 
256  /** Functor constructor
257  * \param[in] m_data_points the number of data points to evaluate
258  * \param[in,out] estimator pointer to the estimator object
259  */
260  OptimizationFunctor(int m_data_points, const TransformationEstimationLM* estimator)
261  : Functor<MatScalar>(m_data_points), estimator_(estimator)
262  {}
263 
264  /** Copy constructor
265  * \param[in] src the optimization functor to copy into this
266  */
268  : Functor<MatScalar>(src.m_data_points_), estimator_()
269  {
270  *this = src;
271  }
272 
273  /** Copy operator
274  * \param[in] src the optimization functor to copy into this
275  */
276  inline OptimizationFunctor&
278  {
280  estimator_ = src.estimator_;
281  return (*this);
282  }
283 
284  /** \brief Destructor. */
286 
287  /** Fill fvec from x. For the current state vector x fill the f values
288  * \param[in] x state vector
289  * \param[out] fvec f values vector
290  */
291  int
292  operator()(const VectorX& x, VectorX& fvec) const;
293 
295  };
296 
297  struct OptimizationFunctorWithIndices : public Functor<MatScalar> {
299 
300  /** Functor constructor
301  * \param[in] m_data_points the number of data points to evaluate
302  * \param[in,out] estimator pointer to the estimator object
303  */
305  const TransformationEstimationLM* estimator)
306  : Functor<MatScalar>(m_data_points), estimator_(estimator)
307  {}
308 
309  /** Copy constructor
310  * \param[in] src the optimization functor to copy into this
311  */
313  : Functor<MatScalar>(src.m_data_points_), estimator_()
314  {
315  *this = src;
316  }
317 
318  /** Copy operator
319  * \param[in] src the optimization functor to copy into this
320  */
323  {
325  estimator_ = src.estimator_;
326  return (*this);
327  }
328 
329  /** \brief Destructor. */
331 
332  /** Fill fvec from x. For the current state vector x fill the f values
333  * \param[in] x state vector
334  * \param[out] fvec f values vector
335  */
336  int
337  operator()(const VectorX& x, VectorX& fvec) const;
338 
340  };
341 
342 public:
344 };
345 } // namespace registration
346 } // namespace pcl
347 
348 #include <pcl/registration/impl/transformation_estimation_lm.hpp>
pcl::registration::TransformationEstimationLM::computeDistance
virtual MatScalar computeDistance(const Vector4 &p_src, const PointTarget &p_tgt) const
Compute the distance between a source point and its corresponding target point.
Definition: transformation_estimation_lm.h:195
pcl
Definition: convolution.h:46
pcl::registration::TransformationEstimationLM::OptimizationFunctorWithIndices::operator=
OptimizationFunctorWithIndices & operator=(const OptimizationFunctorWithIndices &src)
Copy operator.
Definition: transformation_estimation_lm.h:322
pcl::registration::TransformationEstimationLM::Functor< MatScalar >::Scalar
MatScalar Scalar
Definition: transformation_estimation_lm.h:224
pcl::registration::TransformationEstimation< PointSource, PointTarget, float >::Matrix4
Eigen::Matrix< float, 4, 4 > Matrix4
Definition: transformation_estimation.h:64
pcl::registration::TransformationEstimationLM::Functor::~Functor
virtual ~Functor()
Destructor.
Definition: transformation_estimation_lm.h:240
pcl::registration::TransformationEstimationLM::tmp_idx_tgt_
const std::vector< int > * tmp_idx_tgt_
Temporary pointer to the target dataset indices.
Definition: transformation_estimation_lm.h:211
pcl::registration::TransformationEstimationLM< PointSource, PointTarget, float >::Vector4
Eigen::Matrix< float, 4, 1 > Vector4
Definition: transformation_estimation_lm.h:76
pcl::registration::TransformationEstimationLM::~TransformationEstimationLM
~TransformationEstimationLM()
Destructor.
Definition: transformation_estimation_lm.h:107
pcl::registration::TransformationEstimationLM::OptimizationFunctorWithIndices::operator()
int operator()(const VectorX &x, VectorX &fvec) const
Fill fvec from x.
Definition: transformation_estimation_lm.hpp:271
pcl::registration::TransformationEstimationLM::Functor::Functor
Functor()
Empty Constructor.
Definition: transformation_estimation_lm.h:232
pcl::registration::TransformationEstimationLM::tmp_idx_src_
const std::vector< int > * tmp_idx_src_
Temporary pointer to the source dataset indices.
Definition: transformation_estimation_lm.h:208
pcl::registration::TransformationEstimationLM::OptimizationFunctorWithIndices::~OptimizationFunctorWithIndices
~OptimizationFunctorWithIndices()
Destructor.
Definition: transformation_estimation_lm.h:330
pcl::PointCloud
PointCloud represents the base class in PCL for storing collections of 3D points.
Definition: distances.h:55
pcl::registration::TransformationEstimationLM::OptimizationFunctorWithIndices::OptimizationFunctorWithIndices
OptimizationFunctorWithIndices(const OptimizationFunctorWithIndices &src)
Copy constructor.
Definition: transformation_estimation_lm.h:312
pcl::registration::TransformationEstimationLM::Functor< MatScalar >::JacobianType
Eigen::Matrix< MatScalar, ValuesAtCompileTime, InputsAtCompileTime > JacobianType
Definition: transformation_estimation_lm.h:229
pcl::registration::TransformationEstimationLM::Functor< MatScalar >::ValueType
Eigen::Matrix< MatScalar, ValuesAtCompileTime, 1 > ValueType
Definition: transformation_estimation_lm.h:227
pcl::registration::TransformationEstimationLM::OptimizationFunctor::operator()
int operator()(const VectorX &x, VectorX &fvec) const
Fill fvec from x.
Definition: transformation_estimation_lm.hpp:243
pcl::registration::TransformationEstimationLM::operator=
TransformationEstimationLM & operator=(const TransformationEstimationLM &src)
Copy operator.
Definition: transformation_estimation_lm.h:97
pcl::registration::TransformationEstimationLM::OptimizationFunctor::OptimizationFunctor
OptimizationFunctor(int m_data_points, const TransformationEstimationLM *estimator)
Functor constructor.
Definition: transformation_estimation_lm.h:260
pcl::registration::TransformationEstimationLM::OptimizationFunctor::operator=
OptimizationFunctor & operator=(const OptimizationFunctor &src)
Copy operator.
Definition: transformation_estimation_lm.h:277
pcl::registration::TransformationEstimationLM::Matrix4
typename TransformationEstimation< PointSource, PointTarget, MatScalar >::Matrix4 Matrix4
Definition: transformation_estimation_lm.h:78
pcl::registration::TransformationEstimationLM::Functor::Functor
Functor(int m_data_points)
Constructor.
Definition: transformation_estimation_lm.h:237
pcl::PointIndices::ConstPtr
shared_ptr< const ::pcl::PointIndices > ConstPtr
Definition: PointIndices.h:14
pcl::registration::TransformationEstimationLM::Functor::ValuesAtCompileTime
@ ValuesAtCompileTime
Definition: transformation_estimation_lm.h:225
pcl::registration::TransformationEstimationLM::TransformationEstimationLM
TransformationEstimationLM()
Constructor.
Definition: transformation_estimation_lm.hpp:50
pcl::registration::TransformationEstimationLM::computeDistance
virtual MatScalar computeDistance(const PointSource &p_src, const PointTarget &p_tgt) const
Compute the distance between a source point and its corresponding target point.
Definition: transformation_estimation_lm.h:179
pcl::registration::TransformationEstimationLM::OptimizationFunctorWithIndices
Definition: transformation_estimation_lm.h:297
pcl::registration::TransformationEstimationLM::OptimizationFunctorWithIndices::estimator_
const TransformationEstimationLM< PointSource, PointTarget, MatScalar > * estimator_
Definition: transformation_estimation_lm.h:339
pcl::registration::TransformationEstimationLM::TransformationEstimationLM
TransformationEstimationLM(const TransformationEstimationLM &src)
Copy constructor.
Definition: transformation_estimation_lm.h:86
pcl::registration::TransformationEstimationLM::Functor< MatScalar >::InputType
Eigen::Matrix< MatScalar, InputsAtCompileTime, 1 > InputType
Definition: transformation_estimation_lm.h:226
PCL_MAKE_ALIGNED_OPERATOR_NEW
#define PCL_MAKE_ALIGNED_OPERATOR_NEW
Macro to signal a class requires a custom allocator.
Definition: memory.h:63
pcl::registration::TransformationEstimationLM::estimateRigidTransformation
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.
Definition: transformation_estimation_lm.hpp:61
pcl::registration::TransformationEstimationLM::Functor::m_data_points_
int m_data_points_
Definition: transformation_estimation_lm.h:250
pcl::registration::TransformationEstimationLM
TransformationEstimationLM implements Levenberg Marquardt-based estimation of the transformation alig...
Definition: transformation_estimation_lm.h:58
pcl::registration::TransformationEstimationLM::tmp_tgt_
const PointCloudTarget * tmp_tgt_
Temporary pointer to the target dataset.
Definition: transformation_estimation_lm.h:205
pcl::registration::TransformationEstimationLM::warp_point_
pcl::registration::WarpPointRigid< PointSource, PointTarget, MatScalar >::Ptr warp_point_
The parameterized function used to warp the source to the target.
Definition: transformation_estimation_lm.h:215
pcl::PointIndices::Ptr
shared_ptr< ::pcl::PointIndices > Ptr
Definition: PointIndices.h:13
pcl::registration::TransformationEstimationLM::Functor::InputsAtCompileTime
@ InputsAtCompileTime
Definition: transformation_estimation_lm.h:225
pcl::registration::TransformationEstimationLM::OptimizationFunctor::OptimizationFunctor
OptimizationFunctor(const OptimizationFunctor &src)
Copy constructor.
Definition: transformation_estimation_lm.h:267
pcl::PointCloud< PointSource >::Ptr
shared_ptr< PointCloud< PointSource > > Ptr
Definition: point_cloud.h:406
pcl::registration::TransformationEstimationLM::setWarpFunction
void setWarpFunction(const typename WarpPointRigid< PointSource, PointTarget, MatScalar >::Ptr &warp_fcn)
Set the function we use to warp points.
Definition: transformation_estimation_lm.h:162
pcl::registration::TransformationEstimationLM::Ptr
shared_ptr< TransformationEstimationLM< PointSource, PointTarget, MatScalar > > Ptr
Definition: transformation_estimation_lm.h:71
pcl::PointCloud< PointSource >::ConstPtr
shared_ptr< const PointCloud< PointSource > > ConstPtr
Definition: point_cloud.h:407
pcl::registration::TransformationEstimationLM::OptimizationFunctor
Definition: transformation_estimation_lm.h:253
pcl::Correspondences
std::vector< pcl::Correspondence, Eigen::aligned_allocator< pcl::Correspondence > > Correspondences
Definition: correspondence.h:89
pcl::registration::TransformationEstimationLM::Functor
Base functor all the models that need non linear optimization must define their own one and implement...
Definition: transformation_estimation_lm.h:223
pcl::registration::TransformationEstimation< PointSource, PointTarget, float >::ConstPtr
shared_ptr< const TransformationEstimation< PointSource, PointTarget, float > > ConstPtr
Definition: transformation_estimation.h:120
pcl::registration::TransformationEstimationLM::OptimizationFunctor::estimator_
const TransformationEstimationLM< PointSource, PointTarget, MatScalar > * estimator_
Definition: transformation_estimation_lm.h:294
pcl::registration::TransformationEstimationLM::tmp_src_
const PointCloudSource * tmp_src_
Temporary pointer to the source dataset.
Definition: transformation_estimation_lm.h:202
pcl::registration::TransformationEstimationLM::OptimizationFunctorWithIndices::OptimizationFunctorWithIndices
OptimizationFunctorWithIndices(int m_data_points, const TransformationEstimationLM *estimator)
Functor constructor.
Definition: transformation_estimation_lm.h:304
pcl::registration::TransformationEstimationLM::Functor::values
int values() const
Get the number of values.
Definition: transformation_estimation_lm.h:244
memory.h
Defines functions, macros and traits for allocating and using memory.
pcl::registration::TransformationEstimationLM::OptimizationFunctor::~OptimizationFunctor
~OptimizationFunctor()
Destructor.
Definition: transformation_estimation_lm.h:285
pcl::registration::TransformationEstimationLM< PointSource, PointTarget, float >::VectorX
Eigen::Matrix< float, Eigen::Dynamic, 1 > VectorX
Definition: transformation_estimation_lm.h:75
pcl::registration::WarpPointRigid::Ptr
shared_ptr< WarpPointRigid< PointSourceT, PointTargetT, Scalar > > Ptr
Definition: warp_point_rigid.h:62
pcl::registration::TransformationEstimation
TransformationEstimation represents the base class for methods for transformation estimation based on...
Definition: transformation_estimation.h:62