dune-geometry  2.2.0
type.hh
Go to the documentation of this file.
00001 // -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
00002 // vi: set et ts=4 sw=4 sts=4:
00003 
00004 #include <cassert>
00005 #ifndef DUNE_GEOMETRY_TYPE_HH
00006 #define DUNE_GEOMETRY_TYPE_HH
00007 
00008 #define DISABLE_GEOMETRYTYPE_DEPRECATION_WARNING
00009 #include <dune/common/geometrytype.hh>
00010 #undef DISABLE_GEOMETRYTYPE_DEPRECATION_WARNING
00011 
00012 #ifdef DOXYGEN
00013 // Compare the following code with the original header dune/common/geometrytype.hh
00014 // for changes before removing the deprecated header!
00015 
00020 #include <dune/common/exceptions.hh>
00021 #include <dune/common/deprecated.hh>
00022 
00023 namespace Dune {
00024 
00032     class GeometryType
00033     {
00034     public:
00037         enum BasicType {
00038           simplex,   
00039           cube,      
00040           pyramid,   
00041           prism,     
00042           extended,  
00043           none       
00044         };
00045 
00047         enum Binary {
00048             b0001 = 1,
00049             b0011 = 3,
00050             b0101 = 5,
00051             b0111 = 7
00052         };
00053     private:
00054 
00056         unsigned int  topologyId_;
00057 
00059         unsigned char dim_  : 7;
00060 
00062         bool          none_ : 1;
00063 
00064     public:
00066         GeometryType ()
00067             : topologyId_(0), dim_(0), none_(true)
00068         {}
00069 
00071         GeometryType(BasicType basicType, unsigned int dim)
00072             : topologyId_(0), dim_(dim), none_(false)
00073         {
00074             if (dim < 2)
00075                 return;
00076             switch( basicType )
00077             {
00078                 case GeometryType::simplex:
00079                     makeSimplex(dim);
00080                     break;
00081                 case GeometryType::cube:
00082                     makeCube(dim);
00083                     break;
00084                 case GeometryType::pyramid:
00085                     if (dim == 3)
00086                         makePyramid();
00087                     break;
00088                 case GeometryType::prism:
00089                     if (dim == 3)
00090                         makePrism();
00091                     break;
00092                 case GeometryType::none:
00093                     makeNone(dim);
00094                     break;
00095                 default:
00096                     DUNE_THROW( RangeError,
00097                         "Invalid basic geometry type: " << basicType << " for dimension " << dim << "." );
00098             }
00099         }
00100 
00102         GeometryType(unsigned int topologyId, unsigned int dim)
00103             : topologyId_(topologyId), dim_(dim), none_(false)
00104         {}
00105 
00116         template<class TopologyType>
00117         explicit GeometryType(TopologyType t)
00118             : topologyId_(TopologyType::id), dim_(TopologyType::dimension), none_(false)
00119         {}
00120 
00122         explicit GeometryType(unsigned int dim)
00123             : topologyId_(0), dim_(dim), none_(false)
00124         {
00125             assert(dim < 2);
00126         }
00127 
00129         // We need this constructor for "int" and "unsigned int",
00130         // because otherwise GeometryType(int) would try to call the
00131         // generic GeometryType(TopologyType) constructor
00132         explicit GeometryType(int dim)
00133             : topologyId_(0), dim_(dim), none_(false)
00134         {
00135             assert(dim < 2);
00136         }
00137 
00140 
00142         void makeVertex() {
00143             none_  = false;
00144             dim_ = 0;
00145             topologyId_ = 0;
00146         }
00147         
00149         void makeLine() {
00150             none_  = false;
00151             dim_ = 1;
00152             topologyId_ = 0;
00153         }
00154         
00156         void makeTriangle() {
00157             makeSimplex(2);
00158         }
00159         
00161         void makeQuadrilateral() {
00162             makeCube(2);
00163         }
00164         
00166         void makeTetrahedron() {
00167             makeSimplex(3);
00168         }
00169         
00171         void makePyramid() {
00172             none_  = false;
00173             dim_ = 3;
00174             topologyId_ = b0011;
00175         }
00176         
00178         void makePrism() {
00179             none_  = false;
00180             dim_ = 3;
00181             topologyId_ = b0101; // (1 << (dim_-1)) - 1;
00182         }
00183         
00185         void makeHexahedron() {
00186             makeCube(3);
00187         }
00188         
00190         void makeSimplex(unsigned int dim) {
00191             none_  = false;
00192             dim_ = dim;
00193             topologyId_ = 0;
00194         }
00195         
00197         void makeCube(unsigned int dim) {
00198             none_  = false;
00199             dim_ = dim;
00200             topologyId_ = ((dim>1) ? ((1 << dim) - 1) : 0);
00201         }
00202         
00204         void makeNone(unsigned int dim) {
00205             none_ = true;
00206             dim_ = dim;
00207             topologyId_  = 0;
00208         }
00209         
00216         bool isVertex() const {
00217             return dim_==0;
00218         }
00219         
00221         bool isLine() const {
00222             return dim_==1;
00223         }
00224         
00226         bool isTriangle() const {
00227             return ! none_ && dim_==2 && (topologyId_ | 1) == b0001;
00228         }
00229         
00231         bool isQuadrilateral() const {
00232             return ! none_ && dim_==2 && (topologyId_ | 1) == b0011;
00233         }
00234         
00236         bool isTetrahedron() const {
00237             return ! none_ && dim_==3 && (topologyId_ | 1) == b0001;
00238         }
00239         
00241         bool isPyramid() const {
00242             return ! none_ && dim_==3 && (topologyId_ | 1) == b0011;
00243         }
00244         
00246         bool isPrism() const {
00247             return ! none_ && dim_==3 && (topologyId_ | 1) == b0101;
00248         }
00249         
00251         bool isHexahedron() const {
00252             return ! none_ && dim_==3 && (topologyId_ | 1) == b0111;
00253         }
00254         
00256         bool isSimplex() const {
00257             return ! none_ && (topologyId_ | 1) == 1;
00258         }
00259         
00261         bool isCube() const {
00262             return ! none_ && ((topologyId_ ^ ((1 << dim_)-1)) >> 1 == 0);
00263         }
00264         
00266         bool isNone() const {
00267             return none_;
00268         }
00269         
00271         unsigned int dim() const {
00272             return dim_;
00273         }
00274 
00276         BasicType basicType() const DUNE_DEPRECATED {
00277             if (isSimplex())
00278                 return GeometryType::simplex;
00279             if (isCube())
00280                 return GeometryType::cube;
00281             if (isPyramid())
00282                 return GeometryType::pyramid;
00283             if (isPrism())
00284                 return GeometryType::prism;
00285             if (isNone())
00286                 return GeometryType::none;
00287             return GeometryType::extended;
00288         }
00289 
00291         unsigned int id() const {
00292             return topologyId_;
00293         }
00294 
00300         bool operator==(const GeometryType& other) const {
00301             return ( ( none_ == other.none_ )
00302                 && ( ( none_ == true )
00303                     || ( ( dim_ == other.dim_ )
00304                         && ( (topologyId_ >> 1) == (other.topologyId_ >> 1) )
00305                         )
00306                     )
00307                 );
00308         }
00309 
00311         bool operator!=(const GeometryType& other) const {
00312             return ! ((*this)==other);
00313         }
00314 
00316         bool operator < (const GeometryType& other) const {
00317             return ( ( none_ < other.none_ )
00318                      || ( !( other.none_ < none_ ) 
00319                           && ( ( dim_ < other.dim_ )
00320                                || ( (other.dim_ == dim_)
00321                                     && ((topologyId_ >> 1) < (other.topologyId_ >> 1) ) 
00322                                   )
00323                              )
00324                         )
00325                    );
00326         }
00327     };
00328 
00330     inline std::ostream& operator<< (std::ostream& s, const GeometryType& a)
00331     {
00332         if (a.isSimplex())
00333         {
00334             s << "(simplex, " << a.dim() << ")";
00335             return s;
00336         }
00337         if (a.isCube())
00338         {
00339             s << "(cube, " << a.dim() << ")";
00340             return s;
00341         }
00342         if (a.isPyramid())
00343         {
00344             s << "(pyramid, 3)";
00345             return s;
00346         }
00347         if (a.isPrism())
00348         {
00349             s << "(prism, 3)";
00350             return s;
00351         }
00352         if (a.isNone())
00353         {
00354             s << "(none, " << a.dim() << ")"; else // Doxygen
00355             return s;
00356         }
00357         s << "(other [" << a.id() << "], " << a.dim() << ")";
00358         return s;
00359     }
00360 
00362     inline std::ostream& operator<< (std::ostream& s, GeometryType::BasicType type)
00363     {
00364         switch (type) {
00365             case GeometryType::simplex:
00366                 s << "simplex";
00367                 break;
00368             case GeometryType::cube:
00369                 s << "cube";
00370                 break;
00371             case GeometryType::pyramid:
00372                 s << "pyramid";
00373                 break;
00374             case GeometryType::prism:
00375                 s << "prism";
00376                 break;
00377             case GeometryType::extended:
00378                 s << "other";
00379             case GeometryType::none:
00380                 s << "none";
00381                 break;
00382             default:
00383                 DUNE_THROW(Exception, "invalid GeometryType::BasicType");
00384         }
00385         return s;
00386     }
00387 }
00388 #endif // #ifdef DOXYGEN
00389 
00390 #endif // DUNE_GEOMETRY_TYPE_HH