dune-grid  2.2.1
geometrygrid/entity.hh
Go to the documentation of this file.
1 #ifndef DUNE_GEOGRID_ENTITY_HH
2 #define DUNE_GEOGRID_ENTITY_HH
3 
4 #include <dune/common/nullptr.hh>
5 
6 #include <dune/geometry/referenceelements.hh>
7 
11 
12 namespace Dune
13 {
14 
15  namespace GeoGrid
16  {
17 
18  // Internal Forward Declarations
19  // -----------------------------
20 
31  template< int codim, class Grid, bool fake = !(Capabilities::hasHostEntity< Grid, codim >::v) >
32  class EntityBase;
33 
46  template< int codim, int dim, class Grid >
47  class Entity;
48 
49 
50 
51  // External Forward Declarations
52  // -----------------------------
53 
54  template< class Grid >
56 
57  template< class Grid >
59 
60  template< class Grid >
62 
63 
64 
65  // EntityBase (real)
66  // -----------------
67 
75  template< int codim, class Grid >
76  class EntityBase< codim, Grid, false >
77  {
78  typedef typename remove_const< Grid >::type::Traits Traits;
79 
80  public:
84 
85  static const int codimension = codim;
87  static const int dimension = Traits::dimension;
89  static const int mydimension = dimension - codimension;
91  static const int dimensionworld = Traits::dimensionworld;
92 
94  static const bool fake = false;
95 
101 
102  typedef typename Traits::ctype ctype;
103 
105  typedef typename Traits::template Codim< codimension >::Geometry Geometry;
108  private:
109  typedef typename Traits::HostGrid HostGrid;
110  typedef typename Traits::CoordFunction CoordFunction;
111 
112  public:
116 
117  typedef typename HostGrid::template Codim< codimension >::Entity HostEntity;
119  typedef typename HostGrid::template Codim< codimension >::EntityPointer HostEntityPointer;
120 
122  typedef typename Traits::template Codim< codimension >::EntitySeed EntitySeed;
123 
125  typedef typename HostGrid::template Codim< 0 >::Entity HostElement;
128  typedef typename Traits::template Codim< codim >::GeometryImpl GeometryImpl;
129 
130  private:
131  typedef typename HostGrid::template Codim< codimension >::Geometry HostGeometry;
132 
134 
135  public:
145  explicit EntityBase ( const Grid &grid )
146  : geo_( grid ),
147  hostEntity_( nullptr )
148  {}
149 
156  explicit EntityBase ( const GeometryImpl &geo )
157  : geo_( geo ),
158  hostEntity_( nullptr )
159  {}
160 
161  EntityBase ( const EntityBase &other )
162  : geo_( other.geo_ ),
163  hostEntity_( nullptr )
164  {}
165 
168  const EntityBase &operator= ( const EntityBase &other )
169  {
170  geo_ = other.geo_;
171  hostEntity_ = nullptr;
172  return *this;
173  }
174 
175  operator bool () const { return bool( hostEntity_ ); }
176 
177  public:
185  GeometryType type () const
186  {
187  return hostEntity().type();
188  }
189 
190  unsigned int topologyId () const DUNE_DEPRECATED
191  {
192  return type().id();
193  }
194 
196  int level () const
197  {
198  return hostEntity().level();
199  }
200 
202  PartitionType partitionType () const
203  {
204  return hostEntity().partitionType();
205  }
206 
221  Geometry geometry () const
222  {
223  if( !geo_ )
224  {
225  CoordVector coords( hostEntity(), grid().coordFunction() );
226  geo_ = GeometryImpl( grid(), type(), coords );
227  }
228  return Geometry( geo_ );
229  }
230 
232  EntitySeed seed () const { return EntitySeed( hostEntity().seed() ); }
239  const Grid &grid () const { return geo_.grid(); }
240 
241  const HostEntity &hostEntity () const
242  {
243  assert( *this );
244  return *hostEntity_;
245  }
246 
254  void initialize ( const HostEntity &hostEntity ) { hostEntity_ = &hostEntity; }
255 
263  template< class HostIndexSet >
264  typename HostIndexSet::IndexType
265  index ( const HostIndexSet &indexSet ) const
266  {
267  return indexSet.template index< codimension >( hostEntity() );
268  }
269 
279  template< class HostIndexSet >
280  typename HostIndexSet::IndexType
281  subIndex ( const HostIndexSet &indexSet, int i, unsigned int cd ) const
282  {
283  return indexSet.subIndex( hostEntity(), i, cd );
284  }
285 
293  template< class HostIndexSet >
294  bool isContained ( const HostIndexSet &indexSet ) const
295  {
296  return indexSet.contains( hostEntity() );
297  }
298 
306  template< class HostIdSet >
307  typename HostIdSet::IdType id ( const HostIdSet &idSet ) const
308  {
309  return idSet.template id< codimension >( hostEntity() );
310  }
313  private:
314  mutable GeometryImpl geo_;
315  const HostEntity *hostEntity_;
316  };
317 
318 
319 
320  // EntityBase (fake)
321  // -----------------
322 
330  template< int codim, class Grid >
331  class EntityBase< codim, Grid, true >
332  {
333  typedef typename remove_const< Grid >::type::Traits Traits;
334 
335  public:
339 
340  static const int codimension = codim;
342  static const int dimension = Traits::dimension;
344  static const int mydimension = dimension - codimension;
346  static const int dimensionworld = Traits::dimensionworld;
347 
349  static const bool fake = true;
355 
356  typedef typename Traits::ctype ctype;
357 
359  typedef typename Traits::template Codim< codimension >::Geometry Geometry;
362  private:
363  typedef typename Traits::HostGrid HostGrid;
364  typedef typename Traits::CoordFunction CoordFunction;
365 
366  public:
370 
371  typedef typename HostGrid::template Codim< codimension >::Entity HostEntity;
373  typedef typename HostGrid::template Codim< codimension >::EntityPointer HostEntityPointer;
374 
376  typedef typename Traits::template Codim< codimension >::EntitySeed EntitySeed;
377 
379  typedef typename HostGrid::template Codim< 0 >::Entity HostElement;
382  typedef typename Traits::template Codim< codimension >::GeometryImpl GeometryImpl;
383 
384  private:
385  typedef typename HostGrid::template Codim< 0 >::Geometry HostGeometry;
386  typedef typename HostGrid::template Codim< dimension >::EntityPointer HostVertexPointer;
387 
389 
390  public:
401  EntityBase ( const Grid &grid, int subEntity )
402  : geo_( grid ),
403  hostElement_( nullptr ),
404  subEntity_( subEntity )
405  {}
406 
414  EntityBase ( const GeometryImpl &geo, int subEntity )
415  : geo_( geo ),
416  hostElement_( nullptr ),
417  subEntity_( subEntity )
418  {}
419 
420  EntityBase ( const EntityBase &other )
421  : geo_( other.geo_ ),
422  hostElement_( nullptr ),
423  subEntity_( other.subEntity_ )
424  {}
425 
428  const EntityBase &operator= ( const EntityBase &other )
429  {
430  geo_ = other.geo_;
431  hostElement_ = nullptr;
432  subEntity_ = other.subEntity_;
433  return *this;
434  }
435 
436  operator bool () const { return bool( hostElement_ ); }
437 
445  GeometryType type () const
446  {
447  const GenericReferenceElement< ctype, dimension > &refElement
448  = GenericReferenceElements< ctype, dimension >::general( hostElement().type() );
449  return refElement.type( subEntity_, codimension );
450  }
451 
452  unsigned int topologyId () const DUNE_DEPRECATED
453  {
454  const GenericReferenceElement< ctype, dimension > &refElement
455  = GenericReferenceElements< ctype, dimension >::general( hostElement().type() );
456  return refElement.topologyId( subEntity_, codimension );
457  }
458 
460  int level () const
461  {
462  return hostElement().level();
463  }
464 
466  PartitionType partitionType () const
467  {
469  return InteriorEntity;
470 
471  const GenericReferenceElement< ctype, dimension > &refElement
472  = GenericReferenceElements< ctype, dimension >::general( hostElement().type() );
473 
474  PartitionType type = vertexPartitionType( refElement, 0 );
475  if( (type != BorderEntity) && (type != FrontEntity) )
476  return type;
477 
478  const int numVertices = refElement.size( subEntity_, codimension, dimension );
479  for( int i = 1; i < numVertices; ++i )
480  {
481  PartitionType vtxType = vertexPartitionType( refElement, i );
482  if( (vtxType != BorderEntity) && (vtxType != FrontEntity) )
483  return vtxType;
484  if( type != vtxType )
485  return OverlapEntity;
486  }
487  assert( (type == BorderEntity) || (type == FrontEntity) );
488  return type;
489  }
490 
505  Geometry geometry () const
506  {
507  if( !geo_ )
508  {
509  CoordVector coords( hostElement(), subEntity_, grid().coordFunction() );
510  geo_ = GeometryImpl( grid(), type(), coords );
511  }
512  return Geometry( geo_ );
513  }
514 
516  EntitySeed seed () const { return EntitySeed( hostElement().seed(), subEntity_ ); }
522  const Grid &grid () const { return geo_.grid(); }
523 
524  const HostEntity &hostEntity () const
525  {
526  DUNE_THROW( NotImplemented, "HostGrid has no entities of codimension " << codimension << "." );
527  }
528 
529  const HostElement &hostElement () const
530  {
531  assert( *this );
532  return *hostElement_;
533  }
534 
535  int subEntity () const { return subEntity_; }
536 
544  void initialize ( const HostElement &hostElement ) { hostElement_ = &hostElement; }
545 
553  template< class HostIndexSet >
554  typename HostIndexSet::IndexType index ( const HostIndexSet &indexSet ) const
555  {
556  return indexSet.subIndex( hostElement(), subEntity_, codimension );
557  }
558 
568  template< class HostIndexSet >
569  typename HostIndexSet::IndexType
570  subIndex ( const HostIndexSet &indexSet, int i, unsigned int cd ) const
571  {
572  const GenericReferenceElement< ctype, dimension > &refElement
573  = GenericReferenceElements< ctype, dimension >::general( hostElement().type() );
574  const int j = refElement.subEntity( subEntity_, codimension, i, codimension+cd );
575  return indexSet.subIndex( hostElement(), j, codimension+cd );
576  }
577 
585  template< class HostIndexSet >
586  bool isContained ( const HostIndexSet &indexSet ) const
587  {
588  return indexSet.contains( hostElement() );
589  }
590 
598  template< class HostIdSet >
599  typename HostIdSet::IdType id ( const HostIdSet &idSet ) const
600  {
601  return idSet.subId( hostElement(), subEntity_, codimension );
602  }
605  private:
607  vertexPartitionType ( const GenericReferenceElement< ctype, dimension > &refElement, int i ) const
608  {
609  const int j = refElement.subEntity( subEntity_, codimension, 0, dimension );
610  return hostElement().template subEntity< dimension >( j )->partitionType();
611  }
612 
613  private:
614  mutable GeometryImpl geo_;
615  const HostElement *hostElement_;
616  unsigned int subEntity_;
617  };
618 
619 
620 
621  // Entity
622  // ------
623 
624  template< int codim, int dim, class Grid >
625  class Entity
626  : public EntityBase< codim, Grid >
627  {
628  typedef EntityBase< codim, Grid > Base;
629 
630  public:
631  typedef typename Base::HostEntity HostEntity;
632  typedef typename Base::HostElement HostElement;
633  typedef typename Base::GeometryImpl GeometryImpl;
634 
635  explicit Entity ( const Grid &grid )
636  : Base( grid )
637  {}
638 
639  explicit Entity ( const GeometryImpl &geo )
640  : Base( geo )
641  {}
642 
643  Entity ( const Grid &grid, int subEntity )
644  : Base( grid, subEntity )
645  {}
646 
647  Entity ( const GeometryImpl &geo, int subEntity )
648  : Base( geo, subEntity )
649  {}
650  };
651 
652 
653 
654  // Entity for codimension 0
655  // ------------------------
656 
657  template< int dim, class Grid >
658  class Entity< 0, dim, Grid >
659  : public EntityBase< 0, Grid >
660  {
661  typedef EntityBase< 0, Grid > Base;
662 
663  typedef typename remove_const< Grid >::type::Traits Traits;
664 
665  public:
669 
670  static const int codimension = Base::codimension;
672  static const int dimension = Base::dimension;
674  static const int mydimension = Base::mydimension;
676  static const int dimensionworld = Base::dimensionworld;
677 
679  static const bool fake = Base::fake;
685 
686  typedef typename Traits::template Codim< codimension >::LocalGeometry LocalGeometry;
688  typedef typename Traits::template Codim< codimension >::EntityPointer EntityPointer;
689 
691  typedef typename Traits::HierarchicIterator HierarchicIterator;
693  typedef typename Traits::LeafIntersectionIterator LeafIntersectionIterator;
695  typedef typename Traits::LevelIntersectionIterator LevelIntersectionIterator;
696 
699  typedef typename Base::HostEntity HostEntity;
700  typedef typename Base::HostElement HostElement;
701  typedef typename Base::GeometryImpl GeometryImpl;
702 
703  using Base::grid;
704  using Base::hostEntity;
705 
706  explicit Entity ( const Grid &grid )
707  : Base( grid )
708  {}
709 
710  explicit Entity ( const GeometryImpl &geo )
711  : Base( geo )
712  {}
713 
714  template< int codim >
715  int count () const
716  {
717  return hostEntity().template count< codim >();
718  }
719 
720  template< int codim >
721  typename Grid::template Codim< codim >::EntityPointer
722  subEntity ( int i ) const
723  {
724  typedef typename Traits::template Codim< codim >::EntityPointerImpl EntityPointerImpl;
725  return EntityPointerImpl( grid(), hostEntity(), i );
726  }
727 
728  LevelIntersectionIterator ilevelbegin () const
729  {
730  typedef GeoGrid::LevelIntersectionIterator< Grid > LevelIntersectionIteratorImpl;
731  return LevelIntersectionIteratorImpl( *this, hostEntity().ilevelbegin() );
732  }
733 
734  LevelIntersectionIterator ilevelend () const
735  {
736  typedef GeoGrid::LevelIntersectionIterator< Grid > LevelIntersectionIteratorImpl;
737  return LevelIntersectionIteratorImpl( *this, hostEntity().ilevelend() );
738  }
739 
740  LeafIntersectionIterator ileafbegin () const
741  {
742  typedef GeoGrid::LeafIntersectionIterator< Grid > LeafIntersectionIteratorImpl;
743  return LeafIntersectionIteratorImpl( *this, hostEntity().ileafbegin() );
744  }
745 
746  LeafIntersectionIterator ileafend () const
747  {
748  typedef GeoGrid::LeafIntersectionIterator< Grid > LeafIntersectionIteratorImpl;
749  return LeafIntersectionIteratorImpl( *this, hostEntity().ileafend() );
750  }
751 
752  bool hasBoundaryIntersections () const
753  {
754  return hostEntity().hasBoundaryIntersections();
755  }
756 
757  bool isLeaf () const
758  {
759  return hostEntity().isLeaf();
760  }
761 
762  EntityPointer father () const
763  {
764  typedef typename Traits::template Codim< 0 >::EntityPointerImpl EntityPointerImpl;
765  return EntityPointerImpl( grid(), hostEntity().father() );
766  }
767 
768  bool hasFather () const
769  {
770  return hostEntity().hasFather();
771  }
772 
773  LocalGeometry geometryInFather () const
774  {
775  return hostEntity().geometryInFather();
776  }
777 
778  HierarchicIterator hbegin ( int maxLevel ) const
779  {
780  typedef GeoGrid::HierarchicIterator< Grid > HierarchicIteratorImpl;
781  return HierarchicIteratorImpl( grid(), hostEntity().hbegin( maxLevel ) );
782  }
783 
784  HierarchicIterator hend ( int maxLevel ) const
785  {
786  typedef GeoGrid::HierarchicIterator< Grid > HierarchicIteratorImpl;
787  return HierarchicIteratorImpl( grid(), hostEntity().hend( maxLevel ) );
788  }
789 
790  bool isRegular () const
791  {
792  return hostEntity().isRegular();
793  }
794 
795  bool isNew () const
796  {
797  return hostEntity().isNew();
798  }
799 
800  bool mightVanish () const
801  {
802  return hostEntity().mightVanish();
803  }
804  };
805 
806  } // namespace GeoGrid
807 
808 } // namespace Dune
809 
810 #endif // #ifndef DUNE_GEOGRID_ENTITY_HH