dune-geometry  2.8.0
type.hh
Go to the documentation of this file.
1 // -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 // vi: set et ts=4 sw=2 sts=2:
3 #ifndef DUNE_GEOMETRY_TYPE_HH
4 #define DUNE_GEOMETRY_TYPE_HH
5 
10 #include <cassert>
11 
12 #include <string>
13 #include <type_traits>
14 
15 #include <dune/common/exceptions.hh>
16 #include <dune/common/keywords.hh>
17 #include <dune/common/typetraits.hh>
18 #include <dune/common/unused.hh>
19 
20 namespace Dune
21 {
22 
23  namespace Impl
24  {
25 
26  enum TopologyConstruction { pyramidConstruction = 0, prismConstruction = 1 };
27 
28  // Dynamic Topology Properties
29  // ---------------------------
30 
39  inline static unsigned int numTopologies ( int dim ) noexcept
40  {
41  return (1u << dim);
42  }
43 
55  inline bool static isPyramid ( unsigned int topologyId, int dim, int codim = 0 ) noexcept
56  {
57  assert( (dim > 0) && (topologyId < numTopologies( dim )) );
58  assert( (0 <= codim) && (codim < dim) );
59  return (((topologyId & ~1) & (1u << (dim-codim-1))) == 0);
60  }
61 
73  inline static bool isPrism ( unsigned int topologyId, int dim, int codim = 0 ) noexcept
74  {
75  assert( (dim > 0) && (topologyId < numTopologies( dim )) );
76  assert( (0 <= codim) && (codim < dim) );
77  return (( (topologyId | 1) & (1u << (dim-codim-1))) != 0);
78  }
79 
87  inline static unsigned int baseTopologyId ( unsigned int topologyId, int dim, int codim = 1 ) noexcept
88  {
89  assert( (dim >= 0) && (topologyId < numTopologies( dim )) );
90  assert( (0 <= codim) && (codim <= dim) );
91  return topologyId & ((1u << (dim-codim)) - 1);
92  }
93 
94  } // namespace Impl
95 
96 // the Topology classes are deprecated and will be removed for the 2.8.
97 // Temporarily a header 'deprecated_topology.hh' is provided which will be removed after the 2.9 release.
98 #if __GNUC__ >= 7
99 # pragma GCC diagnostic push
100 # pragma GCC diagnostic ignored "-Wdeprecated-declarations"
101 #endif
103 #if __GNUC__ >= 7
104 # pragma GCC diagnostic pop
105 #endif
106 
107  // GeometryType
108  // -------------
109 
123  {
124  public:
125 
128  enum
129  BasicType {
135  none
136  };
137 
138  private:
139 
141  unsigned char dim_;
142 
144  bool none_;
145 
147  unsigned int topologyId_;
148 
149  // Internal type used for the Id. The exact nature of this type is kept
150  // as an implementation detail on purpose. We use a scoped enum here because scoped enums
151  // can be used as template parameters, but are not implicitly converted to other integral
152  // types by the compiler. That way, we avoid unfortunate implicit conversion chains, e.g.
153  // people trying to work with GlobalGeometryTypeIndex, but forgetting to actually call
154  // GlobalGeometryTypeIndex::index(gt) and just using gt directly.
155  enum class IdType : std::uint64_t
156  {};
157 
158  public:
159 
190  using Id = IdType;
191 
199  constexpr operator Id() const
200  {
201  // recreate the exact storage layout that this class is using, making conversion
202  // extremely cheap
203  std::uint64_t id = dim_ | (std::uint64_t(none_) << 8) | (std::uint64_t(topologyId_) << 32);
204  return static_cast<Id>(id);
205  }
206 
219  constexpr Id toId() const
220  {
221  return static_cast<Id>(*this);
222  }
223 
231  constexpr GeometryType(Id id)
232  : dim_(static_cast<std::uint64_t>(id) & 0xFF)
233  , none_(static_cast<std::uint64_t>(id) & 0x100)
234  , topologyId_(static_cast<std::uint64_t>(id) >> 32)
235  {}
236 
239 
241  constexpr GeometryType ()
242  : dim_(0), none_(true), topologyId_(0)
243  {}
244 
251  constexpr GeometryType(unsigned int topologyId, unsigned int dim, bool isNone)
252  : dim_(dim), none_(isNone), topologyId_(topologyId)
253  {}
254 
260  constexpr GeometryType(unsigned int topologyId, unsigned int dim)
261  : dim_(dim), none_(false), topologyId_(topologyId)
262  {}
263 
274  template<class TopologyType,
275  class = std::void_t<decltype(TopologyType::dimension), decltype(TopologyType::id)>>
276  explicit GeometryType(TopologyType t)
277  : dim_(TopologyType::dimension), none_(false), topologyId_(TopologyType::id)
278  {
279  DUNE_UNUSED_PARAMETER(t);
280  }
281 
288  constexpr bool isVertex() const {
289  return dim_==0;
290  }
291 
293  constexpr bool isLine() const {
294  return dim_==1;
295  }
296 
298  constexpr bool isTriangle() const {
299  return ! none_ && dim_==2 && (topologyId_ | 1) == 0b0001;
300  }
301 
303  constexpr bool isQuadrilateral() const {
304  return ! none_ && dim_==2 && (topologyId_ | 1) == 0b0011;
305  }
306 
308  constexpr bool isTetrahedron() const {
309  return ! none_ && dim_==3 && (topologyId_ | 1) == 0b0001;
310  }
311 
313  constexpr bool isPyramid() const {
314  return ! none_ && dim_==3 && (topologyId_ | 1) == 0b0011;
315  }
316 
318  constexpr bool isPrism() const {
319  return ! none_ && dim_==3 && (topologyId_ | 1) == 0b0101;
320  }
321 
323  constexpr bool isHexahedron() const {
324  return ! none_ && dim_==3 && (topologyId_ | 1) == 0b0111;
325  }
326 
328  constexpr bool isSimplex() const {
329  return ! none_ && (topologyId_ | 1) == 1;
330  }
331 
333  constexpr bool isCube() const {
334  return ! none_ && ((topologyId_ ^ ((1 << dim_)-1)) >> 1 == 0);
335  }
336 
338  constexpr bool isConical() const {
339  return ! none_ && (((topologyId_ & ~1) & (1u << (dim_-1))) == 0);
340  }
341 
346  constexpr bool isConical(const int& step) const {
347  return ! none_ && (((topologyId_ & ~1) & (1u << step)) == 0);
348  }
349 
351  constexpr bool isPrismatic() const {
352  return ! none_ && (( (topologyId_ | 1) & (1u << (dim_-1))) != 0);
353  }
354 
359  constexpr bool isPrismatic(const int& step) const {
360  return ! none_ && (( (topologyId_ | 1) & (1u << step)) != 0);
361  }
362 
364  constexpr bool isNone() const {
365  return none_;
366  }
367 
369  constexpr unsigned int dim() const {
370  return dim_;
371  }
372 
374  constexpr unsigned int id() const {
375  return topologyId_;
376  }
377 
385  constexpr bool operator==(const GeometryType& other) const {
386  return ( ( none_ == other.none_ )
387  && ( ( none_ == true )
388  || ( ( dim_ == other.dim_ )
389  && ( (topologyId_ >> 1) == (other.topologyId_ >> 1) )
390  )
391  )
392  );
393  }
394 
396  constexpr bool operator!=(const GeometryType& other) const {
397  return ! ((*this)==other);
398  }
399 
401  constexpr bool operator < (const GeometryType& other) const {
402  return ( ( none_ < other.none_ )
403  || ( !( other.none_ < none_ )
404  && ( ( dim_ < other.dim_ )
405  || ( (other.dim_ == dim_)
406  && ((topologyId_ >> 1) < (other.topologyId_ >> 1) )
407  )
408  )
409  )
410  );
411  }
412 
415  };
416 
418  inline std::ostream& operator<< (std::ostream& s, const GeometryType& a)
419  {
420  if (a.isSimplex())
421  {
422  s << "(simplex, " << a.dim() << ")";
423  return s;
424  }
425  if (a.isCube())
426  {
427  s << "(cube, " << a.dim() << ")";
428  return s;
429  }
430  if (a.isPyramid())
431  {
432  s << "(pyramid, 3)";
433  return s;
434  }
435  if (a.isPrism())
436  {
437  s << "(prism, 3)";
438  return s;
439  }
440  if (a.isNone())
441  {
442  s << "(none, " << a.dim() << ")";
443  return s;
444  }
445  s << "(other [" << a.id() << "], " << a.dim() << ")";
446  return s;
447  }
448 
449 
451 
455  namespace GeometryTypes {
456 
458 
461  inline constexpr GeometryType simplex(unsigned int dim)
462  {
463  return GeometryType(0,dim,false);
464  }
465 
467 
470  inline constexpr GeometryType cube(unsigned int dim)
471  {
472  return GeometryType(((dim>1) ? ((1 << dim) - 1) : 0),dim,false);
473  }
474 
476 
479  inline constexpr GeometryType none(unsigned int dim)
480  {
481  return GeometryType(0,dim,true);
482  }
483 
485  inline constexpr GeometryType conicalExtension(const GeometryType& gt)
486  {
487  return GeometryType(gt.id(), gt.dim()+1, gt.isNone());
488  }
489 
491  inline constexpr GeometryType prismaticExtension(const GeometryType& gt)
492  {
493  return GeometryType(gt.id() | ((1 << gt.dim())), gt.dim()+1, gt.isNone());
494  }
495 
496 #ifndef __cpp_inline_variables
497  namespace {
498 #endif
499 
501 
504  DUNE_INLINE_VARIABLE constexpr GeometryType vertex = GeometryType(0,0,false);
505 
507 
510  DUNE_INLINE_VARIABLE constexpr GeometryType line = GeometryType(0,1,false);
511 
513 
516  DUNE_INLINE_VARIABLE constexpr GeometryType triangle = simplex(2);
517 
519 
522  DUNE_INLINE_VARIABLE constexpr GeometryType quadrilateral = cube(2);
523 
525 
528  DUNE_INLINE_VARIABLE constexpr GeometryType tetrahedron = simplex(3);
529 
531 
534  DUNE_INLINE_VARIABLE constexpr GeometryType pyramid = GeometryType(0b0011,3,false);
535 
537 
540  DUNE_INLINE_VARIABLE constexpr GeometryType prism = GeometryType(0b0101,3,false);
541 
543 
546  DUNE_INLINE_VARIABLE constexpr GeometryType hexahedron = cube(3);
547 
548 #ifndef __cpp_inline_variables
549  }
550 #endif
551 
552  }
553 
554  namespace Impl
555  {
556 
558  inline constexpr GeometryType getBase(const GeometryType& gt) {
559  return GeometryType(gt.id() & ((1 << (gt.dim()-1))-1), gt.dim()-1, gt.isNone());
560  }
561 
562 
563  // IfGeometryType
564  // ----------
565 
566  template< template< GeometryType::Id > class Operation, int dim, GeometryType::Id geometryId = GeometryTypes::vertex >
567  struct IfGeometryType
568  {
569  static constexpr GeometryType geometry = geometryId;
570  template< class... Args >
571  static auto apply ( GeometryType gt, Args &&... args )
572  {
573  GeometryType lowerGeometry(gt.id() >>1 , gt.dim()-1, gt.isNone());
574 
575  if( gt.id() & 1 )
576  return IfGeometryType< Operation, dim-1, GeometryTypes::prismaticExtension(geometry).toId() >::apply( lowerGeometry, std::forward< Args >( args )... );
577  else
578  return IfGeometryType< Operation, dim-1, GeometryTypes::conicalExtension(geometry).toId() >::apply( lowerGeometry, std::forward< Args >( args )... );
579  }
580  };
581 
582  template< template< GeometryType::Id > class Operation, GeometryType::Id geometryId >
583  struct IfGeometryType< Operation, 0, geometryId>
584  {
585  template< class... Args >
586  static auto apply ([[maybe_unused]] GeometryType gt, Args &&... args )
587  {
588  return Operation< geometryId >::apply( std::forward< Args >( args )... );
589  }
590  };
591  } // namespace Impl
592 } // namespace Dune
593 
594 #endif // DUNE_GEOMETRY_TYPE_HH
constexpr GeometryType line
GeometryType representing a line.
Definition: type.hh:510
constexpr GeometryType cube(unsigned int dim)
Returns a GeometryType representing a hypercube of dimension dim.
Definition: type.hh:470
constexpr GeometryType prism
GeometryType representing a 3D prism.
Definition: type.hh:540
constexpr GeometryType triangle
GeometryType representing a triangle.
Definition: type.hh:516
constexpr GeometryType quadrilateral
GeometryType representing a quadrilateral (a square).
Definition: type.hh:522
constexpr GeometryType hexahedron
GeometryType representing a hexahedron.
Definition: type.hh:546
constexpr GeometryType pyramid
GeometryType representing a 3D pyramid.
Definition: type.hh:534
constexpr GeometryType tetrahedron
GeometryType representing a tetrahedron.
Definition: type.hh:528
constexpr GeometryType none(unsigned int dim)
Returns a GeometryType representing a singular of dimension dim.
Definition: type.hh:479
constexpr GeometryType simplex(unsigned int dim)
Returns a GeometryType representing a simplex of dimension dim.
Definition: type.hh:461
constexpr GeometryType vertex
GeometryType representing a vertex.
Definition: type.hh:504
Definition: affinegeometry.hh:19
std::ostream & operator<<(std::ostream &s, const GeometryType &a)
Prints the type to an output stream.
Definition: type.hh:418
constexpr GeometryType prismaticExtension(const GeometryType &gt)
Return GeometryType of a prismatic construction with gt as base
Definition: type.hh:491
constexpr GeometryType conicalExtension(const GeometryType &gt)
Return GeometryType of a conical construction with gt as base
Definition: type.hh:485
Unique label for each type of entities that can occur in DUNE grids.
Definition: type.hh:123
constexpr GeometryType(unsigned int topologyId, unsigned int dim)
Constructor, using the topologyId (integer) and the dimension.
Definition: type.hh:260
constexpr bool operator<(const GeometryType &other) const
less-than operation for use with maps
Definition: type.hh:401
constexpr bool operator!=(const GeometryType &other) const
Check for inequality.
Definition: type.hh:396
constexpr bool isPyramid() const
Return true if entity is a pyramid.
Definition: type.hh:313
constexpr bool isTetrahedron() const
Return true if entity is a tetrahedron.
Definition: type.hh:308
constexpr bool isPrism() const
Return true if entity is a prism.
Definition: type.hh:318
constexpr bool isVertex() const
Return true if entity is a vertex.
Definition: type.hh:288
constexpr bool operator==(const GeometryType &other) const
Check for equality. This method knows that in dimension 0 and 1 all BasicTypes are equal.
Definition: type.hh:385
constexpr Id toId() const
Create an Id representation of this GeometryType.
Definition: type.hh:219
constexpr bool isConical(const int &step) const
Return true if entity was constructed with a conical product in the chosen step.
Definition: type.hh:346
constexpr unsigned int dim() const
Return dimension of the type.
Definition: type.hh:369
constexpr bool isPrismatic(const int &step) const
Return true if entity was constructed with a prismatic product in the chosen step.
Definition: type.hh:359
constexpr bool isTriangle() const
Return true if entity is a triangle.
Definition: type.hh:298
GeometryType(TopologyType t)
Constructor from static TopologyType class.
Definition: type.hh:276
constexpr GeometryType(unsigned int topologyId, unsigned int dim, bool isNone)
Constructor, using the topologyId (integer), the dimension and a flag for type none.
Definition: type.hh:251
BasicType
Each entity can be tagged by one of these basic types plus its space dimension.
Definition: type.hh:129
@ cube
Cube element in any nonnegative dimension.
Definition: type.hh:131
@ simplex
Simplicial element in any nonnegative dimension.
Definition: type.hh:130
@ pyramid
Four sided pyramid in three dimensions.
Definition: type.hh:132
@ extended
Other, more general topology, representable as topologyId.
Definition: type.hh:134
@ none
Even more general topology, cannot be specified by a topologyId. Two GeometryTypes with 'none' type a...
Definition: type.hh:135
@ prism
Prism element in three dimensions.
Definition: type.hh:133
constexpr GeometryType(Id id)
Reconstruct a Geometry type from a GeometryType::Id.
Definition: type.hh:231
constexpr bool isCube() const
Return true if entity is a cube of any dimension.
Definition: type.hh:333
constexpr GeometryType()
Default constructor, not initializing anything.
Definition: type.hh:241
constexpr bool isConical() const
Return true if entity was constructed with a conical product in the last step.
Definition: type.hh:338
constexpr bool isLine() const
Return true if entity is a line segment.
Definition: type.hh:293
constexpr bool isQuadrilateral() const
Return true if entity is a quadrilateral.
Definition: type.hh:303
constexpr bool isPrismatic() const
Return true if entity was constructed with a prismatic product in the last step.
Definition: type.hh:351
constexpr unsigned int id() const
Return the topology id of the type.
Definition: type.hh:374
constexpr bool isNone() const
Return true if entity is a singular of any dimension.
Definition: type.hh:364
constexpr bool isHexahedron() const
Return true if entity is a hexahedron.
Definition: type.hh:323
constexpr bool isSimplex() const
Return true if entity is a simplex of any dimension.
Definition: type.hh:328
IdType Id
An integral id representing a GeometryType.
Definition: type.hh:190