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