Main MRPT website > C++ reference for MRPT 1.5.9
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-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_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:
26  DEFINE_SERIALIZABLE_PRE_CUSTOM_BASE_LINKAGE( CMesh, CRenderizableDisplayList, OPENGL_IMPEXP )
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 */
101  void setZ( const mrpt::math::CMatrixTemplateNumeric<float> &in_Z );
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 */
114  void setMask( const mrpt::math::CMatrixTemplateNumeric<float> &in_mask );
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  CMesh(bool enableTransparency = false, float xMin = 0.0f, float xMax = 0.0f, float yMin = 0.0f, float yMax = 0.0f);
196 
197  /** Private, virtual destructor: only can be deleted from smart pointers */
198  virtual ~CMesh();
199  };
200  DEFINE_SERIALIZABLE_POST_CUSTOM_BASE_LINKAGE( CMesh, CRenderizableDisplayList, OPENGL_IMPEXP )
201 
202  } // end namespace
203 
204 } // End of namespace
205 
206 #endif
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
void setXMax(const float nxm)
Definition: CMesh.h:127
GLenum GLint GLuint mask
Definition: glext.h:3888
std::vector< mrpt::math::TPolygonWithPlane > tmpPolys
Definition: CMesh.h:75
#define min(a, b)
#define MRPT_OVERRIDE
C++11 "override" for virtuals:
TColormap
Different colormaps for use in mrpt::utils::colormap()
Definition: color_maps.h:30
A class for storing images as grayscale or RGB bitmaps.
Definition: CImage.h:101
[New in MRPT 1.5.0]
Definition: color_maps.h:34
bool BASE_IMPEXP traceRay(const std::vector< TPolygonWithPlane > &vec, const mrpt::poses::CPose3D &pose, double &dist)
Fast ray tracing method using polygons&#39; properties.
Definition: geometry.cpp:1989
void enableTransparency(bool v)
Definition: CMesh.h:91
void setXMin(const float nxm)
Definition: CMesh.h:123
bool m_enableTransparency
Definition: CMesh.h:48
void enableWireFrame(bool v)
Definition: CMesh.h:92
EIGEN_STRONG_INLINE void notifyChange() const
Must be called to notify that the object has changed (so, the display list must be updated) ...
bool m_isWireFrame
Definition: CMesh.h:50
void getXBounds(float &min, float &max) const
Definition: CMesh.h:139
bool m_modified_Z
Whether C is not up-to-date wrt to Z.
Definition: CMesh.h:63
void getGridLimits(float &xmin, float &xmax, float &ymin, float &ymax) const
Definition: CMesh.h:85
void setYMax(const float nym)
Definition: CMesh.h:135
float getYMin() const
Definition: CMesh.h:121
A renderizable object suitable for rendering with OpenGL&#39;s display lists.
math::CMatrix C_b
Blue Component of the Color [0,1] for each cell, updated by updateColorsMatrix.
Definition: CMesh.h:59
bool m_colorFromZ
Definition: CMesh.h:49
math::CMatrix C_g
Green Component of the Color [0,1] for each cell, updated by updateColorsMatrix.
Definition: CMesh.h:58
#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...
float getXMax() const
Definition: CMesh.h:120
GLint GLvoid * img
Definition: glext.h:3645
std::vector< std::pair< CSetOfTriangles::TTriangle, TTriangleVertexIndices > > actualMesh
List of triangles in the mesh.
Definition: CMesh.h:71
void setYBounds(const float min, const float max)
Definition: CMesh.h:152
math::CMatrix Z
Z(x,y): Z-coordinate of the point (x,y)
Definition: CMesh.h:53
float getYMax() const
Definition: CMesh.h:122
void setGridLimits(float xmin, float xmax, float ymin, float ymax)
Definition: CMesh.h:78
bool polygonsUpToDate
Definition: CMesh.h:74
const GLdouble * v
Definition: glext.h:3603
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...
void getZ(mrpt::math::CMatrixFloat &out) const
Returns a reference to the internal Z matrix, allowing changing it efficiently.
Definition: CMesh.h:104
A class used to store a 3D pose (a 3D translation + a rotation in 3D).
Definition: CPose3D.h:72
mrpt::utils::CImage m_textureImage
Definition: CMesh.h:46
void enableColorFromZ(bool v, mrpt::utils::TColormap colorMap=mrpt::utils::cmHOT)
Definition: CMesh.h:93
math::CMatrix C_r
Red Component of the Color [0,1] for each cell, updated by updateColorsMatrix.
Definition: CMesh.h:57
float getXMin() const
Definition: CMesh.h:119
#define ASSERT_(f)
void getYBounds(float &min, float &max) const
Definition: CMesh.h:143
void setXBounds(const float min, const float max)
Definition: CMesh.h:147
math::CMatrix V
Texture coordinates.
Definition: CMesh.h:55
math::CMatrix mask
Definition: CMesh.h:54
A planar (XY) grid where each cell has an associated height and, optionally, a texture map...
Definition: CMesh.h:40
This class is a "CSerializable" wrapper for "CMatrixFloat".
Definition: CMatrix.h:30
#define DEFINE_SERIALIZABLE_POST_CUSTOM_BASE_LINKAGE(class_name, base_name, _LINKAGE_)
size_t getWidth() const MRPT_OVERRIDE
Returns the width of the image in pixels.
Definition: CImage.cpp:855
mrpt::utils::TColormap m_colorMap
Used when m_colorFromZ is true.
Definition: CMesh.h:61
bool m_modified_Image
Whether C is not up-to-date wrt to the texture image.
Definition: CMesh.h:64
size_t getHeight() const MRPT_OVERRIDE
Returns the height of the image in pixels.
Definition: CImage.cpp:884
void getMask(mrpt::math::CMatrixFloat &out) const
Returns a reference to the internal mask matrix, allowing changing it efficiently.
Definition: CMesh.h:109
void setYMin(const float nym)
Definition: CMesh.h:131
math::CMatrix C
Grayscale Color [0,1] for each cell, updated by updateColorsMatrix.
Definition: CMesh.h:56
bool trianglesUpToDate
Whether the actual mesh needs to be recalculated.
Definition: CMesh.h:73



Page generated by Doxygen 1.8.14 for MRPT 1.5.9 Git: 690a4699f Wed Apr 15 19:29:53 2020 +0200 at miƩ abr 15 19:30:12 CEST 2020