dune-common
2.2.0
|
00001 // -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- 00002 // vi: set et ts=4 sw=4 sts=4: 00003 00004 #ifndef DISABLE_GEOMETRYTYPE_DEPRECATION_WARNING 00005 #warning #include <dune/common/geometrytype.hh> is deprecated. Use 00006 #warning #include <dune/geometry/type.hh> instead. You may need the new 00007 #warning Dune-Geometry core module. 00008 #endif // DISABLE_GEOMETRYTYPE_DEPRECATION_WARNING 00009 00010 #ifndef DUNE_COMMON_GEOMETRYTYPE_HH 00011 #define DUNE_COMMON_GEOMETRYTYPE_HH 00012 00013 #include <cassert> 00014 00022 #include <dune/common/exceptions.hh> 00023 00024 #include <dune/common/deprecated.hh> 00025 00026 namespace Dune { 00027 00038 class GeometryType 00039 { 00040 public: 00043 enum BasicType { 00044 simplex, 00045 cube, 00046 pyramid, 00047 prism, 00048 extended, 00049 none 00050 }; 00051 00053 enum Binary { 00054 b0001 = 1, 00055 b0011 = 3, 00056 b0101 = 5, 00057 b0111 = 7 00058 }; 00059 private: 00060 00062 unsigned int topologyId_; 00063 00065 unsigned char dim_ : 7; 00066 00068 bool none_ : 1; 00069 00070 public: 00072 GeometryType () 00073 : topologyId_(0), dim_(0), none_(true) 00074 {} 00075 00077 GeometryType(BasicType basicType, unsigned int dim) 00078 : topologyId_(0), dim_(dim), none_(false) 00079 { 00080 if (dim < 2) 00081 return; 00082 switch( basicType ) 00083 { 00084 case GeometryType::simplex: 00085 makeSimplex(dim); 00086 break; 00087 case GeometryType::cube: 00088 makeCube(dim); 00089 break; 00090 case GeometryType::pyramid: 00091 if (dim == 3) 00092 makePyramid(); 00093 break; 00094 case GeometryType::prism: 00095 if (dim == 3) 00096 makePrism(); 00097 break; 00098 case GeometryType::none: 00099 makeNone(dim); 00100 break; 00101 default: 00102 DUNE_THROW( RangeError, 00103 "Invalid basic geometry type: " << basicType << " for dimension " << dim << "." ); 00104 } 00105 } 00106 00108 GeometryType(unsigned int topologyId, unsigned int dim) 00109 : topologyId_(topologyId), dim_(dim), none_(false) 00110 {} 00111 00122 template<class TopologyType> 00123 explicit GeometryType(TopologyType t) 00124 : topologyId_(TopologyType::id), dim_(TopologyType::dimension), none_(false) 00125 {} 00126 00128 explicit GeometryType(unsigned int dim) 00129 : topologyId_(0), dim_(dim), none_(false) 00130 { 00131 assert(dim < 2); 00132 } 00133 00135 // We need this constructor for "int" and "unsigned int", 00136 // because otherwise GeometryType(int) would try to call the 00137 // generic GeometryType(TopologyType) constructor 00138 explicit GeometryType(int dim) 00139 : topologyId_(0), dim_(dim), none_(false) 00140 { 00141 assert(dim < 2); 00142 } 00143 00146 00148 void makeVertex() { 00149 none_ = false; 00150 dim_ = 0; 00151 topologyId_ = 0; 00152 } 00153 00155 void makeLine() { 00156 none_ = false; 00157 dim_ = 1; 00158 topologyId_ = 0; 00159 } 00160 00162 void makeTriangle() { 00163 makeSimplex(2); 00164 } 00165 00167 void makeQuadrilateral() { 00168 makeCube(2); 00169 } 00170 00172 void makeTetrahedron() { 00173 makeSimplex(3); 00174 } 00175 00177 void makePyramid() { 00178 none_ = false; 00179 dim_ = 3; 00180 topologyId_ = b0011; 00181 } 00182 00184 void makePrism() { 00185 none_ = false; 00186 dim_ = 3; 00187 topologyId_ = b0101; // (1 << (dim_-1)) - 1; 00188 } 00189 00191 void makeHexahedron() { 00192 makeCube(3); 00193 } 00194 00196 void makeSimplex(unsigned int dim) { 00197 none_ = false; 00198 dim_ = dim; 00199 topologyId_ = 0; 00200 } 00201 00203 void makeCube(unsigned int dim) { 00204 none_ = false; 00205 dim_ = dim; 00206 topologyId_ = ((dim>1) ? ((1 << dim) - 1) : 0); 00207 } 00208 00210 void makeNone(unsigned int dim) { 00211 none_ = true; 00212 dim_ = dim; 00213 topologyId_ = 0; 00214 } 00215 00222 bool isVertex() const { 00223 return dim_==0; 00224 } 00225 00227 bool isLine() const { 00228 return dim_==1; 00229 } 00230 00232 bool isTriangle() const { 00233 return ! none_ && dim_==2 && (topologyId_ | 1) == b0001; 00234 } 00235 00237 bool isQuadrilateral() const { 00238 return ! none_ && dim_==2 && (topologyId_ | 1) == b0011; 00239 } 00240 00242 bool isTetrahedron() const { 00243 return ! none_ && dim_==3 && (topologyId_ | 1) == b0001; 00244 } 00245 00247 bool isPyramid() const { 00248 return ! none_ && dim_==3 && (topologyId_ | 1) == b0011; 00249 } 00250 00252 bool isPrism() const { 00253 return ! none_ && dim_==3 && (topologyId_ | 1) == b0101; 00254 } 00255 00257 bool isHexahedron() const { 00258 return ! none_ && dim_==3 && (topologyId_ | 1) == b0111; 00259 } 00260 00262 bool isSimplex() const { 00263 return ! none_ && (topologyId_ | 1) == 1; 00264 } 00265 00267 bool isCube() const { 00268 return ! none_ && ((topologyId_ ^ ((1 << dim_)-1)) >> 1 == 0); 00269 } 00270 00272 bool isNone() const { 00273 return none_; 00274 } 00275 00277 unsigned int dim() const { 00278 return dim_; 00279 } 00280 00282 BasicType basicType() const DUNE_DEPRECATED { 00283 if (isSimplex()) 00284 return GeometryType::simplex; 00285 if (isCube()) 00286 return GeometryType::cube; 00287 if (isPyramid()) 00288 return GeometryType::pyramid; 00289 if (isPrism()) 00290 return GeometryType::prism; 00291 if (isNone()) 00292 return GeometryType::none; 00293 return GeometryType::extended; 00294 } 00295 00297 unsigned int id() const { 00298 return topologyId_; 00299 } 00300 00306 bool operator==(const GeometryType& other) const { 00307 return ( ( none_ == other.none_ ) 00308 && ( ( none_ == true ) 00309 || ( ( dim_ == other.dim_ ) 00310 && ( (topologyId_ >> 1) == (other.topologyId_ >> 1) ) 00311 ) 00312 ) 00313 ); 00314 } 00315 00317 bool operator!=(const GeometryType& other) const { 00318 return ! ((*this)==other); 00319 } 00320 00322 bool operator < (const GeometryType& other) const { 00323 return ( ( none_ < other.none_ ) 00324 || ( !( other.none_ < none_ ) 00325 && ( ( dim_ < other.dim_ ) 00326 || ( (other.dim_ == dim_) 00327 && ((topologyId_ >> 1) < (other.topologyId_ >> 1) ) 00328 ) 00329 ) 00330 ) 00331 ); 00332 } 00333 }; 00334 00336 inline std::ostream& operator<< (std::ostream& s, const GeometryType& a) 00337 { 00338 if (a.isSimplex()) 00339 { 00340 s << "(simplex, " << a.dim() << ")"; 00341 return s; 00342 } 00343 if (a.isCube()) 00344 { 00345 s << "(cube, " << a.dim() << ")"; 00346 return s; 00347 } 00348 if (a.isPyramid()) 00349 { 00350 s << "(pyramid, 3)"; 00351 return s; 00352 } 00353 if (a.isPrism()) 00354 { 00355 s << "(prism, 3)"; 00356 return s; 00357 } 00358 if (a.isNone()) 00359 { 00360 s << "(none, " << a.dim() << ")"; 00361 return s; 00362 } 00363 s << "(other [" << a.id() << "], " << a.dim() << ")"; 00364 return s; 00365 } 00366 00368 inline std::ostream& operator<< (std::ostream& s, GeometryType::BasicType type) 00369 { 00370 switch (type) { 00371 case GeometryType::simplex: 00372 s << "simplex"; 00373 break; 00374 case GeometryType::cube: 00375 s << "cube"; 00376 break; 00377 case GeometryType::pyramid: 00378 s << "pyramid"; 00379 break; 00380 case GeometryType::prism: 00381 s << "prism"; 00382 break; 00383 case GeometryType::extended: 00384 s << "other"; 00385 case GeometryType::none: 00386 s << "none"; 00387 break; 00388 default: 00389 DUNE_THROW(Exception, "invalid GeometryType::BasicType"); 00390 } 00391 return s; 00392 } 00393 } 00394 00395 #endif // DUNE_COMMON_GEOMETRYTYPE_HH