Point Cloud Library (PCL)  1.15.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  if (this == &src)
100  return *this;
101  tmp_src_ = src.tmp_src_;
102  tmp_tgt_ = src.tmp_tgt_;
105  warp_point_ = src.warp_point_;
106  return *this;
107  }
108 
109  /** \brief Destructor. */
110  ~TransformationEstimationLM() override = default;
111 
112  /** \brief Estimate a rigid rotation transformation between a source and a target
113  * point cloud using LM. \param[in] cloud_src the source point cloud dataset
114  * \param[in] cloud_tgt the target point cloud dataset
115  * \param[out] transformation_matrix the resultant transformation matrix
116  */
117  inline void
119  const pcl::PointCloud<PointTarget>& cloud_tgt,
120  Matrix4& transformation_matrix) const override;
121 
122  /** \brief Estimate a rigid rotation transformation between a source and a target
123  * point cloud using LM. \param[in] cloud_src the source point cloud dataset
124  * \param[in] indices_src the vector of indices describing the points of interest in
125  * \a cloud_src
126  * \param[in] cloud_tgt the target point cloud dataset
127  * \param[out] transformation_matrix the resultant transformation matrix
128  */
129  inline void
131  const pcl::Indices& indices_src,
132  const pcl::PointCloud<PointTarget>& cloud_tgt,
133  Matrix4& transformation_matrix) const override;
134 
135  /** \brief Estimate a rigid rotation transformation between a source and a target
136  * point cloud using LM. \param[in] cloud_src the source point cloud dataset
137  * \param[in] indices_src the vector of indices describing the points of interest in
138  * \a cloud_src
139  * \param[in] cloud_tgt the target point cloud dataset
140  * \param[in] indices_tgt the vector of indices describing the correspondences of the
141  * interest points from \a indices_src
142  * \param[out] transformation_matrix the resultant transformation matrix
143  */
144  inline void
146  const pcl::Indices& indices_src,
147  const pcl::PointCloud<PointTarget>& cloud_tgt,
148  const pcl::Indices& indices_tgt,
149  Matrix4& transformation_matrix) const override;
150 
151  /** \brief Estimate a rigid rotation transformation between a source and a target
152  * point cloud using LM. \param[in] cloud_src the source point cloud dataset
153  * \param[in] cloud_tgt the target point cloud dataset
154  * \param[in] correspondences the vector of correspondences between source and target
155  * point cloud \param[out] transformation_matrix the resultant transformation matrix
156  */
157  inline void
159  const pcl::PointCloud<PointTarget>& cloud_tgt,
160  const pcl::Correspondences& correspondences,
161  Matrix4& transformation_matrix) const override;
162 
163  /** \brief Set the function we use to warp points. Defaults to rigid 6D warp.
164  * \param[in] warp_fcn a shared pointer to an object that warps points
165  */
166  void
169  {
170  warp_point_ = warp_fcn;
171  }
172 
173 protected:
174  /** \brief Compute the distance between a source point and its corresponding target
175  * point \param[in] p_src The source point \param[in] p_tgt The target point \return
176  * The distance between \a p_src and \a p_tgt
177  *
178  * \note Older versions of PCL used this method internally for calculating the
179  * optimization gradient. Since PCL 1.7, a switch has been made to the
180  * computeDistance method using Vector4 types instead. This method is only
181  * kept for API compatibility reasons.
182  */
183  virtual MatScalar
184  computeDistance(const PointSource& p_src, const PointTarget& p_tgt) const
185  {
186  Vector4 s(p_src.x, p_src.y, p_src.z, 0);
187  Vector4 t(p_tgt.x, p_tgt.y, p_tgt.z, 0);
188  return ((s - t).norm());
189  }
190 
191  /** \brief Compute the distance between a source point and its corresponding target
192  * point \param[in] p_src The source point \param[in] p_tgt The target point \return
193  * The distance between \a p_src and \a p_tgt
194  *
195  * \note A different distance function can be defined by creating a subclass of
196  * TransformationEstimationLM and overriding this method.
197  * (See \a TransformationEstimationPointToPlane)
198  */
199  virtual MatScalar
200  computeDistance(const Vector4& p_src, const PointTarget& p_tgt) const
201  {
202  Vector4 t(p_tgt.x, p_tgt.y, p_tgt.z, 0);
203  return ((p_src - t).norm());
204  }
205 
206  /** \brief Temporary pointer to the source dataset. */
207  mutable const PointCloudSource* tmp_src_{nullptr};
208 
209  /** \brief Temporary pointer to the target dataset. */
210  mutable const PointCloudTarget* tmp_tgt_{nullptr};
211 
212  /** \brief Temporary pointer to the source dataset indices. */
213  mutable const pcl::Indices* tmp_idx_src_{nullptr};
214 
215  /** \brief Temporary pointer to the target dataset indices. */
216  mutable const pcl::Indices* tmp_idx_tgt_{nullptr};
217 
218  /** \brief The parameterized function used to warp the source to the target. */
221 
222  /** Base functor all the models that need non linear optimization must
223  * define their own one and implement operator() (const Eigen::VectorXd& x,
224  * Eigen::VectorXd& fvec) or operator() (const Eigen::VectorXf& x, Eigen::VectorXf&
225  * fvec) depending on the chosen _Scalar
226  */
227  template <typename _Scalar, int NX = Eigen::Dynamic, int NY = Eigen::Dynamic>
228  struct Functor {
229  using Scalar = _Scalar;
231  using InputType = Eigen::Matrix<_Scalar, InputsAtCompileTime, 1>;
232  using ValueType = Eigen::Matrix<_Scalar, ValuesAtCompileTime, 1>;
233  using JacobianType =
234  Eigen::Matrix<_Scalar, ValuesAtCompileTime, InputsAtCompileTime>;
235 
236  /** \brief Empty Constructor. */
238 
239  /** \brief Constructor
240  * \param[in] m_data_points number of data points to evaluate.
241  */
242  Functor(int m_data_points) : m_data_points_(m_data_points) {}
243 
244  /** \brief Destructor. */
245  virtual ~Functor() = default;
246 
247  /** \brief Get the number of values. */
248  int
249  values() const
250  {
251  return (m_data_points_);
252  }
253 
254  protected:
256  };
257 
258  struct OptimizationFunctor : public Functor<MatScalar> {
260 
261  /** Functor constructor
262  * \param[in] m_data_points the number of data points to evaluate
263  * \param[in,out] estimator pointer to the estimator object
264  */
265  OptimizationFunctor(int m_data_points, const TransformationEstimationLM* estimator)
266  : Functor<MatScalar>(m_data_points), estimator_(estimator)
267  {}
268 
269  /** Copy constructor
270  * \param[in] src the optimization functor to copy into this
271  */
273  : Functor<MatScalar>(src.m_data_points_), estimator_()
274  {
275  *this = src;
276  }
277 
278  /** Copy operator
279  * \param[in] src the optimization functor to copy into this
280  */
281  inline OptimizationFunctor&
283  {
284  if (this == &src)
285  return *this;
287  estimator_ = src.estimator_;
288  return *this;
289  }
290 
291  /** \brief Destructor. */
292  ~OptimizationFunctor() override = default;
293 
294  /** Fill fvec from x. For the current state vector x fill the f values
295  * \param[in] x state vector
296  * \param[out] fvec f values vector
297  */
298  int
299  operator()(const VectorX& x, VectorX& fvec) const;
300 
302  };
303 
304  struct OptimizationFunctorWithIndices : public Functor<MatScalar> {
306 
307  /** Functor constructor
308  * \param[in] m_data_points the number of data points to evaluate
309  * \param[in,out] estimator pointer to the estimator object
310  */
312  const TransformationEstimationLM* estimator)
313  : Functor<MatScalar>(m_data_points), estimator_(estimator)
314  {}
315 
316  /** Copy constructor
317  * \param[in] src the optimization functor to copy into this
318  */
320  : Functor<MatScalar>(src.m_data_points_), estimator_()
321  {
322  *this = src;
323  }
324 
325  /** Copy operator
326  * \param[in] src the optimization functor to copy into this
327  */
330  {
331  if (this == &src)
332  return *this;
334  estimator_ = src.estimator_;
335  return *this;
336  }
337 
338  /** \brief Destructor. */
339  ~OptimizationFunctorWithIndices() override = default;
340 
341  /** Fill fvec from x. For the current state vector x fill the f values
342  * \param[in] x state vector
343  * \param[out] fvec f values vector
344  */
345  int
346  operator()(const VectorX& x, VectorX& fvec) const;
347 
349  };
350 
351 public:
353 };
354 } // namespace registration
355 } // namespace pcl
356 
357 #include <pcl/registration/impl/transformation_estimation_lm.hpp>
358 
359 #if !defined(PCL_NO_PRECOMPILE) && \
360  !defined(PCL_REGISTRATION_TRANSFORMATION_ESTIMATION_LM_CPP_)
362  pcl::PointXYZ>;
367 #endif // PCL_NO_PRECOMPILE
shared_ptr< PointCloud< PointSource > > Ptr
Definition: point_cloud.h:414
shared_ptr< const PointCloud< PointSource > > ConstPtr
Definition: point_cloud.h:415
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:86
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
A point structure representing Euclidean xyz coordinates.
A point structure representing Euclidean xyz coordinates, and the RGB color.
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_