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 std::vector<int> best_model;
62 std::vector<int> selection;
63 Eigen::VectorXf model_coefficients;
64 std::vector<double> distances;
65 std::set<int> indices_subset;
67 int n_inliers_count = 0;
68 unsigned skipped_count = 0;
70 const unsigned max_skip = max_iterations_ * 10;
73 size_t fraction_nr_points = pcl_lrint (
static_cast<double>(sac_model_->getIndices ()->size ()) * fraction_nr_pretest_ / 100.0);
76 while (iterations_ < k && skipped_count < max_skip)
79 sac_model_->getSamples (iterations_, selection);
81 if (selection.empty ())
break;
84 if (!sac_model_->computeModelCoefficients (selection, model_coefficients))
93 this->getRandomSamples (sac_model_->getIndices (), fraction_nr_points, indices_subset);
95 if (!sac_model_->doSamplesVerifyModel (indices_subset, model_coefficients, threshold_))
105 double d_cur_penalty = 0;
107 sac_model_->getDistancesToModel (model_coefficients, distances);
109 if (distances.empty () && k > 1.0)
112 for (
size_t i = 0; i < distances.size (); ++i)
113 d_cur_penalty += (std::min) (distances[i], threshold_);
116 if (d_cur_penalty < d_best_penalty)
118 d_best_penalty = d_cur_penalty;
122 model_coefficients_ = model_coefficients;
126 for (
size_t i = 0; i < distances.size (); ++i)
127 if (distances[i] <= threshold_)
131 double w =
static_cast<double> (n_inliers_count) /
static_cast<double>(sac_model_->getIndices ()->size ());
132 double p_no_outliers = 1 - pow (w,
static_cast<double> (selection.size ()));
133 p_no_outliers = (std::max) (std::numeric_limits<double>::epsilon (), p_no_outliers);
134 p_no_outliers = (std::min) (1 - std::numeric_limits<double>::epsilon (), p_no_outliers);
135 k = log (1 - probability_) / log (p_no_outliers);
139 if (debug_verbosity_level > 1)
140 PCL_DEBUG (
"[pcl::RandomizedMEstimatorSampleConsensus::computeModel] Trial %d out of %d. Best penalty is %f.\n", iterations_,
static_cast<int> (ceil (k)), d_best_penalty);
141 if (iterations_ > max_iterations_)
143 if (debug_verbosity_level > 0)
144 PCL_DEBUG (
"[pcl::RandomizedMEstimatorSampleConsensus::computeModel] MSAC reached the maximum number of trials.\n");
151 if (debug_verbosity_level > 0)
152 PCL_DEBUG (
"[pcl::RandomizedMEstimatorSampleConsensus::computeModel] Unable to find a solution!\n");
157 sac_model_->getDistancesToModel (model_coefficients_, distances);
158 std::vector<int> &indices = *sac_model_->getIndices ();
159 if (distances.size () != indices.size ())
161 PCL_ERROR (
"[pcl::RandomizedMEstimatorSampleConsensus::computeModel] Estimated distances (%lu) differs than the normal of indices (%lu).\n", distances.size (), indices.size ());
165 inliers_.resize (distances.size ());
168 for (
size_t i = 0; i < distances.size (); ++i)
169 if (distances[i] <= threshold_)
170 inliers_[n_inliers_count++] = indices[i];
173 inliers_.resize (n_inliers_count);
175 if (debug_verbosity_level > 0)
176 PCL_DEBUG (
"[pcl::RandomizedMEstimatorSampleConsensus::computeModel] Model: %lu size, %d inliers.\n", model_.size (), n_inliers_count);
181 #define PCL_INSTANTIATE_RandomizedMEstimatorSampleConsensus(T) template class PCL_EXPORTS pcl::RandomizedMEstimatorSampleConsensus<T>;
183 #endif // PCL_SAMPLE_CONSENSUS_IMPL_RMSAC_H_