dune-geometry  2.2.1
type.hh
Go to the documentation of this file.
1 // -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 // vi: set et ts=4 sw=4 sts=4:
3 
4 #include <cassert>
5 #ifndef DUNE_GEOMETRY_TYPE_HH
6 #define DUNE_GEOMETRY_TYPE_HH
7 
8 #define DISABLE_GEOMETRYTYPE_DEPRECATION_WARNING
9 #include <dune/common/geometrytype.hh>
10 #undef DISABLE_GEOMETRYTYPE_DEPRECATION_WARNING
11 
12 #ifdef DOXYGEN
13 // Compare the following code with the original header dune/common/geometrytype.hh
14 // for changes before removing the deprecated header!
15 
20 #include <dune/common/exceptions.hh>
21 #include <dune/common/deprecated.hh>
22 
23 namespace Dune {
24 
33  {
34  public:
37  enum BasicType {
39  cube,
44  };
45 
47  enum Binary {
48  b0001 = 1,
49  b0011 = 3,
50  b0101 = 5,
51  b0111 = 7
52  };
53  private:
54 
56  unsigned int topologyId_;
57 
59  unsigned char dim_ : 7;
60 
62  bool none_ : 1;
63 
64  public:
67  : topologyId_(0), dim_(0), none_(true)
68  {}
69 
72  : topologyId_(0), dim_(dim), none_(false)
73  {
74  if (dim < 2)
75  return;
76  switch( basicType )
77  {
79  makeSimplex(dim);
80  break;
81  case GeometryType::cube:
82  makeCube(dim);
83  break;
85  if (dim == 3)
86  makePyramid();
87  else
88  DUNE_THROW( RangeError,
89  "Invalid basic geometry type: no pyramids for dimension " << dim << "." );
90  break;
92  if (dim == 3)
93  makePrism();
94  else
95  DUNE_THROW( RangeError,
96  "Invalid basic geometry type: no prisms for dimension " << dim << "." );
97  break;
98  case GeometryType::none:
99  makeNone(dim);
100  break;
101  default:
102  DUNE_THROW( RangeError,
103  "Invalid basic geometry type: " << basicType << " for dimension " << dim << "." );
104  }
105  }
106 
108  GeometryType(unsigned int topologyId, unsigned int dim)
109  : topologyId_(topologyId), dim_(dim), none_(false)
110  {}
111 
122  template<class TopologyType>
123  explicit GeometryType(TopologyType t)
124  : topologyId_(TopologyType::id), dim_(TopologyType::dimension), none_(false)
125  {}
126 
128  explicit GeometryType(unsigned int dim)
129  : topologyId_(0), dim_(dim), none_(false)
130  {
131  assert(dim < 2);
132  }
133 
135  // We need this constructor for "int" and "unsigned int",
136  // because otherwise GeometryType(int) would try to call the
137  // generic GeometryType(TopologyType) constructor
138  explicit GeometryType(int dim)
139  : topologyId_(0), dim_(dim), none_(false)
140  {
141  assert(dim < 2);
142  }
143 
146 
148  void makeVertex() {
149  none_ = false;
150  dim_ = 0;
151  topologyId_ = 0;
152  }
153 
155  void makeLine() {
156  none_ = false;
157  dim_ = 1;
158  topologyId_ = 0;
159  }
160 
162  void makeTriangle() {
163  makeSimplex(2);
164  }
165 
168  makeCube(2);
169  }
170 
173  makeSimplex(3);
174  }
175 
177  void makePyramid() {
178  none_ = false;
179  dim_ = 3;
180  topologyId_ = b0011;
181  }
182 
184  void makePrism() {
185  none_ = false;
186  dim_ = 3;
187  topologyId_ = b0101; // (1 << (dim_-1)) - 1;
188  }
189 
191  void makeHexahedron() {
192  makeCube(3);
193  }
194 
196  void makeSimplex(unsigned int dim) {
197  none_ = false;
198  dim_ = dim;
199  topologyId_ = 0;
200  }
201 
203  void makeCube(unsigned int dim) {
204  none_ = false;
205  dim_ = dim;
206  topologyId_ = ((dim>1) ? ((1 << dim) - 1) : 0);
207  }
208 
210  void makeNone(unsigned int dim) {
211  none_ = true;
212  dim_ = dim;
213  topologyId_ = 0;
214  }
215 
222  bool isVertex() const {
223  return dim_==0;
224  }
225 
227  bool isLine() const {
228  return dim_==1;
229  }
230 
232  bool isTriangle() const {
233  return ! none_ && dim_==2 && (topologyId_ | 1) == b0001;
234  }
235 
237  bool isQuadrilateral() const {
238  return ! none_ && dim_==2 && (topologyId_ | 1) == b0011;
239  }
240 
242  bool isTetrahedron() const {
243  return ! none_ && dim_==3 && (topologyId_ | 1) == b0001;
244  }
245 
247  bool isPyramid() const {
248  return ! none_ && dim_==3 && (topologyId_ | 1) == b0011;
249  }
250 
252  bool isPrism() const {
253  return ! none_ && dim_==3 && (topologyId_ | 1) == b0101;
254  }
255 
257  bool isHexahedron() const {
258  return ! none_ && dim_==3 && (topologyId_ | 1) == b0111;
259  }
260 
262  bool isSimplex() const {
263  return ! none_ && (topologyId_ | 1) == 1;
264  }
265 
267  bool isCube() const {
268  return ! none_ && ((topologyId_ ^ ((1 << dim_)-1)) >> 1 == 0);
269  }
270 
272  bool isNone() const {
273  return none_;
274  }
275 
277  unsigned int dim() const {
278  return dim_;
279  }
280 
282  BasicType basicType() const DUNE_DEPRECATED {
283  if (isSimplex())
284  return GeometryType::simplex;
285  if (isCube())
286  return GeometryType::cube;
287  if (isPyramid())
288  return GeometryType::pyramid;
289  if (isPrism())
290  return GeometryType::prism;
291  if (isNone())
292  return GeometryType::none;
293  return GeometryType::extended;
294  }
295 
297  unsigned int id() const {
298  return topologyId_;
299  }
300 
306  bool operator==(const GeometryType& other) const {
307  return ( ( none_ == other.none_ )
308  && ( ( none_ == true )
309  || ( ( dim_ == other.dim_ )
310  && ( (topologyId_ >> 1) == (other.topologyId_ >> 1) )
311  )
312  )
313  );
314  }
315 
317  bool operator!=(const GeometryType& other) const {
318  return ! ((*this)==other);
319  }
320 
322  bool operator < (const GeometryType& other) const {
323  return ( ( none_ < other.none_ )
324  || ( !( other.none_ < none_ )
325  && ( ( dim_ < other.dim_ )
326  || ( (other.dim_ == dim_)
327  && ((topologyId_ >> 1) < (other.topologyId_ >> 1) )
328  )
329  )
330  )
331  );
332  }
333  };
334 
336  inline std::ostream& operator<< (std::ostream& s, const GeometryType& a)
337  {
338  if (a.isSimplex())
339  {
340  s << "(simplex, " << a.dim() << ")";
341  return s;
342  }
343  if (a.isCube())
344  {
345  s << "(cube, " << a.dim() << ")";
346  return s;
347  }
348  if (a.isPyramid())
349  {
350  s << "(pyramid, 3)";
351  return s;
352  }
353  if (a.isPrism())
354  {
355  s << "(prism, 3)";
356  return s;
357  }
358  if (a.isNone())
359  {
360  s << "(none, " << a.dim() << ")"; else // Doxygen
361  return s;
362  }
363  s << "(other [" << a.id() << "], " << a.dim() << ")";
364  return s;
365  }
366 
368  inline std::ostream& operator<< (std::ostream& s, GeometryType::BasicType type)
369  {
370  switch (type) {
372  s << "simplex";
373  break;
374  case GeometryType::cube:
375  s << "cube";
376  break;
378  s << "pyramid";
379  break;
380  case GeometryType::prism:
381  s << "prism";
382  break;
384  s << "other";
385  case GeometryType::none:
386  s << "none";
387  break;
388  default:
389  DUNE_THROW(Exception, "invalid GeometryType::BasicType");
390  }
391  return s;
392  }
393 }
394 #endif // #ifdef DOXYGEN
395 
396 #endif // DUNE_GEOMETRY_TYPE_HH