38 #ifndef PCL_SAMPLE_CONSENSUS_IMPL_MSAC_H_
39 #define PCL_SAMPLE_CONSENSUS_IMPL_MSAC_H_
44 template <
typename Po
intT>
bool
48 if (threshold_ == std::numeric_limits<double>::max())
50 PCL_ERROR (
"[pcl::MEstimatorSampleConsensus::computeModel] No threshold set!\n");
55 double d_best_penalty = std::numeric_limits<double>::max();
58 std::vector<int> best_model;
59 std::vector<int> selection;
60 Eigen::VectorXf model_coefficients;
61 std::vector<double> distances;
63 int n_inliers_count = 0;
64 unsigned skipped_count = 0;
66 const unsigned max_skip = max_iterations_ * 10;
69 while (iterations_ < k && skipped_count < max_skip)
72 sac_model_->getSamples (iterations_, selection);
74 if (selection.empty ())
break;
77 if (!sac_model_->computeModelCoefficients (selection, model_coefficients))
84 double d_cur_penalty = 0;
86 sac_model_->getDistancesToModel (model_coefficients, distances);
88 if (distances.empty () && k > 1.0)
91 for (
size_t i = 0; i < distances.size (); ++i)
92 d_cur_penalty += (std::min) (distances[i], threshold_);
95 if (d_cur_penalty < d_best_penalty)
97 d_best_penalty = d_cur_penalty;
101 model_coefficients_ = model_coefficients;
105 for (
size_t i = 0; i < distances.size (); ++i)
106 if (distances[i] <= threshold_)
110 double w =
static_cast<double> (n_inliers_count) / static_cast<double> (sac_model_->getIndices ()->size ());
111 double p_no_outliers = 1.0 - pow (w, static_cast<double> (selection.size ()));
112 p_no_outliers = (std::max) (std::numeric_limits<double>::epsilon (), p_no_outliers);
113 p_no_outliers = (std::min) (1.0 - std::numeric_limits<double>::epsilon (), p_no_outliers);
114 k = log (1.0 - probability_) / log (p_no_outliers);
118 if (debug_verbosity_level > 1)
119 PCL_DEBUG (
"[pcl::MEstimatorSampleConsensus::computeModel] Trial %d out of %d. Best penalty is %f.\n", iterations_, static_cast<int> (ceil (k)), d_best_penalty);
120 if (iterations_ > max_iterations_)
122 if (debug_verbosity_level > 0)
123 PCL_DEBUG (
"[pcl::MEstimatorSampleConsensus::computeModel] MSAC reached the maximum number of trials.\n");
130 if (debug_verbosity_level > 0)
131 PCL_DEBUG (
"[pcl::MEstimatorSampleConsensus::computeModel] Unable to find a solution!\n");
136 sac_model_->getDistancesToModel (model_coefficients_, distances);
137 std::vector<int> &indices = *sac_model_->getIndices ();
139 if (distances.size () != indices.size ())
141 PCL_ERROR (
"[pcl::MEstimatorSampleConsensus::computeModel] Estimated distances (%zu) differs than the normal of indices (%zu).\n", distances.size (), indices.size ());
145 inliers_.resize (distances.size ());
148 for (
size_t i = 0; i < distances.size (); ++i)
149 if (distances[i] <= threshold_)
150 inliers_[n_inliers_count++] = indices[i];
153 inliers_.resize (n_inliers_count);
155 if (debug_verbosity_level > 0)
156 PCL_DEBUG (
"[pcl::MEstimatorSampleConsensus::computeModel] Model: %zu size, %d inliers.\n", model_.size (), n_inliers_count);
161 #define PCL_INSTANTIATE_MEstimatorSampleConsensus(T) template class PCL_EXPORTS pcl::MEstimatorSampleConsensus<T>;
163 #endif // PCL_SAMPLE_CONSENSUS_IMPL_MSAC_H_