OpenVDB: Iterator.h Source File

OpenVDB 5.0.0

Iterator.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 //
34
35 #ifndef OPENVDB_TREE_ITERATOR_HAS_BEEN_INCLUDED
36 #define OPENVDB_TREE_ITERATOR_HAS_BEEN_INCLUDED
37
38 #include <sstream>
39 #include <boost/static_assert.hpp>
40 #include <boost/type_traits/is_const.hpp>
41 #include <boost/type_traits/remove_const.hpp>
42 #include <openvdb/util/NodeMasks.h>
43 #include <openvdb/Exceptions.h>
44
45 namespace openvdb {
46 OPENVDB_USE_VERSION_NAMESPACE
47 namespace OPENVDB_VERSION_NAME {
48 namespace tree {
49
57 template<typename MaskIterT, typename NodeT>
58 class IteratorBase
59 {
60 public:
61 IteratorBase(): mParentNode(nullptr) {}
62 IteratorBase(const MaskIterT& iter, NodeT* parent): mParentNode(parent), mMaskIter(iter) {}
63 IteratorBase(const IteratorBase&) = default;
64 IteratorBase& operator=(const IteratorBase&) = default;
65
66 bool operator==(const IteratorBase& other) const
67 {
68 return (mParentNode == other.mParentNode) && (mMaskIter == other.mMaskIter);
69 }
70 bool operator!=(const IteratorBase& other) const
71 {
72 return !(*this == other);
73 }
74
76 NodeT* getParentNode() const { return mParentNode; }
79 NodeT& parent() const
80 {
81 if (!mParentNode) OPENVDB_THROW(ValueError, "iterator references a null node");
82 return *mParentNode;
83 }
84
86 Index offset() const { return mMaskIter.offset(); }
87
89 Index pos() const { return mMaskIter.offset(); }
90
92 bool test() const { return mMaskIter.test(); }
94 operator bool() const { return this->test(); }
95
97 bool next() { return mMaskIter.next(); }
99 void increment() { mMaskIter.increment(); }
101 IteratorBase& operator++() { this->increment(); return *this; }
103 void increment(Index n) { mMaskIter.increment(n); }
104
107 bool isValueOn() const { return parent().isValueMaskOn(this->pos()); }
110 void setValueOn(bool on = true) const { parent().setValueMask(this->pos(), on); }
115 void setValueOff() const { parent().mValueMask.setOff(this->pos()); }
116
118 Coord getCoord() const { return parent().offsetToGlobalCoord(this->pos()); }
120 void getCoord(Coord& xyz) const { xyz = this->getCoord(); }
121
122 private:
129 mutable NodeT* mParentNode;
130 MaskIterT mMaskIter;
131 }; // class IteratorBase
132
133
135
136
138 template<
139 typename MaskIterT, // mask iterator type (OnIterator, OffIterator, etc.)
140 typename IterT, // SparseIteratorBase subclass (the "Curiously Recurring Template Pattern")
141 typename NodeT, // type of node over which to iterate
142 typename ItemT> // type of value to which this iterator points
143 struct SparseIteratorBase: public IteratorBase<MaskIterT, NodeT>
144 {
145 typedef NodeT NodeType;
146 typedef ItemT ValueType;
147 typedef typename boost::remove_const<NodeT>::type NonConstNodeType;
148 typedef typename boost::remove_const<ItemT>::type NonConstValueType;
149 static const bool IsSparseIterator = true, IsDenseIterator = false;
150
151 SparseIteratorBase() {}
152 SparseIteratorBase(const MaskIterT& iter, NodeT* parent):
153 IteratorBase<MaskIterT, NodeT>(iter, parent) {}
154
157 ItemT& getItem(Index) const;
160 void setItem(Index, const ItemT&) const;
161
163 ItemT& operator*() const { return this->getValue(); }
165 ItemT* operator->() const { return &(this->operator*()); }
166
168 ItemT& getValue() const
169 {
170 return static_cast<const IterT*>(this)->getItem(this->pos()); // static polymorphism
171 }
174 void setValue(const ItemT& value) const
175 {
176 BOOST_STATIC_ASSERT(!boost::is_const<NodeT>::value);
177 static_cast<const IterT*>(this)->setItem(this->pos(), value); // static polymorphism
178 }
184 template<typename ModifyOp>
185 void modifyValue(const ModifyOp& op) const
186 {
187 BOOST_STATIC_ASSERT(!boost::is_const<NodeT>::value);
188 static_cast<const IterT*>(this)->modifyItem(this->pos(), op); // static polymorphism
189 }
190 }; // class SparseIteratorBase
191
192
194
195
200 template<
201 typename MaskIterT, // mask iterator type (typically a DenseIterator)
202 typename IterT, // DenseIteratorBase subclass (the "Curiously Recurring Template Pattern")
203 typename NodeT, // type of node over which to iterate
204 typename SetItemT, // type of set value (ChildNodeType, for non-leaf nodes)
205 typename UnsetItemT> // type of unset value (ValueType, usually)
206 struct DenseIteratorBase: public IteratorBase<MaskIterT, NodeT>
207 {
208 typedef NodeT NodeType;
209 typedef UnsetItemT ValueType;
210 typedef SetItemT ChildNodeType;
211 typedef typename boost::remove_const<NodeT>::type NonConstNodeType;
212 typedef typename boost::remove_const<UnsetItemT>::type NonConstValueType;
213 typedef typename boost::remove_const<SetItemT>::type NonConstChildNodeType;
214 static const bool IsSparseIterator = false, IsDenseIterator = true;
215
216 DenseIteratorBase() {}
217 DenseIteratorBase(const MaskIterT& iter, NodeT* parent):
218 IteratorBase<MaskIterT, NodeT>(iter, parent) {}
219
224 bool getItem(Index, SetItemT*& child, NonConstValueType& value) const;
227 void setItem(Index, SetItemT*) const;
230 void unsetItem(Index, const UnsetItemT&) const;
231
233 bool isChildNode() const { return this->parent().isChildMaskOn(this->pos()); }
234
237 SetItemT* probeChild(NonConstValueType& value) const
238 {
239 SetItemT* child = nullptr;
240 static_cast<const IterT*>(this)->getItem(this->pos(), child, value); // static polymorphism
241 return child;
242 }
246 bool probeChild(SetItemT*& child, NonConstValueType& value) const
247 {
248 child = probeChild(value);
249 return (child != nullptr);
250 }
251
254 bool probeValue(NonConstValueType& value) const
255 {
256 SetItemT* child = nullptr;
257 const bool isChild = static_cast<const IterT*>(this)-> // static polymorphism
258 getItem(this->pos(), child, value);
259 return !isChild;
260 }
261
264 void setChild(SetItemT* child) const
265 {
266 static_cast<const IterT*>(this)->setItem(this->pos(), child); // static polymorphism
267 }
268
271 void setValue(const UnsetItemT& value) const
272 {
273 static_cast<const IterT*>(this)->unsetItem(this->pos(), value); // static polymorphism
274 }
275 }; // struct DenseIteratorBase
276
277 } // namespace tree
278 } // namespace OPENVDB_VERSION_NAME
279 } // namespace openvdb
280
281 #endif // OPENVDB_TREE_ITERATOR_HAS_BEEN_INCLUDED
282
283 // Copyright (c) 2012-2017 DreamWorks Animation LLC
284 // All rights reserved. This software is distributed under the
285 // Mozilla Public License 2.0 ( http://www.mozilla.org/MPL/2.0/ )
openvdb::v5_0::tree::IteratorBase::getCoord
Coord getCoord() const
Return the coordinates of the item to which this iterator is pointing.
Definition: Iterator.h:118
openvdb::v5_0::tree::SparseIteratorBase::ValueType
ItemT ValueType
Definition: Iterator.h:146
openvdb::v5_0::tree::SparseIteratorBase::NonConstNodeType
boost::remove_const< NodeT >::type NonConstNodeType
Definition: Iterator.h:147
openvdb::v5_0::tree::DenseIteratorBase
Base class for dense iterators over internal and leaf nodes.
Definition: Iterator.h:206
openvdb::v5_0::tree::IteratorBase
Base class for iterators over internal and leaf nodes.
Definition: Iterator.h:58
openvdb::v5_0::tree::DenseIteratorBase::NonConstChildNodeType
boost::remove_const< SetItemT >::type NonConstChildNodeType
Definition: Iterator.h:213
OPENVDB_THROW
#define OPENVDB_THROW(exception, message)
Definition: Exceptions.h:108
openvdb::v5_0::tree::SparseIteratorBase::NodeType
NodeT NodeType
Definition: Iterator.h:145
openvdb::v5_0::tree::DenseIteratorBase::setValue
void setValue(const UnsetItemT &value) const
Replace with the given value the item in the parent node&#39;s table to which this iterator is pointing...
Definition: Iterator.h:271
openvdb::v5_0::tree::DenseIteratorBase::DenseIteratorBase
DenseIteratorBase(const MaskIterT &iter, NodeT *parent)
Definition: Iterator.h:217
openvdb::v5_0::tree::DenseIteratorBase::isChildNode
bool isChildNode() const
Return true if this iterator is pointing to a child node.
Definition: Iterator.h:233
openvdb::v5_0::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:107
openvdb::v5_0::tree::DenseIteratorBase::DenseIteratorBase
DenseIteratorBase()
Definition: Iterator.h:216
openvdb::v5_0::math::Coord
Signed (x, y, z) 32-bit integer coordinates.
Definition: Coord.h:51
openvdb::v5_0::tree::SparseIteratorBase::SparseIteratorBase
SparseIteratorBase()
Definition: Iterator.h:151
openvdb::v5_0::tree::SparseIteratorBase::operator*
ItemT & operator*() const
Return a reference to the item to which this iterator is pointing.
Definition: Iterator.h:163
openvdb::v5_0::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:120
openvdb::v5_0::tree::IteratorBase::offset
Index offset() const
Return this iterator&#39;s position as an index into the parent node&#39;s table.
Definition: Iterator.h:86
openvdb::v5_0::tree::IteratorBase::operator==
bool operator==(const IteratorBase &other) const
Definition: Iterator.h:66
openvdb::v5_0::tree::IteratorBase::parent
NodeT & parent() const
Return a reference to the node over which this iterator is iterating.
Definition: Iterator.h:79
openvdb::v5_0::tree::DenseIteratorBase::ValueType
UnsetItemT ValueType
Definition: Iterator.h:209
openvdb::v5_0::tree::DenseIteratorBase::probeChild
SetItemT * probeChild(NonConstValueType &value) const
If this iterator is pointing to a child node, return a pointer to the node. Otherwise, return nullptr and, in value, the value to which this iterator is pointing.
Definition: Iterator.h:237
openvdb::v5_0::tree::IteratorBase::setValueOff
void setValueOff() const
If this iterator is pointing to a value, mark the value as inactive.
Definition: Iterator.h:115
openvdb::v5_0::tree::IteratorBase::IteratorBase
IteratorBase()
Definition: Iterator.h:61
Exceptions.h
openvdb::v5_0::Index
Index32 Index
Definition: Types.h:60
OPENVDB_VERSION_NAME
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h:136
openvdb::v5_0::ValueError
Definition: Exceptions.h:91
openvdb::v5_0::tree::IteratorBase::increment
void increment()
Advance to the next item in the parent node&#39;s table.
Definition: Iterator.h:99
openvdb::v5_0::tree::IteratorBase::setValueOn
void setValueOn(bool on=true) const
If this iterator is pointing to a value, set the value&#39;s active state. Otherwise, do nothing...
Definition: Iterator.h:110
openvdb
Definition: Exceptions.h:39
openvdb::v5_0::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:246
openvdb::v5_0::tree::IteratorBase::getParentNode
NodeT * getParentNode() const
Return a pointer to the node (if any) over which this iterator is iterating.
Definition: Iterator.h:76
openvdb::v5_0::tree::IteratorBase::operator!=
bool operator!=(const IteratorBase &other) const
Definition: Iterator.h:70
openvdb::v5_0::tree::IteratorBase::IteratorBase
IteratorBase(const MaskIterT &iter, NodeT *parent)
Definition: Iterator.h:62
openvdb::v5_0::tree::DenseIteratorBase::setChild
void setChild(SetItemT *child) const
Replace with the given child node the item in the parent node&#39;s table to which this iterator is point...
Definition: Iterator.h:264
openvdb::v5_0::tree::IteratorBase::increment
void increment(Index n)
Advance n items in the parent node&#39;s table.
Definition: Iterator.h:103
openvdb::v5_0::tree::DenseIteratorBase::NonConstNodeType
boost::remove_const< NodeT >::type NonConstNodeType
Definition: Iterator.h:211
openvdb::v5_0::tree::SparseIteratorBase::operator->
ItemT * operator->() const
Return a pointer to the item to which this iterator is pointing.
Definition: Iterator.h:165
openvdb::v5_0::tree::DenseIteratorBase::NodeType
NodeT NodeType
Definition: Iterator.h:208
openvdb::v5_0::tree::IteratorBase::pos
Index pos() const
Identical to offset.
Definition: Iterator.h:89
openvdb::v5_0::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::v5_0::tree::IteratorBase::test
bool test() const
Return true if this iterator is not yet exhausted.
Definition: Iterator.h:92
openvdb::v5_0::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:185
openvdb::v5_0::tree::SparseIteratorBase::SparseIteratorBase
SparseIteratorBase(const MaskIterT &iter, NodeT *parent)
Definition: Iterator.h:152
NodeMasks.h
openvdb::v5_0::tree::SparseIteratorBase
Base class for sparse iterators over internal and leaf nodes.
Definition: Iterator.h:143
openvdb::v5_0::tree::SparseIteratorBase::NonConstValueType
boost::remove_const< ItemT >::type NonConstValueType
Definition: Iterator.h:148
OPENVDB_USE_VERSION_NAMESPACE
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:188
openvdb::v5_0::tree::DenseIteratorBase::ChildNodeType
SetItemT ChildNodeType
Definition: Iterator.h:210
openvdb::v5_0::tree::DenseIteratorBase::probeValue
bool probeValue(NonConstValueType &value) const
Return true if this iterator is pointing to a value and return the value in value. Otherwise, return false.
Definition: Iterator.h:254
openvdb::v5_0::tree::IteratorBase::next
bool next()
Advance to the next item in the parent node&#39;s table.
Definition: Iterator.h:97
openvdb::v5_0::tree::SparseIteratorBase::getValue
ItemT & getValue() const
Return the item to which this iterator is pointing.
Definition: Iterator.h:168
openvdb::v5_0::tree::IteratorBase::operator++
IteratorBase & operator++()
Advance to the next item in the parent node&#39;s table.
Definition: Iterator.h:101
openvdb::v5_0::tree::DenseIteratorBase::NonConstValueType
boost::remove_const< UnsetItemT >::type NonConstValueType
Definition: Iterator.h:212
openvdb::v5_0::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:174