Dune Core Modules (2.9.1)

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#include <cstdint>
14
15#include <string>
16#include <type_traits>
17
21#include <dune/common/unused.hh>
22
23namespace Dune
24{
25
26 namespace Impl
27 {
28
29 enum TopologyConstruction { pyramidConstruction = 0, prismConstruction = 1 };
30
31 // Dynamic Topology Properties
32 // ---------------------------
33
42 inline static unsigned int numTopologies ( int dim ) noexcept
43 {
44 return (1u << dim);
45 }
46
58 inline bool static isPyramid ( unsigned int topologyId, int dim, int codim = 0 ) noexcept
59 {
60 assert( (dim > 0) && (topologyId < numTopologies( dim )) );
61 assert( (0 <= codim) && (codim < dim) );
62 return (((topologyId & ~1) & (1u << (dim-codim-1))) == 0);
63 }
64
76 inline static bool isPrism ( unsigned int topologyId, int dim, int codim = 0 ) noexcept
77 {
78 assert( (dim > 0) && (topologyId < numTopologies( dim )) );
79 assert( (0 <= codim) && (codim < dim) );
80 return (( (topologyId | 1) & (1u << (dim-codim-1))) != 0);
81 }
82
90 inline static unsigned int baseTopologyId ( unsigned int topologyId, int dim, int codim = 1 ) noexcept
91 {
92 assert( (dim >= 0) && (topologyId < numTopologies( dim )) );
93 assert( (0 <= codim) && (codim <= dim) );
94 return topologyId & ((1u << (dim-codim)) - 1);
95 }
96
97 } // namespace Impl
98
99// the Topology classes are deprecated and will be removed for the 2.8.
100// Temporarily a header 'deprecated_topology.hh' is provided which will be removed after the 2.9 release.
101#if __GNUC__ >= 7
102# pragma GCC diagnostic push
103# pragma GCC diagnostic ignored "-Wdeprecated-declarations"
104#endif
105#include <dune/geometry/deprecated_topology.hh>
106#if __GNUC__ >= 7
107# pragma GCC diagnostic pop
108#endif
109
110 // GeometryType
111 // -------------
112
126 {
127 public:
128
131 enum
132 BasicType {
138 none
139 };
140
141 private:
142
144 unsigned char dim_;
145
147 bool none_;
148
150 unsigned int topologyId_;
151
152 // Internal type used for the Id. The exact nature of this type is kept
153 // as an implementation detail on purpose. We use a scoped enum here because scoped enums
154 // can be used as template parameters, but are not implicitly converted to other integral
155 // types by the compiler. That way, we avoid unfortunate implicit conversion chains, e.g.
156 // people trying to work with GlobalGeometryTypeIndex, but forgetting to actually call
157 // GlobalGeometryTypeIndex::index(gt) and just using gt directly.
158 enum class IdType : std::uint64_t
159 {};
160
161 public:
162
193 using Id = IdType;
194
202 constexpr operator Id() const
203 {
204 // recreate the exact storage layout that this class is using, making conversion
205 // extremely cheap
206 std::uint64_t id = dim_ | (std::uint64_t(none_) << 8) | (std::uint64_t(topologyId_) << 32);
207 return static_cast<Id>(id);
208 }
209
222 constexpr Id toId() const
223 {
224 return static_cast<Id>(*this);
225 }
226
234 constexpr GeometryType(Id id)
235 : dim_(static_cast<std::uint64_t>(id) & 0xFF)
236 , none_(static_cast<std::uint64_t>(id) & 0x100)
237 , topologyId_(static_cast<std::uint64_t>(id) >> 32)
238 {}
239
242
244 constexpr GeometryType ()
245 : dim_(0), none_(true), topologyId_(0)
246 {}
247
254 constexpr GeometryType(unsigned int topologyId, unsigned int dim, bool isNone)
255 : dim_(dim), none_(isNone), topologyId_(topologyId)
256 {}
257
263 constexpr GeometryType(unsigned int topologyId, unsigned int dim)
264 : dim_(dim), none_(false), topologyId_(topologyId)
265 {}
266
277 template<class TopologyType,
278 class = std::void_t<decltype(TopologyType::dimension), decltype(TopologyType::id)>>
279 explicit GeometryType(TopologyType t)
280 : dim_(TopologyType::dimension), none_(false), topologyId_(TopologyType::id)
281 {
283 }
284
291 constexpr bool isVertex() const {
292 return dim_==0;
293 }
294
296 constexpr bool isLine() const {
297 return dim_==1;
298 }
299
301 constexpr bool isTriangle() const {
302 return ! none_ && dim_==2 && (topologyId_ | 1) == 0b0001;
303 }
304
306 constexpr bool isQuadrilateral() const {
307 return ! none_ && dim_==2 && (topologyId_ | 1) == 0b0011;
308 }
309
311 constexpr bool isTetrahedron() const {
312 return ! none_ && dim_==3 && (topologyId_ | 1) == 0b0001;
313 }
314
316 constexpr bool isPyramid() const {
317 return ! none_ && dim_==3 && (topologyId_ | 1) == 0b0011;
318 }
319
321 constexpr bool isPrism() const {
322 return ! none_ && dim_==3 && (topologyId_ | 1) == 0b0101;
323 }
324
326 constexpr bool isHexahedron() const {
327 return ! none_ && dim_==3 && (topologyId_ | 1) == 0b0111;
328 }
329
331 constexpr bool isSimplex() const {
332 return ! none_ && (topologyId_ | 1) == 1;
333 }
334
336 constexpr bool isCube() const {
337 return ! none_ && ((topologyId_ ^ ((1 << dim_)-1)) >> 1 == 0);
338 }
339
341 constexpr bool isConical() const {
342 return ! none_ && (((topologyId_ & ~1) & (1u << (dim_-1))) == 0);
343 }
344
349 constexpr bool isConical(const int& step) const {
350 return ! none_ && (((topologyId_ & ~1) & (1u << step)) == 0);
351 }
352
354 constexpr bool isPrismatic() const {
355 return ! none_ && (( (topologyId_ | 1) & (1u << (dim_-1))) != 0);
356 }
357
362 constexpr bool isPrismatic(const int& step) const {
363 return ! none_ && (( (topologyId_ | 1) & (1u << step)) != 0);
364 }
365
367 constexpr bool isNone() const {
368 return none_;
369 }
370
372 constexpr unsigned int dim() const {
373 return dim_;
374 }
375
377 constexpr unsigned int id() const {
378 return topologyId_;
379 }
380
388 constexpr bool operator==(const GeometryType& other) const {
389 return ( ( none_ == other.none_ )
390 && ( ( none_ == true )
391 || ( ( dim_ == other.dim_ )
392 && ( (topologyId_ >> 1) == (other.topologyId_ >> 1) )
393 )
394 )
395 );
396 }
397
399 constexpr bool operator!=(const GeometryType& other) const {
400 return ! ((*this)==other);
401 }
402
404 constexpr bool operator < (const GeometryType& other) const {
405 return ( ( none_ < other.none_ )
406 || ( !( other.none_ < none_ )
407 && ( ( dim_ < other.dim_ )
408 || ( (other.dim_ == dim_)
409 && ((topologyId_ >> 1) < (other.topologyId_ >> 1) )
410 )
411 )
412 )
413 );
414 }
415
418 };
419
421 inline std::ostream& operator<< (std::ostream& s, const GeometryType& a)
422 {
423 if (a.isSimplex())
424 {
425 s << "(simplex, " << a.dim() << ")";
426 return s;
427 }
428 if (a.isCube())
429 {
430 s << "(cube, " << a.dim() << ")";
431 return s;
432 }
433 if (a.isPyramid())
434 {
435 s << "(pyramid, 3)";
436 return s;
437 }
438 if (a.isPrism())
439 {
440 s << "(prism, 3)";
441 return s;
442 }
443 if (a.isNone())
444 {
445 s << "(none, " << a.dim() << ")";
446 return s;
447 }
448 s << "(other [" << a.id() << "], " << a.dim() << ")";
449 return s;
450 }
451
452
454
458 namespace GeometryTypes {
459
461
464 inline constexpr GeometryType simplex(unsigned int dim)
465 {
466 return GeometryType(0,dim,false);
467 }
468
470
473 inline constexpr GeometryType cube(unsigned int dim)
474 {
475 return GeometryType(((dim>1) ? ((1 << dim) - 1) : 0),dim,false);
476 }
477
479
482 inline constexpr GeometryType none(unsigned int dim)
483 {
484 return GeometryType(0,dim,true);
485 }
486
489 {
490 return GeometryType(gt.id(), gt.dim()+1, gt.isNone());
491 }
492
495 {
496 return GeometryType(gt.id() | ((1 << gt.dim())), gt.dim()+1, gt.isNone());
497 }
498
499#ifndef __cpp_inline_variables
500 namespace {
501#endif
502
504
507 DUNE_INLINE_VARIABLE constexpr GeometryType vertex = GeometryType(0,0,false);
508
510
513 DUNE_INLINE_VARIABLE constexpr GeometryType line = GeometryType(0,1,false);
514
516
519 DUNE_INLINE_VARIABLE constexpr GeometryType triangle = simplex(2);
520
522
525 DUNE_INLINE_VARIABLE constexpr GeometryType quadrilateral = cube(2);
526
528
531 DUNE_INLINE_VARIABLE constexpr GeometryType tetrahedron = simplex(3);
532
534
537 DUNE_INLINE_VARIABLE constexpr GeometryType pyramid = GeometryType(0b0011,3,false);
538
540
543 DUNE_INLINE_VARIABLE constexpr GeometryType prism = GeometryType(0b0101,3,false);
544
546
549 DUNE_INLINE_VARIABLE constexpr GeometryType hexahedron = cube(3);
550
551#ifndef __cpp_inline_variables
552 }
553#endif
554
555 }
556
557 namespace Impl
558 {
559
561 inline constexpr GeometryType getBase(const GeometryType& gt) {
562 return GeometryType(gt.id() & ((1 << (gt.dim()-1))-1), gt.dim()-1, gt.isNone());
563 }
564
565
566 // IfGeometryType
567 // ----------
568
569 template< template< GeometryType::Id > class Operation, int dim, GeometryType::Id geometryId = GeometryTypes::vertex >
570 struct IfGeometryType
571 {
572 static constexpr GeometryType geometry = geometryId;
573 template< class... Args >
574 static auto apply ( GeometryType gt, Args &&... args )
575 {
576 GeometryType lowerGeometry(gt.id() >>1 , gt.dim()-1, gt.isNone());
577
578 if( gt.id() & 1 )
579 return IfGeometryType< Operation, dim-1, GeometryTypes::prismaticExtension(geometry).toId() >::apply( lowerGeometry, std::forward< Args >( args )... );
580 else
581 return IfGeometryType< Operation, dim-1, GeometryTypes::conicalExtension(geometry).toId() >::apply( lowerGeometry, std::forward< Args >( args )... );
582 }
583 };
584
585 template< template< GeometryType::Id > class Operation, GeometryType::Id geometryId >
586 struct IfGeometryType< Operation, 0, geometryId>
587 {
588 template< class... Args >
589 static auto apply ([[maybe_unused]] GeometryType gt, Args &&... args )
590 {
591 return Operation< geometryId >::apply( std::forward< Args >( args )... );
592 }
593 };
594 } // namespace Impl
595} // namespace Dune
596
597#endif // DUNE_GEOMETRY_TYPE_HH
Unique label for each type of entities that can occur in DUNE grids.
Definition: type.hh:126
constexpr GeometryType(unsigned int topologyId, unsigned int dim)
Constructor, using the topologyId (integer) and the dimension.
Definition: type.hh:263
constexpr bool operator<(const GeometryType &other) const
less-than operation for use with maps
Definition: type.hh:404
constexpr bool operator!=(const GeometryType &other) const
Check for inequality.
Definition: type.hh:399
constexpr bool isPyramid() const
Return true if entity is a pyramid.
Definition: type.hh:316
constexpr bool isTetrahedron() const
Return true if entity is a tetrahedron.
Definition: type.hh:311
constexpr bool isPrism() const
Return true if entity is a prism.
Definition: type.hh:321
constexpr bool isVertex() const
Return true if entity is a vertex.
Definition: type.hh:291
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:388
constexpr Id toId() const
Create an Id representation of this GeometryType.
Definition: type.hh:222
constexpr bool isConical(const int &step) const
Return true if entity was constructed with a conical product in the chosen step.
Definition: type.hh:349
constexpr unsigned int dim() const
Return dimension of the type.
Definition: type.hh:372
constexpr bool isPrismatic(const int &step) const
Return true if entity was constructed with a prismatic product in the chosen step.
Definition: type.hh:362
constexpr bool isTriangle() const
Return true if entity is a triangle.
Definition: type.hh:301
GeometryType(TopologyType t)
Constructor from static TopologyType class.
Definition: type.hh:279
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:254
BasicType
Each entity can be tagged by one of these basic types plus its space dimension.
Definition: type.hh:132
@ cube
Cube element in any nonnegative dimension.
Definition: type.hh:134
@ simplex
Simplicial element in any nonnegative dimension.
Definition: type.hh:133
@ pyramid
Four sided pyramid in three dimensions.
Definition: type.hh:135
@ extended
Other, more general topology, representable as topologyId.
Definition: type.hh:137
@ none
Even more general topology, cannot be specified by a topologyId. Two GeometryTypes with 'none' type a...
Definition: type.hh:138
@ prism
Prism element in three dimensions.
Definition: type.hh:136
constexpr GeometryType(Id id)
Reconstruct a Geometry type from a GeometryType::Id.
Definition: type.hh:234
constexpr bool isCube() const
Return true if entity is a cube of any dimension.
Definition: type.hh:336
constexpr GeometryType()
Default constructor, not initializing anything.
Definition: type.hh:244
constexpr bool isConical() const
Return true if entity was constructed with a conical product in the last step.
Definition: type.hh:341
constexpr bool isLine() const
Return true if entity is a line segment.
Definition: type.hh:296
constexpr bool isQuadrilateral() const
Return true if entity is a quadrilateral.
Definition: type.hh:306
constexpr bool isPrismatic() const
Return true if entity was constructed with a prismatic product in the last step.
Definition: type.hh:354
constexpr unsigned int id() const
Return the topology id of the type.
Definition: type.hh:377
constexpr bool isNone() const
Return true if entity is a singular of any dimension.
Definition: type.hh:367
constexpr bool isHexahedron() const
Return true if entity is a hexahedron.
Definition: type.hh:326
constexpr bool isSimplex() const
Return true if entity is a simplex of any dimension.
Definition: type.hh:331
IdType Id
An integral id representing a GeometryType.
Definition: type.hh:193
A few common exception classes.
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:513
constexpr GeometryType cube(unsigned int dim)
Returns a GeometryType representing a hypercube of dimension dim.
Definition: type.hh:473
constexpr GeometryType prism
GeometryType representing a 3D prism.
Definition: type.hh:543
constexpr GeometryType triangle
GeometryType representing a triangle.
Definition: type.hh:519
constexpr GeometryType quadrilateral
GeometryType representing a quadrilateral (a square).
Definition: type.hh:525
constexpr GeometryType hexahedron
GeometryType representing a hexahedron.
Definition: type.hh:549
constexpr GeometryType pyramid
GeometryType representing a 3D pyramid.
Definition: type.hh:537
constexpr GeometryType tetrahedron
GeometryType representing a tetrahedron.
Definition: type.hh:531
constexpr GeometryType none(unsigned int dim)
Returns a GeometryType representing a singular of dimension dim.
Definition: type.hh:482
constexpr GeometryType simplex(unsigned int dim)
Returns a GeometryType representing a simplex of dimension dim.
Definition: type.hh:464
constexpr GeometryType vertex
GeometryType representing a vertex.
Definition: type.hh:507
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:494
constexpr GeometryType conicalExtension(const GeometryType &gt)
Return GeometryType of a conical construction with gt as base
Definition: type.hh:488
Dune namespace.
Definition: alignedallocator.hh:13
STL namespace.
Traits for type conversions and type information.
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)