23 template <
typename NUMTYPE>
28 const unsigned int minimumSizeSamplesToFit,
29 std::vector<size_t>& out_best_inliers,
31 const size_t maxIter)
const 35 ASSERT_(minimumSizeSamplesToFit >= 1);
38 const size_t D =
data.rows();
39 const size_t Npts =
data.cols();
44 const size_t maxDataTrials =
49 out_best_inliers.clear();
51 size_t trialcount = 0;
52 size_t bestscore = std::string::npos;
55 std::vector<size_t> ind(minimumSizeSamplesToFit);
57 while (N > trialcount)
62 bool degenerate =
true;
64 std::vector<CMatrixDynamic<NUMTYPE>> MODELS;
69 ind.resize(minimumSizeSamplesToFit);
74 ind, 0.0, Npts - 1 + 0.999999);
77 degenerate = degen_func(
data, ind);
83 fit_func(
data, ind, MODELS);
89 degenerate = MODELS.empty();
93 if (++count > maxDataTrials)
106 unsigned int bestModelIdx = 1000;
107 std::vector<size_t> inliers;
111 data, MODELS, NUMTYPE(distanceThreshold), bestModelIdx,
113 ASSERT_(bestModelIdx < MODELS.size());
117 const size_t ninliers = inliers.size();
118 bool update_estim_num_iters =
123 if (ninliers > bestscore ||
124 (bestscore == std::string::npos && ninliers != 0))
126 bestscore = ninliers;
128 out_best_model = MODELS[bestModelIdx];
129 out_best_inliers = inliers;
130 update_estim_num_iters =
true;
133 if (update_estim_num_iters)
137 double fracinliers = ninliers /
static_cast<double>(Npts);
140 pow(fracinliers, static_cast<double>(minimumSizeSamplesToFit));
142 pNoOutliers = std::max(
143 std::numeric_limits<double>::epsilon(),
145 pNoOutliers = std::min(
146 1.0 - std::numeric_limits<double>::epsilon(),
149 N =
static_cast<size_t>(log(1 - p) / log(pNoOutliers));
151 "Iter #%u Estimated number of iters: %u pNoOutliers = %f " 153 (
unsigned)trialcount, (
unsigned)N, pNoOutliers,
154 (
unsigned)ninliers));
160 "trial %u out of %u \r", (
unsigned int)trialcount,
161 (
unsigned int)ceil(static_cast<double>(N))));
164 if (trialcount > maxIter)
167 "Warning: maximum number of trials (%u) reached\n",
173 if (out_best_model.
rows() > 0)
176 format(
"Finished in %u iterations.\n", (
unsigned)trialcount));
A namespace of pseudo-random numbers generators of diferent distributions.
std::function< bool(const CMatrixDynamic< NUMTYPE > &allData, const std::vector< size_t > &useIndices)> TRansacDegenerateFunctor
The type of the function passed to mrpt::math::ransac - See the documentation for that method for mor...
#define MRPT_LOG_DEBUG(_STRING)
Use: MRPT_LOG_DEBUG("message");
std::string std::string format(std::string_view fmt, ARGS &&... args)
#define ASSERT_(f)
Defines an assertion mechanism.
This base provides a set of functions for maths stuff.
size_type rows() const
Number of rows in the matrix.
std::function< void(const CMatrixDynamic< NUMTYPE > &allData, const std::vector< size_t > &useIndices, std::vector< CMatrixDynamic< NUMTYPE > > &fitModels)> TRansacFitFunctor
The type of the function passed to mrpt::math::ransac - See the documentation for that method for mor...
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
void setSize(size_t row, size_t col, bool zeroNewElements=false)
Changes the size of matrix, maintaining the previous contents.
#define MRPT_LOG_WARN(_STRING)
A generic RANSAC implementation with models as matrices.
CRandomGenerator & getRandomGenerator()
A static instance of a CRandomGenerator class, for use in single-thread applications.
void drawUniformVector(VEC &v, const double unif_min=0, const double unif_max=1)
Fills the given vector with independent, uniformly distributed samples.
#define MRPT_LOG_INFO(_STRING)
static struct FontData data
std::function< void(const CMatrixDynamic< NUMTYPE > &allData, const std::vector< CMatrixDynamic< NUMTYPE > > &testModels, const NUMTYPE distanceThreshold, unsigned int &out_bestModelIndex, std::vector< size_t > &out_inlierIndices)> TRansacDistanceFunctor
The type of the function passed to mrpt::math::ransac - See the documentation for that method for mor...