Point Cloud Library (PCL)  1.7.1
ransac.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_RANSAC_H_
42 #define PCL_SAMPLE_CONSENSUS_IMPL_RANSAC_H_
43 
44 #include <pcl/sample_consensus/ransac.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::RandomSampleConsensus::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 
64  double log_probability = log (1.0 - probability_);
65  double one_over_indices = 1.0 / static_cast<double> (sac_model_->getIndices ()->size ());
66 
67  int n_inliers_count = 0;
68  unsigned skipped_count = 0;
69  // supress infinite loops by just allowing 10 x maximum allowed iterations for invalid model parameters!
70  const unsigned max_skip = max_iterations_ * 10;
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 ())
79  {
80  PCL_ERROR ("[pcl::RandomSampleConsensus::computeModel] No samples could be selected!\n");
81  break;
82  }
83 
84  // Search for inliers in the point cloud for the current plane model M
85  if (!sac_model_->computeModelCoefficients (selection, model_coefficients))
86  {
87  //++iterations_;
88  ++skipped_count;
89  continue;
90  }
91 
92  // Select the inliers that are within threshold_ from the model
93  //sac_model_->selectWithinDistance (model_coefficients, threshold_, inliers);
94  //if (inliers.empty () && k > 1.0)
95  // continue;
96 
97  n_inliers_count = sac_model_->countWithinDistance (model_coefficients, threshold_);
98 
99  // Better match ?
100  if (n_inliers_count > n_best_inliers_count)
101  {
102  n_best_inliers_count = n_inliers_count;
103 
104  // Save the current model/inlier/coefficients selection as being the best so far
105  model_ = selection;
106  model_coefficients_ = model_coefficients;
107 
108  // Compute the k parameter (k=log(z)/log(1-w^n))
109  double w = static_cast<double> (n_best_inliers_count) * one_over_indices;
110  double p_no_outliers = 1.0 - pow (w, static_cast<double> (selection.size ()));
111  p_no_outliers = (std::max) (std::numeric_limits<double>::epsilon (), p_no_outliers); // Avoid division by -Inf
112  p_no_outliers = (std::min) (1.0 - std::numeric_limits<double>::epsilon (), p_no_outliers); // Avoid division by 0.
113  k = log_probability / log (p_no_outliers);
114  }
115 
116  ++iterations_;
117  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);
118  if (iterations_ > max_iterations_)
119  {
120  PCL_DEBUG ("[pcl::RandomSampleConsensus::computeModel] RANSAC reached the maximum number of trials.\n");
121  break;
122  }
123  }
124 
125  PCL_DEBUG ("[pcl::RandomSampleConsensus::computeModel] Model: %zu size, %d inliers.\n", model_.size (), n_best_inliers_count);
126 
127  if (model_.empty ())
128  {
129  inliers_.clear ();
130  return (false);
131  }
132 
133  // Get the set of inliers that correspond to the best model found so far
134  sac_model_->selectWithinDistance (model_coefficients_, threshold_, inliers_);
135  return (true);
136 }
137 
138 #define PCL_INSTANTIATE_RandomSampleConsensus(T) template class PCL_EXPORTS pcl::RandomSampleConsensus<T>;
139 
140 #endif // PCL_SAMPLE_CONSENSUS_IMPL_RANSAC_H_
141