Point Cloud Library (PCL)  1.6.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
rransac.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: rransac.hpp 6144 2012-07-04 22:06:28Z rusu $
35  *
36  */
37 
38 #ifndef PCL_SAMPLE_CONSENSUS_IMPL_RRANSAC_H_
39 #define PCL_SAMPLE_CONSENSUS_IMPL_RRANSAC_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::RandomizedRandomSampleConsensus::computeModel] No threshold set!\n");
51  return (false);
52  }
53 
54  iterations_ = 0;
55  int n_best_inliers_count = -INT_MAX;
56  double k = 1.0;
57 
58  std::vector<int> selection;
59  Eigen::VectorXf model_coefficients;
60  std::set<int> indices_subset;
61 
62  int n_inliers_count = 0;
63  unsigned skipped_count = 0;
64  // supress infinite loops by just allowing 10 x maximum allowed iterations for invalid model parameters!
65  const unsigned max_skip = max_iterations_ * 10;
66 
67  // Number of samples to try randomly
68  size_t fraction_nr_points = pcl_lrint (static_cast<double>(sac_model_->getIndices ()->size ()) * fraction_nr_pretest_ / 100.0);
69 
70  // Iterate
71  while (iterations_ < k && skipped_count < max_skip)
72  {
73  // Get X samples which satisfy the model criteria
74  sac_model_->getSamples (iterations_, selection);
75 
76  if (selection.empty ()) break;
77 
78  // Search for inliers in the point cloud for the current plane model M
79  if (!sac_model_->computeModelCoefficients (selection, model_coefficients))
80  {
81  //iterations_++;
82  ++ skipped_count;
83  continue;
84  }
85 
86  // RRANSAC addon: verify a random fraction of the data
87  // Get X random samples which satisfy the model criterion
88  this->getRandomSamples (sac_model_->getIndices (), fraction_nr_points, indices_subset);
89  if (!sac_model_->doSamplesVerifyModel (indices_subset, model_coefficients, threshold_))
90  {
91  // Unfortunately we cannot "continue" after the first iteration, because k might not be set, while iterations gets incremented
92  if (k > 1.0)
93  {
94  ++iterations_;
95  continue;
96  }
97  }
98 
99  // Select the inliers that are within threshold_ from the model
100  n_inliers_count = sac_model_->countWithinDistance (model_coefficients, threshold_);
101 
102  // Better match ?
103  if (n_inliers_count > n_best_inliers_count)
104  {
105  n_best_inliers_count = n_inliers_count;
106 
107  // Save the current model/inlier/coefficients selection as being the best so far
108  model_ = selection;
109  model_coefficients_ = model_coefficients;
110 
111  // Compute the k parameter (k=log(z)/log(1-w^n))
112  double w = static_cast<double> (n_inliers_count) / static_cast<double> (sac_model_->getIndices ()->size ());
113  double p_no_outliers = 1 - pow (w, static_cast<double> (selection.size ()));
114  p_no_outliers = (std::max) (std::numeric_limits<double>::epsilon (), p_no_outliers); // Avoid division by -Inf
115  p_no_outliers = (std::min) (1 - std::numeric_limits<double>::epsilon (), p_no_outliers); // Avoid division by 0.
116  k = log (1 - probability_) / log (p_no_outliers);
117  }
118 
119  ++iterations_;
120 
121  if (debug_verbosity_level > 1)
122  PCL_DEBUG ("[pcl::RandomizedRandomSampleConsensus::computeModel] Trial %d out of %d: %d inliers (best is: %d so far).\n", iterations_, static_cast<int> (ceil (k)), n_inliers_count, n_best_inliers_count);
123  if (iterations_ > max_iterations_)
124  {
125  if (debug_verbosity_level > 0)
126  PCL_DEBUG ("[pcl::RandomizedRandomSampleConsensus::computeModel] RRANSAC reached the maximum number of trials.\n");
127  break;
128  }
129  }
130 
131  if (debug_verbosity_level > 0)
132  PCL_DEBUG ("[pcl::RandomizedRandomSampleConsensus::computeModel] Model: %zu size, %d inliers.\n", model_.size (), n_best_inliers_count);
133 
134  if (model_.empty ())
135  {
136  inliers_.clear ();
137  return (false);
138  }
139 
140  // Get the set of inliers that correspond to the best model found so far
141  sac_model_->selectWithinDistance (model_coefficients_, threshold_, inliers_);
142  return (true);
143 }
144 
145 #define PCL_INSTANTIATE_RandomizedRandomSampleConsensus(T) template class PCL_EXPORTS pcl::RandomizedRandomSampleConsensus<T>;
146 
147 #endif // PCL_SAMPLE_CONSENSUS_IMPL_RRANSAC_H_
148