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  * $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>
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 
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 
115  reference operator*() const {
116  return hnd_;
117  }
118 
120  pointer operator->() const {
121  return &hnd_;
122  }
123 
129  DEPRECATED("This function clutters your code. Use dereferencing operators -> and * instead.")
130  value_handle handle() const {
131  return hnd_;
132  }
133 
140  DEPRECATED("Implicit casts of iterators are unsafe. Use dereferencing operators -> and * instead.")
141  operator value_handle() const {
142  return hnd_;
143  }
144 
146  bool operator==(const GenericIteratorT& _rhs) const {
147  return ((mesh_ == _rhs.mesh_) && (hnd_ == _rhs.hnd_));
148  }
149 
151  bool operator!=(const GenericIteratorT& _rhs) const {
152  return !operator==(_rhs);
153  }
154 
157  hnd_.__increment();
158  if (skip_bits_)
159  skip_fwd();
160  return *this;
161  }
162 
165  GenericIteratorT cpy(*this);
166  ++(*this);
167  return cpy;
168  }
169 
170 #if ((defined(_MSC_VER) && (_MSC_VER >= 1800)) || __cplusplus > 199711L || defined(__GXX_EXPERIMENTAL_CXX0X__)) && !defined(OPENMESH_VECTOR_LEGACY)
171  template<class T = value_handle>
172  auto operator+=(int amount) ->
173  typename std::enable_if<
174  sizeof(decltype(std::declval<T>().__increment(amount))) >= 0,
175  GenericIteratorT&>::type {
176  static_assert(std::is_same<T, value_handle>::value,
177  "Template parameter must not deviate from default.");
178  if (skip_bits_)
179  throw std::logic_error("Skipping iterators do not support "
180  "random access.");
181  hnd_.__increment(amount);
182  return *this;
183  }
184 
185  template<class T = value_handle>
186  auto operator+(int rhs) ->
187  typename std::enable_if<
188  sizeof(decltype(std::declval<T>().__increment(rhs), void (), int {})) >= 0,
189  GenericIteratorT>::type {
190  static_assert(std::is_same<T, value_handle>::value,
191  "Template parameter must not deviate from default.");
192  if (skip_bits_)
193  throw std::logic_error("Skipping iterators do not support "
194  "random access.");
195  GenericIteratorT result = *this;
196  result.hnd_.__increment(rhs);
197  return result;
198  }
199 #endif
200 
203  hnd_.__decrement();
204  if (skip_bits_)
205  skip_bwd();
206  return *this;
207  }
208 
211  GenericIteratorT cpy(*this);
212  --(*this);
213  return cpy;
214  }
215 
218  if (mesh_ && (mesh_->*PrimitiveStatusMember)()) {
219  Attributes::StatusInfo status;
220  status.set_deleted(true);
221  status.set_hidden(true);
222  skip_bits_ = status.bits();
223  skip_fwd();
224  } else
225  skip_bits_ = 0;
226  }
227 
230  skip_bits_ = 0;
231  }
232 
233  private:
234 
235  void skip_fwd() {
236  assert(mesh_ && skip_bits_);
237  while ((hnd_.idx() < (signed) (mesh_->*PrimitiveCountMember)())
238  && (mesh_->status(hnd_).bits() & skip_bits_))
239  hnd_.__increment();
240  }
241 
242  void skip_bwd() {
243  assert(mesh_ && skip_bits_);
244  while ((hnd_.idx() >= 0) && (mesh_->status(hnd_).bits() & skip_bits_))
245  hnd_.__decrement();
246  }
247 
248  protected:
249  mesh_ptr mesh_;
250  value_handle hnd_;
251  unsigned int skip_bits_;
252 };
253 
254 //=============================================================================
255 } // namespace Iterators
256 } // namespace OpenMesh
257 //=============================================================================
258 #endif
259 //=============================================================================
Definition: IteratorsT.hh:83
void disable_skipping()
Turn on skipping: automatically skip deleted/hidden elements.
Definition: IteratorsT.hh:229
Definition: IteratorsT.hh:85
Contains all the mesh ingredients like the polygonal mesh, the triangle mesh, different mesh kernels ...
Definition: MeshItems.hh:64
GenericIteratorT(mesh_ref _mesh, value_handle _hnd, bool _skip=false)
Construct with mesh and a target handle.
Definition: IteratorsT.hh:108
value_handle handle() const
Get the handle of the item the iterator refers to.
Definition: IteratorsT.hh:130
void set_deleted(bool _b)
set deleted
Definition: Status.hh:110
Add status information to a base class.
Definition: Status.hh:99
Definition: IteratorsT.hh:80
Definition: IteratorsT.hh:89
bool operator!=(const GenericIteratorT &_rhs) const
Not equal?
Definition: IteratorsT.hh:151
void enable_skipping()
Turn on skipping: automatically skip deleted/hidden elements.
Definition: IteratorsT.hh:217
GenericIteratorT & operator--()
Standard pre-decrement operator.
Definition: IteratorsT.hh:202
Definition: IteratorsT.hh:82
bool operator==(const GenericIteratorT &_rhs) const
Are two iterators equal? Only valid if they refer to the same mesh!
Definition: IteratorsT.hh:146
GenericIteratorT & operator++()
Standard pre-increment operator.
Definition: IteratorsT.hh:156
GenericIteratorT operator++(int)
Standard post-increment operator.
Definition: IteratorsT.hh:164
GenericIteratorT()
Default constructor.
Definition: IteratorsT.hh:103
pointer operator->() const
Standard pointer operator.
Definition: IteratorsT.hh:120
Definition: IteratorsT.hh:79
Definition: IteratorsT.hh:84
GenericIteratorT operator--(int)
Standard post-decrement operator.
Definition: IteratorsT.hh:210
Definition: IteratorsT.hh:81
reference operator*() const
Standard dereferencing operator.
Definition: IteratorsT.hh:115
Definition: IteratorsT.hh:78
void set_hidden(bool _b)
set hidden
Definition: Status.hh:128
unsigned int bits() const
return whole status
Definition: Status.hh:156

Project OpenMesh, ©  Computer Graphics Group, RWTH Aachen. Documentation generated using doxygen .