Main MRPT website > C++ reference for MRPT 1.5.6
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 
18 using namespace mrpt;
19 using namespace mrpt::opengl;
20 using namespace mrpt::utils;
21 using namespace mrpt::math;
22 using namespace std;
23 
25 
26 /** Ctor */
28  m_enable_lighting(false),
29  m_enable_cube_transparency(true),
30  m_showVoxelsAsPoints(false),
31  m_showVoxelsAsPointsSize(3.0f),
32  m_show_grids (false),
33  m_grid_width (1.0f),
34  m_grid_color (0xE0,0xE0,0xE0, 0x90),
35  m_visual_mode (COctoMapVoxels::COLOR_FROM_OCCUPANCY)
36 {
37 }
38 
39 /** Clears everything */
41 {
42  m_voxel_sets.clear();
43  m_grid_cubes.clear();
44 
46 }
47 
49 {
50  m_bb_min = bb_min;
51  m_bb_max = bb_max;
52 }
53 
54 
55 #if MRPT_HAS_OPENGL_GLUT
56 
57 // See: http://www.songho.ca/opengl/gl_vertexarray.html
58 
59 // cube ///////////////////////////////////////////////////////////////////////
60 // v6----- v5
61 // +Z /| /|
62 // A v1------v0|
63 // | | | | |
64 // | | |v7---|-|v4 / -X
65 // | |/ |/ /
66 // | v2------v3 L +X
67 // ----------------------> +Y
68 //
69 
70 const GLubyte grid_line_indices[] = {
71  0,1, 1,2, 2,3, 3,0,
72  4,5, 5,6, 6,7, 7,4,
73  0,5, 1,6, 2,7, 3,4
74  };
75 
76 const GLubyte cube_indices[36] = {
77  0,1,2, 2,3,0,
78  0,3,4, 4,5,0,
79  0,5,6, 6,1,0,
80  1,6,7, 7,2,1,
81  7,4,3, 3,2,7,
82  4,7,6, 6,5,4};
83 
84 // normal array
85 const GLfloat normals_cube[3*6*4] = {
86  1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, // v0,v1,v2,v3 (front)
87  0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, // v0,v3,v4,v5 (right)
88  0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, // v0,v5,v6,v1 (top)
89  0,-1, 0, 0,-1, 0, 0,-1, 0, 0,-1, 0, // v1,v6,v7,v2 (left)
90  0, 0,-1, 0, 0,-1, 0, 0,-1, 0, 0,-1, // v7,v4,v3,v2 (bottom)
91  -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0 }; // v4,v7,v6,v5 (back)
92 
93 #endif
94 
95 /*---------------------------------------------------------------
96  render
97  ---------------------------------------------------------------*/
99 {
100 #if MRPT_HAS_OPENGL_GLUT
101 
103 
104  // Draw grids ====================================
105  if (m_show_grids)
106  {
107  glLineWidth(m_grid_width);
109 
110  glDisable(GL_LIGHTING); // Disable lights when drawing lines
111 
112  // Antialiasing:
115  if ( m_grid_color.A != 255 )
116  {
119  }
120 
121  glColor4ub(m_grid_color.R,m_grid_color.G,m_grid_color.B,m_grid_color.A);
122 
123  const size_t nGrids = m_grid_cubes.size();
124  for (size_t i=0;i<nGrids;i++)
125  {
126  const TGridCube &c = m_grid_cubes[i];
127 
128  const GLdouble vertices[8*3] = {
129  c.max.x,c.max.y,c.max.z,
130  c.max.x,c.min.y,c.max.z,
131  c.max.x,c.min.y,c.min.z,
132  c.max.x,c.max.y,c.min.z,
133  c.min.x,c.max.y,c.min.z,
134  c.min.x,c.max.y,c.max.z,
135  c.min.x,c.min.y,c.max.z,
136  c.min.x,c.min.y,c.min.z
137  };
138  glVertexPointer(3, GL_DOUBLE, 0, vertices);
139  glDrawElements(GL_LINES, sizeof(grid_line_indices)/sizeof(grid_line_indices[0]), GL_UNSIGNED_BYTE, grid_line_indices);
140  }
141 
142  glEnable(GL_LIGHTING); // Disable lights when drawing lines
143  // End of antialiasing:
144  glPopAttrib();
145  }
146 
147  // Draw cubes ====================================
148  if (!m_enable_lighting) glDisable(GL_LIGHTING);
149 
151 
152  glNormalPointer(GL_FLOAT, 0, normals_cube);
153 
154  if (m_enable_cube_transparency)
155  {
158  }
159 
160  if (m_showVoxelsAsPoints)
161  {
162  glPointSize(m_showVoxelsAsPointsSize);
163  glBegin( GL_POINTS );
164  }
165 
166  for (size_t i=0;i<m_voxel_sets.size();i++)
167  {
168  if (!m_voxel_sets[i].visible) continue;
169 
170  const std::vector<TVoxel> & voxels = m_voxel_sets[i].voxels;
171  const size_t N = voxels.size();
172  for (size_t j=0;j<N;j++)
173  {
174  const mrpt::utils::TColor &vx_j_col = voxels[j].color;
175  glColor4ub(vx_j_col.R, vx_j_col.G, vx_j_col.B, vx_j_col.A);
176 
177  const mrpt::math::TPoint3D &c = voxels[j].coords;
178  const double L = voxels[j].side_length * 0.5;
179 
180  if (!m_showVoxelsAsPoints)
181  {
182  // Render as cubes:
183  const GLdouble vertices[8*3] = {
184  c.x+L,c.y+L,c.z+L,
185  c.x+L,c.y-L,c.z+L,
186  c.x+L,c.y-L,c.z-L,
187  c.x+L,c.y+L,c.z-L,
188  c.x-L,c.y+L,c.z-L,
189  c.x-L,c.y+L,c.z+L,
190  c.x-L,c.y-L,c.z+L,
191  c.x-L,c.y-L,c.z-L
192  };
193  glVertexPointer(3, GL_DOUBLE, 0, vertices);
194  glDrawElements(GL_TRIANGLES, sizeof(cube_indices)/sizeof(cube_indices[0]), GL_UNSIGNED_BYTE, cube_indices);
195  }
196  else
197  {
198  // Render as simple points:
199  glVertex3f(c.x,c.y,c.z);
200  }
201  }
202  }
203 
204 
205  if (m_showVoxelsAsPoints)
206  {
207  glEnd(); // of GL_POINTS
208  }
209 
210  if (m_enable_cube_transparency)
212 
214 
215  if (!m_enable_lighting) glEnable(GL_LIGHTING);
216 
219 
220 #endif
221 }
222 
226 
227 namespace mrpt{
228  namespace utils
229  {
231  out << a.visible << a.voxels;
232  return out;
233  }
235  in >> a.visible >> a.voxels;
236  return in;
237  }
238 
240  out << a.min << a.max;
241  return out;
242  }
244  in >> a.min >> a.max;
245  return in;
246  }
247 
249  out << a.coords << a.side_length << a.color;
250  return out;
251  }
253  in >> a.coords >> a.side_length >> a.color;
254  return in;
255  }
256 
257 }
258 }
259 
260 
261 /*---------------------------------------------------------------
262  Implements the writing to a CStream capability of
263  CSerializable objects
264  ---------------------------------------------------------------*/
266 {
267  if (version) *version=2;
268  else
269  {
270  writeToStreamRender(out);
271 
272  out << m_voxel_sets
273  << m_grid_cubes
274  << m_bb_min << m_bb_max
275  << m_enable_lighting << m_showVoxelsAsPoints << m_showVoxelsAsPointsSize
276  << m_show_grids << m_grid_width << m_grid_color
277  << m_enable_cube_transparency // added in v1
278  << uint32_t(m_visual_mode); // added in v2
279  }
280 }
281 
282 /*---------------------------------------------------------------
283  Implements the reading from a CStream capability of
284  CSerializable objects
285  ---------------------------------------------------------------*/
287 {
288  switch(version)
289  {
290  case 0:
291  case 1:
292  case 2:
293  {
294  readFromStreamRender(in);
295 
296  in >> m_voxel_sets
297  >> m_grid_cubes
298  >> m_bb_min >> m_bb_max
299  >> m_enable_lighting >> m_showVoxelsAsPoints >> m_showVoxelsAsPointsSize
300  >> m_show_grids >> m_grid_width >> m_grid_color;
301 
302  if (version>=1)
303  in >> m_enable_cube_transparency;
304  else m_enable_cube_transparency = false;
305 
306  if (version>=2)
307  {
308  uint32_t i;
309  in >> i;
310  m_visual_mode = static_cast<COctoMapVoxels::visualization_mode_t>(i);
311  }
312  else m_visual_mode = COctoMapVoxels::COLOR_FROM_OCCUPANCY;
313  }
314  break;
315  default:
317 
318  };
319 
321 }
322 
324 {
325  bb_min = m_bb_min;
326  bb_max = m_bb_max;
327 
328  // Convert to coordinates of my parent:
329  m_pose.composePoint(bb_min, bb_min);
330  m_pose.composePoint(bb_max, bb_max);
331 }
332 
334 {
335  return a.coords.z < b.coords.z;
336 }
337 
339 {
340  for (size_t i=0;i<m_voxel_sets.size();i++)
341  {
342  std::sort( m_voxel_sets[i].voxels.begin(),m_voxel_sets[i].voxels.end(), &sort_voxels_z );
343  }
344 }
GLAPI void GLAPIENTRY glDisableClientState(GLenum array)
GLboolean GLboolean GLboolean GLboolean a
Definition: glew.h:5406
GLAPI void GLAPIENTRY glEnableClientState(GLenum array)
void getBoundingBox(mrpt::math::TPoint3D &bb_min, mrpt::math::TPoint3D &bb_max) const MRPT_OVERRIDE
Evaluates the bounding box of this object (including possible children) in the coordinate frame of th...
OPENGL_IMPEXP 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...
GLAPI void GLAPIENTRY glEnable(GLenum cap)
GLAPI void GLAPIENTRY glPointSize(GLfloat size)
#define GL_VERTEX_ARRAY
Definition: glew.h:720
::mrpt::utils::CStream & operator>>(mrpt::utils::CStream &in, CAngularObservationMeshPtr &pObj)
#define IMPLEMENTS_SERIALIZABLE(class_name, base, NameSpace)
This must be inserted in all CSerializable classes implementation files.
const GLfloat * c
Definition: glew.h:10088
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)
#define GL_TRIANGLES
Definition: glew.h:272
void readFromStream(mrpt::utils::CStream &in, int version)
Introduces a pure virtual method responsible for loading from a CStream This can not be used directly...
EIGEN_STRONG_INLINE void notifyChange() const
Must be called to notify that the object has changed (so, the display list must be updated) ...
#define GL_UNSIGNED_BYTE
Definition: glew.h:298
#define GL_ONE_MINUS_SRC_ALPHA
Definition: glew.h:283
double z
X,Y,Z coordinates.
void clear()
Clears everything.
GLAPI void GLAPIENTRY glPopAttrib(void)
#define GL_LIGHTING
Definition: glew.h:381
bool sort_voxels_z(const COctoMapVoxels::TVoxel &a, const COctoMapVoxels::TVoxel &b)
GLAPI void GLAPIENTRY glLineWidth(GLfloat width)
GLuint in
Definition: glew.h:7146
GLAPI void GLAPIENTRY glBlendFunc(GLenum sfactor, GLenum dfactor)
void render_dl() const MRPT_OVERRIDE
Render.
A renderizable object suitable for rendering with OpenGL'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:261
This base class is used to provide a unified interface to files,memory buffers,..Please see the deriv...
Definition: CStream.h:38
#define GL_LINE_SMOOTH
Definition: glew.h:363
#define GL_NORMAL_ARRAY
Definition: glew.h:721
#define MRPT_THROW_UNKNOWN_SERIALIZATION_VERSION(__V)
For use in CSerializable implementations.
double GLdouble
Definition: glew.h:215
A RGB color - 8bit.
Definition: TColor.h:26
GLAPI void GLAPIENTRY glVertexPointer(GLint size, GLenum type, GLsizei stride, const GLvoid *pointer)
int version
Definition: mrpt_jpeglib.h:898
GLAPI void GLAPIENTRY glBegin(GLenum mode)
#define GL_BLEND
Definition: glew.h:428
#define GL_POINTS
Definition: glew.h:268
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.
float GLfloat
Definition: glew.h:213
GLAPI void GLAPIENTRY glDrawElements(GLenum mode, GLsizei count, GLenum type, const GLvoid *indices)
#define GL_SRC_ALPHA
Definition: glew.h:282
The info of each of the voxels.
Color goes from black (occupied voxel) to the chosen color (free voxel)
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:57
mrpt::math::TPoint3D max
opposite corners of the cube
void OPENGL_IMPEXP checkOpenGLError()
Checks glGetError and throws an exception if an error situation is found.
Definition: gl_utils.cpp:134
GLAPI void GLAPIENTRY glEnd(void)
#define GL_DOUBLE
Definition: glew.h:307
#define GL_FLOAT
Definition: glew.h:303
#define GL_LINES
Definition: glew.h:269
GLdouble GLdouble GLdouble b
Definition: glew.h:5092
Lightweight 3D point.
unsigned __int32 uint32_t
Definition: rptypes.h:49
GLAPI void GLAPIENTRY glDisable(GLenum cap)
void writeToStream(mrpt::utils::CStream &out, int *getVersion) const
Introduces a pure virtual method responsible for writing to a CStream.
unsigned char GLubyte
Definition: glew.h:210
#define GL_LINE_BIT
Definition: glew.h:249
GLdouble GLdouble GLdouble GLdouble GLdouble GLdouble f
Definition: glew.h:5092



Page generated by Doxygen 1.8.6 for MRPT 1.5.6 Git: 4c65e84 Tue Apr 24 08:18:17 2018 +0200 at mar abr 24 08:26:17 CEST 2018