41 #ifndef PCL_SAMPLE_CONSENSUS_IMPL_RANSAC_H_
42 #define PCL_SAMPLE_CONSENSUS_IMPL_RANSAC_H_
44 #include <pcl/sample_consensus/ransac.h>
49 #if defined _OPENMP && _OPENMP >= 201107 // We need OpenMP 3.1 for the atomic constructs
50 #define OPENMP_AVAILABLE_RANSAC true
52 #define OPENMP_AVAILABLE_RANSAC false
56 template <
typename Po
intT>
bool
60 if (threshold_ == std::numeric_limits<double>::max())
62 PCL_ERROR (
"[pcl::RandomSampleConsensus::computeModel] No threshold set!\n");
67 std::size_t n_best_inliers_count = 0;
68 double k = std::numeric_limits<double>::max();
71 Eigen::VectorXf model_coefficients;
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 ());
76 std::size_t n_inliers_count;
77 unsigned skipped_count = 0;
80 const unsigned max_skip = max_iterations_ * 10;
82 int threads = threads_;
85 #if OPENMP_AVAILABLE_RANSAC
88 threads = omp_get_num_procs();
89 PCL_DEBUG (
"[pcl::RandomSampleConsensus::computeModel] Automatic number of threads requested, choosing %i threads.\n", threads);
93 PCL_WARN (
"[pcl::RandomSampleConsensus::computeModel] Parallelization is requested, but OpenMP 3.1 is not available! Continuing without parallelization.\n");
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
102 #if OPENMP_AVAILABLE_RANSAC
103 if (omp_in_parallel())
105 PCL_DEBUG (
"[pcl::RandomSampleConsensus::computeModel] Computing in parallel with up to %i threads.\n", omp_get_num_threads());
108 PCL_DEBUG (
"[pcl::RandomSampleConsensus::computeModel] Computing not parallel.\n");
114 #if OPENMP_AVAILABLE_RANSAC
115 #pragma omp critical(samples)
118 sac_model_->getSamples (iterations_, selection);
121 if (selection.empty ())
123 PCL_ERROR (
"[pcl::RandomSampleConsensus::computeModel] No samples could be selected!\n");
128 if (!sac_model_->computeModelCoefficients (selection, model_coefficients))
131 unsigned skipped_count_tmp;
132 #if OPENMP_AVAILABLE_RANSAC
133 #pragma omp atomic capture
135 skipped_count_tmp = ++skipped_count;
136 if (skipped_count_tmp < max_skip)
147 n_inliers_count = sac_model_->countWithinDistance (model_coefficients, threshold_);
149 std::size_t n_best_inliers_count_tmp;
150 #if OPENMP_AVAILABLE_RANSAC
151 #pragma omp atomic read
153 n_best_inliers_count_tmp = n_best_inliers_count;
155 if (n_inliers_count > n_best_inliers_count_tmp)
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
162 if (n_inliers_count > n_best_inliers_count)
164 n_best_inliers_count = n_inliers_count;
165 n_best_inliers_count_tmp = n_best_inliers_count;
169 model_coefficients_ = model_coefficients;
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);
175 p_no_outliers = (std::min) (1.0 - std::numeric_limits<double>::epsilon (), p_no_outliers);
176 k = log_probability / std::log (p_no_outliers);
183 #if OPENMP_AVAILABLE_RANSAC
184 #pragma omp atomic capture
186 iterations_tmp = ++iterations_;
187 #if OPENMP_AVAILABLE_RANSAC
188 #pragma omp atomic read
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());
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);
196 if (iterations_tmp > k_tmp)
198 if (iterations_tmp > max_iterations_)
200 PCL_DEBUG (
"[pcl::RandomSampleConsensus::computeModel] RANSAC reached the maximum number of trials.\n");
206 PCL_DEBUG (
"[pcl::RandomSampleConsensus::computeModel] Model: %lu size, %u inliers.\n", model_.size (), n_best_inliers_count);
210 PCL_ERROR (
"[pcl::RandomSampleConsensus::computeModel] RANSAC found no model.\n");
216 sac_model_->selectWithinDistance (model_coefficients_, threshold_, inliers_);
220 #define PCL_INSTANTIATE_RandomSampleConsensus(T) template class PCL_EXPORTS pcl::RandomSampleConsensus<T>;
222 #endif // PCL_SAMPLE_CONSENSUS_IMPL_RANSAC_H_
bool computeModel(int debug_verbosity_level=0) override
Compute the actual model and find the inliers.