Main MRPT website > C++ reference for MRPT 1.9.9
test.cpp
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2018, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 
10 #include <mrpt/math/ransac.h>
12 #include <mrpt/random.h>
13 #include <mrpt/system/CTicTac.h>
14 #include <mrpt/poses/CPose3D.h>
19 #include <iostream>
20 
21 using namespace mrpt;
22 using namespace mrpt::gui;
23 using namespace mrpt::math;
24 using namespace mrpt::random;
25 using namespace mrpt::poses;
26 using namespace mrpt::system;
27 using namespace std;
28 
30  const CMatrixDouble& allData, const std::vector<size_t>& useIndices,
31  vector<CMatrixDouble>& fitModels)
32 {
33  ASSERT_(useIndices.size() == 3);
34 
35  TPoint3D p1(
36  allData(0, useIndices[0]), allData(1, useIndices[0]),
37  allData(2, useIndices[0]));
38  TPoint3D p2(
39  allData(0, useIndices[1]), allData(1, useIndices[1]),
40  allData(2, useIndices[1]));
41  TPoint3D p3(
42  allData(0, useIndices[2]), allData(1, useIndices[2]),
43  allData(2, useIndices[2]));
44 
45  try
46  {
47  TPlane plane(p1, p2, p3);
48  fitModels.resize(1);
49  CMatrixDouble& M = fitModels[0];
50 
51  M.setSize(1, 4);
52  for (size_t i = 0; i < 4; i++) M(0, i) = plane.coefs[i];
53  }
54  catch (exception&)
55  {
56  fitModels.clear();
57  return;
58  }
59 }
60 
62  const CMatrixDouble& allData, const vector<CMatrixDouble>& testModels,
63  const double distanceThreshold, unsigned int& out_bestModelIndex,
64  std::vector<size_t>& out_inlierIndices)
65 {
66  ASSERT_(testModels.size() == 1);
67  out_bestModelIndex = 0;
68  const CMatrixDouble& M = testModels[0];
69 
70  ASSERT_(M.rows() == 1 && M.cols() == 4);
71 
72  TPlane plane;
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);
77 
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++)
82  {
83  const double d = plane.distance(
84  TPoint3D(
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);
88  }
89 }
90 
91 /** Return "true" if the selected points are a degenerate (invalid) case.
92  */
94  const CMatrixDouble& allData, const std::vector<size_t>& useIndices)
95 {
96  return false;
97 }
98 
99 // ------------------------------------------------------
100 // TestRANSAC
101 // ------------------------------------------------------
102 void TestRANSAC()
103 {
105 
106  // Generate random points:
107  // ------------------------------------
108  const size_t N_plane = 300;
109  const size_t N_noise = 100;
110 
111  const double PLANE_EQ[4] = {1, -1, 1, -2};
112 
113  CMatrixDouble data(3, N_plane + N_noise);
114  for (size_t i = 0; i < N_plane; i++)
115  {
116  const double xx = getRandomGenerator().drawUniform(-3, 3);
117  const double yy = getRandomGenerator().drawUniform(-3, 3);
118  const double zz =
119  -(PLANE_EQ[3] + PLANE_EQ[0] * xx + PLANE_EQ[1] * yy) / PLANE_EQ[2];
120  data(0, i) = xx;
121  data(1, i) = yy;
122  data(2, i) = zz;
123  }
124 
125  for (size_t i = 0; i < N_noise; i++)
126  {
127  data(0, i + N_plane) = getRandomGenerator().drawUniform(-4, 4);
128  data(1, i + N_plane) = getRandomGenerator().drawUniform(-4, 4);
129  data(2, i + N_plane) = getRandomGenerator().drawUniform(-4, 4);
130  }
131 
132  // Run RANSAC
133  // ------------------------------------
134  CMatrixDouble best_model;
135  std::vector<size_t> best_inliers;
136  const double DIST_THRESHOLD = 0.2;
137 
138  CTicTac tictac;
139  const size_t TIMES = 100;
140 
141  math::RANSAC myransac;
142  for (size_t iters = 0; iters < TIMES; iters++)
143  myransac.execute(
145  ransac3Dplane_degenerate, DIST_THRESHOLD,
146  3, // Minimum set of points
147  best_inliers, best_model,
148  iters == 0 ? mrpt::system::LVL_DEBUG
149  : mrpt::system::LVL_INFO // Verbose
150  );
151 
152  cout << "Computation time: " << tictac.Tac() * 1000.0 / TIMES << " ms"
153  << endl;
154 
155  ASSERT_(best_model.rows() == 1 && best_model.cols() == 4);
156 
157  cout << "RANSAC finished: Best model: " << best_model << endl;
158  // cout << "Best inliers: " << best_inliers << endl;
159 
160  TPlane plane(
161  best_model(0, 0), best_model(0, 1), best_model(0, 2), best_model(0, 3));
162 
163  // Show GUI
164  // --------------------------
165  mrpt::gui::CDisplayWindow3D win("Set of points", 500, 500);
167  mrpt::make_aligned_shared<opengl::COpenGLScene>();
168 
169  scene->insert(
170  mrpt::make_aligned_shared<opengl::CGridPlaneXY>(
171  -20, 20, -20, 20, 0, 1));
172  scene->insert(opengl::stock_objects::CornerXYZ());
173 
175  mrpt::make_aligned_shared<opengl::CPointCloud>();
176  points->setColor(0, 0, 1);
177  points->setPointSize(3);
178  points->enableColorFromZ();
179 
180  {
181  std::vector<float> xs, ys, zs;
182 
183  data.extractRow(0, xs);
184  data.extractRow(1, ys);
185  data.extractRow(2, zs);
186  points->setAllPointsFast(xs, ys, zs);
187  }
188 
189  scene->insert(points);
190 
192  mrpt::make_aligned_shared<opengl::CTexturedPlane>(-4, 4, -4, 4);
193 
194  TPose3D glPlanePose;
195  plane.getAsPose3D(glPlanePose);
196  glPlane->setPose(glPlanePose);
197 
198  scene->insert(glPlane);
199 
200  win.get3DSceneAndLock() = scene;
201  win.unlockAccess3DScene();
202  win.forceRepaint();
203 
204  win.waitForKey();
205 }
206 
207 // ------------------------------------------------------
208 // MAIN
209 // ------------------------------------------------------
210 int main()
211 {
212  try
213  {
214  TestRANSAC();
215  return 0;
216  }
217  catch (std::exception& e)
218  {
219  std::cout << "MRPT exception caught: " << e.what() << std::endl;
220  return -1;
221  }
222  catch (...)
223  {
224  printf("Untyped exception!!");
225  return -1;
226  }
227 }
mrpt::math::TPlane::distance
double distance(const TPoint3D &point) const
Distance to 3D point.
Definition: lightweight_geom_data.cpp:831
mrpt::system::CTicTac
A high-performance stopwatch, with typical resolution of nanoseconds.
Definition: system/CTicTac.h:19
mrpt::opengl::CTexturedPlane::Ptr
std::shared_ptr< CTexturedPlane > Ptr
Definition: CTexturedPlane.h:24
points
GLsizei const GLfloat * points
Definition: glext.h:5339
mrpt
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
Definition: CKalmanFilterCapable.h:30
mrpt::math::ransac3Dplane_distance
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)
Definition: ransac_applications.cpp:60
mrpt::system::LVL_INFO
@ LVL_INFO
Definition: system/COutputLogger.h:31
ASSERT_
#define ASSERT_(f)
Defines an assertion mechanism.
Definition: exceptions.h:113
stock_objects.h
CPointCloud.h
mrpt::poses
Classes for 2D/3D geometry representation, both of single values and probability density distribution...
Definition: CHierarchicalMapMHPartition.h:25
mrpt::opengl::CPointCloud::Ptr
std::shared_ptr< CPointCloud > Ptr
Definition: CPointCloud.h:50
CDisplayWindow3D.h
random.h
mrpt::random::CRandomGenerator::randomize
void randomize(const uint32_t seed)
Initialize the PRNG from the given random seed.
Definition: RandomGenerator.cpp:32
mrpt::system::CTicTac::Tac
double Tac() noexcept
Stops the stopwatch.
Definition: CTicTac.cpp:90
mrpt::math::CMatrixTemplateNumeric< double >
main
int main()
Definition: vision_stereo_rectify/test.cpp:78
mrpt::math::ransac3Dplane_degenerate
bool ransac3Dplane_degenerate(const CMatrixTemplateNumeric< T > &allData, const std::vector< size_t > &useIndices)
Return "true" if the selected points are a degenerate (invalid) case.
Definition: ransac_applications.cpp:94
TestRANSAC
void TestRANSAC()
Definition: vision_stereo_rectify/test.cpp:70
ransac3Dplane_fit
void ransac3Dplane_fit(const CMatrixDouble &allData, const std::vector< size_t > &useIndices, vector< CMatrixDouble > &fitModels)
Definition: vision_stereo_rectify/test.cpp:29
data
GLsizei GLsizei GLenum GLenum const GLvoid * data
Definition: glext.h:3547
mrpt::random::CRandomGenerator::drawUniform
double drawUniform(const double Min, const double Max)
Generate a uniformly distributed pseudo-random number using the MT19937 algorithm,...
Definition: RandomGenerators.h:111
win
mrpt::gui::CDisplayWindow3D::Ptr win
Definition: vision_stereo_rectify/test.cpp:31
mrpt::math::TPlane::getAsPose3D
void getAsPose3D(mrpt::math::TPose3D &outPose)
Definition: lightweight_geom_data.cpp:855
mrpt::math::TPose3D
Lightweight 3D pose (three spatial coordinates, plus three angular coordinates).
Definition: lightweight_geom_data.h:603
mrpt::gui
Classes for creating GUI windows for 2D and 3D visualization.
Definition: about_box.h:16
mrpt::math::RANSAC_Template::execute
bool execute(const CMatrixTemplateNumeric< NUMTYPE > &data, const TRansacFitFunctor &fit_func, const TRansacDistanceFunctor &dist_func, const TRansacDegenerateFunctor &degen_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.
Definition: ransac.cpp:24
mrpt::math::TPoint3D
Lightweight 3D point.
Definition: lightweight_geom_data.h:378
CPose3D.h
mrpt::math::TPlane
3D Plane, represented by its equation
Definition: lightweight_geom_data.h:1309
mrpt::system::LVL_DEBUG
@ LVL_DEBUG
Definition: system/COutputLogger.h:30
mrpt::opengl::stock_objects::CornerXYZ
CSetOfObjects::Ptr CornerXYZ(float scale=1.0)
Returns three arrows representing a X,Y,Z 3D corner.
Definition: StockObjects.cpp:209
mrpt::random::getRandomGenerator
CRandomGenerator & getRandomGenerator()
A static instance of a CRandomGenerator class, for use in single-thread applications.
Definition: RandomGenerator.cpp:19
CTicTac.h
mrpt::math
This base provides a set of functions for maths stuff.
Definition: math/include/mrpt/math/bits_math.h:13
mrpt::math::RANSAC_Template
A generic RANSAC implementation with models as matrices.
Definition: ransac.h:30
mrpt::opengl::COpenGLScene::Ptr
std::shared_ptr< COpenGLScene > Ptr
Definition: COpenGLScene.h:61
mrpt::random
A namespace of pseudo-random numbers generators of diferent distributions.
Definition: random_shuffle.h:17
CGridPlaneXY.h
mrpt::gui::CDisplayWindow3D
A graphical user interface (GUI) for efficiently rendering 3D scenes in real-time.
Definition: CDisplayWindow3D.h:117
mrpt::math::TPlane::coefs
double coefs[4]
Plane coefficients, stored as an array: .
Definition: lightweight_geom_data.h:1315
CTexturedPlane.h
mrpt::system
This namespace provides a OS-independent interface to many useful functions: filenames manipulation,...
Definition: math_frwds.h:25
ransac.h



Page generated by Doxygen 1.8.17 for MRPT 1.9.9 Git: ad3a9d8ae Tue May 1 23:10:22 2018 -0700 at miƩ 12 jul 2023 10:03:34 CEST