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
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 (sac_model_->getModelSize ());
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 unsigned skipped_count = 0;
79 const unsigned max_skip = max_iterations_ * 10;
81 int threads = threads_;
84 #if OPENMP_AVAILABLE_RANSAC
87 threads = omp_get_num_procs();
88 PCL_DEBUG (
"[pcl::RandomSampleConsensus::computeModel] Automatic number of threads requested, choosing %i threads.\n", threads);
92 PCL_WARN (
"[pcl::RandomSampleConsensus::computeModel] Parallelization is requested, but OpenMP 3.1 is not available! Continuing without parallelization.\n");
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)
101 #if OPENMP_AVAILABLE_RANSAC
102 if (omp_in_parallel())
104 PCL_DEBUG (
"[pcl::RandomSampleConsensus::computeModel] Computing in parallel with up to %i threads.\n", omp_get_num_threads());
107 PCL_DEBUG (
"[pcl::RandomSampleConsensus::computeModel] Computing not parallel.\n");
113 #if OPENMP_AVAILABLE_RANSAC
114 #pragma omp critical(samples)
117 sac_model_->getSamples (iterations_, selection);
120 if (selection.empty ())
122 PCL_ERROR (
"[pcl::RandomSampleConsensus::computeModel] No samples could be selected!\n");
127 if (!sac_model_->computeModelCoefficients (selection, model_coefficients))
130 unsigned skipped_count_tmp;
131 #if OPENMP_AVAILABLE_RANSAC
132 #pragma omp atomic capture
134 skipped_count_tmp = ++skipped_count;
135 if (skipped_count_tmp < max_skip)
146 std::size_t n_inliers_count = sac_model_->countWithinDistance (model_coefficients, threshold_);
148 std::size_t n_best_inliers_count_tmp;
149 #if OPENMP_AVAILABLE_RANSAC
150 #pragma omp atomic read
152 n_best_inliers_count_tmp = n_best_inliers_count;
154 if (n_inliers_count > n_best_inliers_count_tmp)
156 #if OPENMP_AVAILABLE_RANSAC
157 #pragma omp critical(update)
161 if (n_inliers_count > n_best_inliers_count)
163 n_best_inliers_count = n_inliers_count;
164 n_best_inliers_count_tmp = n_best_inliers_count;
168 model_coefficients_ = model_coefficients;
171 const double w =
static_cast<double> (n_best_inliers_count) * one_over_indices;
172 double p_outliers = 1.0 - std::pow (w,
static_cast<double> (selection.size ()));
173 p_outliers = (std::max) (std::numeric_limits<double>::epsilon (), p_outliers);
174 p_outliers = (std::min) (1.0 - std::numeric_limits<double>::epsilon (), p_outliers);
175 k = log_probability / std::log (p_outliers);
182 #if OPENMP_AVAILABLE_RANSAC
183 #pragma omp atomic capture
185 iterations_tmp = ++iterations_;
186 #if OPENMP_AVAILABLE_RANSAC
187 #pragma omp atomic read
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());
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);
195 if (iterations_tmp > k_tmp)
197 if (iterations_tmp > max_iterations_)
199 PCL_DEBUG (
"[pcl::RandomSampleConsensus::computeModel] RANSAC reached the maximum number of trials.\n");
205 PCL_DEBUG (
"[pcl::RandomSampleConsensus::computeModel] Model: %lu size, %u inliers.\n", model_.size (), n_best_inliers_count);
209 PCL_ERROR (
"[pcl::RandomSampleConsensus::computeModel] RANSAC found no model.\n");
215 sac_model_->selectWithinDistance (model_coefficients_, threshold_, inliers_);
219 #define PCL_INSTANTIATE_RandomSampleConsensus(T) template class PCL_EXPORTS pcl::RandomSampleConsensus<T>;
bool computeModel(int debug_verbosity_level=0) override
Compute the actual model and find the inliers.
IndicesAllocator<> Indices
Type used for indices in PCL.