dune-geometry  2.8.0
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/indices.hh>
15 #include <dune/common/hybridutilities.hh>
16 
17 #include "type.hh"
18 
19 namespace Dune
20 {
23  {
30  inline static constexpr std::size_t regular_size(std::size_t dim)
31  {
32  // The following expression is derived from the expression for
33  // GlobalGeometryTypeIndex::regular_offset(). Subtracting
34  // regular_offset(dim+1)-regular_offset(dim) we get:
35  //
36  // ((1 << dim+1) >> 1) - ((1 << dim) >> 1)
37  //
38  // We always have
39  //
40  // dim >= 0,
41  //
42  // so
43  //
44  // (1 << dim+1) >= 2 and (1 << dim+2) % 2 == 0.
45  //
46  // So if we apply a single right-shift to that, we will never lose any
47  // set bits, thus
48  //
49  // ((1 << dim+1) >> 1) == (1 << dim)
50  return (1 << dim) - ((1 << dim) >> 1);
51  }
52 
53  public:
59  inline static constexpr std::size_t size(std::size_t dim)
60  {
61  // one for "none"
62  return regular_size(dim) + 1;
63  }
64 
71  inline static constexpr std::size_t index(const GeometryType &gt)
72  {
73  return gt.isNone() ? regular_size(gt.dim()) : (gt.id() >> 1);
74  }
75 
77  inline static constexpr GeometryType type(std::size_t dim, std::size_t index) {
78  return (index == regular_size(dim)) ?
79  GeometryTypes::none(dim) :
80  // the cast to unsigned makes sure this is interpreted as the topology
81  // ID constructor
82  GeometryType(static_cast< unsigned int >(index << 1), dim);
83  }
84  };
85 
88  {
96  inline static constexpr std::size_t regular_offset(std::size_t dim)
97  {
98  // The number of regular geometry types in a given dimension is
99  // 2^(dim-1). For dim==0 this would yield 1/2 geometry types (which is
100  // obviously bogus, dim==0 has one regular geometry type, the point).
101  // The following expression relies on 1 >> 1 == 0 to treat dim==0
102  // specially.
103  return (1 << dim) >> 1;
104  }
105 
106  public:
111  inline static constexpr std::size_t offset(std::size_t dim)
112  {
113  // dim times "none"
114  return regular_offset(dim) + dim;
115  }
116 
123  inline static constexpr std::size_t size(std::size_t maxdim)
124  {
125  return offset(maxdim+1);
126  }
127 
136  inline static constexpr std::size_t index(const GeometryType &gt)
137  {
138  return offset(gt.dim()) + LocalGeometryTypeIndex::index(gt);
139  }
140  };
141 
142  namespace Impl {
143 
144  // Map a dynamic GeometryType to a static integral_constant<GeometryType::Id, ...>
145  template<int dim, class F>
146  static auto toGeometryTypeIdConstant(const GeometryType& gt, F&& f) {
147  // Transform LocalGeometryTypeIndex to GeometryType::Id
148  auto callWithId = [&](auto index) {
149  static constexpr auto id = LocalGeometryTypeIndex::type(dim, decltype(index)::value).toId();
150  return f(std::integral_constant<GeometryType::Id, id>{});
151  };
152  // switchCases needs a fallback to determine the return type.
153  auto fallBack = [&]() { return callWithId(Dune::Indices::_0); };
154  // Iterate over all _regular_ GeometryType indices for given dimension
155  auto allIndices = std::make_index_sequence<LocalGeometryTypeIndex::size(dim)-1>{};
156  return Dune::Hybrid::switchCases(allIndices, LocalGeometryTypeIndex::index(gt), callWithId, fallBack);
157  }
158 
159  } // namespace Impl
160 } // namespace Dune
161 
162 #endif // DUNE_GEOMETRY_TYPEINDEX_HH
A unique label for each type of element that can occur in a grid.
constexpr GeometryType none(unsigned int dim)
Returns a GeometryType representing a singular of dimension dim.
Definition: type.hh:479
Definition: affinegeometry.hh:19
Unique label for each type of entities that can occur in DUNE grids.
Definition: type.hh:123
constexpr Id toId() const
Create an Id representation of this GeometryType.
Definition: type.hh:219
constexpr unsigned int dim() const
Return dimension of the type.
Definition: type.hh:369
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
Compute per-dimension indices for geometry types.
Definition: typeindex.hh:23
static constexpr std::size_t size(std::size_t dim)
Compute total number of geometry types for the given dimension.
Definition: typeindex.hh:59
static constexpr GeometryType type(std::size_t dim, std::size_t index)
compute the geometry type for the given local index and dimension
Definition: typeindex.hh:77
static constexpr std::size_t index(const GeometryType &gt)
Compute the index for the given geometry type within its dimension.
Definition: typeindex.hh:71
Compute indices for geometry types, taking the dimension into account.
Definition: typeindex.hh:88
static constexpr std::size_t index(const GeometryType &gt)
Compute the index for the given geometry type over all dimensions.
Definition: typeindex.hh:136
static constexpr std::size_t offset(std::size_t dim)
Compute the starting index for a given dimension including irregular geometry types.
Definition: typeindex.hh:111
static 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:123