1 #ifndef DUNE_POLYGONGRID_ENTITY_HH
2 #define DUNE_POLYGONGRID_ENTITY_HH
3 
4 #include <cassert>
5 
6 #include <exception>
7 #include <type_traits>
8 
9 #include <dune/grid/common/entity.hh>
10 
11 #include <dune/geometry/dimension.hh>
12 
13 #include <dune/grid/common/entity.hh>
14 #include <dune/grid/common/entityiterator.hh>
15 
16 #include <dune/polygongrid/entityseed.hh>
17 #include <dune/polygongrid/geometry.hh>
18 #include <dune/polygongrid/subentity.hh>
19 
20 namespace Dune
21 {
22 
23   namespace __PolygonGrid
24   {
25 
26     // Internal Forward Declarations
27     // -----------------------------
28 
29     template< int codim, int dim, class Grid >
30     class Entity;
31 
32 
33 
34     // External Forward Declarations
35     // -----------------------------
36 
37     template< int codim, class Grid >
38     class EntityIterator;
39 
40 
41 
42     // BasicEntity
43     // -----------
44 
45     template< int codim, class Item, class Grid >
46     class BasicEntity
47     {
48       typedef BasicEntity< codim, Item, Grid > This;
49 
50       typedef __PolygonGrid::EntitySeed< typename Item::Index, codim > EntitySeedImpl;
51       typedef __PolygonGrid::Geometry< 2 - codim, 2, Grid > GeometryImpl;
52 
53       typedef typename std::remove_const< Grid >::type::ctype ctype;
54 
55     public:
56       static const int codimension = codim;
57       static const int dimension = 2;
58       static const int mydimension = dimension - codimension;
59 
60       template< int cd >
61       struct Codim
62       {
63         typedef Dune::Entity< cd, 2, Grid, __PolygonGrid::Entity > Entity;
64       };
65 
66       typedef Dune::EntitySeed< Grid, EntitySeedImpl > EntitySeed;
67       typedef Dune::Geometry< 2 - codim, 2, Grid, __PolygonGrid::Geometry > Geometry;
68 
BasicEntity(const Item & item)69       explicit BasicEntity ( const Item &item ) : item_( item ) {}
70       BasicEntity () noexcept = default;
71 
type() const72       GeometryType type () const { return GeometryTypes::none( mydimension ); }
73 
partitionType() const74       PartitionType partitionType () const { return InteriorEntity; }
75 
geometry() const76       Geometry geometry () const { return Geometry( GeometryImpl( item() ) ); }
77 
seed() const78       EntitySeed seed () const { return EntitySeedImpl( item().index() ); }
79 
level() const80       int level () const noexcept { return 0; }
81 
equals(const This & other) const82       bool equals ( const This &other ) const { return (item_ == other.item_); }
83 
84       template< int cd >
subEntity(int i) const85       typename Codim< cd >::Entity subEntity ( int i ) const
86       {
87         typedef __PolygonGrid::Entity< cd, 2, Grid > EntityImpl;
88         return EntityImpl( __PolygonGrid::subEntity( item(), Dune::Codim< cd - codimension >(), i ) );
89       }
90 
index() const91       std::size_t index () const { return item().uniqueIndex(); }
92 
item() const93       const Item &item () const { return item_; }
94 
95     protected:
96       Item item_;
97     };
98 
99 
100 
101     // Entity for Codimension 2
102     // ------------------------
103 
104     template< int dim, class Grid >
105     class Entity< 2, dim, Grid >
106       : public BasicEntity< 2, Node< typename std::remove_const< Grid >::type::ctype >, Grid >
107     {
108       typedef Entity< 2, dim, Grid > This;
109       typedef BasicEntity< 2, Node< typename std::remove_const< Grid >::type::ctype >, Grid > Base;
110 
111       typedef Node< typename std::remove_const< Grid >::type::ctype > Item;
112 
113     public:
114       using Base::item;
115 
Entity(const Item & item)116       explicit Entity ( const Item &item ) : Base( item ) {}
117       Entity () noexcept = default;
118 
subEntities(int codim) const119       std::size_t subEntities ( int codim ) const noexcept { return (codim == 2 ? 1u : 0u); }
120 
subIndex(int codim,std::size_t i) const121       std::size_t subIndex ( int codim, std::size_t i ) const noexcept
122       {
123         assert( i < subEntities( codim ) );
124         return __PolygonGrid::subEntity( item(), Dune::Codim< 0 >(), i ).uniqueIndex();
125       }
126     };
127 
128 
129 
130     // Entity for Codimension 1
131     // ------------------------
132 
133     template< int dim, class Grid >
134     class Entity< 1, dim, Grid >
135       : public BasicEntity< 1, HalfEdge< typename std::remove_const< Grid >::type::ctype >, Grid >
136     {
137       typedef Entity<1, dim, Grid > This;
138       typedef BasicEntity< 1, HalfEdge< typename std::remove_const< Grid >::type::ctype >, Grid > Base;
139 
140       typedef HalfEdge< typename std::remove_const< Grid >::type::ctype > Item;
141 
142     public:
143       using Base::item;
144 
Entity(const Item & item)145       explicit Entity ( const Item &item ) : Base( item ) {}
146       Entity () noexcept = default;
147 
subEntities(int codim) const148       std::size_t subEntities ( int codim ) const noexcept { return (codim == 2 ? 2u : (codim == 1 ? 1u : 0u)); }
149 
subIndex(int codim,std::size_t i) const150       std::size_t subIndex ( int codim, std::size_t i ) const noexcept
151       {
152         assert( i < subEntities( codim ) );
153         if( codim == 2 )
154           return __PolygonGrid::subEntity( item(), Dune::Codim< 0 >(), i ).uniqueIndex();
155         else
156           return __PolygonGrid::subEntity( item(), Dune::Codim< 1 >(), i ).uniqueIndex();
157       }
158     };
159 
160 
161 
162     // Entity for codimension 0
163     // ------------------------
164 
165     template< int dim, class Grid >
166     class Entity< 0, dim, Grid >
167       : public BasicEntity< 0, Node< typename std::remove_const< Grid >::type::ctype >, Grid >
168     {
169       typedef Entity< 0, dim, Grid > This;
170       typedef BasicEntity< 0, Node< typename std::remove_const< Grid >::type::ctype >, Grid > Base;
171 
172       typedef Node< typename std::remove_const< Grid >::type::ctype > Item;
173 
174       typedef __PolygonGrid::EntityIterator< 0, Grid > HierarchicIteratorImpl;
175 
176     public:
177       typedef typename Base::Geometry LocalGeometry;
178 
179       typedef Dune::EntityIterator< 0, Grid, HierarchicIteratorImpl > HierarchicIterator;
180 
181       using Base::item;
182 
Entity(const Item & item)183       explicit Entity ( const Item &item ) : Base( item ) {}
184       Entity () noexcept = default;
185 
subEntities(int codim) const186       unsigned int subEntities ( int codim ) const noexcept
187       {
188         return ((codim == 1) || (codim == 2) ? item().halfEdges().size() : (codim == 0u ? 1u : 0u));
189       }
190 
191       template< int codim >
count() const192       int count () const
193       {
194         return subEntities( codim );
195       }
196 
subIndex(int codim,std::size_t i) const197       std::size_t subIndex ( int codim, std::size_t i ) const noexcept
198       {
199         assert( i < subEntities( codim ) );
200         switch( codim )
201         {
202         case 0:
203           return __PolygonGrid::subEntity( item(), Dune::Codim< 0 >(), i ).uniqueIndex();
204 
205         case 1:
206           return __PolygonGrid::subEntity( item(), Dune::Codim< 1 >(), i ).uniqueIndex();
207 
208         case 2:
209           return __PolygonGrid::subEntity( item(), Dune::Codim< 2 >(), i ).uniqueIndex();
210 
211         default:
212           std::terminate();
213         }
214       }
215 
isLeaf() const216       bool isLeaf () const noexcept { return true; }
217 
father() const218       Dune::Entity< 0, 2, Grid, __PolygonGrid::Entity > father () const { assert( hasFather() ); std::terminate(); }
219 
hasFather() const220       bool hasFather () const { return false; }
221 
geometryInFather() const222       LocalGeometry geometryInFather () const { assert( hasFather() ); std::terminate(); }
223 
hbegin(int maxLevel) const224       HierarchicIterator hbegin ( int maxLevel ) const { return HierarchicIteratorImpl(); }
hend(int maxLevel) const225       HierarchicIterator hend ( int maxLevel ) const { return HierarchicIteratorImpl(); }
226 
isRegular() const227       bool isRegular () const { return true; }
isNew() const228       bool isNew () const { return false; }
mightVanish() const229       bool mightVanish () const { return false; }
230 
hasBoundaryIntersections() const231       bool hasBoundaryIntersections () const
232       {
233         bool hasBoundaryIntersections = false;
234         for( const auto halfEdge : item().halfEdges() )
235           hasBoundaryIntersections |= !halfEdge.neighbor().regular();
236         return hasBoundaryIntersections;
237       }
238     };
239 
240  } // namespace __PolygonGrid
241 
242 } // namespace Dune
243 
244 #endif // #ifndef DUNE_POLYGONGRID_ENTITY_HH
245