OpenVDB  6.2.1
LeafNode.h
Go to the documentation of this file.
1 //
3 // Copyright (c) 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 <openvdb/Types.h>
35 #include <openvdb/util/NodeMasks.h>
36 #include <openvdb/io/Compression.h> // for io::readData(), etc.
37 #include "Iterator.h"
38 #include "LeafBuffer.h"
39 #include <algorithm> // for std::nth_element()
40 #include <iostream>
41 #include <memory>
42 #include <sstream>
43 #include <string>
44 #include <type_traits>
45 #include <vector>
46 
47 
48 class TestLeaf;
49 template<typename> class TestLeafIO;
50 
51 namespace openvdb {
53 namespace OPENVDB_VERSION_NAME {
54 namespace tree {
55 
56 template<Index, typename> struct SameLeafConfig; // forward declaration
57 
58 
63 template<typename T, Index Log2Dim>
64 class LeafNode
65 {
66 public:
67  using BuildType = T;
68  using ValueType = T;
73 
74  static const Index
75  LOG2DIM = Log2Dim, // needed by parent nodes
76  TOTAL = Log2Dim, // needed by parent nodes
77  DIM = 1 << TOTAL, // dimension along one coordinate direction
78  NUM_VALUES = 1 << 3 * Log2Dim,
79  NUM_VOXELS = NUM_VALUES, // total number of voxels represented by this node
80  SIZE = NUM_VALUES,
81  LEVEL = 0; // level 0 = leaf
82 
85  template<typename OtherValueType>
87 
90  template<typename OtherNodeType>
93  };
94 
95 
97  LeafNode();
98 
103  explicit LeafNode(const Coord& coords,
104  const ValueType& value = zeroVal<ValueType>(),
105  bool active = false);
106 
107 
108 #if OPENVDB_ABI_VERSION_NUMBER >= 3
115  const Coord& coords,
116  const ValueType& value = zeroVal<ValueType>(),
117  bool active = false);
118 #endif
119 
121  LeafNode(const LeafNode&);
122 
124  LeafNode& operator=(const LeafNode&) = default;
125 
127  template<typename OtherValueType>
128  explicit LeafNode(const LeafNode<OtherValueType, Log2Dim>& other);
129 
131  template<typename OtherValueType>
133  const ValueType& offValue, const ValueType& onValue, TopologyCopy);
134 
136  template<typename OtherValueType>
138  const ValueType& background, TopologyCopy);
139 
141  ~LeafNode();
142 
143  //
144  // Statistics
145  //
147  static Index log2dim() { return Log2Dim; }
149  static Index dim() { return DIM; }
151  static Index size() { return SIZE; }
153  static Index numValues() { return SIZE; }
155  static Index getLevel() { return LEVEL; }
157  static void getNodeLog2Dims(std::vector<Index>& dims) { dims.push_back(Log2Dim); }
159  static Index getChildDim() { return 1; }
161  static Index32 leafCount() { return 1; }
163  static Index32 nonLeafCount() { return 0; }
164 
166  Index64 onVoxelCount() const { return mValueMask.countOn(); }
168  Index64 offVoxelCount() const { return mValueMask.countOff(); }
169  Index64 onLeafVoxelCount() const { return onVoxelCount(); }
170  Index64 offLeafVoxelCount() const { return offVoxelCount(); }
171  static Index64 onTileCount() { return 0; }
172  static Index64 offTileCount() { return 0; }
174  bool isEmpty() const { return mValueMask.isOff(); }
176  bool isDense() const { return mValueMask.isOn(); }
177 
178 #if OPENVDB_ABI_VERSION_NUMBER >= 3
179  bool isAllocated() const { return !mBuffer.isOutOfCore() && !mBuffer.empty(); }
182  bool allocate() { return mBuffer.allocate(); }
183 #endif
184 
186  Index64 memUsage() const;
187 
191  void evalActiveBoundingBox(CoordBBox& bbox, bool visitVoxels = true) const;
192 
195  CoordBBox getNodeBoundingBox() const { return CoordBBox::createCube(mOrigin, DIM); }
196 
198  void setOrigin(const Coord& origin) { mOrigin = origin; }
200  const Coord& origin() const { return mOrigin; }
202  void getOrigin(Coord& origin) const { origin = mOrigin; }
203  void getOrigin(Int32& x, Int32& y, Int32& z) const { mOrigin.asXYZ(x, y, z); }
205 
207  static Index coordToOffset(const Coord& xyz);
210  static Coord offsetToLocalCoord(Index n);
212  Coord offsetToGlobalCoord(Index n) const;
213 
215  std::string str() const;
216 
219  template<typename OtherType, Index OtherLog2Dim>
220  bool hasSameTopology(const LeafNode<OtherType, OtherLog2Dim>* other) const;
221 
223  bool operator==(const LeafNode& other) const;
224  bool operator!=(const LeafNode& other) const { return !(other == *this); }
225 
226 protected:
230 
231  // Type tags to disambiguate template instantiations
232  struct ValueOn {}; struct ValueOff {}; struct ValueAll {};
233  struct ChildOn {}; struct ChildOff {}; struct ChildAll {};
234 
235  template<typename MaskIterT, typename NodeT, typename ValueT, typename TagT>
236  struct ValueIter:
237  // Derives from SparseIteratorBase, but can also be used as a dense iterator,
238  // if MaskIterT is a dense mask iterator type.
239  public SparseIteratorBase<
240  MaskIterT, ValueIter<MaskIterT, NodeT, ValueT, TagT>, NodeT, ValueT>
241  {
243 
245  ValueIter(const MaskIterT& iter, NodeT* parent): BaseT(iter, parent) {}
246 
247  ValueT& getItem(Index pos) const { return this->parent().getValue(pos); }
248  ValueT& getValue() const { return this->parent().getValue(this->pos()); }
249 
250  // Note: setItem() can't be called on const iterators.
251  void setItem(Index pos, const ValueT& value) const
252  {
253  this->parent().setValueOnly(pos, value);
254  }
255  // Note: setValue() can't be called on const iterators.
256  void setValue(const ValueT& value) const
257  {
258  this->parent().setValueOnly(this->pos(), value);
259  }
260 
261  // Note: modifyItem() can't be called on const iterators.
262  template<typename ModifyOp>
263  void modifyItem(Index n, const ModifyOp& op) const { this->parent().modifyValue(n, op); }
264  // Note: modifyValue() can't be called on const iterators.
265  template<typename ModifyOp>
266  void modifyValue(const ModifyOp& op) const { this->parent().modifyValue(this->pos(), op); }
267  };
268 
270  template<typename MaskIterT, typename NodeT, typename TagT>
271  struct ChildIter:
272  public SparseIteratorBase<MaskIterT, ChildIter<MaskIterT, NodeT, TagT>, NodeT, ValueType>
273  {
275  ChildIter(const MaskIterT& iter, NodeT* parent): SparseIteratorBase<
276  MaskIterT, ChildIter<MaskIterT, NodeT, TagT>, NodeT, ValueType>(iter, parent) {}
277  };
278 
279  template<typename NodeT, typename ValueT, typename TagT>
280  struct DenseIter: public DenseIteratorBase<
281  MaskDenseIterator, DenseIter<NodeT, ValueT, TagT>, NodeT, /*ChildT=*/void, ValueT>
282  {
285 
287  DenseIter(const MaskDenseIterator& iter, NodeT* parent): BaseT(iter, parent) {}
288 
289  bool getItem(Index pos, void*& child, NonConstValueT& value) const
290  {
291  value = this->parent().getValue(pos);
292  child = nullptr;
293  return false; // no child
294  }
295 
296  // Note: setItem() can't be called on const iterators.
297  //void setItem(Index pos, void* child) const {}
298 
299  // Note: unsetItem() can't be called on const iterators.
300  void unsetItem(Index pos, const ValueT& value) const
301  {
302  this->parent().setValueOnly(pos, value);
303  }
304  };
305 
306 public:
319 
320  ValueOnCIter cbeginValueOn() const { return ValueOnCIter(mValueMask.beginOn(), this); }
321  ValueOnCIter beginValueOn() const { return ValueOnCIter(mValueMask.beginOn(), this); }
322  ValueOnIter beginValueOn() { return ValueOnIter(mValueMask.beginOn(), this); }
323  ValueOffCIter cbeginValueOff() const { return ValueOffCIter(mValueMask.beginOff(), this); }
324  ValueOffCIter beginValueOff() const { return ValueOffCIter(mValueMask.beginOff(), this); }
325  ValueOffIter beginValueOff() { return ValueOffIter(mValueMask.beginOff(), this); }
326  ValueAllCIter cbeginValueAll() const { return ValueAllCIter(mValueMask.beginDense(), this); }
327  ValueAllCIter beginValueAll() const { return ValueAllCIter(mValueMask.beginDense(), this); }
328  ValueAllIter beginValueAll() { return ValueAllIter(mValueMask.beginDense(), this); }
329 
330  ValueOnCIter cendValueOn() const { return ValueOnCIter(mValueMask.endOn(), this); }
331  ValueOnCIter endValueOn() const { return ValueOnCIter(mValueMask.endOn(), this); }
332  ValueOnIter endValueOn() { return ValueOnIter(mValueMask.endOn(), this); }
333  ValueOffCIter cendValueOff() const { return ValueOffCIter(mValueMask.endOff(), this); }
334  ValueOffCIter endValueOff() const { return ValueOffCIter(mValueMask.endOff(), this); }
335  ValueOffIter endValueOff() { return ValueOffIter(mValueMask.endOff(), this); }
336  ValueAllCIter cendValueAll() const { return ValueAllCIter(mValueMask.endDense(), this); }
337  ValueAllCIter endValueAll() const { return ValueAllCIter(mValueMask.endDense(), this); }
338  ValueAllIter endValueAll() { return ValueAllIter(mValueMask.endDense(), this); }
339 
340  // Note that [c]beginChildOn() and [c]beginChildOff() actually return end iterators,
341  // because leaf nodes have no children.
342  ChildOnCIter cbeginChildOn() const { return ChildOnCIter(mValueMask.endOn(), this); }
343  ChildOnCIter beginChildOn() const { return ChildOnCIter(mValueMask.endOn(), this); }
344  ChildOnIter beginChildOn() { return ChildOnIter(mValueMask.endOn(), this); }
345  ChildOffCIter cbeginChildOff() const { return ChildOffCIter(mValueMask.endOff(), this); }
346  ChildOffCIter beginChildOff() const { return ChildOffCIter(mValueMask.endOff(), this); }
347  ChildOffIter beginChildOff() { return ChildOffIter(mValueMask.endOff(), this); }
348  ChildAllCIter cbeginChildAll() const { return ChildAllCIter(mValueMask.beginDense(), this); }
349  ChildAllCIter beginChildAll() const { return ChildAllCIter(mValueMask.beginDense(), this); }
350  ChildAllIter beginChildAll() { return ChildAllIter(mValueMask.beginDense(), this); }
351 
352  ChildOnCIter cendChildOn() const { return ChildOnCIter(mValueMask.endOn(), this); }
353  ChildOnCIter endChildOn() const { return ChildOnCIter(mValueMask.endOn(), this); }
354  ChildOnIter endChildOn() { return ChildOnIter(mValueMask.endOn(), this); }
355  ChildOffCIter cendChildOff() const { return ChildOffCIter(mValueMask.endOff(), this); }
356  ChildOffCIter endChildOff() const { return ChildOffCIter(mValueMask.endOff(), this); }
357  ChildOffIter endChildOff() { return ChildOffIter(mValueMask.endOff(), this); }
358  ChildAllCIter cendChildAll() const { return ChildAllCIter(mValueMask.endDense(), this); }
359  ChildAllCIter endChildAll() const { return ChildAllCIter(mValueMask.endDense(), this); }
360  ChildAllIter endChildAll() { return ChildAllIter(mValueMask.endDense(), this); }
361 
362  //
363  // Buffer management
364  //
367  void swap(Buffer& other) { mBuffer.swap(other); }
368  const Buffer& buffer() const { return mBuffer; }
369  Buffer& buffer() { return mBuffer; }
370 
371  //
372  // I/O methods
373  //
377  void readTopology(std::istream& is, bool fromHalf = false);
381  void writeTopology(std::ostream& os, bool toHalf = false) const;
382 
386  void readBuffers(std::istream& is, bool fromHalf = false);
391  void readBuffers(std::istream& is, const CoordBBox& bbox, bool fromHalf = false);
395  void writeBuffers(std::ostream& os, bool toHalf = false) const;
396 
397  size_t streamingSize(bool toHalf = false) const;
398 
399  //
400  // Accessor methods
401  //
403  const ValueType& getValue(const Coord& xyz) const;
405  const ValueType& getValue(Index offset) const;
406 
410  bool probeValue(const Coord& xyz, ValueType& val) const;
414  bool probeValue(Index offset, ValueType& val) const;
415 
417  static Index getValueLevel(const Coord&) { return LEVEL; }
418 
420  void setActiveState(const Coord& xyz, bool on);
422  void setActiveState(Index offset, bool on) { assert(offset<SIZE); mValueMask.set(offset, on); }
423 
425  void setValueOnly(const Coord& xyz, const ValueType& val);
427  void setValueOnly(Index offset, const ValueType& val);
428 
430  void setValueOff(const Coord& xyz) { mValueMask.setOff(LeafNode::coordToOffset(xyz)); }
432  void setValueOff(Index offset) { assert(offset < SIZE); mValueMask.setOff(offset); }
433 
435  void setValueOff(const Coord& xyz, const ValueType& val);
437  void setValueOff(Index offset, const ValueType& val);
438 
440  void setValueOn(const Coord& xyz) { mValueMask.setOn(LeafNode::coordToOffset(xyz)); }
442  void setValueOn(Index offset) { assert(offset < SIZE); mValueMask.setOn(offset); }
444  void setValueOn(const Coord& xyz, const ValueType& val) {
445  this->setValueOn(LeafNode::coordToOffset(xyz), val);
446  }
448  void setValue(const Coord& xyz, const ValueType& val) { this->setValueOn(xyz, val); }
450  void setValueOn(Index offset, const ValueType& val) {
451  mBuffer.setValue(offset, val);
452  mValueMask.setOn(offset);
453  }
454 
457  template<typename ModifyOp>
458  void modifyValue(Index offset, const ModifyOp& op)
459  {
460  mBuffer.loadValues();
461  if (!mBuffer.empty()) {
462  // in-place modify value
463  ValueType& val = const_cast<ValueType&>(mBuffer[offset]);
464  op(val);
465  mValueMask.setOn(offset);
466  }
467  }
468 
471  template<typename ModifyOp>
472  void modifyValue(const Coord& xyz, const ModifyOp& op)
473  {
474  this->modifyValue(this->coordToOffset(xyz), op);
475  }
476 
478  template<typename ModifyOp>
479  void modifyValueAndActiveState(const Coord& xyz, const ModifyOp& op)
480  {
481  mBuffer.loadValues();
482  if (!mBuffer.empty()) {
483  const Index offset = this->coordToOffset(xyz);
484  bool state = mValueMask.isOn(offset);
485  // in-place modify value
486  ValueType& val = const_cast<ValueType&>(mBuffer[offset]);
487  op(val, state);
488  mValueMask.set(offset, state);
489  }
490  }
491 
493  void setValuesOn() { mValueMask.setOn(); }
495  void setValuesOff() { mValueMask.setOff(); }
496 
498  bool isValueOn(const Coord& xyz) const {return this->isValueOn(LeafNode::coordToOffset(xyz));}
500  bool isValueOn(Index offset) const { return mValueMask.isOn(offset); }
501 
503  static bool hasActiveTiles() { return false; }
504 
506  void clip(const CoordBBox&, const ValueType& background);
507 
509  void fill(const CoordBBox& bbox, const ValueType&, bool active = true);
511  void denseFill(const CoordBBox& bbox, const ValueType& value, bool active = true)
512  {
513  this->fill(bbox, value, active);
514  }
515 
517  void fill(const ValueType& value);
519  void fill(const ValueType& value, bool active);
520 
532  template<typename DenseT>
533  void copyToDense(const CoordBBox& bbox, DenseT& dense) const;
534 
551  template<typename DenseT>
552  void copyFromDense(const CoordBBox& bbox, const DenseT& dense,
553  const ValueType& background, const ValueType& tolerance);
554 
557  template<typename AccessorT>
558  const ValueType& getValueAndCache(const Coord& xyz, AccessorT&) const
559  {
560  return this->getValue(xyz);
561  }
562 
565  template<typename AccessorT>
566  bool isValueOnAndCache(const Coord& xyz, AccessorT&) const { return this->isValueOn(xyz); }
567 
570  template<typename AccessorT>
571  void setValueAndCache(const Coord& xyz, const ValueType& val, AccessorT&)
572  {
573  this->setValueOn(xyz, val);
574  }
575 
579  template<typename AccessorT>
580  void setValueOnlyAndCache(const Coord& xyz, const ValueType& val, AccessorT&)
581  {
582  this->setValueOnly(xyz, val);
583  }
584 
588  template<typename ModifyOp, typename AccessorT>
589  void modifyValueAndCache(const Coord& xyz, const ModifyOp& op, AccessorT&)
590  {
591  this->modifyValue(xyz, op);
592  }
593 
596  template<typename ModifyOp, typename AccessorT>
597  void modifyValueAndActiveStateAndCache(const Coord& xyz, const ModifyOp& op, AccessorT&)
598  {
599  this->modifyValueAndActiveState(xyz, op);
600  }
601 
604  template<typename AccessorT>
605  void setValueOffAndCache(const Coord& xyz, const ValueType& value, AccessorT&)
606  {
607  this->setValueOff(xyz, value);
608  }
609 
613  template<typename AccessorT>
614  void setActiveStateAndCache(const Coord& xyz, bool on, AccessorT&)
615  {
616  this->setActiveState(xyz, on);
617  }
618 
622  template<typename AccessorT>
623  bool probeValueAndCache(const Coord& xyz, ValueType& val, AccessorT&) const
624  {
625  return this->probeValue(xyz, val);
626  }
627 
631  template<typename AccessorT>
632  const ValueType& getValue(const Coord& xyz, bool& state, int& level, AccessorT&) const
633  {
634  const Index offset = this->coordToOffset(xyz);
635  state = mValueMask.isOn(offset);
636  level = LEVEL;
637  return mBuffer[offset];
638  }
639 
642  template<typename AccessorT>
643  static Index getValueLevelAndCache(const Coord&, AccessorT&) { return LEVEL; }
644 
648  const ValueType& getFirstValue() const { return mBuffer[0]; }
650  const ValueType& getLastValue() const { return mBuffer[SIZE - 1]; }
651 
654  void resetBackground(const ValueType& oldBackground, const ValueType& newBackground);
655 
656  void negate();
657 
660  void voxelizeActiveTiles(bool = true) {}
661 
662  template<MergePolicy Policy> void merge(const LeafNode&);
663  template<MergePolicy Policy> void merge(const ValueType& tileValue, bool tileActive);
664  template<MergePolicy Policy>
665  void merge(const LeafNode& other, const ValueType& /*bg*/, const ValueType& /*otherBG*/);
666 
673  template<typename OtherType>
674  void topologyUnion(const LeafNode<OtherType, Log2Dim>& other);
675 
687  template<typename OtherType>
688  void topologyIntersection(const LeafNode<OtherType, Log2Dim>& other, const ValueType&);
689 
701  template<typename OtherType>
702  void topologyDifference(const LeafNode<OtherType, Log2Dim>& other, const ValueType&);
703 
704  template<typename CombineOp>
705  void combine(const LeafNode& other, CombineOp& op);
706  template<typename CombineOp>
707  void combine(const ValueType& value, bool valueIsActive, CombineOp& op);
708 
709  template<typename CombineOp, typename OtherType /*= ValueType*/>
710  void combine2(const LeafNode& other, const OtherType&, bool valueIsActive, CombineOp&);
711  template<typename CombineOp, typename OtherNodeT /*= LeafNode*/>
712  void combine2(const ValueType&, const OtherNodeT& other, bool valueIsActive, CombineOp&);
713  template<typename CombineOp, typename OtherNodeT /*= LeafNode*/>
714  void combine2(const LeafNode& b0, const OtherNodeT& b1, CombineOp&);
715 
721  template<typename BBoxOp> void visitActiveBBox(BBoxOp&) const;
722 
723  template<typename VisitorOp> void visit(VisitorOp&);
724  template<typename VisitorOp> void visit(VisitorOp&) const;
725 
726  template<typename OtherLeafNodeType, typename VisitorOp>
727  void visit2Node(OtherLeafNodeType& other, VisitorOp&);
728  template<typename OtherLeafNodeType, typename VisitorOp>
729  void visit2Node(OtherLeafNodeType& other, VisitorOp&) const;
730  template<typename IterT, typename VisitorOp>
731  void visit2(IterT& otherIter, VisitorOp&, bool otherIsLHS = false);
732  template<typename IterT, typename VisitorOp>
733  void visit2(IterT& otherIter, VisitorOp&, bool otherIsLHS = false) const;
734 
736  void prune(const ValueType& /*tolerance*/ = zeroVal<ValueType>()) {}
738  void addLeaf(LeafNode*) {}
739  template<typename AccessorT>
740  void addLeafAndCache(LeafNode*, AccessorT&) {}
741  template<typename NodeT>
742  NodeT* stealNode(const Coord&, const ValueType&, bool) { return nullptr; }
743  template<typename NodeT>
744  NodeT* probeNode(const Coord&) { return nullptr; }
745  template<typename NodeT>
746  const NodeT* probeConstNode(const Coord&) const { return nullptr; }
747  template<typename ArrayT> void getNodes(ArrayT&) const {}
748  template<typename ArrayT> void stealNodes(ArrayT&, const ValueType&, bool) {}
750 
751  void addTile(Index level, const Coord&, const ValueType&, bool);
752  void addTile(Index offset, const ValueType&, bool);
753  template<typename AccessorT>
754  void addTileAndCache(Index, const Coord&, const ValueType&, bool, AccessorT&);
755 
757  LeafNode* touchLeaf(const Coord&) { return this; }
759  template<typename AccessorT>
760  LeafNode* touchLeafAndCache(const Coord&, AccessorT&) { return this; }
761  template<typename NodeT, typename AccessorT>
762  NodeT* probeNodeAndCache(const Coord&, AccessorT&)
763  {
765  if (!(std::is_same<NodeT, LeafNode>::value)) return nullptr;
766  return reinterpret_cast<NodeT*>(this);
768  }
769  LeafNode* probeLeaf(const Coord&) { return this; }
770  template<typename AccessorT>
771  LeafNode* probeLeafAndCache(const Coord&, AccessorT&) { return this; }
773 
774  const LeafNode* probeConstLeaf(const Coord&) const { return this; }
776  template<typename AccessorT>
777  const LeafNode* probeConstLeafAndCache(const Coord&, AccessorT&) const { return this; }
778  template<typename AccessorT>
779  const LeafNode* probeLeafAndCache(const Coord&, AccessorT&) const { return this; }
780  const LeafNode* probeLeaf(const Coord&) const { return this; }
781  template<typename NodeT, typename AccessorT>
782  const NodeT* probeConstNodeAndCache(const Coord&, AccessorT&) const
783  {
785  if (!(std::is_same<NodeT, LeafNode>::value)) return nullptr;
786  return reinterpret_cast<const NodeT*>(this);
788  }
790 
800  bool isConstant(ValueType& firstValue, bool& state,
801  const ValueType& tolerance = zeroVal<ValueType>()) const;
802 
814  bool isConstant(ValueType& minValue, ValueType& maxValue,
815  bool& state, const ValueType& tolerance = zeroVal<ValueType>()) const;
816 
817 
832  ValueType medianAll(ValueType *tmp = nullptr) const;
833 
848  Index medianOn(ValueType &value, ValueType *tmp = nullptr) const;
849 
864  Index medianOff(ValueType &value, ValueType *tmp = nullptr) const;
865 
867  bool isInactive() const { return mValueMask.isOff(); }
868 
869 protected:
870  friend class ::TestLeaf;
871  template<typename> friend class ::TestLeafIO;
872 
873  // During topology-only construction, access is needed
874  // to protected/private members of other template instances.
875  template<typename, Index> friend class LeafNode;
876 
883 
884  // Allow iterators to call mask accessor methods (see below).
889 
890  // Mask accessors
891 public:
892  bool isValueMaskOn(Index n) const { return mValueMask.isOn(n); }
893  bool isValueMaskOn() const { return mValueMask.isOn(); }
894  bool isValueMaskOff(Index n) const { return mValueMask.isOff(n); }
895  bool isValueMaskOff() const { return mValueMask.isOff(); }
896  const NodeMaskType& getValueMask() const { return mValueMask; }
897  NodeMaskType& getValueMask() { return mValueMask; }
898  const NodeMaskType& valueMask() const { return mValueMask; }
899  void setValueMask(const NodeMaskType& mask) { mValueMask = mask; }
900  bool isChildMaskOn(Index) const { return false; } // leaf nodes have no children
901  bool isChildMaskOff(Index) const { return true; }
902  bool isChildMaskOff() const { return true; }
903 protected:
904  void setValueMask(Index n, bool on) { mValueMask.set(n, on); }
905  void setValueMaskOn(Index n) { mValueMask.setOn(n); }
906  void setValueMaskOff(Index n) { mValueMask.setOff(n); }
907 
908  inline void skipCompressedValues(bool seekable, std::istream&, bool fromHalf);
909 
911  static void evalNodeOrigin(Coord& xyz) { xyz &= ~(DIM - 1); }
912 
913  template<typename NodeT, typename VisitorOp, typename ChildAllIterT>
914  static inline void doVisit(NodeT&, VisitorOp&);
915 
916  template<typename NodeT, typename OtherNodeT, typename VisitorOp,
917  typename ChildAllIterT, typename OtherChildAllIterT>
918  static inline void doVisit2Node(NodeT& self, OtherNodeT& other, VisitorOp&);
919 
920  template<typename NodeT, typename VisitorOp,
921  typename ChildAllIterT, typename OtherChildAllIterT>
922  static inline void doVisit2(NodeT& self, OtherChildAllIterT&, VisitorOp&, bool otherIsLHS);
923 
924 private:
926  Buffer mBuffer;
928  NodeMaskType mValueMask;
930  Coord mOrigin;
931 }; // end of LeafNode class
932 
933 
935 
936 
938 template<Index Dim1, typename NodeT2>
941 struct SameLeafConfig { static const bool value = false; };
942 
943 template<Index Dim1, typename T2>
944 struct SameLeafConfig<Dim1, LeafNode<T2, Dim1> > { static const bool value = true; };
946 
947 
949 
950 
951 template<typename T, Index Log2Dim>
952 inline
954  mValueMask(),//default is off!
955  mOrigin(0, 0, 0)
956 {
957 }
958 
959 
960 template<typename T, Index Log2Dim>
961 inline
962 LeafNode<T, Log2Dim>::LeafNode(const Coord& xyz, const ValueType& val, bool active):
963  mBuffer(val),
964  mValueMask(active),
965  mOrigin(xyz & (~(DIM - 1)))
966 {
967 }
968 
969 
970 #if OPENVDB_ABI_VERSION_NUMBER >= 3
971 template<typename T, Index Log2Dim>
972 inline
973 LeafNode<T, Log2Dim>::LeafNode(PartialCreate, const Coord& xyz, const ValueType& val, bool active):
974  mBuffer(PartialCreate(), val),
975  mValueMask(active),
976  mOrigin(xyz & (~(DIM - 1)))
977 {
978 }
979 #endif
980 
981 
982 template<typename T, Index Log2Dim>
983 inline
985  mBuffer(other.mBuffer),
986  mValueMask(other.valueMask()),
987  mOrigin(other.mOrigin)
988 {
989 }
990 
991 
992 // Copy-construct from a leaf node with the same configuration but a different ValueType.
993 template<typename T, Index Log2Dim>
994 template<typename OtherValueType>
995 inline
997  mValueMask(other.valueMask()),
998  mOrigin(other.mOrigin)
999 {
1000  struct Local {
1002  static inline ValueType convertValue(const OtherValueType& val) { return ValueType(val); }
1003  };
1004 
1005  for (Index i = 0; i < SIZE; ++i) {
1006  mBuffer[i] = Local::convertValue(other.mBuffer[i]);
1007  }
1008 }
1009 
1010 
1011 template<typename T, Index Log2Dim>
1012 template<typename OtherValueType>
1013 inline
1015  const ValueType& background, TopologyCopy):
1016  mBuffer(background),
1017  mValueMask(other.valueMask()),
1018  mOrigin(other.mOrigin)
1019 {
1020 }
1021 
1022 
1023 template<typename T, Index Log2Dim>
1024 template<typename OtherValueType>
1025 inline
1027  const ValueType& offValue, const ValueType& onValue, TopologyCopy):
1028  mValueMask(other.valueMask()),
1029  mOrigin(other.mOrigin)
1030 {
1031  for (Index i = 0; i < SIZE; ++i) {
1032  mBuffer[i] = (mValueMask.isOn(i) ? onValue : offValue);
1033  }
1034 }
1035 
1036 
1037 template<typename T, Index Log2Dim>
1038 inline
1040 {
1041 }
1042 
1043 
1044 template<typename T, Index Log2Dim>
1045 inline std::string
1047 {
1048  std::ostringstream ostr;
1049  ostr << "LeafNode @" << mOrigin << ": " << mBuffer;
1050  return ostr.str();
1051 }
1052 
1053 
1055 
1056 
1057 template<typename T, Index Log2Dim>
1058 inline Index
1060 {
1061  assert ((xyz[0] & (DIM-1u)) < DIM && (xyz[1] & (DIM-1u)) < DIM && (xyz[2] & (DIM-1u)) < DIM);
1062  return ((xyz[0] & (DIM-1u)) << 2*Log2Dim)
1063  + ((xyz[1] & (DIM-1u)) << Log2Dim)
1064  + (xyz[2] & (DIM-1u));
1065 }
1066 
1067 template<typename T, Index Log2Dim>
1068 inline Coord
1070 {
1071  assert(n<(1<< 3*Log2Dim));
1072  Coord xyz;
1073  xyz.setX(n >> 2*Log2Dim);
1074  n &= ((1<<2*Log2Dim)-1);
1075  xyz.setY(n >> Log2Dim);
1076  xyz.setZ(n & ((1<<Log2Dim)-1));
1077  return xyz;
1078 }
1079 
1080 
1081 template<typename T, Index Log2Dim>
1082 inline Coord
1084 {
1085  return (this->offsetToLocalCoord(n) + this->origin());
1086 }
1087 
1088 
1090 
1091 
1092 template<typename ValueT, Index Log2Dim>
1093 inline const ValueT&
1095 {
1096  return this->getValue(LeafNode::coordToOffset(xyz));
1097 }
1098 
1099 template<typename ValueT, Index Log2Dim>
1100 inline const ValueT&
1102 {
1103  assert(offset < SIZE);
1104  return mBuffer[offset];
1105 }
1106 
1107 
1108 template<typename T, Index Log2Dim>
1109 inline bool
1110 LeafNode<T, Log2Dim>::probeValue(const Coord& xyz, ValueType& val) const
1111 {
1112  return this->probeValue(LeafNode::coordToOffset(xyz), val);
1113 }
1114 
1115 template<typename T, Index Log2Dim>
1116 inline bool
1118 {
1119  assert(offset < SIZE);
1120  val = mBuffer[offset];
1121  return mValueMask.isOn(offset);
1122 }
1123 
1124 
1125 template<typename T, Index Log2Dim>
1126 inline void
1127 LeafNode<T, Log2Dim>::setValueOff(const Coord& xyz, const ValueType& val)
1128 {
1129  this->setValueOff(LeafNode::coordToOffset(xyz), val);
1130 }
1131 
1132 template<typename T, Index Log2Dim>
1133 inline void
1135 {
1136  assert(offset < SIZE);
1137  mBuffer.setValue(offset, val);
1138  mValueMask.setOff(offset);
1139 }
1140 
1141 
1142 template<typename T, Index Log2Dim>
1143 inline void
1144 LeafNode<T, Log2Dim>::setActiveState(const Coord& xyz, bool on)
1145 {
1146  mValueMask.set(this->coordToOffset(xyz), on);
1147 }
1148 
1149 
1150 template<typename T, Index Log2Dim>
1151 inline void
1152 LeafNode<T, Log2Dim>::setValueOnly(const Coord& xyz, const ValueType& val)
1153 {
1154  this->setValueOnly(LeafNode::coordToOffset(xyz), val);
1155 }
1156 
1157 template<typename T, Index Log2Dim>
1158 inline void
1160 {
1161  assert(offset<SIZE); mBuffer.setValue(offset, val);
1162 }
1163 
1164 
1166 
1167 
1168 template<typename T, Index Log2Dim>
1169 inline void
1170 LeafNode<T, Log2Dim>::clip(const CoordBBox& clipBBox, const T& background)
1171 {
1172  CoordBBox nodeBBox = this->getNodeBoundingBox();
1173  if (!clipBBox.hasOverlap(nodeBBox)) {
1174  // This node lies completely outside the clipping region. Fill it with the background.
1175  this->fill(background, /*active=*/false);
1176  } else if (clipBBox.isInside(nodeBBox)) {
1177  // This node lies completely inside the clipping region. Leave it intact.
1178  return;
1179  }
1180 
1181  // This node isn't completely contained inside the clipping region.
1182  // Set any voxels that lie outside the region to the background value.
1183 
1184  // Construct a boolean mask that is on inside the clipping region and off outside it.
1185  NodeMaskType mask;
1186  nodeBBox.intersect(clipBBox);
1187  Coord xyz;
1188  int &x = xyz.x(), &y = xyz.y(), &z = xyz.z();
1189  for (x = nodeBBox.min().x(); x <= nodeBBox.max().x(); ++x) {
1190  for (y = nodeBBox.min().y(); y <= nodeBBox.max().y(); ++y) {
1191  for (z = nodeBBox.min().z(); z <= nodeBBox.max().z(); ++z) {
1192  mask.setOn(static_cast<Index32>(this->coordToOffset(xyz)));
1193  }
1194  }
1195  }
1196 
1197  // Set voxels that lie in the inactive region of the mask (i.e., outside
1198  // the clipping region) to the background value.
1199  for (MaskOffIterator maskIter = mask.beginOff(); maskIter; ++maskIter) {
1200  this->setValueOff(maskIter.pos(), background);
1201  }
1202 }
1203 
1204 
1206 
1207 
1208 template<typename T, Index Log2Dim>
1209 inline void
1210 LeafNode<T, Log2Dim>::fill(const CoordBBox& bbox, const ValueType& value, bool active)
1211 {
1212 #if OPENVDB_ABI_VERSION_NUMBER >= 3
1213  if (!this->allocate()) return;
1214 #endif
1215 
1216  auto clippedBBox = this->getNodeBoundingBox();
1217  clippedBBox.intersect(bbox);
1218  if (!clippedBBox) return;
1219 
1220  for (Int32 x = clippedBBox.min().x(); x <= clippedBBox.max().x(); ++x) {
1221  const Index offsetX = (x & (DIM-1u)) << 2*Log2Dim;
1222  for (Int32 y = clippedBBox.min().y(); y <= clippedBBox.max().y(); ++y) {
1223  const Index offsetXY = offsetX + ((y & (DIM-1u)) << Log2Dim);
1224  for (Int32 z = clippedBBox.min().z(); z <= clippedBBox.max().z(); ++z) {
1225  const Index offset = offsetXY + (z & (DIM-1u));
1226  mBuffer[offset] = value;
1227  mValueMask.set(offset, active);
1228  }
1229  }
1230  }
1231 }
1232 
1233 template<typename T, Index Log2Dim>
1234 inline void
1236 {
1237  mBuffer.fill(value);
1238 }
1239 
1240 template<typename T, Index Log2Dim>
1241 inline void
1242 LeafNode<T, Log2Dim>::fill(const ValueType& value, bool active)
1243 {
1244  mBuffer.fill(value);
1245  mValueMask.set(active);
1246 }
1247 
1248 
1250 
1251 
1252 template<typename T, Index Log2Dim>
1253 template<typename DenseT>
1254 inline void
1255 LeafNode<T, Log2Dim>::copyToDense(const CoordBBox& bbox, DenseT& dense) const
1256 {
1257 #if OPENVDB_ABI_VERSION_NUMBER >= 3
1258  mBuffer.loadValues();
1259 #endif
1260 
1261  using DenseValueType = typename DenseT::ValueType;
1262 
1263  const size_t xStride = dense.xStride(), yStride = dense.yStride(), zStride = dense.zStride();
1264  const Coord& min = dense.bbox().min();
1265  DenseValueType* t0 = dense.data() + zStride * (bbox.min()[2] - min[2]); // target array
1266  const T* s0 = &mBuffer[bbox.min()[2] & (DIM-1u)]; // source array
1267  for (Int32 x = bbox.min()[0], ex = bbox.max()[0] + 1; x < ex; ++x) {
1268  DenseValueType* t1 = t0 + xStride * (x - min[0]);
1269  const T* s1 = s0 + ((x & (DIM-1u)) << 2*Log2Dim);
1270  for (Int32 y = bbox.min()[1], ey = bbox.max()[1] + 1; y < ey; ++y) {
1271  DenseValueType* t2 = t1 + yStride * (y - min[1]);
1272  const T* s2 = s1 + ((y & (DIM-1u)) << Log2Dim);
1273  for (Int32 z = bbox.min()[2], ez = bbox.max()[2] + 1; z < ez; ++z, t2 += zStride) {
1274  *t2 = DenseValueType(*s2++);
1275  }
1276  }
1277  }
1278 }
1279 
1280 
1281 template<typename T, Index Log2Dim>
1282 template<typename DenseT>
1283 inline void
1284 LeafNode<T, Log2Dim>::copyFromDense(const CoordBBox& bbox, const DenseT& dense,
1285  const ValueType& background, const ValueType& tolerance)
1286 {
1287 #if OPENVDB_ABI_VERSION_NUMBER >= 3
1288  if (!this->allocate()) return;
1289 #endif
1290 
1291  using DenseValueType = typename DenseT::ValueType;
1292 
1293  const size_t xStride = dense.xStride(), yStride = dense.yStride(), zStride = dense.zStride();
1294  const Coord& min = dense.bbox().min();
1295 
1296  const DenseValueType* s0 = dense.data() + zStride * (bbox.min()[2] - min[2]); // source
1297  const Int32 n0 = bbox.min()[2] & (DIM-1u);
1298  for (Int32 x = bbox.min()[0], ex = bbox.max()[0]+1; x < ex; ++x) {
1299  const DenseValueType* s1 = s0 + xStride * (x - min[0]);
1300  const Int32 n1 = n0 + ((x & (DIM-1u)) << 2*LOG2DIM);
1301  for (Int32 y = bbox.min()[1], ey = bbox.max()[1]+1; y < ey; ++y) {
1302  const DenseValueType* s2 = s1 + yStride * (y - min[1]);
1303  Int32 n2 = n1 + ((y & (DIM-1u)) << LOG2DIM);
1304  for (Int32 z = bbox.min()[2], ez = bbox.max()[2]+1; z < ez; ++z, ++n2, s2 += zStride) {
1305  if (math::isApproxEqual(background, ValueType(*s2), tolerance)) {
1306  mValueMask.setOff(n2);
1307  mBuffer[n2] = background;
1308  } else {
1309  mValueMask.setOn(n2);
1310  mBuffer[n2] = ValueType(*s2);
1311  }
1312  }
1313  }
1314  }
1315 }
1316 
1317 
1319 
1320 
1321 template<typename T, Index Log2Dim>
1322 inline void
1323 LeafNode<T, Log2Dim>::readTopology(std::istream& is, bool /*fromHalf*/)
1324 {
1325  mValueMask.load(is);
1326 }
1327 
1328 
1329 template<typename T, Index Log2Dim>
1330 inline void
1331 LeafNode<T, Log2Dim>::writeTopology(std::ostream& os, bool /*toHalf*/) const
1332 {
1333  mValueMask.save(os);
1334 }
1335 
1336 
1338 
1339 
1340 
1341 template<typename T, Index Log2Dim>
1342 inline void
1343 LeafNode<T,Log2Dim>::skipCompressedValues(bool seekable, std::istream& is, bool fromHalf)
1344 {
1345  if (seekable) {
1346  // Seek over voxel values.
1347  io::readCompressedValues<ValueType, NodeMaskType>(
1348  is, nullptr, SIZE, mValueMask, fromHalf);
1349  } else {
1350  // Read and discard voxel values.
1351  Buffer temp;
1352  io::readCompressedValues(is, temp.mData, SIZE, mValueMask, fromHalf);
1353  }
1354 }
1355 
1356 
1357 template<typename T, Index Log2Dim>
1358 inline void
1359 LeafNode<T,Log2Dim>::readBuffers(std::istream& is, bool fromHalf)
1360 {
1361  this->readBuffers(is, CoordBBox::inf(), fromHalf);
1362 }
1363 
1364 
1365 template<typename T, Index Log2Dim>
1366 inline void
1367 LeafNode<T,Log2Dim>::readBuffers(std::istream& is, const CoordBBox& clipBBox, bool fromHalf)
1368 {
1370  const bool seekable = meta && meta->seekable();
1371 
1372 #if OPENVDB_ABI_VERSION_NUMBER >= 3
1373  std::streamoff maskpos = is.tellg();
1374 #endif
1375 
1376  if (seekable) {
1377  // Seek over the value mask.
1378  mValueMask.seek(is);
1379  } else {
1380  // Read in the value mask.
1381  mValueMask.load(is);
1382  }
1383 
1384  int8_t numBuffers = 1;
1386  // Read in the origin.
1387  is.read(reinterpret_cast<char*>(&mOrigin), sizeof(Coord::ValueType) * 3);
1388 
1389  // Read in the number of buffers, which should now always be one.
1390  is.read(reinterpret_cast<char*>(&numBuffers), sizeof(int8_t));
1391  }
1392 
1393  CoordBBox nodeBBox = this->getNodeBoundingBox();
1394  if (!clipBBox.hasOverlap(nodeBBox)) {
1395  // This node lies completely outside the clipping region.
1396  skipCompressedValues(seekable, is, fromHalf);
1397  mValueMask.setOff();
1398  mBuffer.setOutOfCore(false);
1399  } else {
1400 #if OPENVDB_ABI_VERSION_NUMBER >= 3
1401  // If this node lies completely inside the clipping region and it is being read
1402  // from a memory-mapped file, delay loading of its buffer until the buffer
1403  // is actually accessed. (If this node requires clipping, its buffer
1404  // must be accessed and therefore must be loaded.)
1405  io::MappedFile::Ptr mappedFile = io::getMappedFilePtr(is);
1406  const bool delayLoad = ((mappedFile.get() != nullptr) && clipBBox.isInside(nodeBBox));
1407 
1408  if (delayLoad) {
1409  mBuffer.setOutOfCore(true);
1410  mBuffer.mFileInfo = new typename Buffer::FileInfo;
1411  mBuffer.mFileInfo->meta = meta;
1412  mBuffer.mFileInfo->bufpos = is.tellg();
1413  mBuffer.mFileInfo->mapping = mappedFile;
1414  // Save the offset to the value mask, because the in-memory copy
1415  // might change before the value buffer gets read.
1416  mBuffer.mFileInfo->maskpos = maskpos;
1417  // Skip over voxel values.
1418  skipCompressedValues(seekable, is, fromHalf);
1419  } else {
1420 #endif
1421  mBuffer.allocate();
1422  io::readCompressedValues(is, mBuffer.mData, SIZE, mValueMask, fromHalf);
1423  mBuffer.setOutOfCore(false);
1424 
1425  // Get this tree's background value.
1426  T background = zeroVal<T>();
1427  if (const void* bgPtr = io::getGridBackgroundValuePtr(is)) {
1428  background = *static_cast<const T*>(bgPtr);
1429  }
1430  this->clip(clipBBox, background);
1431 #if OPENVDB_ABI_VERSION_NUMBER >= 3
1432  }
1433 #endif
1434  }
1435 
1436  if (numBuffers > 1) {
1437  // Read in and discard auxiliary buffers that were created with earlier
1438  // versions of the library. (Auxiliary buffers are not mask compressed.)
1439  const bool zipped = io::getDataCompression(is) & io::COMPRESS_ZIP;
1440  Buffer temp;
1441  for (int i = 1; i < numBuffers; ++i) {
1442  if (fromHalf) {
1443  io::HalfReader<io::RealToHalf<T>::isReal, T>::read(is, temp.mData, SIZE, zipped);
1444  } else {
1445  io::readData<T>(is, temp.mData, SIZE, zipped);
1446  }
1447  }
1448  }
1449 
1450  // increment the leaf number
1451  if (meta) meta->setLeaf(meta->leaf() + 1);
1452 }
1453 
1454 
1455 template<typename T, Index Log2Dim>
1456 inline void
1457 LeafNode<T, Log2Dim>::writeBuffers(std::ostream& os, bool toHalf) const
1458 {
1459  // Write out the value mask.
1460  mValueMask.save(os);
1461 
1462  mBuffer.loadValues();
1463 
1464  io::writeCompressedValues(os, mBuffer.mData, SIZE,
1465  mValueMask, /*childMask=*/NodeMaskType(), toHalf);
1466 }
1467 
1468 
1470 
1471 
1472 template<typename T, Index Log2Dim>
1473 inline bool
1475 {
1476  return mOrigin == other.mOrigin &&
1477  mValueMask == other.valueMask() &&
1478  mBuffer == other.mBuffer;
1479 }
1480 
1481 
1482 template<typename T, Index Log2Dim>
1483 inline Index64
1485 {
1486  // Use sizeof(*this) to capture alignment-related padding
1487  // (but note that sizeof(*this) includes sizeof(mBuffer)).
1488  return sizeof(*this) + mBuffer.memUsage() - sizeof(mBuffer);
1489 }
1490 
1491 
1492 template<typename T, Index Log2Dim>
1493 inline void
1494 LeafNode<T, Log2Dim>::evalActiveBoundingBox(CoordBBox& bbox, bool visitVoxels) const
1495 {
1496  CoordBBox this_bbox = this->getNodeBoundingBox();
1497  if (bbox.isInside(this_bbox)) return;//this LeafNode is already enclosed in the bbox
1498  if (ValueOnCIter iter = this->cbeginValueOn()) {//any active values?
1499  if (visitVoxels) {//use voxel granularity?
1500  this_bbox.reset();
1501  for(; iter; ++iter) this_bbox.expand(this->offsetToLocalCoord(iter.pos()));
1502  this_bbox.translate(this->origin());
1503  }
1504  bbox.expand(this_bbox);
1505  }
1506 }
1507 
1508 
1509 template<typename T, Index Log2Dim>
1510 template<typename OtherType, Index OtherLog2Dim>
1511 inline bool
1513 {
1514  assert(other);
1515  return (Log2Dim == OtherLog2Dim && mValueMask == other->getValueMask());
1516 }
1517 
1518 template<typename T, Index Log2Dim>
1519 inline bool
1521  bool& state,
1522  const ValueType& tolerance) const
1523 {
1524  if (!mValueMask.isConstant(state)) return false;// early termination
1525  firstValue = mBuffer[0];
1526  for (Index i = 1; i < SIZE; ++i) {
1527  if ( !math::isApproxEqual(mBuffer[i], firstValue, tolerance) ) return false;// early termination
1528  }
1529  return true;
1530 }
1531 
1532 template<typename T, Index Log2Dim>
1533 inline bool
1535  ValueType& maxValue,
1536  bool& state,
1537  const ValueType& tolerance) const
1538 {
1539  if (!mValueMask.isConstant(state)) return false;// early termination
1540  minValue = maxValue = mBuffer[0];
1541  for (Index i = 1; i < SIZE; ++i) {
1542  const T& v = mBuffer[i];
1543  if (v < minValue) {
1544  if ((maxValue - v) > tolerance) return false;// early termination
1545  minValue = v;
1546  } else if (v > maxValue) {
1547  if ((v - minValue) > tolerance) return false;// early termination
1548  maxValue = v;
1549  }
1550  }
1551  return true;
1552 }
1553 
1554 template<typename T, Index Log2Dim>
1555 inline T
1557 {
1558  std::unique_ptr<T[]> data(nullptr);
1559  if (tmp == nullptr) {//allocate temporary storage
1560  data.reset(new T[NUM_VALUES]);
1561  tmp = data.get();
1562  }
1563  if (tmp != mBuffer.data()) {
1564  const T* src = mBuffer.data();
1565  for (T* dst = tmp; dst-tmp < NUM_VALUES;) *dst++ = *src++;
1566  }
1567  static const size_t midpoint = (NUM_VALUES - 1) >> 1;
1568  std::nth_element(tmp, tmp + midpoint, tmp + NUM_VALUES);
1569  return tmp[midpoint];
1570 }
1571 
1572 template<typename T, Index Log2Dim>
1573 inline Index
1574 LeafNode<T, Log2Dim>::medianOn(T &value, T *tmp) const
1575 {
1576  const Index count = mValueMask.countOn();
1577  if (count == NUM_VALUES) {//special case: all voxels are active
1578  value = this->medianAll(tmp);
1579  return NUM_VALUES;
1580  } else if (count == 0) {
1581  return 0;
1582  }
1583  std::unique_ptr<T[]> data(nullptr);
1584  if (tmp == nullptr) {//allocate temporary storage
1585  data.reset(new T[count]);// 0 < count < NUM_VALUES
1586  tmp = data.get();
1587  }
1588  for (auto iter=this->cbeginValueOn(); iter; ++iter) *tmp++ = *iter;
1589  T *begin = tmp - count;
1590  const size_t midpoint = (count - 1) >> 1;
1591  std::nth_element(begin, begin + midpoint, tmp);
1592  value = begin[midpoint];
1593  return count;
1594 }
1595 
1596 template<typename T, Index Log2Dim>
1597 inline Index
1598 LeafNode<T, Log2Dim>::medianOff(T &value, T *tmp) const
1599 {
1600  const Index count = mValueMask.countOff();
1601  if (count == NUM_VALUES) {//special case: all voxels are inactive
1602  value = this->medianAll(tmp);
1603  return NUM_VALUES;
1604  } else if (count == 0) {
1605  return 0;
1606  }
1607  std::unique_ptr<T[]> data(nullptr);
1608  if (tmp == nullptr) {//allocate temporary storage
1609  data.reset(new T[count]);// 0 < count < NUM_VALUES
1610  tmp = data.get();
1611  }
1612  for (auto iter=this->cbeginValueOff(); iter; ++iter) *tmp++ = *iter;
1613  T *begin = tmp - count;
1614  const size_t midpoint = (count - 1) >> 1;
1615  std::nth_element(begin, begin + midpoint, tmp);
1616  value = begin[midpoint];
1617  return count;
1618 }
1619 
1621 
1622 
1623 template<typename T, Index Log2Dim>
1624 inline void
1625 LeafNode<T, Log2Dim>::addTile(Index /*level*/, const Coord& xyz, const ValueType& val, bool active)
1626 {
1627  this->addTile(this->coordToOffset(xyz), val, active);
1628 }
1629 
1630 template<typename T, Index Log2Dim>
1631 inline void
1632 LeafNode<T, Log2Dim>::addTile(Index offset, const ValueType& val, bool active)
1633 {
1634  assert(offset < SIZE);
1635  setValueOnly(offset, val);
1636  setActiveState(offset, active);
1637 }
1638 
1639 template<typename T, Index Log2Dim>
1640 template<typename AccessorT>
1641 inline void
1643  const ValueType& val, bool active, AccessorT&)
1644 {
1645  this->addTile(level, xyz, val, active);
1646 }
1647 
1648 
1650 
1651 
1652 template<typename T, Index Log2Dim>
1653 inline void
1655  const ValueType& newBackground)
1656 {
1657 #if OPENVDB_ABI_VERSION_NUMBER >= 3
1658  if (!this->allocate()) return;
1659 #endif
1660 
1661  typename NodeMaskType::OffIterator iter;
1662  // For all inactive values...
1663  for (iter = this->mValueMask.beginOff(); iter; ++iter) {
1664  ValueType &inactiveValue = mBuffer[iter.pos()];
1665  if (math::isApproxEqual(inactiveValue, oldBackground)) {
1666  inactiveValue = newBackground;
1667  } else if (math::isApproxEqual(inactiveValue, math::negative(oldBackground))) {
1668  inactiveValue = math::negative(newBackground);
1669  }
1670  }
1671 }
1672 
1673 
1674 template<typename T, Index Log2Dim>
1675 template<MergePolicy Policy>
1676 inline void
1678 {
1679 #if OPENVDB_ABI_VERSION_NUMBER >= 3
1680  if (!this->allocate()) return;
1681 #endif
1682 
1684  if (Policy == MERGE_NODES) return;
1685  typename NodeMaskType::OnIterator iter = other.valueMask().beginOn();
1686  for (; iter; ++iter) {
1687  const Index n = iter.pos();
1688  if (mValueMask.isOff(n)) {
1689  mBuffer[n] = other.mBuffer[n];
1690  mValueMask.setOn(n);
1691  }
1692  }
1694 }
1695 
1696 template<typename T, Index Log2Dim>
1697 template<MergePolicy Policy>
1698 inline void
1700  const ValueType& /*bg*/, const ValueType& /*otherBG*/)
1701 {
1702  this->template merge<Policy>(other);
1703 }
1704 
1705 template<typename T, Index Log2Dim>
1706 template<MergePolicy Policy>
1707 inline void
1708 LeafNode<T, Log2Dim>::merge(const ValueType& tileValue, bool tileActive)
1709 {
1710 #if OPENVDB_ABI_VERSION_NUMBER >= 3
1711  if (!this->allocate()) return;
1712 #endif
1713 
1715  if (Policy != MERGE_ACTIVE_STATES_AND_NODES) return;
1716  if (!tileActive) return;
1717  // Replace all inactive values with the active tile value.
1718  for (typename NodeMaskType::OffIterator iter = mValueMask.beginOff(); iter; ++iter) {
1719  const Index n = iter.pos();
1720  mBuffer[n] = tileValue;
1721  mValueMask.setOn(n);
1722  }
1724 }
1725 
1726 
1727 template<typename T, Index Log2Dim>
1728 template<typename OtherType>
1729 inline void
1731 {
1732  mValueMask |= other.valueMask();
1733 }
1734 
1735 template<typename T, Index Log2Dim>
1736 template<typename OtherType>
1737 inline void
1739  const ValueType&)
1740 {
1741  mValueMask &= other.valueMask();
1742 }
1743 
1744 template<typename T, Index Log2Dim>
1745 template<typename OtherType>
1746 inline void
1748  const ValueType&)
1749 {
1750  mValueMask &= !other.valueMask();
1751 }
1752 
1753 template<typename T, Index Log2Dim>
1754 inline void
1756 {
1757 #if OPENVDB_ABI_VERSION_NUMBER >= 3
1758  if (!this->allocate()) return;
1759 #endif
1760  for (Index i = 0; i < SIZE; ++i) {
1761  mBuffer[i] = -mBuffer[i];
1762  }
1763 }
1764 
1765 
1767 
1768 
1769 template<typename T, Index Log2Dim>
1770 template<typename CombineOp>
1771 inline void
1772 LeafNode<T, Log2Dim>::combine(const LeafNode& other, CombineOp& op)
1773 {
1774 #if OPENVDB_ABI_VERSION_NUMBER >= 3
1775  if (!this->allocate()) return;
1776 #endif
1777  CombineArgs<T> args;
1778  for (Index i = 0; i < SIZE; ++i) {
1779  op(args.setARef(mBuffer[i])
1780  .setAIsActive(mValueMask.isOn(i))
1781  .setBRef(other.mBuffer[i])
1782  .setBIsActive(other.valueMask().isOn(i))
1783  .setResultRef(mBuffer[i]));
1784  mValueMask.set(i, args.resultIsActive());
1785  }
1786 }
1787 
1788 
1789 template<typename T, Index Log2Dim>
1790 template<typename CombineOp>
1791 inline void
1792 LeafNode<T, Log2Dim>::combine(const ValueType& value, bool valueIsActive, CombineOp& op)
1793 {
1794 #if OPENVDB_ABI_VERSION_NUMBER >= 3
1795  if (!this->allocate()) return;
1796 #endif
1797  CombineArgs<T> args;
1798  args.setBRef(value).setBIsActive(valueIsActive);
1799  for (Index i = 0; i < SIZE; ++i) {
1800  op(args.setARef(mBuffer[i])
1801  .setAIsActive(mValueMask.isOn(i))
1802  .setResultRef(mBuffer[i]));
1803  mValueMask.set(i, args.resultIsActive());
1804  }
1805 }
1806 
1807 
1809 
1810 
1811 template<typename T, Index Log2Dim>
1812 template<typename CombineOp, typename OtherType>
1813 inline void
1814 LeafNode<T, Log2Dim>::combine2(const LeafNode& other, const OtherType& value,
1815  bool valueIsActive, CombineOp& op)
1816 {
1817 #if OPENVDB_ABI_VERSION_NUMBER >= 3
1818  if (!this->allocate()) return;
1819 #endif
1821  args.setBRef(value).setBIsActive(valueIsActive);
1822  for (Index i = 0; i < SIZE; ++i) {
1823  op(args.setARef(other.mBuffer[i])
1824  .setAIsActive(other.valueMask().isOn(i))
1825  .setResultRef(mBuffer[i]));
1826  mValueMask.set(i, args.resultIsActive());
1827  }
1828 }
1829 
1830 
1831 template<typename T, Index Log2Dim>
1832 template<typename CombineOp, typename OtherNodeT>
1833 inline void
1834 LeafNode<T, Log2Dim>::combine2(const ValueType& value, const OtherNodeT& other,
1835  bool valueIsActive, CombineOp& op)
1836 {
1837 #if OPENVDB_ABI_VERSION_NUMBER >= 3
1838  if (!this->allocate()) return;
1839 #endif
1841  args.setARef(value).setAIsActive(valueIsActive);
1842  for (Index i = 0; i < SIZE; ++i) {
1843  op(args.setBRef(other.mBuffer[i])
1844  .setBIsActive(other.valueMask().isOn(i))
1845  .setResultRef(mBuffer[i]));
1846  mValueMask.set(i, args.resultIsActive());
1847  }
1848 }
1849 
1850 
1851 template<typename T, Index Log2Dim>
1852 template<typename CombineOp, typename OtherNodeT>
1853 inline void
1854 LeafNode<T, Log2Dim>::combine2(const LeafNode& b0, const OtherNodeT& b1, CombineOp& op)
1855 {
1856 #if OPENVDB_ABI_VERSION_NUMBER >= 3
1857  if (!this->allocate()) return;
1858 #endif
1860  for (Index i = 0; i < SIZE; ++i) {
1861  mValueMask.set(i, b0.valueMask().isOn(i) || b1.valueMask().isOn(i));
1862  op(args.setARef(b0.mBuffer[i])
1863  .setAIsActive(b0.valueMask().isOn(i))
1864  .setBRef(b1.mBuffer[i])
1865  .setBIsActive(b1.valueMask().isOn(i))
1866  .setResultRef(mBuffer[i]));
1867  mValueMask.set(i, args.resultIsActive());
1868  }
1869 }
1870 
1871 
1873 
1874 
1875 template<typename T, Index Log2Dim>
1876 template<typename BBoxOp>
1877 inline void
1879 {
1880  if (op.template descent<LEVEL>()) {
1881  for (ValueOnCIter i=this->cbeginValueOn(); i; ++i) {
1882 #ifdef _MSC_VER
1883  op.operator()<LEVEL>(CoordBBox::createCube(i.getCoord(), 1));
1884 #else
1885  op.template operator()<LEVEL>(CoordBBox::createCube(i.getCoord(), 1));
1886 #endif
1887  }
1888  } else {
1889 #ifdef _MSC_VER
1890  op.operator()<LEVEL>(this->getNodeBoundingBox());
1891 #else
1892  op.template operator()<LEVEL>(this->getNodeBoundingBox());
1893 #endif
1894  }
1895 }
1896 
1897 
1898 template<typename T, Index Log2Dim>
1899 template<typename VisitorOp>
1900 inline void
1902 {
1903  doVisit<LeafNode, VisitorOp, ChildAllIter>(*this, op);
1904 }
1905 
1906 
1907 template<typename T, Index Log2Dim>
1908 template<typename VisitorOp>
1909 inline void
1910 LeafNode<T, Log2Dim>::visit(VisitorOp& op) const
1911 {
1912  doVisit<const LeafNode, VisitorOp, ChildAllCIter>(*this, op);
1913 }
1914 
1915 
1916 template<typename T, Index Log2Dim>
1917 template<typename NodeT, typename VisitorOp, typename ChildAllIterT>
1918 inline void
1919 LeafNode<T, Log2Dim>::doVisit(NodeT& self, VisitorOp& op)
1920 {
1921  for (ChildAllIterT iter = self.beginChildAll(); iter; ++iter) {
1922  op(iter);
1923  }
1924 }
1925 
1926 
1928 
1929 
1930 template<typename T, Index Log2Dim>
1931 template<typename OtherLeafNodeType, typename VisitorOp>
1932 inline void
1933 LeafNode<T, Log2Dim>::visit2Node(OtherLeafNodeType& other, VisitorOp& op)
1934 {
1935  doVisit2Node<LeafNode, OtherLeafNodeType, VisitorOp, ChildAllIter,
1936  typename OtherLeafNodeType::ChildAllIter>(*this, other, op);
1937 }
1938 
1939 
1940 template<typename T, Index Log2Dim>
1941 template<typename OtherLeafNodeType, typename VisitorOp>
1942 inline void
1943 LeafNode<T, Log2Dim>::visit2Node(OtherLeafNodeType& other, VisitorOp& op) const
1944 {
1945  doVisit2Node<const LeafNode, OtherLeafNodeType, VisitorOp, ChildAllCIter,
1946  typename OtherLeafNodeType::ChildAllCIter>(*this, other, op);
1947 }
1948 
1949 
1950 template<typename T, Index Log2Dim>
1951 template<
1952  typename NodeT,
1953  typename OtherNodeT,
1954  typename VisitorOp,
1955  typename ChildAllIterT,
1956  typename OtherChildAllIterT>
1957 inline void
1958 LeafNode<T, Log2Dim>::doVisit2Node(NodeT& self, OtherNodeT& other, VisitorOp& op)
1959 {
1960  // Allow the two nodes to have different ValueTypes, but not different dimensions.
1961  static_assert(OtherNodeT::SIZE == NodeT::SIZE,
1962  "can't visit nodes of different sizes simultaneously");
1963  static_assert(OtherNodeT::LEVEL == NodeT::LEVEL,
1964  "can't visit nodes at different tree levels simultaneously");
1965 
1966  ChildAllIterT iter = self.beginChildAll();
1967  OtherChildAllIterT otherIter = other.beginChildAll();
1968 
1969  for ( ; iter && otherIter; ++iter, ++otherIter) {
1970  op(iter, otherIter);
1971  }
1972 }
1973 
1974 
1976 
1977 
1978 template<typename T, Index Log2Dim>
1979 template<typename IterT, typename VisitorOp>
1980 inline void
1981 LeafNode<T, Log2Dim>::visit2(IterT& otherIter, VisitorOp& op, bool otherIsLHS)
1982 {
1983  doVisit2<LeafNode, VisitorOp, ChildAllIter, IterT>(
1984  *this, otherIter, op, otherIsLHS);
1985 }
1986 
1987 
1988 template<typename T, Index Log2Dim>
1989 template<typename IterT, typename VisitorOp>
1990 inline void
1991 LeafNode<T, Log2Dim>::visit2(IterT& otherIter, VisitorOp& op, bool otherIsLHS) const
1992 {
1993  doVisit2<const LeafNode, VisitorOp, ChildAllCIter, IterT>(
1994  *this, otherIter, op, otherIsLHS);
1995 }
1996 
1997 
1998 template<typename T, Index Log2Dim>
1999 template<
2000  typename NodeT,
2001  typename VisitorOp,
2002  typename ChildAllIterT,
2003  typename OtherChildAllIterT>
2004 inline void
2005 LeafNode<T, Log2Dim>::doVisit2(NodeT& self, OtherChildAllIterT& otherIter,
2006  VisitorOp& op, bool otherIsLHS)
2007 {
2008  if (!otherIter) return;
2009 
2010  if (otherIsLHS) {
2011  for (ChildAllIterT iter = self.beginChildAll(); iter; ++iter) {
2012  op(otherIter, iter);
2013  }
2014  } else {
2015  for (ChildAllIterT iter = self.beginChildAll(); iter; ++iter) {
2016  op(iter, otherIter);
2017  }
2018  }
2019 }
2020 
2021 
2023 
2024 
2025 template<typename T, Index Log2Dim>
2026 inline std::ostream&
2027 operator<<(std::ostream& os, const typename LeafNode<T, Log2Dim>::Buffer& buf)
2028 {
2029  for (Index32 i = 0, N = buf.size(); i < N; ++i) os << buf.mData[i] << ", ";
2030  return os;
2031 }
2032 
2033 } // namespace tree
2034 } // namespace OPENVDB_VERSION_NAME
2035 } // namespace openvdb
2036 
2037 
2039 
2040 
2041 // Specialization for LeafNodes of type bool
2042 #include "LeafNodeBool.h"
2043 
2044 // Specialization for LeafNodes with mask information only
2045 #include "LeafNodeMask.h"
2046 
2047 #endif // OPENVDB_TREE_LEAFNODE_HAS_BEEN_INCLUDED
2048 
2049 // Copyright (c) DreamWorks Animation LLC
2050 // All rights reserved. This software is distributed under the
2051 // Mozilla Public License 2.0 ( http://www.mozilla.org/MPL/2.0/ )
ValueT & getValue() const
Definition: LeafNode.h:248
static const Index LEVEL
Definition: LeafNode.h:81
Index64 offLeafVoxelCount() const
Definition: LeafNode.h:170
T negative(const T &val)
Return the unary negation of the given value.
Definition: Math.h:108
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:1210
CoordBBox getNodeBoundingBox() const
Return the bounding box of this node, i.e., the full index space spanned by this leaf node...
Definition: LeafNode.h:195
std::string str() const
Return a string representation of this node.
Definition: LeafNode.h:1046
static void doVisit(NodeT &, VisitorOp &)
Definition: LeafNode.h:1919
static Index32 leafCount()
Return the leaf count for this node, which is one.
Definition: LeafNode.h:161
bool isChildMaskOn(Index) const
Definition: LeafNode.h:900
const NodeMaskType & valueMask() const
Definition: LeafNode.h:898
DenseIter< const LeafNode, const ValueType, ChildAll > ChildAllCIter
Definition: LeafNode.h:318
OPENVDB_API 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 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:367
void visit2Node(OtherLeafNodeType &other, VisitorOp &)
Definition: LeafNode.h:1933
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:571
bool isDense() const
Return true if this node contains only active voxels.
Definition: LeafNode.h:176
Base class for dense iterators over internal and leaf nodes.
Definition: Iterator.h:205
bool operator!=(const LeafNode &other) const
Definition: LeafNode.h:224
static bool hasActiveTiles()
Return false since leaf nodes never contain tiles.
Definition: LeafNode.h:503
void clip(const CoordBBox &, const ValueType &background)
Set all voxels that lie outside the given axis-aligned box to the background.
Definition: LeafNode.h:1170
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:632
Index32 countOff() const
Return the total number of on bits.
Definition: NodeMasks.h:452
const NodeMaskType & getValueMask() const
Definition: LeafNode.h:896
const LeafNode * probeLeafAndCache(const Coord &, AccessorT &) const
Return a const pointer to this node.
Definition: LeafNode.h:779
ValueOnCIter cendValueOn() const
Definition: LeafNode.h:330
void visitActiveBBox(BBoxOp &) const
Calls the templated functor BBoxOp with bounding box information. An additional level argument is pro...
Definition: LeafNode.h:1878
void seek(std::istream &is) const
Definition: NodeMasks.h:572
Definition: LeafNode.h:49
static Index log2dim()
Return log2 of the dimension of this LeafNode, e.g. 3 if dimensions are 8^3.
Definition: LeafNode.h:147
ValueConverter<T>::Type is the type of a LeafNode having the same dimensions as this node but a diffe...
Definition: LeafNode.h:86
const ValueType & getValueAndCache(const Coord &xyz, AccessorT &) const
Return the value of the voxel at the given coordinates.
Definition: LeafNode.h:558
Leaf nodes have no children, so their child iterators have no get/set accessors.
Definition: LeafNode.h:271
void setValueOn(const Coord &xyz)
Mark the voxel at the given coordinates as active but don&#39;t change its value.
Definition: LeafNode.h:440
Coord offsetToGlobalCoord(Index n) const
Return the global coordinates for a linear table offset.
Definition: LeafNode.h:1083
void skipCompressedValues(bool seekable, std::istream &, bool fromHalf)
Definition: LeafNode.h:1343
static const Index DIM
Definition: LeafNode.h:77
void setValueMaskOff(Index n)
Definition: LeafNode.h:906
LeafNode * probeLeafAndCache(const Coord &, AccessorT &)
Return a pointer to this node.
Definition: LeafNode.h:771
bool probeValue(const Coord &xyz, ValueType &val) const
Return true if the voxel at the given coordinates is active.
Definition: LeafNode.h:1110
void setValue(Index i, const ValueType &)
Set the i&#39;th value of this buffer to the specified value.
Definition: LeafBuffer.h:294
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:1730
static Coord offsetToLocalCoord(Index n)
Return the local coordinates for a linear table offset, where offset 0 has coordinates (0...
Definition: LeafNode.h:1069
bool isInactive() const
Return true if all of this node&#39;s values are inactive.
Definition: LeafNode.h:867
ChildAllCIter beginChildAll() const
Definition: LeafNode.h:349
ChildOnCIter beginChildOn() const
Definition: LeafNode.h:343
ChildOnIter endChildOn()
Definition: LeafNode.h:354
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:472
Index medianOn(ValueType &value, ValueType *tmp=nullptr) const
Computes the median value of all the active voxels in this node.
Definition: LeafNode.h:1574
OnIterator beginOn() const
Definition: NodeMasks.h:354
bool isConstant(bool &isOn) const
Definition: NodeMasks.h:528
static Index getValueLevelAndCache(const Coord &, AccessorT &)
Return the LEVEL (=0) at which leaf node values reside.
Definition: LeafNode.h:643
void prune(TreeT &tree, typename TreeT::ValueType tolerance=zeroVal< typename TreeT::ValueType >(), bool threaded=true, size_t grainSize=1)
Reduce the memory footprint of a tree by replacing with tiles any nodes whose values are all the same...
Definition: Prune.h:361
void modifyValueAndActiveStateAndCache(const Coord &xyz, const ModifyOp &op, AccessorT &)
Definition: LeafNode.h:597
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:580
static Index64 onTileCount()
Definition: LeafNode.h:171
void setValueMaskOn(Index n)
Definition: LeafNode.h:905
const ValueType * data() const
Return a const pointer to the array of voxel values.
Definition: LeafBuffer.h:397
void writeTopology(std::ostream &os, bool toHalf=false) const
Write out just the topology.
Definition: LeafNode.h:1331
void getOrigin(Int32 &x, Int32 &y, Int32 &z) const
Return the grid index coordinates of this node&#39;s local origin.
Definition: LeafNode.h:203
Base class for iterators over internal and leaf nodes.
Definition: Iterator.h:56
const NodeT * probeConstNode(const Coord &) const
This function exists only to enable template instantiation.
Definition: LeafNode.h:746
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:1152
typename NodeMaskType::DenseIterator MaskDenseIterator
Definition: LeafNode.h:229
static Index getChildDim()
Return the dimension of child nodes of this LeafNode, which is one for voxels.
Definition: LeafNode.h:159
bool isValueMaskOn() const
Definition: LeafNode.h:893
OPENVDB_API SharedPtr< StreamMetadata > getStreamMetadataPtr(std::ios_base &)
Return a shared pointer to an object that stores metadata (file format, compression scheme...
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:1512
Index32 pos() const
Definition: NodeMasks.h:202
const ValueType & getFirstValue() const
Return a const reference to the first value in the buffer.
Definition: LeafNode.h:648
Bit mask for the internal and leaf nodes of VDB. This is a 64-bit implementation. ...
Definition: NodeMasks.h:309
void addTileAndCache(Index, const Coord &, const ValueType &, bool, AccessorT &)
Definition: LeafNode.h:1642
void readBuffers(std::istream &is, bool fromHalf=false)
Read buffers from a stream.
Definition: LeafNode.h:1359
bool isValueMaskOff(Index n) const
Definition: LeafNode.h:894
Index32 Index
Definition: Types.h:61
OPENVDB_API SharedPtr< MappedFile > getMappedFilePtr(std::ios_base &)
Return a shared pointer to the memory-mapped file with which the given stream is associated, or a null pointer if the stream is not associated with a memory-mapped file.
ValueType * mData
Definition: LeafBuffer.h:225
void visit(VisitorOp &)
Definition: LeafNode.h:1901
std::shared_ptr< T > SharedPtr
Definition: Types.h:139
ChildAllIter endChildAll()
Definition: LeafNode.h:360
const Coord & origin() const
Return the grid index coordinates of this node&#39;s local origin.
Definition: LeafNode.h:201
void getNodes(ArrayT &) const
This function exists only to enable template instantiation.
Definition: LeafNode.h:747
ChildOnCIter cendChildOn() const
Definition: LeafNode.h:352
ChildAllCIter endChildAll() const
Definition: LeafNode.h:359
void setValue(const ValueT &value) const
Definition: LeafNode.h:256
void setItem(Index pos, const ValueT &value) const
Definition: LeafNode.h:251
void addLeafAndCache(LeafNode *, AccessorT &)
This function exists only to enable template instantiation.
Definition: LeafNode.h:740
ValueOffIter beginValueOff()
Definition: LeafNode.h:325
ValueOffCIter endValueOff() const
Definition: LeafNode.h:334
ValueAllIter beginValueAll()
Definition: LeafNode.h:328
DenseIter< LeafNode, ValueType, ChildAll > ChildAllIter
Definition: LeafNode.h:317
ChildAllIter beginChildAll()
Definition: LeafNode.h:350
ChildOffCIter beginChildOff() const
Definition: LeafNode.h:346
static Index getLevel()
Return the level of this node, which by definition is zero for LeafNodes.
Definition: LeafNode.h:155
void setValuesOn()
Mark all voxels as active but don&#39;t change their values.
Definition: LeafNode.h:493
DenseIter(const MaskDenseIterator &iter, NodeT *parent)
Definition: LeafNode.h:287
ValueIter(const MaskIterT &iter, NodeT *parent)
Definition: LeafNode.h:245
static Index dim()
Return the number of voxels in each coordinate dimension.
Definition: LeafNode.h:149
OPENVDB_API const void * getGridBackgroundValuePtr(std::ios_base &)
Return a pointer to the background value of the grid currently being read from or written to the give...
Tag dispatch class that distinguishes topology copy constructors from deep copy constructors.
Definition: Types.h:750
Definition: LeafNode.h:233
const ValueType & getLastValue() const
Return a const reference to the last value in the buffer.
Definition: LeafNode.h:650
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:1144
Definition: NodeMasks.h:241
bool isChildMaskOff(Index) const
Definition: LeafNode.h:901
ValueOffCIter beginValueOff() const
Definition: LeafNode.h:324
void setOrigin(const Coord &origin)
Set the grid index coordinates of this node&#39;s local origin.
Definition: LeafNode.h:198
Definition: NodeMasks.h:210
static void getNodeLog2Dims(std::vector< Index > &dims)
Append the Log2Dim of this LeafNode to the specified vector.
Definition: LeafNode.h:157
ValueIter()
Definition: LeafNode.h:244
bool isValueOn(const Coord &xyz) const
Return true if the voxel at the given coordinates is active.
Definition: LeafNode.h:498
ValueOffCIter cbeginValueOff() const
Definition: LeafNode.h:323
SharedPtr< LeafNode > Ptr
Definition: LeafNode.h:72
void merge(const LeafNode &)
Definition: LeafNode.h:1677
typename NodeMaskType::OnIterator MaskOnIterator
Definition: LeafNode.h:227
Definition: LeafNode.h:233
void writeCompressedValues(std::ostream &os, ValueT *srcBuf, Index srcCount, const MaskT &valueMask, const MaskT &childMask, bool toHalf)
Definition: Compression.h:672
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:422
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h:128
typename NodeMaskType::OffIterator MaskOffIterator
Definition: LeafNode.h:228
ValueAllCIter cbeginValueAll() const
Definition: LeafNode.h:326
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:444
ChildIter(const MaskIterT &iter, NodeT *parent)
Definition: LeafNode.h:275
Index medianOff(ValueType &value, ValueType *tmp=nullptr) const
Computes the median value of all the inactive voxels in this node.
Definition: LeafNode.h:1598
Definition: LeafNode.h:232
NodeT * stealNode(const Coord &, const ValueType &, bool)
This function exists only to enable template instantiation.
Definition: LeafNode.h:742
OPENVDB_API uint32_t getFormatVersion(std::ios_base &)
Return the file format version number associated with the given input stream.
ChildOffCIter cbeginChildOff() const
Definition: LeafNode.h:345
LeafNode * touchLeafAndCache(const Coord &, AccessorT &)
Return a pointer to this node.
Definition: LeafNode.h:760
ValueOnCIter endValueOn() const
Definition: LeafNode.h:331
ChildOnIter beginChildOn()
Definition: LeafNode.h:344
static void doVisit2(NodeT &self, OtherChildAllIterT &, VisitorOp &, bool otherIsLHS)
Definition: LeafNode.h:2005
SameConfiguration<OtherNodeType>::value is true if and only if OtherNodeType is the type of a LeafNod...
Definition: LeafNode.h:91
ChildOffCIter cendChildOff() const
Definition: LeafNode.h:355
ValueOnCIter cbeginValueOn() const
Definition: LeafNode.h:320
void setOn(Index32 n)
Set the nth bit on.
Definition: NodeMasks.h:454
const Buffer & buffer() const
Definition: LeafNode.h:368
void denseFill(const CoordBBox &bbox, const ValueType &value, bool active=true)
Set all voxels within an axis-aligned box to the specified value and active state.
Definition: LeafNode.h:511
ChildAllCIter cendChildAll() const
Definition: LeafNode.h:358
Definition: Compression.h:319
bool isValueOnAndCache(const Coord &xyz, AccessorT &) const
Return true if the voxel at the given coordinates is active.
Definition: LeafNode.h:566
T ValueType
Definition: LeafNode.h:68
ValueAllCIter endValueAll() const
Definition: LeafNode.h:337
FileInfo * mFileInfo
Definition: LeafBuffer.h:226
bool isApproxEqual(const Type &a, const Type &b)
Return true if a is equal to b to within the default floating-point comparison tolerance.
Definition: Math.h:378
Definition: Exceptions.h:40
NodeT * probeNode(const Coord &)
This function exists only to enable template instantiation.
Definition: LeafNode.h:744
void evalActiveBoundingBox(CoordBBox &bbox, bool visitVoxels=true) const
Definition: LeafNode.h:1494
#define OPENVDB_NO_UNREACHABLE_CODE_WARNING_BEGIN
Definition: Platform.h:149
Index64 memUsage() const
Return the memory in bytes occupied by this node.
Definition: LeafNode.h:1484
ChildOffCIter endChildOff() const
Definition: LeafNode.h:356
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:448
ValueAllIter endValueAll()
Definition: LeafNode.h:338
const LeafNode * probeLeaf(const Coord &) const
Return a const pointer to this node.
Definition: LeafNode.h:780
void visit2(IterT &otherIter, VisitorOp &, bool otherIsLHS=false)
Definition: LeafNode.h:1981
ChildOffIter endChildOff()
Definition: LeafNode.h:357
void setValueOff(Index offset)
Mark the voxel at the given offset as inactive but don&#39;t change its value.
Definition: LeafNode.h:432
Base class for sparse iterators over internal and leaf nodes.
Definition: Iterator.h:141
T BuildType
Definition: LeafNode.h:67
void unsetItem(Index pos, const ValueT &value) const
Definition: LeafNode.h:300
bool isValueMaskOff() const
Definition: LeafNode.h:895
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:595
static Index size()
Return the total number of voxels represented by this LeafNode.
Definition: LeafNode.h:151
uint64_t Index64
Definition: Types.h:60
void writeBuffers(std::ostream &os, bool toHalf=false) const
Write buffers to a stream.
Definition: LeafNode.h:1457
void combine(const LeafNode &other, CombineOp &op)
Definition: LeafNode.h:1772
bool resultIsActive() const
Definition: Types.h:681
Index64 onLeafVoxelCount() const
Definition: LeafNode.h:169
CombineArgs & setBRef(const BValueType &b)
Redirect the B value to a new external source.
Definition: Types.h:672
ChildOnCIter endChildOn() const
Definition: LeafNode.h:353
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:614
Templated block class to hold specific data types and a fixed number of values determined by Log2Dim...
Definition: LeafNode.h:64
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:1738
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:589
const LeafNode * probeConstLeafAndCache(const Coord &, AccessorT &) const
Return a const pointer to this node.
Definition: LeafNode.h:777
NodeT * probeNodeAndCache(const Coord &, AccessorT &)
Return a pointer to this node.
Definition: LeafNode.h:762
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:1747
void addLeaf(LeafNode *)
This function exists only to enable template instantiation.
Definition: LeafNode.h:738
bool isEmpty() const
Return true if this node has no active voxels.
Definition: LeafNode.h:174
void setValueOn(Index offset)
Mark the voxel at the given offset as active but don&#39;t change its value.
Definition: LeafNode.h:442
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:448
void readCompressedValues(std::istream &is, ValueT *destBuf, Index destCount, const MaskT &valueMask, bool fromHalf)
Definition: Compression.h:492
static const Index LOG2DIM
Definition: LeafNode.h:75
LeafNode * probeLeaf(const Coord &)
Return a pointer to this node.
Definition: LeafNode.h:769
This struct collects both input and output arguments to "grid combiner" functors used with the tree::...
Definition: Types.h:617
const std::enable_if<!VecTraits< T >::IsVec, T >::type & min(const T &a, const T &b)
Definition: Composite.h:129
ValueOnIter endValueOn()
Definition: LeafNode.h:332
bool isOff(Index32 n) const
Return true if the nth bit is off.
Definition: NodeMasks.h:510
void setOff(Index32 n)
Set the nth bit off.
Definition: NodeMasks.h:459
bool isOn(Index32 n) const
Return true if the nth bit is on.
Definition: NodeMasks.h:504
~LeafNode()
Destructor.
Definition: LeafNode.h:1039
Index32 countOn() const
Return the total number of on bits.
Definition: NodeMasks.h:445
void modifyValueAndActiveState(const Coord &xyz, const ModifyOp &op)
Apply a functor to the voxel at the given coordinates.
Definition: LeafNode.h:479
#define OPENVDB_NO_UNREACHABLE_CODE_WARNING_END
Definition: Platform.h:150
ValueOnCIter beginValueOn() const
Definition: LeafNode.h:321
Index64 onVoxelCount() const
Return the number of voxels marked On.
Definition: LeafNode.h:166
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:458
ValueT & getItem(Index pos) const
Definition: LeafNode.h:247
CombineArgs & setARef(const AValueType &a)
Redirect the A value to a new external source.
Definition: Types.h:670
ValueAllCIter beginValueAll() const
Definition: LeafNode.h:327
Definition: LeafNode.h:233
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:1255
const NodeT * probeConstNodeAndCache(const Coord &, AccessorT &) const
Return a const pointer to this node.
Definition: LeafNode.h:782
bool operator==(const LeafNode &other) const
Check for buffer, state and origin equivalence.
Definition: LeafNode.h:1474
GridType::Ptr clip(const GridType &grid, const BBoxd &bbox, bool keepInterior=true)
Clip the given grid against a world-space bounding box and return a new grid containing the result...
Definition: Clip.h:379
ChildAllCIter cbeginChildAll() const
Definition: LeafNode.h:348
static Index getValueLevel(const Coord &)
Return the level (i.e., 0) at which leaf node values reside.
Definition: LeafNode.h:417
void setValueOff(const Coord &xyz)
Mark the voxel at the given coordinates as inactive but don&#39;t change its value.
Definition: LeafNode.h:430
bool operator==(const Vec3< T0 > &v0, const Vec3< T1 > &v1)
Equality operator, does exact floating point comparisons.
Definition: Vec3.h:498
void combine2(const LeafNode &other, const OtherType &, bool valueIsActive, CombineOp &)
Definition: LeafNode.h:1814
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:605
Definition: Compression.h:81
bool isValueMaskOn(Index n) const
Definition: LeafNode.h:892
OffIterator beginOff() const
Definition: NodeMasks.h:356
void fill(const ValueType &)
Populate this buffer with a constant value.
Definition: LeafBuffer.h:343
bool allocate()
Allocate memory for this node&#39;s buffer if it has not already been allocated.
Definition: LeafNode.h:182
Index memUsage() const
Return the memory footprint of this buffer in bytes.
Definition: LeafBuffer.h:382
Definition: Types.h:557
Buffer & buffer()
Definition: LeafNode.h:369
Index64 offVoxelCount() const
Return the number of voxels marked Off.
Definition: LeafNode.h:168
NodeMaskType & getValueMask()
Definition: LeafNode.h:897
static Index32 nonLeafCount()
Return the non-leaf count for this node, which is zero.
Definition: LeafNode.h:163
Definition: LeafNode.h:232
ValueOffCIter cendValueOff() const
Definition: LeafNode.h:333
static Index64 offTileCount()
Definition: LeafNode.h:172
static Index numValues()
Return the total number of voxels represented by this LeafNode.
Definition: LeafNode.h:153
void getOrigin(Coord &origin) const
Return the grid index coordinates of this node&#39;s local origin.
Definition: LeafNode.h:202
bool isChildMaskOff() const
Definition: LeafNode.h:902
void modifyItem(Index n, const ModifyOp &op) const
Definition: LeafNode.h:263
static void evalNodeOrigin(Coord &xyz)
Compute the origin of the leaf node that contains the voxel with the given coordinates.
Definition: LeafNode.h:911
LeafNode()
Default constructor.
Definition: LeafNode.h:953
ValueAllCIter cendValueAll() const
Definition: LeafNode.h:336
int32_t Int32
Definition: Types.h:63
static const Index SIZE
Definition: LeafNode.h:80
const ValueType & getValue(const Coord &xyz) const
Return the value of the voxel at the given coordinates.
Definition: LeafNode.h:1094
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:180
void load(std::istream &is)
Definition: NodeMasks.h:571
void modifyValue(const ModifyOp &op) const
Definition: LeafNode.h:266
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:450
void voxelizeActiveTiles(bool=true)
No-op.
Definition: LeafNode.h:660
void setValuesOff()
Mark all voxels as inactive but don&#39;t change their values.
Definition: LeafNode.h:495
static void doVisit2Node(NodeT &self, OtherNodeT &other, VisitorOp &)
Definition: LeafNode.h:1958
DenseIter()
Definition: LeafNode.h:286
static const Index NUM_VALUES
Definition: LeafNode.h:78
ValueOffIter endValueOff()
Definition: LeafNode.h:335
ChildOnCIter cbeginChildOn() const
Definition: LeafNode.h:342
ChildOffIter beginChildOff()
Definition: LeafNode.h:347
typename BaseT::NonConstValueType NonConstValueT
Definition: LeafNode.h:284
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:623
SharedPtr< MappedFile > Ptr
Definition: io.h:163
void set(Index32 n, bool On)
Set the nth bit to the specified state.
Definition: NodeMasks.h:464
Tag dispatch class that distinguishes constructors during file input.
Definition: Types.h:752
void save(std::ostream &os) const
Definition: NodeMasks.h:567
Definition: LeafNode.h:232
Definition: PointDataGrid.h:201
bool isValueOn(Index offset) const
Return true if the voxel at the given offset is active.
Definition: LeafNode.h:500
ValueType combine(const ValueType &v0, const ValueType &v1, const ValueType &v2, const openvdb::Vec3d &w)
Combine different value types.
Definition: AttributeTransferUtil.h:168
bool getItem(Index pos, void *&child, NonConstValueT &value) const
Definition: LeafNode.h:289
ValueOnIter beginValueOn()
Definition: LeafNode.h:322
ValueType medianAll(ValueType *tmp=nullptr) const
Computes the median value of all the active AND inactive voxels in this node.
Definition: LeafNode.h:1556
void resetBackground(const ValueType &oldBackground, const ValueType &newBackground)
Replace inactive occurrences of oldBackground with newBackground, and inactive occurrences of -oldBac...
Definition: LeafNode.h:1654
void stealNodes(ArrayT &, const ValueType &, bool)
This function exists only to enable template instantiation.
Definition: LeafNode.h:748
uint32_t Index32
Definition: Types.h:59
bool isConstant(ValueType &firstValue, bool &state, const ValueType &tolerance=zeroVal< ValueType >()) const
Definition: LeafNode.h:1520
bool allocate()
Allocate memory for this buffer if it has not already been allocated.
Definition: LeafBuffer.h:152
Definition: NodeMasks.h:272
void addTile(Index level, const Coord &, const ValueType &, bool)
Definition: LeafNode.h:1625
void readTopology(std::istream &is, bool fromHalf=false)
Read in just the topology.
Definition: LeafNode.h:1323
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:1284
void setValueMask(const NodeMaskType &mask)
Definition: LeafNode.h:899
void negate()
Definition: LeafNode.h:1755
void setValueMask(Index n, bool on)
Definition: LeafNode.h:904
typename std::remove_const< UnsetItemT >::type NonConstValueType
Definition: Iterator.h:211
ChildIter()
Definition: LeafNode.h:274
static Index coordToOffset(const Coord &xyz)
Return the linear table offset of the given global or local coordinates.
Definition: LeafNode.h:1059