DOLFIN
DOLFIN C++ interface
MeshEntityIterator.h
1 // Copyright (C) 2006-2008 Anders Logg
2 //
3 // This file is part of DOLFIN.
4 //
5 // DOLFIN is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU Lesser General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // DOLFIN is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public License
16 // along with DOLFIN. If not, see <http://www.gnu.org/licenses/>.
17 //
18 // Modified by Andre Massing 2009
19 //
20 // First added: 2006-05-09
21 // Last changed: 2014-07-02
22 
23 #ifndef __MESH_ENTITY_ITERATOR_H
24 #define __MESH_ENTITY_ITERATOR_H
25 
26 #include "Mesh.h"
27 #include "MeshEntity.h"
28 
29 namespace dolfin
30 {
31 
63 
65  {
66  public:
67 
69  MeshEntityIterator() : _pos(0), pos_end(0), index(0) {}
70 
72  MeshEntityIterator(const Mesh& mesh, std::size_t dim)
73  : _entity(), _pos(0), pos_end(0), index(0)
74  {
75  // Check if mesh is empty
76  if (mesh.num_vertices() == 0)
77  return;
78 
79  // Initialize mesh entity
80  _entity.init(mesh, dim, 0);
81 
82  mesh.init(dim);
83  // End at ghost cells for normal iterator
84  pos_end = mesh.topology().ghost_offset(dim);
85  }
86 
89  MeshEntityIterator(const Mesh& mesh, std::size_t dim, std::string opt)
90  : _entity(), _pos(0), pos_end(0), index(0)
91  {
92  // Check if mesh is empty
93  if (mesh.num_vertices() == 0)
94  return;
95 
96  // Initialize mesh entity
97  _entity.init(mesh, dim, 0);
98  mesh.init(dim);
99 
100  pos_end = mesh.topology().size(dim);
101  if (opt == "regular")
102  pos_end = mesh.topology().ghost_offset(dim);
103  else if (opt == "ghost")
104  _pos = mesh.topology().ghost_offset(dim);
105  else if (opt != "all")
106  dolfin_error("MeshEntityIterator.h",
107  "initialize MeshEntityIterator",
108  "unknown opt=\"%s\", choose from "
109  "opt=[\"regular\", \"ghost\", \"all\"]", opt.c_str());
110  }
111 
114  MeshEntityIterator(const MeshEntity& entity, std::size_t dim)
115  : _entity(entity.mesh(), dim, 0), _pos(0), index(0)
116  {
117  // Get connectivity
118  const MeshConnectivity& c = entity.mesh().topology()(entity.dim(), dim);
119 
120  // Compute connectivity if empty
121  if (c.empty())
122  entity.mesh().init(entity.dim(), dim);
123 
124  // Get size and index map
125  if (c.empty())
126  {
127  pos_end = 0;
128  index = 0;
129  }
130  else
131  {
132  pos_end = c.size(entity.index());
133  index = c(entity.index());
134  }
135  }
136 
139  : _entity(it._entity), _pos(it._pos), pos_end(it.pos_end),
140  index(it.index) {}
141 
143  virtual ~MeshEntityIterator() {}
144 
147  {
148  ++_pos;
149  return *this;
150  }
151 
154  {
155  --_pos;
156  return *this;
157  }
158 
160  std::size_t pos() const
161  { return _pos; }
162 
164  bool operator==(const MeshEntityIterator& it) const
165  {
166  // Use const_cast to use operator* inside comparison, which
167  // automatically updates the entity index corresponding to pos
168  // *before* comparison (since update of entity delays until
169  // request for entity)
170  return ((const_cast<MeshEntityIterator *>(this))->operator*()
171  == (const_cast<MeshEntityIterator *>(&it))->operator*()
172  && _pos == it._pos && index == it.index);
173  }
174 
176  bool operator!=(const MeshEntityIterator & it) const
177  { return !operator==(it); }
178 
181  { return *operator->(); }
182 
185  { _entity._local_index = (index ? index[_pos] : _pos); return &_entity; }
186 
188  bool end() const
189  { return _pos >= pos_end; }
190 
196  {
198  sg(*this);
199  sg.set_end();
200  return sg;
201  }
202 
203  // Note: Not a subclass of Variable for efficiency!
204  // Commented out to avoid warning about shadowing str() for MeshEntity
205  // Return informal string representation (pretty-print)
206  // std::string str(bool verbose) const;
207 
208  private:
209 
210  // Set pos to end position. To create a kind of mesh.end() iterator.
211  void set_end()
212  { _pos = pos_end; }
213 
214  // Mesh entity
215  MeshEntity _entity;
216 
217  // Current position
218  std::size_t _pos;
219 
220  // End position
221  std::size_t pos_end;
222 
223  // Mapping from pos to index (if any)
224  const unsigned int* index;
225 
226  };
227 
228 }
229 
230 #endif
MeshEntityIterator end_iterator()
Definition: MeshEntityIterator.h:195
MeshEntityIterator(const MeshEntity &entity, std::size_t dim)
Definition: MeshEntityIterator.h:114
MeshEntity & operator*()
Dereference operator.
Definition: MeshEntityIterator.h:180
MeshEntityIterator(const MeshEntityIterator &it)
Copy constructor.
Definition: MeshEntityIterator.h:138
std::size_t init(std::size_t dim) const
Definition: Mesh.cpp:138
MeshEntity * operator->()
Member access operator.
Definition: MeshEntityIterator.h:184
MeshEntityIterator & operator++()
Step to next mesh entity (prefix increment)
Definition: MeshEntityIterator.h:146
std::size_t size(std::size_t dim) const
Return number of entities for given dimension.
Definition: MeshTopology.cpp:75
Definition: adapt.h:29
std::size_t ghost_offset(std::size_t dim) const
Definition: MeshTopology.cpp:93
std::size_t pos() const
Return current position.
Definition: MeshEntityIterator.h:160
Definition: MeshConnectivity.h:39
std::size_t dim() const
Definition: MeshEntity.h:106
std::size_t size() const
Return total number of connections.
Definition: MeshConnectivity.h:60
MeshTopology & topology()
Definition: Mesh.h:219
Definition: MeshEntityIterator.h:64
std::size_t num_vertices() const
Definition: Mesh.h:134
bool operator!=(const MeshEntityIterator &it) const
Comparison operator.
Definition: MeshEntityIterator.h:176
bool end() const
Check if iterator has reached the end.
Definition: MeshEntityIterator.h:188
void init(const Mesh &mesh, std::size_t dim, std::size_t index)
Definition: MeshEntity.cpp:39
bool operator==(const MeshEntityIterator &it) const
Comparison operator.
Definition: MeshEntityIterator.h:164
Definition: MeshEntity.h:42
MeshEntityIterator(const Mesh &mesh, std::size_t dim)
Create iterator for mesh entities over given topological dimension.
Definition: MeshEntityIterator.h:72
const Mesh & mesh() const
Definition: MeshEntity.h:99
void dolfin_error(std::string location, std::string task, std::string reason,...)
Definition: log.cpp:129
MeshEntityIterator()
Default constructor.
Definition: MeshEntityIterator.h:69
MeshEntityIterator(const Mesh &mesh, std::size_t dim, std::string opt)
Definition: MeshEntityIterator.h:89
std::size_t index() const
Definition: MeshEntity.h:113
MeshEntityIterator & operator--()
Step to the previous mesh entity (prefix decrease)
Definition: MeshEntityIterator.h:153
virtual ~MeshEntityIterator()
Destructor.
Definition: MeshEntityIterator.h:143
bool empty() const
Return true if the total number of connections is equal to zero.
Definition: MeshConnectivity.h:56
Definition: Mesh.h:82