Loading...
Searching...
No Matches
MeshEntityIterator.h
// Copyright (C) 2006-2008 Anders Logg
//
// This file is part of DOLFIN.
//
// DOLFIN is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// DOLFIN is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with DOLFIN. If not, see <http://www.gnu.org/licenses/>.
//
// Modified by Andre Massing 2009
//
// First added: 2006-05-09
// Last changed: 2014-07-02
#ifndef __MESH_ENTITY_ITERATOR_H
#define __MESH_ENTITY_ITERATOR_H
#include "Mesh.h"
#include "MeshEntity.h"
namespace dolfin
{
class MeshEntityIterator
{
public:
MeshEntityIterator() : _pos(0), pos_end(0), index(0) {}
MeshEntityIterator(const Mesh& mesh, std::size_t dim)
: _entity(), _pos(0), pos_end(0), index(0)
{
// Check if mesh is empty
if (mesh.num_vertices() == 0)
return;
// Initialize mesh entity
_entity.init(mesh, dim, 0);
mesh.init(dim);
// End at ghost cells for normal iterator
pos_end = mesh.topology().ghost_offset(dim);
}
MeshEntityIterator(const Mesh& mesh, std::size_t dim, std::string opt)
: _entity(), _pos(0), pos_end(0), index(0)
{
// Check if mesh is empty
if (mesh.num_vertices() == 0)
return;
// Initialize mesh entity
_entity.init(mesh, dim, 0);
mesh.init(dim);
pos_end = mesh.topology().size(dim);
if (opt == "regular")
pos_end = mesh.topology().ghost_offset(dim);
else if (opt == "ghost")
_pos = mesh.topology().ghost_offset(dim);
else if (opt != "all")
dolfin_error("MeshEntityIterator.h",
"initialize MeshEntityIterator",
"unknown opt=\"%s\", choose from "
"opt=[\"regular\", \"ghost\", \"all\"]", opt.c_str());
}
MeshEntityIterator(const MeshEntity& entity, std::size_t dim)
: _entity(entity.mesh(), dim, 0), _pos(0), index(0)
{
// Get connectivity
const MeshConnectivity& c = entity.mesh().topology()(entity.dim(), dim);
// Compute connectivity if empty
if (c.empty())
entity.mesh().init(entity.dim(), dim);
// Get size and index map
if (c.empty())
{
pos_end = 0;
index = 0;
}
else
{
pos_end = c.size(entity.index());
index = c(entity.index());
}
}
MeshEntityIterator(const MeshEntityIterator& it)
: _entity(it._entity), _pos(it._pos), pos_end(it.pos_end),
index(it.index) {}
virtual ~MeshEntityIterator() {}
MeshEntityIterator& operator++()
{
++_pos;
return *this;
}
MeshEntityIterator& operator--()
{
--_pos;
return *this;
}
std::size_t pos() const
{ return _pos; }
bool operator==(const MeshEntityIterator& it) const
{
// Use const_cast to use operator* inside comparison, which
// automatically updates the entity index corresponding to pos
// *before* comparison (since update of entity delays until
// request for entity)
return ((const_cast<MeshEntityIterator *>(this))->operator*()
== (const_cast<MeshEntityIterator *>(&it))->operator*()
&& _pos == it._pos && index == it.index);
}
bool operator!=(const MeshEntityIterator & it) const
{ return !operator==(it); }
MeshEntity& operator*()
{ return *operator->(); }
MeshEntity* operator->()
{ _entity._local_index = (index ? index[_pos] : _pos); return &_entity; }
bool end() const
{ return _pos >= pos_end; }
MeshEntityIterator end_iterator()
{
MeshEntityIterator
sg(*this);
sg.set_end();
return sg;
}
// Note: Not a subclass of Variable for efficiency!
// Commented out to avoid warning about shadowing str() for MeshEntity
// Return informal string representation (pretty-print)
// std::string str(bool verbose) const;
private:
// Set pos to end position. To create a kind of mesh.end() iterator.
void set_end()
{ _pos = pos_end; }
// Mesh entity
MeshEntity _entity;
// Current position
std::size_t _pos;
// End position
std::size_t pos_end;
// Mapping from pos to index (if any)
const unsigned int* index;
};
}
#endif
dolfin::MeshConnectivity
Definition MeshConnectivity.h:42
dolfin::MeshConnectivity::empty
bool empty() const
Return true if the total number of connections is equal to zero.
Definition MeshConnectivity.h:58
dolfin::MeshEntityIterator::MeshEntityIterator
MeshEntityIterator(const MeshEntity &entity, std::size_t dim)
Definition MeshEntityIterator.h:114
dolfin::MeshEntityIterator::MeshEntityIterator
MeshEntityIterator()
Default constructor.
Definition MeshEntityIterator.h:69
dolfin::MeshEntityIterator::MeshEntityIterator
MeshEntityIterator(const MeshEntityIterator &it)
Copy constructor.
Definition MeshEntityIterator.h:138
dolfin::MeshEntityIterator::operator++
MeshEntityIterator & operator++()
Step to next mesh entity (prefix increment).
Definition MeshEntityIterator.h:146
dolfin::MeshEntityIterator::end_iterator
MeshEntityIterator end_iterator()
Definition MeshEntityIterator.h:195
dolfin::MeshEntityIterator::operator==
bool operator==(const MeshEntityIterator &it) const
Comparison operator.
Definition MeshEntityIterator.h:164
dolfin::MeshEntityIterator::MeshEntityIterator
MeshEntityIterator(const Mesh &mesh, std::size_t dim)
Create iterator for mesh entities over given topological dimension.
Definition MeshEntityIterator.h:72
dolfin::MeshEntityIterator::end
bool end() const
Check if iterator has reached the end.
Definition MeshEntityIterator.h:188
dolfin::MeshEntityIterator::operator*
MeshEntity & operator*()
Dereference operator.
Definition MeshEntityIterator.h:180
dolfin::MeshEntityIterator::~MeshEntityIterator
virtual ~MeshEntityIterator()
Destructor.
Definition MeshEntityIterator.h:143
dolfin::MeshEntityIterator::MeshEntityIterator
MeshEntityIterator(const Mesh &mesh, std::size_t dim, std::string opt)
Definition MeshEntityIterator.h:89
dolfin::MeshEntityIterator::operator->
MeshEntity * operator->()
Member access operator.
Definition MeshEntityIterator.h:184
dolfin::MeshEntityIterator::pos
std::size_t pos() const
Return current position.
Definition MeshEntityIterator.h:160
dolfin::MeshEntityIterator::operator!=
bool operator!=(const MeshEntityIterator &it) const
Comparison operator.
Definition MeshEntityIterator.h:176
dolfin::MeshEntityIterator::operator--
MeshEntityIterator & operator--()
Step to the previous mesh entity (prefix decrease).
Definition MeshEntityIterator.h:153
dolfin::MeshEntity
Definition MeshEntity.h:43
dolfin::MeshEntity::mesh
const Mesh & mesh() const
Definition MeshEntity.h:99
dolfin::MeshEntity::dim
std::size_t dim() const
Definition MeshEntity.h:106
dolfin::MeshTopology::size
std::size_t size(std::size_t dim) const
Return number of entities for given dimension.
Definition MeshTopology.cpp:79
dolfin::MeshTopology::ghost_offset
std::size_t ghost_offset(std::size_t dim) const
Definition MeshTopology.cpp:97
dolfin::Mesh
Definition Mesh.h:84
dolfin::Mesh::init
std::size_t init(std::size_t dim) const
Definition Mesh.cpp:139
dolfin::Mesh::topology
MeshTopology & topology()
Definition Mesh.h:220
dolfin::Mesh::num_vertices
std::size_t num_vertices() const
Definition Mesh.h:135
dolfin
Definition adapt.h:30
dolfin::dolfin_error
void dolfin_error(std::string location, std::string task, std::string reason,...)
Definition log.cpp:129