Main MRPT website > C++ reference for MRPT 1.9.9
COctoMapVoxels.h
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 #ifndef opengl_COctoMapVoxels_H
11 #define opengl_COctoMapVoxels_H
12 
15 
16 namespace mrpt
17 {
18 namespace opengl
19 {
21 {
24 };
25 
26 /** A flexible renderer of voxels, typically from a 3D octo map (see
27  *mrpt::maps::COctoMap).
28  * This class is sort of equivalent to octovis::OcTreeDrawer from the octomap
29  *package, but
30  * relying on MRPT's CRenderizableDisplayList so there's no need to manually
31  *cache the rendering of OpenGL primitives.
32  *
33  * Normally users call mrpt::maps::COctoMap::getAs3DObject() to obtain a
34  *generic mrpt::opengl::CSetOfObjects which insides holds an instance of
35  *COctoMapVoxels.
36  * You can also alternativelly call COctoMapVoxels::setFromOctoMap(), so you
37  *can tune the display parameters, colors, etc.
38  * As with any other mrpt::opengl class, all object coordinates refer to some
39  *frame of reference which is relative to the object parent and can be changed
40  *with mrpt::opengl::CRenderizable::setPose()
41  *
42  * This class draws these separate elements to represent an OctoMap:
43  * - A grid representation of all cubes, as simple lines (occupied/free,
44  *leafs/nodes,... whatever). See:
45  * - showGridLines()
46  * - setGridLinesColor()
47  * - setGridLinesWidth()
48  * - push_back_GridCube()
49  * - A number of <b>voxel collections</b>, drawn as cubes each having a
50  *different color (e.g. depending on the color scheme in the original
51  *mrpt::maps::COctoMap object).
52  * The meanning of each collection is user-defined, but you can use the
53  *constants VOXEL_SET_OCCUPIED, VOXEL_SET_FREESPACE for predefined meanings.
54  * - showVoxels()
55  * - push_back_Voxel()
56  *
57  * Several coloring schemes can be selected with setVisualizationMode(). See
58  *COctoMapVoxels::visualization_mode_t
59  *
60  * <div align="center">
61  * <table border="0" cellspan="4" cellspacing="4" style="border-width: 1px;
62  *border-style: solid;">
63  * <tr> <td> mrpt::opengl::COctoMapVoxels </td> <td> \image html
64  *preview_COctoMapVoxels.png </td> </tr>
65  * </table>
66  * </div>
67  *
68  * \sa opengl::COpenGLScene
69  * \ingroup mrpt_opengl_grp
70  */
72 {
74  public:
75  /** The different coloring schemes, which modulate the generic
76  * mrpt::opengl::CRenderizable object color. Set with setVisualizationMode()
77  */
79  {
80  /** Color goes from black (at the bottom) to the chosen color (at the
81  top) */
83  /** Color goes from black (occupied voxel) to the chosen color (free
84  voxel) */
86  /** Transparency goes from opaque (occupied voxel) to transparent (free
87  voxel). */
89  /** Color goes from black (occupaid voxel) to the chosen color (free
90  voxel) and they are transparent */
92  /** Combination of COLOR_FROM_HEIGHT and TRANSPARENCY_FROM_OCCUPANCY */
94  /** All cubes are of identical color. */
95  FIXED
96  };
97 
98  /** The info of each of the voxels */
99  struct TVoxel
100  {
102  double side_length;
104 
105  TVoxel() {}
107  const mrpt::math::TPoint3D& coords_, const double side_length_,
108  mrpt::utils::TColor color_)
109  : coords(coords_), side_length(side_length_), color(color_)
110  {
111  }
112  };
113 
114  /** The info of each grid block */
115  struct TGridCube
116  {
117  /** opposite corners of the cube */
119 
122  const mrpt::math::TPoint3D& min_, const mrpt::math::TPoint3D& max_)
123  : min(min_), max(max_)
124  {
125  }
126  };
127 
129  {
130  bool visible;
131  std::vector<TVoxel> voxels;
132 
134  };
135 
136  protected:
137  std::deque<TInfoPerVoxelSet> m_voxel_sets;
138  std::vector<TGridCube> m_grid_cubes;
139 
140  /** Cached bounding boxes */
142 
151 
152  public:
153  /** Clears everything */
154  void clear();
155 
156  /** Select the visualization mode. To have any effect, this method has to be
157  * called before loading the octomap. */
159  {
162  }
164  {
165  return m_visual_mode;
166  }
167 
168  /** Can be used to enable/disable the effects of lighting in this object */
169  inline void enableLights(bool enable)
170  {
171  m_enable_lighting = enable;
173  }
174  inline bool areLightsEnabled() const { return m_enable_lighting; }
175  /** By default, the alpha (transparency) component of voxel cubes is taken
176  * into account, but transparency can be disabled with this method. */
177  inline void enableCubeTransparency(bool enable)
178  {
181  }
182  inline bool isCubeTransparencyEnabled() const
183  {
185  }
186 
187  /** Shows/hides the grid lines */
188  inline void showGridLines(bool show)
189  {
190  m_show_grids = show;
192  }
193  inline bool areGridLinesVisible() const { return m_show_grids; }
194  /** Shows/hides the voxels (voxel_set is a 0-based index for the set of
195  * voxels to modify, e.g. VOXEL_SET_OCCUPIED, VOXEL_SET_FREESPACE) */
196  inline void showVoxels(unsigned int voxel_set, bool show)
197  {
198  ASSERT_(voxel_set < m_voxel_sets.size())
199  m_voxel_sets[voxel_set].visible = show;
201  }
202  inline bool areVoxelsVisible(unsigned int voxel_set) const
203  {
204  ASSERT_(voxel_set < m_voxel_sets.size())
205  return m_voxel_sets[voxel_set].visible;
206  }
207 
208  /** For quick renders: render voxels as points instead of cubes. \sa
209  * setVoxelAsPointsSize */
210  inline void showVoxelsAsPoints(const bool enable)
211  {
212  m_showVoxelsAsPoints = enable;
214  }
215  inline bool areVoxelsShownAsPoints() const { return m_showVoxelsAsPoints; }
216  /** Only used when showVoxelsAsPoints() is enabled. */
217  inline void setVoxelAsPointsSize(float pointSize)
218  {
219  m_showVoxelsAsPointsSize = pointSize;
221  }
222  inline float getVoxelAsPointsSize() const
223  {
225  }
226 
227  /** Sets the width of grid lines */
228  inline void setGridLinesWidth(float w)
229  {
230  m_grid_width = w;
232  }
233  /** Gets the width of grid lines */
234  inline float getGridLinesWidth() const { return m_grid_width; }
236  {
239  }
241  {
242  return m_grid_color;
243  }
244 
245  /** Returns the total count of grid cubes. */
246  inline size_t getGridCubeCount() const { return m_grid_cubes.size(); }
247  /** Returns the number of voxel sets. */
248  inline size_t getVoxelSetCount() const { return m_voxel_sets.size(); }
249  /** Returns the total count of voxels in one voxel set. */
250  inline size_t getVoxelCount(const size_t set_index) const
251  {
252  ASSERT_(set_index < m_voxel_sets.size())
253  return m_voxel_sets[set_index].voxels.size();
254  }
255 
256  /** Manually changes the bounding box (normally the user doesn't need to
257  * call this) */
258  void setBoundingBox(
259  const mrpt::math::TPoint3D& bb_min, const mrpt::math::TPoint3D& bb_max);
260 
261  inline void resizeGridCubes(const size_t nCubes)
262  {
263  m_grid_cubes.resize(nCubes);
265  }
266  inline void resizeVoxelSets(const size_t nVoxelSets)
267  {
268  m_voxel_sets.resize(nVoxelSets);
270  }
271  inline void resizeVoxels(const size_t set_index, const size_t nVoxels)
272  {
273  ASSERT_(set_index < m_voxel_sets.size())
274  m_voxel_sets[set_index].voxels.resize(nVoxels);
276  }
277 
278  inline void reserveGridCubes(const size_t nCubes)
279  {
280  m_grid_cubes.reserve(nCubes);
281  }
282  inline void reserveVoxels(const size_t set_index, const size_t nVoxels)
283  {
284  ASSERT_(set_index < m_voxel_sets.size())
285  m_voxel_sets[set_index].voxels.reserve(nVoxels);
287  }
288 
289  inline TGridCube& getGridCubeRef(const size_t idx)
290  {
291  ASSERTDEB_(idx < m_grid_cubes.size())
293  return m_grid_cubes[idx];
294  }
295  inline const TGridCube& getGridCube(const size_t idx) const
296  {
297  ASSERTDEB_(idx < m_grid_cubes.size()) return m_grid_cubes[idx];
298  }
299 
300  inline TVoxel& getVoxelRef(const size_t set_index, const size_t idx)
301  {
302  ASSERTDEB_(
303  set_index < m_voxel_sets.size() &&
304  idx < m_voxel_sets[set_index].voxels.size())
306  return m_voxel_sets[set_index].voxels[idx];
307  }
308  inline const TVoxel& getVoxel(
309  const size_t set_index, const size_t idx) const
310  {
311  ASSERTDEB_(
312  set_index < m_voxel_sets.size() &&
313  idx < m_voxel_sets[set_index].voxels.size())
315  return m_voxel_sets[set_index].voxels[idx];
316  }
317 
318  inline void push_back_GridCube(const TGridCube& c)
319  {
321  m_grid_cubes.push_back(c);
322  }
323  inline void push_back_Voxel(const size_t set_index, const TVoxel& v)
324  {
325  ASSERTDEB_(set_index < m_voxel_sets.size())
327  m_voxel_sets[set_index].voxels.push_back(v);
328  }
329 
330  void sort_voxels_by_z();
331 
332  /** Render */
333  void render_dl() const override;
334 
335  /** Evaluates the bounding box of this object (including possible children)
336  * in the coordinate frame of the object parent. */
337  void getBoundingBox(
338  mrpt::math::TPoint3D& bb_min,
339  mrpt::math::TPoint3D& bb_max) const override;
340 
341  /** Sets the contents of the object from a mrpt::maps::COctoMap object.
342  * \tparam Typically, an mrpt::maps::COctoMap object
343  *
344  * \note Declared as a template because in the library [mrpt-opengl] we
345  * don't have access to the library [mrpt-maps].
346  */
347  template <class OCTOMAP>
348  void setFromOctoMap(OCTOMAP& m)
349  {
350  m.getAsOctoMapVoxels(*this);
351  }
352 
353  /** Constructor */
354  COctoMapVoxels();
355  /** Private, virtual destructor: only can be deleted from smart pointers. */
356  virtual ~COctoMapVoxels() {}
357 };
358 
359 } // end namespace
360 } // End of namespace
361 
362 #endif
#define DEFINE_SERIALIZABLE(class_name)
This declaration must be inserted in all CSerializable classes definition, within the class declarati...
A flexible renderer of voxels, typically from a 3D octo map (see mrpt::maps::COctoMap).
void showGridLines(bool show)
Shows/hides the grid lines.
visualization_mode_t getVisualizationMode() const
visualization_mode_t m_visual_mode
const mrpt::utils::TColor & getGridLinesColor() const
void setVoxelAsPointsSize(float pointSize)
Only used when showVoxelsAsPoints() is enabled.
const TGridCube & getGridCube(const size_t idx) const
void clear()
Clears everything.
void showVoxelsAsPoints(const bool enable)
For quick renders: render voxels as points instead of cubes.
void push_back_Voxel(const size_t set_index, const TVoxel &v)
void setFromOctoMap(OCTOMAP &m)
Sets the contents of the object from a mrpt::maps::COctoMap object.
bool areVoxelsVisible(unsigned int voxel_set) const
void resizeGridCubes(const size_t nCubes)
void enableLights(bool enable)
Can be used to enable/disable the effects of lighting in this object.
size_t getVoxelSetCount() const
Returns the number of voxel sets.
bool isCubeTransparencyEnabled() const
mrpt::utils::TColor m_grid_color
float getGridLinesWidth() const
Gets the width of grid lines.
TGridCube & getGridCubeRef(const size_t idx)
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)
mrpt::math::TPoint3D m_bb_max
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...
virtual ~COctoMapVoxels()
Private, virtual destructor: only can be deleted from smart pointers.
size_t getVoxelCount(const size_t set_index) const
Returns the total count of voxels in one voxel set.
void enableCubeTransparency(bool enable)
By default, the alpha (transparency) component of voxel cubes is taken into account,...
visualization_mode_t
The different coloring schemes, which modulate the generic mrpt::opengl::CRenderizable object color.
@ MIXED
Combination of COLOR_FROM_HEIGHT and TRANSPARENCY_FROM_OCCUPANCY.
@ TRANSPARENCY_FROM_OCCUPANCY
Transparency goes from opaque (occupied voxel) to transparent (free voxel).
@ TRANS_AND_COLOR_FROM_OCCUPANCY
Color goes from black (occupaid voxel) to the chosen color (free voxel) and they are transparent.
@ COLOR_FROM_OCCUPANCY
Color goes from black (occupied voxel) to the chosen color (free voxel)
@ COLOR_FROM_HEIGHT
Color goes from black (at the bottom) to the chosen color (at the top)
@ FIXED
All cubes are of identical color.
std::vector< TGridCube > m_grid_cubes
std::deque< TInfoPerVoxelSet > m_voxel_sets
void setGridLinesWidth(float w)
Sets the width of grid lines.
void reserveVoxels(const size_t set_index, const size_t nVoxels)
void setGridLinesColor(const mrpt::utils::TColor &color)
void setVisualizationMode(visualization_mode_t mode)
Select the visualization mode.
void showVoxels(unsigned int voxel_set, bool show)
Shows/hides the voxels (voxel_set is a 0-based index for the set of voxels to modify,...
void reserveGridCubes(const size_t nCubes)
mrpt::math::TPoint3D m_bb_min
Cached bounding boxes.
void render_dl() const override
Render.
const TVoxel & getVoxel(const size_t set_index, const size_t idx) const
void resizeVoxelSets(const size_t nVoxelSets)
void push_back_GridCube(const TGridCube &c)
TVoxel & getVoxelRef(const size_t set_index, const size_t idx)
void resizeVoxels(const size_t set_index, const size_t nVoxels)
size_t getGridCubeCount() const
Returns the total count of grid cubes.
A renderizable object suitable for rendering with OpenGL's display lists.
EIGEN_STRONG_INLINE void notifyChange() const
Must be called to notify that the object has changed (so, the display list must be updated)
const GLdouble * v
Definition: glext.h:3678
GLubyte GLubyte GLubyte GLubyte w
Definition: glext.h:4178
GLint mode
Definition: glext.h:5669
const GLubyte * c
Definition: glext.h:6313
GLuint color
Definition: glext.h:8300
const GLshort * coords
Definition: glext.h:7386
#define ASSERT_(f)
Definition: mrpt_macros.h:309
#define ASSERTDEB_(f)
Defines an assertion mechanism - only when compiled in debug.
Definition: mrpt_macros.h:355
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
Lightweight 3D point.
The info of each grid block.
TGridCube(const mrpt::math::TPoint3D &min_, const mrpt::math::TPoint3D &max_)
mrpt::math::TPoint3D min
opposite corners of the cube
The info of each of the voxels.
TVoxel(const mrpt::math::TPoint3D &coords_, const double side_length_, mrpt::utils::TColor color_)
A RGB color - 8bit.
Definition: TColor.h:26



Page generated by Doxygen 1.9.1 for MRPT 1.9.9 Git: 63ea9d1f1 Thu Nov 23 00:06:53 2017 +0100 at mar 26 may 2026 12:19:29 CEST