Main MRPT website > C++ reference
MRPT logo
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-2014, 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  using mrpt::utils::TColor;
22 
23 
25  {
28  };
29 
30  // This must be added to any CSerializable derived class:
31  DEFINE_SERIALIZABLE_PRE_CUSTOM_BASE_LINKAGE( COctoMapVoxels, CRenderizableDisplayList, OPENGL_IMPEXP )
32 
33  /** A flexible renderer of voxels, typically from a 3D octo map (see mrpt::slam::COctoMap).
34  * This class is sort of equivalent to octovis::OcTreeDrawer from the octomap package, but
35  * relying on MRPT's CRenderizableDisplayList so there's no need to manually cache the rendering of OpenGL primitives.
36  *
37  * Normally users call mrpt::slam::COctoMap::getAs3DObject() to obtain a generic mrpt::opengl::CSetOfObjects which insides holds an instance of COctoMapVoxels.
38  * You can also alternativelly call COctoMapVoxels::setFromOctoMap(), so you can tune the display parameters, colors, etc.
39  * As with any other mrpt::opengl class, all object coordinates refer to some frame of reference which is relative to the object parent and can be changed with mrpt::opengl::CRenderizable::setPose()
40  *
41  * This class draws these separate elements to represent an OctoMap:
42  * - A grid representation of all cubes, as simple lines (occupied/free, leafs/nodes,... whatever). See:
43  * - showGridLines()
44  * - setGridLinesColor()
45  * - setGridLinesWidth()
46  * - push_back_GridCube()
47  * - A number of <b>voxel collections</b>, drawn as cubes each having a different color (e.g. depending on the color scheme in the original mrpt::slam::COctoMap object).
48  * The meanning of each collection is user-defined, but you can use the constants VOXEL_SET_OCCUPIED, VOXEL_SET_FREESPACE for predefined meanings.
49  * - showVoxels()
50  * - push_back_Voxel()
51  *
52  * Several coloring schemes can be selected with setVisualizationMode(). See COctoMapVoxels::visualization_mode_t
53  *
54  * <div align="center">
55  * <table border="0" cellspan="4" cellspacing="4" style="border-width: 1px; border-style: solid;">
56  * <tr> <td> mrpt::opengl::COctoMapVoxels </td> <td> \image html preview_COctoMapVoxels.png </td> </tr>
57  * </table>
58  * </div>
59  *
60  * \sa opengl::COpenGLScene
61  * \ingroup mrpt_opengl_grp
62  */
64  {
66  public:
67 
68  /** The different coloring schemes, which modulate the generic mrpt::opengl::CRenderizable object color. Set with setVisualizationMode() */
70  {
71  COLOR_FROM_HEIGHT, //!< Color goes from black (at the bottom) to the chosen color (at the top)
72  COLOR_FROM_OCCUPANCY, //!< Color goes from black (occupied voxel) to the chosen color (free voxel)
73  TRANSPARENCY_FROM_OCCUPANCY, //!< Transparency goes from opaque (occupied voxel) to transparent (free voxel).
74  TRANS_AND_COLOR_FROM_OCCUPANCY, //!< Color goes from black (occupaid voxel) to the chosen color (free voxel) and they are transparent
75  MIXED, //!< Combination of COLOR_FROM_HEIGHT and TRANSPARENCY_FROM_OCCUPANCY
76  FIXED //!< All cubes are of identical color.
77  };
78 
79 
80  /** The info of each of the voxels */
82  {
84  double side_length;
86 
87  TVoxel() {}
88  TVoxel(const mrpt::math::TPoint3D &coords_, const double side_length_, mrpt::utils::TColor color_) : coords(coords_), side_length(side_length_),color(color_) {}
89  };
90 
91  /** The info of each grid block */
93  {
94  mrpt::math::TPoint3D min,max; //!< opposite corners of the cube
95 
96  TGridCube() {}
97  TGridCube(const mrpt::math::TPoint3D &min_,const mrpt::math::TPoint3D &max_) : min(min_),max(max_) { }
98  };
99 
101  {
102  bool visible;
103  std::vector<TVoxel> voxels;
104 
105  TInfoPerVoxelSet() : visible(true) {}
106  };
107  protected:
108 
109  std::deque<TInfoPerVoxelSet> m_voxel_sets;
110  std::vector<TGridCube> m_grid_cubes;
111 
112  mrpt::math::TPoint3D m_bb_min, m_bb_max; //!< Cached bounding boxes
113 
122 
123  public:
124 
125  /** Clears everything */
126  void clear();
127 
128  /** Select the visualization mode. To have any effect, this method has to be called before loading the octomap. */
130  m_visual_mode = mode; CRenderizableDisplayList::notifyChange();
131  }
132  inline visualization_mode_t getVisualizationMode() const {return m_visual_mode;}
133 
134  /** Can be used to enable/disable the effects of lighting in this object */
135  inline void enableLights(bool enable) { m_enable_lighting=enable; CRenderizableDisplayList::notifyChange(); }
136  inline bool areLightsEnabled() const { return m_enable_lighting; }
137 
138 
139 
140  /** By default, the alpha (transparency) component of voxel cubes is taken into account, but transparency can be disabled with this method. */
141  inline void enableCubeTransparency(bool enable) { m_enable_cube_transparency=enable; CRenderizableDisplayList::notifyChange(); }
142  inline bool isCubeTransparencyEnabled() const { return m_enable_cube_transparency; }
143 
144  /** Shows/hides the grid lines */
145  inline void showGridLines(bool show) { m_show_grids=show; CRenderizableDisplayList::notifyChange(); }
146  inline bool areGridLinesVisible() const { return m_show_grids; }
147 
148  /** Shows/hides the voxels (voxel_set is a 0-based index for the set of voxels to modify, e.g. VOXEL_SET_OCCUPIED, VOXEL_SET_FREESPACE) */
149  inline void showVoxels(unsigned int voxel_set, bool show) { ASSERT_(voxel_set<m_voxel_sets.size()) m_voxel_sets[voxel_set].visible=show; CRenderizableDisplayList::notifyChange(); }
150  inline bool areVoxelsVisible(unsigned int voxel_set) const { ASSERT_(voxel_set<m_voxel_sets.size()) return m_voxel_sets[voxel_set].visible; }
151 
152  /** For quick renders: render voxels as points instead of cubes. \sa setVoxelAsPointsSize */
153  inline void showVoxelsAsPoints(const bool enable) { m_showVoxelsAsPoints=enable; CRenderizableDisplayList::notifyChange(); }
154  inline bool areVoxelsShownAsPoints() const { return m_showVoxelsAsPoints; }
155 
156  /** Only used when showVoxelsAsPoints() is enabled. */
157  inline void setVoxelAsPointsSize(float pointSize) { m_showVoxelsAsPointsSize=pointSize; CRenderizableDisplayList::notifyChange(); }
158  inline float getVoxelAsPointsSize() const { return m_showVoxelsAsPointsSize; }
159 
160  /** Sets the width of grid lines */
161  inline void setGridLinesWidth(float w) { m_grid_width=w; CRenderizableDisplayList::notifyChange(); }
162  /** Gets the width of grid lines */
163  inline float getGridLinesWidth() const { return m_grid_width; }
164 
165  inline void setGridLinesColor(const mrpt::utils::TColor &color) { m_grid_color=color; CRenderizableDisplayList::notifyChange(); }
166  inline const mrpt::utils::TColor & getGridLinesColor() const { return m_grid_color; }
167 
168  /** Returns the total count of grid cubes. */
169  inline size_t getGridCubeCount() const { return m_grid_cubes.size(); }
170  /** Returns the number of voxel sets. */
171  inline size_t getVoxelSetCount() const { return m_voxel_sets.size(); }
172  /** Returns the total count of voxels in one voxel set. */
173  inline size_t getVoxelCount(const size_t set_index) const { ASSERT_(set_index<m_voxel_sets.size()) return m_voxel_sets[set_index].voxels.size(); }
174 
175  /** Manually changes the bounding box (normally the user doesn't need to call this) */
176  void setBoundingBox(const mrpt::math::TPoint3D &bb_min, const mrpt::math::TPoint3D &bb_max);
177 
178  inline void resizeGridCubes(const size_t nCubes) { m_grid_cubes.resize(nCubes); CRenderizableDisplayList::notifyChange(); }
179  inline void resizeVoxelSets(const size_t nVoxelSets) { m_voxel_sets.resize(nVoxelSets); CRenderizableDisplayList::notifyChange(); }
180  inline void resizeVoxels(const size_t set_index, const size_t nVoxels) { ASSERT_(set_index<m_voxel_sets.size()) m_voxel_sets[set_index].voxels.resize(nVoxels); CRenderizableDisplayList::notifyChange(); }
181 
182  inline void reserveGridCubes(const size_t nCubes) { m_grid_cubes.reserve(nCubes); }
183  inline void reserveVoxels(const size_t set_index, const size_t nVoxels) { ASSERT_(set_index<m_voxel_sets.size()) m_voxel_sets[set_index].voxels.reserve(nVoxels); CRenderizableDisplayList::notifyChange(); }
184 
185  inline TGridCube & getGridCubeRef(const size_t idx) { ASSERTDEB_(idx<m_grid_cubes.size()) CRenderizableDisplayList::notifyChange(); return m_grid_cubes[idx]; }
186  inline const TGridCube & getGridCube(const size_t idx) const { ASSERTDEB_(idx<m_grid_cubes.size()) return m_grid_cubes[idx]; }
187 
188  inline TVoxel & getVoxelRef(const size_t set_index, const size_t idx) { ASSERTDEB_(set_index<m_voxel_sets.size() && idx<m_voxel_sets[set_index].voxels.size()) CRenderizableDisplayList::notifyChange(); return m_voxel_sets[set_index].voxels[idx]; }
189  inline const TVoxel & getVoxel(const size_t set_index, const size_t idx) const { ASSERTDEB_(set_index<m_voxel_sets.size() && idx<m_voxel_sets[set_index].voxels.size()) CRenderizableDisplayList::notifyChange(); return m_voxel_sets[set_index].voxels[idx]; }
190 
191  inline void push_back_GridCube(const TGridCube & c) { CRenderizableDisplayList::notifyChange(); m_grid_cubes.push_back(c); }
192  inline void push_back_Voxel(const size_t set_index, const TVoxel & v) { ASSERTDEB_(set_index<m_voxel_sets.size()) CRenderizableDisplayList::notifyChange(); m_voxel_sets[set_index].voxels.push_back(v); }
193 
194  void sort_voxels_by_z();
195 
196  /** Render */
197  void render_dl() const;
198 
199  /** Evaluates the bounding box of this object (including possible children) in the coordinate frame of the object parent. */
200  virtual void getBoundingBox(mrpt::math::TPoint3D &bb_min, mrpt::math::TPoint3D &bb_max) const;
201 
202  /** Sets the contents of the object from a mrpt::slam::COctoMap object.
203  * \tparam Typically, an mrpt::slam::COctoMap object
204  *
205  * \note Declared as a template because in the library [mrpt-opengl] we don't have access to the library [mrpt-maps].
206  */
207  template <class OCTOMAP>
208  void setFromOctoMap(OCTOMAP &m) {
209  m.getAsOctoMapVoxels(*this);
210  }
211 
212  private:
213  /** Constructor */
214  COctoMapVoxels();
215  /** Private, virtual destructor: only can be deleted from smart pointers. */
216  virtual ~COctoMapVoxels() { }
217  };
218  DEFINE_SERIALIZABLE_POST_CUSTOM_BASE_LINKAGE( COctoMapVoxels, CRenderizableDisplayList, OPENGL_IMPEXP )
219 
220  } // end namespace
221 } // End of namespace
222 
223 #endif
bool areVoxelsVisible(unsigned int voxel_set) const
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, e.g.
void reserveVoxels(const size_t set_index, const size_t nVoxels)
TGridCube & getGridCubeRef(const size_t idx)
void enableLights(bool enable)
Can be used to enable/disable the effects of lighting in this object.
mrpt::math::TPoint3D m_bb_min
std::deque< TInfoPerVoxelSet > m_voxel_sets
void setFromOctoMap(OCTOMAP &m)
Sets the contents of the object from a mrpt::slam::COctoMap object.
void setVoxelAsPointsSize(float pointSize)
Only used when showVoxelsAsPoints() is enabled.
size_t getGridCubeCount() const
Returns the total count of grid cubes.
TVoxel & getVoxelRef(const size_t set_index, const size_t idx)
void enableCubeTransparency(bool enable)
By default, the alpha (transparency) component of voxel cubes is taken into account, but transparency can be disabled with this method.
void reserveGridCubes(const size_t nCubes)
Color goes from black (at the bottom) to the chosen color (at the top)
Color goes from black (occupied voxel) to the chosen color (free voxel)
EIGEN_STRONG_INLINE void notifyChange() const
Must be called to notify that the object has changed (so, the display list must be updated) ...
visualization_mode_t
The different coloring schemes, which modulate the generic mrpt::opengl::CRenderizable object color...
void resizeVoxels(const size_t set_index, const size_t nVoxels)
A renderizable object suitable for rendering with OpenGL's display lists.
A flexible renderer of voxels, typically from a 3D octo map (see mrpt::slam::COctoMap).
void push_back_GridCube(const TGridCube &c)
float getVoxelAsPointsSize() const
#define DEFINE_SERIALIZABLE_PRE_CUSTOM_BASE_LINKAGE(class_name, base_name, _LINKAGE_)
This declaration must be inserted in all CSerializable classes definition, before the class declarati...
Color goes from black (occupaid voxel) to the chosen color (free voxel) and they are transparent...
float getGridLinesWidth() const
Gets the width of grid lines.
const TGridCube & getGridCube(const size_t idx) const
A RGB color - 8bit.
Definition: TColor.h:25
Combination of COLOR_FROM_HEIGHT and TRANSPARENCY_FROM_OCCUPANCY.
Transparency goes from opaque (occupied voxel) to transparent (free voxel).
bool isCubeTransparencyEnabled() const
const mrpt::utils::TColor & getGridLinesColor() const
The info of each grid block.
void push_back_Voxel(const size_t set_index, const TVoxel &v)
TVoxel(const mrpt::math::TPoint3D &coords_, const double side_length_, mrpt::utils::TColor color_)
The info of each of the voxels.
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
#define DEFINE_SERIALIZABLE(class_name)
This declaration must be inserted in all CSerializable classes definition, within the class declarati...
size_t getVoxelSetCount() const
Returns the number of voxel sets.
#define ASSERTDEB_(f)
Defines an assertion mechanism - only when compiled in debug.
visualization_mode_t m_visual_mode
visualization_mode_t getVisualizationMode() const
void showVoxelsAsPoints(const bool enable)
For quick renders: render voxels as points instead of cubes.
void resizeVoxelSets(const size_t nVoxelSets)
const TVoxel & getVoxel(const size_t set_index, const size_t idx) const
void setGridLinesColor(const mrpt::utils::TColor &color)
#define ASSERT_(f)
void showGridLines(bool show)
Shows/hides the grid lines.
size_t getVoxelCount(const size_t set_index) const
Returns the total count of voxels in one voxel set.
void setVisualizationMode(visualization_mode_t mode)
Select the visualization mode.
std::vector< TGridCube > m_grid_cubes
Lightweight 3D point.
mrpt::utils::TColor m_grid_color
void resizeGridCubes(const size_t nCubes)
#define DEFINE_SERIALIZABLE_POST_CUSTOM_BASE_LINKAGE(class_name, base_name, _LINKAGE_)
virtual ~COctoMapVoxels()
Private, virtual destructor: only can be deleted from smart pointers.
void setGridLinesWidth(float w)
Sets the width of grid lines.
bool areVoxelsShownAsPoints() const
TGridCube(const mrpt::math::TPoint3D &min_, const mrpt::math::TPoint3D &max_)



Page generated by Doxygen 1.8.8 for MRPT 1.2.2 SVN:Unversioned directory at Tue Oct 14 02:14:08 UTC 2014