OpenVDB 7.0.0

IndexIterator.h
Go to the documentation of this file.
1 // Copyright Contributors to the OpenVDB Project
2 // SPDX-License-Identifier: MPL-2.0
3
9
10 #ifndef OPENVDB_POINTS_INDEX_ITERATOR_HAS_BEEN_INCLUDED
11 #define OPENVDB_POINTS_INDEX_ITERATOR_HAS_BEEN_INCLUDED
12
13 #include <openvdb/version.h>
14 #include <openvdb/Types.h>
15
16 namespace openvdb {
17 OPENVDB_USE_VERSION_NAMESPACE
18 namespace OPENVDB_VERSION_NAME {
19 namespace points {
20
21
27 template <typename IterT>
28 inline Index64 iterCount(const IterT& iter);
29
30
32
33
34 namespace index {
35 // Enum for informing early-exit optimizations
36 // PARTIAL - No optimizations are possible
37 // NONE - No indices to evaluate, can skip computation
38 // ALL - All indices to evaluate, can skip filtering
39 enum State
40 {
41 PARTIAL=0,
42 NONE,
43 ALL
44 };
45 }
46
47
50 class NullFilter
51 {
52 public:
53 static bool initialized() { return true; }
54 static index::State state() { return index::ALL; }
55 template <typename LeafT>
56 static index::State state(const LeafT&) { return index::ALL; }
57
58 template <typename LeafT> void reset(const LeafT&) { }
59 template <typename IterT> static bool valid(const IterT&) { return true; }
60 }; // class NullFilter
61
62
64 class ValueVoxelCIter
65 {
66 public:
67 struct Parent
68 {
69 Parent() = default;
70 explicit Parent(Index32 offset): mOffset(offset) { }
71 Index32 getValue(unsigned /*offset*/) const { return mOffset; }
72 private:
73 Index32 mOffset = 0;
74 }; // struct Parent
75
76 using NodeType = Parent;
77
78 ValueVoxelCIter() = default;
79 ValueVoxelCIter(Index32 prevOffset, Index32 offset)
80 : mOffset(offset), mParent(prevOffset) {}
81 ValueVoxelCIter(const ValueVoxelCIter& other)
82 : mOffset(other.mOffset), mParent(other.mParent), mValid(other.mValid) {}
83
85 Index32 operator*() { return mOffset; }
86 Index32 operator*() const { return mOffset; }
87
89 ValueVoxelCIter& operator++() { mValid = false; return *this; }
90
91 operator bool() const { return mValid; }
92 bool test() const { return mValid; }
93 Index32 end() const { return mOffset+1; }
94
95 void reset(Index32 /*item*/, Index32 /*end*/) {}
96
97 Parent& parent() { return mParent; }
98 Index32 offset() { return mOffset; }
99 inline bool next() { this->operator++(); return this->test(); }
100
103 Coord getCoord [[noreturn]] () const {
104 OPENVDB_THROW(RuntimeError, "ValueVoxelCIter does not provide a valid Coord.");
105 }
106 void getCoord [[noreturn]] (Coord& /*coord*/) const {
107 OPENVDB_THROW(RuntimeError, "ValueVoxelCIter does not provide a valid Coord.");
108 }
109 bool isValueOn [[noreturn]] () const {
110 OPENVDB_THROW(RuntimeError, "ValueVoxelCIter does not test if voxel is active.");
111 }
112
115 bool operator==(const ValueVoxelCIter& other) const { return mOffset == other.mOffset; }
116 bool operator!=(const ValueVoxelCIter& other) const { return !this->operator==(other); }
118
119 private:
120 Index32 mOffset = 0;
121 Parent mParent;
122 mutable bool mValid = true;
123 }; // class ValueVoxelCIter
124
125
138 template <typename IteratorT, typename FilterT>
139 class IndexIter
140 {
141 public:
143 class ValueIndexIter
144 {
145 public:
146 ValueIndexIter(const IteratorT& iter)
147 : mIter(iter), mParent(&mIter.parent())
148 {
149 if (mIter) {
150 assert(mParent);
151 Index32 start = (mIter.offset() > 0 ?
152 Index32(mParent->getValue(mIter.offset() - 1)) : Index32(0));
153 this->reset(start, *mIter);
154 if (mItem >= mEnd) this->operator++();
155 }
156 }
157 ValueIndexIter(const ValueIndexIter& other)
158 : mEnd(other.mEnd), mItem(other.mItem), mIter(other.mIter), mParent(other.mParent)
159 {
160 assert(mParent);
161 }
162 ValueIndexIter& operator=(const ValueIndexIter&) = default;
163
164 inline Index32 end() const { return mEnd; }
165
166 inline void reset(Index32 item, Index32 end) {
167 mItem = item;
168 mEnd = end;
169 }
170
172 inline Index32 operator*() { assert(mIter); return mItem; }
173 inline Index32 operator*() const { assert(mIter); return mItem; }
174
176 inline operator bool() const { return mIter; }
177 inline bool test() const { return mIter; }
178
180 inline ValueIndexIter& operator++() {
181 ++mItem;
182 while (mItem >= mEnd && mIter.next()) {
183 assert(mParent);
184 this->reset(mParent->getValue(mIter.offset() - 1), *mIter);
185 }
186 return *this;
187 }
188
190 inline bool next() { this->operator++(); return this->test(); }
191 inline bool increment() { this->next(); return this->test(); }
192
194 inline Coord getCoord() const { assert(mIter); return mIter.getCoord(); }
196 inline void getCoord(Coord& xyz) const { assert(mIter); xyz = mIter.getCoord(); }
197
199 inline bool isValueOn() const { assert(mIter); return mIter.isValueOn(); }
200
202 inline const IteratorT& valueIter() const { return mIter; }
203
205 bool operator==(const ValueIndexIter& other) const { return mItem == other.mItem; }
206 bool operator!=(const ValueIndexIter& other) const { return !this->operator==(other); }
207
208 private:
209 Index32 mEnd = 0;
210 Index32 mItem = 0;
211 IteratorT mIter;
212 const typename IteratorT::NodeType* mParent;
213 }; // ValueIndexIter
214
215 IndexIter(const IteratorT& iterator, const FilterT& filter)
216 : mIterator(iterator)
217 , mFilter(filter)
218 {
219 if (!mFilter.initialized()) {
220 OPENVDB_THROW(RuntimeError,
221 "Filter needs to be initialized before constructing the iterator.");
222 }
223 if (mIterator) {
224 this->reset(*mIterator, mIterator.end());
225 }
226 }
227 IndexIter(const IndexIter& other)
228 : mIterator(other.mIterator)
229 , mFilter(other.mFilter)
230 {
231 if (!mFilter.initialized()) {
232 OPENVDB_THROW(RuntimeError,
233 "Filter needs to be initialized before constructing the iterator.");
234 }
235 }
236 IndexIter& operator=(const IndexIter& other)
237 {
238 if (&other != this) {
239 mIterator = other.mIterator;
240 mFilter = other.mFilter;
241 if (!mFilter.initialized()) {
242 OPENVDB_THROW(RuntimeError,
243 "Filter needs to be initialized before constructing the iterator.");
244 }
245 }
246 return *this;
247 }
248
249 Index32 end() const { return mIterator.end(); }
250
252 void reset(Index32 begin, Index32 end) {
253 mIterator.reset(begin, end);
254 while (mIterator.test() && !mFilter.template valid<ValueIndexIter>(mIterator)) {
255 ++mIterator;
256 }
257 }
258
260 Index32 operator*() { assert(mIterator); return *mIterator; }
261 Index32 operator*() const { assert(mIterator); return *mIterator; }
262
264 operator bool() const { return mIterator.test(); }
265 bool test() const { return mIterator.test(); }
266
268 IndexIter& operator++() {
269 while (true) {
270 ++mIterator;
271 if (!mIterator.test() || mFilter.template valid<ValueIndexIter>(mIterator)) {
272 break;
273 }
274 }
275 return *this;
276 }
277
279 IndexIter operator++(int /*dummy*/) {
280 IndexIter newIterator(*this);
281 this->operator++();
282 return newIterator;
283 }
284
286 bool next() { this->operator++(); return this->test(); }
287 bool increment() { this->next(); return this->test(); }
288
290 inline const FilterT& filter() const { return mFilter; }
291
293 inline Coord getCoord() const { assert(mIterator); return mIterator.getCoord(); }
295 inline void getCoord(Coord& xyz) const { assert(mIterator); xyz = mIterator.getCoord(); }
296
298 inline bool isValueOn() const { assert(mIterator); return mIterator.valueIter().isValueOn(); }
299
301 bool operator==(const IndexIter& other) const { return mIterator == other.mIterator; }
302 bool operator!=(const IndexIter& other) const { return !this->operator==(other); }
303
304 private:
305 ValueIndexIter mIterator;
306 FilterT mFilter;
307 }; // class IndexIter
308
309
311
312
313 template <typename IterT>
314 inline Index64 iterCount(const IterT& iter)
315 {
316 Index64 size = 0;
317 for (IterT newIter(iter); newIter; ++newIter, ++size) { }
318 return size;
319 }
320
321
323
324
325 } // namespace points
326 } // namespace OPENVDB_VERSION_NAME
327 } // namespace openvdb
328
329 #endif // OPENVDB_POINTS_INDEX_ITERATOR_HAS_BEEN_INCLUDED
openvdb::v7_0::points::IndexIter::ValueIndexIter::operator++
ValueIndexIter & operator++()
Advance to the next (valid) item (prefix).
Definition: IndexIterator.h:180
openvdb::v7_0::points::ValueVoxelCIter::parent
Parent & parent()
Definition: IndexIterator.h:97
openvdb::v7_0::points::IndexIter::getCoord
Coord getCoord() const
Return the coordinates of the item to which the value iterator is pointing.
Definition: IndexIterator.h:293
openvdb::v7_0::points::NullFilter::reset
void reset(const LeafT &)
Definition: IndexIterator.h:58
openvdb::v7_0::points::IndexIter::IndexIter
IndexIter(const IndexIter &other)
Definition: IndexIterator.h:227
Types.h
openvdb::v7_0::points::ValueVoxelCIter::test
bool test() const
Definition: IndexIterator.h:92
openvdb::v7_0::points::IndexIter::ValueIndexIter::reset
void reset(Index32 item, Index32 end)
Definition: IndexIterator.h:166
openvdb::v7_0::math::Coord
Signed (x, y, z) 32-bit integer coordinates.
Definition: Coord.h:25
openvdb::v7_0::points::ValueVoxelCIter::operator*
Index32 operator*() const
Definition: IndexIterator.h:86
openvdb::v7_0::points::IndexIter::reset
void reset(Index32 begin, Index32 end)
Reset the begining and end of the iterator.
Definition: IndexIterator.h:252
openvdb::v7_0::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:295
openvdb::v7_0::points::ValueVoxelCIter::Parent
Definition: IndexIterator.h:67
openvdb::v7_0::points::index::NONE
Definition: IndexIterator.h:42
openvdb::v7_0::points::ValueVoxelCIter::next
bool next()
Definition: IndexIterator.h:99
openvdb::v7_0::points::IndexIter::ValueIndexIter::getCoord
Coord getCoord() const
Return the coordinates of the item to which the value iterator is pointing.
Definition: IndexIterator.h:194
openvdb::v7_0::points::IndexIter::operator*
Index32 operator*() const
Definition: IndexIterator.h:261
openvdb::v7_0::points::ValueVoxelCIter::end
Index32 end() const
Definition: IndexIterator.h:93
openvdb::v7_0::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:196
openvdb::v7_0::points::IndexIter::operator++
IndexIter operator++(int)
Advance to the next (valid) item (postfix).
Definition: IndexIterator.h:279
openvdb::v7_0::points::NullFilter::valid
static bool valid(const IterT &)
Definition: IndexIterator.h:59
version.h
Library and file format version numbers.
openvdb::v7_0::points::index::PARTIAL
Definition: IndexIterator.h:41
openvdb::v7_0::points::IndexIter::test
bool test() const
Definition: IndexIterator.h:265
openvdb::v7_0::points::IndexIter::ValueIndexIter::end
Index32 end() const
Definition: IndexIterator.h:164
openvdb::v7_0::points::IndexIter::filter
const FilterT & filter() const
Return the const filter.
Definition: IndexIterator.h:290
openvdb::v7_0::points::ValueVoxelCIter::ValueVoxelCIter
ValueVoxelCIter(const ValueVoxelCIter &other)
Definition: IndexIterator.h:81
openvdb::v7_0::points::IndexIter::ValueIndexIter::valueIter
const IteratorT & valueIter() const
Return the const value iterator.
Definition: IndexIterator.h:202
openvdb::v7_0::points::NullFilter::state
static index::State state()
Definition: IndexIterator.h:54
openvdb::v7_0::points::IndexIter::ValueIndexIter::ValueIndexIter
ValueIndexIter(const IteratorT &iter)
Definition: IndexIterator.h:146
openvdb::v7_0::points::IndexIter::ValueIndexIter::next
bool next()
Advance to the next (valid) item.
Definition: IndexIterator.h:190
openvdb::v7_0::points::NullFilter::state
static index::State state(const LeafT &)
Definition: IndexIterator.h:56
openvdb::v7_0::points::ValueVoxelCIter::operator!=
bool operator!=(const ValueVoxelCIter &other) const
Equality operators.
Definition: IndexIterator.h:116
openvdb::v7_0::points::IndexIter::end
Index32 end() const
Definition: IndexIterator.h:249
openvdb::v7_0::points::iterCount
Index64 iterCount(const IterT &iter)
Count up the number of times the iterator can iterate.
Definition: IndexIterator.h:314
openvdb::v7_0::points::IndexIter::operator=
IndexIter & operator=(const IndexIter &other)
Definition: IndexIterator.h:236
openvdb::v7_0::points::ValueVoxelCIter::offset
Index32 offset()
Definition: IndexIterator.h:98
openvdb::v7_0::math::operator==
bool operator==(const Vec3< T0 > &v0, const Vec3< T1 > &v1)
Equality operator, does exact floating point comparisons.
Definition: Vec3.h:471
openvdb::v7_0::points::IndexIter::ValueIndexIter::test
bool test() const
Definition: IndexIterator.h:177
openvdb::v7_0::points::IndexIter::ValueIndexIter::operator*
Index32 operator*()
Returns the item to which this iterator is currently pointing.
Definition: IndexIterator.h:172
openvdb::v7_0::points::ValueVoxelCIter::reset
void reset(Index32, Index32)
Definition: IndexIterator.h:95
openvdb::v7_0::points::IndexIter::ValueIndexIter::isValueOn
bool isValueOn() const
Return true if this iterator is pointing to an active value.
Definition: IndexIterator.h:199
openvdb::v7_0::points::IndexIter::ValueIndexIter::operator!=
bool operator!=(const ValueIndexIter &other) const
Definition: IndexIterator.h:206
openvdb::v7_0::points::IndexIter::ValueIndexIter::ValueIndexIter
ValueIndexIter(const ValueIndexIter &other)
Definition: IndexIterator.h:157
openvdb::v7_0::points::IndexIter
A forward iterator over array indices with filtering IteratorT can be either IndexIter or ValueIndexI...
Definition: IndexIterator.h:139
openvdb::v7_0::points::IndexIter::ValueIndexIter::increment
bool increment()
Definition: IndexIterator.h:191
openvdb::v7_0::RuntimeError
Definition: Exceptions.h:63
OPENVDB_USE_VERSION_NAMESPACE
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:154
openvdb::v7_0::points::ValueVoxelCIter::Parent::Parent
Parent(Index32 offset)
Definition: IndexIterator.h:70
openvdb::v7_0::points::IndexIter::operator++
IndexIter & operator++()
Advance to the next (valid) item (prefix).
Definition: IndexIterator.h:268
openvdb::v7_0::points::NullFilter
A no-op filter that can be used when iterating over all indices.
Definition: IndexIterator.h:50
openvdb::v7_0::points::ValueVoxelCIter::operator++
ValueVoxelCIter & operator++()
Advance to the next (valid) item (prefix).
Definition: IndexIterator.h:89
openvdb::v7_0::points::IndexIter::operator!=
bool operator!=(const IndexIter &other) const
Definition: IndexIterator.h:302
openvdb::v7_0::points::IndexIter::isValueOn
bool isValueOn() const
Return true if the value iterator is pointing to an active value.
Definition: IndexIterator.h:298
openvdb::v7_0::points::ValueVoxelCIter::operator==
bool operator==(const ValueVoxelCIter &other) const
Equality operators.
Definition: IndexIterator.h:115
openvdb::v7_0::points::IndexIter::operator*
Index32 operator*()
Returns the item to which this iterator is currently pointing.
Definition: IndexIterator.h:260
OPENVDB_VERSION_NAME
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h:102
openvdb::v7_0::points::ValueVoxelCIter::Parent::getValue
Index32 getValue(unsigned) const
Definition: IndexIterator.h:71
openvdb::v7_0::points::ValueVoxelCIter::operator*
Index32 operator*()
Return the item to which this iterator is currently pointing.
Definition: IndexIterator.h:85
openvdb::v7_0::points::index::ALL
Definition: IndexIterator.h:43
openvdb::v7_0::points::IndexIter::ValueIndexIter::operator*
Index32 operator*() const
Definition: IndexIterator.h:173
openvdb::v7_0::points::IndexIter::increment
bool increment()
Definition: IndexIterator.h:287
openvdb::v7_0::Index32
uint32_t Index32
Definition: Types.h:29
openvdb::v7_0::points::ValueVoxelCIter
A forward iterator over array indices in a single voxel.
Definition: IndexIterator.h:64
openvdb::v7_0::points::IndexIter::ValueIndexIter
A forward iterator over array indices from a value iterator (such as ValueOnCIter)
Definition: IndexIterator.h:143
openvdb::v7_0::points::ValueVoxelCIter::ValueVoxelCIter
ValueVoxelCIter(Index32 prevOffset, Index32 offset)
Definition: IndexIterator.h:79
openvdb
Definition: Exceptions.h:13
openvdb::v7_0::points::IndexIter::ValueIndexIter::operator==
bool operator==(const ValueIndexIter &other) const
Equality operators.
Definition: IndexIterator.h:205
openvdb::v7_0::Index64
uint64_t Index64
Definition: Types.h:30
openvdb::v7_0::points::index::State
State
Definition: IndexIterator.h:39
openvdb::v7_0::points::NullFilter::initialized
static bool initialized()
Definition: IndexIterator.h:53
openvdb::v7_0::points::IndexIter::next
bool next()
Advance to the next (valid) item.
Definition: IndexIterator.h:286
OPENVDB_THROW
#define OPENVDB_THROW(exception, message)
Definition: Exceptions.h:82
openvdb::v7_0::points::IndexIter::IndexIter
IndexIter(const IteratorT &iterator, const FilterT &filter)
Definition: IndexIterator.h:215
openvdb::v7_0::points::IndexIter::operator==
bool operator==(const IndexIter &other) const
Equality operators.
Definition: IndexIterator.h:301