Point Cloud Library (PCL)  1.14.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  std::size_t n_best_inliers_count = 0;
59  double k = std::numeric_limits<double>::max();
60 
61  Indices selection;
62  Eigen::VectorXf model_coefficients (sac_model_->getModelSize ());
63  std::set<index_t> indices_subset;
64 
65  const double log_probability = std::log (1.0 - probability_);
66  const double one_over_indices = 1.0 / static_cast<double> (sac_model_->getIndices ()->size ());
67 
68  std::size_t n_inliers_count;
69  unsigned skipped_count = 0;
70  // suppress infinite loops by just allowing 10 x maximum allowed iterations for invalid model parameters!
71  const unsigned max_skip = max_iterations_ * 10;
72 
73  // Number of samples to try randomly
74  const std::size_t fraction_nr_points = pcl_lrint (static_cast<double>(sac_model_->getIndices ()->size ()) * fraction_nr_pretest_ / 100.0);
75 
76  // Iterate
77  while (iterations_ < k)
78  {
79  // Get X samples which satisfy the model criteria
80  sac_model_->getSamples (iterations_, selection);
81 
82  if (selection.empty ())
83  {
84  PCL_ERROR ("[pcl::RandomizedRandomSampleConsensus::computeModel] No samples could be selected!\n");
85  break;
86  }
87 
88  // Search for inliers in the point cloud for the current plane model M
89  if (!sac_model_->computeModelCoefficients (selection, model_coefficients))
90  {
91  //iterations_++;
92  ++skipped_count;
93  if (skipped_count < max_skip)
94  {
95  PCL_DEBUG ("[pcl::RandomizedRandomSampleConsensus::computeModel] The function computeModelCoefficients failed, so continue with next iteration.\n");
96  continue;
97  }
98  else
99  {
100  PCL_DEBUG ("[pcl::RandomizedRandomSampleConsensus::computeModel] The function computeModelCoefficients failed, and RRANSAC reached the maximum number of trials.\n");
101  break;
102  }
103  }
104 
105  // RRANSAC addon: verify a random fraction of the data
106  // Get X random samples which satisfy the model criterion
107  this->getRandomSamples (sac_model_->getIndices (), fraction_nr_points, indices_subset);
108  if (!sac_model_->doSamplesVerifyModel (indices_subset, model_coefficients, threshold_))
109  {
110  ++iterations_;
111  PCL_DEBUG ("[pcl::RandomizedRandomSampleConsensus::computeModel] The function doSamplesVerifyModel failed, so continue with next iteration.\n");
112  continue;
113  }
114 
115  // Select the inliers that are within threshold_ from the model
116  n_inliers_count = sac_model_->countWithinDistance (model_coefficients, threshold_);
117 
118  // Better match ?
119  if (n_inliers_count > n_best_inliers_count)
120  {
121  n_best_inliers_count = n_inliers_count;
122 
123  // Save the current model/inlier/coefficients selection as being the best so far
124  model_ = selection;
125  model_coefficients_ = model_coefficients;
126 
127  // Compute the k parameter (k=std::log(z)/std::log(1-w^n))
128  const double w = static_cast<double> (n_best_inliers_count) * one_over_indices;
129  double p_outliers = 1.0 - std::pow (w, static_cast<double> (selection.size ())); // Probability that selection is contaminated by at least one outlier
130  p_outliers = (std::max) (std::numeric_limits<double>::epsilon (), p_outliers); // Avoid division by -Inf
131  p_outliers = (std::min) (1.0 - std::numeric_limits<double>::epsilon (), p_outliers); // Avoid division by 0.
132  k = log_probability / std::log (p_outliers);
133  }
134 
135  ++iterations_;
136 
137  if (debug_verbosity_level > 1)
138  PCL_DEBUG ("[pcl::RandomizedRandomSampleConsensus::computeModel] Trial %d out of %d: %u inliers (best is: %u so far).\n", iterations_, static_cast<int> (std::ceil (k)), n_inliers_count, n_best_inliers_count);
139  if (iterations_ > max_iterations_)
140  {
141  if (debug_verbosity_level > 0)
142  PCL_DEBUG ("[pcl::RandomizedRandomSampleConsensus::computeModel] RRANSAC reached the maximum number of trials.\n");
143  break;
144  }
145  }
146 
147  if (debug_verbosity_level > 0)
148  PCL_DEBUG ("[pcl::RandomizedRandomSampleConsensus::computeModel] Model: %lu size, %u inliers.\n", model_.size (), n_best_inliers_count);
149 
150  if (model_.empty ())
151  {
152  PCL_ERROR ("[pcl::RandomizedRandomSampleConsensus::computeModel] RRANSAC found no model.\n");
153  inliers_.clear ();
154  return (false);
155  }
156 
157  // Get the set of inliers that correspond to the best model found so far
158  sac_model_->selectWithinDistance (model_coefficients_, threshold_, inliers_);
159  return (true);
160 }
161 
162 #define PCL_INSTANTIATE_RandomizedRandomSampleConsensus(T) template class PCL_EXPORTS pcl::RandomizedRandomSampleConsensus<T>;
163 
164 #endif // PCL_SAMPLE_CONSENSUS_IMPL_RRANSAC_H_
165 
bool computeModel(int debug_verbosity_level=0) override
Compute the actual model and find the inliers.
Definition: rransac.hpp:48
IndicesAllocator<> Indices
Type used for indices in PCL.
Definition: types.h:133
#define pcl_lrint(x)
Definition: pcl_macros.h:253