Dune Core Modules (2.7.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 #ifndef DUNE_GEOMETRY_TYPE_HH
4 #define DUNE_GEOMETRY_TYPE_HH
5 
10 #include <cassert>
11 
12 #include <string>
13 
16 #include <dune/common/keywords.hh>
18 #include <dune/common/unused.hh>
19 
20 namespace Dune
21 {
22 
23  // forward declaration needed for deprecated makeFromVertices
24  class GeometryType;
25  GeometryType geometryTypeFromVertexCount(unsigned int dim, unsigned int vertices);
26 
27  namespace Impl
28  {
29 
30  enum TopologyConstruction { pyramidConstruction = 0, prismConstruction = 1 };
31 
32 
33 
34  // Basic Topology Types
35  // --------------------
36 
37  struct Point
38  {
39  static const unsigned int dimension = 0;
40  static const unsigned int numCorners = 1;
41 
42  static const unsigned int id = 0;
43 
44  static std::string name () { return "p"; }
45  };
46 
47 
48  template< class BaseTopology >
49  struct Prism
50  {
51  static const unsigned int dimension = BaseTopology::dimension + 1;
52  static const unsigned int numCorners = 2 * BaseTopology::numCorners;
53 
54  static const unsigned int id = BaseTopology::id | ((unsigned int)prismConstruction << (dimension-1));
55 
56  static std::string name () { return BaseTopology::name() + "l"; }
57  };
58 
59 
60  template< class BaseTopology >
61  struct Pyramid
62  {
63  static const unsigned int dimension = BaseTopology::dimension + 1;
64  static const unsigned int numCorners = BaseTopology::numCorners + 1;
65 
66  static const unsigned int id = BaseTopology::id | ((unsigned int)pyramidConstruction << (dimension-1));
67 
68  static std::string name () { return BaseTopology::name() + "o"; }
69  };
70 
71 
72 
73  // Properties of Topologies
74  // ------------------------
75 
76  template< class Topology >
77  struct IsSimplex
78  : public std::integral_constant< bool, (Topology::id >> 1) == 0 >
79  {};
80 
81  template< class Topology >
82  struct IsCube
83  : public std::integral_constant< bool, (Topology::id | 1) == (1 << Topology::dimension) - 1 >
84  {};
85 
86 
87 
88  // Dynamic Topology Properties
89  // ---------------------------
90 
99  inline static unsigned int numTopologies ( int dim ) noexcept
100  {
101  return (1u << dim);
102  }
103 
115  inline bool static isPyramid ( unsigned int topologyId, int dim, int codim = 0 ) noexcept
116  {
117  assert( (dim > 0) && (topologyId < numTopologies( dim )) );
118  assert( (0 <= codim) && (codim < dim) );
119  return (((topologyId & ~1) & (1u << (dim-codim-1))) == 0);
120  }
121 
133  inline static bool isPrism ( unsigned int topologyId, int dim, int codim = 0 ) noexcept
134  {
135  assert( (dim > 0) && (topologyId < numTopologies( dim )) );
136  assert( (0 <= codim) && (codim < dim) );
137  return (( (topologyId | 1) & (1u << (dim-codim-1))) != 0);
138  }
139 
152  inline static bool isTopology ( TopologyConstruction construction, unsigned int topologyId, int dim, int codim = 0 ) noexcept
153  {
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);
157  }
158 
166  inline static unsigned int baseTopologyId ( unsigned int topologyId, int dim, int codim = 1 ) noexcept
167  {
168  assert( (dim >= 0) && (topologyId < numTopologies( dim )) );
169  assert( (0 <= codim) && (codim <= dim) );
170  return topologyId & ((1u << (dim-codim)) - 1);
171  }
172 
173 
174 
175  // SimplexTopology
176  // ---------------
177 
178  template< unsigned int dim >
179  struct SimplexTopology
180  {
181  typedef Pyramid< typename SimplexTopology< dim-1 >::type > type;
182  };
183 
184  template<>
185  struct SimplexTopology< 0 >
186  {
187  typedef Point type;
188  };
189 
190 
191 
192  // CubeTopology
193  // ------------
194 
195  template< unsigned int dim >
196  struct CubeTopology
197  {
198  typedef Prism< typename CubeTopology< dim-1 >::type > type;
199  };
200 
201  template<>
202  struct CubeTopology< 0 >
203  {
204  typedef Point type;
205  };
206 
207 
208 
209  // PyramidTopology
210  // ---------------
211 
212  template< unsigned int dim >
213  struct PyramidTopology
214  {
215  typedef Pyramid< typename CubeTopology< dim-1 >::type > type;
216  };
217 
218 
219 
220  // PrismTopology
221  // -------------
222 
223  template< unsigned int dim >
224  struct PrismTopology
225  {
226  typedef Prism< typename SimplexTopology< dim-1 >::type > type;
227  };
228 
229 
230 
231 
232  // IfTopology
233  // ----------
234 
235  template< template< class > class Operation, int dim, class Topology = Point >
236  struct IfTopology
237  {
238  template< class... Args >
239  static auto apply ( unsigned int topologyId, Args &&... args )
240  {
241  if( topologyId & 1 )
242  return IfTopology< Operation, dim-1, Prism< Topology > >::apply( topologyId >> 1, std::forward< Args >( args )... );
243  else
244  return IfTopology< Operation, dim-1, Pyramid< Topology > >::apply( topologyId >> 1, std::forward< Args >( args )... );
245  }
246  };
247 
248  template< template< class > class Operation, class Topology >
249  struct IfTopology< Operation, 0, Topology >
250  {
251  template< class... Args >
252  static auto apply ( unsigned int topologyId, Args &&... args )
253  {
254  DUNE_UNUSED_PARAMETER( topologyId );
255  return Operation< Topology >::apply( std::forward< Args >( args )... );
256  }
257  };
258 
259  } // namespace Impl
260 
261 
262 
263  // GeometryType
264  // -------------
265 
278  class GeometryType
279  {
280  public:
281 
284  enum
285  BasicType {
286  simplex,
287  cube,
288  pyramid,
289  prism,
290  extended,
291  none
292  };
293 
294  private:
295 
297  unsigned char dim_;
298 
300  bool none_;
301 
303  unsigned int topologyId_;
304 
305  // Internal type used for the Id. The exact nature of this type is kept
306  // as an implementation detail on purpose. We use a scoped enum here because scoped enums
307  // can be used as template parameters, but are not implicitly converted to other integral
308  // types by the compiler. That way, we avoid unfortunate implicit conversion chains, e.g.
309  // people trying to work with GlobalGeometryTypeIndex, but forgetting to actually call
310  // GlobalGeometryTypeIndex::index(gt) and just using gt directly.
311  enum class IdType : std::uint64_t
312  {};
313 
314  public:
315 
346  using Id = IdType;
347 
355  constexpr operator Id() const
356  {
357  // recreate the exact storage layout that this class is using, making conversion
358  // extremely cheap
359  std::uint64_t id = dim_ | (std::uint64_t(none_) << 8) | (std::uint64_t(topologyId_) << 32);
360  return static_cast<Id>(id);
361  }
362 
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)
374  {}
375 
378 
380  constexpr GeometryType ()
381  : dim_(0), none_(true), topologyId_(0)
382  {}
383 
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)
389  {
390  if (dim < 2)
391  return;
392  switch( basicType )
393  {
394  case GeometryType::simplex :
395  topologyId_ = 0;
396  break;
397  case GeometryType::cube :
398  topologyId_ = ((1 << dim) - 1);
399  break;
400  case GeometryType::pyramid :
401  if (dim == 3)
402  topologyId_ = 0b0011;
403  else
404  DUNE_THROW( RangeError,
405  "Invalid basic geometry type: no pyramids for dimension " << dim << "." );
406  break;
407  case GeometryType::prism :
408  if (dim == 3)
409  topologyId_ = 0b0101;
410  else
411  DUNE_THROW( RangeError,
412  "Invalid basic geometry type: no prisms for dimension " << dim << "." );
413  break;
414  case GeometryType::none :
415  break;
416  default :
417  DUNE_THROW( RangeError,
418  "Invalid basic geometry type: " << basicType << " for dimension " << dim << "." );
419  }
420  }
421  DUNE_NO_DEPRECATED_END
422 
429  constexpr GeometryType(unsigned int topologyId, unsigned int dim, bool none)
430  : dim_(dim), none_(none), topologyId_(topologyId)
431  {}
432 
438  constexpr GeometryType(unsigned int topologyId, unsigned int dim)
439  : dim_(dim), none_(false), topologyId_(topologyId)
440  {}
441 
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)
456  {
457  DUNE_UNUSED_PARAMETER(t);
458  }
459 
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)
464  {
465  assert(dim < 2);
466  }
467 
469  // We need this constructor for "int" and "unsigned int",
470  // because otherwise GeometryType(int) would try to call the
471  // generic GeometryType(TopologyType) constructor
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)
475  {
476  assert(dim < 2);
477  }
478 
484 
486  DUNE_DEPRECATED_MSG("makeVertex() is deprecated in DUNE 2.6, please use Dune::GeometryTypes::vertex instead")
487  void makeVertex() {
488  none_ = false;
489  dim_ = 0;
490  topologyId_ = 0;
491  }
492 
494  DUNE_DEPRECATED_MSG("makeLine() is deprecated in DUNE 2.6, please use Dune::GeometryTypes::line instead")
495  void makeLine() {
496  none_ = false;
497  dim_ = 1;
498  topologyId_ = 0;
499  }
500 
502  DUNE_DEPRECATED_MSG("makeTriangle() is deprecated in DUNE 2.6, please use Dune::GeometryTypes::triangle instead")
503  void makeTriangle() {
504  none_ = false;
505  dim_ = 2;
506  topologyId_ = 0;
507  }
508 
510  DUNE_DEPRECATED_MSG("makeQuadrilateral() is deprecated in DUNE 2.6, please use Dune::GeometryTypes::quadrilateral instead")
511  void makeQuadrilateral() {
512  none_ = false;
513  dim_ = 2;
514  topologyId_ = 0b0011;
515  }
516 
518  DUNE_DEPRECATED_MSG("makeTetrahedron() is deprecated in DUNE 2.6, please use Dune::GeometryTypes::tetrahedron instead")
519  void makeTetrahedron() {
520  none_ = false;
521  dim_ = 3;
522  topologyId_ = 0;
523  }
524 
526  DUNE_DEPRECATED_MSG("makePyramid() is deprecated in DUNE 2.6, please use Dune::GeometryTypes::pyramid instead")
527  void makePyramid() {
528  none_ = false;
529  dim_ = 3;
530  topologyId_ = 0b0011;
531  }
532 
534  DUNE_DEPRECATED_MSG("makePrism() is deprecated in DUNE 2.6, please use Dune::GeometryTypes::prism instead")
535  void makePrism() {
536  none_ = false;
537  dim_ = 3;
538  topologyId_ = 0b0101; // (1 << (dim_-1)) - 1;
539  }
540 
542  DUNE_DEPRECATED_MSG("makeHexahedron() is deprecated in DUNE 2.6, please use Dune::GeometryTypes::hexahedron instead")
543  void makeHexahedron() {
544  none_ = false;
545  dim_ = 3;
546  topologyId_ = 0b0111;
547  }
548 
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) {
552  none_ = false;
553  dim_ = dim;
554  topologyId_ = 0;
555  }
556 
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) {
560  none_ = false;
561  dim_ = dim;
562  topologyId_ = ((dim>1) ? ((1 << dim) - 1) : 0);
563  }
564 
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) {
568  none_ = true;
569  dim_ = dim;
570  topologyId_ = 0;
571  }
572 
577  void makeFromVertices(unsigned int dim, unsigned int vertices) DUNE_DEPRECATED_MSG("Use the utility function geometryTypeFromVertexCount(...) instead.")
578  {
579  *this = geometryTypeFromVertexCount(dim, vertices);
580  return;
581  }
582 
589  constexpr bool isVertex() const {
590  return dim_==0;
591  }
592 
594  constexpr bool isLine() const {
595  return dim_==1;
596  }
597 
599  constexpr bool isTriangle() const {
600  return ! none_ && dim_==2 && (topologyId_ | 1) == 0b0001;
601  }
602 
604  constexpr bool isQuadrilateral() const {
605  return ! none_ && dim_==2 && (topologyId_ | 1) == 0b0011;
606  }
607 
609  constexpr bool isTetrahedron() const {
610  return ! none_ && dim_==3 && (topologyId_ | 1) == 0b0001;
611  }
612 
614  constexpr bool isPyramid() const {
615  return ! none_ && dim_==3 && (topologyId_ | 1) == 0b0011;
616  }
617 
619  constexpr bool isPrism() const {
620  return ! none_ && dim_==3 && (topologyId_ | 1) == 0b0101;
621  }
622 
624  constexpr bool isHexahedron() const {
625  return ! none_ && dim_==3 && (topologyId_ | 1) == 0b0111;
626  }
627 
629  constexpr bool isSimplex() const {
630  return ! none_ && (topologyId_ | 1) == 1;
631  }
632 
634  constexpr bool isCube() const {
635  return ! none_ && ((topologyId_ ^ ((1 << dim_)-1)) >> 1 == 0);
636  }
637 
639  constexpr bool isNone() const {
640  return none_;
641  }
642 
644  constexpr unsigned int dim() const {
645  return dim_;
646  }
647 
649  constexpr unsigned int id() const {
650  return topologyId_;
651  }
652 
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) )
666  )
667  )
668  );
669  }
670 
672  constexpr bool operator!=(const GeometryType& other) const {
673  return ! ((*this)==other);
674  }
675 
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) )
683  )
684  )
685  )
686  );
687  }
688 
691  };
692 
694  inline std::ostream& operator<< (std::ostream& s, const GeometryType& a)
695  {
696  if (a.isSimplex())
697  {
698  s << "(simplex, " << a.dim() << ")";
699  return s;
700  }
701  if (a.isCube())
702  {
703  s << "(cube, " << a.dim() << ")";
704  return s;
705  }
706  if (a.isPyramid())
707  {
708  s << "(pyramid, 3)";
709  return s;
710  }
711  if (a.isPrism())
712  {
713  s << "(prism, 3)";
714  return s;
715  }
716  if (a.isNone())
717  {
718  s << "(none, " << a.dim() << ")";
719  return s;
720  }
721  s << "(other [" << a.id() << "], " << a.dim() << ")";
722  return s;
723  }
724 
725  DUNE_NO_DEPRECATED_BEGIN
727  inline std::ostream& operator<< (std::ostream& s, GeometryType::BasicType type)
728  {
729  switch (type) {
730  case GeometryType::simplex :
731  s << "simplex";
732  break;
733  case GeometryType::cube :
734  s << "cube";
735  break;
736  case GeometryType::pyramid :
737  s << "pyramid";
738  break;
739  case GeometryType::prism :
740  s << "prism";
741  break;
742  case GeometryType::extended :
743  s << "other";
744  case GeometryType::none :
745  s << "none";
746  break;
747  default :
748  DUNE_THROW(Exception, "invalid GeometryType::BasicType");
749  }
750  return s;
751  }
752  DUNE_NO_DEPRECATED_END
753 
754 
756 
760  namespace GeometryTypes {
761 
763 
766  inline constexpr GeometryType simplex(unsigned int dim)
767  {
768  return GeometryType(0,dim,false);
769  }
770 
772 
775  inline constexpr GeometryType cube(unsigned int dim)
776  {
777  return GeometryType(((dim>1) ? ((1 << dim) - 1) : 0),dim,false);
778  }
779 
781 
784  inline constexpr GeometryType none(unsigned int dim)
785  {
786  return GeometryType(0,dim,true);
787  }
788 
789 #ifndef __cpp_inline_variables
790  namespace {
791 #endif
792 
794 
797  DUNE_INLINE_VARIABLE constexpr GeometryType vertex = GeometryType(0,0,false);
798 
800 
803  DUNE_INLINE_VARIABLE constexpr GeometryType line = GeometryType(0,1,false);
804 
806 
809  DUNE_INLINE_VARIABLE constexpr GeometryType triangle = simplex(2);
810 
812 
815  DUNE_INLINE_VARIABLE constexpr GeometryType quadrilateral = cube(2);
816 
818 
821  DUNE_INLINE_VARIABLE constexpr GeometryType tetrahedron = simplex(3);
822 
824 
827  DUNE_INLINE_VARIABLE constexpr GeometryType pyramid = GeometryType(0b0011,3,false);
828 
830 
833  DUNE_INLINE_VARIABLE constexpr GeometryType prism = GeometryType(0b0101,3,false);
834 
836 
839  DUNE_INLINE_VARIABLE constexpr GeometryType hexahedron = cube(3);
840 
841 #ifndef __cpp_inline_variables
842  }
843 #endif
844 
845  }
846 
847 
848 } // namespace Dune
849 
850 // include utility header needed for deprecated makeFromVertices
851 #include "utility/typefromvertexcount.hh"
852 
853 #endif // DUNE_GEOMETRY_TYPE_HH
Definition of the DUNE_DEPRECATED macro for the case that config.h is not available.
A few common exception classes.
GeometryType
Type representing VTK's entity geometry types.
Definition: common.hh:180
Definitions of several macros that conditionally make C++ syntax available.
Dune namespace.
Definition: alignedallocator.hh:14
GeometryType geometryTypeFromVertexCount(unsigned int dim, unsigned int vertices)
Utitlity function to construct the correct geometry type given the dimension and the number of vertic...
Definition: typefromvertexcount.hh:15
Traits for type conversions and type information.
Definition of the DUNE_UNUSED macro for the case that config.h is not available.
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.80.0 (May 13, 22:30, 2024)