MRPT  1.9.9
test.cpp
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | https://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2019, Individual contributors, see AUTHORS file |
6  | See: https://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See: https://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 
11 #include <mrpt/math/model_search.h>
12 #include <mrpt/math/ransac.h>
17 #include <mrpt/poses/CPose3D.h>
18 #include <mrpt/random.h>
19 #include <mrpt/system/CTicTac.h>
20 #include <iostream>
21 
22 using namespace mrpt;
23 using namespace mrpt::gui;
24 using namespace mrpt::math;
25 using namespace mrpt::random;
26 using namespace mrpt::poses;
27 using namespace mrpt::system;
28 using namespace std;
29 
30 struct Fit3DPlane
31 {
32  typedef TPlane3D Model;
33  typedef double Real;
34 
35  const std::vector<TPoint3D>& allData;
36 
37  Fit3DPlane(const std::vector<TPoint3D>& _allData) : allData(_allData) {}
38  size_t getSampleCount(void) const { return allData.size(); }
39  bool fitModel(const std::vector<size_t>& useIndices, TPlane3D& model) const
40  {
41  ASSERT_(useIndices.size() == 3);
42  TPoint3D p1(allData[useIndices[0]]);
43  TPoint3D p2(allData[useIndices[1]]);
44  TPoint3D p3(allData[useIndices[2]]);
45 
46  try
47  {
48  model = TPlane(p1, p2, p3);
49  }
50  catch (exception&)
51  {
52  return false;
53  }
54 
55  return true;
56  }
57 
58  double testSample(size_t index, const TPlane3D& model) const
59  {
60  return model.distance(allData[index]);
61  }
62 };
63 
64 // ------------------------------------------------------
65 // TestRANSAC
66 // ------------------------------------------------------
67 void TestRANSAC()
68 {
70 
71  // Generate random points:
72  // ------------------------------------
73  const size_t N_plane = 300;
74  const size_t N_noise = 100;
75 
76  const double PLANE_EQ[4] = {1, -1, 1, -2};
77 
78  std::vector<TPoint3D> data;
79  for (size_t i = 0; i < N_plane; i++)
80  {
81  const double xx = getRandomGenerator().drawUniform(-3, 3);
82  const double yy = getRandomGenerator().drawUniform(-3, 3);
83  const double zz =
84  -(PLANE_EQ[3] + PLANE_EQ[0] * xx + PLANE_EQ[1] * yy) / PLANE_EQ[2];
85  data.push_back(TPoint3D(xx, yy, zz));
86  }
87 
88  for (size_t i = 0; i < N_noise; i++)
89  {
90  TPoint3D& p = data[i];
91  p += TPoint3D(
92  getRandomGenerator().drawUniform(-4, 4),
93  getRandomGenerator().drawUniform(-4, 4),
94  getRandomGenerator().drawUniform(-4, 4));
95  }
96 
97  // Run RANSAC
98  // ------------------------------------
99  TPlane3D best_model;
100  std::vector<size_t> best_inliers;
101  const double DIST_THRESHOLD = 0.2;
102 
103  CTicTac tictac;
104  const size_t TIMES = 100;
105 
106  bool found = false;
107  for (size_t iters = 0; iters < TIMES; iters++)
108  {
109  ModelSearch search;
110  Fit3DPlane fit(data);
111  found = search.geneticSingleModel(
112  fit, 3, DIST_THRESHOLD, 10, 100, best_model, best_inliers);
113  }
114  cout << "Computation time (genetic): " << tictac.Tac() * 1000.0 / TIMES
115  << " ms" << endl;
116 
117  for (size_t iters = 0; iters < TIMES; iters++)
118  {
119  ModelSearch search;
120  Fit3DPlane fit(data);
121  found = search.ransacSingleModel(
122  fit, 3, DIST_THRESHOLD, best_model, best_inliers);
123  }
124  cout << "Computation time (ransac): " << tictac.Tac() * 1000.0 / TIMES
125  << " ms" << endl;
126 
127  if (!found) return;
128 
129  // cout << "RANSAC finished: Best model: " << best_model << endl;
130  // cout << "Best inliers: " << best_inliers << endl;
131 
132  // Show GUI
133  // --------------------------
134  mrpt::gui::CDisplayWindow3D win("Set of points", 500, 500);
136 
137  scene->insert(opengl::CGridPlaneXY::Create(-20, 20, -20, 20, 0, 1));
138  scene->insert(opengl::stock_objects::CornerXYZ());
139 
141  points->setColor(0, 0, 1);
142  points->setPointSize(3);
143  points->enableColorFromZ();
144 
145  {
146  std::vector<float> x, y, z;
147  x.reserve(data.size());
148  y.reserve(data.size());
149  z.reserve(data.size());
150  for (size_t i = 0; i < data.size(); i++)
151  {
152  x.push_back(data[i].x);
153  y.push_back(data[i].y);
154  z.push_back(data[i].z);
155  }
156  points->setAllPointsFast(x, y, z);
157  }
158 
159  scene->insert(points);
160 
162  opengl::CTexturedPlane::Create(-4, 4, -4, 4);
163 
164  TPose3D glPlanePose;
165  best_model.getAsPose3D(glPlanePose);
166  glPlane->setPose(glPlanePose);
167 
168  scene->insert(glPlane);
169 
170  win.get3DSceneAndLock() = scene;
171  win.unlockAccess3DScene();
172  win.forceRepaint();
173 
174  win.waitForKey();
175 }
176 
177 // ------------------------------------------------------
178 // MAIN
179 // ------------------------------------------------------
180 int main()
181 {
182  try
183  {
184  TestRANSAC();
185  return 0;
186  }
187  catch (const std::exception& e)
188  {
189  std::cerr << "MRPT error: " << mrpt::exception_to_str(e) << std::endl;
190  return -1;
191  }
192  catch (...)
193  {
194  printf("Untyped exception!!");
195  return -1;
196  }
197 }
A namespace of pseudo-random numbers generators of diferent distributions.
double Tac() noexcept
Stops the stopwatch.
Definition: CTicTac.cpp:86
double drawUniform(const double Min, const double Max)
Generate a uniformly distributed pseudo-random number using the MT19937 algorithm, scaled to the selected range.
static Ptr Create(Args &&... args)
GLdouble GLdouble z
Definition: glext.h:3879
Model search implementations: RANSAC and genetic algorithm.
Definition: model_search.h:63
void randomize(const uint32_t seed)
Initialize the PRNG from the given random seed.
A high-performance stopwatch, with typical resolution of nanoseconds.
bool ransacSingleModel(const TModelFit &p_state, size_t p_kernelSize, const typename TModelFit::Real &p_fitnessThreshold, typename TModelFit::Model &p_bestModel, std::vector< size_t > &p_inliers)
Run the ransac algorithm searching for a single model.
STL namespace.
GLsizei const GLfloat * points
Definition: glext.h:5414
static Ptr Create(Args &&... args)
Definition: CPointCloud.h:48
#define ASSERT_(f)
Defines an assertion mechanism.
Definition: exceptions.h:120
This base provides a set of functions for maths stuff.
void TestRANSAC()
GLuint index
Definition: glext.h:4068
_u8 model
Definition: rplidar_cmd.h:19
3D Plane, represented by its equation
Definition: TPlane.h:22
mrpt::gui::CDisplayWindow3D::Ptr win
Classes for 2D/3D geometry representation, both of single values and probability density distribution...
bool geneticSingleModel(const TModelFit &p_state, size_t p_kernelSize, const typename TModelFit::Real &p_fitnessThreshold, size_t p_populationSize, size_t p_maxIteration, typename TModelFit::Model &p_bestModel, std::vector< size_t > &p_inliers)
Run a generic programming version of ransac searching for a single model.
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.
Lightweight 3D pose (three spatial coordinates, plus three angular coordinates).
Definition: TPose3D.h:23
void getAsPose3D(mrpt::math::TPose3D &outPose) const
std::string exception_to_str(const std::exception &e)
Builds a nice textual representation of a nested exception, which if generated using MRPT macros (THR...
Definition: exceptions.cpp:59
GLenum GLint GLint y
Definition: glext.h:3542
static Ptr Create(Args &&... args)
Definition: COpenGLScene.h:60
Classes for creating GUI windows for 2D and 3D visualization.
Definition: about_box.h:14
GLenum GLint x
Definition: glext.h:3542
Lightweight 3D point.
Definition: TPoint3D.h:90
CRandomGenerator & getRandomGenerator()
A static instance of a CRandomGenerator class, for use in single-thread applications.
GLsizei GLsizei GLenum GLenum const GLvoid * data
Definition: glext.h:3550
GLfloat GLfloat p
Definition: glext.h:6398
static Ptr Create(Args &&... args)
Definition: CGridPlaneXY.h:31
A graphical user interface (GUI) for efficiently rendering 3D scenes in real-time.



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: 8fe78517f Sun Jul 14 19:43:28 2019 +0200 at lun oct 28 02:10:00 CET 2019