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 
18 #include <mrpt/poses/CPose3D.h>
19 #include <mrpt/random.h>
20 #include <mrpt/system/CTicTac.h>
21 #include <iostream>
22 
23 using namespace mrpt;
24 using namespace mrpt::gui;
25 using namespace mrpt::math;
26 using namespace mrpt::random;
27 using namespace mrpt::poses;
28 using namespace mrpt::system;
29 using namespace std;
30 
32 
33 // ------------------------------------------------------
34 // TestRANSACPlanes
35 // ------------------------------------------------------
36 void TestRANSACPlanes()
37 {
39 
40  // Generate random points:
41  // ------------------------------------
42  const size_t N_PLANES = 3;
43 
44  const size_t N_plane = 300;
45  const size_t N_noise = 300;
46 
47  const double PLANE_EQ[N_PLANES][4] = {
48  {1, -1, 1, -2}, {1, +1.5, 1, -1}, {0, -1, 1, +2}};
49 
50  CVectorDouble xs, ys, zs;
51  for (size_t p = 0; p < N_PLANES; p++)
52  {
53  for (size_t i = 0; i < N_plane; i++)
54  {
55  const double xx =
56  getRandomGenerator().drawUniform(-3, 3) + 5 * cos(0.4 * p);
57  const double yy =
58  getRandomGenerator().drawUniform(-3, 3) + 5 * sin(0.4 * p);
59  const double zz =
60  -(PLANE_EQ[p][3] + PLANE_EQ[p][0] * xx + PLANE_EQ[p][1] * yy) /
61  PLANE_EQ[p][2];
62  xs.push_back(xx);
63  ys.push_back(yy);
64  zs.push_back(zz);
65  }
66  }
67 
68  for (size_t i = 0; i < N_noise; i++)
69  {
70  xs.push_back(getRandomGenerator().drawUniform(-7, 7));
71  ys.push_back(getRandomGenerator().drawUniform(-7, 7));
72  zs.push_back(getRandomGenerator().drawUniform(-7, 7));
73  }
74 
75  // Run RANSAC
76  // ------------------------------------
77  vector<pair<size_t, TPlane>> detectedPlanes;
78  const double DIST_THRESHOLD = 0.05;
79 
80  CTicTac tictac;
81 
82  ransac_detect_3D_planes(xs, ys, zs, detectedPlanes, DIST_THRESHOLD, 40);
83 
84  // Display output:
85  cout << "RANSAC method: ransac_detect_3D_planes" << endl;
86  cout << " Computation time: " << tictac.Tac() * 1000.0 << " ms" << endl;
87  cout << " " << detectedPlanes.size() << " planes detected." << endl;
88 
89  // Show GUI
90  // --------------------------
92  new mrpt::gui::CDisplayWindow3D("RANSAC: 3D planes", 500, 500));
93 
95 
96  scene->insert(opengl::CGridPlaneXY::Create(-20, 20, -20, 20, 0, 1));
97  scene->insert(opengl::stock_objects::CornerXYZ());
98 
99  for (vector<pair<size_t, TPlane>>::iterator p = detectedPlanes.begin();
100  p != detectedPlanes.end(); ++p)
101  {
103  opengl::CTexturedPlane::Create(-10, 10, -10, 10);
104 
105  TPose3D glPlanePose;
106  p->second.getAsPose3D(glPlanePose);
107  glPlane->setPose(glPlanePose);
108 
109  glPlane->setColor(
110  getRandomGenerator().drawUniform(0, 1),
111  getRandomGenerator().drawUniform(0, 1),
112  getRandomGenerator().drawUniform(0, 1), 0.6);
113 
114  scene->insert(glPlane);
115  }
116 
117  {
119  points->setColor(0, 0, 1);
120  points->setPointSize(3);
121  points->enableColorFromZ();
122 
123  // Convert double -> float:
124  vector<float> xsf, ysf, zsf;
128 
129  points->setAllPoints(xsf, ysf, zsf);
130 
131  scene->insert(points);
132  }
133 
134  win->get3DSceneAndLock() = scene;
135  win->unlockAccess3DScene();
136  win->forceRepaint();
137 
138  win->waitForKey();
139 }
140 
141 // ------------------------------------------------------
142 // TestRANSACLines
143 // ------------------------------------------------------
144 void TestRANSACLines()
145 {
147 
148  // Generate random points in 2D
149  // ------------------------------------
150  const size_t N_LINES = 4;
151 
152  const size_t N_line = 30;
153  const size_t N_noise = 50;
154 
155  const double LINE_EQ[N_LINES][3] = {
156  {1, -1, -2}, {1, +1.5, -1}, {0, -1, +2}, {0.5, -0.3, +1}};
157 
158  CVectorDouble xs, ys;
159  for (size_t p = 0; p < N_LINES; p++)
160  {
161  for (size_t i = 0; i < N_line; i++)
162  {
163  const double xx = getRandomGenerator().drawUniform(-10, 10);
164  const double yy =
166  (LINE_EQ[p][2] + LINE_EQ[p][0] * xx) / LINE_EQ[p][1];
167  xs.push_back(xx);
168  ys.push_back(yy);
169  }
170  }
171 
172  for (size_t i = 0; i < N_noise; i++)
173  {
174  xs.push_back(getRandomGenerator().drawUniform(-15, 15));
175  ys.push_back(getRandomGenerator().drawUniform(-15, 15));
176  }
177 
178  // Run RANSAC
179  // ------------------------------------
180  vector<pair<size_t, TLine2D>> detectedLines;
181  const double DIST_THRESHOLD = 0.2;
182 
183  CTicTac tictac;
184 
185  ransac_detect_2D_lines(xs, ys, detectedLines, DIST_THRESHOLD, 20);
186 
187  // Display output:
188  cout << "RANSAC method: ransac_detect_2D_lines" << endl;
189  cout << " Computation time: " << tictac.Tac() * 1000.0 << " ms" << endl;
190  cout << " " << detectedLines.size() << " lines detected." << endl;
191 
192  // Show GUI
193  // --------------------------
194  mrpt::gui::CDisplayWindowPlots win2("Set of points", 500, 500);
195 
196  win2.plot(xs, ys, ".b4", "points");
197 
198  unsigned int n = 0;
199  for (vector<pair<size_t, TLine2D>>::iterator p = detectedLines.begin();
200  p != detectedLines.end(); ++p)
201  {
202  CVectorDouble lx(2), ly(2);
203  lx[0] = -15;
204  lx[1] = 15;
205  for (CVectorDouble::Index q = 0; q < lx.size(); q++)
206  ly[q] = -(p->second.coefs[2] + p->second.coefs[0] * lx[q]) /
207  p->second.coefs[1];
208  win2.plot(lx, ly, "r-1", format("line_%u", n++));
209  }
210 
211  win2.axis_fit();
212  win2.axis_equal();
213 
214  win2.waitForKey();
215 }
216 
217 // ------------------------------------------------------
218 // MAIN
219 // ------------------------------------------------------
220 int main()
221 {
222  try
223  {
225  cout << endl << "Now running detection of lines..." << endl << endl;
226  TestRANSACLines();
227 
228  win.reset();
229 
230  return 0;
231  }
232  catch (const std::exception& e)
233  {
234  std::cerr << "MRPT error: " << mrpt::exception_to_str(e) << std::endl;
235  return -1;
236  }
237  catch (...)
238  {
239  printf("Untyped exception!!");
240  return -1;
241  }
242 }
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)
Create a GUI window and display plots with MATLAB-like interfaces and commands.
void TestRANSACPlanes()
void copy_container_typecasting(const src_container &src, dst_container &trg)
Copy all the elements in a container (vector, deque, list) into a different one performing the approp...
GLdouble GLdouble GLdouble GLdouble q
Definition: glext.h:3727
void randomize(const uint32_t seed)
Initialize the PRNG from the given random seed.
GLenum GLsizei n
Definition: glext.h:5136
A high-performance stopwatch, with typical resolution of nanoseconds.
STL namespace.
double drawGaussian1D(const double mean, const double std)
Generate a normally distributed pseudo-random number.
void push_back(const T &val)
GLsizei const GLfloat * points
Definition: glext.h:5414
static Ptr Create(Args &&... args)
Definition: CPointCloud.h:48
void ransac_detect_3D_planes(const CVectorDynamic< NUMTYPE > &x, const CVectorDynamic< NUMTYPE > &y, const CVectorDynamic< NUMTYPE > &z, std::vector< std::pair< size_t, TPlane >> &out_detected_planes, const double threshold, const size_t min_inliers_for_valid_plane=10)
Fit a number of 3-D planes to a given point cloud, automatically determining the number of existing p...
This base provides a set of functions for maths stuff.
std::shared_ptr< CDisplayWindow3D > Ptr
mrpt::gui::CDisplayWindow3D::Ptr win
Classes for 2D/3D geometry representation, both of single values and probability density distribution...
void TestRANSACLines()
void ransac_detect_2D_lines(const CVectorDynamic< NUMTYPE > &x, const CVectorDynamic< NUMTYPE > &y, std::vector< std::pair< size_t, TLine2D >> &out_detected_lines, const double threshold, const size_t min_inliers_for_valid_line=5)
Fit a number of 2-D lines to a given point cloud, automatically determining the number of existing li...
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.
std::string format(const char *fmt,...) MRPT_printf_format_check(1
A std::string version of C sprintf.
Definition: format.cpp:16
Lightweight 3D pose (three spatial coordinates, plus three angular coordinates).
Definition: TPose3D.h:23
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
static Ptr Create(Args &&... args)
Definition: COpenGLScene.h:60
Classes for creating GUI windows for 2D and 3D visualization.
Definition: about_box.h:14
CRandomGenerator & getRandomGenerator()
A static instance of a CRandomGenerator class, for use in single-thread applications.
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