MRPT  1.9.9
CSetOfTriangles.h
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | https://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2019, Individual contributors, see AUTHORS file |
6  | See: https://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See: https://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 #pragma once
10 
11 #include <mrpt/math/geometry.h>
13 
14 namespace mrpt::opengl
15 {
16 /** A set of colored triangles.
17  * This class can be used to draw any solid, arbitrarily complex object
18  * (without textures).
19  * \sa opengl::COpenGLScene, CSetOfTexturedTriangles
20  * \ingroup mrpt_opengl_grp
21  */
23 {
25  public:
26  /**
27  * Triangle definition. Each vertex has three spatial coordinates and four
28  * color values.
29  */
30  struct TTriangle
31  {
32  inline TTriangle()
33  {
34  for (size_t i = 0; i < 3; i++)
35  {
36  r[i] = g[i] = b[i] = a[i] = 1.0f;
37  }
38  }
40  {
41  ASSERT_(p.size() == 3);
42  for (size_t i = 0; i < 3; i++)
43  {
44  x[i] = p[i].x;
45  y[i] = p[i].y;
46  z[i] = p[i].z;
47  r[i] = g[i] = b[i] = a[i] = 1.0f;
48  }
49  }
50  float x[3], y[3], z[3];
51  float r[3], g[3], b[3], a[3];
52  };
53  using const_iterator = std::vector<TTriangle>::const_iterator;
55  std::vector<TTriangle>::const_reverse_iterator;
56 
57  protected:
58  /**
59  * List of triangles.
60  * \sa TTriangle
61  */
62  std::vector<TTriangle> m_triangles;
63  /**
64  * Transparency enabling.
65  */
67  /**
68  * Mutable variable used to check whether polygons need to be recalculated.
69  */
70  mutable bool polygonsUpToDate{false};
71  /**
72  * Polygon cache.
73  */
74  mutable std::vector<mrpt::math::TPolygonWithPlane> tmpPolygons;
75 
76  public:
77  /**
78  * Polygon cache updating.
79  */
80  void updatePolygons() const;
81  /**
82  * Clear this object.
83  */
84  inline void clearTriangles()
85  {
86  m_triangles.clear();
87  polygonsUpToDate = false;
89  }
90  /**
91  * Get triangle count.
92  */
93  inline size_t getTrianglesCount() const { return m_triangles.size(); }
94  /**
95  * Gets the triangle in a given position.
96  */
97  inline void getTriangle(size_t idx, TTriangle& t) const
98  {
99  ASSERT_(idx < m_triangles.size());
100  t = m_triangles[idx];
101  }
102  /**
103  * Inserts a triangle into the set.
104  */
105  inline void insertTriangle(const TTriangle& t)
106  {
107  m_triangles.push_back(t);
108  polygonsUpToDate = false;
110  }
111  /**
112  * Inserts a set of triangles, bounded by iterators, into this set.
113  * \sa insertTriangle
114  */
115  template <class InputIterator>
116  inline void insertTriangles(
117  const InputIterator& begin, const InputIterator& end)
118  {
119  m_triangles.insert(m_triangles.end(), begin, end);
120  polygonsUpToDate = false;
122  }
123  /**
124  * Inserts an existing CSetOfTriangles into this one.
125  */
127  /**
128  * Reserves memory for certain number of triangles, avoiding multiple
129  * memory allocation calls.
130  */
131  inline void reserve(size_t t)
132  {
133  m_triangles.reserve(t);
135  }
136 
137  /** Enables or disables transparency. */
138  inline void enableTransparency(bool v)
139  {
142  }
143 
144  CRenderizable& setColor_u8(const mrpt::img::TColor& c) override;
145  CRenderizable& setColorR_u8(const uint8_t r) override;
146  CRenderizable& setColorG_u8(const uint8_t g) override;
147  CRenderizable& setColorB_u8(const uint8_t b) override;
148  CRenderizable& setColorA_u8(const uint8_t a) override;
149 
150  /** Render
151  */
152  void render_dl() const override;
153 
154  /** Ray tracing
155  */
156  bool traceRay(const mrpt::poses::CPose3D& o, double& dist) const override;
157 
158  /**
159  * Gets the polygon cache.
160  * \sa insertTriangles
161  */
162  void getPolygons(std::vector<mrpt::math::TPolygon3D>& polys) const;
163 
164  /**
165  * Inserts a set of triangles, given in a container of either TTriangle's
166  * or TPolygon3D
167  * \sa insertTriangle
168  */
169  template <class CONTAINER>
170  inline void insertTriangles(const CONTAINER& c)
171  {
172  this->insertTriangles(c.begin(), c.end());
174  }
175 
176  /**
177  * Gets the beginning iterator to this object.
178  */
179  inline const_iterator begin() const { return m_triangles.begin(); }
180  /**
181  * Gets the ending iterator to this object.
182  */
183  inline const_iterator end() const { return m_triangles.end(); }
184  /**
185  * Gets the reverse beginning iterator to this object, which points to the
186  * last triangle.
187  */
189  {
190  return m_triangles.rbegin();
191  }
192  /**
193  * Gets the reverse ending iterator to this object, which points to the
194  * beginning of the actual set.
195  */
196  inline const_reverse_iterator rend() const { return m_triangles.rend(); }
197  /** Evaluates the bounding box of this object (including possible children)
198  * in the coordinate frame of the object parent. */
199  void getBoundingBox(
201  mrpt::math::TPoint3D& bb_max) const override;
202 
203  /** Constructor
204  */
207 
208  {
209  }
210 
211  /** Private, virtual destructor: only can be deleted from smart pointers */
212  ~CSetOfTriangles() override = default;
213 };
214 /** Inserts a set of triangles into the list; note that this method allows to
215  * pass another CSetOfTriangles as argument. Allows call chaining.
216  * \sa mrpt::opengl::CSetOfTriangles::insertTriangle
217  */
218 template <class T>
220 {
221  s->insertTriangles(t.begin(), t.end());
222  return s;
223 }
224 /** Inserts a triangle into the list. Allows call chaining.
225  * \sa mrpt::opengl::CSetOfTriangles::insertTriangle
226  */
227 template <>
230 {
231  s->insertTriangle(t);
232  return s;
233 }
234 } // namespace mrpt::opengl
void getTriangle(size_t idx, TTriangle &t) const
Gets the triangle in a given position.
const_reverse_iterator rend() const
Gets the reverse ending iterator to this object, which points to the beginning of the actual set...
void notifyChange() const
Must be called to notify that the object has changed (so, the display list must be updated) ...
GLdouble GLdouble t
Definition: glext.h:3695
GLdouble GLdouble z
Definition: glext.h:3879
CRenderizable & setColorG_u8(const uint8_t g) override
Color components in the range [0,255].
bool traceRay(const mrpt::poses::CPose3D &o, double &dist) const override
Ray tracing.
void getPolygons(std::vector< mrpt::math::TPolygon3D > &polys) const
Gets the polygon cache.
mrpt::serialization::CArchive & operator<<(mrpt::serialization::CArchive &out, const mrpt::opengl::CLight &o)
Definition: CLight.cpp:123
const_iterator end() const
Gets the ending iterator to this object.
The base class of 3D objects that can be directly rendered through OpenGL.
Definition: CRenderizable.h:40
std::vector< mrpt::math::TPolygonWithPlane > tmpPolygons
Polygon cache.
CRenderizable & setColor_u8(const mrpt::img::TColor &c) override
Changes the default object color.
size_t getTrianglesCount() const
Get triangle count.
GLdouble s
Definition: glext.h:3682
const_iterator begin() const
Gets the beginning iterator to this object.
CRenderizable & setColorR_u8(const uint8_t r) override
Color components in the range [0,255].
TTriangle(const mrpt::math::TPolygon3D &p)
unsigned char uint8_t
Definition: rptypes.h:44
A renderizable object suitable for rendering with OpenGL&#39;s display lists.
#define ASSERT_(f)
Defines an assertion mechanism.
Definition: exceptions.h:120
const GLubyte * c
Definition: glext.h:6406
void insertTriangle(const TTriangle &t)
Inserts a triangle into the set.
GLuint GLuint end
Definition: glext.h:3532
void render_dl() const override
Render.
GLubyte g
Definition: glext.h:6372
GLubyte GLubyte b
Definition: glext.h:6372
CSetOfTriangles(bool enableTransparency=false)
Constructor.
void getBoundingBox(mrpt::math::TPoint3D &bb_min, mrpt::math::TPoint3D &bb_max) const override
Evaluates the bounding box of this object (including possible children) in the coordinate frame of th...
std::vector< TTriangle > m_triangles
List of triangles.
void enableTransparency(bool v)
Enables or disables transparency.
void updatePolygons() const
Polygon cache updating.
const GLdouble * v
Definition: glext.h:3684
#define DEFINE_SERIALIZABLE(class_name)
This declaration must be inserted in all CSerializable classes definition, within the class declarati...
std::vector< TTriangle >::const_iterator const_iterator
const_reverse_iterator rbegin() const
Gets the reverse beginning iterator to this object, which points to the last triangle.
GLdouble GLdouble GLdouble r
Definition: glext.h:3711
A class used to store a 3D pose (a 3D translation + a rotation in 3D).
Definition: CPose3D.h:84
The namespace for 3D scene representation and rendering.
Definition: CGlCanvasBase.h:15
const auto bb_max
void reserve(size_t t)
Reserves memory for certain number of triangles, avoiding multiple memory allocation calls...
GLenum GLint GLint y
Definition: glext.h:3542
A set of colored triangles.
const auto bb_min
void clearTriangles()
Clear this object.
std::vector< TTriangle >::const_reverse_iterator const_reverse_iterator
A RGB color - 8bit.
Definition: TColor.h:20
GLenum GLint x
Definition: glext.h:3542
Lightweight 3D point.
Definition: TPoint3D.h:90
void insertTriangles(const CONTAINER &c)
Inserts a set of triangles, given in a container of either TTriangle&#39;s or TPolygon3D.
bool polygonsUpToDate
Mutable variable used to check whether polygons need to be recalculated.
CRenderizable & setColorB_u8(const uint8_t b) override
Color components in the range [0,255].
GLubyte GLubyte GLubyte a
Definition: glext.h:6372
bool m_enableTransparency
Transparency enabling.
GLfloat GLfloat p
Definition: glext.h:6398
CRenderizable & setColorA_u8(const uint8_t a) override
Color components in the range [0,255].
void insertTriangles(const InputIterator &begin, const InputIterator &end)
Inserts a set of triangles, bounded by iterators, into this set.
3D polygon, inheriting from std::vector<TPoint3D>
Definition: TPolygon3D.h:18
~CSetOfTriangles() override=default
Private, virtual destructor: only can be deleted from smart pointers.



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: ee555d257 Fri Aug 16 10:05:39 2019 +0200 at vie ago 16 10:10:14 CEST 2019