OpenVDB  1.2.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/type_traits/remove_const.hpp>
40 #include <openvdb/util/NodeMasks.h>
41 #include <openvdb/Exceptions.h>
42 
43 namespace openvdb {
45 namespace OPENVDB_VERSION_NAME {
46 namespace tree {
47 
55 template<typename MaskIterT, typename NodeT>
57 {
58 public:
59  IteratorBase(): mParentNode(NULL) {}
60  IteratorBase(const MaskIterT& iter, NodeT* parent):
61  mParentNode(parent), mMaskIter(iter) {}
62 
63  void operator=(const IteratorBase& other)
64  {
65  mParentNode = other.mParentNode;
66  mMaskIter = other.mMaskIter;
67  }
68 
69  bool operator==(const IteratorBase& other) const
70  {
71  return (mParentNode == other.mParentNode) && (mMaskIter == other.mMaskIter);
72  }
73  bool operator!=(const IteratorBase& other) const
74  {
75  return !(*this == other);
76  }
77 
79  NodeT* getParentNode() const { return mParentNode; }
82  NodeT& parent() const
83  {
84  if (!mParentNode) OPENVDB_THROW(ValueError, "iterator references a null node");
85  return *mParentNode;
86  }
87 
89  Index offset() const { return mMaskIter.offset(); }
90 
92  Index pos() const { return mMaskIter.offset(); }
93 
95  bool test() const { return mMaskIter.test(); }
97  operator bool() const { return this->test(); }
98 
100  bool next() { return mMaskIter.next(); }
102  void increment() { mMaskIter.increment(); }
104  IteratorBase& operator++() { this->increment(); return *this; }
106  void increment(Index n) { mMaskIter.increment(n); }
107 
110  bool isValueOn() const { return parent().isValueMaskOn(this->pos()); }
113  void setValueOn(bool on = true) const { parent().setValueMask(this->pos(), on); }
118  void setValueOff() const { parent().mValueMask.setOff(this->pos()); }
119 
121  Coord getCoord() const { return parent().offset2globalCoord(this->pos()); }
123  void getCoord(Coord& xyz) const { xyz = this->getCoord(); }
124 
125 private:
132  mutable NodeT* mParentNode;
133  MaskIterT mMaskIter;
134 }; // class IteratorBase
135 
136 
138 
139 
141 template<
142  typename MaskIterT, // mask iterator type (OnIterator, OffIterator, etc.)
143  typename IterT, // SparseIteratorBase subclass (the "Curiously Recurring Template Pattern")
144  typename NodeT, // type of node over which to iterate
145  typename ItemT> // type of value to which this iterator points
146 struct SparseIteratorBase: public IteratorBase<MaskIterT, NodeT>
147 {
148  typedef NodeT NodeType;
149  typedef ItemT ValueType;
150  typedef typename boost::remove_const<NodeT>::type NonConstNodeType;
151  typedef typename boost::remove_const<ItemT>::type NonConstValueType;
152  static const bool IsSparseIterator = true, IsDenseIterator = false;
153 
155  SparseIteratorBase(const MaskIterT& iter, NodeT* parent):
156  IteratorBase<MaskIterT, NodeT>(iter, parent) {}
157 
160  ItemT& getItem(Index) const;
163  void setItem(Index, const ItemT&) const;
164 
166  ItemT& operator*() const { return this->getValue(); }
168  ItemT* operator->() const { return &(this->operator*()); }
169 
171  ItemT& getValue() const
172  {
173  return static_cast<const IterT*>(this)->getItem(this->pos()); // static polymorphism
174  }
177  void setValue(const ItemT& value) const
178  {
179  static_cast<const IterT*>(this)->setItem(this->pos(), value); // static polymorphism
180  }
181 }; // class SparseIteratorBase
182 
183 
185 
186 
191 template<
192  typename MaskIterT, // mask iterator type (typically a DenseIterator)
193  typename IterT, // DenseIteratorBase subclass (the "Curiously Recurring Template Pattern")
194  typename NodeT, // type of node over which to iterate
195  typename SetItemT, // type of set value (ChildNodeType, for non-leaf nodes)
196  typename UnsetItemT> // type of unset value (ValueType, usually)
197 struct DenseIteratorBase: public IteratorBase<MaskIterT, NodeT>
198 {
199  typedef NodeT NodeType;
200  typedef UnsetItemT ValueType;
201  typedef SetItemT ChildNodeType;
202  typedef typename boost::remove_const<NodeT>::type NonConstNodeType;
203  typedef typename boost::remove_const<UnsetItemT>::type NonConstValueType;
204  typedef typename boost::remove_const<SetItemT>::type NonConstChildNodeType;
205  static const bool IsSparseIterator = false, IsDenseIterator = true;
206 
208  DenseIteratorBase(const MaskIterT& iter, NodeT* parent):
209  IteratorBase<MaskIterT, NodeT>(iter, parent) {}
210 
215  bool getItem(Index, SetItemT*& child, NonConstValueType& value) const;
218  void setItem(Index, SetItemT*) const;
221  void unsetItem(Index, const UnsetItemT&) const;
222 
224  bool isChildNode() const { return this->parent().isChildMaskOn(this->pos()); }
225 
228  SetItemT* probeChild(NonConstValueType& value) const
229  {
230  SetItemT* child = NULL;
231  static_cast<const IterT*>(this)->getItem(this->pos(), child, value); // static polymorphism
232  return child;
233  }
237  bool probeChild(SetItemT*& child, NonConstValueType& value) const
238  {
239  child = probeChild(value);
240  return (child != NULL);
241  }
242 
245  bool probeValue(NonConstValueType& value) const
246  {
247  SetItemT* child = NULL;
248  const bool isChild = static_cast<const IterT*>(this)-> // static polymorphism
249  getItem(this->pos(), child, value);
250  return !isChild;
251  }
252 
255  void setChild(SetItemT* child) const
256  {
257  static_cast<const IterT*>(this)->setItem(this->pos(), child); // static polymorphism
258  }
259 
262  void setValue(const UnsetItemT& value) const
263  {
264  static_cast<const IterT*>(this)->unsetItem(this->pos(), value); // static polymorphism
265  }
266 }; // struct DenseIteratorBase
267 
268 } // namespace tree
269 } // namespace OPENVDB_VERSION_NAME
270 } // namespace openvdb
271 
272 #endif // OPENVDB_TREE_ITERATOR_HAS_BEEN_INCLUDED
273 
274 // Copyright (c) 2012-2013 DreamWorks Animation LLC
275 // All rights reserved. This software is distributed under the
276 // Mozilla Public License 2.0 ( http://www.mozilla.org/MPL/2.0/ )