Point Cloud Library (PCL)  1.10.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 #ifdef _OPENMP
46 #include <omp.h>
47 #endif
48 
49 #if defined _OPENMP && _OPENMP >= 201107 // We need OpenMP 3.1 for the atomic constructs
50 #define OPENMP_AVAILABLE_RANSAC true
51 #else
52 #define OPENMP_AVAILABLE_RANSAC false
53 #endif
54 
55 //////////////////////////////////////////////////////////////////////////
56 template <typename PointT> bool
58 {
59  // Warn and exit if no threshold was set
60  if (threshold_ == std::numeric_limits<double>::max())
61  {
62  PCL_ERROR ("[pcl::RandomSampleConsensus::computeModel] No threshold set!\n");
63  return (false);
64  }
65 
66  iterations_ = 0;
67  std::size_t n_best_inliers_count = 0;
68  double k = std::numeric_limits<double>::max();
69 
70  std::vector<int> selection;
71  Eigen::VectorXf model_coefficients;
72 
73  const double log_probability = std::log (1.0 - probability_);
74  const double one_over_indices = 1.0 / static_cast<double> (sac_model_->getIndices ()->size ());
75 
76  std::size_t n_inliers_count;
77  unsigned skipped_count = 0;
78 
79  // suppress infinite loops by just allowing 10 x maximum allowed iterations for invalid model parameters!
80  const unsigned max_skip = max_iterations_ * 10;
81 
82  int threads = threads_;
83  if (threads >= 0)
84  {
85 #if OPENMP_AVAILABLE_RANSAC
86  if (threads == 0)
87  {
88  threads = omp_get_num_procs();
89  PCL_DEBUG ("[pcl::RandomSampleConsensus::computeModel] Automatic number of threads requested, choosing %i threads.\n", threads);
90  }
91 #else
92  // Parallelization desired, but not available
93  PCL_WARN ("[pcl::RandomSampleConsensus::computeModel] Parallelization is requested, but OpenMP 3.1 is not available! Continuing without parallelization.\n");
94  threads = -1;
95 #endif
96  }
97 
98 #if OPENMP_AVAILABLE_RANSAC
99 #pragma omp parallel if(threads > 0) num_threads(threads) shared(k, skipped_count, n_best_inliers_count) private(selection, model_coefficients, n_inliers_count) // would be nice to have a default(none)-clause here, but then some compilers complain about the shared const variables
100 #endif
101  {
102 #if OPENMP_AVAILABLE_RANSAC
103  if (omp_in_parallel())
104 #pragma omp master
105  PCL_DEBUG ("[pcl::RandomSampleConsensus::computeModel] Computing in parallel with up to %i threads.\n", omp_get_num_threads());
106  else
107 #endif
108  PCL_DEBUG ("[pcl::RandomSampleConsensus::computeModel] Computing not parallel.\n");
109 
110  // Iterate
111  while (true) // infinite loop with four possible breaks
112  {
113  // Get X samples which satisfy the model criteria
114 #if OPENMP_AVAILABLE_RANSAC
115 #pragma omp critical(samples)
116 #endif
117  {
118  sac_model_->getSamples (iterations_, selection); // The random number generator used when choosing the samples should not be called in parallel
119  }
120 
121  if (selection.empty ())
122  {
123  PCL_ERROR ("[pcl::RandomSampleConsensus::computeModel] No samples could be selected!\n");
124  break;
125  }
126 
127  // Search for inliers in the point cloud for the current plane model M
128  if (!sac_model_->computeModelCoefficients (selection, model_coefficients)) // This function has to be thread-safe
129  {
130  //++iterations_;
131  unsigned skipped_count_tmp;
132 #if OPENMP_AVAILABLE_RANSAC
133 #pragma omp atomic capture
134 #endif
135  skipped_count_tmp = ++skipped_count;
136  if (skipped_count_tmp < max_skip)
137  continue;
138  else
139  break;
140  }
141 
142  // Select the inliers that are within threshold_ from the model
143  //sac_model_->selectWithinDistance (model_coefficients, threshold_, inliers);
144  //if (inliers.empty () && k > 1.0)
145  // continue;
146 
147  n_inliers_count = sac_model_->countWithinDistance (model_coefficients, threshold_); // This functions has to be thread-safe. Most work is done here
148 
149  std::size_t n_best_inliers_count_tmp;
150 #if OPENMP_AVAILABLE_RANSAC
151 #pragma omp atomic read
152 #endif
153  n_best_inliers_count_tmp = n_best_inliers_count;
154 
155  if (n_inliers_count > n_best_inliers_count_tmp) // This condition is false most of the time, and the critical region is not entered, hopefully leading to more efficient concurrency
156  {
157 #if OPENMP_AVAILABLE_RANSAC
158 #pragma omp critical(update) // n_best_inliers_count, model_, model_coefficients_, k are shared and read/write must be protected
159 #endif
160  {
161  // Better match ?
162  if (n_inliers_count > n_best_inliers_count)
163  {
164  n_best_inliers_count = n_inliers_count; // This write and the previous read of n_best_inliers_count must be consecutive and must not be interrupted!
165  n_best_inliers_count_tmp = n_best_inliers_count;
166 
167  // Save the current model/inlier/coefficients selection as being the best so far
168  model_ = selection;
169  model_coefficients_ = model_coefficients;
170 
171  // Compute the k parameter (k=std::log(z)/std::log(1-w^n))
172  const double w = static_cast<double> (n_best_inliers_count) * one_over_indices;
173  double p_no_outliers = 1.0 - std::pow (w, static_cast<double> (selection.size ()));
174  p_no_outliers = (std::max) (std::numeric_limits<double>::epsilon (), p_no_outliers); // Avoid division by -Inf
175  p_no_outliers = (std::min) (1.0 - std::numeric_limits<double>::epsilon (), p_no_outliers); // Avoid division by 0.
176  k = log_probability / std::log (p_no_outliers);
177  }
178  } // omp critical
179  }
180 
181  int iterations_tmp;
182  double k_tmp;
183 #if OPENMP_AVAILABLE_RANSAC
184 #pragma omp atomic capture
185 #endif
186  iterations_tmp = ++iterations_;
187 #if OPENMP_AVAILABLE_RANSAC
188 #pragma omp atomic read
189 #endif
190  k_tmp = k;
191 #if OPENMP_AVAILABLE_RANSAC
192  PCL_DEBUG ("[pcl::RandomSampleConsensus::computeModel] Trial %d out of %f: %u inliers (best is: %u so far) (thread %d).\n", iterations_tmp, k_tmp, n_inliers_count, n_best_inliers_count_tmp, omp_get_thread_num());
193 #else
194  PCL_DEBUG ("[pcl::RandomSampleConsensus::computeModel] Trial %d out of %f: %u inliers (best is: %u so far).\n", iterations_tmp, k_tmp, n_inliers_count, n_best_inliers_count_tmp);
195 #endif
196  if (iterations_tmp > k_tmp)
197  break;
198  if (iterations_tmp > max_iterations_)
199  {
200  PCL_DEBUG ("[pcl::RandomSampleConsensus::computeModel] RANSAC reached the maximum number of trials.\n");
201  break;
202  }
203  } // while
204  } // omp parallel
205 
206  PCL_DEBUG ("[pcl::RandomSampleConsensus::computeModel] Model: %lu size, %u inliers.\n", model_.size (), n_best_inliers_count);
207 
208  if (model_.empty ())
209  {
210  PCL_ERROR ("[pcl::RandomSampleConsensus::computeModel] RANSAC found no model.\n");
211  inliers_.clear ();
212  return (false);
213  }
214 
215  // Get the set of inliers that correspond to the best model found so far
216  sac_model_->selectWithinDistance (model_coefficients_, threshold_, inliers_);
217  return (true);
218 }
219 
220 #define PCL_INSTANTIATE_RandomSampleConsensus(T) template class PCL_EXPORTS pcl::RandomSampleConsensus<T>;
221 
222 #endif // PCL_SAMPLE_CONSENSUS_IMPL_RANSAC_H_
223 
pcl::RandomSampleConsensus::computeModel
bool computeModel(int debug_verbosity_level=0) override
Compute the actual model and find the inliers.
Definition: ransac.hpp:57