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/img/TColor.h>
12 #include <mrpt/math/geometry.h>
14 #include <mrpt/random.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 = opengl::CPointCloud::Create();
82  theScene->insert(gl_pointcloud);
83 
84  gl_pointcloud->setPointSize(3.0);
85  gl_pointcloud->enablePointSmooth();
86  gl_pointcloud->enableColorFromZ();
87 
88  // Set the list of all points:
89 
90  const double L = 1e3;
91 
92  cout << "Building point cloud...";
93  cout.flush();
94 
95  for (int XX = -10; XX <= 10; XX++)
96  {
97  const double off_x = XX * 2 * L;
98 
99  for (int YY = -10; YY <= 10; YY++)
100  {
101  const double off_y = YY * 2 * L;
102 
104  1e4, gl_pointcloud, TPoint3D(off_x + 0, off_y + 0, 0),
105  TPoint3D(off_x + L, off_y + 0, 500));
106 
108  1e4, gl_pointcloud, TPoint3D(off_x + L, off_y + 0, 500),
109  TPoint3D(off_x + L, off_y + L, -500));
110 
112  1e4, gl_pointcloud, TPoint3D(off_x + L, off_y + L, -500),
113  TPoint3D(off_x + 0, off_y + L, 500));
114 
116  1e4, gl_pointcloud, TPoint3D(off_x + 0, off_y + L, 500),
117  TPoint3D(off_x + 0, off_y + 0, 0));
118  }
119  }
120 
121  cout << "Done.\n";
122  cout.flush();
123 
124  printf("Point count: %e\n", (double)gl_pointcloud->size());
125 
126  // Draw the octree bounding boxes:
129  gl_pointcloud->octree_get_graphics_boundingboxes(*gl_bb);
130  theScene->insert(gl_bb);
131 
132  // gl_pointcloud->octree_debug_dump_tree(std::cout);
133 
134  win.setCameraZoom(600);
135  {
136  mrpt::opengl::COpenGLViewport::Ptr view = theScene->getViewport("main");
137  view->setViewportClipDistances(0.1, 1e6);
138  }
139 
140  // IMPORTANT!!! IF NOT UNLOCKED, THE WINDOW WILL NOT BE UPDATED!
141  win.unlockAccess3DScene();
142  win.repaint();
143 
144  cout << "Close the window or press any key to end.\n";
145  bool end = false;
146  while (win.isOpen() && !end)
147  {
148  std::this_thread::sleep_for(5ms);
149 
150  if (win.keyHit())
151  {
152  switch (win.getPushedKey())
153  {
154  case 'q':
155  end = true;
156  break;
157  case 'b':
158  gl_bb->setVisibility(!gl_bb->isVisible());
159  break;
160  };
161  }
162 
163  // Update the texts on the gl display:
164  string s = mrpt::format(
165  "FPS=%5.02f | Rendered points=%.02e/%.02e (%.02f%%) | "
166  "Visib.oct.nodes: %u/%u",
167  win.getRenderingFPS(), (double)gl_pointcloud->getActuallyRendered(),
168  (double)gl_pointcloud->size(),
169  100 * double(gl_pointcloud->getActuallyRendered()) /
170  double(gl_pointcloud->size()),
171  (unsigned int)gl_pointcloud->octree_get_visible_nodes(),
172  (unsigned int)gl_pointcloud->octree_get_node_count());
173 
174  win.get3DSceneAndLock();
175  win.addTextMessage(
176  5, 5, s, TColorf(1, 1, 1), 0, MRPT_GLUT_BITMAP_HELVETICA_18);
177  win.addTextMessage(
178  5, 35, "'b': switch bounding-boxes visible, 'q': quit",
180  win.unlockAccess3DScene();
181  win.repaint();
182  }
183 }
184 
185 // ------------------------------------------------------
186 // MAIN
187 // ------------------------------------------------------
188 int main()
189 {
190  try
191  {
193 
194  std::this_thread::sleep_for(500ms);
195 
196  return 0;
197  }
198  catch (const std::exception& e)
199  {
200  std::cerr << "MRPT error: " << mrpt::exception_to_str(e) << std::endl;
201  return -1;
202  }
203  catch (...)
204  {
205  printf("Untyped exception!!");
206  return -1;
207  }
208 }
void TestOctreeRenderHugePointCloud()
double drawUniform(const double Min, const double Max)
Generate a uniformly distributed pseudo-random number using the MT19937 algorithm, scaled to the selected range.
double x
X,Y,Z coordinates.
Definition: TPoint3D.h:83
void insertRandomPoints_gauss(const size_t N, opengl::CPointCloud::Ptr &gl, const TPoint3D &p_mean, const TPoint3D &p_stddevs)
STL namespace.
static Ptr Create(Args &&... args)
Definition: CSetOfObjects.h:28
GLdouble s
Definition: glext.h:3682
double drawGaussian1D(const double mean, const double std)
Generate a normally distributed pseudo-random number.
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:804
This base provides a set of functions for maths stuff.
GLuint GLuint end
Definition: glext.h:3532
double lat
[deg], [deg], hgt over sea level[m]
mrpt::gui::CDisplayWindow3D::Ptr win
void insertRandomPoints_uniform(const size_t N, opengl::CPointCloud::Ptr &gl, const TPoint3D &p_min, const TPoint3D &p_max)
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
std::string format(const char *fmt,...) MRPT_printf_format_check(1
A std::string version of C sprintf.
Definition: format.cpp:16
A RGB color - floats in the range [0,1].
Definition: TColor.h:77
The namespace for 3D scene representation and rendering.
Definition: CGlCanvasBase.h:15
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
Classes for creating GUI windows for 2D and 3D visualization.
Definition: about_box.h:14
Lightweight 3D point.
Definition: TPoint3D.h:90
void insertRandomPoints_screw(const size_t N, opengl::CPointCloud::Ptr &gl, const TPoint3D &p_start, const TPoint3D &p_end)
CRandomGenerator & getRandomGenerator()
A static instance of a CRandomGenerator class, for use in single-thread applications.
GLfloat GLfloat p
Definition: glext.h:6398
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