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



Page generated by Doxygen 1.8.14 for MRPT 1.5.9 Git: 690a4699f Wed Apr 15 19:29:53 2020 +0200 at miƩ abr 15 19:30:12 CEST 2020