Main MRPT website > C++ reference
MRPT logo
CMesh.h
Go to the documentation of this file.
1 /* +---------------------------------------------------------------------------+
2  | The Mobile Robot Programming Toolkit (MRPT) C++ library |
3  | |
4  | http://www.mrpt.org/ |
5  | |
6  | Copyright (C) 2005-2012 University of Malaga |
7  | |
8  | This software was written by the Machine Perception and Intelligent |
9  | Robotics Lab, University of Malaga (Spain). |
10  | Contact: Jose-Luis Blanco <jlblanco@ctima.uma.es> |
11  | |
12  | This file is part of the MRPT project. |
13  | |
14  | MRPT is free software: you can redistribute it and/or modify |
15  | it under the terms of the GNU General Public License as published by |
16  | the Free Software Foundation, either version 3 of the License, or |
17  | (at your option) any later version. |
18  | |
19  | MRPT is distributed in the hope that it will be useful, |
20  | but WITHOUT ANY WARRANTY; without even the implied warranty of |
21  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
22  | GNU General Public License for more details. |
23  | |
24  | You should have received a copy of the GNU General Public License |
25  | along with MRPT. If not, see <http://www.gnu.org/licenses/>. |
26  | |
27  +---------------------------------------------------------------------------+ */
28 
29 #ifndef opengl_CMesh_H
30 #define opengl_CMesh_H
31 
33 #include <mrpt/math/CMatrix.h>
34 #include <mrpt/utils/CImage.h>
35 #include <mrpt/utils/color_maps.h>
37 
38 namespace mrpt
39 {
40  namespace opengl
41  {
43 
44  // This must be added to any CSerializable derived class:
46 
47  /** A planar (XY) grid where each cell has an associated height and, optionally, a texture map.
48  * A typical usage example would be an elevation map or a 3D model of a terrain.
49  * \sa opengl::COpenGLScene
50  *
51  * <div align="center">
52  * <table border="0" cellspan="4" cellspacing="4" style="border-width: 1px; border-style: solid;">
53  * <tr> <td> mrpt::opengl::CMesh </td> <td> \image html preview_CMesh.png </td> </tr>
54  * </table>
55  * </div>
56  *
57  * \ingroup mrpt_opengl_grp
58  */
60  {
62  protected:
63  mrpt::utils::CImage m_textureImage;
64 
65  bool m_enableTransparency;
66  bool m_colorFromZ;
67  bool m_isWireFrame;
68 
69  math::CMatrix Z; //!< Z(x,y): Z-coordinate of the point (x,y)
70  math::CMatrix mask;
71  math::CMatrix U, V; //!< Texture coordinates
72  mutable math::CMatrix C; //!< Color [0,1] for each cell, updated by updateColorsMatrix
73 
74  mrpt::utils::TColormap m_colorMap; //!< Used when m_colorFromZ is true
75 
76  mutable bool m_modified_Z; //!< Whether C is not up-to-date wrt to Z
77 
78  void updateColorsMatrix() const; //!< Called internally to assure C is updated.
79  void updateTriangles() const; //!< Called internally to assure the triangle list is updated.
80  void updatePolygons() const; //<!Called internally to assure that the polygon list is updated.
81 
82  float xMin,xMax,yMin,yMax; //!< Mesh bounds
83  mutable std::vector<CSetOfTriangles::TTriangle> actualMesh; //!< List of triangles in the mesh
84  mutable bool trianglesUpToDate; //!<Whether the actual mesh needs to be recalculated
85  mutable bool polygonsUpToDate; //<!Whether the polygon mesh (auxiliary structure for ray tracing) needs to be recalculated
86  mutable std::vector<mrpt::math::TPolygonWithPlane> tmpPolys;
87 
88  public:
89  void setGridLimits(float xmin,float xmax, float ymin, float ymax)
90  {
91  xMin=xmin; xMax = xmax;
92  yMin=ymin; yMax = ymax;
94  }
95 
96  void getGridLimits(float &xmin,float &xmax, float &ymin, float &ymax) const
97  {
98  xmin=xMin; xmax=xMax;
99  ymin=yMin; ymax=yMax;
100  }
101 
102  void enableTransparency( bool v ) { m_enableTransparency = v; CRenderizableDisplayList::notifyChange(); }
103  void enableWireFrame( bool v ) { m_isWireFrame = v; CRenderizableDisplayList::notifyChange(); }
104  void enableColorFromZ( bool v, mrpt::utils::TColormap colorMap = mrpt::utils::cmJET )
105  {
106  m_colorFromZ = v;
107  m_colorMap = colorMap;
109  }
110 
111  /** This method sets the matrix of heights for each position (cell) in the mesh grid */
112  void setZ( const mrpt::math::CMatrixTemplateNumeric<float> &in_Z );
113 
114  /** Returns a reference to the internal Z matrix, allowing changing it efficiently */
115  inline void getZ(mrpt::math::CMatrixFloat &out) const {
116  out=Z;
117  }
118 
119  /** Returns a reference to the internal mask matrix, allowing changing it efficiently */
120  inline void getMask(mrpt::math::CMatrixFloat &out) const {
121  out=mask;
122  }
123 
124  /** This method sets the boolean mask of valid heights for each position (cell) in the mesh grid */
125  void setMask( const mrpt::math::CMatrixTemplateNumeric<float> &in_mask );
126 
127  /** Sets the (u,v) texture coordinates (in range [0,1]) for each cell */
129 
130  inline float getXMin() const { return xMin; }
131  inline float getXMax() const { return xMax; }
132  inline float getYMin() const { return yMin; }
133  inline float getYMax() const { return yMax; }
134  inline void setXMin(const float &nxm) {
135  xMin=nxm;
136  trianglesUpToDate=false; CRenderizableDisplayList::notifyChange();
137  }
138  inline void setXMax(const float &nxm) {
139  xMax=nxm;
140  trianglesUpToDate=false; CRenderizableDisplayList::notifyChange();
141  }
142  inline void setYMin(const float &nym) {
143  yMin=nym;
144  trianglesUpToDate=false; CRenderizableDisplayList::notifyChange();
145  }
146  inline void setYMax(const float &nym) {
147  yMax=nym;
148  trianglesUpToDate=false; CRenderizableDisplayList::notifyChange();
149  }
150  inline void getXBounds(float &min,float &max) const {
151  min=xMin;
152  max=xMax;
153  }
154  inline void getYBounds(float &min,float &max) const {
155  min=yMin;
156  max=yMax;
157  }
158  inline void setXBounds(const float &min,const float &max) {
159  xMin=min;
160  xMax=max;
161  trianglesUpToDate=false; CRenderizableDisplayList::notifyChange();
162  }
163  inline void setYBounds(const float &min,const float &max) {
164  yMin=min;
165  yMax=max;
166  trianglesUpToDate=false; CRenderizableDisplayList::notifyChange();
167  }
168 
169 
170  /** Class factory */
171  static CMeshPtr Create(bool enableTransparency, float xMin = 0.0f, float xMax = 0.0f, float yMin = 0.0f, float yMax = 0.0f )
172  {
173  return CMeshPtr( new CMesh( enableTransparency, xMin ,xMax , yMin ,yMax ) );
174  }
175 
176  /** Render
177  */
178  void render_dl() const;
179 
180  /** Assigns a texture image, and disable transparency.
181  */
182  void assignImage(const utils::CImage& img );
183 
184  /** Trace ray
185  */
186  virtual bool traceRay(const mrpt::poses::CPose3D &o,double &dist) const;
187 
188  private:
189  /** Constructor
190  */
191  CMesh( bool enableTransparency = false, float xMin = 0.0f, float xMax = 0.0f, float yMin = 0.0f, float yMax = 0.0f ) :
192  m_textureImage(0,0),
193  m_enableTransparency(enableTransparency),
194  m_colorFromZ(false),
195  m_isWireFrame(false),
196  Z(0,0), mask(0,0), U(0,0), V(0,0), C(0,0),
197  m_colorMap( mrpt::utils::cmJET ),
198  m_modified_Z(true),
199  xMin(xMin), xMax(xMax), yMin(yMin), yMax(yMax),
200  trianglesUpToDate(false)
201  {
202  m_color.A = 255;
203  m_color.R = 0;
204  m_color.G = 0;
205  m_color.B = 150;
206  }
207  /** Private, virtual destructor: only can be deleted from smart pointers */
208  virtual ~CMesh() { }
209 
210  };
211 
212  } // end namespace
213 
214 } // End of namespace
215 
216 #endif



Page generated by Doxygen 1.8.3 for MRPT 0.9.6 SVN: at Fri Feb 15 22:05:02 EST 2013