30 const CMatrixDouble& allData,
const std::vector<size_t>& useIndices,
31 vector<CMatrixDouble>& fitModels)
33 ASSERT_(useIndices.size() == 3);
36 allData(0, useIndices[0]), allData(1, useIndices[0]),
37 allData(2, useIndices[0]));
39 allData(0, useIndices[1]), allData(1, useIndices[1]),
40 allData(2, useIndices[1]));
42 allData(0, useIndices[2]), allData(1, useIndices[2]),
43 allData(2, useIndices[2]));
52 for (
size_t i = 0; i < 4; i++) M(0, i) = plane.coefs[i];
62 const CMatrixDouble& allData,
const vector<CMatrixDouble>& testModels,
63 const double distanceThreshold,
unsigned int& out_bestModelIndex,
64 std::vector<size_t>& out_inlierIndices)
66 ASSERT_(testModels.size() == 1);
67 out_bestModelIndex = 0;
70 ASSERT_(M.rows() == 1 && M.cols() == 4);
73 plane.
coefs[0] = M(0, 0);
74 plane.coefs[1] = M(0, 1);
75 plane.coefs[2] = M(0, 2);
76 plane.coefs[3] = M(0, 3);
78 const size_t N = allData.cols();
79 out_inlierIndices.clear();
80 out_inlierIndices.reserve(100);
81 for (
size_t i = 0; i < N; i++)
83 const double d = plane.distance(
85 allData.get_unsafe(0, i), allData.get_unsafe(1, i),
86 allData.get_unsafe(2, i)));
87 if (d < distanceThreshold) out_inlierIndices.push_back(i);
94 const CMatrixDouble& allData,
const std::vector<size_t>& useIndices)
108 const size_t N_plane = 300;
109 const size_t N_noise = 100;
111 const double PLANE_EQ[4] = {1, -1, 1, -2};
114 for (
size_t i = 0; i < N_plane; i++)
119 -(PLANE_EQ[3] + PLANE_EQ[0] * xx + PLANE_EQ[1] * yy) / PLANE_EQ[2];
125 for (
size_t i = 0; i < N_noise; i++)
135 std::vector<size_t> best_inliers;
136 const double DIST_THRESHOLD = 0.2;
139 const size_t TIMES = 100;
142 for (
size_t iters = 0; iters < TIMES; iters++)
147 best_inliers, best_model,
152 cout <<
"Computation time: " << tictac.
Tac() * 1000.0 / TIMES <<
" ms" 155 ASSERT_(best_model.rows() == 1 && best_model.cols() == 4);
157 cout <<
"RANSAC finished: Best model: " << best_model << endl;
161 best_model(0, 0), best_model(0, 1), best_model(0, 2), best_model(0, 3));
167 mrpt::make_aligned_shared<opengl::COpenGLScene>();
170 mrpt::make_aligned_shared<opengl::CGridPlaneXY>(
171 -20, 20, -20, 20, 0, 1));
175 mrpt::make_aligned_shared<opengl::CPointCloud>();
176 points->setColor(0, 0, 1);
178 points->enableColorFromZ();
181 std::vector<float> xs, ys, zs;
183 data.extractRow(0, xs);
184 data.extractRow(1, ys);
185 data.extractRow(2, zs);
186 points->setAllPointsFast(xs, ys, zs);
192 mrpt::make_aligned_shared<opengl::CTexturedPlane>(-4, 4, -4, 4);
195 plane.getAsPose3D(glPlanePose);
196 glPlane->setPose(glPlanePose);
198 scene->insert(glPlane);
200 win.get3DSceneAndLock() = scene;
201 win.unlockAccess3DScene();
217 catch (std::exception& e)
219 std::cout <<
"MRPT exception caught: " << e.what() << std::endl;
224 printf(
"Untyped exception!!");
A namespace of pseudo-random numbers generators of diferent distributions.
double Tac() noexcept
Stops the stopwatch.
double drawUniform(const double Min, const double Max)
Generate a uniformly distributed pseudo-random number using the MT19937 algorithm, scaled to the selected range.
void randomize(const uint32_t seed)
Initialize the PRNG from the given random seed.
A high-performance stopwatch, with typical resolution of nanoseconds.
GLsizei const GLfloat * points
#define ASSERT_(f)
Defines an assertion mechanism.
This base provides a set of functions for maths stuff.
void ransac3Dplane_fit(const CMatrixDouble &allData, const std::vector< size_t > &useIndices, vector< CMatrixDouble > &fitModels)
3D Plane, represented by its equation
mrpt::gui::CDisplayWindow3D::Ptr win
Classes for 2D/3D geometry representation, both of single values and probability density distribution...
bool execute(const CMatrixTemplateNumeric< NUMTYPE > &data, const TRansacFitFunctor &fit_func, const TRansacDistanceFunctor &dist_func, const TRansacDegenerateFunctor °en_func, const double distanceThreshold, const unsigned int minimumSizeSamplesToFit, std::vector< size_t > &out_best_inliers, CMatrixTemplateNumeric< NUMTYPE > &out_best_model, const double prob_good_sample=0.999, const size_t maxIter=2000) const
An implementation of the RANSAC algorithm for robust fitting of models to data.
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
CSetOfObjects::Ptr CornerXYZ(float scale=1.0)
Returns three arrows representing a X,Y,Z 3D corner.
double coefs[4]
Plane coefficients, stored as an array: .
Lightweight 3D pose (three spatial coordinates, plus three angular coordinates).
Classes for creating GUI windows for 2D and 3D visualization.
void ransac3Dplane_distance(const CMatrixTemplateNumeric< T > &allData, const vector< CMatrixTemplateNumeric< T >> &testModels, const T distanceThreshold, unsigned int &out_bestModelIndex, std::vector< size_t > &out_inlierIndices)
bool ransac3Dplane_degenerate(const CMatrixTemplateNumeric< T > &allData, const std::vector< size_t > &useIndices)
Return "true" if the selected points are a degenerate (invalid) case.
A generic RANSAC implementation with models as matrices.
CRandomGenerator & getRandomGenerator()
A static instance of a CRandomGenerator class, for use in single-thread applications.
GLsizei GLsizei GLenum GLenum const GLvoid * data
A graphical user interface (GUI) for efficiently rendering 3D scenes in real-time.