Developer Documentation
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 #pragma once
44 
45 //=============================================================================
46 //
47 // Iterators for PolyMesh/TriMesh
48 //
49 //=============================================================================
50 
51 
52 
53 //== INCLUDES =================================================================
54 
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>
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 
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 
151  hnd_.__increment();
152  if (skip_bits_)
153  skip_fwd();
154  return *this;
155  }
156 
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 
197  hnd_.__decrement();
198  if (skip_bits_)
199  skip_bwd();
200  return *this;
201  }
202 
205  GenericIteratorT cpy(*this);
206  --(*this);
207  return cpy;
208  }
209 
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 
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 //=============================================================================
GenericIteratorT & operator--()
Standard pre-decrement operator.
Definition: IteratorsT.hh:196
bool operator==(const GenericIteratorT &_rhs) const
Are two iterators equal? Only valid if they refer to the same mesh!
Definition: IteratorsT.hh:140
bool operator!=(const GenericIteratorT &_rhs) const
Not equal?
Definition: IteratorsT.hh:145
value_handle handle() const
Get the handle of the item the iterator refers to.
Definition: IteratorsT.hh:124
pointer operator->() const
Standard pointer operator.
Definition: IteratorsT.hh:114
SmartVertexHandle make_smart(VertexHandle _vh, const PolyConnectivity *_mesh)
Creats a SmartVertexHandle from a VertexHandle and a Mesh.
void disable_skipping()
Turn on skipping: automatically skip deleted/hidden elements.
Definition: IteratorsT.hh:223
void set_hidden(bool _b)
set hidden
Definition: Status.hh:123
#define OM_DEPRECATED(msg)
define OM_SUPPRESS_DEPRECATED to suppress deprecated code warnings
Definition: config.h:102
void set_deleted(bool _b)
set deleted
Definition: Status.hh:105
unsigned int bits() const
return whole status
Definition: Status.hh:151
GenericIteratorT operator--(int)
Standard post-decrement operator.
Definition: IteratorsT.hh:204
void enable_skipping()
Turn on skipping: automatically skip deleted/hidden elements.
Definition: IteratorsT.hh:211
GenericIteratorT operator++(int)
Standard post-increment operator.
Definition: IteratorsT.hh:158
GenericIteratorT(mesh_ref _mesh, value_handle _hnd, bool _skip=false)
Construct with mesh and a target handle.
Definition: IteratorsT.hh:102
GenericIteratorT()
Default constructor.
Definition: IteratorsT.hh:97
reference operator*() const
Standard dereferencing operator.
Definition: IteratorsT.hh:109
GenericIteratorT & operator++()
Standard pre-increment operator.
Definition: IteratorsT.hh:150