OpenVDB  3.0.0
LeafNodeBool.h
Go to the documentation of this file.
1 //
3 // Copyright (c) 2012-2014 DreamWorks Animation LLC
4 //
5 // All rights reserved. This software is distributed under the
6 // Mozilla Public License 2.0 ( http://www.mozilla.org/MPL/2.0/ )
7 //
8 // Redistributions of source code must retain the above copyright
9 // and license notice and the following restrictions and disclaimer.
10 //
11 // * Neither the name of DreamWorks Animation nor the names of
12 // its contributors may be used to endorse or promote products derived
13 // from this software without specific prior written permission.
14 //
15 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY INDIRECT, INCIDENTAL,
20 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 // IN NO EVENT SHALL THE COPYRIGHT HOLDERS' AND CONTRIBUTORS' AGGREGATE
27 // LIABILITY FOR ALL CLAIMS REGARDLESS OF THEIR BASIS EXCEED US$250.00.
28 //
30 
31 #ifndef OPENVDB_TREE_LEAFNODEBOOL_HAS_BEEN_INCLUDED
32 #define OPENVDB_TREE_LEAFNODEBOOL_HAS_BEEN_INCLUDED
33 
34 #include <iostream>
35 #include <boost/shared_ptr.hpp>
36 #include <boost/shared_array.hpp>
37 #include <boost/static_assert.hpp>
38 #include <openvdb/Types.h>
39 #include <openvdb/io/Compression.h> // for io::readData(), etc.
40 #include <openvdb/math/Math.h> // for math::isZero()
41 #include <openvdb/util/NodeMasks.h>
42 #include "LeafNode.h"
43 #include "Iterator.h"
44 
45 
46 namespace openvdb {
48 namespace OPENVDB_VERSION_NAME {
49 namespace tree {
50 
53 template<Index Log2Dim>
54 class LeafNode<bool, Log2Dim>
55 {
56 public:
58  typedef boost::shared_ptr<LeafNodeType> Ptr;
59  typedef bool ValueType;
61 
62  // These static declarations must be on separate lines to avoid VC9 compiler errors.
63  static const Index LOG2DIM = Log2Dim; // needed by parent nodes
64  static const Index TOTAL = Log2Dim; // needed by parent nodes
65  static const Index DIM = 1 << TOTAL; // dimension along one coordinate direction
66  static const Index NUM_VALUES = 1 << 3 * Log2Dim;
67  static const Index NUM_VOXELS = NUM_VALUES; // total number of voxels represented by this node
68  static const Index SIZE = NUM_VALUES;
69  static const Index LEVEL = 0; // level 0 = leaf
70 
73  template<typename ValueType>
74  struct ValueConverter { typedef LeafNode<ValueType, Log2Dim> Type; };
75 
78  template<typename OtherNodeType>
79  struct SameConfiguration {
81  };
82 
83 
84  class Buffer
85  {
86  public:
87  Buffer() {}
88  Buffer(bool on) : mData(on) {}
89  Buffer(const NodeMaskType& other): mData(other) {}
90  Buffer(const Buffer& other): mData(other.mData) {}
91  ~Buffer() {}
92  void fill(bool val) { mData.set(val); }
93  Buffer& operator=(const Buffer& b) { if (&b != this) { mData = b.mData; } return *this; }
94 
95  const bool& getValue(Index i) const
96  {
97  assert(i < SIZE);
98  // We can't use the ternary operator here, otherwise Visual C++ returns
99  // a reference to a temporary.
100  if (mData.isOn(i)) return LeafNode::sOn; else return LeafNode::sOff;
101  }
102  const bool& operator[](Index i) const { return this->getValue(i); }
103 
104  bool operator==(const Buffer& other) const { return mData == other.mData; }
105  bool operator!=(const Buffer& other) const { return mData != other.mData; }
106 
107  void setValue(Index i, bool val) { assert(i < SIZE); mData.set(i, val); }
108 
109  void swap(Buffer& other) { if (&other != this) std::swap(mData, other.mData); }
110 
111  Index memUsage() const { return mData.memUsage(); }
112  static Index size() { return SIZE; }
113 
114  private:
115  friend class ::TestLeaf;
116  // Allow the parent LeafNode to access this Buffer's bit mask.
117  friend class LeafNode;
118 
119  NodeMaskType mData;
120  }; // class Buffer
121 
122 
124  LeafNode();
125 
130  explicit LeafNode(const Coord& xyz, bool value = false, bool active = false);
131 
132 #ifndef OPENVDB_2_ABI_COMPATIBLE
133  LeafNode(PartialCreate, const Coord& xyz, bool value = false, bool active = false);
135 #endif
136 
138  LeafNode(const LeafNode&);
139 
141  template<typename OtherValueType>
142  explicit LeafNode(const LeafNode<OtherValueType, Log2Dim>& other);
143 
145  template<typename ValueType>
147 
149  template<typename ValueType>
152  LeafNode(const LeafNode<ValueType, Log2Dim>& other, bool offValue, bool onValue, TopologyCopy);
153  template<typename ValueType>
154  LeafNode(const LeafNode<ValueType, Log2Dim>& other, bool background, TopologyCopy);
156 
158  ~LeafNode();
159 
160  //
161  // Statistics
162  //
164  static Index log2dim() { return Log2Dim; }
166  static Index dim() { return DIM; }
167  static Index size() { return SIZE; }
168  static Index numValues() { return SIZE; }
169  static Index getLevel() { return LEVEL; }
170  static void getNodeLog2Dims(std::vector<Index>& dims) { dims.push_back(Log2Dim); }
171  static Index getChildDim() { return 1; }
172 
173  static Index32 leafCount() { return 1; }
174  static Index32 nonLeafCount() { return 0; }
175 
177  Index64 onVoxelCount() const { return mValueMask.countOn(); }
179  Index64 offVoxelCount() const { return mValueMask.countOff(); }
180  Index64 onLeafVoxelCount() const { return onVoxelCount(); }
181  Index64 offLeafVoxelCount() const { return offVoxelCount(); }
182  static Index64 onTileCount() { return 0; }
183  static Index64 offTileCount() { return 0; }
184 
186  bool isEmpty() const { return mValueMask.isOff(); }
188  bool isDense() const { return mValueMask.isOn(); }
189 
190 #ifndef OPENVDB_2_ABI_COMPATIBLE
191  bool isAllocated() const { return true; }
198  bool allocate() { return true; }
199 #endif
200 
202  Index64 memUsage() const;
203 
207  void evalActiveBoundingBox(CoordBBox& bbox, bool visitVoxels = true) const;
208 
211  CoordBBox getNodeBoundingBox() const { return CoordBBox::createCube(mOrigin, DIM); }
212 
214  void setOrigin(const Coord& origin) { mOrigin = origin; }
216  const Coord& origin() const { return mOrigin; }
218  void getOrigin(Coord& origin) const { origin = mOrigin; }
219  void getOrigin(Int32& x, Int32& y, Int32& z) const { mOrigin.asXYZ(x, y, z); }
221 
223  static Index coordToOffset(const Coord& xyz);
226  static Coord offsetToLocalCoord(Index n);
228  Coord offsetToGlobalCoord(Index n) const;
229 
231  std::string str() const;
232 
235  template<typename OtherType, Index OtherLog2Dim>
236  bool hasSameTopology(const LeafNode<OtherType, OtherLog2Dim>* other) const;
237 
239  bool operator==(const LeafNode&) const;
240  bool operator!=(const LeafNode&) const;
241 
242  //
243  // Buffer management
244  //
247  void swap(Buffer& other) { mBuffer.swap(other); }
248  const Buffer& buffer() const { return mBuffer; }
249  Buffer& buffer() { return mBuffer; }
250 
251  //
252  // I/O methods
253  //
255  void readTopology(std::istream&, bool fromHalf = false);
257  void writeTopology(std::ostream&, bool toHalf = false) const;
258 
260  void readBuffers(std::istream&, bool fromHalf = false);
261  void readBuffers(std::istream& is, const CoordBBox&, bool fromHalf = false);
263  void writeBuffers(std::ostream&, bool toHalf = false) const;
264 
265  //
266  // Accessor methods
267  //
269  const bool& getValue(const Coord& xyz) const;
271  const bool& getValue(Index offset) const;
272 
276  bool probeValue(const Coord& xyz, bool& val) const;
277 
279  static Index getValueLevel(const Coord&) { return LEVEL; }
280 
282  void setActiveState(const Coord& xyz, bool on);
284  void setActiveState(Index offset, bool on) { assert(offset<SIZE); mValueMask.set(offset, on); }
285 
287  void setValueOnly(const Coord& xyz, bool val);
289  void setValueOnly(Index offset, bool val) { assert(offset<SIZE); mBuffer.setValue(offset,val); }
290 
292  void setValueOff(const Coord& xyz) { mValueMask.setOff(this->coordToOffset(xyz)); }
294  void setValueOff(Index offset) { assert(offset < SIZE); mValueMask.setOff(offset); }
295 
297  void setValueOff(const Coord& xyz, bool val);
299  void setValueOff(Index offset, bool val);
300 
302  void setValueOn(const Coord& xyz) { mValueMask.setOn(this->coordToOffset(xyz)); }
304  void setValueOn(Index offset) { assert(offset < SIZE); mValueMask.setOn(offset); }
305 
307  void setValueOn(const Coord& xyz, bool val);
309  void setValue(const Coord& xyz, bool val) { this->setValueOn(xyz, val); };
311  void setValueOn(Index offset, bool val);
312 
315  template<typename ModifyOp>
316  void modifyValue(Index offset, const ModifyOp& op);
319  template<typename ModifyOp>
320  void modifyValue(const Coord& xyz, const ModifyOp& op);
321 
323  template<typename ModifyOp>
324  void modifyValueAndActiveState(const Coord& xyz, const ModifyOp& op);
325 
327  void setValuesOn() { mValueMask.setOn(); }
329  void setValuesOff() { mValueMask.setOff(); }
330 
332  bool isValueOn(const Coord& xyz) const { return mValueMask.isOn(this->coordToOffset(xyz)); }
334  bool isValueOn(Index offset) const { assert(offset < SIZE); return mValueMask.isOn(offset); }
335 
337  static bool hasActiveTiles() { return false; }
338 
340  void clip(const CoordBBox&, bool background);
341 
343  void fill(const CoordBBox& bbox, bool value, bool active = true);
344 
346  void fill(const bool& value);
348  void fill(const bool& value, bool active);
349 
361  template<typename DenseT>
362  void copyToDense(const CoordBBox& bbox, DenseT& dense) const;
363 
380  template<typename DenseT>
381  void copyFromDense(const CoordBBox& bbox, const DenseT& dense, bool background, bool tolerance);
382 
385  template<typename AccessorT>
386  const bool& getValueAndCache(const Coord& xyz, AccessorT&) const {return this->getValue(xyz);}
387 
390  template<typename AccessorT>
391  bool isValueOnAndCache(const Coord& xyz, AccessorT&) const { return this->isValueOn(xyz); }
392 
395  template<typename AccessorT>
396  void setValueAndCache(const Coord& xyz, bool val, AccessorT&) { this->setValueOn(xyz, val); }
397 
401  template<typename AccessorT>
402  void setValueOnlyAndCache(const Coord& xyz, bool val, AccessorT&) {this->setValueOnly(xyz,val);}
403 
406  template<typename AccessorT>
407  void setValueOffAndCache(const Coord& xyz, bool value, AccessorT&)
408  {
409  this->setValueOff(xyz, value);
410  }
411 
415  template<typename ModifyOp, typename AccessorT>
416  void modifyValueAndCache(const Coord& xyz, const ModifyOp& op, AccessorT&)
417  {
418  this->modifyValue(xyz, op);
419  }
420 
423  template<typename ModifyOp, typename AccessorT>
424  void modifyValueAndActiveStateAndCache(const Coord& xyz, const ModifyOp& op, AccessorT&)
425  {
426  this->modifyValueAndActiveState(xyz, op);
427  }
428 
432  template<typename AccessorT>
433  void setActiveStateAndCache(const Coord& xyz, bool on, AccessorT&)
434  {
435  this->setActiveState(xyz, on);
436  }
437 
441  template<typename AccessorT>
442  bool probeValueAndCache(const Coord& xyz, bool& val, AccessorT&) const
443  {
444  return this->probeValue(xyz, val);
445  }
446 
449  template<typename AccessorT>
450  static Index getValueLevelAndCache(const Coord&, AccessorT&) { return LEVEL; }
451 
455  const bool& getFirstValue() const { if (mValueMask.isOn(0)) return sOn; else return sOff; }
459  const bool& getLastValue() const { if (mValueMask.isOn(SIZE-1)) return sOn; else return sOff; }
460 
464  bool isConstant(bool& constValue, bool& state, bool tolerance = 0) const;
466  bool isInactive() const { return mValueMask.isOff(); }
467 
468  void resetBackground(bool oldBackground, bool newBackground);
469 
470  void negate() { mBuffer.mData.toggle(); }
471 
472  template<MergePolicy Policy>
473  void merge(const LeafNode& other, bool bg = false, bool otherBG = false);
474  template<MergePolicy Policy> void merge(bool tileValue, bool tileActive);
475 
477 
484  template<typename OtherType>
485  void topologyUnion(const LeafNode<OtherType, Log2Dim>& other);
486 
498  template<typename OtherType>
499  void topologyIntersection(const LeafNode<OtherType, Log2Dim>& other, const bool&);
500 
512  template<typename OtherType>
513  void topologyDifference(const LeafNode<OtherType, Log2Dim>& other, const bool&);
514 
515  template<typename CombineOp>
516  void combine(const LeafNode& other, CombineOp& op);
517  template<typename CombineOp>
518  void combine(bool, bool valueIsActive, CombineOp& op);
519 
520  template<typename CombineOp, typename OtherType /*= bool*/>
521  void combine2(const LeafNode& other, const OtherType&, bool valueIsActive, CombineOp&);
522  template<typename CombineOp, typename OtherNodeT /*= LeafNode*/>
523  void combine2(bool, const OtherNodeT& other, bool valueIsActive, CombineOp&);
524  template<typename CombineOp, typename OtherNodeT /*= LeafNode*/>
525  void combine2(const LeafNode& b0, const OtherNodeT& b1, CombineOp&);
526 
531  template<typename BBoxOp> void visitActiveBBox(BBoxOp&) const;
532 
533  template<typename VisitorOp> void visit(VisitorOp&);
534  template<typename VisitorOp> void visit(VisitorOp&) const;
535 
536  template<typename OtherLeafNodeType, typename VisitorOp>
537  void visit2Node(OtherLeafNodeType& other, VisitorOp&);
538  template<typename OtherLeafNodeType, typename VisitorOp>
539  void visit2Node(OtherLeafNodeType& other, VisitorOp&) const;
540  template<typename IterT, typename VisitorOp>
541  void visit2(IterT& otherIter, VisitorOp&, bool otherIsLHS = false);
542  template<typename IterT, typename VisitorOp>
543  void visit2(IterT& otherIter, VisitorOp&, bool otherIsLHS = false) const;
544 
546  void prune(const ValueType& /*tolerance*/ = zeroVal<ValueType>()) {}
548  void addLeaf(LeafNode*) {}
549  template<typename AccessorT>
550  void addLeafAndCache(LeafNode*, AccessorT&) {}
551  template<typename NodeT>
552  NodeT* stealNode(const Coord&, const ValueType&, bool) { return NULL; }
553  template<typename NodeT>
554  NodeT* probeNode(const Coord&) { return NULL; }
555  template<typename NodeT>
556  const NodeT* probeConstNode(const Coord&) const { return NULL; }
557  template<typename ArrayT> void getNodes(ArrayT&) const {}
559 
560  void addTile(Index level, const Coord&, bool val, bool active);
561  void addTile(Index offset, bool val, bool active);
562  template<typename AccessorT>
563  void addTileAndCache(Index level, const Coord&, bool val, bool active, AccessorT&);
564 
566  LeafNode* touchLeaf(const Coord&) { return this; }
568  template<typename AccessorT>
569  LeafNode* touchLeafAndCache(const Coord&, AccessorT&) { return this; }
570  LeafNode* probeLeaf(const Coord&) { return this; }
571  template<typename AccessorT>
572  LeafNode* probeLeafAndCache(const Coord&, AccessorT&) { return this; }
573  template<typename NodeT, typename AccessorT>
574  NodeT* probeNodeAndCache(const Coord&, AccessorT&)
575  {
577  if (!(boost::is_same<NodeT,LeafNode>::value)) return NULL;
578  return reinterpret_cast<NodeT*>(this);
580  }
582 
583  const LeafNode* probeLeaf(const Coord&) const { return this; }
585  template<typename AccessorT>
586  const LeafNode* probeLeafAndCache(const Coord&, AccessorT&) const { return this; }
587  const LeafNode* probeConstLeaf(const Coord&) const { return this; }
588  template<typename AccessorT>
589  const LeafNode* probeConstLeafAndCache(const Coord&, AccessorT&) const { return this; }
590  template<typename NodeT, typename AccessorT>
591  const NodeT* probeConstNodeAndCache(const Coord&, AccessorT&) const
592  {
594  if (!(boost::is_same<NodeT,LeafNode>::value)) return NULL;
595  return reinterpret_cast<const NodeT*>(this);
597  }
599 
600  //
601  // Iterators
602  //
603 protected:
607 
608  template<typename MaskIterT, typename NodeT, typename ValueT>
609  struct ValueIter:
610  // Derives from SparseIteratorBase, but can also be used as a dense iterator,
611  // if MaskIterT is a dense mask iterator type.
612  public SparseIteratorBase<MaskIterT, ValueIter<MaskIterT, NodeT, ValueT>, NodeT, ValueT>
613  {
615 
617  ValueIter(const MaskIterT& iter, NodeT* parent): BaseT(iter, parent) {}
618 
619  const bool& getItem(Index pos) const { return this->parent().getValue(pos); }
620  const bool& getValue() const { return this->getItem(this->pos()); }
621 
622  // Note: setItem() can't be called on const iterators.
623  void setItem(Index pos, bool value) const { this->parent().setValueOnly(pos, value); }
624  // Note: setValue() can't be called on const iterators.
625  void setValue(bool value) const { this->setItem(this->pos(), value); }
626 
627  // Note: modifyItem() can't be called on const iterators.
628  template<typename ModifyOp>
629  void modifyItem(Index n, const ModifyOp& op) const { this->parent().modifyValue(n, op); }
630  // Note: modifyValue() can't be called on const iterators.
631  template<typename ModifyOp>
632  void modifyValue(const ModifyOp& op) const { this->modifyItem(this->pos(), op); }
633  };
634 
636  template<typename MaskIterT, typename NodeT>
637  struct ChildIter:
638  public SparseIteratorBase<MaskIterT, ChildIter<MaskIterT, NodeT>, NodeT, bool>
639  {
641  ChildIter(const MaskIterT& iter, NodeT* parent): SparseIteratorBase<
642  MaskIterT, ChildIter<MaskIterT, NodeT>, NodeT, bool>(iter, parent) {}
643  };
644 
645  template<typename NodeT, typename ValueT>
646  struct DenseIter: public DenseIteratorBase<
647  MaskDenseIter, DenseIter<NodeT, ValueT>, NodeT, /*ChildT=*/void, ValueT>
648  {
651 
653  DenseIter(const MaskDenseIter& iter, NodeT* parent): BaseT(iter, parent) {}
654 
655  bool getItem(Index pos, void*& child, NonConstValueT& value) const
656  {
657  value = this->parent().getValue(pos);
658  child = NULL;
659  return false; // no child
660  }
661 
662  // Note: setItem() can't be called on const iterators.
663  //void setItem(Index pos, void* child) const {}
664 
665  // Note: unsetItem() can't be called on const iterators.
666  void unsetItem(Index pos, const ValueT& val) const {this->parent().setValueOnly(pos, val);}
667  };
668 
669 public:
670  typedef ValueIter<MaskOnIter, LeafNode, const bool> ValueOnIter;
671  typedef ValueIter<MaskOnIter, const LeafNode, const bool> ValueOnCIter;
672  typedef ValueIter<MaskOffIter, LeafNode, const bool> ValueOffIter;
673  typedef ValueIter<MaskOffIter, const LeafNode, const bool> ValueOffCIter;
674  typedef ValueIter<MaskDenseIter, LeafNode, const bool> ValueAllIter;
675  typedef ValueIter<MaskDenseIter, const LeafNode, const bool> ValueAllCIter;
676  typedef ChildIter<MaskOnIter, LeafNode> ChildOnIter;
677  typedef ChildIter<MaskOnIter, const LeafNode> ChildOnCIter;
678  typedef ChildIter<MaskOffIter, LeafNode> ChildOffIter;
679  typedef ChildIter<MaskOffIter, const LeafNode> ChildOffCIter;
680  typedef DenseIter<LeafNode, bool> ChildAllIter;
681  typedef DenseIter<const LeafNode, const bool> ChildAllCIter;
682 
683  ValueOnCIter cbeginValueOn() const { return ValueOnCIter(mValueMask.beginOn(), this); }
684  ValueOnCIter beginValueOn() const { return ValueOnCIter(mValueMask.beginOn(), this); }
685  ValueOnIter beginValueOn() { return ValueOnIter(mValueMask.beginOn(), this); }
686  ValueOffCIter cbeginValueOff() const { return ValueOffCIter(mValueMask.beginOff(), this); }
687  ValueOffCIter beginValueOff() const { return ValueOffCIter(mValueMask.beginOff(), this); }
688  ValueOffIter beginValueOff() { return ValueOffIter(mValueMask.beginOff(), this); }
689  ValueAllCIter cbeginValueAll() const { return ValueAllCIter(mValueMask.beginDense(), this); }
690  ValueAllCIter beginValueAll() const { return ValueAllCIter(mValueMask.beginDense(), this); }
691  ValueAllIter beginValueAll() { return ValueAllIter(mValueMask.beginDense(), this); }
692 
693  ValueOnCIter cendValueOn() const { return ValueOnCIter(mValueMask.endOn(), this); }
694  ValueOnCIter endValueOn() const { return ValueOnCIter(mValueMask.endOn(), this); }
695  ValueOnIter endValueOn() { return ValueOnIter(mValueMask.endOn(), this); }
696  ValueOffCIter cendValueOff() const { return ValueOffCIter(mValueMask.endOff(), this); }
697  ValueOffCIter endValueOff() const { return ValueOffCIter(mValueMask.endOff(), this); }
698  ValueOffIter endValueOff() { return ValueOffIter(mValueMask.endOff(), this); }
699  ValueAllCIter cendValueAll() const { return ValueAllCIter(mValueMask.endDense(), this); }
700  ValueAllCIter endValueAll() const { return ValueAllCIter(mValueMask.endDense(), this); }
701  ValueAllIter endValueAll() { return ValueAllIter(mValueMask.endDense(), this); }
702 
703  // Note that [c]beginChildOn() and [c]beginChildOff() actually return end iterators,
704  // because leaf nodes have no children.
705  ChildOnCIter cbeginChildOn() const { return ChildOnCIter(mValueMask.endOn(), this); }
706  ChildOnCIter beginChildOn() const { return ChildOnCIter(mValueMask.endOn(), this); }
707  ChildOnIter beginChildOn() { return ChildOnIter(mValueMask.endOn(), this); }
708  ChildOffCIter cbeginChildOff() const { return ChildOffCIter(mValueMask.endOff(), this); }
709  ChildOffCIter beginChildOff() const { return ChildOffCIter(mValueMask.endOff(), this); }
710  ChildOffIter beginChildOff() { return ChildOffIter(mValueMask.endOff(), this); }
711  ChildAllCIter cbeginChildAll() const { return ChildAllCIter(mValueMask.beginDense(), this); }
712  ChildAllCIter beginChildAll() const { return ChildAllCIter(mValueMask.beginDense(), this); }
713  ChildAllIter beginChildAll() { return ChildAllIter(mValueMask.beginDense(), this); }
714 
715  ChildOnCIter cendChildOn() const { return ChildOnCIter(mValueMask.endOn(), this); }
716  ChildOnCIter endChildOn() const { return ChildOnCIter(mValueMask.endOn(), this); }
717  ChildOnIter endChildOn() { return ChildOnIter(mValueMask.endOn(), this); }
718  ChildOffCIter cendChildOff() const { return ChildOffCIter(mValueMask.endOff(), this); }
719  ChildOffCIter endChildOff() const { return ChildOffCIter(mValueMask.endOff(), this); }
720  ChildOffIter endChildOff() { return ChildOffIter(mValueMask.endOff(), this); }
721  ChildAllCIter cendChildAll() const { return ChildAllCIter(mValueMask.endDense(), this); }
722  ChildAllCIter endChildAll() const { return ChildAllCIter(mValueMask.endDense(), this); }
723  ChildAllIter endChildAll() { return ChildAllIter(mValueMask.endDense(), this); }
724 
725  //
726  // Mask accessors
727  //
728  bool isValueMaskOn(Index n) const { return mValueMask.isOn(n); }
729  bool isValueMaskOn() const { return mValueMask.isOn(); }
730  bool isValueMaskOff(Index n) const { return mValueMask.isOff(n); }
731  bool isValueMaskOff() const { return mValueMask.isOff(); }
732  const NodeMaskType& getValueMask() const { return mValueMask; }
733  NodeMaskType& getValueMask() { return mValueMask; }
734  void setValueMask(const NodeMaskType& mask) { mValueMask = mask; }
735  bool isChildMaskOn(Index) const { return false; } // leaf nodes have no children
736  bool isChildMaskOff(Index) const { return true; }
737  bool isChildMaskOff() const { return true; }
738 protected:
739  void setValueMask(Index n, bool on) { mValueMask.set(n, on); }
740  void setValueMaskOn(Index n) { mValueMask.setOn(n); }
741  void setValueMaskOff(Index n) { mValueMask.setOff(n); }
742 
744  static void evalNodeOrigin(Coord& xyz) { xyz &= ~(DIM - 1); }
745 
746  template<typename NodeT, typename VisitorOp, typename ChildAllIterT>
747  static inline void doVisit(NodeT&, VisitorOp&);
748 
749  template<typename NodeT, typename OtherNodeT, typename VisitorOp,
750  typename ChildAllIterT, typename OtherChildAllIterT>
751  static inline void doVisit2Node(NodeT& self, OtherNodeT& other, VisitorOp&);
752 
753  template<typename NodeT, typename VisitorOp,
754  typename ChildAllIterT, typename OtherChildAllIterT>
755  static inline void doVisit2(NodeT& self, OtherChildAllIterT&, VisitorOp&, bool otherIsLHS);
756 
757 
759  NodeMaskType mValueMask;
761  Buffer mBuffer;
763  Coord mOrigin;
764 
765  // These static declarations must be on separate lines to avoid VC9 compiler errors.
766  static const bool sOn;
767  static const bool sOff;
768 
769 private:
772  template<typename, Index> friend class LeafNode;
773 
774  friend struct ValueIter<MaskOnIter, LeafNode, bool>;
775  friend struct ValueIter<MaskOffIter, LeafNode, bool>;
776  friend struct ValueIter<MaskDenseIter, LeafNode, bool>;
777  friend struct ValueIter<MaskOnIter, const LeafNode, bool>;
778  friend struct ValueIter<MaskOffIter, const LeafNode, bool>;
779  friend struct ValueIter<MaskDenseIter, const LeafNode, bool>;
780 
782  friend class IteratorBase<MaskOnIter, LeafNode>;
785  friend class IteratorBase<MaskOffIter, LeafNode>;
786  friend class IteratorBase<MaskDenseIter, LeafNode>;
788 
789 }; // class LeafNode<bool>
790 
791 
796 template<Index Log2Dim> const bool LeafNode<bool, Log2Dim>::sOn = true;
797 template<Index Log2Dim> const bool LeafNode<bool, Log2Dim>::sOff = false;
798 
799 
801 
802 
803 template<Index Log2Dim>
804 inline
806 {
807 }
808 
809 
810 template<Index Log2Dim>
811 inline
812 LeafNode<bool, Log2Dim>::LeafNode(const Coord& xyz, bool value, bool active):
813  mValueMask(active),
814  mBuffer(value),
815  mOrigin(xyz & (~(DIM - 1)))
816 {
817 }
818 
819 
820 #ifndef OPENVDB_2_ABI_COMPATIBLE
821 template<Index Log2Dim>
822 inline
823 LeafNode<bool, Log2Dim>::LeafNode(PartialCreate, const Coord& xyz, bool value, bool active):
824  mValueMask(active),
825  mBuffer(value),
826  mOrigin(xyz & (~(DIM - 1)))
827 {
831 }
832 #endif
833 
834 
835 template<Index Log2Dim>
836 inline
838  mValueMask(other.mValueMask),
839  mBuffer(other.mBuffer),
840  mOrigin(other.mOrigin)
841 {
842 }
843 
844 
845 // Copy-construct from a leaf node with the same configuration but a different ValueType.
846 template<Index Log2Dim>
847 template<typename ValueT>
848 inline
850  mValueMask(other.getValueMask()),
851  mOrigin(other.origin())
852 {
853  struct Local {
855  static inline bool convertValue(const ValueT& val) { return bool(val); }
856  };
857 
858  for (Index i = 0; i < SIZE; ++i) {
859  mBuffer.setValue(i, Local::convertValue(other.mBuffer[i]));
860  }
861 }
862 
863 
864 template<Index Log2Dim>
865 template<typename ValueT>
866 inline
868  bool background, TopologyCopy):
869  mValueMask(other.getValueMask()),
870  mBuffer(background),
871  mOrigin(other.origin())
872 {
873 }
874 
875 
876 template<Index Log2Dim>
877 template<typename ValueT>
878 inline
880  mValueMask(other.getValueMask()),
881  mBuffer(other.getValueMask()), // value = active state
882  mOrigin(other.origin())
883 {
884 }
885 
886 
887 template<Index Log2Dim>
888 template<typename ValueT>
889 inline
891  bool offValue, bool onValue, TopologyCopy):
892  mValueMask(other.getValueMask()),
893  mBuffer(other.getValueMask()),
894  mOrigin(other.origin())
895 {
896  if (offValue) { if (!onValue) mBuffer.mData.toggle(); else mBuffer.mData.setOn(); }
897 }
898 
899 
900 template<Index Log2Dim>
901 inline
903 {
904 }
905 
906 
908 
909 
910 template<Index Log2Dim>
911 inline Index64
913 {
914  return sizeof(mOrigin) + mValueMask.memUsage() + mBuffer.memUsage();
915 }
916 
917 
918 template<Index Log2Dim>
919 inline void
920 LeafNode<bool, Log2Dim>::evalActiveBoundingBox(CoordBBox& bbox, bool visitVoxels) const
921 {
922  CoordBBox this_bbox = this->getNodeBoundingBox();
923  if (bbox.isInside(this_bbox)) return;//this LeafNode is already enclosed in the bbox
924  if (ValueOnCIter iter = this->cbeginValueOn()) {//any active values?
925  if (visitVoxels) {//use voxel granularity?
926  this_bbox.reset();
927  for(; iter; ++iter) this_bbox.expand(this->offsetToLocalCoord(iter.pos()));
928  this_bbox.translate(this->origin());
929  }
930  bbox.expand(this_bbox);
931  }
932 }
933 
934 
935 template<Index Log2Dim>
936 template<typename OtherType, Index OtherLog2Dim>
937 inline bool
939 {
940  assert(other);
941  return (Log2Dim == OtherLog2Dim && mValueMask == other->getValueMask());
942 }
943 
944 
945 template<Index Log2Dim>
946 inline std::string
948 {
949  std::ostringstream ostr;
950  ostr << "LeafNode @" << mOrigin << ": ";
951  for (Index32 n = 0; n < SIZE; ++n) ostr << (mValueMask.isOn(n) ? '#' : '.');
952  return ostr.str();
953 }
954 
955 
957 
958 
959 template<Index Log2Dim>
960 inline Index
962 {
963  assert ((xyz[0] & (DIM-1u)) < DIM && (xyz[1] & (DIM-1u)) < DIM && (xyz[2] & (DIM-1u)) < DIM);
964  return ((xyz[0] & (DIM-1u)) << 2*Log2Dim)
965  + ((xyz[1] & (DIM-1u)) << Log2Dim)
966  + (xyz[2] & (DIM-1u));
967 }
968 
969 
970 template<Index Log2Dim>
971 inline Coord
973 {
974  assert(n < (1 << 3*Log2Dim));
975  Coord xyz;
976  xyz.setX(n >> 2*Log2Dim);
977  n &= ((1 << 2*Log2Dim) - 1);
978  xyz.setY(n >> Log2Dim);
979  xyz.setZ(n & ((1 << Log2Dim) - 1));
980  return xyz;
981 }
982 
983 
984 template<Index Log2Dim>
985 inline Coord
987 {
988  return (this->offsetToLocalCoord(n) + this->origin());
989 }
990 
991 
993 
994 
995 template<Index Log2Dim>
996 inline void
997 LeafNode<bool, Log2Dim>::readTopology(std::istream& is, bool /*fromHalf*/)
998 {
999  mValueMask.load(is);
1000 }
1001 
1002 
1003 template<Index Log2Dim>
1004 inline void
1005 LeafNode<bool, Log2Dim>::writeTopology(std::ostream& os, bool /*toHalf*/) const
1006 {
1007  mValueMask.save(os);
1008 }
1009 
1010 
1011 template<Index Log2Dim>
1012 inline void
1013 LeafNode<bool, Log2Dim>::readBuffers(std::istream& is, const CoordBBox& clipBBox, bool fromHalf)
1014 {
1015  // Boolean LeafNodes don't currently implement lazy loading.
1016  // Instead, load the full buffer, then clip it.
1017 
1018  this->readBuffers(is, fromHalf);
1019 
1020  // Get this tree's background value.
1021  bool background = false;
1022  if (const void* bgPtr = io::getGridBackgroundValuePtr(is)) {
1023  background = *static_cast<const bool*>(bgPtr);
1024  }
1025  this->clip(clipBBox, background);
1026 }
1027 
1028 
1029 template<Index Log2Dim>
1030 inline void
1031 LeafNode<bool, Log2Dim>::readBuffers(std::istream& is, bool /*fromHalf*/)
1032 {
1033  // Read in the value mask.
1034  mValueMask.load(is);
1035  // Read in the origin.
1036  is.read(reinterpret_cast<char*>(&mOrigin), sizeof(Coord::ValueType) * 3);
1037 
1039  // Read in the mask for the voxel values.
1040  mBuffer.mData.load(is);
1041  } else {
1042  // Older files stored one or more bool arrays.
1043 
1044  // Read in the number of buffers, which should now always be one.
1045  int8_t numBuffers = 0;
1046  is.read(reinterpret_cast<char*>(&numBuffers), sizeof(int8_t));
1047 
1048  // Read in the buffer.
1049  // (Note: prior to the bool leaf optimization, buffers were always compressed.)
1050  boost::shared_array<bool> buf(new bool[SIZE]);
1051  io::readData<bool>(is, buf.get(), SIZE, /*isCompressed=*/true);
1052 
1053  // Transfer values to mBuffer.
1054  mBuffer.mData.setOff();
1055  for (Index i = 0; i < SIZE; ++i) {
1056  if (buf[i]) mBuffer.mData.setOn(i);
1057  }
1058 
1059  if (numBuffers > 1) {
1060  // Read in and discard auxiliary buffers that were created with
1061  // earlier versions of the library.
1062  for (int i = 1; i < numBuffers; ++i) {
1063  io::readData<bool>(is, buf.get(), SIZE, /*isCompressed=*/true);
1064  }
1065  }
1066  }
1067 }
1068 
1069 
1070 template<Index Log2Dim>
1071 inline void
1072 LeafNode<bool, Log2Dim>::writeBuffers(std::ostream& os, bool /*toHalf*/) const
1073 {
1074  // Write out the value mask.
1075  mValueMask.save(os);
1076  // Write out the origin.
1077  os.write(reinterpret_cast<const char*>(&mOrigin), sizeof(Coord::ValueType) * 3);
1078  // Write out the voxel values.
1079  mBuffer.mData.save(os);
1080 }
1081 
1082 
1084 
1085 
1086 template<Index Log2Dim>
1087 inline bool
1089 {
1090  return (mValueMask == other.mValueMask && mBuffer.mData == other.mBuffer.mData);
1091 }
1092 
1093 
1094 template<Index Log2Dim>
1095 inline bool
1097 {
1098  return !(this->operator==(other));
1099 }
1100 
1101 
1103 
1104 
1105 template<Index Log2Dim>
1106 inline bool
1107 LeafNode<bool, Log2Dim>::isConstant(bool& constValue, bool& state, bool tolerance) const
1108 {
1109  state = mValueMask.isOn();
1110 
1111  if (!(state || mValueMask.isOff())) return false;
1112 
1113  // Note: if tolerance is true (i.e., 1), then all boolean values compare equal.
1114  if (!tolerance && !(mBuffer.mData.isOn() || mBuffer.mData.isOff())) return false;
1115 
1116  state = mValueMask.isOn();
1117  constValue = mBuffer.mData.isOn();
1118  return true;
1119 }
1120 
1121 
1123 
1124 
1125 template<Index Log2Dim>
1126 inline void
1127 LeafNode<bool, Log2Dim>::addTile(Index /*level*/, const Coord& xyz, bool val, bool active)
1128 {
1129  this->addTile(this->coordToOffset(xyz), val, active);
1130 }
1131 
1132 template<Index Log2Dim>
1133 inline void
1134 LeafNode<bool, Log2Dim>::addTile(Index offset, bool val, bool active)
1135 {
1136  assert(offset < SIZE);
1137  setValueOnly(offset, val);
1138  setActiveState(offset, active);
1139 }
1140 
1141 template<Index Log2Dim>
1142 template<typename AccessorT>
1143 inline void
1145  bool val, bool active, AccessorT&)
1146 {
1147  this->addTile(level, xyz, val, active);
1148 }
1149 
1150 
1152 
1153 
1154 template<Index Log2Dim>
1155 inline const bool&
1156 LeafNode<bool, Log2Dim>::getValue(const Coord& xyz) const
1157 {
1158  // This *CANNOT* use operator ? because Visual C++
1159  if (mBuffer.mData.isOn(this->coordToOffset(xyz))) return sOn; else return sOff;
1160 }
1161 
1162 
1163 template<Index Log2Dim>
1164 inline const bool&
1166 {
1167  assert(offset < SIZE);
1168  // This *CANNOT* use operator ? for Windows
1169  if (mBuffer.mData.isOn(offset)) return sOn; else return sOff;
1170 }
1171 
1172 
1173 template<Index Log2Dim>
1174 inline bool
1175 LeafNode<bool, Log2Dim>::probeValue(const Coord& xyz, bool& val) const
1176 {
1177  const Index offset = this->coordToOffset(xyz);
1178  val = mBuffer.mData.isOn(offset);
1179  return mValueMask.isOn(offset);
1180 }
1181 
1182 
1183 template<Index Log2Dim>
1184 inline void
1185 LeafNode<bool, Log2Dim>::setValueOn(const Coord& xyz, bool val)
1186 {
1187  this->setValueOn(this->coordToOffset(xyz), val);
1188 }
1189 
1190 
1191 template<Index Log2Dim>
1192 inline void
1194 {
1195  assert(offset < SIZE);
1196  mValueMask.setOn(offset);
1197  mBuffer.mData.set(offset, val);
1198 }
1199 
1200 
1201 template<Index Log2Dim>
1202 inline void
1203 LeafNode<bool, Log2Dim>::setValueOnly(const Coord& xyz, bool val)
1204 {
1205  this->setValueOnly(this->coordToOffset(xyz), val);
1206 }
1207 
1208 
1209 template<Index Log2Dim>
1210 inline void
1212 {
1213  mValueMask.set(this->coordToOffset(xyz), on);
1214 }
1215 
1216 
1217 template<Index Log2Dim>
1218 inline void
1219 LeafNode<bool, Log2Dim>::setValueOff(const Coord& xyz, bool val)
1220 {
1221  this->setValueOff(this->coordToOffset(xyz), val);
1222 }
1223 
1224 
1225 template<Index Log2Dim>
1226 inline void
1228 {
1229  assert(offset < SIZE);
1230  mValueMask.setOff(offset);
1231  mBuffer.mData.set(offset, val);
1232 }
1233 
1234 
1235 template<Index Log2Dim>
1236 template<typename ModifyOp>
1237 inline void
1238 LeafNode<bool, Log2Dim>::modifyValue(Index offset, const ModifyOp& op)
1239 {
1240  bool val = mBuffer.mData.isOn(offset);
1241  op(val);
1242  mBuffer.mData.set(offset, val);
1243  mValueMask.setOn(offset);
1244 }
1245 
1246 
1247 template<Index Log2Dim>
1248 template<typename ModifyOp>
1249 inline void
1250 LeafNode<bool, Log2Dim>::modifyValue(const Coord& xyz, const ModifyOp& op)
1251 {
1252  this->modifyValue(this->coordToOffset(xyz), op);
1253 }
1254 
1255 
1256 template<Index Log2Dim>
1257 template<typename ModifyOp>
1258 inline void
1259 LeafNode<bool, Log2Dim>::modifyValueAndActiveState(const Coord& xyz, const ModifyOp& op)
1260 {
1261  const Index offset = this->coordToOffset(xyz);
1262  bool val = mBuffer.mData.isOn(offset), state = mValueMask.isOn(offset);
1263  op(val, state);
1264  mBuffer.mData.set(offset, val);
1265  mValueMask.set(offset, state);
1266 }
1267 
1268 
1270 
1271 
1272 template<Index Log2Dim>
1273 inline void
1274 LeafNode<bool, Log2Dim>::resetBackground(bool oldBackground, bool newBackground)
1275 {
1276  if (newBackground != oldBackground) {
1277  // Flip mBuffer's background bits and zero its foreground bits.
1278  NodeMaskType bgMask = !(mBuffer.mData | mValueMask);
1279  // Overwrite mBuffer's background bits, leaving its foreground bits intact.
1280  mBuffer.mData = (mBuffer.mData & mValueMask) | bgMask;
1281  }
1282 }
1283 
1284 
1286 
1287 
1288 template<Index Log2Dim>
1289 template<MergePolicy Policy>
1290 inline void
1291 LeafNode<bool, Log2Dim>::merge(const LeafNode& other, bool /*bg*/, bool /*otherBG*/)
1292 {
1294  if (Policy == MERGE_NODES) return;
1295  for (typename NodeMaskType::OnIterator iter = other.mValueMask.beginOn(); iter; ++iter) {
1296  const Index n = iter.pos();
1297  if (mValueMask.isOff(n)) {
1298  mBuffer.mData.set(n, other.mBuffer.mData.isOn(n));
1299  mValueMask.setOn(n);
1300  }
1301  }
1303 }
1304 
1305 template<Index Log2Dim>
1306 template<MergePolicy Policy>
1307 inline void
1308 LeafNode<bool, Log2Dim>::merge(bool tileValue, bool tileActive)
1309 {
1311  if (Policy != MERGE_ACTIVE_STATES_AND_NODES) return;
1312  if (!tileActive) return;
1313  // Replace all inactive values with the active tile value.
1314  if (tileValue) mBuffer.mData |= !mValueMask; // -0=>1, +0=>0, -1=>1, +1=>1 (-,+ = off,on)
1315  else mBuffer.mData &= mValueMask; // -0=>0, +0=>0, -1=>0, +1=>1
1316  mValueMask.setOn();
1318 }
1319 
1320 
1322 
1323 
1324 template<Index Log2Dim>
1325 template<typename OtherType>
1326 inline void
1328 {
1329  mValueMask |= other.getValueMask();
1330 }
1331 
1332 
1333 template<Index Log2Dim>
1334 template<typename OtherType>
1335 inline void
1337  const bool&)
1338 {
1339  mValueMask &= other.getValueMask();
1340 }
1341 
1342 
1343 template<Index Log2Dim>
1344 template<typename OtherType>
1345 inline void
1347  const bool&)
1348 {
1349  mValueMask &= !other.getValueMask();
1350 }
1351 
1352 
1354 
1355 
1356 template<Index Log2Dim>
1357 inline void
1358 LeafNode<bool, Log2Dim>::clip(const CoordBBox& clipBBox, bool background)
1359 {
1360  CoordBBox nodeBBox = this->getNodeBoundingBox();
1361  if (!clipBBox.hasOverlap(nodeBBox)) {
1362  // This node lies completely outside the clipping region. Fill it with background tiles.
1363  this->fill(nodeBBox, background, /*active=*/false);
1364  } else if (clipBBox.isInside(nodeBBox)) {
1365  // This node lies completely inside the clipping region. Leave it intact.
1366  return;
1367  }
1368 
1369  // This node isn't completely contained inside the clipping region.
1370  // Set any voxels that lie outside the region to the background value.
1371 
1372  // Construct a boolean mask that is on inside the clipping region and off outside it.
1373  NodeMaskType mask;
1374  nodeBBox.intersect(clipBBox);
1375  Coord xyz;
1376  int &x = xyz.x(), &y = xyz.y(), &z = xyz.z();
1377  for (x = nodeBBox.min().x(); x <= nodeBBox.max().x(); ++x) {
1378  for (y = nodeBBox.min().y(); y <= nodeBBox.max().y(); ++y) {
1379  for (z = nodeBBox.min().z(); z <= nodeBBox.max().z(); ++z) {
1380  mask.setOn(static_cast<Index32>(this->coordToOffset(xyz)));
1381  }
1382  }
1383  }
1384 
1385  // Set voxels that lie in the inactive region of the mask (i.e., outside
1386  // the clipping region) to the background value.
1387  for (MaskOffIter maskIter = mask.beginOff(); maskIter; ++maskIter) {
1388  this->setValueOff(maskIter.pos(), background);
1389  }
1390 }
1391 
1392 
1394 
1395 
1396 template<Index Log2Dim>
1397 inline void
1398 LeafNode<bool, Log2Dim>::fill(const CoordBBox& bbox, bool value, bool active)
1399 {
1400  for (Int32 x = bbox.min().x(); x <= bbox.max().x(); ++x) {
1401  const Index offsetX = (x & (DIM-1u))<<2*Log2Dim;
1402  for (Int32 y = bbox.min().y(); y <= bbox.max().y(); ++y) {
1403  const Index offsetXY = offsetX + ((y & (DIM-1u))<< Log2Dim);
1404  for (Int32 z = bbox.min().z(); z <= bbox.max().z(); ++z) {
1405  const Index offset = offsetXY + (z & (DIM-1u));
1406  mValueMask.set(offset, active);
1407  mBuffer.mData.set(offset, value);
1408  }
1409  }
1410  }
1411 }
1412 
1413 template<Index Log2Dim>
1414 inline void
1416 {
1417  mBuffer.fill(value);
1418 }
1419 
1420 template<Index Log2Dim>
1421 inline void
1422 LeafNode<bool, Log2Dim>::fill(const bool& value, bool active)
1423 {
1424  mBuffer.fill(value);
1425  mValueMask.set(active);
1426 }
1427 
1428 
1430 
1431 
1432 template<Index Log2Dim>
1433 template<typename DenseT>
1434 inline void
1435 LeafNode<bool, Log2Dim>::copyToDense(const CoordBBox& bbox, DenseT& dense) const
1436 {
1437  typedef typename DenseT::ValueType DenseValueType;
1438 
1439  const size_t xStride = dense.xStride(), yStride = dense.yStride(), zStride = dense.zStride();
1440  const Coord& min = dense.bbox().min();
1441  DenseValueType* t0 = dense.data() + zStride * (bbox.min()[2] - min[2]); // target array
1442  const Int32 n0 = bbox.min()[2] & (DIM-1u);
1443  for (Int32 x = bbox.min()[0], ex = bbox.max()[0] + 1; x < ex; ++x) {
1444  DenseValueType* t1 = t0 + xStride * (x - min[0]);
1445  const Int32 n1 = n0 + ((x & (DIM-1u)) << 2*LOG2DIM);
1446  for (Int32 y = bbox.min()[1], ey = bbox.max()[1] + 1; y < ey; ++y) {
1447  DenseValueType* t2 = t1 + yStride * (y - min[1]);
1448  Int32 n2 = n1 + ((y & (DIM-1u)) << LOG2DIM);
1449  for (Int32 z = bbox.min()[2], ez = bbox.max()[2] + 1; z < ez; ++z, t2 += zStride) {
1450  *t2 = DenseValueType(mBuffer.mData.isOn(n2++));
1451  }
1452  }
1453  }
1454 }
1455 
1456 
1457 template<Index Log2Dim>
1458 template<typename DenseT>
1459 inline void
1460 LeafNode<bool, Log2Dim>::copyFromDense(const CoordBBox& bbox, const DenseT& dense,
1461  bool background, bool tolerance)
1462 {
1463  typedef typename DenseT::ValueType DenseValueType;
1464  struct Local {
1465  inline static bool toBool(const DenseValueType& v) { return !math::isZero(v); }
1466  };
1467 
1468  const size_t xStride = dense.xStride(), yStride = dense.yStride(), zStride = dense.zStride();
1469  const Coord& min = dense.bbox().min();
1470  const DenseValueType* s0 = dense.data() + zStride * (bbox.min()[2] - min[2]); // source
1471  const Int32 n0 = bbox.min()[2] & (DIM-1u);
1472  for (Int32 x = bbox.min()[0], ex = bbox.max()[0] + 1; x < ex; ++x) {
1473  const DenseValueType* s1 = s0 + xStride * (x - min[0]);
1474  const Int32 n1 = n0 + ((x & (DIM-1u)) << 2*LOG2DIM);
1475  for (Int32 y = bbox.min()[1], ey = bbox.max()[1] + 1; y < ey; ++y) {
1476  const DenseValueType* s2 = s1 + yStride * (y - min[1]);
1477  Int32 n2 = n1 + ((y & (DIM-1u)) << LOG2DIM);
1478  for (Int32 z = bbox.min()[2], ez = bbox.max()[2]+1; z < ez; ++z, ++n2, s2 += zStride) {
1479  // Note: if tolerance is true (i.e., 1), then all boolean values compare equal.
1480  if (tolerance || (background == Local::toBool(*s2))) {
1481  mValueMask.setOff(n2);
1482  mBuffer.mData.set(n2, background);
1483  } else {
1484  mValueMask.setOn(n2);
1485  mBuffer.mData.set(n2, Local::toBool(*s2));
1486  }
1487  }
1488  }
1489  }
1490 }
1491 
1492 
1494 
1495 
1496 template<Index Log2Dim>
1497 template<typename CombineOp>
1498 inline void
1499 LeafNode<bool, Log2Dim>::combine(const LeafNode& other, CombineOp& op)
1500 {
1501  CombineArgs<bool> args;
1502  for (Index i = 0; i < SIZE; ++i) {
1503  bool result = false, aVal = mBuffer.mData.isOn(i), bVal = other.mBuffer.mData.isOn(i);
1504  op(args.setARef(aVal)
1505  .setAIsActive(mValueMask.isOn(i))
1506  .setBRef(bVal)
1507  .setBIsActive(other.mValueMask.isOn(i))
1508  .setResultRef(result));
1509  mValueMask.set(i, args.resultIsActive());
1510  mBuffer.mData.set(i, result);
1511  }
1512 }
1513 
1514 
1515 template<Index Log2Dim>
1516 template<typename CombineOp>
1517 inline void
1518 LeafNode<bool, Log2Dim>::combine(bool value, bool valueIsActive, CombineOp& op)
1519 {
1520  CombineArgs<bool> args;
1521  args.setBRef(value).setBIsActive(valueIsActive);
1522  for (Index i = 0; i < SIZE; ++i) {
1523  bool result = false, aVal = mBuffer.mData.isOn(i);
1524  op(args.setARef(aVal)
1525  .setAIsActive(mValueMask.isOn(i))
1526  .setResultRef(result));
1527  mValueMask.set(i, args.resultIsActive());
1528  mBuffer.mData.set(i, result);
1529  }
1530 }
1531 
1532 
1534 
1535 
1536 template<Index Log2Dim>
1537 template<typename CombineOp, typename OtherType>
1538 inline void
1539 LeafNode<bool, Log2Dim>::combine2(const LeafNode& other, const OtherType& value,
1540  bool valueIsActive, CombineOp& op)
1541 {
1543  args.setBRef(value).setBIsActive(valueIsActive);
1544  for (Index i = 0; i < SIZE; ++i) {
1545  bool result = false, aVal = other.mBuffer.mData.isOn(i);
1546  op(args.setARef(aVal)
1547  .setAIsActive(other.mValueMask.isOn(i))
1548  .setResultRef(result));
1549  mValueMask.set(i, args.resultIsActive());
1550  mBuffer.mData.set(i, result);
1551  }
1552 }
1553 
1554 
1555 template<Index Log2Dim>
1556 template<typename CombineOp, typename OtherNodeT>
1557 inline void
1558 LeafNode<bool, Log2Dim>::combine2(bool value, const OtherNodeT& other,
1559  bool valueIsActive, CombineOp& op)
1560 {
1562  args.setARef(value).setAIsActive(valueIsActive);
1563  for (Index i = 0; i < SIZE; ++i) {
1564  bool result = false, bVal = other.mBuffer.mData.isOn(i);
1565  op(args.setBRef(bVal)
1566  .setBIsActive(other.mValueMask.isOn(i))
1567  .setResultRef(result));
1568  mValueMask.set(i, args.resultIsActive());
1569  mBuffer.mData.set(i, result);
1570  }
1571 }
1572 
1573 
1574 template<Index Log2Dim>
1575 template<typename CombineOp, typename OtherNodeT>
1576 inline void
1577 LeafNode<bool, Log2Dim>::combine2(const LeafNode& b0, const OtherNodeT& b1, CombineOp& op)
1578 {
1580  for (Index i = 0; i < SIZE; ++i) {
1581  // Default behavior: output voxel is active if either input voxel is active.
1582  mValueMask.set(i, b0.mValueMask.isOn(i) || b1.mValueMask.isOn(i));
1583 
1584  bool result = false, b0Val = b0.mBuffer.mData.isOn(i), b1Val = b1.mBuffer.mData.isOn(i);
1585  op(args.setARef(b0Val)
1586  .setAIsActive(b0.mValueMask.isOn(i))
1587  .setBRef(b1Val)
1588  .setBIsActive(b1.mValueMask.isOn(i))
1589  .setResultRef(result));
1590  mValueMask.set(i, args.resultIsActive());
1591  mBuffer.mData.set(i, result);
1592  }
1593 }
1594 
1595 
1597 
1598 template<Index Log2Dim>
1599 template<typename BBoxOp>
1600 inline void
1602 {
1603  if (op.template descent<LEVEL>()) {
1604  for (ValueOnCIter i=this->cbeginValueOn(); i; ++i) {
1605 #ifdef _MSC_VER
1606  op.operator()<LEVEL>(CoordBBox::createCube(i.getCoord(), 1));
1607 #else
1608  op.template operator()<LEVEL>(CoordBBox::createCube(i.getCoord(), 1));
1609 #endif
1610  }
1611  } else {
1612 #ifdef _MSC_VER
1613  op.operator()<LEVEL>(this->getNodeBoundingBox());
1614 #else
1615  op.template operator()<LEVEL>(this->getNodeBoundingBox());
1616 #endif
1617  }
1618 }
1619 
1620 
1621 template<Index Log2Dim>
1622 template<typename VisitorOp>
1623 inline void
1625 {
1626  doVisit<LeafNode, VisitorOp, ChildAllIter>(*this, op);
1627 }
1628 
1629 
1630 template<Index Log2Dim>
1631 template<typename VisitorOp>
1632 inline void
1634 {
1635  doVisit<const LeafNode, VisitorOp, ChildAllCIter>(*this, op);
1636 }
1637 
1638 
1639 template<Index Log2Dim>
1640 template<typename NodeT, typename VisitorOp, typename ChildAllIterT>
1641 inline void
1642 LeafNode<bool, Log2Dim>::doVisit(NodeT& self, VisitorOp& op)
1643 {
1644  for (ChildAllIterT iter = self.beginChildAll(); iter; ++iter) {
1645  op(iter);
1646  }
1647 }
1648 
1649 
1651 
1652 
1653 template<Index Log2Dim>
1654 template<typename OtherLeafNodeType, typename VisitorOp>
1655 inline void
1656 LeafNode<bool, Log2Dim>::visit2Node(OtherLeafNodeType& other, VisitorOp& op)
1657 {
1658  doVisit2Node<LeafNode, OtherLeafNodeType, VisitorOp, ChildAllIter,
1659  typename OtherLeafNodeType::ChildAllIter>(*this, other, op);
1660 }
1661 
1662 
1663 template<Index Log2Dim>
1664 template<typename OtherLeafNodeType, typename VisitorOp>
1665 inline void
1666 LeafNode<bool, Log2Dim>::visit2Node(OtherLeafNodeType& other, VisitorOp& op) const
1667 {
1668  doVisit2Node<const LeafNode, OtherLeafNodeType, VisitorOp, ChildAllCIter,
1669  typename OtherLeafNodeType::ChildAllCIter>(*this, other, op);
1670 }
1671 
1672 
1673 template<Index Log2Dim>
1674 template<
1675  typename NodeT,
1676  typename OtherNodeT,
1677  typename VisitorOp,
1678  typename ChildAllIterT,
1679  typename OtherChildAllIterT>
1680 inline void
1681 LeafNode<bool, Log2Dim>::doVisit2Node(NodeT& self, OtherNodeT& other, VisitorOp& op)
1682 {
1683  // Allow the two nodes to have different ValueTypes, but not different dimensions.
1684  BOOST_STATIC_ASSERT(OtherNodeT::SIZE == NodeT::SIZE);
1685  BOOST_STATIC_ASSERT(OtherNodeT::LEVEL == NodeT::LEVEL);
1686 
1687  ChildAllIterT iter = self.beginChildAll();
1688  OtherChildAllIterT otherIter = other.beginChildAll();
1689 
1690  for ( ; iter && otherIter; ++iter, ++otherIter) {
1691  op(iter, otherIter);
1692  }
1693 }
1694 
1695 
1697 
1698 
1699 template<Index Log2Dim>
1700 template<typename IterT, typename VisitorOp>
1701 inline void
1702 LeafNode<bool, Log2Dim>::visit2(IterT& otherIter, VisitorOp& op, bool otherIsLHS)
1703 {
1704  doVisit2<LeafNode, VisitorOp, ChildAllIter, IterT>(*this, otherIter, op, otherIsLHS);
1705 }
1706 
1707 
1708 template<Index Log2Dim>
1709 template<typename IterT, typename VisitorOp>
1710 inline void
1711 LeafNode<bool, Log2Dim>::visit2(IterT& otherIter, VisitorOp& op, bool otherIsLHS) const
1712 {
1713  doVisit2<const LeafNode, VisitorOp, ChildAllCIter, IterT>(*this, otherIter, op, otherIsLHS);
1714 }
1715 
1716 
1717 template<Index Log2Dim>
1718 template<
1719  typename NodeT,
1720  typename VisitorOp,
1721  typename ChildAllIterT,
1722  typename OtherChildAllIterT>
1723 inline void
1724 LeafNode<bool, Log2Dim>::doVisit2(NodeT& self, OtherChildAllIterT& otherIter,
1725  VisitorOp& op, bool otherIsLHS)
1726 {
1727  if (!otherIter) return;
1728 
1729  if (otherIsLHS) {
1730  for (ChildAllIterT iter = self.beginChildAll(); iter; ++iter) {
1731  op(otherIter, iter);
1732  }
1733  } else {
1734  for (ChildAllIterT iter = self.beginChildAll(); iter; ++iter) {
1735  op(iter, otherIter);
1736  }
1737  }
1738 }
1739 
1740 } // namespace tree
1741 } // namespace OPENVDB_VERSION_NAME
1742 } // namespace openvdb
1743 
1744 #endif // OPENVDB_TREE_LEAFNODEBOOL_HAS_BEEN_INCLUDED
1745 
1746 // Copyright (c) 2012-2014 DreamWorks Animation LLC
1747 // All rights reserved. This software is distributed under the
1748 // Mozilla Public License 2.0 ( http://www.mozilla.org/MPL/2.0/ )
void writeBuffers(std::ostream &os, bool toHalf=false) const
Write buffers to a stream.
Definition: LeafNode.h:1631
ChildIter< MaskOffIter, const LeafNode > ChildOffCIter
Definition: LeafNodeBool.h:679
CombineArgs & setBRef(const BValueType &b)
Redirect the B value to a new external source.
Definition: Types.h:349
util::NodeMask< Log2Dim > NodeMaskType
Definition: LeafNodeBool.h:60
void setValueAndCache(const Coord &xyz, bool val, AccessorT &)
Change the value of the voxel at the given coordinates and mark it as active.
Definition: LeafNodeBool.h:396
static Index coordToOffset(const Coord &xyz)
Return the linear table offset of the given global or local coordinates.
Definition: LeafNode.h:1217
ChildOffCIter cendChildOff() const
Definition: LeafNodeBool.h:718
ChildOnCIter cbeginChildOn() const
Definition: LeafNodeBool.h:705
const Coord & origin() const
Return the grid index coordinates of this node's local origin.
Definition: LeafNode.h:441
static Index getChildDim()
Definition: LeafNodeBool.h:171
Definition: Types.h:424
ChildAllCIter beginChildAll() const
Definition: LeafNodeBool.h:712
NodeT * probeNode(const Coord &)
This function exists only to enable template instantiation.
Definition: LeafNodeBool.h:554
void save(std::ostream &os) const
Definition: NodeMasks.h:507
void addTile(Index level, const Coord &, const ValueType &, bool)
Definition: LeafNode.h:1718
ValueAllCIter cendValueAll() const
Definition: LeafNodeBool.h:699
DenseIter(const MaskDenseIter &iter, NodeT *parent)
Definition: LeafNodeBool.h:653
const NodeMaskType & getValueMask() const
Definition: LeafNode.h:1051
DenseIter< const LeafNode, const bool > ChildAllCIter
Definition: LeafNodeBool.h:681
LeafNode * probeLeafAndCache(const Coord &, AccessorT &)
Return a pointer to this node.
Definition: LeafNodeBool.h:572
void modifyValueAndActiveStateAndCache(const Coord &xyz, const ModifyOp &op, AccessorT &)
Definition: LeafNodeBool.h:424
bool operator==(const LeafNode &other) const
Check for buffer, state and origin equivalence.
Definition: LeafNode.h:1648
NodeMaskType::OffIterator MaskOffIter
Definition: LeafNodeBool.h:605
LeafNode< bool, Log2Dim > LeafNodeType
Definition: LeafNodeBool.h:57
Coord offsetToGlobalCoord(Index n) const
Return the global coordinates for a linear table offset.
Definition: LeafNode.h:1241
ChildOffCIter cbeginChildOff() const
Definition: LeafNodeBool.h:708
ValueIter< MaskDenseIter, LeafNode, const bool > ValueAllIter
Definition: LeafNodeBool.h:674
void addLeafAndCache(LeafNode *, AccessorT &)
This function exists only to enable template instantiation.
Definition: LeafNodeBool.h:550
void load(std::istream &is)
Definition: NodeMasks.h:511
LeafNode< ValueType, Log2Dim > Type
Definition: LeafNodeBool.h:74
ChildAllIter endChildAll()
Definition: LeafNodeBool.h:723
void setOrigin(const Coord &origin)
Set the grid index coordinates of this node's local origin.
Definition: LeafNodeBool.h:214
static Coord offsetToLocalCoord(Index n)
Return the local coordinates for a linear table offset, where offset 0 has coordinates (0...
Definition: LeafNode.h:1227
ChildAllCIter cbeginChildAll() const
Definition: LeafNodeBool.h:711
ChildOffIter endChildOff()
Definition: LeafNodeBool.h:720
uint64_t Index64
Definition: Types.h:58
CoordBBox getNodeBoundingBox() const
Return the bounding box of this node, i.e., the full index space spanned by this leaf node...
Definition: LeafNode.h:435
void setValueOn(const Coord &xyz)
Mark the voxel at the given coordinates as active but don't change its value.
Definition: LeafNodeBool.h:302
ValueIter< MaskOnIter, const LeafNode, const bool > ValueOnCIter
Definition: LeafNodeBool.h:671
ValueOffIter beginValueOff()
Definition: LeafNodeBool.h:688
const bool & getValue(Index i) const
Definition: LeafNodeBool.h:95
ValueOnCIter cendValueOn() const
Definition: LeafNodeBool.h:693
void getOrigin(Int32 &x, Int32 &y, Int32 &z) const
Return the grid index coordinates of this node's local origin.
Definition: LeafNodeBool.h:219
Buffer & buffer()
Definition: LeafNodeBool.h:249
void copyFromDense(const DenseT &dense, GridOrTreeT &sparse, const typename GridOrTreeT::ValueType &tolerance, bool serial=false)
Populate a sparse grid with the values of all of the voxels of a dense grid.
Definition: Dense.h:517
void modifyValue(Index offset, const ModifyOp &op)
Apply a functor to the value of the voxel at the given offset and mark the voxel as active...
Definition: LeafNode.h:698
bool isValueMaskOff(Index n) const
Definition: LeafNodeBool.h:730
ChildOnIter beginChildOn()
Definition: LeafNodeBool.h:707
ValueIter< MaskDenseIter, const LeafNode, const bool > ValueAllCIter
Definition: LeafNodeBool.h:675
void setValueOn(const Coord &xyz)
Mark the voxel at the given coordinates as active but don't change its value.
Definition: LeafNode.h:680
void addLeaf(LeafNode *)
This function exists only to enable template instantiation.
Definition: LeafNodeBool.h:548
bool resultIsActive() const
Definition: Types.h:358
void setValuesOn()
Mark all voxels as active but don't change their values.
Definition: LeafNodeBool.h:327
void visit(VisitorOp &)
Definition: LeafNode.h:1994
CoordBBox getNodeBoundingBox() const
Return the bounding box of this node, i.e., the full index space spanned by this leaf node...
Definition: LeafNodeBool.h:211
ValueAllCIter beginValueAll() const
Definition: LeafNodeBool.h:690
const bool & getValueAndCache(const Coord &xyz, AccessorT &) const
Return the value of the voxel at the given coordinates.
Definition: LeafNodeBool.h:386
const LeafNode * probeLeafAndCache(const Coord &, AccessorT &) const
Return a const pointer to this node.
Definition: LeafNodeBool.h:586
bool isInactive() const
Return true if all of this node's values are inactive.
Definition: LeafNodeBool.h:466
void setValue(bool value) const
Definition: LeafNodeBool.h:625
ChildOffCIter endChildOff() const
Definition: LeafNodeBool.h:719
ValueOffCIter cbeginValueOff() const
Definition: LeafNodeBool.h:686
void setValueOn(Index offset)
Mark the voxel at the given offset as active but don't change its value.
Definition: LeafNodeBool.h:304
ValueIter(const MaskIterT &iter, NodeT *parent)
Definition: LeafNodeBool.h:617
void setValue(Index i, const ValueType &val)
Set the i'th value of this buffer to the specified value.
Definition: LeafNode.h:185
void topologyDifference(const LeafNode< OtherType, Log2Dim > &other, const ValueType &)
Difference this node's set of active values with the active values of the other node, whose ValueType may be different. So a resulting voxel will be active only if the original voxel is active in this LeafNode and inactive in the other LeafNode.
Definition: LeafNode.h:1840
bool isEmpty() const
Return true if this node has no active voxels.
Definition: LeafNodeBool.h:186
bool operator!=(const Vec3< T0 > &v0, const Vec3< T1 > &v1)
Inequality operator, does exact floating point comparisons.
Definition: Vec3.h:458
void setValueOff(const Coord &xyz)
Mark the voxel at the given coordinates as inactive but don't change its value.
Definition: LeafNodeBool.h:292
ValueOnCIter beginValueOn() const
Definition: LeafNodeBool.h:684
CombineArgs & setARef(const AValueType &a)
Redirect the A value to a new external source.
Definition: Types.h:347
ChildIter(const MaskIterT &iter, NodeT *parent)
Definition: LeafNodeBool.h:641
ChildIter< MaskOnIter, LeafNode > ChildOnIter
Definition: LeafNodeBool.h:676
ValueOnCIter cbeginValueOn() const
Definition: LeafNodeBool.h:683
void modifyValueAndCache(const Coord &xyz, const ModifyOp &op, AccessorT &)
Apply a functor to the value of the voxel at the given coordinates and mark the voxel as active...
Definition: LeafNodeBool.h:416
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:369
void swap(Buffer &other)
Definition: LeafNodeBool.h:109
ValueOffCIter endValueOff() const
Definition: LeafNodeBool.h:697
void readBuffers(std::istream &is, bool fromHalf=false)
Read buffers from a stream.
Definition: LeafNode.h:1539
DenseIter< const LeafNode, const ValueType, ChildAll > ChildAllCIter
Definition: LeafNode.h:558
int32_t Int32
Definition: Types.h:61
bool operator!=(const LeafNode &other) const
Definition: LeafNode.h:464
ChildOnIter endChildOn()
Definition: LeafNodeBool.h:717
void setValue(const Coord &xyz, bool val)
Set the value of the voxel at the given coordinates and mark the voxel as active. ...
Definition: LeafNodeBool.h:309
void resetBackground(const ValueType &oldBackground, const ValueType &newBackground)
Replace inactive occurrences of oldBackground with newBackground, and inactive occurrences of -oldBac...
Definition: LeafNode.h:1747
static Index getLevel()
Definition: LeafNodeBool.h:169
ValueIter< MaskOffIter, const LeafNode, const bool > ValueOffCIter
Definition: LeafNodeBool.h:673
ChildAllCIter beginChildAll() const
Definition: LeafNode.h:589
void visitActiveBBox(BBoxOp &) const
Calls the templated functor BBoxOp with bounding box information. An additional level argument is pro...
Definition: LeafNode.h:1971
bool isChildMaskOff() const
Definition: LeafNodeBool.h:737
void fill(const ValueType &val)
Populate this buffer with a constant value.
Definition: LeafNode.h:170
ChildAllCIter cendChildAll() const
Definition: LeafNodeBool.h:721
bool hasSameTopology(const LeafNode< OtherType, OtherLog2Dim > *other) const
Return true if the given node (which may have a different ValueType than this node) has the same acti...
Definition: LeafNode.h:1686
SparseIteratorBase< MaskIterT, ValueIter, NodeT, ValueT > BaseT
Definition: LeafNodeBool.h:614
ValueOnIter endValueOn()
Definition: LeafNodeBool.h:695
void combine2(const LeafNode &other, const OtherType &, bool valueIsActive, CombineOp &)
Definition: LeafNode.h:1907
void setValueMask(const NodeMaskType &mask)
Definition: LeafNodeBool.h:734
void setValueMask(Index n, bool on)
Definition: LeafNodeBool.h:739
ValueOnIter beginValueOn()
Definition: LeafNodeBool.h:685
void combine(FloatTreeT &lhsDist, IntTreeT &lhsIndex, FloatTreeT &rhsDist, IntTreeT &rhsIndex)
Definition: MeshToVolume.h:403
const LeafNode * probeConstLeaf(const Coord &) const
Return a const pointer to this node.
Definition: LeafNodeBool.h:587
const LeafNode * probeConstLeafAndCache(const Coord &, AccessorT &) const
Return a const pointer to this node.
Definition: LeafNodeBool.h:589
bool isOn(Index32 n) const
Return true if the nth bit is on.
Definition: NodeMasks.h:455
void unsetItem(Index pos, const ValueT &val) const
Definition: LeafNodeBool.h:666
Templated block class to hold specific data types and a fixed number of values determined by Log2Dim...
Definition: LeafNode.h:65
void getNodes(ArrayT &) const
This function exists only to enable template instantiation.
Definition: LeafNodeBool.h:557
Index memUsage() const
Definition: LeafNodeBool.h:111
Index memUsage() const
Return the memory footprint of this buffer in bytes.
Definition: LeafNode.h:250
ChildIter< MaskOffIter, LeafNode > ChildOffIter
Definition: LeafNodeBool.h:678
LeafNode * probeLeaf(const Coord &)
Return a pointer to this node.
Definition: LeafNodeBool.h:570
static Index size()
Definition: LeafNodeBool.h:112
NodeMaskType & getValueMask()
Definition: LeafNodeBool.h:733
BaseT::NonConstValueType NonConstValueT
Definition: LeafNodeBool.h:650
void setValueOff(Index offset)
Mark the voxel at the given offset as inactive but don't change its value.
Definition: LeafNodeBool.h:294
bool isChildMaskOff(Index) const
Definition: LeafNodeBool.h:736
#define OPENVDB_VERSION_NAME
Definition: version.h:43
Index64 onLeafVoxelCount() const
Definition: LeafNodeBool.h:180
Definition: Types.h:263
ValueType * mData
Definition: LeafNode.h:327
void setActiveState(const Coord &xyz, bool on)
Set the active state of the voxel at the given coordinates but don't change its value.
Definition: LeafNode.h:1302
static void evalNodeOrigin(Coord &xyz)
Compute the origin of the leaf node that contains the voxel with the given coordinates.
Definition: LeafNodeBool.h:744
OPENVDB_API uint32_t getFormatVersion(std::ios_base &)
Return the file format version number associated with the given input stream.
bool isValueOnAndCache(const Coord &xyz, AccessorT &) const
Return true if the voxel at the given coordinates is active.
Definition: LeafNodeBool.h:391
Index64 offLeafVoxelCount() const
Definition: LeafNodeBool.h:181
static bool hasActiveTiles()
Return false since leaf nodes never contain tiles.
Definition: LeafNodeBool.h:337
Index32 Index
Definition: Types.h:59
void setValueMaskOn(Index n)
Definition: LeafNodeBool.h:740
~LeafNode()
Destructor.
Definition: LeafNode.h:1197
bool isOff(Index32 n) const
Return true if the nth bit is off.
Definition: NodeMasks.h:461
const Buffer & buffer() const
Definition: LeafNodeBool.h:248
const bool & operator[](Index i) const
Definition: LeafNodeBool.h:102
static Index log2dim()
Return log2 of the size of the buffer storage.
Definition: LeafNodeBool.h:164
ChildOffIter beginChildOff()
Definition: LeafNodeBool.h:710
void swap(Buffer &other)
Exchange this node's data buffer with the given data buffer without changing the active states of the...
Definition: LeafNodeBool.h:247
static void getNodeLog2Dims(std::vector< Index > &dims)
Definition: LeafNodeBool.h:170
This struct collects both input and output arguments to "grid combiner" functors used with the tree::...
Definition: Types.h:309
ChildOnCIter cendChildOn() const
Definition: LeafNodeBool.h:715
void clip(const CoordBBox &, const ValueType &background)
Set all voxels that lie outside the given axis-aligned box to the background.
Definition: LeafNode.h:1328
std::string str() const
Return a string representation of this node.
Definition: LeafNode.h:1204
void setActiveState(Index offset, bool on)
Set the active state of the voxel at the given offset but don't change its value. ...
Definition: LeafNodeBool.h:284
OffIterator beginOff() const
Definition: NodeMasks.h:351
bool isChildMaskOn(Index) const
Definition: LeafNodeBool.h:735
Buffer(const NodeMaskType &other)
Definition: LeafNodeBool.h:89
static const bool sOn
Definition: LeafNodeBool.h:766
Buffer(bool on)
Definition: LeafNodeBool.h:88
bool getItem(Index pos, void *&child, NonConstValueT &value) const
Definition: LeafNodeBool.h:655
Definition: NodeMasks.h:236
Definition: Exceptions.h:39
void visit2Node(OtherLeafNodeType &other, VisitorOp &)
Definition: LeafNode.h:2026
static Index dim()
Return the number of voxels in each dimension.
Definition: LeafNodeBool.h:166
static Index getValueLevelAndCache(const Coord &, AccessorT &)
Return the LEVEL (=0) at which leaf node values reside.
Definition: LeafNodeBool.h:450
const NodeMaskType & getValueMask() const
Definition: LeafNodeBool.h:732
#define OPENVDB_NO_UNREACHABLE_CODE_WARNING_BEGIN
Definition: Platform.h:121
const ValueType & getValue(const Coord &xyz) const
Return the value of the voxel at the given coordinates.
Definition: LeafNode.h:1252
static Index64 onTileCount()
Definition: LeafNodeBool.h:182
void set(Index32 n, bool On)
Set the nth bit to the specified state.
Definition: NodeMasks.h:415
OPENVDB_API Hermite min(const Hermite &, const Hermite &)
min and max operations done directly on the compressed data.
bool probeValueAndCache(const Coord &xyz, bool &val, AccessorT &) const
Return true if the voxel at the given coordinates is active and return the voxel value in val...
Definition: LeafNodeBool.h:442
void addTileAndCache(Index, const Coord &, const ValueType &, bool, AccessorT &)
Definition: LeafNode.h:1735
ValueOnCIter cbeginValueOn() const
Definition: LeafNode.h:560
void readTopology(std::istream &is, bool fromHalf=false)
Read in just the topology.
Definition: LeafNode.h:1477
static Index32 memUsage()
Return the byte size of this NodeMask.
Definition: NodeMasks.h:394
void topologyUnion(const LeafNode< OtherType, Log2Dim > &other)
Union this node's set of active values with the active values of the other node, whose ValueType may ...
Definition: LeafNode.h:1823
Index64 offVoxelCount() const
Return the number of inactive voxels.
Definition: LeafNodeBool.h:179
bool isValueMaskOn() const
Definition: LeafNodeBool.h:729
Base class for dense iterators over internal and leaf nodes.
Definition: Iterator.h:211
Coord mOrigin
Global grid index coordinates (x,y,z) of the local origin of this node.
Definition: LeafNodeBool.h:763
void combine(const LeafNode &other, CombineOp &op)
Definition: LeafNode.h:1865
static Index32 nonLeafCount()
Definition: LeafNodeBool.h:174
void setValueOnly(Index offset, bool val)
Set the value of the voxel at the given offset but don't change its active state. ...
Definition: LeafNodeBool.h:289
OPENVDB_STATIC_SPECIALIZATION GridType::Ptr clip(const GridType &grid, const BBoxd &)
Clip the given grid against a world-space bounding box and return a new grid containing the result...
Definition: Clip.h:364
ValueOffCIter beginValueOff() const
Definition: LeafNodeBool.h:687
NodeMaskType mValueMask
Bitmask that determines which voxels are active.
Definition: LeafNodeBool.h:759
ValueAllCIter cbeginValueAll() const
Definition: LeafNodeBool.h:689
Definition: NodeMasks.h:267
bool allocate()
Allocate memory for this node's buffer if it has not already been allocated.
Definition: LeafNodeBool.h:198
boost::shared_ptr< LeafNodeType > Ptr
Definition: LeafNodeBool.h:58
void fill(bool val)
Definition: LeafNodeBool.h:92
void copyToDense(const CoordBBox &bbox, DenseT &dense) const
Copy into a dense grid the values of the voxels that lie within a given bounding box.
Definition: LeafNode.h:1409
ValueAllIter beginValueAll()
Definition: LeafNodeBool.h:691
bool operator==(const Buffer &other) const
Definition: LeafNodeBool.h:104
Base class for sparse iterators over internal and leaf nodes.
Definition: Iterator.h:148
const bool & getItem(Index pos) const
Definition: LeafNodeBool.h:619
bool isValueMaskOff() const
Definition: LeafNodeBool.h:731
ChildOffCIter beginChildOff() const
Definition: LeafNodeBool.h:709
static void doVisit2(NodeT &self, OtherChildAllIterT &, VisitorOp &, bool otherIsLHS)
Definition: LeafNode.h:2096
OPENVDB_API const void * getGridBackgroundValuePtr(std::ios_base &)
Return a pointer to the background value of the grid currently being read from or written to the give...
void setItem(Index pos, bool value) const
Definition: LeafNodeBool.h:623
ChildIter< MaskOnIter, const LeafNode > ChildOnCIter
Definition: LeafNodeBool.h:677
ValueAllCIter endValueAll() const
Definition: LeafNodeBool.h:700
static Index getValueLevel(const Coord &)
Return the level (0) at which leaf node values reside.
Definition: LeafNodeBool.h:279
ChildAllIter beginChildAll()
Definition: LeafNodeBool.h:713
bool probeValue(const Coord &xyz, ValueType &val) const
Return true if the voxel at the given coordinates is active.
Definition: LeafNode.h:1268
bool isValueOn(Index offset) const
Return true if the voxel at the given offset is active.
Definition: LeafNodeBool.h:334
static const Index SIZE
Definition: LeafNode.h:79
void getOrigin(Coord &origin) const
Return the grid index coordinates of this node's local origin.
Definition: LeafNodeBool.h:218
bool operator==(const Vec3< T0 > &v0, const Vec3< T1 > &v1)
Equality operator, does exact floating point comparisons.
Definition: Vec3.h:450
void setOff(Index32 n)
Set the nth bit off.
Definition: NodeMasks.h:410
static Index size()
Definition: LeafNodeBool.h:167
void setValue(Index i, bool val)
Definition: LeafNodeBool.h:107
bool isValueOn(const Coord &xyz) const
Return true if the voxel at the given coordinates is active.
Definition: LeafNodeBool.h:332
void setValueMaskOff(Index n)
Definition: LeafNodeBool.h:741
#define OPENVDB_NO_UNREACHABLE_CODE_WARNING_END
Definition: Platform.h:122
void setOn(Index32 n)
Set the nth bit on.
Definition: NodeMasks.h:405
Definition: NodeMasks.h:205
bool isValueMaskOn(Index n) const
Definition: LeafNodeBool.h:728
void setActiveStateAndCache(const Coord &xyz, bool on, AccessorT &)
Set the active state of the voxel at the given coordinates without changing its value.
Definition: LeafNodeBool.h:433
Index64 memUsage() const
Return the memory in bytes occupied by this node.
Definition: LeafNode.h:1658
const bool & getValue() const
Definition: LeafNodeBool.h:620
ChildOnCIter endChildOn() const
Definition: LeafNodeBool.h:716
static void doVisit2Node(NodeT &self, OtherNodeT &other, VisitorOp &)
Definition: LeafNode.h:2051
LeafNode()
Default constructor.
Definition: LeafNode.h:1111
void setValueOffAndCache(const Coord &xyz, bool value, AccessorT &)
Change the value of the voxel at the given coordinates and mark it as inactive.
Definition: LeafNodeBool.h:407
ValueAllIter endValueAll()
Definition: LeafNodeBool.h:701
LeafNode specialization for values of type bool that stores both the active states and the values of ...
Definition: LeafNodeBool.h:54
DenseIter< LeafNode, bool > ChildAllIter
Definition: LeafNodeBool.h:680
Definition: PointIndexGrid.h:68
Index64 onVoxelCount() const
Return the number of active voxels.
Definition: LeafNodeBool.h:177
ValueOffCIter cendValueOff() const
Definition: LeafNodeBool.h:696
OnIterator beginOn() const
Definition: NodeMasks.h:349
void modifyItem(Index n, const ModifyOp &op) const
Definition: LeafNodeBool.h:629
static Index32 leafCount()
Definition: LeafNodeBool.h:173
ChildOnCIter beginChildOn() const
Definition: LeafNodeBool.h:706
const NodeT * probeConstNodeAndCache(const Coord &, AccessorT &) const
Return a const pointer to this node.
Definition: LeafNodeBool.h:591
void setValueOnly(const Coord &xyz, const ValueType &val)
Set the value of the voxel at the given coordinates but don't change its active state.
Definition: LeafNode.h:1310
static Index numValues()
Definition: LeafNodeBool.h:168
static const Index LOG2DIM
Definition: LeafNode.h:74
ValueOffIter endValueOff()
Definition: LeafNodeBool.h:698
NodeT * stealNode(const Coord &, const ValueType &, bool)
This function exists only to enable template instantiation.
Definition: LeafNodeBool.h:552
void evalActiveBoundingBox(CoordBBox &bbox, bool visitVoxels=true) const
Definition: LeafNode.h:1668
Bit mask for the internal and leaf nodes of VDB. This is a 64-bit implementation. ...
Definition: NodeMasks.h:304
Buffer & operator=(const Buffer &b)
Definition: LeafNodeBool.h:93
DenseIter< LeafNode, ValueType, ChildAll > ChildAllIter
Definition: LeafNode.h:557
const bool & getLastValue() const
Return a const reference to the last entry in the buffer.
Definition: LeafNodeBool.h:459
NodeMaskType::OnIterator MaskOnIter
Definition: LeafNodeBool.h:604
ChildAllCIter endChildAll() const
Definition: LeafNodeBool.h:722
DenseIteratorBase< MaskDenseIter, DenseIter, NodeT, void, ValueT > BaseT
Definition: LeafNodeBool.h:649
void copyFromDense(const CoordBBox &bbox, const DenseT &dense, const ValueType &background, const ValueType &tolerance)
Copy from a dense grid into this node the values of the voxels that lie within a given bounding box...
Definition: LeafNode.h:1438
uint32_t Index32
Definition: Types.h:57
Definition: Types.h:426
void voxelizeActiveTiles()
Definition: LeafNodeBool.h:476
ValueIter< MaskOnIter, LeafNode, const bool > ValueOnIter
Definition: LeafNodeBool.h:670
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:71
void visit2(IterT &otherIter, VisitorOp &, bool otherIsLHS=false)
Definition: LeafNode.h:2072
void setValueOff(const Coord &xyz)
Mark the voxel at the given coordinates as inactive but don't change its value.
Definition: LeafNode.h:670
const NodeT * probeConstNode(const Coord &) const
This function exists only to enable template instantiation.
Definition: LeafNodeBool.h:556
boost::remove_const< UnsetItemT >::type NonConstValueType
Definition: Iterator.h:217
bool ValueType
Definition: LeafNodeBool.h:59
NodeMaskType::DenseIterator MaskDenseIter
Definition: LeafNodeBool.h:606
static Index64 offTileCount()
Definition: LeafNodeBool.h:183
Buffer mBuffer
Bitmask representing the values of voxels.
Definition: LeafNodeBool.h:761
bool isZero(const Type &x)
Return true if x is exactly equal to zero.
Definition: Math.h:314
LeafNode * touchLeafAndCache(const Coord &, AccessorT &)
Return a pointer to this node.
Definition: LeafNodeBool.h:569
bool isDense() const
Return true if this node only contains active voxels.
Definition: LeafNodeBool.h:188
static void doVisit(NodeT &, VisitorOp &)
Definition: LeafNode.h:2012
void setValuesOff()
Mark all voxels as inactive but don't change their values.
Definition: LeafNodeBool.h:329
const bool & getFirstValue() const
Return a const reference to the first entry in the buffer.
Definition: LeafNodeBool.h:455
static const bool sOff
Definition: LeafNodeBool.h:767
ValueIter< MaskOffIter, LeafNode, const bool > ValueOffIter
Definition: LeafNodeBool.h:672
bool operator!=(const Buffer &other) const
Definition: LeafNodeBool.h:105
void topologyIntersection(const LeafNode< OtherType, Log2Dim > &other, const ValueType &)
Intersect this node's set of active values with the active values of the other node, whose ValueType may be different. So a resulting voxel will be active only if both of the original voxels were active.
Definition: LeafNode.h:1831
void setValueOnlyAndCache(const Coord &xyz, bool val, AccessorT &)
Change the value of the voxel at the given coordinates but preserve its state.
Definition: LeafNodeBool.h:402
Base class for iterators over internal and leaf nodes.
Definition: Iterator.h:58
ValueOnCIter endValueOn() const
Definition: LeafNodeBool.h:694
void negate()
Definition: LeafNodeBool.h:470
void merge(const LeafNode &)
Definition: LeafNode.h:1770
void modifyValueAndActiveState(const Coord &xyz, const ModifyOp &op)
Apply a functor to the voxel at the given coordinates.
Definition: LeafNode.h:715
NodeT * probeNodeAndCache(const Coord &, AccessorT &)
Return a pointer to this node.
Definition: LeafNodeBool.h:574
bool isConstant(ValueType &constValue, bool &state, const ValueType &tolerance=zeroVal< ValueType >()) const
Definition: LeafNode.h:1695
Buffer(const Buffer &other)
Definition: LeafNodeBool.h:90
void fill(const CoordBBox &bbox, const ValueType &, bool active=true)
Set all voxels within an axis-aligned box to the specified value and active state.
Definition: LeafNode.h:1368
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:314
void modifyValue(const ModifyOp &op) const
Definition: LeafNodeBool.h:632
void writeTopology(std::ostream &os, bool toHalf=false) const
Write out just the topology.
Definition: LeafNode.h:1485