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 static Index64 nonLeafCount() {
return 0; }
122 Index64 onVoxelCount()
const {
return mBuffer.mData.countOn(); }
124 Index64 offVoxelCount()
const {
return mBuffer.mData.countOff(); }
125 Index64 onLeafVoxelCount()
const {
return this->onVoxelCount(); }
126 Index64 offLeafVoxelCount()
const {
return this->offVoxelCount(); }
127 static Index64 onTileCount() {
return 0; }
128 static Index64 offTileCount() {
return 0; }
131 bool isEmpty()
const {
return mBuffer.mData.isOff(); }
133 bool isDense()
const {
return mBuffer.mData.isOn(); }
137 bool isAllocated()
const {
return true; }
141 bool allocate() {
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; }
198#if OPENVDB_ABI_VERSION_NUMBER < 14
203 void readTopology(std::istream&,
bool fromHalf =
false);
205 void writeTopology(std::ostream&,
bool toHalf =
false)
const;
208 void readBuffers(std::istream&,
bool fromHalf =
false);
209 void readBuffers(std::istream& is,
const CoordBBox&,
bool fromHalf =
false);
211 void writeBuffers(std::ostream&,
bool toHalf =
false)
const;
218 const bool& getValue(
const Coord& xyz)
const;
220 const bool& getValue(Index offset)
const;
225 bool probeValue(
const Coord& xyz,
bool& val)
const;
229 bool probeValue(Index offset,
bool& val)
const;
232 static Index getValueLevel(
const Coord&) {
return LEVEL; }
235 void setActiveState(
const Coord& xyz,
bool on);
237 void setActiveState(Index offset,
bool on) {
OPENVDB_ASSERT(offset<SIZE); mBuffer.mData.set(offset, on); }
240 void setValueOnly(
const Coord& xyz,
bool val);
242 void setValueOnly(Index offset,
bool val) {
OPENVDB_ASSERT(offset<SIZE); mBuffer.setValue(offset,val); }
245 void setValueOff(
const Coord& xyz) { mBuffer.mData.setOff(this->coordToOffset(xyz)); }
247 void setValueOff(Index offset) {
OPENVDB_ASSERT(offset < SIZE); mBuffer.mData.setOff(offset); }
250 void setValueOff(
const Coord& xyz,
bool val);
252 void setValueOff(Index offset,
bool val);
255 void setValueOn(
const Coord& xyz) { mBuffer.mData.setOn(this->coordToOffset(xyz)); }
257 void setValueOn(Index offset) {
OPENVDB_ASSERT(offset < SIZE); mBuffer.mData.setOn(offset); }
260 void setValueOn(
const Coord& xyz,
bool val);
262 void setValue(
const Coord& xyz,
bool val) { this->setValueOn(xyz, val); }
264 void setValueOn(Index offset,
bool val);
268 template<
typename ModifyOp>
269 void modifyValue(Index offset,
const ModifyOp& op);
272 template<
typename ModifyOp>
273 void modifyValue(
const Coord& xyz,
const ModifyOp& op);
276 template<
typename ModifyOp>
277 void modifyValueAndActiveState(
const Coord& xyz,
const ModifyOp& op);
280 void setValuesOn() { mBuffer.mData.setOn(); }
282 void setValuesOff() { mBuffer.mData.setOff(); }
285 bool isValueOn(
const Coord& xyz)
const {
return this->isValueOn(this->coordToOffset(xyz)); }
287 bool isValueOn(Index offset)
const {
OPENVDB_ASSERT(offset < SIZE);
return mBuffer.mData.isOn(offset); }
289 bool isValueOff(
const Coord& xyz)
const {
return this->isValueOff(this->coordToOffset(xyz)); }
291 bool isValueOff(Index offset)
const {
OPENVDB_ASSERT(offset < SIZE);
return mBuffer.mData.isOff(offset); }
294 static bool hasActiveTiles() {
return false; }
297 void clip(
const CoordBBox&,
bool background);
300 void fill(
const CoordBBox& bbox,
bool value,
bool =
false);
302 void denseFill(
const CoordBBox& bbox,
bool value,
bool =
false) { this->fill(bbox, value); }
305 void fill(
const bool& value,
bool dummy =
false);
318 template<
typename DenseT>
319 void copyToDense(
const CoordBBox& bbox, DenseT& dense)
const;
337 template<
typename DenseT>
338 void copyFromDense(
const CoordBBox& bbox,
const DenseT& dense,
bool background,
bool tolerance);
342 template<
typename AccessorT>
343 const bool& getValueAndCache(
const Coord& xyz, AccessorT&)
const {
return this->getValue(xyz);}
347 template<
typename AccessorT>
348 bool isValueOnAndCache(
const Coord& xyz, AccessorT&)
const {
return this->isValueOn(xyz); }
352 template<
typename AccessorT>
353 void setValueAndCache(
const Coord& xyz,
bool val, AccessorT&) { this->setValueOn(xyz, val); }
358 template<
typename AccessorT>
359 void setValueOnlyAndCache(
const Coord& xyz,
bool val, AccessorT&) {this->setValueOnly(xyz,val);}
363 template<
typename AccessorT>
364 void setValueOffAndCache(
const Coord& xyz,
bool value, AccessorT&)
366 this->setValueOff(xyz, value);
372 template<
typename ModifyOp,
typename AccessorT>
373 void modifyValueAndCache(
const Coord& xyz,
const ModifyOp& op, AccessorT&)
375 this->modifyValue(xyz, op);
380 template<
typename ModifyOp,
typename AccessorT>
381 void modifyValueAndActiveStateAndCache(
const Coord& xyz,
const ModifyOp& op, AccessorT&)
383 this->modifyValueAndActiveState(xyz, op);
389 template<
typename AccessorT>
390 void setActiveStateAndCache(
const Coord& xyz,
bool on, AccessorT&)
392 this->setActiveState(xyz, on);
398 template<
typename AccessorT>
399 bool probeValueAndCache(
const Coord& xyz,
bool& val, AccessorT&)
const
401 return this->probeValue(xyz, val);
406 template<
typename AccessorT>
407 static Index getValueLevelAndCache(
const Coord&, AccessorT&) {
return LEVEL; }
412 const bool& getFirstValue()
const {
if (mBuffer.mData.isOn(0))
return Buffer::sOn;
else return Buffer::sOff; }
416 const bool& getLastValue()
const {
if (mBuffer.mData.isOn(SIZE-1))
return Buffer::sOn;
else return Buffer::sOff; }
421 bool isConstant(
bool& constValue,
bool& state,
bool tolerance = 0)
const;
428 bool medianAll()
const;
437 Index medianOn(ValueType &value)
const;
447 Index medianOff(ValueType &value)
const;
450 bool isInactive()
const {
return mBuffer.mData.isOff(); }
459 const bool& getValueUnsafe(Index offset)
const {
return this->getValue(offset); }
461 bool getValueUnsafe(Index offset,
bool& value)
const {
return this->probeValue(offset, value); }
463 void setActiveStateUnsafe(Index offset,
bool on) { this->setActiveState(offset, on); }
465 void setValueOnlyUnsafe(Index offset,
const bool& value) {
return this->setValueOnly(offset, value); }
467 void setValueOnUnsafe(Index offset) { this->setValueOn(offset); }
469 void setValueOnUnsafe(Index offset,
const bool& value) { this->setValueOn(offset, value); }
471 void setValueOffUnsafe(Index offset) { this->setValueOff(offset); }
473 void setValueOffUnsafe(Index offset,
const bool& value) { this->setValueOff(offset, value); }
477 void resetBackground(
bool,
bool) {}
480 void negate() { mBuffer.mData.toggle(); }
482 template<MergePolicy Policy>
483 void merge(
const LeafNode& other,
bool bg =
false,
bool otherBG =
false);
484 template<MergePolicy Policy>
void merge(
bool tileValue,
bool tileActive=
false);
488 void voxelizeActiveTiles(
bool =
true) {}
496 template<
typename OtherType>
497 void topologyUnion(
const LeafNode<OtherType, Log2Dim>& other,
const bool preserveTiles =
false);
510 template<
typename OtherType>
511 void topologyIntersection(
const LeafNode<OtherType, Log2Dim>& other,
const bool&);
524 template<
typename OtherType>
525 void topologyDifference(
const LeafNode<OtherType, Log2Dim>& other,
const bool&);
527 template<
typename CombineOp>
528 void combine(
const LeafNode& other, CombineOp& op);
529 template<
typename CombineOp>
530 void combine(
bool,
bool valueIsActive, CombineOp& op);
532 template<
typename CombineOp,
typename OtherType >
533 void combine2(
const LeafNode& other,
const OtherType&,
bool valueIsActive, CombineOp&);
534 template<
typename CombineOp,
typename OtherNodeT >
535 void combine2(
bool,
const OtherNodeT& other,
bool valueIsActive, CombineOp&);
536 template<
typename CombineOp,
typename OtherNodeT >
537 void combine2(
const LeafNode& b0,
const OtherNodeT& b1, CombineOp&);
541 void prune(
const ValueType& = zeroVal<ValueType>()) {}
542 void addLeaf(LeafNode*) {}
543 template<
typename AccessorT>
544 void addLeafAndCache(LeafNode*, AccessorT&) {}
545 template<
typename NodeT>
546 NodeT* stealNode(
const Coord&,
const ValueType&,
bool) {
return nullptr; }
547 template<
typename NodeT>
548 NodeT* probeNode(
const Coord&) {
return nullptr; }
549 template<
typename NodeT>
550 const NodeT* probeConstNode(
const Coord&)
const {
return nullptr; }
551 template<
typename ArrayT>
void getNodes(ArrayT&)
const {}
552 template<
typename ArrayT>
void stealNodes(ArrayT&,
const ValueType&,
bool) {}
555 void addTile(Index level,
const Coord&,
bool val,
bool active);
556 void addTile(Index offset,
bool val,
bool active);
557 template<
typename AccessorT>
558 void addTileAndCache(Index level,
const Coord&,
bool val,
bool active, AccessorT&);
562 LeafNode* touchLeaf(
const Coord&) {
return this; }
563 template<
typename AccessorT>
564 LeafNode* touchLeafAndCache(
const Coord&, AccessorT&) {
return this; }
565 LeafNode* probeLeaf(
const Coord&) {
return this; }
566 template<
typename AccessorT>
567 LeafNode* probeLeafAndCache(
const Coord&, AccessorT&) {
return this; }
568 template<
typename NodeT,
typename AccessorT>
569 NodeT* probeNodeAndCache(
const Coord&, AccessorT&)
572 if (!(std::is_same<NodeT, LeafNode>::value))
return nullptr;
573 return reinterpret_cast<NodeT*
>(
this);
579 const LeafNode* probeLeaf(
const Coord&)
const {
return this; }
580 template<
typename AccessorT>
581 const LeafNode* probeLeafAndCache(
const Coord&, AccessorT&)
const {
return this; }
582 const LeafNode* probeConstLeaf(
const Coord&)
const {
return this; }
583 template<
typename AccessorT>
584 const LeafNode* probeConstLeafAndCache(
const Coord&, AccessorT&)
const {
return this; }
585 template<
typename NodeT,
typename AccessorT>
586 const NodeT* probeConstNodeAndCache(
const Coord&, AccessorT&)
const
589 if (!(std::is_same<NodeT, LeafNode>::value))
return nullptr;
590 return reinterpret_cast<const NodeT*
>(
this);
599 using MaskOnIter =
typename NodeMaskType::OnIterator;
600 using MaskOffIter =
typename NodeMaskType::OffIterator;
601 using MaskDenseIter =
typename NodeMaskType::DenseIterator;
603 template<
typename MaskIterT,
typename NodeT,
typename ValueT>
607 public SparseIteratorBase<MaskIterT, ValueIter<MaskIterT, NodeT, ValueT>, NodeT, ValueT>
623 template<
typename ModifyOp>
626 template<
typename ModifyOp>
631 template<
typename MaskIterT,
typename NodeT>
640 template<
typename NodeT,
typename ValueT>
642 MaskDenseIter, DenseIter<NodeT, ValueT>, NodeT, void, ValueT>
665 using ValueOnIter = ValueIter<MaskOnIter, LeafNode, const bool>;
666 using ValueOnCIter = ValueIter<MaskOnIter, const LeafNode, const bool>;
667 using ValueOffIter = ValueIter<MaskOffIter, LeafNode, const bool>;
668 using ValueOffCIter = ValueIter<MaskOffIter, const LeafNode, const bool>;
669 using ValueAllIter = ValueIter<MaskDenseIter, LeafNode, const bool>;
670 using ValueAllCIter = ValueIter<MaskDenseIter, const LeafNode, const bool>;
671 using ChildOnIter = ChildIter<MaskOnIter, LeafNode>;
672 using ChildOnCIter = ChildIter<MaskOnIter, const LeafNode>;
673 using ChildOffIter = ChildIter<MaskOffIter, LeafNode>;
674 using ChildOffCIter = ChildIter<MaskOffIter, const LeafNode>;
675 using ChildAllIter = DenseIter<LeafNode, bool>;
676 using ChildAllCIter = DenseIter<const LeafNode, const bool>;
678 ValueOnCIter cbeginValueOn()
const {
return ValueOnCIter(mBuffer.mData.beginOn(),
this); }
679 ValueOnCIter beginValueOn()
const {
return ValueOnCIter(mBuffer.mData.beginOn(),
this); }
680 ValueOnIter beginValueOn() {
return ValueOnIter(mBuffer.mData.beginOn(),
this); }
681 ValueOffCIter cbeginValueOff()
const {
return ValueOffCIter(mBuffer.mData.beginOff(),
this); }
682 ValueOffCIter beginValueOff()
const {
return ValueOffCIter(mBuffer.mData.beginOff(),
this); }
683 ValueOffIter beginValueOff() {
return ValueOffIter(mBuffer.mData.beginOff(),
this); }
684 ValueAllCIter cbeginValueAll()
const {
return ValueAllCIter(mBuffer.mData.beginDense(),
this); }
685 ValueAllCIter beginValueAll()
const {
return ValueAllCIter(mBuffer.mData.beginDense(),
this); }
686 ValueAllIter beginValueAll() {
return ValueAllIter(mBuffer.mData.beginDense(),
this); }
688 ValueOnCIter cendValueOn()
const {
return ValueOnCIter(mBuffer.mData.endOn(),
this); }
689 ValueOnCIter endValueOn()
const {
return ValueOnCIter(mBuffer.mData.endOn(),
this); }
690 ValueOnIter endValueOn() {
return ValueOnIter(mBuffer.mData.endOn(),
this); }
691 ValueOffCIter cendValueOff()
const {
return ValueOffCIter(mBuffer.mData.endOff(),
this); }
692 ValueOffCIter endValueOff()
const {
return ValueOffCIter(mBuffer.mData.endOff(),
this); }
693 ValueOffIter endValueOff() {
return ValueOffIter(mBuffer.mData.endOff(),
this); }
694 ValueAllCIter cendValueAll()
const {
return ValueAllCIter(mBuffer.mData.endDense(),
this); }
695 ValueAllCIter endValueAll()
const {
return ValueAllCIter(mBuffer.mData.endDense(),
this); }
696 ValueAllIter endValueAll() {
return ValueAllIter(mBuffer.mData.endDense(),
this); }
700 ChildOnCIter cbeginChildOn()
const {
return ChildOnCIter(mBuffer.mData.endOn(),
this); }
701 ChildOnCIter beginChildOn()
const {
return ChildOnCIter(mBuffer.mData.endOn(),
this); }
702 ChildOnIter beginChildOn() {
return ChildOnIter(mBuffer.mData.endOn(),
this); }
703 ChildOffCIter cbeginChildOff()
const {
return ChildOffCIter(mBuffer.mData.endOff(),
this); }
704 ChildOffCIter beginChildOff()
const {
return ChildOffCIter(mBuffer.mData.endOff(),
this); }
705 ChildOffIter beginChildOff() {
return ChildOffIter(mBuffer.mData.endOff(),
this); }
706 ChildAllCIter cbeginChildAll()
const {
return ChildAllCIter(mBuffer.mData.beginDense(),
this); }
707 ChildAllCIter beginChildAll()
const {
return ChildAllCIter(mBuffer.mData.beginDense(),
this); }
708 ChildAllIter beginChildAll() {
return ChildAllIter(mBuffer.mData.beginDense(),
this); }
710 ChildOnCIter cendChildOn()
const {
return ChildOnCIter(mBuffer.mData.endOn(),
this); }
711 ChildOnCIter endChildOn()
const {
return ChildOnCIter(mBuffer.mData.endOn(),
this); }
712 ChildOnIter endChildOn() {
return ChildOnIter(mBuffer.mData.endOn(),
this); }
713 ChildOffCIter cendChildOff()
const {
return ChildOffCIter(mBuffer.mData.endOff(),
this); }
714 ChildOffCIter endChildOff()
const {
return ChildOffCIter(mBuffer.mData.endOff(),
this); }
715 ChildOffIter endChildOff() {
return ChildOffIter(mBuffer.mData.endOff(),
this); }
716 ChildAllCIter cendChildAll()
const {
return ChildAllCIter(mBuffer.mData.endDense(),
this); }
717 ChildAllCIter endChildAll()
const {
return ChildAllCIter(mBuffer.mData.endDense(),
this); }
718 ChildAllIter endChildAll() {
return ChildAllIter(mBuffer.mData.endDense(),
this); }
723 bool isValueMaskOn(Index n)
const {
return mBuffer.mData.isOn(n); }
724 bool isValueMaskOn()
const {
return mBuffer.mData.isOn(); }
725 bool isValueMaskOff(Index n)
const {
return mBuffer.mData.isOff(n); }
726 bool isValueMaskOff()
const {
return mBuffer.mData.isOff(); }
727 const NodeMaskType& getValueMask()
const {
return mBuffer.mData; }
728 const NodeMaskType& valueMask()
const {
return mBuffer.mData; }
729 NodeMaskType& getValueMask() {
return mBuffer.mData; }
730 void setValueMask(
const NodeMaskType& mask) { mBuffer.mData = mask; }
731 bool isChildMaskOn(Index)
const {
return false; }
732 bool isChildMaskOff(Index)
const {
return true; }
733 bool isChildMaskOff()
const {
return true; }
735 void setValueMask(Index n,
bool on) { mBuffer.mData.set(n, on); }
736 void setValueMaskOn(Index n) { mBuffer.mData.setOn(n); }
737 void setValueMaskOff(Index n) { mBuffer.mData.setOff(n); }
740 static void evalNodeOrigin(Coord& xyz) { xyz &= ~(DIM - 1); }
752 template<
typename, Index>
friend class LeafNode;
754 friend struct ValueIter<MaskOnIter, LeafNode, bool>;
755 friend struct ValueIter<MaskOffIter, LeafNode, bool>;
756 friend struct ValueIter<MaskDenseIter, LeafNode, bool>;
757 friend struct ValueIter<MaskOnIter, const LeafNode, bool>;
758 friend struct ValueIter<MaskOffIter, const LeafNode, bool>;
759 friend struct ValueIter<MaskDenseIter, const LeafNode, bool>;
764 friend class IteratorBase<MaskOnIter, LeafNode>;
765 friend class IteratorBase<MaskOffIter, LeafNode>;
766 friend class IteratorBase<MaskDenseIter, LeafNode>;
769 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>
890 CoordBBox this_bbox = this->getNodeBoundingBox();
891 if (bbox.isInside(this_bbox))
return;
892 if (ValueOnCIter
iter = this->cbeginValueOn()) {
895 for(;
iter; ++
iter) this_bbox.expand(this->offsetToLocalCoord(
iter.pos()));
896 this_bbox.translate(this->origin());
898 bbox.expand(this_bbox);
903template<Index Log2Dim>
904template<
typename OtherType, Index OtherLog2Dim>
909 return (Log2Dim == OtherLog2Dim && mBuffer.mData == other->getValueMask());
913template<Index Log2Dim>
917 std::ostringstream ostr;
918 ostr <<
"LeafNode @" << mOrigin <<
": ";
919 for (
Index32 n = 0; n < SIZE; ++n) ostr << (mBuffer.mData.isOn(n) ?
'#' :
'.');
927template<Index Log2Dim>
931 OPENVDB_ASSERT((xyz[0] & (DIM-1u)) < DIM && (xyz[1] & (DIM-1u)) < DIM && (xyz[2] & (DIM-1u)) < DIM);
932 return ((xyz[0] & (DIM-1u)) << 2*Log2Dim)
933 + ((xyz[1] & (DIM-1u)) << Log2Dim)
934 + (xyz[2] & (DIM-1u));
938template<Index Log2Dim>
944 xyz.setX(n >> 2*Log2Dim);
945 n &= ((1 << 2*Log2Dim) - 1);
946 xyz.setY(n >> Log2Dim);
947 xyz.setZ(n & ((1 << Log2Dim) - 1));
952template<Index Log2Dim>
956 return (this->offsetToLocalCoord(n) + this->origin());
963#if OPENVDB_ABI_VERSION_NUMBER < 14
965template<Index Log2Dim>
969 mBuffer.mData.load(is);
973template<Index Log2Dim>
977 mBuffer.mData.save(os);
981template<Index Log2Dim>
988 this->readBuffers(is, fromHalf);
991 bool background =
false;
993 background = *
static_cast<const bool*
>(bgPtr);
995 this->
clip(clipBBox, background);
999template<Index Log2Dim>
1004 mBuffer.mData.load(is);
1010template<Index Log2Dim>
1015 mBuffer.mData.save(os);
1017 os.write(
reinterpret_cast<const char*
>(&mOrigin),
sizeof(
Coord::ValueType) * 3);
1026template<Index Log2Dim>
1030 return mOrigin == other.mOrigin && mBuffer == other.mBuffer;
1034template<Index Log2Dim>
1045template<Index Log2Dim>
1049 if (!mBuffer.mData.isConstant(state))
return false;
1058template<Index Log2Dim>
1062 const Index countTrue = mBuffer.mData.countOn();
1063 return countTrue > (NUM_VALUES >> 1);
1066template<Index Log2Dim>
1070 const Index countTrueOn = mBuffer.mData.countOn();
1075template<Index Log2Dim>
1079 const Index countFalseOff = mBuffer.mData.countOff();
1081 return countFalseOff;
1088template<Index Log2Dim>
1092 this->addTile(this->coordToOffset(xyz), val, active);
1095template<Index Log2Dim>
1100 this->setValueOnly(offset, val);
1101 this->setActiveState(offset, active);
1104template<Index Log2Dim>
1105template<
typename AccessorT>
1108 bool val,
bool active, AccessorT&)
1110 this->addTile(level, xyz, val, active);
1117template<Index Log2Dim>
1122 if (mBuffer.mData.isOn(this->coordToOffset(xyz)))
return Buffer::sOn;
else return Buffer::sOff;
1126template<Index Log2Dim>
1132 if (mBuffer.mData.isOn(offset))
return Buffer::sOn;
else return Buffer::sOff;
1136template<Index Log2Dim>
1140 return this->probeValue(this->coordToOffset(xyz), val);
1144template<Index Log2Dim>
1148 val = mBuffer.mData.isOn(offset);
1153template<Index Log2Dim>
1157 this->setValueOn(this->coordToOffset(xyz), val);
1161template<Index Log2Dim>
1166 mBuffer.mData.set(offset, val);
1170template<Index Log2Dim>
1174 this->setValueOnly(this->coordToOffset(xyz), val);
1178template<Index Log2Dim>
1182 mBuffer.mData.set(this->coordToOffset(xyz), on);
1186template<Index Log2Dim>
1190 this->setValueOff(this->coordToOffset(xyz), val);
1194template<Index Log2Dim>
1199 mBuffer.mData.set(offset, val);
1203template<Index Log2Dim>
1204template<
typename ModifyOp>
1208 bool val = mBuffer.mData.isOn(offset);
1210 mBuffer.mData.set(offset, val);
1214template<Index Log2Dim>
1215template<
typename ModifyOp>
1219 this->modifyValue(this->coordToOffset(xyz), op);
1223template<Index Log2Dim>
1224template<
typename ModifyOp>
1228 const Index offset = this->coordToOffset(xyz);
1229 bool val = mBuffer.mData.isOn(offset), state = val;
1231 mBuffer.mData.set(offset, val);
1238template<Index Log2Dim>
1239template<MergePolicy Policy>
1245 mBuffer.mData |= other.mBuffer.mData;
1249template<Index Log2Dim>
1250template<MergePolicy Policy>
1256 if (tileValue) mBuffer.mData.setOn();
1264template<Index Log2Dim>
1265template<
typename OtherType>
1269 mBuffer.mData |= other.valueMask();
1273template<Index Log2Dim>
1274template<
typename OtherType>
1279 mBuffer.mData &= other.valueMask();
1283template<Index Log2Dim>
1284template<
typename OtherType>
1289 mBuffer.mData &= !other.valueMask();
1296template<Index Log2Dim>
1300 CoordBBox nodeBBox = this->getNodeBoundingBox();
1301 if (!clipBBox.hasOverlap(nodeBBox)) {
1303 this->fill(nodeBBox, background,
false);
1304 }
else if (clipBBox.isInside(nodeBBox)) {
1314 nodeBBox.intersect(clipBBox);
1316 int &x = xyz.x(), &y = xyz.y(), &z = xyz.z();
1317 for (x = nodeBBox.min().x(); x <= nodeBBox.max().x(); ++x) {
1318 for (y = nodeBBox.min().y(); y <= nodeBBox.max().y(); ++y) {
1319 for (z = nodeBBox.min().z(); z <= nodeBBox.max().z(); ++z) {
1320 mask.setOn(
static_cast<Index32>(this->coordToOffset(xyz)));
1327 for (MaskOffIter maskIter = mask.beginOff(); maskIter; ++maskIter) {
1328 this->setValueOff(maskIter.pos(), background);
1336template<Index Log2Dim>
1340 auto clippedBBox = this->getNodeBoundingBox();
1341 clippedBBox.intersect(bbox);
1342 if (!clippedBBox)
return;
1344 for (
Int32 x = clippedBBox.min().x(); x <= clippedBBox.max().x(); ++x) {
1345 const Index offsetX = (x & (DIM-1u))<<2*Log2Dim;
1346 for (
Int32 y = clippedBBox.min().y(); y <= clippedBBox.max().y(); ++y) {
1347 const Index offsetXY = offsetX + ((y & (DIM-1u))<< Log2Dim);
1348 for (
Int32 z = clippedBBox.min().z(); z <= clippedBBox.max().z(); ++z) {
1349 const Index offset = offsetXY + (z & (DIM-1u));
1350 mBuffer.mData.set(offset, value);
1356template<Index Log2Dim>
1360 mBuffer.fill(value);
1367template<Index Log2Dim>
1368template<
typename DenseT>
1372 using DenseValueType =
typename DenseT::ValueType;
1374 const size_t xStride = dense.xStride(), yStride = dense.yStride(), zStride = dense.zStride();
1375 const Coord&
min = dense.bbox().min();
1376 DenseValueType* t0 = dense.data() + zStride * (bbox.min()[2] - min[2]);
1377 const Int32 n0 = bbox.min()[2] & (DIM-1u);
1378 for (
Int32 x = bbox.min()[0], ex = bbox.max()[0] + 1; x < ex; ++x) {
1379 DenseValueType* t1 = t0 + xStride * (x -
min[0]);
1380 const Int32 n1 = n0 + ((x & (DIM-1u)) << 2*LOG2DIM);
1381 for (
Int32 y = bbox.min()[1], ey = bbox.max()[1] + 1; y < ey; ++y) {
1382 DenseValueType* t2 = t1 + yStride * (y -
min[1]);
1383 Int32 n2 = n1 + ((y & (DIM-1u)) << LOG2DIM);
1384 for (
Int32 z = bbox.min()[2], ez = bbox.max()[2] + 1; z < ez; ++z, t2 += zStride) {
1385 *t2 = DenseValueType(mBuffer.mData.isOn(n2++));
1392template<Index Log2Dim>
1393template<
typename DenseT>
1396 bool background,
bool tolerance)
1398 using DenseValueType =
typename DenseT::ValueType;
1400 inline static bool toBool(
const DenseValueType& v) {
return !math::isZero(v); }
1403 const size_t xStride = dense.xStride(), yStride = dense.yStride(), zStride = dense.zStride();
1404 const Coord&
min = dense.bbox().min();
1405 const DenseValueType* s0 = dense.data() + zStride * (bbox.min()[2] - min[2]);
1406 const Int32 n0 = bbox.min()[2] & (DIM-1u);
1407 for (
Int32 x = bbox.min()[0], ex = bbox.max()[0] + 1; x < ex; ++x) {
1408 const DenseValueType* s1 = s0 + xStride * (x -
min[0]);
1409 const Int32 n1 = n0 + ((x & (DIM-1u)) << 2*LOG2DIM);
1410 for (
Int32 y = bbox.min()[1], ey = bbox.max()[1] + 1; y < ey; ++y) {
1411 const DenseValueType* s2 = s1 + yStride * (y -
min[1]);
1412 Int32 n2 = n1 + ((y & (DIM-1u)) << LOG2DIM);
1413 for (
Int32 z = bbox.min()[2], ez = bbox.max()[2]+1; z < ez; ++z, ++n2, s2 += zStride) {
1415 if (tolerance || (background == Local::toBool(*s2))) {
1416 mBuffer.mData.set(n2, background);
1418 mBuffer.mData.set(n2, Local::toBool(*s2));
1429template<Index Log2Dim>
1430template<
typename CombineOp>
1434 CombineArgs<bool> args;
1435 for (
Index i = 0; i < SIZE; ++i) {
1436 bool result =
false, aVal = mBuffer.mData.isOn(i), bVal = other.mBuffer.mData.isOn(i);
1437 op(args.setARef(aVal)
1441 .setResultRef(result));
1442 mBuffer.mData.set(i, result);
1447template<Index Log2Dim>
1448template<
typename CombineOp>
1452 CombineArgs<bool> args;
1453 args.setBRef(value).setBIsActive(valueIsActive);
1454 for (
Index i = 0; i < SIZE; ++i) {
1455 bool result =
false, aVal = mBuffer.mData.isOn(i);
1456 op(args.setARef(aVal)
1458 .setResultRef(result));
1459 mBuffer.mData.set(i, result);
1467template<Index Log2Dim>
1468template<
typename CombineOp,
typename OtherType>
1471 bool valueIsActive, CombineOp& op)
1473 CombineArgs<bool, OtherType> args;
1474 args.setBRef(value).setBIsActive(valueIsActive);
1475 for (
Index i = 0; i < SIZE; ++i) {
1476 bool result =
false, aVal = other.mBuffer.mData.isOn(i);
1477 op(args.setARef(aVal)
1479 .setResultRef(result));
1480 mBuffer.mData.set(i, result);
1485template<Index Log2Dim>
1486template<
typename CombineOp,
typename OtherNodeT>
1489 bool valueIsActive, CombineOp& op)
1491 CombineArgs<bool, typename OtherNodeT::ValueType> args;
1492 args.setARef(value).setAIsActive(valueIsActive);
1493 for (
Index i = 0; i < SIZE; ++i) {
1494 bool result =
false, bVal = other.mBuffer.mData.isOn(i);
1495 op(args.setBRef(bVal)
1497 .setResultRef(result));
1498 mBuffer.mData.set(i, result);
1503template<Index Log2Dim>
1504template<
typename CombineOp,
typename OtherNodeT>
1508 CombineArgs<bool, typename OtherNodeT::ValueType> args;
1509 for (
Index i = 0; i < SIZE; ++i) {
1510 bool result =
false, b0Val = b0.mBuffer.mData.isOn(i), b1Val = b1.mBuffer.mData.isOn(i);
1511 op(args.setARef(b0Val)
1512 .setAIsActive(b0Val)
1514 .setBIsActive(b1Val)
1515 .setResultRef(result));
1516 mBuffer.mData.set(i, result);
#define OPENVDB_ASSERT(X)
Definition Assert.h:41
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:1086
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:1270
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:1295
const ValueType & getValue(const Coord &xyz) const
Return the value of the voxel at the given coordinates.
Definition LeafNode.h:1111
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:1169
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:1580
ValueType medianAll(ValueType *tmp=nullptr) const
Computes the median value of all the active AND inactive voxels in this node.
Definition LeafNode.h:1532
~LeafNode()
Destructor.
Definition LeafNode.h:1056
Index medianOn(ValueType &value, ValueType *tmp=nullptr) const
Computes the median value of all the active voxels in this node.
Definition LeafNode.h:1553
void addTile(Index level, const Coord &, const ValueType &, bool)
Definition LeafNode.h:1610
void modifyValueAndActiveState(const Coord &xyz, const ModifyOp &op)
Apply a functor to the voxel at the given coordinates.
Definition LeafNode.h:489
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:1187
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:1161
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:1076
void setValueOff(const Coord &xyz)
Mark the voxel at the given coordinates as inactive but don't change its value.
Definition LeafNode.h:441
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:1488
void combine(const LeafNode &other, CombineOp &op)
Definition LeafNode.h:1751
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:1227
bool isConstant(ValueType &firstValue, bool &state, const ValueType &tolerance=zeroVal< ValueType >()) const
Definition LeafNode.h:1496
friend class LeafNode
Definition LeafNode.h:892
Coord offsetToGlobalCoord(Index n) const
Return the global coordinates for a linear table offset.
Definition LeafNode.h:1100
bool operator==(const LeafNode &other) const
Check for buffer, state and origin equivalence.
Definition LeafNode.h:1450
void evalActiveBoundingBox(CoordBBox &bbox, bool visitVoxels=true) const
Definition LeafNode.h:1470
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:451
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:469
Index64 memUsage() const
Return the memory in bytes occupied by this node.
Definition LeafNode.h:1460
std::string str() const
Return a string representation of this node.
Definition LeafNode.h:1063
bool probeValue(const Coord &xyz, ValueType &val) const
Return true if the voxel at the given coordinates is active.
Definition LeafNode.h:1127
math::BBox< Coord > CoordBBox
Definition Math.h:2838
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
General-purpose arithmetic and comparison routines, most of which accept arbitrary value types (or at...
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:636
ChildIter()
Definition LeafNodeMask.h:635
bool getItem(Index pos, void *&child, NonConstValueT &value) const
Definition LeafNodeMask.h:650
DenseIter()
Definition LeafNodeMask.h:647
DenseIter(const MaskDenseIter &iter, NodeT *parent)
Definition LeafNodeMask.h:648
typename BaseT::NonConstValueType NonConstValueT
Definition LeafNodeMask.h:645
DenseIteratorBase< MaskDenseIter, DenseIter, NodeT, void, ValueT > BaseT
Definition LeafNodeMask.h:644
void unsetItem(Index pos, const ValueT &val) const
Definition LeafNodeMask.h:661
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:618
void modifyValue(const ModifyOp &op) const
Definition LeafNodeMask.h:627
ValueIter(const MaskIterT &iter, NodeT *parent)
Definition LeafNodeMask.h:612
const bool & getValue() const
Definition LeafNodeMask.h:615
const bool & getItem(Index pos) const
Definition LeafNodeMask.h:614
ValueIter()
Definition LeafNodeMask.h:611
void setValue(bool value) const
Definition LeafNodeMask.h:620
void modifyItem(Index n, const ModifyOp &op) const
Definition LeafNodeMask.h:624
SparseIteratorBase< MaskIterT, ValueIter, NodeT, ValueT > BaseT
Definition LeafNodeMask.h:609
static const bool value
Definition LeafNode.h:952
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:291