Point Cloud Library (PCL)  1.6.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ransac.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: ransac.hpp 6144 2012-07-04 22:06:28Z rusu $
35  *
36  */
37 
38 #ifndef PCL_SAMPLE_CONSENSUS_IMPL_RANSAC_H_
39 #define PCL_SAMPLE_CONSENSUS_IMPL_RANSAC_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::RandomSampleConsensus::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 
61  int n_inliers_count = 0;
62  unsigned skipped_count = 0;
63  // supress infinite loops by just allowing 10 x maximum allowed iterations for invalid model parameters!
64  const unsigned max_skip = max_iterations_ * 10;
65 
66  // Iterate
67  while (iterations_ < k && skipped_count < max_skip)
68  {
69  // Get X samples which satisfy the model criteria
70  sac_model_->getSamples (iterations_, selection);
71 
72  if (selection.empty ())
73  {
74  PCL_ERROR ("[pcl::RandomSampleConsensus::computeModel] No samples could be selected!\n");
75  break;
76  }
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  // Select the inliers that are within threshold_ from the model
87  //sac_model_->selectWithinDistance (model_coefficients, threshold_, inliers);
88  //if (inliers.empty () && k > 1.0)
89  // continue;
90 
91  n_inliers_count = sac_model_->countWithinDistance (model_coefficients, threshold_);
92 
93  // Better match ?
94  if (n_inliers_count > n_best_inliers_count)
95  {
96  n_best_inliers_count = n_inliers_count;
97 
98  // Save the current model/inlier/coefficients selection as being the best so far
99  model_ = selection;
100  model_coefficients_ = model_coefficients;
101 
102  // Compute the k parameter (k=log(z)/log(1-w^n))
103  double w = static_cast<double> (n_best_inliers_count) / static_cast<double> (sac_model_->getIndices ()->size ());
104  double p_no_outliers = 1.0 - pow (w, static_cast<double> (selection.size ()));
105  p_no_outliers = (std::max) (std::numeric_limits<double>::epsilon (), p_no_outliers); // Avoid division by -Inf
106  p_no_outliers = (std::min) (1.0 - std::numeric_limits<double>::epsilon (), p_no_outliers); // Avoid division by 0.
107  k = log (1.0 - probability_) / log (p_no_outliers);
108  }
109 
110  ++iterations_;
111  if (debug_verbosity_level > 1)
112  PCL_DEBUG ("[pcl::RandomSampleConsensus::computeModel] Trial %d out of %f: %d inliers (best is: %d so far).\n", iterations_, k, n_inliers_count, n_best_inliers_count);
113  if (iterations_ > max_iterations_)
114  {
115  if (debug_verbosity_level > 0)
116  PCL_DEBUG ("[pcl::RandomSampleConsensus::computeModel] RANSAC reached the maximum number of trials.\n");
117  break;
118  }
119  }
120 
121  if (debug_verbosity_level > 0)
122  PCL_DEBUG ("[pcl::RandomSampleConsensus::computeModel] Model: %zu size, %d inliers.\n", model_.size (), n_best_inliers_count);
123 
124  if (model_.empty ())
125  {
126  inliers_.clear ();
127  return (false);
128  }
129 
130  // Get the set of inliers that correspond to the best model found so far
131  sac_model_->selectWithinDistance (model_coefficients_, threshold_, inliers_);
132  return (true);
133 }
134 
135 #define PCL_INSTANTIATE_RandomSampleConsensus(T) template class PCL_EXPORTS pcl::RandomSampleConsensus<T>;
136 
137 #endif // PCL_SAMPLE_CONSENSUS_IMPL_RANSAC_H_
138