OpenVDB  4.0.1
RootNode.h
Go to the documentation of this file.
1 //
3 // Copyright (c) 2012-2017 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 //
34 
35 #ifndef OPENVDB_TREE_ROOTNODE_HAS_BEEN_INCLUDED
36 #define OPENVDB_TREE_ROOTNODE_HAS_BEEN_INCLUDED
37 
38 #include <map>
39 #include <set>
40 #include <sstream>
41 #include <deque>
42 #include <boost/type_traits/remove_const.hpp>
43 #include <boost/type_traits/remove_pointer.hpp>
44 #include <boost/type_traits/is_pointer.hpp>
45 #include <boost/type_traits/is_const.hpp>
46 #include <boost/mpl/contains.hpp>
47 #include <boost/mpl/if.hpp>
48 #include <boost/mpl/vector.hpp>//for boost::mpl::vector
49 #include <boost/mpl/at.hpp>
50 #include <boost/mpl/push_back.hpp>
51 #include <boost/mpl/size.hpp>
52 #include <tbb/parallel_for.h>
53 #include <openvdb/Exceptions.h>
54 #include <openvdb/Types.h>
55 #include <openvdb/io/Compression.h> // for truncateRealToHalf()
56 #include <openvdb/math/Math.h> // for isZero(), isExactlyEqual(), etc.
57 #include <openvdb/math/BBox.h>
58 #include <openvdb/util/NodeMasks.h> // for backward compatibility only (see readTopology())
59 #include <openvdb/version.h>
60 
61 
62 namespace openvdb {
64 namespace OPENVDB_VERSION_NAME {
65 namespace tree {
66 
67 // Forward declarations
68 template<typename HeadType, int HeadLevel> struct NodeChain;
69 template<typename, typename> struct SameRootConfig;
70 template<typename, typename, bool> struct RootNodeCopyHelper;
71 template<typename, typename, typename, bool> struct RootNodeCombineHelper;
72 
73 
74 template<typename ChildType>
75 class RootNode
76 {
77 public:
78  typedef ChildType ChildNodeType;
79  typedef typename ChildType::LeafNodeType LeafNodeType;
80  typedef typename ChildType::ValueType ValueType;
81  typedef typename ChildType::BuildType BuildType;
82 
83  static const Index LEVEL = 1 + ChildType::LEVEL; // level 0 = leaf
84 
87  BOOST_STATIC_ASSERT(boost::mpl::size<NodeChainType>::value == LEVEL + 1);
88 
91  template<typename OtherValueType>
92  struct ValueConverter {
94  };
95 
99  template<typename OtherNodeType>
102  };
103 
104 
106  RootNode();
107 
109  explicit RootNode(const ValueType& background);
110 
111  RootNode(const RootNode& other) { *this = other; }
112 
119  template<typename OtherChildType>
120  explicit RootNode(const RootNode<OtherChildType>& other) { *this = other; }
121 
130  template<typename OtherChildType>
131  RootNode(const RootNode<OtherChildType>& other,
132  const ValueType& background, const ValueType& foreground, TopologyCopy);
133 
144  template<typename OtherChildType>
145  RootNode(const RootNode<OtherChildType>& other, const ValueType& background, TopologyCopy);
146 
148  RootNode& operator=(const RootNode& other);
156  template<typename OtherChildType>
157  RootNode& operator=(const RootNode<OtherChildType>& other);
158 
159  ~RootNode() { this->clear(); }
160 
161 private:
162  struct Tile {
163  Tile(): value(zeroVal<ValueType>()), active(false) {}
164  Tile(const ValueType& v, bool b): value(v), active(b) {}
165  ValueType value;
166  bool active;
167  };
168 
169  // This lightweight struct pairs child pointers and tiles.
170  struct NodeStruct {
171  ChildType* child;
172  Tile tile;
173 
174  NodeStruct(): child(nullptr) {}
175  NodeStruct(ChildType& c): child(&c) {}
176  NodeStruct(const Tile& t): child(nullptr), tile(t) {}
177  NodeStruct(const NodeStruct&) = default;
178  NodeStruct& operator=(const NodeStruct&) = default;
179  ~NodeStruct() {}
180 
181  bool isChild() const { return child != nullptr; }
182  bool isTile() const { return child == nullptr; }
183  bool isTileOff() const { return isTile() && !tile.active; }
184  bool isTileOn() const { return isTile() && tile.active; }
185 
186  void set(ChildType& c) { delete child; child = &c; }
187  void set(const Tile& t) { delete child; child = nullptr; tile = t; }
188  ChildType& steal(const Tile& t) { ChildType* c=child; child=nullptr; tile=t; return *c; }
189  };
190 
191  typedef std::map<Coord, NodeStruct> MapType;
192  typedef typename MapType::iterator MapIter;
193  typedef typename MapType::const_iterator MapCIter;
194 
195  typedef std::set<Coord> CoordSet;
196  typedef typename CoordSet::iterator CoordSetIter;
197  typedef typename CoordSet::const_iterator CoordSetCIter;
198 
199  static void setTile(const MapIter& i, const Tile& t) { i->second.set(t); }
200  static void setChild(const MapIter& i, ChildType& c) { i->second.set(c); }
201  static Tile& getTile(const MapIter& i) { return i->second.tile; }
202  static const Tile& getTile(const MapCIter& i) { return i->second.tile; }
203  static ChildType& getChild(const MapIter& i) { return *(i->second.child); }
204  static const ChildType& getChild(const MapCIter& i) { return *(i->second.child); }
205  static ChildType& stealChild(const MapIter& i, const Tile& t) {return i->second.steal(t);}
206  static const ChildType& stealChild(const MapCIter& i,const Tile& t) {return i->second.steal(t);}
207 
208  static bool isChild(const MapCIter& i) { return i->second.isChild(); }
209  static bool isChild(const MapIter& i) { return i->second.isChild(); }
210  static bool isTile(const MapCIter& i) { return i->second.isTile(); }
211  static bool isTile(const MapIter& i) { return i->second.isTile(); }
212  static bool isTileOff(const MapCIter& i) { return i->second.isTileOff(); }
213  static bool isTileOff(const MapIter& i) { return i->second.isTileOff(); }
214  static bool isTileOn(const MapCIter& i) { return i->second.isTileOn(); }
215  static bool isTileOn(const MapIter& i) { return i->second.isTileOn(); }
216 
217  struct NullPred {
218  static inline bool test(const MapIter&) { return true; }
219  static inline bool test(const MapCIter&) { return true; }
220  };
221  struct ValueOnPred {
222  static inline bool test(const MapIter& i) { return isTileOn(i); }
223  static inline bool test(const MapCIter& i) { return isTileOn(i); }
224  };
225  struct ValueOffPred {
226  static inline bool test(const MapIter& i) { return isTileOff(i); }
227  static inline bool test(const MapCIter& i) { return isTileOff(i); }
228  };
229  struct ValueAllPred {
230  static inline bool test(const MapIter& i) { return isTile(i); }
231  static inline bool test(const MapCIter& i) { return isTile(i); }
232  };
233  struct ChildOnPred {
234  static inline bool test(const MapIter& i) { return isChild(i); }
235  static inline bool test(const MapCIter& i) { return isChild(i); }
236  };
237  struct ChildOffPred {
238  static inline bool test(const MapIter& i) { return isTile(i); }
239  static inline bool test(const MapCIter& i) { return isTile(i); }
240  };
241 
242  template<typename _RootNodeT, typename _MapIterT, typename FilterPredT>
243  class BaseIter
244  {
245  public:
246  typedef _RootNodeT RootNodeT;
247  typedef _MapIterT MapIterT; // either MapIter or MapCIter
248 
249  bool operator==(const BaseIter& other) const
250  {
251  return (mParentNode == other.mParentNode) && (mIter == other.mIter);
252  }
253  bool operator!=(const BaseIter& other) const { return !(*this == other); }
254 
255  RootNodeT* getParentNode() const { return mParentNode; }
257  RootNodeT& parent() const
258  {
259  if (!mParentNode) OPENVDB_THROW(ValueError, "iterator references a null parent node");
260  return *mParentNode;
261  }
262 
263  bool test() const { assert(mParentNode); return mIter != mParentNode->mTable.end(); }
264  operator bool() const { return this->test(); }
265 
266  void increment() { ++mIter; this->skip(); }
267  bool next() { this->increment(); return this->test(); }
268  void increment(Index n) { for (int i = 0; i < n && this->next(); ++i) {} }
269 
272  Index pos() const
273  {
274  return !mParentNode ? 0U : Index(std::distance(mParentNode->mTable.begin(), mIter));
275  }
276 
277  bool isValueOn() const { return RootNodeT::isTileOn(mIter); }
278  bool isValueOff() const { return RootNodeT::isTileOff(mIter); }
279  void setValueOn(bool on = true) const { mIter->second.tile.active = on; }
280  void setValueOff() const { mIter->second.tile.active = false; }
281 
283  Coord getCoord() const { return mIter->first; }
285  void getCoord(Coord& xyz) const { xyz = this->getCoord(); }
286 
287  protected:
288  BaseIter(): mParentNode(nullptr) {}
289  BaseIter(RootNodeT& parent, const MapIterT& iter): mParentNode(&parent), mIter(iter) {}
290 
291  void skip() { while (this->test() && !FilterPredT::test(mIter)) ++mIter; }
292 
293  RootNodeT* mParentNode;
294  MapIterT mIter;
295  }; // BaseIter
296 
297  template<typename RootNodeT, typename MapIterT, typename FilterPredT, typename ChildNodeT>
298  class ChildIter: public BaseIter<RootNodeT, MapIterT, FilterPredT>
299  {
300  public:
301  typedef BaseIter<RootNodeT, MapIterT, FilterPredT> BaseT;
302  typedef RootNodeT NodeType;
303  typedef NodeType ValueType;
304  typedef ChildNodeT ChildNodeType;
305  typedef typename boost::remove_const<NodeType>::type NonConstNodeType;
306  typedef typename boost::remove_const<ValueType>::type NonConstValueType;
307  typedef typename boost::remove_const<ChildNodeType>::type NonConstChildNodeType;
308  using BaseT::mIter;
309 
310  ChildIter() {}
311  ChildIter(RootNodeT& parent, const MapIterT& iter): BaseT(parent, iter) { BaseT::skip(); }
312 
313  ChildIter& operator++() { BaseT::increment(); return *this; }
314 
315  ChildNodeT& getValue() const { return getChild(mIter); }
316  ChildNodeT& operator*() const { return this->getValue(); }
317  ChildNodeT* operator->() const { return &this->getValue(); }
318  }; // ChildIter
319 
320  template<typename RootNodeT, typename MapIterT, typename FilterPredT, typename ValueT>
321  class ValueIter: public BaseIter<RootNodeT, MapIterT, FilterPredT>
322  {
323  public:
324  typedef BaseIter<RootNodeT, MapIterT, FilterPredT> BaseT;
325  typedef RootNodeT NodeType;
326  typedef ValueT ValueType;
327  typedef typename boost::remove_const<NodeType>::type NonConstNodeType;
328  typedef typename boost::remove_const<ValueT>::type NonConstValueType;
329  using BaseT::mIter;
330 
331  ValueIter() {}
332  ValueIter(RootNodeT& parent, const MapIterT& iter): BaseT(parent, iter) { BaseT::skip(); }
333 
334  ValueIter& operator++() { BaseT::increment(); return *this; }
335 
336  ValueT& getValue() const { return getTile(mIter).value; }
337  ValueT& operator*() const { return this->getValue(); }
338  ValueT* operator->() const { return &(this->getValue()); }
339 
340  void setValue(const ValueT& v) const { assert(isTile(mIter)); getTile(mIter).value = v; }
341 
342  template<typename ModifyOp>
343  void modifyValue(const ModifyOp& op) const
344  {
345  assert(isTile(mIter));
346  op(getTile(mIter).value);
347  }
348  }; // ValueIter
349 
350  template<typename RootNodeT, typename MapIterT, typename ChildNodeT, typename ValueT>
351  class DenseIter: public BaseIter<RootNodeT, MapIterT, NullPred>
352  {
353  public:
354  typedef BaseIter<RootNodeT, MapIterT, NullPred> BaseT;
355  typedef RootNodeT NodeType;
356  typedef ValueT ValueType;
357  typedef ChildNodeT ChildNodeType;
358  typedef typename boost::remove_const<NodeType>::type NonConstNodeType;
359  typedef typename boost::remove_const<ValueT>::type NonConstValueType;
360  typedef typename boost::remove_const<ChildNodeT>::type NonConstChildNodeType;
361  using BaseT::mIter;
362 
363  DenseIter() {}
364  DenseIter(RootNodeT& parent, const MapIterT& iter): BaseT(parent, iter) {}
365 
366  DenseIter& operator++() { BaseT::increment(); return *this; }
367 
368  bool isChildNode() const { return isChild(mIter); }
369 
370  ChildNodeT* probeChild(NonConstValueType& value) const
371  {
372  if (isChild(mIter)) return &getChild(mIter);
373  value = getTile(mIter).value;
374  return nullptr;
375  }
376  bool probeChild(ChildNodeT*& child, NonConstValueType& value) const
377  {
378  child = this->probeChild(value);
379  return child != nullptr;
380  }
381  bool probeValue(NonConstValueType& value) const { return !this->probeChild(value); }
382 
383  void setChild(ChildNodeT& c) const { RootNodeT::setChild(mIter, c); }
384  void setChild(ChildNodeT* c) const { assert(c != nullptr); RootNodeT::setChild(mIter, *c); }
385  void setValue(const ValueT& v) const
386  {
387  if (isTile(mIter)) getTile(mIter).value = v;
391  else stealChild(mIter, Tile(v, /*active=*/true));
392  }
393  }; // DenseIter
394 
395 public:
396  typedef ChildIter<RootNode, MapIter, ChildOnPred, ChildType> ChildOnIter;
397  typedef ChildIter<const RootNode, MapCIter, ChildOnPred, const ChildType> ChildOnCIter;
398  typedef ValueIter<RootNode, MapIter, ChildOffPred, const ValueType> ChildOffIter;
399  typedef ValueIter<const RootNode, MapCIter, ChildOffPred, ValueType> ChildOffCIter;
400  typedef DenseIter<RootNode, MapIter, ChildType, ValueType> ChildAllIter;
401  typedef DenseIter<const RootNode, MapCIter, const ChildType, const ValueType> ChildAllCIter;
402 
403  typedef ValueIter<RootNode, MapIter, ValueOnPred, ValueType> ValueOnIter;
404  typedef ValueIter<const RootNode, MapCIter, ValueOnPred, const ValueType> ValueOnCIter;
405  typedef ValueIter<RootNode, MapIter, ValueOffPred, ValueType> ValueOffIter;
406  typedef ValueIter<const RootNode, MapCIter, ValueOffPred, const ValueType> ValueOffCIter;
407  typedef ValueIter<RootNode, MapIter, ValueAllPred, ValueType> ValueAllIter;
408  typedef ValueIter<const RootNode, MapCIter, ValueAllPred, const ValueType> ValueAllCIter;
409 
410 
411  ChildOnCIter cbeginChildOn() const { return ChildOnCIter(*this, mTable.begin()); }
412  ChildOffCIter cbeginChildOff() const { return ChildOffCIter(*this, mTable.begin()); }
413  ChildAllCIter cbeginChildAll() const { return ChildAllCIter(*this, mTable.begin()); }
414  ChildOnCIter beginChildOn() const { return cbeginChildOn(); }
415  ChildOffCIter beginChildOff() const { return cbeginChildOff(); }
416  ChildAllCIter beginChildAll() const { return cbeginChildAll(); }
417  ChildOnIter beginChildOn() { return ChildOnIter(*this, mTable.begin()); }
418  ChildOffIter beginChildOff() { return ChildOffIter(*this, mTable.begin()); }
419  ChildAllIter beginChildAll() { return ChildAllIter(*this, mTable.begin()); }
420 
421  ValueOnCIter cbeginValueOn() const { return ValueOnCIter(*this, mTable.begin()); }
422  ValueOffCIter cbeginValueOff() const { return ValueOffCIter(*this, mTable.begin()); }
423  ValueAllCIter cbeginValueAll() const { return ValueAllCIter(*this, mTable.begin()); }
424  ValueOnCIter beginValueOn() const { return cbeginValueOn(); }
425  ValueOffCIter beginValueOff() const { return cbeginValueOff(); }
426  ValueAllCIter beginValueAll() const { return cbeginValueAll(); }
427  ValueOnIter beginValueOn() { return ValueOnIter(*this, mTable.begin()); }
428  ValueOffIter beginValueOff() { return ValueOffIter(*this, mTable.begin()); }
429  ValueAllIter beginValueAll() { return ValueAllIter(*this, mTable.begin()); }
430 
432  Index64 memUsage() const;
433 
439  void evalActiveBoundingBox(CoordBBox& bbox, bool visitVoxels = true) const;
440 
442  static CoordBBox getNodeBoundingBox() { return CoordBBox::inf(); }
443 
456  void setBackground(const ValueType& value, bool updateChildNodes);
457 
459  const ValueType& background() const { return mBackground; }
460 
462  bool isBackgroundTile(const Tile&) const;
464  bool isBackgroundTile(const MapIter&) const;
466  bool isBackgroundTile(const MapCIter&) const;
468 
470  size_t numBackgroundTiles() const;
473  size_t eraseBackgroundTiles();
474  inline void clear();
475 
477  bool empty() const { return mTable.size() == numBackgroundTiles(); }
478 
482  bool expand(const Coord& xyz);
483 
484  static Index getLevel() { return LEVEL; }
485  static void getNodeLog2Dims(std::vector<Index>& dims);
486  static Index getChildDim() { return ChildType::DIM; }
487 
489  Index getTableSize() const { return static_cast<Index>(mTable.size()); }
490 
491  Index getWidth() const { return this->getMaxIndex()[0] - this->getMinIndex()[0]; }
492  Index getHeight() const { return this->getMaxIndex()[1] - this->getMinIndex()[1]; }
493  Index getDepth() const { return this->getMaxIndex()[2] - this->getMinIndex()[2]; }
494 
496  Coord getMinIndex() const;
498  Coord getMaxIndex() const;
500  void getIndexRange(CoordBBox& bbox) const;
501 
504  template<typename OtherChildType>
505  bool hasSameTopology(const RootNode<OtherChildType>& other) const;
506 
508  template<typename OtherChildType>
509  static bool hasSameConfiguration(const RootNode<OtherChildType>& other);
510 
513  template<typename OtherChildType>
514  static bool hasCompatibleValueType(const RootNode<OtherChildType>& other);
515 
516  Index32 leafCount() const;
517  Index32 nonLeafCount() const;
518  Index64 onVoxelCount() const;
519  Index64 offVoxelCount() const;
520  Index64 onLeafVoxelCount() const;
521  Index64 offLeafVoxelCount() const;
522  Index64 onTileCount() const;
523 
524  bool isValueOn(const Coord& xyz) const;
525 
527  bool hasActiveTiles() const;
528 
529  const ValueType& getValue(const Coord& xyz) const;
530  bool probeValue(const Coord& xyz, ValueType& value) const;
531 
535  int getValueDepth(const Coord& xyz) const;
536 
538  void setActiveState(const Coord& xyz, bool on);
540  void setValueOnly(const Coord& xyz, const ValueType& value);
542  void setValueOn(const Coord& xyz, const ValueType& value);
544  void setValueOff(const Coord& xyz);
546  void setValueOff(const Coord& xyz, const ValueType& value);
547 
550  template<typename ModifyOp>
551  void modifyValue(const Coord& xyz, const ModifyOp& op);
553  template<typename ModifyOp>
554  void modifyValueAndActiveState(const Coord& xyz, const ModifyOp& op);
555 
557  void fill(const CoordBBox& bbox, const ValueType& value, bool active = true);
566  void sparseFill(const CoordBBox& bbox, const ValueType& value, bool active = true)
567  {
568  this->fill(bbox, value, active);
569  }
571 
579  void denseFill(const CoordBBox& bbox, const ValueType& value, bool active = true);
580 
589  void voxelizeActiveTiles(bool threaded = true);
590 
596  template<typename DenseT>
597  void copyToDense(const CoordBBox& bbox, DenseT& dense) const;
598 
599 
600  //
601  // I/O
602  //
603  bool writeTopology(std::ostream&, bool toHalf = false) const;
604  bool readTopology(std::istream&, bool fromHalf = false);
605 
606  void writeBuffers(std::ostream&, bool toHalf = false) const;
607  void readBuffers(std::istream&, bool fromHalf = false);
608  void readBuffers(std::istream&, const CoordBBox&, bool fromHalf = false);
609 
610 
611  //
612  // Voxel access
613  //
618  template<typename AccessorT>
619  const ValueType& getValueAndCache(const Coord& xyz, AccessorT&) const;
624  template<typename AccessorT>
625  bool isValueOnAndCache(const Coord& xyz, AccessorT&) const;
626 
631  template<typename AccessorT>
632  void setValueAndCache(const Coord& xyz, const ValueType& value, AccessorT&);
633 
638  template<typename AccessorT>
639  void setValueOnlyAndCache(const Coord& xyz, const ValueType& value, AccessorT&);
640 
646  template<typename ModifyOp, typename AccessorT>
647  void modifyValueAndCache(const Coord& xyz, const ModifyOp& op, AccessorT&);
648 
653  template<typename ModifyOp, typename AccessorT>
654  void modifyValueAndActiveStateAndCache(const Coord& xyz, const ModifyOp& op, AccessorT&);
655 
660  template<typename AccessorT>
661  void setValueOffAndCache(const Coord& xyz, const ValueType& value, AccessorT&);
662 
667  template<typename AccessorT>
668  void setActiveStateAndCache(const Coord& xyz, bool on, AccessorT&);
669 
675  template<typename AccessorT>
676  bool probeValueAndCache(const Coord& xyz, ValueType& value, AccessorT&) const;
677 
683  template<typename AccessorT>
684  int getValueDepthAndCache(const Coord& xyz, AccessorT&) const;
685 
687  void clip(const CoordBBox&);
688 
694  void prune(const ValueType& tolerance = zeroVal<ValueType>());
695 
698  void addLeaf(LeafNodeType* leaf);
699 
702  template<typename AccessorT>
703  void addLeafAndCache(LeafNodeType* leaf, AccessorT&);
704 
713  template<typename NodeT>
714  NodeT* stealNode(const Coord& xyz, const ValueType& value, bool state);
715 
718  void addTile(const Coord& xyz, const ValueType& value, bool state);
719 
723  void addTile(Index level, const Coord& xyz, const ValueType& value, bool state);
724 
727  template<typename AccessorT>
728  void addTileAndCache(Index level, const Coord& xyz, const ValueType&, bool state, AccessorT&);
729 
735  LeafNodeType* touchLeaf(const Coord& xyz);
736 
739  template<typename AccessorT>
740  LeafNodeType* touchLeafAndCache(const Coord& xyz, AccessorT& acc);
741 
743  template <typename NodeT>
746  NodeT* probeNode(const Coord& xyz);
747  template <typename NodeT>
748  const NodeT* probeConstNode(const Coord& xyz) const;
750 
752  template<typename NodeT, typename AccessorT>
755  NodeT* probeNodeAndCache(const Coord& xyz, AccessorT& acc);
756  template<typename NodeT, typename AccessorT>
757  const NodeT* probeConstNodeAndCache(const Coord& xyz, AccessorT& acc) const;
759 
761  LeafNodeType* probeLeaf(const Coord& xyz);
764  const LeafNodeType* probeConstLeaf(const Coord& xyz) const;
765  const LeafNodeType* probeLeaf(const Coord& xyz) const;
767 
769  template<typename AccessorT>
772  LeafNodeType* probeLeafAndCache(const Coord& xyz, AccessorT& acc);
773  template<typename AccessorT>
774  const LeafNodeType* probeConstLeafAndCache(const Coord& xyz, AccessorT& acc) const;
775  template<typename AccessorT>
776  const LeafNodeType* probeLeafAndCache(const Coord& xyz, AccessorT& acc) const;
778 
779 
780  //
781  // Aux methods
782  //
783 
785  template<typename ArrayT> void getNodes(ArrayT& array);
808  template<typename ArrayT> void getNodes(ArrayT& array) const;
810 
812  template<typename ArrayT>
836  void stealNodes(ArrayT& array, const ValueType& value, bool state);
837  template<typename ArrayT>
838  void stealNodes(ArrayT& array) { this->stealNodes(array, mBackground, false); }
840 
848  template<MergePolicy Policy> void merge(RootNode& other);
849 
863  template<typename OtherChildType>
864  void topologyUnion(const RootNode<OtherChildType>& other);
865 
879  template<typename OtherChildType>
880  void topologyIntersection(const RootNode<OtherChildType>& other);
881 
892  template<typename OtherChildType>
893  void topologyDifference(const RootNode<OtherChildType>& other);
894 
895  template<typename CombineOp>
896  void combine(RootNode& other, CombineOp&, bool prune = false);
897 
898  template<typename CombineOp, typename OtherRootNode /*= RootNode*/>
899  void combine2(const RootNode& other0, const OtherRootNode& other1,
900  CombineOp& op, bool prune = false);
901 
907  template<typename BBoxOp> void visitActiveBBox(BBoxOp&) const;
908 
909  template<typename VisitorOp> void visit(VisitorOp&);
910  template<typename VisitorOp> void visit(VisitorOp&) const;
911 
912  template<typename OtherRootNodeType, typename VisitorOp>
913  void visit2(OtherRootNodeType& other, VisitorOp&);
914  template<typename OtherRootNodeType, typename VisitorOp>
915  void visit2(OtherRootNodeType& other, VisitorOp&) const;
916 
917 private:
920  template<typename> friend class RootNode;
921 
922  template<typename, typename, bool> friend struct RootNodeCopyHelper;
923  template<typename, typename, typename, bool> friend struct RootNodeCombineHelper;
924 
926  void initTable() {}
928  void resetTable(MapType& table) { mTable.swap(table); table.clear(); }
930  void resetTable(const MapType&) const {}
932 
933  Index getChildCount() const;
934  Index getTileCount() const;
935  Index getActiveTileCount() const;
936  Index getInactiveTileCount() const;
937 
939  static Coord coordToKey(const Coord& xyz) { return xyz & ~(ChildType::DIM - 1); }
940 
942  void insertKeys(CoordSet&) const;
943 
945  bool hasKey(const Coord& key) const { return mTable.find(key) != mTable.end(); }
947  MapIter findKey(const Coord& key) { return mTable.find(key); }
950  MapCIter findKey(const Coord& key) const { return mTable.find(key); }
952 
953  MapIter findCoord(const Coord& xyz) { return mTable.find(coordToKey(xyz)); }
956  MapCIter findCoord(const Coord& xyz) const { return mTable.find(coordToKey(xyz)); }
958  MapIter findOrAddCoord(const Coord& xyz);
962 
967  template<typename OtherChildType>
968  static void enforceSameConfiguration(const RootNode<OtherChildType>& other);
969 
975  template<typename OtherChildType>
976  static void enforceCompatibleValueTypes(const RootNode<OtherChildType>& other);
977 
978  template<typename CombineOp, typename OtherRootNode /*= RootNode*/>
979  void doCombine2(const RootNode&, const OtherRootNode&, CombineOp&, bool prune);
980 
981  template<typename RootNodeT, typename VisitorOp, typename ChildAllIterT>
982  static inline void doVisit(RootNodeT&, VisitorOp&);
983 
984  template<typename RootNodeT, typename OtherRootNodeT, typename VisitorOp,
985  typename ChildAllIterT, typename OtherChildAllIterT>
986  static inline void doVisit2(RootNodeT&, OtherRootNodeT&, VisitorOp&);
987 
988 
989  MapType mTable;
990  ValueType mBackground;
991 }; // end of RootNode class
992 
993 
995 
996 
1017 template<typename HeadT, int HeadLevel>
1018 struct NodeChain {
1019  typedef typename NodeChain<typename HeadT::ChildNodeType, HeadLevel-1>::Type SubtreeT;
1020  typedef typename boost::mpl::push_back<SubtreeT, HeadT>::type Type;
1021 };
1022 
1024 template<typename HeadT>
1025 struct NodeChain<HeadT, /*HeadLevel=*/1> {
1026  typedef typename boost::mpl::vector<typename HeadT::ChildNodeType, HeadT>::type Type;
1027 };
1028 
1029 
1031 
1032 
1034 template<typename ChildT1, typename NodeT2>
1037 struct SameRootConfig {
1038  static const bool value = false;
1039 };
1040 
1041 template<typename ChildT1, typename ChildT2>
1042 struct SameRootConfig<ChildT1, RootNode<ChildT2> > {
1043  static const bool value = ChildT1::template SameConfiguration<ChildT2>::value;
1044 };
1046 
1047 
1049 
1050 
1051 template<typename ChildT>
1052 inline
1053 RootNode<ChildT>::RootNode(): mBackground(zeroVal<ValueType>())
1054 {
1055  this->initTable();
1056 }
1057 
1058 
1059 template<typename ChildT>
1060 inline
1061 RootNode<ChildT>::RootNode(const ValueType& background): mBackground(background)
1062 {
1063  this->initTable();
1064 }
1065 
1066 
1067 template<typename ChildT>
1068 template<typename OtherChildType>
1069 inline
1071  const ValueType& backgd, const ValueType& foregd, TopologyCopy):
1072  mBackground(backgd)
1073 {
1074  typedef RootNode<OtherChildType> OtherRootT;
1075 
1076  enforceSameConfiguration(other);
1077 
1078  const Tile bgTile(backgd, /*active=*/false), fgTile(foregd, true);
1079  this->initTable();
1080 
1081  for (typename OtherRootT::MapCIter i=other.mTable.begin(), e=other.mTable.end(); i != e; ++i) {
1082  mTable[i->first] = OtherRootT::isTile(i)
1083  ? NodeStruct(OtherRootT::isTileOn(i) ? fgTile : bgTile)
1084  : NodeStruct(*(new ChildT(OtherRootT::getChild(i), backgd, foregd, TopologyCopy())));
1085  }
1086 }
1087 
1088 
1089 template<typename ChildT>
1090 template<typename OtherChildType>
1091 inline
1093  const ValueType& backgd, TopologyCopy):
1094  mBackground(backgd)
1095 {
1096  typedef RootNode<OtherChildType> OtherRootT;
1097 
1098  enforceSameConfiguration(other);
1099 
1100  const Tile bgTile(backgd, /*active=*/false), fgTile(backgd, true);
1101  this->initTable();
1102  for (typename OtherRootT::MapCIter i=other.mTable.begin(), e=other.mTable.end(); i != e; ++i) {
1103  mTable[i->first] = OtherRootT::isTile(i)
1104  ? NodeStruct(OtherRootT::isTileOn(i) ? fgTile : bgTile)
1105  : NodeStruct(*(new ChildT(OtherRootT::getChild(i), backgd, TopologyCopy())));
1106  }
1107 }
1108 
1109 
1111 
1112 
1113 // This helper class is a friend of RootNode and is needed so that assignment
1114 // with value conversion can be specialized for compatible and incompatible
1115 // pairs of RootNode types.
1116 template<typename RootT, typename OtherRootT, bool Compatible = false>
1117 struct RootNodeCopyHelper
1118 {
1119  static inline void copyWithValueConversion(RootT& self, const OtherRootT& other)
1120  {
1121  // If the two root nodes have different configurations or incompatible ValueTypes,
1122  // throw an exception.
1123  self.enforceSameConfiguration(other);
1124  self.enforceCompatibleValueTypes(other);
1125  // One of the above two tests should throw, so we should never get here:
1126  std::ostringstream ostr;
1127  ostr << "cannot convert a " << typeid(OtherRootT).name()
1128  << " to a " << typeid(RootT).name();
1129  OPENVDB_THROW(TypeError, ostr.str());
1130  }
1131 };
1132 
1133 // Specialization for root nodes of compatible types
1134 template<typename RootT, typename OtherRootT>
1135 struct RootNodeCopyHelper<RootT, OtherRootT, /*Compatible=*/true>
1136 {
1137  static inline void copyWithValueConversion(RootT& self, const OtherRootT& other)
1138  {
1139  typedef typename RootT::ValueType ValueT;
1140  typedef typename RootT::ChildNodeType ChildT;
1141  typedef typename RootT::NodeStruct NodeStruct;
1142  typedef typename RootT::Tile Tile;
1143  typedef typename OtherRootT::ValueType OtherValueT;
1144  typedef typename OtherRootT::MapCIter OtherMapCIter;
1145  typedef typename OtherRootT::Tile OtherTile;
1146 
1147  struct Local {
1149  static inline ValueT convertValue(const OtherValueT& val) { return ValueT(val); }
1150  };
1151 
1152  self.mBackground = Local::convertValue(other.mBackground);
1153 
1154  self.clear();
1155  self.initTable();
1156 
1157  for (OtherMapCIter i = other.mTable.begin(), e = other.mTable.end(); i != e; ++i) {
1158  if (other.isTile(i)) {
1159  // Copy the other node's tile, but convert its value to this node's ValueType.
1160  const OtherTile& otherTile = other.getTile(i);
1161  self.mTable[i->first] = NodeStruct(
1162  Tile(Local::convertValue(otherTile.value), otherTile.active));
1163  } else {
1164  // Copy the other node's child, but convert its values to this node's ValueType.
1165  self.mTable[i->first] = NodeStruct(*(new ChildT(other.getChild(i))));
1166  }
1167  }
1168  }
1169 };
1170 
1171 
1172 // Overload for root nodes of the same type as this node
1173 template<typename ChildT>
1174 inline RootNode<ChildT>&
1176 {
1177  if (&other != this) {
1178  mBackground = other.mBackground;
1179 
1180  this->clear();
1181  this->initTable();
1182 
1183  for (MapCIter i = other.mTable.begin(), e = other.mTable.end(); i != e; ++i) {
1184  mTable[i->first] =
1185  isTile(i) ? NodeStruct(getTile(i)) : NodeStruct(*(new ChildT(getChild(i))));
1186  }
1187  }
1188  return *this;
1189 }
1190 
1191 // Overload for root nodes of different types
1192 template<typename ChildT>
1193 template<typename OtherChildType>
1194 inline RootNode<ChildT>&
1196 {
1197  typedef RootNode<OtherChildType> OtherRootT;
1198  typedef typename OtherRootT::ValueType OtherValueT;
1199  static const bool compatible = (SameConfiguration<OtherRootT>::value
1200  && CanConvertType</*from=*/OtherValueT, /*to=*/ValueType>::value);
1202  return *this;
1203 }
1204 
1205 
1207 
1208 template<typename ChildT>
1209 inline void
1210 RootNode<ChildT>::setBackground(const ValueType& background, bool updateChildNodes)
1211 {
1212  if (math::isExactlyEqual(background, mBackground)) return;
1213 
1214  if (updateChildNodes) {
1215  // Traverse the tree, replacing occurrences of mBackground with background
1216  // and -mBackground with -background.
1217  for (MapIter iter=mTable.begin(); iter!=mTable.end(); ++iter) {
1218  ChildT *child = iter->second.child;
1219  if (child) {
1220  child->resetBackground(/*old=*/mBackground, /*new=*/background);
1221  } else {
1222  Tile& tile = getTile(iter);
1223  if (tile.active) continue;//only change inactive tiles
1224  if (math::isApproxEqual(tile.value, mBackground)) {
1225  tile.value = background;
1226  } else if (math::isApproxEqual(tile.value, math::negative(mBackground))) {
1227  tile.value = math::negative(background);
1228  }
1229  }
1230  }
1231  }
1232  mBackground = background;
1233 }
1234 
1235 template<typename ChildT>
1236 inline bool
1237 RootNode<ChildT>::isBackgroundTile(const Tile& tile) const
1238 {
1239  return !tile.active && math::isApproxEqual(tile.value, mBackground);
1240 }
1241 
1242 template<typename ChildT>
1243 inline bool
1244 RootNode<ChildT>::isBackgroundTile(const MapIter& iter) const
1245 {
1246  return isTileOff(iter) && math::isApproxEqual(getTile(iter).value, mBackground);
1247 }
1248 
1249 template<typename ChildT>
1250 inline bool
1251 RootNode<ChildT>::isBackgroundTile(const MapCIter& iter) const
1252 {
1253  return isTileOff(iter) && math::isApproxEqual(getTile(iter).value, mBackground);
1254 }
1255 
1256 
1257 template<typename ChildT>
1258 inline size_t
1260 {
1261  size_t count = 0;
1262  for (MapCIter i = mTable.begin(), e = mTable.end(); i != e; ++i) {
1263  if (this->isBackgroundTile(i)) ++count;
1264  }
1265  return count;
1266 }
1267 
1268 
1269 template<typename ChildT>
1270 inline size_t
1272 {
1273  std::set<Coord> keysToErase;
1274  for (MapCIter i = mTable.begin(), e = mTable.end(); i != e; ++i) {
1275  if (this->isBackgroundTile(i)) keysToErase.insert(i->first);
1276  }
1277  for (std::set<Coord>::iterator i = keysToErase.begin(), e = keysToErase.end(); i != e; ++i) {
1278  mTable.erase(*i);
1279  }
1280  return keysToErase.size();
1281 }
1282 
1283 
1285 
1286 
1287 template<typename ChildT>
1288 inline void
1289 RootNode<ChildT>::insertKeys(CoordSet& keys) const
1290 {
1291  for (MapCIter i = mTable.begin(), e = mTable.end(); i != e; ++i) {
1292  keys.insert(i->first);
1293  }
1294 }
1295 
1296 
1297 template<typename ChildT>
1298 inline typename RootNode<ChildT>::MapIter
1299 RootNode<ChildT>::findOrAddCoord(const Coord& xyz)
1300 {
1301  const Coord key = coordToKey(xyz);
1302  std::pair<MapIter, bool> result = mTable.insert(
1303  typename MapType::value_type(key, NodeStruct(Tile(mBackground, /*active=*/false))));
1304  return result.first;
1305 }
1306 
1307 
1308 template<typename ChildT>
1309 inline bool
1310 RootNode<ChildT>::expand(const Coord& xyz)
1311 {
1312  const Coord key = coordToKey(xyz);
1313  std::pair<MapIter, bool> result = mTable.insert(
1314  typename MapType::value_type(key, NodeStruct(Tile(mBackground, /*active=*/false))));
1315  return result.second; // return true if the key did not already exist
1316 }
1317 
1318 
1320 
1321 
1322 template<typename ChildT>
1323 inline void
1324 RootNode<ChildT>::getNodeLog2Dims(std::vector<Index>& dims)
1325 {
1326  dims.push_back(0); // magic number; RootNode has no Log2Dim
1327  ChildT::getNodeLog2Dims(dims);
1328 }
1329 
1330 
1331 template<typename ChildT>
1332 inline Coord
1334 {
1335  return mTable.empty() ? Coord(0) : mTable.begin()->first;
1336 }
1337 
1338 template<typename ChildT>
1339 inline Coord
1341 {
1342  return mTable.empty() ? Coord(0) : mTable.rbegin()->first + Coord(ChildT::DIM - 1);
1343 }
1344 
1345 
1346 template<typename ChildT>
1347 inline void
1348 RootNode<ChildT>::getIndexRange(CoordBBox& bbox) const
1349 {
1350  bbox.min() = this->getMinIndex();
1351  bbox.max() = this->getMaxIndex();
1352 }
1353 
1354 
1356 
1357 
1358 template<typename ChildT>
1359 template<typename OtherChildType>
1360 inline bool
1362 {
1363  typedef RootNode<OtherChildType> OtherRootT;
1364  typedef typename OtherRootT::MapType OtherMapT;
1365  typedef typename OtherRootT::MapIter OtherIterT;
1366  typedef typename OtherRootT::MapCIter OtherCIterT;
1367 
1368  if (!hasSameConfiguration(other)) return false;
1369 
1370  // Create a local copy of the other node's table.
1371  OtherMapT copyOfOtherTable = other.mTable;
1372 
1373  // For each entry in this node's table...
1374  for (MapCIter thisIter = mTable.begin(); thisIter != mTable.end(); ++thisIter) {
1375  if (this->isBackgroundTile(thisIter)) continue; // ignore background tiles
1376 
1377  // Fail if there is no corresponding entry in the other node's table.
1378  OtherCIterT otherIter = other.findKey(thisIter->first);
1379  if (otherIter == other.mTable.end()) return false;
1380 
1381  // Fail if this entry is a tile and the other is a child or vice-versa.
1382  if (isChild(thisIter)) {//thisIter points to a child
1383  if (OtherRootT::isTile(otherIter)) return false;
1384  // Fail if both entries are children, but the children have different topology.
1385  if (!getChild(thisIter).hasSameTopology(&OtherRootT::getChild(otherIter))) return false;
1386  } else {//thisIter points to a tile
1387  if (OtherRootT::isChild(otherIter)) return false;
1388  if (getTile(thisIter).active != OtherRootT::getTile(otherIter).active) return false;
1389  }
1390 
1391  // Remove tiles and child nodes with matching topology from
1392  // the copy of the other node's table. This is required since
1393  // the two root tables can include an arbitrary number of
1394  // background tiles and still have the same topology!
1395  copyOfOtherTable.erase(otherIter->first);
1396  }
1397  // Fail if the remaining entries in copyOfOtherTable are not all background tiles.
1398  for (OtherIterT i = copyOfOtherTable.begin(), e = copyOfOtherTable.end(); i != e; ++i) {
1399  if (!other.isBackgroundTile(i)) return false;
1400  }
1401  return true;
1402 }
1403 
1404 
1405 template<typename ChildT>
1406 template<typename OtherChildType>
1407 inline bool
1409 {
1410  std::vector<Index> thisDims, otherDims;
1411  RootNode::getNodeLog2Dims(thisDims);
1413  return (thisDims == otherDims);
1414 }
1415 
1416 
1417 template<typename ChildT>
1418 template<typename OtherChildType>
1419 inline void
1421 {
1422  std::vector<Index> thisDims, otherDims;
1423  RootNode::getNodeLog2Dims(thisDims);
1425  if (thisDims != otherDims) {
1426  std::ostringstream ostr;
1427  ostr << "grids have incompatible configurations (" << thisDims[0];
1428  for (size_t i = 1, N = thisDims.size(); i < N; ++i) ostr << " x " << thisDims[i];
1429  ostr << " vs. " << otherDims[0];
1430  for (size_t i = 1, N = otherDims.size(); i < N; ++i) ostr << " x " << otherDims[i];
1431  ostr << ")";
1432  OPENVDB_THROW(TypeError, ostr.str());
1433  }
1434 }
1435 
1436 
1437 template<typename ChildT>
1438 template<typename OtherChildType>
1439 inline bool
1441 {
1442  typedef typename OtherChildType::ValueType OtherValueType;
1443  return CanConvertType</*from=*/OtherValueType, /*to=*/ValueType>::value;
1444 }
1445 
1446 
1447 template<typename ChildT>
1448 template<typename OtherChildType>
1449 inline void
1451 {
1452  typedef typename OtherChildType::ValueType OtherValueType;
1454  std::ostringstream ostr;
1455  ostr << "values of type " << typeNameAsString<OtherValueType>()
1456  << " cannot be converted to type " << typeNameAsString<ValueType>();
1457  OPENVDB_THROW(TypeError, ostr.str());
1458  }
1459 }
1460 
1461 
1463 
1464 
1465 template<typename ChildT>
1466 inline Index64
1468 {
1469  Index64 sum = sizeof(*this);
1470  for (MapCIter iter=mTable.begin(); iter!=mTable.end(); ++iter) {
1471  if (const ChildT *child = iter->second.child) {
1472  sum += child->memUsage();
1473  }
1474  }
1475  return sum;
1476 }
1477 
1478 
1479 template<typename ChildT>
1480 inline void
1482 {
1483  for (MapIter i = mTable.begin(), e = mTable.end(); i != e; ++i) {
1484  delete i->second.child;
1485  }
1486  mTable.clear();
1487 }
1488 
1489 
1490 template<typename ChildT>
1491 inline void
1492 RootNode<ChildT>::evalActiveBoundingBox(CoordBBox& bbox, bool visitVoxels) const
1493 {
1494  for (MapCIter iter=mTable.begin(); iter!=mTable.end(); ++iter) {
1495  if (const ChildT *child = iter->second.child) {
1496  child->evalActiveBoundingBox(bbox, visitVoxels);
1497  } else if (isTileOn(iter)) {
1498  bbox.expand(iter->first, ChildT::DIM);
1499  }
1500  }
1501 }
1502 
1503 
1504 template<typename ChildT>
1505 inline Index
1507  Index sum = 0;
1508  for (MapCIter i = mTable.begin(), e = mTable.end(); i != e; ++i) {
1509  if (isChild(i)) ++sum;
1510  }
1511  return sum;
1512 }
1513 
1514 
1515 template<typename ChildT>
1516 inline Index
1518 {
1519  Index sum = 0;
1520  for (MapCIter i = mTable.begin(), e = mTable.end(); i != e; ++i) {
1521  if (isTile(i)) ++sum;
1522  }
1523  return sum;
1524 }
1525 
1526 
1527 template<typename ChildT>
1528 inline Index
1530 {
1531  Index sum = 0;
1532  for (MapCIter i = mTable.begin(), e = mTable.end(); i != e; ++i) {
1533  if (isTileOn(i)) ++sum;
1534  }
1535  return sum;
1536 }
1537 
1538 
1539 template<typename ChildT>
1540 inline Index
1542 {
1543  Index sum = 0;
1544  for (MapCIter i = mTable.begin(), e = mTable.end(); i != e; ++i) {
1545  if (isTileOff(i)) ++sum;
1546  }
1547  return sum;
1548 }
1549 
1550 
1551 template<typename ChildT>
1552 inline Index32
1554 {
1555  Index32 sum = 0;
1556  for (MapCIter i = mTable.begin(), e = mTable.end(); i != e; ++i) {
1557  if (isChild(i)) sum += getChild(i).leafCount();
1558  }
1559  return sum;
1560 }
1561 
1562 
1563 template<typename ChildT>
1564 inline Index32
1566 {
1567  Index32 sum = 1;
1568  if (ChildT::LEVEL != 0) {
1569  for (MapCIter i = mTable.begin(), e = mTable.end(); i != e; ++i) {
1570  if (isChild(i)) sum += getChild(i).nonLeafCount();
1571  }
1572  }
1573  return sum;
1574 }
1575 
1576 
1577 template<typename ChildT>
1578 inline Index64
1580 {
1581  Index64 sum = 0;
1582  for (MapCIter i = mTable.begin(), e = mTable.end(); i != e; ++i) {
1583  if (isChild(i)) {
1584  sum += getChild(i).onVoxelCount();
1585  } else if (isTileOn(i)) {
1586  sum += ChildT::NUM_VOXELS;
1587  }
1588  }
1589  return sum;
1590 }
1591 
1592 
1593 template<typename ChildT>
1594 inline Index64
1596 {
1597  Index64 sum = 0;
1598  for (MapCIter i = mTable.begin(), e = mTable.end(); i != e; ++i) {
1599  if (isChild(i)) {
1600  sum += getChild(i).offVoxelCount();
1601  } else if (isTileOff(i) && !this->isBackgroundTile(i)) {
1602  sum += ChildT::NUM_VOXELS;
1603  }
1604  }
1605  return sum;
1606 }
1607 
1608 
1609 template<typename ChildT>
1610 inline Index64
1612 {
1613  Index64 sum = 0;
1614  for (MapCIter i = mTable.begin(), e = mTable.end(); i != e; ++i) {
1615  if (isChild(i)) sum += getChild(i).onLeafVoxelCount();
1616  }
1617  return sum;
1618 }
1619 
1620 
1621 template<typename ChildT>
1622 inline Index64
1624 {
1625  Index64 sum = 0;
1626  for (MapCIter i = mTable.begin(), e = mTable.end(); i != e; ++i) {
1627  if (isChild(i)) sum += getChild(i).offLeafVoxelCount();
1628  }
1629  return sum;
1630 }
1631 
1632 template<typename ChildT>
1633 inline Index64
1635 {
1636  Index64 sum = 0;
1637  for (MapCIter i = mTable.begin(), e = mTable.end(); i != e; ++i) {
1638  if (isChild(i)) {
1639  sum += getChild(i).onTileCount();
1640  } else if (isTileOn(i)) {
1641  sum += 1;
1642  }
1643  }
1644  return sum;
1645 }
1646 
1648 
1649 
1650 template<typename ChildT>
1651 inline bool
1652 RootNode<ChildT>::isValueOn(const Coord& xyz) const
1653 {
1654  MapCIter iter = this->findCoord(xyz);
1655  if (iter == mTable.end() || isTileOff(iter)) return false;
1656  return isTileOn(iter) ? true : getChild(iter).isValueOn(xyz);
1657 }
1658 
1659 template<typename ChildT>
1660 inline bool
1662 {
1663  for (MapCIter i = mTable.begin(), e = mTable.end(); i != e; ++i) {
1664  if (isChild(i) ? getChild(i).hasActiveTiles() : getTile(i).active) return true;
1665  }
1666  return false;
1667 }
1668 
1669 template<typename ChildT>
1670 template<typename AccessorT>
1671 inline bool
1672 RootNode<ChildT>::isValueOnAndCache(const Coord& xyz, AccessorT& acc) const
1673 {
1674  MapCIter iter = this->findCoord(xyz);
1675  if (iter == mTable.end() || isTileOff(iter)) return false;
1676  if (isTileOn(iter)) return true;
1677  acc.insert(xyz, &getChild(iter));
1678  return getChild(iter).isValueOnAndCache(xyz, acc);
1679 }
1680 
1681 
1682 template<typename ChildT>
1683 inline const typename ChildT::ValueType&
1684 RootNode<ChildT>::getValue(const Coord& xyz) const
1685 {
1686  MapCIter iter = this->findCoord(xyz);
1687  return iter == mTable.end() ? mBackground
1688  : (isTile(iter) ? getTile(iter).value : getChild(iter).getValue(xyz));
1689 }
1690 
1691 template<typename ChildT>
1692 template<typename AccessorT>
1693 inline const typename ChildT::ValueType&
1694 RootNode<ChildT>::getValueAndCache(const Coord& xyz, AccessorT& acc) const
1695 {
1696  MapCIter iter = this->findCoord(xyz);
1697  if (iter == mTable.end()) return mBackground;
1698  if (isChild(iter)) {
1699  acc.insert(xyz, &getChild(iter));
1700  return getChild(iter).getValueAndCache(xyz, acc);
1701  }
1702  return getTile(iter).value;
1703 }
1704 
1705 
1706 template<typename ChildT>
1707 inline int
1708 RootNode<ChildT>::getValueDepth(const Coord& xyz) const
1709 {
1710  MapCIter iter = this->findCoord(xyz);
1711  return iter == mTable.end() ? -1
1712  : (isTile(iter) ? 0 : int(LEVEL) - int(getChild(iter).getValueLevel(xyz)));
1713 }
1714 
1715 template<typename ChildT>
1716 template<typename AccessorT>
1717 inline int
1718 RootNode<ChildT>::getValueDepthAndCache(const Coord& xyz, AccessorT& acc) const
1719 {
1720  MapCIter iter = this->findCoord(xyz);
1721  if (iter == mTable.end()) return -1;
1722  if (isTile(iter)) return 0;
1723  acc.insert(xyz, &getChild(iter));
1724  return int(LEVEL) - int(getChild(iter).getValueLevelAndCache(xyz, acc));
1725 }
1726 
1727 
1728 template<typename ChildT>
1729 inline void
1731 {
1732  MapIter iter = this->findCoord(xyz);
1733  if (iter != mTable.end() && !isTileOff(iter)) {
1734  if (isTileOn(iter)) {
1735  setChild(iter, *new ChildT(xyz, getTile(iter).value, /*active=*/true));
1736  }
1737  getChild(iter).setValueOff(xyz);
1738  }
1739 }
1740 
1741 
1742 template<typename ChildT>
1743 inline void
1744 RootNode<ChildT>::setActiveState(const Coord& xyz, bool on)
1745 {
1746  ChildT* child = nullptr;
1747  MapIter iter = this->findCoord(xyz);
1748  if (iter == mTable.end()) {
1749  if (on) {
1750  child = new ChildT(xyz, mBackground);
1751  mTable[this->coordToKey(xyz)] = NodeStruct(*child);
1752  } else {
1753  // Nothing to do; (x, y, z) is background and therefore already inactive.
1754  }
1755  } else if (isChild(iter)) {
1756  child = &getChild(iter);
1757  } else if (on != getTile(iter).active) {
1758  child = new ChildT(xyz, getTile(iter).value, !on);
1759  setChild(iter, *child);
1760  }
1761  if (child) child->setActiveState(xyz, on);
1762 }
1763 
1764 template<typename ChildT>
1765 template<typename AccessorT>
1766 inline void
1767 RootNode<ChildT>::setActiveStateAndCache(const Coord& xyz, bool on, AccessorT& acc)
1768 {
1769  ChildT* child = nullptr;
1770  MapIter iter = this->findCoord(xyz);
1771  if (iter == mTable.end()) {
1772  if (on) {
1773  child = new ChildT(xyz, mBackground);
1774  mTable[this->coordToKey(xyz)] = NodeStruct(*child);
1775  } else {
1776  // Nothing to do; (x, y, z) is background and therefore already inactive.
1777  }
1778  } else if (isChild(iter)) {
1779  child = &getChild(iter);
1780  } else if (on != getTile(iter).active) {
1781  child = new ChildT(xyz, getTile(iter).value, !on);
1782  setChild(iter, *child);
1783  }
1784  if (child) {
1785  acc.insert(xyz, child);
1786  child->setActiveStateAndCache(xyz, on, acc);
1787  }
1788 }
1789 
1790 
1791 template<typename ChildT>
1792 inline void
1793 RootNode<ChildT>::setValueOff(const Coord& xyz, const ValueType& value)
1794 {
1795  ChildT* child = nullptr;
1796  MapIter iter = this->findCoord(xyz);
1797  if (iter == mTable.end()) {
1798  if (!math::isExactlyEqual(mBackground, value)) {
1799  child = new ChildT(xyz, mBackground);
1800  mTable[this->coordToKey(xyz)] = NodeStruct(*child);
1801  }
1802  } else if (isChild(iter)) {
1803  child = &getChild(iter);
1804  } else if (isTileOn(iter) || !math::isExactlyEqual(getTile(iter).value, value)) {
1805  child = new ChildT(xyz, getTile(iter).value, isTileOn(iter));
1806  setChild(iter, *child);
1807  }
1808  if (child) child->setValueOff(xyz, value);
1809 }
1810 
1811 template<typename ChildT>
1812 template<typename AccessorT>
1813 inline void
1814 RootNode<ChildT>::setValueOffAndCache(const Coord& xyz, const ValueType& value, AccessorT& acc)
1815 {
1816  ChildT* child = nullptr;
1817  MapIter iter = this->findCoord(xyz);
1818  if (iter == mTable.end()) {
1819  if (!math::isExactlyEqual(mBackground, value)) {
1820  child = new ChildT(xyz, mBackground);
1821  mTable[this->coordToKey(xyz)] = NodeStruct(*child);
1822  }
1823  } else if (isChild(iter)) {
1824  child = &getChild(iter);
1825  } else if (isTileOn(iter) || !math::isExactlyEqual(getTile(iter).value, value)) {
1826  child = new ChildT(xyz, getTile(iter).value, isTileOn(iter));
1827  setChild(iter, *child);
1828  }
1829  if (child) {
1830  acc.insert(xyz, child);
1831  child->setValueOffAndCache(xyz, value, acc);
1832  }
1833 }
1834 
1835 
1836 template<typename ChildT>
1837 inline void
1838 RootNode<ChildT>::setValueOn(const Coord& xyz, const ValueType& value)
1839 {
1840  ChildT* child = nullptr;
1841  MapIter iter = this->findCoord(xyz);
1842  if (iter == mTable.end()) {
1843  child = new ChildT(xyz, mBackground);
1844  mTable[this->coordToKey(xyz)] = NodeStruct(*child);
1845  } else if (isChild(iter)) {
1846  child = &getChild(iter);
1847  } else if (isTileOff(iter) || !math::isExactlyEqual(getTile(iter).value, value)) {
1848  child = new ChildT(xyz, getTile(iter).value, isTileOn(iter));
1849  setChild(iter, *child);
1850  }
1851  if (child) child->setValueOn(xyz, value);
1852 }
1853 
1854 template<typename ChildT>
1855 template<typename AccessorT>
1856 inline void
1857 RootNode<ChildT>::setValueAndCache(const Coord& xyz, const ValueType& value, AccessorT& acc)
1858 {
1859  ChildT* child = nullptr;
1860  MapIter iter = this->findCoord(xyz);
1861  if (iter == mTable.end()) {
1862  child = new ChildT(xyz, mBackground);
1863  mTable[this->coordToKey(xyz)] = NodeStruct(*child);
1864  } else if (isChild(iter)) {
1865  child = &getChild(iter);
1866  } else if (isTileOff(iter) || !math::isExactlyEqual(getTile(iter).value, value)) {
1867  child = new ChildT(xyz, getTile(iter).value, isTileOn(iter));
1868  setChild(iter, *child);
1869  }
1870  if (child) {
1871  acc.insert(xyz, child);
1872  child->setValueAndCache(xyz, value, acc);
1873  }
1874 }
1875 
1876 
1877 template<typename ChildT>
1878 inline void
1879 RootNode<ChildT>::setValueOnly(const Coord& xyz, const ValueType& value)
1880 {
1881  ChildT* child = nullptr;
1882  MapIter iter = this->findCoord(xyz);
1883  if (iter == mTable.end()) {
1884  child = new ChildT(xyz, mBackground);
1885  mTable[this->coordToKey(xyz)] = NodeStruct(*child);
1886  } else if (isChild(iter)) {
1887  child = &getChild(iter);
1888  } else if (!math::isExactlyEqual(getTile(iter).value, value)) {
1889  child = new ChildT(xyz, getTile(iter).value, isTileOn(iter));
1890  setChild(iter, *child);
1891  }
1892  if (child) child->setValueOnly(xyz, value);
1893 }
1894 
1895 template<typename ChildT>
1896 template<typename AccessorT>
1897 inline void
1898 RootNode<ChildT>::setValueOnlyAndCache(const Coord& xyz, const ValueType& value, AccessorT& acc)
1899 {
1900  ChildT* child = nullptr;
1901  MapIter iter = this->findCoord(xyz);
1902  if (iter == mTable.end()) {
1903  child = new ChildT(xyz, mBackground);
1904  mTable[this->coordToKey(xyz)] = NodeStruct(*child);
1905  } else if (isChild(iter)) {
1906  child = &getChild(iter);
1907  } else if (!math::isExactlyEqual(getTile(iter).value, value)) {
1908  child = new ChildT(xyz, getTile(iter).value, isTileOn(iter));
1909  setChild(iter, *child);
1910  }
1911  if (child) {
1912  acc.insert(xyz, child);
1913  child->setValueOnlyAndCache(xyz, value, acc);
1914  }
1915 }
1916 
1917 
1918 template<typename ChildT>
1919 template<typename ModifyOp>
1920 inline void
1921 RootNode<ChildT>::modifyValue(const Coord& xyz, const ModifyOp& op)
1922 {
1923  ChildT* child = nullptr;
1924  MapIter iter = this->findCoord(xyz);
1925  if (iter == mTable.end()) {
1926  child = new ChildT(xyz, mBackground);
1927  mTable[this->coordToKey(xyz)] = NodeStruct(*child);
1928  } else if (isChild(iter)) {
1929  child = &getChild(iter);
1930  } else {
1931  // Need to create a child if the tile is inactive,
1932  // in order to activate voxel (x, y, z).
1933  bool createChild = isTileOff(iter);
1934  if (!createChild) {
1935  // Need to create a child if applying the functor
1936  // to the tile value produces a different value.
1937  const ValueType& tileVal = getTile(iter).value;
1938  ValueType modifiedVal = tileVal;
1939  op(modifiedVal);
1940  createChild = !math::isExactlyEqual(tileVal, modifiedVal);
1941  }
1942  if (createChild) {
1943  child = new ChildT(xyz, getTile(iter).value, isTileOn(iter));
1944  setChild(iter, *child);
1945  }
1946  }
1947  if (child) child->modifyValue(xyz, op);
1948 }
1949 
1950 template<typename ChildT>
1951 template<typename ModifyOp, typename AccessorT>
1952 inline void
1953 RootNode<ChildT>::modifyValueAndCache(const Coord& xyz, const ModifyOp& op, AccessorT& acc)
1954 {
1955  ChildT* child = nullptr;
1956  MapIter iter = this->findCoord(xyz);
1957  if (iter == mTable.end()) {
1958  child = new ChildT(xyz, mBackground);
1959  mTable[this->coordToKey(xyz)] = NodeStruct(*child);
1960  } else if (isChild(iter)) {
1961  child = &getChild(iter);
1962  } else {
1963  // Need to create a child if the tile is inactive,
1964  // in order to activate voxel (x, y, z).
1965  bool createChild = isTileOff(iter);
1966  if (!createChild) {
1967  // Need to create a child if applying the functor
1968  // to the tile value produces a different value.
1969  const ValueType& tileVal = getTile(iter).value;
1970  ValueType modifiedVal = tileVal;
1971  op(modifiedVal);
1972  createChild = !math::isExactlyEqual(tileVal, modifiedVal);
1973  }
1974  if (createChild) {
1975  child = new ChildT(xyz, getTile(iter).value, isTileOn(iter));
1976  setChild(iter, *child);
1977  }
1978  }
1979  if (child) {
1980  acc.insert(xyz, child);
1981  child->modifyValueAndCache(xyz, op, acc);
1982  }
1983 }
1984 
1985 
1986 template<typename ChildT>
1987 template<typename ModifyOp>
1988 inline void
1989 RootNode<ChildT>::modifyValueAndActiveState(const Coord& xyz, const ModifyOp& op)
1990 {
1991  ChildT* child = nullptr;
1992  MapIter iter = this->findCoord(xyz);
1993  if (iter == mTable.end()) {
1994  child = new ChildT(xyz, mBackground);
1995  mTable[this->coordToKey(xyz)] = NodeStruct(*child);
1996  } else if (isChild(iter)) {
1997  child = &getChild(iter);
1998  } else {
1999  const Tile& tile = getTile(iter);
2000  bool modifiedState = tile.active;
2001  ValueType modifiedVal = tile.value;
2002  op(modifiedVal, modifiedState);
2003  // Need to create a child if applying the functor to the tile
2004  // produces a different value or active state.
2005  if (modifiedState != tile.active || !math::isExactlyEqual(modifiedVal, tile.value)) {
2006  child = new ChildT(xyz, tile.value, tile.active);
2007  setChild(iter, *child);
2008  }
2009  }
2010  if (child) child->modifyValueAndActiveState(xyz, op);
2011 }
2012 
2013 template<typename ChildT>
2014 template<typename ModifyOp, typename AccessorT>
2015 inline void
2017  const Coord& xyz, const ModifyOp& op, AccessorT& acc)
2018 {
2019  ChildT* child = nullptr;
2020  MapIter iter = this->findCoord(xyz);
2021  if (iter == mTable.end()) {
2022  child = new ChildT(xyz, mBackground);
2023  mTable[this->coordToKey(xyz)] = NodeStruct(*child);
2024  } else if (isChild(iter)) {
2025  child = &getChild(iter);
2026  } else {
2027  const Tile& tile = getTile(iter);
2028  bool modifiedState = tile.active;
2029  ValueType modifiedVal = tile.value;
2030  op(modifiedVal, modifiedState);
2031  // Need to create a child if applying the functor to the tile
2032  // produces a different value or active state.
2033  if (modifiedState != tile.active || !math::isExactlyEqual(modifiedVal, tile.value)) {
2034  child = new ChildT(xyz, tile.value, tile.active);
2035  setChild(iter, *child);
2036  }
2037  }
2038  if (child) {
2039  acc.insert(xyz, child);
2040  child->modifyValueAndActiveStateAndCache(xyz, op, acc);
2041  }
2042 }
2043 
2044 
2045 template<typename ChildT>
2046 inline bool
2047 RootNode<ChildT>::probeValue(const Coord& xyz, ValueType& value) const
2048 {
2049  MapCIter iter = this->findCoord(xyz);
2050  if (iter == mTable.end()) {
2051  value = mBackground;
2052  return false;
2053  } else if (isChild(iter)) {
2054  return getChild(iter).probeValue(xyz, value);
2055  }
2056  value = getTile(iter).value;
2057  return isTileOn(iter);
2058 }
2059 
2060 template<typename ChildT>
2061 template<typename AccessorT>
2062 inline bool
2063 RootNode<ChildT>::probeValueAndCache(const Coord& xyz, ValueType& value, AccessorT& acc) const
2064 {
2065  MapCIter iter = this->findCoord(xyz);
2066  if (iter == mTable.end()) {
2067  value = mBackground;
2068  return false;
2069  } else if (isChild(iter)) {
2070  acc.insert(xyz, &getChild(iter));
2071  return getChild(iter).probeValueAndCache(xyz, value, acc);
2072  }
2073  value = getTile(iter).value;
2074  return isTileOn(iter);
2075 }
2076 
2077 
2079 
2080 
2081 template<typename ChildT>
2082 inline void
2083 RootNode<ChildT>::fill(const CoordBBox& bbox, const ValueType& value, bool active)
2084 {
2085  if (bbox.empty()) return;
2086 
2087  // Iterate over the fill region in axis-aligned, tile-sized chunks.
2088  // (The first and last chunks along each axis might be smaller than a tile.)
2089  Coord xyz, tileMax;
2090  for (int x = bbox.min().x(); x <= bbox.max().x(); x = tileMax.x() + 1) {
2091  xyz.setX(x);
2092  for (int y = bbox.min().y(); y <= bbox.max().y(); y = tileMax.y() + 1) {
2093  xyz.setY(y);
2094  for (int z = bbox.min().z(); z <= bbox.max().z(); z = tileMax.z() + 1) {
2095  xyz.setZ(z);
2096 
2097  // Get the bounds of the tile that contains voxel (x, y, z).
2098  Coord tileMin = coordToKey(xyz);
2099  tileMax = tileMin.offsetBy(ChildT::DIM - 1);
2100 
2101  if (xyz != tileMin || Coord::lessThan(bbox.max(), tileMax)) {
2102  // If the box defined by (xyz, bbox.max()) doesn't completely enclose
2103  // the tile to which xyz belongs, create a child node (or retrieve
2104  // the existing one).
2105  ChildT* child = nullptr;
2106  MapIter iter = this->findKey(tileMin);
2107  if (iter == mTable.end()) {
2108  // No child or tile exists. Create a child and initialize it
2109  // with the background value.
2110  child = new ChildT(xyz, mBackground);
2111  mTable[tileMin] = NodeStruct(*child);
2112  } else if (isTile(iter)) {
2113  // Replace the tile with a newly-created child that is filled
2114  // with the tile's value and active state.
2115  const Tile& tile = getTile(iter);
2116  child = new ChildT(xyz, tile.value, tile.active);
2117  mTable[tileMin] = NodeStruct(*child);
2118  } else if (isChild(iter)) {
2119  child = &getChild(iter);
2120  }
2121  // Forward the fill request to the child.
2122  if (child) {
2123  const Coord tmp = Coord::minComponent(bbox.max(), tileMax);
2124  child->fill(CoordBBox(xyz, tmp), value, active);
2125  }
2126  } else {
2127  // If the box given by (xyz, bbox.max()) completely encloses
2128  // the tile to which xyz belongs, create the tile (if it
2129  // doesn't already exist) and give it the fill value.
2130  MapIter iter = this->findOrAddCoord(tileMin);
2131  setTile(iter, Tile(value, active));
2132  }
2133  }
2134  }
2135  }
2136 }
2137 
2138 
2139 template<typename ChildT>
2140 inline void
2141 RootNode<ChildT>::denseFill(const CoordBBox& bbox, const ValueType& value, bool active)
2142 {
2143  if (bbox.empty()) return;
2144 
2145  if (active && mTable.empty()) {
2146  // If this tree is empty, then a sparse fill followed by (threaded)
2147  // densification of active tiles is the more efficient approach.
2148  sparseFill(bbox, value, active);
2149  voxelizeActiveTiles(/*threaded=*/true);
2150  return;
2151  }
2152 
2153  // Iterate over the fill region in axis-aligned, tile-sized chunks.
2154  // (The first and last chunks along each axis might be smaller than a tile.)
2155  Coord xyz, tileMin, tileMax;
2156  for (int x = bbox.min().x(); x <= bbox.max().x(); x = tileMax.x() + 1) {
2157  xyz.setX(x);
2158  for (int y = bbox.min().y(); y <= bbox.max().y(); y = tileMax.y() + 1) {
2159  xyz.setY(y);
2160  for (int z = bbox.min().z(); z <= bbox.max().z(); z = tileMax.z() + 1) {
2161  xyz.setZ(z);
2162 
2163  // Get the bounds of the tile that contains voxel (x, y, z).
2164  tileMin = coordToKey(xyz);
2165  tileMax = tileMin.offsetBy(ChildT::DIM - 1);
2166 
2167  // Retrieve the table entry for the tile that contains xyz,
2168  // or, if there is no table entry, add a background tile.
2169  const auto iter = findOrAddCoord(tileMin);
2170 
2171  if (isTile(iter)) {
2172  // If the table entry is a tile, replace it with a child node
2173  // that is filled with the tile's value and active state.
2174  const auto& tile = getTile(iter);
2175  auto* child = new ChildT{tileMin, tile.value, tile.active};
2176  setChild(iter, *child);
2177  }
2178  // Forward the fill request to the child.
2179  getChild(iter).denseFill(bbox, value, active);
2180  }
2181  }
2182  }
2183 }
2184 
2185 
2187 
2188 
2189 template<typename ChildT>
2190 inline void
2192 {
2193  // There is little point in threading over the root table since each tile
2194  // spans a huge index space (by default 4096^3) and hence we expect few
2195  // active tiles if any at all. In fact, you're very likely to run out of
2196  // memory if this method is called on a tree with root-level active tiles!
2197  for (MapIter i = mTable.begin(), e = mTable.end(); i != e; ++i) {
2198  if (this->isTileOff(i)) continue;
2199  ChildT* child = i->second.child;
2200  if (child == nullptr) {
2201  // If this table entry is an active tile (i.e., not off and not a child node),
2202  // replace it with a child node filled with active tiles of the same value.
2203  child = new ChildT{i->first, this->getTile(i).value, true};
2204  i->second.child = child;
2205  }
2206  child->voxelizeActiveTiles(threaded);
2207  }
2208 }
2209 
2210 
2212 
2213 
2214 template<typename ChildT>
2215 template<typename DenseT>
2216 inline void
2217 RootNode<ChildT>::copyToDense(const CoordBBox& bbox, DenseT& dense) const
2218 {
2219  typedef typename DenseT::ValueType DenseValueType;
2220 
2221  const size_t xStride = dense.xStride(), yStride = dense.yStride(), zStride = dense.zStride();
2222  const Coord& min = dense.bbox().min();
2223  CoordBBox nodeBBox;
2224  for (Coord xyz = bbox.min(); xyz[0] <= bbox.max()[0]; xyz[0] = nodeBBox.max()[0] + 1) {
2225  for (xyz[1] = bbox.min()[1]; xyz[1] <= bbox.max()[1]; xyz[1] = nodeBBox.max()[1] + 1) {
2226  for (xyz[2] = bbox.min()[2]; xyz[2] <= bbox.max()[2]; xyz[2] = nodeBBox.max()[2] + 1) {
2227 
2228  // Get the coordinate bbox of the child node that contains voxel xyz.
2229  nodeBBox = CoordBBox::createCube(coordToKey(xyz), ChildT::DIM);
2230 
2231  // Get the coordinate bbox of the interection of inBBox and nodeBBox
2232  CoordBBox sub(xyz, Coord::minComponent(bbox.max(), nodeBBox.max()));
2233 
2234  MapCIter iter = this->findKey(nodeBBox.min());
2235  if (iter != mTable.end() && isChild(iter)) {//is a child
2236  getChild(iter).copyToDense(sub, dense);
2237  } else {//is background or a tile value
2238  const ValueType value = iter==mTable.end() ? mBackground : getTile(iter).value;
2239  sub.translate(-min);
2240  DenseValueType* a0 = dense.data() + zStride*sub.min()[2];
2241  for (Int32 x=sub.min()[0], ex=sub.max()[0]+1; x<ex; ++x) {
2242  DenseValueType* a1 = a0 + x*xStride;
2243  for (Int32 y=sub.min()[1], ey=sub.max()[1]+1; y<ey; ++y) {
2244  DenseValueType* a2 = a1 + y*yStride;
2245  for (Int32 z=sub.min()[2], ez=sub.max()[2]+1; z<ez; ++z, a2 += zStride) {
2246  *a2 = DenseValueType(value);
2247  }
2248  }
2249  }
2250  }
2251  }
2252  }
2253  }
2254 }
2255 
2257 
2258 
2259 template<typename ChildT>
2260 inline bool
2261 RootNode<ChildT>::writeTopology(std::ostream& os, bool toHalf) const
2262 {
2263  if (!toHalf) {
2264  os.write(reinterpret_cast<const char*>(&mBackground), sizeof(ValueType));
2265  } else {
2266  ValueType truncatedVal = io::truncateRealToHalf(mBackground);
2267  os.write(reinterpret_cast<const char*>(&truncatedVal), sizeof(ValueType));
2268  }
2269  io::setGridBackgroundValuePtr(os, &mBackground);
2270 
2271  const Index numTiles = this->getTileCount(), numChildren = this->getChildCount();
2272  os.write(reinterpret_cast<const char*>(&numTiles), sizeof(Index));
2273  os.write(reinterpret_cast<const char*>(&numChildren), sizeof(Index));
2274 
2275  if (numTiles == 0 && numChildren == 0) return false;
2276 
2277  // Write tiles.
2278  for (MapCIter i = mTable.begin(), e = mTable.end(); i != e; ++i) {
2279  if (isChild(i)) continue;
2280  os.write(reinterpret_cast<const char*>(i->first.asPointer()), 3 * sizeof(Int32));
2281  os.write(reinterpret_cast<const char*>(&getTile(i).value), sizeof(ValueType));
2282  os.write(reinterpret_cast<const char*>(&getTile(i).active), sizeof(bool));
2283  }
2284  // Write child nodes.
2285  for (MapCIter i = mTable.begin(), e = mTable.end(); i != e; ++i) {
2286  if (isTile(i)) continue;
2287  os.write(reinterpret_cast<const char*>(i->first.asPointer()), 3 * sizeof(Int32));
2288  getChild(i).writeTopology(os, toHalf);
2289  }
2290 
2291  return true; // not empty
2292 }
2293 
2294 
2295 template<typename ChildT>
2296 inline bool
2297 RootNode<ChildT>::readTopology(std::istream& is, bool fromHalf)
2298 {
2299  // Delete the existing tree.
2300  this->clear();
2301 
2303  // Read and convert an older-format RootNode.
2304 
2305  // For backward compatibility with older file formats, read both
2306  // outside and inside background values.
2307  is.read(reinterpret_cast<char*>(&mBackground), sizeof(ValueType));
2308  ValueType inside;
2309  is.read(reinterpret_cast<char*>(&inside), sizeof(ValueType));
2310 
2311  io::setGridBackgroundValuePtr(is, &mBackground);
2312 
2313  // Read the index range.
2314  Coord rangeMin, rangeMax;
2315  is.read(reinterpret_cast<char*>(rangeMin.asPointer()), 3 * sizeof(Int32));
2316  is.read(reinterpret_cast<char*>(rangeMax.asPointer()), 3 * sizeof(Int32));
2317 
2318  this->initTable();
2319  Index tableSize = 0, log2Dim[4] = { 0, 0, 0, 0 };
2320  Int32 offset[3];
2321  for (int i = 0; i < 3; ++i) {
2322  offset[i] = rangeMin[i] >> ChildT::TOTAL;
2323  rangeMin[i] = offset[i] << ChildT::TOTAL;
2324  log2Dim[i] = 1 + util::FindHighestOn((rangeMax[i] >> ChildT::TOTAL) - offset[i]);
2325  tableSize += log2Dim[i];
2326  rangeMax[i] = (((1 << log2Dim[i]) + offset[i]) << ChildT::TOTAL) - 1;
2327  }
2328  log2Dim[3] = log2Dim[1] + log2Dim[2];
2329  tableSize = 1U << tableSize;
2330 
2331  // Read masks.
2332  util::RootNodeMask childMask(tableSize), valueMask(tableSize);
2333  childMask.load(is);
2334  valueMask.load(is);
2335 
2336  // Read child nodes/values.
2337  for (Index i = 0; i < tableSize; ++i) {
2338  // Compute origin = offset2coord(i).
2339  Index n = i;
2340  Coord origin;
2341  origin[0] = (n >> log2Dim[3]) + offset[0];
2342  n &= (1U << log2Dim[3]) - 1;
2343  origin[1] = (n >> log2Dim[2]) + offset[1];
2344  origin[2] = (n & ((1U << log2Dim[2]) - 1)) + offset[1];
2345  origin <<= ChildT::TOTAL;
2346 
2347  if (childMask.isOn(i)) {
2348  // Read in and insert a child node.
2349 #ifdef OPENVDB_2_ABI_COMPATIBLE
2350  ChildT* child = new ChildT(origin, mBackground);
2351 #else
2352  ChildT* child = new ChildT(PartialCreate(), origin, mBackground);
2353 #endif
2354  child->readTopology(is);
2355  mTable[origin] = NodeStruct(*child);
2356  } else {
2357  // Read in a tile value and insert a tile, but only if the value
2358  // is either active or non-background.
2359  ValueType value;
2360  is.read(reinterpret_cast<char*>(&value), sizeof(ValueType));
2361  if (valueMask.isOn(i) || (!math::isApproxEqual(value, mBackground))) {
2362  mTable[origin] = NodeStruct(Tile(value, valueMask.isOn(i)));
2363  }
2364  }
2365  }
2366  return true;
2367  }
2368 
2369  // Read a RootNode that was stored in the current format.
2370 
2371  is.read(reinterpret_cast<char*>(&mBackground), sizeof(ValueType));
2372  io::setGridBackgroundValuePtr(is, &mBackground);
2373 
2374  Index numTiles = 0, numChildren = 0;
2375  is.read(reinterpret_cast<char*>(&numTiles), sizeof(Index));
2376  is.read(reinterpret_cast<char*>(&numChildren), sizeof(Index));
2377 
2378  if (numTiles == 0 && numChildren == 0) return false;
2379 
2380  Int32 vec[3];
2381  ValueType value;
2382  bool active;
2383 
2384  // Read tiles.
2385  for (Index n = 0; n < numTiles; ++n) {
2386  is.read(reinterpret_cast<char*>(vec), 3 * sizeof(Int32));
2387  is.read(reinterpret_cast<char*>(&value), sizeof(ValueType));
2388  is.read(reinterpret_cast<char*>(&active), sizeof(bool));
2389  mTable[Coord(vec)] = NodeStruct(Tile(value, active));
2390  }
2391 
2392  // Read child nodes.
2393  for (Index n = 0; n < numChildren; ++n) {
2394  is.read(reinterpret_cast<char*>(vec), 3 * sizeof(Int32));
2395  Coord origin(vec);
2396 #ifdef OPENVDB_2_ABI_COMPATIBLE
2397  ChildT* child = new ChildT(origin, mBackground);
2398 #else
2399  ChildT* child = new ChildT(PartialCreate(), origin, mBackground);
2400 #endif
2401  child->readTopology(is, fromHalf);
2402  mTable[Coord(vec)] = NodeStruct(*child);
2403  }
2404 
2405  return true; // not empty
2406 }
2407 
2408 
2409 template<typename ChildT>
2410 inline void
2411 RootNode<ChildT>::writeBuffers(std::ostream& os, bool toHalf) const
2412 {
2413  for (MapCIter i = mTable.begin(), e = mTable.end(); i != e; ++i) {
2414  if (isChild(i)) getChild(i).writeBuffers(os, toHalf);
2415  }
2416 }
2417 
2418 
2419 template<typename ChildT>
2420 inline void
2421 RootNode<ChildT>::readBuffers(std::istream& is, bool fromHalf)
2422 {
2423  for (MapIter i = mTable.begin(), e = mTable.end(); i != e; ++i) {
2424  if (isChild(i)) getChild(i).readBuffers(is, fromHalf);
2425  }
2426 }
2427 
2428 
2429 template<typename ChildT>
2430 inline void
2431 RootNode<ChildT>::readBuffers(std::istream& is, const CoordBBox& clipBBox, bool fromHalf)
2432 {
2433  const Tile bgTile(mBackground, /*active=*/false);
2434 
2435  for (MapIter i = mTable.begin(), e = mTable.end(); i != e; ++i) {
2436  if (isChild(i)) {
2437  // Stream in and clip the branch rooted at this child.
2438  // (We can't skip over children that lie outside the clipping region,
2439  // because buffers are serialized in depth-first order and need to be
2440  // unserialized in the same order.)
2441  ChildT& child = getChild(i);
2442  child.readBuffers(is, clipBBox, fromHalf);
2443  }
2444  }
2445  // Clip root-level tiles and prune children that were clipped.
2446  this->clip(clipBBox);
2447 }
2448 
2449 
2451 
2452 
2453 template<typename ChildT>
2454 inline void
2455 RootNode<ChildT>::clip(const CoordBBox& clipBBox)
2456 {
2457  const Tile bgTile(mBackground, /*active=*/false);
2458 
2459  // Iterate over a copy of this node's table so that we can modify the original.
2460  // (Copying the table copies child node pointers, not the nodes themselves.)
2461  MapType copyOfTable(mTable);
2462  for (MapIter i = copyOfTable.begin(), e = copyOfTable.end(); i != e; ++i) {
2463  const Coord& xyz = i->first; // tile or child origin
2464  CoordBBox tileBBox(xyz, xyz.offsetBy(ChildT::DIM - 1)); // tile or child bounds
2465  if (!clipBBox.hasOverlap(tileBBox)) {
2466  // This table entry lies completely outside the clipping region. Delete it.
2467  setTile(this->findCoord(xyz), bgTile); // delete any existing child node first
2468  mTable.erase(xyz);
2469  } else if (!clipBBox.isInside(tileBBox)) {
2470  // This table entry does not lie completely inside the clipping region
2471  // and must be clipped.
2472  if (isChild(i)) {
2473  getChild(i).clip(clipBBox, mBackground);
2474  } else {
2475  // Replace this tile with a background tile, then fill the clip region
2476  // with the tile's original value. (This might create a child branch.)
2477  tileBBox.intersect(clipBBox);
2478  const Tile& origTile = getTile(i);
2479  setTile(this->findCoord(xyz), bgTile);
2480  this->sparseFill(tileBBox, origTile.value, origTile.active);
2481  }
2482  } else {
2483  // This table entry lies completely inside the clipping region. Leave it intact.
2484  }
2485  }
2486  this->prune(); // also erases root-level background tiles
2487 }
2488 
2489 
2491 
2492 
2493 template<typename ChildT>
2494 inline void
2495 RootNode<ChildT>::prune(const ValueType& tolerance)
2496 {
2497  bool state = false;
2498  ValueType value = zeroVal<ValueType>();
2499  for (MapIter i = mTable.begin(), e = mTable.end(); i != e; ++i) {
2500  if (this->isTile(i)) continue;
2501  this->getChild(i).prune(tolerance);
2502  if (this->getChild(i).isConstant(value, state, tolerance)) {
2503  this->setTile(i, Tile(value, state));
2504  }
2505  }
2506  this->eraseBackgroundTiles();
2507 }
2508 
2509 
2511 
2512 
2513 template<typename ChildT>
2514 template<typename NodeT>
2515 inline NodeT*
2516 RootNode<ChildT>::stealNode(const Coord& xyz, const ValueType& value, bool state)
2517 {
2518  if ((NodeT::LEVEL == ChildT::LEVEL && !(boost::is_same<NodeT, ChildT>::value)) ||
2519  NodeT::LEVEL > ChildT::LEVEL) return nullptr;
2521  MapIter iter = this->findCoord(xyz);
2522  if (iter == mTable.end() || isTile(iter)) return nullptr;
2523  return (boost::is_same<NodeT, ChildT>::value)
2524  ? reinterpret_cast<NodeT*>(&stealChild(iter, Tile(value, state)))
2525  : getChild(iter).template stealNode<NodeT>(xyz, value, state);
2527 }
2528 
2529 
2531 
2532 
2533 template<typename ChildT>
2534 inline void
2535 RootNode<ChildT>::addLeaf(LeafNodeType* leaf)
2536 {
2537  if (leaf == nullptr) return;
2538  ChildT* child = nullptr;
2539  const Coord& xyz = leaf->origin();
2540  MapIter iter = this->findCoord(xyz);
2541  if (iter == mTable.end()) {
2542  if (ChildT::LEVEL>0) {
2543  child = new ChildT(xyz, mBackground, false);
2544  } else {
2545  child = reinterpret_cast<ChildT*>(leaf);
2546  }
2547  mTable[this->coordToKey(xyz)] = NodeStruct(*child);
2548  } else if (isChild(iter)) {
2549  if (ChildT::LEVEL>0) {
2550  child = &getChild(iter);
2551  } else {
2552  child = reinterpret_cast<ChildT*>(leaf);
2553  setChild(iter, *child);//this also deletes the existing child node
2554  }
2555  } else {//tile
2556  if (ChildT::LEVEL>0) {
2557  child = new ChildT(xyz, getTile(iter).value, isTileOn(iter));
2558  } else {
2559  child = reinterpret_cast<ChildT*>(leaf);
2560  }
2561  setChild(iter, *child);
2562  }
2563  child->addLeaf(leaf);
2564 }
2565 
2566 
2567 template<typename ChildT>
2568 template<typename AccessorT>
2569 inline void
2570 RootNode<ChildT>::addLeafAndCache(LeafNodeType* leaf, AccessorT& acc)
2571 {
2572  if (leaf == nullptr) return;
2573  ChildT* child = nullptr;
2574  const Coord& xyz = leaf->origin();
2575  MapIter iter = this->findCoord(xyz);
2576  if (iter == mTable.end()) {
2577  if (ChildT::LEVEL>0) {
2578  child = new ChildT(xyz, mBackground, false);
2579  } else {
2580  child = reinterpret_cast<ChildT*>(leaf);
2581  }
2582  mTable[this->coordToKey(xyz)] = NodeStruct(*child);
2583  } else if (isChild(iter)) {
2584  if (ChildT::LEVEL>0) {
2585  child = &getChild(iter);
2586  } else {
2587  child = reinterpret_cast<ChildT*>(leaf);
2588  setChild(iter, *child);//this also deletes the existing child node
2589  }
2590  } else {//tile
2591  if (ChildT::LEVEL>0) {
2592  child = new ChildT(xyz, getTile(iter).value, isTileOn(iter));
2593  } else {
2594  child = reinterpret_cast<ChildT*>(leaf);
2595  }
2596  setChild(iter, *child);
2597  }
2598  acc.insert(xyz, child);
2599  child->addLeafAndCache(leaf, acc);
2600 }
2601 
2602 template<typename ChildT>
2603 inline void
2604 RootNode<ChildT>::addTile(const Coord& xyz, const ValueType& value, bool state)
2605 {
2606  MapIter iter = this->findCoord(xyz);
2607  if (iter == mTable.end()) {//background
2608  mTable[this->coordToKey(xyz)] = NodeStruct(Tile(value, state));
2609  } else {//child or tile
2610  setTile(iter, Tile(value, state));//this also deletes the existing child node
2611  }
2612 }
2613 
2614 template<typename ChildT>
2615 inline void
2616 RootNode<ChildT>::addTile(Index level, const Coord& xyz,
2617  const ValueType& value, bool state)
2618 {
2619  if (LEVEL >= level) {
2620  MapIter iter = this->findCoord(xyz);
2621  if (iter == mTable.end()) {//background
2622  if (LEVEL > level) {
2623  ChildT* child = new ChildT(xyz, mBackground, false);
2624  mTable[this->coordToKey(xyz)] = NodeStruct(*child);
2625  child->addTile(level, xyz, value, state);
2626  } else {
2627  mTable[this->coordToKey(xyz)] = NodeStruct(Tile(value, state));
2628  }
2629  } else if (isChild(iter)) {//child
2630  if (LEVEL > level) {
2631  getChild(iter).addTile(level, xyz, value, state);
2632  } else {
2633  setTile(iter, Tile(value, state));//this also deletes the existing child node
2634  }
2635  } else {//tile
2636  if (LEVEL > level) {
2637  ChildT* child = new ChildT(xyz, getTile(iter).value, isTileOn(iter));
2638  setChild(iter, *child);
2639  child->addTile(level, xyz, value, state);
2640  } else {
2641  setTile(iter, Tile(value, state));
2642  }
2643  }
2644  }
2645 }
2646 
2647 
2648 template<typename ChildT>
2649 template<typename AccessorT>
2650 inline void
2651 RootNode<ChildT>::addTileAndCache(Index level, const Coord& xyz, const ValueType& value,
2652  bool state, AccessorT& acc)
2653 {
2654  if (LEVEL >= level) {
2655  MapIter iter = this->findCoord(xyz);
2656  if (iter == mTable.end()) {//background
2657  if (LEVEL > level) {
2658  ChildT* child = new ChildT(xyz, mBackground, false);
2659  acc.insert(xyz, child);
2660  mTable[this->coordToKey(xyz)] = NodeStruct(*child);
2661  child->addTileAndCache(level, xyz, value, state, acc);
2662  } else {
2663  mTable[this->coordToKey(xyz)] = NodeStruct(Tile(value, state));
2664  }
2665  } else if (isChild(iter)) {//child
2666  if (LEVEL > level) {
2667  ChildT* child = &getChild(iter);
2668  acc.insert(xyz, child);
2669  child->addTileAndCache(level, xyz, value, state, acc);
2670  } else {
2671  setTile(iter, Tile(value, state));//this also deletes the existing child node
2672  }
2673  } else {//tile
2674  if (LEVEL > level) {
2675  ChildT* child = new ChildT(xyz, getTile(iter).value, isTileOn(iter));
2676  acc.insert(xyz, child);
2677  setChild(iter, *child);
2678  child->addTileAndCache(level, xyz, value, state, acc);
2679  } else {
2680  setTile(iter, Tile(value, state));
2681  }
2682  }
2683  }
2684 }
2685 
2686 
2688 
2689 
2690 template<typename ChildT>
2691 inline typename ChildT::LeafNodeType*
2693 {
2694  ChildT* child = nullptr;
2695  MapIter iter = this->findCoord(xyz);
2696  if (iter == mTable.end()) {
2697  child = new ChildT(xyz, mBackground, false);
2698  mTable[this->coordToKey(xyz)] = NodeStruct(*child);
2699  } else if (isChild(iter)) {
2700  child = &getChild(iter);
2701  } else {
2702  child = new ChildT(xyz, getTile(iter).value, isTileOn(iter));
2703  setChild(iter, *child);
2704  }
2705  return child->touchLeaf(xyz);
2706 }
2707 
2708 
2709 template<typename ChildT>
2710 template<typename AccessorT>
2711 inline typename ChildT::LeafNodeType*
2712 RootNode<ChildT>::touchLeafAndCache(const Coord& xyz, AccessorT& acc)
2713 {
2714  ChildT* child = nullptr;
2715  MapIter iter = this->findCoord(xyz);
2716  if (iter == mTable.end()) {
2717  child = new ChildT(xyz, mBackground, false);
2718  mTable[this->coordToKey(xyz)] = NodeStruct(*child);
2719  } else if (isChild(iter)) {
2720  child = &getChild(iter);
2721  } else {
2722  child = new ChildT(xyz, getTile(iter).value, isTileOn(iter));
2723  setChild(iter, *child);
2724  }
2725  acc.insert(xyz, child);
2726  return child->touchLeafAndCache(xyz, acc);
2727 }
2728 
2729 
2731 
2732 
2733 template<typename ChildT>
2734 template<typename NodeT>
2735 inline NodeT*
2737 {
2738  if ((NodeT::LEVEL == ChildT::LEVEL && !(boost::is_same<NodeT, ChildT>::value)) ||
2739  NodeT::LEVEL > ChildT::LEVEL) return nullptr;
2741  MapIter iter = this->findCoord(xyz);
2742  if (iter == mTable.end() || isTile(iter)) return nullptr;
2743  ChildT* child = &getChild(iter);
2744  return (boost::is_same<NodeT, ChildT>::value)
2745  ? reinterpret_cast<NodeT*>(child)
2746  : child->template probeNode<NodeT>(xyz);
2748 }
2749 
2750 
2751 template<typename ChildT>
2752 template<typename NodeT>
2753 inline const NodeT*
2754 RootNode<ChildT>::probeConstNode(const Coord& xyz) const
2755 {
2756  if ((NodeT::LEVEL == ChildT::LEVEL && !(boost::is_same<NodeT, ChildT>::value)) ||
2757  NodeT::LEVEL > ChildT::LEVEL) return nullptr;
2759  MapCIter iter = this->findCoord(xyz);
2760  if (iter == mTable.end() || isTile(iter)) return nullptr;
2761  const ChildT* child = &getChild(iter);
2762  return (boost::is_same<NodeT, ChildT>::value)
2763  ? reinterpret_cast<const NodeT*>(child)
2764  : child->template probeConstNode<NodeT>(xyz);
2766 }
2767 
2768 
2769 template<typename ChildT>
2770 inline typename ChildT::LeafNodeType*
2772 {
2773  return this->template probeNode<LeafNodeType>(xyz);
2774 }
2775 
2776 
2777 template<typename ChildT>
2778 inline const typename ChildT::LeafNodeType*
2779 RootNode<ChildT>::probeConstLeaf(const Coord& xyz) const
2780 {
2781  return this->template probeConstNode<LeafNodeType>(xyz);
2782 }
2783 
2784 
2785 template<typename ChildT>
2786 template<typename AccessorT>
2787 inline typename ChildT::LeafNodeType*
2788 RootNode<ChildT>::probeLeafAndCache(const Coord& xyz, AccessorT& acc)
2789 {
2790  return this->template probeNodeAndCache<LeafNodeType>(xyz, acc);
2791 }
2792 
2793 
2794 template<typename ChildT>
2795 template<typename AccessorT>
2796 inline const typename ChildT::LeafNodeType*
2797 RootNode<ChildT>::probeConstLeafAndCache(const Coord& xyz, AccessorT& acc) const
2798 {
2799  return this->template probeConstNodeAndCache<LeafNodeType>(xyz, acc);
2800 }
2801 
2802 
2803 template<typename ChildT>
2804 template<typename AccessorT>
2805 inline const typename ChildT::LeafNodeType*
2806 RootNode<ChildT>::probeLeafAndCache(const Coord& xyz, AccessorT& acc) const
2807 {
2808  return this->probeConstLeafAndCache(xyz, acc);
2809 }
2810 
2811 
2812 template<typename ChildT>
2813 template<typename NodeT, typename AccessorT>
2814 inline NodeT*
2815 RootNode<ChildT>::probeNodeAndCache(const Coord& xyz, AccessorT& acc)
2816 {
2817  if ((NodeT::LEVEL == ChildT::LEVEL && !(boost::is_same<NodeT, ChildT>::value)) ||
2818  NodeT::LEVEL > ChildT::LEVEL) return nullptr;
2820  MapIter iter = this->findCoord(xyz);
2821  if (iter == mTable.end() || isTile(iter)) return nullptr;
2822  ChildT* child = &getChild(iter);
2823  acc.insert(xyz, child);
2824  return (boost::is_same<NodeT, ChildT>::value)
2825  ? reinterpret_cast<NodeT*>(child)
2826  : child->template probeNodeAndCache<NodeT>(xyz, acc);
2828 }
2829 
2830 
2831 template<typename ChildT>
2832 template<typename NodeT,typename AccessorT>
2833 inline const NodeT*
2834 RootNode<ChildT>::probeConstNodeAndCache(const Coord& xyz, AccessorT& acc) const
2835 {
2836  if ((NodeT::LEVEL == ChildT::LEVEL && !(boost::is_same<NodeT, ChildT>::value)) ||
2837  NodeT::LEVEL > ChildT::LEVEL) return nullptr;
2839  MapCIter iter = this->findCoord(xyz);
2840  if (iter == mTable.end() || isTile(iter)) return nullptr;
2841  const ChildT* child = &getChild(iter);
2842  acc.insert(xyz, child);
2843  return (boost::is_same<NodeT, ChildT>::value)
2844  ? reinterpret_cast<const NodeT*>(child)
2845  : child->template probeConstNodeAndCache<NodeT>(xyz, acc);
2847 }
2848 
2849 
2851 
2852 template<typename ChildT>
2853 template<typename ArrayT>
2854 inline void
2856 {
2857  typedef typename ArrayT::value_type NodePtr;
2858  BOOST_STATIC_ASSERT(boost::is_pointer<NodePtr>::value);
2859  typedef typename boost::remove_pointer<NodePtr>::type NodeType;
2860  typedef typename boost::remove_const<NodeType>::type NonConstNodeType;
2861  typedef typename boost::mpl::contains<NodeChainType, NonConstNodeType>::type result;
2862  BOOST_STATIC_ASSERT(result::value);
2863  typedef typename boost::mpl::if_<boost::is_const<NodeType>,
2864  const ChildT, ChildT>::type ArrayChildT;
2865 
2866  for (MapIter iter=mTable.begin(); iter!=mTable.end(); ++iter) {
2867  if (ChildT* child = iter->second.child) {
2869  if (boost::is_same<NodePtr, ArrayChildT*>::value) {
2870  array.push_back(reinterpret_cast<NodePtr>(iter->second.child));
2871  } else {
2872  child->getNodes(array);//descent
2873  }
2875  }
2876  }
2877 }
2878 
2879 template<typename ChildT>
2880 template<typename ArrayT>
2881 inline void
2882 RootNode<ChildT>::getNodes(ArrayT& array) const
2883 {
2884  typedef typename ArrayT::value_type NodePtr;
2885  BOOST_STATIC_ASSERT(boost::is_pointer<NodePtr>::value);
2886  typedef typename boost::remove_pointer<NodePtr>::type NodeType;
2887  BOOST_STATIC_ASSERT(boost::is_const<NodeType>::value);
2888  typedef typename boost::remove_const<NodeType>::type NonConstNodeType;
2889  typedef typename boost::mpl::contains<NodeChainType, NonConstNodeType>::type result;
2890  BOOST_STATIC_ASSERT(result::value);
2891 
2892  for (MapCIter iter=mTable.begin(); iter!=mTable.end(); ++iter) {
2893  if (const ChildNodeType *child = iter->second.child) {
2895  if (boost::is_same<NodePtr, const ChildT*>::value) {
2896  array.push_back(reinterpret_cast<NodePtr>(iter->second.child));
2897  } else {
2898  child->getNodes(array);//descent
2899  }
2901  }
2902  }
2903 }
2904 
2906 
2907 template<typename ChildT>
2908 template<typename ArrayT>
2909 inline void
2910 RootNode<ChildT>::stealNodes(ArrayT& array, const ValueType& value, bool state)
2911 {
2912  typedef typename ArrayT::value_type NodePtr;
2913  BOOST_STATIC_ASSERT(boost::is_pointer<NodePtr>::value);
2914  typedef typename boost::remove_pointer<NodePtr>::type NodeType;
2915  typedef typename boost::remove_const<NodeType>::type NonConstNodeType;
2916  typedef typename boost::mpl::contains<NodeChainType, NonConstNodeType>::type result;
2917  BOOST_STATIC_ASSERT(result::value);
2918  typedef typename boost::mpl::if_<boost::is_const<NodeType>,
2919  const ChildT, ChildT>::type ArrayChildT;
2920 
2921  for (MapIter iter=mTable.begin(); iter!=mTable.end(); ++iter) {
2922  if (ChildT* child = iter->second.child) {
2924  if (boost::is_same<NodePtr, ArrayChildT*>::value) {
2925  array.push_back(reinterpret_cast<NodePtr>(&stealChild(iter, Tile(value, state))));
2926  } else {
2927  child->stealNodes(array, value, state);//descent
2928  }
2930  }
2931  }
2932 }
2933 
2934 
2936 
2937 
2938 template<typename ChildT>
2939 template<MergePolicy Policy>
2940 inline void
2942 {
2944 
2945  switch (Policy) {
2946 
2947  default:
2948  case MERGE_ACTIVE_STATES:
2949  for (MapIter i = other.mTable.begin(), e = other.mTable.end(); i != e; ++i) {
2950  MapIter j = mTable.find(i->first);
2951  if (other.isChild(i)) {
2952  if (j == mTable.end()) { // insert other node's child
2953  ChildNodeType& child = stealChild(i, Tile(other.mBackground, /*on=*/false));
2954  child.resetBackground(other.mBackground, mBackground);
2955  mTable[i->first] = NodeStruct(child);
2956  } else if (isTile(j)) {
2957  if (isTileOff(j)) { // replace inactive tile with other node's child
2958  ChildNodeType& child = stealChild(i, Tile(other.mBackground, /*on=*/false));
2959  child.resetBackground(other.mBackground, mBackground);
2960  setChild(j, child);
2961  }
2962  } else { // merge both child nodes
2963  getChild(j).template merge<MERGE_ACTIVE_STATES>(getChild(i),
2964  other.mBackground, mBackground);
2965  }
2966  } else if (other.isTileOn(i)) {
2967  if (j == mTable.end()) { // insert other node's active tile
2968  mTable[i->first] = i->second;
2969  } else if (!isTileOn(j)) {
2970  // Replace anything except an active tile with the other node's active tile.
2971  setTile(j, Tile(other.getTile(i).value, true));
2972  }
2973  }
2974  }
2975  break;
2976 
2977  case MERGE_NODES:
2978  for (MapIter i = other.mTable.begin(), e = other.mTable.end(); i != e; ++i) {
2979  MapIter j = mTable.find(i->first);
2980  if (other.isChild(i)) {
2981  if (j == mTable.end()) { // insert other node's child
2982  ChildNodeType& child = stealChild(i, Tile(other.mBackground, /*on=*/false));
2983  child.resetBackground(other.mBackground, mBackground);
2984  mTable[i->first] = NodeStruct(child);
2985  } else if (isTile(j)) { // replace tile with other node's child
2986  ChildNodeType& child = stealChild(i, Tile(other.mBackground, /*on=*/false));
2987  child.resetBackground(other.mBackground, mBackground);
2988  setChild(j, child);
2989  } else { // merge both child nodes
2990  getChild(j).template merge<MERGE_NODES>(
2991  getChild(i), other.mBackground, mBackground);
2992  }
2993  }
2994  }
2995  break;
2996 
2998  for (MapIter i = other.mTable.begin(), e = other.mTable.end(); i != e; ++i) {
2999  MapIter j = mTable.find(i->first);
3000  if (other.isChild(i)) {
3001  if (j == mTable.end()) {
3002  // Steal and insert the other node's child.
3003  ChildNodeType& child = stealChild(i, Tile(other.mBackground, /*on=*/false));
3004  child.resetBackground(other.mBackground, mBackground);
3005  mTable[i->first] = NodeStruct(child);
3006  } else if (isTile(j)) {
3007  // Replace this node's tile with the other node's child.
3008  ChildNodeType& child = stealChild(i, Tile(other.mBackground, /*on=*/false));
3009  child.resetBackground(other.mBackground, mBackground);
3010  const Tile tile = getTile(j);
3011  setChild(j, child);
3012  if (tile.active) {
3013  // Merge the other node's child with this node's active tile.
3014  child.template merge<MERGE_ACTIVE_STATES_AND_NODES>(
3015  tile.value, tile.active);
3016  }
3017  } else /*if (isChild(j))*/ {
3018  // Merge the other node's child into this node's child.
3019  getChild(j).template merge<MERGE_ACTIVE_STATES_AND_NODES>(getChild(i),
3020  other.mBackground, mBackground);
3021  }
3022  } else if (other.isTileOn(i)) {
3023  if (j == mTable.end()) {
3024  // Insert a copy of the other node's active tile.
3025  mTable[i->first] = i->second;
3026  } else if (isTileOff(j)) {
3027  // Replace this node's inactive tile with a copy of the other's active tile.
3028  setTile(j, Tile(other.getTile(i).value, true));
3029  } else if (isChild(j)) {
3030  // Merge the other node's active tile into this node's child.
3031  const Tile& tile = getTile(i);
3032  getChild(j).template merge<MERGE_ACTIVE_STATES_AND_NODES>(
3033  tile.value, tile.active);
3034  }
3035  } // else if (other.isTileOff(i)) {} // ignore the other node's inactive tiles
3036  }
3037  break;
3038  }
3039 
3040  // Empty the other tree so as not to leave it in a partially cannibalized state.
3041  other.clear();
3042 
3044 }
3045 
3046 
3048 
3049 
3050 template<typename ChildT>
3051 template<typename OtherChildType>
3052 inline void
3054 {
3055  typedef RootNode<OtherChildType> OtherRootT;
3056  typedef typename OtherRootT::MapCIter OtherCIterT;
3057 
3058  enforceSameConfiguration(other);
3059 
3060  for (OtherCIterT i = other.mTable.begin(), e = other.mTable.end(); i != e; ++i) {
3061  MapIter j = mTable.find(i->first);
3062  if (other.isChild(i)) {
3063  if (j == mTable.end()) { // create child branch with identical topology
3064  mTable[i->first] = NodeStruct(
3065  *(new ChildT(other.getChild(i), mBackground, TopologyCopy())));
3066  } else if (this->isChild(j)) { // union with child branch
3067  this->getChild(j).topologyUnion(other.getChild(i));
3068  } else {// this is a tile so replace it with a child branch with identical topology
3069  ChildT* child = new ChildT(
3070  other.getChild(i), this->getTile(j).value, TopologyCopy());
3071  if (this->isTileOn(j)) child->setValuesOn();//this is an active tile
3072  this->setChild(j, *child);
3073  }
3074  } else if (other.isTileOn(i)) { // other is an active tile
3075  if (j == mTable.end()) { // insert an active tile
3076  mTable[i->first] = NodeStruct(Tile(mBackground, true));
3077  } else if (this->isChild(j)) {
3078  this->getChild(j).setValuesOn();
3079  } else if (this->isTileOff(j)) {
3080  this->setTile(j, Tile(this->getTile(j).value, true));
3081  }
3082  }
3083  }
3084 }
3085 
3086 template<typename ChildT>
3087 template<typename OtherChildType>
3088 inline void
3090 {
3091  typedef RootNode<OtherChildType> OtherRootT;
3092  typedef typename OtherRootT::MapCIter OtherCIterT;
3093 
3094  enforceSameConfiguration(other);
3095 
3096  std::set<Coord> tmp;//keys to erase
3097  for (MapIter i = mTable.begin(), e = mTable.end(); i != e; ++i) {
3098  OtherCIterT j = other.mTable.find(i->first);
3099  if (this->isChild(i)) {
3100  if (j == other.mTable.end() || other.isTileOff(j)) {
3101  tmp.insert(i->first);//delete child branch
3102  } else if (other.isChild(j)) { // intersect with child branch
3103  this->getChild(i).topologyIntersection(other.getChild(j), mBackground);
3104  }
3105  } else if (this->isTileOn(i)) {
3106  if (j == other.mTable.end() || other.isTileOff(j)) {
3107  this->setTile(i, Tile(this->getTile(i).value, false));//turn inactive
3108  } else if (other.isChild(j)) { //replace with a child branch with identical topology
3109  ChildT* child =
3110  new ChildT(other.getChild(j), this->getTile(i).value, TopologyCopy());
3111  this->setChild(i, *child);
3112  }
3113  }
3114  }
3115  for (std::set<Coord>::iterator i = tmp.begin(), e = tmp.end(); i != e; ++i) {
3116  MapIter it = this->findCoord(*i);
3117  setTile(it, Tile()); // delete any existing child node first
3118  mTable.erase(it);
3119  }
3120 }
3121 
3122 template<typename ChildT>
3123 template<typename OtherChildType>
3124 inline void
3126 {
3127  typedef RootNode<OtherChildType> OtherRootT;
3128  typedef typename OtherRootT::MapCIter OtherCIterT;
3129 
3130  enforceSameConfiguration(other);
3131 
3132  for (OtherCIterT i = other.mTable.begin(), e = other.mTable.end(); i != e; ++i) {
3133  MapIter j = mTable.find(i->first);
3134  if (other.isChild(i)) {
3135  if (j == mTable.end() || this->isTileOff(j)) {
3136  //do nothing
3137  } else if (this->isChild(j)) { // difference with child branch
3138  this->getChild(j).topologyDifference(other.getChild(i), mBackground);
3139  } else if (this->isTileOn(j)) {
3140  // this is an active tile so create a child node and descent
3141  ChildT* child = new ChildT(j->first, this->getTile(j).value, true);
3142  child->topologyDifference(other.getChild(i), mBackground);
3143  this->setChild(j, *child);
3144  }
3145  } else if (other.isTileOn(i)) { // other is an active tile
3146  if (j == mTable.end() || this->isTileOff(j)) {
3147  // do nothing
3148  } else if (this->isChild(j)) {
3149  setTile(j, Tile()); // delete any existing child node first
3150  mTable.erase(j);
3151  } else if (this->isTileOn(j)) {
3152  this->setTile(j, Tile(this->getTile(j).value, false));
3153  }
3154  }
3155  }
3156 }
3157 
3159 
3160 
3161 template<typename ChildT>
3162 template<typename CombineOp>
3163 inline void
3164 RootNode<ChildT>::combine(RootNode& other, CombineOp& op, bool prune)
3165 {
3167 
3168  CoordSet keys;
3169  this->insertKeys(keys);
3170  other.insertKeys(keys);
3171 
3172  for (CoordSetCIter i = keys.begin(), e = keys.end(); i != e; ++i) {
3173  MapIter iter = findOrAddCoord(*i), otherIter = other.findOrAddCoord(*i);
3174  if (isTile(iter) && isTile(otherIter)) {
3175  // Both this node and the other node have constant values (tiles).
3176  // Combine the two values and store the result as this node's new tile value.
3177  op(args.setARef(getTile(iter).value)
3178  .setAIsActive(isTileOn(iter))
3179  .setBRef(getTile(otherIter).value)
3180  .setBIsActive(isTileOn(otherIter)));
3181  setTile(iter, Tile(args.result(), args.resultIsActive()));
3182 
3183  } else if (isChild(iter) && isTile(otherIter)) {
3184  // Combine this node's child with the other node's constant value.
3185  ChildT& child = getChild(iter);
3186  child.combine(getTile(otherIter).value, isTileOn(otherIter), op);
3187 
3188  } else if (isTile(iter) && isChild(otherIter)) {
3189  // Combine this node's constant value with the other node's child,
3190  // but use a new functor in which the A and B values are swapped,
3191  // since the constant value is the A value, not the B value.
3193  ChildT& child = getChild(otherIter);
3194  child.combine(getTile(iter).value, isTileOn(iter), swappedOp);
3195 
3196  // Steal the other node's child.
3197  setChild(iter, stealChild(otherIter, Tile()));
3198 
3199  } else /*if (isChild(iter) && isChild(otherIter))*/ {
3200  // Combine this node's child with the other node's child.
3201  ChildT &child = getChild(iter), &otherChild = getChild(otherIter);
3202  child.combine(otherChild, op);
3203  }
3204  if (prune && isChild(iter)) getChild(iter).prune();
3205  }
3206 
3207  // Combine background values.
3208  op(args.setARef(mBackground).setBRef(other.mBackground));
3209  mBackground = args.result();
3210 
3211  // Empty the other tree so as not to leave it in a partially cannibalized state.
3212  other.clear();
3213 }
3214 
3215 
3217 
3218 
3219 // This helper class is a friend of RootNode and is needed so that combine2
3220 // can be specialized for compatible and incompatible pairs of RootNode types.
3221 template<typename CombineOp, typename RootT, typename OtherRootT, bool Compatible = false>
3222 struct RootNodeCombineHelper
3223 {
3224  static inline void combine2(RootT& self, const RootT&, const OtherRootT& other1,
3225  CombineOp&, bool)
3226  {
3227  // If the two root nodes have different configurations or incompatible ValueTypes,
3228  // throw an exception.
3229  self.enforceSameConfiguration(other1);
3230  self.enforceCompatibleValueTypes(other1);
3231  // One of the above two tests should throw, so we should never get here:
3232  std::ostringstream ostr;
3233  ostr << "cannot combine a " << typeid(OtherRootT).name()
3234  << " into a " << typeid(RootT).name();
3235  OPENVDB_THROW(TypeError, ostr.str());
3236  }
3237 };
3238 
3239 // Specialization for root nodes of compatible types
3240 template<typename CombineOp, typename RootT, typename OtherRootT>
3241 struct RootNodeCombineHelper<CombineOp, RootT, OtherRootT, /*Compatible=*/true>
3242 {
3243  static inline void combine2(RootT& self, const RootT& other0, const OtherRootT& other1,
3244  CombineOp& op, bool prune)
3245  {
3246  self.doCombine2(other0, other1, op, prune);
3247  }
3248 };
3249 
3250 
3251 template<typename ChildT>
3252 template<typename CombineOp, typename OtherRootNode>
3253 inline void
3254 RootNode<ChildT>::combine2(const RootNode& other0, const OtherRootNode& other1,
3255  CombineOp& op, bool prune)
3256 {
3257  typedef typename OtherRootNode::ValueType OtherValueType;
3258  static const bool compatible = (SameConfiguration<OtherRootNode>::value
3259  && CanConvertType</*from=*/OtherValueType, /*to=*/ValueType>::value);
3261  *this, other0, other1, op, prune);
3262 }
3263 
3264 
3265 template<typename ChildT>
3266 template<typename CombineOp, typename OtherRootNode>
3267 inline void
3268 RootNode<ChildT>::doCombine2(const RootNode& other0, const OtherRootNode& other1,
3269  CombineOp& op, bool prune)
3270 {
3271  enforceSameConfiguration(other1);
3272 
3273  typedef typename OtherRootNode::ValueType OtherValueT;
3274  typedef typename OtherRootNode::Tile OtherTileT;
3275  typedef typename OtherRootNode::NodeStruct OtherNodeStructT;
3276  typedef typename OtherRootNode::MapCIter OtherMapCIterT;
3277 
3279 
3280  CoordSet keys;
3281  other0.insertKeys(keys);
3282  other1.insertKeys(keys);
3283 
3284  const NodeStruct bg0(Tile(other0.mBackground, /*active=*/false));
3285  const OtherNodeStructT bg1(OtherTileT(other1.mBackground, /*active=*/false));
3286 
3287  for (CoordSetCIter i = keys.begin(), e = keys.end(); i != e; ++i) {
3288  MapIter thisIter = this->findOrAddCoord(*i);
3289  MapCIter iter0 = other0.findKey(*i);
3290  OtherMapCIterT iter1 = other1.findKey(*i);
3291  const NodeStruct& ns0 = (iter0 != other0.mTable.end()) ? iter0->second : bg0;
3292  const OtherNodeStructT& ns1 = (iter1 != other1.mTable.end()) ? iter1->second : bg1;
3293  if (ns0.isTile() && ns1.isTile()) {
3294  // Both input nodes have constant values (tiles).
3295  // Combine the two values and add a new tile to this node with the result.
3296  op(args.setARef(ns0.tile.value)
3297  .setAIsActive(ns0.isTileOn())
3298  .setBRef(ns1.tile.value)
3299  .setBIsActive(ns1.isTileOn()));
3300  setTile(thisIter, Tile(args.result(), args.resultIsActive()));
3301  } else {
3302  if (!isChild(thisIter)) {
3303  // Add a new child with the same coordinates, etc. as the other node's child.
3304  const Coord& childOrigin =
3305  ns0.isChild() ? ns0.child->origin() : ns1.child->origin();
3306  setChild(thisIter, *(new ChildT(childOrigin, getTile(thisIter).value)));
3307  }
3308  ChildT& child = getChild(thisIter);
3309 
3310  if (ns0.isTile()) {
3311  // Combine node1's child with node0's constant value
3312  // and write the result into this node's child.
3313  child.combine2(ns0.tile.value, *ns1.child, ns0.isTileOn(), op);
3314  } else if (ns1.isTile()) {
3315  // Combine node0's child with node1's constant value
3316  // and write the result into this node's child.
3317  child.combine2(*ns0.child, ns1.tile.value, ns1.isTileOn(), op);
3318  } else {
3319  // Combine node0's child with node1's child
3320  // and write the result into this node's child.
3321  child.combine2(*ns0.child, *ns1.child, op);
3322  }
3323  }
3324  if (prune && isChild(thisIter)) getChild(thisIter).prune();
3325  }
3326 
3327  // Combine background values.
3328  op(args.setARef(other0.mBackground).setBRef(other1.mBackground));
3329  mBackground = args.result();
3330 }
3331 
3332 
3334 
3335 
3336 template<typename ChildT>
3337 template<typename BBoxOp>
3338 inline void
3340 {
3341  const bool descent = op.template descent<LEVEL>();
3342  for (MapCIter i = mTable.begin(), e = mTable.end(); i != e; ++i) {
3343  if (this->isTileOff(i)) continue;
3344  if (this->isChild(i) && descent) {
3345  this->getChild(i).visitActiveBBox(op);
3346  } else {
3347 #ifdef _MSC_VER
3348  op.operator()<LEVEL>(CoordBBox::createCube(i->first, ChildT::DIM));
3349 #else
3350  op.template operator()<LEVEL>(CoordBBox::createCube(i->first, ChildT::DIM));
3351 #endif
3352  }
3353  }
3354 }
3355 
3356 
3357 template<typename ChildT>
3358 template<typename VisitorOp>
3359 inline void
3361 {
3362  doVisit<RootNode, VisitorOp, ChildAllIter>(*this, op);
3363 }
3364 
3365 
3366 template<typename ChildT>
3367 template<typename VisitorOp>
3368 inline void
3369 RootNode<ChildT>::visit(VisitorOp& op) const
3370 {
3371  doVisit<const RootNode, VisitorOp, ChildAllCIter>(*this, op);
3372 }
3373 
3374 
3375 template<typename ChildT>
3376 template<typename RootNodeT, typename VisitorOp, typename ChildAllIterT>
3377 inline void
3378 RootNode<ChildT>::doVisit(RootNodeT& self, VisitorOp& op)
3379 {
3380  typename RootNodeT::ValueType val;
3381  for (ChildAllIterT iter = self.beginChildAll(); iter; ++iter) {
3382  if (op(iter)) continue;
3383  if (typename ChildAllIterT::ChildNodeType* child = iter.probeChild(val)) {
3384  child->visit(op);
3385  }
3386  }
3387 }
3388 
3389 
3391 
3392 
3393 template<typename ChildT>
3394 template<typename OtherRootNodeType, typename VisitorOp>
3395 inline void
3396 RootNode<ChildT>::visit2(OtherRootNodeType& other, VisitorOp& op)
3397 {
3398  doVisit2<RootNode, OtherRootNodeType, VisitorOp, ChildAllIter,
3399  typename OtherRootNodeType::ChildAllIter>(*this, other, op);
3400 }
3401 
3402 
3403 template<typename ChildT>
3404 template<typename OtherRootNodeType, typename VisitorOp>
3405 inline void
3406 RootNode<ChildT>::visit2(OtherRootNodeType& other, VisitorOp& op) const
3407 {
3408  doVisit2<const RootNode, OtherRootNodeType, VisitorOp, ChildAllCIter,
3409  typename OtherRootNodeType::ChildAllCIter>(*this, other, op);
3410 }
3411 
3412 
3413 template<typename ChildT>
3414 template<
3415  typename RootNodeT,
3416  typename OtherRootNodeT,
3417  typename VisitorOp,
3418  typename ChildAllIterT,
3419  typename OtherChildAllIterT>
3420 inline void
3421 RootNode<ChildT>::doVisit2(RootNodeT& self, OtherRootNodeT& other, VisitorOp& op)
3422 {
3423  enforceSameConfiguration(other);
3424 
3425  typename RootNodeT::ValueType val;
3426  typename OtherRootNodeT::ValueType otherVal;
3427 
3428  // The two nodes are required to have corresponding table entries,
3429  // but since that might require background tiles to be added to one or both,
3430  // and the nodes might be const, we operate on shallow copies of the nodes instead.
3431  RootNodeT copyOfSelf(self.mBackground);
3432  copyOfSelf.mTable = self.mTable;
3433  OtherRootNodeT copyOfOther(other.mBackground);
3434  copyOfOther.mTable = other.mTable;
3435 
3436  // Add background tiles to both nodes as needed.
3437  CoordSet keys;
3438  self.insertKeys(keys);
3439  other.insertKeys(keys);
3440  for (CoordSetCIter i = keys.begin(), e = keys.end(); i != e; ++i) {
3441  copyOfSelf.findOrAddCoord(*i);
3442  copyOfOther.findOrAddCoord(*i);
3443  }
3444 
3445  ChildAllIterT iter = copyOfSelf.beginChildAll();
3446  OtherChildAllIterT otherIter = copyOfOther.beginChildAll();
3447 
3448  for ( ; iter && otherIter; ++iter, ++otherIter)
3449  {
3450  const size_t skipBranch = static_cast<size_t>(op(iter, otherIter));
3451 
3452  typename ChildAllIterT::ChildNodeType* child =
3453  (skipBranch & 1U) ? nullptr : iter.probeChild(val);
3454  typename OtherChildAllIterT::ChildNodeType* otherChild =
3455  (skipBranch & 2U) ? nullptr : otherIter.probeChild(otherVal);
3456 
3457  if (child != nullptr && otherChild != nullptr) {
3458  child->visit2Node(*otherChild, op);
3459  } else if (child != nullptr) {
3460  child->visit2(otherIter, op);
3461  } else if (otherChild != nullptr) {
3462  otherChild->visit2(iter, op, /*otherIsLHS=*/true);
3463  }
3464  }
3465  // Remove any background tiles that were added above,
3466  // as well as any that were created by the visitors.
3467  copyOfSelf.eraseBackgroundTiles();
3468  copyOfOther.eraseBackgroundTiles();
3469 
3470  // If either input node is non-const, replace its table with
3471  // the (possibly modified) copy.
3472  self.resetTable(copyOfSelf.mTable);
3473  other.resetTable(copyOfOther.mTable);
3474 }
3475 
3476 } // namespace tree
3477 } // namespace OPENVDB_VERSION_NAME
3478 } // namespace openvdb
3479 
3480 #endif // OPENVDB_TREE_ROOTNODE_HAS_BEEN_INCLUDED
3481 
3482 // Copyright (c) 2012-2017 DreamWorks Animation LLC
3483 // All rights reserved. This software is distributed under the
3484 // Mozilla Public License 2.0 ( http://www.mozilla.org/MPL/2.0/ )
static void copyWithValueConversion(RootT &self, const OtherRootT &other)
Definition: RootNode.h:1119
void modifyValue(const Coord &xyz, const ModifyOp &op)
Apply a functor to the value of the voxel at the given coordinates and mark the voxel as active...
Definition: RootNode.h:1921
bool hasSameTopology(const RootNode< OtherChildType > &other) const
Return true if the given tree has the same node and active value topology as this tree (but possibly ...
Definition: RootNode.h:1361
Tag dispatch class that distinguishes topology copy constructors from deep copy constructors.
Definition: Types.h:503
bool isValueOn(const Coord &xyz) const
Definition: RootNode.h:1652
bool isApproxEqual(const Type &a, const Type &b)
Return true if a is equal to b to within the default floating-point comparison tolerance.
Definition: Math.h:370
void addLeaf(LeafNodeType *leaf)
Add the given leaf node to this tree, creating a new branch if necessary. If a leaf node with the sam...
Definition: RootNode.h:2535
Definition: Types.h:317
uint32_t Index32
Definition: Types.h:55
void visit2(OtherRootNodeType &other, VisitorOp &)
Definition: RootNode.h:3396
const ValueType & getValue(const Coord &xyz) const
Definition: RootNode.h:1684
ChildOffCIter beginChildOff() const
Definition: RootNode.h:415
Index getHeight() const
Definition: RootNode.h:492
RootNode(const RootNode &other)
Definition: RootNode.h:111
LeafNodeType * touchLeafAndCache(const Coord &xyz, AccessorT &acc)
Same as touchLeaf() but, if necessary, update the given accessor with pointers to the nodes along the...
ValueAllCIter cbeginValueAll() const
Definition: RootNode.h:423
void clip(const CoordBBox &)
Set all voxels that lie outside the given axis-aligned box to the background.
Definition: RootNode.h:2455
General-purpose arithmetic and comparison routines, most of which accept arbitrary value types (or at...
Index getDepth() const
Definition: RootNode.h:493
T truncateRealToHalf(const T &val)
Return the given value truncated to 16-bit float precision.
Definition: Compression.h:153
NodeChain< RootNode, LEVEL >::Type NodeChainType
NodeChainType is a list of this tree&#39;s node types, from LeafNodeType to RootNode. ...
Definition: RootNode.h:86
size_t eraseBackgroundTiles()
Remove all background tiles.
Definition: RootNode.h:1271
ValueOnCIter cbeginValueOn() const
Definition: RootNode.h:421
ValueIter< const RootNode, MapCIter, ValueOffPred, const ValueType > ValueOffCIter
Definition: RootNode.h:406
boost::mpl::vector< typename HeadT::ChildNodeType, HeadT >::type Type
Definition: RootNode.h:1026
void denseFill(const CoordBBox &bbox, const ValueType &value, bool active=true)
Set all voxels within a given axis-aligned box to a constant value and ensure that those voxels are a...
Definition: RootNode.h:2141
static void combine2(RootT &self, const RootT &other0, const OtherRootT &other1, CombineOp &op, bool prune)
Definition: RootNode.h:3243
OPENVDB_API void setGridBackgroundValuePtr(std::ios_base &, const void *background)
Specify (a pointer to) the background value of the grid currently being read from or written to the g...
ChildAllIter beginChildAll()
Definition: RootNode.h:419
Mat3< typename promote< T0, T1 >::type > operator*(const Mat3< T0 > &m0, const Mat3< T1 > &m1)
Matrix multiplication.
Definition: Mat3.h:654
static void getNodeLog2Dims(std::vector< Index > &dims)
Definition: RootNode.h:1324
Definition: Types.h:462
const ValueType & getValueAndCache(const Coord &xyz, AccessorT &) const
ValueOffCIter beginValueOff() const
Definition: RootNode.h:425
void getNodes(ArrayT &array)
Adds all nodes of a certain type to a container with the following API:
Definition: RootNode.h:2855
size_t numBackgroundTiles() const
Return the number of background tiles.
Definition: RootNode.h:1259
#define OPENVDB_THROW(exception, message)
Definition: Exceptions.h:101
bool operator==(const Vec3< T0 > &v0, const Vec3< T1 > &v1)
Equality operator, does exact floating point comparisons.
Definition: Vec3.h:502
BOOST_STATIC_ASSERT(boost::mpl::size< NodeChainType >::value==LEVEL+1)
void setValueAndCache(const Coord &xyz, const ValueType &value, AccessorT &)
Definition: RootNode.h:1857
void readBuffers(std::istream &, bool fromHalf=false)
Definition: RootNode.h:2421
bool resultIsActive() const
Definition: Types.h:435
ValueIter< const RootNode, MapCIter, ChildOffPred, ValueType > ChildOffCIter
Definition: RootNode.h:399
T zeroVal()
Return the value of type T that corresponds to zero.
Definition: Math.h:94
bool readTopology(std::istream &, bool fromHalf=false)
Definition: RootNode.h:2297
void setValueOn(const Coord &xyz, const ValueType &value)
Set the value of the voxel at the given coordinates and mark the voxel as active. ...
Definition: RootNode.h:1838
ValueOffCIter cbeginValueOff() const
Definition: RootNode.h:422
bool operator!=(const Vec3< T0 > &v0, const Vec3< T1 > &v1)
Inequality operator, does exact floating point comparisons.
Definition: Vec3.h:510
CombineArgs & setARef(const AValueType &a)
Redirect the A value to a new external source.
Definition: Types.h:424
void copyToDense(const CoordBBox &bbox, DenseT &dense) const
Copy into a dense grid the values of all voxels, both active and inactive, that intersect a given bou...
Definition: RootNode.h:2217
const LeafNodeType * probeConstLeaf(const Coord &xyz) const
Return a pointer to the leaf node that contains voxel (x, y, z). If no such node exists, return nullptr.
Definition: RootNode.h:2779
ChildType::BuildType BuildType
Definition: RootNode.h:81
static CoordBBox getNodeBoundingBox()
Return the bounding box of this RootNode, i.e., an infinite bounding box.
Definition: RootNode.h:442
void addTile(const Coord &xyz, const ValueType &value, bool state)
Add a tile containing voxel (x, y, z) at the root level, deleting the existing branch if necessary...
Definition: RootNode.h:2604
Index32 FindHighestOn(Index32 v)
Return the most significant on bit of the given 32-bit value.
Definition: NodeMasks.h:156
ValueIter< RootNode, MapIter, ChildOffPred, const ValueType > ChildOffIter
Definition: RootNode.h:398
ChildAllCIter beginChildAll() const
Definition: RootNode.h:416
ChildType::ValueType ValueType
Definition: RootNode.h:80
void prune(const ValueType &tolerance=zeroVal< ValueType >())
Reduce the memory footprint of this tree by replacing with tiles any nodes whose values are all the s...
Definition: RootNode.h:2495
RootNode & operator=(const RootNode &other)
Copy a root node of the same type as this node.
Definition: RootNode.h:1175
static void combine2(RootT &self, const RootT &, const OtherRootT &other1, CombineOp &, bool)
Definition: RootNode.h:3224
ChildIter< RootNode, MapIter, ChildOnPred, ChildType > ChildOnIter
Definition: RootNode.h:396
int getValueDepthAndCache(const Coord &xyz, AccessorT &) const
Definition: RootNode.h:1718
void topologyDifference(const RootNode< OtherChildType > &other)
Difference this tree&#39;s set of active values with the active values of the other tree, whose ValueType may be different. So a resulting voxel will be active only if the original voxel is active in this tree and inactive in the other tree.
Definition: RootNode.h:3125
ChildOnCIter cbeginChildOn() const
Definition: RootNode.h:411
ChildOnIter beginChildOn()
Definition: RootNode.h:417
ChildType::LeafNodeType LeafNodeType
Definition: RootNode.h:79
ChildOffIter beginChildOff()
Definition: RootNode.h:418
void prune(TreeT &tree, typename TreeT::ValueType tolerance=zeroVal< typename TreeT::ValueType >(), bool threaded=true, size_t grainSize=1)
Reduce the memory footprint of a tree by replacing with tiles any nodes whose values are all the same...
Definition: Prune.h:361
void combine2(const RootNode &other0, const OtherRootNode &other1, CombineOp &op, bool prune=false)
Definition: RootNode.h:3254
void voxelizeActiveTiles(bool threaded=true)
Densify active tiles, i.e., replace them with leaf-level active voxels.
Definition: RootNode.h:2191
NodeT * probeNodeAndCache(const Coord &xyz, AccessorT &acc)
Same as probeNode() but, if necessary, update the given accessor with pointers to the nodes along the...
Definition: RootNode.h:2815
int getValueDepth(const Coord &xyz) const
Return the tree depth (0 = root) at which the value of voxel (x, y, z) resides.
Definition: RootNode.h:1708
Index getWidth() const
Definition: RootNode.h:491
NodeT * stealNode(const Coord &xyz, const ValueType &value, bool state)
Return a pointer to the node of type NodeT that contains voxel (x, y, z) and replace it with a tile o...
Definition: RootNode.h:2516
~RootNode()
Definition: RootNode.h:159
bool probeValue(const Coord &xyz, ValueType &value) const
Definition: RootNode.h:2047
static bool hasSameConfiguration(const RootNode< OtherChildType > &other)
Return false if the other node&#39;s dimensions don&#39;t match this node&#39;s.
Definition: RootNode.h:1408
NodeChain< typename HeadT::ChildNodeType, HeadLevel-1 >::Type SubtreeT
Definition: RootNode.h:1019
bool isBackgroundTile(const Tile &) const
Return true if the given tile is inactive and has the background value.
Definition: RootNode.h:1237
NodeChain<RootNodeType, RootNodeType::LEVEL>::Type is a boost::mpl::vector that lists the types of th...
Definition: RootNode.h:68
DenseIter< RootNode, MapIter, ChildType, ValueType > ChildAllIter
Definition: RootNode.h:400
ValueConverter<T>::Type is the type of a RootNode having the same child hierarchy as this node but a ...
Definition: RootNode.h:92
const boost::disable_if_c< VecTraits< T >::IsVec, T >::type & min(const T &a, const T &b)
Definition: Composite.h:128
#define OPENVDB_VERSION_NAME
Definition: version.h:43
ChildOnCIter beginChildOn() const
Definition: RootNode.h:414
ValueAllIter beginValueAll()
Definition: RootNode.h:429
SameConfiguration<OtherNodeType>::value is true if and only if OtherNodeType is the type of a RootNod...
Definition: RootNode.h:100
void setValueOff(const Coord &xyz)
Mark the voxel at the given coordinates as inactive but don&#39;t change its value.
Definition: RootNode.h:1730
void combine(RootNode &other, CombineOp &, bool prune=false)
Definition: RootNode.h:3164
Index32 leafCount() const
Definition: RootNode.h:1553
DenseIter< const RootNode, MapCIter, const ChildType, const ValueType > ChildAllCIter
Definition: RootNode.h:401
void stealNodes(ArrayT &array)
Steals all nodes of a certain type from the tree and adds them to a container with the following API:...
Definition: RootNode.h:838
Definition: Exceptions.h:91
void fill(const CoordBBox &bbox, const ValueType &value, bool active=true)
Set all voxels within a given axis-aligned box to a constant value.
Definition: RootNode.h:2083
ValueOnIter beginValueOn()
Definition: RootNode.h:427
bool empty() const
Return true if this node&#39;s table is either empty or contains only background tiles.
Definition: RootNode.h:477
void stealNodes(ArrayT &array, const ValueType &value, bool state)
Steals all nodes of a certain type from the tree and adds them to a container with the following API:...
Definition: RootNode.h:2910
GridType::Ptr clip(const GridType &grid, const BBoxd &bbox, bool keepInterior=true)
Clip the given grid against a world-space bounding box and return a new grid containing the result...
Definition: Clip.h:367
This struct collects both input and output arguments to "grid combiner" functors used with the tree::...
Definition: Types.h:371
Coord getMinIndex() const
Return the smallest index of the current tree.
Definition: RootNode.h:1333
LeafNodeType * probeLeafAndCache(const Coord &xyz, AccessorT &acc)
Same as probeLeaf() but, if necessary, update the given accessor with pointers to the nodes along the...
void addLeafAndCache(LeafNodeType *leaf, AccessorT &)
Same as addLeaf() but, if necessary, update the given accessor with pointers to the nodes along the p...
Definition: RootNode.h:2570
Index64 offVoxelCount() const
Definition: RootNode.h:1595
void setActiveStateAndCache(const Coord &xyz, bool on, AccessorT &)
Definition: RootNode.h:1767
void topologyIntersection(const RootNode< OtherChildType > &other)
Intersects this tree&#39;s set of active values with the active values of the other tree, whose ValueType may be different.
Definition: RootNode.h:3089
void modifyValueAndCache(const Coord &xyz, const ModifyOp &op, AccessorT &)
Definition: RootNode.h:1953
ValueIter< RootNode, MapIter, ValueAllPred, ValueType > ValueAllIter
Definition: RootNode.h:407
Definition: Exceptions.h:39
Index64 onVoxelCount() const
Definition: RootNode.h:1579
#define OPENVDB_NO_UNREACHABLE_CODE_WARNING_BEGIN
Definition: Platform.h:129
void modifyValueAndActiveState(const Coord &xyz, const ModifyOp &op)
Apply a functor to the voxel at the given coordinates.
Definition: RootNode.h:1989
void setValueOffAndCache(const Coord &xyz, const ValueType &value, AccessorT &)
Definition: RootNode.h:1814
ChildOffCIter cbeginChildOff() const
Definition: RootNode.h:412
const ValueType & background() const
Return this node&#39;s background value.
Definition: RootNode.h:459
RootNode()
Construct a new tree with a background value of 0.
Definition: RootNode.h:1053
ValueIter< const RootNode, MapCIter, ValueOnPred, const ValueType > ValueOnCIter
Definition: RootNode.h:404
NodeT * probeNode(const Coord &xyz)
Return a pointer to the node that contains voxel (x, y, z). If no such node exists, return nullptr.
Definition: RootNode.h:2736
void clear()
Definition: RootNode.h:1481
void evalActiveBoundingBox(CoordBBox &bbox, bool visitVoxels=true) const
Expand the specified bbox so it includes the active tiles of this root node as well as all the active...
Definition: RootNode.h:1492
void load(std::istream &is)
Definition: NodeMasks.h:1382
LeafNodeType * touchLeaf(const Coord &xyz)
Return a pointer to the leaf node that contains voxel (x, y, z). If no such node exists, create one that preserves the values and active states of all voxels.
Definition: RootNode.h:2692
const AValueType & result() const
Get the output value.
Definition: Types.h:416
LeafNodeType * probeLeaf(const Coord &xyz)
Return a pointer to the leaf node that contains voxel (x, y, z). If no such node exists, return nullptr.
Definition: RootNode.h:2771
ChildAllCIter cbeginChildAll() const
Definition: RootNode.h:413
void setValueOnly(const Coord &xyz, const ValueType &value)
Set the value of the voxel at the given coordinates but don&#39;t change its active state.
Definition: RootNode.h:1879
ValueAllCIter beginValueAll() const
Definition: RootNode.h:426
ChildIter< const RootNode, MapCIter, ChildOnPred, const ChildType > ChildOnCIter
Definition: RootNode.h:397
Definition: RootNode.h:69
RootNode(const RootNode< OtherChildType > &other)
Construct a new tree that reproduces the topology and active states of a tree of a different ValueTyp...
Definition: RootNode.h:120
Definition: Exceptions.h:92
const NodeT * probeConstNode(const Coord &xyz) const
Return a pointer to the node that contains voxel (x, y, z). If no such node exists, return nullptr.
Definition: RootNode.h:2754
Index64 onTileCount() const
Definition: RootNode.h:1634
Index getTableSize() const
Return the number of entries in this node&#39;s table.
Definition: RootNode.h:489
uint64_t Index64
Definition: Types.h:56
bool hasActiveTiles() const
Return true if this root node, or any of its child nodes, have active tiles.
Definition: RootNode.h:1661
void copyToDense(const GridOrTreeT &sparse, DenseT &dense, bool serial=false)
Populate a dense grid with the values of voxels from a sparse grid, where the sparse grid intersects ...
Definition: Dense.h:445
static void copyWithValueConversion(RootT &self, const OtherRootT &other)
Definition: RootNode.h:1137
void writeBuffers(std::ostream &, bool toHalf=false) const
Definition: RootNode.h:2411
Index64 onLeafVoxelCount() const
Definition: RootNode.h:1611
void setActiveState(const Coord &xyz, bool on)
Set the active state of the voxel at the given coordinates but don&#39;t change its value.
Definition: RootNode.h:1744
#define OPENVDB_NO_UNREACHABLE_CODE_WARNING_END
Definition: Platform.h:130
void topologyUnion(const RootNode< OtherChildType > &other)
Union this tree&#39;s set of active values with the active values of the other tree, whose ValueType may ...
Definition: RootNode.h:3053
bool isExactlyEqual(const T0 &a, const T1 &b)
Return true if a is exactly equal to b.
Definition: Math.h:407
OPENVDB_API uint32_t getFormatVersion(std::ios_base &)
Return the file format version number associated with the given input stream.
static Index getLevel()
Definition: RootNode.h:484
Index64 offLeafVoxelCount() const
Definition: RootNode.h:1623
T negative(const T &val)
Return the unary negation of the given value.
Definition: Math.h:116
boost::mpl::push_back< SubtreeT, HeadT >::type Type
Definition: RootNode.h:1020
bool expand(const Coord &xyz)
Expand this node&#39;s table so that (x, y, z) is included in the index range.
Definition: RootNode.h:1310
bool isValueOnAndCache(const Coord &xyz, AccessorT &) const
Definition: RootNode.h:1672
ValueOffIter beginValueOff()
Definition: RootNode.h:428
CanConvertType<FromType, ToType>::value is true if a value of type ToType can be constructed from a v...
Definition: Types.h:233
ValueIter< const RootNode, MapCIter, ValueAllPred, const ValueType > ValueAllCIter
Definition: RootNode.h:408
ValueOnCIter beginValueOn() const
Definition: RootNode.h:424
bool probeValueAndCache(const Coord &xyz, ValueType &value, AccessorT &) const
Definition: RootNode.h:2063
ValueIter< RootNode, MapIter, ValueOnPred, ValueType > ValueOnIter
Definition: RootNode.h:403
Tag dispatch class that distinguishes constructors during file input.
Definition: Types.h:505
ChildType ChildNodeType
Definition: RootNode.h:78
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:71
Definition: RootNode.h:75
void sparseFill(const CoordBBox &bbox, const ValueType &value, bool active=true)
Set all voxels within a given axis-aligned box to a constant value.
Definition: RootNode.h:566
static bool hasCompatibleValueType(const RootNode< OtherChildType > &other)
Definition: RootNode.h:1440
RootNode< typename ChildType::template ValueConverter< OtherValueType >::Type > Type
Definition: RootNode.h:93
int32_t Int32
Definition: Types.h:59
Index32 Index
Definition: Types.h:57
Index32 nonLeafCount() const
Definition: RootNode.h:1565
void visit(VisitorOp &)
Definition: RootNode.h:3360
Vec2< T > minComponent(const Vec2< T > &v1, const Vec2< T > &v2)
Return component-wise minimum of the two vectors.
Definition: Vec2.h:553
void getIndexRange(CoordBBox &bbox) const
Return the current index range. Both min and max are inclusive.
Definition: RootNode.h:1348
const NodeT * probeConstNodeAndCache(const Coord &xyz, AccessorT &acc) const
Same as probeNode() but, if necessary, update the given accessor with pointers to the nodes along the...
Definition: RootNode.h:2834
static Index getChildDim()
Definition: RootNode.h:486
void setBackground(const ValueType &value, bool updateChildNodes)
Change inactive tiles or voxels with a value equal to +/- the old background to the specified value (...
Definition: RootNode.h:1210
Index64 memUsage() const
Return the total amount of memory in bytes occupied by this node and its children.
Definition: RootNode.h:1467
void addTileAndCache(Index level, const Coord &xyz, const ValueType &, bool state, AccessorT &)
Same as addTile() but, if necessary, update the given accessor with pointers to the nodes along the p...
Definition: RootNode.h:2651
void merge(RootNode &other)
Efficiently merge another tree into this tree using one of several schemes.
Definition: RootNode.h:2941
void modifyValueAndActiveStateAndCache(const Coord &xyz, const ModifyOp &op, AccessorT &)
Definition: RootNode.h:2016
Coord getMaxIndex() const
Return the largest index of the current tree.
Definition: RootNode.h:1340
const LeafNodeType * probeConstLeafAndCache(const Coord &xyz, AccessorT &acc) const
Same as probeLeaf() but, if necessary, update the given accessor with pointers to the nodes along the...
bool isOn(Index32 i) const
Definition: NodeMasks.h:1341
void visitActiveBBox(BBoxOp &) const
Call the templated functor BBoxOp with bounding box information for all active tiles and leaf nodes i...
Definition: RootNode.h:3339
ValueIter< RootNode, MapIter, ValueOffPred, ValueType > ValueOffIter
Definition: RootNode.h:405
bool writeTopology(std::ostream &, bool toHalf=false) const
Definition: RootNode.h:2261
Definition: NodeMasks.h:1076
void setValueOnlyAndCache(const Coord &xyz, const ValueType &value, AccessorT &)
Definition: RootNode.h:1898