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();
62 Eigen::VectorXf model_coefficients;
63 std::vector<double> distances;
64 std::set<index_t> indices_subset;
66 int n_inliers_count = 0;
67 unsigned skipped_count = 0;
69 const unsigned max_skip = max_iterations_ * 10;
72 std::size_t fraction_nr_points =
pcl_lrint (
static_cast<double>(sac_model_->getIndices ()->size ()) * fraction_nr_pretest_ / 100.0);
75 while (iterations_ < k && skipped_count < max_skip)
78 sac_model_->getSamples (iterations_, selection);
80 if (selection.empty ())
break;
83 if (!sac_model_->computeModelCoefficients (selection, model_coefficients))
92 this->getRandomSamples (sac_model_->getIndices (), fraction_nr_points, indices_subset);
94 if (!sac_model_->doSamplesVerifyModel (indices_subset, model_coefficients, threshold_))
104 double d_cur_penalty = 0;
106 sac_model_->getDistancesToModel (model_coefficients, distances);
108 if (distances.empty () && k > 1.0)
111 for (
const double &
distance : distances)
112 d_cur_penalty += std::min (
distance, threshold_);
115 if (d_cur_penalty < d_best_penalty)
117 d_best_penalty = d_cur_penalty;
121 model_coefficients_ = model_coefficients;
125 for (
const double &
distance : distances)
130 double w =
static_cast<double> (n_inliers_count) /
static_cast<double>(sac_model_->getIndices ()->size ());
131 double p_no_outliers = 1 - std::pow (w,
static_cast<double> (selection.size ()));
132 p_no_outliers = (std::max) (std::numeric_limits<double>::epsilon (), p_no_outliers);
133 p_no_outliers = (std::min) (1 - std::numeric_limits<double>::epsilon (), p_no_outliers);
134 k = std::log (1 - probability_) / std::log (p_no_outliers);
138 if (debug_verbosity_level > 1)
139 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);
140 if (iterations_ > max_iterations_)
142 if (debug_verbosity_level > 0)
143 PCL_DEBUG (
"[pcl::RandomizedMEstimatorSampleConsensus::computeModel] MSAC reached the maximum number of trials.\n");
150 if (debug_verbosity_level > 0)
151 PCL_DEBUG (
"[pcl::RandomizedMEstimatorSampleConsensus::computeModel] Unable to find a solution!\n");
156 sac_model_->getDistancesToModel (model_coefficients_, distances);
157 Indices &indices = *sac_model_->getIndices ();
158 if (distances.size () != indices.size ())
160 PCL_ERROR (
"[pcl::RandomizedMEstimatorSampleConsensus::computeModel] Estimated distances (%lu) differs than the normal of indices (%lu).\n", distances.size (), indices.size ());
164 inliers_.resize (distances.size ());
167 for (std::size_t i = 0; i < distances.size (); ++i)
168 if (distances[i] <= threshold_)
169 inliers_[n_inliers_count++] = indices[i];
172 inliers_.resize (n_inliers_count);
174 if (debug_verbosity_level > 0)
175 PCL_DEBUG (
"[pcl::RandomizedMEstimatorSampleConsensus::computeModel] Model: %lu size, %d inliers.\n", model_.size (), n_inliers_count);
180 #define PCL_INSTANTIATE_RandomizedMEstimatorSampleConsensus(T) template class PCL_EXPORTS pcl::RandomizedMEstimatorSampleConsensus<T>;
182 #endif // PCL_SAMPLE_CONSENSUS_IMPL_RMSAC_H_