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 {}
121 void nodeCount(std::vector<Index32> &)
const {}
122 static Index64 nonLeafCount() {
return 0; }
125 Index64 onVoxelCount()
const {
return mValueMask.countOn(); }
127 Index64 offVoxelCount()
const {
return mValueMask.countOff(); }
128 Index64 onLeafVoxelCount()
const {
return onVoxelCount(); }
129 Index64 offLeafVoxelCount()
const {
return offVoxelCount(); }
130 static Index64 onTileCount() {
return 0; }
131 static Index64 offTileCount() {
return 0; }
134 bool isEmpty()
const {
return mValueMask.isOff(); }
136 bool isDense()
const {
return mValueMask.isOn(); }
140 bool isAllocated()
const {
return true; }
144 bool allocate() {
return true; }
153 void evalActiveBoundingBox(CoordBBox& bbox,
bool visitVoxels =
true)
const;
157 CoordBBox getNodeBoundingBox()
const {
return CoordBBox::createCube(mOrigin, DIM); }
160 void setOrigin(
const Coord& origin) { mOrigin = origin; }
163 const Coord& origin()
const {
return mOrigin; }
164 void getOrigin(Coord& origin)
const { origin = mOrigin; }
165 void getOrigin(Int32& x, Int32& y, Int32& z)
const { mOrigin.asXYZ(x, y, z); }
169 static Index coordToOffset(
const Coord& xyz);
172 static Coord offsetToLocalCoord(Index n);
174 Coord offsetToGlobalCoord(Index n)
const;
177 Index32 transientData()
const {
return mTransientData; }
179 void setTransientData(Index32 transientData) { mTransientData = transientData; }
182 std::string str()
const;
186 template<
typename OtherType, Index OtherLog2Dim>
187 bool hasSameTopology(
const LeafNode<OtherType, OtherLog2Dim>* other)
const;
198 void swap(Buffer& other) { mBuffer.swap(other); }
199 const Buffer& buffer()
const {
return mBuffer; }
200 Buffer& buffer() {
return mBuffer; }
206 void readTopology(std::istream&,
bool fromHalf =
false);
208 void writeTopology(std::ostream&,
bool toHalf =
false)
const;
211 void readBuffers(std::istream&,
bool fromHalf =
false);
212 void readBuffers(std::istream& is,
const CoordBBox&,
bool fromHalf =
false);
214 void writeBuffers(std::ostream&,
bool toHalf =
false)
const;
220 const bool& getValue(
const Coord& xyz)
const;
222 const bool& getValue(Index offset)
const;
227 bool probeValue(
const Coord& xyz,
bool& val)
const;
231 bool probeValue(Index offset,
bool& val)
const;
234 static Index getValueLevel(
const Coord&) {
return LEVEL; }
237 void setActiveState(
const Coord& xyz,
bool on);
239 void setActiveState(Index offset,
bool on) {
OPENVDB_ASSERT(offset<SIZE); mValueMask.set(offset, on); }
242 void setValueOnly(
const Coord& xyz,
bool val);
244 void setValueOnly(Index offset,
bool val) {
OPENVDB_ASSERT(offset<SIZE); mBuffer.setValue(offset,val); }
247 void setValueOff(
const Coord& xyz) { mValueMask.setOff(this->coordToOffset(xyz)); }
249 void setValueOff(Index offset) {
OPENVDB_ASSERT(offset < SIZE); mValueMask.setOff(offset); }
252 void setValueOff(
const Coord& xyz,
bool val);
254 void setValueOff(Index offset,
bool val);
257 void setValueOn(
const Coord& xyz) { mValueMask.setOn(this->coordToOffset(xyz)); }
259 void setValueOn(Index offset) {
OPENVDB_ASSERT(offset < SIZE); mValueMask.setOn(offset); }
262 void setValueOn(
const Coord& xyz,
bool val);
264 void setValue(
const Coord& xyz,
bool val) { this->setValueOn(xyz, val); }
266 void setValueOn(Index offset,
bool val);
270 template<
typename ModifyOp>
271 void modifyValue(Index offset,
const ModifyOp& op);
274 template<
typename ModifyOp>
275 void modifyValue(
const Coord& xyz,
const ModifyOp& op);
278 template<
typename ModifyOp>
279 void modifyValueAndActiveState(
const Coord& xyz,
const ModifyOp& op);
282 void setValuesOn() { mValueMask.setOn(); }
284 void setValuesOff() { mValueMask.setOff(); }
287 bool isValueOn(
const Coord& xyz)
const {
return this->isValueOn(this->coordToOffset(xyz)); }
289 bool isValueOn(Index offset)
const {
OPENVDB_ASSERT(offset < SIZE);
return mValueMask.isOn(offset); }
291 bool isValueOff(
const Coord& xyz)
const {
return this->isValueOff(this->coordToOffset(xyz)); }
293 bool isValueOff(Index offset)
const {
OPENVDB_ASSERT(offset < SIZE);
return mValueMask.isOff(offset); }
296 static bool hasActiveTiles() {
return false; }
299 void clip(
const CoordBBox&,
bool background);
302 void fill(
const CoordBBox& bbox,
bool value,
bool active =
true);
304 void denseFill(
const CoordBBox& bbox,
bool val,
bool on =
true) { this->fill(bbox, val, on); }
307 void fill(
const bool& value);
309 void fill(
const bool& value,
bool active);
322 template<
typename DenseT>
323 void copyToDense(
const CoordBBox& bbox, DenseT& dense)
const;
341 template<
typename DenseT>
342 void copyFromDense(
const CoordBBox& bbox,
const DenseT& dense,
bool background,
bool tolerance);
346 template<
typename AccessorT>
347 const bool& getValueAndCache(
const Coord& xyz, AccessorT&)
const {
return this->getValue(xyz);}
351 template<
typename AccessorT>
352 bool isValueOnAndCache(
const Coord& xyz, AccessorT&)
const {
return this->isValueOn(xyz); }
356 template<
typename AccessorT>
357 void setValueAndCache(
const Coord& xyz,
bool val, AccessorT&) { this->setValueOn(xyz, val); }
362 template<
typename AccessorT>
363 void setValueOnlyAndCache(
const Coord& xyz,
bool val, AccessorT&) {this->setValueOnly(xyz,val);}
367 template<
typename AccessorT>
368 void setValueOffAndCache(
const Coord& xyz,
bool value, AccessorT&)
370 this->setValueOff(xyz, value);
376 template<
typename ModifyOp,
typename AccessorT>
377 void modifyValueAndCache(
const Coord& xyz,
const ModifyOp& op, AccessorT&)
379 this->modifyValue(xyz, op);
384 template<
typename ModifyOp,
typename AccessorT>
385 void modifyValueAndActiveStateAndCache(
const Coord& xyz,
const ModifyOp& op, AccessorT&)
387 this->modifyValueAndActiveState(xyz, op);
393 template<
typename AccessorT>
394 void setActiveStateAndCache(
const Coord& xyz,
bool on, AccessorT&)
396 this->setActiveState(xyz, on);
402 template<
typename AccessorT>
403 bool probeValueAndCache(
const Coord& xyz,
bool& val, AccessorT&)
const
405 return this->probeValue(xyz, val);
410 template<
typename AccessorT>
411 static Index getValueLevelAndCache(
const Coord&, AccessorT&) {
return LEVEL; }
416 const bool& getFirstValue()
const {
if (mValueMask.isOn(0))
return Buffer::sOn;
else return Buffer::sOff; }
420 const bool& getLastValue()
const {
if (mValueMask.isOn(SIZE-1))
return Buffer::sOn;
else return Buffer::sOff; }
425 bool isConstant(
bool& constValue,
bool& state,
bool tolerance = 0)
const;
432 bool medianAll()
const;
440 Index medianOn(ValueType &value)
const;
448 Index medianOff(ValueType &value)
const;
451 bool isInactive()
const {
return mValueMask.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); }
476 void resetBackground(
bool oldBackground,
bool newBackground);
478 void negate() { mBuffer.mData.toggle(); }
480 template<MergePolicy Policy>
481 void merge(
const LeafNode& other,
bool bg =
false,
bool otherBG =
false);
482 template<MergePolicy Policy>
void merge(
bool tileValue,
bool tileActive);
486 void voxelizeActiveTiles(
bool =
true) {}
494 template<
typename OtherType>
495 void topologyUnion(
const LeafNode<OtherType, Log2Dim>& other,
const bool preserveTiles =
false);
508 template<
typename OtherType>
509 void topologyIntersection(
const LeafNode<OtherType, Log2Dim>& other,
const bool&);
522 template<
typename OtherType>
523 void topologyDifference(
const LeafNode<OtherType, Log2Dim>& other,
const bool&);
525 template<
typename CombineOp>
526 void combine(
const LeafNode& other, CombineOp& op);
527 template<
typename CombineOp>
528 void combine(
bool,
bool valueIsActive, CombineOp& op);
530 template<
typename CombineOp,
typename OtherType >
531 void combine2(
const LeafNode& other,
const OtherType&,
bool valueIsActive, CombineOp&);
532 template<
typename CombineOp,
typename OtherNodeT >
533 void combine2(
bool,
const OtherNodeT& other,
bool valueIsActive, CombineOp&);
534 template<
typename CombineOp,
typename OtherNodeT >
535 void combine2(
const LeafNode& b0,
const OtherNodeT& b1, CombineOp&);
539 void prune(
const ValueType& = zeroVal<ValueType>()) {}
540 void addLeaf(LeafNode*) {}
541 template<
typename AccessorT>
542 void addLeafAndCache(LeafNode*, AccessorT&) {}
543 template<
typename NodeT>
544 NodeT* stealNode(
const Coord&,
const ValueType&,
bool) {
return nullptr; }
545 template<
typename NodeT>
546 NodeT* probeNode(
const Coord&) {
return nullptr; }
547 template<
typename NodeT>
548 const NodeT* probeConstNode(
const Coord&)
const {
return nullptr; }
549 template<
typename ArrayT>
void getNodes(ArrayT&)
const {}
550 template<
typename ArrayT>
void stealNodes(ArrayT&,
const ValueType&,
bool) {}
553 void addTile(Index level,
const Coord&,
bool val,
bool active);
554 void addTile(Index offset,
bool val,
bool active);
555 template<
typename AccessorT>
556 void addTileAndCache(Index level,
const Coord&,
bool val,
bool active, AccessorT&);
560 LeafNode* touchLeaf(
const Coord&) {
return this; }
561 template<
typename AccessorT>
562 LeafNode* touchLeafAndCache(
const Coord&, AccessorT&) {
return this; }
563 LeafNode* probeLeaf(
const Coord&) {
return this; }
564 template<
typename AccessorT>
565 LeafNode* probeLeafAndCache(
const Coord&, AccessorT&) {
return this; }
566 template<
typename NodeT,
typename AccessorT>
567 NodeT* probeNodeAndCache(
const Coord&, AccessorT&)
570 if (!(std::is_same<NodeT, LeafNode>::value))
return nullptr;
571 return reinterpret_cast<NodeT*
>(
this);
577 const LeafNode* probeLeaf(
const Coord&)
const {
return this; }
578 template<
typename AccessorT>
579 const LeafNode* probeLeafAndCache(
const Coord&, AccessorT&)
const {
return this; }
580 const LeafNode* probeConstLeaf(
const Coord&)
const {
return this; }
581 template<
typename AccessorT>
582 const LeafNode* probeConstLeafAndCache(
const Coord&, AccessorT&)
const {
return this; }
583 template<
typename NodeT,
typename AccessorT>
584 const NodeT* probeConstNodeAndCache(
const Coord&, AccessorT&)
const
587 if (!(std::is_same<NodeT, LeafNode>::value))
return nullptr;
588 return reinterpret_cast<const NodeT*
>(
this);
597 using MaskOnIter =
typename NodeMaskType::OnIterator;
598 using MaskOffIter =
typename NodeMaskType::OffIterator;
599 using MaskDenseIter =
typename NodeMaskType::DenseIterator;
601 template<
typename MaskIterT,
typename NodeT,
typename ValueT>
605 public SparseIteratorBase<MaskIterT, ValueIter<MaskIterT, NodeT, ValueT>, NodeT, ValueT>
621 template<
typename ModifyOp>
624 template<
typename ModifyOp>
629 template<
typename MaskIterT,
typename NodeT>
638 template<
typename NodeT,
typename ValueT>
640 MaskDenseIter, DenseIter<NodeT, ValueT>, NodeT, void, ValueT>
663 using ValueOnIter = ValueIter<MaskOnIter, LeafNode, const bool>;
664 using ValueOnCIter = ValueIter<MaskOnIter, const LeafNode, const bool>;
665 using ValueOffIter = ValueIter<MaskOffIter, LeafNode, const bool>;
666 using ValueOffCIter = ValueIter<MaskOffIter, const LeafNode, const bool>;
667 using ValueAllIter = ValueIter<MaskDenseIter, LeafNode, const bool>;
668 using ValueAllCIter = ValueIter<MaskDenseIter, const LeafNode, const bool>;
669 using ChildOnIter = ChildIter<MaskOnIter, LeafNode>;
670 using ChildOnCIter = ChildIter<MaskOnIter, const LeafNode>;
671 using ChildOffIter = ChildIter<MaskOffIter, LeafNode>;
672 using ChildOffCIter = ChildIter<MaskOffIter, const LeafNode>;
673 using ChildAllIter = DenseIter<LeafNode, bool>;
674 using ChildAllCIter = DenseIter<const LeafNode, const bool>;
676 ValueOnCIter cbeginValueOn()
const {
return ValueOnCIter(mValueMask.beginOn(),
this); }
677 ValueOnCIter beginValueOn()
const {
return ValueOnCIter(mValueMask.beginOn(),
this); }
678 ValueOnIter beginValueOn() {
return ValueOnIter(mValueMask.beginOn(),
this); }
679 ValueOffCIter cbeginValueOff()
const {
return ValueOffCIter(mValueMask.beginOff(),
this); }
680 ValueOffCIter beginValueOff()
const {
return ValueOffCIter(mValueMask.beginOff(),
this); }
681 ValueOffIter beginValueOff() {
return ValueOffIter(mValueMask.beginOff(),
this); }
682 ValueAllCIter cbeginValueAll()
const {
return ValueAllCIter(mValueMask.beginDense(),
this); }
683 ValueAllCIter beginValueAll()
const {
return ValueAllCIter(mValueMask.beginDense(),
this); }
684 ValueAllIter beginValueAll() {
return ValueAllIter(mValueMask.beginDense(),
this); }
686 ValueOnCIter cendValueOn()
const {
return ValueOnCIter(mValueMask.endOn(),
this); }
687 ValueOnCIter endValueOn()
const {
return ValueOnCIter(mValueMask.endOn(),
this); }
688 ValueOnIter endValueOn() {
return ValueOnIter(mValueMask.endOn(),
this); }
689 ValueOffCIter cendValueOff()
const {
return ValueOffCIter(mValueMask.endOff(),
this); }
690 ValueOffCIter endValueOff()
const {
return ValueOffCIter(mValueMask.endOff(),
this); }
691 ValueOffIter endValueOff() {
return ValueOffIter(mValueMask.endOff(),
this); }
692 ValueAllCIter cendValueAll()
const {
return ValueAllCIter(mValueMask.endDense(),
this); }
693 ValueAllCIter endValueAll()
const {
return ValueAllCIter(mValueMask.endDense(),
this); }
694 ValueAllIter endValueAll() {
return ValueAllIter(mValueMask.endDense(),
this); }
698 ChildOnCIter cbeginChildOn()
const {
return ChildOnCIter(mValueMask.endOn(),
this); }
699 ChildOnCIter beginChildOn()
const {
return ChildOnCIter(mValueMask.endOn(),
this); }
700 ChildOnIter beginChildOn() {
return ChildOnIter(mValueMask.endOn(),
this); }
701 ChildOffCIter cbeginChildOff()
const {
return ChildOffCIter(mValueMask.endOff(),
this); }
702 ChildOffCIter beginChildOff()
const {
return ChildOffCIter(mValueMask.endOff(),
this); }
703 ChildOffIter beginChildOff() {
return ChildOffIter(mValueMask.endOff(),
this); }
704 ChildAllCIter cbeginChildAll()
const {
return ChildAllCIter(mValueMask.beginDense(),
this); }
705 ChildAllCIter beginChildAll()
const {
return ChildAllCIter(mValueMask.beginDense(),
this); }
706 ChildAllIter beginChildAll() {
return ChildAllIter(mValueMask.beginDense(),
this); }
708 ChildOnCIter cendChildOn()
const {
return ChildOnCIter(mValueMask.endOn(),
this); }
709 ChildOnCIter endChildOn()
const {
return ChildOnCIter(mValueMask.endOn(),
this); }
710 ChildOnIter endChildOn() {
return ChildOnIter(mValueMask.endOn(),
this); }
711 ChildOffCIter cendChildOff()
const {
return ChildOffCIter(mValueMask.endOff(),
this); }
712 ChildOffCIter endChildOff()
const {
return ChildOffCIter(mValueMask.endOff(),
this); }
713 ChildOffIter endChildOff() {
return ChildOffIter(mValueMask.endOff(),
this); }
714 ChildAllCIter cendChildAll()
const {
return ChildAllCIter(mValueMask.endDense(),
this); }
715 ChildAllCIter endChildAll()
const {
return ChildAllCIter(mValueMask.endDense(),
this); }
716 ChildAllIter endChildAll() {
return ChildAllIter(mValueMask.endDense(),
this); }
721 bool isValueMaskOn(Index n)
const {
return mValueMask.isOn(n); }
722 bool isValueMaskOn()
const {
return mValueMask.isOn(); }
723 bool isValueMaskOff(Index n)
const {
return mValueMask.isOff(n); }
724 bool isValueMaskOff()
const {
return mValueMask.isOff(); }
725 const NodeMaskType& getValueMask()
const {
return mValueMask; }
726 const NodeMaskType& valueMask()
const {
return mValueMask; }
727 NodeMaskType& getValueMask() {
return mValueMask; }
728 void setValueMask(
const NodeMaskType& mask) { mValueMask = mask; }
729 bool isChildMaskOn(Index)
const {
return false; }
730 bool isChildMaskOff(Index)
const {
return true; }
731 bool isChildMaskOff()
const {
return true; }
733 void setValueMask(Index n,
bool on) { mValueMask.set(n, on); }
734 void setValueMaskOn(Index n) { mValueMask.setOn(n); }
735 void setValueMaskOff(Index n) { mValueMask.setOff(n); }
738 static void evalNodeOrigin(Coord& xyz) { xyz &= ~(DIM - 1); }
741 NodeMaskType mValueMask;
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>;
776template<Index Log2Dim>
778LeafNode<bool, Log2Dim>::LeafNode()
784template<Index Log2Dim>
789 , mOrigin(xyz & (~(DIM - 1)))
794template<Index Log2Dim>
799 , mOrigin(xyz & (~(DIM - 1)))
807template<Index Log2Dim>
810 : mValueMask(other.valueMask())
811 , mBuffer(other.mBuffer)
812 , mOrigin(other.mOrigin)
813 , mTransientData(other.mTransientData)
819template<Index Log2Dim>
820template<
typename ValueT>
823 : mValueMask(other.valueMask())
824 , mOrigin(other.origin())
825 , mTransientData(other.mTransientData)
829 static inline bool convertValue(
const ValueT& val) {
return bool(val); }
833 mBuffer.setValue(i, Local::convertValue(other.mBuffer[i]));
838template<Index Log2Dim>
839template<
typename ValueT>
842 bool background, TopologyCopy)
843 : mValueMask(other.valueMask())
844 , mBuffer(background)
845 , mOrigin(other.origin())
846 , mTransientData(other.mTransientData)
851template<Index Log2Dim>
854 const NodeMaskType& mask,
859 , mOrigin(xyz & (~(DIM - 1)))
860 , mTransientData(trans)
864template<Index Log2Dim>
865template<
typename ValueT>
868 bool offValue,
bool onValue, TopologyCopy)
869 : mValueMask(other.valueMask())
871 , mOrigin(other.origin())
872 , mTransientData(other.mTransientData)
875 if (mValueMask.isOn(i)) {
876 mBuffer.setValue(i, onValue);
882template<Index Log2Dim>
892template<Index Log2Dim>
897 return sizeof(*this);
901template<Index Log2Dim>
906 return sizeof(*this);
910template<Index Log2Dim>
914 CoordBBox this_bbox = this->getNodeBoundingBox();
915 if (bbox.isInside(this_bbox))
return;
916 if (ValueOnCIter
iter = this->cbeginValueOn()) {
919 for(;
iter; ++
iter) this_bbox.expand(this->offsetToLocalCoord(
iter.pos()));
920 this_bbox.translate(this->origin());
922 bbox.expand(this_bbox);
927template<Index Log2Dim>
928template<
typename OtherType, Index OtherLog2Dim>
933 return (Log2Dim == OtherLog2Dim && mValueMask == other->getValueMask());
937template<Index Log2Dim>
941 std::ostringstream ostr;
942 ostr <<
"LeafNode @" << mOrigin <<
": ";
943 for (
Index32 n = 0; n < SIZE; ++n) ostr << (mValueMask.isOn(n) ?
'#' :
'.');
951template<Index Log2Dim>
955 OPENVDB_ASSERT((xyz[0] & (DIM-1u)) < DIM && (xyz[1] & (DIM-1u)) < DIM && (xyz[2] & (DIM-1u)) < DIM);
956 return ((xyz[0] & (DIM-1u)) << 2*Log2Dim)
957 + ((xyz[1] & (DIM-1u)) << Log2Dim)
958 + (xyz[2] & (DIM-1u));
962template<Index Log2Dim>
968 xyz.setX(n >> 2*Log2Dim);
969 n &= ((1 << 2*Log2Dim) - 1);
970 xyz.setY(n >> Log2Dim);
971 xyz.setZ(n & ((1 << Log2Dim) - 1));
976template<Index Log2Dim>
980 return (this->offsetToLocalCoord(n) + this->origin());
987template<Index Log2Dim>
995template<Index Log2Dim>
1003template<Index Log2Dim>
1010 this->readBuffers(is, fromHalf);
1013 bool background =
false;
1015 background = *
static_cast<const bool*
>(bgPtr);
1017 this->
clip(clipBBox, background);
1021template<Index Log2Dim>
1028 mValueMask.load(is);
1033 mBuffer.mData.load(is);
1037template<Index Log2Dim>
1042 mValueMask.save(os);
1044 os.write(
reinterpret_cast<const char*
>(&mOrigin),
sizeof(
Coord::ValueType) * 3);
1046 mBuffer.mData.save(os);
1053template<Index Log2Dim>
1057 return mOrigin == other.mOrigin &&
1058 mValueMask == other.valueMask() &&
1059 mBuffer == other.mBuffer;
1063template<Index Log2Dim>
1073template<Index Log2Dim>
1077 if (!mValueMask.isConstant(state))
return false;
1080 if (!tolerance && !(mBuffer.mData.isOn() || mBuffer.mData.isOff()))
return false;
1082 constValue = mBuffer.mData.isOn();
1088template<Index Log2Dim>
1092 const Index countTrue = mBuffer.mData.countOn();
1093 return countTrue > (NUM_VALUES >> 1);
1096template<Index Log2Dim>
1100 const NodeMaskType tmp = mBuffer.mData & mValueMask;
1101 const Index countTrueOn = tmp.countOn(),
countOn = mValueMask.countOn();
1102 state = countTrueOn > (NUM_VALUES >> 1);
1106template<Index Log2Dim>
1110 const NodeMaskType tmp = mBuffer.mData & (!mValueMask);
1111 const Index countTrueOff = tmp.countOn(), countOff = mValueMask.countOff();
1112 state = countTrueOff > (NUM_VALUES >> 1);
1119template<Index Log2Dim>
1123 this->addTile(this->coordToOffset(xyz), val, active);
1126template<Index Log2Dim>
1131 this->setValueOnly(offset, val);
1132 this->setActiveState(offset, active);
1135template<Index Log2Dim>
1136template<
typename AccessorT>
1139 bool val,
bool active, AccessorT&)
1141 this->addTile(level, xyz, val, active);
1148template<Index Log2Dim>
1153 if (mBuffer.mData.isOn(this->coordToOffset(xyz)))
return Buffer::sOn;
else return Buffer::sOff;
1157template<Index Log2Dim>
1163 if (mBuffer.mData.isOn(offset))
return Buffer::sOn;
else return Buffer::sOff;
1167template<Index Log2Dim>
1171 return this->probeValue(this->coordToOffset(xyz), val);
1174template<Index Log2Dim>
1179 val = mBuffer.mData.isOn(offset);
1180 return mValueMask.isOn(offset);
1183template<Index Log2Dim>
1187 this->setValueOn(this->coordToOffset(xyz), val);
1191template<Index Log2Dim>
1196 mValueMask.setOn(offset);
1197 mBuffer.mData.set(offset, val);
1201template<Index Log2Dim>
1205 this->setValueOnly(this->coordToOffset(xyz), val);
1209template<Index Log2Dim>
1213 mValueMask.set(this->coordToOffset(xyz), on);
1217template<Index Log2Dim>
1221 this->setValueOff(this->coordToOffset(xyz), val);
1225template<Index Log2Dim>
1230 mValueMask.setOff(offset);
1231 mBuffer.mData.set(offset, val);
1235template<Index Log2Dim>
1236template<
typename ModifyOp>
1240 bool val = mBuffer.mData.isOn(offset);
1242 mBuffer.mData.set(offset, val);
1243 mValueMask.setOn(offset);
1247template<Index Log2Dim>
1248template<
typename ModifyOp>
1252 this->modifyValue(this->coordToOffset(xyz), op);
1256template<Index Log2Dim>
1257template<
typename ModifyOp>
1261 const Index offset = this->coordToOffset(xyz);
1262 bool val = mBuffer.mData.isOn(offset), state = mValueMask.isOn(offset);
1264 mBuffer.mData.set(offset, val);
1265 mValueMask.set(offset, state);
1272template<Index Log2Dim>
1276 if (newBackground != oldBackground) {
1278 NodeMaskType bgMask = !(mBuffer.mData | mValueMask);
1280 mBuffer.mData = (mBuffer.mData & mValueMask) | bgMask;
1288template<Index Log2Dim>
1289template<MergePolicy Policy>
1295 for (
typename NodeMaskType::OnIterator
iter = other.valueMask().beginOn();
iter; ++
iter) {
1297 if (mValueMask.isOff(n)) {
1298 mBuffer.mData.set(n, other.mBuffer.mData.isOn(n));
1299 mValueMask.setOn(n);
1305template<Index Log2Dim>
1306template<MergePolicy Policy>
1312 if (!tileActive)
return;
1314 if (tileValue) mBuffer.mData |= !mValueMask;
1315 else mBuffer.mData &= mValueMask;
1324template<Index Log2Dim>
1325template<
typename OtherType>
1329 mValueMask |= other.valueMask();
1333template<Index Log2Dim>
1334template<
typename OtherType>
1339 mValueMask &= other.valueMask();
1343template<Index Log2Dim>
1344template<
typename OtherType>
1349 mValueMask &= !other.valueMask();
1356template<Index Log2Dim>
1360 CoordBBox nodeBBox = this->getNodeBoundingBox();
1361 if (!clipBBox.hasOverlap(nodeBBox)) {
1363 this->fill(nodeBBox, background,
false);
1364 }
else if (clipBBox.isInside(nodeBBox)) {
1374 nodeBBox.intersect(clipBBox);
1376 int &x = xyz.x(), &y = xyz.y(), &z = xyz.z();
1377 for (x = nodeBBox.min().x(); x <= nodeBBox.max().x(); ++x) {
1378 for (y = nodeBBox.min().y(); y <= nodeBBox.max().y(); ++y) {
1379 for (z = nodeBBox.min().z(); z <= nodeBBox.max().z(); ++z) {
1380 mask.setOn(
static_cast<Index32>(this->coordToOffset(xyz)));
1387 for (MaskOffIter maskIter = mask.beginOff(); maskIter; ++maskIter) {
1388 this->setValueOff(maskIter.pos(), background);
1396template<Index Log2Dim>
1400 auto clippedBBox = this->getNodeBoundingBox();
1401 clippedBBox.intersect(bbox);
1402 if (!clippedBBox)
return;
1404 for (
Int32 x = clippedBBox.min().x(); x <= clippedBBox.max().x(); ++x) {
1405 const Index offsetX = (x & (DIM-1u))<<2*Log2Dim;
1406 for (
Int32 y = clippedBBox.min().y(); y <= clippedBBox.max().y(); ++y) {
1407 const Index offsetXY = offsetX + ((y & (DIM-1u))<< Log2Dim);
1408 for (
Int32 z = clippedBBox.min().z(); z <= clippedBBox.max().z(); ++z) {
1409 const Index offset = offsetXY + (z & (DIM-1u));
1410 mValueMask.set(offset, active);
1411 mBuffer.mData.set(offset, value);
1417template<Index Log2Dim>
1421 mBuffer.fill(value);
1424template<Index Log2Dim>
1428 mBuffer.fill(value);
1429 mValueMask.set(active);
1436template<Index Log2Dim>
1437template<
typename DenseT>
1441 using DenseValueType =
typename DenseT::ValueType;
1443 const size_t xStride = dense.xStride(), yStride = dense.yStride(), zStride = dense.zStride();
1444 const Coord&
min = dense.bbox().min();
1445 DenseValueType* t0 = dense.data() + zStride * (bbox.min()[2] - min[2]);
1446 const Int32 n0 = bbox.min()[2] & (DIM-1u);
1447 for (
Int32 x = bbox.min()[0], ex = bbox.max()[0] + 1; x < ex; ++x) {
1448 DenseValueType* t1 = t0 + xStride * (x -
min[0]);
1449 const Int32 n1 = n0 + ((x & (DIM-1u)) << 2*LOG2DIM);
1450 for (
Int32 y = bbox.min()[1], ey = bbox.max()[1] + 1; y < ey; ++y) {
1451 DenseValueType* t2 = t1 + yStride * (y -
min[1]);
1452 Int32 n2 = n1 + ((y & (DIM-1u)) << LOG2DIM);
1453 for (
Int32 z = bbox.min()[2], ez = bbox.max()[2] + 1; z < ez; ++z, t2 += zStride) {
1454 *t2 = DenseValueType(mBuffer.mData.isOn(n2++));
1461template<Index Log2Dim>
1462template<
typename DenseT>
1465 bool background,
bool tolerance)
1467 using DenseValueType =
typename DenseT::ValueType;
1469 inline static bool toBool(
const DenseValueType& v) {
return !math::isZero(v); }
1472 const size_t xStride = dense.xStride(), yStride = dense.yStride(), zStride = dense.zStride();
1473 const Coord&
min = dense.bbox().min();
1474 const DenseValueType* s0 = dense.data() + zStride * (bbox.min()[2] - min[2]);
1475 const Int32 n0 = bbox.min()[2] & (DIM-1u);
1476 for (
Int32 x = bbox.min()[0], ex = bbox.max()[0] + 1; x < ex; ++x) {
1477 const DenseValueType* s1 = s0 + xStride * (x -
min[0]);
1478 const Int32 n1 = n0 + ((x & (DIM-1u)) << 2*LOG2DIM);
1479 for (
Int32 y = bbox.min()[1], ey = bbox.max()[1] + 1; y < ey; ++y) {
1480 const DenseValueType* s2 = s1 + yStride * (y -
min[1]);
1481 Int32 n2 = n1 + ((y & (DIM-1u)) << LOG2DIM);
1482 for (
Int32 z = bbox.min()[2], ez = bbox.max()[2]+1; z < ez; ++z, ++n2, s2 += zStride) {
1484 if (tolerance || (background == Local::toBool(*s2))) {
1485 mValueMask.setOff(n2);
1486 mBuffer.mData.set(n2, background);
1488 mValueMask.setOn(n2);
1489 mBuffer.mData.set(n2, Local::toBool(*s2));
1500template<Index Log2Dim>
1501template<
typename CombineOp>
1505 CombineArgs<bool> args;
1506 for (
Index i = 0; i < SIZE; ++i) {
1507 bool result =
false, aVal = mBuffer.mData.isOn(i), bVal = other.mBuffer.mData.isOn(i);
1508 op(args.setARef(aVal)
1509 .setAIsActive(mValueMask.isOn(i))
1511 .setBIsActive(other.valueMask().isOn(i))
1512 .setResultRef(result));
1513 mValueMask.set(i, args.resultIsActive());
1514 mBuffer.mData.set(i, result);
1519template<Index Log2Dim>
1520template<
typename CombineOp>
1524 CombineArgs<bool> args;
1525 args.setBRef(value).setBIsActive(valueIsActive);
1526 for (
Index i = 0; i < SIZE; ++i) {
1527 bool result =
false, aVal = mBuffer.mData.isOn(i);
1528 op(args.setARef(aVal)
1529 .setAIsActive(mValueMask.isOn(i))
1530 .setResultRef(result));
1531 mValueMask.set(i, args.resultIsActive());
1532 mBuffer.mData.set(i, result);
1540template<Index Log2Dim>
1541template<
typename CombineOp,
typename OtherType>
1544 bool valueIsActive, CombineOp& op)
1546 CombineArgs<bool, OtherType> args;
1547 args.setBRef(value).setBIsActive(valueIsActive);
1548 for (
Index i = 0; i < SIZE; ++i) {
1549 bool result =
false, aVal = other.mBuffer.mData.isOn(i);
1550 op(args.setARef(aVal)
1551 .setAIsActive(other.valueMask().isOn(i))
1552 .setResultRef(result));
1553 mValueMask.set(i, args.resultIsActive());
1554 mBuffer.mData.set(i, result);
1559template<Index Log2Dim>
1560template<
typename CombineOp,
typename OtherNodeT>
1563 bool valueIsActive, CombineOp& op)
1565 CombineArgs<bool, typename OtherNodeT::ValueType> args;
1566 args.setARef(value).setAIsActive(valueIsActive);
1567 for (
Index i = 0; i < SIZE; ++i) {
1568 bool result =
false, bVal = other.mBuffer.mData.isOn(i);
1569 op(args.setBRef(bVal)
1570 .setBIsActive(other.valueMask().isOn(i))
1571 .setResultRef(result));
1572 mValueMask.set(i, args.resultIsActive());
1573 mBuffer.mData.set(i, result);
1578template<Index Log2Dim>
1579template<
typename CombineOp,
typename OtherNodeT>
1583 CombineArgs<bool, typename OtherNodeT::ValueType> args;
1584 for (
Index i = 0; i < SIZE; ++i) {
1586 mValueMask.set(i, b0.valueMask().isOn(i) || b1.valueMask().isOn(i));
1588 bool result =
false, b0Val = b0.mBuffer.mData.isOn(i), b1Val = b1.mBuffer.mData.isOn(i);
1589 op(args.setARef(b0Val)
1590 .setAIsActive(b0.valueMask().isOn(i))
1592 .setBIsActive(b1.valueMask().isOn(i))
1593 .setResultRef(result));
1594 mValueMask.set(i, args.resultIsActive());
1595 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 resetBackground(const ValueType &oldBackground, const ValueType &newBackground)
Replace inactive occurrences of oldBackground with newBackground, and inactive occurrences of -oldBac...
Definition LeafNode.h:1658
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
static const Index SIZE
Definition LeafNode.h:54
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
uint32_t countOn(uint64_t v)
Definition Util.h:656
OPENVDB_API void checkFormatVersion(std::ios_base &)
Throws an IoError if the file format version number is not supported.
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 LeafNodeBool.h:634
ChildIter()
Definition LeafNodeBool.h:633
bool getItem(Index pos, void *&child, NonConstValueT &value) const
Definition LeafNodeBool.h:648
DenseIter()
Definition LeafNodeBool.h:645
DenseIter(const MaskDenseIter &iter, NodeT *parent)
Definition LeafNodeBool.h:646
typename BaseT::NonConstValueType NonConstValueT
Definition LeafNodeBool.h:643
DenseIteratorBase< MaskDenseIter, DenseIter, NodeT, void, ValueT > BaseT
Definition LeafNodeBool.h:642
void unsetItem(Index pos, const ValueT &val) const
Definition LeafNodeBool.h:659
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:616
void modifyValue(const ModifyOp &op) const
Definition LeafNodeBool.h:625
ValueIter(const MaskIterT &iter, NodeT *parent)
Definition LeafNodeBool.h:610
const bool & getValue() const
Definition LeafNodeBool.h:613
const bool & getItem(Index pos) const
Definition LeafNodeBool.h:612
ValueIter()
Definition LeafNodeBool.h:609
void setValue(bool value) const
Definition LeafNodeBool.h:618
void modifyItem(Index n, const ModifyOp &op) const
Definition LeafNodeBool.h:622
SparseIteratorBase< MaskIterT, ValueIter, NodeT, ValueT > BaseT
Definition LeafNodeBool.h:607
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