3 #ifndef DUNE_GEOMETRY_TYPE_HH
4 #define DUNE_GEOMETRY_TYPE_HH
14 #include <dune/common/deprecated.hh>
15 #include <dune/common/exceptions.hh>
16 #include <dune/common/keywords.hh>
17 #include <dune/common/typetraits.hh>
18 #include <dune/common/unused.hh>
30 enum TopologyConstruction { pyramidConstruction = 0, prismConstruction = 1 };
39 static const unsigned int dimension = 0;
40 static const unsigned int numCorners = 1;
42 static const unsigned int id = 0;
44 static std::string name () {
return "p"; }
48 template<
class BaseTopology >
51 static const unsigned int dimension = BaseTopology::dimension + 1;
52 static const unsigned int numCorners = 2 * BaseTopology::numCorners;
54 static const unsigned int id = BaseTopology::id | ((
unsigned int)prismConstruction << (dimension-1));
56 static std::string name () {
return BaseTopology::name() +
"l"; }
60 template<
class BaseTopology >
63 static const unsigned int dimension = BaseTopology::dimension + 1;
64 static const unsigned int numCorners = BaseTopology::numCorners + 1;
66 static const unsigned int id = BaseTopology::id | ((
unsigned int)pyramidConstruction << (dimension-1));
68 static std::string name () {
return BaseTopology::name() +
"o"; }
76 template<
class Topology >
78 :
public std::integral_constant< bool, (Topology::id >> 1) == 0 >
81 template<
class Topology >
83 :
public std::integral_constant< bool, (Topology::id | 1) == (1 << Topology::dimension) - 1 >
99 inline static unsigned int numTopologies ( int dim ) noexcept
115 inline bool static isPyramid ( unsigned int topologyId, int dim, int codim = 0 ) noexcept
117 assert( (dim > 0) && (topologyId < numTopologies( dim )) );
118 assert( (0 <= codim) && (codim < dim) );
119 return (((topologyId & ~1) & (1u << (dim-codim-1))) == 0);
133 inline static bool isPrism ( unsigned int topologyId, int dim, int codim = 0 ) noexcept
135 assert( (dim > 0) && (topologyId < numTopologies( dim )) );
136 assert( (0 <= codim) && (codim < dim) );
137 return (( (topologyId | 1) & (1u << (dim-codim-1))) != 0);
152 inline static bool isTopology ( TopologyConstruction construction, unsigned int topologyId, int dim, int codim = 0 ) noexcept
154 assert( (dim > 0) && (topologyId < numTopologies( dim )) );
155 assert( (0 <= codim) && (codim <= dim) );
156 return (codim >= (dim-1)) || (((topologyId >> (dim-codim-1)) & 1) == (unsigned int)construction);
166 inline static unsigned int baseTopologyId ( unsigned int topologyId, int dim, int codim = 1 ) noexcept
168 assert( (dim >= 0) && (topologyId < numTopologies( dim )) );
169 assert( (0 <= codim) && (codim <= dim) );
170 return topologyId & ((1u << (dim-codim)) - 1);
178 template< unsigned int dim >
179 struct SimplexTopology
181 typedef Pyramid< typename SimplexTopology< dim-1 >::type > type;
185 struct SimplexTopology< 0 >
195 template< unsigned int dim >
198 typedef Prism< typename CubeTopology< dim-1 >::type > type;
202 struct CubeTopology< 0 >
212 template< unsigned int dim >
213 struct PyramidTopology
215 typedef Pyramid< typename CubeTopology< dim-1 >::type > type;
223 template< unsigned int dim >
226 typedef Prism< typename SimplexTopology< dim-1 >::type > type;
235 template< template< class > class Operation, int dim, class Topology = Point >
238 template< class... Args >
239 static auto apply ( unsigned int topologyId, Args &&... args )
242 return IfTopology< Operation, dim-1, Prism< Topology > >::apply( topologyId >> 1, std::forward< Args >( args )... );
244 return IfTopology< Operation, dim-1, Pyramid< Topology > >::apply( topologyId >> 1, std::forward< Args >( args )... );
248 template< template< class > class Operation, class Topology >
249 struct IfTopology< Operation, 0, Topology >
251 template< class... Args >
252 static auto apply ( unsigned int topologyId, Args &&... args )
254 DUNE_UNUSED_PARAMETER( topologyId );
255 return Operation< Topology >::apply( std::forward< Args >( args )... );
303 unsigned int topologyId_;
311 enum class IdType : std::uint64_t
355 constexpr operator Id() const
359 std::uint64_t id = dim_ | (std::uint64_t(none_) << 8) | (std::uint64_t(topologyId_) << 32);
360 return static_cast<Id>(id);
370 constexpr GeometryType(Id id)
371 : dim_(static_cast<std::uint64_t>(id) & 0xFF)
372 , none_(static_cast<std::uint64_t>(id) & 0x100)
373 , topologyId_(static_cast<std::uint64_t>(id) >> 32)
380 constexpr GeometryType ()
381 : dim_(0), none_(true), topologyId_(0)
384 DUNE_NO_DEPRECATED_BEGIN
386 GeometryType(BasicType basicType, unsigned int dim)
387 DUNE_DEPRECATED_MSG("The GeometryType constructor taking BasicType is deprecated and will be removed after DUNE 2.6")
388 : dim_(dim), none_((basicType == GeometryType::none) ? true : false), topologyId_(0)
394 case GeometryType::simplex :
397 case GeometryType::cube :
398 topologyId_ = ((1 << dim) - 1);
400 case GeometryType::pyramid :
402 topologyId_ = 0b0011;
404 DUNE_THROW( RangeError,
405 "Invalid basic geometry type: no pyramids for dimension " << dim << "." );
407 case GeometryType::prism :
409 topologyId_ = 0b0101;
411 DUNE_THROW( RangeError,
412 "Invalid basic geometry type: no prisms for dimension " << dim << "." );
414 case GeometryType::none :
417 DUNE_THROW( RangeError,
418 "Invalid basic geometry type: " << basicType << " for dimension " << dim << "." );
421 DUNE_NO_DEPRECATED_END
429 constexpr GeometryType(unsigned int topologyId, unsigned int dim, bool none)
430 : dim_(dim), none_(none), topologyId_(topologyId)
438 constexpr GeometryType(unsigned int topologyId, unsigned int dim)
439 : dim_(dim), none_(false), topologyId_(topologyId)
452 template<class TopologyType,
453 class = Dune::void_t<decltype(TopologyType::dimension), decltype(TopologyType::id)>>
454 explicit GeometryType(TopologyType t)
455 : dim_(TopologyType::dimension), none_(false), topologyId_(TopologyType::id)
457 DUNE_UNUSED_PARAMETER(t);
461 DUNE_DEPRECATED_MSG("GeometryType(unsigned dim) is deprecated in DUNE 2.7, please use Dune::GeometryTypes::cube(dim) instead")
462 explicit GeometryType(unsigned int dim)
463 : dim_(dim), none_(false), topologyId_(0)
472 DUNE_DEPRECATED_MSG("GeometryType(dim) is deprecated in DUNE 2.7, please use Dune::GeometryTypes::cube(dim) instead")
473 explicit GeometryType(int dim)
474 : dim_(dim), none_(false), topologyId_(0)
486 DUNE_DEPRECATED_MSG("makeVertex() is deprecated in DUNE 2.6, please use Dune::GeometryTypes::vertex instead")
494 DUNE_DEPRECATED_MSG("makeLine() is deprecated in DUNE 2.6, please use Dune::GeometryTypes::line instead")
502 DUNE_DEPRECATED_MSG("makeTriangle() is deprecated in DUNE 2.6, please use Dune::GeometryTypes::triangle instead")
503 void makeTriangle() {
510 DUNE_DEPRECATED_MSG("makeQuadrilateral() is deprecated in DUNE 2.6, please use Dune::GeometryTypes::quadrilateral instead")
511 void makeQuadrilateral() {
514 topologyId_ = 0b0011;
518 DUNE_DEPRECATED_MSG("makeTetrahedron() is deprecated in DUNE 2.6, please use Dune::GeometryTypes::tetrahedron instead")
519 void makeTetrahedron() {
526 DUNE_DEPRECATED_MSG("makePyramid() is deprecated in DUNE 2.6, please use Dune::GeometryTypes::pyramid instead")
530 topologyId_ = 0b0011;
534 DUNE_DEPRECATED_MSG("makePrism() is deprecated in DUNE 2.6, please use Dune::GeometryTypes::prism instead")
538 topologyId_ = 0b0101;
542 DUNE_DEPRECATED_MSG("makeHexahedron() is deprecated in DUNE 2.6, please use Dune::GeometryTypes::hexahedron instead")
543 void makeHexahedron() {
546 topologyId_ = 0b0111;
550 DUNE_DEPRECATED_MSG("makeSimplex(dim) is deprecated in DUNE 2.6, please use Dune::GeometryTypes::simplex(dim) instead")
551 void makeSimplex(unsigned int dim) {
558 DUNE_DEPRECATED_MSG("makeCube(dim) is deprecated in DUNE 2.6, please use Dune::GeometryTypes::cube(dim) instead")
559 void makeCube(unsigned int dim) {
562 topologyId_ = ((dim>1) ? ((1 << dim) - 1) : 0);
566 DUNE_DEPRECATED_MSG("makeNone(dim) is deprecated in DUNE 2.6, please use Dune::GeometryTypes::none(dim) instead")
567 void makeNone(unsigned int dim) {
577 void makeFromVertices(unsigned int dim, unsigned int vertices) DUNE_DEPRECATED_MSG("Use the utility function geometryTypeFromVertexCount(...) instead.")
579 *this = geometryTypeFromVertexCount(dim, vertices);
589 constexpr bool isVertex() const {
594 constexpr bool isLine() const {
599 constexpr bool isTriangle() const {
600 return ! none_ && dim_==2 && (topologyId_ | 1) == 0b0001;
604 constexpr bool isQuadrilateral() const {
605 return ! none_ && dim_==2 && (topologyId_ | 1) == 0b0011;
609 constexpr bool isTetrahedron() const {
610 return ! none_ && dim_==3 && (topologyId_ | 1) == 0b0001;
614 constexpr bool isPyramid() const {
615 return ! none_ && dim_==3 && (topologyId_ | 1) == 0b0011;
619 constexpr bool isPrism() const {
620 return ! none_ && dim_==3 && (topologyId_ | 1) == 0b0101;
624 constexpr bool isHexahedron() const {
625 return ! none_ && dim_==3 && (topologyId_ | 1) == 0b0111;
629 constexpr bool isSimplex() const {
630 return ! none_ && (topologyId_ | 1) == 1;
634 constexpr bool isCube() const {
635 return ! none_ && ((topologyId_ ^ ((1 << dim_)-1)) >> 1 == 0);
639 constexpr bool isNone() const {
644 constexpr unsigned int dim() const {
649 constexpr unsigned int id() const {
661 constexpr bool operator==(const GeometryType& other) const {
662 return ( ( none_ == other.none_ )
663 && ( ( none_ == true )
664 || ( ( dim_ == other.dim_ )
665 && ( (topologyId_ >> 1) == (other.topologyId_ >> 1) )
672 constexpr bool operator!=(const GeometryType& other) const {
673 return ! ((*this)==other);
677 constexpr bool operator < (const GeometryType& other) const {
678 return ( ( none_ < other.none_ )
679 || ( !( other.none_ < none_ )
680 && ( ( dim_ < other.dim_ )
681 || ( (other.dim_ == dim_)
682 && ((topologyId_ >> 1) < (other.topologyId_ >> 1) )
694 inline std::ostream& operator<< (std::ostream& s, const GeometryType& a)
698 s << "(simplex, " << a.dim() << ")";
703 s << "(cube, " << a.dim() << ")";
718 s << "(none, " << a.dim() << ")";
721 s << "(other [" << a.id() << "], " << a.dim() << ")";
725 DUNE_NO_DEPRECATED_BEGIN
727 inline std::ostream& operator<< (std::ostream& s, GeometryType::BasicType type)
730 case GeometryType::simplex :
733 case GeometryType::cube :
736 case GeometryType::pyramid :
739 case GeometryType::prism :
742 case GeometryType::extended :
744 case GeometryType::none :
748 DUNE_THROW(Exception, "invalid GeometryType::BasicType");
752 DUNE_NO_DEPRECATED_END
760 namespace GeometryTypes {
766 inline constexpr GeometryType simplex(unsigned int dim)
768 return GeometryType(0,dim,false);
775 inline constexpr GeometryType cube(unsigned int dim)
777 return GeometryType(((dim>1) ? ((1 << dim) - 1) : 0),dim,false);
784 inline constexpr GeometryType none(unsigned int dim)
786 return GeometryType(0,dim,true);
789 #ifndef __cpp_inline_variables
797 DUNE_INLINE_VARIABLE constexpr GeometryType vertex = GeometryType(0,0,false);
803 DUNE_INLINE_VARIABLE constexpr GeometryType line = GeometryType(0,1,false);
809 DUNE_INLINE_VARIABLE constexpr GeometryType triangle = simplex(2);
815 DUNE_INLINE_VARIABLE constexpr GeometryType quadrilateral = cube(2);
821 DUNE_INLINE_VARIABLE constexpr GeometryType tetrahedron = simplex(3);
827 DUNE_INLINE_VARIABLE constexpr GeometryType pyramid = GeometryType(0b0011,3,false);
833 DUNE_INLINE_VARIABLE constexpr GeometryType prism = GeometryType(0b0101,3,false);
839 DUNE_INLINE_VARIABLE constexpr GeometryType hexahedron = cube(3);
841 #ifndef __cpp_inline_variables
851 #include "utility/typefromvertexcount.hh"