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++)
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);
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!!");