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 std::vector<int> best_model;
62 std::vector<int> selection;
63 Eigen::VectorXf model_coefficients;
64 std::vector<double> distances;
66 int n_inliers_count = 0;
67 unsigned skipped_count = 0;
69 const unsigned max_skip = max_iterations_ * 10;
72 while (iterations_ < k && skipped_count < max_skip)
75 sac_model_->getSamples (iterations_, selection);
77 if (selection.empty ())
break;
80 if (!sac_model_->computeModelCoefficients (selection, model_coefficients))
87 double d_cur_penalty = 0;
89 sac_model_->getDistancesToModel (model_coefficients, distances);
91 if (distances.empty () && k > 1.0)
94 for (
const double &
distance : distances)
95 d_cur_penalty += (std::min) (
distance, threshold_);
98 if (d_cur_penalty < d_best_penalty)
100 d_best_penalty = d_cur_penalty;
104 model_coefficients_ = model_coefficients;
108 for (
const double &
distance : distances)
113 double w = static_cast<double> (n_inliers_count) / static_cast<double> (sac_model_->getIndices ()->size ());
114 double p_no_outliers = 1.0 - std::pow (w, static_cast<double> (selection.size ()));
115 p_no_outliers = (std::max) (std::numeric_limits<double>::epsilon (), p_no_outliers);
116 p_no_outliers = (std::min) (1.0 - std::numeric_limits<double>::epsilon (), p_no_outliers);
117 k = std::log (1.0 - probability_) / std::log (p_no_outliers);
121 if (debug_verbosity_level > 1)
122 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);
123 if (iterations_ > max_iterations_)
125 if (debug_verbosity_level > 0)
126 PCL_DEBUG (
"[pcl::MEstimatorSampleConsensus::computeModel] MSAC reached the maximum number of trials.\n");
133 if (debug_verbosity_level > 0)
134 PCL_DEBUG (
"[pcl::MEstimatorSampleConsensus::computeModel] Unable to find a solution!\n");
139 sac_model_->getDistancesToModel (model_coefficients_, distances);
140 std::vector<int> &indices = *sac_model_->getIndices ();
142 if (distances.size () != indices.size ())
144 PCL_ERROR (
"[pcl::MEstimatorSampleConsensus::computeModel] Estimated distances (%lu) differs than the normal of indices (%lu).\n", distances.size (), indices.size ());
148 inliers_.resize (distances.size ());
151 for (std::size_t i = 0; i < distances.size (); ++i)
152 if (distances[i] <= threshold_)
153 inliers_[n_inliers_count++] = indices[i];
156 inliers_.resize (n_inliers_count);
158 if (debug_verbosity_level > 0)
159 PCL_DEBUG (
"[pcl::MEstimatorSampleConsensus::computeModel] Model: %lu size, %d inliers.\n", model_.size (), n_inliers_count);
164 #define PCL_INSTANTIATE_MEstimatorSampleConsensus(T) template class PCL_EXPORTS pcl::MEstimatorSampleConsensus<T>;
166 #endif // PCL_SAMPLE_CONSENSUS_IMPL_MSAC_H_