Point Cloud Library (PCL)  1.6.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
lmeds.hpp
Go to the documentation of this file.
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2009, Willow Garage, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * * Redistributions in binary form must reproduce the above
14  * copyright notice, this list of conditions and the following
15  * disclaimer in the documentation and/or other materials provided
16  * with the distribution.
17  * * Neither the name of Willow Garage, Inc. nor the names of its
18  * contributors may be used to endorse or promote products derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  *
34  * $Id: lmeds.hpp 6144 2012-07-04 22:06:28Z rusu $
35  *
36  */
37 
38 #ifndef PCL_SAMPLE_CONSENSUS_IMPL_LMEDS_H_
39 #define PCL_SAMPLE_CONSENSUS_IMPL_LMEDS_H_
40 
42 
44 template <typename PointT> bool
46 {
47  // Warn and exit if no threshold was set
48  if (threshold_ == std::numeric_limits<double>::max())
49  {
50  PCL_ERROR ("[pcl::LeastMedianSquares::computeModel] No threshold set!\n");
51  return (false);
52  }
53 
54  iterations_ = 0;
55  double d_best_penalty = std::numeric_limits<double>::max();
56 
57  std::vector<int> best_model;
58  std::vector<int> selection;
59  Eigen::VectorXf model_coefficients;
60  std::vector<double> distances;
61 
62  int n_inliers_count = 0;
63 
64  unsigned skipped_count = 0;
65  // supress infinite loops by just allowing 10 x maximum allowed iterations for invalid model parameters!
66  const unsigned max_skip = max_iterations_ * 10;
67 
68  // Iterate
69  while (iterations_ < max_iterations_ && skipped_count < max_skip)
70  {
71  // Get X samples which satisfy the model criteria
72  sac_model_->getSamples (iterations_, selection);
73 
74  if (selection.empty ()) break;
75 
76  // Search for inliers in the point cloud for the current plane model M
77  if (!sac_model_->computeModelCoefficients (selection, model_coefficients))
78  {
79  //iterations_++;
80  ++skipped_count;
81  continue;
82  }
83 
84  double d_cur_penalty = 0;
85  // d_cur_penalty = sum (min (dist, threshold))
86 
87  // Iterate through the 3d points and calculate the distances from them to the model
88  sac_model_->getDistancesToModel (model_coefficients, distances);
89 
90  // No distances? The model must not respect the user given constraints
91  if (distances.empty ())
92  {
93  //iterations_++;
94  ++skipped_count;
95  continue;
96  }
97 
98  std::sort (distances.begin (), distances.end ());
99  // d_cur_penalty = median (distances)
100  size_t mid = sac_model_->getIndices ()->size () / 2;
101  if (mid >= distances.size ())
102  {
103  //iterations_++;
104  ++skipped_count;
105  continue;
106  }
107 
108  // Do we have a "middle" point or should we "estimate" one ?
109  if (sac_model_->getIndices ()->size () % 2 == 0)
110  d_cur_penalty = (sqrt (distances[mid-1]) + sqrt (distances[mid])) / 2;
111  else
112  d_cur_penalty = sqrt (distances[mid]);
113 
114  // Better match ?
115  if (d_cur_penalty < d_best_penalty)
116  {
117  d_best_penalty = d_cur_penalty;
118 
119  // Save the current model/coefficients selection as being the best so far
120  model_ = selection;
121  model_coefficients_ = model_coefficients;
122  }
123 
124  ++iterations_;
125  if (debug_verbosity_level > 1)
126  PCL_DEBUG ("[pcl::LeastMedianSquares::computeModel] Trial %d out of %d. Best penalty is %f.\n", iterations_, max_iterations_, d_best_penalty);
127  }
128 
129  if (model_.empty ())
130  {
131  if (debug_verbosity_level > 0)
132  PCL_DEBUG ("[pcl::LeastMedianSquares::computeModel] Unable to find a solution!\n");
133  return (false);
134  }
135 
136  // Classify the data points into inliers and outliers
137  // Sigma = 1.4826 * (1 + 5 / (n-d)) * sqrt (M)
138  // @note: See "Robust Regression Methods for Computer Vision: A Review"
139  //double sigma = 1.4826 * (1 + 5 / (sac_model_->getIndices ()->size () - best_model.size ())) * sqrt (d_best_penalty);
140  //double threshold = 2.5 * sigma;
141 
142  // Iterate through the 3d points and calculate the distances from them to the model again
143  sac_model_->getDistancesToModel (model_coefficients_, distances);
144  // No distances? The model must not respect the user given constraints
145  if (distances.empty ())
146  {
147  PCL_ERROR ("[pcl::LeastMedianSquares::computeModel] The model found failed to verify against the given constraints!\n");
148  return (false);
149  }
150 
151  std::vector<int> &indices = *sac_model_->getIndices ();
152 
153  if (distances.size () != indices.size ())
154  {
155  PCL_ERROR ("[pcl::LeastMedianSquares::computeModel] Estimated distances (%zu) differs than the normal of indices (%zu).\n", distances.size (), indices.size ());
156  return (false);
157  }
158 
159  inliers_.resize (distances.size ());
160  // Get the inliers for the best model found
161  n_inliers_count = 0;
162  for (size_t i = 0; i < distances.size (); ++i)
163  if (distances[i] <= threshold_)
164  inliers_[n_inliers_count++] = indices[i];
165 
166  // Resize the inliers vector
167  inliers_.resize (n_inliers_count);
168 
169  if (debug_verbosity_level > 0)
170  PCL_DEBUG ("[pcl::LeastMedianSquares::computeModel] Model: %zu size, %d inliers.\n", model_.size (), n_inliers_count);
171 
172  return (true);
173 }
174 
175 #define PCL_INSTANTIATE_LeastMedianSquares(T) template class PCL_EXPORTS pcl::LeastMedianSquares<T>;
176 
177 #endif // PCL_SAMPLE_CONSENSUS_IMPL_LMEDS_H_
178