Main MRPT website > C++ reference
MRPT logo
CPolyhedron.h
Go to the documentation of this file.
1 /* +---------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2014, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://www.mrpt.org/License |
8  +---------------------------------------------------------------------------+ */
9 #ifndef opengl_CPolyhedron_H
10 #define opengl_CPolyhedron_H
11 
13 #include <mrpt/math/geometry.h>
14 
15 namespace mrpt {
16 namespace opengl {
17  using namespace mrpt::utils;
18  using namespace mrpt::poses;
19  using namespace mrpt::math;
20  using namespace std;
21 
23 
24  // This must be added to any CSerializable derived class:
26  /**
27  * This class represents arbitrary polyhedra. The class includes a set of static methods to create common polyhedrons. The class includes many methods to create standard polyhedra, not intended to be fast but to be simple. For example, the dodecahedron is not created efficiently: first, an icosahedron is created, and a duality operator is applied to it, which yields the dodecahedron. This way, code is much smaller, although much slower. This is not a big problem, since polyhedron creation does not usually take a significant amount of time (they are created once and rendered many times).
28  * Polyhedra information and models have been gotten from the Wikipedia, http://wikipedia.org
29  * \sa opengl::COpenGLScene
30  *
31  * <div align="center">
32  * <table border="0" cellspan="4" cellspacing="4" style="border-width: 1px; border-style: solid;">
33  * <tr> <td> mrpt::opengl::CPolyhedron </td> <td> \image html preview_CPolyhedron.png </td> </tr>
34  * </table>
35  * </div>
36  *
37  * \ingroup mrpt_opengl_grp
38  */
41  public:
42  /**
43  * Struct used to store a polyhedron edge. The struct consists only of two vertex indices, used to access the polyhedron vertex list.
44  */
46  /**
47  * First vertex.
48  */
49  uint32_t v1;
50  /**
51  * Second vertex.
52  */
53  uint32_t v2;
54  /**
55  * Default constructor. Initializes to garbage.
56  */
57  TPolyhedronEdge():v1(),v2() {}
58  /**
59  * Comparison agains another edge. Simmetry is taken into account.
60  */
61  bool operator==(const TPolyhedronEdge &e) const {
62  if (e.v1==v1&&e.v2==v2) return true;
63  else return e.v1==v2&&e.v2==v1;
64  }
65  /**
66  * Given a set of vertices, computes the length of the vertex.
67  */
68  double length(const vector<TPoint3D> &vs) const;
69  /**
70  * Destructor.
71  */
73  };
74  /**
75  * Struct used to store a polyhedron face. Consists on a set of vertex indices and a normal vector.
76  */
78  /**
79  * Vector of indices to the vertex list.
80  */
81  vector<uint32_t> vertices;
82  /**
83  * Normal vector.
84  */
85  double normal[3];
86  /**
87  * Fast default constructor. Initializes to garbage.
88  */
89  TPolyhedronFace():vertices() {}
90  /**
91  * Destructor.
92  */
94  /**
95  * Given a set of vertices, computes the area of this face.
96  */
97  double area(const vector<TPoint3D> &vertices) const;
98  /**
99  * Given a set of vertices, get this face's center.
100  */
101  void getCenter(const vector<TPoint3D> &vertices,TPoint3D &p) const;
102  };
103  protected:
104  /**
105  * List of vertices presents in the polyhedron.
106  */
107  vector<TPoint3D> mVertices;
108  /**
109  * List of polyhedron's edges.
110  */
111  vector<TPolyhedronEdge> mEdges;
112  /**
113  * List of polyhedron's faces.
114  */
115  vector<TPolyhedronFace> mFaces;
116  /**
117  * This flag determines whether the polyhedron will be displayed as a solid object or as a set of edges.
118  */
120  /**
121  * When displaying as wireframe object, this variable stores the width of the edges.
122  */
123  double mLineWidth;
124  /**
125  * Mutable list of actual polygons, maintained for speed.
126  */
127  mutable std::vector<TPolygonWithPlane> tempPolygons;
128  /**
129  * Whether the set of actual polygons is up to date or not.
130  */
131  mutable bool polygonsUpToDate;
132  public:
133  /**
134  * Creation of a polyhedron from its vertices and faces.
135  * \throw logic_error if the polyhedron definition has flaws (bad vertex indices, etc.).
136  */
137  static CPolyhedronPtr Create(const vector<TPoint3D> &vertices,const vector<vector<uint32_t> > &faces);
138  /**
139  * Creation of a polyhedron from its vertices and faces.
140  * \throw logic_error if the polyhedron definition has flaws (bad vertex indices, etc.).
141  */
142  static CPolyhedronPtr Create(const vector<TPoint3D> &vertices,const vector<TPolyhedronFace> &faces);
143  /**
144  * Creation from a set of polygons.
145  * \sa mrpt::math::TPolygon3D
146  */
147  static CPolyhedronPtr Create(const std::vector<math::TPolygon3D> &polys);
148 
149  /** Evaluates the bounding box of this object (including possible children) in the coordinate frame of the object parent. */
150  virtual void getBoundingBox(mrpt::math::TPoint3D &bb_min, mrpt::math::TPoint3D &bb_max) const;
151 
152  //Static methods to create frequent polyhedra. More bizarre polyhedra are intended to be added in a near future.
153 
154  /** @name Platonic solids.
155  @{
156  */
157  /**
158  * Creates a regular tetrahedron (see http://en.wikipedia.org/wiki/Tetrahedron). The tetrahedron is created as a triangular pyramid whose edges and vertices are transitive.
159  * The tetrahedron is the dual to itself.
160  <p align="center"><img src="Tetrahedron.gif"></p>
161  * \sa CreatePyramid,CreateJohnsonSolidWithConstantBase,CreateTruncatedTetrahedron
162  */
163  static CPolyhedronPtr CreateTetrahedron(double radius);
164  /**
165  * Creates a regular cube, also called hexahedron (see http://en.wikipedia.org/wiki/Hexahedron). The hexahedron is created as a cubic prism which transitive edges. Another ways to create it include:
166  <ul><li>Dual to an octahedron.</li><li>Parallelepiped with three orthogonal, equally-lengthed vectors.</li><li>Triangular trapezohedron with proper height.</li></ul>
167  <p align="center"><img src="Hexahedron.gif"></p>
168  * \sa CreateOctahedron,getDual,CreateParallelepiped,CreateTrapezohedron,CreateTruncatedHexahedron,CreateTruncatedOctahedron,CreateCuboctahedron,CreateRhombicuboctahedron
169  */
170  static CPolyhedronPtr CreateHexahedron(double radius);
171  /**
172  * Creates a regular octahedron (see http://en.wikipedia.org/wiki/Octahedron). The octahedron is created as a square bipyramid whit transitive edges and vertices. Another ways to create an octahedron are:
173  <ul><li>Dual to an hexahedron</li><li>Triangular antiprism with transitive vertices.</li><li>Conveniently truncated tetrahedron.</li></ul>
174  <p align="center"><img src="Octahedron.gif"></p>
175  * \sa CreateHexahedron,getDual,CreateArchimedeanAntiprism,CreateTetrahedron,truncate,CreateTruncatedOctahedron,CreateTruncatedHexahedron,CreateCuboctahedron,CreateRhombicuboctahedron
176  */
177  static CPolyhedronPtr CreateOctahedron(double radius);
178  /**
179  * Creates a regular dodecahedron (see http://en.wikipedia.org/wiki/Dodecahedron). The dodecahedron is created as the dual to an icosahedron.
180  <p align="center"><img src="Dodecahedron.gif"></p>
181  * \sa CreateIcosahedron,getDual,CreateTruncatedDodecahedron,CreateTruncatedIcosahedron,CreateIcosidodecahedron,CreateRhombicosidodecahedron
182  */
183  static CPolyhedronPtr CreateDodecahedron(double radius);
184  /**
185  * Creates a regular icosahedron (see http://en.wikipedia.org/wiki/Icosahedron). The icosahedron is created as a gyroelongated pentagonal bipyramid with transitive edges, and it's the dual to a dodecahedron.
186  <p align="center"><img src="Icosahedron.gif"></p>
187  * \sa CreateJohnsonSolidWithConstantBase,CreateDodecahedron,getDual,CreateTruncatedIcosahedron,CreateTruncatedDodecahedron,CreateIcosidodecahedron,CreateRhombicosidodecahedron
188  */
189  static CPolyhedronPtr CreateIcosahedron(double radius);
190  /** @}
191  */
192 
193  /** @name Archimedean solids.
194  @{
195  */
196  /**
197  * Creates a truncated tetrahedron, consisting of four triangular faces and for hexagonal ones (see http://en.wikipedia.org/wiki/Truncated_tetrahedron). Its dual is the triakis tetrahedron.
198  <p align="center"><img src="Truncatedtetrahedron.gif"></p>
199  * \sa CreateTetrahedron,CreateTriakisTetrahedron
200  */
201  static CPolyhedronPtr CreateTruncatedTetrahedron(double radius);
202  /**
203  * Creates a cuboctahedron, consisting of six square faces and eight triangular ones (see http://en.wikipedia.org/wiki/Cuboctahedron). There are several ways to create a cuboctahedron:
204  <ul><li>Hexahedron truncated to a certain extent.</li><li>Octahedron truncated to a certain extent.</li><li>Cantellated tetrahedron</li><li>Dual to a rhombic dodecahedron.</li></ul>
205  <p align="center"><img src="Cuboctahedron.gif"></p>
206  * \sa CreateHexahedron,CreateOctahedron,truncate,CreateTetrahedron,cantellate,CreateRhombicuboctahedron,CreateRhombicDodecahedron,
207  */
208  static CPolyhedronPtr CreateCuboctahedron(double radius);
209  /**
210  * Creates a truncated hexahedron, with six octogonal faces and eight triangular ones (see http://en.wikipedia.org/wiki/Truncated_hexahedron). The truncated octahedron is dual to the triakis octahedron.
211  <p align="center"><img src="Truncatedhexahedron.gif"></p>
212  * \sa CreateHexahedron,CreateTriakisOctahedron
213  */
214  static CPolyhedronPtr CreateTruncatedHexahedron(double radius);
215  /**
216  * Creates a truncated octahedron, with eight hexagons and eight squares (see http://en.wikipedia.org/wiki/Truncated_octahedron). It's the dual to the tetrakis hexahedron.
217  <p align="center"><img src="Truncatedoctahedron.gif"></p>
218  * \sa CreateOctahedron,CreateTetrakisHexahedron
219  */
220  static CPolyhedronPtr CreateTruncatedOctahedron(double radius);
221  /**
222  * Creates a rhombicuboctahedron, with 18 squares and 8 triangles (see http://en.wikipedia.org/wiki/Rhombicuboctahedron), calculated as an elongated square bicupola. It can also be calculated as a cantellated hexahedron or octahedron, and its dual is the deltoidal icositetrahedron.
223  * If the second argument is set to false, the lower cupola is rotated, so that the objet created is an elongated square gyrobicupola (see http://en.wikipedia.org/wiki/Elongated_square_gyrobicupola). This is not an archimedean solid, but a Johnson one, since it hasn't got vertex transitivity.
224  <p align="center"><img src="Rhombicuboctahedron.gif"></p>
225  * \sa CreateJohnsonSolidWithConstantBase,CreateHexahedron,CreateOctahedron,cantellate,CreateCuboctahedron,CreateDeltoidalIcositetrahedron
226  */
227  static CPolyhedronPtr CreateRhombicuboctahedron(double radius,bool type=true);
228  /**
229  * Creates an icosidodecahedron, with 12 pentagons and 20 triangles (see http://en.wikipedia.org/wiki/Icosidodecahedron). Certain truncations of either a dodecahedron or an icosahedron yield an icosidodecahedron.
230  * The dual of the icosidodecahedron is the rhombic triacontahedron.
231  * If the second argument is set to false, the lower rotunda is rotated. In this case, the object created is a pentagonal orthobirotunda (see http://en.wikipedia.org/wiki/Pentagonal_orthobirotunda). This object presents symmetry against the XY plane and is not vertex transitive, so it's a Johnson's solid.
232  <p align="center"><img src="Icosidodecahedron.gif"></p>
233  * \sa CreateDodecahedron,CreateIcosahedron,truncate,CreateRhombicosidodecahedron,CreateRhombicTriacontahedron
234  */
235  static CPolyhedronPtr CreateIcosidodecahedron(double radius,bool type=true);
236  /**
237  * Creates a truncated dodecahedron, consisting of 12 dodecagons and 20 triangles (see http://en.wikipedia.org/wiki/Truncated_dodecahedron). The truncated dodecahedron is the dual to the triakis icosahedron.
238  <p align="center"><img src="Truncateddodecahedron.gif"></p>
239  * \sa CreateDodecahedron,CreateTriakisIcosahedron
240  */
241  static CPolyhedronPtr CreateTruncatedDodecahedron(double radius);
242  /**
243  * Creates a truncated icosahedron, consisting of 20 hexagons and 12 pentagons. This object resembles a typical soccer ball (see http://en.wikipedia.org/wiki/Truncated_icosahedron). The pentakis dodecahedron is the dual to the truncated icosahedron.
244  <p align="center"><img src="Truncatedicosahedron.gif"></p>
245  * \sa CreateIcosahedron,CreatePentakisDodecahedron
246  */
247  static CPolyhedronPtr CreateTruncatedIcosahedron(double radius);
248  /**
249  * Creates a rhombicosidodecahedron, consisting of 30 squares, 12 pentagons and 20 triangles (see http://en.wikipedia.org/wiki/Rhombicosidodecahedron). This object can be obtained as the cantellation of either a dodecahedron or an icosahedron. The dual of the rhombicosidodecahedron is the deltoidal hexecontahedron.
250  <p align="center"><img src="Rhombicosidodecahedron.gif"></p>
251  * \sa CreateDodecahedron,CreateIcosahedron,CreateIcosidodecahedron,CreateDeltoidalHexecontahedron
252  */
253  static CPolyhedronPtr CreateRhombicosidodecahedron(double radius);
254  /** @}
255  */
256 
257  /** @name Other Johnson solids.
258  @{
259  */
260  /**
261  * Creates a pentagonal rotunda (half an icosidodecahedron), consisting of six pentagons, ten triangles and a decagon (see http://en.wikipedia.org/wiki/Pentagonal_rotunda).
262  * \sa CreateIcosidodecahedron,CreateJohnsonSolidWithConstantBase
263  */
264  static CPolyhedronPtr CreatePentagonalRotunda(double radius);
265  /** @}
266  */
267 
268  /** @name Catalan solids.
269  @{
270  */
271  /**
272  * Creates a triakis tetrahedron, dual to the truncated tetrahedron. This body consists of 12 isosceles triangles (see http://en.wikipedia.org/wiki/Triakis_tetrahedron).
273  <p align="center"><img src="Triakistetrahedron.gif"></p>
274  * \sa CreateTruncatedTetrahedron
275  */
276  static CPolyhedronPtr CreateTriakisTetrahedron(double radius);
277 
278  /**
279  * Creates a rhombic dodecahedron, dual to the cuboctahedron. This body consists of 12 rhombi (see http://en.wikipedia.org/wiki/Rhombic_dodecahedron).
280  <p align="center"><img src="Rhombicdodecahedron.gif"></p>
281  * \sa CreateCuboctahedron
282  */
283  static CPolyhedronPtr CreateRhombicDodecahedron(double radius);
284 
285  /**
286  * Creates a triakis octahedron, dual to the truncated hexahedron. This body consists of 24 isosceles triangles (see http://en.wikipedia.org/wiki/Triakis_octahedron).
287  <p align="center"><img src="Triakisoctahedron.gif"></p>
288  * \sa CreateTruncatedHexahedron
289  */
290  static CPolyhedronPtr CreateTriakisOctahedron(double radius);
291 
292  /**
293  * Creates a tetrakis hexahedron, dual to the truncated octahedron. This body consists of 24 isosceles triangles (see http://en.wikipedia.org/wiki/Tetrakis_hexahedron).
294  <p align="center"><img src="Tetrakishexahedron.gif"></p>
295  * \sa CreateTruncatedOctahedron
296  */
297  static CPolyhedronPtr CreateTetrakisHexahedron(double radius);
298 
299  /**
300  * Creates a deltoidal icositetrahedron, dual to the rhombicuboctahedron. This body consists of 24 kites (see http://en.wikipedia.org/wiki/Deltoidal_icositetrahedron).
301  <p align="center"><img src="Deltoidalicositetrahedron.gif"></p>
302  * \sa CreateRhombicuboctahedron
303  */
304  static CPolyhedronPtr CreateDeltoidalIcositetrahedron(double radius);
305 
306  /**
307  * Creates a rhombic triacontahedron, dual to the icosidodecahedron. This body consists of 30 rhombi (see http://en.wikipedia.org/wiki/Rhombic_triacontahedron).
308  <p align="center"><img src="Rhombictriacontahedron.gif"></p>
309  * \sa CreateIcosidodecahedron
310  */
311  static CPolyhedronPtr CreateRhombicTriacontahedron(double radius);
312 
313  /**
314  * Creates a triakis icosahedron, dual to the truncated dodecahedron. This body consists of 60 isosceles triangles http://en.wikipedia.org/wiki/Triakis_icosahedron).
315  <p align="center"><img src="Triakisicosahedron.gif"></p>
316  * \sa CreateTruncatedDodecahedron
317  */
318  static CPolyhedronPtr CreateTriakisIcosahedron(double radius);
319 
320  /**
321  * Creates a pentakis dodecahedron, dual to the truncated icosahedron. This body consists of 60 isosceles triangles (see http://en.wikipedia.org/wiki/Pentakis_dodecahedron).
322  <p align="center"><img src="Pentakisdodecahedron.gif"></p>
323  * \sa CreateTruncatedIcosahedron
324  */
325  static CPolyhedronPtr CreatePentakisDodecahedron(double radius);
326 
327  /**
328  * Creates a deltoidal hexecontahedron, dual to the rhombicosidodecahedron. This body consists of 60 kites (see http://en.wikipedia.org/wiki/Deltoidal_hexecontahedron).
329  <p align="center"><img src="Deltoidalhexecontahedron.gif"></p>
330  * \sa CreateRhombicosidodecahedron
331  */
332  static CPolyhedronPtr CreateDeltoidalHexecontahedron(double radius);
333  /** @}
334  */
335 
336  /** @name Customizable polyhedra
337  @{
338  */
339  /**
340  * Creates a cubic prism, given the coordinates of two opposite vertices. Each edge will be parallel to one of the coordinate axes, although the orientation may change by assigning a pose to the object.
341  * \sa CreateCubicPrism(const mrpt::math::TPoint3D &,const mrpt::math::TPoint3D &),CreateParallelepiped,CreateCustomPrism,CreateRegularPrism,CreateArchimedeanRegularPrism
342  */
343  static CPolyhedronPtr CreateCubicPrism(double x1,double x2,double y1,double y2,double z1,double z2);
344  /**
345  * Creates a cubic prism, given two opposite vertices.
346  * \sa CreateCubicPrism(double,double,double,double,double,double),CreateParallelepiped,CreateCustomPrism,CreateRegularPrism,CreateArchimedeanRegularPrism
347  */
348  static CPolyhedronPtr CreateCubicPrism(const TPoint3D &p1,const TPoint3D &p2);
349  /**
350  * Creates a custom pyramid, using a set of 2D vertices which will lie on the XY plane.
351  * \sa CreateDoublePyramid,CreateFrustum,CreateBifrustum,CreateRegularPyramid
352  */
353  static CPolyhedronPtr CreatePyramid(const vector<TPoint2D> &baseVertices,double height);
354  /**
355  * Creates a double pyramid, using a set of 2D vertices which will lie on the XY plane. The second height is used with the downwards pointing pyramid, so that it will effectively point downwards if it's positive.
356  * \sa CreatePyramid,CreateBifrustum,CreateRegularDoublePyramid
357  */
358  static CPolyhedronPtr CreateDoublePyramid(const vector<TPoint2D> &baseVertices,double height1,double height2);
359  /**
360  * Creates a truncated pyramid, using a set of vertices which will lie on the XY plane.
361  * Do not try to use with a ratio equal to zero; use CreatePyramid instead. When using a ratio of 1, it will create a Prism.
362  * \sa CreatePyramid,CreateBifrustum
363  */
364  static CPolyhedronPtr CreateTruncatedPyramid(const vector<TPoint2D> &baseVertices,double height,double ratio);
365  /**
366  * This is a synonym for CreateTruncatedPyramid.
367  * \sa CreateTruncatedPyramid
368  */
369  static CPolyhedronPtr CreateFrustum(const vector<TPoint2D> &baseVertices,double height,double ratio);
370  /**
371  * Creates a custom prism with vertical edges, given any base which will lie on the XY plane.
372  * \sa CreateCubicPrism,CreateCustomAntiprism,CreateRegularPrism,CreateArchimedeanRegularPrism
373  */
374  static CPolyhedronPtr CreateCustomPrism(const vector<TPoint2D> &baseVertices,double height);
375  /**
376  * Creates a custom antiprism, using two custom bases. For better results, the top base should be slightly rotated with respect to the bottom one.
377  * \sa CreateCustomPrism,CreateRegularAntiprism,CreateArchimedeanRegularAntiprism
378  */
379  static CPolyhedronPtr CreateCustomAntiprism(const vector<TPoint2D> &bottomBase,const vector<TPoint2D> &topBase,double height);
380  /**
381  * Creates a parallelepiped, given a base point and three vectors represented as points.
382  * \sa CreateCubicPrism
383  */
384  static CPolyhedronPtr CreateParallelepiped(const TPoint3D &base,const TPoint3D &v1,const TPoint3D &v2,const TPoint3D &v3);
385  /**
386  * Creates a bifrustum, or double truncated pyramid, given a base which will lie on the XY plane.
387  * \sa CreateFrustum,CreateDoublePyramid
388  */
389  static CPolyhedronPtr CreateBifrustum(const vector<TPoint2D> &baseVertices,double height1,double ratio1,double height2,double ratio2);
390  /**
391  * Creates a trapezohedron, consisting of 2*N kites, where N is the number of edges in the base. The base radius controls the polyhedron height, whilst the distance between bases affects the height.
392  * When the number of edges equals 3, the polyhedron is actually a parallelepiped, and it can even be a cube.
393  */
394  static CPolyhedronPtr CreateTrapezohedron(uint32_t numBaseEdges,double baseRadius,double basesDistance);
395  /**
396  * Creates an antiprism whose base is a regular polygon. The upper base is rotated \f$\frac\pi N\f$ with respect to the lower one, where N is the number of vertices in the base, and thus the lateral triangles are isosceles.
397  * \sa CreateCustomAntiprism,CreateArchimedeanRegularAntiprism
398  */
399  static CPolyhedronPtr CreateRegularAntiprism(uint32_t numBaseEdges,double baseRadius,double height);
400  /**
401  * Creates a regular prism whose base is a regular polygon and whose edges are either parallel or perpendicular to the XY plane.
402  * \sa CreateCubicPrism,CreateCustomPrism,CreateArchimedeanRegularAntiprism
403  */
404  static CPolyhedronPtr CreateRegularPrism(uint32_t numBaseEdges,double baseRadius,double height);
405  /**
406  * Creates a regular pyramid whose base is a regular polygon.
407  * \sa CreatePyramid
408  */
409  static CPolyhedronPtr CreateRegularPyramid(uint32_t numBaseEdges,double baseRadius,double height);
410  /**
411  * Creates a regular double pyramid whose base is a regular polygon.
412  * \sa CreateDoublePyramid
413  */
414  static CPolyhedronPtr CreateRegularDoublePyramid(uint32_t numBaseEdges,double baseRadius,double height1,double height2);
415  /**
416  * Creates a regular prism whose lateral area is comprised of squares, and so each face of its is a regular polygon. Due to vertex transitivity, the resulting object is always archimedean.
417  * \sa CreateRegularPrism,CreateCustomPrism
418  */
419  static CPolyhedronPtr CreateArchimedeanRegularPrism(uint32_t numBaseEdges,double baseRadius);
420  /**
421  * Creates a regular antiprism whose lateral polygons are equilateral triangles, and so each face of its is a regular polygon. Due to vertex transitivity, the resulting object is always archimedean.
422  * \sa CreateRegularAntiprism,CreateCustomAntiprism
423  */
424  static CPolyhedronPtr CreateArchimedeanRegularAntiprism(uint32_t numBaseEdges,double baseRadius);
425  /**
426  * Creates a regular truncated pyramid whose base is a regular polygon.
427  * \sa CreateTruncatedPyramid
428  */
429  static CPolyhedronPtr CreateRegularTruncatedPyramid(uint32_t numBaseEdges,double baseRadius,double height,double ratio);
430  /**
431  * This is a synonym for CreateRegularTruncatedPyramid.
432  * \sa CreateRegularTruncatedPyramid
433  */
434  static CPolyhedronPtr CreateRegularFrustum(uint32_t numBaseEdges,double baseRadius,double height,double ratio);
435  /**
436  * Creates a bifrustum (double truncated pyramid) whose base is a regular polygon lying in the XY plane.
437  * \sa CreateBifrustum
438  */
439  static CPolyhedronPtr CreateRegularBifrustum(uint32_t numBaseEdges,double baseRadius,double height1,double ratio1,double height2,double ratio2);
440  /**
441  * Creates a cupola.
442  * \throw std::logic_error if the number of edges is odd or less than four.
443  */
444  static CPolyhedronPtr CreateCupola(uint32_t numBaseEdges,double edgeLength);
445  /**
446  * Creates a trapezohedron whose dual is exactly an archimedean antiprism. Creates a cube if numBaseEdges is equal to 3.
447  * \todo Actually resulting height is significantly higher than that passed to the algorithm.
448  * \sa CreateTrapezohedron,CreateArchimedeanRegularAntiprism,getDual
449  */
450  static CPolyhedronPtr CreateCatalanTrapezohedron(uint32_t numBaseEdges,double height);
451  /**
452  * Creates a double pyramid whose dual is exactly an archimedean prism. Creates an octahedron if numBaseEdges is equal to 4.
453  * \todo Actually resulting height is significantly higher than that passed to the algorithm.
454  * \sa CreateDoublePyramid,CreateArchimedeanRegularPrism,getDual
455  */
456  static CPolyhedronPtr CreateCatalanDoublePyramid(uint32_t numBaseEdges,double height);
457  /**
458  * Creates a series of concatenated solids (most of which are prismatoids) whose base is a regular polygon with a given number of edges. Every face of the resulting body will be a regular polygon, so it is a Johnson solid; in special cases, it may be archimedean or even platonic.
459  * The shape of the body is defined by the string argument, which can include one or more of the following:
460  <center><table>
461  <tr><td><b>String</b></td><td><b>Body</b></td><td><b>Restrictions</b></td></tr>
462  <tr><td>P+</td><td>Upward pointing pyramid</td><td>Must be the last object, vertex number cannot surpass 5</td></tr>
463  <tr><td>P-</td><td>Downward pointing pyramid</td><td>Must be the first object, vertex number cannot surpass 5</td></tr>
464  <tr><td>C+</td><td>Upward pointing cupola</td><td>Must be the last object, vertex number must be an even number in the range 4-10.</td></tr>
465  <tr><td>C-</td><td>Downward pointing cupola</td><td>Must be the first object, vertex number must be an even number in the range 4-10.</td></tr>
466  <tr><td>GC+</td><td>Upward pointing cupola, rotated</td><td>Must be the last object, vertex number must be an even number in the range 4-10.</td></tr>
467  <tr><td>GC-</td><td>Downward pointing cupola, rotated</td><td>Must be the first object, vertex number must be an even number in the range 4-10.</td></tr>
468  <tr><td>PR</td><td>Archimedean prism</td><td>Cannot abut other prism</td></tr>
469  <tr><td>A</td><td>Archimedean antiprism</td><td>None</td></tr>
470  <tr><td>R+</td><td>Upward pointing rotunda</td><td>Must be the last object, vertex number must be exactly 10</td></tr>
471  <tr><td>R-</td><td>Downward pointing rotunda</td><td>Must be the first object, vertex number must be exactly 10</td></tr>
472  <tr><td>GR+</td><td>Upward pointing rotunda, rotated</td><td>Must be the last object, vertex number must be exactly 10</td></tr>
473  <tr><td>GR-</td><td>Downward pointing rotunda</td><td>Must be the first object, vertex number must be exactly 10</td></tr>
474  </table></center>
475  * Some examples of bodies are:
476  <center><table>
477  <tr><td><b>String</b></td><td><b>Vertices</b></td><td><b>Resulting body</b></td></tr>
478  <tr><td>P+</td><td align="center">3</td><td>Tetrahedron</td></tr>
479  <tr><td>PR</td><td align="center">4</td><td>Hexahedron</td></tr>
480  <tr><td>P-P+</td><td align="center">4</td><td>Octahedron</td></tr>
481  <tr><td>A</td><td align="center">3</td><td>Octahedron</td></tr>
482  <tr><td>C+PRC-</td><td align="center">8</td><td>Rhombicuboctahedron</td></tr>
483  <tr><td>P-AP+</td><td align="center">5</td><td>Icosahedron</td></tr>
484  <tr><td>R-R+</td><td align="center">10</td><td>Icosidodecahedron</td></tr>
485  </table></center>
486  */
487  static CPolyhedronPtr CreateJohnsonSolidWithConstantBase(uint32_t numBaseEdges,double baseRadius,const std::string &components,size_t shifts=0);
488  /** @}
489  */
490 
491  /**
492  * Render
493  * \sa CRenderizable
494  */
495  void render_dl() const;
496  /**
497  * Ray trace
498  * \sa CRenderizable
499  */
500  virtual bool traceRay(const mrpt::poses::CPose3D &o,double &dist) const;
501  /**
502  * Gets a list with the polyhedron's vertices.
503  */
504  inline void getVertices(vector<TPoint3D> &vertices) const {
505  vertices=mVertices;
506  }
507  /**
508  * Gets a list with the polyhedron's edges.
509  */
510  inline void getEdges(vector<TPolyhedronEdge> &edges) const {
511  edges=mEdges;
512  }
513  /**
514  * Gets a list with the polyhedron's faces.
515  */
516  inline void getFaces(vector<TPolyhedronFace> &faces) const {
517  faces=mFaces;
518  }
519  /**
520  * Gets the amount of vertices.
521  */
522  inline uint32_t getNumberOfVertices() const {
523  return mVertices.size();
524  }
525  /**
526  * Gets the amount of edges.
527  */
528  inline uint32_t getNumberOfEdges() const {
529  return mEdges.size();
530  }
531  /**
532  * Gets the amount of faces.
533  */
534  inline uint32_t getNumberOfFaces() const {
535  return mFaces.size();
536  }
537  /**
538  * Gets a vector with each edge's length.
539  */
540  void getEdgesLength(vector<double> &lengths) const;
541  /**
542  * Gets a vector with each face's area. Won't work properly if the polygons are not convex.
543  */
544  void getFacesArea(vector<double> &areas) const;
545  /**
546  * Gets the polyhedron volume. Won't work properly if the polyhedron is not convex.
547  */
548  double getVolume() const;
549  /**
550  * Returns whether the polyhedron will be rendered as a wireframe object.
551  */
552  inline bool isWireframe() const {
553  return mWireframe;
554  }
555  /**
556  * Sets whether the polyhedron will be rendered as a wireframe object.
557  */
558  inline void setWireframe(bool enabled=true) {
559  mWireframe=enabled;
561  }
562  /**
563  * Gets the wireframe lines width.
564  */
565  inline double getLineWidth() const {
566  return mLineWidth;
567  }
568  /**
569  * Sets the width used to render lines, when wireframe rendering is activated.
570  */
571  inline void setLineWidth(double lineWidth) {
572  mLineWidth=lineWidth;
574  }
575  /**
576  * Gets the polyhedron as a set of polygons.
577  * \sa mrpt::math::TPolygon3D
578  */
579  void getSetOfPolygons(std::vector<math::TPolygon3D> &vec) const;
580  /**
581  * Gets the polyhedron as a set of polygons, with the pose transformation already applied.
582  * \sa mrpt::math::TPolygon3D,mrpt::poses::CPose3D
583  */
584  void getSetOfPolygonsAbsolute(std::vector<math::TPolygon3D> &vec) const;
585  /** Gets the intersection of two polyhedra, either as a set or as a matrix of intersections. Each intersection is represented by a TObject3D.
586  * \sa mrpt::math::TObject3D
587  */
588  template<class T> inline static size_t getIntersection(const CPolyhedronPtr &p1,const CPolyhedronPtr &p2,T &container);
589  /**
590  * Returns true if the polygon is a completely closed object.
591  */
592  inline bool isClosed() const {
593  for (size_t i=0;i<mVertices.size();i++) if (edgesInVertex(i)!=facesInVertex(i)) return false;
594  return true;
595  }
596  /**
597  * Recomputes polygons, if necessary, so that each one is convex.
598  */
599  void makeConvexPolygons();
600  /**
601  * Gets the center of the polyhedron.
602  */
603  void getCenter(TPoint3D &center) const;
604  /**
605  * Creates a random polyhedron from the static methods.
606  */
607  static CPolyhedronPtr CreateRandomPolyhedron(double radius);
608 
609  /** @name Polyhedron special operations.
610  @{
611  */
612  /**
613  * Given a polyhedron, creates its dual.
614  * \sa truncate,cantellate,augment
615  * \throw std::logic_error Can't get the dual to this polyhedron.
616  */
617  CPolyhedronPtr getDual() const;
618  /**
619  * Truncates a polyhedron to a given factor.
620  * \sa getDual,cantellate,augment
621  * \throw std::logic_error Polyhedron truncation results in skew polygons and thus it's impossible to perform.
622  */
623  CPolyhedronPtr truncate(double factor) const;
624  /**
625  * Cantellates a polyhedron to a given factor.
626  * \sa getDual,truncate,augment
627  */
628  CPolyhedronPtr cantellate(double factor) const;
629  /**
630  * Augments a polyhedron to a given height. This operation is roughly dual to the truncation: given a body P, the operation dtdP and aP yield resembling results.
631  * \sa getDual,truncate,cantellate
632  */
633  CPolyhedronPtr augment(double height) const;
634  /**
635  * Augments a polyhedron to a given height. This method only affects to faces with certain number of vertices.
636  * \sa augment(double) const
637  */
638  CPolyhedronPtr augment(double height,size_t numVertices) const;
639  /**
640  * Augments a polyhedron, so that the resulting triangles are equilateral. If the argument is true, triangles are "cut" from the polyhedron, instead of being added.
641  * \throw std::logic_error a non-regular face has been found.
642  * \sa augment(double) const
643  */
644  CPolyhedronPtr augment(bool direction=false) const;
645  /**
646  * Augments a polyhedron, so that the resulting triangles are equilateral; affects only faces with certain number of faces. If the second argument is true, triangles are "cut" from the polyhedron.
647  * \throw std::logic_error a non-regular face has been found.
648  * \sa augment(double) const
649  */
650  CPolyhedronPtr augment(size_t numVertices,bool direction=false) const;
651  /**
652  * Rotates a polyhedron around the Z axis a given amount of radians. In some cases, this operation may be necessary to view the symmetry between related objects.
653  * \sa scale
654  */
655  CPolyhedronPtr rotate(double angle) const;
656  /**
657  * Scales a polyhedron to a given factor.
658  * \throw std::logic_error factor is not a strictly positive number.
659  * \sa rotate
660  */
661  CPolyhedronPtr scale(double factor) const;
662  /** @}
663  */
664  /**
665  * Updates the mutable list of polygons used in rendering and ray tracing.
666  */
667  void updatePolygons() const;
668  private:
669  /**
670  * Generates a list of 2D vertices constituting a regular polygon.
671  */
672  static vector<TPoint2D> generateBase(uint32_t numBaseEdges,double baseRadius);
673  /**
674  * Generates a list of 2D vertices constituting a regular polygon, with an angle shift which makes it suitable for antiprisms.
675  */
676  static vector<TPoint2D> generateShiftedBase(uint32_t numBaseEdges,double baseRadius);
677  /**
678  * Generates a list of 3D vertices constituting a regular polygon, appending it to an existing vector.
679  */
680  static void generateBase(uint32_t numBaseEdges,double baseRadius,double height,vector<TPoint3D> &vec);
681  /**
682  * Generates a list of 3D vertices constituting a regular polygon conveniently shifted, appending it to an existing vector.
683  */
684  static void generateShiftedBase(uint32_t numBaseEdges,double baseRadius,double height,double shift,vector<TPoint3D> &vec);
685  /**
686  * Calculates the normal vector to a face.
687  */
688  bool setNormal(TPolyhedronFace &f,bool doCheck=true);
689  /**
690  * Adds, to the existing list of edges, each edge in a given face.
691  */
692  void addEdges(const TPolyhedronFace &e);
693  /**
694  * Checks whether a set of faces is suitable for a set of vertices.
695  */
696  static bool checkConsistence(const vector<TPoint3D> &vertices,const vector<TPolyhedronFace> &faces);
697  /**
698  * Returns how many edges converge in a given vertex.
699  */
700  size_t edgesInVertex(size_t vertex) const;
701  /**
702  * Returns how many faces converge in a given vertex.
703  */
704  size_t facesInVertex(size_t vertex) const;
705  /**
706  * Basic empty constructor.
707  */
708  inline CPolyhedron():mVertices(),mEdges(),mFaces(),mWireframe(false),mLineWidth(1),polygonsUpToDate(false) {}
709  /**
710  * Basic constructor with a list of vertices and another of faces, checking for correctness.
711  */
712  inline CPolyhedron(const vector<TPoint3D> &vertices,const vector<TPolyhedronFace> &faces,bool doCheck=true):mVertices(vertices),mEdges(),mFaces(faces),mWireframe(false),mLineWidth(1),polygonsUpToDate(false) {
713  if (doCheck) if (!checkConsistence(vertices,faces)) throw std::logic_error("Face list accesses a vertex out of range");
714  for (vector<TPolyhedronFace>::iterator it=mFaces.begin();it!=mFaces.end();++it) {
715  if (!setNormal(*it,doCheck)) throw std::logic_error("Bad face specification");
716  addEdges(*it);
717  }
718  }
719  /** Creates a polyhedron without checking its correctness. */
720  static CPolyhedronPtr CreateNoCheck(const vector<TPoint3D> &vertices,const vector<TPolyhedronFace> &faces);
721  /** Creates an empty Polyhedron. */
722  static CPolyhedronPtr CreateEmpty();
723  /** Destructor. */
724  virtual ~CPolyhedron() {}
725  };
727 
728  // Implemented after the definition of SmartPtrs in the _POST() macro above.
729  template<class T>
730  size_t CPolyhedron::getIntersection(const CPolyhedronPtr &p1,const CPolyhedronPtr &p2,T &container) {
731  std::vector<TPolygon3D> polys1,polys2;
732  p1->getSetOfPolygonsAbsolute(polys1);
733  p2->getSetOfPolygonsAbsolute(polys2);
734  return mrpt::math::intersect(polys1,polys2,container);
735  }
736 
737  /**
738  * Reads a polyhedron edge from a binary stream.
739  */
741  /**
742  * Writes a polyhedron edge to a binary stream.
743  */
745  /**
746  * Reads a polyhedron face from a binary stream.
747  */
749  /**
750  * Writes a polyhedron face to a binary stream.
751  */
753 }
754  namespace utils {
755  using namespace mrpt::opengl;
756  // Specialization must occur in the same namespace
758  MRPT_DECLARE_TTYPENAME(CPolyhedron::TPolyhedronFace)
759  }
760 }
761 #endif
bool mWireframe
This flag determines whether the polyhedron will be displayed as a solid object or as a set of edges...
Definition: CPolyhedron.h:119
Classes for serialization, sockets, ini-file manipulation, streams, list of properties-values, timewatch, extensions to STL.
Definition: zip.h:16
bool operator==(const TPolyhedronEdge &e) const
Comparison agains another edge.
Definition: CPolyhedron.h:61
OPENGL_IMPEXP mrpt::utils::CStream & operator<<(mrpt::utils::CStream &out, const mrpt::opengl::CLight &o)
vector< TPolyhedronFace > mFaces
List of polyhedron's faces.
Definition: CPolyhedron.h:115
vector< uint32_t > vertices
Vector of indices to the vertex list.
Definition: CPolyhedron.h:81
::mrpt::utils::CStream & operator>>(mrpt::utils::CStream &in, CAngularObservationMeshPtr &pObj)
Scalar * iterator
Definition: eigen_plugins.h:23
EIGEN_STRONG_INLINE void notifyChange() const
Must be called to notify that the object has changed (so, the display list must be updated) ...
STL namespace.
This class represents arbitrary polyhedra.
Definition: CPolyhedron.h:39
A renderizable object suitable for rendering with OpenGL's display lists.
vector< TPolyhedronEdge > mEdges
List of polyhedron's edges.
Definition: CPolyhedron.h:111
This base class is used to provide a unified interface to files,memory buffers,..Please see the deriv...
Definition: CStream.h:38
This base provides a set of functions for maths stuff.
Definition: CArray.h:18
#define DEFINE_SERIALIZABLE_PRE_CUSTOM_BASE_LINKAGE(class_name, base_name, _LINKAGE_)
This declaration must be inserted in all CSerializable classes definition, before the class declarati...
Struct used to store a polyhedron face.
Definition: CPolyhedron.h:77
Struct used to store a polyhedron edge.
Definition: CPolyhedron.h:45
TPolyhedronFace()
Fast default constructor.
Definition: CPolyhedron.h:89
Classes for 2D/3D geometry representation, both of single values and probability density distribution...
Definition: CPoint.h:17
bool BASE_IMPEXP traceRay(const vector< TPolygonWithPlane > &vec, const mrpt::poses::CPose3D &pose, double &dist)
Fast ray tracing method using polygons' properties.
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
#define DEFINE_SERIALIZABLE(class_name)
This declaration must be inserted in all CSerializable classes definition, within the class declarati...
bool polygonsUpToDate
Whether the set of actual polygons is up to date or not.
Definition: CPolyhedron.h:131
class OPENGL_IMPEXP CPolyhedron
Definition: CPolyhedron.h:22
A class used to store a 3D pose (a 3D translation + a rotation in 3D).
Definition: CPose3D.h:69
TPolyhedronEdge()
Default constructor.
Definition: CPolyhedron.h:57
The namespace for 3D scene representation and rendering.
void getSetOfPolygonsAbsolute(std::vector< math::TPolygon3D > &vec) const
Gets the polyhedron as a set of polygons, with the pose transformation already applied.
bool BASE_IMPEXP intersect(const TSegment3D &s1, const TSegment3D &s2, TObject3D &obj)
Gets the intersection between two 3D segments.
double mLineWidth
When displaying as wireframe object, this variable stores the width of the edges. ...
Definition: CPolyhedron.h:123
std::vector< TPolygonWithPlane > tempPolygons
Mutable list of actual polygons, maintained for speed.
Definition: CPolyhedron.h:127
Lightweight 3D point.
#define DEFINE_SERIALIZABLE_POST_CUSTOM_BASE_LINKAGE(class_name, base_name, _LINKAGE_)
vector< TPoint3D > mVertices
List of vertices presents in the polyhedron.
Definition: CPolyhedron.h:107
#define MRPT_DECLARE_TTYPENAME(_TYPE)
Definition: TTypeName.h:60



Page generated by Doxygen 1.8.8 for MRPT 1.2.2 SVN:Unversioned directory at Tue Oct 14 02:14:08 UTC 2014