Commit d3c38202 authored by Hans-Christian Ebke's avatar Hans-Christian Ebke
Browse files

Consolidated iterator code. Functionally equivalent but way cleaner than before.


git-svn-id: http://www.openmesh.org/svnrepo/OpenMesh/trunk@548 fdac6126-5c0c-442c-9429-916003d36597
parent ddcf2f46
...@@ -77,523 +77,117 @@ template <class Mesh> class ConstFaceIterT; ...@@ -77,523 +77,117 @@ template <class Mesh> class ConstFaceIterT;
template <class Mesh> class FaceIterT; template <class Mesh> class FaceIterT;
template <class Mesh, class ValueHandle>
class GenericIteratorT {
//== CLASS DEFINITION =========================================================
/** \class VertexIterT IteratorsT.hh <OpenMesh/Mesh/Iterators/IteratorsT.hh>
Linear iterator.
*/
template <class Mesh>
class ConstVertexIterT
{
public:
//--- Typedefs ---
typedef typename Mesh::VertexHandle value_handle;
typedef value_handle value_type;
typedef std::bidirectional_iterator_tag iterator_category;
typedef std::ptrdiff_t difference_type;
typedef const value_type& reference;
typedef const value_type* pointer;
typedef const Mesh* mesh_ptr;
typedef const Mesh& mesh_ref;
/// Default constructor.
ConstVertexIterT()
: mesh_(0), skip_bits_(0)
{}
/// Construct with mesh and a target handle.
ConstVertexIterT(mesh_ref _mesh, value_handle _hnd, bool _skip=false)
: mesh_(&_mesh), hnd_(_hnd), skip_bits_(0)
{
if (_skip) enable_skipping();
// Set vertex handle invalid if the mesh contains no vertex
if(_mesh.n_vertices() == 0) hnd_ = value_handle(-1);
}
/// Standard dereferencing operator.
reference operator*() const { return hnd_; }
/// Standard pointer operator.
pointer operator->() const { return &hnd_; }
/// Get the handle of the item the iterator refers to.
value_handle handle() const { return hnd_; }
/// Cast to the handle of the item the iterator refers to.
operator value_handle() const { return hnd_; }
/// Are two iterators equal? Only valid if they refer to the same mesh!
bool operator==(const ConstVertexIterT& _rhs) const
{ return ((mesh_ == _rhs.mesh_) && (hnd_ == _rhs.hnd_)); }
/// Not equal?
bool operator!=(const ConstVertexIterT& _rhs) const
{ return !operator==(_rhs); }
/// Standard pre-increment operator
ConstVertexIterT& operator++()
{ hnd_.__increment(); if (skip_bits_) skip_fwd(); return *this; }
/// Standard pre-decrement operator
ConstVertexIterT& operator--()
{ hnd_.__decrement(); if (skip_bits_) skip_bwd(); return *this; }
/// Turn on skipping: automatically skip deleted/hidden elements
void enable_skipping()
{
if (mesh_ && mesh_->has_vertex_status())
{
Attributes::StatusInfo status;
status.set_deleted(true);
status.set_hidden(true);
skip_bits_ = status.bits();
skip_fwd();
}
else skip_bits_ = 0;
}
/// Turn on skipping: automatically skip deleted/hidden elements
void disable_skipping() { skip_bits_ = 0; }
private:
void skip_fwd()
{
assert(mesh_ && skip_bits_);
while ((hnd_.idx() < (signed) mesh_->n_vertices()) &&
(mesh_->status(hnd_).bits() & skip_bits_))
hnd_.__increment();
}
void skip_bwd()
{
assert(mesh_ && skip_bits_);
while ((hnd_.idx() >= 0) &&
(mesh_->status(hnd_).bits() & skip_bits_))
hnd_.__decrement();
}
protected:
mesh_ptr mesh_;
value_handle hnd_;
unsigned int skip_bits_;
};
// We would prefer a templated typedef here but that's C++11.
template<class Mesh>
class VertexIterT: public ConstVertexIterT<Mesh> {
public: public:
VertexIterT() {} //--- Typedefs ---
VertexIterT(typename ConstVertexIterT<Mesh>::mesh_ref _mesh,
typename ConstVertexIterT<Mesh>::value_handle _hnd, bool _skip = false) : typedef ValueHandle value_handle;
ConstVertexIterT<Mesh>(_mesh, _hnd, _skip) {} typedef value_handle value_type;
}; typedef std::bidirectional_iterator_tag iterator_category;
typedef std::ptrdiff_t difference_type;
//== CLASS DEFINITION ========================================================= typedef const value_type& reference;
typedef const value_type* pointer;
typedef const Mesh* mesh_ptr;
/** \class HalfedgeIterT IteratorsT.hh <OpenMesh/Mesh/Iterators/IteratorsT.hh> typedef const Mesh& mesh_ref;
Linear iterator.
*/ /// Default constructor.
GenericIteratorT()
template <class Mesh> : mesh_(0), skip_bits_(0)
class ConstHalfedgeIterT {}
{
public: /// Construct with mesh and a target handle.
GenericIteratorT(mesh_ref _mesh, value_handle _hnd, bool _skip=false)
: mesh_(&_mesh), hnd_(_hnd), skip_bits_(0)
//--- Typedefs --- {
if (_skip) enable_skipping();
typedef typename Mesh::HalfedgeHandle value_handle;
typedef value_handle value_type; // Set vertex handle invalid if the mesh contains no vertex
if(_mesh.n_vertices() == 0) hnd_ = value_handle(-1);
typedef std::bidirectional_iterator_tag iterator_category; }
typedef std::ptrdiff_t difference_type;
typedef const value_type& reference; /// Standard dereferencing operator.
typedef const value_type* pointer; reference operator*() const {
typedef const Mesh* mesh_ptr; return hnd_;
typedef const Mesh& mesh_ref; }
/// Standard pointer operator.
pointer operator->() const {
return &hnd_;
/// Default constructor. }
ConstHalfedgeIterT()
: mesh_(0), skip_bits_(0) /// Get the handle of the item the iterator refers to.
{} value_handle handle() const {
return hnd_;
}
/// Construct with mesh and a target handle.
ConstHalfedgeIterT(mesh_ref _mesh, value_handle _hnd, bool _skip=false) /// Cast to the handle of the item the iterator refers to.
: mesh_(&_mesh), hnd_(_hnd), skip_bits_(0) operator value_handle() const {
{ return hnd_;
if (_skip) enable_skipping(); }
// Set halfedge handle invalid if the mesh contains no edge /// Are two iterators equal? Only valid if they refer to the same mesh!
if(_mesh.n_edges() == 0) hnd_ = value_handle(-1); bool operator==(const GenericIteratorT& _rhs) const {
} return ((mesh_ == _rhs.mesh_) && (hnd_ == _rhs.hnd_));
}
/// Standard dereferencing operator.
reference operator*() const { return hnd_; } /// Not equal?
bool operator!=(const GenericIteratorT& _rhs) const {
/// Standard pointer operator. return !operator==(_rhs);
pointer operator->() const { return &hnd_; } }
/// Get the handle of the item the iterator refers to. /// Standard pre-increment operator
value_handle handle() const { return hnd_; } GenericIteratorT& operator++() {
hnd_.__increment();
/// Cast to the handle of the item the iterator refers to. if (skip_bits_)
operator value_handle() const { return hnd_; } skip_fwd();
return *this;
/// Are two iterators equal? Only valid if they refer to the same mesh! }
bool operator==(const ConstHalfedgeIterT& _rhs) const
{ return ((mesh_ == _rhs.mesh_) && (hnd_ == _rhs.hnd_)); } /// Standard pre-decrement operator
GenericIteratorT& operator--() {
/// Not equal? hnd_.__decrement();
bool operator!=(const ConstHalfedgeIterT& _rhs) const if (skip_bits_)
{ return !operator==(_rhs); } skip_bwd();
return *this;
/// Standard pre-increment operator }
ConstHalfedgeIterT& operator++()
{ hnd_.__increment(); if (skip_bits_) skip_fwd(); return *this; } /// Turn on skipping: automatically skip deleted/hidden elements
void enable_skipping() {
/// Standard pre-decrement operator if (mesh_ && mesh_->has_vertex_status()) {
ConstHalfedgeIterT& operator--() Attributes::StatusInfo status;
{ hnd_.__decrement(); if (skip_bits_) skip_bwd(); return *this; } status.set_deleted(true);
status.set_hidden(true);
skip_bits_ = status.bits();
/// Turn on skipping: automatically skip deleted/hidden elements skip_fwd();
void enable_skipping() } else
{ skip_bits_ = 0;
if (mesh_ && mesh_->has_halfedge_status()) }
{
Attributes::StatusInfo status; /// Turn on skipping: automatically skip deleted/hidden elements
status.set_deleted(true); void disable_skipping() {
status.set_hidden(true); skip_bits_ = 0;
skip_bits_ = status.bits(); }
skip_fwd();
} private:
else skip_bits_ = 0;
} void skip_fwd() {
assert(mesh_ && skip_bits_);
while ((hnd_.idx() < (signed) mesh_->n_vertices())
/// Turn on skipping: automatically skip deleted/hidden elements && (mesh_->status(hnd_).bits() & skip_bits_))
void disable_skipping() { skip_bits_ = 0; } hnd_.__increment();
}
void skip_bwd() {
private: assert(mesh_ && skip_bits_);
while ((hnd_.idx() >= 0) && (mesh_->status(hnd_).bits() & skip_bits_))
void skip_fwd() hnd_.__decrement();
{ }
assert(mesh_ && skip_bits_);
while ((hnd_.idx() < (signed) mesh_->n_halfedges()) && protected:
(mesh_->status(hnd_).bits() & skip_bits_)) mesh_ptr mesh_;
hnd_.__increment(); value_handle hnd_;
} unsigned int skip_bits_;
void skip_bwd()
{
assert(mesh_ && skip_bits_);
while ((hnd_.idx() >= 0) &&
(mesh_->status(hnd_).bits() & skip_bits_))
hnd_.__decrement();
}
private:
mesh_ptr mesh_;
value_handle hnd_;
unsigned int skip_bits_;
};
// We would prefer a templated typedef here but that's C++11.
template<class Mesh>
class HalfedgeIterT: public ConstHalfedgeIterT<Mesh> {
public:
HalfedgeIterT() {}
HalfedgeIterT(typename ConstHalfedgeIterT<Mesh>::mesh_ref _mesh,
typename ConstHalfedgeIterT<Mesh>::value_handle _hnd, bool _skip = false) :
ConstHalfedgeIterT<Mesh>(_mesh, _hnd, _skip) {}
};
//== CLASS DEFINITION =========================================================
/** \class EdgeIterT IteratorsT.hh <OpenMesh/Mesh/Iterators/IteratorsT.hh>
Linear iterator.
*/
template <class Mesh>
class ConstEdgeIterT
{
public:
//--- Typedefs ---
typedef typename Mesh::EdgeHandle value_handle;
typedef value_handle value_type;
typedef std::bidirectional_iterator_tag iterator_category;
typedef std::ptrdiff_t difference_type;
typedef const value_type& reference;
typedef const value_type* pointer;
typedef const Mesh* mesh_ptr;
typedef const Mesh& mesh_ref;
/// Default constructor.
ConstEdgeIterT()
: mesh_(0), skip_bits_(0)
{}
/// Construct with mesh and a target handle.
ConstEdgeIterT(mesh_ref _mesh, value_handle _hnd, bool _skip=false)
: mesh_(&_mesh), hnd_(_hnd), skip_bits_(0)
{
if (_skip) enable_skipping();
// Set halfedge handle invalid if the mesh contains no edge
if(_mesh.n_edges() == 0) hnd_ = value_handle(-1);
}
/// Standard dereferencing operator.
reference operator*() const { return hnd_; }
/// Standard pointer operator.
pointer operator->() const { return &hnd_; }
/// Get the handle of the item the iterator refers to.
value_handle handle() const { return hnd_; }
/// Cast to the handle of the item the iterator refers to.
operator value_handle() const { return hnd_; }
/// Are two iterators equal? Only valid if they refer to the same mesh!
bool operator==(const ConstEdgeIterT& _rhs) const
{ return ((mesh_ == _rhs.mesh_) && (hnd_ == _rhs.hnd_)); }
/// Not equal?
bool operator!=(const ConstEdgeIterT& _rhs) const
{ return !operator==(_rhs); }
/// Standard pre-increment operator
ConstEdgeIterT& operator++()
{ hnd_.__increment(); if (skip_bits_) skip_fwd(); return *this; }
/// Standard pre-decrement operator
ConstEdgeIterT& operator--()
{ hnd_.__decrement(); if (skip_bits_) skip_bwd(); return *this; }
/// Turn on skipping: automatically skip deleted/hidden elements
void enable_skipping()
{
if (mesh_ && mesh_->has_edge_status())
{
Attributes::StatusInfo status;
status.set_deleted(true);
status.set_hidden(true);
skip_bits_ = status.bits();
skip_fwd();
}
else skip_bits_ = 0;
}
/// Turn on skipping: automatically skip deleted/hidden elements
void disable_skipping() { skip_bits_ = 0; }
private:
void skip_fwd()
{
assert(mesh_ && skip_bits_);
while ((hnd_.idx() < (signed) mesh_->n_edges()) &&
(mesh_->status(hnd_).bits() & skip_bits_))
hnd_.__increment();
}
void skip_bwd()
{
assert(mesh_ && skip_bits_);
while ((hnd_.idx() >= 0) &&
(mesh_->status(hnd_).bits() & skip_bits_))
hnd_.__decrement();
}
private:
mesh_ptr mesh_;
value_handle hnd_;
unsigned int skip_bits_;
};
// We would prefer a templated typedef here but that's C++11.
template<class Mesh>
class EdgeIterT: public ConstEdgeIterT<Mesh> {
public:
EdgeIterT() {}
EdgeIterT(typename ConstEdgeIterT<Mesh>::mesh_ref _mesh,
typename ConstEdgeIterT<Mesh>::value_handle _hnd, bool _skip = false) :
ConstEdgeIterT<Mesh>(_mesh, _hnd, _skip) {}
};
//== CLASS DEFINITION =========================================================
/** \class FaceIterT IteratorsT.hh <OpenMesh/Mesh/Iterators/IteratorsT.hh>
Linear iterator.
*/
template <class Mesh>
class ConstFaceIterT
{
public:
//--- Typedefs ---
typedef typename Mesh::FaceHandle value_handle;
typedef value_handle value_type;
typedef std::bidirectional_iterator_tag iterator_category;
typedef std::ptrdiff_t difference_type;
typedef const value_type& reference;
typedef const value_type* pointer;
typedef const Mesh* mesh_ptr;
typedef const Mesh& mesh_ref;
/// Default constructor.
ConstFaceIterT()
: mesh_(0), skip_bits_(0)
{}
/// Construct with mesh and a target handle.
ConstFaceIterT(mesh_ref _mesh, value_handle _hnd, bool _skip=false)
: mesh_(&_mesh), hnd_(_hnd), skip_bits_(0)
{
if (_skip) enable_skipping();
// Set face handle invalid if the mesh contains no faces
if(_mesh.n_faces() == 0) hnd_ = value_handle(-1);
}
/// Standard dereferencing operator.
reference operator*() const { return hnd_; }
/// Standard pointer operator.
pointer operator->() const { return &hnd_; }
/// Get the handle of the item the iterator refers to.
value_handle handle() const { return hnd_; }
/// Cast to the handle of the item the iterator refers to.
operator value_handle() const { return hnd_; }
/// Are two iterators equal? Only valid if they refer to the same mesh!
bool operator==(const ConstFaceIterT& _rhs) const
{ return ((mesh_ == _rhs.mesh_) && (hnd_ == _rhs.hnd_)); }
/// Not equal?
bool operator!=(const ConstFaceIterT& _rhs) const
{ return !operator==(_rhs); }
/// Standard pre-increment operator
ConstFaceIterT& operator++()
{ hnd_.__increment(); if (skip_bits_) skip_fwd(); return *this; }
/// Standard pre-decrement operator
ConstFaceIterT& operator--()
{ hnd_.__decrement(); if (skip_bits_) skip_bwd(); return *this; }
/// Turn on skipping: automatically skip deleted/hidden elements
void enable_skipping()
{
if (mesh_ && mesh_->has_face_status())
{
Attributes::StatusInfo status;
status.set_deleted(true);
status.set_hidden(true);
skip_bits_ = status.bits();
skip_fwd();
}
else skip_bits_ = 0;
}
/// Turn on skipping: automatically skip deleted/hidden elements
void disable_skipping() { skip_bits_ = 0; }
private:
void skip_fwd()
{
assert(mesh_ && skip_bits_);
while ((hnd_.idx() < (signed) mesh_->n_faces()) &&
(mesh_->status(hnd_).bits() & skip_bits_))
hnd_.__increment();
}
void skip_bwd()
{
assert(mesh_ && skip_bits_);
while ((hnd_.idx() >= 0) &&
(mesh_->status(hnd_).bits() & skip_bits_))
hnd_.__decrement();
}
private:
mesh_ptr mesh_;
value_handle hnd_;
unsigned int skip_bits_;
};
// We would prefer a templated typedef here but that's C++11.
template<class Mesh>
class FaceIterT: public ConstFaceIterT<Mesh> {
public:
FaceIterT() {}
FaceIterT(typename ConstFaceIterT<Mesh>::mesh_ref _mesh,
typename ConstFaceIterT<Mesh>::value_handle _hnd, bool _skip = false) :
ConstFaceIterT<Mesh>(_mesh, _hnd, _skip) {}
}; };
//============================================================================= //=============================================================================
......