| OpenMesh

IteratorsT.hh
1 /* ========================================================================= *
2 * *
3 * OpenMesh *
4 * Copyright (c) 2001-2015, RWTH-Aachen University *
5 * Department of Computer Graphics and Multimedia *
6 * All rights reserved. *
7 * www.openmesh.org *
8 * *
9 *---------------------------------------------------------------------------*
10 * This file is part of OpenMesh. *
11 *---------------------------------------------------------------------------*
12 * *
13 * Redistribution and use in source and binary forms, with or without *
14 * modification, are permitted provided that the following conditions *
15 * are met: *
16 * *
17 * 1. Redistributions of source code must retain the above copyright notice, *
18 * this list of conditions and the following disclaimer. *
19 * *
20 * 2. Redistributions in binary form must reproduce the above copyright *
21 * notice, this list of conditions and the following disclaimer in the *
22 * documentation and/or other materials provided with the distribution. *
23 * *
24 * 3. Neither the name of the copyright holder nor the names of its *
25 * contributors may be used to endorse or promote products derived from *
26 * this software without specific prior written permission. *
27 * *
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
29 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
30 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
31 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
32 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, *
33 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, *
34 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR *
35 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF *
36 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING *
37 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS *
38 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
39 * *
40 * ========================================================================= */
41
42
43
44 #ifndef OPENMESH_ITERATORS_HH
45 #define OPENMESH_ITERATORS_HH
46
47 //=============================================================================
48 //
49 // Iterators for PolyMesh/TriMesh
50 //
51 //=============================================================================
52
53
54
55 //== INCLUDES =================================================================
56
57 #include <OpenMesh/Core/System/config.h>
58 #include <OpenMesh/Core/Mesh/Status.hh>
59 #include <cassert>
60 #include <cstddef>
61 #include <iterator>
62
63
64 //== NAMESPACES ===============================================================
65
66 namespace OpenMesh {
67 namespace Iterators {
68
69
70 //== FORWARD DECLARATIONS =====================================================
71
72
73 template <class Mesh> class ConstVertexIterT;
74 template <class Mesh> class VertexIterT;
75 template <class Mesh> class ConstHalfedgeIterT;
76 template <class Mesh> class HalfedgeIterT;
77 template <class Mesh> class ConstEdgeIterT;
78 template <class Mesh> class EdgeIterT;
79 template <class Mesh> class ConstFaceIterT;
80 template <class Mesh> class FaceIterT;
81
82
83 template <class Mesh, class ValueHandle, class MemberOwner, bool (MemberOwner::*PrimitiveStatusMember)() const, size_t (MemberOwner::*PrimitiveCountMember)() const>
84 class GenericIteratorT {
85 public:
86 //--- Typedefs ---
87
88 typedef ValueHandle value_handle;
89 typedef value_handle value_type;
90 typedef std::bidirectional_iterator_tag iterator_category;
91 typedef std::ptrdiff_t difference_type;
92 typedef const value_type& reference;
93 typedef const value_type* pointer;
94 typedef const Mesh* mesh_ptr;
95 typedef const Mesh& mesh_ref;
96
98 GenericIteratorT()
99 : mesh_(0), skip_bits_(0)
100 {}
101
103 GenericIteratorT(mesh_ref _mesh, value_handle _hnd, bool _skip=false)
104 : mesh_(&_mesh), hnd_(_hnd), skip_bits_(0)
105 {
106 if (_skip) enable_skipping();
107 }
108
110 reference operator*() const {
111 return hnd_;
112 }
113
115 pointer operator->() const {
116 return &hnd_;
117 }
118
124 OM_DEPRECATED("This function clutters your code. Use dereferencing operators -> and * instead.")
125 value_handle handle() const {
126 return hnd_;
127 }
128
135 OM_DEPRECATED("Implicit casts of iterators are unsafe. Use dereferencing operators -> and * instead.")
136 operator value_handle() const {
137 return hnd_;
138 }
139
141 bool operator==(const GenericIteratorT& _rhs) const {
142 return ((mesh_ == _rhs.mesh_) && (hnd_ == _rhs.hnd_));
143 }
144
146 bool operator!=(const GenericIteratorT& _rhs) const {
147 return !operator==(_rhs);
148 }
149
151 GenericIteratorT& operator++() {
152 hnd_.__increment();
153 if (skip_bits_)
154 skip_fwd();
155 return *this;
156 }
157
159 GenericIteratorT operator++(int) {
160 GenericIteratorT cpy(*this);
161 ++(*this);
162 return cpy;
163 }
164
165 #if ((defined(_MSC_VER) && (_MSC_VER >= 1800)) || __cplusplus > 199711L || defined(__GXX_EXPERIMENTAL_CXX0X__)) && !defined(OPENMESH_VECTOR_LEGACY)
166 template<class T = value_handle>
167 auto operator+=(int amount) ->
168 typename std::enable_if<
169 sizeof(decltype(std::declval<T>().__increment(amount))) >= 0,
170 GenericIteratorT&>::type {
171 static_assert(std::is_same<T, value_handle>::value,
172 "Template parameter must not deviate from default.");
173 if (skip_bits_)
174 throw std::logic_error("Skipping iterators do not support "
175 "random access.");
176 hnd_.__increment(amount);
177 return *this;
178 }
179
180 template<class T = value_handle>
181 auto operator+(int rhs) ->
182 typename std::enable_if<
183 sizeof(decltype(std::declval<T>().__increment(rhs), void (), int {})) >= 0,
184 GenericIteratorT>::type {
185 static_assert(std::is_same<T, value_handle>::value,
186 "Template parameter must not deviate from default.");
187 if (skip_bits_)
188 throw std::logic_error("Skipping iterators do not support "
189 "random access.");
190 GenericIteratorT result = *this;
191 result.hnd_.__increment(rhs);
192 return result;
193 }
194 #endif
195
197 GenericIteratorT& operator--() {
198 hnd_.__decrement();
199 if (skip_bits_)
200 skip_bwd();
201 return *this;
202 }
203
205 GenericIteratorT operator--(int) {
206 GenericIteratorT cpy(*this);
207 --(*this);
208 return cpy;
209 }
210
212 void enable_skipping() {
213 if (mesh_ && (mesh_->*PrimitiveStatusMember)()) {
214 Attributes::StatusInfo status;
215 status.set_deleted(true);
216 status.set_hidden(true);
217 skip_bits_ = status.bits();
218 skip_fwd();
219 } else
220 skip_bits_ = 0;
221 }
222
224 void disable_skipping() {
225 skip_bits_ = 0;
226 }
227
228 private:
229
230 void skip_fwd() {
231 assert(mesh_ && skip_bits_);
232 while ((hnd_.idx() < (signed) (mesh_->*PrimitiveCountMember)())
233 && (mesh_->status(hnd_).bits() & skip_bits_))
234 hnd_.__increment();
235 }
236
237 void skip_bwd() {
238 assert(mesh_ && skip_bits_);
239 while ((hnd_.idx() >= 0) && (mesh_->status(hnd_).bits() & skip_bits_))
240 hnd_.__decrement();
241 }
242
243 protected:
244 mesh_ptr mesh_;
245 value_handle hnd_;
246 unsigned int skip_bits_;
247 };
248
249 //=============================================================================
250 } // namespace Iterators
251 } // namespace OpenMesh
252 //=============================================================================
253 #endif
254 //=============================================================================
OpenMesh::Iterators::GenericIteratorT::operator*
reference operator*() const
Standard dereferencing operator.
Definition: IteratorsT.hh:110
OpenMesh::Attributes::StatusInfo::set_hidden
void set_hidden(bool _b)
set hidden
Definition: Status.hh:123
OpenMesh::Attributes::StatusInfo::set_deleted
void set_deleted(bool _b)
set deleted
Definition: Status.hh:105
OpenMesh::Attributes::StatusInfo
Add status information to a base class.
Definition: Status.hh:94
OpenMesh::Attributes::StatusInfo::bits
unsigned int bits() const
return whole status
Definition: Status.hh:151
OpenMesh::Iterators::GenericIteratorT::operator++
GenericIteratorT & operator++()
Standard pre-increment operator.
Definition: IteratorsT.hh:151
OpenMesh::Iterators::GenericIteratorT::enable_skipping
void enable_skipping()
Turn on skipping: automatically skip deleted/hidden elements.
Definition: IteratorsT.hh:212
OpenMesh::Iterators::GenericIteratorT::GenericIteratorT
GenericIteratorT(mesh_ref _mesh, value_handle _hnd, bool _skip=false)
Construct with mesh and a target handle.
Definition: IteratorsT.hh:103
OpenMesh::Iterators::HalfedgeIterT
Definition: IteratorsT.hh:76
OpenMesh::Iterators::GenericIteratorT::handle
value_handle handle() const
Get the handle of the item the iterator refers to.
Definition: IteratorsT.hh:125
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:141
OpenMesh::Iterators::GenericIteratorT
Definition: IteratorsT.hh:84
OpenMesh::Iterators::GenericIteratorT::operator--
GenericIteratorT operator--(int)
Standard post-decrement operator.
Definition: IteratorsT.hh:205
OpenMesh::Iterators::ConstVertexIterT
Definition: IteratorsT.hh:73
OpenMesh
Contains all the mesh ingredients like the polygonal mesh, the triangle mesh, different mesh kernels ...
Definition: MeshItems.hh:59
OpenMesh::Iterators::GenericIteratorT::operator!=
bool operator!=(const GenericIteratorT &_rhs) const
Not equal?
Definition: IteratorsT.hh:146
OpenMesh::Iterators::EdgeIterT
Definition: IteratorsT.hh:78
OpenMesh::Iterators::ConstHalfedgeIterT
Definition: IteratorsT.hh:75
OpenMesh::Iterators::GenericIteratorT::GenericIteratorT
GenericIteratorT()
Default constructor.
Definition: IteratorsT.hh:98
OpenMesh::Iterators::GenericIteratorT::disable_skipping
void disable_skipping()
Turn on skipping: automatically skip deleted/hidden elements.
Definition: IteratorsT.hh:224
OpenMesh::Iterators::GenericIteratorT::operator--
GenericIteratorT & operator--()
Standard pre-decrement operator.
Definition: IteratorsT.hh:197
OpenMesh::Iterators::FaceIterT
Definition: IteratorsT.hh:80
OpenMesh::Iterators::GenericIteratorT::operator->
pointer operator->() const
Standard pointer operator.
Definition: IteratorsT.hh:115
OpenMesh::Iterators::GenericIteratorT::operator++
GenericIteratorT operator++(int)
Standard post-increment operator.
Definition: IteratorsT.hh:159
OpenMesh::Iterators::ConstEdgeIterT
Definition: IteratorsT.hh:77
OpenMesh::Iterators::VertexIterT
Definition: IteratorsT.hh:74
OpenMesh::PolyMesh_ArrayKernelT
Polygonal mesh based on the ArrayKernel.
Definition: PolyMesh_ArrayKernelT.hh:94
OpenMesh::Iterators::ConstFaceIterT
Definition: IteratorsT.hh:79