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>
18
19namespace 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
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 */
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
#define DEFINE_SERIALIZABLE_POST_CUSTOM_BASE_LINKAGE(class_name, base_name, _LINKAGE_)
#define DEFINE_SERIALIZABLE(class_name)
This declaration must be inserted in all CSerializable classes definition, within the class declarati...
#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...
This class is a "CSerializable" wrapper for "CMatrixFloat".
Definition: CMatrix.h:31
A planar (XY) grid where each cell has an associated height and, optionally, a texture map.
Definition: CMesh.h:41
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
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.
void setXMin(const float &nxm)
Definition: CMesh.h:123
virtual ~CMesh()
Private, virtual destructor: only can be deleted from smart pointers.
Definition: CMesh.h:215
std::vector< mrpt::math::TPolygonWithPlane > tmpPolys
Definition: CMesh.h:75
math::CMatrix Z
Z(x,y): Z-coordinate of the point (x,y)
Definition: CMesh.h:53
void getMask(mrpt::math::CMatrixFloat &out) const
Returns a reference to the internal mask matrix, allowing changing it efficiently.
Definition: CMesh.h:109
void render_dl() const MRPT_OVERRIDE
Render.
bool m_colorFromZ
Definition: CMesh.h:49
void updateColorsMatrix() const
Called internally to assure C is updated.
void enableTransparency(bool v)
Definition: CMesh.h:91
float getXMax() const
Definition: CMesh.h:120
void setYBounds(const float &min, const float &max)
Definition: CMesh.h:152
void enableWireFrame(bool v)
Definition: CMesh.h:92
bool polygonsUpToDate
Definition: CMesh.h:74
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
void setYMin(const float &nym)
Definition: CMesh.h:131
float getYMin() const
Definition: CMesh.h:121
math::CMatrix C_g
Green Component of the Color [0,1] for each cell, updated by updateColorsMatrix.
Definition: CMesh.h:58
mrpt::utils::TColormap m_colorMap
Used when m_colorFromZ is true.
Definition: CMesh.h:61
bool m_modified_Z
Whether C is not up-to-date wrt to Z.
Definition: CMesh.h:63
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.
void updatePolygons() const
math::CMatrix C
Grayscale Color [0,1] for each cell, updated by updateColorsMatrix.
Definition: CMesh.h:56
float getYMax() const
Definition: CMesh.h:122
void getYBounds(float &min, float &max) const
Definition: CMesh.h:143
bool traceRay(const mrpt::poses::CPose3D &o, double &dist) const MRPT_OVERRIDE
Trace ray.
void getZ(mrpt::math::CMatrixFloat &out) const
Returns a reference to the internal Z matrix, allowing changing it efficiently.
Definition: CMesh.h:104
void updateTriangles() const
Called internally to assure the triangle list is updated.
void getXBounds(float &min, float &max) const
Definition: CMesh.h:139
bool m_isWireFrame
Definition: CMesh.h:50
void enableColorFromZ(bool v, mrpt::utils::TColormap colorMap=mrpt::utils::cmJET)
Definition: CMesh.h:93
math::CMatrix mask
Definition: CMesh.h:54
std::vector< std::pair< CSetOfTriangles::TTriangle, TTriangleVertexIndices > > actualMesh
List of triangles in the mesh.
Definition: CMesh.h:71
math::CMatrix C_b
Blue Component of the Color [0,1] for each cell, updated by updateColorsMatrix.
Definition: CMesh.h:59
bool m_modified_Image
Whether C is not up-to-date wrt to the texture image.
Definition: CMesh.h:64
bool m_enableTransparency
Definition: CMesh.h:48
math::CMatrix C_r
Red Component of the Color [0,1] for each cell, updated by updateColorsMatrix.
Definition: CMesh.h:57
static CMeshPtr Create(bool enableTransparency, float xMin=0.0f, float xMax=0.0f, float yMin=0.0f, float yMax=0.0f)
Class factory
math::CMatrix U
Definition: CMesh.h:55
void setGridLimits(float xmin, float xmax, float ymin, float ymax)
Definition: CMesh.h:78
float getXMin() const
Definition: CMesh.h:119
bool trianglesUpToDate
Whether the actual mesh needs to be recalculated.
Definition: CMesh.h:73
void setYMax(const float &nym)
Definition: CMesh.h:135
void setZ(const mrpt::math::CMatrixTemplateNumeric< float > &in_Z)
This method sets the matrix of heights for each position (cell) in the mesh grid.
void setXBounds(const float &min, const float &max)
Definition: CMesh.h:147
mrpt::utils::CImage m_textureImage
Definition: CMesh.h:46
void getGridLimits(float &xmin, float &xmax, float &ymin, float &ymax) const
Definition: CMesh.h:85
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)
A class used to store a 3D pose (a 3D translation + a rotation in 3D).
Definition: CPose3D.h:73
A class for storing images as grayscale or RGB bitmaps.
Definition: CImage.h:102
size_t getWidth() const MRPT_OVERRIDE
Returns the width of the image in pixels.
size_t getHeight() const MRPT_OVERRIDE
Returns the height of the image in pixels.
TColormap
Different colormaps.
Definition: color_maps.h:49
#define ASSERT_(f)
Definition: mrpt_macros.h:261
#define MRPT_OVERRIDE
C++11 "override" for virtuals:
Definition: mrpt_macros.h:28
struct OPENGL_IMPEXP CMeshPtr
Definition: CMesh.h:26
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.



Page generated by Doxygen 1.9.5 for MRPT 1.4.0 SVN: at Tue Dec 27 00:53:09 UTC 2022