MRPT  2.0.4
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-2020, 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>
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  */
39 {
41  public:
43  {
44  size_t vind[3] = {0, 0, 0};
45  };
46 
47  public:
48  /** @name Renderizable shader API virtual methods
49  * @{ */
50  void render(const RenderContext& rc) const override;
51  void renderUpdateBuffers() const override;
52 
53  virtual shader_list_t requiredShaders() const override
54  {
55  // May use up to two shaders (triangles and lines):
58  }
59  void onUpdateBuffers_Wireframe() override;
60  void onUpdateBuffers_TexturedTriangles() override;
61  void freeOpenGLResources() override
62  {
65  }
66  /** @} */
67 
68  CMesh(
69  bool enableTransparency = false, float xMin = -1.0f, float xMax = 1.0f,
70  float yMin = -1.0f, float yMax = 1.0f);
71 
72  virtual ~CMesh() override;
73 
74  template <typename T>
75  void setGridLimits(T xMin, T xMax, T yMin, T yMax)
76  {
77  m_xMin = static_cast<float>(xMin);
78  m_xMax = static_cast<float>(xMax);
79  m_yMin = static_cast<float>(yMin);
80  m_yMax = static_cast<float>(yMax);
82  }
83 
84  void getGridLimits(float& xMin, float& xMax, float& yMin, float& yMax) const
85  {
86  xMin = m_xMin;
87  xMax = m_xMax;
88  yMin = m_yMin;
89  yMax = m_yMax;
90  }
91 
92  void enableTransparency(bool v)
93  {
96  }
97  void enableWireFrame(bool v)
98  {
99  m_isWireFrame = v;
101  }
103  bool v, mrpt::img::TColormap colorMap = mrpt::img::cmHOT)
104  {
105  m_colorFromZ = v;
106  m_colorMap = colorMap;
108  }
109 
110  /** This method sets the matrix of heights for each position (cell) in the
111  * mesh grid */
112  void setZ(const mrpt::math::CMatrixDynamic<float>& in_Z);
113 
114  /** Returns a reference to the internal Z matrix, allowing changing it
115  * efficiently */
116  inline void getZ(mrpt::math::CMatrixFloat& out) const { out = Z; }
117  /** Returns a reference to the internal mask matrix, allowing changing it
118  * efficiently */
119  inline void getMask(mrpt::math::CMatrixFloat& out) const { out = mask; }
120  /** This method sets the boolean mask of valid heights for each position
121  * (cell) in the mesh grid */
122  void setMask(const mrpt::math::CMatrixDynamic<float>& in_mask);
123 
124  inline float getxMin() const { return m_xMin; }
125  inline float getxMax() const { return m_xMax; }
126  inline float getyMin() const { return m_yMin; }
127  inline float getyMax() const { return m_yMax; }
128  inline void setxMin(const float nxm)
129  {
130  m_xMin = nxm;
131  m_trianglesUpToDate = false;
133  }
134  inline void setxMax(const float nxm)
135  {
136  m_xMax = nxm;
137  m_trianglesUpToDate = false;
139  }
140  inline void setyMin(const float nym)
141  {
142  m_yMin = nym;
143  m_trianglesUpToDate = false;
145  }
146  inline void setyMax(const float nym)
147  {
148  m_yMax = nym;
149  m_trianglesUpToDate = false;
151  }
152  inline void getXBounds(float& min, float& max) const
153  {
154  min = m_xMin;
155  max = m_xMax;
156  }
157  inline void getYBounds(float& min, float& max) const
158  {
159  min = m_yMin;
160  max = m_yMax;
161  }
162  inline void setXBounds(const float min, const float max)
163  {
164  m_xMin = min;
165  m_xMax = max;
166  m_trianglesUpToDate = false;
168  }
169  inline void setYBounds(const float min, const float max)
170  {
171  m_yMin = min;
172  m_yMax = max;
173  m_trianglesUpToDate = false;
175  }
176 
177  void getBoundingBox(
179  mrpt::math::TPoint3D& bb_max) const override;
180 
181  /** Assigns a texture image.
182  */
183  void assignImage(const mrpt::img::CImage& img);
184 
185  /** Assigns a texture image and Z simultaneously, and disable transparency.
186  */
187  void assignImageAndZ(
188  const mrpt::img::CImage& img,
190 
191  /** Adjust grid limits according to the image aspect ratio, maintaining the
192  * X limits and resizing in the Y direction.
193  */
194  void adjustGridToImageAR();
195 
196  /** Trace ray
197  */
198  bool traceRay(const mrpt::poses::CPose3D& o, double& dist) const override;
199 
200  protected:
202  bool m_colorFromZ{false};
203  bool m_isWireFrame{false};
204  bool m_isImage{false};
205 
206  /** Z(x,y): Z-coordinate of the point (x,y) */
209 
210  /** Grayscale Color [0,1] for each cell, updated by updateColorsMatrix */
211  mutable math::CMatrixF C;
212  /** Red Component of the Color [0,1] for each cell, updated by
213  * updateColorsMatrix */
215  /** Green Component of the Color [0,1] for each cell, updated by
216  * updateColorsMatrix */
218  /** Blue Component of the Color [0,1] for each cell, updated by
219  * updateColorsMatrix */
221 
222  /** Used when m_colorFromZ is true */
224 
225  /** Whether C is not up-to-date wrt to Z */
226  mutable bool m_modified_Z{true};
227  /** Whether C is not up-to-date wrt to the texture image */
228  mutable bool m_modified_Image{false};
229 
230  /** Called internally to assure C is updated. */
231  void updateColorsMatrix() const;
232  /** Called internally to assure the triangle list is updated. */
233  void updateTriangles() const;
234  void updatePolygons() const; //<!Called internally to assure that the
235  // polygon list is updated.
236 
237  /** Mesh bounds */
239  /** List of triangles in the mesh */
240  mutable std::vector<
241  std::pair<mrpt::opengl::TTriangle, TTriangleVertexIndices>>
243  /** The accumulated normals & counts for each vertex, so normals can be
244  * averaged. */
245  mutable std::vector<std::pair<mrpt::math::TPoint3D, size_t>> vertex_normals;
246  /**Whether the actual mesh needs to be recalculated */
247  mutable bool m_trianglesUpToDate{false};
248  /**Whether the polygon mesh (auxiliary structure for ray tracing) needs to
249  * be recalculated */
250  mutable bool m_polygonsUpToDate{false};
251  mutable std::vector<mrpt::math::TPolygonWithPlane> tmpPolys;
252 };
253 
254 } // namespace mrpt::opengl
virtual ~CMesh() override
CMesh(bool enableTransparency=false, float xMin=-1.0f, float xMax=1.0f, float yMin=-1.0f, float yMax=1.0f)
Definition: CMesh.cpp:30
math::CMatrixF C
Grayscale Color [0,1] for each cell, updated by updateColorsMatrix.
Definition: CMesh.h:211
TColormap
Different colormaps for use in mrpt::img::colormap()
Definition: color_maps.h:30
float getyMin() const
Definition: CMesh.h:126
std::vector< mrpt::math::TPolygonWithPlane > tmpPolys
Definition: CMesh.h:251
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:395
static constexpr shader_id_t TEXTURED_TRIANGLES
bool m_trianglesUpToDate
Whether the actual mesh needs to be recalculated.
Definition: CMesh.h:247
virtual shader_list_t requiredShaders() const override
Returns the ID of the OpenGL shader program required to render this class.
Definition: CMesh.h:53
bool m_polygonsUpToDate
Whether the polygon mesh (auxiliary structure for ray tracing) needs to be recalculated.
Definition: CMesh.h:250
void notifyChange() const
Call to enable calling renderUpdateBuffers() before the next render() rendering iteration.
math::CMatrixF mask
Definition: CMesh.h:208
void enableTransparency(bool v)
Definition: CMesh.h:92
bool m_enableTransparency
Definition: CMesh.h:201
void enableWireFrame(bool v)
Definition: CMesh.h:97
void freeOpenGLResources() override
Free opengl buffers.
Definition: CMesh.h:61
float getxMax() const
Definition: CMesh.h:125
math::CMatrixF C_b
Blue Component of the Color [0,1] for each cell, updated by updateColorsMatrix.
Definition: CMesh.h:220
bool m_isWireFrame
Definition: CMesh.h:203
float m_xMin
Mesh bounds.
Definition: CMesh.h:238
void getXBounds(float &min, float &max) const
Definition: CMesh.h:152
bool m_modified_Z
Whether C is not up-to-date wrt to Z.
Definition: CMesh.h:226
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:559
mrpt::img::TColormap m_colorMap
Used when m_colorFromZ is true.
Definition: CMesh.h:223
std::vector< shader_id_t > shader_list_t
A list of shader IDs.
Definition: Shader.h:26
float getyMax() const
Definition: CMesh.h:127
void assignImage(const mrpt::img::CImage &img)
Assigns a texture image.
Definition: CMesh.cpp:374
void getGridLimits(float &xMin, float &xMax, float &yMin, float &yMax) const
Definition: CMesh.h:84
void setyMax(const float nym)
Definition: CMesh.h:146
[New in MRPT 1.5.0]
Definition: color_maps.h:37
static constexpr shader_id_t WIREFRAME
bool traceRay(const mrpt::poses::CPose3D &o, double &dist) const override
Trace ray.
Definition: CMesh.cpp:566
void setYBounds(const float min, const float max)
Definition: CMesh.h:169
math::CMatrixF C_g
Green Component of the Color [0,1] for each cell, updated by updateColorsMatrix.
Definition: CMesh.h:217
void enableColorFromZ(bool v, mrpt::img::TColormap colorMap=mrpt::img::cmHOT)
Definition: CMesh.h:102
void setxMax(const float nxm)
Definition: CMesh.h:134
void updatePolygons() const
Definition: CMesh.cpp:581
math::CMatrixF C_r
Red Component of the Color [0,1] for each cell, updated by updateColorsMatrix.
Definition: CMesh.h:214
void render(const RenderContext &rc) const override
Implements the rendering of 3D objects in each class derived from CRenderizable.
Definition: CMesh.cpp:293
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:547
This class is a "CSerializable" wrapper for "CMatrixFloat".
Definition: CMatrixF.h:22
void setxMin(const float nxm)
Definition: CMesh.h:128
void getZ(mrpt::math::CMatrixFloat &out) const
Returns a reference to the internal Z matrix, allowing changing it efficiently.
Definition: CMesh.h:116
float getxMin() const
Definition: CMesh.h:124
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:593
void onUpdateBuffers_Wireframe() override
Must be implemented in derived classes to update the geometric entities to be drawn in "m_*_buffer" f...
Definition: CMesh.cpp:314
void setGridLimits(T xMin, T xMax, T yMin, T yMax)
Definition: CMesh.h:75
A class used to store a 3D pose (a 3D translation + a rotation in 3D).
Definition: CPose3D.h:85
mrpt::vision::TStereoCalibResults out
std::vector< std::pair< mrpt::opengl::TTriangle, TTriangleVertexIndices > > actualMesh
List of triangles in the mesh.
Definition: CMesh.h:242
void setyMin(const float nym)
Definition: CMesh.h:140
Renderizable generic renderer for objects using the wireframe shader.
The namespace for 3D scene representation and rendering.
Definition: CGlCanvasBase.h:13
void updateColorsMatrix() const
Called internally to assure C is updated.
Definition: CMesh.cpp:476
const auto bb_max
void getYBounds(float &min, float &max) const
Definition: CMesh.h:157
void adjustGridToImageAR()
Adjust grid limits according to the image aspect ratio, maintaining the X limits and resizing in the ...
Definition: CMesh.cpp:609
#define DEFINE_SERIALIZABLE(class_name, NS)
This declaration must be inserted in all CSerializable classes definition, within the class declarati...
const auto bb_min
void setXBounds(const float min, const float max)
Definition: CMesh.h:162
void onUpdateBuffers_TexturedTriangles() override
Must be implemented in derived classes to update the geometric entities to be drawn in "m_*_buffer" f...
Definition: CMesh.cpp:339
A planar (XY) grid where each cell has an associated height and, optionally, a texture map...
Definition: CMesh.h:37
void freeOpenGLResources() override
Free opengl buffers.
This template class provides the basic functionality for a general 2D any-size, resizable container o...
Renderizable generic renderer for objects using the triangles-with-a-texture shader.
math::CMatrixF Z
Z(x,y): Z-coordinate of the point (x,y)
Definition: CMesh.h:207
void updateTriangles() const
Called internally to assure the triangle list is updated.
Definition: CMesh.cpp:56
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:245
bool m_modified_Image
Whether C is not up-to-date wrt to the texture image.
Definition: CMesh.h:228
void renderUpdateBuffers() const override
Called whenever m_outdatedBuffers is true: used to re-generate OpenGL vertex buffers, etc.
Definition: CMesh.cpp:306
void getMask(mrpt::math::CMatrixFloat &out) const
Returns a reference to the internal mask matrix, allowing changing it efficiently.
Definition: CMesh.h:119
A class for storing images as grayscale or RGB bitmaps.
Definition: img/CImage.h:148



Page generated by Doxygen 1.8.14 for MRPT 2.0.4 Git: 1e710274b Thu May 28 15:30:14 2020 +0200 at jue may 28 15:45:10 CEST 2020