4#ifndef OPENVDB_TREE_LEAF_NODE_BOOL_HAS_BEEN_INCLUDED
5#define OPENVDB_TREE_LEAF_NODE_BOOL_HAS_BEEN_INCLUDED
28template<Index Log2Dim>
29class LeafNode<bool, Log2Dim>
32 using LeafNodeType = LeafNode<bool, Log2Dim>;
33 using BuildType = bool;
34 using ValueType = bool;
35 using Buffer = LeafBuffer<ValueType, Log2Dim>;
36 using NodeMaskType = util::NodeMask<Log2Dim>;
37 using Ptr = SharedPtr<LeafNodeType>;
40 static const Index LOG2DIM = Log2Dim;
41 static const Index TOTAL = Log2Dim;
42 static const Index DIM = 1 << TOTAL;
43 static const Index NUM_VALUES = 1 << 3 * Log2Dim;
44 static const Index NUM_VOXELS = NUM_VALUES;
45 static const Index SIZE = NUM_VALUES;
46 static const Index LEVEL = 0;
50 template<
typename ValueType>
55 template<
typename OtherNodeType>
68 explicit LeafNode(
const Coord& xyz,
bool value =
false,
bool active =
false);
74 LeafNode(
const LeafNode&);
77 LeafNode& operator=(
const LeafNode&) =
default;
80 template<
typename OtherValueType>
81 explicit LeafNode(
const LeafNode<OtherValueType, Log2Dim>& other);
88 LeafNode(
const Coord& xyz,
89 const NodeMaskType& mask,
95 template<
typename ValueType>
96 LeafNode(
const LeafNode<ValueType, Log2Dim>& other,
bool offValue,
bool onValue,
TopologyCopy);
97 template<
typename ValueType>
98 LeafNode(
const LeafNode<ValueType, Log2Dim>& other,
bool background,
TopologyCopy);
108 static Index log2dim() {
return Log2Dim; }
110 static Index dim() {
return DIM; }
111 static Index size() {
return SIZE; }
112 static Index numValues() {
return SIZE; }
113 static Index getLevel() {
return LEVEL; }
114 static void getNodeLog2Dims(std::vector<Index>& dims) { dims.push_back(Log2Dim); }
115 static Index getChildDim() {
return 1; }
117 static Index64 leafCount() {
return 1; }
119 void nodeCount(std::vector<Index64> &)
const {}
120 static Index64 nonLeafCount() {
return 0; }
123 Index64 onVoxelCount()
const {
return mValueMask.countOn(); }
125 Index64 offVoxelCount()
const {
return mValueMask.countOff(); }
126 Index64 onLeafVoxelCount()
const {
return onVoxelCount(); }
127 Index64 offLeafVoxelCount()
const {
return offVoxelCount(); }
128 static Index64 onTileCount() {
return 0; }
129 static Index64 offTileCount() {
return 0; }
132 bool isEmpty()
const {
return mValueMask.isOff(); }
134 bool isDense()
const {
return mValueMask.isOn(); }
138 bool isAllocated()
const {
return true; }
142 bool allocate() {
return true; }
150 void evalActiveBoundingBox(CoordBBox& bbox,
bool visitVoxels =
true)
const;
154 CoordBBox getNodeBoundingBox()
const {
return CoordBBox::createCube(mOrigin, DIM); }
157 void setOrigin(
const Coord& origin) { mOrigin = origin; }
160 const Coord& origin()
const {
return mOrigin; }
161 void getOrigin(Coord& origin)
const { origin = mOrigin; }
162 void getOrigin(Int32& x, Int32& y, Int32& z)
const { mOrigin.asXYZ(x, y, z); }
166 static Index coordToOffset(
const Coord& xyz);
169 static Coord offsetToLocalCoord(Index n);
171 Coord offsetToGlobalCoord(Index n)
const;
174 Index32 transientData()
const {
return mTransientData; }
176 void setTransientData(Index32 transientData) { mTransientData = transientData; }
179 std::string str()
const;
183 template<
typename OtherType, Index OtherLog2Dim>
184 bool hasSameTopology(
const LeafNode<OtherType, OtherLog2Dim>* other)
const;
195 void swap(Buffer& other) { mBuffer.swap(other); }
196 const Buffer& buffer()
const {
return mBuffer; }
197 Buffer& buffer() {
return mBuffer; }
199#if OPENVDB_ABI_VERSION_NUMBER < 14
204 void readTopology(std::istream&,
bool fromHalf =
false);
206 void writeTopology(std::ostream&,
bool toHalf =
false)
const;
209 void readBuffers(std::istream&,
bool fromHalf =
false);
210 void readBuffers(std::istream& is,
const CoordBBox&,
bool fromHalf =
false);
212 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); mValueMask.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) { mValueMask.setOff(this->coordToOffset(xyz)); }
248 void setValueOff(Index offset) {
OPENVDB_ASSERT(offset < SIZE); mValueMask.setOff(offset); }
251 void setValueOff(
const Coord& xyz,
bool val);
253 void setValueOff(Index offset,
bool val);
256 void setValueOn(
const Coord& xyz) { mValueMask.setOn(this->coordToOffset(xyz)); }
258 void setValueOn(Index offset) {
OPENVDB_ASSERT(offset < SIZE); mValueMask.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() { mValueMask.setOn(); }
283 void setValuesOff() { mValueMask.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 mValueMask.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 mValueMask.isOff(offset); }
295 static bool hasActiveTiles() {
return false; }
298 void clip(
const CoordBBox&,
bool background);
301 void fill(
const CoordBBox& bbox,
bool value,
bool active =
true);
303 void denseFill(
const CoordBBox& bbox,
bool val,
bool on =
true) { this->fill(bbox, val, on); }
306 void fill(
const bool& value);
308 void fill(
const bool& value,
bool active);
321 template<
typename DenseT>
322 void copyToDense(
const CoordBBox& bbox, DenseT& dense)
const;
340 template<
typename DenseT>
341 void copyFromDense(
const CoordBBox& bbox,
const DenseT& dense,
bool background,
bool tolerance);
345 template<
typename AccessorT>
346 const bool& getValueAndCache(
const Coord& xyz, AccessorT&)
const {
return this->getValue(xyz);}
350 template<
typename AccessorT>
351 bool isValueOnAndCache(
const Coord& xyz, AccessorT&)
const {
return this->isValueOn(xyz); }
355 template<
typename AccessorT>
356 void setValueAndCache(
const Coord& xyz,
bool val, AccessorT&) { this->setValueOn(xyz, val); }
361 template<
typename AccessorT>
362 void setValueOnlyAndCache(
const Coord& xyz,
bool val, AccessorT&) {this->setValueOnly(xyz,val);}
366 template<
typename AccessorT>
367 void setValueOffAndCache(
const Coord& xyz,
bool value, AccessorT&)
369 this->setValueOff(xyz, value);
375 template<
typename ModifyOp,
typename AccessorT>
376 void modifyValueAndCache(
const Coord& xyz,
const ModifyOp& op, AccessorT&)
378 this->modifyValue(xyz, op);
383 template<
typename ModifyOp,
typename AccessorT>
384 void modifyValueAndActiveStateAndCache(
const Coord& xyz,
const ModifyOp& op, AccessorT&)
386 this->modifyValueAndActiveState(xyz, op);
392 template<
typename AccessorT>
393 void setActiveStateAndCache(
const Coord& xyz,
bool on, AccessorT&)
395 this->setActiveState(xyz, on);
401 template<
typename AccessorT>
402 bool probeValueAndCache(
const Coord& xyz,
bool& val, AccessorT&)
const
404 return this->probeValue(xyz, val);
409 template<
typename AccessorT>
410 static Index getValueLevelAndCache(
const Coord&, AccessorT&) {
return LEVEL; }
415 const bool& getFirstValue()
const {
if (mValueMask.isOn(0))
return Buffer::sOn;
else return Buffer::sOff; }
419 const bool& getLastValue()
const {
if (mValueMask.isOn(SIZE-1))
return Buffer::sOn;
else return Buffer::sOff; }
424 bool isConstant(
bool& constValue,
bool& state,
bool tolerance = 0)
const;
431 bool medianAll()
const;
439 Index medianOn(ValueType &value)
const;
447 Index medianOff(ValueType &value)
const;
450 bool isInactive()
const {
return mValueMask.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); }
475 void resetBackground(
bool oldBackground,
bool newBackground);
477 void negate() { mBuffer.mData.toggle(); }
479 template<MergePolicy Policy>
480 void merge(
const LeafNode& other,
bool bg =
false,
bool otherBG =
false);
481 template<MergePolicy Policy>
void merge(
bool tileValue,
bool tileActive);
485 void voxelizeActiveTiles(
bool =
true) {}
493 template<
typename OtherType>
494 void topologyUnion(
const LeafNode<OtherType, Log2Dim>& other,
const bool preserveTiles =
false);
507 template<
typename OtherType>
508 void topologyIntersection(
const LeafNode<OtherType, Log2Dim>& other,
const bool&);
521 template<
typename OtherType>
522 void topologyDifference(
const LeafNode<OtherType, Log2Dim>& other,
const bool&);
524 template<
typename CombineOp>
525 void combine(
const LeafNode& other, CombineOp& op);
526 template<
typename CombineOp>
527 void combine(
bool,
bool valueIsActive, CombineOp& op);
529 template<
typename CombineOp,
typename OtherType >
530 void combine2(
const LeafNode& other,
const OtherType&,
bool valueIsActive, CombineOp&);
531 template<
typename CombineOp,
typename OtherNodeT >
532 void combine2(
bool,
const OtherNodeT& other,
bool valueIsActive, CombineOp&);
533 template<
typename CombineOp,
typename OtherNodeT >
534 void combine2(
const LeafNode& b0,
const OtherNodeT& b1, CombineOp&);
538 void prune(
const ValueType& = zeroVal<ValueType>()) {}
539 void addLeaf(LeafNode*) {}
540 template<
typename AccessorT>
541 void addLeafAndCache(LeafNode*, AccessorT&) {}
542 template<
typename NodeT>
543 NodeT* stealNode(
const Coord&,
const ValueType&,
bool) {
return nullptr; }
544 template<
typename NodeT>
545 NodeT* probeNode(
const Coord&) {
return nullptr; }
546 template<
typename NodeT>
547 const NodeT* probeConstNode(
const Coord&)
const {
return nullptr; }
548 template<
typename ArrayT>
void getNodes(ArrayT&)
const {}
549 template<
typename ArrayT>
void stealNodes(ArrayT&,
const ValueType&,
bool) {}
552 void addTile(Index level,
const Coord&,
bool val,
bool active);
553 void addTile(Index offset,
bool val,
bool active);
554 template<
typename AccessorT>
555 void addTileAndCache(Index level,
const Coord&,
bool val,
bool active, AccessorT&);
559 LeafNode* touchLeaf(
const Coord&) {
return this; }
560 template<
typename AccessorT>
561 LeafNode* touchLeafAndCache(
const Coord&, AccessorT&) {
return this; }
562 LeafNode* probeLeaf(
const Coord&) {
return this; }
563 template<
typename AccessorT>
564 LeafNode* probeLeafAndCache(
const Coord&, AccessorT&) {
return this; }
565 template<
typename NodeT,
typename AccessorT>
566 NodeT* probeNodeAndCache(
const Coord&, AccessorT&)
569 if (!(std::is_same<NodeT, LeafNode>::value))
return nullptr;
570 return reinterpret_cast<NodeT*
>(
this);
576 const LeafNode* probeLeaf(
const Coord&)
const {
return this; }
577 template<
typename AccessorT>
578 const LeafNode* probeLeafAndCache(
const Coord&, AccessorT&)
const {
return this; }
579 const LeafNode* probeConstLeaf(
const Coord&)
const {
return this; }
580 template<
typename AccessorT>
581 const LeafNode* probeConstLeafAndCache(
const Coord&, AccessorT&)
const {
return this; }
582 template<
typename NodeT,
typename AccessorT>
583 const NodeT* probeConstNodeAndCache(
const Coord&, AccessorT&)
const
586 if (!(std::is_same<NodeT, LeafNode>::value))
return nullptr;
587 return reinterpret_cast<const NodeT*
>(
this);
596 using MaskOnIter =
typename NodeMaskType::OnIterator;
597 using MaskOffIter =
typename NodeMaskType::OffIterator;
598 using MaskDenseIter =
typename NodeMaskType::DenseIterator;
600 template<
typename MaskIterT,
typename NodeT,
typename ValueT>
604 public SparseIteratorBase<MaskIterT, ValueIter<MaskIterT, NodeT, ValueT>, NodeT, ValueT>
620 template<
typename ModifyOp>
623 template<
typename ModifyOp>
628 template<
typename MaskIterT,
typename NodeT>
637 template<
typename NodeT,
typename ValueT>
639 MaskDenseIter, DenseIter<NodeT, ValueT>, NodeT, void, ValueT>
662 using ValueOnIter = ValueIter<MaskOnIter, LeafNode, const bool>;
663 using ValueOnCIter = ValueIter<MaskOnIter, const LeafNode, const bool>;
664 using ValueOffIter = ValueIter<MaskOffIter, LeafNode, const bool>;
665 using ValueOffCIter = ValueIter<MaskOffIter, const LeafNode, const bool>;
666 using ValueAllIter = ValueIter<MaskDenseIter, LeafNode, const bool>;
667 using ValueAllCIter = ValueIter<MaskDenseIter, const LeafNode, const bool>;
668 using ChildOnIter = ChildIter<MaskOnIter, LeafNode>;
669 using ChildOnCIter = ChildIter<MaskOnIter, const LeafNode>;
670 using ChildOffIter = ChildIter<MaskOffIter, LeafNode>;
671 using ChildOffCIter = ChildIter<MaskOffIter, const LeafNode>;
672 using ChildAllIter = DenseIter<LeafNode, bool>;
673 using ChildAllCIter = DenseIter<const LeafNode, const bool>;
675 ValueOnCIter cbeginValueOn()
const {
return ValueOnCIter(mValueMask.beginOn(),
this); }
676 ValueOnCIter beginValueOn()
const {
return ValueOnCIter(mValueMask.beginOn(),
this); }
677 ValueOnIter beginValueOn() {
return ValueOnIter(mValueMask.beginOn(),
this); }
678 ValueOffCIter cbeginValueOff()
const {
return ValueOffCIter(mValueMask.beginOff(),
this); }
679 ValueOffCIter beginValueOff()
const {
return ValueOffCIter(mValueMask.beginOff(),
this); }
680 ValueOffIter beginValueOff() {
return ValueOffIter(mValueMask.beginOff(),
this); }
681 ValueAllCIter cbeginValueAll()
const {
return ValueAllCIter(mValueMask.beginDense(),
this); }
682 ValueAllCIter beginValueAll()
const {
return ValueAllCIter(mValueMask.beginDense(),
this); }
683 ValueAllIter beginValueAll() {
return ValueAllIter(mValueMask.beginDense(),
this); }
685 ValueOnCIter cendValueOn()
const {
return ValueOnCIter(mValueMask.endOn(),
this); }
686 ValueOnCIter endValueOn()
const {
return ValueOnCIter(mValueMask.endOn(),
this); }
687 ValueOnIter endValueOn() {
return ValueOnIter(mValueMask.endOn(),
this); }
688 ValueOffCIter cendValueOff()
const {
return ValueOffCIter(mValueMask.endOff(),
this); }
689 ValueOffCIter endValueOff()
const {
return ValueOffCIter(mValueMask.endOff(),
this); }
690 ValueOffIter endValueOff() {
return ValueOffIter(mValueMask.endOff(),
this); }
691 ValueAllCIter cendValueAll()
const {
return ValueAllCIter(mValueMask.endDense(),
this); }
692 ValueAllCIter endValueAll()
const {
return ValueAllCIter(mValueMask.endDense(),
this); }
693 ValueAllIter endValueAll() {
return ValueAllIter(mValueMask.endDense(),
this); }
697 ChildOnCIter cbeginChildOn()
const {
return ChildOnCIter(mValueMask.endOn(),
this); }
698 ChildOnCIter beginChildOn()
const {
return ChildOnCIter(mValueMask.endOn(),
this); }
699 ChildOnIter beginChildOn() {
return ChildOnIter(mValueMask.endOn(),
this); }
700 ChildOffCIter cbeginChildOff()
const {
return ChildOffCIter(mValueMask.endOff(),
this); }
701 ChildOffCIter beginChildOff()
const {
return ChildOffCIter(mValueMask.endOff(),
this); }
702 ChildOffIter beginChildOff() {
return ChildOffIter(mValueMask.endOff(),
this); }
703 ChildAllCIter cbeginChildAll()
const {
return ChildAllCIter(mValueMask.beginDense(),
this); }
704 ChildAllCIter beginChildAll()
const {
return ChildAllCIter(mValueMask.beginDense(),
this); }
705 ChildAllIter beginChildAll() {
return ChildAllIter(mValueMask.beginDense(),
this); }
707 ChildOnCIter cendChildOn()
const {
return ChildOnCIter(mValueMask.endOn(),
this); }
708 ChildOnCIter endChildOn()
const {
return ChildOnCIter(mValueMask.endOn(),
this); }
709 ChildOnIter endChildOn() {
return ChildOnIter(mValueMask.endOn(),
this); }
710 ChildOffCIter cendChildOff()
const {
return ChildOffCIter(mValueMask.endOff(),
this); }
711 ChildOffCIter endChildOff()
const {
return ChildOffCIter(mValueMask.endOff(),
this); }
712 ChildOffIter endChildOff() {
return ChildOffIter(mValueMask.endOff(),
this); }
713 ChildAllCIter cendChildAll()
const {
return ChildAllCIter(mValueMask.endDense(),
this); }
714 ChildAllCIter endChildAll()
const {
return ChildAllCIter(mValueMask.endDense(),
this); }
715 ChildAllIter endChildAll() {
return ChildAllIter(mValueMask.endDense(),
this); }
720 bool isValueMaskOn(Index n)
const {
return mValueMask.isOn(n); }
721 bool isValueMaskOn()
const {
return mValueMask.isOn(); }
722 bool isValueMaskOff(Index n)
const {
return mValueMask.isOff(n); }
723 bool isValueMaskOff()
const {
return mValueMask.isOff(); }
724 const NodeMaskType& getValueMask()
const {
return mValueMask; }
725 const NodeMaskType& valueMask()
const {
return mValueMask; }
726 NodeMaskType& getValueMask() {
return mValueMask; }
727 void setValueMask(
const NodeMaskType& mask) { mValueMask = mask; }
728 bool isChildMaskOn(Index)
const {
return false; }
729 bool isChildMaskOff(Index)
const {
return true; }
730 bool isChildMaskOff()
const {
return true; }
732 void setValueMask(Index n,
bool on) { mValueMask.set(n, on); }
733 void setValueMaskOn(Index n) { mValueMask.setOn(n); }
734 void setValueMaskOff(Index n) { mValueMask.setOff(n); }
737 static void evalNodeOrigin(Coord& xyz) { xyz &= ~(DIM - 1); }
740 NodeMaskType mValueMask;
751 template<
typename, Index>
friend class LeafNode;
753 friend struct ValueIter<MaskOnIter, LeafNode, bool>;
754 friend struct ValueIter<MaskOffIter, LeafNode, bool>;
755 friend struct ValueIter<MaskDenseIter, LeafNode, bool>;
756 friend struct ValueIter<MaskOnIter, const LeafNode, bool>;
757 friend struct ValueIter<MaskOffIter, const LeafNode, bool>;
758 friend struct ValueIter<MaskDenseIter, const LeafNode, bool>;
763 friend class IteratorBase<MaskOnIter, LeafNode>;
764 friend class IteratorBase<MaskOffIter, LeafNode>;
765 friend class IteratorBase<MaskDenseIter, LeafNode>;
775template<Index Log2Dim>
777LeafNode<bool, Log2Dim>::LeafNode()
783template<Index Log2Dim>
788 , mOrigin(xyz & (~(DIM - 1)))
793template<Index Log2Dim>
798 , mOrigin(xyz & (~(DIM - 1)))
806template<Index Log2Dim>
809 : mValueMask(other.valueMask())
810 , mBuffer(other.mBuffer)
811 , mOrigin(other.mOrigin)
812 , mTransientData(other.mTransientData)
818template<Index Log2Dim>
819template<
typename ValueT>
822 : mValueMask(other.valueMask())
823 , mOrigin(other.origin())
824 , mTransientData(other.mTransientData)
828 static inline bool convertValue(
const ValueT& val) {
return bool(val); }
832 mBuffer.setValue(i, Local::convertValue(other.mBuffer[i]));
837template<Index Log2Dim>
838template<
typename ValueT>
841 bool background, TopologyCopy)
842 : mValueMask(other.valueMask())
843 , mBuffer(background)
844 , mOrigin(other.origin())
845 , mTransientData(other.mTransientData)
850template<Index Log2Dim>
853 const NodeMaskType& mask,
858 , mOrigin(xyz & (~(DIM - 1)))
859 , mTransientData(trans)
863template<Index Log2Dim>
864template<
typename ValueT>
867 bool offValue,
bool onValue, TopologyCopy)
868 : mValueMask(other.valueMask())
870 , mOrigin(other.origin())
871 , mTransientData(other.mTransientData)
874 if (mValueMask.isOn(i)) {
875 mBuffer.setValue(i, onValue);
881template<Index Log2Dim>
891template<Index Log2Dim>
896 return sizeof(*this);
900template<Index Log2Dim>
904 CoordBBox this_bbox = this->getNodeBoundingBox();
905 if (bbox.isInside(this_bbox))
return;
906 if (ValueOnCIter
iter = this->cbeginValueOn()) {
909 for(;
iter; ++
iter) this_bbox.expand(this->offsetToLocalCoord(
iter.pos()));
910 this_bbox.translate(this->origin());
912 bbox.expand(this_bbox);
917template<Index Log2Dim>
918template<
typename OtherType, Index OtherLog2Dim>
923 return (Log2Dim == OtherLog2Dim && mValueMask == other->getValueMask());
927template<Index Log2Dim>
931 std::ostringstream ostr;
932 ostr <<
"LeafNode @" << mOrigin <<
": ";
933 for (
Index32 n = 0; n < SIZE; ++n) ostr << (mValueMask.isOn(n) ?
'#' :
'.');
941template<Index Log2Dim>
945 OPENVDB_ASSERT((xyz[0] & (DIM-1u)) < DIM && (xyz[1] & (DIM-1u)) < DIM && (xyz[2] & (DIM-1u)) < DIM);
946 return ((xyz[0] & (DIM-1u)) << 2*Log2Dim)
947 + ((xyz[1] & (DIM-1u)) << Log2Dim)
948 + (xyz[2] & (DIM-1u));
952template<Index Log2Dim>
958 xyz.setX(n >> 2*Log2Dim);
959 n &= ((1 << 2*Log2Dim) - 1);
960 xyz.setY(n >> Log2Dim);
961 xyz.setZ(n & ((1 << Log2Dim) - 1));
966template<Index Log2Dim>
970 return (this->offsetToLocalCoord(n) + this->origin());
977#if OPENVDB_ABI_VERSION_NUMBER < 14
979template<Index Log2Dim>
987template<Index Log2Dim>
995template<Index Log2Dim>
1002 this->readBuffers(is, fromHalf);
1005 bool background =
false;
1007 background = *
static_cast<const bool*
>(bgPtr);
1009 this->
clip(clipBBox, background);
1013template<Index Log2Dim>
1020 mValueMask.load(is);
1025 mBuffer.mData.load(is);
1029template<Index Log2Dim>
1034 mValueMask.save(os);
1036 os.write(
reinterpret_cast<const char*
>(&mOrigin),
sizeof(
Coord::ValueType) * 3);
1038 mBuffer.mData.save(os);
1047template<Index Log2Dim>
1051 return mOrigin == other.mOrigin &&
1052 mValueMask == other.valueMask() &&
1053 mBuffer == other.mBuffer;
1057template<Index Log2Dim>
1067template<Index Log2Dim>
1071 if (!mValueMask.isConstant(state))
return false;
1074 if (!tolerance && !(mBuffer.mData.isOn() || mBuffer.mData.isOff()))
return false;
1076 constValue = mBuffer.mData.isOn();
1082template<Index Log2Dim>
1086 const Index countTrue = mBuffer.mData.countOn();
1087 return countTrue > (NUM_VALUES >> 1);
1090template<Index Log2Dim>
1094 const NodeMaskType tmp = mBuffer.mData & mValueMask;
1095 const Index countTrueOn = tmp.countOn(),
countOn = mValueMask.countOn();
1096 state = countTrueOn > (NUM_VALUES >> 1);
1100template<Index Log2Dim>
1104 const NodeMaskType tmp = mBuffer.mData & (!mValueMask);
1105 const Index countTrueOff = tmp.countOn(), countOff = mValueMask.countOff();
1106 state = countTrueOff > (NUM_VALUES >> 1);
1113template<Index Log2Dim>
1117 this->addTile(this->coordToOffset(xyz), val, active);
1120template<Index Log2Dim>
1125 this->setValueOnly(offset, val);
1126 this->setActiveState(offset, active);
1129template<Index Log2Dim>
1130template<
typename AccessorT>
1133 bool val,
bool active, AccessorT&)
1135 this->addTile(level, xyz, val, active);
1142template<Index Log2Dim>
1147 if (mBuffer.mData.isOn(this->coordToOffset(xyz)))
return Buffer::sOn;
else return Buffer::sOff;
1151template<Index Log2Dim>
1157 if (mBuffer.mData.isOn(offset))
return Buffer::sOn;
else return Buffer::sOff;
1161template<Index Log2Dim>
1165 return this->probeValue(this->coordToOffset(xyz), val);
1168template<Index Log2Dim>
1173 val = mBuffer.mData.isOn(offset);
1174 return mValueMask.isOn(offset);
1177template<Index Log2Dim>
1181 this->setValueOn(this->coordToOffset(xyz), val);
1185template<Index Log2Dim>
1190 mValueMask.setOn(offset);
1191 mBuffer.mData.set(offset, val);
1195template<Index Log2Dim>
1199 this->setValueOnly(this->coordToOffset(xyz), val);
1203template<Index Log2Dim>
1207 mValueMask.set(this->coordToOffset(xyz), on);
1211template<Index Log2Dim>
1215 this->setValueOff(this->coordToOffset(xyz), val);
1219template<Index Log2Dim>
1224 mValueMask.setOff(offset);
1225 mBuffer.mData.set(offset, val);
1229template<Index Log2Dim>
1230template<
typename ModifyOp>
1234 bool val = mBuffer.mData.isOn(offset);
1236 mBuffer.mData.set(offset, val);
1237 mValueMask.setOn(offset);
1241template<Index Log2Dim>
1242template<
typename ModifyOp>
1246 this->modifyValue(this->coordToOffset(xyz), op);
1250template<Index Log2Dim>
1251template<
typename ModifyOp>
1255 const Index offset = this->coordToOffset(xyz);
1256 bool val = mBuffer.mData.isOn(offset), state = mValueMask.isOn(offset);
1258 mBuffer.mData.set(offset, val);
1259 mValueMask.set(offset, state);
1266template<Index Log2Dim>
1270 if (newBackground != oldBackground) {
1272 NodeMaskType bgMask = !(mBuffer.mData | mValueMask);
1274 mBuffer.mData = (mBuffer.mData & mValueMask) | bgMask;
1282template<Index Log2Dim>
1283template<MergePolicy Policy>
1289 for (
typename NodeMaskType::OnIterator
iter = other.valueMask().beginOn();
iter; ++
iter) {
1291 if (mValueMask.isOff(n)) {
1292 mBuffer.mData.set(n, other.mBuffer.mData.isOn(n));
1293 mValueMask.setOn(n);
1299template<Index Log2Dim>
1300template<MergePolicy Policy>
1306 if (!tileActive)
return;
1308 if (tileValue) mBuffer.mData |= !mValueMask;
1309 else mBuffer.mData &= mValueMask;
1318template<Index Log2Dim>
1319template<
typename OtherType>
1323 mValueMask |= other.valueMask();
1327template<Index Log2Dim>
1328template<
typename OtherType>
1333 mValueMask &= other.valueMask();
1337template<Index Log2Dim>
1338template<
typename OtherType>
1343 mValueMask &= !other.valueMask();
1350template<Index Log2Dim>
1354 CoordBBox nodeBBox = this->getNodeBoundingBox();
1355 if (!clipBBox.hasOverlap(nodeBBox)) {
1357 this->fill(nodeBBox, background,
false);
1358 }
else if (clipBBox.isInside(nodeBBox)) {
1368 nodeBBox.intersect(clipBBox);
1370 int &x = xyz.x(), &y = xyz.y(), &z = xyz.z();
1371 for (x = nodeBBox.min().x(); x <= nodeBBox.max().x(); ++x) {
1372 for (y = nodeBBox.min().y(); y <= nodeBBox.max().y(); ++y) {
1373 for (z = nodeBBox.min().z(); z <= nodeBBox.max().z(); ++z) {
1374 mask.setOn(
static_cast<Index32>(this->coordToOffset(xyz)));
1381 for (MaskOffIter maskIter = mask.beginOff(); maskIter; ++maskIter) {
1382 this->setValueOff(maskIter.pos(), background);
1390template<Index Log2Dim>
1394 auto clippedBBox = this->getNodeBoundingBox();
1395 clippedBBox.intersect(bbox);
1396 if (!clippedBBox)
return;
1398 for (
Int32 x = clippedBBox.min().x(); x <= clippedBBox.max().x(); ++x) {
1399 const Index offsetX = (x & (DIM-1u))<<2*Log2Dim;
1400 for (
Int32 y = clippedBBox.min().y(); y <= clippedBBox.max().y(); ++y) {
1401 const Index offsetXY = offsetX + ((y & (DIM-1u))<< Log2Dim);
1402 for (
Int32 z = clippedBBox.min().z(); z <= clippedBBox.max().z(); ++z) {
1403 const Index offset = offsetXY + (z & (DIM-1u));
1404 mValueMask.set(offset, active);
1405 mBuffer.mData.set(offset, value);
1411template<Index Log2Dim>
1415 mBuffer.fill(value);
1418template<Index Log2Dim>
1422 mBuffer.fill(value);
1423 mValueMask.set(active);
1430template<Index Log2Dim>
1431template<
typename DenseT>
1435 using DenseValueType =
typename DenseT::ValueType;
1437 const size_t xStride = dense.xStride(), yStride = dense.yStride(), zStride = dense.zStride();
1438 const Coord&
min = dense.bbox().min();
1439 DenseValueType* t0 = dense.data() + zStride * (bbox.min()[2] - min[2]);
1440 const Int32 n0 = bbox.min()[2] & (DIM-1u);
1441 for (
Int32 x = bbox.min()[0], ex = bbox.max()[0] + 1; x < ex; ++x) {
1442 DenseValueType* t1 = t0 + xStride * (x -
min[0]);
1443 const Int32 n1 = n0 + ((x & (DIM-1u)) << 2*LOG2DIM);
1444 for (
Int32 y = bbox.min()[1], ey = bbox.max()[1] + 1; y < ey; ++y) {
1445 DenseValueType* t2 = t1 + yStride * (y -
min[1]);
1446 Int32 n2 = n1 + ((y & (DIM-1u)) << LOG2DIM);
1447 for (
Int32 z = bbox.min()[2], ez = bbox.max()[2] + 1; z < ez; ++z, t2 += zStride) {
1448 *t2 = DenseValueType(mBuffer.mData.isOn(n2++));
1455template<Index Log2Dim>
1456template<
typename DenseT>
1459 bool background,
bool tolerance)
1461 using DenseValueType =
typename DenseT::ValueType;
1463 inline static bool toBool(
const DenseValueType& v) {
return !math::isZero(v); }
1466 const size_t xStride = dense.xStride(), yStride = dense.yStride(), zStride = dense.zStride();
1467 const Coord&
min = dense.bbox().min();
1468 const DenseValueType* s0 = dense.data() + zStride * (bbox.min()[2] - min[2]);
1469 const Int32 n0 = bbox.min()[2] & (DIM-1u);
1470 for (
Int32 x = bbox.min()[0], ex = bbox.max()[0] + 1; x < ex; ++x) {
1471 const DenseValueType* s1 = s0 + xStride * (x -
min[0]);
1472 const Int32 n1 = n0 + ((x & (DIM-1u)) << 2*LOG2DIM);
1473 for (
Int32 y = bbox.min()[1], ey = bbox.max()[1] + 1; y < ey; ++y) {
1474 const DenseValueType* s2 = s1 + yStride * (y -
min[1]);
1475 Int32 n2 = n1 + ((y & (DIM-1u)) << LOG2DIM);
1476 for (
Int32 z = bbox.min()[2], ez = bbox.max()[2]+1; z < ez; ++z, ++n2, s2 += zStride) {
1478 if (tolerance || (background == Local::toBool(*s2))) {
1479 mValueMask.setOff(n2);
1480 mBuffer.mData.set(n2, background);
1482 mValueMask.setOn(n2);
1483 mBuffer.mData.set(n2, Local::toBool(*s2));
1494template<Index Log2Dim>
1495template<
typename CombineOp>
1499 CombineArgs<bool> args;
1500 for (
Index i = 0; i < SIZE; ++i) {
1501 bool result =
false, aVal = mBuffer.mData.isOn(i), bVal = other.mBuffer.mData.isOn(i);
1502 op(args.setARef(aVal)
1503 .setAIsActive(mValueMask.isOn(i))
1505 .setBIsActive(other.valueMask().isOn(i))
1506 .setResultRef(result));
1507 mValueMask.set(i, args.resultIsActive());
1508 mBuffer.mData.set(i, result);
1513template<Index Log2Dim>
1514template<
typename CombineOp>
1518 CombineArgs<bool> args;
1519 args.setBRef(value).setBIsActive(valueIsActive);
1520 for (
Index i = 0; i < SIZE; ++i) {
1521 bool result =
false, aVal = mBuffer.mData.isOn(i);
1522 op(args.setARef(aVal)
1523 .setAIsActive(mValueMask.isOn(i))
1524 .setResultRef(result));
1525 mValueMask.set(i, args.resultIsActive());
1526 mBuffer.mData.set(i, result);
1534template<Index Log2Dim>
1535template<
typename CombineOp,
typename OtherType>
1538 bool valueIsActive, CombineOp& op)
1540 CombineArgs<bool, OtherType> args;
1541 args.setBRef(value).setBIsActive(valueIsActive);
1542 for (
Index i = 0; i < SIZE; ++i) {
1543 bool result =
false, aVal = other.mBuffer.mData.isOn(i);
1544 op(args.setARef(aVal)
1545 .setAIsActive(other.valueMask().isOn(i))
1546 .setResultRef(result));
1547 mValueMask.set(i, args.resultIsActive());
1548 mBuffer.mData.set(i, result);
1553template<Index Log2Dim>
1554template<
typename CombineOp,
typename OtherNodeT>
1557 bool valueIsActive, CombineOp& op)
1559 CombineArgs<bool, typename OtherNodeT::ValueType> args;
1560 args.setARef(value).setAIsActive(valueIsActive);
1561 for (
Index i = 0; i < SIZE; ++i) {
1562 bool result =
false, bVal = other.mBuffer.mData.isOn(i);
1563 op(args.setBRef(bVal)
1564 .setBIsActive(other.valueMask().isOn(i))
1565 .setResultRef(result));
1566 mValueMask.set(i, args.resultIsActive());
1567 mBuffer.mData.set(i, result);
1572template<Index Log2Dim>
1573template<
typename CombineOp,
typename OtherNodeT>
1577 CombineArgs<bool, typename OtherNodeT::ValueType> args;
1578 for (
Index i = 0; i < SIZE; ++i) {
1580 mValueMask.set(i, b0.valueMask().isOn(i) || b1.valueMask().isOn(i));
1582 bool result =
false, b0Val = b0.mBuffer.mData.isOn(i), b1Val = b1.mBuffer.mData.isOn(i);
1583 op(args.setARef(b0Val)
1584 .setAIsActive(b0.valueMask().isOn(i))
1586 .setBIsActive(b1.valueMask().isOn(i))
1587 .setResultRef(result));
1588 mValueMask.set(i, args.resultIsActive());
1589 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 resetBackground(const ValueType &oldBackground, const ValueType &newBackground)
Replace inactive occurrences of oldBackground with newBackground, and inactive occurrences of -oldBac...
Definition LeafNode.h:1639
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
static const Index SIZE
Definition LeafNode.h:54
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
uint32_t countOn(uint64_t v)
Definition Util.h:699
math::BBox< Coord > CoordBBox
Definition Math.h:2838
void checkFormatVersion(std::ios_base &)
Throws an IoError if the file format version number is not supported.
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 LeafNodeBool.h:633
ChildIter()
Definition LeafNodeBool.h:632
bool getItem(Index pos, void *&child, NonConstValueT &value) const
Definition LeafNodeBool.h:647
DenseIter()
Definition LeafNodeBool.h:644
DenseIter(const MaskDenseIter &iter, NodeT *parent)
Definition LeafNodeBool.h:645
typename BaseT::NonConstValueType NonConstValueT
Definition LeafNodeBool.h:642
DenseIteratorBase< MaskDenseIter, DenseIter, NodeT, void, ValueT > BaseT
Definition LeafNodeBool.h:641
void unsetItem(Index pos, const ValueT &val) const
Definition LeafNodeBool.h:658
SameConfiguration<OtherNodeType>::value is true if and only if OtherNodeType is the type of a LeafNod...
Definition LeafNodeBool.h:56
static const bool value
Definition LeafNodeBool.h:57
ValueConverter<T>::Type is the type of a LeafNode having the same dimensions as this node but a diffe...
Definition LeafNodeBool.h:51
LeafNode< ValueType, Log2Dim > Type
Definition LeafNodeBool.h:51
void setItem(Index pos, bool value) const
Definition LeafNodeBool.h:615
void modifyValue(const ModifyOp &op) const
Definition LeafNodeBool.h:624
ValueIter(const MaskIterT &iter, NodeT *parent)
Definition LeafNodeBool.h:609
const bool & getValue() const
Definition LeafNodeBool.h:612
const bool & getItem(Index pos) const
Definition LeafNodeBool.h:611
ValueIter()
Definition LeafNodeBool.h:608
void setValue(bool value) const
Definition LeafNodeBool.h:617
void modifyItem(Index n, const ModifyOp &op) const
Definition LeafNodeBool.h:621
SparseIteratorBase< MaskIterT, ValueIter, NodeT, ValueT > BaseT
Definition LeafNodeBool.h:606
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