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-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 "opengl-precomp.h" // Precompiled header
11 
15 #include "opengl_internals.h"
16 
17 using namespace mrpt;
18 using namespace mrpt::opengl;
19 using namespace mrpt::math;
20 using namespace std;
21 
23 
24 /** Ctor */
26  : m_enable_lighting(false),
27  m_enable_cube_transparency(true),
28  m_showVoxelsAsPoints(false),
29  m_showVoxelsAsPointsSize(3.0f),
30  m_show_grids(false),
31  m_grid_width(1.0f),
32  m_grid_color(0xE0, 0xE0, 0xE0, 0x90),
33  m_visual_mode(COctoMapVoxels::COLOR_FROM_OCCUPANCY)
34 {
35 }
36 
37 /** Clears everything */
39 {
40  m_voxel_sets.clear();
41  m_grid_cubes.clear();
42 
44 }
45 
47  const mrpt::math::TPoint3D& bb_min, const mrpt::math::TPoint3D& bb_max)
48 {
49  m_bb_min = bb_min;
50  m_bb_max = bb_max;
51 }
52 
53 #if MRPT_HAS_OPENGL_GLUT
54 
55 // See: http://www.songho.ca/opengl/gl_vertexarray.html
56 
57 // cube ///////////////////////////////////////////////////////////////////////
58 // v6----- v5
59 // +Z /| /|
60 // A v1------v0|
61 // | | | | |
62 // | | |v7---|-|v4 / -X
63 // | |/ |/ /
64 // | v2------v3 L +X
65 // ----------------------> +Y
66 //
67 
68 const GLubyte grid_line_indices[] = {0, 1, 1, 2, 2, 3, 3, 0, 4, 5, 5, 6,
69  6, 7, 7, 4, 0, 5, 1, 6, 2, 7, 3, 4};
70 
71 const GLubyte cube_indices[36] = {0, 1, 2, 2, 3, 0, 0, 3, 4, 4, 5, 0,
72  0, 5, 6, 6, 1, 0, 1, 6, 7, 7, 2, 1,
73  7, 4, 3, 3, 2, 7, 4, 7, 6, 6, 5, 4};
74 
75 // normal array
76 const GLfloat normals_cube[3 * 6 * 4] = {
77  1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, // v0,v1,v2,v3 (front)
78  0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, // v0,v3,v4,v5 (right)
79  0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, // v0,v5,v6,v1 (top)
80  0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, // v1,v6,v7,v2 (left)
81  0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, // v7,v4,v3,v2 (bottom)
82  -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0}; // v4,v7,v6,v5 (back)
83 
84 #endif
85 
86 /*---------------------------------------------------------------
87  render
88  ---------------------------------------------------------------*/
90 {
91 #if MRPT_HAS_OPENGL_GLUT
92 
94 
95  // Draw grids ====================================
96  if (m_show_grids)
97  {
98  glLineWidth(m_grid_width);
100 
101  glDisable(GL_LIGHTING); // Disable lights when drawing lines
102 
103  // Antialiasing:
106  if (m_grid_color.A != 255)
107  {
110  }
111 
112  glColor4ub(
113  m_grid_color.R, m_grid_color.G, m_grid_color.B, m_grid_color.A);
114 
115  const size_t nGrids = m_grid_cubes.size();
116  for (size_t i = 0; i < nGrids; i++)
117  {
118  const TGridCube& c = m_grid_cubes[i];
119 
120  const GLdouble vertices[8 * 3] = {
121  c.max.x, c.max.y, c.max.z, c.max.x, c.min.y, c.max.z,
122  c.max.x, c.min.y, c.min.z, c.max.x, c.max.y, c.min.z,
123  c.min.x, c.max.y, c.min.z, c.min.x, c.max.y, c.max.z,
124  c.min.x, c.min.y, c.max.z, c.min.x, c.min.y, c.min.z};
125  glVertexPointer(3, GL_DOUBLE, 0, vertices);
127  GL_LINES,
128  sizeof(grid_line_indices) / sizeof(grid_line_indices[0]),
129  GL_UNSIGNED_BYTE, grid_line_indices);
130  }
131 
132  glEnable(GL_LIGHTING); // Disable lights when drawing lines
133  // End of antialiasing:
134  glPopAttrib();
135  }
136 
137  // Draw cubes ====================================
138  if (!m_enable_lighting) glDisable(GL_LIGHTING);
139 
141 
142  glNormalPointer(GL_FLOAT, 0, normals_cube);
143 
144  if (m_enable_cube_transparency)
145  {
148  }
149 
150  if (m_showVoxelsAsPoints)
151  {
152  glPointSize(m_showVoxelsAsPointsSize);
154  }
155 
156  for (size_t i = 0; i < m_voxel_sets.size(); i++)
157  {
158  if (!m_voxel_sets[i].visible) continue;
159 
160  const std::vector<TVoxel>& voxels = m_voxel_sets[i].voxels;
161  const size_t N = voxels.size();
162  for (size_t j = 0; j < N; j++)
163  {
164  const mrpt::img::TColor& vx_j_col = voxels[j].color;
165  glColor4ub(vx_j_col.R, vx_j_col.G, vx_j_col.B, vx_j_col.A);
166 
167  const mrpt::math::TPoint3D& c = voxels[j].coords;
168  const double L = voxels[j].side_length * 0.5;
169 
170  if (!m_showVoxelsAsPoints)
171  {
172  // Render as cubes:
173  const GLdouble vertices[8 * 3] = {
174  c.x + L, c.y + L, c.z + L, c.x + L, c.y - L, c.z + L,
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  glVertexPointer(3, GL_DOUBLE, 0, vertices);
180  GL_TRIANGLES,
181  sizeof(cube_indices) / sizeof(cube_indices[0]),
182  GL_UNSIGNED_BYTE, cube_indices);
183  }
184  else
185  {
186  // Render as simple points:
187  glVertex3f(c.x, c.y, c.z);
188  }
189  }
190  }
191 
192  if (m_showVoxelsAsPoints)
193  {
194  glEnd(); // of GL_POINTS
195  }
196 
197  if (m_enable_cube_transparency) glDisable(GL_BLEND);
198 
200 
201  if (!m_enable_lighting) glEnable(GL_LIGHTING);
202 
205 
206 #endif
207 }
208 
212 
213 namespace mrpt::opengl
214 {
217 {
218  out << a.visible << a.voxels;
219  return out;
220 }
222 {
223  in >> a.visible >> a.voxels;
224  return in;
225 }
226 
228 {
229  out << a.min << a.max;
230  return out;
231 }
233 {
234  in >> a.min >> a.max;
235  return in;
236 }
237 
239 {
240  out << a.coords << a.side_length << a.color;
241  return out;
242 }
244 {
245  in >> a.coords >> a.side_length >> a.color;
246  return in;
247 }
248 } // end of namespace mrpt::opengl
249 
252 {
253  writeToStreamRender(out);
254 
255  out << m_voxel_sets << m_grid_cubes << m_bb_min << m_bb_max
256  << m_enable_lighting << m_showVoxelsAsPoints << m_showVoxelsAsPointsSize
257  << m_show_grids << m_grid_width << m_grid_color
258  << m_enable_cube_transparency // added in v1
259  << uint32_t(m_visual_mode); // added in v2
260 }
261 
263 {
264  switch (version)
265  {
266  case 0:
267  case 1:
268  case 2:
269  {
270  readFromStreamRender(in);
271 
272  in >> m_voxel_sets >> m_grid_cubes >> m_bb_min >> m_bb_max >>
273  m_enable_lighting >> m_showVoxelsAsPoints >>
274  m_showVoxelsAsPointsSize >> m_show_grids >> m_grid_width >>
275  m_grid_color;
276 
277  if (version >= 1)
278  in >> m_enable_cube_transparency;
279  else
280  m_enable_cube_transparency = false;
281 
282  if (version >= 2)
283  {
284  uint32_t i;
285  in >> i;
286  m_visual_mode =
287  static_cast<COctoMapVoxels::visualization_mode_t>(i);
288  }
289  else
290  m_visual_mode = COctoMapVoxels::COLOR_FROM_OCCUPANCY;
291  }
292  break;
293  default:
295  };
296 
298 }
299 
301  mrpt::math::TPoint3D& bb_min, mrpt::math::TPoint3D& bb_max) const
302 {
303  bb_min = m_bb_min;
304  bb_max = m_bb_max;
305 
306  // Convert to coordinates of my parent:
307  m_pose.composePoint(bb_min, bb_min);
308  m_pose.composePoint(bb_max, bb_max);
309 }
310 
313 {
314  return a.coords.z < b.coords.z;
315 }
316 
318 {
319  for (size_t i = 0; i < m_voxel_sets.size(); i++)
320  {
321  std::sort(
322  m_voxel_sets[i].voxels.begin(), m_voxel_sets[i].voxels.end(),
323  &sort_voxels_z);
324  }
325 }
GLAPI void GLAPIENTRY glDisableClientState(GLenum array)
GLAPI void GLAPIENTRY glEnableClientState(GLenum array)
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
mrpt::serialization::CArchive & operator<<(mrpt::serialization::CArchive &out, const mrpt::opengl::CLight &o)
Definition: CLight.cpp:128
#define IMPLEMENTS_SERIALIZABLE(class_name, base, NameSpace)
This must be inserted in all CSerializable classes implementation files.
void serializeFrom(mrpt::serialization::CArchive &in, uint8_t serial_version) override
Pure virtual method for reading (deserializing) from an abstract archive.
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.
uint8_t B
Definition: TColor.h:46
#define GL_UNSIGNED_BYTE
Definition: glew.h:302
#define GL_ONE_MINUS_SRC_ALPHA
Definition: glew.h:287
uint8_t G
Definition: TColor.h:46
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)
unsigned char uint8_t
Definition: rptypes.h:41
A renderizable object suitable for rendering with OpenGL&#39;s display lists.
#define MRPT_THROW_UNKNOWN_SERIALIZATION_VERSION(__V)
For use in CSerializable implementations.
Definition: exceptions.h:90
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 provides a set of functions for maths stuff.
#define GL_LINE_SMOOTH
Definition: glew.h:367
#define GL_NORMAL_ARRAY
Definition: glew.h:725
const GLubyte * c
Definition: glext.h:6313
void render_dl() const override
Render.
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
GLAPI void GLAPIENTRY glVertex3f(GLfloat x, GLfloat y, GLfloat z)
uint8_t R
Definition: TColor.h:46
GLAPI void GLAPIENTRY glColor4ub(GLubyte red, GLubyte green, GLubyte blue, GLubyte alpha)
The info of each grid block.
void serializeTo(mrpt::serialization::CArchive &out) const override
Pure virtual method for writing (serializing) to an abstract archive.
GLAPI void GLAPIENTRY glDrawElements(GLenum mode, GLsizei count, GLenum type, const GLvoid *indices)
uint8_t serializeGetVersion() const override
Must return the current versioning number of the object.
#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)
Virtual base class for "archives": classes abstracting I/O streams.
Definition: CArchive.h:52
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:86
void checkOpenGLError()
Checks glGetError and throws an exception if an error situation is found.
Definition: gl_utils.cpp:143
GLuint in
Definition: glext.h:7274
The namespace for 3D scene representation and rendering.
Definition: CGlCanvasBase.h:15
GLAPI void GLAPIENTRY glEnd(void)
mrpt::serialization::CArchive & operator>>(mrpt::serialization::CArchive &in, mrpt::opengl::CLight &o)
Definition: CLight.cpp:122
#define GL_DOUBLE
Definition: glew.h:311
#define GL_FLOAT
Definition: glew.h:307
#define GL_LINES
Definition: glew.h:273
A RGB color - 8bit.
Definition: TColor.h:20
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
unsigned char GLubyte
Definition: glew.h:214
#define GL_LINE_BIT
Definition: glew.h:253
uint8_t A
Definition: TColor.h:46



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: 7d5e6d718 Fri Aug 24 01:51:28 2018 +0200 at lun nov 2 08:35:50 CET 2020