41 #ifndef PCL_SAMPLE_CONSENSUS_IMPL_RRANSAC_H_
42 #define PCL_SAMPLE_CONSENSUS_IMPL_RRANSAC_H_
44 #include <pcl/sample_consensus/rransac.h>
47 template <
typename Po
intT>
bool
51 if (threshold_ == std::numeric_limits<double>::max())
53 PCL_ERROR (
"[pcl::RandomizedRandomSampleConsensus::computeModel] No threshold set!\n");
58 std::size_t n_best_inliers_count = 0;
59 double k = std::numeric_limits<double>::max();
62 Eigen::VectorXf model_coefficients (sac_model_->getModelSize ());
63 std::set<index_t> indices_subset;
65 const double log_probability = std::log (1.0 - probability_);
66 const double one_over_indices = 1.0 /
static_cast<double> (sac_model_->getIndices ()->size ());
68 std::size_t n_inliers_count;
69 unsigned skipped_count = 0;
71 const unsigned max_skip = max_iterations_ * 10;
74 const std::size_t fraction_nr_points = (fraction_nr_pretest_ < 0.0 ? nr_samples_pretest_ :
pcl_lrint (
static_cast<double>(sac_model_->getIndices ()->size ()) * fraction_nr_pretest_ / 100.0));
75 PCL_DEBUG (
"[pcl::RandomizedRandomSampleConsensus::computeModel] Using %lu points for RRANSAC pre-test.\n", fraction_nr_points);
78 while (iterations_ < k)
81 sac_model_->getSamples (iterations_, selection);
83 if (selection.empty ())
85 PCL_ERROR (
"[pcl::RandomizedRandomSampleConsensus::computeModel] No samples could be selected!\n");
90 if (!sac_model_->computeModelCoefficients (selection, model_coefficients))
94 if (skipped_count < max_skip)
96 PCL_DEBUG (
"[pcl::RandomizedRandomSampleConsensus::computeModel] The function computeModelCoefficients failed, so continue with next iteration.\n");
101 PCL_DEBUG (
"[pcl::RandomizedRandomSampleConsensus::computeModel] The function computeModelCoefficients failed, and RRANSAC reached the maximum number of trials.\n");
108 this->getRandomSamples (sac_model_->getIndices (), fraction_nr_points, indices_subset);
109 if (!sac_model_->doSamplesVerifyModel (indices_subset, model_coefficients, threshold_))
112 PCL_DEBUG (
"[pcl::RandomizedRandomSampleConsensus::computeModel] The function doSamplesVerifyModel failed, so continue with next iteration.\n");
117 n_inliers_count = sac_model_->countWithinDistance (model_coefficients, threshold_);
120 if (n_inliers_count > n_best_inliers_count)
122 n_best_inliers_count = n_inliers_count;
126 model_coefficients_ = model_coefficients;
129 const double w =
static_cast<double> (n_best_inliers_count) * one_over_indices;
130 double p_outliers = 1.0 - std::pow (w,
static_cast<double> (selection.size ()));
131 p_outliers = (std::max) (std::numeric_limits<double>::epsilon (), p_outliers);
132 p_outliers = (std::min) (1.0 - std::numeric_limits<double>::epsilon (), p_outliers);
133 k = log_probability / std::log (p_outliers);
138 if (debug_verbosity_level > 1)
139 PCL_DEBUG (
"[pcl::RandomizedRandomSampleConsensus::computeModel] Trial %d out of %d: %u inliers (best is: %u so far).\n", iterations_,
static_cast<int> (std::ceil (k)), n_inliers_count, n_best_inliers_count);
140 if (iterations_ > max_iterations_)
142 if (debug_verbosity_level > 0)
143 PCL_DEBUG (
"[pcl::RandomizedRandomSampleConsensus::computeModel] RRANSAC reached the maximum number of trials.\n");
148 if (debug_verbosity_level > 0)
149 PCL_DEBUG (
"[pcl::RandomizedRandomSampleConsensus::computeModel] Model: %lu size, %u inliers.\n", model_.size (), n_best_inliers_count);
153 PCL_ERROR (
"[pcl::RandomizedRandomSampleConsensus::computeModel] RRANSAC found no model.\n");
159 sac_model_->selectWithinDistance (model_coefficients_, threshold_, inliers_);
163 #define PCL_INSTANTIATE_RandomizedRandomSampleConsensus(T) template class PCL_EXPORTS pcl::RandomizedRandomSampleConsensus<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.