OpenVDB: Iterator.h Source File

OpenVDB 6.1.0

Iterator.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 //
34
35 #ifndef OPENVDB_TREE_ITERATOR_HAS_BEEN_INCLUDED
36 #define OPENVDB_TREE_ITERATOR_HAS_BEEN_INCLUDED
37
38 #include <sstream>
39 #include <type_traits>
40 #include <openvdb/util/NodeMasks.h>
41 #include <openvdb/Exceptions.h>
42
43 namespace openvdb {
44 OPENVDB_USE_VERSION_NAMESPACE
45 namespace OPENVDB_VERSION_NAME {
46 namespace tree {
47
55 template<typename MaskIterT, typename NodeT>
56 class IteratorBase
57 {
58 public:
59 IteratorBase(): mParentNode(nullptr) {}
60 IteratorBase(const MaskIterT& iter, NodeT* parent): mParentNode(parent), mMaskIter(iter) {}
61 IteratorBase(const IteratorBase&) = default;
62 IteratorBase& operator=(const IteratorBase&) = default;
63
64 bool operator==(const IteratorBase& other) const
65 {
66 return (mParentNode == other.mParentNode) && (mMaskIter == other.mMaskIter);
67 }
68 bool operator!=(const IteratorBase& other) const
69 {
70 return !(*this == other);
71 }
72
74 NodeT* getParentNode() const { return mParentNode; }
77 NodeT& parent() const
78 {
79 if (!mParentNode) OPENVDB_THROW(ValueError, "iterator references a null node");
80 return *mParentNode;
81 }
82
84 Index offset() const { return mMaskIter.offset(); }
85
87 Index pos() const { return mMaskIter.offset(); }
88
90 bool test() const { return mMaskIter.test(); }
92 operator bool() const { return this->test(); }
93
95 bool next() { return mMaskIter.next(); }
97 void increment() { mMaskIter.increment(); }
99 IteratorBase& operator++() { this->increment(); return *this; }
101 void increment(Index n) { mMaskIter.increment(n); }
102
105 bool isValueOn() const { return parent().isValueMaskOn(this->pos()); }
108 void setValueOn(bool on = true) const { parent().setValueMask(this->pos(), on); }
113 void setValueOff() const { parent().mValueMask.setOff(this->pos()); }
114
116 Coord getCoord() const { return parent().offsetToGlobalCoord(this->pos()); }
118 void getCoord(Coord& xyz) const { xyz = this->getCoord(); }
119
120 private:
127 mutable NodeT* mParentNode;
128 MaskIterT mMaskIter;
129 }; // class IteratorBase
130
131
133
134
136 template<
137 typename MaskIterT, // mask iterator type (OnIterator, OffIterator, etc.)
138 typename IterT, // SparseIteratorBase subclass (the "Curiously Recurring Template Pattern")
139 typename NodeT, // type of node over which to iterate
140 typename ItemT> // type of value to which this iterator points
141 struct SparseIteratorBase: public IteratorBase<MaskIterT, NodeT>
142 {
143 using NodeType = NodeT;
144 using ValueType = ItemT;
145 using NonConstNodeType = typename std::remove_const<NodeT>::type;
146 using NonConstValueType = typename std::remove_const<ItemT>::type;
147 static const bool IsSparseIterator = true, IsDenseIterator = false;
148
149 SparseIteratorBase() {}
150 SparseIteratorBase(const MaskIterT& iter, NodeT* parent):
151 IteratorBase<MaskIterT, NodeT>(iter, parent) {}
152
155 ItemT& getItem(Index) const;
158 void setItem(Index, const ItemT&) const;
159
161 ItemT& operator*() const { return this->getValue(); }
163 ItemT* operator->() const { return &(this->operator*()); }
164
166 ItemT& getValue() const
167 {
168 return static_cast<const IterT*>(this)->getItem(this->pos()); // static polymorphism
169 }
172 void setValue(const ItemT& value) const
173 {
174 static_assert(!std::is_const<NodeT>::value, "setValue() not allowed for const iterators");
175 static_cast<const IterT*>(this)->setItem(this->pos(), value); // static polymorphism
176 }
182 template<typename ModifyOp>
183 void modifyValue(const ModifyOp& op) const
184 {
185 static_assert(!std::is_const<NodeT>::value,
186 "modifyValue() not allowed for const iterators");
187 static_cast<const IterT*>(this)->modifyItem(this->pos(), op); // static polymorphism
188 }
189 }; // class SparseIteratorBase
190
191
193
194
199 template<
200 typename MaskIterT, // mask iterator type (typically a DenseIterator)
201 typename IterT, // DenseIteratorBase subclass (the "Curiously Recurring Template Pattern")
202 typename NodeT, // type of node over which to iterate
203 typename SetItemT, // type of set value (ChildNodeType, for non-leaf nodes)
204 typename UnsetItemT> // type of unset value (ValueType, usually)
205 struct DenseIteratorBase: public IteratorBase<MaskIterT, NodeT>
206 {
207 using NodeType = NodeT;
208 using ValueType = UnsetItemT;
209 using ChildNodeType = SetItemT;
210 using NonConstNodeType = typename std::remove_const<NodeT>::type;
211 using NonConstValueType = typename std::remove_const<UnsetItemT>::type;
212 using NonConstChildNodeType = typename std::remove_const<SetItemT>::type;
213 static const bool IsSparseIterator = false, IsDenseIterator = true;
214
215 DenseIteratorBase() {}
216 DenseIteratorBase(const MaskIterT& iter, NodeT* parent):
217 IteratorBase<MaskIterT, NodeT>(iter, parent) {}
218
223 bool getItem(Index, SetItemT*& child, NonConstValueType& value) const;
226 void setItem(Index, SetItemT*) const;
229 void unsetItem(Index, const UnsetItemT&) const;
230
232 bool isChildNode() const { return this->parent().isChildMaskOn(this->pos()); }
233
236 SetItemT* probeChild(NonConstValueType& value) const
237 {
238 SetItemT* child = nullptr;
239 static_cast<const IterT*>(this)->getItem(this->pos(), child, value); // static polymorphism
240 return child;
241 }
245 bool probeChild(SetItemT*& child, NonConstValueType& value) const
246 {
247 child = probeChild(value);
248 return (child != nullptr);
249 }
250
253 bool probeValue(NonConstValueType& value) const
254 {
255 SetItemT* child = nullptr;
256 const bool isChild = static_cast<const IterT*>(this)-> // static polymorphism
257 getItem(this->pos(), child, value);
258 return !isChild;
259 }
260
263 void setChild(SetItemT* child) const
264 {
265 static_cast<const IterT*>(this)->setItem(this->pos(), child); // static polymorphism
266 }
267
270 void setValue(const UnsetItemT& value) const
271 {
272 static_cast<const IterT*>(this)->unsetItem(this->pos(), value); // static polymorphism
273 }
274 }; // struct DenseIteratorBase
275
276 } // namespace tree
277 } // namespace OPENVDB_VERSION_NAME
278 } // namespace openvdb
279
280 #endif // OPENVDB_TREE_ITERATOR_HAS_BEEN_INCLUDED
281
282 // Copyright (c) 2012-2018 DreamWorks Animation LLC
283 // All rights reserved. This software is distributed under the
284 // Mozilla Public License 2.0 ( http://www.mozilla.org/MPL/2.0/ )
openvdb::v6_1::tree::DenseIteratorBase::isChildNode
bool isChildNode() const
Return true if this iterator is pointing to a child node.
Definition: Iterator.h:232
openvdb::v6_1::tree::SparseIteratorBase< MaskIterT, ValueIter< MaskIterT, NodeT, ValueT, TagT >, NodeT, ValueT >::NonConstValueType
typename std::remove_const< ValueT >::type NonConstValueType
Definition: Iterator.h:146
openvdb::v6_1::tree::DenseIteratorBase::probeChild
SetItemT * probeChild(NonConstValueType &value) const
If this iterator is pointing to a child node, return a pointer to the node. Otherwise,...
Definition: Iterator.h:236
openvdb::v6_1::tree::DenseIteratorBase< MaskDenseIterator, DenseIter< NodeT, ChildT, ValueT, TagT >, NodeT, ChildT, ValueT >::NonConstValueType
typename std::remove_const< ValueT >::type NonConstValueType
Definition: Iterator.h:211
openvdb::v6_1::tree::IteratorBase::increment
void increment()
Advance to the next item in the parent node's table.
Definition: Iterator.h:97
openvdb::v6_1::tree::IteratorBase::getCoord
void getCoord(Coord &xyz) const
Return in xyz the coordinates of the item to which this iterator is pointing.
Definition: Iterator.h:118
openvdb::v6_1::tree::IteratorBase::getCoord
Coord getCoord() const
Return the coordinates of the item to which this iterator is pointing.
Definition: Iterator.h:116
OPENVDB_THROW
#define OPENVDB_THROW(exception, message)
Definition: Exceptions.h:109
openvdb::v6_1::tree::SparseIteratorBase< MaskIterT, ValueIter< MaskIterT, NodeT, ValueT, TagT >, NodeT, ValueT >::NodeType
NodeT NodeType
Definition: Iterator.h:143
openvdb::v6_1::tree::DenseIteratorBase< MaskDenseIterator, DenseIter< NodeT, ChildT, ValueT, TagT >, NodeT, ChildT, ValueT >::NonConstChildNodeType
typename std::remove_const< ChildT >::type NonConstChildNodeType
Definition: Iterator.h:212
openvdb::v6_1::tree::IteratorBase::setValueOn
void setValueOn(bool on=true) const
If this iterator is pointing to a value, set the value's active state. Otherwise, do nothing.
Definition: Iterator.h:108
openvdb::v6_1::tree::IteratorBase::operator++
IteratorBase & operator++()
Advance to the next item in the parent node's table.
Definition: Iterator.h:99
openvdb::v6_1::tree::DenseIteratorBase::DenseIteratorBase
DenseIteratorBase(const MaskIterT &iter, NodeT *parent)
Definition: Iterator.h:216
openvdb::v6_1::tree::SparseIteratorBase::modifyValue
void modifyValue(const ModifyOp &op) const
Apply a functor to the item to which this iterator is pointing. (Not valid for const iterators....
Definition: Iterator.h:183
openvdb::v6_1::tree::DenseIteratorBase< MaskDenseIterator, DenseIter< NodeT, ChildT, ValueT, TagT >, NodeT, ChildT, ValueT >::NodeType
NodeT NodeType
Definition: Iterator.h:207
openvdb::v6_1::tree::SparseIteratorBase< MaskIterT, ValueIter< MaskIterT, NodeT, ValueT, TagT >, NodeT, ValueT >::ValueType
ValueT ValueType
Definition: Iterator.h:144
openvdb::v6_1::tree::SparseIteratorBase::SparseIteratorBase
SparseIteratorBase(const MaskIterT &iter, NodeT *parent)
Definition: Iterator.h:150
Exceptions.h
OPENVDB_VERSION_NAME
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h:125
openvdb::v6_1::tree::IteratorBase::offset
Index offset() const
Return this iterator's position as an index into the parent node's table.
Definition: Iterator.h:84
openvdb::v6_1::tree::IteratorBase::operator==
bool operator==(const IteratorBase &other) const
Definition: Iterator.h:64
openvdb::v6_1::tree::IteratorBase::IteratorBase
IteratorBase(const MaskIterT &iter, NodeT *parent)
Definition: Iterator.h:60
openvdb::v6_1::Index
Index32 Index
Definition: Types.h:61
openvdb::v6_1::tree::DenseIteratorBase< MaskDenseIterator, DenseIter< NodeT, ChildT, ValueT, TagT >, NodeT, ChildT, ValueT >::NonConstNodeType
typename std::remove_const< NodeT >::type NonConstNodeType
Definition: Iterator.h:210
openvdb::v6_1::tree::DenseIteratorBase::probeValue
bool probeValue(NonConstValueType &value) const
Return true if this iterator is pointing to a value and return the value in value....
Definition: Iterator.h:253
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::tree::SparseIteratorBase< MaskIterT, ValueIter< MaskIterT, NodeT, ValueT, TagT >, NodeT, ValueT >::NonConstNodeType
typename std::remove_const< NodeT >::type NonConstNodeType
Definition: Iterator.h:145
openvdb::v6_1::tree::DenseIteratorBase::setValue
void setValue(const UnsetItemT &value) const
Replace with the given value the item in the parent node's table to which this iterator is pointing.
Definition: Iterator.h:270
openvdb::v6_1::tree::DenseIteratorBase< MaskDenseIterator, DenseIter< NodeT, ChildT, ValueT, TagT >, NodeT, ChildT, ValueT >::ValueType
ValueT ValueType
Definition: Iterator.h:208
openvdb::v6_1::tree::DenseIteratorBase::DenseIteratorBase
DenseIteratorBase()
Definition: Iterator.h:215
openvdb::v6_1::tree::SparseIteratorBase::SparseIteratorBase
SparseIteratorBase()
Definition: Iterator.h:149
openvdb::v6_1::tree::IteratorBase::setValueOff
void setValueOff() const
If this iterator is pointing to a value, mark the value as inactive.
Definition: Iterator.h:113
openvdb::v6_1::tree::IteratorBase::isValueOn
bool isValueOn() const
Return true if this iterator is pointing to an active value. Return false if it is pointing to either...
Definition: Iterator.h:105
openvdb::v6_1::tree::IteratorBase::parent
NodeT & parent() const
Return a reference to the node over which this iterator is iterating.
Definition: Iterator.h:77
openvdb::v6_1::tree::IteratorBase::operator!=
bool operator!=(const IteratorBase &other) const
Definition: Iterator.h:68
openvdb::v6_1::tree::DenseIteratorBase
Base class for dense iterators over internal and leaf nodes.
Definition: Iterator.h:205
openvdb::v6_1::tree::DenseIteratorBase< MaskDenseIterator, DenseIter< NodeT, ChildT, ValueT, TagT >, NodeT, ChildT, ValueT >::ChildNodeType
ChildT ChildNodeType
Definition: Iterator.h:209
openvdb::v6_1::tree::IteratorBase::next
bool next()
Advance to the next item in the parent node's table.
Definition: Iterator.h:95
openvdb::v6_1::tree::IteratorBase::pos
Index pos() const
Identical to offset.
Definition: Iterator.h:87
openvdb::v6_1::math::Coord
Signed (x, y, z) 32-bit integer coordinates.
Definition: Coord.h:52
openvdb::v6_1::tree::IteratorBase::getParentNode
NodeT * getParentNode() const
Return a pointer to the node (if any) over which this iterator is iterating.
Definition: Iterator.h:74
openvdb::v6_1::tree::DenseIteratorBase::setChild
void setChild(SetItemT *child) const
Replace with the given child node the item in the parent node's table to which this iterator is point...
Definition: Iterator.h:263
openvdb::v6_1::ValueError
Definition: Exceptions.h:92
openvdb::v6_1::tree::SparseIteratorBase::operator->
ItemT * operator->() const
Return a pointer to the item to which this iterator is pointing.
Definition: Iterator.h:163
NodeMasks.h
OPENVDB_USE_VERSION_NAMESPACE
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:177
openvdb::v6_1::tree::SparseIteratorBase::getValue
ItemT & getValue() const
Return the item to which this iterator is pointing.
Definition: Iterator.h:166
openvdb::v6_1::tree::IteratorBase::IteratorBase
IteratorBase()
Definition: Iterator.h:59
openvdb::v6_1::tree::IteratorBase::test
bool test() const
Return true if this iterator is not yet exhausted.
Definition: Iterator.h:90
openvdb::v6_1::tree::IteratorBase::increment
void increment(Index n)
Advance n items in the parent node's table.
Definition: Iterator.h:101
openvdb::v6_1::tree::SparseIteratorBase::setValue
void setValue(const ItemT &value) const
Set the value of the item to which this iterator is pointing. (Not valid for const iterators....
Definition: Iterator.h:172
openvdb::v6_1::tree::DenseIteratorBase::probeChild
bool probeChild(SetItemT *&child, NonConstValueType &value) const
If this iterator is pointing to a child node, return true and return a pointer to the child node in c...
Definition: Iterator.h:245
openvdb::v6_1::tree::IteratorBase
Base class for iterators over internal and leaf nodes.
Definition: Iterator.h:56
openvdb::v6_1::tree::SparseIteratorBase
Base class for sparse iterators over internal and leaf nodes.
Definition: Iterator.h:141