Developer Documentation

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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 * $Revision$ *
45 * $Date$ *
46 * *
47 \*===========================================================================*/
48
49 #ifndef OPENMESH_ITERATORS_HH
50 #define OPENMESH_ITERATORS_HH
51
52 //=============================================================================
53 //
54 // Iterators for PolyMesh/TriMesh
55 //
56 //=============================================================================
57
58
59
60 //== INCLUDES =================================================================
61
62 #include <OpenMesh/Core/System/config.h>
63 #include <OpenMesh/Core/Mesh/Status.hh>
64 #include <cassert>
65 #include <cstddef>
66 #include <iterator>
67
68
69 //== NAMESPACES ===============================================================
70
71 namespace OpenMesh {
72 namespace Iterators {
73
74
75 //== FORWARD DECLARATIONS =====================================================
76
77
78 template <class Mesh> class ConstVertexIterT;
79 template <class Mesh> class VertexIterT;
80 template <class Mesh> class ConstHalfedgeIterT;
81 template <class Mesh> class HalfedgeIterT;
82 template <class Mesh> class ConstEdgeIterT;
83 template <class Mesh> class EdgeIterT;
84 template <class Mesh> class ConstFaceIterT;
85 template <class Mesh> class FaceIterT;
86
87
88 template <class Mesh, class ValueHandle, class MemberOwner, bool (MemberOwner::*PrimitiveStatusMember)() const, size_t (MemberOwner::*PrimitiveCountMember)() const>
89 class GenericIteratorT {
90 public:
91 //--- Typedefs ---
92
93 typedef ValueHandle value_handle;
94 typedef value_handle value_type;
95 typedef std::bidirectional_iterator_tag iterator_category;
96 typedef std::ptrdiff_t difference_type;
97 typedef const value_type& reference;
98 typedef const value_type* pointer;
99 typedef const Mesh* mesh_ptr;
100 typedef const Mesh& mesh_ref;
101
103 GenericIteratorT()
104 : mesh_(0), skip_bits_(0)
105 {}
106
108 GenericIteratorT(mesh_ref _mesh, value_handle _hnd, bool _skip=false)
109 : mesh_(&_mesh), hnd_(_hnd), skip_bits_(0)
110 {
111 if (_skip) enable_skipping();
112
113 // Set vertex handle invalid if the mesh contains no vertex
114 if((mesh_->*PrimitiveCountMember)() == 0) hnd_ = value_handle(-1);
115 }
116
118 reference operator*() const {
119 return hnd_;
120 }
121
123 pointer operator->() const {
124 return &hnd_;
125 }
126
132 DEPRECATED("This function clutters your code. Use dereferencing operators -> and * instead.")
133 value_handle handle() const {
134 return hnd_;
135 }
136
143 DEPRECATED("Implicit casts of iterators are unsafe. Use dereferencing operators -> and * instead.")
144 operator value_handle() const {
145 return hnd_;
146 }
147
149 bool operator==(const GenericIteratorT& _rhs) const {
150 return ((mesh_ == _rhs.mesh_) && (hnd_ == _rhs.hnd_));
151 }
152
154 bool operator!=(const GenericIteratorT& _rhs) const {
155 return !operator==(_rhs);
156 }
157
159 GenericIteratorT& operator++() {
160 hnd_.__increment();
161 if (skip_bits_)
162 skip_fwd();
163 return *this;
164 }
165
167 GenericIteratorT operator++(int) {
168 GenericIteratorT cpy(*this);
169 ++(*this);
170 return cpy;
171 }
172
173 #if (_MSC_VER >= 1900 || __cplusplus > 199711L || defined(__GXX_EXPERIMENTAL_CXX0X__)) && !defined(OPENMESH_VECTOR_LEGACY)
174 template<class T = value_handle>
175 auto operator+=(int amount) ->
176 typename std::enable_if<
177 sizeof(decltype(std::declval<T>().__increment(amount))) >= 0,
178 GenericIteratorT&>::type {
179 static_assert(std::is_same<T, value_handle>::value,
180 "Template parameter must not deviate from default.");
181 if (skip_bits_)
182 throw std::logic_error("Skipping iterators do not support "
183 "random access.");
184 hnd_.__increment(amount);
185 return *this;
186 }
187
188 template<class T = value_handle>
189 auto operator+(int rhs) ->
190 typename std::enable_if<
191 sizeof(decltype(std::declval<T>().__increment(rhs), void (), int {})) >= 0,
192 GenericIteratorT>::type {
193 static_assert(std::is_same<T, value_handle>::value,
194 "Template parameter must not deviate from default.");
195 if (skip_bits_)
196 throw std::logic_error("Skipping iterators do not support "
197 "random access.");
198 GenericIteratorT result = *this;
199 result.hnd_.__increment(rhs);
200 return result;
201 }
202 #endif
203
205 GenericIteratorT& operator--() {
206 hnd_.__decrement();
207 if (skip_bits_)
208 skip_bwd();
209 return *this;
210 }
211
213 GenericIteratorT operator--(int) {
214 GenericIteratorT cpy(*this);
215 --(*this);
216 return cpy;
217 }
218
220 void enable_skipping() {
221 if (mesh_ && (mesh_->*PrimitiveStatusMember)()) {
222 Attributes::StatusInfo status;
223 status.set_deleted(true);
224 status.set_hidden(true);
225 skip_bits_ = status.bits();
226 skip_fwd();
227 } else
228 skip_bits_ = 0;
229 }
230
232 void disable_skipping() {
233 skip_bits_ = 0;
234 }
235
236 private:
237
238 void skip_fwd() {
239 assert(mesh_ && skip_bits_);
240 while ((hnd_.idx() < (signed) (mesh_->*PrimitiveCountMember)())
241 && (mesh_->status(hnd_).bits() & skip_bits_))
242 hnd_.__increment();
243 }
244
245 void skip_bwd() {
246 assert(mesh_ && skip_bits_);
247 while ((hnd_.idx() >= 0) && (mesh_->status(hnd_).bits() & skip_bits_))
248 hnd_.__decrement();
249 }
250
251 protected:
252 mesh_ptr mesh_;
253 value_handle hnd_;
254 unsigned int skip_bits_;
255 };
256
257 //=============================================================================
258 } // namespace Iterators
259 } // namespace OpenMesh
260 //=============================================================================
261 #endif
262 //=============================================================================
OpenMesh::Iterators::VertexIterT
Definition: IteratorsT.hh:79
OpenMesh::Iterators::EdgeIterT
Definition: IteratorsT.hh:83
OpenMesh::Iterators::ConstHalfedgeIterT
Definition: IteratorsT.hh:80
OpenMesh
Definition: MeshItems.hh:64
OpenMesh::Iterators::GenericIteratorT::disable_skipping
void disable_skipping()
Turn on skipping: automatically skip deleted/hidden elements.
Definition: IteratorsT.hh:232
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::GenericIteratorT
GenericIteratorT(mesh_ref _mesh, value_handle _hnd, bool _skip=false)
Construct with mesh and a target handle.
Definition: IteratorsT.hh:108
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:149
OpenMesh::Iterators::ConstFaceIterT
Definition: IteratorsT.hh:84
OpenMesh::TriMesh_ArrayKernelT< CustomTraits >
OpenMesh::Iterators::GenericIteratorT::operator++
GenericIteratorT operator++(int)
Standard post-increment operator.
Definition: IteratorsT.hh:167
config.h
OpenMesh::Iterators::GenericIteratorT
Definition: IteratorsT.hh:89
OpenMesh::Iterators::FaceIterT
Definition: IteratorsT.hh:85
OpenMesh::Attributes::StatusInfo
Definition: Status.hh:99
OpenMesh::Iterators::GenericIteratorT::handle
value_handle handle() const
Get the handle of the item the iterator refers to.
Definition: IteratorsT.hh:133
OpenMesh::Iterators::ConstEdgeIterT
Definition: IteratorsT.hh:82
OpenMesh::Iterators::GenericIteratorT::operator->
pointer operator->() const
Standard pointer operator.
Definition: IteratorsT.hh:123
OpenMesh::Iterators::GenericIteratorT::operator++
GenericIteratorT & operator++()
Standard pre-increment operator.
Definition: IteratorsT.hh:159
OpenMesh::Iterators::ConstVertexIterT
Definition: IteratorsT.hh:78
OpenMesh::Iterators::GenericIteratorT::operator*
reference operator*() const
Standard dereferencing operator.
Definition: IteratorsT.hh:118
OpenMesh::Attributes::StatusInfo::bits
unsigned int bits() const
return whole status
Definition: Status.hh:156
OpenMesh::Iterators::GenericIteratorT::GenericIteratorT
GenericIteratorT()
Default constructor.
Definition: IteratorsT.hh:103
OpenMesh::Iterators::GenericIteratorT::enable_skipping
void enable_skipping()
Turn on skipping: automatically skip deleted/hidden elements.
Definition: IteratorsT.hh:220
OpenMesh::Iterators::GenericIteratorT::operator!=
bool operator!=(const GenericIteratorT &_rhs) const
Not equal?
Definition: IteratorsT.hh:154
OpenMesh::Iterators::GenericIteratorT::operator--
GenericIteratorT operator--(int)
Standard post-decrement operator.
Definition: IteratorsT.hh:213
OpenMesh::Iterators::GenericIteratorT::operator--
GenericIteratorT & operator--()
Standard pre-decrement operator.
Definition: IteratorsT.hh:205
OpenMesh::Iterators::HalfedgeIterT
Definition: IteratorsT.hh:81