Point Cloud Library (PCL)  1.9.1-dev
rransac.hpp
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Point Cloud Library (PCL) - www.pointclouds.org
5  * Copyright (c) 2009, Willow Garage, Inc.
6  * Copyright (c) 2012-, Open Perception, Inc.
7  *
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  *
14  * * Redistributions of source code must retain the above copyright
15  * notice, this list of conditions and the following disclaimer.
16  * * Redistributions in binary form must reproduce the above
17  * copyright notice, this list of conditions and the following
18  * disclaimer in the documentation and/or other materials provided
19  * with the distribution.
20  * * Neither the name of the copyright holder(s) nor the names of its
21  * contributors may be used to endorse or promote products derived
22  * from this software without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
28  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
29  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
30  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
34  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35  * POSSIBILITY OF SUCH DAMAGE.
36  *
37  * $Id$
38  *
39  */
40 
41 #ifndef PCL_SAMPLE_CONSENSUS_IMPL_RRANSAC_H_
42 #define PCL_SAMPLE_CONSENSUS_IMPL_RRANSAC_H_
43 
44 #include <pcl/sample_consensus/rransac.h>
45 
46 //////////////////////////////////////////////////////////////////////////
47 template <typename PointT> bool
49 {
50  // Warn and exit if no threshold was set
51  if (threshold_ == std::numeric_limits<double>::max())
52  {
53  PCL_ERROR ("[pcl::RandomizedRandomSampleConsensus::computeModel] No threshold set!\n");
54  return (false);
55  }
56 
57  iterations_ = 0;
58  int n_best_inliers_count = -INT_MAX;
59  double k = 1.0;
60 
61  std::vector<int> selection;
62  Eigen::VectorXf model_coefficients;
63  std::set<int> indices_subset;
64 
65  int n_inliers_count = 0;
66  unsigned skipped_count = 0;
67  // suppress infinite loops by just allowing 10 x maximum allowed iterations for invalid model parameters!
68  const unsigned max_skip = max_iterations_ * 10;
69 
70  // Number of samples to try randomly
71  std::size_t fraction_nr_points = pcl_lrint (static_cast<double>(sac_model_->getIndices ()->size ()) * fraction_nr_pretest_ / 100.0);
72 
73  // Iterate
74  while (iterations_ < k && skipped_count < max_skip)
75  {
76  // Get X samples which satisfy the model criteria
77  sac_model_->getSamples (iterations_, selection);
78 
79  if (selection.empty ()) break;
80 
81  // Search for inliers in the point cloud for the current plane model M
82  if (!sac_model_->computeModelCoefficients (selection, model_coefficients))
83  {
84  //iterations_++;
85  ++ skipped_count;
86  continue;
87  }
88 
89  // RRANSAC addon: verify a random fraction of the data
90  // Get X random samples which satisfy the model criterion
91  this->getRandomSamples (sac_model_->getIndices (), fraction_nr_points, indices_subset);
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  // Select the inliers that are within threshold_ from the model
103  n_inliers_count = sac_model_->countWithinDistance (model_coefficients, threshold_);
104 
105  // Better match ?
106  if (n_inliers_count > n_best_inliers_count)
107  {
108  n_best_inliers_count = n_inliers_count;
109 
110  // Save the current model/inlier/coefficients selection as being the best so far
111  model_ = selection;
112  model_coefficients_ = model_coefficients;
113 
114  // Compute the k parameter (k=std::log(z)/std::log(1-w^n))
115  double w = static_cast<double> (n_inliers_count) / static_cast<double> (sac_model_->getIndices ()->size ());
116  double p_no_outliers = 1 - pow (w, static_cast<double> (selection.size ()));
117  p_no_outliers = (std::max) (std::numeric_limits<double>::epsilon (), p_no_outliers); // Avoid division by -Inf
118  p_no_outliers = (std::min) (1 - std::numeric_limits<double>::epsilon (), p_no_outliers); // Avoid division by 0.
119  k = std::log (1 - probability_) / std::log (p_no_outliers);
120  }
121 
122  ++iterations_;
123 
124  if (debug_verbosity_level > 1)
125  PCL_DEBUG ("[pcl::RandomizedRandomSampleConsensus::computeModel] Trial %d out of %d: %d inliers (best is: %d so far).\n", iterations_, static_cast<int> (std::ceil (k)), n_inliers_count, n_best_inliers_count);
126  if (iterations_ > max_iterations_)
127  {
128  if (debug_verbosity_level > 0)
129  PCL_DEBUG ("[pcl::RandomizedRandomSampleConsensus::computeModel] RRANSAC reached the maximum number of trials.\n");
130  break;
131  }
132  }
133 
134  if (debug_verbosity_level > 0)
135  PCL_DEBUG ("[pcl::RandomizedRandomSampleConsensus::computeModel] Model: %lu size, %d inliers.\n", model_.size (), n_best_inliers_count);
136 
137  if (model_.empty ())
138  {
139  inliers_.clear ();
140  return (false);
141  }
142 
143  // Get the set of inliers that correspond to the best model found so far
144  sac_model_->selectWithinDistance (model_coefficients_, threshold_, inliers_);
145  return (true);
146 }
147 
148 #define PCL_INSTANTIATE_RandomizedRandomSampleConsensus(T) template class PCL_EXPORTS pcl::RandomizedRandomSampleConsensus<T>;
149 
150 #endif // PCL_SAMPLE_CONSENSUS_IMPL_RRANSAC_H_
151 
#define pcl_lrint(x)
Definition: pcl_macros.h:168
bool computeModel(int debug_verbosity_level=0) override
Compute the actual model and find the inliers.
Definition: rransac.hpp:48