OpenVDB: IndexIterator.h Source File

OpenVDB 6.1.0

IndexIterator.h
Go to the documentation of this file.
1 //
3 // Copyright (c) 2012-2018 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 {
44 OPENVDB_USE_VERSION_NAMESPACE
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
61 namespace index {
62 // Enum for informing early-exit optimizations
63 // PARTIAL - No optimizations are possible
64 // NONE - No indices to evaluate, can skip computation
65 // ALL - All indices to evaluate, can skip filtering
66 enum State
67 {
68 PARTIAL=0,
69 NONE,
70 ALL
71 };
72 }
73
74
77 class NullFilter
78 {
79 public:
80 static bool initialized() { return true; }
81 static index::State state() { return index::ALL; }
82 template <typename LeafT>
83 static index::State state(const LeafT&) { return index::ALL; }
84
85 template <typename LeafT> void reset(const LeafT&) { }
86 template <typename IterT> static bool valid(const IterT&) { return true; }
87 }; // class NullFilter
88
89
91 class ValueVoxelCIter
92 {
93 public:
94 struct Parent
95 {
96 Parent() = default;
97 explicit Parent(Index32 offset): mOffset(offset) { }
98 Index32 getValue(unsigned /*offset*/) const { return mOffset; }
99 private:
100 Index32 mOffset = 0;
101 }; // struct Parent
102
103 using NodeType = Parent;
104
105 ValueVoxelCIter() = default;
106 ValueVoxelCIter(Index32 prevOffset, Index32 offset)
107 : mOffset(offset), mParent(prevOffset) {}
108 ValueVoxelCIter(const ValueVoxelCIter& other)
109 : mOffset(other.mOffset), mParent(other.mParent), mValid(other.mValid) {}
110
112 Index32 operator*() { return mOffset; }
113 Index32 operator*() const { return mOffset; }
114
116 ValueVoxelCIter& operator++() { mValid = false; return *this; }
117
118 operator bool() const { return mValid; }
119 bool test() const { return mValid; }
120 Index32 end() const { return mOffset+1; }
121
122 void reset(Index32 /*item*/, Index32 /*end*/) {}
123
124 Parent& parent() { return mParent; }
125 Index32 offset() { return mOffset; }
126 inline bool next() { this->operator++(); return this->test(); }
127
130 Coord getCoord [[noreturn]] () const {
131 OPENVDB_THROW(RuntimeError, "ValueVoxelCIter does not provide a valid Coord.");
132 }
133 void getCoord [[noreturn]] (Coord& /*coord*/) const {
134 OPENVDB_THROW(RuntimeError, "ValueVoxelCIter does not provide a valid Coord.");
135 }
136 bool isValueOn [[noreturn]] () const {
137 OPENVDB_THROW(RuntimeError, "ValueVoxelCIter does not test if voxel is active.");
138 }
139
142 bool operator==(const ValueVoxelCIter& other) const { return mOffset == other.mOffset; }
143 bool operator!=(const ValueVoxelCIter& other) const { return !this->operator==(other); }
145
146 private:
147 Index32 mOffset = 0;
148 Parent mParent;
149 mutable bool mValid = true;
150 }; // class ValueVoxelCIter
151
152
165 template <typename IteratorT, typename FilterT>
166 class IndexIter
167 {
168 public:
170 class ValueIndexIter
171 {
172 public:
173 ValueIndexIter(const IteratorT& iter)
174 : mIter(iter), mParent(&mIter.parent())
175 {
176 if (mIter) {
177 assert(mParent);
178 Index32 start = (mIter.offset() > 0 ?
179 Index32(mParent->getValue(mIter.offset() - 1)) : Index32(0));
180 this->reset(start, *mIter);
181 if (mItem >= mEnd) this->operator++();
182 }
183 }
184 ValueIndexIter(const ValueIndexIter& other)
185 : mEnd(other.mEnd), mItem(other.mItem), mIter(other.mIter), mParent(other.mParent)
186 {
187 assert(mParent);
188 }
189 ValueIndexIter& operator=(const ValueIndexIter&) = default;
190
191 inline Index32 end() const { return mEnd; }
192
193 inline void reset(Index32 item, Index32 end) {
194 mItem = item;
195 mEnd = end;
196 }
197
199 inline Index32 operator*() { assert(mIter); return mItem; }
200 inline Index32 operator*() const { assert(mIter); return mItem; }
201
203 inline operator bool() const { return mIter; }
204 inline bool test() const { return mIter; }
205
207 inline ValueIndexIter& operator++() {
208 ++mItem;
209 while (mItem >= mEnd && mIter.next()) {
210 assert(mParent);
211 this->reset(mParent->getValue(mIter.offset() - 1), *mIter);
212 }
213 return *this;
214 }
215
217 inline bool next() { this->operator++(); return this->test(); }
218 inline bool increment() { this->next(); return this->test(); }
219
221 inline Coord getCoord() const { assert(mIter); return mIter.getCoord(); }
223 inline void getCoord(Coord& xyz) const { assert(mIter); xyz = mIter.getCoord(); }
224
226 inline bool isValueOn() const { assert(mIter); return mIter.isValueOn(); }
227
229 inline const IteratorT& valueIter() const { return mIter; }
230
232 bool operator==(const ValueIndexIter& other) const { return mItem == other.mItem; }
233 bool operator!=(const ValueIndexIter& other) const { return !this->operator==(other); }
234
235 private:
236 Index32 mEnd = 0;
237 Index32 mItem = 0;
238 IteratorT mIter;
239 const typename IteratorT::NodeType* mParent;
240 }; // ValueIndexIter
241
242 IndexIter(const IteratorT& iterator, const FilterT& filter)
243 : mIterator(iterator)
244 , mFilter(filter)
245 {
246 if (!mFilter.initialized()) {
247 OPENVDB_THROW(RuntimeError,
248 "Filter needs to be initialized before constructing the iterator.");
249 }
250 if (mIterator) {
251 this->reset(*mIterator, mIterator.end());
252 }
253 }
254 IndexIter(const IndexIter& other)
255 : mIterator(other.mIterator)
256 , mFilter(other.mFilter)
257 {
258 if (!mFilter.initialized()) {
259 OPENVDB_THROW(RuntimeError,
260 "Filter needs to be initialized before constructing the iterator.");
261 }
262 }
263 IndexIter& operator=(const IndexIter& other)
264 {
265 if (&other != this) {
266 mIterator = other.mIterator;
267 mFilter = other.mFilter;
268 if (!mFilter.initialized()) {
269 OPENVDB_THROW(RuntimeError,
270 "Filter needs to be initialized before constructing the iterator.");
271 }
272 }
273 return *this;
274 }
275
276 Index32 end() const { return mIterator.end(); }
277
279 void reset(Index32 begin, Index32 end) {
280 mIterator.reset(begin, end);
281 while (mIterator.test() && !mFilter.template valid<ValueIndexIter>(mIterator)) {
282 ++mIterator;
283 }
284 }
285
287 Index32 operator*() { assert(mIterator); return *mIterator; }
288 Index32 operator*() const { assert(mIterator); return *mIterator; }
289
291 operator bool() const { return mIterator.test(); }
292 bool test() const { return mIterator.test(); }
293
295 IndexIter& operator++() {
296 while (true) {
297 ++mIterator;
298 if (!mIterator.test() || mFilter.template valid<ValueIndexIter>(mIterator)) {
299 break;
300 }
301 }
302 return *this;
303 }
304
306 IndexIter operator++(int /*dummy*/) {
307 IndexIter newIterator(*this);
308 this->operator++();
309 return newIterator;
310 }
311
313 bool next() { this->operator++(); return this->test(); }
314 bool increment() { this->next(); return this->test(); }
315
317 inline const FilterT& filter() const { return mFilter; }
318
320 inline Coord getCoord() const { assert(mIterator); return mIterator.getCoord(); }
322 inline void getCoord(Coord& xyz) const { assert(mIterator); xyz = mIterator.getCoord(); }
323
325 inline bool isValueOn() const { assert(mIterator); return mIterator.valueIter().isValueOn(); }
326
328 bool operator==(const IndexIter& other) const { return mIterator == other.mIterator; }
329 bool operator!=(const IndexIter& other) const { return !this->operator==(other); }
330
331 private:
332 ValueIndexIter mIterator;
333 FilterT mFilter;
334 }; // class IndexIter
335
336
338
339
340 template <typename IterT>
341 inline Index64 iterCount(const IterT& iter)
342 {
343 Index64 size = 0;
344 for (IterT newIter(iter); newIter; ++newIter, ++size) { }
345 return size;
346 }
347
348
350
351
352 } // namespace points
353 } // namespace OPENVDB_VERSION_NAME
354 } // namespace openvdb
355
356 #endif // OPENVDB_POINTS_INDEX_ITERATOR_HAS_BEEN_INCLUDED
357
358 // Copyright (c) 2012-2018 DreamWorks Animation LLC
359 // All rights reserved. This software is distributed under the
360 // Mozilla Public License 2.0 ( http://www.mozilla.org/MPL/2.0/ )
openvdb::v6_1::points::IndexIter::operator=
IndexIter & operator=(const IndexIter &other)
Definition: IndexIterator.h:263
openvdb::v6_1::points::IndexIter::getCoord
Coord getCoord() const
Return the coordinates of the item to which the value iterator is pointing.
Definition: IndexIterator.h:320
openvdb::v6_1::points::IndexIter::ValueIndexIter::getCoord
void getCoord(Coord &xyz) const
Return in xyz the coordinates of the item to which the value iterator is pointing.
Definition: IndexIterator.h:223
openvdb::v6_1::points::NullFilter::reset
void reset(const LeafT &)
Definition: IndexIterator.h:85
openvdb::v6_1::points::IndexIter::ValueIndexIter::next
bool next()
Advance to the next (valid) item.
Definition: IndexIterator.h:217
Types.h
openvdb::v6_1::points::IndexIter::operator!=
bool operator!=(const IndexIter &other) const
Definition: IndexIterator.h:329
openvdb::v6_1::points::NullFilter::valid
static bool valid(const IterT &)
Definition: IndexIterator.h:86
openvdb::v6_1::points::IndexIter::ValueIndexIter::end
Index32 end() const
Definition: IndexIterator.h:191
openvdb::v6_1::points::ValueVoxelCIter::reset
void reset(Index32, Index32)
Definition: IndexIterator.h:122
openvdb::v6_1::points::ValueVoxelCIter::ValueVoxelCIter
ValueVoxelCIter(const ValueVoxelCIter &other)
Definition: IndexIterator.h:108
openvdb::v6_1::points::IndexIter::operator++
IndexIter & operator++()
Advance to the next (valid) item (prefix).
Definition: IndexIterator.h:295
openvdb::v6_1::points::IndexIter::ValueIndexIter::test
bool test() const
Definition: IndexIterator.h:204
openvdb::v6_1::points::ValueVoxelCIter::test
bool test() const
Definition: IndexIterator.h:119
openvdb::v6_1::points::IndexIter::ValueIndexIter::operator!=
bool operator!=(const ValueIndexIter &other) const
Definition: IndexIterator.h:233
openvdb::v6_1::points::IndexIter::isValueOn
bool isValueOn() const
Return true if the value iterator is pointing to an active value.
Definition: IndexIterator.h:325
openvdb::v6_1::points::IndexIter::ValueIndexIter::ValueIndexIter
ValueIndexIter(const ValueIndexIter &other)
Definition: IndexIterator.h:184
openvdb::v6_1::points::IndexIter::reset
void reset(Index32 begin, Index32 end)
Reset the begining and end of the iterator.
Definition: IndexIterator.h:279
OPENVDB_THROW
#define OPENVDB_THROW(exception, message)
Definition: Exceptions.h:109
openvdb::v6_1::points::IndexIter::ValueIndexIter::reset
void reset(Index32 item, Index32 end)
Definition: IndexIterator.h:193
openvdb::v6_1::points::ValueVoxelCIter::Parent::getValue
Index32 getValue(unsigned) const
Definition: IndexIterator.h:98
openvdb::v6_1::points::IndexIter
A forward iterator over array indices with filtering IteratorT can be either IndexIter or ValueIndexI...
Definition: IndexIterator.h:166
openvdb::v6_1::points::NullFilter
A no-op filter that can be used when iterating over all indices.
Definition: IndexIterator.h:77
openvdb::v6_1::points::ValueVoxelCIter::next
bool next()
Definition: IndexIterator.h:126
openvdb::v6_1::points::IndexIter::ValueIndexIter
A forward iterator over array indices from a value iterator (such as ValueOnCIter)
Definition: IndexIterator.h:170
openvdb::v6_1::points::IndexIter::IndexIter
IndexIter(const IteratorT &iterator, const FilterT &filter)
Definition: IndexIterator.h:242
openvdb::v6_1::points::IndexIter::ValueIndexIter::operator++
ValueIndexIter & operator++()
Advance to the next (valid) item (prefix).
Definition: IndexIterator.h:207
openvdb::v6_1::points::IndexIter::test
bool test() const
Definition: IndexIterator.h:292
openvdb::v6_1::points::NullFilter::state
static index::State state()
Definition: IndexIterator.h:81
openvdb::v6_1::points::ValueVoxelCIter::end
Index32 end() const
Definition: IndexIterator.h:120
openvdb::v6_1::points::ValueVoxelCIter::ValueVoxelCIter
ValueVoxelCIter(Index32 prevOffset, Index32 offset)
Definition: IndexIterator.h:106
OPENVDB_VERSION_NAME
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h:125
openvdb::v6_1::points::IndexIter::getCoord
void getCoord(Coord &xyz) const
Return in xyz the coordinates of the item to which the value iterator is pointing.
Definition: IndexIterator.h:322
openvdb::v6_1::points::NullFilter::state
static index::State state(const LeafT &)
Definition: IndexIterator.h:83
openvdb::v6_1::points::ValueVoxelCIter::offset
Index32 offset()
Definition: IndexIterator.h:125
openvdb::v6_1::Index64
uint64_t Index64
Definition: Types.h:60
openvdb::v6_1::math::operator *
Mat3< typename promote< T0, T1 >::type > operator *(const Mat3< T0 > &m0, const Mat3< T1 > &m1)
Multiply m0 by m1 and return the resulting matrix.
Definition: Mat3.h:645
openvdb
Definition: Exceptions.h:40
openvdb::v6_1::points::IndexIter::filter
const FilterT & filter() const
Return the const filter.
Definition: IndexIterator.h:317
openvdb::v6_1::points::IndexIter::operator==
bool operator==(const IndexIter &other) const
Equality operators.
Definition: IndexIterator.h:328
openvdb::v6_1::points::IndexIter::ValueIndexIter::getCoord
Coord getCoord() const
Return the coordinates of the item to which the value iterator is pointing.
Definition: IndexIterator.h:221
openvdb::v6_1::points::ValueVoxelCIter::Parent::Parent
Parent(Index32 offset)
Definition: IndexIterator.h:97
openvdb::v6_1::RuntimeError
Definition: Exceptions.h:90
openvdb::v6_1::points::IndexIter::ValueIndexIter::operator==
bool operator==(const ValueIndexIter &other) const
Equality operators.
Definition: IndexIterator.h:232
openvdb::v6_1::points::IndexIter::end
Index32 end() const
Definition: IndexIterator.h:276
openvdb::v6_1::points::IndexIter::ValueIndexIter::ValueIndexIter
ValueIndexIter(const IteratorT &iter)
Definition: IndexIterator.h:173
version.h
Library and file format version numbers.
openvdb::v6_1::points::IndexIter::ValueIndexIter::valueIter
const IteratorT & valueIter() const
Return the const value iterator.
Definition: IndexIterator.h:229
openvdb::v6_1::points::ValueVoxelCIter::Parent
Definition: IndexIterator.h:94
openvdb::v6_1::points::NullFilter::initialized
static bool initialized()
Definition: IndexIterator.h:80
openvdb::v6_1::points::IndexIter::increment
bool increment()
Definition: IndexIterator.h:314
openvdb::v6_1::math::Coord
Signed (x, y, z) 32-bit integer coordinates.
Definition: Coord.h:52
openvdb::v6_1::points::index::ALL
Definition: IndexIterator.h:70
openvdb::v6_1::points::ValueVoxelCIter::operator++
ValueVoxelCIter & operator++()
Advance to the next (valid) item (prefix).
Definition: IndexIterator.h:116
openvdb::v6_1::points::ValueVoxelCIter::operator!=
bool operator!=(const ValueVoxelCIter &other) const
Equality operators.
Definition: IndexIterator.h:143
openvdb::v6_1::points::ValueVoxelCIter::operator==
bool operator==(const ValueVoxelCIter &other) const
Equality operators.
Definition: IndexIterator.h:142
openvdb::v6_1::points::IndexIter::ValueIndexIter::isValueOn
bool isValueOn() const
Return true if this iterator is pointing to an active value.
Definition: IndexIterator.h:226
openvdb::v6_1::points::IndexIter::IndexIter
IndexIter(const IndexIter &other)
Definition: IndexIterator.h:254
OPENVDB_USE_VERSION_NAMESPACE
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:177
openvdb::v6_1::math::operator==
bool operator==(const Vec3< T0 > &v0, const Vec3< T1 > &v1)
Equality operator, does exact floating point comparisons.
Definition: Vec3.h:498
openvdb::v6_1::points::ValueVoxelCIter
A forward iterator over array indices in a single voxel.
Definition: IndexIterator.h:91
openvdb::v6_1::points::index::State
State
Definition: IndexIterator.h:66
openvdb::v6_1::points::IndexIter::ValueIndexIter::increment
bool increment()
Definition: IndexIterator.h:218
openvdb::v6_1::points::index::PARTIAL
Definition: IndexIterator.h:68
openvdb::v6_1::points::IndexIter::operator++
IndexIter operator++(int)
Advance to the next (valid) item (postfix).
Definition: IndexIterator.h:306
openvdb::v6_1::points::IndexIter::next
bool next()
Advance to the next (valid) item.
Definition: IndexIterator.h:313
openvdb::v6_1::points::ValueVoxelCIter::parent
Parent & parent()
Definition: IndexIterator.h:124
openvdb::v6_1::points::index::NONE
Definition: IndexIterator.h:69
openvdb::v6_1::Index32
uint32_t Index32
Definition: Types.h:59
openvdb::v6_1::points::iterCount
Index64 iterCount(const IterT &iter)
Count up the number of times the iterator can iterate.
Definition: IndexIterator.h:341