dune-spgrid  2.7
tree.hh
Go to the documentation of this file.
1 #ifndef DUNE_GRID_SPGRID_TREE_HH
2 #define DUNE_GRID_SPGRID_TREE_HH
3 
4 #include <type_traits>
5 
6 #include <dune/grid/common/entity.hh>
7 #include <dune/grid/common/entityiterator.hh>
8 #include <dune/grid/common/intersection.hh>
9 #include <dune/grid/common/intersectioniterator.hh>
10 
15 
16 namespace Dune
17 {
18 
19  // Forward Declarations
20  // --------------------
21 
22  template< int codim, class Grid, class IsLeaf >
23  class EntityTree;
24 
25  template< class Grid, class IsLeaf >
27 
28 
29 
30  namespace __SPGrid
31  {
32 
33  // Internal Forward Declarations
34  // -----------------------------
35 
36  template< class, class >
37  class TreeIterator;
38 
39 
40 
41  // TreeIterator for Entity
42  // -----------------------
43 
44  template< int codim, int dim, class Grid, template< int, int, class > class EImpl, class IsLeaf >
45  class TreeIterator< Dune::Entity< codim, dim, Grid, EImpl >, IsLeaf >
46  {
48 
49  public:
50  typedef typename std::remove_const< Grid >::type::Traits Traits;
51 
52  static const int dimension = Traits::ReferenceCube::dimension;
53  static const int codimension = codim;
54  static const int mydimension = dimension - codimension;
55 
56  typedef typename Traits::template Codim< codimension >::Entity Entity;
57 
58  private:
59  typedef EImpl< codimension, dimension, Grid > EntityImpl;
60 
61  public:
62  typedef typename EntityImpl::EntityInfo EntityInfo;
63  typedef typename EntityImpl::GridLevel GridLevel;
64 
65  TreeIterator () = default;
66 
67  explicit TreeIterator ( const IsLeaf &isLeaf ) : isLeaf_( isLeaf ) {}
68 
69  explicit TreeIterator ( const Entity &entity, const IsLeaf &isLeaf )
70  : entityInfo_( entity.impl().entityInfo() ),
71  rootLevel_( &gridLevel() ),
72  isLeaf_( isLeaf )
73  {}
74 
75  Entity dereference () const { return EntityImpl( entityInfo() ); }
76 
77  bool equals ( const This &other ) const { return entityInfo().equals( other.entityInfo() ); }
78 
79  void increment ()
80  {
81  if( isLeaf_( dereference() ) || (&gridLevel() == &leafLevel()) )
82  {
83  while( !isDone() )
84  {
85  if( entityInfo().nextChild() )
86  return;
87  entityInfo().up();
88  }
89  entityInfo() = EntityInfo();
90  }
91  else
92  entityInfo().down();
93  }
94 
95  const EntityInfo &entityInfo () const { return entityInfo_; }
96  EntityInfo &entityInfo () { return entityInfo_; }
97 
98  const GridLevel &gridLevel () const { return entityInfo().gridLevel(); }
99 
100  private:
101  const GridLevel &leafLevel () const { return gridLevel().grid().leafLevel(); }
102 
103  bool isDone () const { return (&gridLevel() == rootLevel_); }
104 
105  EntityInfo entityInfo_;
106  const GridLevel *rootLevel_;
107  IsLeaf isLeaf_;
108  };
109 
110 
111 
112  // TreeIterator for Intersection
113  // -----------------------------
114 
115  template< class Grid, class IntersectionImpl, class IsLeaf >
116  class TreeIterator< Dune::Intersection< Grid, IntersectionImpl >, IsLeaf >
117  {
119 
120  public:
121  typedef Dune::Intersection< Grid, IntersectionImpl > Intersection;
122 
123  static const int dimension = Intersection::dimension;
124  static const int codimension = Intersection::codimension;
125  static const int mydimension = Intersection::mydimension;
126 
127  typedef typename IntersectionImpl::EntityInfo EntityInfo;
128  typedef typename IntersectionImpl::ElementInfo ElementInfo;
129  typedef typename IntersectionImpl::GridLevel GridLevel;
130 
131  TreeIterator () = default;
132 
133  explicit TreeIterator ( int face, const IsLeaf &isLeaf )
134  : intersection_( IntersectionImpl( ElementInfo(), face ) )
135  {}
136 
137  explicit TreeIterator ( const Intersection &intersection, const IsLeaf &isLeaf )
138  : intersection_( intersection ),
139  rootLevel_( &intersection.impl().gridLevel() ),
140  isLeaf_( isLeaf )
141  {}
142 
143  TreeIterator ( const This & ) = default;
144  TreeIterator ( This && ) = default;
145 
146  This &operator= ( const This & ) = default;
147  This &operator= ( This && ) = default;
148 
149  const Intersection &dereference () const { return intersection_; }
150 
151  bool equals ( const This &other ) const
152  {
153  return intersection_.impl().equals( other.intersection_.impl() );
154  }
155 
156  void increment ()
157  {
158  if( isLeaf_( intersection_ ) || (&gridLevel() == &leafLevel()) )
159  {
160  EntityInfo info = entityInfo();
161  while( !isDone( info ) )
162  {
163  if( info.nextChild() )
164  {
165  intersection_.impl().setEntityInfo( info );
166  return;
167  }
168  info.up();
169  }
170  intersection_.impl().setInside( ElementInfo() );
171  }
172  else
173  {
174  EntityInfo info = entityInfo();
175  info.down();
176  intersection_.impl().setEntityInfo( info );
177  }
178  }
179 
180  private:
181  EntityInfo entityInfo () const { return intersection_.impl().entityInfo(); }
182 
183  const GridLevel &gridLevel () const { return entityInfo().gridLevel(); }
184  const GridLevel &leafLevel () const { return gridLevel().grid().leafLevel(); }
185 
186  bool isDone ( const EntityInfo &entityInfo ) const { return (&entityInfo.gridLevel() == rootLevel_); }
187 
188  Intersection intersection_;
189  const GridLevel *rootLevel_;
190  IsLeaf isLeaf_;
191  };
192 
193  } // namespace __SPGrid
194 
195 
196 
197  // EntityTree for SPGrid
198  // ---------------------
199 
200  template< int codim, class ct, int dim, template< int > class Ref, class Comm, class IsLeaf >
201  class EntityTree< codim, SPGrid< ct, dim, Ref, Comm >, IsLeaf >
202  {
203  public:
205 
206  typedef Dune::Entity< codim, dim, const Grid, SPEntity > Entity;
207  typedef Dune::EntityIterator< codim, Grid, __SPGrid::TreeIterator< Entity, IsLeaf > > Iterator;
208 
209  EntityTree ( const Grid &grid, const Entity &entity, const IsLeaf &isLeaf )
210  : entity_( entity ), isLeaf_( isLeaf )
211  {}
212 
213  Iterator begin () const { return __SPGrid::TreeIterator< Entity, IsLeaf >( entity_, isLeaf_ ); }
214  Iterator end () const { return __SPGrid::TreeIterator< Entity, IsLeaf >( isLeaf_ ); }
215 
216  bool empty () const { return false; }
217 
218  private:
219  Entity entity_;
220  IsLeaf isLeaf_;
221  };
222 
223 
224 
225  // IntersectionTree for SPGrid
226  // ---------------------------
227 
228  template< class ct, int dim, template< int > class Ref, class Comm, class IsLeaf >
229  class IntersectionTree< SPGrid< ct, dim, Ref, Comm >, IsLeaf >
230  {
231  public:
233 
234  typedef Dune::Intersection< const Grid, SPIntersection< const Grid > > Intersection;
235  typedef Dune::IntersectionIterator< const Grid, __SPGrid::TreeIterator< Intersection, IsLeaf >, SPIntersection< const Grid > > Iterator;
236 
237  IntersectionTree ( const Grid &grid, const Intersection &intersection, const IsLeaf &isLeaf )
238  : intersection_( intersection ), isLeaf_( isLeaf )
239  {}
240 
241  Iterator begin () const { return __SPGrid::TreeIterator< Intersection, IsLeaf >( intersection_, isLeaf_ ); }
242  Iterator end () const { return __SPGrid::TreeIterator< Intersection, IsLeaf >( intersection_.indexInInside(), isLeaf_ ); }
243 
244  bool empty () const { return false; }
245 
246  private:
247  Intersection intersection_;
248  IsLeaf isLeaf_;
249  };
250 
251 } // namespace Dune
252 
253 #endif // #ifndef DUNE_GRID_SPGRID_TREE_HH
Definition: iostream.hh:7
structured, parallel DUNE grid
Definition: grid.hh:136
Definition: entityinfo.hh:24
const GridLevel & gridLevel() const
Definition: entityinfo.hh:66
Definition: grid.hh:39
const Grid & grid() const
Definition: gridlevel.hh:82
Definition: intersection.hh:37
Definition: tree.hh:23
Definition: tree.hh:26
bool equals(const This &other) const
Definition: tree.hh:77
TreeIterator(const Entity &entity, const IsLeaf &isLeaf)
Definition: tree.hh:69
std::remove_const< Grid >::type::Traits Traits
Definition: tree.hh:50
Traits::template Codim< codimension >::Entity Entity
Definition: tree.hh:56
TreeIterator(const Intersection &intersection, const IsLeaf &isLeaf)
Definition: tree.hh:137
Dune::Intersection< Grid, IntersectionImpl > Intersection
Definition: tree.hh:121
TreeIterator(int face, const IsLeaf &isLeaf)
Definition: tree.hh:133
EntityTree(const Grid &grid, const Entity &entity, const IsLeaf &isLeaf)
Definition: tree.hh:209
Dune::EntityIterator< codim, Grid, __SPGrid::TreeIterator< Entity, IsLeaf > > Iterator
Definition: tree.hh:207
SPGrid< ct, dim, Ref, Comm > Grid
Definition: tree.hh:204
Dune::Entity< codim, dim, const Grid, SPEntity > Entity
Definition: tree.hh:206
SPGrid< ct, dim, Ref, Comm > Grid
Definition: tree.hh:232
IntersectionTree(const Grid &grid, const Intersection &intersection, const IsLeaf &isLeaf)
Definition: tree.hh:237
Dune::Intersection< const Grid, SPIntersection< const Grid > > Intersection
Definition: tree.hh:234
Dune::IntersectionIterator< const Grid, __SPGrid::TreeIterator< Intersection, IsLeaf >, SPIntersection< const Grid > > Iterator
Definition: tree.hh:235