41 #ifndef PCL_SAMPLE_CONSENSUS_IMPL_LMEDS_H_
42 #define PCL_SAMPLE_CONSENSUS_IMPL_LMEDS_H_
44 #include <pcl/sample_consensus/lmeds.h>
47 template <
typename Po
intT>
bool
51 if (threshold_ == std::numeric_limits<double>::max())
53 PCL_ERROR (
"[pcl::LeastMedianSquares::computeModel] No threshold set!\n");
58 double d_best_penalty = std::numeric_limits<double>::max();
61 Eigen::VectorXf model_coefficients;
62 std::vector<double> distances;
64 unsigned skipped_count = 0;
66 const unsigned max_skip = max_iterations_ * 10;
69 while ((iterations_ < max_iterations_) && (skipped_count < max_skip))
72 sac_model_->getSamples (iterations_, selection);
74 if (selection.empty ())
80 if (!sac_model_->computeModelCoefficients (selection, model_coefficients))
91 sac_model_->getDistancesToModel (model_coefficients, distances);
94 if (distances.empty ())
101 const auto new_end = (sac_model_->getInputCloud()->is_dense ? distances.end() : std::partition (distances.begin(), distances.end(), [](
double d){return !std::isnan (d);}));
102 const auto nr_valid_dists =
std::distance (distances.begin (), new_end);
105 const std::size_t mid = nr_valid_dists / 2;
106 PCL_DEBUG (
"[pcl::LeastMedianSquares::computeModel] There are %lu valid distances remaining after removing NaN values.\n", nr_valid_dists);
107 if (nr_valid_dists == 0)
115 if ((nr_valid_dists % 2) == 0)
118 std::nth_element (distances.begin (), distances.begin () + (mid - 1), new_end);
119 const double tmp = distances[mid-1];
120 const double tmp2 = *(std::min_element (distances.begin () + mid, new_end));
121 d_cur_penalty = (sqrt (tmp) + sqrt (tmp2)) / 2.0;
122 PCL_DEBUG (
"[pcl::LeastMedianSquares::computeModel] Computing median with two values (%g and %g) because number of distances is even.\n", tmp, distances[mid]);
126 std::nth_element (distances.begin (), distances.begin () + mid, new_end);
127 d_cur_penalty = sqrt (distances[mid]);
128 PCL_DEBUG (
"[pcl::LeastMedianSquares::computeModel] Computing median with one value (%g) because number of distances is odd.\n", distances[mid]);
132 if (d_cur_penalty < d_best_penalty)
134 d_best_penalty = d_cur_penalty;
138 model_coefficients_ = model_coefficients;
142 if (debug_verbosity_level > 1)
144 PCL_DEBUG (
"[pcl::LeastMedianSquares::computeModel] Trial %d out of %d. Best penalty is %f.\n", iterations_, max_iterations_, d_best_penalty);
150 if (debug_verbosity_level > 0)
152 PCL_DEBUG (
"[pcl::LeastMedianSquares::computeModel] Unable to find a solution!\n");
164 sac_model_->getDistancesToModel (model_coefficients_, distances);
166 if (distances.empty ())
168 PCL_ERROR (
"[pcl::LeastMedianSquares::computeModel] The model found failed to verify against the given constraints!\n");
172 Indices &indices = *sac_model_->getIndices ();
174 if (distances.size () != indices.size ())
176 PCL_ERROR (
"[pcl::LeastMedianSquares::computeModel] Estimated distances (%lu) differs than the normal of indices (%lu).\n", distances.size (), indices.size ());
180 inliers_.resize (distances.size ());
182 std::size_t n_inliers_count = 0;
183 for (std::size_t i = 0; i < distances.size (); ++i)
185 if (distances[i] <= threshold_)
187 inliers_[n_inliers_count++] = indices[i];
192 inliers_.resize (n_inliers_count);
194 if (debug_verbosity_level > 0)
196 PCL_DEBUG (
"[pcl::LeastMedianSquares::computeModel] Model: %lu size, %lu inliers.\n", model_.size (), n_inliers_count);
202 #define PCL_INSTANTIATE_LeastMedianSquares(T) template class PCL_EXPORTS pcl::LeastMedianSquares<T>;
float distance(const PointT &p1, const PointT &p2)
IndicesAllocator<> Indices
Type used for indices in PCL.