MRPT  1.9.9
CMesh.h
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | https://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2019, Individual contributors, see AUTHORS file |
6  | See: https://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See: https://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 
10 #pragma once
11 
12 #include <mrpt/img/CImage.h>
13 #include <mrpt/img/color_maps.h>
14 #include <mrpt/math/CMatrixF.h>
17 
18 namespace mrpt::opengl
19 {
20 /** A planar (XY) grid where each cell has an associated height and, optionally,
21  * a texture map.
22  * A typical usage example would be an elevation map or a 3D model of a
23  * terrain.
24  * \sa opengl::COpenGLScene
25  *
26  * <div align="center">
27  * <table border="0" cellspan="4" cellspacing="4" style="border-width: 1px;
28  * border-style: solid;">
29  * <tr> <td> mrpt::opengl::CMesh </td> <td> \image html preview_CMesh.png
30  * </td> </tr>
31  * </table>
32  * </div>
33  *
34  * \ingroup mrpt_opengl_grp
35  */
37 {
39  public:
41  {
42  size_t vind[3];
43  };
44 
45  protected:
47 
49  bool m_colorFromZ{false};
50  bool m_isWireFrame{false};
51  bool m_isImage{false};
52 
53  /** Z(x,y): Z-coordinate of the point (x,y) */
56  /** Texture coordinates */
58  /** Grayscale Color [0,1] for each cell, updated by updateColorsMatrix */
59  mutable math::CMatrixF C;
60  /** Red Component of the Color [0,1] for each cell, updated by
61  * updateColorsMatrix */
63  /** Green Component of the Color [0,1] for each cell, updated by
64  * updateColorsMatrix */
66  /** Blue Component of the Color [0,1] for each cell, updated by
67  * updateColorsMatrix */
69 
70  /** Used when m_colorFromZ is true */
72 
73  /** Whether C is not up-to-date wrt to Z */
74  mutable bool m_modified_Z{true};
75  /** Whether C is not up-to-date wrt to the texture image */
76  mutable bool m_modified_Image{false};
77 
78  /** Called internally to assure C is updated. */
79  void updateColorsMatrix() const;
80  /** Called internally to assure the triangle list is updated. */
81  void updateTriangles() const;
82  void updatePolygons() const; //<!Called internally to assure that the
83  // polygon list is updated.
84 
85  /** Mesh bounds */
86  float xMin, xMax, yMin, yMax;
87  /** List of triangles in the mesh */
88  mutable std::vector<
89  std::pair<CSetOfTriangles::TTriangle, TTriangleVertexIndices>>
91  /** The accumulated normals & counts for each vertex, so normals can be
92  * averaged. */
93  mutable std::vector<std::pair<mrpt::math::TPoint3D, size_t>> vertex_normals;
94  /**Whether the actual mesh needs to be recalculated */
95  mutable bool trianglesUpToDate{false};
96  /**Whether the polygon mesh (auxiliary structure for ray tracing) needs to
97  * be recalculated */
98  mutable bool polygonsUpToDate{false};
99  mutable std::vector<mrpt::math::TPolygonWithPlane> tmpPolys;
100 
101  public:
102  void setGridLimits(float xmin, float xmax, float ymin, float ymax)
103  {
104  xMin = xmin;
105  xMax = xmax;
106  yMin = ymin;
107  yMax = ymax;
109  }
110 
111  void getGridLimits(float& xmin, float& xmax, float& ymin, float& ymax) const
112  {
113  xmin = xMin;
114  xmax = xMax;
115  ymin = yMin;
116  ymax = yMax;
117  }
118 
120  {
123  }
124  void enableWireFrame(bool v)
125  {
126  m_isWireFrame = v;
128  }
130  bool v, mrpt::img::TColormap colorMap = mrpt::img::cmHOT)
131  {
132  m_colorFromZ = v;
133  m_colorMap = colorMap;
135  }
136 
137  /** This method sets the matrix of heights for each position (cell) in the
138  * mesh grid */
139  void setZ(const mrpt::math::CMatrixDynamic<float>& in_Z);
140 
141  /** Returns a reference to the internal Z matrix, allowing changing it
142  * efficiently */
143  inline void getZ(mrpt::math::CMatrixFloat& out) const { out = Z; }
144  /** Returns a reference to the internal mask matrix, allowing changing it
145  * efficiently */
146  inline void getMask(mrpt::math::CMatrixFloat& out) const { out = mask; }
147  /** This method sets the boolean mask of valid heights for each position
148  * (cell) in the mesh grid */
149  void setMask(const mrpt::math::CMatrixDynamic<float>& in_mask);
150 
151  /** Sets the (u,v) texture coordinates (in range [0,1]) for each cell */
152  void setUV(
155 
156  inline float getXMin() const { return xMin; }
157  inline float getXMax() const { return xMax; }
158  inline float getYMin() const { return yMin; }
159  inline float getYMax() const { return yMax; }
160  inline void setXMin(const float nxm)
161  {
162  xMin = nxm;
163  trianglesUpToDate = false;
165  }
166  inline void setXMax(const float nxm)
167  {
168  xMax = nxm;
169  trianglesUpToDate = false;
171  }
172  inline void setYMin(const float nym)
173  {
174  yMin = nym;
175  trianglesUpToDate = false;
177  }
178  inline void setYMax(const float nym)
179  {
180  yMax = nym;
181  trianglesUpToDate = false;
183  }
184  inline void getXBounds(float& min, float& max) const
185  {
186  min = xMin;
187  max = xMax;
188  }
189  inline void getYBounds(float& min, float& max) const
190  {
191  min = yMin;
192  max = yMax;
193  }
194  inline void setXBounds(const float min, const float max)
195  {
196  xMin = min;
197  xMax = max;
198  trianglesUpToDate = false;
200  }
201  inline void setYBounds(const float min, const float max)
202  {
203  yMin = min;
204  yMax = max;
205  trianglesUpToDate = false;
207  }
208 
209  /** Render
210  */
211  void render_dl() const override;
212 
213  /** Evaluates the bounding box of this object (including possible children)
214  * in the coordinate frame of the object parent. */
215  void getBoundingBox(
217  mrpt::math::TPoint3D& bb_max) const override;
218 
219  /** Assigns a texture image, and disable transparency.
220  */
221  void assignImage(const mrpt::img::CImage& img);
222 
223  /** Assigns a texture image and Z simultaneously, and disable transparency.
224  */
225  void assignImageAndZ(
226  const mrpt::img::CImage& img,
228 
229  /** Adjust grid limits according to the image aspect ratio, maintaining the
230  * X limits and resizing in the Y direction.
231  */
232  inline void adjustGridToImageAR()
233  {
235  const float ycenter = 0.5 * (yMin + yMax);
236  const float xwidth = xMax - xMin;
237  const float newratio = float(m_textureImage.getWidth()) /
238  float(m_textureImage.getHeight());
239  yMax = ycenter + 0.5 * newratio * xwidth;
240  yMin = ycenter - 0.5 * newratio * xwidth;
242  }
243 
244  /** Trace ray
245  */
246  bool traceRay(const mrpt::poses::CPose3D& o, double& dist) const override;
247 
248  /** Constructor */
249  CMesh(
250  bool enableTransparency = false, float xMin = 0.0f, float xMax = 0.0f,
251  float yMin = 0.0f, float yMax = 0.0f);
252 
253  /** Private, virtual destructor: only can be deleted from smart pointers */
254  ~CMesh() override;
255 };
256 
257 } // namespace mrpt::opengl
~CMesh() override
Private, virtual destructor: only can be deleted from smart pointers.
void setXMax(const float nxm)
Definition: CMesh.h:166
void notifyChange() const
Must be called to notify that the object has changed (so, the display list must be updated) ...
math::CMatrixF C
Grayscale Color [0,1] for each cell, updated by updateColorsMatrix.
Definition: CMesh.h:59
mrpt::img::CImage m_textureImage
Definition: CMesh.h:46
TColormap
Different colormaps for use in mrpt::img::colormap()
Definition: color_maps.h:29
std::vector< mrpt::math::TPolygonWithPlane > tmpPolys
Definition: CMesh.h:99
#define min(a, b)
void assignImageAndZ(const mrpt::img::CImage &img, const mrpt::math::CMatrixDynamic< float > &in_Z)
Assigns a texture image and Z simultaneously, and disable transparency.
Definition: CMesh.cpp:362
math::CMatrixF V
Definition: CMesh.h:57
void adjustGridToImageAR()
Adjust grid limits according to the image aspect ratio, maintaining the X limits and resizing in the ...
Definition: CMesh.h:232
math::CMatrixF mask
Definition: CMesh.h:55
float xMin
Mesh bounds.
Definition: CMesh.h:86
void enableTransparency(bool v)
Definition: CMesh.h:119
void setXMin(const float nxm)
Definition: CMesh.h:160
math::CMatrixF U
Texture coordinates.
Definition: CMesh.h:57
bool m_enableTransparency
Definition: CMesh.h:48
void setUV(const mrpt::math::CMatrixDynamic< float > &in_U, const mrpt::math::CMatrixDynamic< float > &in_V)
Sets the (u,v) texture coordinates (in range [0,1]) for each cell.
Definition: CMesh.cpp:534
void enableWireFrame(bool v)
Definition: CMesh.h:124
size_t getHeight() const override
Returns the height of the image in pixels.
Definition: CImage.cpp:878
void render_dl() const override
Render.
Definition: CMesh.cpp:288
math::CMatrixF C_b
Blue Component of the Color [0,1] for each cell, updated by updateColorsMatrix.
Definition: CMesh.h:68
bool m_isWireFrame
Definition: CMesh.h:50
void getXBounds(float &min, float &max) const
Definition: CMesh.h:184
bool m_modified_Z
Whether C is not up-to-date wrt to Z.
Definition: CMesh.h:74
void setMask(const mrpt::math::CMatrixDynamic< float > &in_mask)
This method sets the boolean mask of valid heights for each position (cell) in the mesh grid...
Definition: CMesh.cpp:527
mrpt::img::TColormap m_colorMap
Used when m_colorFromZ is true.
Definition: CMesh.h:71
void getGridLimits(float &xmin, float &xmax, float &ymin, float &ymax) const
Definition: CMesh.h:111
void setYMax(const float nym)
Definition: CMesh.h:178
float getYMin() const
Definition: CMesh.h:158
void assignImage(const mrpt::img::CImage &img)
Assigns a texture image, and disable transparency.
Definition: CMesh.cpp:341
A renderizable object suitable for rendering with OpenGL&#39;s display lists.
bool m_colorFromZ
Definition: CMesh.h:49
#define ASSERT_(f)
Defines an assertion mechanism.
Definition: exceptions.h:120
size_t getWidth() const override
Returns the width of the image in pixels.
Definition: CImage.cpp:847
float getXMax() const
Definition: CMesh.h:157
[New in MRPT 1.5.0]
Definition: color_maps.h:35
GLint GLvoid * img
Definition: glext.h:3769
bool traceRay(const mrpt::poses::CPose3D &o, double &dist) const override
Trace ray.
Definition: CMesh.cpp:543
void setYBounds(const float min, const float max)
Definition: CMesh.h:201
math::CMatrixF C_g
Green Component of the Color [0,1] for each cell, updated by updateColorsMatrix.
Definition: CMesh.h:65
void enableColorFromZ(bool v, mrpt::img::TColormap colorMap=mrpt::img::cmHOT)
Definition: CMesh.h:129
void updatePolygons() const
Definition: CMesh.cpp:564
math::CMatrixF C_r
Red Component of the Color [0,1] for each cell, updated by updateColorsMatrix.
Definition: CMesh.h:62
float getYMax() const
Definition: CMesh.h:159
void setGridLimits(float xmin, float xmax, float ymin, float ymax)
Definition: CMesh.h:102
void setZ(const mrpt::math::CMatrixDynamic< float > &in_Z)
This method sets the matrix of heights for each position (cell) in the mesh grid. ...
Definition: CMesh.cpp:515
bool polygonsUpToDate
Whether the polygon mesh (auxiliary structure for ray tracing) needs to be recalculated.
Definition: CMesh.h:98
This class is a "CSerializable" wrapper for "CMatrixFloat".
Definition: CMatrixF.h:22
const GLdouble * v
Definition: glext.h:3684
#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:143
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:576
CMesh(bool enableTransparency=false, float xMin=0.0f, float xMax=0.0f, float yMin=0.0f, float yMax=0.0f)
Constructor.
Definition: CMesh.cpp:30
A class used to store a 3D pose (a 3D translation + a rotation in 3D).
Definition: CPose3D.h:84
float getXMin() const
Definition: CMesh.h:156
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:444
const auto bb_max
void getYBounds(float &min, float &max) const
Definition: CMesh.h:189
const auto bb_min
void setXBounds(const float min, const float max)
Definition: CMesh.h:194
A planar (XY) grid where each cell has an associated height and, optionally, a texture map...
Definition: CMesh.h:36
Lightweight 3D point.
Definition: TPoint3D.h:90
This template class provides the basic functionality for a general 2D any-size, resizable container o...
math::CMatrixF Z
Z(x,y): Z-coordinate of the point (x,y)
Definition: CMesh.h:54
void updateTriangles() const
Called internally to assure the triangle list is updated.
Definition: CMesh.cpp:58
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:93
std::vector< std::pair< CSetOfTriangles::TTriangle, TTriangleVertexIndices > > actualMesh
List of triangles in the mesh.
Definition: CMesh.h:90
bool m_modified_Image
Whether C is not up-to-date wrt to the texture image.
Definition: CMesh.h:76
void getMask(mrpt::math::CMatrixFloat &out) const
Returns a reference to the internal mask matrix, allowing changing it efficiently.
Definition: CMesh.h:146
A class for storing images as grayscale or RGB bitmaps.
Definition: img/CImage.h:147
void setYMin(const float nym)
Definition: CMesh.h:172
bool trianglesUpToDate
Whether the actual mesh needs to be recalculated.
Definition: CMesh.h:95



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: 8fe78517f Sun Jul 14 19:43:28 2019 +0200 at lun oct 28 02:10:00 CET 2019