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
|
/* +------------------------------------------------------------------------+
| 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/maps/CSimplePointsMap.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;
// Define as needed for your application:
using MyRansac = mrpt::math::RANSAC_Template<
float, // Numeric data type: float since pointclouds use float
mrpt::maps::CPointsMap, // Dataset type (can be an abstract base type)
mrpt::math::TPlane // Model type to be estimated
>;
namespace mrpt::math
{
// Overload for your custom dataset type:
size_t ransacDatasetSize(const mrpt::maps::CPointsMap& dataset)
{
return dataset.size();
}
} // namespace mrpt::math
void ransac3Dplane_fit(
const mrpt::maps::CPointsMap& allData,
const std::vector<size_t>& useIndices,
vector<mrpt::math::TPlane>& fitModels)
{
ASSERT_(useIndices.size() == 3);
TPoint3D pt[3];
for (int i = 0; i < 3; i++)
allData.getPoint(useIndices[i], pt[i]);
try
{
fitModels.resize(1);
fitModels[0] = TPlane(pt[0], pt[1], pt[2]);
}
catch (exception&)
{
// If the three points are degenerated, an exception may be thrown in
// the plane ctor
fitModels.clear();
return;
}
}
void ransac3Dplane_distance(
const mrpt::maps::CPointsMap& allData,
const vector<mrpt::math::TPlane>& testModels,
const double distanceThreshold, unsigned int& out_bestModelIndex,
std::vector<size_t>& out_inlierIndices)
{
ASSERT_(testModels.size() == 1);
out_bestModelIndex = 0;
const mrpt::math::TPlane& plane = testModels[0];
const size_t N = allData.size();
out_inlierIndices.clear();
out_inlierIndices.reserve(100);
for (size_t i = 0; i < N; i++)
{
mrpt::math::TPoint3D pt;
allData.getPoint(i, pt);
const double d = plane.distance(pt);
if (d < distanceThreshold) out_inlierIndices.push_back(i);
}
}
/** Return "true" if the selected points are a degenerate (invalid) case.
*/
bool ransac3Dplane_degenerate(
const mrpt::maps::CPointsMap& allData,
const std::vector<size_t>& useIndices)
{
return false;
}
// ------------------------------------------------------
// TestRANSAC
// ------------------------------------------------------
void TestRANSAC()
{
auto& rng = getRandomGenerator();
rng.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};
mrpt::maps::CSimplePointsMap data;
data.reserve(N_plane + N_noise);
for (size_t i = 0; i < N_plane; i++)
{
const double xx = rng.drawUniform(-3, 3);
const double yy = rng.drawUniform(-3, 3);
const double zz =
-(PLANE_EQ[3] + PLANE_EQ[0] * xx + PLANE_EQ[1] * yy) / PLANE_EQ[2];
data.insertPointFast(xx, yy, zz);
}
for (size_t i = 0; i < N_noise; i++)
{
data.insertPointFast(
rng.drawUniform(-4, 4), rng.drawUniform(-4, 4),
rng.drawUniform(-4, 4));
}
// Run RANSAC
// ------------------------------------
mrpt::math::TPlane best_model;
std::vector<size_t> best_inliers;
const double DIST_THRESHOLD = 0.05;
CTicTac tictac;
MyRansac myransac;
myransac.setVerbosityLevel(mrpt::system::LVL_DEBUG);
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 << " ms\n";
cout << "RANSAC finished: Best model: " << best_model.coefs[0] << " "
<< best_model.coefs[1] << " " << best_model.coefs[2] << " "
<< best_model.coefs[3] << endl;
// 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();
points->loadFromPointsMap(&data);
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;
best_model.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;
}
}
|