41 #ifndef PCL_SAMPLE_CONSENSUS_IMPL_RMSAC_H_
42 #define PCL_SAMPLE_CONSENSUS_IMPL_RMSAC_H_
44 #include <pcl/sample_consensus/rmsac.h>
47 template <
typename Po
intT>
bool
51 if (threshold_ == std::numeric_limits<double>::max())
53 PCL_ERROR (
"[pcl::RandomizedMEstimatorSampleConsensus::computeModel] No threshold set!\n");
58 double d_best_penalty = std::numeric_limits<double>::max();
61 const double log_probability = std::log (1.0 - probability_);
62 const double one_over_indices = 1.0 /
static_cast<double> (sac_model_->getIndices ()->size ());
65 Eigen::VectorXf model_coefficients (sac_model_->getModelSize ());
66 std::vector<double> distances;
67 std::set<index_t> indices_subset;
69 int n_inliers_count = 0;
70 unsigned skipped_count = 0;
72 const unsigned max_skip = max_iterations_ * 10;
75 std::size_t fraction_nr_points =
pcl_lrint (
static_cast<double>(sac_model_->getIndices ()->size ()) * fraction_nr_pretest_ / 100.0);
78 while (iterations_ < k && skipped_count < max_skip)
81 sac_model_->getSamples (iterations_, selection);
83 if (selection.empty ())
break;
86 if (!sac_model_->computeModelCoefficients (selection, model_coefficients))
95 this->getRandomSamples (sac_model_->getIndices (), fraction_nr_points, indices_subset);
97 if (!sac_model_->doSamplesVerifyModel (indices_subset, model_coefficients, threshold_))
107 double d_cur_penalty = 0;
109 sac_model_->getDistancesToModel (model_coefficients, distances);
111 if (distances.empty ())
117 for (
const double &
distance : distances)
118 d_cur_penalty += std::min (
distance, threshold_);
121 if (d_cur_penalty < d_best_penalty)
123 d_best_penalty = d_cur_penalty;
127 model_coefficients_ = model_coefficients;
131 for (
const double &
distance : distances)
136 const double w =
static_cast<double> (n_inliers_count) * one_over_indices;
137 double p_outliers = 1.0 - std::pow (w,
static_cast<double> (selection.size ()));
138 p_outliers = (std::max) (std::numeric_limits<double>::epsilon (), p_outliers);
139 p_outliers = (std::min) (1.0 - std::numeric_limits<double>::epsilon (), p_outliers);
140 k = log_probability / std::log (p_outliers);
144 if (debug_verbosity_level > 1)
145 PCL_DEBUG (
"[pcl::RandomizedMEstimatorSampleConsensus::computeModel] Trial %d out of %d. Best penalty is %f.\n", iterations_,
static_cast<int> (std::ceil (k)), d_best_penalty);
146 if (iterations_ > max_iterations_)
148 if (debug_verbosity_level > 0)
149 PCL_DEBUG (
"[pcl::RandomizedMEstimatorSampleConsensus::computeModel] MSAC reached the maximum number of trials.\n");
156 if (debug_verbosity_level > 0)
157 PCL_DEBUG (
"[pcl::RandomizedMEstimatorSampleConsensus::computeModel] Unable to find a solution!\n");
162 sac_model_->getDistancesToModel (model_coefficients_, distances);
163 Indices &indices = *sac_model_->getIndices ();
164 if (distances.size () != indices.size ())
166 PCL_ERROR (
"[pcl::RandomizedMEstimatorSampleConsensus::computeModel] Estimated distances (%lu) differs than the normal of indices (%lu).\n", distances.size (), indices.size ());
170 inliers_.resize (distances.size ());
173 for (std::size_t i = 0; i < distances.size (); ++i)
174 if (distances[i] <= threshold_)
175 inliers_[n_inliers_count++] = indices[i];
178 inliers_.resize (n_inliers_count);
180 if (debug_verbosity_level > 0)
181 PCL_DEBUG (
"[pcl::RandomizedMEstimatorSampleConsensus::computeModel] Model: %lu size, %d inliers.\n", model_.size (), n_inliers_count);
186 #define PCL_INSTANTIATE_RandomizedMEstimatorSampleConsensus(T) template class PCL_EXPORTS pcl::RandomizedMEstimatorSampleConsensus<T>;
bool computeModel(int debug_verbosity_level=0) override
Compute the actual model and find the inliers.
float distance(const PointT &p1, const PointT &p2)
IndicesAllocator<> Indices
Type used for indices in PCL.