MRPT  1.9.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-2018, 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/img/CImage.h>
16 #include <mrpt/img/color_maps.h>
18 
19 namespace mrpt::opengl
20 {
21 /** A planar (XY) grid where each cell has an associated height and, optionally,
22  * a texture map.
23  * A typical usage example would be an elevation map or a 3D model of a
24  * terrain.
25  * \sa opengl::COpenGLScene
26  *
27  * <div align="center">
28  * <table border="0" cellspan="4" cellspacing="4" style="border-width: 1px;
29  * border-style: solid;">
30  * <tr> <td> mrpt::opengl::CMesh </td> <td> \image html preview_CMesh.png
31  * </td> </tr>
32  * </table>
33  * </div>
34  *
35  * \ingroup mrpt_opengl_grp
36  */
38 {
40  public:
42  {
43  size_t vind[3];
44  };
45 
46  protected:
48 
52  bool m_isImage;
53 
54  /** Z(x,y): Z-coordinate of the point (x,y) */
57  /** Texture coordinates */
59  /** Grayscale Color [0,1] for each cell, updated by updateColorsMatrix */
60  mutable math::CMatrix C;
61  /** Red Component of the Color [0,1] for each cell, updated by
62  * updateColorsMatrix */
63  mutable math::CMatrix C_r;
64  /** Green Component of the Color [0,1] for each cell, updated by
65  * updateColorsMatrix */
66  mutable math::CMatrix C_g;
67  /** Blue Component of the Color [0,1] for each cell, updated by
68  * updateColorsMatrix */
69  mutable math::CMatrix C_b;
70 
71  /** Used when m_colorFromZ is true */
73 
74  /** Whether C is not up-to-date wrt to Z */
75  mutable bool m_modified_Z;
76  /** Whether C is not up-to-date wrt to the texture image */
77  mutable bool m_modified_Image;
78 
79  /** Called internally to assure C is updated. */
80  void updateColorsMatrix() const;
81  /** Called internally to assure the triangle list is updated. */
82  void updateTriangles() const;
83  void updatePolygons() const; //<!Called internally to assure that the
84  // polygon list is updated.
85 
86  /** Mesh bounds */
87  float xMin, xMax, yMin, yMax;
88  /** List of triangles in the mesh */
89  mutable std::vector<
90  std::pair<CSetOfTriangles::TTriangle, TTriangleVertexIndices>>
92  /** The accumulated normals & counts for each vertex, so normals can be
93  * averaged. */
94  mutable std::vector<std::pair<mrpt::math::TPoint3D, size_t>> vertex_normals;
95  /**Whether the actual mesh needs to be recalculated */
96  mutable bool trianglesUpToDate;
97  mutable bool polygonsUpToDate; //<!Whether the polygon mesh (auxiliary
98  // structure for ray tracing) needs to be
99  // recalculated
100  mutable std::vector<mrpt::math::TPolygonWithPlane> tmpPolys;
101 
102  public:
103  void setGridLimits(float xmin, float xmax, float ymin, float ymax)
104  {
105  xMin = xmin;
106  xMax = xmax;
107  yMin = ymin;
108  yMax = ymax;
110  }
111 
112  void getGridLimits(float& xmin, float& xmax, float& ymin, float& ymax) const
113  {
114  xmin = xMin;
115  xmax = xMax;
116  ymin = yMin;
117  ymax = yMax;
118  }
119 
121  {
124  }
125  void enableWireFrame(bool v)
126  {
127  m_isWireFrame = v;
129  }
131  bool v, mrpt::img::TColormap colorMap = mrpt::img::cmHOT)
132  {
133  m_colorFromZ = v;
134  m_colorMap = colorMap;
136  }
137 
138  /** This method sets the matrix of heights for each position (cell) in the
139  * mesh grid */
141 
142  /** Returns a reference to the internal Z matrix, allowing changing it
143  * efficiently */
144  inline void getZ(mrpt::math::CMatrixFloat& out) const { out = Z; }
145  /** Returns a reference to the internal mask matrix, allowing changing it
146  * efficiently */
147  inline void getMask(mrpt::math::CMatrixFloat& out) const { out = mask; }
148  /** This method sets the boolean mask of valid heights for each position
149  * (cell) in the mesh grid */
151 
152  /** Sets the (u,v) texture coordinates (in range [0,1]) for each cell */
153  void setUV(
156 
157  inline float getXMin() const { return xMin; }
158  inline float getXMax() const { return xMax; }
159  inline float getYMin() const { return yMin; }
160  inline float getYMax() const { return yMax; }
161  inline void setXMin(const float nxm)
162  {
163  xMin = nxm;
164  trianglesUpToDate = false;
166  }
167  inline void setXMax(const float nxm)
168  {
169  xMax = nxm;
170  trianglesUpToDate = false;
172  }
173  inline void setYMin(const float nym)
174  {
175  yMin = nym;
176  trianglesUpToDate = false;
178  }
179  inline void setYMax(const float nym)
180  {
181  yMax = nym;
182  trianglesUpToDate = false;
184  }
185  inline void getXBounds(float& min, float& max) const
186  {
187  min = xMin;
188  max = xMax;
189  }
190  inline void getYBounds(float& min, float& max) const
191  {
192  min = yMin;
193  max = yMax;
194  }
195  inline void setXBounds(const float min, const float max)
196  {
197  xMin = min;
198  xMax = max;
199  trianglesUpToDate = false;
201  }
202  inline void setYBounds(const float min, const float max)
203  {
204  yMin = min;
205  yMax = max;
206  trianglesUpToDate = false;
208  }
209 
210  /** Class factory */
211  static CMesh::Ptr Create(
212  bool enableTransparency, float xMin = 0.0f, float xMax = 0.0f,
213  float yMin = 0.0f, float yMax = 0.0f);
214 
215  /** Render
216  */
217  void render_dl() const override;
218 
219  /** Evaluates the bounding box of this object (including possible children)
220  * in the coordinate frame of the object parent. */
221  void getBoundingBox(
222  mrpt::math::TPoint3D& bb_min,
223  mrpt::math::TPoint3D& bb_max) const override;
224 
225  /** Assigns a texture image, and disable transparency.
226  */
227  void assignImage(const mrpt::img::CImage& img);
228 
229  /** Assigns a texture image and Z simultaneously, and disable transparency.
230  */
231  void assignImageAndZ(
232  const mrpt::img::CImage& img,
234 
235  /** Adjust grid limits according to the image aspect ratio, maintaining the
236  * X limits and resizing in the Y direction.
237  */
238  inline void adjustGridToImageAR()
239  {
241  const float ycenter = 0.5 * (yMin + yMax);
242  const float xwidth = xMax - xMin;
243  const float newratio = float(m_textureImage.getWidth()) /
244  float(m_textureImage.getHeight());
245  yMax = ycenter + 0.5 * newratio * xwidth;
246  yMin = ycenter - 0.5 * newratio * xwidth;
248  }
249 
250  /** Trace ray
251  */
252  bool traceRay(const mrpt::poses::CPose3D& o, double& dist) const override;
253 
254  /** Constructor */
255  CMesh(
256  bool enableTransparency = false, float xMin = 0.0f, float xMax = 0.0f,
257  float yMin = 0.0f, float yMax = 0.0f);
258 
259  /** Private, virtual destructor: only can be deleted from smart pointers */
260  virtual ~CMesh();
261 };
262 
263 }
264 #endif
265 
266 
void setXMax(const float nxm)
Definition: CMesh.h:167
virtual ~CMesh()
Private, virtual destructor: only can be deleted from smart pointers.
Definition: CMesh.cpp:60
mrpt::img::CImage m_textureImage
Definition: CMesh.h:47
TColormap
Different colormaps for use in mrpt::img::colormap()
Definition: color_maps.h:29
std::vector< mrpt::math::TPolygonWithPlane > tmpPolys
Definition: CMesh.h:100
#define min(a, b)
void adjustGridToImageAR()
Adjust grid limits according to the image aspect ratio, maintaining the X limits and resizing in the ...
Definition: CMesh.h:238
void setZ(const mrpt::math::CMatrixTemplateNumeric< float > &in_Z)
This method sets the matrix of heights for each position (cell) in the mesh grid. ...
Definition: CMesh.cpp:525
float xMin
Mesh bounds.
Definition: CMesh.h:87
void enableTransparency(bool v)
Definition: CMesh.h:120
void setXMin(const float nxm)
Definition: CMesh.h:161
bool m_enableTransparency
Definition: CMesh.h:49
void enableWireFrame(bool v)
Definition: CMesh.h:125
size_t getHeight() const override
Returns the height of the image in pixels.
Definition: CImage.cpp:892
static Ptr Create(Args &&... args)
Definition: CMesh.h:39
void render_dl() const override
Render.
Definition: CMesh.cpp:297
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:51
void getXBounds(float &min, float &max) const
Definition: CMesh.h:185
bool m_modified_Z
Whether C is not up-to-date wrt to Z.
Definition: CMesh.h:75
mrpt::img::TColormap m_colorMap
Used when m_colorFromZ is true.
Definition: CMesh.h:72
void getGridLimits(float &xmin, float &xmax, float &ymin, float &ymax) const
Definition: CMesh.h:112
void setYMax(const float nym)
Definition: CMesh.h:179
float getYMin() const
Definition: CMesh.h:159
void assignImage(const mrpt::img::CImage &img)
Assigns a texture image, and disable transparency.
Definition: CMesh.cpp:350
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:69
bool m_colorFromZ
Definition: CMesh.h:50
#define ASSERT_(f)
Defines an assertion mechanism.
Definition: exceptions.h:113
math::CMatrix C_g
Green Component of the Color [0,1] for each cell, updated by updateColorsMatrix.
Definition: CMesh.h:66
size_t getWidth() const override
Returns the width of the image in pixels.
Definition: CImage.cpp:864
float getXMax() const
Definition: CMesh.h:158
[New in MRPT 1.5.0]
Definition: color_maps.h:35
void assignImageAndZ(const mrpt::img::CImage &img, const mrpt::math::CMatrixTemplateNumeric< float > &in_Z)
Assigns a texture image and Z simultaneously, and disable transparency.
Definition: CMesh.cpp:372
GLint GLvoid * img
Definition: glext.h:3763
bool traceRay(const mrpt::poses::CPose3D &o, double &dist) const override
Trace ray.
Definition: CMesh.cpp:553
void setYBounds(const float min, const float max)
Definition: CMesh.h:202
void enableColorFromZ(bool v, mrpt::img::TColormap colorMap=mrpt::img::cmHOT)
Definition: CMesh.h:130
void updatePolygons() const
Definition: CMesh.cpp:574
math::CMatrix Z
Z(x,y): Z-coordinate of the point (x,y)
Definition: CMesh.h:55
float getYMax() const
Definition: CMesh.h:160
void setGridLimits(float xmin, float xmax, float ymin, float ymax)
Definition: CMesh.h:103
bool polygonsUpToDate
Definition: CMesh.h:97
const GLdouble * v
Definition: glext.h:3678
#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:144
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...
Definition: CMesh.cpp:586
CMesh(bool enableTransparency=false, float xMin=0.0f, float xMax=0.0f, float yMin=0.0f, float yMax=0.0f)
Constructor.
Definition: CMesh.cpp:29
A class used to store a 3D pose (a 3D translation + a rotation in 3D).
Definition: CPose3D.h:86
math::CMatrix C_r
Red Component of the Color [0,1] for each cell, updated by updateColorsMatrix.
Definition: CMesh.h:63
float getXMin() const
Definition: CMesh.h:157
The namespace for 3D scene representation and rendering.
Definition: CGlCanvasBase.h:15
void updateColorsMatrix() const
Called internally to assure C is updated.
Definition: CMesh.cpp:454
void getYBounds(float &min, float &max) const
Definition: CMesh.h:190
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.
Definition: CMesh.cpp:544
void setXBounds(const float min, const float max)
Definition: CMesh.h:195
math::CMatrix V
Definition: CMesh.h:58
math::CMatrix mask
Definition: CMesh.h:56
A planar (XY) grid where each cell has an associated height and, optionally, a texture map...
Definition: CMesh.h:37
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...
Definition: CMesh.cpp:537
Lightweight 3D point.
This class is a "CSerializable" wrapper for "CMatrixFloat".
Definition: CMatrix.h:22
math::CMatrix U
Texture coordinates.
Definition: CMesh.h:58
void updateTriangles() const
Called internally to assure the triangle list is updated.
Definition: CMesh.cpp:61
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:94
std::vector< std::pair< CSetOfTriangles::TTriangle, TTriangleVertexIndices > > actualMesh
List of triangles in the mesh.
Definition: CMesh.h:91
bool m_modified_Image
Whether C is not up-to-date wrt to the texture image.
Definition: CMesh.h:77
void getMask(mrpt::math::CMatrixFloat &out) const
Returns a reference to the internal mask matrix, allowing changing it efficiently.
Definition: CMesh.h:147
A class for storing images as grayscale or RGB bitmaps.
Definition: img/CImage.h:130
void setYMin(const float nym)
Definition: CMesh.h:173
math::CMatrix C
Grayscale Color [0,1] for each cell, updated by updateColorsMatrix.
Definition: CMesh.h:60
bool trianglesUpToDate
Whether the actual mesh needs to be recalculated.
Definition: CMesh.h:96



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: 7d5e6d718 Fri Aug 24 01:51:28 2018 +0200 at lun nov 2 08:35:50 CET 2020