OpenVDB  2.1.0
Iterator.h
Go to the documentation of this file.
1 //
3 // Copyright (c) 2012-2013 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 {
47 namespace OPENVDB_VERSION_NAME {
48 namespace tree {
49 
57 template<typename MaskIterT, typename NodeT>
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 
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 
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-2013 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/ )
NodeT * getParentNode() const
Return a pointer to the node (if any) over which this iterator is iterating.
Definition: Iterator.h:81
bool operator==(const IteratorBase &other) const
Definition: Iterator.h:71
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
Coord getCoord() const
Return the coordinates of the item to which this iterator is pointing.
Definition: Iterator.h:123
Index offset() const
Return this iterator&#39;s position as an index into the parent node&#39;s table.
Definition: Iterator.h:91
bool test() const
Return true if this iterator is not yet exhausted.
Definition: Iterator.h:97
boost::remove_const< UnsetItemT >::type NonConstValueType
Definition: Iterator.h:217
#define OPENVDB_THROW(exception, message)
Definition: Exceptions.h:97
Definition: Exceptions.h:88
ItemT & getValue() const
Return the item to which this iterator is pointing.
Definition: Iterator.h:173
IteratorBase()
Definition: Iterator.h:61
boost::remove_const< NodeT >::type NonConstNodeType
Definition: Iterator.h:216
Index32 Index
Definition: Types.h:56
IteratorBase & operator++()
Advance to the next item in the parent node&#39;s table.
Definition: Iterator.h:106
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
SetItemT ChildNodeType
Definition: Iterator.h:215
bool next()
Advance to the next item in the parent node&#39;s table.
Definition: Iterator.h:102
Base class for sparse iterators over internal and leaf nodes.
Definition: Iterator.h:148
bool operator!=(const IteratorBase &other) const
Definition: Iterator.h:75
ItemT * operator->() const
Return a pointer to the item to which this iterator is pointing.
Definition: Iterator.h:170
NodeT & parent() const
Return a reference to the node over which this iterator is iterating.
Definition: Iterator.h:84
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
DenseIteratorBase()
Definition: Iterator.h:221
boost::remove_const< NodeT >::type NonConstNodeType
Definition: Iterator.h:152
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
NodeT NodeType
Definition: Iterator.h:213
SparseIteratorBase(const MaskIterT &iter, NodeT *parent)
Definition: Iterator.h:157
boost::remove_const< SetItemT >::type NonConstChildNodeType
Definition: Iterator.h:218
void setValueOff() const
If this iterator is pointing to a value, mark the value as inactive.
Definition: Iterator.h:120
#define OPENVDB_VERSION_NAME
Definition: version.h:45
bool isChildNode() const
Return true if this iterator is pointing to a child node.
Definition: Iterator.h:238
Base class for iterators over internal and leaf nodes.
Definition: Iterator.h:58
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
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
NodeT NodeType
Definition: Iterator.h:150
Base class for dense iterators over internal and leaf nodes.
Definition: Iterator.h:211
void increment(Index n)
Advance n items in the parent node&#39;s table.
Definition: Iterator.h:108
DenseIteratorBase(const MaskIterT &iter, NodeT *parent)
Definition: Iterator.h:222
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
Mat3< typename promote< T0, T1 >::type > operator*(const Mat3< T0 > &m0, const Mat3< T1 > &m1)
Matrix multiplication.
Definition: Mat3.h:608
void operator=(const IteratorBase &other)
Definition: Iterator.h:65
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
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
ItemT ValueType
Definition: Iterator.h:151
UnsetItemT ValueType
Definition: Iterator.h:214
ItemT & operator*() const
Return a reference to the item to which this iterator is pointing.
Definition: Iterator.h:168
SparseIteratorBase()
Definition: Iterator.h:156
Index pos() const
Identical to offset.
Definition: Iterator.h:94
boost::remove_const< ItemT >::type NonConstValueType
Definition: Iterator.h:153
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:67
void increment()
Advance to the next item in the parent node&#39;s table.
Definition: Iterator.h:104
IteratorBase(const MaskIterT &iter, NodeT *parent)
Definition: Iterator.h:62
void getCoord(Coord &xyz) const
Return in xyz the coordinates of the item to which this iterator is pointing.
Definition: Iterator.h:125