1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
|
/* +------------------------------------------------------------------------+
| Mobile Robot Programming Toolkit (MRPT) |
| https://www.mrpt.org/ |
| |
| Copyright (c) 2005-2023, Individual contributors, see AUTHORS file |
| See: https://www.mrpt.org/Authors - All rights reserved. |
| Released under BSD License. See: https://www.mrpt.org/License |
+------------------------------------------------------------------------+ */
#include <mrpt/gui/CDisplayWindow3D.h>
#include <mrpt/math/ransac.h>
#include <mrpt/opengl/CGridPlaneXY.h>
#include <mrpt/opengl/CPointCloud.h>
#include <mrpt/opengl/CTexturedPlane.h>
#include <mrpt/opengl/stock_objects.h>
#include <mrpt/poses/CPose3D.h>
#include <mrpt/random.h>
#include <mrpt/system/CTicTac.h>
#include <iostream>
using namespace mrpt;
using namespace mrpt::gui;
using namespace mrpt::math;
using namespace mrpt::random;
using namespace mrpt::poses;
using namespace mrpt::system;
using namespace std;
void ransac3Dplane_fit(
const CMatrixDouble& allData, const std::vector<size_t>& useIndices,
vector<CMatrixDouble>& fitModels)
{
ASSERT_(useIndices.size() == 3);
TPoint3D p1(
allData(0, useIndices[0]), allData(1, useIndices[0]),
allData(2, useIndices[0]));
TPoint3D p2(
allData(0, useIndices[1]), allData(1, useIndices[1]),
allData(2, useIndices[1]));
TPoint3D p3(
allData(0, useIndices[2]), allData(1, useIndices[2]),
allData(2, useIndices[2]));
try
{
TPlane plane(p1, p2, p3);
fitModels.resize(1);
CMatrixDouble& M = fitModels[0];
M.setSize(1, 4);
for (size_t i = 0; i < 4; i++)
M(0, i) = plane.coefs[i];
}
catch (exception&)
{
fitModels.clear();
return;
}
}
void ransac3Dplane_distance(
const CMatrixDouble& allData, const vector<CMatrixDouble>& testModels,
const double distanceThreshold, unsigned int& out_bestModelIndex,
std::vector<size_t>& out_inlierIndices)
{
ASSERT_(testModels.size() == 1);
out_bestModelIndex = 0;
const CMatrixDouble& M = testModels[0];
ASSERT_(M.rows() == 1 && M.cols() == 4);
TPlane plane;
plane.coefs[0] = M(0, 0);
plane.coefs[1] = M(0, 1);
plane.coefs[2] = M(0, 2);
plane.coefs[3] = M(0, 3);
const size_t N = allData.cols();
out_inlierIndices.clear();
out_inlierIndices.reserve(100);
for (size_t i = 0; i < N; i++)
{
const double d = plane.distance(
TPoint3D(allData(0, i), allData(1, i), allData(2, i)));
if (d < distanceThreshold) out_inlierIndices.push_back(i);
}
}
/** Return "true" if the selected points are a degenerate (invalid) case.
*/
bool ransac3Dplane_degenerate(
const CMatrixDouble& allData, const std::vector<size_t>& useIndices)
{
return false;
}
// ------------------------------------------------------
// TestRANSAC
// ------------------------------------------------------
void TestRANSAC()
{
getRandomGenerator().randomize();
// Generate random points:
// ------------------------------------
const size_t N_plane = 300;
const size_t N_noise = 100;
const double PLANE_EQ[4] = {1, -1, 1, -2};
CMatrixDouble data(3, N_plane + N_noise);
for (size_t i = 0; i < N_plane; i++)
{
const double xx = getRandomGenerator().drawUniform(-3, 3);
const double yy = getRandomGenerator().drawUniform(-3, 3);
const double zz =
-(PLANE_EQ[3] + PLANE_EQ[0] * xx + PLANE_EQ[1] * yy) / PLANE_EQ[2];
data(0, i) = xx;
data(1, i) = yy;
data(2, i) = zz;
}
for (size_t i = 0; i < N_noise; i++)
{
data(0, i + N_plane) = getRandomGenerator().drawUniform(-4, 4);
data(1, i + N_plane) = getRandomGenerator().drawUniform(-4, 4);
data(2, i + N_plane) = getRandomGenerator().drawUniform(-4, 4);
}
// Run RANSAC
// ------------------------------------
CMatrixDouble best_model;
std::vector<size_t> best_inliers;
const double DIST_THRESHOLD = 0.05;
CTicTac tictac;
const size_t TIMES = 100;
math::RANSAC myransac;
for (size_t iters = 0; iters < TIMES; iters++)
{
myransac.setVerbosityLevel(
iters == 0 ? mrpt::system::LVL_DEBUG : mrpt::system::LVL_INFO);
myransac.execute(
data, ransac3Dplane_fit, ransac3Dplane_distance,
ransac3Dplane_degenerate, DIST_THRESHOLD,
3, // Minimum set of points
best_inliers, best_model);
}
cout << "Computation time: " << tictac.Tac() * 1000.0 / TIMES << " ms"
<< endl;
ASSERT_(best_model.rows() == 1 && best_model.cols() == 4);
cout << "RANSAC finished: Best model: " << best_model << endl;
// cout << "Best inliers: " << best_inliers << endl;
TPlane plane(
best_model(0, 0), best_model(0, 1), best_model(0, 2), best_model(0, 3));
// Show GUI
// --------------------------
mrpt::gui::CDisplayWindow3D win("Set of points", 500, 500);
opengl::COpenGLScene::Ptr scene = opengl::COpenGLScene::Create();
scene->insert(opengl::CGridPlaneXY::Create(-20, 20, -20, 20, 0, 1));
scene->insert(opengl::stock_objects::CornerXYZ());
opengl::CPointCloud::Ptr points = opengl::CPointCloud::Create();
points->setColor(0, 0, 1);
points->setPointSize(3);
points->enableColorFromZ();
{
std::vector<double> xs, ys, zs;
data.extractRow(0, xs);
data.extractRow(1, ys);
data.extractRow(2, zs);
points->setAllPoints(xs, ys, zs);
}
scene->insert(points);
opengl::CTexturedPlane::Ptr glPlane =
opengl::CTexturedPlane::Create(-4, 4, -4, 4);
glPlane->setColor_u8(mrpt::img::TColor(0xff, 0x00, 0x00, 0x80)); // RGBA
TPose3D glPlanePose;
plane.getAsPose3D(glPlanePose);
glPlane->setPose(glPlanePose);
scene->insert(glPlane);
win.get3DSceneAndLock() = scene;
win.unlockAccess3DScene();
win.forceRepaint();
win.waitForKey();
}
// ------------------------------------------------------
// MAIN
// ------------------------------------------------------
int main()
{
try
{
TestRANSAC();
return 0;
}
catch (const std::exception& e)
{
std::cerr << "MRPT error: " << mrpt::exception_to_str(e) << std::endl;
return -1;
}
}
|