HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
IndexIterator.h
Go to the documentation of this file.
1 ///////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (c) 2012-2017 DreamWorks Animation LLC
4 //
5 // All rights reserved. This software is distributed under the
6 // Mozilla Public License 2.0 ( http://www.mozilla.org/MPL/2.0/ )
7 //
8 // Redistributions of source code must retain the above copyright
9 // and license notice and the following restrictions and disclaimer.
10 //
11 // * Neither the name of DreamWorks Animation nor the names of
12 // its contributors may be used to endorse or promote products derived
13 // from this software without specific prior written permission.
14 //
15 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY INDIRECT, INCIDENTAL,
20 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 // IN NO EVENT SHALL THE COPYRIGHT HOLDERS' AND CONTRIBUTORS' AGGREGATE
27 // LIABILITY FOR ALL CLAIMS REGARDLESS OF THEIR BASIS EXCEED US$250.00.
28 //
29 ///////////////////////////////////////////////////////////////////////////
30 
31 /// @file points/IndexIterator.h
32 ///
33 /// @author Dan Bailey
34 ///
35 /// @brief Index Iterators.
36 
37 #ifndef OPENVDB_POINTS_INDEX_ITERATOR_HAS_BEEN_INCLUDED
38 #define OPENVDB_POINTS_INDEX_ITERATOR_HAS_BEEN_INCLUDED
39 
40 #include <openvdb/version.h>
41 #include <openvdb/Types.h>
42 
43 namespace openvdb {
45 namespace OPENVDB_VERSION_NAME {
46 namespace points {
47 
48 
49 /// @brief Count up the number of times the iterator can iterate
50 ///
51 /// @param iter the iterator.
52 ///
53 /// @note counting by iteration only performed where a dynamic filter is in use,
54 template <typename IterT>
55 inline Index64 iterCount(const IterT& iter);
56 
57 
58 ////////////////////////////////////////
59 
60 
61 /// @brief A no-op filter that can be used when iterating over all indices
63 {
64 public:
65  static bool initialized() { return true; }
66  template <typename LeafT> void reset(const LeafT&) { }
67  template <typename IterT> static bool valid(const IterT&) { return true; }
68 }; // class NullFilter
69 
70 
71 /// @brief A forward iterator over array indices in a single voxel
73 {
74 public:
75  struct Parent
76  {
77  Parent() = default;
78  explicit Parent(Index32 offset): mOffset(offset) { }
79  Index32 getValue(unsigned /*offset*/) const { return mOffset; }
80  private:
81  Index32 mOffset = 0;
82  }; // struct Parent
83 
84  using NodeType = Parent;
85 
86  ValueVoxelCIter() = default;
88  : mOffset(offset), mParent(prevOffset) {}
90  : mOffset(other.mOffset), mParent(other.mParent), mValid(other.mValid) {}
91 
92  /// @brief Return the item to which this iterator is currently pointing.
93  Index32 operator*() { return mOffset; }
94  Index32 operator*() const { return mOffset; }
95 
96  /// @brief Advance to the next (valid) item (prefix).
97  ValueVoxelCIter& operator++() { mValid = false; return *this; }
98 
99  operator bool() const { return mValid; }
100  bool test() const { return mValid; }
101  Index32 end() const { return mOffset+1; }
102 
103  void reset(Index32 /*item*/, Index32 /*end*/) {}
104 
105  Parent& parent() { return mParent; }
106  Index32 offset() { return mOffset; }
107  inline bool next() { this->operator++(); return this->test(); }
108 
109  /// @brief For efficiency, Coord assumed to be readily available
110  /// when iterating over indices of a single voxel
111  Coord getCoord [[noreturn]] () const {
112  OPENVDB_THROW(RuntimeError, "ValueVoxelCIter does not provide a valid Coord.");
113  }
114  void getCoord [[noreturn]] (Coord& /*coord*/) const {
115  OPENVDB_THROW(RuntimeError, "ValueVoxelCIter does not provide a valid Coord.");
116  }
117 
118  /// @{
119  /// @brief Equality operators
120  bool operator==(const ValueVoxelCIter& other) const { return mOffset == other.mOffset; }
121  bool operator!=(const ValueVoxelCIter& other) const { return !this->operator==(other); }
122  /// @}
123 
124 private:
125  Index32 mOffset = 0;
126  Parent mParent;
127  mutable bool mValid = true;
128 }; // class ValueVoxelCIter
129 
130 
131 /// @brief A forward iterator over array indices with filtering
132 /// IteratorT can be either IndexIter or ValueIndexIter (or some custom index iterator)
133 /// FilterT should be a struct or class with a valid() method than can be evaluated per index
134 /// Here's a simple filter example that only accepts even indices:
135 ///
136 /// struct EvenIndexFilter
137 /// {
138 /// bool valid(const Index32 offset) const {
139 /// return (offset % 2) == 0;
140 /// }
141 /// };
142 ///
143 template <typename IteratorT, typename FilterT>
145 {
146 public:
147  /// @brief A forward iterator over array indices from a value iterator (such as ValueOnCIter)
149  {
150  public:
151  ValueIndexIter(const IteratorT& iter)
152  : mIter(iter), mParent(&mIter.parent())
153  {
154  if (mIter) {
155  assert(mParent);
156  Index32 start = (mIter.offset() > 0 ?
157  Index32(mParent->getValue(mIter.offset() - 1)) : Index32(0));
158  this->reset(start, *mIter);
159  if (mItem >= mEnd) this->operator++();
160  }
161  }
163  : mEnd(other.mEnd), mItem(other.mItem), mIter(other.mIter), mParent(other.mParent)
164  {
165  assert(mParent);
166  }
167  ValueIndexIter& operator=(const ValueIndexIter&) = default;
168 
169  inline Index32 end() const { return mEnd; }
170 
171  inline void reset(Index32 item, Index32 end) {
172  mItem = item;
173  mEnd = end;
174  }
175 
176  /// @brief Returns the item to which this iterator is currently pointing.
177  inline Index32 operator*() { assert(mIter); return mItem; }
178  inline Index32 operator*() const { assert(mIter); return mItem; }
179 
180  /// @brief Return @c true if this iterator is not yet exhausted.
181  inline operator bool() const { return mIter; }
182  inline bool test() const { return mIter; }
183 
184  /// @brief Advance to the next (valid) item (prefix).
186  ++mItem;
187  while (mItem >= mEnd && mIter.next()) {
188  assert(mParent);
189  this->reset(mParent->getValue(mIter.offset() - 1), *mIter);
190  }
191  return *this;
192  }
193 
194  /// @brief Advance to the next (valid) item.
195  inline bool next() { this->operator++(); return this->test(); }
196  inline bool increment() { this->next(); return this->test(); }
197 
198  /// Return the coordinates of the item to which the value iterator is pointing.
199  inline Coord getCoord() const { assert(mIter); return mIter.getCoord(); }
200  /// Return in @a xyz the coordinates of the item to which the value iterator is pointing.
201  inline void getCoord(Coord& xyz) const { assert(mIter); xyz = mIter.getCoord(); }
202 
203  /// Return the const value iterator
204  inline const IteratorT& valueIter() const { return mIter; }
205 
206  /// @brief Equality operators
207  bool operator==(const ValueIndexIter& other) const { return mItem == other.mItem; }
208  bool operator!=(const ValueIndexIter& other) const { return !this->operator==(other); }
209 
210  private:
211  Index32 mEnd = 0;
212  Index32 mItem = 0;
213  IteratorT mIter;
214  const typename IteratorT::NodeType* mParent;
215  }; // ValueIndexIter
216 
217  IndexIter(const IteratorT& iterator, const FilterT& filter)
218  : mIterator(iterator)
219  , mFilter(filter)
220  {
221  if (!mFilter.initialized()) {
222  OPENVDB_THROW(RuntimeError,
223  "Filter needs to be initialized before constructing the iterator.");
224  }
225  if (mIterator) {
226  this->reset(*mIterator, mIterator.end());
227  }
228  }
229  IndexIter(const IndexIter& other)
230  : mIterator(other.mIterator)
231  , mFilter(other.mFilter)
232  {
233  if (!mFilter.initialized()) {
234  OPENVDB_THROW(RuntimeError,
235  "Filter needs to be initialized before constructing the iterator.");
236  }
237  }
239  {
240  if (&other != this) {
241  mIterator = other.mIterator;
242  mFilter = other.mFilter;
243  if (!mFilter.initialized()) {
244  OPENVDB_THROW(RuntimeError,
245  "Filter needs to be initialized before constructing the iterator.");
246  }
247  }
248  return *this;
249  }
250 
251  Index32 end() const { return mIterator.end(); }
252 
253  /// @brief Reset the begining and end of the iterator.
254  void reset(Index32 begin, Index32 end) {
255  mIterator.reset(begin, end);
256  while (mIterator.test() && !mFilter.template valid<ValueIndexIter>(mIterator)) {
257  ++mIterator;
258  }
259  }
260 
261  /// @brief Returns the item to which this iterator is currently pointing.
262  Index32 operator*() { assert(mIterator); return *mIterator; }
263  Index32 operator*() const { assert(mIterator); return *mIterator; }
264 
265  /// @brief Return @c true if this iterator is not yet exhausted.
266  operator bool() const { return mIterator.test(); }
267  bool test() const { return mIterator.test(); }
268 
269  /// @brief Advance to the next (valid) item (prefix).
271  while (true) {
272  ++mIterator;
273  if (!mIterator.test() || mFilter.template valid<ValueIndexIter>(mIterator)) {
274  break;
275  }
276  }
277  return *this;
278  }
279 
280  /// @brief Advance to the next (valid) item (postfix).
281  IndexIter operator++(int /*dummy*/) {
282  IndexIter newIterator(*this);
283  this->operator++();
284  return newIterator;
285  }
286 
287  /// @brief Advance to the next (valid) item.
288  bool next() { this->operator++(); return this->test(); }
289  bool increment() { this->next(); return this->test(); }
290 
291  /// Return the const filter
292  inline const FilterT& filter() const { return mFilter; }
293 
294  /// Return the coordinates of the item to which the value iterator is pointing.
295  inline Coord getCoord() const { assert(mIterator); return mIterator.getCoord(); }
296  /// Return in @a xyz the coordinates of the item to which the value iterator is pointing.
297  inline void getCoord(Coord& xyz) const { assert(mIterator); xyz = mIterator.getCoord(); }
298 
299  /// @brief Equality operators
300  bool operator==(const IndexIter& other) const { return mIterator == other.mIterator; }
301  bool operator!=(const IndexIter& other) const { return !this->operator==(other); }
302 
303 private:
304  ValueIndexIter mIterator;
305  FilterT mFilter;
306 }; // class IndexIter
307 
308 
309 ////////////////////////////////////////
310 
311 
312 template <typename IterT>
313 inline Index64 iterCount(const IterT& iter)
314 {
315  Index64 size = 0;
316  for (IterT newIter(iter); newIter; ++newIter, ++size) { }
317  return size;
318 }
319 
320 
321 ////////////////////////////////////////
322 
323 
324 } // namespace points
325 } // namespace OPENVDB_VERSION_NAME
326 } // namespace openvdb
327 
328 #endif // OPENVDB_POINTS_INDEX_ITERATOR_HAS_BEEN_INCLUDED
329 
330 // Copyright (c) 2012-2017 DreamWorks Animation LLC
331 // All rights reserved. This software is distributed under the
332 // Mozilla Public License 2.0 ( http://www.mozilla.org/MPL/2.0/ )
GLuint start
Definition: glcorearb.h:474
const IteratorT & valueIter() const
Return the const value iterator.
bool next()
Advance to the next (valid) item.
bool operator!=(const IndexIter &other) const
A no-op filter that can be used when iterating over all indices.
Definition: IndexIterator.h:62
void getCoord(Coord &xyz) const
Return in xyz the coordinates of the item to which the value iterator is pointing.
ValueVoxelCIter(Index32 prevOffset, Index32 offset)
Definition: IndexIterator.h:87
Index32 operator*()
Returns the item to which this iterator is currently pointing.
Coord getCoord() const
Return the coordinates of the item to which the value iterator is pointing.
GLsizeiptr size
Definition: glcorearb.h:663
Index32 operator*()
Return the item to which this iterator is currently pointing.
Definition: IndexIterator.h:93
A forward iterator over array indices in a single voxel.
Definition: IndexIterator.h:72
Coord getCoord() const
Return the coordinates of the item to which the value iterator is pointing.
#define OPENVDB_VERSION_NAME
Definition: version.h:43
GLuint GLuint end
Definition: glcorearb.h:474
GLintptr offset
Definition: glcorearb.h:664
IndexIter & operator++()
Advance to the next (valid) item (prefix).
bool operator==(const ValueIndexIter &other) const
Equality operators.
A forward iterator over array indices with filtering IteratorT can be either IndexIter or ValueIndexI...
ValueIndexIter & operator=(const ValueIndexIter &)=default
ValueVoxelCIter & operator++()
Advance to the next (valid) item (prefix).
Definition: IndexIterator.h:97
void getCoord(Coord &xyz) const
Return in xyz the coordinates of the item to which the value iterator is pointing.
Coord getCoord() const
For efficiency, Coord assumed to be readily available when iterating over indices of a single voxel...
IndexIter(const IteratorT &iterator, const FilterT &filter)
const FilterT & filter() const
Return the const filter.
bool operator!=(const ValueVoxelCIter &other) const
Equality operators.
A forward iterator over array indices from a value iterator (such as ValueOnCIter) ...
Index32 operator*()
Returns the item to which this iterator is currently pointing.
IndexIter operator++(int)
Advance to the next (valid) item (postfix).
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:71
Index64 iterCount(const IterT &iter)
Count up the number of times the iterator can iterate.
IndexIter & operator=(const IndexIter &other)
void reset(Index32 begin, Index32 end)
Reset the begining and end of the iterator.
ValueIndexIter & operator++()
Advance to the next (valid) item (prefix).
bool operator==(const IndexIter &other) const
Equality operators.
#define OPENVDB_THROW(exception, message)
Definition: Exceptions.h:101
bool operator==(const ValueVoxelCIter &other) const
Equality operators.
GLint GLint GLint GLint GLint GLint GLint GLbitfield GLenum filter
Definition: glcorearb.h:1296