41 #ifndef PCL_SAMPLE_CONSENSUS_IMPL_MSAC_H_
42 #define PCL_SAMPLE_CONSENSUS_IMPL_MSAC_H_
44 #include <pcl/sample_consensus/msac.h>
47 template <
typename Po
intT>
bool
51 if (threshold_ == std::numeric_limits<double>::max())
53 PCL_ERROR (
"[pcl::MEstimatorSampleConsensus::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;
68 int n_inliers_count = 0;
69 unsigned skipped_count = 0;
71 const unsigned max_skip = max_iterations_ * 10;
74 while (iterations_ < k && skipped_count < max_skip)
77 sac_model_->getSamples (iterations_, selection);
79 if (selection.empty ())
break;
82 if (!sac_model_->computeModelCoefficients (selection, model_coefficients))
89 double d_cur_penalty = 0;
91 sac_model_->getDistancesToModel (model_coefficients, distances);
93 if (distances.empty ())
100 for (
const double &
distance : distances)
101 d_cur_penalty += (std::min) (
distance, threshold_);
104 if (d_cur_penalty < d_best_penalty)
106 d_best_penalty = d_cur_penalty;
110 model_coefficients_ = model_coefficients;
114 for (
const double &
distance : distances)
119 const double w =
static_cast<double> (n_inliers_count) * one_over_indices;
120 double p_outliers = 1.0 - std::pow (w,
static_cast<double> (selection.size ()));
121 p_outliers = (std::max) (std::numeric_limits<double>::epsilon (), p_outliers);
122 p_outliers = (std::min) (1.0 - std::numeric_limits<double>::epsilon (), p_outliers);
123 k = log_probability / std::log (p_outliers);
127 if (debug_verbosity_level > 1)
128 PCL_DEBUG (
"[pcl::MEstimatorSampleConsensus::computeModel] Trial %d out of %d. Best penalty is %f.\n", iterations_,
static_cast<int> (std::ceil (k)), d_best_penalty);
129 if (iterations_ > max_iterations_)
131 if (debug_verbosity_level > 0)
132 PCL_DEBUG (
"[pcl::MEstimatorSampleConsensus::computeModel] MSAC reached the maximum number of trials.\n");
139 if (debug_verbosity_level > 0)
140 PCL_DEBUG (
"[pcl::MEstimatorSampleConsensus::computeModel] Unable to find a solution!\n");
145 sac_model_->getDistancesToModel (model_coefficients_, distances);
146 Indices &indices = *sac_model_->getIndices ();
148 if (distances.size () != indices.size ())
150 PCL_ERROR (
"[pcl::MEstimatorSampleConsensus::computeModel] Estimated distances (%lu) differs than the normal of indices (%lu).\n", distances.size (), indices.size ());
154 inliers_.resize (distances.size ());
157 for (std::size_t i = 0; i < distances.size (); ++i)
158 if (distances[i] <= threshold_)
159 inliers_[n_inliers_count++] = indices[i];
162 inliers_.resize (n_inliers_count);
164 if (debug_verbosity_level > 0)
165 PCL_DEBUG (
"[pcl::MEstimatorSampleConsensus::computeModel] Model: %lu size, %d inliers.\n", model_.size (), n_inliers_count);
170 #define PCL_INSTANTIATE_MEstimatorSampleConsensus(T) template class PCL_EXPORTS pcl::MEstimatorSampleConsensus<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.