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