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>
11 #include <mrpt/math/model_search.h>
13 #include <mrpt/system/CTicTac.h>
18 #include <mrpt/poses/CPose3D.h>
19 #include <mrpt/random.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  mrpt::make_aligned_shared<opengl::COpenGLScene>();
137 
138  scene->insert(
139  mrpt::make_aligned_shared<opengl::CGridPlaneXY>(
140  -20, 20, -20, 20, 0, 1));
141  scene->insert(opengl::stock_objects::CornerXYZ());
142 
144  mrpt::make_aligned_shared<opengl::CPointCloud>();
145  points->setColor(0, 0, 1);
146  points->setPointSize(3);
147  points->enableColorFromZ();
148 
149  {
150  std::vector<float> x, y, z;
151  x.reserve(data.size());
152  y.reserve(data.size());
153  z.reserve(data.size());
154  for (size_t i = 0; i < data.size(); i++)
155  {
156  x.push_back(data[i].x);
157  y.push_back(data[i].y);
158  z.push_back(data[i].z);
159  }
160  points->setAllPointsFast(x, y, z);
161  }
162 
163  scene->insert(points);
164 
166  mrpt::make_aligned_shared<opengl::CTexturedPlane>(-4, 4, -4, 4);
167 
168  TPose3D glPlanePose;
169  best_model.getAsPose3D(glPlanePose);
170  glPlane->setPose(glPlanePose);
171 
172  scene->insert(glPlane);
173 
174  win.get3DSceneAndLock() = scene;
175  win.unlockAccess3DScene();
176  win.forceRepaint();
177 
178  win.waitForKey();
179 }
180 
181 // ------------------------------------------------------
182 // MAIN
183 // ------------------------------------------------------
184 int main()
185 {
186  try
187  {
188  TestRANSAC();
189  return 0;
190  }
191  catch (std::exception& e)
192  {
193  std::cout << "MRPT exception caught: " << e.what() << std::endl;
194  return -1;
195  }
196  catch (...)
197  {
198  printf("Untyped exception!!");
199  return -1;
200  }
201 }
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
model_search.h
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::ModelSearch
Model search implementations: RANSAC and genetic algorithm.
Definition: model_search.h:66
ASSERT_
#define ASSERT_(f)
Defines an assertion mechanism.
Definition: exceptions.h:113
CPointCloud.h
stock_objects.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
Fit3DPlane
Definition: vision_stereo_rectify/test.cpp:30
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
TestRANSAC
void TestRANSAC()
Definition: vision_stereo_rectify/test.cpp:70
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
index
GLuint index
Definition: glext.h:4054
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::math::ModelSearch::geneticSingleModel
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.
Definition: model_search_impl.h:97
mrpt::gui
Classes for creating GUI windows for 2D and 3D visualization.
Definition: about_box.h:16
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::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
z
GLdouble GLdouble z
Definition: glext.h:3872
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::math::ModelSearch::ransacSingleModel
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.
Definition: model_search_impl.h:26
y
GLenum GLint GLint y
Definition: glext.h:3538
mrpt::gui::CDisplayWindow3D
A graphical user interface (GUI) for efficiently rendering 3D scenes in real-time.
Definition: CDisplayWindow3D.h:117
x
GLenum GLint x
Definition: glext.h:3538
CTexturedPlane.h
mrpt::system
This namespace provides a OS-independent interface to many useful functions: filenames manipulation,...
Definition: math_frwds.h:25
ransac.h
model
_u8 model
Definition: rplidar_cmd.h:2



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