4#ifndef OPENVDB_TREE_LEAF_NODE_MASK_HAS_BEEN_INCLUDED
5#define OPENVDB_TREE_LEAF_NODE_MASK_HAS_BEEN_INCLUDED
7#include <openvdb/version.h>
29template<Index Log2Dim>
30class LeafNode<ValueMask, Log2Dim>
33 using LeafNodeType = LeafNode<ValueMask, Log2Dim>;
34 using BuildType = ValueMask;
35 using ValueType = bool;
36 using Buffer = LeafBuffer<ValueType, Log2Dim>;
37 using NodeMaskType = util::NodeMask<Log2Dim>;
38 using Ptr = SharedPtr<LeafNodeType>;
41 static const Index LOG2DIM = Log2Dim;
42 static const Index TOTAL = Log2Dim;
43 static const Index DIM = 1 << TOTAL;
44 static const Index NUM_VALUES = 1 << 3 * Log2Dim;
45 static const Index NUM_VOXELS = NUM_VALUES;
46 static const Index SIZE = NUM_VALUES;
47 static const Index LEVEL = 0;
51 template<
typename OtherValueType>
56 template<
typename OtherNodeType>
68 explicit LeafNode(
const Coord& xyz,
bool value =
false,
bool dummy =
false);
74 LeafNode(
const LeafNode&);
77 template<
typename OtherValueType>
78 explicit LeafNode(
const LeafNode<OtherValueType, Log2Dim>& other);
81 template<
typename ValueType>
82 LeafNode(
const LeafNode<ValueType, Log2Dim>& other,
TopologyCopy);
87 template<
typename ValueType>
88 LeafNode(
const LeafNode<ValueType, Log2Dim>& other,
bool offValue,
bool onValue,
TopologyCopy);
89 template<
typename ValueType>
90 LeafNode(
const LeafNode<ValueType, Log2Dim>& other,
bool background,
TopologyCopy);
100 static Index log2dim() {
return Log2Dim; }
102 static Index dim() {
return DIM; }
104 static Index size() {
return SIZE; }
106 static Index numValues() {
return SIZE; }
108 static Index getLevel() {
return LEVEL; }
110 static void getNodeLog2Dims(std::vector<Index>& dims) { dims.push_back(Log2Dim); }
112 static Index getChildDim() {
return 1; }
114 static Index32 leafCount() {
return 1; }
116 void nodeCount(std::vector<Index32> &)
const {}
118 static Index32 nonLeafCount() {
return 0; }
121 Index64 onVoxelCount()
const {
return mBuffer.mData.countOn(); }
123 Index64 offVoxelCount()
const {
return mBuffer.mData.countOff(); }
124 Index64 onLeafVoxelCount()
const {
return this->onVoxelCount(); }
125 Index64 offLeafVoxelCount()
const {
return this->offVoxelCount(); }
126 static Index64 onTileCount() {
return 0; }
127 static Index64 offTileCount() {
return 0; }
130 bool isEmpty()
const {
return mBuffer.mData.isOff(); }
132 bool isDense()
const {
return mBuffer.mData.isOn(); }
136 bool isAllocated()
const {
return true; }
149 void evalActiveBoundingBox(CoordBBox& bbox,
bool visitVoxels =
true)
const;
153 CoordBBox getNodeBoundingBox()
const {
return CoordBBox::createCube(mOrigin, DIM); }
156 void setOrigin(
const Coord& origin) { mOrigin = origin; }
159 const Coord& origin()
const {
return mOrigin; }
160 void getOrigin(Coord& origin)
const { origin = mOrigin; }
161 void getOrigin(Int32& x, Int32& y, Int32& z)
const { mOrigin.asXYZ(x, y, z); }
165 static Index coordToOffset(
const Coord& xyz);
168 static Coord offsetToLocalCoord(Index n);
170 Coord offsetToGlobalCoord(Index n)
const;
173 Index32 transientData()
const {
return mTransientData; }
175 void setTransientData(Index32 transientData) { mTransientData = transientData; }
178 std::string str()
const;
182 template<
typename OtherType, Index OtherLog2Dim>
183 bool hasSameTopology(
const LeafNode<OtherType, OtherLog2Dim>* other)
const;
194 void swap(Buffer& other) { mBuffer.swap(other); }
195 const Buffer& buffer()
const {
return mBuffer; }
196 Buffer& buffer() {
return mBuffer; }
202 void readTopology(std::istream&,
bool fromHalf =
false);
204 void writeTopology(std::ostream&,
bool toHalf =
false)
const;
207 void readBuffers(std::istream&,
bool fromHalf =
false);
208 void readBuffers(std::istream& is,
const CoordBBox&,
bool fromHalf =
false);
210 void writeBuffers(std::ostream&,
bool toHalf =
false)
const;
216 const bool& getValue(
const Coord& xyz)
const;
218 const bool& getValue(Index offset)
const;
223 bool probeValue(
const Coord& xyz,
bool& val)
const;
226 static Index getValueLevel(
const Coord&) {
return LEVEL; }
229 void setActiveState(
const Coord& xyz,
bool on);
231 void setActiveState(Index offset,
bool on) { assert(offset<SIZE); mBuffer.mData.set(offset, on); }
234 void setValueOnly(
const Coord& xyz,
bool val);
236 void setValueOnly(Index offset,
bool val) { assert(offset<SIZE); mBuffer.setValue(offset,val); }
239 void setValueOff(
const Coord& xyz) { mBuffer.mData.setOff(this->coordToOffset(xyz)); }
241 void setValueOff(Index offset) { assert(offset < SIZE); mBuffer.mData.setOff(offset); }
244 void setValueOff(
const Coord& xyz,
bool val);
246 void setValueOff(Index offset,
bool val);
249 void setValueOn(
const Coord& xyz) { mBuffer.mData.setOn(this->coordToOffset(xyz)); }
251 void setValueOn(Index offset) { assert(offset < SIZE); mBuffer.mData.setOn(offset); }
254 void setValueOn(
const Coord& xyz,
bool val);
256 void setValue(
const Coord& xyz,
bool val) { this->setValueOn(xyz, val); }
258 void setValueOn(Index offset,
bool val);
262 template<
typename ModifyOp>
263 void modifyValue(Index offset,
const ModifyOp& op);
266 template<
typename ModifyOp>
267 void modifyValue(
const Coord& xyz,
const ModifyOp& op);
270 template<
typename ModifyOp>
271 void modifyValueAndActiveState(
const Coord& xyz,
const ModifyOp& op);
274 void setValuesOn() { mBuffer.mData.setOn(); }
276 void setValuesOff() { mBuffer.mData.setOff(); }
279 bool isValueOn(
const Coord& xyz)
const {
return mBuffer.mData.isOn(this->coordToOffset(xyz)); }
281 bool isValueOn(Index offset)
const { assert(offset < SIZE);
return mBuffer.mData.isOn(offset); }
284 static bool hasActiveTiles() {
return false; }
287 void clip(
const CoordBBox&,
bool background);
290 void fill(
const CoordBBox& bbox,
bool value,
bool =
false);
292 void denseFill(
const CoordBBox& bbox,
bool value,
bool =
false) { this->fill(bbox, value); }
295 void fill(
const bool& value,
bool dummy =
false);
308 template<
typename DenseT>
309 void copyToDense(
const CoordBBox& bbox, DenseT& dense)
const;
327 template<
typename DenseT>
328 void copyFromDense(
const CoordBBox& bbox,
const DenseT& dense,
bool background,
bool tolerance);
332 template<
typename AccessorT>
333 const bool& getValueAndCache(
const Coord& xyz, AccessorT&)
const {
return this->getValue(xyz);}
337 template<
typename AccessorT>
338 bool isValueOnAndCache(
const Coord& xyz, AccessorT&)
const {
return this->isValueOn(xyz); }
342 template<
typename AccessorT>
343 void setValueAndCache(
const Coord& xyz,
bool val, AccessorT&) { this->setValueOn(xyz, val); }
348 template<
typename AccessorT>
349 void setValueOnlyAndCache(
const Coord& xyz,
bool val, AccessorT&) {this->setValueOnly(xyz,val);}
353 template<
typename AccessorT>
354 void setValueOffAndCache(
const Coord& xyz,
bool value, AccessorT&)
356 this->setValueOff(xyz, value);
362 template<
typename ModifyOp,
typename AccessorT>
363 void modifyValueAndCache(
const Coord& xyz,
const ModifyOp& op, AccessorT&)
365 this->modifyValue(xyz, op);
370 template<
typename ModifyOp,
typename AccessorT>
371 void modifyValueAndActiveStateAndCache(
const Coord& xyz,
const ModifyOp& op, AccessorT&)
373 this->modifyValueAndActiveState(xyz, op);
379 template<
typename AccessorT>
380 void setActiveStateAndCache(
const Coord& xyz,
bool on, AccessorT&)
382 this->setActiveState(xyz, on);
388 template<
typename AccessorT>
389 bool probeValueAndCache(
const Coord& xyz,
bool& val, AccessorT&)
const
391 return this->probeValue(xyz, val);
396 template<
typename AccessorT>
397 static Index getValueLevelAndCache(
const Coord&, AccessorT&) {
return LEVEL; }
402 const bool& getFirstValue()
const {
if (mBuffer.mData.isOn(0))
return Buffer::sOn;
else return Buffer::sOff; }
406 const bool& getLastValue()
const {
if (mBuffer.mData.isOn(SIZE-1))
return Buffer::sOn;
else return Buffer::sOff; }
411 bool isConstant(
bool& constValue,
bool& state,
bool tolerance = 0)
const;
418 bool medianAll()
const;
427 Index medianOn(ValueType &value)
const;
437 Index medianOff(ValueType &value)
const;
440 bool isInactive()
const {
return mBuffer.mData.isOff(); }
444 void resetBackground(
bool,
bool) {}
447 void negate() { mBuffer.mData.toggle(); }
449 template<MergePolicy Policy>
450 void merge(
const LeafNode& other,
bool bg =
false,
bool otherBG =
false);
451 template<MergePolicy Policy>
void merge(
bool tileValue,
bool tileActive=
false);
455 void voxelizeActiveTiles(
bool =
true) {}
463 template<
typename OtherType>
464 void topologyUnion(
const LeafNode<OtherType, Log2Dim>& other,
const bool preserveTiles =
false);
477 template<
typename OtherType>
478 void topologyIntersection(
const LeafNode<OtherType, Log2Dim>& other,
const bool&);
491 template<
typename OtherType>
492 void topologyDifference(
const LeafNode<OtherType, Log2Dim>& other,
const bool&);
494 template<
typename CombineOp>
495 void combine(
const LeafNode& other, CombineOp& op);
496 template<
typename CombineOp>
497 void combine(
bool,
bool valueIsActive, CombineOp& op);
499 template<
typename CombineOp,
typename OtherType >
500 void combine2(
const LeafNode& other,
const OtherType&,
bool valueIsActive, CombineOp&);
501 template<
typename CombineOp,
typename OtherNodeT >
502 void combine2(
bool,
const OtherNodeT& other,
bool valueIsActive, CombineOp&);
503 template<
typename CombineOp,
typename OtherNodeT >
504 void combine2(
const LeafNode& b0,
const OtherNodeT& b1, CombineOp&);
508 void prune(
const ValueType& = zeroVal<ValueType>()) {}
509 void addLeaf(LeafNode*) {}
510 template<
typename AccessorT>
511 void addLeafAndCache(LeafNode*, AccessorT&) {}
512 template<
typename NodeT>
513 NodeT* stealNode(
const Coord&,
const ValueType&,
bool) {
return nullptr; }
514 template<
typename NodeT>
515 NodeT* probeNode(
const Coord&) {
return nullptr; }
516 template<
typename NodeT>
517 const NodeT* probeConstNode(
const Coord&)
const {
return nullptr; }
518 template<
typename ArrayT>
void getNodes(ArrayT&)
const {}
519 template<
typename ArrayT>
void stealNodes(ArrayT&,
const ValueType&,
bool) {}
522 void addTile(Index level,
const Coord&,
bool val,
bool active);
523 void addTile(Index offset,
bool val,
bool active);
524 template<
typename AccessorT>
525 void addTileAndCache(Index level,
const Coord&,
bool val,
bool active, AccessorT&);
529 LeafNode* touchLeaf(
const Coord&) {
return this; }
530 template<
typename AccessorT>
531 LeafNode* touchLeafAndCache(
const Coord&, AccessorT&) {
return this; }
532 LeafNode* probeLeaf(
const Coord&) {
return this; }
533 template<
typename AccessorT>
534 LeafNode* probeLeafAndCache(
const Coord&, AccessorT&) {
return this; }
535 template<
typename NodeT,
typename AccessorT>
536 NodeT* probeNodeAndCache(
const Coord&, AccessorT&)
539 if (!(std::is_same<NodeT, LeafNode>::value))
return nullptr;
540 return reinterpret_cast<NodeT*
>(
this);
546 const LeafNode* probeLeaf(
const Coord&)
const {
return this; }
547 template<
typename AccessorT>
548 const LeafNode* probeLeafAndCache(
const Coord&, AccessorT&)
const {
return this; }
549 const LeafNode* probeConstLeaf(
const Coord&)
const {
return this; }
550 template<
typename AccessorT>
551 const LeafNode* probeConstLeafAndCache(
const Coord&, AccessorT&)
const {
return this; }
552 template<
typename NodeT,
typename AccessorT>
553 const NodeT* probeConstNodeAndCache(
const Coord&, AccessorT&)
const
556 if (!(std::is_same<NodeT, LeafNode>::value))
return nullptr;
557 return reinterpret_cast<const NodeT*
>(
this);
566 using MaskOnIter =
typename NodeMaskType::OnIterator;
567 using MaskOffIter =
typename NodeMaskType::OffIterator;
568 using MaskDenseIter =
typename NodeMaskType::DenseIterator;
570 template<
typename MaskIterT,
typename NodeT,
typename ValueT>
574 public SparseIteratorBase<MaskIterT, ValueIter<MaskIterT, NodeT, ValueT>, NodeT, ValueT>
590 template<
typename ModifyOp>
593 template<
typename ModifyOp>
598 template<
typename MaskIterT,
typename NodeT>
607 template<
typename NodeT,
typename ValueT>
609 MaskDenseIter, DenseIter<NodeT, ValueT>, NodeT, void, ValueT>
632 using ValueOnIter = ValueIter<MaskOnIter, LeafNode, const bool>;
633 using ValueOnCIter = ValueIter<MaskOnIter, const LeafNode, const bool>;
634 using ValueOffIter = ValueIter<MaskOffIter, LeafNode, const bool>;
635 using ValueOffCIter = ValueIter<MaskOffIter, const LeafNode, const bool>;
636 using ValueAllIter = ValueIter<MaskDenseIter, LeafNode, const bool>;
637 using ValueAllCIter = ValueIter<MaskDenseIter, const LeafNode, const bool>;
638 using ChildOnIter = ChildIter<MaskOnIter, LeafNode>;
639 using ChildOnCIter = ChildIter<MaskOnIter, const LeafNode>;
640 using ChildOffIter = ChildIter<MaskOffIter, LeafNode>;
641 using ChildOffCIter = ChildIter<MaskOffIter, const LeafNode>;
642 using ChildAllIter = DenseIter<LeafNode, bool>;
643 using ChildAllCIter = DenseIter<const LeafNode, const bool>;
645 ValueOnCIter cbeginValueOn()
const {
return ValueOnCIter(mBuffer.mData.beginOn(),
this); }
646 ValueOnCIter beginValueOn()
const {
return ValueOnCIter(mBuffer.mData.beginOn(),
this); }
647 ValueOnIter beginValueOn() {
return ValueOnIter(mBuffer.mData.beginOn(),
this); }
648 ValueOffCIter cbeginValueOff()
const {
return ValueOffCIter(mBuffer.mData.beginOff(),
this); }
649 ValueOffCIter beginValueOff()
const {
return ValueOffCIter(mBuffer.mData.beginOff(),
this); }
650 ValueOffIter beginValueOff() {
return ValueOffIter(mBuffer.mData.beginOff(),
this); }
651 ValueAllCIter cbeginValueAll()
const {
return ValueAllCIter(mBuffer.mData.beginDense(),
this); }
652 ValueAllCIter beginValueAll()
const {
return ValueAllCIter(mBuffer.mData.beginDense(),
this); }
653 ValueAllIter beginValueAll() {
return ValueAllIter(mBuffer.mData.beginDense(),
this); }
655 ValueOnCIter cendValueOn()
const {
return ValueOnCIter(mBuffer.mData.endOn(),
this); }
656 ValueOnCIter endValueOn()
const {
return ValueOnCIter(mBuffer.mData.endOn(),
this); }
657 ValueOnIter endValueOn() {
return ValueOnIter(mBuffer.mData.endOn(),
this); }
658 ValueOffCIter cendValueOff()
const {
return ValueOffCIter(mBuffer.mData.endOff(),
this); }
659 ValueOffCIter endValueOff()
const {
return ValueOffCIter(mBuffer.mData.endOff(),
this); }
660 ValueOffIter endValueOff() {
return ValueOffIter(mBuffer.mData.endOff(),
this); }
661 ValueAllCIter cendValueAll()
const {
return ValueAllCIter(mBuffer.mData.endDense(),
this); }
662 ValueAllCIter endValueAll()
const {
return ValueAllCIter(mBuffer.mData.endDense(),
this); }
663 ValueAllIter endValueAll() {
return ValueAllIter(mBuffer.mData.endDense(),
this); }
667 ChildOnCIter cbeginChildOn()
const {
return ChildOnCIter(mBuffer.mData.endOn(),
this); }
668 ChildOnCIter beginChildOn()
const {
return ChildOnCIter(mBuffer.mData.endOn(),
this); }
669 ChildOnIter beginChildOn() {
return ChildOnIter(mBuffer.mData.endOn(),
this); }
670 ChildOffCIter cbeginChildOff()
const {
return ChildOffCIter(mBuffer.mData.endOff(),
this); }
671 ChildOffCIter beginChildOff()
const {
return ChildOffCIter(mBuffer.mData.endOff(),
this); }
672 ChildOffIter beginChildOff() {
return ChildOffIter(mBuffer.mData.endOff(),
this); }
673 ChildAllCIter cbeginChildAll()
const {
return ChildAllCIter(mBuffer.mData.beginDense(),
this); }
674 ChildAllCIter beginChildAll()
const {
return ChildAllCIter(mBuffer.mData.beginDense(),
this); }
675 ChildAllIter beginChildAll() {
return ChildAllIter(mBuffer.mData.beginDense(),
this); }
677 ChildOnCIter cendChildOn()
const {
return ChildOnCIter(mBuffer.mData.endOn(),
this); }
678 ChildOnCIter endChildOn()
const {
return ChildOnCIter(mBuffer.mData.endOn(),
this); }
679 ChildOnIter endChildOn() {
return ChildOnIter(mBuffer.mData.endOn(),
this); }
680 ChildOffCIter cendChildOff()
const {
return ChildOffCIter(mBuffer.mData.endOff(),
this); }
681 ChildOffCIter endChildOff()
const {
return ChildOffCIter(mBuffer.mData.endOff(),
this); }
682 ChildOffIter endChildOff() {
return ChildOffIter(mBuffer.mData.endOff(),
this); }
683 ChildAllCIter cendChildAll()
const {
return ChildAllCIter(mBuffer.mData.endDense(),
this); }
684 ChildAllCIter endChildAll()
const {
return ChildAllCIter(mBuffer.mData.endDense(),
this); }
685 ChildAllIter endChildAll() {
return ChildAllIter(mBuffer.mData.endDense(),
this); }
690 bool isValueMaskOn(Index n)
const {
return mBuffer.mData.isOn(n); }
691 bool isValueMaskOn()
const {
return mBuffer.mData.isOn(); }
692 bool isValueMaskOff(Index n)
const {
return mBuffer.mData.isOff(n); }
693 bool isValueMaskOff()
const {
return mBuffer.mData.isOff(); }
694 const NodeMaskType& getValueMask()
const {
return mBuffer.mData; }
695 const NodeMaskType& valueMask()
const {
return mBuffer.mData; }
696 NodeMaskType& getValueMask() {
return mBuffer.mData; }
697 void setValueMask(
const NodeMaskType& mask) { mBuffer.mData = mask; }
698 bool isChildMaskOn(Index)
const {
return false; }
699 bool isChildMaskOff(Index)
const {
return true; }
700 bool isChildMaskOff()
const {
return true; }
702 void setValueMask(Index n,
bool on) { mBuffer.mData.set(n, on); }
703 void setValueMaskOn(Index n) { mBuffer.mData.setOn(n); }
704 void setValueMaskOff(Index n) { mBuffer.mData.setOff(n); }
707 static void evalNodeOrigin(Coord& xyz) { xyz &= ~(DIM - 1); }
719 template<
typename, Index>
friend class LeafNode;
721 friend struct ValueIter<MaskOnIter, LeafNode, bool>;
722 friend struct ValueIter<MaskOffIter, LeafNode, bool>;
723 friend struct ValueIter<MaskDenseIter, LeafNode, bool>;
724 friend struct ValueIter<MaskOnIter, const LeafNode, bool>;
725 friend struct ValueIter<MaskOffIter, const LeafNode, bool>;
726 friend struct ValueIter<MaskDenseIter, const LeafNode, bool>;
731 friend class IteratorBase<MaskOnIter, LeafNode>;
732 friend class IteratorBase<MaskOffIter, LeafNode>;
733 friend class IteratorBase<MaskDenseIter, LeafNode>;
736 template<
typename, Index>
friend class LeafBuffer;
744template<Index Log2Dim>
746LeafNode<ValueMask, Log2Dim>::LeafNode()
751template<Index Log2Dim>
754 : mBuffer(value || active)
755 , mOrigin(xyz & (~(DIM - 1)))
760template<Index Log2Dim>
763 : mBuffer(value || active)
764 , mOrigin(xyz & (~(DIM - 1)))
769template<Index Log2Dim>
772 : mBuffer(other.mBuffer)
773 , mOrigin(other.mOrigin)
774 , mTransientData(other.mTransientData)
780template<Index Log2Dim>
781template<
typename ValueT>
784 : mBuffer(other.valueMask())
785 , mOrigin(other.origin())
786 , mTransientData(other.mTransientData)
791template<Index Log2Dim>
792template<
typename ValueT>
796 : mBuffer(other.valueMask())
797 , mOrigin(other.origin())
798 , mTransientData(other.mTransientData)
803template<Index Log2Dim>
804template<
typename ValueT>
807 : mBuffer(other.valueMask())
808 , mOrigin(other.origin())
809 , mTransientData(other.mTransientData)
814template<Index Log2Dim>
815template<
typename ValueT>
818 bool offValue,
bool onValue, TopologyCopy)
819 : mBuffer(other.valueMask())
820 , mOrigin(other.origin())
821 , mTransientData(other.mTransientData)
823 if (offValue==
true) {
824 if (onValue==
false) {
825 mBuffer.mData.toggle();
827 mBuffer.mData.setOn();
833template<Index Log2Dim>
843template<Index Log2Dim>
848 return sizeof(*this);
852template<Index Log2Dim>
857 return sizeof(*this);
861template<Index Log2Dim>
865 CoordBBox this_bbox = this->getNodeBoundingBox();
866 if (bbox.isInside(this_bbox))
return;
867 if (ValueOnCIter
iter = this->cbeginValueOn()) {
870 for(;
iter; ++
iter) this_bbox.expand(this->offsetToLocalCoord(
iter.pos()));
871 this_bbox.translate(this->origin());
873 bbox.expand(this_bbox);
878template<Index Log2Dim>
879template<
typename OtherType, Index OtherLog2Dim>
884 return (Log2Dim == OtherLog2Dim && mBuffer.mData == other->getValueMask());
888template<Index Log2Dim>
892 std::ostringstream ostr;
893 ostr <<
"LeafNode @" << mOrigin <<
": ";
894 for (
Index32 n = 0; n < SIZE; ++n) ostr << (mBuffer.mData.isOn(n) ?
'#' :
'.');
902template<Index Log2Dim>
906 assert ((xyz[0] & (DIM-1u)) < DIM && (xyz[1] & (DIM-1u)) < DIM && (xyz[2] & (DIM-1u)) < DIM);
907 return ((xyz[0] & (DIM-1u)) << 2*Log2Dim)
908 + ((xyz[1] & (DIM-1u)) << Log2Dim)
909 + (xyz[2] & (DIM-1u));
913template<Index Log2Dim>
917 assert(n < (1 << 3*Log2Dim));
919 xyz.setX(n >> 2*Log2Dim);
920 n &= ((1 << 2*Log2Dim) - 1);
921 xyz.setY(n >> Log2Dim);
922 xyz.setZ(n & ((1 << Log2Dim) - 1));
927template<Index Log2Dim>
931 return (this->offsetToLocalCoord(n) + this->origin());
938template<Index Log2Dim>
942 mBuffer.mData.load(is);
946template<Index Log2Dim>
950 mBuffer.mData.save(os);
954template<Index Log2Dim>
961 this->readBuffers(is, fromHalf);
964 bool background =
false;
966 background = *
static_cast<const bool*
>(bgPtr);
968 this->
clip(clipBBox, background);
972template<Index Log2Dim>
977 mBuffer.mData.load(is);
983template<Index Log2Dim>
988 mBuffer.mData.save(os);
990 os.write(
reinterpret_cast<const char*
>(&mOrigin),
sizeof(
Coord::ValueType) * 3);
997template<Index Log2Dim>
1001 return mOrigin == other.mOrigin && mBuffer == other.mBuffer;
1005template<Index Log2Dim>
1016template<Index Log2Dim>
1020 if (!mBuffer.mData.isConstant(state))
return false;
1029template<Index Log2Dim>
1033 const Index countTrue = mBuffer.mData.countOn();
1034 return countTrue > (NUM_VALUES >> 1);
1037template<Index Log2Dim>
1041 const Index countTrueOn = mBuffer.mData.countOn();
1046template<Index Log2Dim>
1050 const Index countFalseOff = mBuffer.mData.countOff();
1052 return countFalseOff;
1059template<Index Log2Dim>
1063 this->addTile(this->coordToOffset(xyz), val, active);
1066template<Index Log2Dim>
1070 assert(offset < SIZE);
1071 this->setValueOnly(offset, val);
1072 this->setActiveState(offset, active);
1075template<Index Log2Dim>
1076template<
typename AccessorT>
1079 bool val,
bool active, AccessorT&)
1081 this->addTile(level, xyz, val, active);
1088template<Index Log2Dim>
1093 if (mBuffer.mData.isOn(this->coordToOffset(xyz)))
return Buffer::sOn;
else return Buffer::sOff;
1097template<Index Log2Dim>
1101 assert(offset < SIZE);
1103 if (mBuffer.mData.isOn(offset))
return Buffer::sOn;
else return Buffer::sOff;
1107template<Index Log2Dim>
1111 const Index offset = this->coordToOffset(xyz);
1112 val = mBuffer.mData.isOn(offset);
1117template<Index Log2Dim>
1121 this->setValueOn(this->coordToOffset(xyz), val);
1125template<Index Log2Dim>
1129 assert(offset < SIZE);
1130 mBuffer.mData.set(offset, val);
1134template<Index Log2Dim>
1138 this->setValueOnly(this->coordToOffset(xyz), val);
1142template<Index Log2Dim>
1146 mBuffer.mData.set(this->coordToOffset(xyz), on);
1150template<Index Log2Dim>
1154 this->setValueOff(this->coordToOffset(xyz), val);
1158template<Index Log2Dim>
1162 assert(offset < SIZE);
1163 mBuffer.mData.set(offset, val);
1167template<Index Log2Dim>
1168template<
typename ModifyOp>
1172 bool val = mBuffer.mData.isOn(offset);
1174 mBuffer.mData.set(offset, val);
1178template<Index Log2Dim>
1179template<
typename ModifyOp>
1183 this->modifyValue(this->coordToOffset(xyz), op);
1187template<Index Log2Dim>
1188template<
typename ModifyOp>
1192 const Index offset = this->coordToOffset(xyz);
1193 bool val = mBuffer.mData.isOn(offset), state = val;
1195 mBuffer.mData.set(offset, val);
1202template<Index Log2Dim>
1203template<MergePolicy Policy>
1209 mBuffer.mData |= other.mBuffer.mData;
1213template<Index Log2Dim>
1214template<MergePolicy Policy>
1220 if (tileValue) mBuffer.mData.setOn();
1228template<Index Log2Dim>
1229template<
typename OtherType>
1233 mBuffer.mData |= other.valueMask();
1237template<Index Log2Dim>
1238template<
typename OtherType>
1243 mBuffer.mData &= other.valueMask();
1247template<Index Log2Dim>
1248template<
typename OtherType>
1253 mBuffer.mData &= !other.valueMask();
1260template<Index Log2Dim>
1264 CoordBBox nodeBBox = this->getNodeBoundingBox();
1265 if (!clipBBox.hasOverlap(nodeBBox)) {
1267 this->fill(nodeBBox, background,
false);
1268 }
else if (clipBBox.isInside(nodeBBox)) {
1278 nodeBBox.intersect(clipBBox);
1280 int &x = xyz.x(), &y = xyz.y(), &z = xyz.z();
1281 for (x = nodeBBox.min().x(); x <= nodeBBox.max().x(); ++x) {
1282 for (y = nodeBBox.min().y(); y <= nodeBBox.max().y(); ++y) {
1283 for (z = nodeBBox.min().z(); z <= nodeBBox.max().z(); ++z) {
1284 mask.setOn(
static_cast<Index32>(this->coordToOffset(xyz)));
1291 for (MaskOffIter maskIter = mask.beginOff(); maskIter; ++maskIter) {
1292 this->setValueOff(maskIter.pos(), background);
1300template<Index Log2Dim>
1304 auto clippedBBox = this->getNodeBoundingBox();
1305 clippedBBox.intersect(bbox);
1306 if (!clippedBBox)
return;
1308 for (
Int32 x = clippedBBox.min().x(); x <= clippedBBox.max().x(); ++x) {
1309 const Index offsetX = (x & (DIM-1u))<<2*Log2Dim;
1310 for (
Int32 y = clippedBBox.min().y(); y <= clippedBBox.max().y(); ++y) {
1311 const Index offsetXY = offsetX + ((y & (DIM-1u))<< Log2Dim);
1312 for (
Int32 z = clippedBBox.min().z(); z <= clippedBBox.max().z(); ++z) {
1313 const Index offset = offsetXY + (z & (DIM-1u));
1314 mBuffer.mData.set(offset, value);
1320template<Index Log2Dim>
1324 mBuffer.fill(value);
1331template<Index Log2Dim>
1332template<
typename DenseT>
1336 using DenseValueType =
typename DenseT::ValueType;
1338 const size_t xStride = dense.xStride(), yStride = dense.yStride(), zStride = dense.zStride();
1339 const Coord&
min = dense.bbox().min();
1340 DenseValueType* t0 = dense.data() + zStride * (bbox.min()[2] - min[2]);
1341 const Int32 n0 = bbox.min()[2] & (DIM-1u);
1342 for (
Int32 x = bbox.min()[0], ex = bbox.max()[0] + 1; x < ex; ++x) {
1343 DenseValueType* t1 = t0 + xStride * (x -
min[0]);
1344 const Int32 n1 = n0 + ((x & (DIM-1u)) << 2*LOG2DIM);
1345 for (
Int32 y = bbox.min()[1], ey = bbox.max()[1] + 1; y < ey; ++y) {
1346 DenseValueType* t2 = t1 + yStride * (y -
min[1]);
1347 Int32 n2 = n1 + ((y & (DIM-1u)) << LOG2DIM);
1348 for (
Int32 z = bbox.min()[2], ez = bbox.max()[2] + 1; z < ez; ++z, t2 += zStride) {
1349 *t2 = DenseValueType(mBuffer.mData.isOn(n2++));
1356template<Index Log2Dim>
1357template<
typename DenseT>
1360 bool background,
bool tolerance)
1362 using DenseValueType =
typename DenseT::ValueType;
1364 inline static bool toBool(
const DenseValueType& v) {
return !math::isZero(v); }
1367 const size_t xStride = dense.xStride(), yStride = dense.yStride(), zStride = dense.zStride();
1368 const Coord&
min = dense.bbox().min();
1369 const DenseValueType* s0 = dense.data() + zStride * (bbox.min()[2] - min[2]);
1370 const Int32 n0 = bbox.min()[2] & (DIM-1u);
1371 for (
Int32 x = bbox.min()[0], ex = bbox.max()[0] + 1; x < ex; ++x) {
1372 const DenseValueType* s1 = s0 + xStride * (x -
min[0]);
1373 const Int32 n1 = n0 + ((x & (DIM-1u)) << 2*LOG2DIM);
1374 for (
Int32 y = bbox.min()[1], ey = bbox.max()[1] + 1; y < ey; ++y) {
1375 const DenseValueType* s2 = s1 + yStride * (y -
min[1]);
1376 Int32 n2 = n1 + ((y & (DIM-1u)) << LOG2DIM);
1377 for (
Int32 z = bbox.min()[2], ez = bbox.max()[2]+1; z < ez; ++z, ++n2, s2 += zStride) {
1379 if (tolerance || (background == Local::toBool(*s2))) {
1380 mBuffer.mData.set(n2, background);
1382 mBuffer.mData.set(n2, Local::toBool(*s2));
1393template<Index Log2Dim>
1394template<
typename CombineOp>
1398 CombineArgs<bool> args;
1399 for (
Index i = 0; i < SIZE; ++i) {
1400 bool result =
false, aVal = mBuffer.mData.isOn(i), bVal = other.mBuffer.mData.isOn(i);
1401 op(args.setARef(aVal)
1405 .setResultRef(result));
1406 mBuffer.mData.set(i, result);
1411template<Index Log2Dim>
1412template<
typename CombineOp>
1416 CombineArgs<bool> args;
1417 args.setBRef(value).setBIsActive(valueIsActive);
1418 for (
Index i = 0; i < SIZE; ++i) {
1419 bool result =
false, aVal = mBuffer.mData.isOn(i);
1420 op(args.setARef(aVal)
1422 .setResultRef(result));
1423 mBuffer.mData.set(i, result);
1431template<Index Log2Dim>
1432template<
typename CombineOp,
typename OtherType>
1435 bool valueIsActive, CombineOp& op)
1437 CombineArgs<bool, OtherType> args;
1438 args.setBRef(value).setBIsActive(valueIsActive);
1439 for (
Index i = 0; i < SIZE; ++i) {
1440 bool result =
false, aVal = other.mBuffer.mData.isOn(i);
1441 op(args.setARef(aVal)
1443 .setResultRef(result));
1444 mBuffer.mData.set(i, result);
1449template<Index Log2Dim>
1450template<
typename CombineOp,
typename OtherNodeT>
1453 bool valueIsActive, CombineOp& op)
1455 CombineArgs<bool, typename OtherNodeT::ValueType> args;
1456 args.setARef(value).setAIsActive(valueIsActive);
1457 for (
Index i = 0; i < SIZE; ++i) {
1458 bool result =
false, bVal = other.mBuffer.mData.isOn(i);
1459 op(args.setBRef(bVal)
1461 .setResultRef(result));
1462 mBuffer.mData.set(i, result);
1467template<Index Log2Dim>
1468template<
typename CombineOp,
typename OtherNodeT>
1472 CombineArgs<bool, typename OtherNodeT::ValueType> args;
1473 for (
Index i = 0; i < SIZE; ++i) {
1474 bool result =
false, b0Val = b0.mBuffer.mData.isOn(i), b1Val = b1.mBuffer.mData.isOn(i);
1475 op(args.setARef(b0Val)
1476 .setAIsActive(b0Val)
1478 .setBIsActive(b1Val)
1479 .setResultRef(result));
1480 mBuffer.mData.set(i, result);
General-purpose arithmetic and comparison routines, most of which accept arbitrary value types (or at...
Int32 ValueType
Definition Coord.h:32
Tag dispatch class that distinguishes constructors during file input.
Definition Types.h:689
Tag dispatch class that distinguishes topology copy constructors from deep copy constructors.
Definition Types.h:683
Signed (x, y, z) 32-bit integer coordinates.
Definition Coord.h:25
Index pos() const
Definition Iterator.h:60
LeafNode & parent() const
Definition Iterator.h:50
Templated block class to hold specific data types and a fixed number of values determined by Log2Dim....
Definition LeafNode.h:38
static Coord offsetToLocalCoord(Index n)
Return the local coordinates for a linear table offset, where offset 0 has coordinates (0,...
Definition LeafNode.h:1020
void writeTopology(std::ostream &os, bool toHalf=false) const
Write out just the topology.
Definition LeafNode.h:1276
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:1204
bool operator!=(const LeafNode &other) const
Definition LeafNode.h:201
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:1231
void setValueOnly(const Coord &xyz, const ValueType &val)
Set the value of the voxel at the given coordinates but don't change its active state.
Definition LeafNode.h:1103
void topologyDifference(const LeafNode< OtherType, Log2Dim > &other, const ValueType &)
Difference this node's set of active values with the active values of the other node,...
Definition LeafNode.h:1696
Index medianOff(ValueType &value, ValueType *tmp=nullptr) const
Computes the median value of all the inactive voxels in this node.
Definition LeafNode.h:1553
~LeafNode()
Destructor.
Definition LeafNode.h:990
void readTopology(std::istream &is, bool fromHalf=false)
Read in just the topology.
Definition LeafNode.h:1268
Index medianOn(ValueType &value, ValueType *tmp=nullptr) const
Computes the median value of all the active voxels in this node.
Definition LeafNode.h:1529
void addTile(Index level, const Coord &, const ValueType &, bool)
Definition LeafNode.h:1580
ValueType medianAll(ValueType *tmp=nullptr) const
Computes the median value of all the active AND inactive voxels in this node.
Definition LeafNode.h:1511
void modifyValueAndActiveState(const Coord &xyz, const ModifyOp &op)
Apply a functor to the voxel at the given coordinates.
Definition LeafNode.h:456
LeafNode(const Coord &xyz, bool value, bool active)
Definition LeafNodeBool.h:768
void topologyIntersection(const LeafNode< OtherType, Log2Dim > &other, const ValueType &)
Intersect this node's set of active values with the active values of the other node,...
Definition LeafNode.h:1687
void clip(const CoordBBox &, const ValueType &background)
Set all voxels that lie outside the given axis-aligned box to the background.
Definition LeafNode.h:1121
void setActiveState(const Coord &xyz, bool on)
Set the active state of the voxel at the given coordinates but don't change its value.
Definition LeafNode.h:1095
void topologyUnion(const LeafNode< OtherType, Log2Dim > &other, const bool preserveTiles=false)
Union this node's set of active values with the active values of the other node, whose ValueType may ...
Definition LeafNode.h:1679
static Index coordToOffset(const Coord &xyz)
Return the linear table offset of the given global or local coordinates.
Definition LeafNode.h:1010
void setValueOff(const Coord &xyz)
Mark the voxel at the given coordinates as inactive but don't change its value.
Definition LeafNode.h:407
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:1467
void writeBuffers(std::ostream &os, bool toHalf=false) const
Write buffers to a stream.
Definition LeafNode.h:1402
void combine(const LeafNode &other, CombineOp &op)
Definition LeafNode.h:1720
Index64 memUsageIfLoaded() const
Definition LeafNode.h:1439
void combine2(const LeafNode &other, const OtherType &, bool valueIsActive, CombineOp &)
Definition LeafNode.h:1760
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:1161
void readBuffers(std::istream &is, bool fromHalf=false)
Read buffers from a stream.
Definition LeafNode.h:1304
bool isConstant(ValueType &firstValue, bool &state, const ValueType &tolerance=zeroVal< ValueType >()) const
Definition LeafNode.h:1475
friend class LeafNode
Definition LeafNode.h:833
Coord offsetToGlobalCoord(Index n) const
Return the global coordinates for a linear table offset.
Definition LeafNode.h:1034
const ValueType & getValue(const Coord &xyz) const
Return the value of the voxel at the given coordinates.
Definition LeafNode.h:1045
bool operator==(const LeafNode &other) const
Check for buffer, state and origin equivalence.
Definition LeafNode.h:1419
void evalActiveBoundingBox(CoordBBox &bbox, bool visitVoxels=true) const
Definition LeafNode.h:1449
void merge(const LeafNode &)
Definition LeafNode.h:1630
void setValueOn(const Coord &xyz)
Mark the voxel at the given coordinates as active but don't change its value.
Definition LeafNode.h:417
void addTileAndCache(Index, const Coord &, const ValueType &, bool, AccessorT &)
Definition LeafNode.h:1597
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:435
Index64 memUsage() const
Return the memory in bytes occupied by this node.
Definition LeafNode.h:1429
std::string str() const
Return a string representation of this node.
Definition LeafNode.h:997
bool probeValue(const Coord &xyz, ValueType &val) const
Return true if the voxel at the given coordinates is active.
Definition LeafNode.h:1061
BBox< Coord > CoordBBox
Definition NanoVDB.h:2535
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...
bool operator==(const Vec3< T0 > &v0, const Vec3< T1 > &v1)
Equality operator, does exact floating point comparisons.
Definition Vec3.h:473
bool operator!=(const Vec3< T0 > &v0, const Vec3< T1 > &v1)
Inequality operator, does exact floating point comparisons.
Definition Vec3.h:481
Definition TreeIterator.h:29
Index32 Index
Definition Types.h:54
uint32_t Index32
Definition Types.h:52
int32_t Int32
Definition Types.h:56
uint64_t Index64
Definition Types.h:53
@ MERGE_NODES
Definition Types.h:508
@ MERGE_ACTIVE_STATES_AND_NODES
Definition Types.h:509
ValueType combine(const ValueType &v0, const ValueType &v1, const ValueType &v2, const openvdb::Vec3d &w)
Combine different value types.
Definition AttributeTransferUtil.h:141
Definition Exceptions.h:13
static pnanovdb_uint32_t allocate(pnanovdb_uint32_t *poffset, pnanovdb_uint32_t size, pnanovdb_uint32_t alignment)
Definition pnanovdb_validate_strides.h:20
typename std::remove_const< ValueT >::type NonConstValueType
Definition Iterator.h:184
DenseIteratorBase()
Definition Iterator.h:188
typename BaseT::NonConstValueType NonConstValueT
Definition LeafNode.h:261
DenseIteratorBase< MaskDenseIterator, DenseIter, LeafNode, void, bool > BaseT
Definition LeafNode.h:260
SparseIteratorBase< MaskOnIter, ValueIter, LeafNode, const bool > BaseT
Definition LeafNode.h:219
ChildIter(const MaskIterT &iter, NodeT *parent)
Definition LeafNodeMask.h:603
ChildIter()
Definition LeafNodeMask.h:602
bool getItem(Index pos, void *&child, NonConstValueT &value) const
Definition LeafNodeMask.h:617
DenseIter()
Definition LeafNodeMask.h:614
DenseIter(const MaskDenseIter &iter, NodeT *parent)
Definition LeafNodeMask.h:615
typename BaseT::NonConstValueType NonConstValueT
Definition LeafNodeMask.h:612
DenseIteratorBase< MaskDenseIter, DenseIter, NodeT, void, ValueT > BaseT
Definition LeafNodeMask.h:611
void unsetItem(Index pos, const ValueT &val) const
Definition LeafNodeMask.h:628
SameConfiguration<OtherNodeType>::value is true if and only if OtherNodeType is the type of a LeafNod...
Definition LeafNodeMask.h:57
static const bool value
Definition LeafNodeMask.h:58
ValueConverter<T>::Type is the type of a LeafNode having the same dimensions as this node but a diffe...
Definition LeafNodeMask.h:52
LeafNode< OtherValueType, Log2Dim > Type
Definition LeafNodeMask.h:52
void setItem(Index pos, bool value) const
Definition LeafNodeMask.h:585
void modifyValue(const ModifyOp &op) const
Definition LeafNodeMask.h:594
ValueIter(const MaskIterT &iter, NodeT *parent)
Definition LeafNodeMask.h:579
const bool & getValue() const
Definition LeafNodeMask.h:582
const bool & getItem(Index pos) const
Definition LeafNodeMask.h:581
ValueIter()
Definition LeafNodeMask.h:578
void setValue(bool value) const
Definition LeafNodeMask.h:587
void modifyItem(Index n, const ModifyOp &op) const
Definition LeafNodeMask.h:591
SparseIteratorBase< MaskIterT, ValueIter, NodeT, ValueT > BaseT
Definition LeafNodeMask.h:576
static const bool value
Definition LeafNode.h:890
SparseIteratorBase()
Definition Iterator.h:122
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition version.h.in:121
#define OPENVDB_USE_VERSION_NAMESPACE
Definition version.h.in:212