Dune Core Modules (2.9.0)

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// SPDX-FileCopyrightInfo: Copyright (C) DUNE Project contributors, see file LICENSE.md in module root
4// SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception
5#ifndef DUNE_GEOMETRY_TYPE_HH
6#define DUNE_GEOMETRY_TYPE_HH
7
12#include <cassert>
13
14#include <string>
15#include <type_traits>
16
20#include <dune/common/unused.hh>
21
22namespace Dune
23{
24
25 namespace Impl
26 {
27
28 enum TopologyConstruction { pyramidConstruction = 0, prismConstruction = 1 };
29
30 // Dynamic Topology Properties
31 // ---------------------------
32
41 inline static unsigned int numTopologies ( int dim ) noexcept
42 {
43 return (1u << dim);
44 }
45
57 inline bool static isPyramid ( unsigned int topologyId, int dim, int codim = 0 ) noexcept
58 {
59 assert( (dim > 0) && (topologyId < numTopologies( dim )) );
60 assert( (0 <= codim) && (codim < dim) );
61 return (((topologyId & ~1) & (1u << (dim-codim-1))) == 0);
62 }
63
75 inline static bool isPrism ( unsigned int topologyId, int dim, int codim = 0 ) noexcept
76 {
77 assert( (dim > 0) && (topologyId < numTopologies( dim )) );
78 assert( (0 <= codim) && (codim < dim) );
79 return (( (topologyId | 1) & (1u << (dim-codim-1))) != 0);
80 }
81
89 inline static unsigned int baseTopologyId ( unsigned int topologyId, int dim, int codim = 1 ) noexcept
90 {
91 assert( (dim >= 0) && (topologyId < numTopologies( dim )) );
92 assert( (0 <= codim) && (codim <= dim) );
93 return topologyId & ((1u << (dim-codim)) - 1);
94 }
95
96 } // namespace Impl
97
98// the Topology classes are deprecated and will be removed for the 2.8.
99// Temporarily a header 'deprecated_topology.hh' is provided which will be removed after the 2.9 release.
100#if __GNUC__ >= 7
101# pragma GCC diagnostic push
102# pragma GCC diagnostic ignored "-Wdeprecated-declarations"
103#endif
104#include <dune/geometry/deprecated_topology.hh>
105#if __GNUC__ >= 7
106# pragma GCC diagnostic pop
107#endif
108
109 // GeometryType
110 // -------------
111
125 {
126 public:
127
130 enum
131 BasicType {
137 none
138 };
139
140 private:
141
143 unsigned char dim_;
144
146 bool none_;
147
149 unsigned int topologyId_;
150
151 // Internal type used for the Id. The exact nature of this type is kept
152 // as an implementation detail on purpose. We use a scoped enum here because scoped enums
153 // can be used as template parameters, but are not implicitly converted to other integral
154 // types by the compiler. That way, we avoid unfortunate implicit conversion chains, e.g.
155 // people trying to work with GlobalGeometryTypeIndex, but forgetting to actually call
156 // GlobalGeometryTypeIndex::index(gt) and just using gt directly.
157 enum class IdType : std::uint64_t
158 {};
159
160 public:
161
192 using Id = IdType;
193
201 constexpr operator Id() const
202 {
203 // recreate the exact storage layout that this class is using, making conversion
204 // extremely cheap
205 std::uint64_t id = dim_ | (std::uint64_t(none_) << 8) | (std::uint64_t(topologyId_) << 32);
206 return static_cast<Id>(id);
207 }
208
221 constexpr Id toId() const
222 {
223 return static_cast<Id>(*this);
224 }
225
233 constexpr GeometryType(Id id)
234 : dim_(static_cast<std::uint64_t>(id) & 0xFF)
235 , none_(static_cast<std::uint64_t>(id) & 0x100)
236 , topologyId_(static_cast<std::uint64_t>(id) >> 32)
237 {}
238
241
243 constexpr GeometryType ()
244 : dim_(0), none_(true), topologyId_(0)
245 {}
246
253 constexpr GeometryType(unsigned int topologyId, unsigned int dim, bool isNone)
254 : dim_(dim), none_(isNone), topologyId_(topologyId)
255 {}
256
262 constexpr GeometryType(unsigned int topologyId, unsigned int dim)
263 : dim_(dim), none_(false), topologyId_(topologyId)
264 {}
265
276 template<class TopologyType,
277 class = std::void_t<decltype(TopologyType::dimension), decltype(TopologyType::id)>>
278 explicit GeometryType(TopologyType t)
279 : dim_(TopologyType::dimension), none_(false), topologyId_(TopologyType::id)
280 {
282 }
283
290 constexpr bool isVertex() const {
291 return dim_==0;
292 }
293
295 constexpr bool isLine() const {
296 return dim_==1;
297 }
298
300 constexpr bool isTriangle() const {
301 return ! none_ && dim_==2 && (topologyId_ | 1) == 0b0001;
302 }
303
305 constexpr bool isQuadrilateral() const {
306 return ! none_ && dim_==2 && (topologyId_ | 1) == 0b0011;
307 }
308
310 constexpr bool isTetrahedron() const {
311 return ! none_ && dim_==3 && (topologyId_ | 1) == 0b0001;
312 }
313
315 constexpr bool isPyramid() const {
316 return ! none_ && dim_==3 && (topologyId_ | 1) == 0b0011;
317 }
318
320 constexpr bool isPrism() const {
321 return ! none_ && dim_==3 && (topologyId_ | 1) == 0b0101;
322 }
323
325 constexpr bool isHexahedron() const {
326 return ! none_ && dim_==3 && (topologyId_ | 1) == 0b0111;
327 }
328
330 constexpr bool isSimplex() const {
331 return ! none_ && (topologyId_ | 1) == 1;
332 }
333
335 constexpr bool isCube() const {
336 return ! none_ && ((topologyId_ ^ ((1 << dim_)-1)) >> 1 == 0);
337 }
338
340 constexpr bool isConical() const {
341 return ! none_ && (((topologyId_ & ~1) & (1u << (dim_-1))) == 0);
342 }
343
348 constexpr bool isConical(const int& step) const {
349 return ! none_ && (((topologyId_ & ~1) & (1u << step)) == 0);
350 }
351
353 constexpr bool isPrismatic() const {
354 return ! none_ && (( (topologyId_ | 1) & (1u << (dim_-1))) != 0);
355 }
356
361 constexpr bool isPrismatic(const int& step) const {
362 return ! none_ && (( (topologyId_ | 1) & (1u << step)) != 0);
363 }
364
366 constexpr bool isNone() const {
367 return none_;
368 }
369
371 constexpr unsigned int dim() const {
372 return dim_;
373 }
374
376 constexpr unsigned int id() const {
377 return topologyId_;
378 }
379
387 constexpr bool operator==(const GeometryType& other) const {
388 return ( ( none_ == other.none_ )
389 && ( ( none_ == true )
390 || ( ( dim_ == other.dim_ )
391 && ( (topologyId_ >> 1) == (other.topologyId_ >> 1) )
392 )
393 )
394 );
395 }
396
398 constexpr bool operator!=(const GeometryType& other) const {
399 return ! ((*this)==other);
400 }
401
403 constexpr bool operator < (const GeometryType& other) const {
404 return ( ( none_ < other.none_ )
405 || ( !( other.none_ < none_ )
406 && ( ( dim_ < other.dim_ )
407 || ( (other.dim_ == dim_)
408 && ((topologyId_ >> 1) < (other.topologyId_ >> 1) )
409 )
410 )
411 )
412 );
413 }
414
417 };
418
420 inline std::ostream& operator<< (std::ostream& s, const GeometryType& a)
421 {
422 if (a.isSimplex())
423 {
424 s << "(simplex, " << a.dim() << ")";
425 return s;
426 }
427 if (a.isCube())
428 {
429 s << "(cube, " << a.dim() << ")";
430 return s;
431 }
432 if (a.isPyramid())
433 {
434 s << "(pyramid, 3)";
435 return s;
436 }
437 if (a.isPrism())
438 {
439 s << "(prism, 3)";
440 return s;
441 }
442 if (a.isNone())
443 {
444 s << "(none, " << a.dim() << ")";
445 return s;
446 }
447 s << "(other [" << a.id() << "], " << a.dim() << ")";
448 return s;
449 }
450
451
453
457 namespace GeometryTypes {
458
460
463 inline constexpr GeometryType simplex(unsigned int dim)
464 {
465 return GeometryType(0,dim,false);
466 }
467
469
472 inline constexpr GeometryType cube(unsigned int dim)
473 {
474 return GeometryType(((dim>1) ? ((1 << dim) - 1) : 0),dim,false);
475 }
476
478
481 inline constexpr GeometryType none(unsigned int dim)
482 {
483 return GeometryType(0,dim,true);
484 }
485
488 {
489 return GeometryType(gt.id(), gt.dim()+1, gt.isNone());
490 }
491
494 {
495 return GeometryType(gt.id() | ((1 << gt.dim())), gt.dim()+1, gt.isNone());
496 }
497
498#ifndef __cpp_inline_variables
499 namespace {
500#endif
501
503
506 DUNE_INLINE_VARIABLE constexpr GeometryType vertex = GeometryType(0,0,false);
507
509
512 DUNE_INLINE_VARIABLE constexpr GeometryType line = GeometryType(0,1,false);
513
515
518 DUNE_INLINE_VARIABLE constexpr GeometryType triangle = simplex(2);
519
521
524 DUNE_INLINE_VARIABLE constexpr GeometryType quadrilateral = cube(2);
525
527
530 DUNE_INLINE_VARIABLE constexpr GeometryType tetrahedron = simplex(3);
531
533
536 DUNE_INLINE_VARIABLE constexpr GeometryType pyramid = GeometryType(0b0011,3,false);
537
539
542 DUNE_INLINE_VARIABLE constexpr GeometryType prism = GeometryType(0b0101,3,false);
543
545
548 DUNE_INLINE_VARIABLE constexpr GeometryType hexahedron = cube(3);
549
550#ifndef __cpp_inline_variables
551 }
552#endif
553
554 }
555
556 namespace Impl
557 {
558
560 inline constexpr GeometryType getBase(const GeometryType& gt) {
561 return GeometryType(gt.id() & ((1 << (gt.dim()-1))-1), gt.dim()-1, gt.isNone());
562 }
563
564
565 // IfGeometryType
566 // ----------
567
568 template< template< GeometryType::Id > class Operation, int dim, GeometryType::Id geometryId = GeometryTypes::vertex >
569 struct IfGeometryType
570 {
571 static constexpr GeometryType geometry = geometryId;
572 template< class... Args >
573 static auto apply ( GeometryType gt, Args &&... args )
574 {
575 GeometryType lowerGeometry(gt.id() >>1 , gt.dim()-1, gt.isNone());
576
577 if( gt.id() & 1 )
578 return IfGeometryType< Operation, dim-1, GeometryTypes::prismaticExtension(geometry).toId() >::apply( lowerGeometry, std::forward< Args >( args )... );
579 else
580 return IfGeometryType< Operation, dim-1, GeometryTypes::conicalExtension(geometry).toId() >::apply( lowerGeometry, std::forward< Args >( args )... );
581 }
582 };
583
584 template< template< GeometryType::Id > class Operation, GeometryType::Id geometryId >
585 struct IfGeometryType< Operation, 0, geometryId>
586 {
587 template< class... Args >
588 static auto apply ([[maybe_unused]] GeometryType gt, Args &&... args )
589 {
590 return Operation< geometryId >::apply( std::forward< Args >( args )... );
591 }
592 };
593 } // namespace Impl
594} // namespace Dune
595
596#endif // DUNE_GEOMETRY_TYPE_HH
Unique label for each type of entities that can occur in DUNE grids.
Definition: type.hh:125
constexpr GeometryType(unsigned int topologyId, unsigned int dim)
Constructor, using the topologyId (integer) and the dimension.
Definition: type.hh:262
constexpr bool operator<(const GeometryType &other) const
less-than operation for use with maps
Definition: type.hh:403
constexpr bool operator!=(const GeometryType &other) const
Check for inequality.
Definition: type.hh:398
constexpr bool isPyramid() const
Return true if entity is a pyramid.
Definition: type.hh:315
constexpr bool isTetrahedron() const
Return true if entity is a tetrahedron.
Definition: type.hh:310
constexpr bool isPrism() const
Return true if entity is a prism.
Definition: type.hh:320
constexpr bool isVertex() const
Return true if entity is a vertex.
Definition: type.hh:290
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:387
constexpr Id toId() const
Create an Id representation of this GeometryType.
Definition: type.hh:221
constexpr bool isConical(const int &step) const
Return true if entity was constructed with a conical product in the chosen step.
Definition: type.hh:348
constexpr unsigned int dim() const
Return dimension of the type.
Definition: type.hh:371
constexpr bool isPrismatic(const int &step) const
Return true if entity was constructed with a prismatic product in the chosen step.
Definition: type.hh:361
constexpr bool isTriangle() const
Return true if entity is a triangle.
Definition: type.hh:300
GeometryType(TopologyType t)
Constructor from static TopologyType class.
Definition: type.hh:278
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:253
BasicType
Each entity can be tagged by one of these basic types plus its space dimension.
Definition: type.hh:131
@ cube
Cube element in any nonnegative dimension.
Definition: type.hh:133
@ simplex
Simplicial element in any nonnegative dimension.
Definition: type.hh:132
@ pyramid
Four sided pyramid in three dimensions.
Definition: type.hh:134
@ extended
Other, more general topology, representable as topologyId.
Definition: type.hh:136
@ none
Even more general topology, cannot be specified by a topologyId. Two GeometryTypes with 'none' type a...
Definition: type.hh:137
@ prism
Prism element in three dimensions.
Definition: type.hh:135
constexpr GeometryType(Id id)
Reconstruct a Geometry type from a GeometryType::Id.
Definition: type.hh:233
constexpr bool isCube() const
Return true if entity is a cube of any dimension.
Definition: type.hh:335
constexpr GeometryType()
Default constructor, not initializing anything.
Definition: type.hh:243
constexpr bool isConical() const
Return true if entity was constructed with a conical product in the last step.
Definition: type.hh:340
constexpr bool isLine() const
Return true if entity is a line segment.
Definition: type.hh:295
constexpr bool isQuadrilateral() const
Return true if entity is a quadrilateral.
Definition: type.hh:305
constexpr bool isPrismatic() const
Return true if entity was constructed with a prismatic product in the last step.
Definition: type.hh:353
constexpr unsigned int id() const
Return the topology id of the type.
Definition: type.hh:376
constexpr bool isNone() const
Return true if entity is a singular of any dimension.
Definition: type.hh:366
constexpr bool isHexahedron() const
Return true if entity is a hexahedron.
Definition: type.hh:325
constexpr bool isSimplex() const
Return true if entity is a simplex of any dimension.
Definition: type.hh:330
IdType Id
An integral id representing a GeometryType.
Definition: type.hh:192
A few common exception classes.
Traits for type conversions and type information.
GeometryType
Type representing VTK's entity geometry types.
Definition: common.hh:132
typename Impl::voider< Types... >::type void_t
Is void for all valid input types. The workhorse for C++11 SFINAE-techniques.
Definition: typetraits.hh:40
#define DUNE_UNUSED_PARAMETER(param)
Definition: unused.hh:21
bool gt(const T &first, const T &second, typename EpsilonType< T >::Type epsilon)
test if first greater than second
Definition: float_cmp.cc:158
constexpr GeometryType line
GeometryType representing a line.
Definition: type.hh:512
constexpr GeometryType cube(unsigned int dim)
Returns a GeometryType representing a hypercube of dimension dim.
Definition: type.hh:472
constexpr GeometryType prism
GeometryType representing a 3D prism.
Definition: type.hh:542
constexpr GeometryType triangle
GeometryType representing a triangle.
Definition: type.hh:518
constexpr GeometryType quadrilateral
GeometryType representing a quadrilateral (a square).
Definition: type.hh:524
constexpr GeometryType hexahedron
GeometryType representing a hexahedron.
Definition: type.hh:548
constexpr GeometryType pyramid
GeometryType representing a 3D pyramid.
Definition: type.hh:536
constexpr GeometryType tetrahedron
GeometryType representing a tetrahedron.
Definition: type.hh:530
constexpr GeometryType none(unsigned int dim)
Returns a GeometryType representing a singular of dimension dim.
Definition: type.hh:481
constexpr GeometryType simplex(unsigned int dim)
Returns a GeometryType representing a simplex of dimension dim.
Definition: type.hh:463
constexpr GeometryType vertex
GeometryType representing a vertex.
Definition: type.hh:506
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:493
constexpr GeometryType conicalExtension(const GeometryType &gt)
Return GeometryType of a conical construction with gt as base
Definition: type.hh:487
Dune namespace.
Definition: alignedallocator.hh:13
STL namespace.
Definition of the DUNE_UNUSED_PARAMETER macro.
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Jul 15, 22:36, 2024)