OpenVDB: IndexIterator.h Source File

OpenVDB 5.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
62 class NullFilter
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
72 class ValueVoxelCIter
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) {}
89 ValueVoxelCIter(const ValueVoxelCIter& other)
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>
144 class IndexIter
145 {
146 public:
148 class ValueIndexIter
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 }
162 ValueIndexIter(const ValueIndexIter& other)
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
185 inline ValueIndexIter& operator++() {
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()) {
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 }
238 IndexIter& operator=(const IndexIter& other)
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
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
270 IndexIter& operator++() {
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-2018 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/ )
openvdb::v5_1::Index32
uint32_t Index32
Definition: Types.h:59
openvdb::v5_1::points::IndexIter::increment
bool increment()
Definition: IndexIterator.h:289
openvdb::v5_1::points::IndexIter::ValueIndexIter::end
Index32 end() const
Definition: IndexIterator.h:169
openvdb::v5_1::points::IndexIter::ValueIndexIter::test
bool test() const
Definition: IndexIterator.h:182
Types.h
openvdb::v5_1::points::IndexIter::ValueIndexIter::operator!=
bool operator!=(const ValueIndexIter &other) const
Definition: IndexIterator.h:208
openvdb::v5_1::points::IndexIter::operator*
Index32 operator*()
Returns the item to which this iterator is currently pointing.
Definition: IndexIterator.h:262
openvdb::v5_1::points::IndexIter::IndexIter
IndexIter(const IteratorT &iterator, const FilterT &filter)
Definition: IndexIterator.h:217
openvdb::v5_1::points::ValueVoxelCIter::Parent::Parent
Parent(Index32 offset)
Definition: IndexIterator.h:78
openvdb::v5_1::points::IndexIter::ValueIndexIter::operator*
Index32 operator*() const
Definition: IndexIterator.h:178
openvdb::v5_1::points::IndexIter::ValueIndexIter::operator==
bool operator==(const ValueIndexIter &other) const
Equality operators.
Definition: IndexIterator.h:207
openvdb::v5_1::points::IndexIter::IndexIter
IndexIter(const IndexIter &other)
Definition: IndexIterator.h:229
openvdb::v5_1::points::IndexIter::ValueIndexIter::reset
void reset(Index32 item, Index32 end)
Definition: IndexIterator.h:171
openvdb::v5_1::points::IndexIter::ValueIndexIter::ValueIndexIter
ValueIndexIter(const ValueIndexIter &other)
Definition: IndexIterator.h:162
OPENVDB_THROW
#define OPENVDB_THROW(exception, message)
Definition: Exceptions.h:109
openvdb::v5_1::points::ValueVoxelCIter::ValueVoxelCIter
ValueVoxelCIter(const ValueVoxelCIter &other)
Definition: IndexIterator.h:89
openvdb::v5_1::points::IndexIter::getCoord
Coord getCoord() const
Return the coordinates of the item to which the value iterator is pointing.
Definition: IndexIterator.h:295
openvdb::v5_1::points::IndexIter::ValueIndexIter::ValueIndexIter
ValueIndexIter(const IteratorT &iter)
Definition: IndexIterator.h:151
openvdb::v5_1::points::IndexIter
A forward iterator over array indices with filtering IteratorT can be either IndexIter or ValueIndexI...
Definition: IndexIterator.h:144
openvdb::v5_1::points::IndexIter::ValueIndexIter::valueIter
const IteratorT & valueIter() const
Return the const value iterator.
Definition: IndexIterator.h:204
openvdb::v5_1::points::ValueVoxelCIter::parent
Parent & parent()
Definition: IndexIterator.h:105
openvdb::v5_1::points::IndexIter::ValueIndexIter
A forward iterator over array indices from a value iterator (such as ValueOnCIter) ...
Definition: IndexIterator.h:148
openvdb::v5_1::points::IndexIter::next
bool next()
Advance to the next (valid) item.
Definition: IndexIterator.h:288
openvdb::v5_1::points::ValueVoxelCIter::offset
Index32 offset()
Definition: IndexIterator.h:106
openvdb::v5_1::points::IndexIter::operator==
bool operator==(const IndexIter &other) const
Equality operators.
Definition: IndexIterator.h:300
openvdb::v5_1::points::IndexIter::end
Index32 end() const
Definition: IndexIterator.h:251
openvdb::v5_1::points::IndexIter::ValueIndexIter::operator++
ValueIndexIter & operator++()
Advance to the next (valid) item (prefix).
Definition: IndexIterator.h:185
openvdb::v5_1::points::NullFilter
A no-op filter that can be used when iterating over all indices.
Definition: IndexIterator.h:62
openvdb::v5_1::points::NullFilter::reset
void reset(const LeafT &)
Definition: IndexIterator.h:66
openvdb::v5_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:297
openvdb::v5_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:201
openvdb::v5_1::points::IndexIter::operator++
IndexIter & operator++()
Advance to the next (valid) item (prefix).
Definition: IndexIterator.h:270
openvdb::v5_1::points::IndexIter::operator++
IndexIter operator++(int)
Advance to the next (valid) item (postfix).
Definition: IndexIterator.h:281
openvdb::v5_1::points::NullFilter::valid
static bool valid(const IterT &)
Definition: IndexIterator.h:67
OPENVDB_VERSION_NAME
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h:136
openvdb::v5_1::points::IndexIter::ValueIndexIter::next
bool next()
Advance to the next (valid) item.
Definition: IndexIterator.h:195
openvdb::v5_1::points::NullFilter::initialized
static bool initialized()
Definition: IndexIterator.h:65
openvdb::v5_1::points::ValueVoxelCIter::Parent
Definition: IndexIterator.h:75
openvdb::v5_1::points::IndexIter::filter
const FilterT & filter() const
Return the const filter.
Definition: IndexIterator.h:292
openvdb::v5_1::points::IndexIter::reset
void reset(Index32 begin, Index32 end)
Reset the begining and end of the iterator.
Definition: IndexIterator.h:254
openvdb::v5_1::Index64
uint64_t Index64
Definition: Types.h:60
openvdb::v5_1::points::IndexIter::ValueIndexIter::getCoord
Coord getCoord() const
Return the coordinates of the item to which the value iterator is pointing.
Definition: IndexIterator.h:199
openvdb::v5_1::points::ValueVoxelCIter::Parent::getValue
Index32 getValue(unsigned) const
Definition: IndexIterator.h:79
openvdb::v5_1::points::iterCount
Index64 iterCount(const IterT &iter)
Count up the number of times the iterator can iterate.
Definition: IndexIterator.h:313
openvdb
Definition: Exceptions.h:40
openvdb::v5_1::points::ValueVoxelCIter::operator++
ValueVoxelCIter & operator++()
Advance to the next (valid) item (prefix).
Definition: IndexIterator.h:97
openvdb::v5_1::points::IndexIter::test
bool test() const
Definition: IndexIterator.h:267
openvdb::v5_1::points::IndexIter::ValueIndexIter::operator*
Index32 operator*()
Returns the item to which this iterator is currently pointing.
Definition: IndexIterator.h:177
openvdb::v5_1::math::operator==
bool operator==(const Vec3< T0 > &v0, const Vec3< T1 > &v1)
Equality operator, does exact floating point comparisons.
Definition: Vec3.h:488
openvdb::v5_1::points::IndexIter::operator*
Index32 operator*() const
Definition: IndexIterator.h:263
openvdb::v5_1::points::ValueVoxelCIter::operator*
Index32 operator*()
Return the item to which this iterator is currently pointing.
Definition: IndexIterator.h:93
openvdb::v5_1::points::ValueVoxelCIter::operator*
Index32 operator*() const
Definition: IndexIterator.h:94
version.h
Library and file format version numbers.
openvdb::v5_1::points::ValueVoxelCIter::operator!=
bool operator!=(const ValueVoxelCIter &other) const
Equality operators.
Definition: IndexIterator.h:121
openvdb::v5_1::points::ValueVoxelCIter::end
Index32 end() const
Definition: IndexIterator.h:101
openvdb::v5_1::points::ValueVoxelCIter::operator==
bool operator==(const ValueVoxelCIter &other) const
Equality operators.
Definition: IndexIterator.h:120
openvdb::v5_1::math::Coord
Signed (x, y, z) 32-bit integer coordinates.
Definition: Coord.h:51
openvdb::v5_1::points::ValueVoxelCIter::reset
void reset(Index32, Index32)
Definition: IndexIterator.h:103
openvdb::v5_1::points::IndexIter::operator=
IndexIter & operator=(const IndexIter &other)
Definition: IndexIterator.h:238
OPENVDB_USE_VERSION_NAMESPACE
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:188
openvdb::v5_1::points::ValueVoxelCIter
A forward iterator over array indices in a single voxel.
Definition: IndexIterator.h:72
openvdb::v5_1::points::ValueVoxelCIter::ValueVoxelCIter
ValueVoxelCIter(Index32 prevOffset, Index32 offset)
Definition: IndexIterator.h:87
openvdb::v5_1::points::IndexIter::operator!=
bool operator!=(const IndexIter &other) const
Definition: IndexIterator.h:301
openvdb::v5_1::points::ValueVoxelCIter::next
bool next()
Definition: IndexIterator.h:107
openvdb::v5_1::points::ValueVoxelCIter::test
bool test() const
Definition: IndexIterator.h:100
openvdb::v5_1::points::IndexIter::ValueIndexIter::increment
bool increment()
Definition: IndexIterator.h:196
openvdb::v5_1::RuntimeError
Definition: Exceptions.h:90