OpenVDB  2.1.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 
1118 
1120 
1121 
1122 template<typename T, Index Log2Dim>
1123 template<typename DenseT>
1124 inline void
1125 LeafNode<T, Log2Dim>::copyToDense(const CoordBBox& bbox, DenseT& dense) const
1126 {
1127  typedef typename DenseT::ValueType DenseValueType;
1128 
1129  const size_t xStride = dense.xStride(), yStride = dense.yStride(), zStride = dense.zStride();
1130  const Coord& min = dense.bbox().min();
1131  DenseValueType* t0 = dense.data() + zStride * (bbox.min()[2] - min[2]); // target array
1132  const T* s0 = &mBuffer[bbox.min()[2] & (DIM-1u)]; // source array
1133  for (Int32 x = bbox.min()[0], ex = bbox.max()[0] + 1; x < ex; ++x) {
1134  DenseValueType* t1 = t0 + xStride * (x - min[0]);
1135  const T* s1 = s0 + ((x & (DIM-1u)) << 2*Log2Dim);
1136  for (Int32 y = bbox.min()[1], ey = bbox.max()[1] + 1; y < ey; ++y) {
1137  DenseValueType* t2 = t1 + yStride * (y - min[1]);
1138  const T* s2 = s1 + ((y & (DIM-1u)) << Log2Dim);
1139  for (Int32 z = bbox.min()[2], ez = bbox.max()[2] + 1; z < ez; ++z, t2 += zStride) {
1140  *t2 = DenseValueType(*s2++);
1141  }
1142  }
1143  }
1144 }
1145 
1146 
1147 template<typename T, Index Log2Dim>
1148 template<typename DenseT>
1149 inline void
1150 LeafNode<T, Log2Dim>::copyFromDense(const CoordBBox& bbox, const DenseT& dense,
1151  const ValueType& background, const ValueType& tolerance)
1152 {
1153  typedef typename DenseT::ValueType DenseValueType;
1154 
1155  const size_t xStride = dense.xStride(), yStride = dense.yStride(), zStride = dense.zStride();
1156  const Coord& min = dense.bbox().min();
1157 
1158  const DenseValueType* s0 = dense.data() + zStride * (bbox.min()[2] - min[2]); // source
1159  const Int32 n0 = bbox.min()[2] & (DIM-1u);
1160  for (Int32 x = bbox.min()[0], ex = bbox.max()[0]+1; x < ex; ++x) {
1161  const DenseValueType* s1 = s0 + xStride * (x - min[0]);
1162  const Int32 n1 = n0 + ((x & (DIM-1u)) << 2*LOG2DIM);
1163  for (Int32 y = bbox.min()[1], ey = bbox.max()[1]+1; y < ey; ++y) {
1164  const DenseValueType* s2 = s1 + yStride * (y - min[1]);
1165  Int32 n2 = n1 + ((y & (DIM-1u)) << LOG2DIM);
1166  for (Int32 z = bbox.min()[2], ez = bbox.max()[2]+1; z < ez; ++z, ++n2, s2 += zStride) {
1167  if (math::isApproxEqual(background, ValueType(*s2), tolerance)) {
1168  mValueMask.setOff(n2);
1169  mBuffer[n2] = background;
1170  } else {
1171  mValueMask.setOn(n2);
1172  mBuffer[n2] = ValueType(*s2);
1173  }
1174  }
1175  }
1176  }
1177 }
1178 
1179 
1181 
1182 
1183 template<typename T, Index Log2Dim>
1184 inline void
1185 LeafNode<T, Log2Dim>::readTopology(std::istream& is, bool /*fromHalf*/)
1186 {
1187  mValueMask.load(is);
1188 }
1189 
1190 
1191 template<typename T, Index Log2Dim>
1192 inline void
1193 LeafNode<T, Log2Dim>::writeTopology(std::ostream& os, bool /*toHalf*/) const
1194 {
1195  mValueMask.save(os);
1196 }
1197 
1198 
1200 
1201 
1202 template<typename T, Index Log2Dim>
1203 inline void
1204 LeafNode<T,Log2Dim>::readBuffers(std::istream& is, bool fromHalf)
1205 {
1206  // Read in the value mask.
1207  mValueMask.load(is);
1208 
1209  int8_t numBuffers = 1;
1211  // Read in the origin.
1212  is.read(reinterpret_cast<char*>(&mOrigin), sizeof(Coord::ValueType) * 3);
1213 
1214  // Read in the number of buffers, which should now always be one.
1215  is.read(reinterpret_cast<char*>(&numBuffers), sizeof(int8_t));
1216  }
1217 
1218  io::readCompressedValues(is, mBuffer.mData, SIZE, mValueMask, fromHalf);
1219 
1220  if (numBuffers > 1) {
1221  // Read in and discard auxiliary buffers that were created with earlier
1222  // versions of the library. (Auxiliary buffers are not mask compressed.)
1223  const bool zipped = io::getDataCompression(is) & io::COMPRESS_ZIP;
1224  Buffer temp;
1225  for (int i = 1; i < numBuffers; ++i) {
1226  if (fromHalf) {
1227  io::HalfReader<io::RealToHalf<T>::isReal, T>::read(is, temp.mData, SIZE, zipped);
1228  } else {
1229  io::readData<T>(is, temp.mData, SIZE, zipped);
1230  }
1231  }
1232  }
1233 }
1234 
1235 
1236 template<typename T, Index Log2Dim>
1237 inline void
1238 LeafNode<T, Log2Dim>::writeBuffers(std::ostream& os, bool toHalf) const
1239 {
1240  // Write out the value mask.
1241  mValueMask.save(os);
1242 
1243  io::writeCompressedValues(os, mBuffer.mData, SIZE,
1244  mValueMask, /*childMask=*/NodeMaskType(), toHalf);
1245 }
1246 
1247 
1249 
1250 
1251 template<typename T, Index Log2Dim>
1252 inline bool
1254 {
1255  return mOrigin == other.mOrigin &&
1256  mValueMask == other.mValueMask &&
1257  mBuffer == other.mBuffer;
1258 }
1259 
1260 
1261 template<typename T, Index Log2Dim>
1262 inline Index64
1264 {
1265  return mBuffer.memUsage() + sizeof(mOrigin) + mValueMask.memUsage();
1266 }
1267 
1268 
1269 template<typename T, Index Log2Dim>
1270 inline void
1272 {
1273  CoordBBox this_bbox = this->getNodeBoundingBox();
1274  if (bbox.isInside(this_bbox)) return;//this LeafNode is already enclosed in the bbox
1275  if (ValueOnCIter iter = this->cbeginValueOn()) {//any active values?
1276  this_bbox.reset();
1277  for(; iter; ++iter) this_bbox.expand(this->offsetToLocalCoord(iter.pos()));
1278  this_bbox.translate(this->origin());
1279  bbox.expand(this_bbox);
1280  }
1281 }
1282 
1283 template<typename T, Index Log2Dim>
1284 inline void
1285 LeafNode<T, Log2Dim>::evalActiveBoundingBox(CoordBBox& bbox, bool visitVoxels) const
1286 {
1287  CoordBBox this_bbox = this->getNodeBoundingBox();
1288  if (bbox.isInside(this_bbox)) return;//this LeafNode is already enclosed in the bbox
1289  if (ValueOnCIter iter = this->cbeginValueOn()) {//any active values?
1290  if (visitVoxels) {//use voxel granularity?
1291  this_bbox.reset();
1292  for(; iter; ++iter) this_bbox.expand(this->offsetToLocalCoord(iter.pos()));
1293  this_bbox.translate(this->origin());
1294  }
1295  bbox.expand(this_bbox);
1296  }
1297 }
1298 
1299 
1300 template<typename T, Index Log2Dim>
1301 template<typename OtherType, Index OtherLog2Dim>
1302 inline bool
1304 {
1305  assert(other);
1306  return (Log2Dim == OtherLog2Dim && mValueMask == other->getValueMask());
1307 }
1308 
1309 
1310 template<typename T, Index Log2Dim>
1311 inline bool
1313  bool& state, const ValueType& tolerance) const
1314 {
1315  state = mValueMask.isOn();
1316 
1317  if (!(state || mValueMask.isOff())) return false;
1318 
1319  bool allEqual = true;
1320  const T value = mBuffer[0];
1321  for (Index i = 1; allEqual && i < SIZE; ++i) {
1323  allEqual = math::isApproxEqual(mBuffer[i], value, tolerance);
1324  }
1325  if (allEqual) constValue = value;
1326  return allEqual;
1327 }
1328 
1329 
1331 
1332 
1333 template<typename T, Index Log2Dim>
1334 inline void
1335 LeafNode<T, Log2Dim>::addTile(Index level, const Coord& xyz, const ValueType& val, bool active)
1336 {
1337  assert(level == 0);
1338  this->addTile(this->coordToOffset(xyz), val, active);
1339 }
1340 
1341 template<typename T, Index Log2Dim>
1342 inline void
1343 LeafNode<T, Log2Dim>::addTile(Index offset, const ValueType& val, bool active)
1344 {
1345  assert(offset < SIZE);
1346  setValueOnly(offset, val);
1347  setActiveState(offset, active);
1348 }
1349 
1350 template<typename T, Index Log2Dim>
1351 template<typename AccessorT>
1352 inline void
1354  const ValueType& val, bool active, AccessorT&)
1355 {
1356  this->addTile(level, xyz, val, active);
1357 }
1358 
1359 
1361 
1362 
1363 template<typename T, Index Log2Dim>
1364 inline void
1366 {
1367  this->signedFloodFill(background, math::negative(background));
1368 }
1369 
1370 template<typename T, Index Log2Dim>
1371 inline void
1373  const ValueType& insideValue)
1374 {
1375  const Index first = mValueMask.findFirstOn();
1376  if (first < SIZE) {
1377  bool xInside = math::isNegative(mBuffer[first]), yInside = xInside, zInside = xInside;
1378  for (Index x = 0; x != (1 << Log2Dim); ++x) {
1379  const Index x00 = x << (2 * Log2Dim);
1380  if (mValueMask.isOn(x00)) {
1381  xInside = math::isNegative(mBuffer[x00]); // element(x, 0, 0)
1382  }
1383  yInside = xInside;
1384  for (Index y = 0; y != (1 << Log2Dim); ++y) {
1385  const Index xy0 = x00 + (y << Log2Dim);
1386  if (mValueMask.isOn(xy0)) {
1387  yInside = math::isNegative(mBuffer[xy0]); // element(x, y, 0)
1388  }
1389  zInside = yInside;
1390  for (Index z = 0; z != (1 << Log2Dim); ++z) {
1391  const Index xyz = xy0 + z; // element(x, y, z)
1392  if (mValueMask.isOn(xyz)) {
1393  zInside = math::isNegative(mBuffer[xyz]);
1394  } else {
1395  mBuffer[xyz] = zInside ? insideValue : outsideValue;
1396  }
1397  }
1398  }
1399  }
1400  } else {// if no active voxels exist simply use the sign of the first value
1401  mBuffer.fill(math::isNegative(mBuffer[0]) ? insideValue : outsideValue);
1402  }
1403 }
1404 
1405 
1406 template<typename T, Index Log2Dim>
1407 inline void
1409  const ValueType& newBackground)
1410 {
1411  typename NodeMaskType::OffIterator iter;
1412  // For all inactive values...
1413  for (iter = this->mValueMask.beginOff(); iter; ++iter) {
1414  ValueType &inactiveValue = mBuffer[iter.pos()];
1415  if (math::isApproxEqual(inactiveValue, oldBackground)) {
1416  inactiveValue = newBackground;
1417  } else if (math::isApproxEqual(inactiveValue, math::negative(oldBackground))) {
1418  inactiveValue = math::negative(newBackground);
1419  }
1420  }
1421 }
1422 
1423 
1424 template<typename T, Index Log2Dim>
1425 template<MergePolicy Policy>
1426 inline void
1428 {
1430  if (Policy == MERGE_NODES) return;
1431  typename NodeMaskType::OnIterator iter = other.mValueMask.beginOn();
1432  for (; iter; ++iter) {
1433  const Index n = iter.pos();
1434  if (mValueMask.isOff(n)) {
1435  mBuffer[n] = other.mBuffer[n];
1436  mValueMask.setOn(n);
1437  }
1438  }
1440 }
1441 
1442 template<typename T, Index Log2Dim>
1443 template<MergePolicy Policy>
1444 inline void
1446  const ValueType& /*bg*/, const ValueType& /*otherBG*/)
1447 {
1448  this->template merge<Policy>(other);
1449 }
1450 
1451 template<typename T, Index Log2Dim>
1452 template<MergePolicy Policy>
1453 inline void
1454 LeafNode<T, Log2Dim>::merge(const ValueType& tileValue, bool tileActive)
1455 {
1457  if (Policy != MERGE_ACTIVE_STATES_AND_NODES) return;
1458  if (!tileActive) return;
1459  // Replace all inactive values with the active tile value.
1460  for (typename NodeMaskType::OffIterator iter = mValueMask.beginOff(); iter; ++iter) {
1461  const Index n = iter.pos();
1462  mBuffer[n] = tileValue;
1463  mValueMask.setOn(n);
1464  }
1466 }
1467 
1468 
1469 template<typename T, Index Log2Dim>
1470 template<typename OtherType>
1471 inline void
1473 {
1474  mValueMask |= other.getValueMask();
1475 }
1476 
1477 template<typename T, Index Log2Dim>
1478 template<typename OtherType>
1479 inline void
1481  const ValueType&)
1482 {
1483  mValueMask &= other.getValueMask();
1484 }
1485 
1486 template<typename T, Index Log2Dim>
1487 template<typename OtherType>
1488 inline void
1490  const ValueType&)
1491 {
1492  mValueMask &= !other.getValueMask();
1493 }
1494 
1495 template<typename T, Index Log2Dim>
1496 inline void
1498 {
1499  for (Index i = 0; i < SIZE; ++i) {
1500  mBuffer[i] = -mBuffer[i];
1501  }
1502 }
1503 
1504 
1506 
1507 
1508 template<typename T, Index Log2Dim>
1509 template<typename CombineOp>
1510 inline void
1511 LeafNode<T, Log2Dim>::combine(const LeafNode& other, CombineOp& op)
1512 {
1513  CombineArgs<T> args;
1514  for (Index i = 0; i < SIZE; ++i) {
1515  op(args.setARef(mBuffer[i])
1516  .setAIsActive(mValueMask.isOn(i))
1517  .setBRef(other.mBuffer[i])
1518  .setBIsActive(other.mValueMask.isOn(i))
1519  .setResultRef(mBuffer[i]));
1520  mValueMask.set(i, args.resultIsActive());
1521  }
1522 }
1523 
1524 
1525 template<typename T, Index Log2Dim>
1526 template<typename CombineOp>
1527 inline void
1528 LeafNode<T, Log2Dim>::combine(const ValueType& value, bool valueIsActive, CombineOp& op)
1529 {
1530  CombineArgs<T> args;
1531  args.setBRef(value).setBIsActive(valueIsActive);
1532  for (Index i = 0; i < SIZE; ++i) {
1533  op(args.setARef(mBuffer[i])
1534  .setAIsActive(mValueMask.isOn(i))
1535  .setResultRef(mBuffer[i]));
1536  mValueMask.set(i, args.resultIsActive());
1537  }
1538 }
1539 
1540 
1542 
1543 
1544 template<typename T, Index Log2Dim>
1545 template<typename CombineOp>
1546 inline void
1548  bool valueIsActive, CombineOp& op)
1549 {
1550  CombineArgs<T> args;
1551  args.setBRef(value).setBIsActive(valueIsActive);
1552  for (Index i = 0; i < SIZE; ++i) {
1553  op(args.setARef(other.mBuffer[i])
1554  .setAIsActive(other.mValueMask.isOn(i))
1555  .setResultRef(mBuffer[i]));
1556  mValueMask.set(i, args.resultIsActive());
1557  }
1558 }
1559 
1560 
1561 template<typename T, Index Log2Dim>
1562 template<typename CombineOp>
1563 inline void
1565  bool valueIsActive, CombineOp& op)
1566 {
1567  CombineArgs<T> args;
1568  args.setARef(value).setAIsActive(valueIsActive);
1569  for (Index i = 0; i < SIZE; ++i) {
1570  op(args.setBRef(other.mBuffer[i])
1571  .setBIsActive(other.mValueMask.isOn(i))
1572  .setResultRef(mBuffer[i]));
1573  mValueMask.set(i, args.resultIsActive());
1574  }
1575 }
1576 
1577 
1578 template<typename T, Index Log2Dim>
1579 template<typename CombineOp>
1580 inline void
1581 LeafNode<T, Log2Dim>::combine2(const LeafNode& b0, const LeafNode& b1, CombineOp& op)
1582 {
1583  CombineArgs<T> args;
1584  for (Index i = 0; i < SIZE; ++i) {
1585  mValueMask.set(i, b0.mValueMask.isOn(i) || b1.mValueMask.isOn(i));
1586  op(args.setARef(b0.mBuffer[i])
1587  .setAIsActive(b0.mValueMask.isOn(i))
1588  .setBRef(b1.mBuffer[i])
1589  .setBIsActive(b1.mValueMask.isOn(i))
1590  .setResultRef(mBuffer[i]));
1591  mValueMask.set(i, args.resultIsActive());
1592  }
1593 }
1594 
1595 
1597 
1598 
1599 template<typename T, Index Log2Dim>
1600 template<typename BBoxOp>
1601 inline void
1603 {
1604  if (op.template descent<LEVEL>()) {
1605  for (ValueOnCIter i=this->cbeginValueOn(); i; ++i) {
1606 #ifdef _MSC_VER
1607  op.operator()<LEVEL>(CoordBBox::createCube(i.getCoord(), 1));
1608 #else
1609  op.template operator()<LEVEL>(CoordBBox::createCube(i.getCoord(), 1));
1610 #endif
1611  }
1612  } else {
1613 #ifdef _MSC_VER
1614  op.operator()<LEVEL>(this->getNodeBoundingBox());
1615 #else
1616  op.template operator()<LEVEL>(this->getNodeBoundingBox());
1617 #endif
1618  }
1619 }
1620 
1621 
1622 template<typename T, Index Log2Dim>
1623 template<typename VisitorOp>
1624 inline void
1626 {
1627  doVisit<LeafNode, VisitorOp, ChildAllIter>(*this, op);
1628 }
1629 
1630 
1631 template<typename T, Index Log2Dim>
1632 template<typename VisitorOp>
1633 inline void
1634 LeafNode<T, Log2Dim>::visit(VisitorOp& op) const
1635 {
1636  doVisit<const LeafNode, VisitorOp, ChildAllCIter>(*this, op);
1637 }
1638 
1639 
1640 template<typename T, Index Log2Dim>
1641 template<typename NodeT, typename VisitorOp, typename ChildAllIterT>
1642 inline void
1643 LeafNode<T, Log2Dim>::doVisit(NodeT& self, VisitorOp& op)
1644 {
1645  for (ChildAllIterT iter = self.beginChildAll(); iter; ++iter) {
1646  op(iter);
1647  }
1648 }
1649 
1650 
1652 
1653 
1654 template<typename T, Index Log2Dim>
1655 template<typename OtherLeafNodeType, typename VisitorOp>
1656 inline void
1657 LeafNode<T, Log2Dim>::visit2Node(OtherLeafNodeType& other, VisitorOp& op)
1658 {
1659  doVisit2Node<LeafNode, OtherLeafNodeType, VisitorOp, ChildAllIter,
1660  typename OtherLeafNodeType::ChildAllIter>(*this, other, op);
1661 }
1662 
1663 
1664 template<typename T, Index Log2Dim>
1665 template<typename OtherLeafNodeType, typename VisitorOp>
1666 inline void
1667 LeafNode<T, Log2Dim>::visit2Node(OtherLeafNodeType& other, VisitorOp& op) const
1668 {
1669  doVisit2Node<const LeafNode, OtherLeafNodeType, VisitorOp, ChildAllCIter,
1670  typename OtherLeafNodeType::ChildAllCIter>(*this, other, op);
1671 }
1672 
1673 
1674 template<typename T, Index Log2Dim>
1675 template<
1676  typename NodeT,
1677  typename OtherNodeT,
1678  typename VisitorOp,
1679  typename ChildAllIterT,
1680  typename OtherChildAllIterT>
1681 inline void
1682 LeafNode<T, Log2Dim>::doVisit2Node(NodeT& self, OtherNodeT& other, VisitorOp& op)
1683 {
1684  // Allow the two nodes to have different ValueTypes, but not different dimensions.
1685  BOOST_STATIC_ASSERT(OtherNodeT::SIZE == NodeT::SIZE);
1686  BOOST_STATIC_ASSERT(OtherNodeT::LEVEL == NodeT::LEVEL);
1687 
1688  ChildAllIterT iter = self.beginChildAll();
1689  OtherChildAllIterT otherIter = other.beginChildAll();
1690 
1691  for ( ; iter && otherIter; ++iter, ++otherIter) {
1692  op(iter, otherIter);
1693  }
1694 }
1695 
1696 
1698 
1699 
1700 template<typename T, Index Log2Dim>
1701 template<typename IterT, typename VisitorOp>
1702 inline void
1703 LeafNode<T, Log2Dim>::visit2(IterT& otherIter, VisitorOp& op, bool otherIsLHS)
1704 {
1705  doVisit2<LeafNode, VisitorOp, ChildAllIter, IterT>(
1706  *this, otherIter, op, otherIsLHS);
1707 }
1708 
1709 
1710 template<typename T, Index Log2Dim>
1711 template<typename IterT, typename VisitorOp>
1712 inline void
1713 LeafNode<T, Log2Dim>::visit2(IterT& otherIter, VisitorOp& op, bool otherIsLHS) const
1714 {
1715  doVisit2<const LeafNode, VisitorOp, ChildAllCIter, IterT>(
1716  *this, otherIter, op, otherIsLHS);
1717 }
1718 
1719 
1720 template<typename T, Index Log2Dim>
1721 template<
1722  typename NodeT,
1723  typename VisitorOp,
1724  typename ChildAllIterT,
1725  typename OtherChildAllIterT>
1726 inline void
1727 LeafNode<T, Log2Dim>::doVisit2(NodeT& self, OtherChildAllIterT& otherIter,
1728  VisitorOp& op, bool otherIsLHS)
1729 {
1730  if (!otherIter) return;
1731 
1732  if (otherIsLHS) {
1733  for (ChildAllIterT iter = self.beginChildAll(); iter; ++iter) {
1734  op(otherIter, iter);
1735  }
1736  } else {
1737  for (ChildAllIterT iter = self.beginChildAll(); iter; ++iter) {
1738  op(iter, otherIter);
1739  }
1740  }
1741 }
1742 
1743 
1745 
1746 
1747 template<typename T, Index Log2Dim>
1748 inline std::ostream&
1749 operator<<(std::ostream& os, const typename LeafNode<T, Log2Dim>::Buffer& buf)
1750 {
1751  for (Index32 i = 0, N = buf.size(); i < N; ++i) os << buf.mData[i] << ", ";
1752  return os;
1753 }
1754 
1755 } // namespace tree
1756 } // namespace OPENVDB_VERSION_NAME
1757 } // namespace openvdb
1758 
1759 
1761 
1762 
1763 // Specialization for LeafNodes of type bool
1764 #include "LeafNodeBool.h"
1765 
1766 #endif // OPENVDB_TREE_LEAFNODE_HAS_BEEN_INCLUDED
1767 
1768 // Copyright (c) 2012-2013 DreamWorks Animation LLC
1769 // All rights reserved. This software is distributed under the
1770 // Mozilla Public License 2.0 ( http://www.mozilla.org/MPL/2.0/ )
ValueIter()
Definition: LeafNode.h:285
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
static Index32 leafCount()
Return the leaf count for this node, which is one.
Definition: LeafNode.h:204
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
DenseIter< LeafNode, ValueType, ChildAll > ChildAllIter
Definition: LeafNode.h:358
LeafNode * touchLeafAndCache(const Coord &, AccessorT &)
Return a pointer to this node.
Definition: LeafNode.h:789
LeafNode< ValueType, Log2Dim > LeafNodeType
Definition: LeafNode.h:66
DenseIter(const MaskDenseIterator &iter, NodeT *parent)
Definition: LeafNode.h:328
Buffer mBuffer
Buffer containing the actual data values.
Definition: LeafNode.h:850
const ValueType & getFirstValue() const
Return a const reference to the first value in the buffer.
Definition: LeafNode.h:665
#define OPENVDB_DEPRECATED
Definition: Platform.h:47
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
OPENVDB_API Hermite min(const Hermite &, const Hermite &)
min and max operations done directly on the compressed data.
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
Coord offsetToGlobalCoord(Index n) const
Return the global coordinates for a linear table offset.
Definition: LeafNode.h:1001
void evalActiveBoundingBox(CoordBBox &, bool visitVoxels=true) const
Definition: LeafNode.h:1285
ChildOnCIter cbeginChildOn() const
Definition: LeafNode.h:383
ChildOnIter beginChildOn()
Definition: LeafNode.h:385
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:1303
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:1125
Definition: LeafNode.h:50
Index32 pos() const
Definition: NodeMasks.h:177
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:1365
const ValueType & operator[](Index i) const
Return a const reference to the i&#39;th element of the Buffer.
Definition: LeafNode.h:109
ValueIter< MaskDenseIterator, const LeafNode, const ValueType, ValueAll > ValueAllCIter
Definition: LeafNode.h:353
ChildOffIter beginChildOff()
Definition: LeafNode.h:388
bool operator==(const LeafNode &other) const
Check for buffer, state and origin equivalence.
Definition: LeafNode.h:1253
boost::remove_const< UnsetItemT >::type NonConstValueType
Definition: Iterator.h:217
ValueOnCIter cbeginValueOn() const
Definition: LeafNode.h:361
void writeCompressedValues(std::ostream &os, ValueT *srcBuf, Index srcCount, const MaskT &valueMask, const MaskT &childMask, bool toHalf)
Definition: Compression.h:378
Definition: NodeMasks.h:189
bool isChildMaskOn(Index) const
Definition: LeafNode.h:865
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
ChildIter< MaskOnIterator, LeafNode, ChildOn > ChildOnIter
Definition: LeafNode.h:354
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
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
ValueT & getItem(Index pos) const
Definition: LeafNode.h:288
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:1480
static Index memUsage()
Return the memory-footprint of this Buffer in units of bytes.
Definition: LeafNode.h:142
void visit2Node(OtherLeafNodeType &other, VisitorOp &)
Definition: LeafNode.h:1657
OnIterator beginOn() const
Definition: NodeMasks.h:332
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:1472
util::NodeMask< Log2Dim > NodeMaskType
Definition: LeafNode.h:68
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
void addLeafAndCache(LeafNode *, AccessorT &)
This function exists only to enable template instantiation.
Definition: LeafNode.h:771
ChildIter< MaskOffIterator, const LeafNode, ChildOff > ChildOffCIter
Definition: LeafNode.h:357
static Index coordToOffset(const Coord &xyz)
Return the linear table offset of the given global or local coordinates.
Definition: LeafNode.h:965
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
void combine2(const LeafNode &other, const ValueType &, bool valueIsActive, CombineOp &)
Definition: LeafNode.h:1547
Buffer()
Empty default constructor.
Definition: LeafNode.h:92
ValueOnIter beginValueOn()
Definition: LeafNode.h:363
void negate()
Definition: LeafNode.h:1497
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 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
ValueAllCIter endValueAll() const
Definition: LeafNode.h:378
ValueOnIter endValueOn()
Definition: LeafNode.h:373
Bit mask for the internal and leaf nodes of VDB. This is a 64-bit implementation. ...
Definition: NodeMasks.h:288
ValueIter< MaskDenseIterator, LeafNode, const ValueType, ValueAll > ValueAllIter
Definition: LeafNode.h:352
Index32 Index
Definition: Types.h:56
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:1682
ChildAllCIter endChildAll() const
Definition: LeafNode.h:400
ValueOnCIter beginValueOn() const
Definition: LeafNode.h:362
void swap(Buffer &other)
Replace the values in this Buffer with the values in the other Buffer.
Definition: LeafNode.h:135
bool operator==(const Vec3< T0 > &v0, const Vec3< T1 > &v1)
Equality operator, does exact floating point comparisons.
Definition: Vec3.h:430
bool isValueOn(Index offset) const
Return true if the voxel at the given offset is active.
Definition: LeafNode.h:525
void combine(FloatTreeT &lhsDist, IntTreeT &lhsIndex, FloatTreeT &rhsDist, IntTreeT &rhsIndex)
Definition: MeshToVolume.h:396
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
void addLeaf(LeafNode *)
This function exists only to enable template instantiation.
Definition: LeafNode.h:769
Definition: Compression.h:70
void setItem(Index pos, const ValueT &value) const
Definition: LeafNode.h:292
static Index getChildDim()
Return the dimension of child nodes of this LeafNode, which is one for voxels.
Definition: LeafNode.h:202
Definition: Types.h:190
Base class for sparse iterators over internal and leaf nodes.
Definition: Iterator.h:148
ChildOnCIter cendChildOn() const
Definition: LeafNode.h:393
void unsetItem(Index pos, const ValueT &value) const
Definition: LeafNode.h:341
void readBuffers(std::istream &is, bool fromHalf=false)
Read buffers from a stream.
Definition: LeafNode.h:1204
void modifyValue(const ModifyOp &op) const
Definition: LeafNode.h:307
ValueOffCIter cbeginValueOff() const
Definition: LeafNode.h:364
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
ChildIter(const MaskIterT &iter, NodeT *parent)
Definition: LeafNode.h:316
LeafNode * probeLeafAndCache(const Coord &, AccessorT &)
Return a pointer to this node.
Definition: LeafNode.h:800
~Buffer()
Destructor.
Definition: LeafNode.h:98
void setValueOn(Index offset)
Mark the voxel at the given offset as active but don&#39;t change its value.
Definition: LeafNode.h:478
bool operator!=(const LeafNode &other) const
Definition: LeafNode.h:265
SparseIteratorBase< MaskIterT, ValueIter, NodeT, ValueT > BaseT
Definition: LeafNode.h:283
static Index64 onTileCount()
Definition: LeafNode.h:214
Stores the actual values in the LeafNode. Its dimension it fixed to 2^(3*Log2Dim) ...
Definition: LeafNode.h:88
BaseT::NonConstValueType NonConstValueT
Definition: LeafNode.h:325
ValueIter< MaskOffIterator, const LeafNode, const ValueType, ValueOff > ValueOffCIter
Definition: LeafNode.h:351
CombineArgs & setBRef(const ValueType &b)
Redirect the B value to a new external source.
Definition: Types.h:271
This struct collects both input and output arguments to &quot;grid combiner&quot; functors used with the tree::...
Definition: Types.h:232
Definition: Compression.h:165
static Index64 offTileCount()
Definition: LeafNode.h:215
void setValuesOn()
Mark all voxels as active but don&#39;t change their values.
Definition: LeafNode.h:518
ChildAllCIter cbeginChildAll() const
Definition: LeafNode.h:389
#define OPENVDB_VERSION_NAME
Definition: version.h:45
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.
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
Buffer(const ValueType &val)
Constructs a buffer populated with the specified value.
Definition: LeafNode.h:94
ChildOffCIter cbeginChildOff() const
Definition: LeafNode.h:386
bool isValueMaskOff() const
Definition: LeafNode.h:861
ChildOffIter endChildOff()
Definition: LeafNode.h:398
void fill(const ValueType &val)
Populates the buffer with a constant value.
Definition: LeafNode.h:100
ValueT & getValue() const
Definition: LeafNode.h:289
Base class for iterators over internal and leaf nodes.
Definition: Iterator.h:58
ChildOnCIter endChildOn() const
Definition: LeafNode.h:394
bool isOn(Index32 n) const
Return true if the nth bit is on.
Definition: NodeMasks.h:440
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:1602
void visit(VisitorOp &)
Definition: LeafNode.h:1625
bool isChildMaskOff(Index) const
Definition: LeafNode.h:866
bool isConstant(ValueType &constValue, bool &state, const ValueType &tolerance=zeroVal< ValueType >()) const
Definition: LeafNode.h:1312
static void doVisit(NodeT &, VisitorOp &)
Definition: LeafNode.h:1643
bool isExactlyEqual(const T0 &a, const T1 &b)
Return true if a is exactly equal to b.
Definition: Math.h:360
ValueIter< MaskOnIterator, LeafNode, const ValueType, ValueOn > ValueOnIter
Definition: LeafNode.h:348
NodeMaskType & getValueMask()
Definition: LeafNode.h:863
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
uint32_t Index32
Definition: Types.h:54
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
void readCompressedValues(std::istream &is, ValueT *destBuf, Index destCount, const MaskT &valueMask, bool fromHalf)
Definition: Compression.h:276
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
uint64_t Index64
Definition: Types.h:55
int32_t Int32
Definition: Types.h:58
bool isValueMaskOff(Index n) const
Definition: LeafNode.h:860
void addTile(Index level, const Coord &, const ValueType &, bool)
Definition: LeafNode.h:1335
Index64 memUsage() const
Return the memory in bytes occupied by this node.
Definition: LeafNode.h:1263
void setValue(const ValueT &value) const
Definition: LeafNode.h:297
Leaf nodes have no children, so their child iterators have no get/set accessors.
Definition: LeafNode.h:312
#define OPENVDB_NO_UNREACHABLE_CODE_WARNING_BEGIN
Definition: Platform.h:96
Buffer(const Buffer &other)
Copy constructor.
Definition: LeafNode.h:96
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
Base class for dense iterators over internal and leaf nodes.
Definition: Iterator.h:211
void addTileAndCache(Index, const Coord &, const ValueType &, bool, AccessorT &)
Definition: LeafNode.h:1353
CombineArgs & setARef(const ValueType &a)
Redirect the A value to a new external source.
Definition: Types.h:269
const NodeMaskType & getValueMask() const
Definition: LeafNode.h:862
bool getItem(Index pos, void *&child, NonConstValueT &value) const
Definition: LeafNode.h:330
ValueIter(const MaskIterT &iter, NodeT *parent)
Definition: LeafNode.h:286
static Index dim()
Return the number of voxels in each coordinate dimension.
Definition: LeafNode.h:192
void setValue(Index i, const ValueType &val)
Set the i&#39;th value of the Buffer to the specified value.
Definition: LeafNode.h:111
static Index getLevel()
Return the level of this node, which by definition is zero for LeafNodes.
Definition: LeafNode.h:198
NodeMaskType::OffIterator MaskOffIterator
Definition: LeafNode.h:269
ValueIter< MaskOnIterator, const LeafNode, const ValueType, ValueOn > ValueOnCIter
Definition: LeafNode.h:349
LeafNode< OtherValueType, Log2Dim > Type
Definition: LeafNode.h:83
bool isValueMaskOn() const
Definition: LeafNode.h:859
NodeT * probeNodeAndCache(const Coord &, AccessorT &)
Return a pointer to this node.
Definition: LeafNode.h:791
ValueOffCIter beginValueOff() const
Definition: LeafNode.h:365
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
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:367
Index64 onLeafVoxelCount() const
Definition: LeafNode.h:212
static Index size()
Return the total number of voxels represented by this LeafNode.
Definition: LeafNode.h:194
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
bool isDense() const
Return true if this node contains only active voxels.
Definition: LeafNode.h:219
ChildOffCIter cendChildOff() const
Definition: LeafNode.h:396
void readTopology(std::istream &is, bool fromHalf=false)
Read in just the topology.
Definition: LeafNode.h:1185
static void doVisit2(NodeT &self, OtherChildAllIterT &, VisitorOp &, bool otherIsLHS)
Definition: LeafNode.h:1727
ValueOnCIter cendValueOn() const
Definition: LeafNode.h:371
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
void getOrigin(Coord &origin) const
Return the grid index coordinates of this node&#39;s local origin.
Definition: LeafNode.h:242
void visit2(IterT &otherIter, VisitorOp &, bool otherIsLHS=false)
Definition: LeafNode.h:1703
std::string str() const
Return a string representation of this node.
Definition: LeafNode.h:952
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 ValueType & getValue(Index i) const
Return a const reference to the i&#39;th element of the Buffer.
Definition: LeafNode.h:107
const LeafNode * probeLeafAndCache(const Coord &, AccessorT &) const
Return a const pointer to this node.
Definition: LeafNode.h:808
void resetBackground(const ValueType &oldBackground, const ValueType &newBackground)
Replace inactive occurrences of oldBackground with newBackground, and inactive occurrences of -oldBac...
Definition: LeafNode.h:1408
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:1150
#define OPENVDB_NO_UNREACHABLE_CODE_WARNING_END
Definition: Platform.h:97
OPENVDB_DEPRECATED void evalActiveVoxelBoundingBox(CoordBBox &) const
Definition: LeafNode.h:1271
Buffer & buffer()
Definition: LeafNode.h:410
void writeBuffers(std::ostream &os, bool toHalf=false) const
Write buffers to a stream.
Definition: LeafNode.h:1238
DenseIter< const LeafNode, const ValueType, ChildAll > ChildAllCIter
Definition: LeafNode.h:359
Coord mOrigin
Global grid index coordinates (x,y,z) of the local origin of this node.
Definition: LeafNode.h:854
bool isInactive() const
Return true if all of this node&#39;s values are inactive.
Definition: LeafNode.h:826
ChildOffCIter beginChildOff() const
Definition: LeafNode.h:387
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
ChildAllIter beginChildAll()
Definition: LeafNode.h:391
ChildIter()
Definition: LeafNode.h:315
bool isApproxEqual(const Hermite &lhs, const Hermite &rhs)
Definition: Hermite.h:470
ValueAllCIter cbeginValueAll() const
Definition: LeafNode.h:367
Buffer & operator=(const Buffer &other)
Assigns the values in the other Buffer to this Buffer.
Definition: LeafNode.h:113
static Index getValueLevel(const Coord &)
Return the level (i.e., 0) at which leaf node values reside.
Definition: LeafNode.h:453
static void evalNodeOrigin(Coord &xyz)
Compute the origin of the leaf node that contains the voxel with the given coordinates.
Definition: LeafNode.h:874
ChildOnCIter beginChildOn() const
Definition: LeafNode.h:384
Definition: Types.h:346
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
void modifyItem(Index n, const ModifyOp &op) const
Definition: LeafNode.h:304
bool operator==(const Buffer &other) const
Definition: LeafNode.h:123
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
bool isNegative(const Type &x)
Return true if x is less than zero.
Definition: Math.h:313
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
ValueAllIter endValueAll()
Definition: LeafNode.h:379
Definition: NodeMasks.h:220
bool resultIsActive() const
Definition: Types.h:280
ChildIter< MaskOnIterator, const LeafNode, ChildOn > ChildOnCIter
Definition: LeafNode.h:355
ChildAllCIter cendChildAll() const
Definition: LeafNode.h:399
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:1489
const ValueType & getValueAndCache(const Coord &xyz, AccessorT &) const
Return the value of the voxel at the given coordinates.
Definition: LeafNode.h:575
Templated block class to hold specific data types and a fixed number of values determined by Log2Dim...
Definition: LeafNode.h:62
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
LeafNode()
Default constructor.
Definition: LeafNode.h:894
ValueAllCIter cendValueAll() const
Definition: LeafNode.h:377
void setValueMaskOn(Index n)
Definition: LeafNode.h:870
static bool hasActiveTiles()
Return false since leaf nodes never contain tiles.
Definition: LeafNode.h:528
bool operator!=(const Buffer &other) const
Definition: LeafNode.h:133
static Index numValues()
Return the total number of voxels represented by this LeafNode.
Definition: LeafNode.h:196
Definition: NodeMasks.h:251
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
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 Index size()
Return the number of values represented in this Buffer.
Definition: LeafNode.h:144
~LeafNode()
Destructor.
Definition: LeafNode.h:946
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:67
ValueOffIter endValueOff()
Definition: LeafNode.h:376
ValueAllCIter beginValueAll() const
Definition: LeafNode.h:368
NodeMaskType::OnIterator MaskOnIterator
Definition: LeafNode.h:268
T negative(const T &val)
Return the unary negation of the given value.
Definition: Math.h:107
bool isChildMaskOff() const
Definition: LeafNode.h:867
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
static Index getValueLevelAndCache(const Coord &, AccessorT &)
Return the LEVEL (=0) at which leaf node values reside.
Definition: LeafNode.h:660
boost::shared_ptr< LeafNode > Ptr
Definition: LeafNode.h:67
const LeafNode * probeLeaf(const Coord &) const
Return a const pointer to this node.
Definition: LeafNode.h:809
static Index32 nonLeafCount()
Return the non-leaf count for this node, which is zero.
Definition: LeafNode.h:206
OPENVDB_DEPRECATED const Coord & getOrigin() const
Return the grid index coordinates of this node&#39;s local origin.
Definition: LeafNode.h:238
NodeMaskType mValueMask
Bitmask that determines which voxels are active.
Definition: LeafNode.h:852
DenseIteratorBase< MaskDenseIterator, DenseIter, NodeT, void, ValueT > BaseT
Definition: LeafNode.h:324
void writeTopology(std::ostream &os, bool toHalf=false) const
Write out just the topology.
Definition: LeafNode.h:1193
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
void setValueOff(Index offset)
Mark the voxel at the given offset as inactive but don&#39;t change its value.
Definition: LeafNode.h:468
OPENVDB_IMPORT uint32_t getFormatVersion(std::istream &)
Return the file format version number associated with the given input stream.
void merge(const LeafNode &)
Definition: LeafNode.h:1427
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 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:514
ChildOffCIter endChildOff() const
Definition: LeafNode.h:397
void setValueMask(Index n, bool on)
Definition: LeafNode.h:869
const Buffer & buffer() const
Definition: LeafNode.h:409
ChildIter< MaskOffIterator, LeafNode, ChildOff > ChildOffIter
Definition: LeafNode.h:356
ChildAllIter endChildAll()
Definition: LeafNode.h:401
ValueOffIter beginValueOff()
Definition: LeafNode.h:366
DenseIter()
Definition: LeafNode.h:327
Index64 offVoxelCount() const
Return the number of voxels marked Off.
Definition: LeafNode.h:211
static Index log2dim()
Return log2 of the dimension of this LeafNode, e.g. 3 if dimensions are 8^3.
Definition: LeafNode.h:190
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
void combine(const LeafNode &other, CombineOp &op)
Definition: LeafNode.h:1511