dune-geometry 2.12-git
Loading...
Searching...
No Matches
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
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 // GeometryType
98 // -------------
99
113 {
114 public:
115
127
128 private:
129
131 unsigned char dim_;
132
134 bool none_;
135
137 unsigned int topologyId_;
138
139 // Internal type used for the Id. The exact nature of this type is kept
140 // as an implementation detail on purpose. We use a scoped enum here because scoped enums
141 // can be used as template parameters, but are not implicitly converted to other integral
142 // types by the compiler. That way, we avoid unfortunate implicit conversion chains, e.g.
143 // people trying to work with GlobalGeometryTypeIndex, but forgetting to actually call
144 // GlobalGeometryTypeIndex::index(gt) and just using gt directly.
145 enum class IdType : std::uint64_t
146 {};
147
148 public:
149
180 using Id = IdType;
181
189 constexpr operator Id() const
190 {
191 // recreate the exact storage layout that this class is using, making conversion
192 // extremely cheap
193 std::uint64_t id = dim_ | (std::uint64_t(none_) << 8) | (std::uint64_t(topologyId_) << 32);
194 return static_cast<Id>(id);
195 }
196
209 constexpr Id toId() const
210 {
211 return static_cast<Id>(*this);
212 }
213
221 constexpr GeometryType(Id id)
222 : dim_(static_cast<std::uint64_t>(id) & 0xFF)
223 , none_(static_cast<std::uint64_t>(id) & 0x100)
224 , topologyId_(static_cast<std::uint64_t>(id) >> 32)
225 {}
226
229
231 constexpr GeometryType ()
232 : dim_(0), none_(true), topologyId_(0)
233 {}
234
241 constexpr GeometryType(unsigned int topologyId, unsigned int dim, bool isNone)
242 : dim_(dim), none_(isNone), topologyId_(topologyId)
243 {}
244
250 constexpr GeometryType(unsigned int topologyId, unsigned int dim)
251 : dim_(dim), none_(false), topologyId_(topologyId)
252 {}
253
264 template<class TopologyType,
265 class = std::void_t<decltype(TopologyType::dimension), decltype(TopologyType::id)>>
266 explicit GeometryType([[maybe_unused]] TopologyType t)
267 : dim_(TopologyType::dimension), none_(false), topologyId_(TopologyType::id)
268 {}
269
276 constexpr bool isVertex() const {
277 return dim_==0;
278 }
279
281 constexpr bool isLine() const {
282 return dim_==1;
283 }
284
286 constexpr bool isTriangle() const {
287 return ! none_ && dim_==2 && (topologyId_ | 1) == 0b0001;
288 }
289
291 constexpr bool isQuadrilateral() const {
292 return ! none_ && dim_==2 && (topologyId_ | 1) == 0b0011;
293 }
294
296 constexpr bool isTetrahedron() const {
297 return ! none_ && dim_==3 && (topologyId_ | 1) == 0b0001;
298 }
299
301 constexpr bool isPyramid() const {
302 return ! none_ && dim_==3 && (topologyId_ | 1) == 0b0011;
303 }
304
306 constexpr bool isPrism() const {
307 return ! none_ && dim_==3 && (topologyId_ | 1) == 0b0101;
308 }
309
311 constexpr bool isHexahedron() const {
312 return ! none_ && dim_==3 && (topologyId_ | 1) == 0b0111;
313 }
314
316 constexpr bool isSimplex() const {
317 return ! none_ && (topologyId_ | 1) == 1;
318 }
319
321 constexpr bool isCube() const {
322 return ! none_ && ((topologyId_ ^ ((1 << dim_)-1)) >> 1 == 0);
323 }
324
326 constexpr bool isConical() const {
327 return ! none_ && (((topologyId_ & ~1) & (1u << (dim_-1))) == 0);
328 }
329
334 constexpr bool isConical(const int& step) const {
335 return ! none_ && (((topologyId_ & ~1) & (1u << step)) == 0);
336 }
337
339 constexpr bool isPrismatic() const {
340 return ! none_ && (( (topologyId_ | 1) & (1u << (dim_-1))) != 0);
341 }
342
347 constexpr bool isPrismatic(const int& step) const {
348 return ! none_ && (( (topologyId_ | 1) & (1u << step)) != 0);
349 }
350
352 constexpr bool isNone() const {
353 return none_;
354 }
355
357 constexpr unsigned int dim() const {
358 return dim_;
359 }
360
362 constexpr unsigned int id() const {
363 return topologyId_;
364 }
365
370
374 constexpr bool operator==(const GeometryType& other) const {
375 return ( ( none_ == other.none_ )
376 && ( ( none_ == true )
377 || ( ( dim_ == other.dim_ )
378 && ( (topologyId_ >> 1) == (other.topologyId_ >> 1) )
379 )
380 )
381 );
382 }
383
385 constexpr bool operator!=(const GeometryType& other) const {
386 return ! ((*this)==other);
387 }
388
390 constexpr bool operator < (const GeometryType& other) const {
391 return ( ( none_ < other.none_ )
392 || ( !( other.none_ < none_ )
393 && ( ( dim_ < other.dim_ )
394 || ( (other.dim_ == dim_)
395 && ((topologyId_ >> 1) < (other.topologyId_ >> 1) )
396 )
397 )
398 )
399 );
400 }
401
404 };
405
408 {
409 if (a.isSimplex())
410 {
411 s << "(simplex, " << a.dim() << ")";
412 return s;
413 }
414 if (a.isCube())
415 {
416 s << "(cube, " << a.dim() << ")";
417 return s;
418 }
419 if (a.isPyramid())
420 {
421 s << "(pyramid, 3)";
422 return s;
423 }
424 if (a.isPrism())
425 {
426 s << "(prism, 3)";
427 return s;
428 }
429 if (a.isNone())
430 {
431 s << "(none, " << a.dim() << ")";
432 return s;
433 }
434 s << "(other [" << a.id() << "], " << a.dim() << ")";
435 return s;
436 }
437
438
440
444 namespace GeometryTypes {
445
447
450 inline constexpr GeometryType simplex(unsigned int dim)
451 {
452 return GeometryType(0,dim,false);
453 }
454
456
459 inline constexpr GeometryType cube(unsigned int dim)
460 {
461 return GeometryType(((dim>1) ? ((1 << dim) - 1) : 0),dim,false);
462 }
463
465
468 inline constexpr GeometryType none(unsigned int dim)
469 {
470 return GeometryType(0,dim,true);
471 }
472
474 inline constexpr GeometryType conicalExtension(const GeometryType& gt)
475 {
476 return GeometryType(gt.id(), gt.dim()+1, gt.isNone());
477 }
478
480 inline constexpr GeometryType prismaticExtension(const GeometryType& gt)
481 {
482 return GeometryType(gt.id() | ((1 << gt.dim())), gt.dim()+1, gt.isNone());
483 }
484
486
489 inline constexpr GeometryType vertex = GeometryType(0,0,false);
490
492
495 inline constexpr GeometryType line = GeometryType(0,1,false);
496
498
501 inline constexpr GeometryType triangle = simplex(2);
502
504
507 inline constexpr GeometryType quadrilateral = cube(2);
508
510
513 inline constexpr GeometryType tetrahedron = simplex(3);
514
516
519 inline constexpr GeometryType pyramid = GeometryType(0b0011,3,false);
520
522
525 inline constexpr GeometryType prism = GeometryType(0b0101,3,false);
526
528
531 inline constexpr GeometryType hexahedron = cube(3);
532
533 }
534
535 namespace Impl
536 {
537
539 inline constexpr GeometryType getBase(const GeometryType& gt) {
540 return GeometryType(gt.id() & ((1 << (gt.dim()-1))-1), gt.dim()-1, gt.isNone());
541 }
542
543
544 // IfGeometryType
545 // ----------
546
547 template< template< GeometryType::Id > class Operation, int dim, GeometryType::Id geometryId = GeometryTypes::vertex >
548 struct IfGeometryType
549 {
550 static constexpr GeometryType geometry = geometryId;
551 template< class... Args >
552 static auto apply ( GeometryType gt, Args &&... args )
553 {
554 GeometryType lowerGeometry(gt.id() >>1 , gt.dim()-1, gt.isNone());
555
556 if( gt.id() & 1 )
557 return IfGeometryType< Operation, dim-1, GeometryTypes::prismaticExtension(geometry).toId() >::apply( lowerGeometry, std::forward< Args >( args )... );
558 else
559 return IfGeometryType< Operation, dim-1, GeometryTypes::conicalExtension(geometry).toId() >::apply( lowerGeometry, std::forward< Args >( args )... );
560 }
561 };
562
563 template< template< GeometryType::Id > class Operation, GeometryType::Id geometryId >
564 struct IfGeometryType< Operation, 0, geometryId>
565 {
566 template< class... Args >
567 static auto apply ([[maybe_unused]] GeometryType gt, Args &&... args )
568 {
569 return Operation< geometryId >::apply( std::forward< Args >( args )... );
570 }
571 };
572 } // namespace Impl
573} // namespace Dune
574
575#endif // DUNE_GEOMETRY_TYPE_HH
STL namespace.
std::ostream & operator<<(std::ostream &s, const bigunsignedint< k > &x)
size_type dim() const
Unique label for each type of entities that can occur in DUNE grids.
Definition type.hh:113
constexpr GeometryType(unsigned int topologyId, unsigned int dim)
Constructor, using the topologyId (integer) and the dimension.
Definition type.hh:250
constexpr bool operator<(const GeometryType &other) const
less-than operation for use with maps
Definition type.hh:390
constexpr bool operator!=(const GeometryType &other) const
Check for inequality.
Definition type.hh:385
constexpr bool isPyramid() const
Return true if entity is a pyramid.
Definition type.hh:301
constexpr bool isTetrahedron() const
Return true if entity is a tetrahedron.
Definition type.hh:296
constexpr bool isPrism() const
Return true if entity is a prism.
Definition type.hh:306
constexpr bool isVertex() const
Return true if entity is a vertex.
Definition type.hh:276
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:374
constexpr Id toId() const
Create an Id representation of this GeometryType.
Definition type.hh:209
constexpr bool isConical(const int &step) const
Return true if entity was constructed with a conical product in the chosen step.
Definition type.hh:334
constexpr unsigned int dim() const
Return dimension of the type.
Definition type.hh:357
constexpr bool isPrismatic(const int &step) const
Return true if entity was constructed with a prismatic product in the chosen step.
Definition type.hh:347
constexpr bool isTriangle() const
Return true if entity is a triangle.
Definition type.hh:286
GeometryType(TopologyType t)
Constructor from static TopologyType class.
Definition type.hh:266
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:241
BasicType
Each entity can be tagged by one of these basic types plus its space dimension.
Definition type.hh:119
@ cube
Cube element in any nonnegative dimension.
Definition type.hh:121
@ simplex
Simplicial element in any nonnegative dimension.
Definition type.hh:120
@ pyramid
Four sided pyramid in three dimensions.
Definition type.hh:122
@ extended
Other, more general topology, representable as topologyId.
Definition type.hh:124
@ none
Even more general topology, cannot be specified by a topologyId. Two GeometryTypes with 'none' type a...
Definition type.hh:125
@ prism
Prism element in three dimensions.
Definition type.hh:123
constexpr GeometryType(Id id)
Reconstruct a Geometry type from a GeometryType::Id.
Definition type.hh:221
constexpr bool isCube() const
Return true if entity is a cube of any dimension.
Definition type.hh:321
constexpr GeometryType()
Default constructor, not initializing anything.
Definition type.hh:231
constexpr bool isConical() const
Return true if entity was constructed with a conical product in the last step.
Definition type.hh:326
constexpr bool isLine() const
Return true if entity is a line segment.
Definition type.hh:281
constexpr bool isQuadrilateral() const
Return true if entity is a quadrilateral.
Definition type.hh:291
constexpr bool isPrismatic() const
Return true if entity was constructed with a prismatic product in the last step.
Definition type.hh:339
constexpr unsigned int id() const
Return the topology id of the type.
Definition type.hh:362
constexpr bool isNone() const
Return true if entity is a singular of any dimension.
Definition type.hh:352
constexpr bool isHexahedron() const
Return true if entity is a hexahedron.
Definition type.hh:311
constexpr bool isSimplex() const
Return true if entity is a simplex of any dimension.
Definition type.hh:316
IdType Id
An integral id representing a GeometryType.
Definition type.hh:180
T apply(T... args)