Point Cloud Library (PCL)  1.6.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
rmsac.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: rmsac.hpp 6144 2012-07-04 22:06:28Z rusu $
35  *
36  */
37 
38 #ifndef PCL_SAMPLE_CONSENSUS_IMPL_RMSAC_H_
39 #define PCL_SAMPLE_CONSENSUS_IMPL_RMSAC_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::RandomizedMEstimatorSampleConsensus::computeModel] No threshold set!\n");
51  return (false);
52  }
53 
54  iterations_ = 0;
55  double d_best_penalty = std::numeric_limits<double>::max();
56  double k = 1.0;
57 
58  std::vector<int> best_model;
59  std::vector<int> selection;
60  Eigen::VectorXf model_coefficients;
61  std::vector<double> distances;
62  std::set<int> indices_subset;
63 
64  int n_inliers_count = 0;
65  unsigned skipped_count = 0;
66  // supress infinite loops by just allowing 10 x maximum allowed iterations for invalid model parameters!
67  const unsigned max_skip = max_iterations_ * 10;
68 
69  // Number of samples to try randomly
70  size_t fraction_nr_points = pcl_lrint (static_cast<double>(sac_model_->getIndices ()->size ()) * fraction_nr_pretest_ / 100.0);
71 
72  // Iterate
73  while (iterations_ < k && skipped_count < max_skip)
74  {
75  // Get X samples which satisfy the model criteria
76  sac_model_->getSamples (iterations_, selection);
77 
78  if (selection.empty ()) break;
79 
80  // Search for inliers in the point cloud for the current plane model M
81  if (!sac_model_->computeModelCoefficients (selection, model_coefficients))
82  {
83  //iterations_++;
84  ++ skipped_count;
85  continue;
86  }
87 
88  // RMSAC addon: verify a random fraction of the data
89  // Get X random samples which satisfy the model criterion
90  this->getRandomSamples (sac_model_->getIndices (), fraction_nr_points, indices_subset);
91 
92  if (!sac_model_->doSamplesVerifyModel (indices_subset, model_coefficients, threshold_))
93  {
94  // Unfortunately we cannot "continue" after the first iteration, because k might not be set, while iterations gets incremented
95  if (k != 1.0)
96  {
97  ++iterations_;
98  continue;
99  }
100  }
101 
102  double d_cur_penalty = 0;
103  // Iterate through the 3d points and calculate the distances from them to the model
104  sac_model_->getDistancesToModel (model_coefficients, distances);
105 
106  if (distances.empty () && k > 1.0)
107  continue;
108 
109  for (size_t i = 0; i < distances.size (); ++i)
110  d_cur_penalty += (std::min) (distances[i], threshold_);
111 
112  // Better match ?
113  if (d_cur_penalty < d_best_penalty)
114  {
115  d_best_penalty = d_cur_penalty;
116 
117  // Save the current model/coefficients selection as being the best so far
118  model_ = selection;
119  model_coefficients_ = model_coefficients;
120 
121  n_inliers_count = 0;
122  // Need to compute the number of inliers for this model to adapt k
123  for (size_t i = 0; i < distances.size (); ++i)
124  if (distances[i] <= threshold_)
125  n_inliers_count++;
126 
127  // Compute the k parameter (k=log(z)/log(1-w^n))
128  double w = static_cast<double> (n_inliers_count) / static_cast<double>(sac_model_->getIndices ()->size ());
129  double p_no_outliers = 1 - pow (w, static_cast<double> (selection.size ()));
130  p_no_outliers = (std::max) (std::numeric_limits<double>::epsilon (), p_no_outliers); // Avoid division by -Inf
131  p_no_outliers = (std::min) (1 - std::numeric_limits<double>::epsilon (), p_no_outliers); // Avoid division by 0.
132  k = log (1 - probability_) / log (p_no_outliers);
133  }
134 
135  ++iterations_;
136  if (debug_verbosity_level > 1)
137  PCL_DEBUG ("[pcl::RandomizedMEstimatorSampleConsensus::computeModel] Trial %d out of %d. Best penalty is %f.\n", iterations_, static_cast<int> (ceil (k)), d_best_penalty);
138  if (iterations_ > max_iterations_)
139  {
140  if (debug_verbosity_level > 0)
141  PCL_DEBUG ("[pcl::RandomizedMEstimatorSampleConsensus::computeModel] MSAC reached the maximum number of trials.\n");
142  break;
143  }
144  }
145 
146  if (model_.empty ())
147  {
148  if (debug_verbosity_level > 0)
149  PCL_DEBUG ("[pcl::RandomizedMEstimatorSampleConsensus::computeModel] Unable to find a solution!\n");
150  return (false);
151  }
152 
153  // Iterate through the 3d points and calculate the distances from them to the model again
154  sac_model_->getDistancesToModel (model_coefficients_, distances);
155  std::vector<int> &indices = *sac_model_->getIndices ();
156  if (distances.size () != indices.size ())
157  {
158  PCL_ERROR ("[pcl::RandomizedMEstimatorSampleConsensus::computeModel] Estimated distances (%zu) differs than the normal of indices (%zu).\n", distances.size (), indices.size ());
159  return (false);
160  }
161 
162  inliers_.resize (distances.size ());
163  // Get the inliers for the best model found
164  n_inliers_count = 0;
165  for (size_t i = 0; i < distances.size (); ++i)
166  if (distances[i] <= threshold_)
167  inliers_[n_inliers_count++] = indices[i];
168 
169  // Resize the inliers vector
170  inliers_.resize (n_inliers_count);
171 
172  if (debug_verbosity_level > 0)
173  PCL_DEBUG ("[pcl::RandomizedMEstimatorSampleConsensus::computeModel] Model: %zu size, %d inliers.\n", model_.size (), n_inliers_count);
174 
175  return (true);
176 }
177 
178 #define PCL_INSTANTIATE_RandomizedMEstimatorSampleConsensus(T) template class PCL_EXPORTS pcl::RandomizedMEstimatorSampleConsensus<T>;
179 
180 #endif // PCL_SAMPLE_CONSENSUS_IMPL_RMSAC_H_
181