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 
12 #include <mrpt/random.h>
13 #include <mrpt/math/geometry.h>
14 #include <mrpt/img/TColor.h>
15 #include <iostream>
16 
17 using namespace std;
18 using namespace mrpt;
19 using namespace mrpt::gui;
20 using namespace mrpt::opengl;
21 using namespace mrpt::math;
22 using namespace mrpt::img;
23 
25  const size_t N, opengl::CPointCloud::Ptr& gl, const TPoint3D& p_min,
26  const TPoint3D& p_max)
27 {
28  for (size_t i = 0; i < N; i++)
29  gl->insertPoint(
30  random::getRandomGenerator().drawUniform(p_min.x, p_max.x),
31  random::getRandomGenerator().drawUniform(p_min.y, p_max.y),
32  random::getRandomGenerator().drawUniform(p_min.z, p_max.z));
33 }
34 
36  const size_t N, opengl::CPointCloud::Ptr& gl, const TPoint3D& p_start,
37  const TPoint3D& p_end)
38 {
39  TPoint3D d = p_end - p_start;
40  d *= 1.0 / N;
41 
42  TPoint3D up(0, 0, 1);
43  TPoint3D lat;
45  lat *= 1.0 / lat.norm();
46 
47  TPoint3D p = p_start;
48  for (size_t i = 0; i < N; i++)
49  {
50  const double ang = i * 0.01;
51  TPoint3D pp = p + up * 30 * cos(ang) + lat * 30 * sin(ang);
52  gl->insertPoint(pp.x, pp.y, pp.z);
53  p += d;
54  }
55 }
56 
58  const size_t N, opengl::CPointCloud::Ptr& gl, const TPoint3D& p_mean,
59  const TPoint3D& p_stddevs)
60 {
61  for (size_t i = 0; i < N; i++)
62  gl->insertPoint(
63  random::getRandomGenerator().drawGaussian1D(p_mean.x, p_stddevs.x),
64  random::getRandomGenerator().drawGaussian1D(p_mean.y, p_stddevs.y),
65  random::getRandomGenerator().drawGaussian1D(p_mean.z, p_stddevs.z));
66 }
67 
68 // ------------------------------------------------------
69 // TestOctreeRenderHugePointCloud
70 // ------------------------------------------------------
72 {
73  // Change this in your program as needed:
74  // mrpt::global_settings::OCTREE_RENDER_MAX_DENSITY_POINTS_PER_SQPIXEL(0.1f);
75 
76  CDisplayWindow3D win("Demo of MRPT's octree pointclouds", 640, 480);
77 
78  COpenGLScene::Ptr& theScene = win.get3DSceneAndLock();
79 
80  // CPointCloud
81  opengl::CPointCloud::Ptr gl_pointcloud =
82  mrpt::make_aligned_shared<opengl::CPointCloud>();
83  theScene->insert(gl_pointcloud);
84 
85  gl_pointcloud->setPointSize(3.0);
86  gl_pointcloud->enablePointSmooth();
87  gl_pointcloud->enableColorFromZ();
88 
89  // Set the list of all points:
90 
91  const double L = 1e3;
92 
93  cout << "Building point cloud...";
94  cout.flush();
95 
96  for (int XX = -10; XX <= 10; XX++)
97  {
98  const double off_x = XX * 2 * L;
99 
100  for (int YY = -10; YY <= 10; YY++)
101  {
102  const double off_y = YY * 2 * L;
103 
105  1e4, gl_pointcloud, TPoint3D(off_x + 0, off_y + 0, 0),
106  TPoint3D(off_x + L, off_y + 0, 500));
107 
109  1e4, gl_pointcloud, TPoint3D(off_x + L, off_y + 0, 500),
110  TPoint3D(off_x + L, off_y + L, -500));
111 
113  1e4, gl_pointcloud, TPoint3D(off_x + L, off_y + L, -500),
114  TPoint3D(off_x + 0, off_y + L, 500));
115 
117  1e4, gl_pointcloud, TPoint3D(off_x + 0, off_y + L, 500),
118  TPoint3D(off_x + 0, off_y + 0, 0));
119  }
120  }
121 
122  cout << "Done.\n";
123  cout.flush();
124 
125  printf("Point count: %e\n", (double)gl_pointcloud->size());
126 
127  // Draw the octree bounding boxes:
129  mrpt::make_aligned_shared<mrpt::opengl::CSetOfObjects>();
130  gl_pointcloud->octree_get_graphics_boundingboxes(*gl_bb);
131  theScene->insert(gl_bb);
132 
133  // gl_pointcloud->octree_debug_dump_tree(std::cout);
134 
135  win.setCameraZoom(600);
136  {
137  mrpt::opengl::COpenGLViewport::Ptr view = theScene->getViewport("main");
138  view->setViewportClipDistances(0.1, 1e6);
139  }
140 
141  // IMPORTANT!!! IF NOT UNLOCKED, THE WINDOW WILL NOT BE UPDATED!
142  win.unlockAccess3DScene();
143  win.repaint();
144 
145  cout << "Close the window or press any key to end.\n";
146  bool end = false;
147  while (win.isOpen() && !end)
148  {
149  std::this_thread::sleep_for(5ms);
150 
151  if (win.keyHit())
152  {
153  switch (win.getPushedKey())
154  {
155  case 'q':
156  end = true;
157  break;
158  case 'b':
159  gl_bb->setVisibility(!gl_bb->isVisible());
160  break;
161  };
162  }
163 
164  // Update the texts on the gl display:
165  string s = mrpt::format(
166  "FPS=%5.02f | Rendered points=%.02e/%.02e (%.02f%%) | "
167  "Visib.oct.nodes: %u/%u",
168  win.getRenderingFPS(), (double)gl_pointcloud->getActuallyRendered(),
169  (double)gl_pointcloud->size(),
170  100 * double(gl_pointcloud->getActuallyRendered()) /
171  double(gl_pointcloud->size()),
172  (unsigned int)gl_pointcloud->octree_get_visible_nodes(),
173  (unsigned int)gl_pointcloud->octree_get_node_count());
174 
175  win.get3DSceneAndLock();
176  win.addTextMessage(
177  5, 5, s, TColorf(1, 1, 1), 0, MRPT_GLUT_BITMAP_HELVETICA_18);
178  win.addTextMessage(
179  5, 35, "'b': switch bounding-boxes visible, 'q': quit",
181  win.unlockAccess3DScene();
182  win.repaint();
183  }
184 }
185 
186 // ------------------------------------------------------
187 // MAIN
188 // ------------------------------------------------------
189 int main()
190 {
191  try
192  {
194 
195  std::this_thread::sleep_for(500ms);
196 
197  return 0;
198  }
199  catch (std::exception& e)
200  {
201  std::cout << "MRPT exception caught: " << e.what() << std::endl;
202  return -1;
203  }
204  catch (...)
205  {
206  printf("Untyped exception!!");
207  return -1;
208  }
209 }
TestOctreeRenderHugePointCloud
void TestOctreeRenderHugePointCloud()
Definition: vision_stereo_rectify/test.cpp:71
s
GLdouble s
Definition: glext.h:3676
geometry.h
insertRandomPoints_gauss
void insertRandomPoints_gauss(const size_t N, opengl::CPointCloud::Ptr &gl, const TPoint3D &p_mean, const TPoint3D &p_stddevs)
Definition: vision_stereo_rectify/test.cpp:57
end
GLuint GLuint end
Definition: glext.h:3528
mrpt::math::TPoint3D::z
double z
Definition: lightweight_geom_data.h:385
mrpt
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
Definition: CKalmanFilterCapable.h:30
CPointCloud.h
p
GLfloat GLfloat p
Definition: glext.h:6305
mrpt::opengl::CPointCloud::Ptr
std::shared_ptr< CPointCloud > Ptr
Definition: CPointCloud.h:50
CDisplayWindow3D.h
random.h
mrpt::opengl::MRPT_GLUT_BITMAP_HELVETICA_18
@ MRPT_GLUT_BITMAP_HELVETICA_18
Definition: opengl_fonts.h:32
main
int main()
Definition: vision_stereo_rectify/test.cpp:78
mrpt::img
Definition: CCanvas.h:17
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::math::TPoint3D::x
double x
X,Y,Z coordinates.
Definition: lightweight_geom_data.h:385
insertRandomPoints_uniform
void insertRandomPoints_uniform(const size_t N, opengl::CPointCloud::Ptr &gl, const TPoint3D &p_min, const TPoint3D &p_max)
Definition: vision_stereo_rectify/test.cpp:24
mrpt::img::TColorf
A RGB color - floats in the range [0,1].
Definition: TColor.h:79
mrpt::obs::gnss::lat
double lat
[deg], [deg], hgt over sea level[m]
Definition: gnss_messages_novatel.h:230
mrpt::opengl::CSetOfObjects::Ptr
std::shared_ptr< CSetOfObjects > Ptr
Definition: CSetOfObjects.h:30
mrpt::gui
Classes for creating GUI windows for 2D and 3D visualization.
Definition: about_box.h:16
mrpt::math::crossProduct3D
void crossProduct3D(const T &v0, const U &v1, V &vOut)
Computes the cross product of two 3D vectors, returning a vector normal to both.
Definition: geometry.h:814
mrpt::math::TPoint3D
Lightweight 3D point.
Definition: lightweight_geom_data.h:378
mrpt::math::TPoint3D::y
double y
Definition: lightweight_geom_data.h:385
mrpt::random::getRandomGenerator
CRandomGenerator & getRandomGenerator()
A static instance of a CRandomGenerator class, for use in single-thread applications.
Definition: RandomGenerator.cpp:19
mrpt::math
This base provides a set of functions for maths stuff.
Definition: math/include/mrpt/math/bits_math.h:13
insertRandomPoints_screw
void insertRandomPoints_screw(const size_t N, opengl::CPointCloud::Ptr &gl, const TPoint3D &p_start, const TPoint3D &p_end)
Definition: vision_stereo_rectify/test.cpp:35
mrpt::opengl::COpenGLScene::Ptr
std::shared_ptr< COpenGLScene > Ptr
Definition: COpenGLScene.h:61
mrpt::random::CRandomGenerator::drawGaussian1D
double drawGaussian1D(const double mean, const double std)
Generate a normally distributed pseudo-random number.
Definition: RandomGenerators.h:162
mrpt::opengl
The namespace for 3D scene representation and rendering.
Definition: CGlCanvasBase.h:15
mrpt::opengl::COpenGLViewport::Ptr
std::shared_ptr< COpenGLViewport > Ptr
Definition: COpenGLViewport.h:63
mrpt::gui::CDisplayWindow3D
A graphical user interface (GUI) for efficiently rendering 3D scenes in real-time.
Definition: CDisplayWindow3D.h:117
TColor.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