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; }
143 bool allocate() {
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;
779template<Index Log2Dim>
781LeafNode<ValueMask, Log2Dim>::LeafNode()
786template<Index Log2Dim>
789 : mBuffer(value || active)
790 , mOrigin(xyz & (~(DIM - 1)))
795template<Index Log2Dim>
798 : mBuffer(value || active)
799 , mOrigin(xyz & (~(DIM - 1)))
804template<Index Log2Dim>
807 : mBuffer(other.mBuffer)
808 , mOrigin(other.mOrigin)
809 , mTransientData(other.mTransientData)
815template<Index Log2Dim>
816template<
typename ValueT>
819 : mBuffer(other.valueMask())
820 , mOrigin(other.origin())
821 , mTransientData(other.mTransientData)
826template<Index Log2Dim>
827template<
typename ValueT>
831 : mBuffer(other.valueMask())
832 , mOrigin(other.origin())
833 , mTransientData(other.mTransientData)
838template<Index Log2Dim>
839template<
typename ValueT>
842 : mBuffer(other.valueMask())
843 , mOrigin(other.origin())
844 , mTransientData(other.mTransientData)
849template<Index Log2Dim>
850template<
typename ValueT>
853 bool offValue,
bool onValue, TopologyCopy)
854 : mBuffer(other.valueMask())
855 , mOrigin(other.origin())
856 , mTransientData(other.mTransientData)
858 if (offValue==
true) {
859 if (onValue==
false) {
860 mBuffer.mData.toggle();
862 mBuffer.mData.setOn();
868template<Index Log2Dim>
878template<Index Log2Dim>
883 return sizeof(*this);
887template<Index Log2Dim>
892 return sizeof(*this);
896template<Index Log2Dim>
900 CoordBBox this_bbox = this->getNodeBoundingBox();
901 if (bbox.isInside(this_bbox))
return;
902 if (ValueOnCIter
iter = this->cbeginValueOn()) {
905 for(;
iter; ++
iter) this_bbox.expand(this->offsetToLocalCoord(
iter.pos()));
906 this_bbox.translate(this->origin());
908 bbox.expand(this_bbox);
913template<Index Log2Dim>
914template<
typename OtherType, Index OtherLog2Dim>
919 return (Log2Dim == OtherLog2Dim && mBuffer.mData == other->getValueMask());
923template<Index Log2Dim>
927 std::ostringstream ostr;
928 ostr <<
"LeafNode @" << mOrigin <<
": ";
929 for (
Index32 n = 0; n < SIZE; ++n) ostr << (mBuffer.mData.isOn(n) ?
'#' :
'.');
937template<Index Log2Dim>
941 OPENVDB_ASSERT((xyz[0] & (DIM-1u)) < DIM && (xyz[1] & (DIM-1u)) < DIM && (xyz[2] & (DIM-1u)) < DIM);
942 return ((xyz[0] & (DIM-1u)) << 2*Log2Dim)
943 + ((xyz[1] & (DIM-1u)) << Log2Dim)
944 + (xyz[2] & (DIM-1u));
948template<Index Log2Dim>
954 xyz.setX(n >> 2*Log2Dim);
955 n &= ((1 << 2*Log2Dim) - 1);
956 xyz.setY(n >> Log2Dim);
957 xyz.setZ(n & ((1 << Log2Dim) - 1));
962template<Index Log2Dim>
966 return (this->offsetToLocalCoord(n) + this->origin());
973template<Index Log2Dim>
977 mBuffer.mData.load(is);
981template<Index Log2Dim>
985 mBuffer.mData.save(os);
989template<Index Log2Dim>
996 this->readBuffers(is, fromHalf);
999 bool background =
false;
1001 background = *
static_cast<const bool*
>(bgPtr);
1003 this->
clip(clipBBox, background);
1007template<Index Log2Dim>
1012 mBuffer.mData.load(is);
1018template<Index Log2Dim>
1023 mBuffer.mData.save(os);
1025 os.write(
reinterpret_cast<const char*
>(&mOrigin),
sizeof(
Coord::ValueType) * 3);
1032template<Index Log2Dim>
1036 return mOrigin == other.mOrigin && mBuffer == other.mBuffer;
1040template<Index Log2Dim>
1051template<Index Log2Dim>
1055 if (!mBuffer.mData.isConstant(state))
return false;
1064template<Index Log2Dim>
1068 const Index countTrue = mBuffer.mData.countOn();
1069 return countTrue > (NUM_VALUES >> 1);
1072template<Index Log2Dim>
1076 const Index countTrueOn = mBuffer.mData.countOn();
1081template<Index Log2Dim>
1085 const Index countFalseOff = mBuffer.mData.countOff();
1087 return countFalseOff;
1094template<Index Log2Dim>
1098 this->addTile(this->coordToOffset(xyz), val, active);
1101template<Index Log2Dim>
1106 this->setValueOnly(offset, val);
1107 this->setActiveState(offset, active);
1110template<Index Log2Dim>
1111template<
typename AccessorT>
1114 bool val,
bool active, AccessorT&)
1116 this->addTile(level, xyz, val, active);
1123template<Index Log2Dim>
1128 if (mBuffer.mData.isOn(this->coordToOffset(xyz)))
return Buffer::sOn;
else return Buffer::sOff;
1132template<Index Log2Dim>
1138 if (mBuffer.mData.isOn(offset))
return Buffer::sOn;
else return Buffer::sOff;
1142template<Index Log2Dim>
1146 return this->probeValue(this->coordToOffset(xyz), val);
1150template<Index Log2Dim>
1154 val = mBuffer.mData.isOn(offset);
1159template<Index Log2Dim>
1163 this->setValueOn(this->coordToOffset(xyz), val);
1167template<Index Log2Dim>
1172 mBuffer.mData.set(offset, val);
1176template<Index Log2Dim>
1180 this->setValueOnly(this->coordToOffset(xyz), val);
1184template<Index Log2Dim>
1188 mBuffer.mData.set(this->coordToOffset(xyz), on);
1192template<Index Log2Dim>
1196 this->setValueOff(this->coordToOffset(xyz), val);
1200template<Index Log2Dim>
1205 mBuffer.mData.set(offset, val);
1209template<Index Log2Dim>
1210template<
typename ModifyOp>
1214 bool val = mBuffer.mData.isOn(offset);
1216 mBuffer.mData.set(offset, val);
1220template<Index Log2Dim>
1221template<
typename ModifyOp>
1225 this->modifyValue(this->coordToOffset(xyz), op);
1229template<Index Log2Dim>
1230template<
typename ModifyOp>
1234 const Index offset = this->coordToOffset(xyz);
1235 bool val = mBuffer.mData.isOn(offset), state = val;
1237 mBuffer.mData.set(offset, val);
1244template<Index Log2Dim>
1245template<MergePolicy Policy>
1251 mBuffer.mData |= other.mBuffer.mData;
1255template<Index Log2Dim>
1256template<MergePolicy Policy>
1262 if (tileValue) mBuffer.mData.setOn();
1270template<Index Log2Dim>
1271template<
typename OtherType>
1275 mBuffer.mData |= other.valueMask();
1279template<Index Log2Dim>
1280template<
typename OtherType>
1285 mBuffer.mData &= other.valueMask();
1289template<Index Log2Dim>
1290template<
typename OtherType>
1295 mBuffer.mData &= !other.valueMask();
1302template<Index Log2Dim>
1306 CoordBBox nodeBBox = this->getNodeBoundingBox();
1307 if (!clipBBox.hasOverlap(nodeBBox)) {
1309 this->fill(nodeBBox, background,
false);
1310 }
else if (clipBBox.isInside(nodeBBox)) {
1320 nodeBBox.intersect(clipBBox);
1322 int &x = xyz.x(), &y = xyz.y(), &z = xyz.z();
1323 for (x = nodeBBox.min().x(); x <= nodeBBox.max().x(); ++x) {
1324 for (y = nodeBBox.min().y(); y <= nodeBBox.max().y(); ++y) {
1325 for (z = nodeBBox.min().z(); z <= nodeBBox.max().z(); ++z) {
1326 mask.setOn(
static_cast<Index32>(this->coordToOffset(xyz)));
1333 for (MaskOffIter maskIter = mask.beginOff(); maskIter; ++maskIter) {
1334 this->setValueOff(maskIter.pos(), background);
1342template<Index Log2Dim>
1346 auto clippedBBox = this->getNodeBoundingBox();
1347 clippedBBox.intersect(bbox);
1348 if (!clippedBBox)
return;
1350 for (
Int32 x = clippedBBox.min().x(); x <= clippedBBox.max().x(); ++x) {
1351 const Index offsetX = (x & (DIM-1u))<<2*Log2Dim;
1352 for (
Int32 y = clippedBBox.min().y(); y <= clippedBBox.max().y(); ++y) {
1353 const Index offsetXY = offsetX + ((y & (DIM-1u))<< Log2Dim);
1354 for (
Int32 z = clippedBBox.min().z(); z <= clippedBBox.max().z(); ++z) {
1355 const Index offset = offsetXY + (z & (DIM-1u));
1356 mBuffer.mData.set(offset, value);
1362template<Index Log2Dim>
1366 mBuffer.fill(value);
1373template<Index Log2Dim>
1374template<
typename DenseT>
1378 using DenseValueType =
typename DenseT::ValueType;
1380 const size_t xStride = dense.xStride(), yStride = dense.yStride(), zStride = dense.zStride();
1381 const Coord&
min = dense.bbox().min();
1382 DenseValueType* t0 = dense.data() + zStride * (bbox.min()[2] - min[2]);
1383 const Int32 n0 = bbox.min()[2] & (DIM-1u);
1384 for (
Int32 x = bbox.min()[0], ex = bbox.max()[0] + 1; x < ex; ++x) {
1385 DenseValueType* t1 = t0 + xStride * (x -
min[0]);
1386 const Int32 n1 = n0 + ((x & (DIM-1u)) << 2*LOG2DIM);
1387 for (
Int32 y = bbox.min()[1], ey = bbox.max()[1] + 1; y < ey; ++y) {
1388 DenseValueType* t2 = t1 + yStride * (y -
min[1]);
1389 Int32 n2 = n1 + ((y & (DIM-1u)) << LOG2DIM);
1390 for (
Int32 z = bbox.min()[2], ez = bbox.max()[2] + 1; z < ez; ++z, t2 += zStride) {
1391 *t2 = DenseValueType(mBuffer.mData.isOn(n2++));
1398template<Index Log2Dim>
1399template<
typename DenseT>
1402 bool background,
bool tolerance)
1404 using DenseValueType =
typename DenseT::ValueType;
1406 inline static bool toBool(
const DenseValueType& v) {
return !math::isZero(v); }
1409 const size_t xStride = dense.xStride(), yStride = dense.yStride(), zStride = dense.zStride();
1410 const Coord&
min = dense.bbox().min();
1411 const DenseValueType* s0 = dense.data() + zStride * (bbox.min()[2] - min[2]);
1412 const Int32 n0 = bbox.min()[2] & (DIM-1u);
1413 for (
Int32 x = bbox.min()[0], ex = bbox.max()[0] + 1; x < ex; ++x) {
1414 const DenseValueType* s1 = s0 + xStride * (x -
min[0]);
1415 const Int32 n1 = n0 + ((x & (DIM-1u)) << 2*LOG2DIM);
1416 for (
Int32 y = bbox.min()[1], ey = bbox.max()[1] + 1; y < ey; ++y) {
1417 const DenseValueType* s2 = s1 + yStride * (y -
min[1]);
1418 Int32 n2 = n1 + ((y & (DIM-1u)) << LOG2DIM);
1419 for (
Int32 z = bbox.min()[2], ez = bbox.max()[2]+1; z < ez; ++z, ++n2, s2 += zStride) {
1421 if (tolerance || (background == Local::toBool(*s2))) {
1422 mBuffer.mData.set(n2, background);
1424 mBuffer.mData.set(n2, Local::toBool(*s2));
1435template<Index Log2Dim>
1436template<
typename CombineOp>
1440 CombineArgs<bool> args;
1441 for (
Index i = 0; i < SIZE; ++i) {
1442 bool result =
false, aVal = mBuffer.mData.isOn(i), bVal = other.mBuffer.mData.isOn(i);
1443 op(args.setARef(aVal)
1447 .setResultRef(result));
1448 mBuffer.mData.set(i, result);
1453template<Index Log2Dim>
1454template<
typename CombineOp>
1458 CombineArgs<bool> args;
1459 args.setBRef(value).setBIsActive(valueIsActive);
1460 for (
Index i = 0; i < SIZE; ++i) {
1461 bool result =
false, aVal = mBuffer.mData.isOn(i);
1462 op(args.setARef(aVal)
1464 .setResultRef(result));
1465 mBuffer.mData.set(i, result);
1473template<Index Log2Dim>
1474template<
typename CombineOp,
typename OtherType>
1477 bool valueIsActive, CombineOp& op)
1479 CombineArgs<bool, OtherType> args;
1480 args.setBRef(value).setBIsActive(valueIsActive);
1481 for (
Index i = 0; i < SIZE; ++i) {
1482 bool result =
false, aVal = other.mBuffer.mData.isOn(i);
1483 op(args.setARef(aVal)
1485 .setResultRef(result));
1486 mBuffer.mData.set(i, result);
1491template<Index Log2Dim>
1492template<
typename CombineOp,
typename OtherNodeT>
1495 bool valueIsActive, CombineOp& op)
1497 CombineArgs<bool, typename OtherNodeT::ValueType> args;
1498 args.setARef(value).setAIsActive(valueIsActive);
1499 for (
Index i = 0; i < SIZE; ++i) {
1500 bool result =
false, bVal = other.mBuffer.mData.isOn(i);
1501 op(args.setBRef(bVal)
1503 .setResultRef(result));
1504 mBuffer.mData.set(i, result);
1509template<Index Log2Dim>
1510template<
typename CombineOp,
typename OtherNodeT>
1514 CombineArgs<bool, typename OtherNodeT::ValueType> args;
1515 for (
Index i = 0; i < SIZE; ++i) {
1516 bool result =
false, b0Val = b0.mBuffer.mData.isOn(i), b1Val = b1.mBuffer.mData.isOn(i);
1517 op(args.setARef(b0Val)
1518 .setAIsActive(b0Val)
1520 .setBIsActive(b1Val)
1521 .setResultRef(result));
1522 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:760
Tag dispatch class that distinguishes topology copy constructors from deep copy constructors.
Definition Types.h:754
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:1079
void writeTopology(std::ostream &os, bool toHalf=false) const
Write out just the topology.
Definition LeafNode.h:1335
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:1263
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:1290
const ValueType & getValue(const Coord &xyz) const
Return the value of the voxel at the given coordinates.
Definition LeafNode.h:1104
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:1162
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:1746
Index medianOff(ValueType &value, ValueType *tmp=nullptr) const
Computes the median value of all the inactive voxels in this node.
Definition LeafNode.h:1602
ValueType medianAll(ValueType *tmp=nullptr) const
Computes the median value of all the active AND inactive voxels in this node.
Definition LeafNode.h:1560
~LeafNode()
Destructor.
Definition LeafNode.h:1049
void readTopology(std::istream &is, bool fromHalf=false)
Read in just the topology.
Definition LeafNode.h:1327
Index medianOn(ValueType &value, ValueType *tmp=nullptr) const
Computes the median value of all the active voxels in this node.
Definition LeafNode.h:1578
void addTile(Index level, const Coord &, const ValueType &, bool)
Definition LeafNode.h:1629
void modifyValueAndActiveState(const Coord &xyz, const ModifyOp &op)
Apply a functor to the voxel at the given coordinates.
Definition LeafNode.h:488
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:1737
void clip(const CoordBBox &, const ValueType &background)
Set all voxels that lie outside the given axis-aligned box to the background.
Definition LeafNode.h:1180
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:1154
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:1729
static Index coordToOffset(const Coord &xyz)
Return the linear table offset of the given global or local coordinates.
Definition LeafNode.h:1069
void setValueOff(const Coord &xyz)
Mark the voxel at the given coordinates as inactive but don't change its value.
Definition LeafNode.h:439
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:1516
void writeBuffers(std::ostream &os, bool toHalf=false) const
Write buffers to a stream.
Definition LeafNode.h:1451
void combine(const LeafNode &other, CombineOp &op)
Definition LeafNode.h:1770
Index64 memUsageIfLoaded() const
Definition LeafNode.h:1488
void combine2(const LeafNode &other, const OtherType &, bool valueIsActive, CombineOp &)
Definition LeafNode.h:1810
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:1220
void readBuffers(std::istream &is, bool fromHalf=false)
Read buffers from a stream.
Definition LeafNode.h:1363
bool isConstant(ValueType &firstValue, bool &state, const ValueType &tolerance=zeroVal< ValueType >()) const
Definition LeafNode.h:1524
friend class LeafNode
Definition LeafNode.h:892
Coord offsetToGlobalCoord(Index n) const
Return the global coordinates for a linear table offset.
Definition LeafNode.h:1093
bool operator==(const LeafNode &other) const
Check for buffer, state and origin equivalence.
Definition LeafNode.h:1468
void evalActiveBoundingBox(CoordBBox &bbox, bool visitVoxels=true) const
Definition LeafNode.h:1498
void merge(const LeafNode &)
Definition LeafNode.h:1680
void setValueOn(const Coord &xyz)
Mark the voxel at the given coordinates as active but don't change its value.
Definition LeafNode.h:449
void addTileAndCache(Index, const Coord &, const ValueType &, bool, AccessorT &)
Definition LeafNode.h:1646
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:467
Index64 memUsage() const
Return the memory in bytes occupied by this node.
Definition LeafNode.h:1478
std::string str() const
Return a string representation of this node.
Definition LeafNode.h:1056
bool probeValue(const Coord &xyz, ValueType &val) const
Return true if the voxel at the given coordinates is active.
Definition LeafNode.h:1120
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:34
uint32_t Index32
Definition Types.h:32
int32_t Int32
Definition Types.h:36
uint64_t Index64
Definition Types.h:33
@ MERGE_NODES
Definition Types.h:579
@ MERGE_ACTIVE_STATES_AND_NODES
Definition Types.h:580
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
typename std::remove_const< ValueT >::type NonConstValueType
Definition Iterator.h:184
DenseIteratorBase()
Definition Iterator.h:188
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:949
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