OpenVDB  2.0.0
LeafNode.h
Go to the documentation of this file.
1 //
3 // Copyright (c) 2012-2013 DreamWorks Animation LLC
4 //
5 // All rights reserved. This software is distributed under the
6 // Mozilla Public License 2.0 ( http://www.mozilla.org/MPL/2.0/ )
7 //
8 // Redistributions of source code must retain the above copyright
9 // and license notice and the following restrictions and disclaimer.
10 //
11 // * Neither the name of DreamWorks Animation nor the names of
12 // its contributors may be used to endorse or promote products derived
13 // from this software without specific prior written permission.
14 //
15 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY INDIRECT, INCIDENTAL,
20 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 // IN NO EVENT SHALL THE COPYRIGHT HOLDERS' AND CONTRIBUTORS' AGGREGATE
27 // LIABILITY FOR ALL CLAIMS REGARDLESS OF THEIR BASIS EXCEED US$250.00.
28 //
30 
31 #ifndef OPENVDB_TREE_LEAFNODE_HAS_BEEN_INCLUDED
32 #define OPENVDB_TREE_LEAFNODE_HAS_BEEN_INCLUDED
33 
34 #include <iostream>
35 #include <algorithm> // for std::swap
36 #include <cstring> // for std::memcpy()
37 #include <boost/shared_ptr.hpp>
38 #include <boost/static_assert.hpp>
39 #include <boost/bind.hpp>
40 #include <tbb/blocked_range.h>
41 #include <tbb/parallel_for.h>
42 #include <openvdb/Types.h>
43 #include <openvdb/util/NodeMasks.h>
44 #include <openvdb/io/Compression.h> // for io::readData(), etc.
45 #include "Iterator.h"
46 #include "Util.h"
47 
48 
49 class TestLeaf;
50 template<typename> class TestLeafIO;
51 
52 namespace openvdb {
54 namespace OPENVDB_VERSION_NAME {
55 namespace tree {
56 
61 template<typename T, Index Log2Dim>
62 class LeafNode
63 {
64 public:
65  typedef T ValueType;
67  typedef boost::shared_ptr<LeafNode> Ptr;
69 
70  static const Index
71  LOG2DIM = Log2Dim, // needed by parent nodes
72  TOTAL = Log2Dim, // needed by parent nodes
73  DIM = 1 << TOTAL, // dimension along one coordinate direction
74  NUM_VALUES = 1 << 3 * Log2Dim,
75  NUM_VOXELS = NUM_VALUES, // total number of voxels represented by this node
76  SIZE = NUM_VALUES,
77  LEVEL = 0; // level 0 = leaf
78 
81  template<typename OtherValueType>
82  struct ValueConverter {
84  };
85 
88  class Buffer
89  {
90  public:
92  Buffer(): mData(new ValueType[SIZE]) {}
94  Buffer(const ValueType& val) : mData(new ValueType[SIZE]) { this->fill(val); }
96  Buffer(const Buffer& other) : mData(new ValueType[SIZE]) { *this = other; }
98  ~Buffer() { delete [] mData; }
100  void fill(const ValueType& val)
101  {
102  ValueType* target = mData;
103  Index n = SIZE;
104  while (n--) *target++ = val;
105  }
107  const ValueType& getValue(Index i) const { assert(i < SIZE); return mData[i]; }
109  const ValueType& operator[](Index i) const { return this->getValue(i); }
111  void setValue(Index i, const ValueType& val) { assert(i < SIZE); mData[i] = val; }
113  Buffer& operator=(const Buffer& other)
114  {
115  ValueType* target = mData;
116  const ValueType* source = other.mData;
117  Index n = SIZE;
118  while (n--) *target++ = *source++;
119  return *this;
120  }
123  bool operator==(const Buffer& other) const
124  {
125  const ValueType* target = mData;
126  const ValueType* source = other.mData;
127  Index n = SIZE;
128  while (n && math::isExactlyEqual(*target++, *source++)) --n;
129  return n == 0;
130  }
133  bool operator!=(const Buffer& other) const { return !(other == *this); }
135  void swap(Buffer& other)
136  {
137  ValueType* tmp = mData;
138  mData = other.mData;
139  other.mData = tmp;
140  }
142  static Index memUsage() { return sizeof(ValueType*) + SIZE * sizeof(ValueType); }
144  static Index size() { return SIZE; }
145 
146  private:
149  ValueType& operator[](Index i) { assert(i < SIZE); return mData[i]; }
150 
151  friend class ::TestLeaf;
152  // Allow the parent LeafNode to access this Buffer's data pointer.
153  friend class LeafNode;
154 
155  ValueType* mData;
156  }; // class Buffer
157 
158 
160  LeafNode();
161 
166  explicit LeafNode(const Coord& coords,
167  const ValueType& value = zeroVal<ValueType>(),
168  bool active = false);
169 
171  LeafNode(const LeafNode&);
172 
174  template<typename OtherValueType>
176  const ValueType& offValue, const ValueType& onValue, TopologyCopy);
177 
179  template<typename OtherValueType>
181  const ValueType& background, TopologyCopy);
182 
184  ~LeafNode();
185 
186  //
187  // Statistics
188  //
190  static Index log2dim() { return Log2Dim; }
192  static Index dim() { return DIM; }
194  static Index size() { return SIZE; }
196  static Index numValues() { return SIZE; }
198  static Index getLevel() { return LEVEL; }
200  static void getNodeLog2Dims(std::vector<Index>& dims) { dims.push_back(Log2Dim); }
202  static Index getChildDim() { return 1; }
204  static Index32 leafCount() { return 1; }
206  static Index32 nonLeafCount() { return 0; }
207 
209  Index64 onVoxelCount() const { return mValueMask.countOn(); }
211  Index64 offVoxelCount() const { return mValueMask.countOff(); }
212  Index64 onLeafVoxelCount() const { return onVoxelCount(); }
213  Index64 offLeafVoxelCount() const { return offVoxelCount(); }
214  static Index64 onTileCount() { return 0; }
215  static Index64 offTileCount() { return 0; }
217  bool isEmpty() const { return mValueMask.isOff(); }
219  bool isDense() const { return mValueMask.isOn(); }
220 
222  Index64 memUsage() const;
223 
227  void evalActiveBoundingBox(CoordBBox&, bool visitVoxels = true) const;
228  OPENVDB_DEPRECATED void evalActiveVoxelBoundingBox(CoordBBox&) const;
229 
232  CoordBBox getNodeBoundingBox() const { return CoordBBox::createCube(mOrigin, DIM); }
233 
235  void setOrigin(const Coord& origin) { mOrigin = origin; }
238  OPENVDB_DEPRECATED const Coord& getOrigin() const { return mOrigin; }
240  const Coord& origin() const { return mOrigin; }
242  void getOrigin(Coord& origin) const { origin = mOrigin; }
243  void getOrigin(Int32& x, Int32& y, Int32& z) const { mOrigin.asXYZ(x, y, z); }
245 
247  static Index coordToOffset(const Coord& xyz);
250  static Coord offsetToLocalCoord(Index n);
251  OPENVDB_DEPRECATED static void offsetToLocalCoord(Index n, Coord& xyz);
253  Coord offsetToGlobalCoord(Index n) const;
254 
256  std::string str() const;
257 
260  template<typename OtherType, Index OtherLog2Dim>
261  bool hasSameTopology(const LeafNode<OtherType, OtherLog2Dim>* other) const;
262 
264  bool operator==(const LeafNode& other) const;
265  bool operator!=(const LeafNode& other) const { return !(other == *this); }
266 
267 protected:
271 
272  // Type tags to disambiguate template instantiations
273  struct ValueOn {}; struct ValueOff {}; struct ValueAll {};
274  struct ChildOn {}; struct ChildOff {}; struct ChildAll {};
275 
276  template<typename MaskIterT, typename NodeT, typename ValueT, typename TagT>
277  struct ValueIter:
278  // Derives from SparseIteratorBase, but can also be used as a dense iterator,
279  // if MaskIterT is a dense mask iterator type.
280  public SparseIteratorBase<
281  MaskIterT, ValueIter<MaskIterT, NodeT, ValueT, TagT>, NodeT, ValueT>
282  {
284 
286  ValueIter(const MaskIterT& iter, NodeT* parent): BaseT(iter, parent) {}
287 
288  ValueT& getItem(Index pos) const { return this->parent().getValue(pos); }
289  ValueT& getValue() const { return this->parent().getValue(this->pos()); }
290 
291  // Note: setItem() can't be called on const iterators.
292  void setItem(Index pos, const ValueT& value) const
293  {
294  this->parent().setValueOnly(pos, value);
295  }
296  // Note: setValue() can't be called on const iterators.
297  void setValue(const ValueT& value) const
298  {
299  this->parent().setValueOnly(this->pos(), value);
300  }
301 
302  // Note: modifyItem() can't be called on const iterators.
303  template<typename ModifyOp>
304  void modifyItem(Index n, const ModifyOp& op) const { this->parent().modifyValue(n, op); }
305  // Note: modifyValue() can't be called on const iterators.
306  template<typename ModifyOp>
307  void modifyValue(const ModifyOp& op) const { this->parent().modifyValue(this->pos(), op); }
308  };
309 
311  template<typename MaskIterT, typename NodeT, typename TagT>
312  struct ChildIter:
313  public SparseIteratorBase<MaskIterT, ChildIter<MaskIterT, NodeT, TagT>, NodeT, ValueType>
314  {
316  ChildIter(const MaskIterT& iter, NodeT* parent): SparseIteratorBase<
317  MaskIterT, ChildIter<MaskIterT, NodeT, TagT>, NodeT, ValueType>(iter, parent) {}
318  };
319 
320  template<typename NodeT, typename ValueT, typename TagT>
321  struct DenseIter: public DenseIteratorBase<
322  MaskDenseIterator, DenseIter<NodeT, ValueT, TagT>, NodeT, /*ChildT=*/void, ValueT>
323  {
326 
328  DenseIter(const MaskDenseIterator& iter, NodeT* parent): BaseT(iter, parent) {}
329 
330  bool getItem(Index pos, void*& child, NonConstValueT& value) const
331  {
332  value = this->parent().getValue(pos);
333  child = NULL;
334  return false; // no child
335  }
336 
337  // Note: setItem() can't be called on const iterators.
338  //void setItem(Index pos, void* child) const {}
339 
340  // Note: unsetItem() can't be called on const iterators.
341  void unsetItem(Index pos, const ValueT& value) const
342  {
343  this->parent().setValueOnly(pos, value);
344  }
345  };
346 
347 public:
360 
361  ValueOnCIter cbeginValueOn() const { return ValueOnCIter(mValueMask.beginOn(), this); }
362  ValueOnCIter beginValueOn() const { return ValueOnCIter(mValueMask.beginOn(), this); }
363  ValueOnIter beginValueOn() { return ValueOnIter(mValueMask.beginOn(), this); }
364  ValueOffCIter cbeginValueOff() const { return ValueOffCIter(mValueMask.beginOff(), this); }
365  ValueOffCIter beginValueOff() const { return ValueOffCIter(mValueMask.beginOff(), this); }
366  ValueOffIter beginValueOff() { return ValueOffIter(mValueMask.beginOff(), this); }
367  ValueAllCIter cbeginValueAll() const { return ValueAllCIter(mValueMask.beginDense(), this); }
368  ValueAllCIter beginValueAll() const { return ValueAllCIter(mValueMask.beginDense(), this); }
369  ValueAllIter beginValueAll() { return ValueAllIter(mValueMask.beginDense(), this); }
370 
371  ValueOnCIter cendValueOn() const { return ValueOnCIter(mValueMask.endOn(), this); }
372  ValueOnCIter endValueOn() const { return ValueOnCIter(mValueMask.endOn(), this); }
373  ValueOnIter endValueOn() { return ValueOnIter(mValueMask.endOn(), this); }
374  ValueOffCIter cendValueOff() const { return ValueOffCIter(mValueMask.endOff(), this); }
375  ValueOffCIter endValueOff() const { return ValueOffCIter(mValueMask.endOff(), this); }
376  ValueOffIter endValueOff() { return ValueOffIter(mValueMask.endOff(), this); }
377  ValueAllCIter cendValueAll() const { return ValueAllCIter(mValueMask.endDense(), this); }
378  ValueAllCIter endValueAll() const { return ValueAllCIter(mValueMask.endDense(), this); }
379  ValueAllIter endValueAll() { return ValueAllIter(mValueMask.endDense(), this); }
380 
381  // Note that [c]beginChildOn() and [c]beginChildOff() actually return end iterators,
382  // because leaf nodes have no children.
383  ChildOnCIter cbeginChildOn() const { return ChildOnCIter(mValueMask.endOn(), this); }
384  ChildOnCIter beginChildOn() const { return ChildOnCIter(mValueMask.endOn(), this); }
385  ChildOnIter beginChildOn() { return ChildOnIter(mValueMask.endOn(), this); }
386  ChildOffCIter cbeginChildOff() const { return ChildOffCIter(mValueMask.endOff(), this); }
387  ChildOffCIter beginChildOff() const { return ChildOffCIter(mValueMask.endOff(), this); }
388  ChildOffIter beginChildOff() { return ChildOffIter(mValueMask.endOff(), this); }
389  ChildAllCIter cbeginChildAll() const { return ChildAllCIter(mValueMask.beginDense(), this); }
390  ChildAllCIter beginChildAll() const { return ChildAllCIter(mValueMask.beginDense(), this); }
391  ChildAllIter beginChildAll() { return ChildAllIter(mValueMask.beginDense(), this); }
392 
393  ChildOnCIter cendChildOn() const { return ChildOnCIter(mValueMask.endOn(), this); }
394  ChildOnCIter endChildOn() const { return ChildOnCIter(mValueMask.endOn(), this); }
395  ChildOnIter endChildOn() { return ChildOnIter(mValueMask.endOn(), this); }
396  ChildOffCIter cendChildOff() const { return ChildOffCIter(mValueMask.endOff(), this); }
397  ChildOffCIter endChildOff() const { return ChildOffCIter(mValueMask.endOff(), this); }
398  ChildOffIter endChildOff() { return ChildOffIter(mValueMask.endOff(), this); }
399  ChildAllCIter cendChildAll() const { return ChildAllCIter(mValueMask.endDense(), this); }
400  ChildAllCIter endChildAll() const { return ChildAllCIter(mValueMask.endDense(), this); }
401  ChildAllIter endChildAll() { return ChildAllIter(mValueMask.endDense(), this); }
402 
403  //
404  // Buffer management
405  //
408  void swap(Buffer& other) { mBuffer.swap(other); }
409  const Buffer& buffer() const { return mBuffer; }
410  Buffer& buffer() { return mBuffer; }
411 
412  //
413  // I/O methods
414  //
418  void readTopology(std::istream& is, bool fromHalf = false);
422  void writeTopology(std::ostream& os, bool toHalf = false) const;
423 
427  void readBuffers(std::istream& is, bool fromHalf = false);
431  void writeBuffers(std::ostream& os, bool toHalf = false) const;
432 
433  size_t streamingSize(bool toHalf = false) const;
434 
435  //
436  // Accessor methods
437  //
439  const ValueType& getValue(const Coord& xyz) const;
441  const ValueType& getValue(Index offset) const;
442 
446  bool probeValue(const Coord& xyz, ValueType& val) const;
450  bool probeValue(Index offset, ValueType& val) const;
451 
453  static Index getValueLevel(const Coord&) { return LEVEL; }
454 
456  void setActiveState(const Coord& xyz, bool on);
458  void setActiveState(Index offset, bool on) { assert(offset<SIZE); mValueMask.set(offset, on); }
459 
461  void setValueOnly(const Coord& xyz, const ValueType& val);
463  void setValueOnly(Index offset, const ValueType& val);
464 
466  void setValueOff(const Coord& xyz) { mValueMask.setOff(LeafNode::coordToOffset(xyz)); }
468  void setValueOff(Index offset) { assert(offset < SIZE); mValueMask.setOff(offset); }
469 
471  void setValueOff(const Coord& xyz, const ValueType& val);
473  void setValueOff(Index offset, const ValueType& val);
474 
476  void setValueOn(const Coord& xyz) { mValueMask.setOn(LeafNode::coordToOffset(xyz)); }
478  void setValueOn(Index offset) { assert(offset < SIZE); mValueMask.setOn(offset); }
480  void setValueOn(const Coord& xyz, const ValueType& val) {
481  this->setValueOn(LeafNode::coordToOffset(xyz), val);
482  }
484  void setValue(const Coord& xyz, const ValueType& val) { this->setValueOn(xyz, val); };
486  void setValueOn(Index offset, const ValueType& val) {
487  mBuffer[offset] = val;
488  mValueMask.setOn(offset);
489  }
490 
493  template<typename ModifyOp>
494  void modifyValue(Index offset, const ModifyOp& op)
495  {
496  op(mBuffer[offset]);
497  mValueMask.setOn(offset);
498  }
501  template<typename ModifyOp>
502  void modifyValue(const Coord& xyz, const ModifyOp& op)
503  {
504  this->modifyValue(this->coordToOffset(xyz), op);
505  }
506 
508  template<typename ModifyOp>
509  void modifyValueAndActiveState(const Coord& xyz, const ModifyOp& op)
510  {
511  const Index offset = this->coordToOffset(xyz);
512  bool state = mValueMask.isOn(offset);
513  op(mBuffer[offset], state);
514  mValueMask.set(offset, state);
515  }
516 
518  void setValuesOn() { mValueMask.setOn(); }
520  void setValuesOff() { mValueMask.setOff(); }
521 
523  bool isValueOn(const Coord& xyz) const {return this->isValueOn(LeafNode::coordToOffset(xyz));}
525  bool isValueOn(Index offset) const { return mValueMask.isOn(offset); }
526 
528  static bool hasActiveTiles() { return false; }
529 
531  void fill(const CoordBBox& bbox, const ValueType&, bool active = true);
532 
534  void fill(const ValueType& value);
536  void fill(const ValueType& value, bool active);
537 
549  template<typename DenseT>
550  void copyToDense(const CoordBBox& bbox, DenseT& dense) const;
551 
568  template<typename DenseT>
569  void copyFromDense(const CoordBBox& bbox, const DenseT& dense,
570  const ValueType& background, const ValueType& tolerance);
571 
574  template<typename AccessorT>
575  const ValueType& getValueAndCache(const Coord& xyz, AccessorT&) const
576  {
577  return this->getValue(xyz);
578  }
579 
582  template<typename AccessorT>
583  bool isValueOnAndCache(const Coord& xyz, AccessorT&) const { return this->isValueOn(xyz); }
584 
587  template<typename AccessorT>
588  void setValueAndCache(const Coord& xyz, const ValueType& val, AccessorT&)
589  {
590  this->setValueOn(xyz, val);
591  }
592 
596  template<typename AccessorT>
597  void setValueOnlyAndCache(const Coord& xyz, const ValueType& val, AccessorT&)
598  {
599  this->setValueOnly(xyz, val);
600  }
601 
605  template<typename ModifyOp, typename AccessorT>
606  void modifyValueAndCache(const Coord& xyz, const ModifyOp& op, AccessorT&)
607  {
608  this->modifyValue(xyz, op);
609  }
610 
613  template<typename ModifyOp, typename AccessorT>
614  void modifyValueAndActiveStateAndCache(const Coord& xyz, const ModifyOp& op, AccessorT&)
615  {
616  this->modifyValueAndActiveState(xyz, op);
617  }
618 
621  template<typename AccessorT>
622  void setValueOffAndCache(const Coord& xyz, const ValueType& value, AccessorT&)
623  {
624  this->setValueOff(xyz, value);
625  }
626 
630  template<typename AccessorT>
631  void setActiveStateAndCache(const Coord& xyz, bool on, AccessorT&)
632  {
633  this->setActiveState(xyz, on);
634  }
635 
639  template<typename AccessorT>
640  bool probeValueAndCache(const Coord& xyz, ValueType& val, AccessorT&) const
641  {
642  return this->probeValue(xyz, val);
643  }
644 
648  template<typename AccessorT>
649  const ValueType& getValue(const Coord& xyz, bool& state, int& level, AccessorT&) const
650  {
651  const Index offset = this->coordToOffset(xyz);
652  state = mValueMask.isOn(offset);
653  level = LEVEL;
654  return mBuffer[offset];
655  }
656 
659  template<typename AccessorT>
660  static Index getValueLevelAndCache(const Coord&, AccessorT&) { return LEVEL; }
661 
665  const ValueType& getFirstValue() const { return mBuffer[0]; }
667  const ValueType& getLastValue() const { return mBuffer[SIZE - 1]; }
668 
671  void resetBackground(const ValueType& oldBackground, const ValueType& newBackground);
672 
678  void signedFloodFill(const ValueType& background);
679 
685  void signedFloodFill(const ValueType& outside, const ValueType& inside);
686 
687  void negate();
688 
690 
691  template<MergePolicy Policy> void merge(const LeafNode&);
692  template<MergePolicy Policy> void merge(const ValueType& tileValue, bool tileActive);
693  template<MergePolicy Policy>
694  void merge(const LeafNode& other, const ValueType& /*bg*/, const ValueType& /*otherBG*/);
695 
702  template<typename OtherType>
703  void topologyUnion(const LeafNode<OtherType, Log2Dim>& other);
704 
716  template<typename OtherType>
717  void topologyIntersection(const LeafNode<OtherType, Log2Dim>& other, const ValueType&);
718 
730  template<typename OtherType>
731  void topologyDifference(const LeafNode<OtherType, Log2Dim>& other, const ValueType&);
732 
733  template<typename CombineOp>
734  void combine(const LeafNode& other, CombineOp& op);
735  template<typename CombineOp>
736  void combine(const ValueType& value, bool valueIsActive, CombineOp& op);
737 
738  template<typename CombineOp>
739  void combine2(const LeafNode& other, const ValueType&, bool valueIsActive, CombineOp&);
740  template<typename CombineOp>
741  void combine2(const ValueType&, const LeafNode& other, bool valueIsActive, CombineOp&);
742  template<typename CombineOp>
743  void combine2(const LeafNode& b0, const LeafNode& b1, CombineOp&);
744 
750  template<typename BBoxOp> void visitActiveBBox(BBoxOp&) const;
751 
752  template<typename VisitorOp> void visit(VisitorOp&);
753  template<typename VisitorOp> void visit(VisitorOp&) const;
754 
755  template<typename OtherLeafNodeType, typename VisitorOp>
756  void visit2Node(OtherLeafNodeType& other, VisitorOp&);
757  template<typename OtherLeafNodeType, typename VisitorOp>
758  void visit2Node(OtherLeafNodeType& other, VisitorOp&) const;
759  template<typename IterT, typename VisitorOp>
760  void visit2(IterT& otherIter, VisitorOp&, bool otherIsLHS = false);
761  template<typename IterT, typename VisitorOp>
762  void visit2(IterT& otherIter, VisitorOp&, bool otherIsLHS = false) const;
763 
765  template<typename PruneOp> void pruneOp(PruneOp&) {}
767  void prune(const ValueType& /*tolerance*/ = zeroVal<ValueType>()) {}
768  void pruneInactive(const ValueType&) {}
769  void addLeaf(LeafNode*) {}
770  template<typename AccessorT>
771  void addLeafAndCache(LeafNode*, AccessorT&) {}
772  template<typename NodeT>
773  NodeT* stealNode(const Coord&, const ValueType&, bool) { return NULL; }
774  template<typename NodeT>
775  NodeT* probeNode(const Coord&) { return NULL; }
776  template<typename NodeT>
777  const NodeT* probeConstNode(const Coord&) const { return NULL; }
779 
780  void addTile(Index level, const Coord&, const ValueType&, bool);
781  void addTile(Index offset, const ValueType&, bool);
782  template<typename AccessorT>
783  void addTileAndCache(Index, const Coord&, const ValueType&, bool, AccessorT&);
784 
786  LeafNode* touchLeaf(const Coord&) { return this; }
788  template<typename AccessorT>
789  LeafNode* touchLeafAndCache(const Coord&, AccessorT&) { return this; }
790  template<typename NodeT, typename AccessorT>
791  NodeT* probeNodeAndCache(const Coord&, AccessorT&)
792  {
794  if (!(boost::is_same<NodeT,LeafNode>::value)) return NULL;
795  return reinterpret_cast<NodeT*>(this);
797  }
798  LeafNode* probeLeaf(const Coord&) { return this; }
799  template<typename AccessorT>
800  LeafNode* probeLeafAndCache(const Coord&, AccessorT&) { return this; }
802 
803  const LeafNode* probeConstLeaf(const Coord&) const { return this; }
805  template<typename AccessorT>
806  const LeafNode* probeConstLeafAndCache(const Coord&, AccessorT&) const { return this; }
807  template<typename AccessorT>
808  const LeafNode* probeLeafAndCache(const Coord&, AccessorT&) const { return this; }
809  const LeafNode* probeLeaf(const Coord&) const { return this; }
810  template<typename NodeT, typename AccessorT>
811  const NodeT* probeConstNodeAndCache(const Coord&, AccessorT&) const
812  {
814  if (!(boost::is_same<NodeT,LeafNode>::value)) return NULL;
815  return reinterpret_cast<const NodeT*>(this);
817  }
819 
823  bool isConstant(ValueType& constValue, bool& state,
824  const ValueType& tolerance = zeroVal<ValueType>()) const;
826  bool isInactive() const { return mValueMask.isOff(); }
827 
828 protected:
829  friend class ::TestLeaf;
830  template<typename> friend class ::TestLeafIO;
831 
832  // During topology-only construction, access is needed
833  // to protected/private members of other template instances.
834  template<typename, Index> friend class LeafNode;
835 
842 
843  // Allow iterators to call mask accessor methods (see below).
848 
854  Coord mOrigin;
855 
856  // Mask accessors
857 public:
858  bool isValueMaskOn(Index n) const { return mValueMask.isOn(n); }
859  bool isValueMaskOn() const { return mValueMask.isOn(); }
860  bool isValueMaskOff(Index n) const { return mValueMask.isOff(n); }
861  bool isValueMaskOff() const { return mValueMask.isOff(); }
862  const NodeMaskType& getValueMask() const { return mValueMask; }
863  NodeMaskType& getValueMask() { return mValueMask; }
864  void setValueMask(const NodeMaskType& mask) { mValueMask = mask; }
865  bool isChildMaskOn(Index) const { return false; } // leaf nodes have no children
866  bool isChildMaskOff(Index) const { return true; }
867  bool isChildMaskOff() const { return true; }
868 protected:
869  void setValueMask(Index n, bool on) { mValueMask.set(n, on); }
870  void setValueMaskOn(Index n) { mValueMask.setOn(n); }
871  void setValueMaskOff(Index n) { mValueMask.setOff(n); }
872 
874  static void evalNodeOrigin(Coord& xyz) { xyz &= ~(DIM - 1); }
875 
876  template<typename NodeT, typename VisitorOp, typename ChildAllIterT>
877  static inline void doVisit(NodeT&, VisitorOp&);
878 
879  template<typename NodeT, typename OtherNodeT, typename VisitorOp,
880  typename ChildAllIterT, typename OtherChildAllIterT>
881  static inline void doVisit2Node(NodeT& self, OtherNodeT& other, VisitorOp&);
882 
883  template<typename NodeT, typename VisitorOp,
884  typename ChildAllIterT, typename OtherChildAllIterT>
885  static inline void doVisit2(NodeT& self, OtherChildAllIterT&, VisitorOp&, bool otherIsLHS);
886 
887 }; // end of LeafNode class
888 
889 
891 
892 template<typename T, Index Log2Dim>
893 inline
895  mValueMask(),//default is off!
896  mOrigin(0, 0, 0)
897 {
898 }
899 
900 
901 template<typename T, Index Log2Dim>
902 inline
903 LeafNode<T, Log2Dim>::LeafNode(const Coord& xyz, const ValueType& val, bool active):
904  mBuffer(val),
905  mValueMask(active),
906  mOrigin(xyz & (~(DIM - 1)))
907 {
908 }
909 
910 template<typename T, Index Log2Dim>
911 template<typename OtherValueType>
912 inline
914  const ValueType& background, TopologyCopy):
915  mBuffer(background),
916  mValueMask(other.mValueMask),
917  mOrigin(other.mOrigin)
918 {
919 }
920 
921 template<typename T, Index Log2Dim>
922 template<typename OtherValueType>
923 inline
925  const ValueType& offValue, const ValueType& onValue, TopologyCopy):
926  mValueMask(other.mValueMask),
927  mOrigin(other.mOrigin)
928 {
929  for (Index i = 0; i < SIZE; ++i) {
930  mBuffer[i] = (mValueMask.isOn(i) ? onValue : offValue);
931  }
932 }
933 
934 template<typename T, Index Log2Dim>
935 inline
937  mBuffer(other.mBuffer),
938  mValueMask(other.mValueMask),
939  mOrigin(other.mOrigin)
940 {
941 }
942 
943 
944 template<typename T, Index Log2Dim>
945 inline
947 {
948 }
949 
950 template<typename T, Index Log2Dim>
951 inline std::string
953 {
954  std::ostringstream ostr;
955  ostr << "LeafNode @" << mOrigin << ": " << mBuffer;
956  return ostr.str();
957 }
958 
959 
961 
962 
963 template<typename T, Index Log2Dim>
964 inline Index
966 {
967  assert ((xyz[0]&DIM-1u)<DIM && (xyz[1]&DIM-1u)<DIM && (xyz[2]&DIM-1u)<DIM);
968  return ((xyz[0]&DIM-1u)<<2*Log2Dim)
969  + ((xyz[1]&DIM-1u)<< Log2Dim)
970  + (xyz[2]&DIM-1u);
971 }
972 
973 template<typename T, Index Log2Dim>
974 inline Coord
976 {
977  assert(n<(1<< 3*Log2Dim));
978  Coord xyz;
979  xyz.setX(n >> 2*Log2Dim);
980  n &= ((1<<2*Log2Dim)-1);
981  xyz.setY(n >> Log2Dim);
982  xyz.setZ(n & ((1<<Log2Dim)-1));
983  return xyz;
984 }
985 
986 //deprecated
987 template<typename T, Index Log2Dim>
988 inline void
990 {
991  assert(n<(1<< 3*Log2Dim));
992  xyz.setX(n >> 2*Log2Dim);
993  n &= ((1<<2*Log2Dim)-1);
994  xyz.setY(n >> Log2Dim);
995  xyz.setZ(n & ((1<<Log2Dim)-1));
996 }
997 
998 
999 template<typename T, Index Log2Dim>
1000 inline Coord
1002 {
1003  return (this->offsetToLocalCoord(n) + this->origin());
1004 }
1005 
1006 
1008 
1009 
1010 template<typename ValueT, Index Log2Dim>
1011 inline const ValueT&
1013 {
1014  return this->getValue(LeafNode::coordToOffset(xyz));
1015 }
1016 
1017 template<typename ValueT, Index Log2Dim>
1018 inline const ValueT&
1020 {
1021  assert(offset < SIZE);
1022  return mBuffer[offset];
1023 }
1024 
1025 
1026 template<typename T, Index Log2Dim>
1027 inline bool
1028 LeafNode<T, Log2Dim>::probeValue(const Coord& xyz, ValueType& val) const
1029 {
1030  return this->probeValue(LeafNode::coordToOffset(xyz), val);
1031 }
1032 
1033 template<typename T, Index Log2Dim>
1034 inline bool
1036 {
1037  assert(offset < SIZE);
1038  val = mBuffer[offset];
1039  return mValueMask.isOn(offset);
1040 }
1041 
1042 
1043 template<typename T, Index Log2Dim>
1044 inline void
1045 LeafNode<T, Log2Dim>::setValueOff(const Coord& xyz, const ValueType& val)
1046 {
1047  this->setValueOff(LeafNode::coordToOffset(xyz), val);
1048 }
1049 
1050 template<typename T, Index Log2Dim>
1051 inline void
1053 {
1054  assert(offset < SIZE);
1055  mBuffer[offset] = val;
1056  mValueMask.setOff(offset);
1057 }
1058 
1059 
1060 template<typename T, Index Log2Dim>
1061 inline void
1062 LeafNode<T, Log2Dim>::setActiveState(const Coord& xyz, bool on)
1063 {
1064  mValueMask.set(this->coordToOffset(xyz), on);
1065 }
1066 
1067 
1068 template<typename T, Index Log2Dim>
1069 inline void
1070 LeafNode<T, Log2Dim>::setValueOnly(const Coord& xyz, const ValueType& val)
1071 {
1072  this->setValueOnly(LeafNode::coordToOffset(xyz), val);
1073 }
1074 
1075 template<typename T, Index Log2Dim>
1076 inline void
1078 {
1079  assert(offset<SIZE); mBuffer[offset] = val;
1080 }
1081 
1082 
1084 
1085 
1086 template<typename T, Index Log2Dim>
1087 inline void
1088 LeafNode<T, Log2Dim>::fill(const CoordBBox& bbox, const ValueType& value, bool active)
1089 {
1090  for (Int32 x = bbox.min().x(); x <= bbox.max().x(); ++x) {
1091  const Index offsetX = (x&DIM-1u)<<2*Log2Dim;
1092  for (Int32 y = bbox.min().y(); y <= bbox.max().y(); ++y) {
1093  const Index offsetXY = offsetX + ((y&DIM-1u)<< Log2Dim);
1094  for (Int32 z = bbox.min().z(); z <= bbox.max().z(); ++z) {
1095  const Index offset = offsetXY + (z&DIM-1u);
1096  mBuffer[offset] = value;
1097  mValueMask.set(offset, active);
1098  }
1099  }
1100  }
1101 }
1102 
1103 template<typename T, Index Log2Dim>
1104 inline void
1106 {
1107  mBuffer.fill(value);
1108 }
1109 
1110 template<typename T, Index Log2Dim>
1111 inline void
1112 LeafNode<T, Log2Dim>::fill(const ValueType& value, bool active)
1113 {
1114  mBuffer.fill(value);
1115  mValueMask.set(active);
1116 }
1117 
1119 
1120 template<typename T, Index Log2Dim>
1121 template<typename DenseT>
1122 inline void
1123 LeafNode<T, Log2Dim>::copyToDense(const CoordBBox& bbox, DenseT& dense) const
1124 {
1125  typedef typename DenseT::ValueType DenseValueType;
1126 
1127  const size_t xStride = dense.xStride(), yStride = dense.yStride();// zStride=1
1128  const Coord& min = dense.bbox().min();
1129  DenseValueType* t0 = dense.data() + bbox.min()[2]-min[2];//target array
1130  const T* s0 = &mBuffer[bbox.min()[2]&DIM-1u];//source array
1131  for (Int32 x = bbox.min()[0], ex=bbox.max()[0]+1; x<ex; ++x) {
1132  DenseValueType* t1 = t0 + xStride*(x-min[0]);
1133  const T* s1 = s0 + ((x&DIM-1u)<<2*Log2Dim);
1134  for (Int32 y = bbox.min()[1], ey=bbox.max()[1]+1; y<ey; ++y) {
1135  DenseValueType* t2 = t1 + yStride*(y-min[1]);
1136  const T* s2 = s1 + ((y&DIM-1u)<<Log2Dim);
1137  for (Int32 z = bbox.min()[2], ez=bbox.max()[2]+1; z<ez ; ++z) {
1138  *t2++ = DenseValueType(*s2++);
1139  }
1140  }
1141  }
1142 }
1143 
1144 template<typename T, Index Log2Dim>
1145 template<typename DenseT>
1146 inline void
1147 LeafNode<T, Log2Dim>::copyFromDense(const CoordBBox& bbox, const DenseT& dense,
1148  const ValueType& background, const ValueType& tolerance)
1149 {
1150  typedef typename DenseT::ValueType DenseValueType;
1151 
1152  const size_t xStride = dense.xStride(), yStride = dense.yStride();// zStride=1
1153  const Coord& min = dense.bbox().min();
1154 
1155  const DenseValueType* s0 = dense.data() + bbox.min()[2]-min[2];//source
1156  const Int32 n0 = bbox.min()[2]&DIM-1u;
1157  for (Int32 x = bbox.min()[0], ex=bbox.max()[0]+1; x<ex; ++x) {
1158  const DenseValueType* s1 = s0 + xStride*(x-min[0]);
1159  const Int32 n1 = n0 + ((x&DIM-1u)<<2*LOG2DIM);
1160  for (Int32 y = bbox.min()[1], ey=bbox.max()[1]+1; y<ey; ++y) {
1161  const DenseValueType* s2 = s1 + yStride*(y-min[1]);
1162  Int32 n2 = n1 + ((y&DIM-1u)<<LOG2DIM) ;
1163  for (Int32 z = bbox.min()[2], ez=bbox.max()[2]+1; z<ez ; ++z, ++n2, ++s2) {
1164  if (math::isApproxEqual(background, ValueType(*s2), tolerance)) {
1165  mValueMask.setOff(n2);
1166  mBuffer[n2] = background;
1167  } else {
1168  mValueMask.setOn(n2);
1169  mBuffer[n2] = ValueType(*s2);
1170  }
1171  }
1172  }
1173  }
1174 }
1175 
1177 
1178 
1179 template<typename T, Index Log2Dim>
1180 inline void
1181 LeafNode<T, Log2Dim>::readTopology(std::istream& is, bool /*fromHalf*/)
1182 {
1183  mValueMask.load(is);
1184 }
1185 
1186 
1187 template<typename T, Index Log2Dim>
1188 inline void
1189 LeafNode<T, Log2Dim>::writeTopology(std::ostream& os, bool /*toHalf*/) const
1190 {
1191  mValueMask.save(os);
1192 }
1193 
1194 
1196 
1197 
1198 template<typename T, Index Log2Dim>
1199 inline void
1200 LeafNode<T,Log2Dim>::readBuffers(std::istream& is, bool fromHalf)
1201 {
1202  // Read in the value mask.
1203  mValueMask.load(is);
1204 
1205  int8_t numBuffers = 1;
1207  // Read in the origin.
1208  is.read(reinterpret_cast<char*>(&mOrigin), sizeof(Coord::ValueType) * 3);
1209 
1210  // Read in the number of buffers, which should now always be one.
1211  is.read(reinterpret_cast<char*>(&numBuffers), sizeof(int8_t));
1212  }
1213 
1214  io::readCompressedValues(is, mBuffer.mData, SIZE, mValueMask, fromHalf);
1215 
1216  if (numBuffers > 1) {
1217  // Read in and discard auxiliary buffers that were created with earlier
1218  // versions of the library. (Auxiliary buffers are not mask compressed.)
1219  const bool zipped = io::getDataCompression(is) & io::COMPRESS_ZIP;
1220  Buffer temp;
1221  for (int i = 1; i < numBuffers; ++i) {
1222  if (fromHalf) {
1223  io::HalfReader<io::RealToHalf<T>::isReal, T>::read(is, temp.mData, SIZE, zipped);
1224  } else {
1225  io::readData<T>(is, temp.mData, SIZE, zipped);
1226  }
1227  }
1228  }
1229 }
1230 
1231 
1232 template<typename T, Index Log2Dim>
1233 inline void
1234 LeafNode<T, Log2Dim>::writeBuffers(std::ostream& os, bool toHalf) const
1235 {
1236  // Write out the value mask.
1237  mValueMask.save(os);
1238 
1239  io::writeCompressedValues(os, mBuffer.mData, SIZE,
1240  mValueMask, /*childMask=*/NodeMaskType(), toHalf);
1241 }
1242 
1243 
1245 
1246 
1247 template<typename T, Index Log2Dim>
1248 inline bool
1250 {
1251  return mOrigin == other.mOrigin &&
1252  mValueMask == other.mValueMask &&
1253  mBuffer == other.mBuffer;
1254 }
1255 
1256 
1257 template<typename T, Index Log2Dim>
1258 inline Index64
1260 {
1261  return mBuffer.memUsage() + sizeof(mOrigin) + mValueMask.memUsage();
1262 }
1263 
1264 
1265 template<typename T, Index Log2Dim>
1266 inline void
1268 {
1269  CoordBBox this_bbox = this->getNodeBoundingBox();
1270  if (bbox.isInside(this_bbox)) return;//this LeafNode is already enclosed in the bbox
1271  if (ValueOnCIter iter = this->cbeginValueOn()) {//any active values?
1272  this_bbox.reset();
1273  for(; iter; ++iter) this_bbox.expand(this->offsetToLocalCoord(iter.pos()));
1274  this_bbox.translate(this->origin());
1275  bbox.expand(this_bbox);
1276  }
1277 }
1278 
1279 template<typename T, Index Log2Dim>
1280 inline void
1281 LeafNode<T, Log2Dim>::evalActiveBoundingBox(CoordBBox& bbox, bool visitVoxels) const
1282 {
1283  CoordBBox this_bbox = this->getNodeBoundingBox();
1284  if (bbox.isInside(this_bbox)) return;//this LeafNode is already enclosed in the bbox
1285  if (ValueOnCIter iter = this->cbeginValueOn()) {//any active values?
1286  if (visitVoxels) {//use voxel granularity?
1287  this_bbox.reset();
1288  for(; iter; ++iter) this_bbox.expand(this->offsetToLocalCoord(iter.pos()));
1289  this_bbox.translate(this->origin());
1290  }
1291  bbox.expand(this_bbox);
1292  }
1293 }
1294 
1295 
1296 template<typename T, Index Log2Dim>
1297 template<typename OtherType, Index OtherLog2Dim>
1298 inline bool
1300 {
1301  assert(other);
1302  return (Log2Dim == OtherLog2Dim && mValueMask == other->getValueMask());
1303 }
1304 
1305 
1306 template<typename T, Index Log2Dim>
1307 inline bool
1309  bool& state, const ValueType& tolerance) const
1310 {
1311  state = mValueMask.isOn();
1312 
1313  if (!(state || mValueMask.isOff())) return false;
1314 
1315  bool allEqual = true;
1316  const T value = mBuffer[0];
1317  for (Index i = 1; allEqual && i < SIZE; ++i) {
1319  allEqual = math::isApproxEqual(mBuffer[i], value, tolerance);
1320  }
1321  if (allEqual) constValue = value;
1322  return allEqual;
1323 }
1324 
1325 
1327 
1328 
1329 template<typename T, Index Log2Dim>
1330 inline void
1331 LeafNode<T, Log2Dim>::addTile(Index level, const Coord& xyz, const ValueType& val, bool active)
1332 {
1333  assert(level == 0);
1334  this->addTile(this->coordToOffset(xyz), val, active);
1335 }
1336 
1337 template<typename T, Index Log2Dim>
1338 inline void
1339 LeafNode<T, Log2Dim>::addTile(Index offset, const ValueType& val, bool active)
1340 {
1341  assert(offset < SIZE);
1342  setValueOnly(offset, val);
1343  setActiveState(offset, active);
1344 }
1345 
1346 template<typename T, Index Log2Dim>
1347 template<typename AccessorT>
1348 inline void
1350  const ValueType& val, bool active, AccessorT&)
1351 {
1352  this->addTile(level, xyz, val, active);
1353 }
1354 
1355 
1357 
1358 
1359 template<typename T, Index Log2Dim>
1360 inline void
1362 {
1363  this->signedFloodFill(background, math::negative(background));
1364 }
1365 
1366 template<typename T, Index Log2Dim>
1367 inline void
1369  const ValueType& insideValue)
1370 {
1371  const Index first = mValueMask.findFirstOn();
1372  if (first < SIZE) {
1373  bool xInside = math::isNegative(mBuffer[first]), yInside = xInside, zInside = xInside;
1374  for (Index x = 0; x != (1 << Log2Dim); ++x) {
1375  const Index x00 = x << (2 * Log2Dim);
1376  if (mValueMask.isOn(x00)) {
1377  xInside = math::isNegative(mBuffer[x00]); // element(x, 0, 0)
1378  }
1379  yInside = xInside;
1380  for (Index y = 0; y != (1 << Log2Dim); ++y) {
1381  const Index xy0 = x00 + (y << Log2Dim);
1382  if (mValueMask.isOn(xy0)) {
1383  yInside = math::isNegative(mBuffer[xy0]); // element(x, y, 0)
1384  }
1385  zInside = yInside;
1386  for (Index z = 0; z != (1 << Log2Dim); ++z) {
1387  const Index xyz = xy0 + z; // element(x, y, z)
1388  if (mValueMask.isOn(xyz)) {
1389  zInside = math::isNegative(mBuffer[xyz]);
1390  } else {
1391  mBuffer[xyz] = zInside ? insideValue : outsideValue;
1392  }
1393  }
1394  }
1395  }
1396  } else {// if no active voxels exist simply use the sign of the first value
1397  mBuffer.fill(math::isNegative(mBuffer[0]) ? insideValue : outsideValue);
1398  }
1399 }
1400 
1401 
1402 template<typename T, Index Log2Dim>
1403 inline void
1405  const ValueType& newBackground)
1406 {
1407  typename NodeMaskType::OffIterator iter;
1408  // For all inactive values...
1409  for (iter = this->mValueMask.beginOff(); iter; ++iter) {
1410  ValueType &inactiveValue = mBuffer[iter.pos()];
1411  if (math::isApproxEqual(inactiveValue, oldBackground)) {
1412  inactiveValue = newBackground;
1413  } else if (math::isApproxEqual(inactiveValue, math::negative(oldBackground))) {
1414  inactiveValue = math::negative(newBackground);
1415  }
1416  }
1417 }
1418 
1419 
1420 template<typename T, Index Log2Dim>
1421 template<MergePolicy Policy>
1422 inline void
1424 {
1426  if (Policy == MERGE_NODES) return;
1427  typename NodeMaskType::OnIterator iter = other.mValueMask.beginOn();
1428  for (; iter; ++iter) {
1429  const Index n = iter.pos();
1430  if (mValueMask.isOff(n)) {
1431  mBuffer[n] = other.mBuffer[n];
1432  mValueMask.setOn(n);
1433  }
1434  }
1436 }
1437 
1438 template<typename T, Index Log2Dim>
1439 template<MergePolicy Policy>
1440 inline void
1442  const ValueType& /*bg*/, const ValueType& /*otherBG*/)
1443 {
1444  this->template merge<Policy>(other);
1445 }
1446 
1447 template<typename T, Index Log2Dim>
1448 template<MergePolicy Policy>
1449 inline void
1450 LeafNode<T, Log2Dim>::merge(const ValueType& tileValue, bool tileActive)
1451 {
1453  if (Policy != MERGE_ACTIVE_STATES_AND_NODES) return;
1454  if (!tileActive) return;
1455  // Replace all inactive values with the active tile value.
1456  for (typename NodeMaskType::OffIterator iter = mValueMask.beginOff(); iter; ++iter) {
1457  const Index n = iter.pos();
1458  mBuffer[n] = tileValue;
1459  mValueMask.setOn(n);
1460  }
1462 }
1463 
1464 
1465 template<typename T, Index Log2Dim>
1466 template<typename OtherType>
1467 inline void
1469 {
1470  mValueMask |= other.getValueMask();
1471 }
1472 
1473 template<typename T, Index Log2Dim>
1474 template<typename OtherType>
1475 inline void
1477  const ValueType&)
1478 {
1479  mValueMask &= other.getValueMask();
1480 }
1481 
1482 template<typename T, Index Log2Dim>
1483 template<typename OtherType>
1484 inline void
1486  const ValueType&)
1487 {
1488  mValueMask &= !other.getValueMask();
1489 }
1490 
1491 template<typename T, Index Log2Dim>
1492 inline void
1494 {
1495  for (Index i = 0; i < SIZE; ++i) {
1496  mBuffer[i] = -mBuffer[i];
1497  }
1498 }
1499 
1500 
1502 
1503 
1504 template<typename T, Index Log2Dim>
1505 template<typename CombineOp>
1506 inline void
1507 LeafNode<T, Log2Dim>::combine(const LeafNode& other, CombineOp& op)
1508 {
1509  CombineArgs<T> args;
1510  for (Index i = 0; i < SIZE; ++i) {
1511  op(args.setARef(mBuffer[i])
1512  .setAIsActive(mValueMask.isOn(i))
1513  .setBRef(other.mBuffer[i])
1514  .setBIsActive(other.mValueMask.isOn(i))
1515  .setResultRef(mBuffer[i]));
1516  mValueMask.set(i, args.resultIsActive());
1517  }
1518 }
1519 
1520 
1521 template<typename T, Index Log2Dim>
1522 template<typename CombineOp>
1523 inline void
1524 LeafNode<T, Log2Dim>::combine(const ValueType& value, bool valueIsActive, CombineOp& op)
1525 {
1526  CombineArgs<T> args;
1527  args.setBRef(value).setBIsActive(valueIsActive);
1528  for (Index i = 0; i < SIZE; ++i) {
1529  op(args.setARef(mBuffer[i])
1530  .setAIsActive(mValueMask.isOn(i))
1531  .setResultRef(mBuffer[i]));
1532  mValueMask.set(i, args.resultIsActive());
1533  }
1534 }
1535 
1536 
1538 
1539 
1540 template<typename T, Index Log2Dim>
1541 template<typename CombineOp>
1542 inline void
1544  bool valueIsActive, CombineOp& op)
1545 {
1546  CombineArgs<T> args;
1547  args.setBRef(value).setBIsActive(valueIsActive);
1548  for (Index i = 0; i < SIZE; ++i) {
1549  op(args.setARef(other.mBuffer[i])
1550  .setAIsActive(other.mValueMask.isOn(i))
1551  .setResultRef(mBuffer[i]));
1552  mValueMask.set(i, args.resultIsActive());
1553  }
1554 }
1555 
1556 
1557 template<typename T, Index Log2Dim>
1558 template<typename CombineOp>
1559 inline void
1561  bool valueIsActive, CombineOp& op)
1562 {
1563  CombineArgs<T> args;
1564  args.setARef(value).setAIsActive(valueIsActive);
1565  for (Index i = 0; i < SIZE; ++i) {
1566  op(args.setBRef(other.mBuffer[i])
1567  .setBIsActive(other.mValueMask.isOn(i))
1568  .setResultRef(mBuffer[i]));
1569  mValueMask.set(i, args.resultIsActive());
1570  }
1571 }
1572 
1573 
1574 template<typename T, Index Log2Dim>
1575 template<typename CombineOp>
1576 inline void
1577 LeafNode<T, Log2Dim>::combine2(const LeafNode& b0, const LeafNode& b1, CombineOp& op)
1578 {
1579  CombineArgs<T> args;
1580  for (Index i = 0; i < SIZE; ++i) {
1581  mValueMask.set(i, b0.mValueMask.isOn(i) || b1.mValueMask.isOn(i));
1582  op(args.setARef(b0.mBuffer[i])
1583  .setAIsActive(b0.mValueMask.isOn(i))
1584  .setBRef(b1.mBuffer[i])
1585  .setBIsActive(b1.mValueMask.isOn(i))
1586  .setResultRef(mBuffer[i]));
1587  mValueMask.set(i, args.resultIsActive());
1588  }
1589 }
1590 
1591 
1593 
1594 
1595 template<typename T, Index Log2Dim>
1596 template<typename BBoxOp>
1597 inline void
1599 {
1600  if (op.template descent<LEVEL>()) {
1601  for (ValueOnCIter i=this->cbeginValueOn(); i; ++i) {
1602 #ifdef _MSC_VER
1603  op.operator()<LEVEL>(CoordBBox::createCube(i.getCoord(), 1));
1604 #else
1605  op.template operator()<LEVEL>(CoordBBox::createCube(i.getCoord(), 1));
1606 #endif
1607  }
1608  } else {
1609 #ifdef _MSC_VER
1610  op.operator()<LEVEL>(this->getNodeBoundingBox());
1611 #else
1612  op.template operator()<LEVEL>(this->getNodeBoundingBox());
1613 #endif
1614  }
1615 }
1616 
1617 
1618 template<typename T, Index Log2Dim>
1619 template<typename VisitorOp>
1620 inline void
1622 {
1623  doVisit<LeafNode, VisitorOp, ChildAllIter>(*this, op);
1624 }
1625 
1626 
1627 template<typename T, Index Log2Dim>
1628 template<typename VisitorOp>
1629 inline void
1630 LeafNode<T, Log2Dim>::visit(VisitorOp& op) const
1631 {
1632  doVisit<const LeafNode, VisitorOp, ChildAllCIter>(*this, op);
1633 }
1634 
1635 
1636 template<typename T, Index Log2Dim>
1637 template<typename NodeT, typename VisitorOp, typename ChildAllIterT>
1638 inline void
1639 LeafNode<T, Log2Dim>::doVisit(NodeT& self, VisitorOp& op)
1640 {
1641  for (ChildAllIterT iter = self.beginChildAll(); iter; ++iter) {
1642  op(iter);
1643  }
1644 }
1645 
1646 
1648 
1649 
1650 template<typename T, Index Log2Dim>
1651 template<typename OtherLeafNodeType, typename VisitorOp>
1652 inline void
1653 LeafNode<T, Log2Dim>::visit2Node(OtherLeafNodeType& other, VisitorOp& op)
1654 {
1655  doVisit2Node<LeafNode, OtherLeafNodeType, VisitorOp, ChildAllIter,
1656  typename OtherLeafNodeType::ChildAllIter>(*this, other, op);
1657 }
1658 
1659 
1660 template<typename T, Index Log2Dim>
1661 template<typename OtherLeafNodeType, typename VisitorOp>
1662 inline void
1663 LeafNode<T, Log2Dim>::visit2Node(OtherLeafNodeType& other, VisitorOp& op) const
1664 {
1665  doVisit2Node<const LeafNode, OtherLeafNodeType, VisitorOp, ChildAllCIter,
1666  typename OtherLeafNodeType::ChildAllCIter>(*this, other, op);
1667 }
1668 
1669 
1670 template<typename T, Index Log2Dim>
1671 template<
1672  typename NodeT,
1673  typename OtherNodeT,
1674  typename VisitorOp,
1675  typename ChildAllIterT,
1676  typename OtherChildAllIterT>
1677 inline void
1678 LeafNode<T, Log2Dim>::doVisit2Node(NodeT& self, OtherNodeT& other, VisitorOp& op)
1679 {
1680  // Allow the two nodes to have different ValueTypes, but not different dimensions.
1681  BOOST_STATIC_ASSERT(OtherNodeT::SIZE == NodeT::SIZE);
1682  BOOST_STATIC_ASSERT(OtherNodeT::LEVEL == NodeT::LEVEL);
1683 
1684  ChildAllIterT iter = self.beginChildAll();
1685  OtherChildAllIterT otherIter = other.beginChildAll();
1686 
1687  for ( ; iter && otherIter; ++iter, ++otherIter) {
1688  op(iter, otherIter);
1689  }
1690 }
1691 
1692 
1694 
1695 
1696 template<typename T, Index Log2Dim>
1697 template<typename IterT, typename VisitorOp>
1698 inline void
1699 LeafNode<T, Log2Dim>::visit2(IterT& otherIter, VisitorOp& op, bool otherIsLHS)
1700 {
1701  doVisit2<LeafNode, VisitorOp, ChildAllIter, IterT>(
1702  *this, otherIter, op, otherIsLHS);
1703 }
1704 
1705 
1706 template<typename T, Index Log2Dim>
1707 template<typename IterT, typename VisitorOp>
1708 inline void
1709 LeafNode<T, Log2Dim>::visit2(IterT& otherIter, VisitorOp& op, bool otherIsLHS) const
1710 {
1711  doVisit2<const LeafNode, VisitorOp, ChildAllCIter, IterT>(
1712  *this, otherIter, op, otherIsLHS);
1713 }
1714 
1715 
1716 template<typename T, Index Log2Dim>
1717 template<
1718  typename NodeT,
1719  typename VisitorOp,
1720  typename ChildAllIterT,
1721  typename OtherChildAllIterT>
1722 inline void
1723 LeafNode<T, Log2Dim>::doVisit2(NodeT& self, OtherChildAllIterT& otherIter,
1724  VisitorOp& op, bool otherIsLHS)
1725 {
1726  if (!otherIter) return;
1727 
1728  if (otherIsLHS) {
1729  for (ChildAllIterT iter = self.beginChildAll(); iter; ++iter) {
1730  op(otherIter, iter);
1731  }
1732  } else {
1733  for (ChildAllIterT iter = self.beginChildAll(); iter; ++iter) {
1734  op(iter, otherIter);
1735  }
1736  }
1737 }
1738 
1739 
1741 
1742 
1743 template<typename T, Index Log2Dim>
1744 inline std::ostream&
1745 operator<<(std::ostream& os, const typename LeafNode<T, Log2Dim>::Buffer& buf)
1746 {
1747  for (Index32 i = 0, N = buf.size(); i < N; ++i) os << buf.mData[i] << ", ";
1748  return os;
1749 }
1750 
1751 } // namespace tree
1752 } // namespace OPENVDB_VERSION_NAME
1753 } // namespace openvdb
1754 
1755 
1757 
1758 
1759 // Specialization for LeafNodes of type bool
1760 #include "LeafNodeBool.h"
1761 
1762 #endif // OPENVDB_TREE_LEAFNODE_HAS_BEEN_INCLUDED
1763 
1764 // Copyright (c) 2012-2013 DreamWorks Animation LLC
1765 // All rights reserved. This software is distributed under the
1766 // Mozilla Public License 2.0 ( http://www.mozilla.org/MPL/2.0/ )
DenseIter< LeafNode, ValueType, ChildAll > ChildAllIter
Definition: LeafNode.h:358
Buffer(const ValueType &val)
Constructs a buffer populated with the specified value.
Definition: LeafNode.h:94
LeafNode * touchLeafAndCache(const Coord &, AccessorT &)
Return a pointer to this node.
Definition: LeafNode.h:789
static Index log2dim()
Return log2 of the dimension of this LeafNode, e.g. 3 if dimensions are 8^3.
Definition: LeafNode.h:190
void combine(const LeafNode &other, CombineOp &op)
Definition: LeafNode.h:1507
const ValueType & getFirstValue() const
Return a const reference to the first value in the buffer.
Definition: LeafNode.h:665
void setValueOff(const Coord &xyz)
Mark the voxel at the given coordinates as inactive but don&#39;t change its value.
Definition: LeafNode.h:466
bool isValueOnAndCache(const Coord &xyz, AccessorT &) const
Return true if the voxel at the given coordinates is active.
Definition: LeafNode.h:583
void setValue(Index i, const ValueType &val)
Set the i&#39;th value of the Buffer to the specified value.
Definition: LeafNode.h:111
bool probeValueAndCache(const Coord &xyz, ValueType &val, AccessorT &) const
Return true if the voxel at the given coordinates is active and return the voxel value in val...
Definition: LeafNode.h:640
bool isValueOn(const Coord &xyz) const
Return true if the voxel at the given coordinates is active.
Definition: LeafNode.h:523
ValueIter< MaskOffIterator, LeafNode, const ValueType, ValueOff > ValueOffIter
Definition: LeafNode.h:350
Index64 onVoxelCount() const
Return the number of voxels marked On.
Definition: LeafNode.h:209
void prune(const ValueType &=zeroVal< ValueType >())
This function exists only to enable template instantiation.
Definition: LeafNode.h:767
ValueOffCIter cendValueOff() const
Definition: LeafNode.h:374
bool isValueMaskOn(Index n) const
Definition: LeafNode.h:858
void evalActiveBoundingBox(CoordBBox &, bool visitVoxels=true) const
Definition: LeafNode.h:1281
LeafNode< ValueType, Log2Dim > LeafNodeType
Definition: LeafNode.h:66
#define OPENVDB_DEPRECATED
Definition: Platform.h:47
Buffer mBuffer
Buffer containing the actual data values.
Definition: LeafNode.h:850
ChildOnIter beginChildOn()
Definition: LeafNode.h:385
const ValueType & getValue(Index i) const
Return a const reference to the i&#39;th element of the Buffer.
Definition: LeafNode.h:107
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:1299
int32_t Int32
Definition: Types.h:58
void setActiveState(Index offset, bool on)
Set the active state of the voxel at the given offset but don&#39;t change its value. ...
Definition: LeafNode.h:458
Definition: Types.h:190
Coord offsetToGlobalCoord(Index n) const
Return the global coordinates for a linear table offset.
Definition: LeafNode.h:1001
Definition: LeafNode.h:50
ChildOnCIter cbeginChildOn() const
Definition: LeafNode.h:383
bool operator==(const LeafNode &other) const
Check for buffer, state and origin equivalence.
Definition: LeafNode.h:1249
ValueOnCIter cbeginValueOn() const
Definition: LeafNode.h:361
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:1123
void swap(Buffer &other)
Exchange this node&#39;s data buffer with the given data buffer without changing the active states of the...
Definition: LeafNode.h:408
void setValue(const Coord &xyz, const ValueType &val)
Set the value of the voxel at the given coordinates and mark the voxel as active. ...
Definition: LeafNode.h:484
void signedFloodFill(const ValueType &background)
Overwrite each inactive value in this node and in any child nodes with a new value whose magnitude is...
Definition: LeafNode.h:1361
static Coord offsetToLocalCoord(Index n)
Return the local coordinates for a linear table offset, where offset 0 has coordinates (0...
Definition: LeafNode.h:975
Index64 offLeafVoxelCount() const
Definition: LeafNode.h:213
ValueIter< MaskDenseIterator, const LeafNode, const ValueType, ValueAll > ValueAllCIter
Definition: LeafNode.h:353
ChildOffIter beginChildOff()
Definition: LeafNode.h:388
bool operator==(const Buffer &other) const
Definition: LeafNode.h:123
void visit2Node(OtherLeafNodeType &other, VisitorOp &)
Definition: LeafNode.h:1653
Definition: NodeMasks.h:220
bool getItem(Index pos, void *&child, NonConstValueT &value) const
Definition: LeafNode.h:330
This struct collects both input and output arguments to &quot;grid combiner&quot; functors used with the tree::...
Definition: Types.h:232
uint32_t Index32
Definition: Types.h:54
const ValueType & getValue(const Coord &xyz) const
Return the value of the voxel at the given coordinates.
Definition: LeafNode.h:1012
ChildOnIter endChildOn()
Definition: LeafNode.h:395
void voxelizeActiveTiles()
Definition: LeafNode.h:689
ChildIter< MaskOffIterator, const LeafNode, ChildOff > ChildOffCIter
Definition: LeafNode.h:357
bool isChildMaskOn(Index) const
Definition: LeafNode.h:865
static Index coordToOffset(const Coord &xyz)
Return the linear table offset of the given global or local coordinates.
Definition: LeafNode.h:965
ChildIter< MaskOnIterator, LeafNode, ChildOn > ChildOnIter
Definition: LeafNode.h:354
void combine2(const LeafNode &other, const ValueType &, bool valueIsActive, CombineOp &)
Definition: LeafNode.h:1543
ValueOnIter beginValueOn()
Definition: LeafNode.h:363
static Index size()
Return the number of values represented in this Buffer.
Definition: LeafNode.h:144
void topologyIntersection(const LeafNode< OtherType, Log2Dim > &other, const ValueType &)
Intersect this node&#39;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:1476
void modifyValue(const ModifyOp &op) const
Definition: LeafNode.h:307
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:494
void topologyUnion(const LeafNode< OtherType, Log2Dim > &other)
Union this node&#39;s set of active values with the active values of the other node, whose ValueType may ...
Definition: LeafNode.h:1468
util::NodeMask< Log2Dim > NodeMaskType
Definition: LeafNode.h:68
ValueOnIter endValueOn()
Definition: LeafNode.h:373
void addLeafAndCache(LeafNode *, AccessorT &)
This function exists only to enable template instantiation.
Definition: LeafNode.h:771
void setValueOffAndCache(const Coord &xyz, const ValueType &value, AccessorT &)
Change the value of the voxel at the given coordinates and mark it as inactive.
Definition: LeafNode.h:622
Definition: Types.h:346
ValueT & getValue() const
Definition: LeafNode.h:289
bool isValueOn(Index offset) const
Return true if the voxel at the given offset is active.
Definition: LeafNode.h:525
void negate()
Definition: LeafNode.h:1493
OnIterator beginOn() const
Definition: NodeMasks.h:332
void addLeaf(LeafNode *)
This function exists only to enable template instantiation.
Definition: LeafNode.h:769
void setOrigin(const Coord &origin)
Set the grid index coordinates of this node&#39;s local origin.
Definition: LeafNode.h:235
ValueOnCIter endValueOn() const
Definition: LeafNode.h:372
static Index getChildDim()
Return the dimension of child nodes of this LeafNode, which is one for voxels.
Definition: LeafNode.h:202
ValueAllCIter endValueAll() const
Definition: LeafNode.h:378
OPENVDB_IMPORT uint32_t getDataCompression(std::ios_base &)
Return a bitwise OR of compression option flags (COMPRESS_ZIP, COMPRESS_ACTIVE_MASK, etc.) specifying whether and how input data is compressed or output data should be compressed.
ValueIter< MaskDenseIterator, LeafNode, const ValueType, ValueAll > ValueAllIter
Definition: LeafNode.h:352
ValueAllIter beginValueAll()
Definition: LeafNode.h:369
static const Index SIZE
Definition: LeafNode.h:76
const NodeT * probeConstNode(const Coord &) const
This function exists only to enable template instantiation.
Definition: LeafNode.h:777
static void doVisit2Node(NodeT &self, OtherNodeT &other, VisitorOp &)
Definition: LeafNode.h:1678
Base class for sparse iterators over internal and leaf nodes.
Definition: Iterator.h:148
ChildAllCIter endChildAll() const
Definition: LeafNode.h:400
ValueOnCIter beginValueOn() const
Definition: LeafNode.h:362
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: LeafNode.h:606
ValueT & getItem(Index pos) const
Definition: LeafNode.h:288
void readBuffers(std::istream &is, bool fromHalf=false)
Read buffers from a stream.
Definition: LeafNode.h:1200
void setValue(const ValueT &value) const
Definition: LeafNode.h:297
Buffer & operator=(const Buffer &other)
Assigns the values in the other Buffer to this Buffer.
Definition: LeafNode.h:113
ValueOffCIter cbeginValueOff() const
Definition: LeafNode.h:364
Leaf nodes have no children, so their child iterators have no get/set accessors.
Definition: LeafNode.h:312
void readCompressedValues(std::istream &is, ValueT *destBuf, Index destCount, const MaskT &valueMask, bool fromHalf)
Definition: Compression.h:276
void setValueOn(Index offset)
Mark the voxel at the given offset as active but don&#39;t change its value.
Definition: LeafNode.h:478
ChildIter()
Definition: LeafNode.h:315
ChildOnCIter cendChildOn() const
Definition: LeafNode.h:393
bool operator!=(const LeafNode &other) const
Definition: LeafNode.h:265
bool operator==(const Vec3< T0 > &v0, const Vec3< T1 > &v1)
Equality operator, does exact floating point comparisons.
Definition: Vec3.h:430
Stores the actual values in the LeafNode. Its dimension it fixed to 2^(3*Log2Dim) ...
Definition: LeafNode.h:88
Templated block class to hold specific data types and a fixed number of values determined by Log2Dim...
Definition: LeafNode.h:62
T negative(const T &val)
Return the unary negation of the given value.
Definition: Math.h:107
void setValueOn(const Coord &xyz)
Mark the voxel at the given coordinates as active but don&#39;t change its value.
Definition: LeafNode.h:476
bool isNegative(const Type &x)
Return true if x is less than zero.
Definition: Math.h:306
OPENVDB_IMPORT uint32_t getFormatVersion(std::istream &)
Return the file format version number associated with the given input stream.
bool operator!=(const Buffer &other) const
Definition: LeafNode.h:133
void setValuesOn()
Mark all voxels as active but don&#39;t change their values.
Definition: LeafNode.h:518
LeafNode * probeLeafAndCache(const Coord &, AccessorT &)
Return a pointer to this node.
Definition: LeafNode.h:800
ValueConverter&lt;T&gt;::Type is the type of a LeafNode having the same child hierarchy and dimensions as t...
Definition: LeafNode.h:82
static Index64 onTileCount()
Definition: LeafNode.h:214
bool isOn(Index32 n) const
Return true if the nth bit is on.
Definition: NodeMasks.h:440
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: LeafNode.h:1062
ValueIter< MaskOffIterator, const LeafNode, const ValueType, ValueOff > ValueOffCIter
Definition: LeafNode.h:351
bool isValueMaskOff() const
Definition: LeafNode.h:861
static Index64 offTileCount()
Definition: LeafNode.h:215
ChildOffIter endChildOff()
Definition: LeafNode.h:398
#define OPENVDB_VERSION_NAME
Definition: version.h:45
ChildAllCIter cbeginChildAll() const
Definition: LeafNode.h:389
ChildOnCIter endChildOn() const
Definition: LeafNode.h:394
ChildOffCIter cbeginChildOff() const
Definition: LeafNode.h:386
ChildAllCIter beginChildAll() const
Definition: LeafNode.h:390
void visitActiveBBox(BBoxOp &) const
Calls the templated functor BBoxOp with bounding box information. An additional level argument is pro...
Definition: LeafNode.h:1598
void visit(VisitorOp &)
Definition: LeafNode.h:1621
bool isChildMaskOff(Index) const
Definition: LeafNode.h:866
static void doVisit(NodeT &, VisitorOp &)
Definition: LeafNode.h:1639
ValueIter< MaskOnIterator, LeafNode, const ValueType, ValueOn > ValueOnIter
Definition: LeafNode.h:348
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:1088
Base class for iterators over internal and leaf nodes.
Definition: Iterator.h:58
DenseIteratorBase< MaskDenseIterator, DenseIter, NodeT, void, ValueT > BaseT
Definition: LeafNode.h:324
void addTile(Index level, const Coord &, const ValueType &, bool)
Definition: LeafNode.h:1331
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:457
CombineArgs & setBRef(const ValueType &b)
Redirect the B value to a new external source.
Definition: Types.h:271
bool isConstant(ValueType &constValue, bool &state, const ValueType &tolerance=zeroVal< ValueType >()) const
Definition: LeafNode.h:1308
void addTileAndCache(Index, const Coord &, const ValueType &, bool, AccessorT &)
Definition: LeafNode.h:1349
OPENVDB_API Hermite min(const Hermite &, const Hermite &)
min and max operations done directly on the compressed data.
NodeMaskType & getValueMask()
Definition: LeafNode.h:863
void modifyValueAndActiveStateAndCache(const Coord &xyz, const ModifyOp &op, AccessorT &)
Definition: LeafNode.h:614
NodeT * probeNode(const Coord &)
This function exists only to enable template instantiation.
Definition: LeafNode.h:775
LeafNode * probeLeaf(const Coord &)
Return a pointer to this node.
Definition: LeafNode.h:798
bool isEmpty() const
Return true if this node has no active voxels.
Definition: LeafNode.h:217
NodeMaskType::OffIterator MaskOffIterator
Definition: LeafNode.h:269
#define OPENVDB_NO_UNREACHABLE_CODE_WARNING_BEGIN
Definition: Platform.h:96
LeafNode< OtherValueType, Log2Dim > Type
Definition: LeafNode.h:83
bool isValueMaskOff(Index n) const
Definition: LeafNode.h:860
Index64 memUsage() const
Return the memory in bytes occupied by this node.
Definition: LeafNode.h:1259
NodeT * probeNodeAndCache(const Coord &, AccessorT &)
Return a pointer to this node.
Definition: LeafNode.h:791
ValueOffCIter beginValueOff() const
Definition: LeafNode.h:365
const ValueType & operator[](Index i) const
Return a const reference to the i&#39;th element of the Buffer.
Definition: LeafNode.h:109
void setValueOn(Index offset, const ValueType &val)
Set the value of the voxel at the given offset and mark the voxel as active.
Definition: LeafNode.h:486
Base class for dense iterators over internal and leaf nodes.
Definition: Iterator.h:211
NodeT * stealNode(const Coord &, const ValueType &, bool)
This function exists only to enable template instantiation.
Definition: LeafNode.h:773
void setValueMask(const NodeMaskType &mask)
Definition: LeafNode.h:864
Index64 onLeafVoxelCount() const
Definition: LeafNode.h:212
const NodeMaskType & getValueMask() const
Definition: LeafNode.h:862
static Index dim()
Return the number of voxels in each coordinate dimension.
Definition: LeafNode.h:192
static Index getLevel()
Return the level of this node, which by definition is zero for LeafNodes.
Definition: LeafNode.h:198
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: LeafNode.h:502
ValueIter< MaskOnIterator, const LeafNode, const ValueType, ValueOn > ValueOnCIter
Definition: LeafNode.h:349
bool isValueMaskOn() const
Definition: LeafNode.h:859
ValueIter(const MaskIterT &iter, NodeT *parent)
Definition: LeafNode.h:286
ChildOffCIter cendChildOff() const
Definition: LeafNode.h:396
void readTopology(std::istream &is, bool fromHalf=false)
Read in just the topology.
Definition: LeafNode.h:1181
bool isExactlyEqual(const T0 &a, const T1 &b)
Return true if a is exactly equal to b.
Definition: Math.h:353
static void doVisit2(NodeT &self, OtherChildAllIterT &, VisitorOp &, bool otherIsLHS)
Definition: LeafNode.h:1723
ValueOnCIter cendValueOn() const
Definition: LeafNode.h:371
void getOrigin(Coord &origin) const
Return the grid index coordinates of this node&#39;s local origin.
Definition: LeafNode.h:242
static Index size()
Return the total number of voxels represented by this LeafNode.
Definition: LeafNode.h:194
Index32 pos() const
Definition: NodeMasks.h:177
std::string str() const
Return a string representation of this node.
Definition: LeafNode.h:952
bool isDense() const
Return true if this node contains only active voxels.
Definition: LeafNode.h:219
Buffer()
Empty default constructor.
Definition: LeafNode.h:92
static void getNodeLog2Dims(std::vector< Index > &dims)
Append the Log2Dim of this LeafNode to the specified vector.
Definition: LeafNode.h:200
bool probeValue(const Coord &xyz, ValueType &val) const
Return true if the voxel at the given coordinates is active.
Definition: LeafNode.h:1028
const LeafNode * probeLeafAndCache(const Coord &, AccessorT &) const
Return a const pointer to this node.
Definition: LeafNode.h:808
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:1147
void setValueOn(const Coord &xyz, const ValueType &val)
Set the value of the voxel at the given coordinates and mark the voxel as active. ...
Definition: LeafNode.h:480
Definition: NodeMasks.h:189
OPENVDB_DEPRECATED void evalActiveVoxelBoundingBox(CoordBBox &) const
Definition: LeafNode.h:1267
void setItem(Index pos, const ValueT &value) const
Definition: LeafNode.h:292
void visit2(IterT &otherIter, VisitorOp &, bool otherIsLHS=false)
Definition: LeafNode.h:1699
Index32 Index
Definition: Types.h:56
void writeBuffers(std::ostream &os, bool toHalf=false) const
Write buffers to a stream.
Definition: LeafNode.h:1234
Coord mOrigin
Global grid index coordinates (x,y,z) of the local origin of this node.
Definition: LeafNode.h:854
ChildOffCIter beginChildOff() const
Definition: LeafNode.h:387
#define OPENVDB_NO_UNREACHABLE_CODE_WARNING_END
Definition: Platform.h:97
static Index memUsage()
Return the memory-footprint of this Buffer in units of bytes.
Definition: LeafNode.h:142
ChildAllIter beginChildAll()
Definition: LeafNode.h:391
void resetBackground(const ValueType &oldBackground, const ValueType &newBackground)
Replace inactive occurrences of oldBackground with newBackground, and inactive occurrences of -oldBac...
Definition: LeafNode.h:1404
boost::remove_const< UnsetItemT >::type NonConstValueType
Definition: Iterator.h:217
DenseIter()
Definition: LeafNode.h:327
Buffer & buffer()
Definition: LeafNode.h:410
ChildOnCIter beginChildOn() const
Definition: LeafNode.h:384
void setValueOnlyAndCache(const Coord &xyz, const ValueType &val, AccessorT &)
Change the value of the voxel at the given coordinates but preserve its state.
Definition: LeafNode.h:597
DenseIter< const LeafNode, const ValueType, ChildAll > ChildAllCIter
Definition: LeafNode.h:359
bool isInactive() const
Return true if all of this node&#39;s values are inactive.
Definition: LeafNode.h:826
const LeafNode * probeConstLeafAndCache(const Coord &, AccessorT &) const
Return a const pointer to this node.
Definition: LeafNode.h:806
void setValueAndCache(const Coord &xyz, const ValueType &val, AccessorT &)
Change the value of the voxel at the given coordinates and mark it as active.
Definition: LeafNode.h:588
const NodeT * probeConstNodeAndCache(const Coord &, AccessorT &) const
Return a const pointer to this node.
Definition: LeafNode.h:811
void setValueMaskOff(Index n)
Definition: LeafNode.h:871
NodeMaskType::DenseIterator MaskDenseIterator
Definition: LeafNode.h:270
ValueAllIter endValueAll()
Definition: LeafNode.h:379
ChildAllCIter cendChildAll() const
Definition: LeafNode.h:399
SparseIteratorBase< MaskIterT, ValueIter, NodeT, ValueT > BaseT
Definition: LeafNode.h:283
void getOrigin(Int32 &x, Int32 &y, Int32 &z) const
Return the grid index coordinates of this node&#39;s local origin.
Definition: LeafNode.h:243
void modifyValueAndActiveState(const Coord &xyz, const ModifyOp &op)
Apply a functor to the voxel at the given coordinates.
Definition: LeafNode.h:509
ValueAllCIter cbeginValueAll() const
Definition: LeafNode.h:367
static Index getValueLevel(const Coord &)
Return the level (i.e., 0) at which leaf node values reside.
Definition: LeafNode.h:453
BaseT::NonConstValueType NonConstValueT
Definition: LeafNode.h:325
LeafNode()
Default constructor.
Definition: LeafNode.h:894
static void evalNodeOrigin(Coord &xyz)
Compute the origin of the leaf node that contains the voxel with the given coordinates.
Definition: LeafNode.h:874
DenseIter(const MaskDenseIterator &iter, NodeT *parent)
Definition: LeafNode.h:328
CombineArgs & setARef(const ValueType &a)
Redirect the A value to a new external source.
Definition: Types.h:269
void setValueMaskOn(Index n)
Definition: LeafNode.h:870
static bool hasActiveTiles()
Return false since leaf nodes never contain tiles.
Definition: LeafNode.h:528
static Index numValues()
Return the total number of voxels represented by this LeafNode.
Definition: LeafNode.h:196
const ValueType & getValue(const Coord &xyz, bool &state, int &level, AccessorT &) const
Return the value of the voxel at the given coordinates and return its active state and level (i...
Definition: LeafNode.h:649
T ValueType
Definition: LeafNode.h:65
~Buffer()
Destructor.
Definition: LeafNode.h:98
void swap(Buffer &other)
Replace the values in this Buffer with the values in the other Buffer.
Definition: LeafNode.h:135
void pruneInactive(const ValueType &)
This function exists only to enable template instantiation.
Definition: LeafNode.h:768
void setValuesOff()
Mark all voxels as inactive but don&#39;t change their values.
Definition: LeafNode.h:520
ChildIter< MaskOnIterator, const LeafNode, ChildOn > ChildOnCIter
Definition: LeafNode.h:355
void topologyDifference(const LeafNode< OtherType, Log2Dim > &other, const ValueType &)
Difference this node&#39;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:1485
const ValueType & getValueAndCache(const Coord &xyz, AccessorT &) const
Return the value of the voxel at the given coordinates.
Definition: LeafNode.h:575
void modifyItem(Index n, const ModifyOp &op) const
Definition: LeafNode.h:304
ValueAllCIter beginValueAll() const
Definition: LeafNode.h:368
NodeMaskType::OnIterator MaskOnIterator
Definition: LeafNode.h:268
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:310
bool resultIsActive() const
Definition: Types.h:280
Definition: NodeMasks.h:251
bool isChildMaskOff() const
Definition: LeafNode.h:867
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:56
ValueAllCIter cendValueAll() const
Definition: LeafNode.h:377
bool isApproxEqual(const Hermite &lhs, const Hermite &rhs)
Definition: Hermite.h:470
static Index getValueLevelAndCache(const Coord &, AccessorT &)
Return the LEVEL (=0) at which leaf node values reside.
Definition: LeafNode.h:660
const LeafNode * probeLeaf(const Coord &) const
Return a const pointer to this node.
Definition: LeafNode.h:809
~LeafNode()
Destructor.
Definition: LeafNode.h:946
static Index32 nonLeafCount()
Return the non-leaf count for this node, which is zero.
Definition: LeafNode.h:206
Definition: Compression.h:70
ValueOffIter endValueOff()
Definition: LeafNode.h:376
NodeMaskType mValueMask
Bitmask that determines which voxels are active.
Definition: LeafNode.h:852
uint64_t Index64
Definition: Types.h:55
Bit mask for the internal and leaf nodes of VDB. This is a 64-bit implementation. ...
Definition: NodeMasks.h:288
void setActiveStateAndCache(const Coord &xyz, bool on, AccessorT &)
Set the active state of the voxel at the given coordinates without changing its value.
Definition: LeafNode.h:631
void writeCompressedValues(std::ostream &os, ValueT *srcBuf, Index srcCount, const MaskT &valueMask, const MaskT &childMask, bool toHalf)
Definition: Compression.h:378
Buffer(const Buffer &other)
Copy constructor.
Definition: LeafNode.h:96
void unsetItem(Index pos, const ValueT &value) const
Definition: LeafNode.h:341
boost::shared_ptr< LeafNode > Ptr
Definition: LeafNode.h:67
void fill(const ValueType &val)
Populates the buffer with a constant value.
Definition: LeafNode.h:100
ChildOffCIter endChildOff() const
Definition: LeafNode.h:397
OPENVDB_DEPRECATED const Coord & getOrigin() const
Return the grid index coordinates of this node&#39;s local origin.
Definition: LeafNode.h:238
ChildAllIter endChildAll()
Definition: LeafNode.h:401
void combine(FloatTreeT &lhsDist, IntTreeT &lhsIndex, FloatTreeT &rhsDist, IntTreeT &rhsIndex)
Definition: MeshToVolume.h:396
ChildIter(const MaskIterT &iter, NodeT *parent)
Definition: LeafNode.h:316
void writeTopology(std::ostream &os, bool toHalf=false) const
Write out just the topology.
Definition: LeafNode.h:1189
void setValueOnly(const Coord &xyz, const ValueType &val)
Set the value of the voxel at the given coordinates but don&#39;t change its active state.
Definition: LeafNode.h:1070
ValueOffIter beginValueOff()
Definition: LeafNode.h:366
ValueIter()
Definition: LeafNode.h:285
void setValueOff(Index offset)
Mark the voxel at the given offset as inactive but don&#39;t change its value.
Definition: LeafNode.h:468
Index64 offVoxelCount() const
Return the number of voxels marked Off.
Definition: LeafNode.h:211
Definition: Compression.h:165
void merge(const LeafNode &)
Definition: LeafNode.h:1423
CoordBBox getNodeBoundingBox() const
Return the bounding box of this node, i.e., the full index space spanned by this leaf node...
Definition: LeafNode.h:232
const ValueType & getLastValue() const
Return a const reference to the last value in the buffer.
Definition: LeafNode.h:667
ValueOffCIter endValueOff() const
Definition: LeafNode.h:375
void setValueMask(Index n, bool on)
Definition: LeafNode.h:869
static Index32 leafCount()
Return the leaf count for this node, which is one.
Definition: LeafNode.h:204
const Buffer & buffer() const
Definition: LeafNode.h:409
ChildIter< MaskOffIterator, LeafNode, ChildOff > ChildOffIter
Definition: LeafNode.h:356