38 #ifndef PCL_SAMPLE_CONSENSUS_IMPL_RANSAC_H_
39 #define PCL_SAMPLE_CONSENSUS_IMPL_RANSAC_H_
44 template <
typename Po
intT>
bool
48 if (threshold_ == std::numeric_limits<double>::max())
50 PCL_ERROR (
"[pcl::RandomSampleConsensus::computeModel] No threshold set!\n");
55 int n_best_inliers_count = -INT_MAX;
58 std::vector<int> selection;
59 Eigen::VectorXf model_coefficients;
61 int n_inliers_count = 0;
62 unsigned skipped_count = 0;
64 const unsigned max_skip = max_iterations_ * 10;
67 while (iterations_ < k && skipped_count < max_skip)
70 sac_model_->getSamples (iterations_, selection);
72 if (selection.empty ())
74 PCL_ERROR (
"[pcl::RandomSampleConsensus::computeModel] No samples could be selected!\n");
79 if (!sac_model_->computeModelCoefficients (selection, model_coefficients))
91 n_inliers_count = sac_model_->countWithinDistance (model_coefficients, threshold_);
94 if (n_inliers_count > n_best_inliers_count)
96 n_best_inliers_count = n_inliers_count;
100 model_coefficients_ = model_coefficients;
103 double w =
static_cast<double> (n_best_inliers_count) / static_cast<double> (sac_model_->getIndices ()->size ());
104 double p_no_outliers = 1.0 - pow (w, static_cast<double> (selection.size ()));
105 p_no_outliers = (std::max) (std::numeric_limits<double>::epsilon (), p_no_outliers);
106 p_no_outliers = (std::min) (1.0 - std::numeric_limits<double>::epsilon (), p_no_outliers);
107 k = log (1.0 - probability_) / log (p_no_outliers);
111 if (debug_verbosity_level > 1)
112 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);
113 if (iterations_ > max_iterations_)
115 if (debug_verbosity_level > 0)
116 PCL_DEBUG (
"[pcl::RandomSampleConsensus::computeModel] RANSAC reached the maximum number of trials.\n");
121 if (debug_verbosity_level > 0)
122 PCL_DEBUG (
"[pcl::RandomSampleConsensus::computeModel] Model: %zu size, %d inliers.\n", model_.size (), n_best_inliers_count);
131 sac_model_->selectWithinDistance (model_coefficients_, threshold_, inliers_);
135 #define PCL_INSTANTIATE_RandomSampleConsensus(T) template class PCL_EXPORTS pcl::RandomSampleConsensus<T>;
137 #endif // PCL_SAMPLE_CONSENSUS_IMPL_RANSAC_H_