Dune Core Modules (unstable)

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 © 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
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 // GeometryType
99 // -------------
100
114 {
115 public:
116
119 enum
120 BasicType {
126 none
127 };
128
129 private:
130
132 unsigned char dim_;
133
135 bool none_;
136
138 unsigned int topologyId_;
139
140 // Internal type used for the Id. The exact nature of this type is kept
141 // as an implementation detail on purpose. We use a scoped enum here because scoped enums
142 // can be used as template parameters, but are not implicitly converted to other integral
143 // types by the compiler. That way, we avoid unfortunate implicit conversion chains, e.g.
144 // people trying to work with GlobalGeometryTypeIndex, but forgetting to actually call
145 // GlobalGeometryTypeIndex::index(gt) and just using gt directly.
146 enum class IdType : std::uint64_t
147 {};
148
149 public:
150
181 using Id = IdType;
182
190 constexpr operator Id() const
191 {
192 // recreate the exact storage layout that this class is using, making conversion
193 // extremely cheap
194 std::uint64_t id = dim_ | (std::uint64_t(none_) << 8) | (std::uint64_t(topologyId_) << 32);
195 return static_cast<Id>(id);
196 }
197
210 constexpr Id toId() const
211 {
212 return static_cast<Id>(*this);
213 }
214
222 constexpr GeometryType(Id id)
223 : dim_(static_cast<std::uint64_t>(id) & 0xFF)
224 , none_(static_cast<std::uint64_t>(id) & 0x100)
225 , topologyId_(static_cast<std::uint64_t>(id) >> 32)
226 {}
227
230
232 constexpr GeometryType ()
233 : dim_(0), none_(true), topologyId_(0)
234 {}
235
242 constexpr GeometryType(unsigned int topologyId, unsigned int dim, bool isNone)
243 : dim_(dim), none_(isNone), topologyId_(topologyId)
244 {}
245
251 constexpr GeometryType(unsigned int topologyId, unsigned int dim)
252 : dim_(dim), none_(false), topologyId_(topologyId)
253 {}
254
265 template<class TopologyType,
266 class = std::void_t<decltype(TopologyType::dimension), decltype(TopologyType::id)>>
267 explicit GeometryType(TopologyType t)
268 : dim_(TopologyType::dimension), none_(false), topologyId_(TopologyType::id)
269 {
271 }
272
279 constexpr bool isVertex() const {
280 return dim_==0;
281 }
282
284 constexpr bool isLine() const {
285 return dim_==1;
286 }
287
289 constexpr bool isTriangle() const {
290 return ! none_ && dim_==2 && (topologyId_ | 1) == 0b0001;
291 }
292
294 constexpr bool isQuadrilateral() const {
295 return ! none_ && dim_==2 && (topologyId_ | 1) == 0b0011;
296 }
297
299 constexpr bool isTetrahedron() const {
300 return ! none_ && dim_==3 && (topologyId_ | 1) == 0b0001;
301 }
302
304 constexpr bool isPyramid() const {
305 return ! none_ && dim_==3 && (topologyId_ | 1) == 0b0011;
306 }
307
309 constexpr bool isPrism() const {
310 return ! none_ && dim_==3 && (topologyId_ | 1) == 0b0101;
311 }
312
314 constexpr bool isHexahedron() const {
315 return ! none_ && dim_==3 && (topologyId_ | 1) == 0b0111;
316 }
317
319 constexpr bool isSimplex() const {
320 return ! none_ && (topologyId_ | 1) == 1;
321 }
322
324 constexpr bool isCube() const {
325 return ! none_ && ((topologyId_ ^ ((1 << dim_)-1)) >> 1 == 0);
326 }
327
329 constexpr bool isConical() const {
330 return ! none_ && (((topologyId_ & ~1) & (1u << (dim_-1))) == 0);
331 }
332
337 constexpr bool isConical(const int& step) const {
338 return ! none_ && (((topologyId_ & ~1) & (1u << step)) == 0);
339 }
340
342 constexpr bool isPrismatic() const {
343 return ! none_ && (( (topologyId_ | 1) & (1u << (dim_-1))) != 0);
344 }
345
350 constexpr bool isPrismatic(const int& step) const {
351 return ! none_ && (( (topologyId_ | 1) & (1u << step)) != 0);
352 }
353
355 constexpr bool isNone() const {
356 return none_;
357 }
358
360 constexpr unsigned int dim() const {
361 return dim_;
362 }
363
365 constexpr unsigned int id() const {
366 return topologyId_;
367 }
368
373
377 constexpr bool operator==(const GeometryType& other) const {
378 return ( ( none_ == other.none_ )
379 && ( ( none_ == true )
380 || ( ( dim_ == other.dim_ )
381 && ( (topologyId_ >> 1) == (other.topologyId_ >> 1) )
382 )
383 )
384 );
385 }
386
388 constexpr bool operator!=(const GeometryType& other) const {
389 return ! ((*this)==other);
390 }
391
393 constexpr bool operator < (const GeometryType& other) const {
394 return ( ( none_ < other.none_ )
395 || ( !( other.none_ < none_ )
396 && ( ( dim_ < other.dim_ )
397 || ( (other.dim_ == dim_)
398 && ((topologyId_ >> 1) < (other.topologyId_ >> 1) )
399 )
400 )
401 )
402 );
403 }
404
407 };
408
410 inline std::ostream& operator<< (std::ostream& s, const GeometryType& a)
411 {
412 if (a.isSimplex())
413 {
414 s << "(simplex, " << a.dim() << ")";
415 return s;
416 }
417 if (a.isCube())
418 {
419 s << "(cube, " << a.dim() << ")";
420 return s;
421 }
422 if (a.isPyramid())
423 {
424 s << "(pyramid, 3)";
425 return s;
426 }
427 if (a.isPrism())
428 {
429 s << "(prism, 3)";
430 return s;
431 }
432 if (a.isNone())
433 {
434 s << "(none, " << a.dim() << ")";
435 return s;
436 }
437 s << "(other [" << a.id() << "], " << a.dim() << ")";
438 return s;
439 }
440
441
443
447 namespace GeometryTypes {
448
450
453 inline constexpr GeometryType simplex(unsigned int dim)
454 {
455 return GeometryType(0,dim,false);
456 }
457
459
462 inline constexpr GeometryType cube(unsigned int dim)
463 {
464 return GeometryType(((dim>1) ? ((1 << dim) - 1) : 0),dim,false);
465 }
466
468
471 inline constexpr GeometryType none(unsigned int dim)
472 {
473 return GeometryType(0,dim,true);
474 }
475
478 {
479 return GeometryType(gt.id(), gt.dim()+1, gt.isNone());
480 }
481
484 {
485 return GeometryType(gt.id() | ((1 << gt.dim())), gt.dim()+1, gt.isNone());
486 }
487
489
492 inline constexpr GeometryType vertex = GeometryType(0,0,false);
493
495
498 inline constexpr GeometryType line = GeometryType(0,1,false);
499
501
504 inline constexpr GeometryType triangle = simplex(2);
505
507
510 inline constexpr GeometryType quadrilateral = cube(2);
511
513
516 inline constexpr GeometryType tetrahedron = simplex(3);
517
519
522 inline constexpr GeometryType pyramid = GeometryType(0b0011,3,false);
523
525
528 inline constexpr GeometryType prism = GeometryType(0b0101,3,false);
529
531
534 inline constexpr GeometryType hexahedron = cube(3);
535
536 }
537
538 namespace Impl
539 {
540
542 inline constexpr GeometryType getBase(const GeometryType& gt) {
543 return GeometryType(gt.id() & ((1 << (gt.dim()-1))-1), gt.dim()-1, gt.isNone());
544 }
545
546
547 // IfGeometryType
548 // ----------
549
550 template< template< GeometryType::Id > class Operation, int dim, GeometryType::Id geometryId = GeometryTypes::vertex >
551 struct IfGeometryType
552 {
553 static constexpr GeometryType geometry = geometryId;
554 template< class... Args >
555 static auto apply ( GeometryType gt, Args &&... args )
556 {
557 GeometryType lowerGeometry(gt.id() >>1 , gt.dim()-1, gt.isNone());
558
559 if( gt.id() & 1 )
560 return IfGeometryType< Operation, dim-1, GeometryTypes::prismaticExtension(geometry).toId() >::apply( lowerGeometry, std::forward< Args >( args )... );
561 else
562 return IfGeometryType< Operation, dim-1, GeometryTypes::conicalExtension(geometry).toId() >::apply( lowerGeometry, std::forward< Args >( args )... );
563 }
564 };
565
566 template< template< GeometryType::Id > class Operation, GeometryType::Id geometryId >
567 struct IfGeometryType< Operation, 0, geometryId>
568 {
569 template< class... Args >
570 static auto apply ([[maybe_unused]] GeometryType gt, Args &&... args )
571 {
572 return Operation< geometryId >::apply( std::forward< Args >( args )... );
573 }
574 };
575 } // namespace Impl
576} // namespace Dune
577
578#endif // DUNE_GEOMETRY_TYPE_HH
Unique label for each type of entities that can occur in DUNE grids.
Definition: type.hh:114
constexpr GeometryType(unsigned int topologyId, unsigned int dim)
Constructor, using the topologyId (integer) and the dimension.
Definition: type.hh:251
constexpr bool operator<(const GeometryType &other) const
less-than operation for use with maps
Definition: type.hh:393
constexpr bool operator!=(const GeometryType &other) const
Check for inequality.
Definition: type.hh:388
constexpr bool isPyramid() const
Return true if entity is a pyramid.
Definition: type.hh:304
constexpr bool isTetrahedron() const
Return true if entity is a tetrahedron.
Definition: type.hh:299
constexpr bool isPrism() const
Return true if entity is a prism.
Definition: type.hh:309
constexpr bool isVertex() const
Return true if entity is a vertex.
Definition: type.hh:279
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:377
constexpr Id toId() const
Create an Id representation of this GeometryType.
Definition: type.hh:210
constexpr bool isConical(const int &step) const
Return true if entity was constructed with a conical product in the chosen step.
Definition: type.hh:337
constexpr unsigned int dim() const
Return dimension of the type.
Definition: type.hh:360
constexpr bool isPrismatic(const int &step) const
Return true if entity was constructed with a prismatic product in the chosen step.
Definition: type.hh:350
constexpr bool isTriangle() const
Return true if entity is a triangle.
Definition: type.hh:289
GeometryType(TopologyType t)
Constructor from static TopologyType class.
Definition: type.hh:267
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:242
BasicType
Each entity can be tagged by one of these basic types plus its space dimension.
Definition: type.hh:120
@ cube
Cube element in any nonnegative dimension.
Definition: type.hh:122
@ simplex
Simplicial element in any nonnegative dimension.
Definition: type.hh:121
@ pyramid
Four sided pyramid in three dimensions.
Definition: type.hh:123
@ extended
Other, more general topology, representable as topologyId.
Definition: type.hh:125
@ none
Even more general topology, cannot be specified by a topologyId. Two GeometryTypes with 'none' type a...
Definition: type.hh:126
@ prism
Prism element in three dimensions.
Definition: type.hh:124
constexpr GeometryType(Id id)
Reconstruct a Geometry type from a GeometryType::Id.
Definition: type.hh:222
constexpr bool isCube() const
Return true if entity is a cube of any dimension.
Definition: type.hh:324
constexpr GeometryType()
Default constructor, not initializing anything.
Definition: type.hh:232
constexpr bool isConical() const
Return true if entity was constructed with a conical product in the last step.
Definition: type.hh:329
constexpr bool isLine() const
Return true if entity is a line segment.
Definition: type.hh:284
constexpr bool isQuadrilateral() const
Return true if entity is a quadrilateral.
Definition: type.hh:294
constexpr bool isPrismatic() const
Return true if entity was constructed with a prismatic product in the last step.
Definition: type.hh:342
constexpr unsigned int id() const
Return the topology id of the type.
Definition: type.hh:365
constexpr bool isNone() const
Return true if entity is a singular of any dimension.
Definition: type.hh:355
constexpr bool isHexahedron() const
Return true if entity is a hexahedron.
Definition: type.hh:314
constexpr bool isSimplex() const
Return true if entity is a simplex of any dimension.
Definition: type.hh:319
IdType Id
An integral id representing a GeometryType.
Definition: type.hh:181
A few common exception classes.
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:498
constexpr GeometryType cube(unsigned int dim)
Returns a GeometryType representing a hypercube of dimension dim.
Definition: type.hh:462
constexpr GeometryType prism
GeometryType representing a 3D prism.
Definition: type.hh:528
constexpr GeometryType triangle
GeometryType representing a triangle.
Definition: type.hh:504
constexpr GeometryType quadrilateral
GeometryType representing a quadrilateral (a square).
Definition: type.hh:510
constexpr GeometryType hexahedron
GeometryType representing a hexahedron.
Definition: type.hh:534
constexpr GeometryType pyramid
GeometryType representing a 3D pyramid.
Definition: type.hh:522
constexpr GeometryType tetrahedron
GeometryType representing a tetrahedron.
Definition: type.hh:516
constexpr GeometryType none(unsigned int dim)
Returns a GeometryType representing a singular of dimension dim.
Definition: type.hh:471
constexpr GeometryType simplex(unsigned int dim)
Returns a GeometryType representing a simplex of dimension dim.
Definition: type.hh:453
constexpr GeometryType vertex
GeometryType representing a vertex.
Definition: type.hh:492
constexpr GeometryType prismaticExtension(const GeometryType &gt)
Return GeometryType of a prismatic construction with gt as base
Definition: type.hh:483
constexpr GeometryType conicalExtension(const GeometryType &gt)
Return GeometryType of a conical construction with gt as base
Definition: type.hh:477
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 (Nov 21, 23:30, 2024)