dune-geometry  2.4
typeindex.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_TYPEINDEX_HH
4 #define DUNE_GEOMETRY_TYPEINDEX_HH
5 
12 #include <cstddef>
13 
14 #include <dune/common/std/constexpr.hh>
15 
16 #include "type.hh"
17 
18 namespace Dune
19 {
22  {
29  DUNE_CONSTEXPR inline static std::size_t regular_size(std::size_t dim)
30  {
31  // The following expression is derived from the expression for
32  // GlobalGeometryTypeIndex::regular_base(). Substracting
33  // regular_base(dim+1)-regular_base(dim) we get:
34  //
35  // ((1 << dim+1) >> 1) - ((1 << dim) >> 1)
36  //
37  // We always have
38  //
39  // dim >= 0,
40  //
41  // so
42  //
43  // (1 << dim+1) >= 2 and (1 << dim+2) % 2 == 0.
44  //
45  // So if we apply a single right-shift to that, we will never lose any
46  // set bits, thus
47  //
48  // ((1 << dim+1) >> 1) == (1 << dim)
49  return (1 << dim) - ((1 << dim) >> 1);
50  }
51 
52  public:
58  DUNE_CONSTEXPR inline static std::size_t size(std::size_t dim)
59  {
60  // one for "none"
61  return regular_size(dim) + 1;
62  }
63 
70  inline static std::size_t index(const GeometryType &gt)
71  {
72  if(gt.isNone())
73  {
74  return regular_size(gt.dim());
75  }
76  else
77  {
78  return gt.id() >> 1;
79  }
80  }
81 
83  inline static GeometryType type(std::size_t dim, std::size_t index) {
84  if(index == regular_size(dim)) {
85  GeometryType gt;
86  gt.makeNone(dim);
87  return gt;
88  }
89  else {
90  // the cast to unsigned makes sure this is interpreted as the topology
91  // ID constructor
92  return GeometryType(unsigned(index << 1), dim);
93  }
94  }
95  };
96 
99  {
107  DUNE_CONSTEXPR inline static std::size_t regular_base(std::size_t dim)
108  {
109  // The number of regular geometry types in a given dimension is
110  // 2^(dim-1). For dim==0 this would yield 1/2 geometry types (which is
111  // obviously bogus, dim==0 has one regular geometry type, the point).
112  // The following expression relies on 1 >> 1 == 0 to treat dim==0
113  // specially.
114  return (1 << dim) >> 1;
115  }
116 
121  DUNE_CONSTEXPR inline static std::size_t base(std::size_t dim)
122  {
123  // dim times "none"
124  return regular_base(dim) + dim;
125  }
126 
127  public:
134  DUNE_CONSTEXPR inline static std::size_t size(std::size_t maxdim)
135  {
136  return base(maxdim+1);
137  }
138 
147  inline static std::size_t index(const GeometryType &gt)
148  {
149  return base(gt.dim()) + LocalGeometryTypeIndex::index(gt);
150  }
151  };
152 } // namespace Dune
153 
154 #endif // DUNE_GEOMETRY_TYPEINDEX_HH
void makeNone(unsigned int dim)
Make a singular of given dimension.
Definition: type.hh:207
unsigned int id() const
Return the topology id the type.
Definition: type.hh:326
Definition: affinegeometry.hh:18
static DUNE_CONSTEXPR std::size_t size(std::size_t maxdim)
Compute total number of geometry types up to and including the given dimension.
Definition: typeindex.hh:134
static GeometryType type(std::size_t dim, std::size_t index)
compute the geometry type for the given local index and dimension
Definition: typeindex.hh:83
bool isNone() const
Return true if entity is a singular of any dimension.
Definition: type.hh:316
static std::size_t index(const GeometryType &gt)
Compute the index for the given geometry type over all dimensions.
Definition: typeindex.hh:147
Compute indices for geometry types, taking the dimension into account.
Definition: typeindex.hh:98
A unique label for each type of element that can occur in a grid.
static std::size_t index(const GeometryType &gt)
Compute the index for the given geometry type within its dimension.
Definition: typeindex.hh:70
Unique label for each type of entities that can occur in DUNE grids.
Definition: type.hh:24
unsigned int dim() const
Return dimension of the type.
Definition: type.hh:321
Compute per-dimension indices for geometry types.
Definition: typeindex.hh:21
static DUNE_CONSTEXPR std::size_t size(std::size_t dim)
Compute total number of geometry types for the given dimension.
Definition: typeindex.hh:58