Main MRPT website > C++ reference
MRPT logo
CSetOfTriangles.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 #ifndef opengl_CSetOfTriangles_H
00029 #define opengl_CSetOfTriangles_H
00030 
00031 #include <mrpt/opengl/CRenderizableDisplayList.h>
00032 #include <mrpt/math/geometry.h>
00033 
00034 namespace mrpt
00035 {
00036         namespace opengl
00037         {
00038                 class OPENGL_IMPEXP CSetOfTriangles;
00039 
00040                 // This must be added to any CSerializable derived class:
00041                 DEFINE_SERIALIZABLE_PRE_CUSTOM_BASE_LINKAGE( CSetOfTriangles, CRenderizableDisplayList, OPENGL_IMPEXP )
00042 
00043                 /** A set of colored triangles.
00044                   *  This class can be used to draw any solid, arbitrarily complex object (without textures).
00045                   *  \sa opengl::COpenGLScene, CSetOfTexturedTriangles
00046                   * \ingroup mrpt_opengl_grp
00047                   */
00048                 class OPENGL_IMPEXP CSetOfTriangles : public CRenderizableDisplayList
00049                 {
00050                         DEFINE_SERIALIZABLE( CSetOfTriangles )
00051                 public:
00052                         /**
00053                           * Triangle definition. Each vertex has three spatial coordinates and four color values.
00054                           */
00055                         struct OPENGL_IMPEXP TTriangle
00056                         {
00057                                 inline TTriangle() {  }
00058                                 inline TTriangle(const mrpt::math::TPolygon3D &p)  {
00059                                         ASSERT_(p.size()==3)
00060                                         for (size_t i=0;i<3;i++) {
00061                                                 x[i]=p[i].x; y[i]=p[i].y; z[i]=p[i].z; r[i]=g[i]=b[i]=a[i]=1; }
00062                                 }
00063                                 float   x[3],y[3],z[3];
00064                                 float   r[3],g[3],b[3],a[3];
00065                         };
00066                         /**
00067                           * Const iterator type.
00068                           */
00069                         typedef std::vector<TTriangle>::const_iterator const_iterator;
00070                         /**
00071                           * Const reverse iterator type.
00072                           */
00073                         typedef std::vector<TTriangle>::const_reverse_iterator const_reverse_iterator;
00074                 protected:
00075                         /**
00076                           * List of triangles.
00077                           * \sa TTriangle
00078                           */
00079                         std::vector<TTriangle>          m_triangles;
00080                         /**
00081                           * Transparency enabling.
00082                           */
00083                         bool                                            m_enableTransparency;
00084                         /**
00085                           * Mutable variable used to check whether polygons need to be recalculated.
00086                           */
00087                         mutable bool polygonsUpToDate;
00088                         /**
00089                           * Polygon cache.
00090                           */
00091                         mutable std::vector<mrpt::math::TPolygonWithPlane> tmpPolygons;
00092                 public:
00093                         /**
00094                           * Polygon cache updating.
00095                           */
00096                         void updatePolygons() const;
00097                         /**
00098                           * Clear this object.
00099                           */
00100                         inline void clearTriangles() { m_triangles.clear();polygonsUpToDate=false; CRenderizableDisplayList::notifyChange(); }
00101                         /**
00102                           * Get triangle count.
00103                           */
00104                         inline size_t getTrianglesCount() const { return m_triangles.size(); }
00105                         /**
00106                           * Gets the triangle in a given position.
00107                           */
00108                         inline void getTriangle(size_t idx, TTriangle &t) const { ASSERT_(idx<m_triangles.size()); t=m_triangles[idx]; }
00109                         /**
00110                           * Inserts a triangle into the set.
00111                           */
00112                         inline void insertTriangle( const TTriangle &t ) { m_triangles.push_back(t);polygonsUpToDate=false; CRenderizableDisplayList::notifyChange(); }
00113                         /**
00114                           * Inserts a set of triangles, bounded by iterators, into this set.
00115                           * \sa insertTriangle
00116                           */
00117                         template<class InputIterator> inline void insertTriangles(const InputIterator &begin,const InputIterator &end)  {
00118                                 m_triangles.insert(m_triangles.end(),begin,end);
00119                                 polygonsUpToDate=false;
00120                                 CRenderizableDisplayList::notifyChange();
00121                         }
00122                         /**
00123                           * Inserts an existing CSetOfTriangles into this one.
00124                           */
00125                         inline void insertTriangles(const CSetOfTrianglesPtr &p)        {
00126                                 reserve(m_triangles.size()+p->m_triangles.size());
00127                                 m_triangles.insert(m_triangles.end(),p->m_triangles.begin(),p->m_triangles.end());
00128                                 polygonsUpToDate=false;
00129                                 CRenderizableDisplayList::notifyChange();
00130                         }
00131                         /**
00132                           * Reserves memory for certain number of triangles, avoiding multiple memory allocation calls.
00133                           */
00134                         inline void reserve(size_t t)   {
00135                                 m_triangles.reserve(t);
00136                                 CRenderizableDisplayList::notifyChange();
00137                         }
00138 
00139                         /** Enables or disables transparency. */
00140                         inline void enableTransparency( bool v )        { m_enableTransparency = v; CRenderizableDisplayList::notifyChange(); }
00141 
00142                         virtual CRenderizable& setColor_u8(const mrpt::utils::TColor &c);
00143                         virtual CRenderizable& setColorR_u8(const uint8_t r);
00144                         virtual CRenderizable& setColorG_u8(const uint8_t g);
00145                         virtual CRenderizable& setColorB_u8(const uint8_t b);
00146                         virtual CRenderizable& setColorA_u8(const uint8_t a);
00147 
00148                         /** Render
00149                           */
00150                         void  render_dl() const;
00151 
00152                         /** Ray tracing
00153                           */
00154                         virtual bool traceRay(const mrpt::poses::CPose3D &o,double &dist) const;
00155 
00156                         /**
00157                           * Gets the polygon cache.
00158                           * \sa insertTriangles
00159                           */
00160                         void getPolygons(std::vector<mrpt::math::TPolygon3D> &polys) const;
00161 
00162                         /**
00163                           * Inserts a set of triangles, given in a container of either TTriangle's or TPolygon3D
00164                           * \sa insertTriangle
00165                           */
00166                         template<class CONTAINER>
00167                         inline void insertTriangles(const CONTAINER &c)  {
00168                                 this->insertTriangles(c.begin(),c.end());
00169                                 CRenderizableDisplayList::notifyChange();
00170                         }
00171 
00172                         /**
00173                           * Gets the beginning iterator to this object.
00174                           */
00175                         inline const_iterator begin() const     {
00176                                 return m_triangles.begin();
00177                         }
00178                         /**
00179                           * Gets the ending iterator to this object.
00180                           */
00181                         inline const_iterator end() const       {
00182                                 return m_triangles.end();
00183                         }
00184                         /**
00185                           * Gets the reverse beginning iterator to this object, which points to the last triangle.
00186                           */
00187                         inline const_reverse_iterator rbegin() const    {
00188                                 return m_triangles.rbegin();
00189                         }
00190                         /**
00191                           * Gets the reverse ending iterator to this object, which points to the beginning of the actual set.
00192                           */
00193                         inline const_reverse_iterator rend() const      {
00194                                 return m_triangles.rend();
00195                         }
00196                 private:
00197                         /** Constructor
00198                           */
00199                         CSetOfTriangles( bool enableTransparency = false ) :
00200                                 m_triangles(),
00201                                 m_enableTransparency(enableTransparency),
00202                                 polygonsUpToDate(false)
00203                         {
00204                         }
00205 
00206                         /** Private, virtual destructor: only can be deleted from smart pointers */
00207                         virtual ~CSetOfTriangles() { }
00208                 };
00209                 /** Inserts a set of triangles into the list; note that this method allows to pass another CSetOfTriangles as argument. Allows call chaining.
00210                   * \sa mrpt::opengl::CSetOfTriangles::insertTriangle
00211                   */
00212                 template<class T> inline CSetOfTrianglesPtr &operator<<(CSetOfTrianglesPtr &s,const T &t)       {
00213                         s->insertTriangles(t.begin(),t.end());
00214                         return s;
00215                 }
00216                 /** Inserts a triangle into the list. Allows call chaining.
00217                   * \sa mrpt::opengl::CSetOfTriangles::insertTriangle
00218                   */
00219                 template<> inline CSetOfTrianglesPtr &operator<<(CSetOfTrianglesPtr &s,const CSetOfTriangles::TTriangle &t)     {
00220                         s->insertTriangle(t);
00221                         return s;
00222                 }
00223 
00224         } // end namespace
00225 
00226 } // End of namespace
00227 
00228 
00229 #endif



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