Main MRPT website > C++ reference
MRPT logo
CSetOfTriangles.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 #ifndef opengl_CSetOfTriangles_H
29 #define opengl_CSetOfTriangles_H
30 
32 #include <mrpt/math/geometry.h>
33 
34 namespace mrpt
35 {
36  namespace opengl
37  {
39 
40  // This must be added to any CSerializable derived class:
42 
43  /** A set of colored triangles.
44  * This class can be used to draw any solid, arbitrarily complex object (without textures).
45  * \sa opengl::COpenGLScene, CSetOfTexturedTriangles
46  * \ingroup mrpt_opengl_grp
47  */
49  {
51  public:
52  /**
53  * Triangle definition. Each vertex has three spatial coordinates and four color values.
54  */
56  {
57  inline TTriangle() { }
58  inline TTriangle(const mrpt::math::TPolygon3D &p) {
59  ASSERT_(p.size()==3)
60  for (size_t i=0;i<3;i++) {
61  x[i]=p[i].x; y[i]=p[i].y; z[i]=p[i].z; r[i]=g[i]=b[i]=a[i]=1; }
62  }
63  float x[3],y[3],z[3];
64  float r[3],g[3],b[3],a[3];
65  };
66  /**
67  * Const iterator type.
68  */
70  /**
71  * Const reverse iterator type.
72  */
73  typedef std::vector<TTriangle>::const_reverse_iterator const_reverse_iterator;
74  protected:
75  /**
76  * List of triangles.
77  * \sa TTriangle
78  */
79  std::vector<TTriangle> m_triangles;
80  /**
81  * Transparency enabling.
82  */
84  /**
85  * Mutable variable used to check whether polygons need to be recalculated.
86  */
87  mutable bool polygonsUpToDate;
88  /**
89  * Polygon cache.
90  */
91  mutable std::vector<mrpt::math::TPolygonWithPlane> tmpPolygons;
92  public:
93  /**
94  * Polygon cache updating.
95  */
96  void updatePolygons() const;
97  /**
98  * Clear this object.
99  */
100  inline void clearTriangles() { m_triangles.clear();polygonsUpToDate=false; CRenderizableDisplayList::notifyChange(); }
101  /**
102  * Get triangle count.
103  */
104  inline size_t getTrianglesCount() const { return m_triangles.size(); }
105  /**
106  * Gets the triangle in a given position.
107  */
108  inline void getTriangle(size_t idx, TTriangle &t) const { ASSERT_(idx<m_triangles.size()); t=m_triangles[idx]; }
109  /**
110  * Inserts a triangle into the set.
111  */
112  inline void insertTriangle( const TTriangle &t ) { m_triangles.push_back(t);polygonsUpToDate=false; CRenderizableDisplayList::notifyChange(); }
113  /**
114  * Inserts a set of triangles, bounded by iterators, into this set.
115  * \sa insertTriangle
116  */
117  template<class InputIterator> inline void insertTriangles(const InputIterator &begin,const InputIterator &end) {
118  m_triangles.insert(m_triangles.end(),begin,end);
119  polygonsUpToDate=false;
121  }
122  /**
123  * Inserts an existing CSetOfTriangles into this one.
124  */
125  inline void insertTriangles(const CSetOfTrianglesPtr &p) {
126  reserve(m_triangles.size()+p->m_triangles.size());
127  m_triangles.insert(m_triangles.end(),p->m_triangles.begin(),p->m_triangles.end());
128  polygonsUpToDate=false;
130  }
131  /**
132  * Reserves memory for certain number of triangles, avoiding multiple memory allocation calls.
133  */
134  inline void reserve(size_t t) {
135  m_triangles.reserve(t);
137  }
138 
139  /** Enables or disables transparency. */
140  inline void enableTransparency( bool v ) { m_enableTransparency = v; CRenderizableDisplayList::notifyChange(); }
141 
142  virtual CRenderizable& setColor_u8(const mrpt::utils::TColor &c);
143  virtual CRenderizable& setColorR_u8(const uint8_t r);
144  virtual CRenderizable& setColorG_u8(const uint8_t g);
145  virtual CRenderizable& setColorB_u8(const uint8_t b);
146  virtual CRenderizable& setColorA_u8(const uint8_t a);
147 
148  /** Render
149  */
150  void render_dl() const;
151 
152  /** Ray tracing
153  */
154  virtual bool traceRay(const mrpt::poses::CPose3D &o,double &dist) const;
155 
156  /**
157  * Gets the polygon cache.
158  * \sa insertTriangles
159  */
160  void getPolygons(std::vector<mrpt::math::TPolygon3D> &polys) const;
161 
162  /**
163  * Inserts a set of triangles, given in a container of either TTriangle's or TPolygon3D
164  * \sa insertTriangle
165  */
166  template<class CONTAINER>
167  inline void insertTriangles(const CONTAINER &c) {
168  this->insertTriangles(c.begin(),c.end());
170  }
171 
172  /**
173  * Gets the beginning iterator to this object.
174  */
175  inline const_iterator begin() const {
176  return m_triangles.begin();
177  }
178  /**
179  * Gets the ending iterator to this object.
180  */
181  inline const_iterator end() const {
182  return m_triangles.end();
183  }
184  /**
185  * Gets the reverse beginning iterator to this object, which points to the last triangle.
186  */
187  inline const_reverse_iterator rbegin() const {
188  return m_triangles.rbegin();
189  }
190  /**
191  * Gets the reverse ending iterator to this object, which points to the beginning of the actual set.
192  */
193  inline const_reverse_iterator rend() const {
194  return m_triangles.rend();
195  }
196  private:
197  /** Constructor
198  */
199  CSetOfTriangles( bool enableTransparency = false ) :
200  m_triangles(),
201  m_enableTransparency(enableTransparency),
202  polygonsUpToDate(false)
203  {
204  }
205 
206  /** Private, virtual destructor: only can be deleted from smart pointers */
207  virtual ~CSetOfTriangles() { }
208  };
209  /** Inserts a set of triangles into the list; note that this method allows to pass another CSetOfTriangles as argument. Allows call chaining.
210  * \sa mrpt::opengl::CSetOfTriangles::insertTriangle
211  */
212  template<class T> inline CSetOfTrianglesPtr &operator<<(CSetOfTrianglesPtr &s,const T &t) {
213  s->insertTriangles(t.begin(),t.end());
214  return s;
215  }
216  /** Inserts a triangle into the list. Allows call chaining.
217  * \sa mrpt::opengl::CSetOfTriangles::insertTriangle
218  */
220  s->insertTriangle(t);
221  return s;
222  }
223 
224  } // end namespace
225 
226 } // End of namespace
227 
228 
229 #endif



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