OpenMesh: OpenMesh/Core/Mesh/IteratorsT.hh Source File

| OpenMesh

IteratorsT.hh
1 /* ========================================================================= *
2 * *
3 * OpenMesh *
4 * Copyright (c) 2001-2022, 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 #pragma once
44
45 //=============================================================================
46 //
47 // Iterators for PolyMesh/TriMesh
48 //
49 //=============================================================================
50
51
52
53 //== INCLUDES =================================================================
54
55 #include <OpenMesh/Core/System/config.h>
56 #include <OpenMesh/Core/Mesh/Status.hh>
57 #include <cassert>
58 #include <cstddef>
59 #include <iterator>
60
61
62 //== NAMESPACES ===============================================================
63
64 namespace OpenMesh {
65 namespace Iterators {
66
67
68 //== FORWARD DECLARATIONS =====================================================
69
70
71 template <class Mesh> class ConstVertexIterT;
72 template <class Mesh> class VertexIterT;
73 template <class Mesh> class ConstHalfedgeIterT;
74 template <class Mesh> class HalfedgeIterT;
75 template <class Mesh> class ConstEdgeIterT;
76 template <class Mesh> class EdgeIterT;
77 template <class Mesh> class ConstFaceIterT;
78 template <class Mesh> class FaceIterT;
79
80
81 template <class Mesh, class ValueHandle, class MemberOwner, bool (MemberOwner::*PrimitiveStatusMember)() const, size_t (MemberOwner::*PrimitiveCountMember)() const>
82 class GenericIteratorT {
83 public:
84 //--- Typedefs ---
85
86 typedef ValueHandle value_handle;
87 typedef value_handle value_type;
88 typedef std::bidirectional_iterator_tag iterator_category;
89 typedef std::ptrdiff_t difference_type;
90 typedef const Mesh* mesh_ptr;
91 typedef const Mesh& mesh_ref;
92 typedef decltype(make_smart(std::declval<ValueHandle>(), std::declval<Mesh>())) SmartHandle;
93 typedef const SmartHandle& reference;
94 typedef const SmartHandle* pointer;
95
97 GenericIteratorT()
98 : hnd_(make_smart(ValueHandle(),nullptr)), skip_bits_(0)
99 {}
100
102 GenericIteratorT(mesh_ref _mesh, value_handle _hnd, bool _skip=false)
103 : hnd_(make_smart(_hnd, _mesh)), skip_bits_(0)
104 {
105 if (_skip) enable_skipping();
106 }
107
109 reference operator*() const {
110 return hnd_;
111 }
112
114 pointer operator->() const {
115 return &hnd_;
116 }
117
123 OM_DEPRECATED("This function clutters your code. Use dereferencing operators -> and * instead.")
124 value_handle handle() const {
125 return hnd_;
126 }
127
134 OM_DEPRECATED("Implicit casts of iterators are unsafe. Use dereferencing operators -> and * instead.")
135 operator value_handle() const {
136 return hnd_;
137 }
138
140 bool operator==(const GenericIteratorT& _rhs) const {
141 return ((hnd_.mesh() == _rhs.hnd_.mesh()) && (hnd_ == _rhs.hnd_));
142 }
143
145 bool operator!=(const GenericIteratorT& _rhs) const {
146 return !operator==(_rhs);
147 }
148
150 GenericIteratorT& operator++() {
151 hnd_.__increment();
152 if (skip_bits_)
153 skip_fwd();
154 return *this;
155 }
156
158 GenericIteratorT operator++(int) {
159 GenericIteratorT cpy(*this);
160 ++(*this);
161 return cpy;
162 }
163
164 #if ((defined(_MSC_VER) && (_MSC_VER >= 1800)) || __cplusplus > 199711L || defined(__GXX_EXPERIMENTAL_CXX0X__)) && !defined(OPENMESH_VECTOR_LEGACY)
165 template<class T = value_handle>
166 auto operator+=(int amount) ->
167 typename std::enable_if<
168 sizeof(decltype(std::declval<T>().__increment(amount))) >= 0,
169 GenericIteratorT&>::type {
170 static_assert(std::is_same<T, value_handle>::value,
171 "Template parameter must not deviate from default.");
172 if (skip_bits_)
173 throw std::logic_error("Skipping iterators do not support "
174 "random access.");
175 hnd_.__increment(amount);
176 return *this;
177 }
178
179 template<class T = value_handle>
180 auto operator+(int rhs) ->
181 typename std::enable_if<
182 sizeof(decltype(std::declval<T>().__increment(rhs), void (), int {})) >= 0,
183 GenericIteratorT>::type {
184 static_assert(std::is_same<T, value_handle>::value,
185 "Template parameter must not deviate from default.");
186 if (skip_bits_)
187 throw std::logic_error("Skipping iterators do not support "
188 "random access.");
189 GenericIteratorT result = *this;
190 result.hnd_.__increment(rhs);
191 return result;
192 }
193 #endif
194
196 GenericIteratorT& operator--() {
197 hnd_.__decrement();
198 if (skip_bits_)
199 skip_bwd();
200 return *this;
201 }
202
204 GenericIteratorT operator--(int) {
205 GenericIteratorT cpy(*this);
206 --(*this);
207 return cpy;
208 }
209
211 void enable_skipping() {
212 if (hnd_.mesh() && (hnd_.mesh()->*PrimitiveStatusMember)()) {
213 Attributes::StatusInfo status;
214 status.set_deleted(true);
215 status.set_hidden(true);
216 skip_bits_ = status.bits();
217 skip_fwd();
218 } else
219 skip_bits_ = 0;
220 }
221
223 void disable_skipping() {
224 skip_bits_ = 0;
225 }
226
227 private:
228
229 void skip_fwd() {
230 assert(hnd_.mesh() && skip_bits_);
231 while ((hnd_.idx() < (signed) (hnd_.mesh()->*PrimitiveCountMember)())
232 && (hnd_.mesh()->status(hnd_).bits() & skip_bits_))
233 hnd_.__increment();
234 }
235
236 void skip_bwd() {
237 assert(hnd_.mesh() && skip_bits_);
238 while ((hnd_.idx() >= 0) && (hnd_.mesh()->status(hnd_).bits() & skip_bits_))
239 hnd_.__decrement();
240 }
241
242 protected:
243 SmartHandle hnd_;
244 unsigned int skip_bits_;
245 };
246
247 //=============================================================================
248 } // namespace Iterators
249 } // namespace OpenMesh
250 //=============================================================================
OpenMesh
Contains all the mesh ingredients like the polygonal mesh, the triangle mesh, different mesh kernels ...
Definition: MeshItems.hh:59
OpenMesh::make_smart
SmartVertexHandle make_smart(VertexHandle _vh, const PolyConnectivity *_mesh)
Creats a SmartVertexHandle from a VertexHandle and a Mesh.
Definition: SmartHandles.hh:256
OpenMesh::Iterators::ConstVertexIterT
Definition: IteratorsT.hh:71
OpenMesh::Iterators::VertexIterT
Definition: IteratorsT.hh:72
OpenMesh::Iterators::ConstHalfedgeIterT
Definition: IteratorsT.hh:73
OpenMesh::Iterators::HalfedgeIterT
Definition: IteratorsT.hh:74
OpenMesh::Iterators::ConstEdgeIterT
Definition: IteratorsT.hh:75
OpenMesh::Iterators::EdgeIterT
Definition: IteratorsT.hh:76
OpenMesh::Iterators::ConstFaceIterT
Definition: IteratorsT.hh:77
OpenMesh::Iterators::FaceIterT
Definition: IteratorsT.hh:78
OpenMesh::Iterators::GenericIteratorT
Definition: IteratorsT.hh:82
OpenMesh::Iterators::GenericIteratorT::disable_skipping
void disable_skipping()
Turn on skipping: automatically skip deleted/hidden elements.
Definition: IteratorsT.hh:223
OpenMesh::Iterators::GenericIteratorT::operator--
GenericIteratorT operator--(int)
Standard post-decrement operator.
Definition: IteratorsT.hh:204
OpenMesh::Iterators::GenericIteratorT::GenericIteratorT
GenericIteratorT(mesh_ref _mesh, value_handle _hnd, bool _skip=false)
Construct with mesh and a target handle.
Definition: IteratorsT.hh:102
OpenMesh::Iterators::GenericIteratorT::operator++
GenericIteratorT & operator++()
Standard pre-increment operator.
Definition: IteratorsT.hh:150
OpenMesh::Iterators::GenericIteratorT::GenericIteratorT
GenericIteratorT()
Default constructor.
Definition: IteratorsT.hh:97
OpenMesh::Iterators::GenericIteratorT::operator->
pointer operator->() const
Standard pointer operator.
Definition: IteratorsT.hh:114
OpenMesh::Iterators::GenericIteratorT::operator++
GenericIteratorT operator++(int)
Standard post-increment operator.
Definition: IteratorsT.hh:158
OpenMesh::Iterators::GenericIteratorT::enable_skipping
void enable_skipping()
Turn on skipping: automatically skip deleted/hidden elements.
Definition: IteratorsT.hh:211
OpenMesh::Iterators::GenericIteratorT::operator*
reference operator*() const
Standard dereferencing operator.
Definition: IteratorsT.hh:109
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:140
OpenMesh::Iterators::GenericIteratorT::operator--
GenericIteratorT & operator--()
Standard pre-decrement operator.
Definition: IteratorsT.hh:196
OpenMesh::Iterators::GenericIteratorT::handle
value_handle handle() const
Get the handle of the item the iterator refers to.
Definition: IteratorsT.hh:124
OpenMesh::Iterators::GenericIteratorT::operator!=
bool operator!=(const GenericIteratorT &_rhs) const
Not equal?
Definition: IteratorsT.hh:145
OpenMesh::PolyMesh_ArrayKernelT
Polygonal mesh based on the ArrayKernel.
Definition: PolyMesh_ArrayKernelT.hh:96
OpenMesh::SmartHandle
Definition: SmartHandles.hh:276
OpenMesh::Attributes::StatusInfo
Add status information to a base class.
Definition: Status.hh:95
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::bits
unsigned int bits() const
return whole status
Definition: Status.hh:151