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://mrpt.sourceforge.net/                            |
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                   */
00047                 class OPENGL_IMPEXP CSetOfTriangles : public CRenderizableDisplayList
00048                 {
00049                         DEFINE_SERIALIZABLE( CSetOfTriangles )
00050                 public:
00051                         /**
00052                           * Triangle definition. Each vertex has three spatial coordinates and four color values.
00053                           */
00054                         struct OPENGL_IMPEXP TTriangle
00055                         {
00056                                 inline TTriangle() {  }
00057                                 inline TTriangle(const mrpt::math::TPolygon3D &p)  {
00058                                         ASSERT_(p.size()==3)
00059                                         for (size_t i=0;i<3;i++) {
00060                                                 x[i]=p[i].x; y[i]=p[i].y; z[i]=p[i].z; r[i]=g[i]=b[i]=a[i]=1; }
00061                                 }
00062                                 float   x[3],y[3],z[3];
00063                                 float   r[3],g[3],b[3],a[3];
00064                         };
00065                         /**
00066                           * Const iterator type.
00067                           */
00068                         typedef std::vector<TTriangle>::const_iterator const_iterator;
00069                         /**
00070                           * Const reverse iterator type.
00071                           */
00072                         typedef std::vector<TTriangle>::const_reverse_iterator const_reverse_iterator;
00073                 protected:
00074                         /**
00075                           * List of triangles.
00076                           * \sa TTriangle
00077                           */
00078                         std::vector<TTriangle>          m_triangles;
00079                         /**
00080                           * Transparency enabling.
00081                           */
00082                         bool                                            m_enableTransparency;
00083                         /**
00084                           * Mutable variable used to check whether polygons need to be recalculated.
00085                           */
00086                         mutable bool polygonsUpToDate;
00087                         /**
00088                           * Polygon cache.
00089                           */
00090                         mutable std::vector<mrpt::math::TPolygonWithPlane> tmpPolygons;
00091                 public:
00092                         /**
00093                           * Polygon cache updating.
00094                           */
00095                         void updatePolygons() const;
00096                         /**
00097                           * Clear this object.
00098                           */
00099                         inline void clearTriangles() { m_triangles.clear();polygonsUpToDate=false; CRenderizableDisplayList::notifyChange(); }
00100                         /**
00101                           * Get triangle count.
00102                           */
00103                         inline size_t getTrianglesCount() const { return m_triangles.size(); }
00104                         /**
00105                           * Gets the triangle in a given position.
00106                           */
00107                         inline void getTriangle(size_t idx, TTriangle &t) const { ASSERT_(idx<m_triangles.size()); t=m_triangles[idx]; }
00108                         /**
00109                           * Inserts a triangle into the set.
00110                           */
00111                         inline void insertTriangle( const TTriangle &t ) { m_triangles.push_back(t);polygonsUpToDate=false; CRenderizableDisplayList::notifyChange(); }
00112                         /**
00113                           * Inserts a set of triangles, bounded by iterators, into this set.
00114                           * \sa insertTriangle
00115                           */
00116                         template<class InputIterator> inline void insertTriangles(const InputIterator &begin,const InputIterator &end)  {
00117                                 m_triangles.insert(m_triangles.end(),begin,end);
00118                                 polygonsUpToDate=false;
00119                                 CRenderizableDisplayList::notifyChange();
00120                         }
00121                         /**
00122                           * Inserts an existing CSetOfTriangles into this one.
00123                           */
00124                         inline void insertTriangles(const CSetOfTrianglesPtr &p)        {
00125                                 reserve(m_triangles.size()+p->m_triangles.size());
00126                                 m_triangles.insert(m_triangles.end(),p->m_triangles.begin(),p->m_triangles.end());
00127                                 polygonsUpToDate=false;
00128                                 CRenderizableDisplayList::notifyChange();
00129                         }
00130                         /**
00131                           * Reserves memory for certain number of triangles, avoiding multiple memory allocation calls.
00132                           */
00133                         inline void reserve(size_t t)   {
00134                                 m_triangles.reserve(t);
00135                                 CRenderizableDisplayList::notifyChange();
00136                         }
00137 
00138                         /** Enables or disables transparency. */
00139                         inline void enableTransparency( bool v )        { m_enableTransparency = v; CRenderizableDisplayList::notifyChange(); }
00140 
00141                         virtual CRenderizable& setColor(const mrpt::utils::TColorf &c);
00142                         virtual CRenderizable& setColor(double r,double g,double b,double a=1);
00143                         virtual CRenderizable& setColorR(const double r);
00144                         virtual CRenderizable& setColorG(const double g);
00145                         virtual CRenderizable& setColorB(const double b);
00146                         virtual CRenderizable& setColorA(const double 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.3 for MRPT 0.9.4 SVN:exported at Tue Jan 25 21:56:31 UTC 2011