HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Iterator.h
Go to the documentation of this file.
1 ///////////////////////////////////////////////////////////////////////////
2 //
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 //
29 ///////////////////////////////////////////////////////////////////////////
30 //
31 /// @file tree/Iterator.h
32 ///
33 /// @author Peter Cucka and Ken Museth
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 {
45 namespace OPENVDB_VERSION_NAME {
46 namespace tree {
47 
48 /// @brief Base class for iterators over internal and leaf nodes
49 ///
50 /// This class is typically not instantiated directly, since it doesn't provide methods
51 /// to dereference the iterator. Those methods (@vdblink::tree::SparseIteratorBase::operator*()
52 /// operator*()@endlink, @vdblink::tree::SparseIteratorBase::setValue() setValue()@endlink, etc.)
53 /// are implemented in the @vdblink::tree::SparseIteratorBase sparse@endlink and
54 /// @vdblink::tree::DenseIteratorBase dense@endlink iterator subclasses.
55 template<typename MaskIterT, typename NodeT>
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 
73  /// Return a pointer to the node (if any) over which this iterator is iterating.
74  NodeT* getParentNode() const { return mParentNode; }
75  /// @brief Return a reference to the node over which this iterator is iterating.
76  /// @throw ValueError if there is no parent node.
77  NodeT& parent() const
78  {
79  if (!mParentNode) OPENVDB_THROW(ValueError, "iterator references a null node");
80  return *mParentNode;
81  }
82 
83  /// Return this iterator's position as an index into the parent node's table.
84  Index offset() const { return mMaskIter.offset(); }
85 
86  /// Identical to offset
87  Index pos() const { return mMaskIter.offset(); }
88 
89  /// Return @c true if this iterator is not yet exhausted.
90  bool test() const { return mMaskIter.test(); }
91  /// Return @c true if this iterator is not yet exhausted.
92  operator bool() const { return this->test(); }
93 
94  /// Advance to the next item in the parent node's table.
95  bool next() { return mMaskIter.next(); }
96  /// Advance to the next item in the parent node's table.
97  void increment() { mMaskIter.increment(); }
98  /// Advance to the next item in the parent node's table.
99  IteratorBase& operator++() { this->increment(); return *this; }
100  /// Advance @a n items in the parent node's table.
101  void increment(Index n) { mMaskIter.increment(n); }
102 
103  /// @brief Return @c true if this iterator is pointing to an active value.
104  /// Return @c false if it is pointing to either an inactive value or a child node.
105  bool isValueOn() const { return parent().isValueMaskOn(this->pos()); }
106  /// @brief If this iterator is pointing to a value, set the value's active state.
107  /// Otherwise, do nothing.
108  void setValueOn(bool on = true) const { parent().setValueMask(this->pos(), on); }
109  /// @brief If this iterator is pointing to a value, mark the value as inactive.
110  /// @details If this iterator is pointing to a child node, then the current item
111  /// in the parent node's table is required to be inactive. In that case,
112  /// this method has no effect.
113  void setValueOff() const { parent().mValueMask.setOff(this->pos()); }
114 
115  /// Return the coordinates of the item to which this iterator is pointing.
116  Coord getCoord() const { return parent().offsetToGlobalCoord(this->pos()); }
117  /// Return in @a xyz the coordinates of the item to which this iterator is pointing.
118  void getCoord(Coord& xyz) const { xyz = this->getCoord(); }
119 
120 private:
121  /// @note This parent node pointer is mutable, because setValueOn() and
122  /// setValueOff(), though const, need to call non-const methods on the parent.
123  /// There is a distinction between a const iterator (e.g., const ValueOnIter),
124  /// which is an iterator that can't be incremented, and an iterator over
125  /// a const node (e.g., ValueOnCIter), which might be const or non-const itself
126  /// but can't call non-const methods like setValue() on the node.
127  mutable NodeT* mParentNode;
128  MaskIterT mMaskIter;
129 }; // class IteratorBase
130 
131 
132 ////////////////////////////////////////
133 
134 
135 /// @brief Base class for sparse iterators over internal and leaf nodes
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;
147  static const bool IsSparseIterator = true, IsDenseIterator = false;
148 
150  SparseIteratorBase(const MaskIterT& iter, NodeT* parent):
151  IteratorBase<MaskIterT, NodeT>(iter, parent) {}
152 
153  /// @brief Return the item at the given index in the parent node's table.
154  /// @note All subclasses must implement this accessor.
155  ItemT& getItem(Index) const;
156  /// @brief Set the value of the item at the given index in the parent node's table.
157  /// @note All non-const iterator subclasses must implement this accessor.
158  void setItem(Index, const ItemT&) const;
159 
160  /// Return a reference to the item to which this iterator is pointing.
161  ItemT& operator*() const { return this->getValue(); }
162  /// Return a pointer to the item to which this iterator is pointing.
163  ItemT* operator->() const { return &(this->operator*()); }
164 
165  /// Return the item to which this iterator is pointing.
166  ItemT& getValue() const
167  {
168  return static_cast<const IterT*>(this)->getItem(this->pos()); // static polymorphism
169  }
170  /// @brief Set the value of the item to which this iterator is pointing.
171  /// (Not valid for const iterators.)
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  }
177  /// @brief Apply a functor to the item to which this iterator is pointing.
178  /// (Not valid for const iterators.)
179  /// @param op a functor of the form <tt>void op(ValueType&) const</tt> that modifies
180  /// its argument in place
181  /// @see Tree::modifyValue()
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 
192 ////////////////////////////////////////
193 
194 
195 /// @brief Base class for dense iterators over internal and leaf nodes
196 /// @note Dense iterators have no @c %operator*() or @c %operator->(),
197 /// because their return type would have to vary depending on whether
198 /// the iterator is pointing to a value or a child node.
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;
213  static const bool IsSparseIterator = false, IsDenseIterator = true;
214 
216  DenseIteratorBase(const MaskIterT& iter, NodeT* parent):
217  IteratorBase<MaskIterT, NodeT>(iter, parent) {}
218 
219  /// @brief Return @c true if the item at the given index in the parent node's table
220  /// is a set value and return either the set value in @a child or the unset value
221  /// in @a value.
222  /// @note All subclasses must implement this accessor.
223  bool getItem(Index, SetItemT*& child, NonConstValueType& value) const;
224  /// @brief Set the value of the item at the given index in the parent node's table.
225  /// @note All non-const iterator subclasses must implement this accessor.
226  void setItem(Index, SetItemT*) const;
227  /// @brief "Unset" the value of the item at the given index in the parent node's table.
228  /// @note All non-const iterator subclasses must implement this accessor.
229  void unsetItem(Index, const UnsetItemT&) const;
230 
231  /// Return @c true if this iterator is pointing to a child node.
232  bool isChildNode() const { return this->parent().isChildMaskOn(this->pos()); }
233 
234  /// @brief If this iterator is pointing to a child node, return a pointer to the node.
235  /// Otherwise, return nullptr and, in @a value, the value to which this iterator is pointing.
237  {
238  SetItemT* child = nullptr;
239  static_cast<const IterT*>(this)->getItem(this->pos(), child, value); // static polymorphism
240  return child;
241  }
242  /// @brief If this iterator is pointing to a child node, return @c true and return
243  /// a pointer to the child node in @a child. Otherwise, return @c false and return
244  /// the value to which this iterator is pointing in @a value.
245  bool probeChild(SetItemT*& child, NonConstValueType& value) const
246  {
247  child = probeChild(value);
248  return (child != nullptr);
249  }
250 
251  /// @brief Return @c true if this iterator is pointing to a value and return
252  /// the value in @a value. Otherwise, return @c false.
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 
261  /// @brief Replace with the given child node the item in the parent node's table
262  /// to which this iterator is pointing.
263  void setChild(SetItemT* child) const
264  {
265  static_cast<const IterT*>(this)->setItem(this->pos(), child); // static polymorphism
266  }
267 
268  /// @brief Replace with the given value the item in the parent node's table
269  /// to which this iterator is pointing.
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/ )
Coord getCoord() const
Return the coordinates of the item to which this iterator is pointing.
Definition: Iterator.h:116
IteratorBase & operator++()
Advance to the next item in the parent node's table.
Definition: Iterator.h:99
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
IteratorBase & operator=(const IteratorBase &)=default
Index pos() const
Identical to offset.
Definition: Iterator.h:87
void increment()
Advance to the next item in the parent node's table.
Definition: Iterator.h:97
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:189
Base class for iterators over internal and leaf nodes.
Definition: Iterator.h:56
void setItem(Index, SetItemT *) const
Set the value of the item at the given index in the parent node's table.
NodeT & parent() const
Return a reference to the node over which this iterator is iterating.
Definition: Iterator.h:77
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
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:236
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:253
IteratorBase(const MaskIterT &iter, NodeT *parent)
Definition: Iterator.h:60
GLdouble n
Definition: glcorearb.h:2007
void unsetItem(Index, const UnsetItemT &) const
"Unset" the value of the item at the given index in the parent node's table.
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
void setItem(Index, const ItemT &) const
Set the value of the item at the given index in the parent node's table.
bool test() const
Return true if this iterator is not yet exhausted.
Definition: Iterator.h:90
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
Index offset() const
Return this iterator's position as an index into the parent node's table.
Definition: Iterator.h:84
bool getItem(Index, SetItemT *&child, NonConstValueType &value) const
Return true if the item at the given index in the parent node's table is a set value and return eithe...
Base class for sparse iterators over internal and leaf nodes.
Definition: Iterator.h:141
bool operator!=(const IteratorBase &other) const
Definition: Iterator.h:68
DenseIteratorBase(const MaskIterT &iter, NodeT *parent)
Definition: Iterator.h:216
ItemT * operator->() const
Return a pointer to the item to which this iterator is pointing.
Definition: Iterator.h:163
Base class for dense iterators over internal and leaf nodes.
Definition: Iterator.h:205
GLsizei const GLfloat * value
Definition: glcorearb.h:823
void increment(Index n)
Advance n items in the parent node's table.
Definition: Iterator.h:101
bool isChildNode() const
Return true if this iterator is pointing to a child node.
Definition: Iterator.h:232
void getCoord(Coord &xyz) const
Return in xyz the coordinates of the item to which this iterator is pointing.
Definition: Iterator.h:118
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
NodeT * getParentNode() const
Return a pointer to the node (if any) over which this iterator is iterating.
Definition: Iterator.h:74
GLint GLint GLsizei GLint GLenum GLenum type
Definition: glcorearb.h:107
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
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
bool operator==(const IteratorBase &other) const
Definition: Iterator.h:64
void setValueOff() const
If this iterator is pointing to a value, mark the value as inactive.
Definition: Iterator.h:113
bool next()
Advance to the next item in the parent node's table.
Definition: Iterator.h:95
ItemT & getValue() const
Return the item to which this iterator is pointing.
Definition: Iterator.h:166
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h:135
ItemT & operator*() const
Return a reference to the item to which this iterator is pointing.
Definition: Iterator.h:161
ItemT & getItem(Index) const
Return the item at the given index in the parent node's table.
#define OPENVDB_THROW(exception, message)
Definition: Exceptions.h:109
SparseIteratorBase(const MaskIterT &iter, NodeT *parent)
Definition: Iterator.h:150