00001 /* +---------------------------------------------------------------------------+ 00002 | The Mobile Robot Programming Toolkit (MRPT) C++ library | 00003 | | 00004 | http://mrpt.sourceforge.net/ | 00005 | | 00006 | Copyright (C) 2005-2009 University of Malaga | 00007 | | 00008 | This software was written by the Machine Perception and Intelligent | 00009 | Robotics Lab, University of Malaga (Spain). | 00010 | Contact: Jose-Luis Blanco <jlblanco@ctima.uma.es> | 00011 | | 00012 | This file is part of the MRPT project. | 00013 | | 00014 | MRPT is free software: you can redistribute it and/or modify | 00015 | it under the terms of the GNU General Public License as published by | 00016 | the Free Software Foundation, either version 3 of the License, or | 00017 | (at your option) any later version. | 00018 | | 00019 | MRPT is distributed in the hope that it will be useful, | 00020 | but WITHOUT ANY WARRANTY; without even the implied warranty of | 00021 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 00022 | GNU General Public License for more details. | 00023 | | 00024 | You should have received a copy of the GNU General Public License | 00025 | along with MRPT. If not, see <http://www.gnu.org/licenses/>. | 00026 | | 00027 +---------------------------------------------------------------------------+ */ 00028 #ifndef opengl_CPolyhedron_H 00029 #define opengl_CPolyhedron_H 00030 00031 #include <mrpt/opengl/CRenderizable.h> 00032 #include <mrpt/utils/stl_extensions.h> 00033 #include <mrpt/math/geometry.h> 00034 00035 namespace mrpt { 00036 namespace opengl { 00037 using namespace mrpt::utils; 00038 using namespace mrpt::poses; 00039 using namespace std; 00040 00041 class MRPTDLLIMPEXP CPolyhedron; 00042 00043 // This must be added to any CSerializable derived class: 00044 DEFINE_SERIALIZABLE_PRE_CUSTOM_BASE(CPolyhedron,CRenderizable) 00045 /** 00046 * 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). 00047 * Polyhedra information and models have been gotten from the Wikipedia, http://wikipedia.org 00048 * \sa opengl::COpenGLScene 00049 */ 00050 class MRPTDLLIMPEXP CPolyhedron:public CRenderizable { 00051 DEFINE_SERIALIZABLE(CPolyhedron) 00052 public: 00053 /** 00054 * Struct used to store a polyhedron edge. The struct consists only of two vertex indices, used to access the polyhedron vertex list. 00055 */ 00056 struct MRPTDLLIMPEXP TPolyhedronEdge { 00057 /** 00058 * First vertex. 00059 */ 00060 uint32_t v1; 00061 /** 00062 * Second vertex. 00063 */ 00064 uint32_t v2; 00065 /** 00066 * Default constructor. Initializes to garbage. 00067 */ 00068 TPolyhedronEdge():v1(),v2() {} 00069 /** 00070 * Comparison agains another edge. Simmetry is taken into account. 00071 */ 00072 bool operator==(const TPolyhedronEdge &e) const { 00073 if (e.v1==v1&&e.v2==v2) return true; 00074 else return e.v1==v2&&e.v2==v1; 00075 } 00076 /** 00077 * Given a set of vertices, computes the length of the vertex. 00078 */ 00079 double length(const vector<TPoint3D> &vs) const; 00080 /** 00081 * Destructor. 00082 */ 00083 ~TPolyhedronEdge() {} 00084 }; 00085 /** 00086 * Struct used to store a polyhedron face. Consists on a set of vertex indices and a normal vector. 00087 */ 00088 struct MRPTDLLIMPEXP TPolyhedronFace { 00089 /** 00090 * Vector of indices to the vertex list. 00091 */ 00092 vector<uint32_t> vertices; 00093 /** 00094 * Normal vector. 00095 */ 00096 double normal[3]; 00097 /** 00098 * Fast default constructor. Initializes to garbage. 00099 */ 00100 TPolyhedronFace():vertices() {} 00101 /** 00102 * Destructor. 00103 */ 00104 ~TPolyhedronFace() {} 00105 /** 00106 * Given a set of vertices, computes the area of this face. 00107 */ 00108 double area(const vector<TPoint3D> &vertices) const; 00109 /** 00110 * Given a set of vertices, get this face's center. 00111 */ 00112 void getCenter(const vector<TPoint3D> &vertices,TPoint3D &p) const; 00113 }; 00114 protected: 00115 /** 00116 * List of vertices presents in the polyhedron. 00117 */ 00118 vector<TPoint3D> mVertices; 00119 /** 00120 * List of polyhedron's edges. 00121 */ 00122 vector<TPolyhedronEdge> mEdges; 00123 /** 00124 * List of polyhedron's faces. 00125 */ 00126 vector<TPolyhedronFace> mFaces; 00127 /** 00128 * This flag determines whether the polyhedron will be displayed as a solid object or as a set of edges. 00129 */ 00130 bool mWireframe; 00131 /** 00132 * When displaying as wireframe object, this variable stores the width of the edges. 00133 */ 00134 double mLineWidth; 00135 /** 00136 * Mutable list of actual polygons, maintained for speed. 00137 */ 00138 mutable std::vector<TPolygonWithPlane> tempPolygons; 00139 /** 00140 * Whether the set of actual polygons is up to date or not. 00141 */ 00142 mutable bool polygonsUpToDate; 00143 public: 00144 /** 00145 * Creation of a polyhedron from its vertices and faces. 00146 * \throw logic_error if the polyhedron definition has flaws (bad vertex indices, etc.). 00147 */ 00148 inline static CPolyhedronPtr Create(const vector<TPoint3D> &vertices,const vector<vector<uint32_t> > &faces) { 00149 vector<TPolyhedronFace> aux; 00150 for (vector<vector<uint32_t> >::const_iterator it=faces.begin();it!=faces.end();++it) { 00151 TPolyhedronFace f; 00152 f.vertices=*it; 00153 aux.push_back(f); 00154 } 00155 return Create(vertices,aux); 00156 } 00157 /** 00158 * Creation of a polyhedron from its vertices and faces. 00159 * \throw logic_error if the polyhedron definition has flaws (bad vertex indices, etc.). 00160 */ 00161 inline static CPolyhedronPtr Create(const vector<TPoint3D> &vertices,const vector<TPolyhedronFace> &faces) { 00162 return CPolyhedronPtr(new CPolyhedron(vertices,faces,true)); 00163 } 00164 /** 00165 * Creation from a set of polygons. 00166 * \sa mrpt::math::TPolygon3D 00167 */ 00168 static CPolyhedronPtr Create(const std::vector<math::TPolygon3D> &polys); 00169 00170 //Static methods to create frequent polyhedra. More bizarre polyhedra are intended to be added in a near future. 00171 00172 /** @name Platonic solids. 00173 @{ 00174 */ 00175 /** 00176 * 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. 00177 * The tetrahedron is the dual to itself. 00178 <p align="center"><img src="Tetrahedron.gif"></p> 00179 * \sa CreatePyramid,CreateJohnsonSolidWithConstantBase,CreateTruncatedTetrahedron 00180 */ 00181 inline static CPolyhedronPtr CreateTetrahedron(double radius) { 00182 CPolyhedronPtr tetra=CreateJohnsonSolidWithConstantBase(3,radius*sqrt(8.0)/3.0,"P+"); 00183 for (vector<TPoint3D>::iterator it=tetra->mVertices.begin();it!=tetra->mVertices.end();++it) it->z-=radius/3; 00184 return tetra; 00185 } 00186 /** 00187 * 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: 00188 <ul><li>Dual to an octahedron.</li><li>Parallelepiped with three orthogonal, equally-lengthed vectors.</li><li>Triangular trapezohedron with proper height.</li></ul> 00189 <p align="center"><img src="Hexahedron.gif"></p> 00190 * \sa CreateOctahedron,getDual,CreateParallelepiped,CreateTrapezohedron,CreateTruncatedHexahedron,CreateTruncatedOctahedron,CreateCuboctahedron,CreateRhombicuboctahedron 00191 */ 00192 inline static CPolyhedronPtr CreateHexahedron(double radius) { 00193 if (radius==0.0) return CreateEmpty(); 00194 double r=radius/sqrt(3.0); 00195 return CreateCubicPrism(-r,r,-r,r,-r,r); 00196 } 00197 /** 00198 * 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: 00199 <ul><li>Dual to an hexahedron</li><li>Triangular antiprism with transitive vertices.</li><li>Conveniently truncated tetrahedron.</li></ul> 00200 <p align="center"><img src="Octahedron.gif"></p> 00201 * \sa CreateHexahedron,getDual,CreateArchimedeanAntiprism,CreateTetrahedron,truncate,CreateTruncatedOctahedron,CreateTruncatedHexahedron,CreateCuboctahedron,CreateRhombicuboctahedron 00202 */ 00203 inline static CPolyhedronPtr CreateOctahedron(double radius) { 00204 return CreateJohnsonSolidWithConstantBase(4,radius,"P-P+"); 00205 } 00206 /** 00207 * Creates a regular dodecahedron (see http://en.wikipedia.org/wiki/Dodecahedron). The dodecahedron is created as the dual to an icosahedron. 00208 <p align="center"><img src="Dodecahedron.gif"></p> 00209 * \sa CreateIcosahedron,getDual,CreateTruncatedDodecahedron,CreateTruncatedIcosahedron,CreateIcosidodecahedron,CreateRhombicosidodecahedron 00210 */ 00211 inline static CPolyhedronPtr CreateDodecahedron(double radius) { 00212 return CreateIcosahedron(radius/sqrt(15-6*sqrt(5.0)))->getDual(); 00213 } 00214 /** 00215 * 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. 00216 <p align="center"><img src="Icosahedron.gif"></p> 00217 * \sa CreateJohnsonSolidWithConstantBase,CreateDodecahedron,getDual,CreateTruncatedIcosahedron,CreateTruncatedDodecahedron,CreateIcosidodecahedron,CreateRhombicosidodecahedron 00218 */ 00219 inline static CPolyhedronPtr CreateIcosahedron(double radius) { 00220 double ang=M_PI/5; 00221 double s2=4*square(sin(ang)); 00222 double prop=sqrt(s2-1)+sqrt(s2-2+2*cos(ang))/2; 00223 return CreateJohnsonSolidWithConstantBase(5,radius/prop,"P-AP+",1); 00224 } 00225 /** @} 00226 */ 00227 00228 /** @name Archimedean solids. 00229 @{ 00230 */ 00231 /** 00232 * 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. 00233 <p align="center"><img src="Truncatedtetrahedron.gif"></p> 00234 * \sa CreateTetrahedron,CreateTriakisTetrahedron 00235 */ 00236 inline static CPolyhedronPtr CreateTruncatedTetrahedron(double radius) { 00237 return CreateTetrahedron(radius*sqrt(27.0/11.0))->truncate(2.0/3.0); 00238 } 00239 /** 00240 * 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: 00241 <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> 00242 <p align="center"><img src="Cuboctahedron.gif"></p> 00243 * \sa CreateHexahedron,CreateOctahedron,truncate,CreateTetrahedron,cantellate,CreateRhombicuboctahedron,CreateRhombicDodecahedron, 00244 */ 00245 inline static CPolyhedronPtr CreateCuboctahedron(double radius) { 00246 return CreateHexahedron(radius*sqrt(1.5))->truncate(1.0); 00247 } 00248 /** 00249 * 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. 00250 <p align="center"><img src="Truncatedhexahedron.gif"></p> 00251 * \sa CreateHexahedron,CreateTriakisOctahedron 00252 */ 00253 inline static CPolyhedronPtr CreateTruncatedHexahedron(double radius) { 00254 return CreateHexahedron(radius*sqrt(3.0/(5-sqrt(8.0))))->truncate(2-sqrt(2.0)); 00255 } 00256 /** 00257 * 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. 00258 <p align="center"><img src="Truncatedoctahedron.gif"></p> 00259 * \sa CreateOctahedron,CreateTetrakisHexahedron 00260 */ 00261 inline static CPolyhedronPtr CreateTruncatedOctahedron(double radius) { 00262 return CreateOctahedron(radius*3/sqrt(5.0))->truncate(2.0/3.0); 00263 } 00264 /** 00265 * 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. 00266 * 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. 00267 <p align="center"><img src="Rhombicuboctahedron.gif"></p> 00268 * \sa CreateJohnsonSolidWithConstantBase,CreateHexahedron,CreateOctahedron,cantellate,CreateCuboctahedron,CreateDeltoidalIcositetrahedron 00269 */ 00270 inline static CPolyhedronPtr CreateRhombicuboctahedron(double radius,bool type=true) { 00271 return CreateJohnsonSolidWithConstantBase(8,radius/sqrt(1+square(sin(M_PI/8))),type?"C-PRC+":"GC-PRC+",3); 00272 } 00273 /** 00274 * 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. 00275 * The dual of the icosidodecahedron is the rhombic triacontahedron. 00276 * 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. 00277 <p align="center"><img src="Icosidodecahedron.gif"></p> 00278 * \sa CreateDodecahedron,CreateIcosahedron,truncate,CreateRhombicosidodecahedron,CreateRhombicTriacontahedron 00279 */ 00280 inline static CPolyhedronPtr CreateIcosidodecahedron(double radius,bool type=true) { 00281 return CreateJohnsonSolidWithConstantBase(10,radius,type?"GR-R+":"R-R+",1); 00282 } 00283 /** 00284 * 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. 00285 <p align="center"><img src="Truncateddodecahedron.gif"></p> 00286 * \sa CreateDodecahedron,CreateTriakisIcosahedron 00287 */ 00288 inline static CPolyhedronPtr CreateTruncatedDodecahedron(double radius) { 00289 return CreateDodecahedron(radius*sqrt(45.0)/sqrt(27+6*sqrt(5.0)))->truncate(1-sqrt(0.2)); 00290 } 00291 /** 00292 * 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. 00293 <p align="center"><img src="Truncatedicosahedron.gif"></p> 00294 * \sa CreateIcosahedron,CreatePentakisDodecahedron 00295 */ 00296 inline static CPolyhedronPtr CreateTruncatedIcosahedron(double radius) { 00297 return CreateIcosahedron(radius*sqrt(45.0)/sqrt(25+4*sqrt(5.0)))->truncate(2.0/3.0); 00298 } 00299 /** 00300 * 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. 00301 <p align="center"><img src="Rhombicosidodecahedron.gif"></p> 00302 * \sa CreateDodecahedron,CreateIcosahedron,CreateIcosidodecahedron,CreateDeltoidalHexecontahedron 00303 */ 00304 inline static CPolyhedronPtr CreateRhombicosidodecahedron(double radius) { 00305 return CreateIcosahedron(radius*sqrt(10.0/(35.0+9.0*sqrt(5.0))))->cantellate(1.5*(sqrt(5.0)-1)); 00306 } 00307 /** @} 00308 */ 00309 00310 /** @name Other Johnson solids. 00311 @{ 00312 */ 00313 /** 00314 * Creates a pentagonal rotunda (half an icosidodecahedron), consisting of six pentagons, ten triangles and a decagon (see http://en.wikipedia.org/wiki/Pentagonal_rotunda). 00315 * \sa CreateIcosidodecahedron,CreateJohnsonSolidWithConstantBase 00316 */ 00317 inline static CPolyhedronPtr CreatePentagonalRotunda(double radius) { 00318 return CreateJohnsonSolidWithConstantBase(10,radius,"R+"); 00319 } 00320 /** @} 00321 */ 00322 00323 /** @name Catalan solids. 00324 @{ 00325 */ 00326 /** 00327 * Creates a triakis tetrahedron, dual to the truncated tetrahedron. This body consists of 12 isosceles triangles (see http://en.wikipedia.org/wiki/Triakis_tetrahedron). 00328 <p align="center"><img src="Triakistetrahedron.gif"></p> 00329 * \sa CreateTruncatedTetrahedron 00330 */ 00331 inline static CPolyhedronPtr CreateTriakisTetrahedron(double radius) { 00332 return CreateTruncatedTetrahedron(radius*3/sqrt(33.0))->getDual(); 00333 } 00334 00335 /** 00336 * Creates a rhombic dodecahedron, dual to the cuboctahedron. This body consists of 12 rhombi (see http://en.wikipedia.org/wiki/Rhombic_dodecahedron). 00337 <p align="center"><img src="Rhombicdodecahedron.gif"></p> 00338 * \sa CreateCuboctahedron 00339 */ 00340 inline static CPolyhedronPtr CreateRhombicDodecahedron(double radius) { 00341 return CreateCuboctahedron(radius/sqrt(2.0))->getDual(); 00342 } 00343 00344 /** 00345 * Creates a triakis octahedron, dual to the truncated hexahedron. This body consists of 24 isosceles triangles (see http://en.wikipedia.org/wiki/Triakis_octahedron). 00346 <p align="center"><img src="Triakisoctahedron.gif"></p> 00347 * \sa CreateTruncatedHexahedron 00348 */ 00349 inline static CPolyhedronPtr CreateTriakisOctahedron(double radius) { 00350 return CreateTruncatedHexahedron(radius/sqrt((5-sqrt(8.0))))->getDual(); 00351 } 00352 00353 /** 00354 * Creates a tetrakis hexahedron, dual to the truncated octahedron. This body consists of 24 isosceles triangles (see http://en.wikipedia.org/wiki/Tetrakis_hexahedron). 00355 <p align="center"><img src="Tetrakishexahedron.gif"></p> 00356 * \sa CreateTruncatedOctahedron 00357 */ 00358 inline static CPolyhedronPtr CreateTetrakisHexahedron(double radius) { 00359 return CreateTruncatedOctahedron(radius*sqrt(0.6))->getDual(); 00360 } 00361 00362 /** 00363 * Creates a deltoidal icositetrahedron, dual to the rhombicuboctahedron. This body consists of 24 kites (see http://en.wikipedia.org/wiki/Deltoidal_icositetrahedron). 00364 <p align="center"><img src="Deltoidalicositetrahedron.gif"></p> 00365 * \sa CreateRhombicuboctahedron 00366 */ 00367 inline static CPolyhedronPtr CreateDeltoidalIcositetrahedron(double radius) { 00368 return CreateRhombicuboctahedron(radius/sqrt(7-sqrt(32.0)),true)->getDual(); 00369 } 00370 00371 /** 00372 * Creates a rhombic triacontahedron, dual to the icosidodecahedron. This body consists of 30 rhombi (see http://en.wikipedia.org/wiki/Rhombic_triacontahedron). 00373 <p align="center"><img src="Rhombictriacontahedron.gif"></p> 00374 * \sa CreateIcosidodecahedron 00375 */ 00376 inline static CPolyhedronPtr CreateRhombicTriacontahedron(double radius) { 00377 return CreateIcosidodecahedron(radius*sqrt(2/(5-sqrt(5.0))),true)->getDual(); 00378 } 00379 00380 /** 00381 * Creates a triakis icosahedron, dual to the truncated dodecahedron. This body consists of 60 isosceles triangles http://en.wikipedia.org/wiki/Triakis_icosahedron). 00382 <p align="center"><img src="Triakisicosahedron.gif"></p> 00383 * \sa CreateTruncatedDodecahedron 00384 */ 00385 inline static CPolyhedronPtr CreateTriakisIcosahedron(double radius) { 00386 return CreateTruncatedDodecahedron(radius*sqrt(5/(25-8*sqrt(5.0))))->getDual(); 00387 } 00388 00389 /** 00390 * Creates a pentakis dodecahedron, dual to the truncated icosahedron. This body consists of 60 isosceles triangles (see http://en.wikipedia.org/wiki/Pentakis_dodecahedron). 00391 <p align="center"><img src="Pentakisdodecahedron.gif"></p> 00392 * \sa CreateTruncatedIcosahedron 00393 */ 00394 inline static CPolyhedronPtr CreatePentakisDodecahedron(double radius) { 00395 return CreateTruncatedIcosahedron(radius*sqrt(3/(17-6*sqrt(5.0))))->getDual(); 00396 } 00397 00398 /** 00399 * Creates a deltoidal hexecontahedron, dual to the rhombicosidodecahedron. This body consists of 60 kites (see http://en.wikipedia.org/wiki/Deltoidal_hexecontahedron). 00400 <p align="center"><img src="Deltoidalhexecontahedron.gif"></p> 00401 * \sa CreateRhombicosidodecahedron 00402 */ 00403 inline static CPolyhedronPtr CreateDeltoidalHexecontahedron(double radius) { 00404 return CreateRhombicosidodecahedron(radius*3.0/sqrt(15-2*sqrt(5.0)))->getDual(); 00405 } 00406 /** @} 00407 */ 00408 00409 /** @name Customizable polyhedra 00410 @{ 00411 */ 00412 /** 00413 * 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. 00414 * \sa CreateCubicPrism(const mrpt::math::TPoint3D &,const mrpt::math::TPoint3D &),CreateParallelepiped,CreateCustomPrism,CreateRegularPrism,CreateArchimedeanRegularPrism 00415 */ 00416 static CPolyhedronPtr CreateCubicPrism(double x1,double x2,double y1,double y2,double z1,double z2); 00417 /** 00418 * Creates a cubic prism, given two opposite vertices. 00419 * \sa CreateCubicPrism(double,double,double,double,double,double),CreateParallelepiped,CreateCustomPrism,CreateRegularPrism,CreateArchimedeanRegularPrism 00420 */ 00421 inline static CPolyhedronPtr CreateCubicPrism(const TPoint3D &p1,const TPoint3D &p2) { 00422 return CreateCubicPrism(p1.x,p2.x,p1.y,p2.y,p1.z,p2.z); 00423 } 00424 /** 00425 * Creates a custom pyramid, using a set of 2D vertices which will lie on the XY plane. 00426 * \sa CreateDoublePyramid,CreateFrustum,CreateBifrustum,CreateRegularPyramid 00427 */ 00428 static CPolyhedronPtr CreatePyramid(const vector<TPoint2D> &baseVertices,double height); 00429 /** 00430 * 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. 00431 * \sa CreatePyramid,CreateBifrustum,CreateRegularDoublePyramid 00432 */ 00433 static CPolyhedronPtr CreateDoublePyramid(const vector<TPoint2D> &baseVertices,double height1,double height2); 00434 /** 00435 * Creates a truncated pyramid, using a set of vertices which will lie on the XY plane. 00436 * 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. 00437 * \sa CreatePyramid,CreateBifrustum 00438 */ 00439 static CPolyhedronPtr CreateTruncatedPyramid(const vector<TPoint2D> &baseVertices,double height,double ratio); 00440 /** 00441 * This is a synonym for CreateTruncatedPyramid. 00442 * \sa CreateTruncatedPyramid 00443 */ 00444 inline CPolyhedronPtr CreateFrustum(const vector<TPoint2D> &baseVertices,double height,double ratio) { 00445 return CreateTruncatedPyramid(baseVertices,height,ratio); 00446 } 00447 /** 00448 * Creates a custom prism with vertical edges, given any base which will lie on the XY plane. 00449 * \sa CreateCubicPrism,CreateCustomAntiprism,CreateRegularPrism,CreateArchimedeanRegularPrism 00450 */ 00451 inline static CPolyhedronPtr CreateCustomPrism(const vector<TPoint2D> &baseVertices,double height) { 00452 return CreateTruncatedPyramid(baseVertices,height,1.0); 00453 } 00454 /** 00455 * Creates a custom antiprism, using two custom bases. For better results, the top base should be slightly rotated with respect to the bottom one. 00456 * \sa CreateCustomPrism,CreateRegularAntiprism,CreateArchimedeanRegularAntiprism 00457 */ 00458 static CPolyhedronPtr CreateCustomAntiprism(const vector<TPoint2D> &bottomBase,const vector<TPoint2D> &topBase,double height); 00459 /** 00460 * Creates a parallelepiped, given a base point and three vectors represented as points. 00461 * \sa CreateCubicPrism 00462 */ 00463 static CPolyhedronPtr CreateParallelepiped(const TPoint3D &base,const TPoint3D &v1,const TPoint3D &v2,const TPoint3D &v3); 00464 /** 00465 * Creates a bifrustum, or double truncated pyramid, given a base which will lie on the XY plane. 00466 * \sa CreateFrustum,CreateDoublePyramid 00467 */ 00468 static CPolyhedronPtr CreateBifrustum(const vector<TPoint2D> &baseVertices,double height1,double ratio1,double height2,double ratio2); 00469 /** 00470 * 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. 00471 * When the number of edges equals 3, the polyhedron is actually a parallelepiped, and it can even be a cube. 00472 */ 00473 static CPolyhedronPtr CreateTrapezohedron(uint32_t numBaseEdges,double baseRadius,double basesDistance); 00474 /** 00475 * 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. 00476 * \sa CreateCustomAntiprism,CreateArchimedeanRegularAntiprism 00477 */ 00478 inline static CPolyhedronPtr CreateRegularAntiprism(uint32_t numBaseEdges,double baseRadius,double height) { 00479 return CreateCustomAntiprism(generateBase(numBaseEdges,baseRadius),generateShiftedBase(numBaseEdges,baseRadius),height); 00480 } 00481 /** 00482 * Creates a regular prism whose base is a regular polygon and whose edges are either parallel or perpendicular to the XY plane. 00483 * \sa CreateCubicPrism,CreateCustomPrism,CreateArchimedeanRegularAntiprism 00484 */ 00485 inline static CPolyhedronPtr CreateRegularPrism(uint32_t numBaseEdges,double baseRadius,double height) { 00486 return CreateCustomPrism(generateBase(numBaseEdges,baseRadius),height); 00487 } 00488 /** 00489 * Creates a regular pyramid whose base is a regular polygon. 00490 * \sa CreatePyramid 00491 */ 00492 inline static CPolyhedronPtr CreateRegularPyramid(uint32_t numBaseEdges,double baseRadius,double height) { 00493 return CreatePyramid(generateBase(numBaseEdges,baseRadius),height); 00494 } 00495 /** 00496 * Creates a regular double pyramid whose base is a regular polygon. 00497 * \sa CreateDoublePyramid 00498 */ 00499 inline static CPolyhedronPtr CreateRegularDoublePyramid(uint32_t numBaseEdges,double baseRadius,double height1,double height2) { 00500 return CreateDoublePyramid(generateBase(numBaseEdges,baseRadius),height1,height2); 00501 } 00502 /** 00503 * 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. 00504 * \sa CreateRegularPrism,CreateCustomPrism 00505 */ 00506 inline static CPolyhedronPtr CreateArchimedeanRegularPrism(uint32_t numBaseEdges,double baseRadius) { 00507 return CreateJohnsonSolidWithConstantBase(numBaseEdges,baseRadius,"PR"); 00508 } 00509 /** 00510 * 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. 00511 * \sa CreateRegularAntiprism,CreateCustomAntiprism 00512 */ 00513 inline static CPolyhedronPtr CreateArchimedeanRegularAntiprism(uint32_t numBaseEdges,double baseRadius) { 00514 return CreateJohnsonSolidWithConstantBase(numBaseEdges,baseRadius,"A"); 00515 } 00516 /** 00517 * Creates a regular truncated pyramid whose base is a regular polygon. 00518 * \sa CreateTruncatedPyramid 00519 */ 00520 inline static CPolyhedronPtr CreateRegularTruncatedPyramid(uint32_t numBaseEdges,double baseRadius,double height,double ratio) { 00521 return CreateTruncatedPyramid(generateBase(numBaseEdges,baseRadius),height,ratio); 00522 } 00523 /** 00524 * This is a synonym for CreateRegularTruncatedPyramid. 00525 * \sa CreateRegularTruncatedPyramid 00526 */ 00527 inline static CPolyhedronPtr CreateRegularFrustum(uint32_t numBaseEdges,double baseRadius,double height,double ratio) { 00528 return CreateRegularTruncatedPyramid(numBaseEdges,baseRadius,height,ratio); 00529 } 00530 /** 00531 * Creates a bifrustum (double truncated pyramid) whose base is a regular polygon lying in the XY plane. 00532 * \sa CreateBifrustum 00533 */ 00534 inline static CPolyhedronPtr CreateRegularBifrustum(uint32_t numBaseEdges,double baseRadius,double height1,double ratio1,double height2,double ratio2) { 00535 return CreateBifrustum(generateBase(numBaseEdges,baseRadius),height1,ratio1,height2,ratio2); 00536 } 00537 /** 00538 * Creates a cupola. 00539 * \throw std::logic_error if the number of edges is odd or less than four. 00540 */ 00541 inline static CPolyhedronPtr CreateCupola(uint32_t numBaseEdges,double edgeLength) { 00542 return CreateJohnsonSolidWithConstantBase(numBaseEdges,edgeLength/(2*sin(M_PI/numBaseEdges)),"C+"); 00543 } 00544 /** 00545 * Creates a trapezohedron whose dual is exactly an archimedean antiprism. Creates a cube if numBaseEdges is equal to 3. 00546 * \todo Actually resulting height is significantly higher than that passed to the algorithm. 00547 * \sa CreateTrapezohedron,CreateArchimedeanRegularAntiprism,getDual 00548 */ 00549 inline static CPolyhedronPtr CreateCatalanTrapezohedron(uint32_t numBaseEdges,double height) { 00550 return CreateArchimedeanRegularAntiprism(numBaseEdges,height)->getDual(); 00551 } 00552 /** 00553 * Creates a double pyramid whose dual is exactly an archimedean prism. Creates an octahedron if numBaseEdges is equal to 4. 00554 * \todo Actually resulting height is significantly higher than that passed to the algorithm. 00555 * \sa CreateDoublePyramid,CreateArchimedeanRegularPrism,getDual 00556 */ 00557 inline static CPolyhedronPtr CreateCatalanDoublePyramid(uint32_t numBaseEdges,double height) { 00558 return CreateArchimedeanRegularPrism(numBaseEdges,height)->getDual(); 00559 } 00560 /** 00561 * 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. 00562 * The shape of the body is defined by the string argument, which can include one or more of the following: 00563 <center><table> 00564 <tr><td><b>String</b></td><td><b>Body</b></td><td><b>Restrictions</b></td></tr> 00565 <tr><td>P+</td><td>Upward pointing pyramid</td><td>Must be the last object, vertex number cannot surpass 5</td></tr> 00566 <tr><td>P-</td><td>Downward pointing pyramid</td><td>Must be the first object, vertex number cannot surpass 5</td></tr> 00567 <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> 00568 <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> 00569 <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> 00570 <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> 00571 <tr><td>PR</td><td>Archimedean prism</td><td>Cannot abut other prism</td></tr> 00572 <tr><td>A</td><td>Archimedean antiprism</td><td>None</td></tr> 00573 <tr><td>R+</td><td>Upward pointing rotunda</td><td>Must be the last object, vertex number must be exactly 10</td></tr> 00574 <tr><td>R-</td><td>Downward pointing rotunda</td><td>Must be the first object, vertex number must be exactly 10</td></tr> 00575 <tr><td>GR+</td><td>Upward pointing rotunda, rotated</td><td>Must be the last object, vertex number must be exactly 10</td></tr> 00576 <tr><td>GR-</td><td>Downward pointing rotunda</td><td>Must be the first object, vertex number must be exactly 10</td></tr> 00577 </table></center> 00578 * Some examples of bodies are: 00579 <center><table> 00580 <tr><td><b>String</b></td><td><b>Vertices</b></td><td><b>Resulting body</b></td></tr> 00581 <tr><td>P+</td><td align="center">3</td><td>Tetrahedron</td></tr> 00582 <tr><td>PR</td><td align="center">4</td><td>Hexahedron</td></tr> 00583 <tr><td>P-P+</td><td align="center">4</td><td>Octahedron</td></tr> 00584 <tr><td>A</td><td align="center">3</td><td>Octahedron</td></tr> 00585 <tr><td>C+PRC-</td><td align="center">8</td><td>Rhombicuboctahedron</td></tr> 00586 <tr><td>P-AP+</td><td align="center">5</td><td>Icosahedron</td></tr> 00587 <tr><td>R-R+</td><td align="center">10</td><td>Icosidodecahedron</td></tr> 00588 </table></center> 00589 */ 00590 static CPolyhedronPtr CreateJohnsonSolidWithConstantBase(uint32_t numBaseEdges,double baseRadius,const std::string &components,size_t shifts=0); 00591 /** @} 00592 */ 00593 00594 /** 00595 * Render 00596 * \sa CRenderizable 00597 */ 00598 void render() const; 00599 /** 00600 * Ray trace 00601 * \sa CRenderizable 00602 */ 00603 virtual bool traceRay(const mrpt::poses::CPose3D &o,double &dist) const; 00604 /** 00605 * Gets a list with the polyhedron's vertices. 00606 */ 00607 inline void getVertices(vector<TPoint3D> &vertices) const { 00608 vertices=mVertices; 00609 } 00610 /** 00611 * Gets a list with the polyhedron's edges. 00612 */ 00613 inline void getEdges(vector<TPolyhedronEdge> &edges) const { 00614 edges=mEdges; 00615 } 00616 /** 00617 * Gets a list with the polyhedron's faces. 00618 */ 00619 inline void getFaces(vector<TPolyhedronFace> &faces) const { 00620 faces=mFaces; 00621 } 00622 /** 00623 * Gets the amount of vertices. 00624 */ 00625 inline uint32_t getNumberOfVertices() const { 00626 return mVertices.size(); 00627 } 00628 /** 00629 * Gets the amount of edges. 00630 */ 00631 inline uint32_t getNumberOfEdges() const { 00632 return mEdges.size(); 00633 } 00634 /** 00635 * Gets the amount of faces. 00636 */ 00637 inline uint32_t getNumberOfFaces() const { 00638 return mFaces.size(); 00639 } 00640 /** 00641 * Gets a vector with each edge's length. 00642 */ 00643 void getEdgesLength(vector<double> &lengths) const; 00644 /** 00645 * Gets a vector with each face's area. Won't work properly if the polygons are not convex. 00646 */ 00647 void getFacesArea(vector<double> &areas) const; 00648 /** 00649 * Gets the polyhedron volume. Won't work properly if the polyhedron is not convex. 00650 */ 00651 double getVolume() const; 00652 /** 00653 * Returns whether the polyhedron will be rendered as a wireframe object. 00654 */ 00655 inline bool isWireframe() const { 00656 return mWireframe; 00657 } 00658 /** 00659 * Sets whether the polyhedron will be rendered as a wireframe object. 00660 */ 00661 inline void setWireframe(bool enabled=true) { 00662 mWireframe=enabled; 00663 } 00664 /** 00665 * Gets the wireframe lines width. 00666 */ 00667 inline double getLineWidth() const { 00668 return mLineWidth; 00669 } 00670 /** 00671 * Sets the width used to render lines, when wireframe rendering is activated. 00672 */ 00673 inline void setLineWidth(double lineWidth) { 00674 mLineWidth=lineWidth; 00675 } 00676 /** 00677 * Gets the polyhedron as a set of polygons. 00678 * \sa mrpt::math::TPolygon3D 00679 */ 00680 void getSetOfPolygons(std::vector<math::TPolygon3D> &vec) const; 00681 /** 00682 * Gets the polyhedron as a set of polygons, with the pose transformation already applied. 00683 * \sa mrpt::math::TPolygon3D,mrpt::poses::CPose3D 00684 */ 00685 void getSetOfPolygonsAbsolute(std::vector<math::TPolygon3D> &vec) const; 00686 /** 00687 * Gets the intersection of two polyhedra, either as a set or as a matrix of intersections. Each intersection is represented by a TObject3D. 00688 * \sa mrpt::math::TObject3D 00689 */ 00690 template<class T> inline static size_t getIntersection(const CPolyhedronPtr &p1,const CPolyhedronPtr &p2,T &container) { 00691 std::vector<TPolygon3D> polys1,polys2; 00692 p1->getSetOfPolygonsAbsolute(polys1); 00693 p2->getSetOfPolygonsAbsolute(polys2); 00694 return mrpt::math::intersect(polys1,polys2,container); 00695 } 00696 /** 00697 * Returns true if the polygon is a completely closed object. 00698 */ 00699 inline bool isClosed() const { 00700 for (size_t i=0;i<mVertices.size();i++) if (edgesInVertex(i)!=facesInVertex(i)) return false; 00701 return true; 00702 } 00703 /** 00704 * Recomputes polygons, if necessary, so that each one is convex. 00705 */ 00706 void makeConvexPolygons(); 00707 /** 00708 * Gets the center of the polyhedron. 00709 */ 00710 void getCenter(TPoint3D ¢er) const; 00711 /** 00712 * Creates a random polyhedron from the static methods. 00713 */ 00714 static CPolyhedronPtr CreateRandomPolyhedron(double radius); 00715 00716 /** @name Polyhedron special operations. 00717 @{ 00718 */ 00719 /** 00720 * Given a polyhedron, creates its dual. 00721 * \sa truncate,cantellate,augment 00722 * \throw std::logic_error Can't get the dual to this polyhedron. 00723 */ 00724 CPolyhedronPtr getDual() const; 00725 /** 00726 * Truncates a polyhedron to a given factor. 00727 * \sa getDual,cantellate,augment 00728 * \throw std::logic_error Polyhedron truncation results in skew polygons and thus it's impossible to perform. 00729 */ 00730 CPolyhedronPtr truncate(double factor) const; 00731 /** 00732 * Cantellates a polyhedron to a given factor. 00733 * \sa getDual,truncate,augment 00734 */ 00735 CPolyhedronPtr cantellate(double factor) const; 00736 /** 00737 * 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. 00738 * \sa getDual,truncate,cantellate 00739 */ 00740 CPolyhedronPtr augment(double height) const; 00741 /** 00742 * Augments a polyhedron to a given height. This method only affects to faces with certain number of vertices. 00743 * \sa augment(double) const 00744 */ 00745 CPolyhedronPtr augment(double height,size_t numVertices) const; 00746 /** 00747 * 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. 00748 * \throw std::logic_error a non-regular face has been found. 00749 * \sa augment(double) const 00750 */ 00751 CPolyhedronPtr augment(bool direction=false) const; 00752 /** 00753 * 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. 00754 * \throw std::logic_error a non-regular face has been found. 00755 * \sa augment(double) const 00756 */ 00757 CPolyhedronPtr augment(size_t numVertices,bool direction=false) const; 00758 /** 00759 * 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. 00760 * \sa scale 00761 */ 00762 CPolyhedronPtr rotate(double angle) const; 00763 /** 00764 * Scales a polyhedron to a given factor. 00765 * \throw std::logic_error factor is not a strictly positive number. 00766 * \sa rotate 00767 */ 00768 CPolyhedronPtr scale(double factor) const; 00769 /** @} 00770 */ 00771 /** 00772 * Updates the mutable list of polygons used in rendering and ray tracing. 00773 */ 00774 void updatePolygons() const; 00775 private: 00776 /** 00777 * Generates a list of 2D vertices constituting a regular polygon. 00778 */ 00779 static vector<TPoint2D> generateBase(uint32_t numBaseEdges,double baseRadius); 00780 /** 00781 * Generates a list of 2D vertices constituting a regular polygon, with an angle shift which makes it suitable for antiprisms. 00782 */ 00783 static vector<TPoint2D> generateShiftedBase(uint32_t numBaseEdges,double baseRadius); 00784 /** 00785 * Generates a list of 3D vertices constituting a regular polygon, appending it to an existing vector. 00786 */ 00787 static void generateBase(uint32_t numBaseEdges,double baseRadius,double height,vector<TPoint3D> &vec); 00788 /** 00789 * Generates a list of 3D vertices constituting a regular polygon conveniently shifted, appending it to an existing vector. 00790 */ 00791 static void generateShiftedBase(uint32_t numBaseEdges,double baseRadius,double height,double shift,vector<TPoint3D> &vec); 00792 /** 00793 * Calculates the normal vector to a face. 00794 */ 00795 bool setNormal(TPolyhedronFace &f,bool doCheck=true); 00796 /** 00797 * Adds, to the existing list of edges, each edge in a given face. 00798 */ 00799 void addEdges(const TPolyhedronFace &e); 00800 /** 00801 * Checks whether a set of faces is suitable for a set of vertices. 00802 */ 00803 static bool checkConsistence(const vector<TPoint3D> &vertices,const vector<TPolyhedronFace> &faces); 00804 /** 00805 * Returns how many edges converge in a given vertex. 00806 */ 00807 size_t edgesInVertex(size_t vertex) const; 00808 /** 00809 * Returns how many faces converge in a given vertex. 00810 */ 00811 size_t facesInVertex(size_t vertex) const; 00812 /** 00813 * Basic empty constructor. 00814 */ 00815 inline CPolyhedron():mVertices(),mEdges(),mFaces(),mWireframe(false),mLineWidth(1),polygonsUpToDate(false) {} 00816 /** 00817 * Basic constructor with a list of vertices and another of faces, checking for correctness. 00818 */ 00819 inline CPolyhedron(const vector<TPoint3D> &vertices,const vector<TPolyhedronFace> &faces,bool doCheck=true):mVertices(vertices),mEdges(),mFaces(faces),mWireframe(false),mLineWidth(1),polygonsUpToDate(false) { 00820 if (doCheck) if (!checkConsistence(vertices,faces)) throw std::logic_error("Face list accesses a vertex out of range"); 00821 for (vector<TPolyhedronFace>::iterator it=mFaces.begin();it!=mFaces.end();++it) { 00822 if (!setNormal(*it,doCheck)) throw std::logic_error("Bad face specification"); 00823 addEdges(*it); 00824 } 00825 } 00826 /** 00827 * Creates a polyhedron without checking its correctness. 00828 */ 00829 inline static CPolyhedronPtr CreateNoCheck(const vector<TPoint3D> &vertices,const vector<TPolyhedronFace> &faces) { 00830 return CPolyhedronPtr(new CPolyhedron(vertices,faces,false)); 00831 } 00832 /** 00833 * Creates an empty Polyhedron. 00834 */ 00835 inline static CPolyhedronPtr CreateEmpty() { 00836 return CPolyhedronPtr(new CPolyhedron()); 00837 } 00838 /** 00839 * Destructor. 00840 */ 00841 virtual ~CPolyhedron() {} 00842 }; 00843 /** 00844 * Reads a polyhedron edge from a binary stream. 00845 */ 00846 MRPTDLLIMPEXP mrpt::utils::CStream& operator>>(mrpt::utils::CStream& in,CPolyhedron::TPolyhedronEdge &o); 00847 /** 00848 * Writes a polyhedron edge to a binary stream. 00849 */ 00850 MRPTDLLIMPEXP mrpt::utils::CStream& operator<<(mrpt::utils::CStream& out,const CPolyhedron::TPolyhedronEdge &o); 00851 /** 00852 * Reads a polyhedron face from a binary stream. 00853 */ 00854 MRPTDLLIMPEXP mrpt::utils::CStream& operator>>(mrpt::utils::CStream& in,CPolyhedron::TPolyhedronFace &o); 00855 /** 00856 * Writes a polyhedron face to a binary stream. 00857 */ 00858 MRPTDLLIMPEXP mrpt::utils::CStream& operator<<(mrpt::utils::CStream& out,const CPolyhedron::TPolyhedronFace &o); 00859 } 00860 namespace utils { 00861 using namespace mrpt::opengl; 00862 // Specialization must occur in the same namespace 00863 MRPT_DECLARE_TTYPENAME(CPolyhedron::TPolyhedronEdge) 00864 MRPT_DECLARE_TTYPENAME(CPolyhedron::TPolyhedronFace) 00865 } 00866 } 00867 #endif
Page generated by Doxygen 1.5.7.1 for MRPT 0.7.1 SVN: at Mon Aug 17 23:02:22 EDT 2009 |