38 #ifndef PCL_SAMPLE_CONSENSUS_IMPL_RMSAC_H_
39 #define PCL_SAMPLE_CONSENSUS_IMPL_RMSAC_H_
44 template <
typename Po
intT>
bool
48 if (threshold_ == std::numeric_limits<double>::max())
50 PCL_ERROR (
"[pcl::RandomizedMEstimatorSampleConsensus::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;
62 std::set<int> indices_subset;
64 int n_inliers_count = 0;
65 unsigned skipped_count = 0;
67 const unsigned max_skip = max_iterations_ * 10;
70 size_t fraction_nr_points =
pcl_lrint (static_cast<double>(sac_model_->getIndices ()->size ()) * fraction_nr_pretest_ / 100.0);
73 while (iterations_ < k && skipped_count < max_skip)
76 sac_model_->getSamples (iterations_, selection);
78 if (selection.empty ())
break;
81 if (!sac_model_->computeModelCoefficients (selection, model_coefficients))
90 this->getRandomSamples (sac_model_->getIndices (), fraction_nr_points, indices_subset);
92 if (!sac_model_->doSamplesVerifyModel (indices_subset, model_coefficients, threshold_))
102 double d_cur_penalty = 0;
104 sac_model_->getDistancesToModel (model_coefficients, distances);
106 if (distances.empty () && k > 1.0)
109 for (
size_t i = 0; i < distances.size (); ++i)
110 d_cur_penalty += (std::min) (distances[i], threshold_);
113 if (d_cur_penalty < d_best_penalty)
115 d_best_penalty = d_cur_penalty;
119 model_coefficients_ = model_coefficients;
123 for (
size_t i = 0; i < distances.size (); ++i)
124 if (distances[i] <= threshold_)
128 double w =
static_cast<double> (n_inliers_count) / static_cast<double>(sac_model_->getIndices ()->size ());
129 double p_no_outliers = 1 - pow (w, static_cast<double> (selection.size ()));
130 p_no_outliers = (std::max) (std::numeric_limits<double>::epsilon (), p_no_outliers);
131 p_no_outliers = (std::min) (1 - std::numeric_limits<double>::epsilon (), p_no_outliers);
132 k = log (1 - probability_) / log (p_no_outliers);
136 if (debug_verbosity_level > 1)
137 PCL_DEBUG (
"[pcl::RandomizedMEstimatorSampleConsensus::computeModel] Trial %d out of %d. Best penalty is %f.\n", iterations_, static_cast<int> (ceil (k)), d_best_penalty);
138 if (iterations_ > max_iterations_)
140 if (debug_verbosity_level > 0)
141 PCL_DEBUG (
"[pcl::RandomizedMEstimatorSampleConsensus::computeModel] MSAC reached the maximum number of trials.\n");
148 if (debug_verbosity_level > 0)
149 PCL_DEBUG (
"[pcl::RandomizedMEstimatorSampleConsensus::computeModel] Unable to find a solution!\n");
154 sac_model_->getDistancesToModel (model_coefficients_, distances);
155 std::vector<int> &indices = *sac_model_->getIndices ();
156 if (distances.size () != indices.size ())
158 PCL_ERROR (
"[pcl::RandomizedMEstimatorSampleConsensus::computeModel] Estimated distances (%zu) differs than the normal of indices (%zu).\n", distances.size (), indices.size ());
162 inliers_.resize (distances.size ());
165 for (
size_t i = 0; i < distances.size (); ++i)
166 if (distances[i] <= threshold_)
167 inliers_[n_inliers_count++] = indices[i];
170 inliers_.resize (n_inliers_count);
172 if (debug_verbosity_level > 0)
173 PCL_DEBUG (
"[pcl::RandomizedMEstimatorSampleConsensus::computeModel] Model: %zu size, %d inliers.\n", model_.size (), n_inliers_count);
178 #define PCL_INSTANTIATE_RandomizedMEstimatorSampleConsensus(T) template class PCL_EXPORTS pcl::RandomizedMEstimatorSampleConsensus<T>;
180 #endif // PCL_SAMPLE_CONSENSUS_IMPL_RMSAC_H_