Dune Core Modules (2.6.0)

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
18#include <dune/common/unused.hh>
19
20namespace 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
276 class GeometryType
277 {
278 public:
279
282 enum
283 BasicType {
284 simplex,
285 cube,
286 pyramid,
287 prism,
288 extended,
289 none
290 };
291
292 private:
293
295 unsigned int topologyId_;
296
298 unsigned char dim_ : 7;
299
301 bool none_ : 1;
302
303 public:
304
307
309 constexpr GeometryType ()
310 : topologyId_(0), dim_(0), none_(true)
311 {}
312
313#pragma GCC diagnostic push
314#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
316 GeometryType(BasicType basicType, unsigned int dim)
317 DUNE_DEPRECATED_MSG("The GeometryType constructor taking BasicType is deprecated and will be removed after DUNE 2.6")
318 : topologyId_(0), dim_(dim), none_((basicType == GeometryType::none) ? true : false)
319 {
320 if (dim < 2)
321 return;
322 switch( basicType )
323 {
324 case GeometryType::simplex :
325 topologyId_ = 0;
326 break;
327 case GeometryType::cube :
328 topologyId_ = ((1 << dim) - 1);
329 break;
330 case GeometryType::pyramid :
331 if (dim == 3)
332 topologyId_ = 0b0011;
333 else
334 DUNE_THROW( RangeError,
335 "Invalid basic geometry type: no pyramids for dimension " << dim << "." );
336 break;
337 case GeometryType::prism :
338 if (dim == 3)
339 topologyId_ = 0b0101;
340 else
341 DUNE_THROW( RangeError,
342 "Invalid basic geometry type: no prisms for dimension " << dim << "." );
343 break;
344 case GeometryType::none :
345 break;
346 default :
347 DUNE_THROW( RangeError,
348 "Invalid basic geometry type: " << basicType << " for dimension " << dim << "." );
349 }
350 }
351#pragma GCC diagnostic pop
352
359 constexpr GeometryType(unsigned int topologyId, unsigned int dim, bool none)
360 : topologyId_(topologyId), dim_(dim), none_(none)
361 {}
362
368 constexpr GeometryType(unsigned int topologyId, unsigned int dim)
369 : topologyId_(topologyId), dim_(dim), none_(false)
370 {}
371
382 template<class TopologyType,
383 class = Dune::void_t<decltype(TopologyType::dimension), decltype(TopologyType::id)>>
384 explicit GeometryType(TopologyType t)
385 : topologyId_(TopologyType::id), dim_(TopologyType::dimension), none_(false)
386 {
387 DUNE_UNUSED_PARAMETER(t);
388 }
389
391 explicit GeometryType(unsigned int dim)
392 : topologyId_(0), dim_(dim), none_(false)
393 {
394 assert(dim < 2);
395 }
396
398 // We need this constructor for "int" and "unsigned int",
399 // because otherwise GeometryType(int) would try to call the
400 // generic GeometryType(TopologyType) constructor
401 explicit GeometryType(int dim)
402 : topologyId_(0), dim_(dim), none_(false)
403 {
404 assert(dim < 2);
405 }
406
412
414 DUNE_DEPRECATED_MSG("makeVertex() is deprecated in DUNE 2.6, please use Dune::GeometryTypes::vertex instead")
415 void makeVertex() {
416 none_ = false;
417 dim_ = 0;
418 topologyId_ = 0;
419 }
420
422 DUNE_DEPRECATED_MSG("makeLine() is deprecated in DUNE 2.6, please use Dune::GeometryTypes::line instead")
423 void makeLine() {
424 none_ = false;
425 dim_ = 1;
426 topologyId_ = 0;
427 }
428
430 DUNE_DEPRECATED_MSG("makeTriangle() is deprecated in DUNE 2.6, please use Dune::GeometryTypes::triangle instead")
431 void makeTriangle() {
432 none_ = false;
433 dim_ = 2;
434 topologyId_ = 0;
435 }
436
438 DUNE_DEPRECATED_MSG("makeQuadrilateral() is deprecated in DUNE 2.6, please use Dune::GeometryTypes::quadrilateral instead")
439 void makeQuadrilateral() {
440 none_ = false;
441 dim_ = 2;
442 topologyId_ = 0b0011;
443 }
444
446 DUNE_DEPRECATED_MSG("makeTetrahedron() is deprecated in DUNE 2.6, please use Dune::GeometryTypes::tetrahedron instead")
447 void makeTetrahedron() {
448 none_ = false;
449 dim_ = 3;
450 topologyId_ = 0;
451 }
452
454 DUNE_DEPRECATED_MSG("makePyramid() is deprecated in DUNE 2.6, please use Dune::GeometryTypes::pyramid instead")
455 void makePyramid() {
456 none_ = false;
457 dim_ = 3;
458 topologyId_ = 0b0011;
459 }
460
462 DUNE_DEPRECATED_MSG("makePrism() is deprecated in DUNE 2.6, please use Dune::GeometryTypes::prism instead")
463 void makePrism() {
464 none_ = false;
465 dim_ = 3;
466 topologyId_ = 0b0101; // (1 << (dim_-1)) - 1;
467 }
468
470 DUNE_DEPRECATED_MSG("makeHexahedron() is deprecated in DUNE 2.6, please use Dune::GeometryTypes::hexahedron instead")
471 void makeHexahedron() {
472 none_ = false;
473 dim_ = 3;
474 topologyId_ = 0b0111;
475 }
476
478 DUNE_DEPRECATED_MSG("makeSimplex(dim) is deprecated in DUNE 2.6, please use Dune::GeometryTypes::simplex(dim) instead")
479 void makeSimplex(unsigned int dim) {
480 none_ = false;
481 dim_ = dim;
482 topologyId_ = 0;
483 }
484
486 DUNE_DEPRECATED_MSG("makeCube(dim) is deprecated in DUNE 2.6, please use Dune::GeometryTypes::cube(dim) instead")
487 void makeCube(unsigned int dim) {
488 none_ = false;
489 dim_ = dim;
490 topologyId_ = ((dim>1) ? ((1 << dim) - 1) : 0);
491 }
492
494 DUNE_DEPRECATED_MSG("makeNone(dim) is deprecated in DUNE 2.6, please use Dune::GeometryTypes::none(dim) instead")
495 void makeNone(unsigned int dim) {
496 none_ = true;
497 dim_ = dim;
498 topologyId_ = 0;
499 }
500
505 void makeFromVertices(unsigned int dim, unsigned int vertices) DUNE_DEPRECATED_MSG("Use the utility function geometryTypeFromVertexCount(...) instead.")
506 {
507 *this = geometryTypeFromVertexCount(dim, vertices);
508 return;
509 }
510
517 constexpr bool isVertex() const {
518 return dim_==0;
519 }
520
522 constexpr bool isLine() const {
523 return dim_==1;
524 }
525
527 constexpr bool isTriangle() const {
528 return ! none_ && dim_==2 && (topologyId_ | 1) == 0b0001;
529 }
530
532 constexpr bool isQuadrilateral() const {
533 return ! none_ && dim_==2 && (topologyId_ | 1) == 0b0011;
534 }
535
537 constexpr bool isTetrahedron() const {
538 return ! none_ && dim_==3 && (topologyId_ | 1) == 0b0001;
539 }
540
542 constexpr bool isPyramid() const {
543 return ! none_ && dim_==3 && (topologyId_ | 1) == 0b0011;
544 }
545
547 constexpr bool isPrism() const {
548 return ! none_ && dim_==3 && (topologyId_ | 1) == 0b0101;
549 }
550
552 constexpr bool isHexahedron() const {
553 return ! none_ && dim_==3 && (topologyId_ | 1) == 0b0111;
554 }
555
557 constexpr bool isSimplex() const {
558 return ! none_ && (topologyId_ | 1) == 1;
559 }
560
562 constexpr bool isCube() const {
563 return ! none_ && ((topologyId_ ^ ((1 << dim_)-1)) >> 1 == 0);
564 }
565
567 constexpr bool isNone() const {
568 return none_;
569 }
570
572 constexpr unsigned int dim() const {
573 return dim_;
574 }
575
577 constexpr unsigned int id() const {
578 return topologyId_;
579 }
580
589 constexpr bool operator==(const GeometryType& other) const {
590 return ( ( none_ == other.none_ )
591 && ( ( none_ == true )
592 || ( ( dim_ == other.dim_ )
593 && ( (topologyId_ >> 1) == (other.topologyId_ >> 1) )
594 )
595 )
596 );
597 }
598
600 constexpr bool operator!=(const GeometryType& other) const {
601 return ! ((*this)==other);
602 }
603
605 constexpr bool operator < (const GeometryType& other) const {
606 return ( ( none_ < other.none_ )
607 || ( !( other.none_ < none_ )
608 && ( ( dim_ < other.dim_ )
609 || ( (other.dim_ == dim_)
610 && ((topologyId_ >> 1) < (other.topologyId_ >> 1) )
611 )
612 )
613 )
614 );
615 }
616
619 };
620
622 inline std::ostream& operator<< (std::ostream& s, const GeometryType& a)
623 {
624 if (a.isSimplex())
625 {
626 s << "(simplex, " << a.dim() << ")";
627 return s;
628 }
629 if (a.isCube())
630 {
631 s << "(cube, " << a.dim() << ")";
632 return s;
633 }
634 if (a.isPyramid())
635 {
636 s << "(pyramid, 3)";
637 return s;
638 }
639 if (a.isPrism())
640 {
641 s << "(prism, 3)";
642 return s;
643 }
644 if (a.isNone())
645 {
646 s << "(none, " << a.dim() << ")";
647 return s;
648 }
649 s << "(other [" << a.id() << "], " << a.dim() << ")";
650 return s;
651 }
652
653#pragma GCC diagnostic push
654#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
656 inline std::ostream& operator<< (std::ostream& s, GeometryType::BasicType type)
657 {
658 switch (type) {
659 case GeometryType::simplex :
660 s << "simplex";
661 break;
662 case GeometryType::cube :
663 s << "cube";
664 break;
665 case GeometryType::pyramid :
666 s << "pyramid";
667 break;
668 case GeometryType::prism :
669 s << "prism";
670 break;
671 case GeometryType::extended :
672 s << "other";
673 case GeometryType::none :
674 s << "none";
675 break;
676 default :
677 DUNE_THROW(Exception, "invalid GeometryType::BasicType");
678 }
679 return s;
680 }
681#pragma GCC diagnostic pop
682
683
684
686
690 namespace GeometryTypes {
691
693
696 inline constexpr GeometryType simplex(unsigned int dim)
697 {
698 return GeometryType(0,dim,false);
699 }
700
702
705 inline constexpr GeometryType cube(unsigned int dim)
706 {
707 return GeometryType(((dim>1) ? ((1 << dim) - 1) : 0),dim,false);
708 }
709
711
714 inline constexpr GeometryType none(unsigned int dim)
715 {
716 return GeometryType(0,dim,true);
717 }
718
719#ifndef __cpp_inline_variables
720 namespace {
721#endif
722
724
727 DUNE_INLINE_VARIABLE constexpr GeometryType vertex = GeometryType(0,0,false);
728
730
733 DUNE_INLINE_VARIABLE constexpr GeometryType line = GeometryType(0,1,false);
734
736
739 DUNE_INLINE_VARIABLE constexpr GeometryType triangle = simplex(2);
740
742
745 DUNE_INLINE_VARIABLE constexpr GeometryType quadrilateral = cube(2);
746
748
751 DUNE_INLINE_VARIABLE constexpr GeometryType tetrahedron = simplex(3);
752
754
757 DUNE_INLINE_VARIABLE constexpr GeometryType pyramid = GeometryType(0b0011,3,false);
758
760
763 DUNE_INLINE_VARIABLE constexpr GeometryType prism = GeometryType(0b0101,3,false);
764
766
769 DUNE_INLINE_VARIABLE constexpr GeometryType hexahedron = cube(3);
770
771#ifndef __cpp_inline_variables
772 }
773#endif
774
775 }
776
777
778} // namespace Dune
779
780// include utility header needed for deprecated makeFromVertices
781#include "utility/typefromvertexcount.hh"
782
783#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:178
Definitions of several macros that conditionally make C++ syntax available.
Dune namespace.
Definition: alignedallocator.hh:10
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.111.3 (Nov 13, 23:29, 2024)