Main MRPT website > C++ reference for MRPT 1.9.9
COctoMapVoxels.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-2017, 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 "opengl-precomp.h" // Precompiled header
11 
13 #include <mrpt/utils/CStream.h>
15 #include "opengl_internals.h"
16 
17 using namespace mrpt;
18 using namespace mrpt::opengl;
19 using namespace mrpt::utils;
20 using namespace mrpt::math;
21 using namespace std;
22 
24 
25 /** Ctor */
27  : m_enable_lighting(false),
28  m_enable_cube_transparency(true),
29  m_showVoxelsAsPoints(false),
30  m_showVoxelsAsPointsSize(3.0f),
31  m_show_grids(false),
32  m_grid_width(1.0f),
33  m_grid_color(0xE0, 0xE0, 0xE0, 0x90),
34  m_visual_mode(COctoMapVoxels::COLOR_FROM_OCCUPANCY)
35 {
36 }
37 
38 /** Clears everything */
40 {
41  m_voxel_sets.clear();
42  m_grid_cubes.clear();
43 
45 }
46 
48  const mrpt::math::TPoint3D& bb_min, const mrpt::math::TPoint3D& bb_max)
49 {
50  m_bb_min = bb_min;
51  m_bb_max = bb_max;
52 }
53 
54 #if MRPT_HAS_OPENGL_GLUT
55 
56 // See: http://www.songho.ca/opengl/gl_vertexarray.html
57 
58 // cube ///////////////////////////////////////////////////////////////////////
59 // v6----- v5
60 // +Z /| /|
61 // A v1------v0|
62 // | | | | |
63 // | | |v7---|-|v4 / -X
64 // | |/ |/ /
65 // | v2------v3 L +X
66 // ----------------------> +Y
67 //
68 
69 const GLubyte grid_line_indices[] = {0, 1, 1, 2, 2, 3, 3, 0, 4, 5, 5, 6,
70  6, 7, 7, 4, 0, 5, 1, 6, 2, 7, 3, 4};
71 
72 const GLubyte cube_indices[36] = {0, 1, 2, 2, 3, 0, 0, 3, 4, 4, 5, 0,
73  0, 5, 6, 6, 1, 0, 1, 6, 7, 7, 2, 1,
74  7, 4, 3, 3, 2, 7, 4, 7, 6, 6, 5, 4};
75 
76 // normal array
77 const GLfloat normals_cube[3 * 6 * 4] = {
78  1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, // v0,v1,v2,v3 (front)
79  0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, // v0,v3,v4,v5 (right)
80  0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, // v0,v5,v6,v1 (top)
81  0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, // v1,v6,v7,v2 (left)
82  0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, // v7,v4,v3,v2 (bottom)
83  -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0}; // v4,v7,v6,v5 (back)
84 
85 #endif
86 
87 /*---------------------------------------------------------------
88  render
89  ---------------------------------------------------------------*/
91 {
92 #if MRPT_HAS_OPENGL_GLUT
93 
95 
96  // Draw grids ====================================
97  if (m_show_grids)
98  {
99  glLineWidth(m_grid_width);
101 
102  glDisable(GL_LIGHTING); // Disable lights when drawing lines
103 
104  // Antialiasing:
107  if (m_grid_color.A != 255)
108  {
111  }
112 
113  glColor4ub(
114  m_grid_color.R, m_grid_color.G, m_grid_color.B, m_grid_color.A);
115 
116  const size_t nGrids = m_grid_cubes.size();
117  for (size_t i = 0; i < nGrids; i++)
118  {
119  const TGridCube& c = m_grid_cubes[i];
120 
121  const GLdouble vertices[8 * 3] = {
122  c.max.x, c.max.y, c.max.z, c.max.x, c.min.y, c.max.z,
123  c.max.x, c.min.y, c.min.z, c.max.x, c.max.y, c.min.z,
124  c.min.x, c.max.y, c.min.z, c.min.x, c.max.y, c.max.z,
125  c.min.x, c.min.y, c.max.z, c.min.x, c.min.y, c.min.z};
126  glVertexPointer(3, GL_DOUBLE, 0, vertices);
128  GL_LINES,
129  sizeof(grid_line_indices) / sizeof(grid_line_indices[0]),
130  GL_UNSIGNED_BYTE, grid_line_indices);
131  }
132 
133  glEnable(GL_LIGHTING); // Disable lights when drawing lines
134  // End of antialiasing:
135  glPopAttrib();
136  }
137 
138  // Draw cubes ====================================
139  if (!m_enable_lighting) glDisable(GL_LIGHTING);
140 
142 
143  glNormalPointer(GL_FLOAT, 0, normals_cube);
144 
145  if (m_enable_cube_transparency)
146  {
149  }
150 
151  if (m_showVoxelsAsPoints)
152  {
153  glPointSize(m_showVoxelsAsPointsSize);
155  }
156 
157  for (size_t i = 0; i < m_voxel_sets.size(); i++)
158  {
159  if (!m_voxel_sets[i].visible) continue;
160 
161  const std::vector<TVoxel>& voxels = m_voxel_sets[i].voxels;
162  const size_t N = voxels.size();
163  for (size_t j = 0; j < N; j++)
164  {
165  const mrpt::utils::TColor& vx_j_col = voxels[j].color;
166  glColor4ub(vx_j_col.R, vx_j_col.G, vx_j_col.B, vx_j_col.A);
167 
168  const mrpt::math::TPoint3D& c = voxels[j].coords;
169  const double L = voxels[j].side_length * 0.5;
170 
171  if (!m_showVoxelsAsPoints)
172  {
173  // Render as cubes:
174  const GLdouble vertices[8 * 3] = {
175  c.x + L, c.y + L, c.z + L, c.x + L, c.y - L, c.z + L,
176  c.x + L, c.y - L, c.z - L, c.x + L, c.y + L, c.z - L,
177  c.x - L, c.y + L, c.z - L, c.x - L, c.y + L, c.z + L,
178  c.x - L, c.y - L, c.z + L, c.x - L, c.y - L, c.z - L};
179  glVertexPointer(3, GL_DOUBLE, 0, vertices);
181  GL_TRIANGLES,
182  sizeof(cube_indices) / sizeof(cube_indices[0]),
183  GL_UNSIGNED_BYTE, cube_indices);
184  }
185  else
186  {
187  // Render as simple points:
188  glVertex3f(c.x, c.y, c.z);
189  }
190  }
191  }
192 
193  if (m_showVoxelsAsPoints)
194  {
195  glEnd(); // of GL_POINTS
196  }
197 
198  if (m_enable_cube_transparency) glDisable(GL_BLEND);
199 
201 
202  if (!m_enable_lighting) glEnable(GL_LIGHTING);
203 
206 
207 #endif
208 }
209 
213 
214 namespace mrpt
215 {
216 namespace utils
217 {
220 {
221  out << a.visible << a.voxels;
222  return out;
223 }
226 {
227  in >> a.visible >> a.voxels;
228  return in;
229 }
230 
233 {
234  out << a.min << a.max;
235  return out;
236 }
238 {
239  in >> a.min >> a.max;
240  return in;
241 }
242 
244 {
245  out << a.coords << a.side_length << a.color;
246  return out;
247 }
249 {
250  in >> a.coords >> a.side_length >> a.color;
251  return in;
252 }
253 }
254 }
255 
256 /*---------------------------------------------------------------
257  Implements the writing to a CStream capability of
258  CSerializable objects
259  ---------------------------------------------------------------*/
261  mrpt::utils::CStream& out, int* version) const
262 {
263  if (version)
264  *version = 2;
265  else
266  {
267  writeToStreamRender(out);
268 
269  out << m_voxel_sets << m_grid_cubes << m_bb_min << m_bb_max
270  << m_enable_lighting << m_showVoxelsAsPoints
271  << m_showVoxelsAsPointsSize << m_show_grids << m_grid_width
272  << m_grid_color << m_enable_cube_transparency // added in v1
273  << uint32_t(m_visual_mode); // added in v2
274  }
275 }
276 
277 /*---------------------------------------------------------------
278  Implements the reading from a CStream capability of
279  CSerializable objects
280  ---------------------------------------------------------------*/
282 {
283  switch (version)
284  {
285  case 0:
286  case 1:
287  case 2:
288  {
289  readFromStreamRender(in);
290 
291  in >> m_voxel_sets >> m_grid_cubes >> m_bb_min >> m_bb_max >>
292  m_enable_lighting >> m_showVoxelsAsPoints >>
293  m_showVoxelsAsPointsSize >> m_show_grids >> m_grid_width >>
294  m_grid_color;
295 
296  if (version >= 1)
297  in >> m_enable_cube_transparency;
298  else
299  m_enable_cube_transparency = false;
300 
301  if (version >= 2)
302  {
303  uint32_t i;
304  in >> i;
305  m_visual_mode =
306  static_cast<COctoMapVoxels::visualization_mode_t>(i);
307  }
308  else
309  m_visual_mode = COctoMapVoxels::COLOR_FROM_OCCUPANCY;
310  }
311  break;
312  default:
314  };
315 
317 }
318 
320  mrpt::math::TPoint3D& bb_min, mrpt::math::TPoint3D& bb_max) const
321 {
322  bb_min = m_bb_min;
323  bb_max = m_bb_max;
324 
325  // Convert to coordinates of my parent:
326  m_pose.composePoint(bb_min, bb_min);
327  m_pose.composePoint(bb_max, bb_max);
328 }
329 
332 {
333  return a.coords.z < b.coords.z;
334 }
335 
337 {
338  for (size_t i = 0; i < m_voxel_sets.size(); i++)
339  {
340  std::sort(
341  m_voxel_sets[i].voxels.begin(), m_voxel_sets[i].voxels.end(),
342  &sort_voxels_z);
343  }
344 }
GLAPI void GLAPIENTRY glDisableClientState(GLenum array)
GLAPI void GLAPIENTRY glEnableClientState(GLenum array)
Classes for serialization, sockets, ini-file manipulation, streams, list of properties-values, timewatch, extensions to STL.
mrpt::utils::CStream & operator<<(mrpt::utils::CStream &out, const mrpt::opengl::CLight &o)
Definition: CLight.cpp:132
visualization_mode_t
The different coloring schemes, which modulate the generic mrpt::opengl::CRenderizable object color...
double GLdouble
Definition: glew.h:219
GLAPI void GLAPIENTRY glEnable(GLenum cap)
GLAPI void GLAPIENTRY glPointSize(GLfloat size)
#define GL_VERTEX_ARRAY
Definition: glew.h:724
#define IMPLEMENTS_SERIALIZABLE(class_name, base, NameSpace)
This must be inserted in all CSerializable classes implementation files.
void setBoundingBox(const mrpt::math::TPoint3D &bb_min, const mrpt::math::TPoint3D &bb_max)
Manually changes the bounding box (normally the user doesn&#39;t need to call this)
#define GL_TRIANGLES
Definition: glew.h:276
EIGEN_STRONG_INLINE void notifyChange() const
Must be called to notify that the object has changed (so, the display list must be updated) ...
STL namespace.
#define GL_UNSIGNED_BYTE
Definition: glew.h:302
#define GL_ONE_MINUS_SRC_ALPHA
Definition: glew.h:287
void clear()
Clears everything.
GLAPI void GLAPIENTRY glPopAttrib(void)
#define GL_LIGHTING
Definition: glew.h:385
bool sort_voxels_z(const COctoMapVoxels::TVoxel &a, const COctoMapVoxels::TVoxel &b)
GLAPI void GLAPIENTRY glLineWidth(GLfloat width)
float GLfloat
Definition: glew.h:217
GLAPI void GLAPIENTRY glBlendFunc(GLenum sfactor, GLenum dfactor)
A renderizable object suitable for rendering with OpenGL&#39;s display lists.
A flexible renderer of voxels, typically from a 3D octo map (see mrpt::maps::COctoMap).
#define GL_COLOR_BUFFER_BIT
Definition: glew.h:265
This base class is used to provide a unified interface to files,memory buffers,..Please see the deriv...
Definition: CStream.h:41
This base provides a set of functions for maths stuff.
Definition: CArrayNumeric.h:19
#define GL_LINE_SMOOTH
Definition: glew.h:367
#define GL_NORMAL_ARRAY
Definition: glew.h:725
const GLubyte * c
Definition: glext.h:6313
#define MRPT_THROW_UNKNOWN_SERIALIZATION_VERSION(__V)
For use in CSerializable implementations.
void render_dl() const override
Render.
A RGB color - 8bit.
Definition: TColor.h:25
GLubyte GLubyte b
Definition: glext.h:6279
double x
X,Y,Z coordinates.
GLAPI void GLAPIENTRY glVertexPointer(GLint size, GLenum type, GLsizei stride, const GLvoid *pointer)
GLAPI void GLAPIENTRY glBegin(GLenum mode)
#define GL_BLEND
Definition: glew.h:432
#define GL_POINTS
Definition: glew.h:272
void readFromStream(mrpt::utils::CStream &in, int version) override
Introduces a pure virtual method responsible for loading from a CStream This can not be used directly...
GLAPI void GLAPIENTRY glVertex3f(GLfloat x, GLfloat y, GLfloat z)
GLAPI void GLAPIENTRY glColor4ub(GLubyte red, GLubyte green, GLubyte blue, GLubyte alpha)
The info of each grid block.
mrpt::utils::CStream & operator>>(mrpt::utils::CStream &in, mrpt::opengl::CLight &o)
Definition: CLight.cpp:126
GLAPI void GLAPIENTRY glDrawElements(GLenum mode, GLsizei count, GLenum type, const GLvoid *indices)
#define GL_SRC_ALPHA
Definition: glew.h:286
The info of each of the voxels.
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
Color goes from black (occupied voxel) to the chosen color (free voxel)
void getBoundingBox(mrpt::math::TPoint3D &bb_min, mrpt::math::TPoint3D &bb_max) const override
Evaluates the bounding box of this object (including possible children) in the coordinate frame of th...
GLAPI void GLAPIENTRY glNormalPointer(GLenum type, GLsizei stride, const GLvoid *pointer)
GLAPI void GLAPIENTRY glPushAttrib(GLbitfield mask)
#define DECLARE_CUSTOM_TTYPENAME(_TYPE)
Identical to MRPT_DECLARE_TTYPENAME but intended for user code.
Definition: TTypeName.h:63
void checkOpenGLError()
Checks glGetError and throws an exception if an error situation is found.
Definition: gl_utils.cpp:140
GLuint in
Definition: glext.h:7274
The namespace for 3D scene representation and rendering.
Definition: CGlCanvasBase.h:15
GLAPI void GLAPIENTRY glEnd(void)
#define GL_DOUBLE
Definition: glew.h:311
#define GL_FLOAT
Definition: glew.h:307
#define GL_LINES
Definition: glew.h:273
Lightweight 3D point.
unsigned __int32 uint32_t
Definition: rptypes.h:47
GLAPI void GLAPIENTRY glDisable(GLenum cap)
GLubyte GLubyte GLubyte a
Definition: glext.h:6279
void writeToStream(mrpt::utils::CStream &out, int *getVersion) const override
Introduces a pure virtual method responsible for writing to a CStream.
unsigned char GLubyte
Definition: glew.h:214
#define GL_LINE_BIT
Definition: glew.h:253



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: ae4571287 Thu Nov 23 00:06:53 2017 +0100 at dom oct 27 23:51:55 CET 2019