4#ifndef OPENVDB_TREE_LEAF_NODE_BOOL_HAS_BEEN_INCLUDED
5#define OPENVDB_TREE_LEAF_NODE_BOOL_HAS_BEEN_INCLUDED
27template<Index Log2Dim>
28class LeafNode<bool, Log2Dim>
31 using LeafNodeType = LeafNode<bool, Log2Dim>;
32 using BuildType = bool;
33 using ValueType = bool;
34 using Buffer = LeafBuffer<ValueType, Log2Dim>;
35 using NodeMaskType = util::NodeMask<Log2Dim>;
36 using Ptr = SharedPtr<LeafNodeType>;
39 static const Index LOG2DIM = Log2Dim;
40 static const Index TOTAL = Log2Dim;
41 static const Index DIM = 1 << TOTAL;
42 static const Index NUM_VALUES = 1 << 3 * Log2Dim;
43 static const Index NUM_VOXELS = NUM_VALUES;
44 static const Index SIZE = NUM_VALUES;
45 static const Index LEVEL = 0;
49 template<
typename ValueType>
54 template<
typename OtherNodeType>
67 explicit LeafNode(
const Coord& xyz,
bool value =
false,
bool active =
false);
73 LeafNode(
const LeafNode&);
76 LeafNode& operator=(
const LeafNode&) =
default;
79 template<
typename OtherValueType>
80 explicit LeafNode(
const LeafNode<OtherValueType, Log2Dim>& other);
95 template<
typename ValueType>
97 LeafNode(const LeafNode<ValueType, Log2Dim>& other,
TopologyCopy);
104 LeafNode(const
Coord& xyz,
105 const NodeMaskType& mask,
111 template<typename ValueType>
112 LeafNode(const LeafNode<ValueType, Log2Dim>& other,
bool offValue,
bool onValue,
TopologyCopy);
113 template<typename ValueType>
114 LeafNode(const LeafNode<ValueType, Log2Dim>& other,
bool background,
TopologyCopy);
124 static
Index log2dim() {
return Log2Dim; }
126 static Index dim() {
return DIM; }
127 static Index size() {
return SIZE; }
128 static Index numValues() {
return SIZE; }
129 static Index getLevel() {
return LEVEL; }
130 static void getNodeLog2Dims(std::vector<Index>& dims) { dims.push_back(Log2Dim); }
131 static Index getChildDim() {
return 1; }
133 static Index32 leafCount() {
return 1; }
135 void nodeCount(std::vector<Index32> &)
const {}
136 static Index32 nonLeafCount() {
return 0; }
139 Index64 onVoxelCount()
const {
return mValueMask.countOn(); }
141 Index64 offVoxelCount()
const {
return mValueMask.countOff(); }
142 Index64 onLeafVoxelCount()
const {
return onVoxelCount(); }
143 Index64 offLeafVoxelCount()
const {
return offVoxelCount(); }
144 static Index64 onTileCount() {
return 0; }
145 static Index64 offTileCount() {
return 0; }
148 bool isEmpty()
const {
return mValueMask.isOff(); }
150 bool isDense()
const {
return mValueMask.isOn(); }
154 bool isAllocated()
const {
return true; }
167 void evalActiveBoundingBox(CoordBBox& bbox,
bool visitVoxels =
true)
const;
171 CoordBBox getNodeBoundingBox()
const {
return CoordBBox::createCube(mOrigin, DIM); }
174 void setOrigin(
const Coord& origin) { mOrigin = origin; }
177 const Coord& origin()
const {
return mOrigin; }
178 void getOrigin(Coord& origin)
const { origin = mOrigin; }
179 void getOrigin(Int32& x, Int32& y, Int32& z)
const { mOrigin.asXYZ(x, y, z); }
183 static Index coordToOffset(
const Coord& xyz);
186 static Coord offsetToLocalCoord(Index n);
188 Coord offsetToGlobalCoord(Index n)
const;
191 Index32 transientData()
const {
return mTransientData; }
193 void setTransientData(Index32 transientData) { mTransientData = transientData; }
196 std::string str()
const;
200 template<
typename OtherType, Index OtherLog2Dim>
201 bool hasSameTopology(
const LeafNode<OtherType, OtherLog2Dim>* other)
const;
212 void swap(Buffer& other) { mBuffer.swap(other); }
213 const Buffer& buffer()
const {
return mBuffer; }
214 Buffer& buffer() {
return mBuffer; }
220 void readTopology(std::istream&,
bool fromHalf =
false);
222 void writeTopology(std::ostream&,
bool toHalf =
false)
const;
225 void readBuffers(std::istream&,
bool fromHalf =
false);
226 void readBuffers(std::istream& is,
const CoordBBox&,
bool fromHalf =
false);
228 void writeBuffers(std::ostream&,
bool toHalf =
false)
const;
234 const bool& getValue(
const Coord& xyz)
const;
236 const bool& getValue(Index offset)
const;
241 bool probeValue(
const Coord& xyz,
bool& val)
const;
244 static Index getValueLevel(
const Coord&) {
return LEVEL; }
247 void setActiveState(
const Coord& xyz,
bool on);
249 void setActiveState(Index offset,
bool on) { assert(offset<SIZE); mValueMask.set(offset, on); }
252 void setValueOnly(
const Coord& xyz,
bool val);
254 void setValueOnly(Index offset,
bool val) { assert(offset<SIZE); mBuffer.setValue(offset,val); }
257 void setValueOff(
const Coord& xyz) { mValueMask.setOff(this->coordToOffset(xyz)); }
259 void setValueOff(Index offset) { assert(offset < SIZE); mValueMask.setOff(offset); }
262 void setValueOff(
const Coord& xyz,
bool val);
264 void setValueOff(Index offset,
bool val);
267 void setValueOn(
const Coord& xyz) { mValueMask.setOn(this->coordToOffset(xyz)); }
269 void setValueOn(Index offset) { assert(offset < SIZE); mValueMask.setOn(offset); }
272 void setValueOn(
const Coord& xyz,
bool val);
274 void setValue(
const Coord& xyz,
bool val) { this->setValueOn(xyz, val); }
276 void setValueOn(Index offset,
bool val);
280 template<
typename ModifyOp>
281 void modifyValue(Index offset,
const ModifyOp& op);
284 template<
typename ModifyOp>
285 void modifyValue(
const Coord& xyz,
const ModifyOp& op);
288 template<
typename ModifyOp>
289 void modifyValueAndActiveState(
const Coord& xyz,
const ModifyOp& op);
292 void setValuesOn() { mValueMask.setOn(); }
294 void setValuesOff() { mValueMask.setOff(); }
297 bool isValueOn(
const Coord& xyz)
const {
return mValueMask.isOn(this->coordToOffset(xyz)); }
299 bool isValueOn(Index offset)
const { assert(offset < SIZE);
return mValueMask.isOn(offset); }
302 static bool hasActiveTiles() {
return false; }
305 void clip(
const CoordBBox&,
bool background);
308 void fill(
const CoordBBox& bbox,
bool value,
bool active =
true);
310 void denseFill(
const CoordBBox& bbox,
bool val,
bool on =
true) { this->fill(bbox, val, on); }
313 void fill(
const bool& value);
315 void fill(
const bool& value,
bool active);
328 template<
typename DenseT>
329 void copyToDense(
const CoordBBox& bbox, DenseT& dense)
const;
347 template<
typename DenseT>
348 void copyFromDense(
const CoordBBox& bbox,
const DenseT& dense,
bool background,
bool tolerance);
352 template<
typename AccessorT>
353 const bool& getValueAndCache(
const Coord& xyz, AccessorT&)
const {
return this->getValue(xyz);}
357 template<
typename AccessorT>
358 bool isValueOnAndCache(
const Coord& xyz, AccessorT&)
const {
return this->isValueOn(xyz); }
362 template<
typename AccessorT>
363 void setValueAndCache(
const Coord& xyz,
bool val, AccessorT&) { this->setValueOn(xyz, val); }
368 template<
typename AccessorT>
369 void setValueOnlyAndCache(
const Coord& xyz,
bool val, AccessorT&) {this->setValueOnly(xyz,val);}
373 template<
typename AccessorT>
374 void setValueOffAndCache(
const Coord& xyz,
bool value, AccessorT&)
376 this->setValueOff(xyz, value);
382 template<
typename ModifyOp,
typename AccessorT>
383 void modifyValueAndCache(
const Coord& xyz,
const ModifyOp& op, AccessorT&)
385 this->modifyValue(xyz, op);
390 template<
typename ModifyOp,
typename AccessorT>
391 void modifyValueAndActiveStateAndCache(
const Coord& xyz,
const ModifyOp& op, AccessorT&)
393 this->modifyValueAndActiveState(xyz, op);
399 template<
typename AccessorT>
400 void setActiveStateAndCache(
const Coord& xyz,
bool on, AccessorT&)
402 this->setActiveState(xyz, on);
408 template<
typename AccessorT>
409 bool probeValueAndCache(
const Coord& xyz,
bool& val, AccessorT&)
const
411 return this->probeValue(xyz, val);
416 template<
typename AccessorT>
417 static Index getValueLevelAndCache(
const Coord&, AccessorT&) {
return LEVEL; }
422 const bool& getFirstValue()
const {
if (mValueMask.isOn(0))
return Buffer::sOn;
else return Buffer::sOff; }
426 const bool& getLastValue()
const {
if (mValueMask.isOn(SIZE-1))
return Buffer::sOn;
else return Buffer::sOff; }
431 bool isConstant(
bool& constValue,
bool& state,
bool tolerance = 0)
const;
438 bool medianAll()
const;
446 Index medianOn(ValueType &value)
const;
454 Index medianOff(ValueType &value)
const;
457 bool isInactive()
const {
return mValueMask.isOff(); }
459 void resetBackground(
bool oldBackground,
bool newBackground);
461 void negate() { mBuffer.mData.toggle(); }
463 template<MergePolicy Policy>
464 void merge(
const LeafNode& other,
bool bg =
false,
bool otherBG =
false);
465 template<MergePolicy Policy>
void merge(
bool tileValue,
bool tileActive);
469 void voxelizeActiveTiles(
bool =
true) {}
477 template<
typename OtherType>
478 void topologyUnion(
const LeafNode<OtherType, Log2Dim>& other,
const bool preserveTiles =
false);
491 template<
typename OtherType>
492 void topologyIntersection(
const LeafNode<OtherType, Log2Dim>& other,
const bool&);
505 template<
typename OtherType>
506 void topologyDifference(
const LeafNode<OtherType, Log2Dim>& other,
const bool&);
508 template<
typename CombineOp>
509 void combine(
const LeafNode& other, CombineOp& op);
510 template<
typename CombineOp>
511 void combine(
bool,
bool valueIsActive, CombineOp& op);
513 template<
typename CombineOp,
typename OtherType >
514 void combine2(
const LeafNode& other,
const OtherType&,
bool valueIsActive, CombineOp&);
515 template<
typename CombineOp,
typename OtherNodeT >
516 void combine2(
bool,
const OtherNodeT& other,
bool valueIsActive, CombineOp&);
517 template<
typename CombineOp,
typename OtherNodeT >
518 void combine2(
const LeafNode& b0,
const OtherNodeT& b1, CombineOp&);
522 void prune(
const ValueType& = zeroVal<ValueType>()) {}
523 void addLeaf(LeafNode*) {}
524 template<
typename AccessorT>
525 void addLeafAndCache(LeafNode*, AccessorT&) {}
526 template<
typename NodeT>
527 NodeT* stealNode(
const Coord&,
const ValueType&,
bool) {
return nullptr; }
528 template<
typename NodeT>
529 NodeT* probeNode(
const Coord&) {
return nullptr; }
530 template<
typename NodeT>
531 const NodeT* probeConstNode(
const Coord&)
const {
return nullptr; }
532 template<
typename ArrayT>
void getNodes(ArrayT&)
const {}
533 template<
typename ArrayT>
void stealNodes(ArrayT&,
const ValueType&,
bool) {}
536 void addTile(Index level,
const Coord&,
bool val,
bool active);
537 void addTile(Index offset,
bool val,
bool active);
538 template<
typename AccessorT>
539 void addTileAndCache(Index level,
const Coord&,
bool val,
bool active, AccessorT&);
543 LeafNode* touchLeaf(
const Coord&) {
return this; }
544 template<
typename AccessorT>
545 LeafNode* touchLeafAndCache(
const Coord&, AccessorT&) {
return this; }
546 LeafNode* probeLeaf(
const Coord&) {
return this; }
547 template<
typename AccessorT>
548 LeafNode* probeLeafAndCache(
const Coord&, AccessorT&) {
return this; }
549 template<
typename NodeT,
typename AccessorT>
550 NodeT* probeNodeAndCache(
const Coord&, AccessorT&)
553 if (!(std::is_same<NodeT, LeafNode>::value))
return nullptr;
554 return reinterpret_cast<NodeT*
>(
this);
560 const LeafNode* probeLeaf(
const Coord&)
const {
return this; }
561 template<
typename AccessorT>
562 const LeafNode* probeLeafAndCache(
const Coord&, AccessorT&)
const {
return this; }
563 const LeafNode* probeConstLeaf(
const Coord&)
const {
return this; }
564 template<
typename AccessorT>
565 const LeafNode* probeConstLeafAndCache(
const Coord&, AccessorT&)
const {
return this; }
566 template<
typename NodeT,
typename AccessorT>
567 const NodeT* probeConstNodeAndCache(
const Coord&, AccessorT&)
const
570 if (!(std::is_same<NodeT, LeafNode>::value))
return nullptr;
571 return reinterpret_cast<const NodeT*
>(
this);
580 using MaskOnIter =
typename NodeMaskType::OnIterator;
581 using MaskOffIter =
typename NodeMaskType::OffIterator;
582 using MaskDenseIter =
typename NodeMaskType::DenseIterator;
584 template<
typename MaskIterT,
typename NodeT,
typename ValueT>
588 public SparseIteratorBase<MaskIterT, ValueIter<MaskIterT, NodeT, ValueT>, NodeT, ValueT>
604 template<
typename ModifyOp>
607 template<
typename ModifyOp>
612 template<
typename MaskIterT,
typename NodeT>
621 template<
typename NodeT,
typename ValueT>
623 MaskDenseIter, DenseIter<NodeT, ValueT>, NodeT, void, ValueT>
646 using ValueOnIter = ValueIter<MaskOnIter, LeafNode, const bool>;
647 using ValueOnCIter = ValueIter<MaskOnIter, const LeafNode, const bool>;
648 using ValueOffIter = ValueIter<MaskOffIter, LeafNode, const bool>;
649 using ValueOffCIter = ValueIter<MaskOffIter, const LeafNode, const bool>;
650 using ValueAllIter = ValueIter<MaskDenseIter, LeafNode, const bool>;
651 using ValueAllCIter = ValueIter<MaskDenseIter, const LeafNode, const bool>;
652 using ChildOnIter = ChildIter<MaskOnIter, LeafNode>;
653 using ChildOnCIter = ChildIter<MaskOnIter, const LeafNode>;
654 using ChildOffIter = ChildIter<MaskOffIter, LeafNode>;
655 using ChildOffCIter = ChildIter<MaskOffIter, const LeafNode>;
656 using ChildAllIter = DenseIter<LeafNode, bool>;
657 using ChildAllCIter = DenseIter<const LeafNode, const bool>;
659 ValueOnCIter cbeginValueOn()
const {
return ValueOnCIter(mValueMask.beginOn(),
this); }
660 ValueOnCIter beginValueOn()
const {
return ValueOnCIter(mValueMask.beginOn(),
this); }
661 ValueOnIter beginValueOn() {
return ValueOnIter(mValueMask.beginOn(),
this); }
662 ValueOffCIter cbeginValueOff()
const {
return ValueOffCIter(mValueMask.beginOff(),
this); }
663 ValueOffCIter beginValueOff()
const {
return ValueOffCIter(mValueMask.beginOff(),
this); }
664 ValueOffIter beginValueOff() {
return ValueOffIter(mValueMask.beginOff(),
this); }
665 ValueAllCIter cbeginValueAll()
const {
return ValueAllCIter(mValueMask.beginDense(),
this); }
666 ValueAllCIter beginValueAll()
const {
return ValueAllCIter(mValueMask.beginDense(),
this); }
667 ValueAllIter beginValueAll() {
return ValueAllIter(mValueMask.beginDense(),
this); }
669 ValueOnCIter cendValueOn()
const {
return ValueOnCIter(mValueMask.endOn(),
this); }
670 ValueOnCIter endValueOn()
const {
return ValueOnCIter(mValueMask.endOn(),
this); }
671 ValueOnIter endValueOn() {
return ValueOnIter(mValueMask.endOn(),
this); }
672 ValueOffCIter cendValueOff()
const {
return ValueOffCIter(mValueMask.endOff(),
this); }
673 ValueOffCIter endValueOff()
const {
return ValueOffCIter(mValueMask.endOff(),
this); }
674 ValueOffIter endValueOff() {
return ValueOffIter(mValueMask.endOff(),
this); }
675 ValueAllCIter cendValueAll()
const {
return ValueAllCIter(mValueMask.endDense(),
this); }
676 ValueAllCIter endValueAll()
const {
return ValueAllCIter(mValueMask.endDense(),
this); }
677 ValueAllIter endValueAll() {
return ValueAllIter(mValueMask.endDense(),
this); }
681 ChildOnCIter cbeginChildOn()
const {
return ChildOnCIter(mValueMask.endOn(),
this); }
682 ChildOnCIter beginChildOn()
const {
return ChildOnCIter(mValueMask.endOn(),
this); }
683 ChildOnIter beginChildOn() {
return ChildOnIter(mValueMask.endOn(),
this); }
684 ChildOffCIter cbeginChildOff()
const {
return ChildOffCIter(mValueMask.endOff(),
this); }
685 ChildOffCIter beginChildOff()
const {
return ChildOffCIter(mValueMask.endOff(),
this); }
686 ChildOffIter beginChildOff() {
return ChildOffIter(mValueMask.endOff(),
this); }
687 ChildAllCIter cbeginChildAll()
const {
return ChildAllCIter(mValueMask.beginDense(),
this); }
688 ChildAllCIter beginChildAll()
const {
return ChildAllCIter(mValueMask.beginDense(),
this); }
689 ChildAllIter beginChildAll() {
return ChildAllIter(mValueMask.beginDense(),
this); }
691 ChildOnCIter cendChildOn()
const {
return ChildOnCIter(mValueMask.endOn(),
this); }
692 ChildOnCIter endChildOn()
const {
return ChildOnCIter(mValueMask.endOn(),
this); }
693 ChildOnIter endChildOn() {
return ChildOnIter(mValueMask.endOn(),
this); }
694 ChildOffCIter cendChildOff()
const {
return ChildOffCIter(mValueMask.endOff(),
this); }
695 ChildOffCIter endChildOff()
const {
return ChildOffCIter(mValueMask.endOff(),
this); }
696 ChildOffIter endChildOff() {
return ChildOffIter(mValueMask.endOff(),
this); }
697 ChildAllCIter cendChildAll()
const {
return ChildAllCIter(mValueMask.endDense(),
this); }
698 ChildAllCIter endChildAll()
const {
return ChildAllCIter(mValueMask.endDense(),
this); }
699 ChildAllIter endChildAll() {
return ChildAllIter(mValueMask.endDense(),
this); }
704 bool isValueMaskOn(Index n)
const {
return mValueMask.isOn(n); }
705 bool isValueMaskOn()
const {
return mValueMask.isOn(); }
706 bool isValueMaskOff(Index n)
const {
return mValueMask.isOff(n); }
707 bool isValueMaskOff()
const {
return mValueMask.isOff(); }
708 const NodeMaskType& getValueMask()
const {
return mValueMask; }
709 const NodeMaskType& valueMask()
const {
return mValueMask; }
710 NodeMaskType& getValueMask() {
return mValueMask; }
711 void setValueMask(
const NodeMaskType& mask) { mValueMask = mask; }
712 bool isChildMaskOn(Index)
const {
return false; }
713 bool isChildMaskOff(Index)
const {
return true; }
714 bool isChildMaskOff()
const {
return true; }
716 void setValueMask(Index n,
bool on) { mValueMask.set(n, on); }
717 void setValueMaskOn(Index n) { mValueMask.setOn(n); }
718 void setValueMaskOff(Index n) { mValueMask.setOff(n); }
721 static void evalNodeOrigin(Coord& xyz) { xyz &= ~(DIM - 1); }
724 NodeMaskType mValueMask;
735 template<
typename, Index>
friend class LeafNode;
737 friend struct ValueIter<MaskOnIter, LeafNode, bool>;
738 friend struct ValueIter<MaskOffIter, LeafNode, bool>;
739 friend struct ValueIter<MaskDenseIter, LeafNode, bool>;
740 friend struct ValueIter<MaskOnIter, const LeafNode, bool>;
741 friend struct ValueIter<MaskOffIter, const LeafNode, bool>;
742 friend struct ValueIter<MaskDenseIter, const LeafNode, bool>;
747 friend class IteratorBase<MaskOnIter, LeafNode>;
748 friend class IteratorBase<MaskOffIter, LeafNode>;
749 friend class IteratorBase<MaskDenseIter, LeafNode>;
758template<Index Log2Dim>
760LeafNode<bool, Log2Dim>::LeafNode()
766template<Index Log2Dim>
771 , mOrigin(xyz & (~(
DIM - 1)))
776template<Index Log2Dim>
781 , mOrigin(xyz & (~(
DIM - 1)))
789template<Index Log2Dim>
792 : mValueMask(other.valueMask())
793 , mBuffer(other.mBuffer)
794 , mOrigin(other.mOrigin)
795 , mTransientData(other.mTransientData)
801template<Index Log2Dim>
802template<
typename ValueT>
807 , mTransientData(other.mTransientData)
811 static inline bool convertValue(
const ValueT& val) {
return bool(val); }
815 mBuffer.setValue(i, Local::convertValue(other.mBuffer[i]));
820template<Index Log2Dim>
821template<
typename ValueT>
826 , mBuffer(background)
828 , mTransientData(other.mTransientData)
833template<Index Log2Dim>
834template<
typename ValueT>
840 , mTransientData(other.mTransientData)
844template<Index Log2Dim>
852 , mOrigin(xyz & (~(
DIM - 1)))
853 , mTransientData(trans)
857template<Index Log2Dim>
858template<
typename ValueT>
865 , mTransientData(other.mTransientData)
868 if (mValueMask.isOn(i)) {
869 mBuffer.setValue(i, onValue);
875template<Index Log2Dim>
885template<Index Log2Dim>
890 return sizeof(*this);
894template<Index Log2Dim>
899 return sizeof(*this);
903template<Index Log2Dim>
907 CoordBBox this_bbox = this->getNodeBoundingBox();
908 if (bbox.isInside(this_bbox))
return;
909 if (ValueOnCIter iter = this->cbeginValueOn()) {
912 for(; iter; ++iter) this_bbox.expand(this->offsetToLocalCoord(iter.pos()));
913 this_bbox.translate(this->origin());
915 bbox.expand(this_bbox);
920template<Index Log2Dim>
921template<
typename OtherType, Index OtherLog2Dim>
926 return (Log2Dim == OtherLog2Dim && mValueMask == other->getValueMask());
930template<Index Log2Dim>
934 std::ostringstream ostr;
935 ostr <<
"LeafNode @" << mOrigin <<
": ";
936 for (
Index32 n = 0; n < SIZE; ++n) ostr << (mValueMask.isOn(n) ?
'#' :
'.');
944template<Index Log2Dim>
948 assert ((xyz[0] & (DIM-1u)) < DIM && (xyz[1] & (DIM-1u)) < DIM && (xyz[2] & (DIM-1u)) < DIM);
949 return ((xyz[0] & (DIM-1u)) << 2*Log2Dim)
950 + ((xyz[1] & (DIM-1u)) << Log2Dim)
951 + (xyz[2] & (DIM-1u));
955template<Index Log2Dim>
959 assert(n < (1 << 3*Log2Dim));
961 xyz.setX(n >> 2*Log2Dim);
962 n &= ((1 << 2*Log2Dim) - 1);
963 xyz.setY(n >> Log2Dim);
964 xyz.setZ(n & ((1 << Log2Dim) - 1));
969template<Index Log2Dim>
973 return (this->offsetToLocalCoord(n) + this->origin());
980template<Index Log2Dim>
988template<Index Log2Dim>
996template<Index Log2Dim>
1003 this->readBuffers(is, fromHalf);
1006 bool background =
false;
1008 background = *
static_cast<const bool*
>(bgPtr);
1010 this->
clip(clipBBox, background);
1014template<Index Log2Dim>
1019 mValueMask.load(is);
1025 mBuffer.mData.load(is);
1030 int8_t numBuffers = 0;
1031 is.read(
reinterpret_cast<char*
>(&numBuffers),
sizeof(int8_t));
1035 std::unique_ptr<bool[]> buf{
new bool[SIZE]};
1039 mBuffer.mData.setOff();
1040 for (
Index i = 0; i < SIZE; ++i) {
1041 if (buf[i]) mBuffer.mData.setOn(i);
1044 if (numBuffers > 1) {
1047 for (
int i = 1; i < numBuffers; ++i) {
1055template<Index Log2Dim>
1060 mValueMask.save(os);
1062 os.write(
reinterpret_cast<const char*
>(&mOrigin),
sizeof(
Coord::ValueType) * 3);
1064 mBuffer.mData.save(os);
1071template<Index Log2Dim>
1075 return mOrigin == other.mOrigin &&
1076 mValueMask == other.valueMask() &&
1077 mBuffer == other.mBuffer;
1081template<Index Log2Dim>
1092template<Index Log2Dim>
1096 if (!mValueMask.isConstant(state))
return false;
1099 if (!tolerance && !(mBuffer.mData.isOn() || mBuffer.mData.isOff()))
return false;
1101 constValue = mBuffer.mData.isOn();
1107template<Index Log2Dim>
1111 const Index countTrue = mBuffer.mData.countOn();
1112 return countTrue > (NUM_VALUES >> 1);
1115template<Index Log2Dim>
1119 const NodeMaskType tmp = mBuffer.mData & mValueMask;
1120 const Index countTrueOn = tmp.countOn(), countOn = mValueMask.countOn();
1121 state = countTrueOn > (NUM_VALUES >> 1);
1125template<Index Log2Dim>
1129 const NodeMaskType tmp = mBuffer.mData & (!mValueMask);
1130 const Index countTrueOff = tmp.countOn(), countOff = mValueMask.countOff();
1131 state = countTrueOff > (NUM_VALUES >> 1);
1138template<Index Log2Dim>
1142 this->addTile(this->coordToOffset(xyz), val, active);
1145template<Index Log2Dim>
1149 assert(offset < SIZE);
1150 this->setValueOnly(offset, val);
1151 this->setActiveState(offset, active);
1154template<Index Log2Dim>
1155template<
typename AccessorT>
1158 bool val,
bool active, AccessorT&)
1160 this->
addTile(level, xyz, val, active);
1167template<Index Log2Dim>
1172 if (mBuffer.mData.isOn(this->coordToOffset(xyz)))
return Buffer::sOn;
else return Buffer::sOff;
1176template<Index Log2Dim>
1180 assert(offset < SIZE);
1182 if (mBuffer.mData.isOn(offset))
return Buffer::sOn;
else return Buffer::sOff;
1186template<Index Log2Dim>
1190 const Index offset = this->coordToOffset(xyz);
1191 val = mBuffer.mData.isOn(offset);
1192 return mValueMask.isOn(offset);
1196template<Index Log2Dim>
1200 this->setValueOn(this->coordToOffset(xyz), val);
1204template<Index Log2Dim>
1208 assert(offset < SIZE);
1209 mValueMask.setOn(offset);
1210 mBuffer.mData.set(offset, val);
1214template<Index Log2Dim>
1218 this->setValueOnly(this->coordToOffset(xyz), val);
1222template<Index Log2Dim>
1226 mValueMask.set(this->coordToOffset(xyz), on);
1230template<Index Log2Dim>
1234 this->setValueOff(this->coordToOffset(xyz), val);
1238template<Index Log2Dim>
1242 assert(offset < SIZE);
1243 mValueMask.setOff(offset);
1244 mBuffer.mData.set(offset, val);
1248template<Index Log2Dim>
1249template<
typename ModifyOp>
1253 bool val = mBuffer.mData.isOn(offset);
1255 mBuffer.mData.set(offset, val);
1256 mValueMask.setOn(offset);
1260template<Index Log2Dim>
1261template<
typename ModifyOp>
1265 this->modifyValue(this->coordToOffset(xyz), op);
1269template<Index Log2Dim>
1270template<
typename ModifyOp>
1274 const Index offset = this->coordToOffset(xyz);
1275 bool val = mBuffer.mData.isOn(offset), state = mValueMask.isOn(offset);
1277 mBuffer.mData.set(offset, val);
1278 mValueMask.set(offset, state);
1285template<Index Log2Dim>
1289 if (newBackground != oldBackground) {
1291 NodeMaskType bgMask = !(mBuffer.mData | mValueMask);
1293 mBuffer.mData = (mBuffer.mData & mValueMask) | bgMask;
1301template<Index Log2Dim>
1302template<MergePolicy Policy>
1310 if (mValueMask.isOff(n)) {
1311 mBuffer.mData.set(n, other.mBuffer.mData.isOn(n));
1312 mValueMask.setOn(n);
1318template<Index Log2Dim>
1319template<MergePolicy Policy>
1325 if (!tileActive)
return;
1327 if (tileValue) mBuffer.mData |= !mValueMask;
1328 else mBuffer.mData &= mValueMask;
1337template<Index Log2Dim>
1338template<
typename OtherType>
1342 mValueMask |= other.valueMask();
1346template<Index Log2Dim>
1347template<
typename OtherType>
1352 mValueMask &= other.valueMask();
1356template<Index Log2Dim>
1357template<
typename OtherType>
1362 mValueMask &= !other.valueMask();
1369template<Index Log2Dim>
1373 CoordBBox nodeBBox = this->getNodeBoundingBox();
1376 this->fill(nodeBBox, background,
false);
1377 }
else if (clipBBox.
isInside(nodeBBox)) {
1389 int &x = xyz.
x(), &y = xyz.
y(), &z = xyz.
z();
1390 for (x = nodeBBox.
min().
x(); x <= nodeBBox.
max().
x(); ++x) {
1391 for (y = nodeBBox.
min().
y(); y <= nodeBBox.
max().
y(); ++y) {
1392 for (z = nodeBBox.
min().
z(); z <= nodeBBox.
max().
z(); ++z) {
1393 mask.setOn(
static_cast<Index32>(this->coordToOffset(xyz)));
1400 for (MaskOffIter maskIter = mask.beginOff(); maskIter; ++maskIter) {
1401 this->setValueOff(maskIter.pos(), background);
1409template<Index Log2Dim>
1413 auto clippedBBox = this->getNodeBoundingBox();
1414 clippedBBox.intersect(bbox);
1415 if (!clippedBBox)
return;
1417 for (
Int32 x = clippedBBox.min().x(); x <= clippedBBox.max().x(); ++x) {
1418 const Index offsetX = (x & (DIM-1u))<<2*Log2Dim;
1419 for (
Int32 y = clippedBBox.min().y(); y <= clippedBBox.max().y(); ++y) {
1420 const Index offsetXY = offsetX + ((y & (DIM-1u))<< Log2Dim);
1421 for (
Int32 z = clippedBBox.min().z(); z <= clippedBBox.max().z(); ++z) {
1422 const Index offset = offsetXY + (z & (DIM-1u));
1423 mValueMask.set(offset, active);
1424 mBuffer.mData.set(offset, value);
1430template<Index Log2Dim>
1434 mBuffer.fill(value);
1437template<Index Log2Dim>
1441 mBuffer.fill(value);
1442 mValueMask.set(active);
1449template<Index Log2Dim>
1450template<
typename DenseT>
1454 using DenseValueType =
typename DenseT::ValueType;
1456 const size_t xStride = dense.xStride(), yStride = dense.yStride(), zStride = dense.zStride();
1457 const Coord&
min = dense.bbox().min();
1458 DenseValueType* t0 = dense.data() + zStride * (bbox.min()[2] - min[2]);
1459 const Int32 n0 = bbox.min()[2] & (DIM-1u);
1460 for (
Int32 x = bbox.min()[0], ex = bbox.max()[0] + 1; x < ex; ++x) {
1461 DenseValueType* t1 = t0 + xStride * (x -
min[0]);
1462 const Int32 n1 = n0 + ((x & (DIM-1u)) << 2*LOG2DIM);
1463 for (
Int32 y = bbox.min()[1], ey = bbox.max()[1] + 1; y < ey; ++y) {
1464 DenseValueType* t2 = t1 + yStride * (y -
min[1]);
1465 Int32 n2 = n1 + ((y & (DIM-1u)) << LOG2DIM);
1466 for (
Int32 z = bbox.min()[2], ez = bbox.max()[2] + 1; z < ez; ++z, t2 += zStride) {
1467 *t2 = DenseValueType(mBuffer.mData.isOn(n2++));
1474template<Index Log2Dim>
1475template<
typename DenseT>
1478 bool background,
bool tolerance)
1480 using DenseValueType =
typename DenseT::ValueType;
1482 inline static bool toBool(
const DenseValueType& v) {
return !
math::isZero(v); }
1485 const size_t xStride = dense.xStride(), yStride = dense.yStride(), zStride = dense.zStride();
1486 const Coord& min = dense.bbox().min();
1487 const DenseValueType* s0 = dense.data() + zStride * (bbox.
min()[2] - min[2]);
1489 for (
Int32 x = bbox.
min()[0], ex = bbox.
max()[0] + 1; x < ex; ++x) {
1490 const DenseValueType* s1 = s0 + xStride * (x - min[0]);
1492 for (
Int32 y = bbox.
min()[1], ey = bbox.
max()[1] + 1; y < ey; ++y) {
1493 const DenseValueType* s2 = s1 + yStride * (y - min[1]);
1495 for (
Int32 z = bbox.
min()[2], ez = bbox.
max()[2]+1; z < ez; ++z, ++n2, s2 += zStride) {
1497 if (tolerance || (background == Local::toBool(*s2))) {
1498 mValueMask.setOff(n2);
1499 mBuffer.mData.set(n2, background);
1501 mValueMask.setOn(n2);
1502 mBuffer.mData.set(n2, Local::toBool(*s2));
1513template<Index Log2Dim>
1514template<
typename CombineOp>
1519 for (
Index i = 0; i < SIZE; ++i) {
1520 bool result =
false, aVal = mBuffer.mData.isOn(i), bVal = other.mBuffer.mData.isOn(i);
1527 mBuffer.mData.set(i, result);
1532template<Index Log2Dim>
1533template<
typename CombineOp>
1540 bool result =
false, aVal = mBuffer.mData.isOn(i);
1545 mBuffer.mData.set(i, result);
1553template<Index Log2Dim>
1554template<
typename CombineOp,
typename OtherType>
1557 bool valueIsActive, CombineOp& op)
1561 for (
Index i = 0; i < SIZE; ++i) {
1562 bool result =
false, aVal = other.mBuffer.mData.isOn(i);
1567 mBuffer.mData.set(i, result);
1572template<Index Log2Dim>
1573template<
typename CombineOp,
typename OtherNodeT>
1576 bool valueIsActive, CombineOp& op)
1581 bool result =
false, bVal = other.mBuffer.mData.isOn(i);
1586 mBuffer.mData.set(i, result);
1591template<Index Log2Dim>
1592template<
typename CombineOp,
typename OtherNodeT>
1597 for (
Index i = 0; i < SIZE; ++i) {
1599 mValueMask.set(i, b0.valueMask().isOn(i) || b1.valueMask().isOn(i));
1601 bool result =
false, b0Val = b0.mBuffer.mData.isOn(i), b1Val = b1.mBuffer.mData.isOn(i);
1608 mBuffer.mData.set(i, result);
General-purpose arithmetic and comparison routines, most of which accept arbitrary value types (or at...
This struct collects both input and output arguments to "grid combiner" functors used with the tree::...
Definition Types.h:569
CombineArgs & setARef(const AValueType &a)
Redirect the A value to a new external source.
Definition Types.h:621
CombineArgs & setBIsActive(bool b)
Set the active state of the B value.
Definition Types.h:637
CombineArgs & setResultRef(AValueType &val)
Redirect the result value to a new external destination.
Definition Types.h:625
CombineArgs & setBRef(const BValueType &b)
Redirect the B value to a new external source.
Definition Types.h:623
bool resultIsActive() const
Definition Types.h:632
CombineArgs & setAIsActive(bool b)
Set the active state of the A value.
Definition Types.h:635
Int32 ValueType
Definition Coord.h:32
Tag dispatch class that distinguishes constructors during file input.
Definition Types.h:689
Tag dispatch class that distinguishes topology copy constructors from deep copy constructors.
Definition Types.h:683
Axis-aligned bounding box of signed integer coordinates.
Definition Coord.h:251
const Coord & min() const
Definition Coord.h:323
bool hasOverlap(const CoordBBox &b) const
Return true if the given bounding box overlaps with this bounding box.
Definition Coord.h:414
const Coord & max() const
Definition Coord.h:324
bool isInside(const Coord &xyz) const
Return true if point (x, y, z) is inside this bounding box.
Definition Coord.h:402
void intersect(const CoordBBox &bbox)
Intersect this bounding box with the given bounding box.
Definition Coord.h:446
Signed (x, y, z) 32-bit integer coordinates.
Definition Coord.h:25
Int32 y() const
Definition Coord.h:131
Int32 x() const
Definition Coord.h:130
Int32 z() const
Definition Coord.h:132
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:38
static Coord offsetToLocalCoord(Index n)
Return the local coordinates for a linear table offset, where offset 0 has coordinates (0,...
Definition LeafNode.h:1020
void writeTopology(std::ostream &os, bool toHalf=false) const
Write out just the topology.
Definition LeafNode.h:1276
util::NodeMask< Log2Dim > NodeMaskType
Definition LeafNode.h:44
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:1204
bool operator!=(const LeafNode &other) const
Definition LeafNode.h:201
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:1231
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:1103
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:1696
LeafBuffer< ValueType, Log2Dim > Buffer
Definition LeafNode.h:42
Index medianOff(ValueType &value, ValueType *tmp=nullptr) const
Computes the median value of all the inactive voxels in this node.
Definition LeafNode.h:1553
~LeafNode()
Destructor.
Definition LeafNode.h:990
void readTopology(std::istream &is, bool fromHalf=false)
Read in just the topology.
Definition LeafNode.h:1268
Index medianOn(ValueType &value, ValueType *tmp=nullptr) const
Computes the median value of all the active voxels in this node.
Definition LeafNode.h:1529
void addTile(Index level, const Coord &, const ValueType &, bool)
Definition LeafNode.h:1580
void resetBackground(const ValueType &oldBackground, const ValueType &newBackground)
Replace inactive occurrences of oldBackground with newBackground, and inactive occurrences of -oldBac...
Definition LeafNode.h:1609
ValueType medianAll(ValueType *tmp=nullptr) const
Computes the median value of all the active AND inactive voxels in this node.
Definition LeafNode.h:1511
const Coord & origin() const
Definition LeafNode.h:173
void modifyValueAndActiveState(const Coord &xyz, const ModifyOp &op)
Apply a functor to the voxel at the given coordinates.
Definition LeafNode.h:456
LeafNode(const Coord &xyz, bool value, bool active)
Definition LeafNodeBool.h:768
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:1687
void clip(const CoordBBox &, const ValueType &background)
Set all voxels that lie outside the given axis-aligned box to the background.
Definition LeafNode.h:1121
static const Index DIM
Definition LeafNode.h:50
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:1095
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:1679
static Index coordToOffset(const Coord &xyz)
Return the linear table offset of the given global or local coordinates.
Definition LeafNode.h:1010
void setValueOff(const Coord &xyz)
Mark the voxel at the given coordinates as inactive but don't change its value.
Definition LeafNode.h:407
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:1467
void writeBuffers(std::ostream &os, bool toHalf=false) const
Write buffers to a stream.
Definition LeafNode.h:1402
void combine(const LeafNode &other, CombineOp &op)
Definition LeafNode.h:1720
Index64 memUsageIfLoaded() const
Definition LeafNode.h:1439
void combine2(const LeafNode &other, const OtherType &, bool valueIsActive, CombineOp &)
Definition LeafNode.h:1760
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:1161
const NodeMaskType & valueMask() const
Definition LeafNode.h:856
void readBuffers(std::istream &is, bool fromHalf=false)
Read buffers from a stream.
Definition LeafNode.h:1304
bool isConstant(ValueType &firstValue, bool &state, const ValueType &tolerance=zeroVal< ValueType >()) const
Definition LeafNode.h:1475
friend class LeafNode
Definition LeafNode.h:833
Coord offsetToGlobalCoord(Index n) const
Return the global coordinates for a linear table offset.
Definition LeafNode.h:1034
const ValueType & getValue(const Coord &xyz) const
Return the value of the voxel at the given coordinates.
Definition LeafNode.h:1045
static const Index LOG2DIM
Definition LeafNode.h:48
bool operator==(const LeafNode &other) const
Check for buffer, state and origin equivalence.
Definition LeafNode.h:1419
static const Index SIZE
Definition LeafNode.h:53
void evalActiveBoundingBox(CoordBBox &bbox, bool visitVoxels=true) const
Definition LeafNode.h:1449
void merge(const LeafNode &)
Definition LeafNode.h:1630
void setValueOn(const Coord &xyz)
Mark the voxel at the given coordinates as active but don't change its value.
Definition LeafNode.h:417
void addTileAndCache(Index, const Coord &, const ValueType &, bool, AccessorT &)
Definition LeafNode.h:1597
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:435
Index64 memUsage() const
Return the memory in bytes occupied by this node.
Definition LeafNode.h:1429
std::string str() const
Return a string representation of this node.
Definition LeafNode.h:997
bool probeValue(const Coord &xyz, ValueType &val) const
Return true if the voxel at the given coordinates is active.
Definition LeafNode.h:1061
OnMaskIterator< NodeMask > OnIterator
Definition NodeMasks.h:348
BBox< Coord > CoordBBox
Definition NanoVDB.h:2535
OPENVDB_API uint32_t getFormatVersion(std::ios_base &)
Return the file format version number associated with the given input stream.
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...
void readData(std::istream &is, T *data, Index count, uint32_t compression, DelayedLoadMetadata *metadata=nullptr, size_t metadataOffset=size_t(0))
Read data from a stream.
Definition Compression.h:247
bool operator==(const Vec3< T0 > &v0, const Vec3< T1 > &v1)
Equality operator, does exact floating point comparisons.
Definition Vec3.h:473
bool isZero(const Type &x)
Return true if x is exactly equal to zero.
Definition Math.h:337
bool operator!=(const Vec3< T0 > &v0, const Vec3< T1 > &v1)
Inequality operator, does exact floating point comparisons.
Definition Vec3.h:481
Definition TreeIterator.h:29
Index32 Index
Definition Types.h:54
@ OPENVDB_FILE_VERSION_BOOL_LEAF_OPTIMIZATION
Definition version.h.in:250
uint32_t Index32
Definition Types.h:52
int32_t Int32
Definition Types.h:56
uint64_t Index64
Definition Types.h:53
@ MERGE_NODES
Definition Types.h:508
@ MERGE_ACTIVE_STATES_AND_NODES
Definition Types.h:509
ValueType combine(const ValueType &v0, const ValueType &v1, const ValueType &v2, const openvdb::Vec3d &w)
Combine different value types.
Definition AttributeTransferUtil.h:141
Definition Exceptions.h:13
static pnanovdb_uint32_t allocate(pnanovdb_uint32_t *poffset, pnanovdb_uint32_t size, pnanovdb_uint32_t alignment)
Definition pnanovdb_validate_strides.h:20
typename std::remove_const< ValueT >::type NonConstValueType
Definition Iterator.h:184
DenseIteratorBase()
Definition Iterator.h:188
typename BaseT::NonConstValueType NonConstValueT
Definition LeafNode.h:261
DenseIteratorBase< MaskDenseIterator, DenseIter, LeafNode, void, bool > BaseT
Definition LeafNode.h:260
SparseIteratorBase< MaskOnIter, ValueIter, LeafNode, const bool > BaseT
Definition LeafNode.h:219
ChildIter(const MaskIterT &iter, NodeT *parent)
Definition LeafNodeBool.h:617
ChildIter()
Definition LeafNodeBool.h:616
bool getItem(Index pos, void *&child, NonConstValueT &value) const
Definition LeafNodeBool.h:631
DenseIter()
Definition LeafNodeBool.h:628
DenseIter(const MaskDenseIter &iter, NodeT *parent)
Definition LeafNodeBool.h:629
typename BaseT::NonConstValueType NonConstValueT
Definition LeafNodeBool.h:626
DenseIteratorBase< MaskDenseIter, DenseIter, NodeT, void, ValueT > BaseT
Definition LeafNodeBool.h:625
void unsetItem(Index pos, const ValueT &val) const
Definition LeafNodeBool.h:642
SameConfiguration<OtherNodeType>::value is true if and only if OtherNodeType is the type of a LeafNod...
Definition LeafNodeBool.h:55
static const bool value
Definition LeafNodeBool.h:56
ValueConverter<T>::Type is the type of a LeafNode having the same dimensions as this node but a diffe...
Definition LeafNodeBool.h:50
LeafNode< ValueType, Log2Dim > Type
Definition LeafNodeBool.h:50
void setItem(Index pos, bool value) const
Definition LeafNodeBool.h:599
void modifyValue(const ModifyOp &op) const
Definition LeafNodeBool.h:608
ValueIter(const MaskIterT &iter, NodeT *parent)
Definition LeafNodeBool.h:593
const bool & getValue() const
Definition LeafNodeBool.h:596
const bool & getItem(Index pos) const
Definition LeafNodeBool.h:595
ValueIter()
Definition LeafNodeBool.h:592
void setValue(bool value) const
Definition LeafNodeBool.h:601
void modifyItem(Index n, const ModifyOp &op) const
Definition LeafNodeBool.h:605
SparseIteratorBase< MaskIterT, ValueIter, NodeT, ValueT > BaseT
Definition LeafNodeBool.h:590
static const bool value
Definition LeafNode.h:890
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:212