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;
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 =
pcl_lrint (
static_cast<double>(sac_model_->getIndices ()->size ()) * fraction_nr_pretest_ / 100.0);
77 while (iterations_ < k)
80 sac_model_->getSamples (iterations_, selection);
82 if (selection.empty ())
84 PCL_ERROR (
"[pcl::RandomizedRandomSampleConsensus::computeModel] No samples could be selected!\n");
89 if (!sac_model_->computeModelCoefficients (selection, model_coefficients))
93 if (skipped_count < max_skip)
95 PCL_DEBUG (
"[pcl::RandomizedRandomSampleConsensus::computeModel] The function computeModelCoefficients failed, so continue with next iteration.\n");
100 PCL_DEBUG (
"[pcl::RandomizedRandomSampleConsensus::computeModel] The function computeModelCoefficients failed, and RRANSAC reached the maximum number of trials.\n");
107 this->getRandomSamples (sac_model_->getIndices (), fraction_nr_points, indices_subset);
108 if (!sac_model_->doSamplesVerifyModel (indices_subset, model_coefficients, threshold_))
111 PCL_DEBUG (
"[pcl::RandomizedRandomSampleConsensus::computeModel] The function doSamplesVerifyModel failed, so continue with next iteration.\n");
116 n_inliers_count = sac_model_->countWithinDistance (model_coefficients, threshold_);
119 if (n_inliers_count > n_best_inliers_count)
121 n_best_inliers_count = n_inliers_count;
125 model_coefficients_ = model_coefficients;
128 const double w =
static_cast<double> (n_inliers_count) * one_over_indices;
129 double p_no_outliers = 1.0 - std::pow (w,
static_cast<double> (selection.size ()));
130 p_no_outliers = (std::max) (std::numeric_limits<double>::epsilon (), p_no_outliers);
131 p_no_outliers = (std::min) (1.0 - std::numeric_limits<double>::epsilon (), p_no_outliers);
132 k = log_probability / std::log (p_no_outliers);
137 if (debug_verbosity_level > 1)
138 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);
139 if (iterations_ > max_iterations_)
141 if (debug_verbosity_level > 0)
142 PCL_DEBUG (
"[pcl::RandomizedRandomSampleConsensus::computeModel] RRANSAC reached the maximum number of trials.\n");
147 if (debug_verbosity_level > 0)
148 PCL_DEBUG (
"[pcl::RandomizedRandomSampleConsensus::computeModel] Model: %lu size, %u inliers.\n", model_.size (), n_best_inliers_count);
152 PCL_ERROR (
"[pcl::RandomizedRandomSampleConsensus::computeModel] RRANSAC found no model.\n");
158 sac_model_->selectWithinDistance (model_coefficients_, threshold_, inliers_);
162 #define PCL_INSTANTIATE_RandomizedRandomSampleConsensus(T) template class PCL_EXPORTS pcl::RandomizedRandomSampleConsensus<T>;
164 #endif // PCL_SAMPLE_CONSENSUS_IMPL_RRANSAC_H_