dune-geometry  2.2.0
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  break;
89  if (dim == 3)
90  makePrism();
91  break;
92  case GeometryType::none:
93  makeNone(dim);
94  break;
95  default:
96  DUNE_THROW( RangeError,
97  "Invalid basic geometry type: " << basicType << " for dimension " << dim << "." );
98  }
99  }
100 
102  GeometryType(unsigned int topologyId, unsigned int dim)
103  : topologyId_(topologyId), dim_(dim), none_(false)
104  {}
105 
116  template<class TopologyType>
117  explicit GeometryType(TopologyType t)
118  : topologyId_(TopologyType::id), dim_(TopologyType::dimension), none_(false)
119  {}
120 
122  explicit GeometryType(unsigned int dim)
123  : topologyId_(0), dim_(dim), none_(false)
124  {
125  assert(dim < 2);
126  }
127 
129  // We need this constructor for "int" and "unsigned int",
130  // because otherwise GeometryType(int) would try to call the
131  // generic GeometryType(TopologyType) constructor
132  explicit GeometryType(int dim)
133  : topologyId_(0), dim_(dim), none_(false)
134  {
135  assert(dim < 2);
136  }
137 
140 
142  void makeVertex() {
143  none_ = false;
144  dim_ = 0;
145  topologyId_ = 0;
146  }
147 
149  void makeLine() {
150  none_ = false;
151  dim_ = 1;
152  topologyId_ = 0;
153  }
154 
156  void makeTriangle() {
157  makeSimplex(2);
158  }
159 
162  makeCube(2);
163  }
164 
167  makeSimplex(3);
168  }
169 
171  void makePyramid() {
172  none_ = false;
173  dim_ = 3;
174  topologyId_ = b0011;
175  }
176 
178  void makePrism() {
179  none_ = false;
180  dim_ = 3;
181  topologyId_ = b0101; // (1 << (dim_-1)) - 1;
182  }
183 
185  void makeHexahedron() {
186  makeCube(3);
187  }
188 
190  void makeSimplex(unsigned int dim) {
191  none_ = false;
192  dim_ = dim;
193  topologyId_ = 0;
194  }
195 
197  void makeCube(unsigned int dim) {
198  none_ = false;
199  dim_ = dim;
200  topologyId_ = ((dim>1) ? ((1 << dim) - 1) : 0);
201  }
202 
204  void makeNone(unsigned int dim) {
205  none_ = true;
206  dim_ = dim;
207  topologyId_ = 0;
208  }
209 
216  bool isVertex() const {
217  return dim_==0;
218  }
219 
221  bool isLine() const {
222  return dim_==1;
223  }
224 
226  bool isTriangle() const {
227  return ! none_ && dim_==2 && (topologyId_ | 1) == b0001;
228  }
229 
231  bool isQuadrilateral() const {
232  return ! none_ && dim_==2 && (topologyId_ | 1) == b0011;
233  }
234 
236  bool isTetrahedron() const {
237  return ! none_ && dim_==3 && (topologyId_ | 1) == b0001;
238  }
239 
241  bool isPyramid() const {
242  return ! none_ && dim_==3 && (topologyId_ | 1) == b0011;
243  }
244 
246  bool isPrism() const {
247  return ! none_ && dim_==3 && (topologyId_ | 1) == b0101;
248  }
249 
251  bool isHexahedron() const {
252  return ! none_ && dim_==3 && (topologyId_ | 1) == b0111;
253  }
254 
256  bool isSimplex() const {
257  return ! none_ && (topologyId_ | 1) == 1;
258  }
259 
261  bool isCube() const {
262  return ! none_ && ((topologyId_ ^ ((1 << dim_)-1)) >> 1 == 0);
263  }
264 
266  bool isNone() const {
267  return none_;
268  }
269 
271  unsigned int dim() const {
272  return dim_;
273  }
274 
276  BasicType basicType() const DUNE_DEPRECATED {
277  if (isSimplex())
278  return GeometryType::simplex;
279  if (isCube())
280  return GeometryType::cube;
281  if (isPyramid())
282  return GeometryType::pyramid;
283  if (isPrism())
284  return GeometryType::prism;
285  if (isNone())
286  return GeometryType::none;
287  return GeometryType::extended;
288  }
289 
291  unsigned int id() const {
292  return topologyId_;
293  }
294 
300  bool operator==(const GeometryType& other) const {
301  return ( ( none_ == other.none_ )
302  && ( ( none_ == true )
303  || ( ( dim_ == other.dim_ )
304  && ( (topologyId_ >> 1) == (other.topologyId_ >> 1) )
305  )
306  )
307  );
308  }
309 
311  bool operator!=(const GeometryType& other) const {
312  return ! ((*this)==other);
313  }
314 
316  bool operator < (const GeometryType& other) const {
317  return ( ( none_ < other.none_ )
318  || ( !( other.none_ < none_ )
319  && ( ( dim_ < other.dim_ )
320  || ( (other.dim_ == dim_)
321  && ((topologyId_ >> 1) < (other.topologyId_ >> 1) )
322  )
323  )
324  )
325  );
326  }
327  };
328 
330  inline std::ostream& operator<< (std::ostream& s, const GeometryType& a)
331  {
332  if (a.isSimplex())
333  {
334  s << "(simplex, " << a.dim() << ")";
335  return s;
336  }
337  if (a.isCube())
338  {
339  s << "(cube, " << a.dim() << ")";
340  return s;
341  }
342  if (a.isPyramid())
343  {
344  s << "(pyramid, 3)";
345  return s;
346  }
347  if (a.isPrism())
348  {
349  s << "(prism, 3)";
350  return s;
351  }
352  if (a.isNone())
353  {
354  s << "(none, " << a.dim() << ")"; else // Doxygen
355  return s;
356  }
357  s << "(other [" << a.id() << "], " << a.dim() << ")";
358  return s;
359  }
360 
362  inline std::ostream& operator<< (std::ostream& s, GeometryType::BasicType type)
363  {
364  switch (type) {
366  s << "simplex";
367  break;
368  case GeometryType::cube:
369  s << "cube";
370  break;
372  s << "pyramid";
373  break;
374  case GeometryType::prism:
375  s << "prism";
376  break;
378  s << "other";
379  case GeometryType::none:
380  s << "none";
381  break;
382  default:
383  DUNE_THROW(Exception, "invalid GeometryType::BasicType");
384  }
385  return s;
386  }
387 }
388 #endif // #ifdef DOXYGEN
389 
390 #endif // DUNE_GEOMETRY_TYPE_HH