Main MRPT website > C++ reference
MRPT logo
CMesh.h
Go to the documentation of this file.
00001 /* +---------------------------------------------------------------------------+
00002    |          The Mobile Robot Programming Toolkit (MRPT) C++ library          |
00003    |                                                                           |
00004    |                       http://www.mrpt.org/                                |
00005    |                                                                           |
00006    |   Copyright (C) 2005-2011  University of Malaga                           |
00007    |                                                                           |
00008    |    This software was written by the Machine Perception and Intelligent    |
00009    |      Robotics Lab, University of Malaga (Spain).                          |
00010    |    Contact: Jose-Luis Blanco  <jlblanco@ctima.uma.es>                     |
00011    |                                                                           |
00012    |  This file is part of the MRPT project.                                   |
00013    |                                                                           |
00014    |     MRPT is free software: you can redistribute it and/or modify          |
00015    |     it under the terms of the GNU General Public License as published by  |
00016    |     the Free Software Foundation, either version 3 of the License, or     |
00017    |     (at your option) any later version.                                   |
00018    |                                                                           |
00019    |   MRPT is distributed in the hope that it will be useful,                 |
00020    |     but WITHOUT ANY WARRANTY; without even the implied warranty of        |
00021    |     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         |
00022    |     GNU General Public License for more details.                          |
00023    |                                                                           |
00024    |     You should have received a copy of the GNU General Public License     |
00025    |     along with MRPT.  If not, see <http://www.gnu.org/licenses/>.         |
00026    |                                                                           |
00027    +---------------------------------------------------------------------------+ */
00028 
00029 #ifndef opengl_CMesh_H
00030 #define opengl_CMesh_H
00031 
00032 #include <mrpt/opengl/CRenderizableDisplayList.h>
00033 #include <mrpt/math/CMatrix.h>
00034 #include <mrpt/utils/CImage.h>
00035 #include <mrpt/utils/color_maps.h>
00036 #include <mrpt/opengl/CSetOfTriangles.h>
00037 
00038 namespace mrpt
00039 {
00040         namespace opengl
00041         {
00042                 class OPENGL_IMPEXP CMesh;
00043 
00044                 // This must be added to any CSerializable derived class:
00045                 DEFINE_SERIALIZABLE_PRE_CUSTOM_BASE_LINKAGE( CMesh, CRenderizableDisplayList, OPENGL_IMPEXP )
00046 
00047                 /** A planar (XY) grid where each cell has an associated height and, optionally, a texture map.
00048                   *  A typical usage example would be an elevation map or a 3D model of a terrain.
00049                   *  \sa opengl::COpenGLScene
00050                   *  
00051                   *  <div align="center">
00052                   *  <table border="0" cellspan="4" cellspacing="4" style="border-width: 1px; border-style: solid;">
00053                   *   <tr> <td> mrpt::opengl::CMesh </td> <td> \image html preview_CMesh.png </td> </tr>
00054                   *  </table>
00055                   *  </div>
00056                   *  
00057                   * \ingroup mrpt_opengl_grp
00058                   */
00059                 class OPENGL_IMPEXP CMesh : public CRenderizableDisplayList
00060                 {
00061                         DEFINE_SERIALIZABLE( CMesh )
00062                 protected:
00063                         mrpt::utils::CImage             m_textureImage;
00064 
00065                         bool                                            m_enableTransparency;
00066                         bool                                            m_colorFromZ;
00067                         bool                                            m_isWireFrame;
00068 
00069                         math::CMatrix           Z;              //!< Z(x,y): Z-coordinate of the point (x,y)
00070                         math::CMatrix           mask;
00071                         math::CMatrix           U, V;   //!< Texture coordinates
00072                         mutable math::CMatrix           C;              //!< Color [0,1] for each cell, updated by updateColorsMatrix
00073 
00074                         mrpt::utils::TColormap          m_colorMap; //!< Used when m_colorFromZ is true
00075 
00076                         mutable bool    m_modified_Z;           //!< Whether C is not up-to-date wrt to Z
00077 
00078                         void updateColorsMatrix() const;        //!< Called internally to assure C is updated.
00079                         void updateTriangles() const;           //!< Called internally to assure the triangle list is updated.
00080                         void updatePolygons() const;    //<!Called internally to assure that the polygon list is updated.
00081 
00082                         float xMin,xMax,yMin,yMax;      //!< Mesh bounds
00083                         mutable std::vector<CSetOfTriangles::TTriangle> actualMesh;     //!< List of triangles in the mesh
00084                         mutable bool trianglesUpToDate;         //!<Whether the actual mesh needs to be recalculated
00085                         mutable bool polygonsUpToDate;  //<!Whether the polygon mesh (auxiliary structure for ray tracing) needs to be recalculated
00086                         mutable std::vector<mrpt::math::TPolygonWithPlane> tmpPolys;
00087 
00088                 public:
00089                         void setGridLimits(float xmin,float xmax, float ymin, float ymax)
00090                         {
00091                                 xMin=xmin; xMax = xmax;
00092                                 yMin=ymin; yMax = ymax;
00093                                 CRenderizableDisplayList::notifyChange();
00094                         }
00095 
00096                         void getGridLimits(float &xmin,float &xmax, float &ymin, float &ymax) const
00097                         {
00098                                 xmin=xMin; xmax=xMax;
00099                                 ymin=yMin; ymax=yMax;
00100                         }
00101 
00102                         void enableTransparency( bool v )       { m_enableTransparency = v; CRenderizableDisplayList::notifyChange(); }
00103                         void enableWireFrame( bool v )          { m_isWireFrame = v; CRenderizableDisplayList::notifyChange(); }
00104                         void enableColorFromZ( bool v, mrpt::utils::TColormap   colorMap = mrpt::utils::cmJET )
00105                         {
00106                                 m_colorFromZ = v;
00107                                 m_colorMap   = colorMap;
00108                                 CRenderizableDisplayList::notifyChange();
00109                         }
00110 
00111                         /** This method sets the matrix of heights for each position (cell) in the mesh grid */
00112                         void setZ( const mrpt::math::CMatrixTemplateNumeric<float> &in_Z );
00113 
00114                         /** Returns a reference to the internal Z matrix, allowing changing it efficiently */
00115                         inline void getZ(mrpt::math::CMatrixFloat &out) const   {
00116                                 out=Z;
00117                         }
00118 
00119                         /** Returns a reference to the internal mask matrix, allowing changing it efficiently */
00120                         inline void getMask(mrpt::math::CMatrixFloat &out) const        {
00121                                 out=mask;
00122                         }
00123 
00124                         /** This method sets the boolean mask of valid heights for each position (cell) in the mesh grid */
00125                         void setMask( const mrpt::math::CMatrixTemplateNumeric<float> &in_mask );
00126 
00127                         /** Sets the (u,v) texture coordinates (in range [0,1]) for each cell */
00128                         void setUV( const mrpt::math::CMatrixTemplateNumeric<float> &in_U, const mrpt::math::CMatrixTemplateNumeric<float> &in_V);
00129 
00130                         inline float getXMin() const    { return xMin; }
00131                         inline float getXMax() const    { return xMax; }
00132                         inline float getYMin() const    { return yMin; }
00133                         inline float getYMax() const    { return yMax; }
00134                         inline void setXMin(const float &nxm)   {
00135                                 xMin=nxm;
00136                                 trianglesUpToDate=false; CRenderizableDisplayList::notifyChange();
00137                         }
00138                         inline void setXMax(const float &nxm)   {
00139                                 xMax=nxm;
00140                                 trianglesUpToDate=false; CRenderizableDisplayList::notifyChange();
00141                         }
00142                         inline void setYMin(const float &nym)   {
00143                                 yMin=nym;
00144                                 trianglesUpToDate=false; CRenderizableDisplayList::notifyChange();
00145                         }
00146                         inline void setYMax(const float &nym)   {
00147                                 yMax=nym;
00148                                 trianglesUpToDate=false; CRenderizableDisplayList::notifyChange();
00149                         }
00150                         inline void getXBounds(float &min,float &max) const     {
00151                                 min=xMin;
00152                                 max=xMax;
00153                         }
00154                         inline void getYBounds(float &min,float &max) const     {
00155                                 min=yMin;
00156                                 max=yMax;
00157                         }
00158                         inline void setXBounds(const float &min,const float &max)       {
00159                                 xMin=min;
00160                                 xMax=max;
00161                                 trianglesUpToDate=false; CRenderizableDisplayList::notifyChange();
00162                         }
00163                         inline void setYBounds(const float &min,const float &max)       {
00164                                 yMin=min;
00165                                 yMax=max;
00166                                 trianglesUpToDate=false; CRenderizableDisplayList::notifyChange();
00167                         }
00168 
00169 
00170                         /** Class factory  */
00171                         static CMeshPtr Create(bool enableTransparency, float xMin = 0.0f, float xMax = 0.0f, float yMin = 0.0f, float yMax = 0.0f )
00172                         {
00173                                 return CMeshPtr( new CMesh( enableTransparency, xMin ,xMax , yMin ,yMax ) );
00174                         }
00175 
00176                         /** Render
00177                           */
00178                         void  render_dl() const;
00179 
00180                         /** Assigns a texture image, and disable transparency.
00181                           */
00182                         void  assignImage(const utils::CImage&  img );
00183 
00184                         /** Trace ray
00185                           */
00186                         virtual bool traceRay(const mrpt::poses::CPose3D &o,double &dist) const;
00187 
00188                 private:
00189                         /** Constructor
00190                           */
00191                         CMesh( bool enableTransparency = false, float xMin = 0.0f, float xMax = 0.0f, float yMin = 0.0f, float yMax = 0.0f ) :
00192                                 m_textureImage(0,0),
00193                                 m_enableTransparency(enableTransparency),
00194                                 m_colorFromZ(false),
00195                                 m_isWireFrame(false),
00196                                 Z(0,0), mask(0,0), U(0,0), V(0,0), C(0,0),
00197                                 m_colorMap( mrpt::utils::cmJET ),
00198                                 m_modified_Z(true),
00199                                 xMin(xMin), xMax(xMax), yMin(yMin), yMax(yMax),
00200                                 trianglesUpToDate(false)
00201                         {
00202                                 m_color.A = 255;
00203                                 m_color.R = 0;
00204                                 m_color.G = 0;
00205                                 m_color.B = 150;
00206                         }
00207                         /** Private, virtual destructor: only can be deleted from smart pointers */
00208                         virtual ~CMesh() { }
00209 
00210                 };
00211 
00212         } // end namespace
00213 
00214 } // End of namespace
00215 
00216 #endif



Page generated by Doxygen 1.7.5 for MRPT 0.9.5 SVN: at Thu Oct 13 21:25:36 UTC 2011