IteratorsT.hh
/* ========================================================================= *
* *
* OpenMesh *
* Copyright (c) 2001-2015, RWTH-Aachen University *
* Department of Computer Graphics and Multimedia *
* All rights reserved. *
* www.openmesh.org *
* *
*---------------------------------------------------------------------------*
* This file is part of OpenMesh. *
*---------------------------------------------------------------------------*
* *
* Redistribution and use in source and binary forms, with or without *
* modification, are permitted provided that the following conditions *
* are met: *
* *
* 1. Redistributions of source code must retain the above copyright notice, *
* this list of conditions and the following disclaimer. *
* *
* 2. Redistributions in binary form must reproduce the above copyright *
* notice, this list of conditions and the following disclaimer in the *
* documentation and/or other materials provided with the distribution. *
* *
* 3. Neither the name of the copyright holder nor the names of its *
* contributors may be used to endorse or promote products derived from *
* this software without specific prior written permission. *
* *
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, *
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, *
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR *
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF *
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING *
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS *
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
* *
* ========================================================================= */
/*===========================================================================*\
* *
* $Revision$ *
* $Date$ *
* *
\*===========================================================================*/
#ifndef OPENMESH_ITERATORS_HH
#define OPENMESH_ITERATORS_HH
//=============================================================================
//
// Iterators for PolyMesh/TriMesh
//
//=============================================================================
//== INCLUDES =================================================================
#include <OpenMesh/Core/System/config.h>
#include <OpenMesh/Core/Mesh/Status.hh>
#include <cassert>
#include <cstddef>
#include <iterator>
//== NAMESPACES ===============================================================
namespace OpenMesh {
namespace Iterators {
//== FORWARD DECLARATIONS =====================================================
template <class Mesh> class ConstVertexIterT;
template <class Mesh> class VertexIterT;
template <class Mesh> class ConstHalfedgeIterT;
template <class Mesh> class HalfedgeIterT;
template <class Mesh> class ConstEdgeIterT;
template <class Mesh> class EdgeIterT;
template <class Mesh> class ConstFaceIterT;
template <class Mesh> class FaceIterT;
template <class Mesh, class ValueHandle, class MemberOwner, bool (MemberOwner::*PrimitiveStatusMember)() const, size_t (MemberOwner::*PrimitiveCountMember)() const>
class GenericIteratorT {
public:
//--- 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;
GenericIteratorT()
: mesh_(0), skip_bits_(0)
{}
GenericIteratorT(mesh_ref _mesh, value_handle _hnd, bool _skip=false)
: mesh_(&_mesh), hnd_(_hnd), skip_bits_(0)
{
if (_skip) enable_skipping();
}
reference operator*() const {
return hnd_;
}
pointer operator->() const {
return &hnd_;
}
DEPRECATED("This function clutters your code. Use dereferencing operators -> and * instead.")
value_handle handle() const {
return hnd_;
}
DEPRECATED("Implicit casts of iterators are unsafe. Use dereferencing operators -> and * instead.")
operator value_handle() const {
return hnd_;
}
bool operator==(const GenericIteratorT& _rhs) const {
return ((mesh_ == _rhs.mesh_) && (hnd_ == _rhs.hnd_));
}
bool operator!=(const GenericIteratorT& _rhs) const {
return !operator==(_rhs);
}
GenericIteratorT& operator++() {
hnd_.__increment();
if (skip_bits_)
skip_fwd();
return *this;
}
GenericIteratorT operator++(int) {
GenericIteratorT cpy(*this);
++(*this);
return cpy;
}
#if ((defined(_MSC_VER) && (_MSC_VER >= 1800)) || __cplusplus > 199711L || defined(__GXX_EXPERIMENTAL_CXX0X__)) && !defined(OPENMESH_VECTOR_LEGACY)
template<class T = value_handle>
auto operator+=(int amount) ->
typename std::enable_if<
sizeof(decltype(std::declval<T>().__increment(amount))) >= 0,
GenericIteratorT&>::type {
static_assert(std::is_same<T, value_handle>::value,
"Template parameter must not deviate from default.");
if (skip_bits_)
throw std::logic_error("Skipping iterators do not support "
"random access.");
hnd_.__increment(amount);
return *this;
}
template<class T = value_handle>
auto operator+(int rhs) ->
typename std::enable_if<
sizeof(decltype(std::declval<T>().__increment(rhs), void (), int {})) >= 0,
GenericIteratorT>::type {
static_assert(std::is_same<T, value_handle>::value,
"Template parameter must not deviate from default.");
if (skip_bits_)
throw std::logic_error("Skipping iterators do not support "
"random access.");
GenericIteratorT result = *this;
result.hnd_.__increment(rhs);
return result;
}
#endif
GenericIteratorT& operator--() {
hnd_.__decrement();
if (skip_bits_)
skip_bwd();
return *this;
}
GenericIteratorT operator--(int) {
GenericIteratorT cpy(*this);
--(*this);
return cpy;
}
void enable_skipping() {
if (mesh_ && (mesh_->*PrimitiveStatusMember)()) {
Attributes::StatusInfo status;
status.set_deleted(true);
status.set_hidden(true);
skip_bits_ = status.bits();
skip_fwd();
} else
skip_bits_ = 0;
}
void disable_skipping() {
skip_bits_ = 0;
}
private:
void skip_fwd() {
assert(mesh_ && skip_bits_);
while ((hnd_.idx() < (signed) (mesh_->*PrimitiveCountMember)())
&& (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_;
};
//=============================================================================
} // namespace Iterators
} // namespace OpenMesh
//=============================================================================
#endif
//=============================================================================
OpenMesh::Attributes::StatusInfo
Add status information to a base class.
Definition: Status.hh:99
OpenMesh::Iterators::GenericIteratorT::operator++
GenericIteratorT & operator++()
Standard pre-increment operator.
Definition: IteratorsT.hh:156
OpenMesh::Iterators::EdgeIterT
Definition: IteratorsT.hh:83
OpenMesh::Iterators::ConstHalfedgeIterT
Definition: IteratorsT.hh:80
OpenMesh::Iterators::GenericIteratorT::operator*
reference operator*() const
Standard dereferencing operator.
Definition: IteratorsT.hh:115
OpenMesh::Iterators::GenericIteratorT::disable_skipping
void disable_skipping()
Turn on skipping: automatically skip deleted/hidden elements.
Definition: IteratorsT.hh:229
OpenMesh::Iterators::ConstEdgeIterT
Definition: IteratorsT.hh:82
OpenMesh::Iterators::ConstFaceIterT
Definition: IteratorsT.hh:84
OpenMesh::Iterators::GenericIteratorT::GenericIteratorT
GenericIteratorT(mesh_ref _mesh, value_handle _hnd, bool _skip=false)
Construct with mesh and a target handle.
Definition: IteratorsT.hh:108
OpenMesh
Contains all the mesh ingredients like the polygonal mesh, the triangle mesh, different mesh kernels ...
Definition: MeshItems.hh:64
OpenMesh::Iterators::GenericIteratorT::operator++
GenericIteratorT operator++(int)
Standard post-increment operator.
Definition: IteratorsT.hh:164
OpenMesh::Iterators::GenericIteratorT::handle
value_handle handle() const
Get the handle of the item the iterator refers to.
Definition: IteratorsT.hh:130
OpenMesh::Iterators::GenericIteratorT::operator==
bool operator==(const GenericIteratorT &_rhs) const
Are two iterators equal? Only valid if they refer to the same mesh!
Definition: IteratorsT.hh:146
OpenMesh::Attributes::StatusInfo::set_hidden
void set_hidden(bool _b)
set hidden
Definition: Status.hh:128
OpenMesh::Attributes::StatusInfo::set_deleted
void set_deleted(bool _b)
set deleted
Definition: Status.hh:110
OpenMesh::Iterators::GenericIteratorT::operator--
GenericIteratorT operator--(int)
Standard post-decrement operator.
Definition: IteratorsT.hh:210
OpenMesh::Iterators::GenericIteratorT
Definition: IteratorsT.hh:89
OpenMesh::Iterators::GenericIteratorT::GenericIteratorT
GenericIteratorT()
Default constructor.
Definition: IteratorsT.hh:103
OpenMesh::Iterators::VertexIterT
Definition: IteratorsT.hh:79
OpenMesh::Iterators::FaceIterT
Definition: IteratorsT.hh:85
OpenMesh::Iterators::GenericIteratorT::operator->
pointer operator->() const
Standard pointer operator.
Definition: IteratorsT.hh:120
OpenMesh::Iterators::GenericIteratorT::enable_skipping
void enable_skipping()
Turn on skipping: automatically skip deleted/hidden elements.
Definition: IteratorsT.hh:217
OpenMesh::Iterators::GenericIteratorT::operator!=
bool operator!=(const GenericIteratorT &_rhs) const
Not equal?
Definition: IteratorsT.hh:151
OpenMesh::Iterators::GenericIteratorT::operator--
GenericIteratorT & operator--()
Standard pre-decrement operator.
Definition: IteratorsT.hh:202
OpenMesh::Attributes::StatusInfo::bits
unsigned int bits() const
return whole status
Definition: Status.hh:156
OpenMesh::Iterators::HalfedgeIterT
Definition: IteratorsT.hh:81
OpenMesh::Iterators::ConstVertexIterT
Definition: IteratorsT.hh:78