Main MRPT website > C++ reference for MRPT 1.4.0
CMesh.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-2016, 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_CMesh_H
11 #define opengl_CMesh_H
12 
14 #include <mrpt/math/CMatrix.h>
15 #include <mrpt/utils/CImage.h>
16 #include <mrpt/utils/color_maps.h>
18 
19 namespace mrpt
20 {
21  namespace opengl
22  {
23 
24 
25  // This must be added to any CSerializable derived class:
27 
28  /** A planar (XY) grid where each cell has an associated height and, optionally, a texture map.
29  * A typical usage example would be an elevation map or a 3D model of a terrain.
30  * \sa opengl::COpenGLScene
31  *
32  * <div align="center">
33  * <table border="0" cellspan="4" cellspacing="4" style="border-width: 1px; border-style: solid;">
34  * <tr> <td> mrpt::opengl::CMesh </td> <td> \image html preview_CMesh.png </td> </tr>
35  * </table>
36  * </div>
37  *
38  * \ingroup mrpt_opengl_grp
39  */
41  {
43  public:
44  struct TTriangleVertexIndices { size_t vind[3]; };
45  protected:
47 
51  bool m_isImage;
52 
53  math::CMatrix Z; //!< Z(x,y): Z-coordinate of the point (x,y)
55  math::CMatrix U, V; //!< Texture coordinates
56  mutable math::CMatrix C; //!< Grayscale Color [0,1] for each cell, updated by updateColorsMatrix
57  mutable math::CMatrix C_r; //!< Red Component of the Color [0,1] for each cell, updated by updateColorsMatrix
58  mutable math::CMatrix C_g; //!< Green Component of the Color [0,1] for each cell, updated by updateColorsMatrix
59  mutable math::CMatrix C_b; //!< Blue Component of the Color [0,1] for each cell, updated by updateColorsMatrix
60 
61  mrpt::utils::TColormap m_colorMap; //!< Used when m_colorFromZ is true
62 
63  mutable bool m_modified_Z; //!< Whether C is not up-to-date wrt to Z
64  mutable bool m_modified_Image; //!< Whether C is not up-to-date wrt to the texture image
65 
66  void updateColorsMatrix() const; //!< Called internally to assure C is updated.
67  void updateTriangles() const; //!< Called internally to assure the triangle list is updated.
68  void updatePolygons() const; //<!Called internally to assure that the polygon list is updated.
69 
70  float xMin,xMax,yMin,yMax; //!< Mesh bounds
71  mutable std::vector<std::pair<CSetOfTriangles::TTriangle,TTriangleVertexIndices> > actualMesh; //!< List of triangles in the mesh
72  mutable std::vector<std::pair<mrpt::math::TPoint3D,size_t> > vertex_normals; //!< The accumulated normals & counts for each vertex, so normals can be averaged.
73  mutable bool trianglesUpToDate; //!<Whether the actual mesh needs to be recalculated
74  mutable bool polygonsUpToDate; //<!Whether the polygon mesh (auxiliary structure for ray tracing) needs to be recalculated
75  mutable std::vector<mrpt::math::TPolygonWithPlane> tmpPolys;
76 
77  public:
78  void setGridLimits(float xmin,float xmax, float ymin, float ymax)
79  {
80  xMin=xmin; xMax = xmax;
81  yMin=ymin; yMax = ymax;
83  }
84 
85  void getGridLimits(float &xmin,float &xmax, float &ymin, float &ymax) const
86  {
87  xmin=xMin; xmax=xMax;
88  ymin=yMin; ymax=yMax;
89  }
90 
91  void enableTransparency( bool v ) { m_enableTransparency = v; CRenderizableDisplayList::notifyChange(); }
92  void enableWireFrame( bool v ) { m_isWireFrame = v; CRenderizableDisplayList::notifyChange(); }
94  {
95  m_colorFromZ = v;
96  m_colorMap = colorMap;
98  }
99 
100  /** This method sets the matrix of heights for each position (cell) in the mesh grid */
102 
103  /** Returns a reference to the internal Z matrix, allowing changing it efficiently */
104  inline void getZ(mrpt::math::CMatrixFloat &out) const {
105  out=Z;
106  }
107 
108  /** Returns a reference to the internal mask matrix, allowing changing it efficiently */
109  inline void getMask(mrpt::math::CMatrixFloat &out) const {
110  out=mask;
111  }
112 
113  /** This method sets the boolean mask of valid heights for each position (cell) in the mesh grid */
115 
116  /** Sets the (u,v) texture coordinates (in range [0,1]) for each cell */
118 
119  inline float getXMin() const { return xMin; }
120  inline float getXMax() const { return xMax; }
121  inline float getYMin() const { return yMin; }
122  inline float getYMax() const { return yMax; }
123  inline void setXMin(const float &nxm) {
124  xMin=nxm;
125  trianglesUpToDate=false; CRenderizableDisplayList::notifyChange();
126  }
127  inline void setXMax(const float &nxm) {
128  xMax=nxm;
129  trianglesUpToDate=false; CRenderizableDisplayList::notifyChange();
130  }
131  inline void setYMin(const float &nym) {
132  yMin=nym;
133  trianglesUpToDate=false; CRenderizableDisplayList::notifyChange();
134  }
135  inline void setYMax(const float &nym) {
136  yMax=nym;
137  trianglesUpToDate=false; CRenderizableDisplayList::notifyChange();
138  }
139  inline void getXBounds(float &min,float &max) const {
140  min=xMin;
141  max=xMax;
142  }
143  inline void getYBounds(float &min,float &max) const {
144  min=yMin;
145  max=yMax;
146  }
147  inline void setXBounds(const float &min,const float &max) {
148  xMin=min;
149  xMax=max;
150  trianglesUpToDate=false; CRenderizableDisplayList::notifyChange();
151  }
152  inline void setYBounds(const float &min,const float &max) {
153  yMin=min;
154  yMax=max;
155  trianglesUpToDate=false; CRenderizableDisplayList::notifyChange();
156  }
157 
158 
159  /** Class factory */
160  static CMeshPtr Create(bool enableTransparency, float xMin = 0.0f, float xMax = 0.0f, float yMin = 0.0f, float yMax = 0.0f );
161 
162  /** Render
163  */
164  void render_dl() const MRPT_OVERRIDE;
165 
166  /** Evaluates the bounding box of this object (including possible children) in the coordinate frame of the object parent. */
167  void getBoundingBox(mrpt::math::TPoint3D &bb_min, mrpt::math::TPoint3D &bb_max) const MRPT_OVERRIDE;
168 
169  /** Assigns a texture image, and disable transparency.
170  */
171  void assignImage(const mrpt::utils::CImage& img );
172 
173  /** Assigns a texture image and Z simultaneously, and disable transparency.
174  */
175  void assignImageAndZ( const mrpt::utils::CImage& img, const mrpt::math::CMatrixTemplateNumeric<float> &in_Z);
176 
177  /** Adjust grid limits according to the image aspect ratio, maintaining the X limits and resizing in the Y direction.
178  */
179  inline void adjustGridToImageAR() {
180  ASSERT_(m_isImage);
181  const float ycenter = 0.5*(yMin+yMax);
182  const float xwidth = xMax - xMin;
183  const float newratio = float(m_textureImage.getWidth())/float(m_textureImage.getHeight());
184  yMax = ycenter + 0.5*newratio*xwidth;
185  yMin = ycenter - 0.5*newratio*xwidth;
187  }
188 
189  /** Trace ray
190  */
191  bool traceRay(const mrpt::poses::CPose3D &o,double &dist) const MRPT_OVERRIDE;
192 
193  private:
194  /** Constructor
195  */
196  CMesh( bool enableTransparency = false, float xMin = 0.0f, float xMax = 0.0f, float yMin = 0.0f, float yMax = 0.0f ) :
197  m_textureImage(0,0),
198  m_enableTransparency(enableTransparency),
199  m_colorFromZ(false),
200  m_isWireFrame(false),
201  m_isImage(false),
202  Z(0,0), mask(0,0), U(0,0), V(0,0), C(0,0), C_r(0,0), C_g(0,0), C_b(0,0),
203  m_colorMap( mrpt::utils::cmJET ),
204  m_modified_Z(true),
205  m_modified_Image(false),
206  xMin(xMin), xMax(xMax), yMin(yMin), yMax(yMax),
207  trianglesUpToDate(false)
208  {
209  m_color.A = 255;
210  m_color.R = 0;
211  m_color.G = 0;
212  m_color.B = 150;
213  }
214  /** Private, virtual destructor: only can be deleted from smart pointers */
215  virtual ~CMesh() { }
216 
217  };
218  DEFINE_SERIALIZABLE_POST_CUSTOM_BASE_LINKAGE( CMesh, CRenderizableDisplayList, OPENGL_IMPEXP )
219 
220  } // end namespace
221 
222 } // End of namespace
223 
224 #endif
mrpt::opengl::CMesh::traceRay
bool traceRay(const mrpt::poses::CPose3D &o, double &dist) const MRPT_OVERRIDE
Trace ray.
mrpt::opengl::CMesh::vertex_normals
std::vector< std::pair< mrpt::math::TPoint3D, size_t > > vertex_normals
The accumulated normals & counts for each vertex, so normals can be averaged.
Definition: CMesh.h:72
mrpt::opengl::CMesh::updatePolygons
void updatePolygons() const
mrpt::utils::TColormap
TColormap
Different colormaps.
Definition: color_maps.h:49
mrpt::opengl::CMesh::setYBounds
void setYBounds(const float &min, const float &max)
Definition: CMesh.h:152
mrpt::opengl::CMesh::getYMax
float getYMax() const
Definition: CMesh.h:122
mrpt::opengl::CMesh::enableWireFrame
void enableWireFrame(bool v)
Definition: CMesh.h:92
mrpt::opengl::CMesh::m_modified_Z
bool m_modified_Z
Whether C is not up-to-date wrt to Z.
Definition: CMesh.h:63
mrpt::opengl::CMesh::m_textureImage
mrpt::utils::CImage m_textureImage
Definition: CMesh.h:46
mrpt::opengl::CMesh::C_g
math::CMatrix C_g
Green Component of the Color [0,1] for each cell, updated by updateColorsMatrix.
Definition: CMesh.h:58
mrpt::opengl::CMesh::setMask
void setMask(const mrpt::math::CMatrixTemplateNumeric< float > &in_mask)
This method sets the boolean mask of valid heights for each position (cell) in the mesh grid.
CRenderizableDisplayList.h
mrpt::opengl::CMesh::enableTransparency
void enableTransparency(bool v)
Definition: CMesh.h:91
color_maps.h
mrpt::opengl::CRenderizableDisplayList
A renderizable object suitable for rendering with OpenGL's display lists.
Definition: CRenderizableDisplayList.h:37
mrpt::opengl::CMesh::polygonsUpToDate
bool polygonsUpToDate
Definition: CMesh.h:74
mrpt::opengl::CMesh::V
math::CMatrix V
Texture coordinates.
Definition: CMesh.h:55
CMatrix.h
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:49
CSetOfTriangles.h
mrpt::opengl::CMesh::updateColorsMatrix
void updateColorsMatrix() const
Called internally to assure C is updated.
mrpt::opengl::CMesh::m_colorFromZ
bool m_colorFromZ
Definition: CMesh.h:49
mrpt
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
Definition: CParticleFilter.h:17
mrpt::opengl::CMesh::getGridLimits
void getGridLimits(float &xmin, float &xmax, float &ymin, float &ymax) const
Definition: CMesh.h:85
mrpt::utils::CImage
A class for storing images as grayscale or RGB bitmaps.
Definition: CImage.h:102
mrpt::opengl::CMesh::~CMesh
virtual ~CMesh()
Private, virtual destructor: only can be deleted from smart pointers.
Definition: CMesh.h:215
mrpt::opengl::CMesh
A planar (XY) grid where each cell has an associated height and, optionally, a texture map.
Definition: CMesh.h:41
mrpt::opengl::CMesh::m_modified_Image
bool m_modified_Image
Whether C is not up-to-date wrt to the texture image.
Definition: CMesh.h:64
mrpt::opengl::CMesh::trianglesUpToDate
bool trianglesUpToDate
Whether the actual mesh needs to be recalculated.
Definition: CMesh.h:73
mrpt::opengl::CMesh::getZ
void getZ(mrpt::math::CMatrixFloat &out) const
Returns a reference to the internal Z matrix, allowing changing it efficiently.
Definition: CMesh.h:104
mrpt::math::CMatrixTemplateNumeric
A matrix of dynamic size.
Definition: CMatrixTemplateNumeric.h:40
mrpt::opengl::CMesh::getXMax
float getXMax() const
Definition: CMesh.h:120
mrpt::opengl::CMesh::m_isWireFrame
bool m_isWireFrame
Definition: CMesh.h:50
mrpt::opengl::CMesh::m_enableTransparency
bool m_enableTransparency
Definition: CMesh.h:48
mrpt::math::CMatrix
This class is a "CSerializable" wrapper for "CMatrixFloat".
Definition: CMatrix.h:31
mrpt::opengl::CMesh::Create
static CMeshPtr Create(bool enableTransparency, float xMin=0.0f, float xMax=0.0f, float yMin=0.0f, float yMax=0.0f)
Class factory
mrpt::utils::CImage::getWidth
size_t getWidth() const MRPT_OVERRIDE
Returns the width of the image in pixels.
mrpt::poses::CPose3D
A class used to store a 3D pose (a 3D translation + a rotation in 3D).
Definition: CPose3D.h:73
mrpt::opengl::CMesh::Z
math::CMatrix Z
Z(x,y): Z-coordinate of the point (x,y)
Definition: CMesh.h:53
DEFINE_SERIALIZABLE_POST_CUSTOM_BASE_LINKAGE
#define DEFINE_SERIALIZABLE_POST_CUSTOM_BASE_LINKAGE(class_name, base_name, _LINKAGE_)
Definition: CSerializable.h:174
mrpt::opengl::CMesh::C_b
math::CMatrix C_b
Blue Component of the Color [0,1] for each cell, updated by updateColorsMatrix.
Definition: CMesh.h:59
mrpt::opengl::CMesh::setXBounds
void setXBounds(const float &min, const float &max)
Definition: CMesh.h:147
mrpt::opengl::CMesh::yMin
float yMin
Definition: CMesh.h:70
mrpt::utils::CImage::getHeight
size_t getHeight() const MRPT_OVERRIDE
Returns the height of the image in pixels.
mrpt::utils::cmJET
@ cmJET
Definition: color_maps.h:51
mrpt::opengl::CMesh::setUV
void setUV(const mrpt::math::CMatrixTemplateNumeric< float > &in_U, const mrpt::math::CMatrixTemplateNumeric< float > &in_V)
Sets the (u,v) texture coordinates (in range [0,1]) for each cell.
mrpt::opengl::CMesh::setXMin
void setXMin(const float &nxm)
Definition: CMesh.h:123
mrpt::opengl::CMesh::updateTriangles
void updateTriangles() const
Called internally to assure the triangle list is updated.
mrpt::opengl::CMesh::m_isImage
bool m_isImage
Definition: CMesh.h:51
mrpt::opengl::CMesh::enableColorFromZ
void enableColorFromZ(bool v, mrpt::utils::TColormap colorMap=mrpt::utils::cmJET)
Definition: CMesh.h:93
mrpt::opengl::CMesh::TTriangleVertexIndices
Definition: CMesh.h:44
DEFINE_SERIALIZABLE_PRE_CUSTOM_BASE_LINKAGE
#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...
Definition: CSerializable.h:170
mrpt::opengl::CMesh::m_colorMap
mrpt::utils::TColormap m_colorMap
Used when m_colorFromZ is true.
Definition: CMesh.h:61
CImage.h
DEFINE_SERIALIZABLE
#define DEFINE_SERIALIZABLE(class_name)
This declaration must be inserted in all CSerializable classes definition, within the class declarati...
Definition: CSerializable.h:147
mrpt::opengl::CMesh::getYBounds
void getYBounds(float &min, float &max) const
Definition: CMesh.h:143
mrpt::opengl::CMesh::C_r
math::CMatrix C_r
Red Component of the Color [0,1] for each cell, updated by updateColorsMatrix.
Definition: CMesh.h:57
mrpt::opengl::CMesh::mask
math::CMatrix mask
Definition: CMesh.h:54
ASSERT_
#define ASSERT_(f)
Definition: mrpt_macros.h:261
mrpt::opengl::CMesh::getMask
void getMask(mrpt::math::CMatrixFloat &out) const
Returns a reference to the internal mask matrix, allowing changing it efficiently.
Definition: CMesh.h:109
mrpt::opengl::CMesh::render_dl
void render_dl() const MRPT_OVERRIDE
Render.
mrpt::opengl::CMesh::getXMin
float getXMin() const
Definition: CMesh.h:119
mrpt::opengl::CMesh::setGridLimits
void setGridLimits(float xmin, float xmax, float ymin, float ymax)
Definition: CMesh.h:78
mrpt::opengl::CMesh::setYMax
void setYMax(const float &nym)
Definition: CMesh.h:135
mrpt::opengl::CMesh::getXBounds
void getXBounds(float &min, float &max) const
Definition: CMesh.h:139
mrpt::opengl::CMesh::actualMesh
std::vector< std::pair< CSetOfTriangles::TTriangle, TTriangleVertexIndices > > actualMesh
List of triangles in the mesh.
Definition: CMesh.h:71
mrpt::opengl::CMesh::getYMin
float getYMin() const
Definition: CMesh.h:121
mrpt::opengl::CMesh::tmpPolys
std::vector< mrpt::math::TPolygonWithPlane > tmpPolys
Definition: CMesh.h:75
mrpt::opengl::CMesh::setYMin
void setYMin(const float &nym)
Definition: CMesh.h:131
mrpt::opengl::CMesh::C
math::CMatrix C
Grayscale Color [0,1] for each cell, updated by updateColorsMatrix.
Definition: CMesh.h:56
mrpt::opengl::CMesh::CMesh
CMesh(bool enableTransparency=false, float xMin=0.0f, float xMax=0.0f, float yMin=0.0f, float yMax=0.0f)
Constructor.
Definition: CMesh.h:196
MRPT_OVERRIDE
#define MRPT_OVERRIDE
C++11 "override" for virtuals:
Definition: mrpt_macros.h:28
mrpt::opengl::CMesh::setXMax
void setXMax(const float &nxm)
Definition: CMesh.h:127
mrpt::opengl::CMesh::setZ
void setZ(const mrpt::math::CMatrixTemplateNumeric< float > &in_Z)
This method sets the matrix of heights for each position (cell) in the mesh grid.



Page generated by Doxygen 1.8.20 for MRPT 1.4.0 SVN: at Thu Aug 27 02:40:23 UTC 2020