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 
13 #include <mrpt/random.h>
14 #include <mrpt/system/CTicTac.h>
16 #include <mrpt/poses/CPose3D.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  mrpt::make_aligned_shared<opengl::COpenGLScene>();
96 
97  scene->insert(
98  mrpt::make_aligned_shared<opengl::CGridPlaneXY>(
99  -20, 20, -20, 20, 0, 1));
100  scene->insert(opengl::stock_objects::CornerXYZ());
101 
102  for (vector<pair<size_t, TPlane>>::iterator p = detectedPlanes.begin();
103  p != detectedPlanes.end(); ++p)
104  {
106  mrpt::make_aligned_shared<opengl::CTexturedPlane>(-10, 10, -10, 10);
107 
108  TPose3D glPlanePose;
109  p->second.getAsPose3D(glPlanePose);
110  glPlane->setPose(glPlanePose);
111 
112  glPlane->setColor(
113  getRandomGenerator().drawUniform(0, 1),
114  getRandomGenerator().drawUniform(0, 1),
115  getRandomGenerator().drawUniform(0, 1), 0.6);
116 
117  scene->insert(glPlane);
118  }
119 
120  {
122  mrpt::make_aligned_shared<opengl::CPointCloud>();
123  points->setColor(0, 0, 1);
124  points->setPointSize(3);
125  points->enableColorFromZ();
126 
127  // Convert double -> float:
128  vector<float> xsf, ysf, zsf;
132 
133  points->setAllPoints(xsf, ysf, zsf);
134 
135  scene->insert(points);
136  }
137 
138  win->get3DSceneAndLock() = scene;
139  win->unlockAccess3DScene();
140  win->forceRepaint();
141 
142  win->waitForKey();
143 }
144 
145 // ------------------------------------------------------
146 // TestRANSACLines
147 // ------------------------------------------------------
148 void TestRANSACLines()
149 {
151 
152  // Generate random points in 2D
153  // ------------------------------------
154  const size_t N_LINES = 4;
155 
156  const size_t N_line = 30;
157  const size_t N_noise = 50;
158 
159  const double LINE_EQ[N_LINES][3] = {
160  {1, -1, -2}, {1, +1.5, -1}, {0, -1, +2}, {0.5, -0.3, +1}};
161 
162  CVectorDouble xs, ys;
163  for (size_t p = 0; p < N_LINES; p++)
164  {
165  for (size_t i = 0; i < N_line; i++)
166  {
167  const double xx = getRandomGenerator().drawUniform(-10, 10);
168  const double yy =
170  (LINE_EQ[p][2] + LINE_EQ[p][0] * xx) / LINE_EQ[p][1];
171  xs.push_back(xx);
172  ys.push_back(yy);
173  }
174  }
175 
176  for (size_t i = 0; i < N_noise; i++)
177  {
178  xs.push_back(getRandomGenerator().drawUniform(-15, 15));
179  ys.push_back(getRandomGenerator().drawUniform(-15, 15));
180  }
181 
182  // Run RANSAC
183  // ------------------------------------
184  vector<pair<size_t, TLine2D>> detectedLines;
185  const double DIST_THRESHOLD = 0.2;
186 
187  CTicTac tictac;
188 
189  ransac_detect_2D_lines(xs, ys, detectedLines, DIST_THRESHOLD, 20);
190 
191  // Display output:
192  cout << "RANSAC method: ransac_detect_2D_lines" << endl;
193  cout << " Computation time: " << tictac.Tac() * 1000.0 << " ms" << endl;
194  cout << " " << detectedLines.size() << " lines detected." << endl;
195 
196  // Show GUI
197  // --------------------------
198  mrpt::gui::CDisplayWindowPlots win2("Set of points", 500, 500);
199 
200  win2.plot(xs, ys, ".b4", "points");
201 
202  unsigned int n = 0;
203  for (vector<pair<size_t, TLine2D>>::iterator p = detectedLines.begin();
204  p != detectedLines.end(); ++p)
205  {
206  CVectorDouble lx(2), ly(2);
207  lx[0] = -15;
208  lx[1] = 15;
209  for (CVectorDouble::Index q = 0; q < lx.size(); q++)
210  ly[q] = -(p->second.coefs[2] + p->second.coefs[0] * lx[q]) /
211  p->second.coefs[1];
212  win2.plot(lx, ly, "r-1", format("line_%u", n++));
213  }
214 
215  win2.axis_fit();
216  win2.axis_equal();
217 
218  win2.waitForKey();
219 }
220 
221 // ------------------------------------------------------
222 // MAIN
223 // ------------------------------------------------------
224 int main()
225 {
226  try
227  {
229  cout << endl << "Now running detection of lines..." << endl << endl;
230  TestRANSACLines();
231 
232  win.reset();
233 
234  return 0;
235  }
236  catch (std::exception& e)
237  {
238  std::cout << "MRPT exception caught: " << e.what() << std::endl;
239  return -1;
240  }
241  catch (...)
242  {
243  printf("Untyped exception!!");
244  return -1;
245  }
246 }
n
GLenum GLsizei n
Definition: glext.h:5074
TestRANSACPlanes
void TestRANSACPlanes()
Definition: vision_stereo_rectify/test.cpp:36
mrpt::containers::copy_container_typecasting
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...
Definition: copy_container_typecasting.h:31
q
GLdouble GLdouble GLdouble GLdouble q
Definition: glext.h:3721
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
mrpt::math::dynamic_vector
Column vector, like Eigen::MatrixX*, but automatically initialized to zeros since construction.
Definition: eigen_frwds.h:44
points
GLsizei const GLfloat * points
Definition: glext.h:5339
mrpt::math::ransac_detect_2D_lines
void ransac_detect_2D_lines(const Eigen::Matrix< NUMTYPE, Eigen::Dynamic, 1 > &x, const Eigen::Matrix< NUMTYPE, Eigen::Dynamic, 1 > &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...
Definition: ransac_applications.cpp:280
mrpt
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
Definition: CKalmanFilterCapable.h:30
stock_objects.h
CPointCloud.h
p
GLfloat GLfloat p
Definition: glext.h:6305
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
main
int main()
Definition: vision_stereo_rectify/test.cpp:78
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::format
std::string format(const char *fmt,...) MRPT_printf_format_check(1
A std::string version of C sprintf.
Definition: format.cpp:16
mrpt::gui::CDisplayWindow3D::Ptr
std::shared_ptr< CDisplayWindow3D > Ptr
Definition: CDisplayWindow3D.h:120
mrpt::math::TPose3D
Lightweight 3D pose (three spatial coordinates, plus three angular coordinates).
Definition: lightweight_geom_data.h:603
TestRANSACLines
void TestRANSACLines()
Definition: vision_stereo_rectify/test.cpp:148
mrpt::gui
Classes for creating GUI windows for 2D and 3D visualization.
Definition: about_box.h:16
mrpt::gui::CDisplayWindowPlots
Create a GUI window and display plots with MATLAB-like interfaces and commands.
Definition: CDisplayWindowPlots.h:31
CPose3D.h
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
copy_container_typecasting.h
CTicTac.h
ransac_applications.h
mrpt::math
This base provides a set of functions for maths stuff.
Definition: math/include/mrpt/math/bits_math.h:13
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
iterator
Scalar * iterator
Definition: eigen_plugins.h:26
mrpt::random::CRandomGenerator::drawGaussian1D
double drawGaussian1D(const double mean, const double std)
Generate a normally distributed pseudo-random number.
Definition: RandomGenerators.h:162
CDisplayWindowPlots.h
CGridPlaneXY.h
mrpt::gui::CDisplayWindow3D
A graphical user interface (GUI) for efficiently rendering 3D scenes in real-time.
Definition: CDisplayWindow3D.h:117
CTexturedPlane.h
mrpt::system
This namespace provides a OS-independent interface to many useful functions: filenames manipulation,...
Definition: math_frwds.h:25
mrpt::math::ransac_detect_3D_planes
void ransac_detect_3D_planes(const Eigen::Matrix< NUMTYPE, Eigen::Dynamic, 1 > &x, const Eigen::Matrix< NUMTYPE, Eigen::Dynamic, 1 > &y, const Eigen::Matrix< NUMTYPE, Eigen::Dynamic, 1 > &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...



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