OpenVDB  1.1.0
TreeIterator.h
Go to the documentation of this file.
1 
2 //
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 //
32 
33 #ifndef OPENVDB_TREE_TREEITERATOR_HAS_BEEN_INCLUDED
34 #define OPENVDB_TREE_TREEITERATOR_HAS_BEEN_INCLUDED
35 
36 #include <boost/mpl/front.hpp>
37 #include <boost/mpl/pop_front.hpp>
38 #include <boost/mpl/push_back.hpp>
39 #include <boost/mpl/size.hpp>
40 #include <boost/mpl/vector.hpp>
41 #include <boost/static_assert.hpp>
42 #include <boost/type_traits/remove_const.hpp>
43 #include <tbb/blocked_range.h>
44 #include <tbb/parallel_for.h>
45 #include <openvdb/version.h>
46 #include <openvdb/Types.h>
47 
48 // Prior to 0.96.1, depth-bounded value iterators always descended to the leaf level
49 // and iterated past leaf nodes. Now, they never descend past the maximum depth.
50 // Comment out the following line to restore the older, less-efficient behavior:
51 #define ENABLE_TREE_VALUE_DEPTH_BOUND_OPTIMIZATION
52 
53 
54 namespace openvdb {
56 namespace OPENVDB_VERSION_NAME {
57 namespace tree {
58 
65 template<typename FromType, typename ToType> struct CopyConstness {
66  typedef typename boost::remove_const<ToType>::type Type;
67 };
68 template<typename FromType, typename ToType> struct CopyConstness<const FromType, ToType> {
69  typedef const ToType Type;
70 };
71 
72 
74 
75 
76 namespace iter {
77 
78 template<typename HeadT, int HeadLevel>
79 struct InvertedTree {
80  typedef typename InvertedTree<typename HeadT::ChildNodeType, HeadLevel-1>::Type SubtreeT;
81  typedef typename boost::mpl::push_back<SubtreeT, HeadT>::type Type;
82 };
83 template<typename HeadT>
84 struct InvertedTree<HeadT, /*HeadLevel=*/1> {
85  typedef typename boost::mpl::vector<typename HeadT::ChildNodeType, HeadT>::type Type;
86 };
87 
88 } // namespace iter
89 
90 
92 
93 
105 template<typename NodeT, typename IterT>
107 {
108  template<typename ChildT> static ChildT* getChild(const IterT&) { return NULL; }
109 };
110 
111 template<typename NodeT>
112 struct IterTraits<NodeT, typename NodeT::ChildOnIter>
113 {
114  typedef typename NodeT::ChildOnIter IterT;
115  static IterT begin(NodeT& node) { return node.beginChildOn(); }
116  template<typename ChildT> static ChildT* getChild(const IterT& iter) {
117  return &iter.getValue();
118  }
119  template<typename OtherNodeT> struct NodeConverter {
120  typedef typename OtherNodeT::ChildOnIter Type;
121  };
122 };
123 
124 template<typename NodeT>
125 struct IterTraits<NodeT, typename NodeT::ChildOnCIter>
126 {
127  typedef typename NodeT::ChildOnCIter IterT;
128  static IterT begin(const NodeT& node) { return node.cbeginChildOn(); }
129  template<typename ChildT> static const ChildT* getChild(const IterT& iter) {
130  return &iter.getValue();
131  }
132  template<typename OtherNodeT> struct NodeConverter {
133  typedef typename OtherNodeT::ChildOnCIter Type;
134  };
135 };
136 
137 template<typename NodeT>
138 struct IterTraits<NodeT, typename NodeT::ChildOffIter>
139 {
140  typedef typename NodeT::ChildOffIter IterT;
141  static IterT begin(NodeT& node) { return node.beginChildOff(); }
142  template<typename OtherNodeT> struct NodeConverter {
143  typedef typename OtherNodeT::ChildOffIter Type;
144  };
145 };
146 
147 template<typename NodeT>
148 struct IterTraits<NodeT, typename NodeT::ChildOffCIter>
149 {
150  typedef typename NodeT::ChildOffCIter IterT;
151  static IterT begin(const NodeT& node) { return node.cbeginChildOff(); }
152  template<typename OtherNodeT> struct NodeConverter {
153  typedef typename OtherNodeT::ChildOffCIter Type;
154  };
155 };
156 
157 template<typename NodeT>
158 struct IterTraits<NodeT, typename NodeT::ChildAllIter>
159 {
160  typedef typename NodeT::ChildAllIter IterT;
161  static IterT begin(NodeT& node) { return node.beginChildAll(); }
162  template<typename ChildT> static ChildT* getChild(const IterT& iter) {
163  typename IterT::NonConstValueType val;
164  return iter.probeChild(val);
165  }
166  template<typename OtherNodeT> struct NodeConverter {
167  typedef typename OtherNodeT::ChildAllIter Type;
168  };
169 };
170 
171 template<typename NodeT>
172 struct IterTraits<NodeT, typename NodeT::ChildAllCIter>
173 {
174  typedef typename NodeT::ChildAllCIter IterT;
175  static IterT begin(const NodeT& node) { return node.cbeginChildAll(); }
176  template<typename ChildT> static ChildT* getChild(const IterT& iter) {
177  typename IterT::NonConstValueType val;
178  return iter.probeChild(val);
179  }
180  template<typename OtherNodeT> struct NodeConverter {
181  typedef typename OtherNodeT::ChildAllCIter Type;
182  };
183 };
184 
185 template<typename NodeT>
186 struct IterTraits<NodeT, typename NodeT::ValueOnIter>
187 {
188  typedef typename NodeT::ValueOnIter IterT;
189  static IterT begin(NodeT& node) { return node.beginValueOn(); }
190  template<typename OtherNodeT> struct NodeConverter {
191  typedef typename OtherNodeT::ValueOnIter Type;
192  };
193 };
194 
195 template<typename NodeT>
196 struct IterTraits<NodeT, typename NodeT::ValueOnCIter>
197 {
198  typedef typename NodeT::ValueOnCIter IterT;
199  static IterT begin(const NodeT& node) { return node.cbeginValueOn(); }
200  template<typename OtherNodeT> struct NodeConverter {
201  typedef typename OtherNodeT::ValueOnCIter Type;
202  };
203 };
204 
205 template<typename NodeT>
206 struct IterTraits<NodeT, typename NodeT::ValueOffIter>
207 {
208  typedef typename NodeT::ValueOffIter IterT;
209  static IterT begin(NodeT& node) { return node.beginValueOff(); }
210  template<typename OtherNodeT> struct NodeConverter {
211  typedef typename OtherNodeT::ValueOffIter Type;
212  };
213 };
214 
215 template<typename NodeT>
216 struct IterTraits<NodeT, typename NodeT::ValueOffCIter>
217 {
218  typedef typename NodeT::ValueOffCIter IterT;
219  static IterT begin(const NodeT& node) { return node.cbeginValueOff(); }
220  template<typename OtherNodeT> struct NodeConverter {
221  typedef typename OtherNodeT::ValueOffCIter Type;
222  };
223 };
224 
225 template<typename NodeT>
226 struct IterTraits<NodeT, typename NodeT::ValueAllIter>
227 {
228  typedef typename NodeT::ValueAllIter IterT;
229  static IterT begin(NodeT& node) { return node.beginValueAll(); }
230  template<typename OtherNodeT> struct NodeConverter {
231  typedef typename OtherNodeT::ValueAllIter Type;
232  };
233 };
234 
235 template<typename NodeT>
236 struct IterTraits<NodeT, typename NodeT::ValueAllCIter>
237 {
238  typedef typename NodeT::ValueAllCIter IterT;
239  static IterT begin(const NodeT& node) { return node.cbeginValueAll(); }
240  template<typename OtherNodeT> struct NodeConverter {
241  typedef typename OtherNodeT::ValueAllCIter Type;
242  };
243 };
244 
245 
247 
248 
259 template<typename PrevItemT, typename NodeVecT, size_t VecSize, Index _Level>
261 {
262 public:
264  typedef typename PrevItemT::IterT PrevIterT;
266  typedef typename boost::mpl::front<NodeVecT>::type _NodeT;
269  NodeConverter<_NodeT>::Type IterT;
270 
272  typedef typename IterT::NodeType NodeT;
274  typedef typename IterT::NonConstNodeType NCNodeT;
276  typedef typename IterT::NonConstValueType NCValueT;
283  static const Index Level = _Level;
284 
285  IterListItem(PrevItemT* prev): mNext(this), mPrev(prev) {}
286 
287  IterListItem(const IterListItem& other): mIter(other.mIter), mNext(other.mNext), mPrev(NULL) {}
288  IterListItem& operator=(const IterListItem& other)
289  {
290  if (&other != this) {
291  mIter = other.mIter;
292  mNext = other.mNext;
293  mPrev = NULL;
294  }
295  return *this;
296  }
297 
298  void updateBackPointers(PrevItemT* prev) { mPrev = prev; mNext.updateBackPointers(this); }
299 
300  void setIter(const IterT& iter) { mIter = iter; }
301  template<typename OtherIterT>
302  void setIter(const OtherIterT& iter) { mNext.setIter(iter); }
303 
305  void getNode(Index lvl, NodeT*& node) const
306  {
307  node = (lvl <= Level) ? mIter.getParentNode() : NULL;
308  }
310  template<typename OtherNodeT>
311  void getNode(Index lvl, OtherNodeT*& node) const { mNext.getNode(lvl, node); }
312 
318  template<typename OtherIterListItemT>
319  void initLevel(Index lvl, OtherIterListItemT& otherListItem)
320  {
321  if (lvl == Level) {
322  const NodeT* node = NULL;
323  otherListItem.getNode(lvl, node);
324  mIter = (node == NULL) ? IterT() : ITraits::begin(*const_cast<NodeT*>(node));
325  } else {
326  // Forward to one of the following list elements.
327  mNext.initLevel(lvl, otherListItem);
328  }
329  }
330 
332  Index pos(Index lvl) const { return (lvl == Level) ? mIter.pos() : mNext.pos(lvl); }
333 
335  bool test(Index lvl) const { return (lvl == Level) ? mIter.test() : mNext.test(lvl); }
336 
338  bool next(Index lvl) { return (lvl == Level) ? mIter.next() : mNext.next(lvl); }
339 
342  bool down(Index lvl)
343  {
344  if (lvl == Level && mPrev != NULL && mIter) {
345  if (ChildT* child = ITraits::template getChild<ChildT>(mIter)) {
346  mPrev->setIter(PrevItemT::ITraits::begin(*child));
347  return true;
348  }
349  }
350  return (lvl > Level) ? mNext.down(lvl) : false;
351  }
352 
355  Coord getCoord(Index lvl) const
356  {
357  return (lvl == Level) ? mIter.getCoord() : mNext.getCoord(lvl);
358  }
359  Index getChildDim(Index lvl) const
360  {
361  return (lvl == Level) ? NodeT::getChildDim() : mNext.getChildDim(lvl);
362  }
364  Index64 getVoxelCount(Index lvl) const
365  {
366  return (lvl == Level) ? ChildT::NUM_VOXELS : mNext.getVoxelCount(lvl);
367  }
368 
370  bool isValueOn(Index lvl) const
371  {
372  return (lvl == Level) ? mIter.isValueOn() : mNext.isValueOn(lvl);
373  }
374 
376  const NCValueT& getValue(Index lvl) const
377  {
378  if (lvl == Level) return mIter.getValue();
379  return mNext.getValue(lvl);
380  }
381 
385  void setValue(Index lvl, const NCValueT& val) const
386  {
387  if (lvl == Level) mIter.setValue(val); else mNext.setValue(lvl, val);
388  }
392  void setValueOn(Index lvl, bool on = true) const
393  {
394  if (lvl == Level) mIter.setValueOn(on); else mNext.setValueOn(lvl, on);
395  }
399  void setValueOff(Index lvl) const
400  {
401  if (lvl == Level) mIter.setValueOff(); else mNext.setValueOff(lvl);
402  }
403 
404 private:
405  typedef typename boost::mpl::pop_front<NodeVecT>::type RestT; // NodeVecT minus its first item
406  typedef IterListItem<IterListItem, RestT, VecSize - 1, Level + 1> NextItem;
407 
408  IterT mIter;
409  NextItem mNext;
410  PrevItemT* mPrev;
411 };
412 
413 
415 template<typename PrevItemT, typename NodeVecT, size_t VecSize>
416 class IterListItem<PrevItemT, NodeVecT, VecSize, /*Level=*/0U>
417 {
418 public:
420  typedef typename PrevItemT::IterT PrevIterT;
422  typedef typename boost::mpl::front<NodeVecT>::type _NodeT;
425  NodeConverter<_NodeT>::Type IterT;
426 
428  typedef typename IterT::NodeType NodeT;
430  typedef typename IterT::NonConstNodeType NCNodeT;
432  typedef typename IterT::NonConstValueType NCValueT;
435  static const Index Level = 0;
436 
437  IterListItem(PrevItemT*): mNext(this), mPrev(NULL) {}
438 
439  IterListItem(const IterListItem& other): mIter(other.mIter), mNext(other.mNext), mPrev(NULL) {}
440  IterListItem& operator=(const IterListItem& other)
441  {
442  if (&other != this) {
443  mIter = other.mIter;
444  mNext = other.mNext;
445  mPrev = NULL;
446  }
447  return *this;
448  }
449 
450  void updateBackPointers(PrevItemT* = NULL) { mPrev = NULL; mNext.updateBackPointers(this); }
451 
452  void setIter(const IterT& iter) { mIter = iter; }
453  template<typename OtherIterT>
454  void setIter(const OtherIterT& iter) { mNext.setIter(iter); }
455 
456  void getNode(Index lvl, NodeT*& node) const
457  {
458  node = (lvl == 0) ? mIter.getParentNode() : NULL;
459  }
460  template<typename OtherNodeT>
461  void getNode(Index lvl, OtherNodeT*& node) const { mNext.getNode(lvl, node); }
462 
463  template<typename OtherIterListItemT>
464  void initLevel(Index lvl, OtherIterListItemT& otherListItem)
465  {
466  if (lvl == 0) {
467  const NodeT* node = NULL;
468  otherListItem.getNode(lvl, node);
469  mIter = (node == NULL) ? IterT() : ITraits::begin(*const_cast<NodeT*>(node));
470  } else {
471  mNext.initLevel(lvl, otherListItem);
472  }
473  }
474 
475  Index pos(Index lvl) const { return (lvl == 0) ? mIter.pos() : mNext.pos(lvl); }
476 
477  bool test(Index lvl) const { return (lvl == 0) ? mIter.test() : mNext.test(lvl); }
478 
479  bool next(Index lvl) { return (lvl == 0) ? mIter.next() : mNext.next(lvl); }
480 
481  bool down(Index lvl) { return (lvl == 0) ? false : mNext.down(lvl); }
482 
483  Coord getCoord(Index lvl) const
484  {
485  return (lvl == 0) ? mIter.getCoord() : mNext.getCoord(lvl);
486  }
487  Index getChildDim(Index lvl) const
488  {
489  return (lvl == 0) ? NodeT::getChildDim() : mNext.getChildDim(lvl);
490  }
491 
492  Index64 getVoxelCount(Index lvl) const
493  {
494  return (lvl == 0) ? 1 : mNext.getVoxelCount(lvl);
495  }
496 
497  bool isValueOn(Index lvl) const
498  {
499  return (lvl == 0) ? mIter.isValueOn() : mNext.isValueOn(lvl);
500  }
501 
502  const NCValueT& getValue(Index lvl) const
503  {
504  if (lvl == 0) return mIter.getValue();
505  return mNext.getValue(lvl);
506  }
507 
508  void setValue(Index lvl, const NCValueT& val) const
509  {
510  if (lvl == 0) mIter.setValue(val); else mNext.setValue(lvl, val);
511  }
512  void setValueOn(Index lvl, bool on = true) const
513  {
514  if (lvl == 0) mIter.setValueOn(on); else mNext.setValueOn(lvl, on);
515  }
516  void setValueOff(Index lvl) const
517  {
518  if (lvl == 0) mIter.setValueOff(); else mNext.setValueOff(lvl);
519  }
520 
521 private:
522  typedef typename boost::mpl::pop_front<NodeVecT>::type RestT; // NodeVecT minus its first item
523  typedef IterListItem<IterListItem, RestT, VecSize - 1, /*Level=*/1> NextItem;
524 
525  IterT mIter;
526  NextItem mNext;
527  PrevItemT* mPrev;
528 };
529 
530 
532 template<typename PrevItemT, typename NodeVecT, Index _Level>
533 class IterListItem<PrevItemT, NodeVecT, /*VecSize=*/1, _Level>
534 {
535 public:
536  typedef typename boost::mpl::front<NodeVecT>::type _NodeT;
538  typedef typename PrevItemT::IterT PrevIterT;
541  NodeConverter<_NodeT>::Type IterT;
542 
544  typedef typename IterT::NodeType NodeT;
546  typedef typename IterT::NonConstNodeType NCNodeT;
548  typedef typename IterT::NonConstValueType NCValueT;
555  static const Index Level = _Level;
556 
557  IterListItem(PrevItemT* prev): mPrev(prev) {}
558 
559  IterListItem(const IterListItem& other): mIter(other.mIter), mPrev(NULL) {}
560  IterListItem& operator=(const IterListItem& other)
561  {
562  if (&other != this) {
563  mIter = other.mIter;
564  mPrev = NULL;
565  }
566  return *this;
567  }
568 
569  void updateBackPointers(PrevItemT* prev) { mPrev = prev; }
570 
571  // The following method specializations differ from the default template
572  // implementations mainly in that they don't forward.
573 
574  void setIter(const IterT& iter) { mIter = iter; }
575 
576  void getNode(Index lvl, NodeT*& node) const
577  {
578  node = (lvl <= Level) ? mIter.getParentNode() : NULL;
579  }
580 
581  template<typename OtherIterListItemT>
582  void initLevel(Index lvl, OtherIterListItemT& otherListItem)
583  {
584  if (lvl == Level) {
585  const NodeT* node = NULL;
586  otherListItem.getNode(lvl, node);
587  mIter = (node == NULL) ? IterT() : ITraits::begin(*const_cast<NodeT*>(node));
588  }
589  }
590 
591  Index pos(Index lvl) const { return (lvl == Level) ? mIter.pos() : Index(-1); }
592 
593  bool test(Index lvl) const { return (lvl == Level) ? mIter.test() : false; }
594 
595  bool next(Index lvl) { return (lvl == Level) ? mIter.next() : false; }
596 
597  bool down(Index lvl)
598  {
599  if (lvl == Level && mPrev != NULL && mIter) {
600  if (ChildT* child = ITraits::template getChild<ChildT>(mIter)) {
601  mPrev->setIter(PrevItemT::ITraits::begin(*child));
602  return true;
603  }
604  }
605  return false;
606  }
607 
608  Coord getCoord(Index lvl) const { return (lvl == Level) ? mIter.getCoord() : Coord(); }
609  Index getChildDim(Index lvl) const { return (lvl == Level) ? NodeT::getChildDim() : 0; }
610  Index64 getVoxelCount(Index lvl) const { return (lvl == Level) ? ChildT::NUM_VOXELS : 0; }
611 
612  bool isValueOn(Index lvl) const { return (lvl == Level) ? mIter.isValueOn() : false; }
613 
614  const NCValueT& getValue(Index lvl) const
615  {
616  assert(lvl == Level);
617  (void)lvl; // avoid unused variable warning in optimized builds
618  return mIter.getValue();
619  }
620 
621  void setValue(Index lvl, const NCValueT& val) const { if (lvl == Level) mIter.setValue(val); }
622  void setValueOn(Index lvl, bool on = true) const { if (lvl == Level) mIter.setValueOn(on); }
623  void setValueOff(Index lvl) const { if (lvl == Level) mIter.setValueOff(); }
624 
625 private:
626  IterT mIter;
627  PrevItemT* mPrev;
628 };
629 
630 
632 
633 
634 //#define DEBUG_TREE_VALUE_ITERATOR
635 
637 template<typename _TreeT, typename ValueIterT>
639 {
640 public:
641  typedef _TreeT TreeT;
642  typedef typename ValueIterT::NodeType NodeT;
643  typedef typename ValueIterT::NonConstValueType ValueT;
644  typedef typename NodeT::ChildOnCIter ChildOnIterT;
645  static const Index ROOT_LEVEL = NodeT::LEVEL;
646  BOOST_STATIC_ASSERT(ValueIterT::NodeType::LEVEL == ROOT_LEVEL);
647  static const Index LEAF_LEVEL = 0, ROOT_DEPTH = 0, LEAF_DEPTH = ROOT_LEVEL;
648 
650 
652  TreeValueIteratorBase& operator=(const TreeValueIteratorBase& other);
653 
655  void setMinDepth(Index minDepth);
657  Index getMinDepth() const { return ROOT_LEVEL - Index(mMaxLevel); }
659  void setMaxDepth(Index maxDepth);
661  Index getMaxDepth() const { return ROOT_LEVEL - Index(mMinLevel); }
662 
664 
665  bool test() const { return mValueIterList.test(mLevel); }
666  operator bool() const { return this->test(); }
668 
671  bool next();
673  TreeValueIteratorBase& operator++() { this->next(); return *this; }
674 
677  Index getLevel() const { return mLevel; }
680  Index getDepth() const { return ROOT_LEVEL - mLevel; }
681  static Index getLeafDepth() { return LEAF_DEPTH; }
682 
687  template<typename NodeType>
688  void getNode(NodeType*& node) const { mValueIterList.getNode(mLevel, node); }
689 
692  Coord getCoord() const { return mValueIterList.getCoord(mLevel); }
696  bool getBoundingBox(CoordBBox&) const;
699  CoordBBox getBoundingBox() const { CoordBBox b; this->getBoundingBox(b); return b; }
700 
702  Index64 getVoxelCount() const { return mValueIterList.getVoxelCount(mLevel);}
703 
705  bool isTileValue() const { return mLevel != 0 && this->test(); }
707  bool isVoxelValue() const { return mLevel == 0 && this->test(); }
709  bool isValueOn() const { return mValueIterList.isValueOn(mLevel); }
710 
712 
713  const ValueT& getValue() const { return mValueIterList.getValue(mLevel); }
714  const ValueT& operator*() const { return this->getValue(); }
715  const ValueT* operator->() const { return &(this->operator*()); }
717 
720  void setValue(const ValueT& val) const { mValueIterList.setValue(mLevel, val); }
723  void setActiveState(bool on) const { mValueIterList.setValueOn(mLevel, on); }
725  void setValueOff() const { mValueIterList.setValueOff(mLevel); }
726 
728  TreeT* getTree() const { return mTree; }
729 
731  std::string summary() const;
732 
733 private:
734  bool advance(bool dontIncrement = false);
735 
736  typedef typename iter::InvertedTree<NodeT, NodeT::LEVEL>::Type InvTreeT;
737  struct PrevChildItem { typedef ChildOnIterT IterT; };
738  struct PrevValueItem { typedef ValueIterT IterT; };
739 
740  IterListItem<PrevChildItem, InvTreeT, /*VecSize=*/ROOT_LEVEL+1, /*Level=*/0> mChildIterList;
741  IterListItem<PrevValueItem, InvTreeT, /*VecSize=*/ROOT_LEVEL+1, /*Level=*/0> mValueIterList;
742  Index mLevel;
743  int mMinLevel, mMaxLevel;
744  TreeT* mTree;
745 }; // class TreeValueIteratorBase
746 
747 
748 template<typename TreeT, typename ValueIterT>
749 inline
751  mChildIterList(NULL),
752  mValueIterList(NULL),
753  mLevel(ROOT_LEVEL),
754  mMinLevel(int(LEAF_LEVEL)),
755  mMaxLevel(int(ROOT_LEVEL)),
756  mTree(&tree)
757 {
758  mChildIterList.setIter(IterTraits<NodeT, ChildOnIterT>::begin(tree.getRootNode()));
759  mValueIterList.setIter(IterTraits<NodeT, ValueIterT>::begin(tree.getRootNode()));
760  this->advance(/*dontIncrement=*/true);
761 }
762 
763 
764 template<typename TreeT, typename ValueIterT>
765 inline
767  mChildIterList(other.mChildIterList),
768  mValueIterList(other.mValueIterList),
769  mLevel(other.mLevel),
770  mMinLevel(other.mMinLevel),
771  mMaxLevel(other.mMaxLevel),
772  mTree(other.mTree)
773 {
774  mChildIterList.updateBackPointers();
775  mValueIterList.updateBackPointers();
776 }
777 
778 
779 template<typename TreeT, typename ValueIterT>
782 {
783  if (&other != this) {
784  mChildIterList = other.mChildIterList;
785  mValueIterList = other.mValueIterList;
786  mLevel = other.mLevel;
787  mMinLevel = other.mMinLevel;
788  mMaxLevel = other.mMaxLevel;
789  mTree = other.mTree;
790  mChildIterList.updateBackPointers();
791  mValueIterList.updateBackPointers();
792  }
793  return *this;
794 }
795 
796 
797 template<typename TreeT, typename ValueIterT>
798 inline void
800 {
801  mMaxLevel = int(ROOT_LEVEL - minDepth); // level = ROOT_LEVEL - depth
802  if (int(mLevel) > mMaxLevel) this->next();
803 }
804 
805 
806 template<typename TreeT, typename ValueIterT>
807 inline void
809 {
810  // level = ROOT_LEVEL - depth
811  mMinLevel = int(ROOT_LEVEL - std::min(maxDepth, this->getLeafDepth()));
812  if (int(mLevel) < mMinLevel) this->next();
813 }
814 
815 
816 template<typename TreeT, typename ValueIterT>
817 inline bool
819 {
820  do {
821  if (!this->advance()) return false;
822  } while (int(mLevel) < mMinLevel || int(mLevel) > mMaxLevel);
823  return true;
824 }
825 
826 
827 template<typename TreeT, typename ValueIterT>
828 inline bool
830 {
831  Index
832  vPos = mValueIterList.pos(mLevel),
833  cPos = mChildIterList.pos(mLevel);
834  if (vPos == cPos && mChildIterList.test(mLevel)) {
836  mValueIterList.next(mLevel);
837  vPos = mValueIterList.pos(mLevel);
838  }
839  if (vPos < cPos) {
840  if (dontIncrement) return true;
841  if (mValueIterList.next(mLevel)) {
842  if (mValueIterList.pos(mLevel) == cPos && mChildIterList.test(mLevel)) {
844  mValueIterList.next(mLevel);
845  }
846  // If there is a next value and it precedes the next child, return.
847  if (mValueIterList.pos(mLevel) < cPos) return true;
848  }
849  } else {
850  // Advance to the next child, which may or may not precede the next value.
851  if (!dontIncrement) mChildIterList.next(mLevel);
852  }
853 #ifdef DEBUG_TREE_VALUE_ITERATOR
854  std::cout << "\n" << this->summary() << std::flush;
855 #endif
856 
857  // Descend to the lowest level at which the next value precedes the next child.
858  while (mChildIterList.pos(mLevel) < mValueIterList.pos(mLevel)) {
859 #ifdef ENABLE_TREE_VALUE_DEPTH_BOUND_OPTIMIZATION
860  if (int(mLevel) == mMinLevel) {
861  // If the current node lies at the lowest allowed level, none of its
862  // children can be visited, so advance its child iterator to the end.
865  while (mChildIterList.test(mLevel)) mChildIterList.next(mLevel);
866  } else
867 #endif
868  if (mChildIterList.down(mLevel)) {
869  --mLevel; // descend one level
870  mValueIterList.initLevel(mLevel, mChildIterList);
871  if (mValueIterList.pos(mLevel) == mChildIterList.pos(mLevel)
872  && mChildIterList.test(mLevel))
873  {
875  mValueIterList.next(mLevel);
876  }
877  } else break;
878 #ifdef DEBUG_TREE_VALUE_ITERATOR
879  std::cout << "\n" << this->summary() << std::flush;
880 #endif
881  }
882  // Ascend to the nearest level at which one of the iterators is not yet exhausted.
883  while (!mChildIterList.test(mLevel) && !mValueIterList.test(mLevel)) {
884  if (mLevel == ROOT_LEVEL) return false;
885  ++mLevel;
886  mChildIterList.next(mLevel);
887  this->advance(/*dontIncrement=*/true);
888  }
889  return true;
890 }
891 
892 
893 template<typename TreeT, typename ValueIterT>
894 inline bool
896 {
897  if (!this->test()) {
898  bbox = CoordBBox();
899  return false;
900  }
901  bbox.min() = mValueIterList.getCoord(mLevel);
902  bbox.max() = bbox.min().offsetBy(mValueIterList.getChildDim(mLevel) - 1);
903  return true;
904 }
905 
906 
907 template<typename TreeT, typename ValueIterT>
908 inline std::string
910 {
911  std::ostringstream ostr;
912  for (int lvl = int(ROOT_LEVEL); lvl >= 0 && lvl >= int(mLevel); --lvl) {
913  if (lvl == 0) ostr << "leaf";
914  else if (lvl == int(ROOT_LEVEL)) ostr << "root";
915  else ostr << "int" << (ROOT_LEVEL - lvl);
916  ostr << " v" << mValueIterList.pos(lvl)
917  << " c" << mChildIterList.pos(lvl);
918  if (lvl > int(mLevel)) ostr << " / ";
919  }
920  if (this->test() && mValueIterList.pos(mLevel) < mChildIterList.pos(mLevel)) {
921  if (mLevel == 0) {
922  ostr << " " << this->getCoord();
923  } else {
924  ostr << " " << this->getBoundingBox();
925  }
926  }
927  return ostr.str();
928 }
929 
930 
932 
933 
935 template<typename _TreeT, typename RootChildOnIterT>
937 {
938 public:
939  typedef _TreeT TreeT;
940  typedef RootChildOnIterT RootIterT;
941  typedef typename RootIterT::NodeType RootNodeT;
942  typedef typename RootIterT::NonConstNodeType NCRootNodeT;
943  static const Index ROOT_LEVEL = RootNodeT::LEVEL;
945  static const Index LEAF_LEVEL = 0, ROOT_DEPTH = 0, LEAF_DEPTH = ROOT_LEVEL;
946 
948 
951 
952  NodeIteratorBase(const NodeIteratorBase& other);
954 
956  void setMinDepth(Index minDepth);
958  Index getMinDepth() const { return ROOT_LEVEL - Index(mMaxLevel); }
960  void setMaxDepth(Index maxDepth);
962  Index getMaxDepth() const { return ROOT_LEVEL - Index(mMinLevel); }
963 
965 
966  bool test() const { return !mDone; }
967  operator bool() const { return this->test(); }
969 
972  bool next();
974  void increment() { this->next(); }
975  NodeIteratorBase& operator++() { this->increment(); return *this; }
977  void increment(Index n) { for (Index i = 0; i < n && this->next(); ++i) {} }
978 
981  Index getLevel() const { return mLevel; }
984  Index getDepth() const { return ROOT_LEVEL - mLevel; }
985  static Index getLeafDepth() { return LEAF_DEPTH; }
986 
989  Coord getCoord() const;
993  bool getBoundingBox(CoordBBox& bbox) const;
996  CoordBBox getBoundingBox() const { CoordBBox b; this->getBoundingBox(b); return b; }
997 
1001  template<typename NodeT>
1002  void getNode(NodeT*& node) const { node = NULL; mIterList.getNode(mLevel, node); }
1003 
1004  TreeT* getTree() const { return mTree; }
1005 
1006  std::string summary() const;
1007 
1008 private:
1009  struct PrevItem { typedef RootIterT IterT; };
1010 
1011  IterListItem<PrevItem, InvTreeT, /*VecSize=*/ROOT_LEVEL+1, LEAF_LEVEL> mIterList;
1012  Index mLevel;
1013  int mMinLevel, mMaxLevel;
1014  bool mDone;
1015  TreeT* mTree;
1016 }; // class NodeIteratorBase
1017 
1018 
1019 template<typename TreeT, typename RootChildOnIterT>
1020 inline
1022  mIterList(NULL),
1023  mLevel(ROOT_LEVEL),
1024  mMinLevel(int(LEAF_LEVEL)),
1025  mMaxLevel(int(ROOT_LEVEL)),
1026  mDone(true),
1027  mTree(NULL)
1028 {
1029 }
1030 
1031 
1032 template<typename TreeT, typename RootChildOnIterT>
1033 inline
1035  mIterList(NULL),
1036  mLevel(ROOT_LEVEL),
1037  mMinLevel(int(LEAF_LEVEL)),
1038  mMaxLevel(int(ROOT_LEVEL)),
1039  mDone(false),
1040  mTree(&tree)
1041 {
1042  mIterList.setIter(RootIterTraits::begin(tree.getRootNode()));
1043 }
1044 
1045 
1046 template<typename TreeT, typename RootChildOnIterT>
1047 inline
1049  mIterList(other.mIterList),
1050  mLevel(other.mLevel),
1051  mMinLevel(other.mMinLevel),
1052  mMaxLevel(other.mMaxLevel),
1053  mDone(other.mDone),
1054  mTree(other.mTree)
1055 {
1056  mIterList.updateBackPointers();
1057 }
1058 
1059 
1060 template<typename TreeT, typename RootChildOnIterT>
1063 {
1064  if (&other != this) {
1065  mLevel = other.mLevel;
1066  mMinLevel = other.mMinLevel;
1067  mMaxLevel = other.mMaxLevel;
1068  mDone = other.mDone;
1069  mTree = other.mTree;
1070  mIterList = other.mIterList;
1071  mIterList.updateBackPointers();
1072  }
1073  return *this;
1074 }
1075 
1076 
1077 template<typename TreeT, typename RootChildOnIterT>
1078 inline void
1080 {
1081  mMaxLevel = int(ROOT_LEVEL - minDepth); // level = ROOT_LEVEL - depth
1082  if (int(mLevel) > mMaxLevel) this->next();
1083 }
1084 
1085 
1086 template<typename TreeT, typename RootChildOnIterT>
1087 inline void
1089 {
1090  // level = ROOT_LEVEL - depth
1091  mMinLevel = int(ROOT_LEVEL - std::min(maxDepth, this->getLeafDepth()));
1092  if (int(mLevel) < mMinLevel) this->next();
1093 }
1094 
1095 
1096 template<typename TreeT, typename RootChildOnIterT>
1097 inline bool
1099 {
1100  do {
1101  if (mDone) return false;
1102 
1103  // If the iterator over the current node points to a child,
1104  // descend to the child (depth-first traversal).
1105  if (int(mLevel) > mMinLevel && mIterList.test(mLevel)) {
1106  if (!mIterList.down(mLevel)) return false;
1107  --mLevel;
1108  } else {
1109  // Ascend to the nearest ancestor that has other children.
1110  while (!mIterList.test(mLevel)) {
1111  if (mLevel == ROOT_LEVEL) {
1112  // Can't ascend higher than the root.
1113  mDone = true;
1114  return false;
1115  }
1116  ++mLevel; // ascend one level
1117  mIterList.next(mLevel); // advance to the next child, if there is one
1118  }
1119  // Descend to the child.
1120  if (!mIterList.down(mLevel)) return false;
1121  --mLevel;
1122  }
1123  } while (int(mLevel) < mMinLevel || int(mLevel) > mMaxLevel);
1124  return true;
1125 }
1126 
1127 
1128 template<typename TreeT, typename RootChildOnIterT>
1129 inline Coord
1131 {
1132  if (mLevel != ROOT_LEVEL) return mIterList.getCoord(mLevel + 1);
1133  RootNodeT* root = NULL;
1134  this->getNode(root);
1135  return root ? root->getMinIndex() : Coord::min();
1136 }
1137 
1138 
1139 template<typename TreeT, typename RootChildOnIterT>
1140 inline bool
1142 {
1143  if (mLevel == ROOT_LEVEL) {
1144  RootNodeT* root = NULL;
1145  this->getNode(root);
1146  if (root == NULL) {
1147  bbox = CoordBBox();
1148  return false;
1149  }
1150  root->getIndexRange(bbox);
1151  return true;
1152  }
1153  bbox.min() = mIterList.getCoord(mLevel + 1);
1154  bbox.max() = bbox.min().offsetBy(mIterList.getChildDim(mLevel + 1) - 1);
1155  return true;
1156 }
1157 
1158 
1159 template<typename TreeT, typename RootChildOnIterT>
1160 inline std::string
1162 {
1163  std::ostringstream ostr;
1164  for (int lvl = int(ROOT_LEVEL); lvl >= 0 && lvl >= int(mLevel); --lvl) {
1165  if (lvl == 0) ostr << "leaf";
1166  else if (lvl == int(ROOT_LEVEL)) ostr << "root";
1167  else ostr << "int" << (ROOT_LEVEL - lvl);
1168  ostr << " c" << mIterList.pos(lvl);
1169  if (lvl > int(mLevel)) ostr << " / ";
1170  }
1171  CoordBBox bbox;
1172  this->getBoundingBox(bbox);
1173  ostr << " " << bbox;
1174  return ostr.str();
1175 }
1176 
1177 
1179 
1180 
1182 template<typename TreeT, typename RootChildOnIterT>
1184 {
1185 public:
1186  typedef RootChildOnIterT RootIterT;
1187  typedef typename RootIterT::NodeType RootNodeT;
1188  typedef typename RootIterT::NonConstNodeType NCRootNodeT;
1189  static const Index ROOT_LEVEL = RootNodeT::LEVEL;
1191  typedef typename boost::mpl::front<InvTreeT>::type NCLeafNodeT;
1194 
1196 
1197  LeafIteratorBase(): mIterList(NULL), mTree(NULL) {}
1198 
1199  LeafIteratorBase(TreeT& tree): mIterList(NULL), mTree(&tree)
1200  {
1201  // Initialize the iterator list with a root node iterator.
1202  mIterList.setIter(RootIterTraits::begin(tree.getRootNode()));
1203  // Descend along the first branch, initializing the node iterator at each level.
1204  Index lvl = ROOT_LEVEL;
1205  for ( ; lvl > 0 && mIterList.down(lvl); --lvl) {}
1206  // If the first branch terminated above the leaf level, backtrack to the next branch.
1207  if (lvl > 0) this->next();
1208  }
1209 
1210  LeafIteratorBase(const LeafIteratorBase& other): mIterList(other.mIterList), mTree(other.mTree)
1211  {
1212  mIterList.updateBackPointers();
1213  }
1215  {
1216  if (&other != this) {
1217  mTree = other.mTree;
1218  mIterList = other.mIterList;
1219  mIterList.updateBackPointers();
1220  }
1221  return *this;
1222  }
1223 
1225 
1226  LeafNodeT* getLeaf() const { LeafNodeT* n = NULL; mIterList.getNode(LEAF_LEVEL, n); return n; }
1227  LeafNodeT& operator*() const { return *this->getLeaf(); }
1228  LeafNodeT* operator->() const { return this->getLeaf(); }
1230 
1231  bool test() const { return mIterList.test(LEAF_PARENT_LEVEL); }
1232  operator bool() const { return this->test(); }
1233 
1235 
1236  bool next();
1237  void increment() { this->next(); }
1238  LeafIteratorBase& operator++() { this->increment(); return *this; }
1240 
1241  void increment(Index n) { for (Index i = 0; i < n && this->next(); ++i) {} }
1242 
1243  TreeT* getTree() const { return mTree; }
1244 
1245 private:
1246  struct PrevItem { typedef RootIterT IterT; };
1247 
1251  IterListItem<PrevItem, InvTreeT, /*VecSize=*/ROOT_LEVEL+1, LEAF_LEVEL> mIterList;
1252  TreeT* mTree;
1253 }; // class LeafIteratorBase
1254 
1255 
1256 template<typename TreeT, typename RootChildOnIterT>
1257 inline bool
1259 {
1260  // If the iterator is valid for the current node one level above the leaf level,
1261  // advance the iterator to the node's next child.
1262  if (mIterList.test(LEAF_PARENT_LEVEL) && mIterList.next(LEAF_PARENT_LEVEL)) {
1263  mIterList.down(LEAF_PARENT_LEVEL); // initialize the leaf iterator
1264  return true;
1265  }
1266 
1267  Index lvl = LEAF_PARENT_LEVEL;
1268  while (!mIterList.test(LEAF_PARENT_LEVEL)) {
1269  if (mIterList.test(lvl)) {
1270  mIterList.next(lvl);
1271  } else {
1272  do {
1273  // Ascend to the nearest level at which
1274  // one of the iterators is not yet exhausted.
1275  if (lvl == ROOT_LEVEL) return false;
1276  ++lvl;
1277  if (mIterList.test(lvl)) mIterList.next(lvl);
1278  } while (!mIterList.test(lvl));
1279  }
1280  // Descend to the lowest child, but not as far as the leaf iterator.
1281  while (lvl > LEAF_PARENT_LEVEL && mIterList.down(lvl)) --lvl;
1282  }
1283  mIterList.down(LEAF_PARENT_LEVEL); // initialize the leaf iterator
1284  return true;
1285 }
1286 
1287 
1289 
1290 
1293 template<typename IterT>
1295 {
1296 public:
1297  IteratorRange(const IterT& iter, size_t grainSize = 8):
1298  mIter(iter),
1299  mGrainSize(grainSize),
1300  mSize(0)
1301  {
1302  mSize = this->size();
1303  }
1304  IteratorRange(IteratorRange& other, tbb::split):
1305  mIter(other.mIter),
1306  mGrainSize(other.mGrainSize),
1307  mSize(other.mSize >> 1)
1308  {
1309  other.increment(mSize);
1310  }
1311 
1315  const IterT& iterator() const { return mIter; }
1316 
1317  bool empty() const { return mSize == 0 || !mIter.test(); }
1318  bool test() const { return !this->empty(); }
1319  operator bool() const { return !this->empty(); }
1320 
1323  bool is_divisible() const { return mSize > mGrainSize; }
1324 
1326  void increment(Index n = 1) { for ( ; n > 0 && mSize > 0; --n, --mSize, ++mIter) {} }
1328  IteratorRange& operator++() { this->increment(); return *this; }
1331  bool next() { this->increment(); return this->test(); }
1332 
1333 private:
1334  Index size() const { Index n = 0; for (IterT it(mIter); it.test(); ++n, ++it) {} return n; }
1335 
1336  IterT mIter;
1337  size_t mGrainSize;
1342  Index mSize;
1343 };
1344 
1345 
1347 
1348 
1351 
1352 } // namespace tree
1353 } // namespace OPENVDB_VERSION_NAME
1354 } // namespace openvdb
1355 
1356 #endif // OPENVDB_TREE_TREEITERATOR_HAS_BEEN_INCLUDED
1357 
1358 // Copyright (c) 2012-2013 DreamWorks Animation LLC
1359 // All rights reserved. This software is distributed under the
1360 // Mozilla Public License 2.0 ( http://www.mozilla.org/MPL/2.0/ )