DUNE PDELab (2.8)

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#include <cstdint>
12
13#include <string>
14#include <type_traits>
15
19#include <dune/common/unused.hh>
20
21namespace Dune
22{
23
24 namespace Impl
25 {
26
27 enum TopologyConstruction { pyramidConstruction = 0, prismConstruction = 1 };
28
29 // Dynamic Topology Properties
30 // ---------------------------
31
40 inline static unsigned int numTopologies ( int dim ) noexcept
41 {
42 return (1u << dim);
43 }
44
56 inline bool static isPyramid ( unsigned int topologyId, int dim, int codim = 0 ) noexcept
57 {
58 assert( (dim > 0) && (topologyId < numTopologies( dim )) );
59 assert( (0 <= codim) && (codim < dim) );
60 return (((topologyId & ~1) & (1u << (dim-codim-1))) == 0);
61 }
62
74 inline static bool isPrism ( unsigned int topologyId, int dim, int codim = 0 ) noexcept
75 {
76 assert( (dim > 0) && (topologyId < numTopologies( dim )) );
77 assert( (0 <= codim) && (codim < dim) );
78 return (( (topologyId | 1) & (1u << (dim-codim-1))) != 0);
79 }
80
88 inline static unsigned int baseTopologyId ( unsigned int topologyId, int dim, int codim = 1 ) noexcept
89 {
90 assert( (dim >= 0) && (topologyId < numTopologies( dim )) );
91 assert( (0 <= codim) && (codim <= dim) );
92 return topologyId & ((1u << (dim-codim)) - 1);
93 }
94
95 } // namespace Impl
96
97// the Topology classes are deprecated and will be removed for the 2.8.
98// Temporarily a header 'deprecated_topology.hh' is provided which will be removed after the 2.9 release.
99#if __GNUC__ >= 7
100# pragma GCC diagnostic push
101# pragma GCC diagnostic ignored "-Wdeprecated-declarations"
102#endif
103#include <dune/geometry/deprecated_topology.hh>
104#if __GNUC__ >= 7
105# pragma GCC diagnostic pop
106#endif
107
108 // GeometryType
109 // -------------
110
124 {
125 public:
126
129 enum
130 BasicType {
136 none
137 };
138
139 private:
140
142 unsigned char dim_;
143
145 bool none_;
146
148 unsigned int topologyId_;
149
150 // Internal type used for the Id. The exact nature of this type is kept
151 // as an implementation detail on purpose. We use a scoped enum here because scoped enums
152 // can be used as template parameters, but are not implicitly converted to other integral
153 // types by the compiler. That way, we avoid unfortunate implicit conversion chains, e.g.
154 // people trying to work with GlobalGeometryTypeIndex, but forgetting to actually call
155 // GlobalGeometryTypeIndex::index(gt) and just using gt directly.
156 enum class IdType : std::uint64_t
157 {};
158
159 public:
160
191 using Id = IdType;
192
200 constexpr operator Id() const
201 {
202 // recreate the exact storage layout that this class is using, making conversion
203 // extremely cheap
204 std::uint64_t id = dim_ | (std::uint64_t(none_) << 8) | (std::uint64_t(topologyId_) << 32);
205 return static_cast<Id>(id);
206 }
207
220 constexpr Id toId() const
221 {
222 return static_cast<Id>(*this);
223 }
224
232 constexpr GeometryType(Id id)
233 : dim_(static_cast<std::uint64_t>(id) & 0xFF)
234 , none_(static_cast<std::uint64_t>(id) & 0x100)
235 , topologyId_(static_cast<std::uint64_t>(id) >> 32)
236 {}
237
240
242 constexpr GeometryType ()
243 : dim_(0), none_(true), topologyId_(0)
244 {}
245
252 constexpr GeometryType(unsigned int topologyId, unsigned int dim, bool isNone)
253 : dim_(dim), none_(isNone), topologyId_(topologyId)
254 {}
255
261 constexpr GeometryType(unsigned int topologyId, unsigned int dim)
262 : dim_(dim), none_(false), topologyId_(topologyId)
263 {}
264
275 template<class TopologyType,
276 class = std::void_t<decltype(TopologyType::dimension), decltype(TopologyType::id)>>
277 explicit GeometryType(TopologyType t)
278 : dim_(TopologyType::dimension), none_(false), topologyId_(TopologyType::id)
279 {
281 }
282
289 constexpr bool isVertex() const {
290 return dim_==0;
291 }
292
294 constexpr bool isLine() const {
295 return dim_==1;
296 }
297
299 constexpr bool isTriangle() const {
300 return ! none_ && dim_==2 && (topologyId_ | 1) == 0b0001;
301 }
302
304 constexpr bool isQuadrilateral() const {
305 return ! none_ && dim_==2 && (topologyId_ | 1) == 0b0011;
306 }
307
309 constexpr bool isTetrahedron() const {
310 return ! none_ && dim_==3 && (topologyId_ | 1) == 0b0001;
311 }
312
314 constexpr bool isPyramid() const {
315 return ! none_ && dim_==3 && (topologyId_ | 1) == 0b0011;
316 }
317
319 constexpr bool isPrism() const {
320 return ! none_ && dim_==3 && (topologyId_ | 1) == 0b0101;
321 }
322
324 constexpr bool isHexahedron() const {
325 return ! none_ && dim_==3 && (topologyId_ | 1) == 0b0111;
326 }
327
329 constexpr bool isSimplex() const {
330 return ! none_ && (topologyId_ | 1) == 1;
331 }
332
334 constexpr bool isCube() const {
335 return ! none_ && ((topologyId_ ^ ((1 << dim_)-1)) >> 1 == 0);
336 }
337
339 constexpr bool isConical() const {
340 return ! none_ && (((topologyId_ & ~1) & (1u << (dim_-1))) == 0);
341 }
342
347 constexpr bool isConical(const int& step) const {
348 return ! none_ && (((topologyId_ & ~1) & (1u << step)) == 0);
349 }
350
352 constexpr bool isPrismatic() const {
353 return ! none_ && (( (topologyId_ | 1) & (1u << (dim_-1))) != 0);
354 }
355
360 constexpr bool isPrismatic(const int& step) const {
361 return ! none_ && (( (topologyId_ | 1) & (1u << step)) != 0);
362 }
363
365 constexpr bool isNone() const {
366 return none_;
367 }
368
370 constexpr unsigned int dim() const {
371 return dim_;
372 }
373
375 constexpr unsigned int id() const {
376 return topologyId_;
377 }
378
386 constexpr bool operator==(const GeometryType& other) const {
387 return ( ( none_ == other.none_ )
388 && ( ( none_ == true )
389 || ( ( dim_ == other.dim_ )
390 && ( (topologyId_ >> 1) == (other.topologyId_ >> 1) )
391 )
392 )
393 );
394 }
395
397 constexpr bool operator!=(const GeometryType& other) const {
398 return ! ((*this)==other);
399 }
400
402 constexpr bool operator < (const GeometryType& other) const {
403 return ( ( none_ < other.none_ )
404 || ( !( other.none_ < none_ )
405 && ( ( dim_ < other.dim_ )
406 || ( (other.dim_ == dim_)
407 && ((topologyId_ >> 1) < (other.topologyId_ >> 1) )
408 )
409 )
410 )
411 );
412 }
413
416 };
417
419 inline std::ostream& operator<< (std::ostream& s, const GeometryType& a)
420 {
421 if (a.isSimplex())
422 {
423 s << "(simplex, " << a.dim() << ")";
424 return s;
425 }
426 if (a.isCube())
427 {
428 s << "(cube, " << a.dim() << ")";
429 return s;
430 }
431 if (a.isPyramid())
432 {
433 s << "(pyramid, 3)";
434 return s;
435 }
436 if (a.isPrism())
437 {
438 s << "(prism, 3)";
439 return s;
440 }
441 if (a.isNone())
442 {
443 s << "(none, " << a.dim() << ")";
444 return s;
445 }
446 s << "(other [" << a.id() << "], " << a.dim() << ")";
447 return s;
448 }
449
450
452
456 namespace GeometryTypes {
457
459
462 inline constexpr GeometryType simplex(unsigned int dim)
463 {
464 return GeometryType(0,dim,false);
465 }
466
468
471 inline constexpr GeometryType cube(unsigned int dim)
472 {
473 return GeometryType(((dim>1) ? ((1 << dim) - 1) : 0),dim,false);
474 }
475
477
480 inline constexpr GeometryType none(unsigned int dim)
481 {
482 return GeometryType(0,dim,true);
483 }
484
487 {
488 return GeometryType(gt.id(), gt.dim()+1, gt.isNone());
489 }
490
493 {
494 return GeometryType(gt.id() | ((1 << gt.dim())), gt.dim()+1, gt.isNone());
495 }
496
497#ifndef __cpp_inline_variables
498 namespace {
499#endif
500
502
505 DUNE_INLINE_VARIABLE constexpr GeometryType vertex = GeometryType(0,0,false);
506
508
511 DUNE_INLINE_VARIABLE constexpr GeometryType line = GeometryType(0,1,false);
512
514
517 DUNE_INLINE_VARIABLE constexpr GeometryType triangle = simplex(2);
518
520
523 DUNE_INLINE_VARIABLE constexpr GeometryType quadrilateral = cube(2);
524
526
529 DUNE_INLINE_VARIABLE constexpr GeometryType tetrahedron = simplex(3);
530
532
535 DUNE_INLINE_VARIABLE constexpr GeometryType pyramid = GeometryType(0b0011,3,false);
536
538
541 DUNE_INLINE_VARIABLE constexpr GeometryType prism = GeometryType(0b0101,3,false);
542
544
547 DUNE_INLINE_VARIABLE constexpr GeometryType hexahedron = cube(3);
548
549#ifndef __cpp_inline_variables
550 }
551#endif
552
553 }
554
555 namespace Impl
556 {
557
559 inline constexpr GeometryType getBase(const GeometryType& gt) {
560 return GeometryType(gt.id() & ((1 << (gt.dim()-1))-1), gt.dim()-1, gt.isNone());
561 }
562
563
564 // IfGeometryType
565 // ----------
566
567 template< template< GeometryType::Id > class Operation, int dim, GeometryType::Id geometryId = GeometryTypes::vertex >
568 struct IfGeometryType
569 {
570 static constexpr GeometryType geometry = geometryId;
571 template< class... Args >
572 static auto apply ( GeometryType gt, Args &&... args )
573 {
574 GeometryType lowerGeometry(gt.id() >>1 , gt.dim()-1, gt.isNone());
575
576 if( gt.id() & 1 )
577 return IfGeometryType< Operation, dim-1, GeometryTypes::prismaticExtension(geometry).toId() >::apply( lowerGeometry, std::forward< Args >( args )... );
578 else
579 return IfGeometryType< Operation, dim-1, GeometryTypes::conicalExtension(geometry).toId() >::apply( lowerGeometry, std::forward< Args >( args )... );
580 }
581 };
582
583 template< template< GeometryType::Id > class Operation, GeometryType::Id geometryId >
584 struct IfGeometryType< Operation, 0, geometryId>
585 {
586 template< class... Args >
587 static auto apply ([[maybe_unused]] GeometryType gt, Args &&... args )
588 {
589 return Operation< geometryId >::apply( std::forward< Args >( args )... );
590 }
591 };
592 } // namespace Impl
593} // namespace Dune
594
595#endif // DUNE_GEOMETRY_TYPE_HH
Unique label for each type of entities that can occur in DUNE grids.
Definition: type.hh:124
constexpr GeometryType(unsigned int topologyId, unsigned int dim)
Constructor, using the topologyId (integer) and the dimension.
Definition: type.hh:261
constexpr bool operator<(const GeometryType &other) const
less-than operation for use with maps
Definition: type.hh:402
constexpr bool operator!=(const GeometryType &other) const
Check for inequality.
Definition: type.hh:397
constexpr bool isPyramid() const
Return true if entity is a pyramid.
Definition: type.hh:314
constexpr bool isTetrahedron() const
Return true if entity is a tetrahedron.
Definition: type.hh:309
constexpr bool isPrism() const
Return true if entity is a prism.
Definition: type.hh:319
constexpr bool isVertex() const
Return true if entity is a vertex.
Definition: type.hh:289
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:386
constexpr Id toId() const
Create an Id representation of this GeometryType.
Definition: type.hh:220
constexpr bool isConical(const int &step) const
Return true if entity was constructed with a conical product in the chosen step.
Definition: type.hh:347
constexpr unsigned int dim() const
Return dimension of the type.
Definition: type.hh:370
constexpr bool isPrismatic(const int &step) const
Return true if entity was constructed with a prismatic product in the chosen step.
Definition: type.hh:360
constexpr bool isTriangle() const
Return true if entity is a triangle.
Definition: type.hh:299
GeometryType(TopologyType t)
Constructor from static TopologyType class.
Definition: type.hh:277
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:252
BasicType
Each entity can be tagged by one of these basic types plus its space dimension.
Definition: type.hh:130
@ cube
Cube element in any nonnegative dimension.
Definition: type.hh:132
@ simplex
Simplicial element in any nonnegative dimension.
Definition: type.hh:131
@ pyramid
Four sided pyramid in three dimensions.
Definition: type.hh:133
@ extended
Other, more general topology, representable as topologyId.
Definition: type.hh:135
@ none
Even more general topology, cannot be specified by a topologyId. Two GeometryTypes with 'none' type a...
Definition: type.hh:136
@ prism
Prism element in three dimensions.
Definition: type.hh:134
constexpr GeometryType(Id id)
Reconstruct a Geometry type from a GeometryType::Id.
Definition: type.hh:232
constexpr bool isCube() const
Return true if entity is a cube of any dimension.
Definition: type.hh:334
constexpr GeometryType()
Default constructor, not initializing anything.
Definition: type.hh:242
constexpr bool isConical() const
Return true if entity was constructed with a conical product in the last step.
Definition: type.hh:339
constexpr bool isLine() const
Return true if entity is a line segment.
Definition: type.hh:294
constexpr bool isQuadrilateral() const
Return true if entity is a quadrilateral.
Definition: type.hh:304
constexpr bool isPrismatic() const
Return true if entity was constructed with a prismatic product in the last step.
Definition: type.hh:352
constexpr unsigned int id() const
Return the topology id of the type.
Definition: type.hh:375
constexpr bool isNone() const
Return true if entity is a singular of any dimension.
Definition: type.hh:365
constexpr bool isHexahedron() const
Return true if entity is a hexahedron.
Definition: type.hh:324
constexpr bool isSimplex() const
Return true if entity is a simplex of any dimension.
Definition: type.hh:329
IdType Id
An integral id representing a GeometryType.
Definition: type.hh:191
A few common exception classes.
Traits for type conversions and type information.
GeometryType
Type representing VTK's entity geometry types.
Definition: common.hh:130
typename Impl::voider< Types... >::type void_t
Is void for all valid input types. The workhorse for C++11 SFINAE-techniques.
Definition: typetraits.hh:38
#define DUNE_UNUSED_PARAMETER(parm)
Definition: unused.hh:37
bool gt(const T &first, const T &second, typename EpsilonType< T >::Type epsilon)
test if first greater than second
Definition: float_cmp.cc:156
constexpr GeometryType line
GeometryType representing a line.
Definition: type.hh:511
constexpr GeometryType cube(unsigned int dim)
Returns a GeometryType representing a hypercube of dimension dim.
Definition: type.hh:471
constexpr GeometryType prism
GeometryType representing a 3D prism.
Definition: type.hh:541
constexpr GeometryType triangle
GeometryType representing a triangle.
Definition: type.hh:517
constexpr GeometryType quadrilateral
GeometryType representing a quadrilateral (a square).
Definition: type.hh:523
constexpr GeometryType hexahedron
GeometryType representing a hexahedron.
Definition: type.hh:547
constexpr GeometryType pyramid
GeometryType representing a 3D pyramid.
Definition: type.hh:535
constexpr GeometryType tetrahedron
GeometryType representing a tetrahedron.
Definition: type.hh:529
constexpr GeometryType none(unsigned int dim)
Returns a GeometryType representing a singular of dimension dim.
Definition: type.hh:480
constexpr GeometryType simplex(unsigned int dim)
Returns a GeometryType representing a simplex of dimension dim.
Definition: type.hh:462
constexpr GeometryType vertex
GeometryType representing a vertex.
Definition: type.hh:505
Definitions of several macros that conditionally make C++ syntax available.
constexpr GeometryType prismaticExtension(const GeometryType &gt)
Return GeometryType of a prismatic construction with gt as base
Definition: type.hh:492
constexpr GeometryType conicalExtension(const GeometryType &gt)
Return GeometryType of a conical construction with gt as base
Definition: type.hh:486
Dune namespace.
Definition: alignedallocator.hh:11
STL namespace.
Definition of the DUNE_UNUSED macro for the case that config.h is not available.
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden & Uni Heidelberg  |  generated with Hugo v0.111.3 (Mar 12, 23:46, 2026)