OpenVDB 3.2.0

Iterator.h
Go to the documentation of this file.
1 //
3 // Copyright (c) 2012-2016 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(NULL) {}
62 IteratorBase(const MaskIterT& iter, NodeT* parent):
63 mParentNode(parent), mMaskIter(iter) {}
64
65 void operator=(const IteratorBase& other)
66 {
67 mParentNode = other.mParentNode;
68 mMaskIter = other.mMaskIter;
69 }
70
71 bool operator==(const IteratorBase& other) const
72 {
73 return (mParentNode == other.mParentNode) && (mMaskIter == other.mMaskIter);
74 }
75 bool operator!=(const IteratorBase& other) const
76 {
77 return !(*this == other);
78 }
79
81 NodeT* getParentNode() const { return mParentNode; }
84 NodeT& parent() const
85 {
86 if (!mParentNode) OPENVDB_THROW(ValueError, "iterator references a null node");
87 return *mParentNode;
88 }
89
91 Index offset() const { return mMaskIter.offset(); }
92
94 Index pos() const { return mMaskIter.offset(); }
95
97 bool test() const { return mMaskIter.test(); }
99 operator bool() const { return this->test(); }
100
102 bool next() { return mMaskIter.next(); }
104 void increment() { mMaskIter.increment(); }
106 IteratorBase& operator++() { this->increment(); return *this; }
108 void increment(Index n) { mMaskIter.increment(n); }
109
112 bool isValueOn() const { return parent().isValueMaskOn(this->pos()); }
115 void setValueOn(bool on = true) const { parent().setValueMask(this->pos(), on); }
120 void setValueOff() const { parent().mValueMask.setOff(this->pos()); }
121
123 Coord getCoord() const { return parent().offsetToGlobalCoord(this->pos()); }
125 void getCoord(Coord& xyz) const { xyz = this->getCoord(); }
126
127 private:
134 mutable NodeT* mParentNode;
135 MaskIterT mMaskIter;
136 }; // class IteratorBase
137
138
140
141
143 template<
144 typename MaskIterT, // mask iterator type (OnIterator, OffIterator, etc.)
145 typename IterT, // SparseIteratorBase subclass (the "Curiously Recurring Template Pattern")
146 typename NodeT, // type of node over which to iterate
147 typename ItemT> // type of value to which this iterator points
148 struct SparseIteratorBase: public IteratorBase<MaskIterT, NodeT>
149 {
150 typedef NodeT NodeType;
151 typedef ItemT ValueType;
152 typedef typename boost::remove_const<NodeT>::type NonConstNodeType;
153 typedef typename boost::remove_const<ItemT>::type NonConstValueType;
154 static const bool IsSparseIterator = true, IsDenseIterator = false;
155
156 SparseIteratorBase() {}
157 SparseIteratorBase(const MaskIterT& iter, NodeT* parent):
158 IteratorBase<MaskIterT, NodeT>(iter, parent) {}
159
162 ItemT& getItem(Index) const;
165 void setItem(Index, const ItemT&) const;
166
168 ItemT& operator*() const { return this->getValue(); }
170 ItemT* operator->() const { return &(this->operator*()); }
171
173 ItemT& getValue() const
174 {
175 return static_cast<const IterT*>(this)->getItem(this->pos()); // static polymorphism
176 }
179 void setValue(const ItemT& value) const
180 {
181 BOOST_STATIC_ASSERT(!boost::is_const<NodeT>::value);
182 static_cast<const IterT*>(this)->setItem(this->pos(), value); // static polymorphism
183 }
189 template<typename ModifyOp>
190 void modifyValue(const ModifyOp& op) const
191 {
192 BOOST_STATIC_ASSERT(!boost::is_const<NodeT>::value);
193 static_cast<const IterT*>(this)->modifyItem(this->pos(), op); // static polymorphism
194 }
195 }; // class SparseIteratorBase
196
197
199
200
205 template<
206 typename MaskIterT, // mask iterator type (typically a DenseIterator)
207 typename IterT, // DenseIteratorBase subclass (the "Curiously Recurring Template Pattern")
208 typename NodeT, // type of node over which to iterate
209 typename SetItemT, // type of set value (ChildNodeType, for non-leaf nodes)
210 typename UnsetItemT> // type of unset value (ValueType, usually)
211 struct DenseIteratorBase: public IteratorBase<MaskIterT, NodeT>
212 {
213 typedef NodeT NodeType;
214 typedef UnsetItemT ValueType;
215 typedef SetItemT ChildNodeType;
216 typedef typename boost::remove_const<NodeT>::type NonConstNodeType;
217 typedef typename boost::remove_const<UnsetItemT>::type NonConstValueType;
218 typedef typename boost::remove_const<SetItemT>::type NonConstChildNodeType;
219 static const bool IsSparseIterator = false, IsDenseIterator = true;
220
221 DenseIteratorBase() {}
222 DenseIteratorBase(const MaskIterT& iter, NodeT* parent):
223 IteratorBase<MaskIterT, NodeT>(iter, parent) {}
224
229 bool getItem(Index, SetItemT*& child, NonConstValueType& value) const;
232 void setItem(Index, SetItemT*) const;
235 void unsetItem(Index, const UnsetItemT&) const;
236
238 bool isChildNode() const { return this->parent().isChildMaskOn(this->pos()); }
239
242 SetItemT* probeChild(NonConstValueType& value) const
243 {
244 SetItemT* child = NULL;
245 static_cast<const IterT*>(this)->getItem(this->pos(), child, value); // static polymorphism
246 return child;
247 }
251 bool probeChild(SetItemT*& child, NonConstValueType& value) const
252 {
253 child = probeChild(value);
254 return (child != NULL);
255 }
256
259 bool probeValue(NonConstValueType& value) const
260 {
261 SetItemT* child = NULL;
262 const bool isChild = static_cast<const IterT*>(this)-> // static polymorphism
263 getItem(this->pos(), child, value);
264 return !isChild;
265 }
266
269 void setChild(SetItemT* child) const
270 {
271 static_cast<const IterT*>(this)->setItem(this->pos(), child); // static polymorphism
272 }
273
276 void setValue(const UnsetItemT& value) const
277 {
278 static_cast<const IterT*>(this)->unsetItem(this->pos(), value); // static polymorphism
279 }
280 }; // struct DenseIteratorBase
281
282 } // namespace tree
283 } // namespace OPENVDB_VERSION_NAME
284 } // namespace openvdb
285
286 #endif // OPENVDB_TREE_ITERATOR_HAS_BEEN_INCLUDED
287
288 // Copyright (c) 2012-2016 DreamWorks Animation LLC
289 // All rights reserved. This software is distributed under the
290 // Mozilla Public License 2.0 ( http://www.mozilla.org/MPL/2.0/ )
openvdb::v3_2_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:179
openvdb::v3_2_0::tree::IteratorBase::operator++
IteratorBase & operator++()
Advance to the next item in the parent node&#39;s table.
Definition: Iterator.h:106
openvdb::v3_2_0::tree::DenseIteratorBase::NodeType
NodeT NodeType
Definition: Iterator.h:213
openvdb::v3_2_0::tree::SparseIteratorBase::operator*
ItemT & operator*() const
Return a reference to the item to which this iterator is pointing.
Definition: Iterator.h:168
OPENVDB_THROW
#define OPENVDB_THROW(exception, message)
Definition: Exceptions.h:97
openvdb::v3_2_0::tree::SparseIteratorBase::SparseIteratorBase
SparseIteratorBase(const MaskIterT &iter, NodeT *parent)
Definition: Iterator.h:157
openvdb::v3_2_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:190
openvdb::v3_2_0::math::Coord
Signed (x, y, z) 32-bit integer coordinates.
Definition: Coord.h:47
openvdb::v3_2_0::math::operator*
Mat3< typename promote< T0, T1 >::type > operator*(const Mat3< T0 > &m0, const Mat3< T1 > &m1)
Matrix multiplication.
Definition: Mat3.h:658
openvdb::v3_2_0::tree::DenseIteratorBase::isChildNode
bool isChildNode() const
Return true if this iterator is pointing to a child node.
Definition: Iterator.h:238
openvdb::v3_2_0::tree::IteratorBase::pos
Index pos() const
Identical to offset.
Definition: Iterator.h:94
openvdb::v3_2_0::tree::IteratorBase::parent
NodeT & parent() const
Return a reference to the node over which this iterator is iterating.
Definition: Iterator.h:84
openvdb::v3_2_0::Index
Index32 Index
Definition: Types.h:58
openvdb::v3_2_0::tree::SparseIteratorBase::NonConstNodeType
boost::remove_const< NodeT >::type NonConstNodeType
Definition: Iterator.h:152
openvdb::v3_2_0::tree::DenseIteratorBase::NonConstChildNodeType
boost::remove_const< SetItemT >::type NonConstChildNodeType
Definition: Iterator.h:218
Exceptions.h
openvdb::v3_2_0::tree::IteratorBase::next
bool next()
Advance to the next item in the parent node&#39;s table.
Definition: Iterator.h:102
openvdb::v3_2_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:115
openvdb::v3_2_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:125
OPENVDB_VERSION_NAME
#define OPENVDB_VERSION_NAME
Definition: version.h:43
openvdb::v3_2_0::tree::IteratorBase::increment
void increment(Index n)
Advance n items in the parent node&#39;s table.
Definition: Iterator.h:108
openvdb::v3_2_0::tree::IteratorBase::setValueOff
void setValueOff() const
If this iterator is pointing to a value, mark the value as inactive.
Definition: Iterator.h:120
openvdb::v3_2_0::tree::DenseIteratorBase::ValueType
UnsetItemT ValueType
Definition: Iterator.h:214
openvdb::v3_2_0::tree::SparseIteratorBase::ValueType
ItemT ValueType
Definition: Iterator.h:151
openvdb::v3_2_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:276
openvdb::v3_2_0::tree::SparseIteratorBase::NodeType
NodeT NodeType
Definition: Iterator.h:150
openvdb::v3_2_0::tree::IteratorBase::IteratorBase
IteratorBase()
Definition: Iterator.h:61
openvdb::v3_2_0::tree::IteratorBase::getCoord
Coord getCoord() const
Return the coordinates of the item to which this iterator is pointing.
Definition: Iterator.h:123
openvdb::v3_2_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:112
openvdb
Definition: Exceptions.h:39
openvdb::v3_2_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 NULL and, in value, the value to which this iterator is pointing.
Definition: Iterator.h:242
openvdb::v3_2_0::tree::IteratorBase::increment
void increment()
Advance to the next item in the parent node&#39;s table.
Definition: Iterator.h:104
openvdb::v3_2_0::tree::DenseIteratorBase
Base class for dense iterators over internal and leaf nodes.
Definition: Iterator.h:211
openvdb::v3_2_0::tree::DenseIteratorBase::DenseIteratorBase
DenseIteratorBase(const MaskIterT &iter, NodeT *parent)
Definition: Iterator.h:222
openvdb::v3_2_0::tree::SparseIteratorBase
Base class for sparse iterators over internal and leaf nodes.
Definition: Iterator.h:148
openvdb::v3_2_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:91
openvdb::v3_2_0::tree::DenseIteratorBase::NonConstValueType
boost::remove_const< UnsetItemT >::type NonConstValueType
Definition: Iterator.h:217
openvdb::v3_2_0::tree::DenseIteratorBase::DenseIteratorBase
DenseIteratorBase()
Definition: Iterator.h:221
openvdb::v3_2_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:259
openvdb::v3_2_0::tree::SparseIteratorBase::NonConstValueType
boost::remove_const< ItemT >::type NonConstValueType
Definition: Iterator.h:153
openvdb::v3_2_0::tree::SparseIteratorBase::getValue
ItemT & getValue() const
Return the item to which this iterator is pointing.
Definition: Iterator.h:173
openvdb::v3_2_0::tree::IteratorBase::test
bool test() const
Return true if this iterator is not yet exhausted.
Definition: Iterator.h:97
openvdb::v3_2_0::tree::IteratorBase::operator==
bool operator==(const IteratorBase &other) const
Definition: Iterator.h:71
openvdb::v3_2_0::tree::DenseIteratorBase::ChildNodeType
SetItemT ChildNodeType
Definition: Iterator.h:215
openvdb::v3_2_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:251
openvdb::v3_2_0::tree::IteratorBase::operator!=
bool operator!=(const IteratorBase &other) const
Definition: Iterator.h:75
openvdb::v3_2_0::tree::IteratorBase::getParentNode
NodeT * getParentNode() const
Return a pointer to the node (if any) over which this iterator is iterating.
Definition: Iterator.h:81
openvdb::v3_2_0::tree::IteratorBase::IteratorBase
IteratorBase(const MaskIterT &iter, NodeT *parent)
Definition: Iterator.h:62
openvdb::v3_2_0::ValueError
Definition: Exceptions.h:88
NodeMasks.h
openvdb::v3_2_0::tree::IteratorBase::operator=
void operator=(const IteratorBase &other)
Definition: Iterator.h:65
OPENVDB_USE_VERSION_NAMESPACE
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:71
openvdb::v3_2_0::tree::SparseIteratorBase::SparseIteratorBase
SparseIteratorBase()
Definition: Iterator.h:156
openvdb::v3_2_0::tree::DenseIteratorBase::NonConstNodeType
boost::remove_const< NodeT >::type NonConstNodeType
Definition: Iterator.h:216
openvdb::v3_2_0::tree::SparseIteratorBase::operator->
ItemT * operator->() const
Return a pointer to the item to which this iterator is pointing.
Definition: Iterator.h:170
openvdb::v3_2_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:269
openvdb::v3_2_0::tree::IteratorBase
Base class for iterators over internal and leaf nodes.
Definition: Iterator.h:58