OpenVDB  4.0.2
IndexIterator.h
Go to the documentation of this file.
1 //
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 //
30 
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 
54 template <typename IterT>
55 inline Index64 iterCount(const IterT& iter);
56 
57 
59 
60 
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 
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;
87  ValueVoxelCIter(Index32 prevOffset, Index32 offset)
88  : mOffset(offset), mParent(prevOffset) {}
90  : mOffset(other.mOffset), mParent(other.mParent), mValid(other.mValid) {}
91 
93  Index32 operator*() { return mOffset; }
94  Index32 operator*() const { return mOffset; }
95 
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 
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 
120  bool operator==(const ValueVoxelCIter& other) const { return mOffset == other.mOffset; }
121  bool operator!=(const ValueVoxelCIter& other) const { return !this->operator==(other); }
123 
124 private:
125  Index32 mOffset = 0;
126  Parent mParent;
127  mutable bool mValid = true;
128 }; // class ValueVoxelCIter
129 
130 
143 template <typename IteratorT, typename FilterT>
145 {
146 public:
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 
177  inline Index32 operator*() { assert(mIter); return mItem; }
178  inline Index32 operator*() const { assert(mIter); return mItem; }
179 
181  inline operator bool() const { return mIter; }
182  inline bool test() const { return mIter; }
183 
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 
195  inline bool next() { this->operator++(); return this->test(); }
196  inline bool increment() { this->next(); return this->test(); }
197 
199  inline Coord getCoord() const { assert(mIter); return mIter.getCoord(); }
201  inline void getCoord(Coord& xyz) const { assert(mIter); xyz = mIter.getCoord(); }
202 
204  inline const IteratorT& valueIter() const { return mIter; }
205 
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()) {
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()) {
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()) {
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 
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 
262  Index32 operator*() { assert(mIterator); return *mIterator; }
263  Index32 operator*() const { assert(mIterator); return *mIterator; }
264 
266  operator bool() const { return mIterator.test(); }
267  bool test() const { return mIterator.test(); }
268 
271  while (true) {
272  ++mIterator;
273  if (!mIterator.test() || mFilter.template valid<ValueIndexIter>(mIterator)) {
274  break;
275  }
276  }
277  return *this;
278  }
279 
281  IndexIter operator++(int /*dummy*/) {
282  IndexIter newIterator(*this);
283  this->operator++();
284  return newIterator;
285  }
286 
288  bool next() { this->operator++(); return this->test(); }
289  bool increment() { this->next(); return this->test(); }
290 
292  inline const FilterT& filter() const { return mFilter; }
293 
295  inline Coord getCoord() const { assert(mIterator); return mIterator.getCoord(); }
297  inline void getCoord(Coord& xyz) const { assert(mIterator); xyz = mIterator.getCoord(); }
298 
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 
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 
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/ )
Parent & parent()
Definition: IndexIterator.h:105
void getCoord(Coord &xyz) const
Return in xyz the coordinates of the item to which the value iterator is pointing.
Definition: IndexIterator.h:201
A forward iterator over array indices with filtering IteratorT can be either IndexIter or ValueIndexI...
Definition: IndexIterator.h:144
bool operator!=(const IndexIter &other) const
Definition: IndexIterator.h:301
ValueIndexIter(const ValueIndexIter &other)
Definition: IndexIterator.h:162
IndexIter & operator++()
Advance to the next (valid) item (prefix).
Definition: IndexIterator.h:270
const IteratorT & valueIter() const
Return the const value iterator.
Definition: IndexIterator.h:204
void reset(Index32 begin, Index32 end)
Reset the begining and end of the iterator.
Definition: IndexIterator.h:254
const FilterT & filter() const
Return the const filter.
Definition: IndexIterator.h:292
#define OPENVDB_THROW(exception, message)
Definition: Exceptions.h:101
uint32_t Index32
Definition: Types.h:55
Index32 getValue(unsigned) const
Definition: IndexIterator.h:79
A forward iterator over array indices in a single voxel.
Definition: IndexIterator.h:72
void reset(Index32 item, Index32 end)
Definition: IndexIterator.h:171
Index32 operator*() const
Definition: IndexIterator.h:178
Index32 operator*()
Return the item to which this iterator is currently pointing.
Definition: IndexIterator.h:93
ValueVoxelCIter(const ValueVoxelCIter &other)
Definition: IndexIterator.h:89
bool next()
Definition: IndexIterator.h:107
A no-op filter that can be used when iterating over all indices.
Definition: IndexIterator.h:62
ValueVoxelCIter(Index32 prevOffset, Index32 offset)
Definition: IndexIterator.h:87
bool test() const
Definition: IndexIterator.h:267
ValueIndexIter & operator++()
Advance to the next (valid) item (prefix).
Definition: IndexIterator.h:185
static bool initialized()
Definition: IndexIterator.h:65
ValueIndexIter(const IteratorT &iter)
Definition: IndexIterator.h:151
Index64 iterCount(const IterT &iter)
Count up the number of times the iterator can iterate.
Definition: IndexIterator.h:313
void reset(Index32, Index32)
Definition: IndexIterator.h:103
uint64_t Index64
Definition: Types.h:56
void reset(const LeafT &)
Definition: IndexIterator.h:66
bool increment()
Definition: IndexIterator.h:289
#define OPENVDB_VERSION_NAME
Definition: version.h:43
bool increment()
Definition: IndexIterator.h:196
A forward iterator over array indices from a value iterator (such as ValueOnCIter) ...
Definition: IndexIterator.h:148
Index32 operator*() const
Definition: IndexIterator.h:263
Index32 operator*()
Returns the item to which this iterator is currently pointing.
Definition: IndexIterator.h:262
ValueVoxelCIter & operator++()
Advance to the next (valid) item (prefix).
Definition: IndexIterator.h:97
bool operator==(const ValueIndexIter &other) const
Equality operators.
Definition: IndexIterator.h:207
Index32 end() const
Definition: IndexIterator.h:251
Definition: Exceptions.h:39
bool operator==(const Vec3< T0 > &v0, const Vec3< T1 > &v1)
Equality operator, does exact floating point comparisons.
Definition: Vec3.h:487
Parent(Index32 offset)
Definition: IndexIterator.h:78
Index32 end() const
Definition: IndexIterator.h:101
bool test() const
Definition: IndexIterator.h:182
Coord getCoord() const
Return the coordinates of the item to which the value iterator is pointing.
Definition: IndexIterator.h:199
bool operator==(const ValueVoxelCIter &other) const
Equality operators.
Definition: IndexIterator.h:120
Index32 offset()
Definition: IndexIterator.h:106
IndexIter(const IndexIter &other)
Definition: IndexIterator.h:229
Coord getCoord() const
Return the coordinates of the item to which the value iterator is pointing.
Definition: IndexIterator.h:295
static bool valid(const IterT &)
Definition: IndexIterator.h:67
void getCoord(Coord &xyz) const
Return in xyz the coordinates of the item to which the value iterator is pointing.
Definition: IndexIterator.h:297
bool next()
Advance to the next (valid) item.
Definition: IndexIterator.h:195
bool next()
Advance to the next (valid) item.
Definition: IndexIterator.h:288
Index32 operator*() const
Definition: IndexIterator.h:94
Signed (x, y, z) 32-bit integer coordinates.
Definition: Coord.h:48
IndexIter & operator=(const IndexIter &other)
Definition: IndexIterator.h:238
Definition: Exceptions.h:90
bool operator!=(const ValueVoxelCIter &other) const
Equality operators.
Definition: IndexIterator.h:121
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:71
Index32 operator*()
Returns the item to which this iterator is currently pointing.
Definition: IndexIterator.h:177
bool test() const
Definition: IndexIterator.h:100
IndexIter operator++(int)
Advance to the next (valid) item (postfix).
Definition: IndexIterator.h:281
IndexIter(const IteratorT &iterator, const FilterT &filter)
Definition: IndexIterator.h:217
static constexpr size_t size
The size of a LeafBuffer when LeafBuffer::mOutOfCore is atomic.
Definition: LeafBuffer.h:85
Index32 end() const
Definition: IndexIterator.h:169
bool operator!=(const ValueIndexIter &other) const
Definition: IndexIterator.h:208
bool operator==(const IndexIter &other) const
Equality operators.
Definition: IndexIterator.h:300