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>
30template<Index Log2Dim>
31class LeafNode<ValueMask, Log2Dim>
34 using LeafNodeType = LeafNode<ValueMask, Log2Dim>;
35 using BuildType = ValueMask;
36 using ValueType =
bool;
37 using Buffer = LeafBuffer<ValueType, Log2Dim>;
38 using NodeMaskType = util::NodeMask<Log2Dim>;
39 using Ptr = SharedPtr<LeafNodeType>;
42 static const Index LOG2DIM = Log2Dim;
43 static const Index TOTAL = Log2Dim;
44 static const Index DIM = 1 << TOTAL;
45 static const Index NUM_VALUES = 1 << 3 * Log2Dim;
46 static const Index NUM_VOXELS = NUM_VALUES;
47 static const Index SIZE = NUM_VALUES;
48 static const Index LEVEL = 0;
52 template<
typename OtherValueType>
57 template<
typename OtherNodeType>
69 explicit LeafNode(
const Coord& xyz,
bool value =
false,
bool dummy =
false);
75 LeafNode(
const LeafNode&);
78 template<
typename OtherValueType>
79 explicit LeafNode(
const LeafNode<OtherValueType, Log2Dim>& other);
82 template<
typename ValueType>
83 LeafNode(
const LeafNode<ValueType, Log2Dim>& other,
TopologyCopy);
88 template<
typename ValueType>
89 LeafNode(
const LeafNode<ValueType, Log2Dim>& other,
bool offValue,
bool onValue,
TopologyCopy);
90 template<
typename ValueType>
91 LeafNode(
const LeafNode<ValueType, Log2Dim>& other,
bool background,
TopologyCopy);
101 static Index log2dim() {
return Log2Dim; }
103 static Index dim() {
return DIM; }
105 static Index size() {
return SIZE; }
107 static Index numValues() {
return SIZE; }
109 static Index getLevel() {
return LEVEL; }
111 static void getNodeLog2Dims(std::vector<Index>& dims) { dims.push_back(Log2Dim); }
113 static Index getChildDim() {
return 1; }
115 static Index64 leafCount() {
return 1; }
117 void nodeCount(std::vector<Index64> &)
const {}
119 void nodeCount(std::vector<Index32> &)
const {}
121 static Index64 nonLeafCount() {
return 0; }
124 Index64 onVoxelCount()
const {
return mBuffer.mData.countOn(); }
126 Index64 offVoxelCount()
const {
return mBuffer.mData.countOff(); }
127 Index64 onLeafVoxelCount()
const {
return this->onVoxelCount(); }
128 Index64 offLeafVoxelCount()
const {
return this->offVoxelCount(); }
129 static Index64 onTileCount() {
return 0; }
130 static Index64 offTileCount() {
return 0; }
133 bool isEmpty()
const {
return mBuffer.mData.isOff(); }
135 bool isDense()
const {
return mBuffer.mData.isOn(); }
139 bool isAllocated()
const {
return true; }
152 void evalActiveBoundingBox(CoordBBox& bbox,
bool visitVoxels =
true)
const;
156 CoordBBox getNodeBoundingBox()
const {
return CoordBBox::createCube(mOrigin, DIM); }
159 void setOrigin(
const Coord& origin) { mOrigin = origin; }
162 const Coord& origin()
const {
return mOrigin; }
163 void getOrigin(Coord& origin)
const { origin = mOrigin; }
164 void getOrigin(Int32& x, Int32& y, Int32& z)
const { mOrigin.asXYZ(x, y, z); }
168 static Index coordToOffset(
const Coord& xyz);
171 static Coord offsetToLocalCoord(Index n);
173 Coord offsetToGlobalCoord(Index n)
const;
176 Index32 transientData()
const {
return mTransientData; }
178 void setTransientData(Index32 transientData) { mTransientData = transientData; }
181 std::string str()
const;
185 template<
typename OtherType, Index OtherLog2Dim>
186 bool hasSameTopology(
const LeafNode<OtherType, OtherLog2Dim>* other)
const;
197 void swap(Buffer& other) { mBuffer.swap(other); }
198 const Buffer& buffer()
const {
return mBuffer; }
199 Buffer& buffer() {
return mBuffer; }
205 void readTopology(std::istream&,
bool fromHalf =
false);
207 void writeTopology(std::ostream&,
bool toHalf =
false)
const;
210 void readBuffers(std::istream&,
bool fromHalf =
false);
211 void readBuffers(std::istream& is,
const CoordBBox&,
bool fromHalf =
false);
213 void writeBuffers(std::ostream&,
bool toHalf =
false)
const;
219 const bool& getValue(
const Coord& xyz)
const;
221 const bool& getValue(Index offset)
const;
226 bool probeValue(
const Coord& xyz,
bool& val)
const;
230 bool probeValue(Index offset,
bool& val)
const;
233 static Index getValueLevel(
const Coord&) {
return LEVEL; }
236 void setActiveState(
const Coord& xyz,
bool on);
238 void setActiveState(Index offset,
bool on) {
OPENVDB_ASSERT(offset<SIZE); mBuffer.mData.set(offset, on); }
241 void setValueOnly(
const Coord& xyz,
bool val);
243 void setValueOnly(Index offset,
bool val) {
OPENVDB_ASSERT(offset<SIZE); mBuffer.setValue(offset,val); }
246 void setValueOff(
const Coord& xyz) { mBuffer.mData.setOff(this->coordToOffset(xyz)); }
248 void setValueOff(Index offset) {
OPENVDB_ASSERT(offset < SIZE); mBuffer.mData.setOff(offset); }
251 void setValueOff(
const Coord& xyz,
bool val);
253 void setValueOff(Index offset,
bool val);
256 void setValueOn(
const Coord& xyz) { mBuffer.mData.setOn(this->coordToOffset(xyz)); }
258 void setValueOn(Index offset) {
OPENVDB_ASSERT(offset < SIZE); mBuffer.mData.setOn(offset); }
261 void setValueOn(
const Coord& xyz,
bool val);
263 void setValue(
const Coord& xyz,
bool val) { this->setValueOn(xyz, val); }
265 void setValueOn(Index offset,
bool val);
269 template<
typename ModifyOp>
270 void modifyValue(Index offset,
const ModifyOp& op);
273 template<
typename ModifyOp>
274 void modifyValue(
const Coord& xyz,
const ModifyOp& op);
277 template<
typename ModifyOp>
278 void modifyValueAndActiveState(
const Coord& xyz,
const ModifyOp& op);
281 void setValuesOn() { mBuffer.mData.setOn(); }
283 void setValuesOff() { mBuffer.mData.setOff(); }
286 bool isValueOn(
const Coord& xyz)
const {
return this->isValueOn(this->coordToOffset(xyz)); }
288 bool isValueOn(Index offset)
const {
OPENVDB_ASSERT(offset < SIZE);
return mBuffer.mData.isOn(offset); }
290 bool isValueOff(
const Coord& xyz)
const {
return this->isValueOff(this->coordToOffset(xyz)); }
292 bool isValueOff(Index offset)
const {
OPENVDB_ASSERT(offset < SIZE);
return mBuffer.mData.isOff(offset); }
295 static bool hasActiveTiles() {
return false; }
298 void clip(
const CoordBBox&,
bool background);
301 void fill(
const CoordBBox& bbox,
bool value,
bool =
false);
303 void denseFill(
const CoordBBox& bbox,
bool value,
bool =
false) { this->fill(bbox, value); }
306 void fill(
const bool& value,
bool dummy =
false);
319 template<
typename DenseT>
320 void copyToDense(
const CoordBBox& bbox, DenseT& dense)
const;
338 template<
typename DenseT>
339 void copyFromDense(
const CoordBBox& bbox,
const DenseT& dense,
bool background,
bool tolerance);
343 template<
typename AccessorT>
344 const bool& getValueAndCache(
const Coord& xyz, AccessorT&)
const {
return this->getValue(xyz);}
348 template<
typename AccessorT>
349 bool isValueOnAndCache(
const Coord& xyz, AccessorT&)
const {
return this->isValueOn(xyz); }
353 template<
typename AccessorT>
354 void setValueAndCache(
const Coord& xyz,
bool val, AccessorT&) { this->setValueOn(xyz, val); }
359 template<
typename AccessorT>
360 void setValueOnlyAndCache(
const Coord& xyz,
bool val, AccessorT&) {this->setValueOnly(xyz,val);}
364 template<
typename AccessorT>
365 void setValueOffAndCache(
const Coord& xyz,
bool value, AccessorT&)
367 this->setValueOff(xyz, value);
373 template<
typename ModifyOp,
typename AccessorT>
374 void modifyValueAndCache(
const Coord& xyz,
const ModifyOp& op, AccessorT&)
376 this->modifyValue(xyz, op);
381 template<
typename ModifyOp,
typename AccessorT>
382 void modifyValueAndActiveStateAndCache(
const Coord& xyz,
const ModifyOp& op, AccessorT&)
384 this->modifyValueAndActiveState(xyz, op);
390 template<
typename AccessorT>
391 void setActiveStateAndCache(
const Coord& xyz,
bool on, AccessorT&)
393 this->setActiveState(xyz, on);
399 template<
typename AccessorT>
400 bool probeValueAndCache(
const Coord& xyz,
bool& val, AccessorT&)
const
402 return this->probeValue(xyz, val);
407 template<
typename AccessorT>
408 static Index getValueLevelAndCache(
const Coord&, AccessorT&) {
return LEVEL; }
413 const bool& getFirstValue()
const {
if (mBuffer.mData.isOn(0))
return Buffer::sOn;
else return Buffer::sOff; }
417 const bool& getLastValue()
const {
if (mBuffer.mData.isOn(SIZE-1))
return Buffer::sOn;
else return Buffer::sOff; }
422 bool isConstant(
bool& constValue,
bool& state,
bool tolerance = 0)
const;
429 bool medianAll()
const;
438 Index medianOn(ValueType &value)
const;
448 Index medianOff(ValueType &value)
const;
451 bool isInactive()
const {
return mBuffer.mData.isOff(); }
460 const bool& getValueUnsafe(Index offset)
const {
return this->getValue(offset); }
462 bool getValueUnsafe(Index offset,
bool& value)
const {
return this->probeValue(offset, value); }
464 void setActiveStateUnsafe(Index offset,
bool on) { this->setActiveState(offset, on); }
466 void setValueOnlyUnsafe(Index offset,
const bool& value) {
return this->setValueOnly(offset, value); }
468 void setValueOnUnsafe(Index offset) { this->setValueOn(offset); }
470 void setValueOnUnsafe(Index offset,
const bool& value) { this->setValueOn(offset, value); }
472 void setValueOffUnsafe(Index offset) { this->setValueOff(offset); }
474 void setValueOffUnsafe(Index offset,
const bool& value) { this->setValueOff(offset, value); }
478 void resetBackground(
bool,
bool) {}
481 void negate() { mBuffer.mData.toggle(); }
483 template<MergePolicy Policy>
484 void merge(
const LeafNode& other,
bool bg =
false,
bool otherBG =
false);
485 template<MergePolicy Policy>
void merge(
bool tileValue,
bool tileActive=
false);
489 void voxelizeActiveTiles(
bool =
true) {}
497 template<
typename OtherType>
498 void topologyUnion(
const LeafNode<OtherType, Log2Dim>& other,
const bool preserveTiles =
false);
511 template<
typename OtherType>
512 void topologyIntersection(
const LeafNode<OtherType, Log2Dim>& other,
const bool&);
525 template<
typename OtherType>
526 void topologyDifference(
const LeafNode<OtherType, Log2Dim>& other,
const bool&);
528 template<
typename CombineOp>
529 void combine(
const LeafNode& other, CombineOp& op);
530 template<
typename CombineOp>
531 void combine(
bool,
bool valueIsActive, CombineOp& op);
533 template<
typename CombineOp,
typename OtherType >
534 void combine2(
const LeafNode& other,
const OtherType&,
bool valueIsActive, CombineOp&);
535 template<
typename CombineOp,
typename OtherNodeT >
536 void combine2(
bool,
const OtherNodeT& other,
bool valueIsActive, CombineOp&);
537 template<
typename CombineOp,
typename OtherNodeT >
538 void combine2(
const LeafNode& b0,
const OtherNodeT& b1, CombineOp&);
542 void prune(
const ValueType& = zeroVal<ValueType>()) {}
543 void addLeaf(LeafNode*) {}
544 template<
typename AccessorT>
545 void addLeafAndCache(LeafNode*, AccessorT&) {}
546 template<
typename NodeT>
547 NodeT* stealNode(
const Coord&,
const ValueType&,
bool) {
return nullptr; }
548 template<
typename NodeT>
549 NodeT* probeNode(
const Coord&) {
return nullptr; }
550 template<
typename NodeT>
551 const NodeT* probeConstNode(
const Coord&)
const {
return nullptr; }
552 template<
typename ArrayT>
void getNodes(ArrayT&)
const {}
553 template<
typename ArrayT>
void stealNodes(ArrayT&,
const ValueType&,
bool) {}
556 void addTile(Index level,
const Coord&,
bool val,
bool active);
557 void addTile(Index offset,
bool val,
bool active);
558 template<
typename AccessorT>
559 void addTileAndCache(Index level,
const Coord&,
bool val,
bool active, AccessorT&);
563 LeafNode* touchLeaf(
const Coord&) {
return this; }
564 template<
typename AccessorT>
565 LeafNode* touchLeafAndCache(
const Coord&, AccessorT&) {
return this; }
566 LeafNode* probeLeaf(
const Coord&) {
return this; }
567 template<
typename AccessorT>
568 LeafNode* probeLeafAndCache(
const Coord&, AccessorT&) {
return this; }
569 template<
typename NodeT,
typename AccessorT>
570 NodeT* probeNodeAndCache(
const Coord&, AccessorT&)
573 if (!(std::is_same<NodeT, LeafNode>::value))
return nullptr;
574 return reinterpret_cast<NodeT*
>(
this);
580 const LeafNode* probeLeaf(
const Coord&)
const {
return this; }
581 template<
typename AccessorT>
582 const LeafNode* probeLeafAndCache(
const Coord&, AccessorT&)
const {
return this; }
583 const LeafNode* probeConstLeaf(
const Coord&)
const {
return this; }
584 template<
typename AccessorT>
585 const LeafNode* probeConstLeafAndCache(
const Coord&, AccessorT&)
const {
return this; }
586 template<
typename NodeT,
typename AccessorT>
587 const NodeT* probeConstNodeAndCache(
const Coord&, AccessorT&)
const
590 if (!(std::is_same<NodeT, LeafNode>::value))
return nullptr;
591 return reinterpret_cast<const NodeT*
>(
this);
600 using MaskOnIter =
typename NodeMaskType::OnIterator;
601 using MaskOffIter =
typename NodeMaskType::OffIterator;
602 using MaskDenseIter =
typename NodeMaskType::DenseIterator;
604 template<
typename MaskIterT,
typename NodeT,
typename ValueT>
608 public SparseIteratorBase<MaskIterT, ValueIter<MaskIterT, NodeT, ValueT>, NodeT, ValueT>
624 template<
typename ModifyOp>
627 template<
typename ModifyOp>
632 template<
typename MaskIterT,
typename NodeT>
641 template<
typename NodeT,
typename ValueT>
643 MaskDenseIter, DenseIter<NodeT, ValueT>, NodeT, void, ValueT>
666 using ValueOnIter = ValueIter<MaskOnIter, LeafNode, const bool>;
667 using ValueOnCIter = ValueIter<MaskOnIter, const LeafNode, const bool>;
668 using ValueOffIter = ValueIter<MaskOffIter, LeafNode, const bool>;
669 using ValueOffCIter = ValueIter<MaskOffIter, const LeafNode, const bool>;
670 using ValueAllIter = ValueIter<MaskDenseIter, LeafNode, const bool>;
671 using ValueAllCIter = ValueIter<MaskDenseIter, const LeafNode, const bool>;
672 using ChildOnIter = ChildIter<MaskOnIter, LeafNode>;
673 using ChildOnCIter = ChildIter<MaskOnIter, const LeafNode>;
674 using ChildOffIter = ChildIter<MaskOffIter, LeafNode>;
675 using ChildOffCIter = ChildIter<MaskOffIter, const LeafNode>;
676 using ChildAllIter = DenseIter<LeafNode, bool>;
677 using ChildAllCIter = DenseIter<const LeafNode, const bool>;
679 ValueOnCIter cbeginValueOn()
const {
return ValueOnCIter(mBuffer.mData.beginOn(),
this); }
680 ValueOnCIter beginValueOn()
const {
return ValueOnCIter(mBuffer.mData.beginOn(),
this); }
681 ValueOnIter beginValueOn() {
return ValueOnIter(mBuffer.mData.beginOn(),
this); }
682 ValueOffCIter cbeginValueOff()
const {
return ValueOffCIter(mBuffer.mData.beginOff(),
this); }
683 ValueOffCIter beginValueOff()
const {
return ValueOffCIter(mBuffer.mData.beginOff(),
this); }
684 ValueOffIter beginValueOff() {
return ValueOffIter(mBuffer.mData.beginOff(),
this); }
685 ValueAllCIter cbeginValueAll()
const {
return ValueAllCIter(mBuffer.mData.beginDense(),
this); }
686 ValueAllCIter beginValueAll()
const {
return ValueAllCIter(mBuffer.mData.beginDense(),
this); }
687 ValueAllIter beginValueAll() {
return ValueAllIter(mBuffer.mData.beginDense(),
this); }
689 ValueOnCIter cendValueOn()
const {
return ValueOnCIter(mBuffer.mData.endOn(),
this); }
690 ValueOnCIter endValueOn()
const {
return ValueOnCIter(mBuffer.mData.endOn(),
this); }
691 ValueOnIter endValueOn() {
return ValueOnIter(mBuffer.mData.endOn(),
this); }
692 ValueOffCIter cendValueOff()
const {
return ValueOffCIter(mBuffer.mData.endOff(),
this); }
693 ValueOffCIter endValueOff()
const {
return ValueOffCIter(mBuffer.mData.endOff(),
this); }
694 ValueOffIter endValueOff() {
return ValueOffIter(mBuffer.mData.endOff(),
this); }
695 ValueAllCIter cendValueAll()
const {
return ValueAllCIter(mBuffer.mData.endDense(),
this); }
696 ValueAllCIter endValueAll()
const {
return ValueAllCIter(mBuffer.mData.endDense(),
this); }
697 ValueAllIter endValueAll() {
return ValueAllIter(mBuffer.mData.endDense(),
this); }
701 ChildOnCIter cbeginChildOn()
const {
return ChildOnCIter(mBuffer.mData.endOn(),
this); }
702 ChildOnCIter beginChildOn()
const {
return ChildOnCIter(mBuffer.mData.endOn(),
this); }
703 ChildOnIter beginChildOn() {
return ChildOnIter(mBuffer.mData.endOn(),
this); }
704 ChildOffCIter cbeginChildOff()
const {
return ChildOffCIter(mBuffer.mData.endOff(),
this); }
705 ChildOffCIter beginChildOff()
const {
return ChildOffCIter(mBuffer.mData.endOff(),
this); }
706 ChildOffIter beginChildOff() {
return ChildOffIter(mBuffer.mData.endOff(),
this); }
707 ChildAllCIter cbeginChildAll()
const {
return ChildAllCIter(mBuffer.mData.beginDense(),
this); }
708 ChildAllCIter beginChildAll()
const {
return ChildAllCIter(mBuffer.mData.beginDense(),
this); }
709 ChildAllIter beginChildAll() {
return ChildAllIter(mBuffer.mData.beginDense(),
this); }
711 ChildOnCIter cendChildOn()
const {
return ChildOnCIter(mBuffer.mData.endOn(),
this); }
712 ChildOnCIter endChildOn()
const {
return ChildOnCIter(mBuffer.mData.endOn(),
this); }
713 ChildOnIter endChildOn() {
return ChildOnIter(mBuffer.mData.endOn(),
this); }
714 ChildOffCIter cendChildOff()
const {
return ChildOffCIter(mBuffer.mData.endOff(),
this); }
715 ChildOffCIter endChildOff()
const {
return ChildOffCIter(mBuffer.mData.endOff(),
this); }
716 ChildOffIter endChildOff() {
return ChildOffIter(mBuffer.mData.endOff(),
this); }
717 ChildAllCIter cendChildAll()
const {
return ChildAllCIter(mBuffer.mData.endDense(),
this); }
718 ChildAllCIter endChildAll()
const {
return ChildAllCIter(mBuffer.mData.endDense(),
this); }
719 ChildAllIter endChildAll() {
return ChildAllIter(mBuffer.mData.endDense(),
this); }
724 bool isValueMaskOn(Index n)
const {
return mBuffer.mData.isOn(n); }
725 bool isValueMaskOn()
const {
return mBuffer.mData.isOn(); }
726 bool isValueMaskOff(Index n)
const {
return mBuffer.mData.isOff(n); }
727 bool isValueMaskOff()
const {
return mBuffer.mData.isOff(); }
728 const NodeMaskType& getValueMask()
const {
return mBuffer.mData; }
729 const NodeMaskType& valueMask()
const {
return mBuffer.mData; }
730 NodeMaskType& getValueMask() {
return mBuffer.mData; }
731 void setValueMask(
const NodeMaskType& mask) { mBuffer.mData = mask; }
732 bool isChildMaskOn(Index)
const {
return false; }
733 bool isChildMaskOff(Index)
const {
return true; }
734 bool isChildMaskOff()
const {
return true; }
736 void setValueMask(Index n,
bool on) { mBuffer.mData.set(n, on); }
737 void setValueMaskOn(Index n) { mBuffer.mData.setOn(n); }
738 void setValueMaskOff(Index n) { mBuffer.mData.setOff(n); }
741 static void evalNodeOrigin(Coord& xyz) { xyz &= ~(DIM - 1); }
753 template<
typename, Index>
friend class LeafNode;
755 friend struct ValueIter<MaskOnIter, LeafNode,
bool>;
756 friend struct ValueIter<MaskOffIter, LeafNode,
bool>;
757 friend struct ValueIter<MaskDenseIter, LeafNode,
bool>;
758 friend struct ValueIter<MaskOnIter, const LeafNode,
bool>;
759 friend struct ValueIter<MaskOffIter, const LeafNode,
bool>;
760 friend struct ValueIter<MaskDenseIter, const LeafNode,
bool>;
765 friend class IteratorBase<MaskOnIter, LeafNode>;
766 friend class IteratorBase<MaskOffIter, LeafNode>;
767 friend class IteratorBase<MaskDenseIter, LeafNode>;
770 template<
typename, Index>
friend class LeafBuffer;
778template<Index Log2Dim>
780LeafNode<ValueMask, Log2Dim>::LeafNode()
785template<Index Log2Dim>
788 : mBuffer(value || active)
789 , mOrigin(xyz & (~(DIM - 1)))
794template<Index Log2Dim>
797 : mBuffer(value || active)
798 , mOrigin(xyz & (~(DIM - 1)))
803template<Index Log2Dim>
806 : mBuffer(other.mBuffer)
807 , mOrigin(other.mOrigin)
808 , mTransientData(other.mTransientData)
814template<Index Log2Dim>
815template<
typename ValueT>
818 : mBuffer(other.valueMask())
819 , mOrigin(other.origin())
820 , mTransientData(other.mTransientData)
825template<Index Log2Dim>
826template<
typename ValueT>
830 : mBuffer(other.valueMask())
831 , mOrigin(other.origin())
832 , mTransientData(other.mTransientData)
837template<Index Log2Dim>
838template<
typename ValueT>
841 : mBuffer(other.valueMask())
842 , mOrigin(other.origin())
843 , mTransientData(other.mTransientData)
848template<Index Log2Dim>
849template<
typename ValueT>
852 bool offValue,
bool onValue, TopologyCopy)
853 : mBuffer(other.valueMask())
854 , mOrigin(other.origin())
855 , mTransientData(other.mTransientData)
857 if (offValue==
true) {
858 if (onValue==
false) {
859 mBuffer.mData.toggle();
861 mBuffer.mData.setOn();
867template<Index Log2Dim>
877template<Index Log2Dim>
882 return sizeof(*this);
886template<Index Log2Dim>
891 return sizeof(*this);
895template<Index Log2Dim>
899 CoordBBox this_bbox = this->getNodeBoundingBox();
900 if (bbox.isInside(this_bbox))
return;
901 if (ValueOnCIter
iter = this->cbeginValueOn()) {
904 for(;
iter; ++
iter) this_bbox.expand(this->offsetToLocalCoord(
iter.pos()));
905 this_bbox.translate(this->origin());
907 bbox.expand(this_bbox);
912template<Index Log2Dim>
913template<
typename OtherType, Index OtherLog2Dim>
918 return (Log2Dim == OtherLog2Dim && mBuffer.mData == other->getValueMask());
922template<Index Log2Dim>
926 std::ostringstream ostr;
927 ostr <<
"LeafNode @" << mOrigin <<
": ";
928 for (
Index32 n = 0; n < SIZE; ++n) ostr << (mBuffer.mData.isOn(n) ?
'#' :
'.');
936template<Index Log2Dim>
940 OPENVDB_ASSERT((xyz[0] & (DIM-1u)) < DIM && (xyz[1] & (DIM-1u)) < DIM && (xyz[2] & (DIM-1u)) < DIM);
941 return ((xyz[0] & (DIM-1u)) << 2*Log2Dim)
942 + ((xyz[1] & (DIM-1u)) << Log2Dim)
943 + (xyz[2] & (DIM-1u));
947template<Index Log2Dim>
953 xyz.setX(n >> 2*Log2Dim);
954 n &= ((1 << 2*Log2Dim) - 1);
955 xyz.setY(n >> Log2Dim);
956 xyz.setZ(n & ((1 << Log2Dim) - 1));
961template<Index Log2Dim>
965 return (this->offsetToLocalCoord(n) + this->origin());
972template<Index Log2Dim>
976 mBuffer.mData.load(is);
980template<Index Log2Dim>
984 mBuffer.mData.save(os);
988template<Index Log2Dim>
995 this->readBuffers(is, fromHalf);
998 bool background =
false;
1000 background = *
static_cast<const bool*
>(bgPtr);
1002 this->
clip(clipBBox, background);
1006template<Index Log2Dim>
1011 mBuffer.mData.load(is);
1017template<Index Log2Dim>
1022 mBuffer.mData.save(os);
1024 os.write(
reinterpret_cast<const char*
>(&mOrigin),
sizeof(
Coord::ValueType) * 3);
1031template<Index Log2Dim>
1035 return mOrigin == other.mOrigin && mBuffer == other.mBuffer;
1039template<Index Log2Dim>
1050template<Index Log2Dim>
1054 if (!mBuffer.mData.isConstant(state))
return false;
1063template<Index Log2Dim>
1067 const Index countTrue = mBuffer.mData.countOn();
1068 return countTrue > (NUM_VALUES >> 1);
1071template<Index Log2Dim>
1075 const Index countTrueOn = mBuffer.mData.countOn();
1080template<Index Log2Dim>
1084 const Index countFalseOff = mBuffer.mData.countOff();
1086 return countFalseOff;
1093template<Index Log2Dim>
1097 this->addTile(this->coordToOffset(xyz), val, active);
1100template<Index Log2Dim>
1105 this->setValueOnly(offset, val);
1106 this->setActiveState(offset, active);
1109template<Index Log2Dim>
1110template<
typename AccessorT>
1113 bool val,
bool active, AccessorT&)
1115 this->addTile(level, xyz, val, active);
1122template<Index Log2Dim>
1127 if (mBuffer.mData.isOn(this->coordToOffset(xyz)))
return Buffer::sOn;
else return Buffer::sOff;
1131template<Index Log2Dim>
1137 if (mBuffer.mData.isOn(offset))
return Buffer::sOn;
else return Buffer::sOff;
1141template<Index Log2Dim>
1145 return this->probeValue(this->coordToOffset(xyz), val);
1149template<Index Log2Dim>
1153 val = mBuffer.mData.isOn(offset);
1158template<Index Log2Dim>
1162 this->setValueOn(this->coordToOffset(xyz), val);
1166template<Index Log2Dim>
1171 mBuffer.mData.set(offset, val);
1175template<Index Log2Dim>
1179 this->setValueOnly(this->coordToOffset(xyz), val);
1183template<Index Log2Dim>
1187 mBuffer.mData.set(this->coordToOffset(xyz), on);
1191template<Index Log2Dim>
1195 this->setValueOff(this->coordToOffset(xyz), val);
1199template<Index Log2Dim>
1204 mBuffer.mData.set(offset, val);
1208template<Index Log2Dim>
1209template<
typename ModifyOp>
1213 bool val = mBuffer.mData.isOn(offset);
1215 mBuffer.mData.set(offset, val);
1219template<Index Log2Dim>
1220template<
typename ModifyOp>
1224 this->modifyValue(this->coordToOffset(xyz), op);
1228template<Index Log2Dim>
1229template<
typename ModifyOp>
1233 const Index offset = this->coordToOffset(xyz);
1234 bool val = mBuffer.mData.isOn(offset), state = val;
1236 mBuffer.mData.set(offset, val);
1243template<Index Log2Dim>
1244template<MergePolicy Policy>
1250 mBuffer.mData |= other.mBuffer.mData;
1254template<Index Log2Dim>
1255template<MergePolicy Policy>
1261 if (tileValue) mBuffer.mData.setOn();
1269template<Index Log2Dim>
1270template<
typename OtherType>
1274 mBuffer.mData |= other.valueMask();
1278template<Index Log2Dim>
1279template<
typename OtherType>
1284 mBuffer.mData &= other.valueMask();
1288template<Index Log2Dim>
1289template<
typename OtherType>
1294 mBuffer.mData &= !other.valueMask();
1301template<Index Log2Dim>
1305 CoordBBox nodeBBox = this->getNodeBoundingBox();
1306 if (!clipBBox.hasOverlap(nodeBBox)) {
1308 this->fill(nodeBBox, background,
false);
1309 }
else if (clipBBox.isInside(nodeBBox)) {
1319 nodeBBox.intersect(clipBBox);
1321 int &x = xyz.x(), &y = xyz.y(), &z = xyz.z();
1322 for (x = nodeBBox.min().x(); x <= nodeBBox.max().x(); ++x) {
1323 for (y = nodeBBox.min().y(); y <= nodeBBox.max().y(); ++y) {
1324 for (z = nodeBBox.min().z(); z <= nodeBBox.max().z(); ++z) {
1325 mask.setOn(
static_cast<Index32>(this->coordToOffset(xyz)));
1332 for (MaskOffIter maskIter = mask.beginOff(); maskIter; ++maskIter) {
1333 this->setValueOff(maskIter.pos(), background);
1341template<Index Log2Dim>
1345 auto clippedBBox = this->getNodeBoundingBox();
1346 clippedBBox.intersect(bbox);
1347 if (!clippedBBox)
return;
1349 for (
Int32 x = clippedBBox.min().x(); x <= clippedBBox.max().x(); ++x) {
1350 const Index offsetX = (x & (DIM-1u))<<2*Log2Dim;
1351 for (
Int32 y = clippedBBox.min().y(); y <= clippedBBox.max().y(); ++y) {
1352 const Index offsetXY = offsetX + ((y & (DIM-1u))<< Log2Dim);
1353 for (
Int32 z = clippedBBox.min().z(); z <= clippedBBox.max().z(); ++z) {
1354 const Index offset = offsetXY + (z & (DIM-1u));
1355 mBuffer.mData.set(offset, value);
1361template<Index Log2Dim>
1365 mBuffer.fill(value);
1372template<Index Log2Dim>
1373template<
typename DenseT>
1377 using DenseValueType =
typename DenseT::ValueType;
1379 const size_t xStride = dense.xStride(), yStride = dense.yStride(), zStride = dense.zStride();
1380 const Coord&
min = dense.bbox().min();
1381 DenseValueType* t0 = dense.data() + zStride * (bbox.min()[2] - min[2]);
1382 const Int32 n0 = bbox.min()[2] & (DIM-1u);
1383 for (
Int32 x = bbox.min()[0], ex = bbox.max()[0] + 1; x < ex; ++x) {
1384 DenseValueType* t1 = t0 + xStride * (x -
min[0]);
1385 const Int32 n1 = n0 + ((x & (DIM-1u)) << 2*LOG2DIM);
1386 for (
Int32 y = bbox.min()[1], ey = bbox.max()[1] + 1; y < ey; ++y) {
1387 DenseValueType* t2 = t1 + yStride * (y -
min[1]);
1388 Int32 n2 = n1 + ((y & (DIM-1u)) << LOG2DIM);
1389 for (
Int32 z = bbox.min()[2], ez = bbox.max()[2] + 1; z < ez; ++z, t2 += zStride) {
1390 *t2 = DenseValueType(mBuffer.mData.isOn(n2++));
1397template<Index Log2Dim>
1398template<
typename DenseT>
1401 bool background,
bool tolerance)
1403 using DenseValueType =
typename DenseT::ValueType;
1405 inline static bool toBool(
const DenseValueType& v) {
return !math::isZero(v); }
1408 const size_t xStride = dense.xStride(), yStride = dense.yStride(), zStride = dense.zStride();
1409 const Coord&
min = dense.bbox().min();
1410 const DenseValueType* s0 = dense.data() + zStride * (bbox.min()[2] - min[2]);
1411 const Int32 n0 = bbox.min()[2] & (DIM-1u);
1412 for (
Int32 x = bbox.min()[0], ex = bbox.max()[0] + 1; x < ex; ++x) {
1413 const DenseValueType* s1 = s0 + xStride * (x -
min[0]);
1414 const Int32 n1 = n0 + ((x & (DIM-1u)) << 2*LOG2DIM);
1415 for (
Int32 y = bbox.min()[1], ey = bbox.max()[1] + 1; y < ey; ++y) {
1416 const DenseValueType* s2 = s1 + yStride * (y -
min[1]);
1417 Int32 n2 = n1 + ((y & (DIM-1u)) << LOG2DIM);
1418 for (
Int32 z = bbox.min()[2], ez = bbox.max()[2]+1; z < ez; ++z, ++n2, s2 += zStride) {
1420 if (tolerance || (background == Local::toBool(*s2))) {
1421 mBuffer.mData.set(n2, background);
1423 mBuffer.mData.set(n2, Local::toBool(*s2));
1434template<Index Log2Dim>
1435template<
typename CombineOp>
1439 CombineArgs<bool> args;
1440 for (
Index i = 0; i < SIZE; ++i) {
1441 bool result =
false, aVal = mBuffer.mData.isOn(i), bVal = other.mBuffer.mData.isOn(i);
1442 op(args.setARef(aVal)
1446 .setResultRef(result));
1447 mBuffer.mData.set(i, result);
1452template<Index Log2Dim>
1453template<
typename CombineOp>
1457 CombineArgs<bool> args;
1458 args.setBRef(value).setBIsActive(valueIsActive);
1459 for (
Index i = 0; i < SIZE; ++i) {
1460 bool result =
false, aVal = mBuffer.mData.isOn(i);
1461 op(args.setARef(aVal)
1463 .setResultRef(result));
1464 mBuffer.mData.set(i, result);
1472template<Index Log2Dim>
1473template<
typename CombineOp,
typename OtherType>
1476 bool valueIsActive, CombineOp& op)
1478 CombineArgs<bool, OtherType> args;
1479 args.setBRef(value).setBIsActive(valueIsActive);
1480 for (
Index i = 0; i < SIZE; ++i) {
1481 bool result =
false, aVal = other.mBuffer.mData.isOn(i);
1482 op(args.setARef(aVal)
1484 .setResultRef(result));
1485 mBuffer.mData.set(i, result);
1490template<Index Log2Dim>
1491template<
typename CombineOp,
typename OtherNodeT>
1494 bool valueIsActive, CombineOp& op)
1496 CombineArgs<bool, typename OtherNodeT::ValueType> args;
1497 args.setARef(value).setAIsActive(valueIsActive);
1498 for (
Index i = 0; i < SIZE; ++i) {
1499 bool result =
false, bVal = other.mBuffer.mData.isOn(i);
1500 op(args.setBRef(bVal)
1502 .setResultRef(result));
1503 mBuffer.mData.set(i, result);
1508template<Index Log2Dim>
1509template<
typename CombineOp,
typename OtherNodeT>
1513 CombineArgs<bool, typename OtherNodeT::ValueType> args;
1514 for (
Index i = 0; i < SIZE; ++i) {
1515 bool result =
false, b0Val = b0.mBuffer.mData.isOn(i), b1Val = b1.mBuffer.mData.isOn(i);
1516 op(args.setARef(b0Val)
1517 .setAIsActive(b0Val)
1519 .setBIsActive(b1Val)
1520 .setResultRef(result));
1521 mBuffer.mData.set(i, result);
#define OPENVDB_ASSERT(X)
Definition Assert.h:41
General-purpose arithmetic and comparison routines, most of which accept arbitrary value types (or at...
Int32 ValueType
Definition Coord.h:33
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:26
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:39
static Coord offsetToLocalCoord(Index n)
Return the local coordinates for a linear table offset, where offset 0 has coordinates (0,...
Definition LeafNode.h:1050
void writeTopology(std::ostream &os, bool toHalf=false) const
Write out just the topology.
Definition LeafNode.h:1306
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:1234
bool operator!=(const LeafNode &other) const
Definition LeafNode.h:204
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:1261
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:1133
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:1727
Index medianOff(ValueType &value, ValueType *tmp=nullptr) const
Computes the median value of all the inactive voxels in this node.
Definition LeafNode.h:1583
~LeafNode()
Destructor.
Definition LeafNode.h:1020
void readTopology(std::istream &is, bool fromHalf=false)
Read in just the topology.
Definition LeafNode.h:1298
Index medianOn(ValueType &value, ValueType *tmp=nullptr) const
Computes the median value of all the active voxels in this node.
Definition LeafNode.h:1559
void addTile(Index level, const Coord &, const ValueType &, bool)
Definition LeafNode.h:1610
ValueType medianAll(ValueType *tmp=nullptr) const
Computes the median value of all the active AND inactive voxels in this node.
Definition LeafNode.h:1541
void modifyValueAndActiveState(const Coord &xyz, const ModifyOp &op)
Apply a functor to the voxel at the given coordinates.
Definition LeafNode.h:459
LeafNode(const Coord &xyz, bool value, bool active)
Definition LeafNodeBool.h:802
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:1718
void clip(const CoordBBox &, const ValueType &background)
Set all voxels that lie outside the given axis-aligned box to the background.
Definition LeafNode.h:1151
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:1125
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:1710
static Index coordToOffset(const Coord &xyz)
Return the linear table offset of the given global or local coordinates.
Definition LeafNode.h:1040
void setValueOff(const Coord &xyz)
Mark the voxel at the given coordinates as inactive but don't change its value.
Definition LeafNode.h:410
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:1497
void writeBuffers(std::ostream &os, bool toHalf=false) const
Write buffers to a stream.
Definition LeafNode.h:1432
void combine(const LeafNode &other, CombineOp &op)
Definition LeafNode.h:1751
Index64 memUsageIfLoaded() const
Definition LeafNode.h:1469
void combine2(const LeafNode &other, const OtherType &, bool valueIsActive, CombineOp &)
Definition LeafNode.h:1791
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:1191
void readBuffers(std::istream &is, bool fromHalf=false)
Read buffers from a stream.
Definition LeafNode.h:1334
bool isConstant(ValueType &firstValue, bool &state, const ValueType &tolerance=zeroVal< ValueType >()) const
Definition LeafNode.h:1505
friend class LeafNode
Definition LeafNode.h:863
Coord offsetToGlobalCoord(Index n) const
Return the global coordinates for a linear table offset.
Definition LeafNode.h:1064
const ValueType & getValue(const Coord &xyz) const
Return the value of the voxel at the given coordinates.
Definition LeafNode.h:1075
bool operator==(const LeafNode &other) const
Check for buffer, state and origin equivalence.
Definition LeafNode.h:1449
void evalActiveBoundingBox(CoordBBox &bbox, bool visitVoxels=true) const
Definition LeafNode.h:1479
void merge(const LeafNode &)
Definition LeafNode.h:1661
void setValueOn(const Coord &xyz)
Mark the voxel at the given coordinates as active but don't change its value.
Definition LeafNode.h:420
void addTileAndCache(Index, const Coord &, const ValueType &, bool, AccessorT &)
Definition LeafNode.h:1627
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:438
Index64 memUsage() const
Return the memory in bytes occupied by this node.
Definition LeafNode.h:1459
std::string str() const
Return a string representation of this node.
Definition LeafNode.h:1027
bool probeValue(const Coord &xyz, ValueType &val) const
Return true if the voxel at the given coordinates is active.
Definition LeafNode.h:1091
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:474
bool operator!=(const Vec3< T0 > &v0, const Vec3< T1 > &v1)
Inequality operator, does exact floating point comparisons.
Definition Vec3.h:482
Definition TreeIterator.h:30
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:264
DenseIteratorBase< MaskDenseIterator, DenseIter, LeafNode, void, bool > BaseT
Definition LeafNode.h:263
SparseIteratorBase< MaskOnIter, ValueIter, LeafNode, const bool > BaseT
Definition LeafNode.h:222
ChildIter(const MaskIterT &iter, NodeT *parent)
Definition LeafNodeMask.h:637
ChildIter()
Definition LeafNodeMask.h:636
bool getItem(Index pos, void *&child, NonConstValueT &value) const
Definition LeafNodeMask.h:651
DenseIter()
Definition LeafNodeMask.h:648
DenseIter(const MaskDenseIter &iter, NodeT *parent)
Definition LeafNodeMask.h:649
typename BaseT::NonConstValueType NonConstValueT
Definition LeafNodeMask.h:646
DenseIteratorBase< MaskDenseIter, DenseIter, NodeT, void, ValueT > BaseT
Definition LeafNodeMask.h:645
void unsetItem(Index pos, const ValueT &val) const
Definition LeafNodeMask.h:662
SameConfiguration<OtherNodeType>::value is true if and only if OtherNodeType is the type of a LeafNod...
Definition LeafNodeMask.h:58
static const bool value
Definition LeafNodeMask.h:59
ValueConverter<T>::Type is the type of a LeafNode having the same dimensions as this node but a diffe...
Definition LeafNodeMask.h:53
LeafNode< OtherValueType, Log2Dim > Type
Definition LeafNodeMask.h:53
void setItem(Index pos, bool value) const
Definition LeafNodeMask.h:619
void modifyValue(const ModifyOp &op) const
Definition LeafNodeMask.h:628
ValueIter(const MaskIterT &iter, NodeT *parent)
Definition LeafNodeMask.h:613
const bool & getValue() const
Definition LeafNodeMask.h:616
const bool & getItem(Index pos) const
Definition LeafNodeMask.h:615
ValueIter()
Definition LeafNodeMask.h:612
void setValue(bool value) const
Definition LeafNodeMask.h:621
void modifyItem(Index n, const ModifyOp &op) const
Definition LeafNodeMask.h:625
SparseIteratorBase< MaskIterT, ValueIter, NodeT, ValueT > BaseT
Definition LeafNodeMask.h:610
static const bool value
Definition LeafNode.h:920
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:218