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