41 #ifndef PCL_SAMPLE_CONSENSUS_IMPL_LMEDS_H_
42 #define PCL_SAMPLE_CONSENSUS_IMPL_LMEDS_H_
44 #include <pcl/sample_consensus/lmeds.h>
48 template <
typename Po
intT>
bool
52 if (threshold_ == std::numeric_limits<double>::max())
54 PCL_ERROR (
"[pcl::LeastMedianSquares::computeModel] No threshold set!\n");
59 double d_best_penalty = std::numeric_limits<double>::max();
62 Eigen::VectorXf model_coefficients (sac_model_->getModelSize ());
63 std::vector<double> distances;
65 unsigned skipped_count = 0;
67 const unsigned max_skip = max_iterations_ * 10;
70 while ((iterations_ < max_iterations_) && (skipped_count < max_skip))
73 sac_model_->getSamples (iterations_, selection);
75 if (selection.empty ())
77 PCL_ERROR (
"[pcl::LeastMedianSquares::computeModel] No samples could be selected!\n");
82 if (!sac_model_->computeModelCoefficients (selection, model_coefficients))
86 PCL_DEBUG (
"[pcl::LeastMedianSquares::computeModel] The function computeModelCoefficients failed, so continue with next iteration.\n");
94 sac_model_->getDistancesToModel (model_coefficients, distances);
97 if (distances.empty ())
104 const auto new_end = (sac_model_->getInputCloud()->is_dense ? distances.end() : std::partition (distances.begin(), distances.end(), [](
double d){return !std::isnan (d);}));
105 const auto nr_valid_dists =
std::distance (distances.begin (), new_end);
108 PCL_DEBUG (
"[pcl::LeastMedianSquares::computeModel] There are %lu valid distances remaining after removing NaN values.\n", nr_valid_dists);
109 if (nr_valid_dists == 0)
115 d_cur_penalty =
pcl::computeMedian (distances.begin (), new_end,
static_cast<double(*)(
double)
>(std::sqrt));
118 if (d_cur_penalty < d_best_penalty)
120 d_best_penalty = d_cur_penalty;
124 model_coefficients_ = model_coefficients;
128 if (debug_verbosity_level > 1)
130 PCL_DEBUG (
"[pcl::LeastMedianSquares::computeModel] Trial %d out of %d. Best penalty is %f.\n", iterations_, max_iterations_, d_best_penalty);
136 if (debug_verbosity_level > 0)
138 PCL_DEBUG (
"[pcl::LeastMedianSquares::computeModel] Unable to find a solution!\n");
150 sac_model_->getDistancesToModel (model_coefficients_, distances);
152 if (distances.empty ())
154 PCL_ERROR (
"[pcl::LeastMedianSquares::computeModel] The model found failed to verify against the given constraints!\n");
158 Indices &indices = *sac_model_->getIndices ();
160 if (distances.size () != indices.size ())
162 PCL_ERROR (
"[pcl::LeastMedianSquares::computeModel] Estimated distances (%lu) differs than the normal of indices (%lu).\n", distances.size (), indices.size ());
166 inliers_.resize (distances.size ());
168 std::size_t n_inliers_count = 0;
169 for (std::size_t i = 0; i < distances.size (); ++i)
171 if (distances[i] <= threshold_)
173 inliers_[n_inliers_count++] = indices[i];
178 inliers_.resize (n_inliers_count);
180 if (debug_verbosity_level > 0)
182 PCL_DEBUG (
"[pcl::LeastMedianSquares::computeModel] Model: %lu size, %lu inliers.\n", model_.size (), n_inliers_count);
188 #define PCL_INSTANTIATE_LeastMedianSquares(T) template class PCL_EXPORTS pcl::LeastMedianSquares<T>;
Define standard C methods and C++ classes that are common to all methods.
auto computeMedian(IteratorT begin, IteratorT end, Functor f) noexcept -> std::result_of_t< Functor(decltype(*begin))>
Compute the median of a list of values (fast).
float distance(const PointT &p1, const PointT &p2)
IndicesAllocator<> Indices
Type used for indices in PCL.