Loading...
Searching...
No Matches
type.hh
Go to the documentation of this file.
// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
// vi: set et ts=4 sw=2 sts=2:
// SPDX-FileCopyrightInfo: Copyright © DUNE Project contributors, see file LICENSE.md in module root
// SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception
#ifndef DUNE_GEOMETRY_TYPE_HH
#define DUNE_GEOMETRY_TYPE_HH
#include <cassert>
#include <cstdint>
#include <string>
#include <type_traits>
#include <dune/common/exceptions.hh>
#include <dune/common/typetraits.hh>
#include <dune/common/unused.hh>
namespace Dune
{
namespace Impl
{
enum TopologyConstruction { pyramidConstruction = 0, prismConstruction = 1 };
// Dynamic Topology Properties
// ---------------------------
inline static unsigned int numTopologies ( int dim ) noexcept
{
return (1u << dim);
}
inline bool static isPyramid ( unsigned int topologyId, int dim, int codim = 0 ) noexcept
{
assert( (dim > 0) && (topologyId < numTopologies( dim )) );
assert( (0 <= codim) && (codim < dim) );
return (((topologyId & ~1) & (1u << (dim-codim-1))) == 0);
}
inline static bool isPrism ( unsigned int topologyId, int dim, int codim = 0 ) noexcept
{
assert( (dim > 0) && (topologyId < numTopologies( dim )) );
assert( (0 <= codim) && (codim < dim) );
return (( (topologyId | 1) & (1u << (dim-codim-1))) != 0);
}
inline static unsigned int baseTopologyId ( unsigned int topologyId, int dim, int codim = 1 ) noexcept
{
assert( (dim >= 0) && (topologyId < numTopologies( dim )) );
assert( (0 <= codim) && (codim <= dim) );
return topologyId & ((1u << (dim-codim)) - 1);
}
} // namespace Impl
// GeometryType
// -------------
class GeometryType
{
public:
enum
BasicType {
simplex,
cube,
pyramid,
prism,
extended,
none
};
private:
unsigned char dim_;
bool none_;
unsigned int topologyId_;
// Internal type used for the Id. The exact nature of this type is kept
// as an implementation detail on purpose. We use a scoped enum here because scoped enums
// can be used as template parameters, but are not implicitly converted to other integral
// types by the compiler. That way, we avoid unfortunate implicit conversion chains, e.g.
// people trying to work with GlobalGeometryTypeIndex, but forgetting to actually call
// GlobalGeometryTypeIndex::index(gt) and just using gt directly.
enum class IdType : std::uint64_t
{};
public:
using Id = IdType;
constexpr operator Id() const
{
// recreate the exact storage layout that this class is using, making conversion
// extremely cheap
std::uint64_t id = dim_ | (std::uint64_t(none_) << 8) | (std::uint64_t(topologyId_) << 32);
return static_cast<Id>(id);
}
constexpr Id toId() const
{
return static_cast<Id>(*this);
}
constexpr GeometryType(Id id)
: dim_(static_cast<std::uint64_t>(id) & 0xFF)
, none_(static_cast<std::uint64_t>(id) & 0x100)
, topologyId_(static_cast<std::uint64_t>(id) >> 32)
{}
constexpr GeometryType ()
: dim_(0), none_(true), topologyId_(0)
{}
constexpr GeometryType(unsigned int topologyId, unsigned int dim, bool isNone)
: dim_(dim), none_(isNone), topologyId_(topologyId)
{}
constexpr GeometryType(unsigned int topologyId, unsigned int dim)
: dim_(dim), none_(false), topologyId_(topologyId)
{}
template<class TopologyType,
class = std::void_t<decltype(TopologyType::dimension), decltype(TopologyType::id)>>
explicit GeometryType(TopologyType t)
: dim_(TopologyType::dimension), none_(false), topologyId_(TopologyType::id)
{
DUNE_UNUSED_PARAMETER(t);
}
constexpr bool isVertex() const {
return dim_==0;
}
constexpr bool isLine() const {
return dim_==1;
}
constexpr bool isTriangle() const {
return ! none_ && dim_==2 && (topologyId_ | 1) == 0b0001;
}
constexpr bool isQuadrilateral() const {
return ! none_ && dim_==2 && (topologyId_ | 1) == 0b0011;
}
constexpr bool isTetrahedron() const {
return ! none_ && dim_==3 && (topologyId_ | 1) == 0b0001;
}
constexpr bool isPyramid() const {
return ! none_ && dim_==3 && (topologyId_ | 1) == 0b0011;
}
constexpr bool isPrism() const {
return ! none_ && dim_==3 && (topologyId_ | 1) == 0b0101;
}
constexpr bool isHexahedron() const {
return ! none_ && dim_==3 && (topologyId_ | 1) == 0b0111;
}
constexpr bool isSimplex() const {
return ! none_ && (topologyId_ | 1) == 1;
}
constexpr bool isCube() const {
return ! none_ && ((topologyId_ ^ ((1 << dim_)-1)) >> 1 == 0);
}
constexpr bool isConical() const {
return ! none_ && (((topologyId_ & ~1) & (1u << (dim_-1))) == 0);
}
constexpr bool isConical(const int& step) const {
return ! none_ && (((topologyId_ & ~1) & (1u << step)) == 0);
}
constexpr bool isPrismatic() const {
return ! none_ && (( (topologyId_ | 1) & (1u << (dim_-1))) != 0);
}
constexpr bool isPrismatic(const int& step) const {
return ! none_ && (( (topologyId_ | 1) & (1u << step)) != 0);
}
constexpr bool isNone() const {
return none_;
}
constexpr unsigned int dim() const {
return dim_;
}
constexpr unsigned int id() const {
return topologyId_;
}
constexpr bool operator==(const GeometryType& other) const {
return ( ( none_ == other.none_ )
&& ( ( none_ == true )
|| ( ( dim_ == other.dim_ )
&& ( (topologyId_ >> 1) == (other.topologyId_ >> 1) )
)
)
);
}
constexpr bool operator!=(const GeometryType& other) const {
return ! ((*this)==other);
}
constexpr bool operator < (const GeometryType& other) const {
return ( ( none_ < other.none_ )
|| ( !( other.none_ < none_ )
&& ( ( dim_ < other.dim_ )
|| ( (other.dim_ == dim_)
&& ((topologyId_ >> 1) < (other.topologyId_ >> 1) )
)
)
)
);
}
};
inline std::ostream& operator<< (std::ostream& s, const GeometryType& a)
{
if (a.isSimplex())
{
s << "(simplex, " << a.dim() << ")";
return s;
}
if (a.isCube())
{
s << "(cube, " << a.dim() << ")";
return s;
}
if (a.isPyramid())
{
s << "(pyramid, 3)";
return s;
}
if (a.isPrism())
{
s << "(prism, 3)";
return s;
}
if (a.isNone())
{
s << "(none, " << a.dim() << ")";
return s;
}
s << "(other [" << a.id() << "], " << a.dim() << ")";
return s;
}
namespace GeometryTypes {
inline constexpr GeometryType simplex(unsigned int dim)
{
return GeometryType(0,dim,false);
}
inline constexpr GeometryType cube(unsigned int dim)
{
return GeometryType(((dim>1) ? ((1 << dim) - 1) : 0),dim,false);
}
inline constexpr GeometryType none(unsigned int dim)
{
return GeometryType(0,dim,true);
}
inline constexpr GeometryType conicalExtension(const GeometryType& gt)
{
return GeometryType(gt.id(), gt.dim()+1, gt.isNone());
}
inline constexpr GeometryType prismaticExtension(const GeometryType& gt)
{
return GeometryType(gt.id() | ((1 << gt.dim())), gt.dim()+1, gt.isNone());
}
inline constexpr GeometryType vertex = GeometryType(0,0,false);
inline constexpr GeometryType line = GeometryType(0,1,false);
inline constexpr GeometryType triangle = simplex(2);
inline constexpr GeometryType quadrilateral = cube(2);
inline constexpr GeometryType tetrahedron = simplex(3);
inline constexpr GeometryType pyramid = GeometryType(0b0011,3,false);
inline constexpr GeometryType prism = GeometryType(0b0101,3,false);
inline constexpr GeometryType hexahedron = cube(3);
}
namespace Impl
{
inline constexpr GeometryType getBase(const GeometryType& gt) {
return GeometryType(gt.id() & ((1 << (gt.dim()-1))-1), gt.dim()-1, gt.isNone());
}
// IfGeometryType
// ----------
template< template< GeometryType::Id > class Operation, int dim, GeometryType::Id geometryId = GeometryTypes::vertex >
struct IfGeometryType
{
static constexpr GeometryType geometry = geometryId;
template< class... Args >
static auto apply ( GeometryType gt, Args &&... args )
{
GeometryType lowerGeometry(gt.id() >>1 , gt.dim()-1, gt.isNone());
if( gt.id() & 1 )
return IfGeometryType< Operation, dim-1, GeometryTypes::prismaticExtension(geometry).toId() >::apply( lowerGeometry, std::forward< Args >( args )... );
else
return IfGeometryType< Operation, dim-1, GeometryTypes::conicalExtension(geometry).toId() >::apply( lowerGeometry, std::forward< Args >( args )... );
}
};
template< template< GeometryType::Id > class Operation, GeometryType::Id geometryId >
struct IfGeometryType< Operation, 0, geometryId>
{
template< class... Args >
static auto apply ([[maybe_unused]] GeometryType gt, Args &&... args )
{
return Operation< geometryId >::apply( std::forward< Args >( args )... );
}
};
} // namespace Impl
} // namespace Dune
#endif // DUNE_GEOMETRY_TYPE_HH
Dune::GeometryTypes::line
constexpr GeometryType line
GeometryType representing a line.
Definition type.hh:498
Dune::GeometryTypes::cube
constexpr GeometryType cube(unsigned int dim)
Returns a GeometryType representing a hypercube of dimension dim.
Definition type.hh:462
Dune::GeometryTypes::prism
constexpr GeometryType prism
GeometryType representing a 3D prism.
Definition type.hh:528
Dune::GeometryTypes::triangle
constexpr GeometryType triangle
GeometryType representing a triangle.
Definition type.hh:504
Dune::GeometryTypes::quadrilateral
constexpr GeometryType quadrilateral
GeometryType representing a quadrilateral (a square).
Definition type.hh:510
Dune::GeometryTypes::hexahedron
constexpr GeometryType hexahedron
GeometryType representing a hexahedron.
Definition type.hh:534
Dune::GeometryTypes::pyramid
constexpr GeometryType pyramid
GeometryType representing a 3D pyramid.
Definition type.hh:522
Dune::GeometryTypes::tetrahedron
constexpr GeometryType tetrahedron
GeometryType representing a tetrahedron.
Definition type.hh:516
Dune::GeometryTypes::none
constexpr GeometryType none(unsigned int dim)
Returns a GeometryType representing a singular of dimension dim.
Definition type.hh:471
Dune::GeometryTypes::simplex
constexpr GeometryType simplex(unsigned int dim)
Returns a GeometryType representing a simplex of dimension dim.
Definition type.hh:453
Dune::GeometryTypes::vertex
constexpr GeometryType vertex
GeometryType representing a vertex.
Definition type.hh:492
std
STL namespace.
Dune
Definition affinegeometry.hh:22
Dune::operator<<
std::ostream & operator<<(std::ostream &s, const GeometryType &a)
Prints the type to an output stream.
Definition type.hh:410
Dune::GeometryTypes
Predefined GeometryTypes for common geometries.
Definition type.hh:447
Dune::GeometryTypes::prismaticExtension
constexpr GeometryType prismaticExtension(const GeometryType &gt)
Return GeometryType of a prismatic construction with gt as base.
Definition type.hh:483
Dune::GeometryTypes::conicalExtension
constexpr GeometryType conicalExtension(const GeometryType &gt)
Return GeometryType of a conical construction with gt as base.
Definition type.hh:477
Dune::GeometryType
Unique label for each type of entities that can occur in DUNE grids.
Definition type.hh:114
Dune::GeometryType::GeometryType
constexpr GeometryType(unsigned int topologyId, unsigned int dim)
Constructor, using the topologyId (integer) and the dimension.
Definition type.hh:251
Dune::GeometryType::operator<
constexpr bool operator<(const GeometryType &other) const
less-than operation for use with maps
Definition type.hh:393
Dune::GeometryType::operator!=
constexpr bool operator!=(const GeometryType &other) const
Check for inequality.
Definition type.hh:388
Dune::GeometryType::isPyramid
constexpr bool isPyramid() const
Return true if entity is a pyramid.
Definition type.hh:304
Dune::GeometryType::isTetrahedron
constexpr bool isTetrahedron() const
Return true if entity is a tetrahedron.
Definition type.hh:299
Dune::GeometryType::isPrism
constexpr bool isPrism() const
Return true if entity is a prism.
Definition type.hh:309
Dune::GeometryType::isVertex
constexpr bool isVertex() const
Return true if entity is a vertex.
Definition type.hh:279
Dune::GeometryType::operator==
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
Dune::GeometryType::toId
constexpr Id toId() const
Create an Id representation of this GeometryType.
Definition type.hh:210
Dune::GeometryType::isConical
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
Dune::GeometryType::dim
constexpr unsigned int dim() const
Return dimension of the type.
Definition type.hh:360
Dune::GeometryType::isPrismatic
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
Dune::GeometryType::isTriangle
constexpr bool isTriangle() const
Return true if entity is a triangle.
Definition type.hh:289
Dune::GeometryType::GeometryType
GeometryType(TopologyType t)
Constructor from static TopologyType class.
Definition type.hh:267
Dune::GeometryType::GeometryType
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
Dune::GeometryType::BasicType
BasicType
Each entity can be tagged by one of these basic types plus its space dimension.
Definition type.hh:120
Dune::GeometryType::cube
@ cube
Cube element in any nonnegative dimension.
Definition type.hh:122
Dune::GeometryType::simplex
@ simplex
Simplicial element in any nonnegative dimension.
Definition type.hh:121
Dune::GeometryType::pyramid
@ pyramid
Four sided pyramid in three dimensions.
Definition type.hh:123
Dune::GeometryType::extended
@ extended
Other, more general topology, representable as topologyId.
Definition type.hh:125
Dune::GeometryType::none
@ none
Even more general topology, cannot be specified by a topologyId. Two GeometryTypes with 'none' type a...
Definition type.hh:126
Dune::GeometryType::prism
@ prism
Prism element in three dimensions.
Definition type.hh:124
Dune::GeometryType::GeometryType
constexpr GeometryType(Id id)
Reconstruct a Geometry type from a GeometryType::Id.
Definition type.hh:222
Dune::GeometryType::isCube
constexpr bool isCube() const
Return true if entity is a cube of any dimension.
Definition type.hh:324
Dune::GeometryType::GeometryType
constexpr GeometryType()
Default constructor, not initializing anything.
Definition type.hh:232
Dune::GeometryType::isConical
constexpr bool isConical() const
Return true if entity was constructed with a conical product in the last step.
Definition type.hh:329
Dune::GeometryType::isLine
constexpr bool isLine() const
Return true if entity is a line segment.
Definition type.hh:284
Dune::GeometryType::isQuadrilateral
constexpr bool isQuadrilateral() const
Return true if entity is a quadrilateral.
Definition type.hh:294
Dune::GeometryType::isPrismatic
constexpr bool isPrismatic() const
Return true if entity was constructed with a prismatic product in the last step.
Definition type.hh:342
Dune::GeometryType::id
constexpr unsigned int id() const
Return the topology id of the type.
Definition type.hh:365
Dune::GeometryType::isNone
constexpr bool isNone() const
Return true if entity is a singular of any dimension.
Definition type.hh:355
Dune::GeometryType::isHexahedron
constexpr bool isHexahedron() const
Return true if entity is a hexahedron.
Definition type.hh:314
Dune::GeometryType::isSimplex
constexpr bool isSimplex() const
Return true if entity is a simplex of any dimension.
Definition type.hh:319
Dune::GeometryType::Id
IdType Id
An integral id representing a GeometryType.
Definition type.hh:181