Point Cloud Library (PCL)  1.14.1-dev
default_convergence_criteria.h
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Point Cloud Library (PCL) - www.pointclouds.org
5  * Copyright (c) 2012-, Open Perception, Inc.
6  *
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * * Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  * * Redistributions in binary form must reproduce the above
16  * copyright notice, this list of conditions and the following
17  * disclaimer in the documentation and/or other materials provided
18  * with the distribution.
19  * * Neither the name of the copyright holder(s) nor the names of its
20  * contributors may be used to endorse or promote products derived
21  * from this software without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
29  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
31  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
33  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34  * POSSIBILITY OF SUCH DAMAGE.
35  *
36  * $Id$
37  *
38  */
39 
40 #pragma once
41 
42 #include <pcl/registration/convergence_criteria.h>
43 #include <pcl/correspondence.h>
44 #include <pcl/memory.h>
45 #include <pcl/pcl_macros.h>
46 
47 namespace pcl {
48 namespace registration {
49 /** \brief @b DefaultConvergenceCriteria represents an instantiation of
50  * ConvergenceCriteria, and implements the following criteria for registration loop
51  * evaluation:
52  *
53  * * a maximum number of iterations has been reached
54  * * the transformation (R, t) cannot be further updated (the difference between
55  * current and previous is smaller than a threshold)
56  * * the Mean Squared Error (MSE) between the current set of correspondences and the
57  * previous one is smaller than some threshold (both relative and absolute tests)
58  *
59  * \note Convergence is considered reached if ANY of the above criteria are met.
60  *
61  * \author Radu B. Rusu
62  * \ingroup registration
63  */
64 template <typename Scalar = float>
66 public:
67  using Ptr = shared_ptr<DefaultConvergenceCriteria<Scalar>>;
68  using ConstPtr = shared_ptr<const DefaultConvergenceCriteria<Scalar>>;
69 
70  using Matrix4 = Eigen::Matrix<Scalar, 4, 4>;
71 
80  };
81 
82  /** \brief Empty constructor.
83  * Sets:
84  * * the maximum number of iterations to 100
85  * * the rotation threshold to 0.256 degrees (0.99999)
86  * * the translation threshold to 0.0003 meters (3e-4^2)
87  * * the MSE relative / absolute thresholds to 0.001% and 1e-12
88  *
89  * \param[in] iterations a reference to the number of iterations the loop has ran so
90  * far \param[in] transform a reference to the current transformation obtained by the
91  * transformation evaluation \param[in] correspondences a reference to the current set
92  * of point correspondences between source and target
93  */
94  DefaultConvergenceCriteria(const int& iterations,
95  const Matrix4& transform,
96  const pcl::Correspondences& correspondences)
97  : iterations_(iterations)
98  , transformation_(transform)
99  , correspondences_(correspondences)
100  {}
101 
102  /** \brief Empty destructor */
103  ~DefaultConvergenceCriteria() override = default;
104 
105  /** \brief Set the maximum number of consecutive iterations that the internal
106  * rotation, translation, and MSE differences are allowed to be similar. \param[in]
107  * nr_iterations the maximum number of iterations
108  */
109  inline void
110  setMaximumIterationsSimilarTransforms(const int nr_iterations)
111  {
112  max_iterations_similar_transforms_ = nr_iterations;
113  }
114 
115  /** \brief Get the maximum number of consecutive iterations that the internal
116  * rotation, translation, and MSE differences are allowed to be similar, as set by the
117  * user.
118  */
119  inline int
121  {
123  }
124 
125  /** \brief Set the maximum number of iterations the internal optimization should run
126  * for. \param[in] nr_iterations the maximum number of iterations the internal
127  * optimization should run for
128  */
129  inline void
130  setMaximumIterations(const int nr_iterations)
131  {
132  max_iterations_ = nr_iterations;
133  }
134 
135  /** \brief Get the maximum number of iterations the internal optimization should run
136  * for, as set by the user. */
137  inline int
139  {
140  return (max_iterations_);
141  }
142 
143  /** \brief Specifies if the registration fails or converges when the maximum number of
144  * iterations is reached. \param[in] failure_after_max_iter If true, the registration
145  * fails. If false, the registration is assumed to have converged.
146  */
147  inline void
148  setFailureAfterMaximumIterations(const bool failure_after_max_iter)
149  {
150  failure_after_max_iter_ = failure_after_max_iter;
151  }
152 
153  /** \brief Get whether the registration will fail or converge when the maximum number
154  * of iterations is reached. */
155  inline bool
157  {
158  return (failure_after_max_iter_);
159  }
160 
161  /** \brief Set the rotation threshold cosine angle (maximum allowable difference
162  * between two consecutive transformations) in order for an optimization to be
163  * considered as having converged to the final solution. \param[in] threshold the
164  * rotation threshold in order for an optimization to be considered as having
165  * converged to the final solution.
166  */
167  inline void
168  setRotationThreshold(const double threshold)
169  {
170  rotation_threshold_ = threshold;
171  }
172 
173  /** \brief Get the rotation threshold cosine angle (maximum allowable difference
174  * between two consecutive transformations) as set by the user.
175  */
176  inline double
178  {
179  return (rotation_threshold_);
180  }
181 
182  /** \brief Set the translation threshold (maximum allowable difference between two
183  * consecutive transformations) in order for an optimization to be considered as
184  * having converged to the final solution. \param[in] threshold the translation
185  * threshold in order for an optimization to be considered as having converged to the
186  * final solution.
187  */
188  inline void
189  setTranslationThreshold(const double threshold)
190  {
191  translation_threshold_ = threshold;
192  }
193 
194  /** \brief Get the rotation threshold cosine angle (maximum allowable difference
195  * between two consecutive transformations) as set by the user.
196  */
197  inline double
199  {
200  return (translation_threshold_);
201  }
202 
203  /** \brief Set the relative MSE between two consecutive sets of correspondences.
204  * \param[in] mse_relative the relative MSE threshold
205  */
206  inline void
207  setRelativeMSE(const double mse_relative)
208  {
209  mse_threshold_relative_ = mse_relative;
210  }
211 
212  /** \brief Get the relative MSE between two consecutive sets of correspondences. */
213  inline double
215  {
216  return (mse_threshold_relative_);
217  }
218 
219  /** \brief Set the absolute MSE between two consecutive sets of correspondences.
220  * \param[in] mse_absolute the relative MSE threshold
221  */
222  inline void
223  setAbsoluteMSE(const double mse_absolute)
224  {
225  mse_threshold_absolute_ = mse_absolute;
226  }
227 
228  /** \brief Get the absolute MSE between two consecutive sets of correspondences. */
229  inline double
231  {
232  return (mse_threshold_absolute_);
233  }
234 
235  /** \brief Check if convergence has been reached. */
236  bool
237  hasConverged() override;
238 
239  /** \brief Return the convergence state after hasConverged () */
242  {
243  return (convergence_state_);
244  }
245 
246  /** \brief Sets the convergence state externally (for example, when ICP does not find
247  * enough correspondences to estimate a transformation, the function is called setting
248  * the convergence state to ConvergenceState::CONVERGENCE_CRITERIA_NO_CORRESPONDENCES)
249  * \param[in] c the convergence state
250  */
251  inline void
253  {
254  convergence_state_ = c;
255  }
256 
257 protected:
258  /** \brief Calculate the mean squared error (MSE) of the distance for a given set of
259  * correspondences. \param[in] correspondences the given set of correspondences
260  */
261  inline double
262  calculateMSE(const pcl::Correspondences& correspondences) const
263  {
264  double mse = 0;
265  for (const auto& correspondence : correspondences)
266  mse += correspondence.distance;
267  mse /= static_cast<double>(correspondences.size());
268  return (mse);
269  }
270 
271  /** \brief The number of iterations done by the registration loop so far. */
272  const int& iterations_;
273 
274  /** \brief The current transformation obtained by the transformation estimation
275  * method. */
277 
278  /** \brief The current set of point correspondences between the source and the target.
279  */
281 
282  /** \brief The MSE for the previous set of correspondences. */
283  double correspondences_prev_mse_{std::numeric_limits<double>::max()};
284 
285  /** \brief The MSE for the current set of correspondences. */
286  double correspondences_cur_mse_{std::numeric_limits<double>::max()};
287 
288  /** \brief The maximum nuyyGmber of iterations that the registration loop is to be
289  * executed. */
290  int max_iterations_{100};
291 
292  /** \brief Specifys if the registration fails or converges when the maximum number of
293  * iterations is reached. */
295 
296  /** \brief The rotation threshold is the relative rotation between two iterations (as
297  * angle cosine). */
298  double rotation_threshold_{0.99999};
299 
300  /** \brief The translation threshold is the relative translation between two
301  * iterations (0 if no translation). */
302  double translation_threshold_{3e-4 * 3e-4}; // 0.0003 meters
303 
304  /** \brief The relative change from the previous MSE for the current set of
305  * correspondences, e.g. .1 means 10% change. */
306  double mse_threshold_relative_{0.00001};
307 
308  /** \brief The absolute change from the previous MSE for the current set of
309  * correspondences. */
310  double mse_threshold_absolute_{1e-12};
311 
312  /** \brief Internal counter for the number of iterations that the internal
313  * rotation, translation, and MSE differences are allowed to be similar. */
315 
316  /** \brief The maximum number of iterations that the internal rotation,
317  * translation, and MSE differences are allowed to be similar. */
319 
320  /** \brief The state of the convergence (e.g., why did the registration converge). */
322 
323 public:
325 };
326 } // namespace registration
327 } // namespace pcl
328 
329 #include <pcl/registration/impl/default_convergence_criteria.hpp>
ConvergenceCriteria represents an abstract base class for different convergence criteria used in regi...
shared_ptr< ConvergenceCriteria > Ptr
shared_ptr< const ConvergenceCriteria > ConstPtr
DefaultConvergenceCriteria represents an instantiation of ConvergenceCriteria, and implements the fol...
ConvergenceState convergence_state_
The state of the convergence (e.g., why did the registration converge).
int iterations_similar_transforms_
Internal counter for the number of iterations that the internal rotation, translation,...
int getMaximumIterations() const
Get the maximum number of iterations the internal optimization should run for, as set by the user.
void setConvergenceState(ConvergenceState c)
Sets the convergence state externally (for example, when ICP does not find enough correspondences to ...
double correspondences_cur_mse_
The MSE for the current set of correspondences.
DefaultConvergenceCriteria(const int &iterations, const Matrix4 &transform, const pcl::Correspondences &correspondences)
Empty constructor.
const Matrix4 & transformation_
The current transformation obtained by the transformation estimation method.
void setAbsoluteMSE(const double mse_absolute)
Set the absolute MSE between two consecutive sets of correspondences.
void setMaximumIterations(const int nr_iterations)
Set the maximum number of iterations the internal optimization should run for.
const pcl::Correspondences & correspondences_
The current set of point correspondences between the source and the target.
const int & iterations_
The number of iterations done by the registration loop so far.
void setTranslationThreshold(const double threshold)
Set the translation threshold (maximum allowable difference between two consecutive transformations) ...
bool getFailureAfterMaximumIterations() const
Get whether the registration will fail or converge when the maximum number of iterations is reached.
void setRelativeMSE(const double mse_relative)
Set the relative MSE between two consecutive sets of correspondences.
int max_iterations_
The maximum nuyyGmber of iterations that the registration loop is to be executed.
void setMaximumIterationsSimilarTransforms(const int nr_iterations)
Set the maximum number of consecutive iterations that the internal rotation, translation,...
double getAbsoluteMSE() const
Get the absolute MSE between two consecutive sets of correspondences.
double translation_threshold_
The translation threshold is the relative translation between two iterations (0 if no translation).
~DefaultConvergenceCriteria() override=default
Empty destructor.
void setRotationThreshold(const double threshold)
Set the rotation threshold cosine angle (maximum allowable difference between two consecutive transfo...
double mse_threshold_absolute_
The absolute change from the previous MSE for the current set of correspondences.
double getTranslationThreshold() const
Get the rotation threshold cosine angle (maximum allowable difference between two consecutive transfo...
ConvergenceState getConvergenceState()
Return the convergence state after hasConverged ()
bool hasConverged() override
Check if convergence has been reached.
int getMaximumIterationsSimilarTransforms() const
Get the maximum number of consecutive iterations that the internal rotation, translation,...
double calculateMSE(const pcl::Correspondences &correspondences) const
Calculate the mean squared error (MSE) of the distance for a given set of correspondences.
void setFailureAfterMaximumIterations(const bool failure_after_max_iter)
Specifies if the registration fails or converges when the maximum number of iterations is reached.
double getRelativeMSE() const
Get the relative MSE between two consecutive sets of correspondences.
bool failure_after_max_iter_
Specifys if the registration fails or converges when the maximum number of iterations is reached.
double rotation_threshold_
The rotation threshold is the relative rotation between two iterations (as angle cosine).
double mse_threshold_relative_
The relative change from the previous MSE for the current set of correspondences, e....
double correspondences_prev_mse_
The MSE for the previous set of correspondences.
int max_iterations_similar_transforms_
The maximum number of iterations that the internal rotation, translation, and MSE differences are all...
double getRotationThreshold() const
Get the rotation threshold cosine angle (maximum allowable difference between two consecutive transfo...
#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
Defines all the PCL and non-PCL macros used.