Point Cloud Library (PCL)  1.10.1
default_convergence_criteria.hpp
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 #ifndef PCL_REGISTRATION_DEFAULT_CONVERGENCE_CRITERIA_HPP_
41 #define PCL_REGISTRATION_DEFAULT_CONVERGENCE_CRITERIA_HPP_
42 
43 #include <pcl/console/print.h>
44 
45 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
46 template <typename Scalar> bool
48 {
49  if (convergence_state_ != CONVERGENCE_CRITERIA_NOT_CONVERGED)
50  {
51  //If it already converged or failed before, reset.
52  iterations_similar_transforms_ = 0;
53  convergence_state_ = CONVERGENCE_CRITERIA_NOT_CONVERGED;
54  }
55 
56  bool is_similar = false;
57 
58  PCL_DEBUG ("[pcl::DefaultConvergenceCriteria::hasConverged] Iteration %d out of %d.\n", iterations_, max_iterations_);
59  // 1. Number of iterations has reached the maximum user imposed number of iterations
60  if (iterations_ >= max_iterations_)
61  {
62  if (!failure_after_max_iter_)
63  {
64  convergence_state_ = CONVERGENCE_CRITERIA_ITERATIONS;
65  return (true);
66  }
67  convergence_state_ = CONVERGENCE_CRITERIA_FAILURE_AFTER_MAX_ITERATIONS;
68  }
69 
70  // 2. The epsilon (difference) between the previous transformation and the current estimated transformation
71  double cos_angle = 0.5 * (transformation_.coeff (0, 0) + transformation_.coeff (1, 1) + transformation_.coeff (2, 2) - 1);
72  double translation_sqr = transformation_.coeff (0, 3) * transformation_.coeff (0, 3) +
73  transformation_.coeff (1, 3) * transformation_.coeff (1, 3) +
74  transformation_.coeff (2, 3) * transformation_.coeff (2, 3);
75  PCL_DEBUG ("[pcl::DefaultConvergenceCriteria::hasConverged] Current transformation gave %f rotation (cosine) and %f translation.\n", cos_angle, translation_sqr);
76 
77  if (cos_angle >= rotation_threshold_ && translation_sqr <= translation_threshold_)
78  {
79  if (iterations_similar_transforms_ >= max_iterations_similar_transforms_)
80  {
81  convergence_state_ = CONVERGENCE_CRITERIA_TRANSFORM;
82  return (true);
83  }
84  is_similar = true;
85  }
86 
87  correspondences_cur_mse_ = calculateMSE (correspondences_);
88  PCL_DEBUG ("[pcl::DefaultConvergenceCriteria::hasConverged] Previous / Current MSE for correspondences distances is: %f / %f.\n", correspondences_prev_mse_, correspondences_cur_mse_);
89 
90  // 3. The relative sum of Euclidean squared errors is smaller than a user defined threshold
91  // Absolute
92  if (std::abs (correspondences_cur_mse_ - correspondences_prev_mse_) < mse_threshold_absolute_)
93  {
94  if (iterations_similar_transforms_ >= max_iterations_similar_transforms_)
95  {
96  convergence_state_ = CONVERGENCE_CRITERIA_ABS_MSE;
97  return (true);
98  }
99  is_similar = true;
100  }
101 
102  // Relative
103  if (std::abs (correspondences_cur_mse_ - correspondences_prev_mse_) / correspondences_prev_mse_ < mse_threshold_relative_)
104  {
105  if (iterations_similar_transforms_ >= max_iterations_similar_transforms_)
106  {
107  convergence_state_ = CONVERGENCE_CRITERIA_REL_MSE;
108  return (true);
109  }
110  is_similar = true;
111  }
112 
113  if (is_similar)
114  {
115  // Increment the number of transforms that the thresholds are allowed to be similar
116  ++iterations_similar_transforms_;
117  }
118  else
119  {
120  // When the transform becomes large, reset.
121  iterations_similar_transforms_ = 0;
122  }
123 
124  correspondences_prev_mse_ = correspondences_cur_mse_;
125 
126  return (false);
127 }
128 
129 #endif // PCL_REGISTRATION_DEFAULT_CONVERGENCE_CRITERIA_HPP_
pcl::registration::DefaultConvergenceCriteria::hasConverged
bool hasConverged() override
Check if convergence has been reached.
Definition: default_convergence_criteria.hpp:47