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 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));
76 PCL_DEBUG (
"[pcl::RandomizedMEstimatorSampleConsensus::computeModel] Using %lu points for RMSAC pre-test.\n", fraction_nr_points);
79 while (iterations_ < k && skipped_count < max_skip)
82 sac_model_->getSamples (iterations_, selection);
84 if (selection.empty ())
break;
87 if (!sac_model_->computeModelCoefficients (selection, model_coefficients))
96 this->getRandomSamples (sac_model_->getIndices (), fraction_nr_points, indices_subset);
98 if (!sac_model_->doSamplesVerifyModel (indices_subset, model_coefficients, threshold_))
108 double d_cur_penalty = 0;
110 sac_model_->getDistancesToModel (model_coefficients, distances);
112 if (distances.empty ())
118 for (
const double &
distance : distances)
119 d_cur_penalty += std::min (
distance, threshold_);
122 if (d_cur_penalty < d_best_penalty)
124 d_best_penalty = d_cur_penalty;
128 model_coefficients_ = model_coefficients;
132 for (
const double &
distance : distances)
137 const double w =
static_cast<double> (n_inliers_count) * one_over_indices;
138 double p_outliers = 1.0 - std::pow (w,
static_cast<double> (selection.size ()));
139 p_outliers = (std::max) (std::numeric_limits<double>::epsilon (), p_outliers);
140 p_outliers = (std::min) (1.0 - std::numeric_limits<double>::epsilon (), p_outliers);
141 k = log_probability / std::log (p_outliers);
145 if (debug_verbosity_level > 1)
146 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);
147 if (iterations_ > max_iterations_)
149 if (debug_verbosity_level > 0)
150 PCL_DEBUG (
"[pcl::RandomizedMEstimatorSampleConsensus::computeModel] MSAC reached the maximum number of trials.\n");
157 if (debug_verbosity_level > 0)
158 PCL_DEBUG (
"[pcl::RandomizedMEstimatorSampleConsensus::computeModel] Unable to find a solution!\n");
163 sac_model_->getDistancesToModel (model_coefficients_, distances);
164 Indices &indices = *sac_model_->getIndices ();
165 if (distances.size () != indices.size ())
167 PCL_ERROR (
"[pcl::RandomizedMEstimatorSampleConsensus::computeModel] Estimated distances (%lu) differs than the normal of indices (%lu).\n", distances.size (), indices.size ());
171 inliers_.resize (distances.size ());
174 for (std::size_t i = 0; i < distances.size (); ++i)
175 if (distances[i] <= threshold_)
176 inliers_[n_inliers_count++] = indices[i];
179 inliers_.resize (n_inliers_count);
181 if (debug_verbosity_level > 0)
182 PCL_DEBUG (
"[pcl::RandomizedMEstimatorSampleConsensus::computeModel] Model: %lu size, %d inliers.\n", model_.size (), n_inliers_count);
187 #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.