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