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-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
214 {
215 namespace opengl
216 {
219 {
220  out << a.visible << a.voxels;
221  return out;
222 }
224 {
225  in >> a.visible >> a.voxels;
226  return in;
227 }
228 
230 {
231  out << a.min << a.max;
232  return out;
233 }
235 {
236  in >> a.min >> a.max;
237  return in;
238 }
239 
241 {
242  out << a.coords << a.side_length << a.color;
243  return out;
244 }
246 {
247  in >> a.coords >> a.side_length >> a.color;
248  return in;
249 }
250 } // namespace opengl
251 } // namespace mrpt
252 
255 {
256  writeToStreamRender(out);
257 
258  out << m_voxel_sets << m_grid_cubes << m_bb_min << m_bb_max
259  << m_enable_lighting << m_showVoxelsAsPoints << m_showVoxelsAsPointsSize
260  << m_show_grids << m_grid_width << m_grid_color
261  << m_enable_cube_transparency // added in v1
262  << uint32_t(m_visual_mode); // added in v2
263 }
264 
266 {
267  switch (version)
268  {
269  case 0:
270  case 1:
271  case 2:
272  {
273  readFromStreamRender(in);
274 
275  in >> m_voxel_sets >> m_grid_cubes >> m_bb_min >> m_bb_max >>
276  m_enable_lighting >> m_showVoxelsAsPoints >>
277  m_showVoxelsAsPointsSize >> m_show_grids >> m_grid_width >>
278  m_grid_color;
279 
280  if (version >= 1)
281  in >> m_enable_cube_transparency;
282  else
283  m_enable_cube_transparency = false;
284 
285  if (version >= 2)
286  {
287  uint32_t i;
288  in >> i;
289  m_visual_mode =
290  static_cast<COctoMapVoxels::visualization_mode_t>(i);
291  }
292  else
293  m_visual_mode = COctoMapVoxels::COLOR_FROM_OCCUPANCY;
294  }
295  break;
296  default:
298  };
299 
301 }
302 
304  mrpt::math::TPoint3D& bb_min, mrpt::math::TPoint3D& bb_max) const
305 {
306  bb_min = m_bb_min;
307  bb_max = m_bb_max;
308 
309  // Convert to coordinates of my parent:
310  m_pose.composePoint(bb_min, bb_min);
311  m_pose.composePoint(bb_max, bb_max);
312 }
313 
316 {
317  return a.coords.z < b.coords.z;
318 }
319 
321 {
322  for (size_t i = 0; i < m_voxel_sets.size(); i++)
323  {
324  std::sort(
325  m_voxel_sets[i].voxels.begin(), m_voxel_sets[i].voxels.end(),
326  &sort_voxels_z);
327  }
328 }
mrpt::opengl::COctoMapVoxels::TGridCube::max
mrpt::math::TPoint3D max
Definition: COctoMapVoxels.h:116
glBegin
GLAPI void GLAPIENTRY glBegin(GLenum mode)
mrpt::opengl::COctoMapVoxels::COLOR_FROM_OCCUPANCY
@ COLOR_FROM_OCCUPANCY
Color goes from black (occupied voxel) to the chosen color (free voxel)
Definition: COctoMapVoxels.h:83
mrpt::opengl::COctoMapVoxels::serializeFrom
void serializeFrom(mrpt::serialization::CArchive &in, uint8_t serial_version) override
Pure virtual method for reading (deserializing) from an abstract archive.
Definition: COctoMapVoxels.cpp:265
COctoMapVoxels.h
GL_SRC_ALPHA
#define GL_SRC_ALPHA
Definition: glew.h:286
GL_DOUBLE
#define GL_DOUBLE
Definition: glew.h:311
mrpt::opengl::operator>>
mrpt::serialization::CArchive & operator>>(mrpt::serialization::CArchive &in, mrpt::opengl::CLight &o)
Definition: CLight.cpp:124
GL_VERTEX_ARRAY
#define GL_VERTEX_ARRAY
Definition: glew.h:724
glNormalPointer
GLAPI void GLAPIENTRY glNormalPointer(GLenum type, GLsizei stride, const GLvoid *pointer)
c
const GLubyte * c
Definition: glext.h:6313
GL_BLEND
#define GL_BLEND
Definition: glew.h:432
glColor4ub
GLAPI void GLAPIENTRY glColor4ub(GLubyte red, GLubyte green, GLubyte blue, GLubyte alpha)
mrpt::opengl::COctoMapVoxels::TGridCube
The info of each grid block.
Definition: COctoMapVoxels.h:113
GL_LINE_SMOOTH
#define GL_LINE_SMOOTH
Definition: glew.h:367
mrpt::opengl::CRenderizableDisplayList
A renderizable object suitable for rendering with OpenGL's display lists.
Definition: CRenderizableDisplayList.h:39
glDisable
GLAPI void GLAPIENTRY glDisable(GLenum cap)
mrpt::opengl::CRenderizableDisplayList::notifyChange
EIGEN_STRONG_INLINE void notifyChange() const
Must be called to notify that the object has changed (so, the display list must be updated)
Definition: CRenderizableDisplayList.h:57
mrpt::img::TColor::R
uint8_t R
Definition: TColor.h:48
glEnable
GLAPI void GLAPIENTRY glEnable(GLenum cap)
stl_serialization.h
mrpt
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
Definition: CKalmanFilterCapable.h:30
glDisableClientState
GLAPI void GLAPIENTRY glDisableClientState(GLenum array)
uint8_t
unsigned char uint8_t
Definition: rptypes.h:41
mrpt::opengl::COctoMapVoxels::serializeTo
void serializeTo(mrpt::serialization::CArchive &out) const override
Pure virtual method for writing (serializing) to an abstract archive.
Definition: COctoMapVoxels.cpp:254
glVertex3f
GLAPI void GLAPIENTRY glVertex3f(GLfloat x, GLfloat y, GLfloat z)
glVertexPointer
GLAPI void GLAPIENTRY glVertexPointer(GLint size, GLenum type, GLsizei stride, const GLvoid *pointer)
mrpt::opengl::COctoMapVoxels::render_dl
void render_dl() const override
Render.
Definition: COctoMapVoxels.cpp:89
mrpt::serialization::CArchive
Virtual base class for "archives": classes abstracting I/O streams.
Definition: CArchive.h:48
glEnd
GLAPI void GLAPIENTRY glEnd(void)
mrpt::opengl::COctoMapVoxels::clear
void clear()
Clears everything.
Definition: COctoMapVoxels.cpp:38
mrpt::img::TColor::B
uint8_t B
Definition: TColor.h:48
GLubyte
unsigned char GLubyte
Definition: glew.h:214
mrpt::opengl::COctoMapVoxels::visualization_mode_t
visualization_mode_t
The different coloring schemes, which modulate the generic mrpt::opengl::CRenderizable object color.
Definition: COctoMapVoxels.h:76
GL_LINE_BIT
#define GL_LINE_BIT
Definition: glew.h:253
glDrawElements
GLAPI void GLAPIENTRY glDrawElements(GLenum mode, GLsizei count, GLenum type, const GLvoid *indices)
mrpt::opengl::gl_utils::checkOpenGLError
void checkOpenGLError()
Checks glGetError and throws an exception if an error situation is found.
Definition: gl_utils.cpp:143
GL_LIGHTING
#define GL_LIGHTING
Definition: glew.h:385
GL_TRIANGLES
#define GL_TRIANGLES
Definition: glew.h:276
mrpt::opengl::COctoMapVoxels::sort_voxels_by_z
void sort_voxels_by_z()
Definition: COctoMapVoxels.cpp:320
glLineWidth
GLAPI void GLAPIENTRY glLineWidth(GLfloat width)
mrpt::opengl::COctoMapVoxels::setBoundingBox
void setBoundingBox(const mrpt::math::TPoint3D &bb_min, const mrpt::math::TPoint3D &bb_max)
Manually changes the bounding box (normally the user doesn't need to call this)
Definition: COctoMapVoxels.cpp:46
GL_ONE_MINUS_SRC_ALPHA
#define GL_ONE_MINUS_SRC_ALPHA
Definition: glew.h:287
glPushAttrib
GLAPI void GLAPIENTRY glPushAttrib(GLbitfield mask)
mrpt::math::TPoint3D::x
double x
X,Y,Z coordinates.
Definition: lightweight_geom_data.h:385
b
GLubyte GLubyte b
Definition: glext.h:6279
mrpt::opengl::COctoMapVoxels::getBoundingBox
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...
Definition: COctoMapVoxels.cpp:303
mrpt::img::TColor
A RGB color - 8bit.
Definition: TColor.h:22
GLdouble
double GLdouble
Definition: glew.h:219
GL_POINTS
#define GL_POINTS
Definition: glew.h:272
glPointSize
GLAPI void GLAPIENTRY glPointSize(GLfloat size)
sort_voxels_z
bool sort_voxels_z(const COctoMapVoxels::TVoxel &a, const COctoMapVoxels::TVoxel &b)
Definition: COctoMapVoxels.cpp:314
mrpt::img::TColor::A
uint8_t A
Definition: TColor.h:48
mrpt::opengl::COctoMapVoxels::TInfoPerVoxelSet
Definition: COctoMapVoxels.h:126
DECLARE_CUSTOM_TTYPENAME
#define DECLARE_CUSTOM_TTYPENAME(_TYPE)
Identical to MRPT_DECLARE_TTYPENAME but intended for user code.
Definition: TTypeName.h:86
IMPLEMENTS_SERIALIZABLE
#define IMPLEMENTS_SERIALIZABLE(class_name, base, NameSpace)
This must be inserted in all CSerializable classes implementation files.
Definition: CSerializable.h:114
glEnableClientState
GLAPI void GLAPIENTRY glEnableClientState(GLenum array)
GL_LINES
#define GL_LINES
Definition: glew.h:273
mrpt::opengl::COctoMapVoxels
A flexible renderer of voxels, typically from a 3D octo map (see mrpt::maps::COctoMap).
Definition: COctoMapVoxels.h:69
mrpt::opengl::operator<<
mrpt::serialization::CArchive & operator<<(mrpt::serialization::CArchive &out, const mrpt::opengl::CLight &o)
Definition: CLight.cpp:130
mrpt::math::TPoint3D
Lightweight 3D point.
Definition: lightweight_geom_data.h:378
opengl-precomp.h
GL_NORMAL_ARRAY
#define GL_NORMAL_ARRAY
Definition: glew.h:725
GL_FLOAT
#define GL_FLOAT
Definition: glew.h:307
opengl_internals.h
glPopAttrib
GLAPI void GLAPIENTRY glPopAttrib(void)
mrpt::img::TColor::G
uint8_t G
Definition: TColor.h:48
mrpt::math
This base provides a set of functions for maths stuff.
Definition: math/include/mrpt/math/bits_math.h:13
mrpt::opengl::COctoMapVoxels::serializeGetVersion
uint8_t serializeGetVersion() const override
Must return the current versioning number of the object.
Definition: COctoMapVoxels.cpp:253
GLfloat
float GLfloat
Definition: glew.h:217
in
GLuint in
Definition: glext.h:7274
CArchive.h
MRPT_THROW_UNKNOWN_SERIALIZATION_VERSION
#define MRPT_THROW_UNKNOWN_SERIALIZATION_VERSION(__V)
For use in CSerializable implementations.
Definition: exceptions.h:90
mrpt::opengl
The namespace for 3D scene representation and rendering.
Definition: CGlCanvasBase.h:15
GL_UNSIGNED_BYTE
#define GL_UNSIGNED_BYTE
Definition: glew.h:302
uint32_t
unsigned __int32 uint32_t
Definition: rptypes.h:47
mrpt::opengl::COctoMapVoxels::TVoxel
The info of each of the voxels.
Definition: COctoMapVoxels.h:97
glBlendFunc
GLAPI void GLAPIENTRY glBlendFunc(GLenum sfactor, GLenum dfactor)
a
GLubyte GLubyte GLubyte a
Definition: glext.h:6279
GL_COLOR_BUFFER_BIT
#define GL_COLOR_BUFFER_BIT
Definition: glew.h:265



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