From d3c382022127271fadd9075a6589187c968340ab Mon Sep 17 00:00:00 2001 From: Hans-Christian Ebke Date: Thu, 1 Mar 2012 15:41:44 +0000 Subject: [PATCH] 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 --- src/OpenMesh/Core/Mesh/IteratorsT.hh | 626 ++++----------------- src/OpenMesh/Core/Mesh/PolyConnectivity.hh | 16 +- 2 files changed, 118 insertions(+), 524 deletions(-) diff --git a/src/OpenMesh/Core/Mesh/IteratorsT.hh b/src/OpenMesh/Core/Mesh/IteratorsT.hh index 1048e692..3eb1f512 100644 --- a/src/OpenMesh/Core/Mesh/IteratorsT.hh +++ b/src/OpenMesh/Core/Mesh/IteratorsT.hh @@ -77,523 +77,117 @@ template class ConstFaceIterT; template class FaceIterT; - - -//== CLASS DEFINITION ========================================================= - -/** \class VertexIterT IteratorsT.hh - Linear iterator. -*/ - -template -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 VertexIterT: public ConstVertexIterT { +template +class GenericIteratorT { public: - VertexIterT() {} - VertexIterT(typename ConstVertexIterT::mesh_ref _mesh, - typename ConstVertexIterT::value_handle _hnd, bool _skip = false) : - ConstVertexIterT(_mesh, _hnd, _skip) {} -}; - -//== CLASS DEFINITION ========================================================= - - -/** \class HalfedgeIterT IteratorsT.hh - Linear iterator. -*/ - -template -class ConstHalfedgeIterT -{ -public: - - - //--- Typedefs --- - - typedef typename Mesh::HalfedgeHandle 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. - ConstHalfedgeIterT() - : mesh_(0), skip_bits_(0) - {} - - - /// Construct with mesh and a target handle. - ConstHalfedgeIterT(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 ConstHalfedgeIterT& _rhs) const - { return ((mesh_ == _rhs.mesh_) && (hnd_ == _rhs.hnd_)); } - - /// Not equal? - bool operator!=(const ConstHalfedgeIterT& _rhs) const - { return !operator==(_rhs); } - - /// Standard pre-increment operator - ConstHalfedgeIterT& operator++() - { hnd_.__increment(); if (skip_bits_) skip_fwd(); return *this; } - - /// Standard pre-decrement operator - ConstHalfedgeIterT& 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_halfedge_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_halfedges()) && - (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 HalfedgeIterT: public ConstHalfedgeIterT { - public: - HalfedgeIterT() {} - - HalfedgeIterT(typename ConstHalfedgeIterT::mesh_ref _mesh, - typename ConstHalfedgeIterT::value_handle _hnd, bool _skip = false) : - ConstHalfedgeIterT(_mesh, _hnd, _skip) {} -}; - - -//== CLASS DEFINITION ========================================================= - - -/** \class EdgeIterT IteratorsT.hh - Linear iterator. -*/ - -template -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 EdgeIterT: public ConstEdgeIterT { - public: - EdgeIterT() {} - - EdgeIterT(typename ConstEdgeIterT::mesh_ref _mesh, - typename ConstEdgeIterT::value_handle _hnd, bool _skip = false) : - ConstEdgeIterT(_mesh, _hnd, _skip) {} -}; - -//== CLASS DEFINITION ========================================================= - - -/** \class FaceIterT IteratorsT.hh - Linear iterator. -*/ - -template -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 FaceIterT: public ConstFaceIterT { - public: - FaceIterT() {} - - FaceIterT(typename ConstFaceIterT::mesh_ref _mesh, - typename ConstFaceIterT::value_handle _hnd, bool _skip = false) : - ConstFaceIterT(_mesh, _hnd, _skip) {} + //--- Typedefs --- + + typedef ValueHandle 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. + GenericIteratorT() + : mesh_(0), skip_bits_(0) + {} + + /// Construct with mesh and a target handle. + GenericIteratorT(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 GenericIteratorT& _rhs) const { + return ((mesh_ == _rhs.mesh_) && (hnd_ == _rhs.hnd_)); + } + + /// Not equal? + bool operator!=(const GenericIteratorT& _rhs) const { + return !operator==(_rhs); + } + + /// Standard pre-increment operator + GenericIteratorT& operator++() { + hnd_.__increment(); + if (skip_bits_) + skip_fwd(); + return *this; + } + + /// Standard pre-decrement operator + GenericIteratorT& 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_; }; //============================================================================= diff --git a/src/OpenMesh/Core/Mesh/PolyConnectivity.hh b/src/OpenMesh/Core/Mesh/PolyConnectivity.hh index 61a84536..63d55711 100644 --- a/src/OpenMesh/Core/Mesh/PolyConnectivity.hh +++ b/src/OpenMesh/Core/Mesh/PolyConnectivity.hh @@ -76,15 +76,15 @@ public: */ //@{ /// Linear iterator - typedef Iterators::VertexIterT VertexIter; - typedef Iterators::HalfedgeIterT HalfedgeIter; - typedef Iterators::EdgeIterT EdgeIter; - typedef Iterators::FaceIterT FaceIter; + typedef Iterators::GenericIteratorT VertexIter; + typedef Iterators::GenericIteratorT HalfedgeIter; + typedef Iterators::GenericIteratorT EdgeIter; + typedef Iterators::GenericIteratorT FaceIter; - typedef Iterators::ConstVertexIterT ConstVertexIter; - typedef Iterators::ConstHalfedgeIterT ConstHalfedgeIter; - typedef Iterators::ConstEdgeIterT ConstEdgeIter; - typedef Iterators::ConstFaceIterT ConstFaceIter; + typedef VertexIter ConstVertexIter; + typedef HalfedgeIter ConstHalfedgeIter; + typedef EdgeIter ConstEdgeIter; + typedef FaceIter ConstFaceIter; //@} //--- circulators ---