OpenVDB  9.1.1
LeafNodeMask.h
Go to the documentation of this file.
1 // Copyright Contributors to the OpenVDB Project
2 // SPDX-License-Identifier: MPL-2.0
3 
4 #ifndef OPENVDB_TREE_LEAF_NODE_MASK_HAS_BEEN_INCLUDED
5 #define OPENVDB_TREE_LEAF_NODE_MASK_HAS_BEEN_INCLUDED
6 
7 #include <openvdb/version.h>
8 #include <openvdb/Types.h>
9 #include <openvdb/io/Compression.h> // for io::readData(), etc.
10 #include <openvdb/math/Math.h> // for math::isZero()
11 #include <openvdb/util/NodeMasks.h>
12 #include "LeafNode.h"
13 #include "Iterator.h"
14 #include <iostream>
15 #include <sstream>
16 #include <string>
17 #include <type_traits>
18 #include <vector>
19 
20 
21 namespace openvdb {
23 namespace OPENVDB_VERSION_NAME {
24 namespace tree {
25 
26 /// @brief LeafNode specialization for values of type ValueMask that encodes both
27 /// the active states and the boolean values of (2^Log2Dim)^3 voxels
28 /// in a single bit mask, i.e. voxel values and states are indistinguishable!
29 template<Index Log2Dim>
30 class LeafNode<ValueMask, Log2Dim>
31 {
32 public:
34  using BuildType = ValueMask;// this is a rare case where
35  using ValueType = bool;// value type != build type
36  using Buffer = LeafBuffer<ValueType, Log2Dim>;// buffer uses the bool specialization
39 
40  // These static declarations must be on separate lines to avoid VC9 compiler errors.
41  static const Index LOG2DIM = Log2Dim; // needed by parent nodes
42  static const Index TOTAL = Log2Dim; // needed by parent nodes
43  static const Index DIM = 1 << TOTAL; // dimension along one coordinate direction
44  static const Index NUM_VALUES = 1 << 3 * Log2Dim;
45  static const Index NUM_VOXELS = NUM_VALUES; // total number of voxels represented by this node
46  static const Index SIZE = NUM_VALUES;
47  static const Index LEVEL = 0; // level 0 = leaf
48 
49  /// @brief ValueConverter<T>::Type is the type of a LeafNode having the same
50  /// dimensions as this node but a different value type, T.
51  template<typename OtherValueType>
52  struct ValueConverter { using Type = LeafNode<OtherValueType, Log2Dim>; };
53 
54  /// @brief SameConfiguration<OtherNodeType>::value is @c true if and only if
55  /// OtherNodeType is the type of a LeafNode with the same dimensions as this node.
56  template<typename OtherNodeType>
57  struct SameConfiguration {
59  };
60 
61  /// Default constructor
62  LeafNode();
63 
64  /// Constructor
65  /// @param xyz the coordinates of a voxel that lies within the node
66  /// @param value the initial value = state for all of this node's voxels
67  /// @param dummy dummy value
68  explicit LeafNode(const Coord& xyz, bool value = false, bool dummy = false);
69 
70  /// "Partial creation" constructor used during file input
71  LeafNode(PartialCreate, const Coord& xyz, bool value = false, bool dummy = false);
72 
73  /// Deep copy constructor
74  LeafNode(const LeafNode&);
75 
76  /// Value conversion copy constructor
77  template<typename OtherValueType>
78  explicit LeafNode(const LeafNode<OtherValueType, Log2Dim>& other);
79 
80  /// Topology copy constructor
81  template<typename ValueType>
83 
84  //@{
85  /// @brief Topology copy constructor
86  /// @note This variant exists mainly to enable template instantiation.
87  template<typename ValueType>
88  LeafNode(const LeafNode<ValueType, Log2Dim>& other, bool offValue, bool onValue, TopologyCopy);
89  template<typename ValueType>
90  LeafNode(const LeafNode<ValueType, Log2Dim>& other, bool background, TopologyCopy);
91  //@}
92 
93  /// Destructor
94  ~LeafNode();
95 
96  //
97  // Statistics
98  //
99  /// Return log2 of the size of the buffer storage.
100  static Index log2dim() { return Log2Dim; }
101  /// Return the number of voxels in each dimension.
102  static Index dim() { return DIM; }
103  /// Return the total number of voxels represented by this LeafNode
104  static Index size() { return SIZE; }
105  /// Return the total number of voxels represented by this LeafNode
106  static Index numValues() { return SIZE; }
107  /// Return the level of this node, which by definition is zero for LeafNodes
108  static Index getLevel() { return LEVEL; }
109  /// Append the Log2Dim of this LeafNode to the specified vector
110  static void getNodeLog2Dims(std::vector<Index>& dims) { dims.push_back(Log2Dim); }
111  /// Return the dimension of child nodes of this LeafNode, which is one for voxels.
112  static Index getChildDim() { return 1; }
113  /// Return the leaf count for this node, which is one.
114  static Index32 leafCount() { return 1; }
115  /// no-op
116  void nodeCount(std::vector<Index32> &) const {}
117  /// Return the non-leaf count for this node, which is zero.
118  static Index32 nonLeafCount() { return 0; }
119 
120  /// Return the number of active voxels.
121  Index64 onVoxelCount() const { return mBuffer.mData.countOn(); }
122  /// Return the number of inactive voxels.
123  Index64 offVoxelCount() const { return mBuffer.mData.countOff(); }
124  Index64 onLeafVoxelCount() const { return this->onVoxelCount(); }
125  Index64 offLeafVoxelCount() const { return this->offVoxelCount(); }
126  static Index64 onTileCount() { return 0; }
127  static Index64 offTileCount() { return 0; }
128 
129  /// Return @c true if this node has no active voxels.
130  bool isEmpty() const { return mBuffer.mData.isOff(); }
131  /// Return @c true if this node only contains active voxels.
132  bool isDense() const { return mBuffer.mData.isOn(); }
133  /// @brief Return @c true if memory for this node's buffer has been allocated.
134  /// @details Currently, boolean leaf nodes don't support partial creation,
135  /// so this always returns @c true.
136  bool isAllocated() const { return true; }
137  /// @brief Allocate memory for this node's buffer if it has not already been allocated.
138  /// @details Currently, boolean leaf nodes don't support partial creation,
139  /// so this has no effect.
140  bool allocate() { return true; }
141 
142  /// Return the memory in bytes occupied by this node.
143  Index64 memUsage() const;
144  Index64 memUsageIfLoaded() const;
145 
146  /// Expand the given bounding box so that it includes this leaf node's active voxels.
147  /// If visitVoxels is false this LeafNode will be approximated as dense, i.e. with all
148  /// voxels active. Else the individual active voxels are visited to produce a tight bbox.
149  void evalActiveBoundingBox(CoordBBox& bbox, bool visitVoxels = true) const;
150 
151  /// @brief Return the bounding box of this node, i.e., the full index space
152  /// spanned by this leaf node.
153  CoordBBox getNodeBoundingBox() const { return CoordBBox::createCube(mOrigin, DIM); }
154 
155  /// Set the grid index coordinates of this node's local origin.
156  void setOrigin(const Coord& origin) { mOrigin = origin; }
157  //@{
158  /// Return the grid index coordinates of this node's local origin.
159  const Coord& origin() const { return mOrigin; }
160  void getOrigin(Coord& origin) const { origin = mOrigin; }
161  void getOrigin(Int32& x, Int32& y, Int32& z) const { mOrigin.asXYZ(x, y, z); }
162  //@}
163 
164  /// Return the linear table offset of the given global or local coordinates.
165  static Index coordToOffset(const Coord& xyz);
166  /// @brief Return the local coordinates for a linear table offset,
167  /// where offset 0 has coordinates (0, 0, 0).
168  static Coord offsetToLocalCoord(Index n);
169  /// Return the global coordinates for a linear table offset.
170  Coord offsetToGlobalCoord(Index n) const;
171 
172 #if OPENVDB_ABI_VERSION_NUMBER >= 9
173  /// Return the transient data value.
174  Index32 transientData() const { return mTransientData; }
175  /// Set the transient data value.
176  void setTransientData(Index32 transientData) { mTransientData = transientData; }
177 #endif
178 
179  /// Return a string representation of this node.
180  std::string str() const;
181 
182  /// @brief Return @c true if the given node (which may have a different @c ValueType
183  /// than this node) has the same active value topology as this node.
184  template<typename OtherType, Index OtherLog2Dim>
185  bool hasSameTopology(const LeafNode<OtherType, OtherLog2Dim>* other) const;
186 
187  /// Check for buffer equivalence by value.
188  bool operator==(const LeafNode&) const;
189  bool operator!=(const LeafNode&) const;
190 
191  //
192  // Buffer management
193  //
194  /// @brief Exchange this node's data buffer with the given data buffer
195  /// without changing the active states of the values.
196  void swap(Buffer& other) { mBuffer.swap(other); }
197  const Buffer& buffer() const { return mBuffer; }
198  Buffer& buffer() { return mBuffer; }
199 
200  //
201  // I/O methods
202  //
203  /// Read in just the topology.
204  void readTopology(std::istream&, bool fromHalf = false);
205  /// Write out just the topology.
206  void writeTopology(std::ostream&, bool toHalf = false) const;
207 
208  /// Read in the topology and the origin.
209  void readBuffers(std::istream&, bool fromHalf = false);
210  void readBuffers(std::istream& is, const CoordBBox&, bool fromHalf = false);
211  /// Write out the topology and the origin.
212  void writeBuffers(std::ostream&, bool toHalf = false) const;
213 
214  //
215  // Accessor methods
216  //
217  /// Return the value of the voxel at the given coordinates.
218  const bool& getValue(const Coord& xyz) const;
219  /// Return the value of the voxel at the given offset.
220  const bool& getValue(Index offset) const;
221 
222  /// @brief Return @c true if the voxel at the given coordinates is active.
223  /// @param xyz the coordinates of the voxel to be probed
224  /// @param[out] val the value of the voxel at the given coordinates
225  bool probeValue(const Coord& xyz, bool& val) const;
226 
227  /// Return the level (0) at which leaf node values reside.
228  static Index getValueLevel(const Coord&) { return LEVEL; }
229 
230  /// Set the active state of the voxel at the given coordinates but don't change its value.
231  void setActiveState(const Coord& xyz, bool on);
232  /// Set the active state of the voxel at the given offset but don't change its value.
233  void setActiveState(Index offset, bool on) { assert(offset<SIZE); mBuffer.mData.set(offset, on); }
234 
235  /// Set the value of the voxel at the given coordinates but don't change its active state.
236  void setValueOnly(const Coord& xyz, bool val);
237  /// Set the value of the voxel at the given offset but don't change its active state.
238  void setValueOnly(Index offset, bool val) { assert(offset<SIZE); mBuffer.setValue(offset,val); }
239 
240  /// Mark the voxel at the given coordinates as inactive but don't change its value.
241  void setValueOff(const Coord& xyz) { mBuffer.mData.setOff(this->coordToOffset(xyz)); }
242  /// Mark the voxel at the given offset as inactive but don't change its value.
243  void setValueOff(Index offset) { assert(offset < SIZE); mBuffer.mData.setOff(offset); }
244 
245  /// Set the value of the voxel at the given coordinates and mark the voxel as inactive.
246  void setValueOff(const Coord& xyz, bool val);
247  /// Set the value of the voxel at the given offset and mark the voxel as inactive.
248  void setValueOff(Index offset, bool val);
249 
250  /// Mark the voxel at the given coordinates as active but don't change its value.
251  void setValueOn(const Coord& xyz) { mBuffer.mData.setOn(this->coordToOffset(xyz)); }
252  /// Mark the voxel at the given offset as active but don't change its value.
253  void setValueOn(Index offset) { assert(offset < SIZE); mBuffer.mData.setOn(offset); }
254 
255  /// Set the value of the voxel at the given coordinates and mark the voxel as active.
256  void setValueOn(const Coord& xyz, bool val);
257  /// Set the value of the voxel at the given coordinates and mark the voxel as active.
258  void setValue(const Coord& xyz, bool val) { this->setValueOn(xyz, val); }
259  /// Set the value of the voxel at the given offset and mark the voxel as active.
260  void setValueOn(Index offset, bool val);
261 
262  /// @brief Apply a functor to the value of the voxel at the given offset
263  /// and mark the voxel as active.
264  template<typename ModifyOp>
265  void modifyValue(Index offset, const ModifyOp& op);
266  /// @brief Apply a functor to the value of the voxel at the given coordinates
267  /// and mark the voxel as active.
268  template<typename ModifyOp>
269  void modifyValue(const Coord& xyz, const ModifyOp& op);
270 
271  /// Apply a functor to the voxel at the given coordinates.
272  template<typename ModifyOp>
273  void modifyValueAndActiveState(const Coord& xyz, const ModifyOp& op);
274 
275  /// Mark all voxels as active but don't change their values.
276  void setValuesOn() { mBuffer.mData.setOn(); }
277  /// Mark all voxels as inactive but don't change their values.
278  void setValuesOff() { mBuffer.mData.setOff(); }
279 
280  /// Return @c true if the voxel at the given coordinates is active.
281  bool isValueOn(const Coord& xyz) const { return mBuffer.mData.isOn(this->coordToOffset(xyz)); }
282  /// Return @c true if the voxel at the given offset is active.
283  bool isValueOn(Index offset) const { assert(offset < SIZE); return mBuffer.mData.isOn(offset); }
284 
285  /// Return @c false since leaf nodes never contain tiles.
286  static bool hasActiveTiles() { return false; }
287 
288  /// Set all voxels that lie outside the given axis-aligned box to the background.
289  void clip(const CoordBBox&, bool background);
290 
291  /// Set all voxels within an axis-aligned box to the specified value.
292  void fill(const CoordBBox& bbox, bool value, bool = false);
293  /// Set all voxels within an axis-aligned box to the specified value.
294  void denseFill(const CoordBBox& bbox, bool value, bool = false) { this->fill(bbox, value); }
295 
296  /// Set the state of all voxels to the specified active state.
297  void fill(const bool& value, bool dummy = false);
298 
299  /// @brief Copy into a dense grid the values of the voxels that lie within
300  /// a given bounding box.
301  ///
302  /// @param bbox inclusive bounding box of the voxels to be copied into the dense grid
303  /// @param dense dense grid with a stride in @e z of one (see tools::Dense
304  /// in tools/Dense.h for the required API)
305  ///
306  /// @note @a bbox is assumed to be identical to or contained in the coordinate domains
307  /// of both the dense grid and this node, i.e., no bounds checking is performed.
308  /// @note Consider using tools::CopyToDense in tools/Dense.h
309  /// instead of calling this method directly.
310  template<typename DenseT>
311  void copyToDense(const CoordBBox& bbox, DenseT& dense) const;
312 
313  /// @brief Copy from a dense grid into this node the values of the voxels
314  /// that lie within a given bounding box.
315  /// @details Only values that are different (by more than the given tolerance)
316  /// from the background value will be active. Other values are inactive
317  /// and truncated to the background value.
318  ///
319  /// @param bbox inclusive bounding box of the voxels to be copied into this node
320  /// @param dense dense grid with a stride in @e z of one (see tools::Dense
321  /// in tools/Dense.h for the required API)
322  /// @param background background value of the tree that this node belongs to
323  /// @param tolerance tolerance within which a value equals the background value
324  ///
325  /// @note @a bbox is assumed to be identical to or contained in the coordinate domains
326  /// of both the dense grid and this node, i.e., no bounds checking is performed.
327  /// @note Consider using tools::CopyFromDense in tools/Dense.h
328  /// instead of calling this method directly.
329  template<typename DenseT>
330  void copyFromDense(const CoordBBox& bbox, const DenseT& dense, bool background, bool tolerance);
331 
332  /// @brief Return the value of the voxel at the given coordinates.
333  /// @note Used internally by ValueAccessor.
334  template<typename AccessorT>
335  const bool& getValueAndCache(const Coord& xyz, AccessorT&) const {return this->getValue(xyz);}
336 
337  /// @brief Return @c true if the voxel at the given coordinates is active.
338  /// @note Used internally by ValueAccessor.
339  template<typename AccessorT>
340  bool isValueOnAndCache(const Coord& xyz, AccessorT&) const { return this->isValueOn(xyz); }
341 
342  /// @brief Change the value of the voxel at the given coordinates and mark it as active.
343  /// @note Used internally by ValueAccessor.
344  template<typename AccessorT>
345  void setValueAndCache(const Coord& xyz, bool val, AccessorT&) { this->setValueOn(xyz, val); }
346 
347  /// @brief Change the value of the voxel at the given coordinates
348  /// but preserve its state.
349  /// @note Used internally by ValueAccessor.
350  template<typename AccessorT>
351  void setValueOnlyAndCache(const Coord& xyz, bool val, AccessorT&) {this->setValueOnly(xyz,val);}
352 
353  /// @brief Change the value of the voxel at the given coordinates and mark it as inactive.
354  /// @note Used internally by ValueAccessor.
355  template<typename AccessorT>
356  void setValueOffAndCache(const Coord& xyz, bool value, AccessorT&)
357  {
358  this->setValueOff(xyz, value);
359  }
360 
361  /// @brief Apply a functor to the value of the voxel at the given coordinates
362  /// and mark the voxel as active.
363  /// @note Used internally by ValueAccessor.
364  template<typename ModifyOp, typename AccessorT>
365  void modifyValueAndCache(const Coord& xyz, const ModifyOp& op, AccessorT&)
366  {
367  this->modifyValue(xyz, op);
368  }
369 
370  /// Apply a functor to the voxel at the given coordinates.
371  /// @note Used internally by ValueAccessor.
372  template<typename ModifyOp, typename AccessorT>
373  void modifyValueAndActiveStateAndCache(const Coord& xyz, const ModifyOp& op, AccessorT&)
374  {
375  this->modifyValueAndActiveState(xyz, op);
376  }
377 
378  /// @brief Set the active state of the voxel at the given coordinates
379  /// without changing its value.
380  /// @note Used internally by ValueAccessor.
381  template<typename AccessorT>
382  void setActiveStateAndCache(const Coord& xyz, bool on, AccessorT&)
383  {
384  this->setActiveState(xyz, on);
385  }
386 
387  /// @brief Return @c true if the voxel at the given coordinates is active
388  /// and return the voxel value in @a val.
389  /// @note Used internally by ValueAccessor.
390  template<typename AccessorT>
391  bool probeValueAndCache(const Coord& xyz, bool& val, AccessorT&) const
392  {
393  return this->probeValue(xyz, val);
394  }
395 
396  /// @brief Return the LEVEL (=0) at which leaf node values reside.
397  /// @note Used internally by ValueAccessor.
398  template<typename AccessorT>
399  static Index getValueLevelAndCache(const Coord&, AccessorT&) { return LEVEL; }
400 
401  /// @brief Return a const reference to the first entry in the buffer.
402  /// @note Since it's actually a reference to a static data member
403  /// it should not be converted to a non-const pointer!
404  const bool& getFirstValue() const { if (mBuffer.mData.isOn(0)) return Buffer::sOn; else return Buffer::sOff; }
405  /// @brief Return a const reference to the last entry in the buffer.
406  /// @note Since it's actually a reference to a static data member
407  /// it should not be converted to a non-const pointer!
408  const bool& getLastValue() const { if (mBuffer.mData.isOn(SIZE-1)) return Buffer::sOn; else return Buffer::sOff; }
409 
410  /// Return @c true if all of this node's voxels have the same active state
411  /// and are equal to within the given tolerance, and return the value in
412  /// @a constValue and the active state in @a state.
413  bool isConstant(bool& constValue, bool& state, bool tolerance = 0) const;
414 
415  /// @brief Computes the median value of all the active and inactive voxels in this node.
416  /// @return The median value.
417  ///
418  /// @details The median for boolean values is defined as the mode
419  /// of the values, i.e. the value that occurs most often.
420  bool medianAll() const;
421 
422  /// @brief Computes the median value of all the active voxels in this node.
423  /// @return The number of active voxels.
424  ///
425  /// @param value Updated with the median value of all the active voxels.
426  ///
427  /// @note Since the value and state are shared for this
428  /// specialization of the LeafNode the @a value will always be true!
429  Index medianOn(ValueType &value) const;
430 
431  /// @brief Computes the median value of all the inactive voxels in this node.
432  /// @return The number of inactive voxels.
433  ///
434  /// @param value Updated with the median value of all the inactive
435  /// voxels.
436  ///
437  /// @note Since the value and state are shared for this
438  /// specialization of the LeafNode the @a value will always be false!
439  Index medianOff(ValueType &value) const;
440 
441  /// Return @c true if all of this node's values are inactive.
442  bool isInactive() const { return mBuffer.mData.isOff(); }
443 
444  /// @brief no-op since for this template specialization voxel
445  /// values and states are indistinguishable.
446  void resetBackground(bool, bool) {}
447 
448  /// @brief Invert the bits of the voxels, i.e. states and values
449  void negate() { mBuffer.mData.toggle(); }
450 
451  template<MergePolicy Policy>
452  void merge(const LeafNode& other, bool bg = false, bool otherBG = false);
453  template<MergePolicy Policy> void merge(bool tileValue, bool tileActive=false);
454 
455  /// @brief No-op
456  /// @details This function exists only to enable template instantiation.
457  void voxelizeActiveTiles(bool = true) {}
458 
459  /// @brief Union this node's set of active values with the active values
460  /// of the other node, whose @c ValueType may be different. So a
461  /// resulting voxel will be active if either of the original voxels
462  /// were active.
463  ///
464  /// @note This operation modifies only active states, not values.
465  template<typename OtherType>
466  void topologyUnion(const LeafNode<OtherType, Log2Dim>& other, const bool preserveTiles = false);
467 
468  /// @brief Intersect this node's set of active values with the active values
469  /// of the other node, whose @c ValueType may be different. So a
470  /// resulting voxel will be active only if both of the original voxels
471  /// were active.
472  ///
473  /// @details The last dummy argument is required to match the signature
474  /// for InternalNode::topologyIntersection.
475  ///
476  /// @note This operation modifies only active states, not
477  /// values. Also note that this operation can result in all voxels
478  /// being inactive so consider subsequently calling prune.
479  template<typename OtherType>
480  void topologyIntersection(const LeafNode<OtherType, Log2Dim>& other, const bool&);
481 
482  /// @brief Difference this node's set of active values with the active values
483  /// of the other node, whose @c ValueType may be different. So a
484  /// resulting voxel will be active only if the original voxel is
485  /// active in this LeafNode and inactive in the other LeafNode.
486  ///
487  /// @details The last dummy argument is required to match the signature
488  /// for InternalNode::topologyDifference.
489  ///
490  /// @note This operation modifies only active states, not values.
491  /// Also, because it can deactivate all of this node's voxels,
492  /// consider subsequently calling prune.
493  template<typename OtherType>
494  void topologyDifference(const LeafNode<OtherType, Log2Dim>& other, const bool&);
495 
496  template<typename CombineOp>
497  void combine(const LeafNode& other, CombineOp& op);
498  template<typename CombineOp>
499  void combine(bool, bool valueIsActive, CombineOp& op);
500 
501  template<typename CombineOp, typename OtherType /*= bool*/>
502  void combine2(const LeafNode& other, const OtherType&, bool valueIsActive, CombineOp&);
503  template<typename CombineOp, typename OtherNodeT /*= LeafNode*/>
504  void combine2(bool, const OtherNodeT& other, bool valueIsActive, CombineOp&);
505  template<typename CombineOp, typename OtherNodeT /*= LeafNode*/>
506  void combine2(const LeafNode& b0, const OtherNodeT& b1, CombineOp&);
507 
508  /// @brief Calls the templated functor BBoxOp with bounding box information.
509  /// An additional level argument is provided to the callback.
510  ///
511  /// @note The bounding boxes are guaranteed to be non-overlapping.
512  template<typename BBoxOp> void visitActiveBBox(BBoxOp&) const;
513 
514  template<typename VisitorOp> void visit(VisitorOp&);
515  template<typename VisitorOp> void visit(VisitorOp&) const;
516 
517  template<typename OtherLeafNodeType, typename VisitorOp>
518  void visit2Node(OtherLeafNodeType& other, VisitorOp&);
519  template<typename OtherLeafNodeType, typename VisitorOp>
520  void visit2Node(OtherLeafNodeType& other, VisitorOp&) const;
521  template<typename IterT, typename VisitorOp>
522  void visit2(IterT& otherIter, VisitorOp&, bool otherIsLHS = false);
523  template<typename IterT, typename VisitorOp>
524  void visit2(IterT& otherIter, VisitorOp&, bool otherIsLHS = false) const;
525 
526  //@{
527  /// This function exists only to enable template instantiation.
528  void prune(const ValueType& /*tolerance*/ = zeroVal<ValueType>()) {}
529  void addLeaf(LeafNode*) {}
530  template<typename AccessorT>
531  void addLeafAndCache(LeafNode*, AccessorT&) {}
532  template<typename NodeT>
533  NodeT* stealNode(const Coord&, const ValueType&, bool) { return nullptr; }
534  template<typename NodeT>
535  NodeT* probeNode(const Coord&) { return nullptr; }
536  template<typename NodeT>
537  const NodeT* probeConstNode(const Coord&) const { return nullptr; }
538  template<typename ArrayT> void getNodes(ArrayT&) const {}
539  template<typename ArrayT> void stealNodes(ArrayT&, const ValueType&, bool) {}
540  //@}
541 
542  void addTile(Index level, const Coord&, bool val, bool active);
543  void addTile(Index offset, bool val, bool active);
544  template<typename AccessorT>
545  void addTileAndCache(Index level, const Coord&, bool val, bool active, AccessorT&);
546 
547  //@{
548  /// @brief Return a pointer to this node.
549  LeafNode* touchLeaf(const Coord&) { return this; }
550  template<typename AccessorT>
551  LeafNode* touchLeafAndCache(const Coord&, AccessorT&) { return this; }
552  LeafNode* probeLeaf(const Coord&) { return this; }
553  template<typename AccessorT>
554  LeafNode* probeLeafAndCache(const Coord&, AccessorT&) { return this; }
555  template<typename NodeT, typename AccessorT>
556  NodeT* probeNodeAndCache(const Coord&, AccessorT&)
557  {
559  if (!(std::is_same<NodeT, LeafNode>::value)) return nullptr;
560  return reinterpret_cast<NodeT*>(this);
562  }
563  //@}
564  //@{
565  /// @brief Return a @const pointer to this node.
566  const LeafNode* probeLeaf(const Coord&) const { return this; }
567  template<typename AccessorT>
568  const LeafNode* probeLeafAndCache(const Coord&, AccessorT&) const { return this; }
569  const LeafNode* probeConstLeaf(const Coord&) const { return this; }
570  template<typename AccessorT>
571  const LeafNode* probeConstLeafAndCache(const Coord&, AccessorT&) const { return this; }
572  template<typename NodeT, typename AccessorT>
573  const NodeT* probeConstNodeAndCache(const Coord&, AccessorT&) const
574  {
576  if (!(std::is_same<NodeT, LeafNode>::value)) return nullptr;
577  return reinterpret_cast<const NodeT*>(this);
579  }
580  //@}
581 
582  //
583  // Iterators
584  //
585 protected:
589 
590  template<typename MaskIterT, typename NodeT, typename ValueT>
591  struct ValueIter:
592  // Derives from SparseIteratorBase, but can also be used as a dense iterator,
593  // if MaskIterT is a dense mask iterator type.
594  public SparseIteratorBase<MaskIterT, ValueIter<MaskIterT, NodeT, ValueT>, NodeT, ValueT>
595  {
597 
599  ValueIter(const MaskIterT& iter, NodeT* parent): BaseT(iter, parent) {}
600 
601  const bool& getItem(Index pos) const { return this->parent().getValue(pos); }
602  const bool& getValue() const { return this->getItem(this->pos()); }
603 
604  // Note: setItem() can't be called on const iterators.
605  void setItem(Index pos, bool value) const { this->parent().setValueOnly(pos, value); }
606  // Note: setValue() can't be called on const iterators.
607  void setValue(bool value) const { this->setItem(this->pos(), value); }
608 
609  // Note: modifyItem() can't be called on const iterators.
610  template<typename ModifyOp>
611  void modifyItem(Index n, const ModifyOp& op) const { this->parent().modifyValue(n, op); }
612  // Note: modifyValue() can't be called on const iterators.
613  template<typename ModifyOp>
614  void modifyValue(const ModifyOp& op) const { this->modifyItem(this->pos(), op); }
615  };
616 
617  /// Leaf nodes have no children, so their child iterators have no get/set accessors.
618  template<typename MaskIterT, typename NodeT>
619  struct ChildIter:
620  public SparseIteratorBase<MaskIterT, ChildIter<MaskIterT, NodeT>, NodeT, bool>
621  {
623  ChildIter(const MaskIterT& iter, NodeT* parent): SparseIteratorBase<
624  MaskIterT, ChildIter<MaskIterT, NodeT>, NodeT, bool>(iter, parent) {}
625  };
626 
627  template<typename NodeT, typename ValueT>
628  struct DenseIter: public DenseIteratorBase<
629  MaskDenseIter, DenseIter<NodeT, ValueT>, NodeT, /*ChildT=*/void, ValueT>
630  {
633 
635  DenseIter(const MaskDenseIter& iter, NodeT* parent): BaseT(iter, parent) {}
636 
637  bool getItem(Index pos, void*& child, NonConstValueT& value) const
638  {
639  value = this->parent().getValue(pos);
640  child = nullptr;
641  return false; // no child
642  }
643 
644  // Note: setItem() can't be called on const iterators.
645  //void setItem(Index pos, void* child) const {}
646 
647  // Note: unsetItem() can't be called on const iterators.
648  void unsetItem(Index pos, const ValueT& val) const {this->parent().setValueOnly(pos, val);}
649  };
650 
651 public:
652  using ValueOnIter = ValueIter<MaskOnIter, LeafNode, const bool>;
653  using ValueOnCIter = ValueIter<MaskOnIter, const LeafNode, const bool>;
654  using ValueOffIter = ValueIter<MaskOffIter, LeafNode, const bool>;
655  using ValueOffCIter = ValueIter<MaskOffIter, const LeafNode, const bool>;
656  using ValueAllIter = ValueIter<MaskDenseIter, LeafNode, const bool>;
657  using ValueAllCIter = ValueIter<MaskDenseIter, const LeafNode, const bool>;
658  using ChildOnIter = ChildIter<MaskOnIter, LeafNode>;
659  using ChildOnCIter = ChildIter<MaskOnIter, const LeafNode>;
660  using ChildOffIter = ChildIter<MaskOffIter, LeafNode>;
661  using ChildOffCIter = ChildIter<MaskOffIter, const LeafNode>;
662  using ChildAllIter = DenseIter<LeafNode, bool>;
663  using ChildAllCIter = DenseIter<const LeafNode, const bool>;
664 
665  ValueOnCIter cbeginValueOn() const { return ValueOnCIter(mBuffer.mData.beginOn(), this); }
666  ValueOnCIter beginValueOn() const { return ValueOnCIter(mBuffer.mData.beginOn(), this); }
667  ValueOnIter beginValueOn() { return ValueOnIter(mBuffer.mData.beginOn(), this); }
668  ValueOffCIter cbeginValueOff() const { return ValueOffCIter(mBuffer.mData.beginOff(), this); }
669  ValueOffCIter beginValueOff() const { return ValueOffCIter(mBuffer.mData.beginOff(), this); }
670  ValueOffIter beginValueOff() { return ValueOffIter(mBuffer.mData.beginOff(), this); }
671  ValueAllCIter cbeginValueAll() const { return ValueAllCIter(mBuffer.mData.beginDense(), this); }
672  ValueAllCIter beginValueAll() const { return ValueAllCIter(mBuffer.mData.beginDense(), this); }
673  ValueAllIter beginValueAll() { return ValueAllIter(mBuffer.mData.beginDense(), this); }
674 
675  ValueOnCIter cendValueOn() const { return ValueOnCIter(mBuffer.mData.endOn(), this); }
676  ValueOnCIter endValueOn() const { return ValueOnCIter(mBuffer.mData.endOn(), this); }
677  ValueOnIter endValueOn() { return ValueOnIter(mBuffer.mData.endOn(), this); }
678  ValueOffCIter cendValueOff() const { return ValueOffCIter(mBuffer.mData.endOff(), this); }
679  ValueOffCIter endValueOff() const { return ValueOffCIter(mBuffer.mData.endOff(), this); }
680  ValueOffIter endValueOff() { return ValueOffIter(mBuffer.mData.endOff(), this); }
681  ValueAllCIter cendValueAll() const { return ValueAllCIter(mBuffer.mData.endDense(), this); }
682  ValueAllCIter endValueAll() const { return ValueAllCIter(mBuffer.mData.endDense(), this); }
683  ValueAllIter endValueAll() { return ValueAllIter(mBuffer.mData.endDense(), this); }
684 
685  // Note that [c]beginChildOn() and [c]beginChildOff() actually return end iterators,
686  // because leaf nodes have no children.
687  ChildOnCIter cbeginChildOn() const { return ChildOnCIter(mBuffer.mData.endOn(), this); }
688  ChildOnCIter beginChildOn() const { return ChildOnCIter(mBuffer.mData.endOn(), this); }
689  ChildOnIter beginChildOn() { return ChildOnIter(mBuffer.mData.endOn(), this); }
690  ChildOffCIter cbeginChildOff() const { return ChildOffCIter(mBuffer.mData.endOff(), this); }
691  ChildOffCIter beginChildOff() const { return ChildOffCIter(mBuffer.mData.endOff(), this); }
692  ChildOffIter beginChildOff() { return ChildOffIter(mBuffer.mData.endOff(), this); }
693  ChildAllCIter cbeginChildAll() const { return ChildAllCIter(mBuffer.mData.beginDense(), this); }
694  ChildAllCIter beginChildAll() const { return ChildAllCIter(mBuffer.mData.beginDense(), this); }
695  ChildAllIter beginChildAll() { return ChildAllIter(mBuffer.mData.beginDense(), this); }
696 
697  ChildOnCIter cendChildOn() const { return ChildOnCIter(mBuffer.mData.endOn(), this); }
698  ChildOnCIter endChildOn() const { return ChildOnCIter(mBuffer.mData.endOn(), this); }
699  ChildOnIter endChildOn() { return ChildOnIter(mBuffer.mData.endOn(), this); }
700  ChildOffCIter cendChildOff() const { return ChildOffCIter(mBuffer.mData.endOff(), this); }
701  ChildOffCIter endChildOff() const { return ChildOffCIter(mBuffer.mData.endOff(), this); }
702  ChildOffIter endChildOff() { return ChildOffIter(mBuffer.mData.endOff(), this); }
703  ChildAllCIter cendChildAll() const { return ChildAllCIter(mBuffer.mData.endDense(), this); }
704  ChildAllCIter endChildAll() const { return ChildAllCIter(mBuffer.mData.endDense(), this); }
705  ChildAllIter endChildAll() { return ChildAllIter(mBuffer.mData.endDense(), this); }
706 
707  //
708  // Mask accessors
709  //
710  bool isValueMaskOn(Index n) const { return mBuffer.mData.isOn(n); }
711  bool isValueMaskOn() const { return mBuffer.mData.isOn(); }
712  bool isValueMaskOff(Index n) const { return mBuffer.mData.isOff(n); }
713  bool isValueMaskOff() const { return mBuffer.mData.isOff(); }
714  const NodeMaskType& getValueMask() const { return mBuffer.mData; }
715  const NodeMaskType& valueMask() const { return mBuffer.mData; }
716  NodeMaskType& getValueMask() { return mBuffer.mData; }
717  void setValueMask(const NodeMaskType& mask) { mBuffer.mData = mask; }
718  bool isChildMaskOn(Index) const { return false; } // leaf nodes have no children
719  bool isChildMaskOff(Index) const { return true; }
720  bool isChildMaskOff() const { return true; }
721 protected:
722  void setValueMask(Index n, bool on) { mBuffer.mData.set(n, on); }
723  void setValueMaskOn(Index n) { mBuffer.mData.setOn(n); }
724  void setValueMaskOff(Index n) { mBuffer.mData.setOff(n); }
725 
726  /// Compute the origin of the leaf node that contains the voxel with the given coordinates.
727  static void evalNodeOrigin(Coord& xyz) { xyz &= ~(DIM - 1); }
728 
729  template<typename NodeT, typename VisitorOp, typename ChildAllIterT>
730  static inline void doVisit(NodeT&, VisitorOp&);
731 
732  template<typename NodeT, typename OtherNodeT, typename VisitorOp,
733  typename ChildAllIterT, typename OtherChildAllIterT>
734  static inline void doVisit2Node(NodeT& self, OtherNodeT& other, VisitorOp&);
735 
736  template<typename NodeT, typename VisitorOp,
737  typename ChildAllIterT, typename OtherChildAllIterT>
738  static inline void doVisit2(NodeT& self, OtherChildAllIterT&, VisitorOp&, bool otherIsLHS);
739 
740  /// Bitmask representing the values AND state of voxels
742  /// Global grid index coordinates (x,y,z) of the local origin of this node
743  Coord mOrigin;
744 #if OPENVDB_ABI_VERSION_NUMBER >= 9
745  /// Transient data (not serialized)
746  Index32 mTransientData = 0;
747 #endif
748 
749 private:
750  /// @brief During topology-only construction, access is needed
751  /// to protected/private members of other template instances.
752  template<typename, Index> friend class LeafNode;
753 
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>;
760 
761  //@{
762  /// Allow iterators to call mask accessor methods (see below).
763  /// @todo Make mask accessors public?
767  //@}
768 
769  template<typename, Index> friend class LeafBuffer;
770 
771 }; // class LeafNode<ValueMask>
772 
773 
774 ////////////////////////////////////////
775 
776 
777 template<Index Log2Dim>
778 inline
780  : mOrigin(0, 0, 0)
781 {
782 }
783 
784 template<Index Log2Dim>
785 inline
786 LeafNode<ValueMask, Log2Dim>::LeafNode(const Coord& xyz, bool value, bool active)
787  : mBuffer(value || active)
788  , mOrigin(xyz & (~(DIM - 1)))
789 {
790 }
791 
792 
793 template<Index Log2Dim>
794 inline
795 LeafNode<ValueMask, Log2Dim>::LeafNode(PartialCreate, const Coord& xyz, bool value, bool active)
796  : mBuffer(value || active)
797  , mOrigin(xyz & (~(DIM - 1)))
798 {
799 }
800 
801 
802 template<Index Log2Dim>
803 inline
805  : mBuffer(other.mBuffer)
806  , mOrigin(other.mOrigin)
807 #if OPENVDB_ABI_VERSION_NUMBER >= 9
808  , mTransientData(other.mTransientData)
809 #endif
810 {
811 }
812 
813 
814 // Copy-construct from a leaf node with the same configuration but a different ValueType.
815 template<Index Log2Dim>
816 template<typename ValueT>
817 inline
819  : mBuffer(other.valueMask())
820  , mOrigin(other.origin())
821 #if OPENVDB_ABI_VERSION_NUMBER >= 9
822  , mTransientData(other.mTransientData)
823 #endif
824 {
825 }
826 
827 
828 template<Index Log2Dim>
829 template<typename ValueT>
830 inline
832  bool, TopologyCopy)
833  : mBuffer(other.valueMask())// value = active state
834  , mOrigin(other.origin())
835 #if OPENVDB_ABI_VERSION_NUMBER >= 9
836  , mTransientData(other.mTransientData)
837 #endif
838 {
839 }
840 
841 
842 template<Index Log2Dim>
843 template<typename ValueT>
844 inline
846  : mBuffer(other.valueMask())// value = active state
847  , mOrigin(other.origin())
848 #if OPENVDB_ABI_VERSION_NUMBER >= 9
849  , mTransientData(other.mTransientData)
850 #endif
851 {
852 }
853 
854 
855 template<Index Log2Dim>
856 template<typename ValueT>
857 inline
859  bool offValue, bool onValue, TopologyCopy)
860  : mBuffer(other.valueMask())
861  , mOrigin(other.origin())
862 #if OPENVDB_ABI_VERSION_NUMBER >= 9
863  , mTransientData(other.mTransientData)
864 #endif
865 {
866  if (offValue==true) {
867  if (onValue==false) {
868  mBuffer.mData.toggle();
869  } else {
870  mBuffer.mData.setOn();
871  }
872  }
873 }
874 
875 
876 template<Index Log2Dim>
877 inline
879 {
880 }
881 
882 
883 ////////////////////////////////////////
884 
885 
886 template<Index Log2Dim>
887 inline Index64
889 {
890  // Use sizeof(*this) to capture alignment-related padding
891  return sizeof(*this);
892 }
893 
894 
895 template<Index Log2Dim>
896 inline Index64
898 {
899  // Use sizeof(*this) to capture alignment-related padding
900  return sizeof(*this);
901 }
902 
903 
904 template<Index Log2Dim>
905 inline void
907 {
908  CoordBBox this_bbox = this->getNodeBoundingBox();
909  if (bbox.isInside(this_bbox)) return;//this LeafNode is already enclosed in the bbox
910  if (ValueOnCIter iter = this->cbeginValueOn()) {//any active values?
911  if (visitVoxels) {//use voxel granularity?
912  this_bbox.reset();
913  for(; iter; ++iter) this_bbox.expand(this->offsetToLocalCoord(iter.pos()));
914  this_bbox.translate(this->origin());
915  }
916  bbox.expand(this_bbox);
917  }
918 }
919 
920 
921 template<Index Log2Dim>
922 template<typename OtherType, Index OtherLog2Dim>
923 inline bool
925 {
926  assert(other);
927  return (Log2Dim == OtherLog2Dim && mBuffer.mData == other->getValueMask());
928 }
929 
930 
931 template<Index Log2Dim>
932 inline std::string
934 {
935  std::ostringstream ostr;
936  ostr << "LeafNode @" << mOrigin << ": ";
937  for (Index32 n = 0; n < SIZE; ++n) ostr << (mBuffer.mData.isOn(n) ? '#' : '.');
938  return ostr.str();
939 }
940 
941 
942 ////////////////////////////////////////
943 
944 
945 template<Index Log2Dim>
946 inline Index
948 {
949  assert ((xyz[0] & (DIM-1u)) < DIM && (xyz[1] & (DIM-1u)) < DIM && (xyz[2] & (DIM-1u)) < DIM);
950  return ((xyz[0] & (DIM-1u)) << 2*Log2Dim)
951  + ((xyz[1] & (DIM-1u)) << Log2Dim)
952  + (xyz[2] & (DIM-1u));
953 }
954 
955 
956 template<Index Log2Dim>
957 inline Coord
959 {
960  assert(n < (1 << 3*Log2Dim));
961  Coord xyz;
962  xyz.setX(n >> 2*Log2Dim);
963  n &= ((1 << 2*Log2Dim) - 1);
964  xyz.setY(n >> Log2Dim);
965  xyz.setZ(n & ((1 << Log2Dim) - 1));
966  return xyz;
967 }
968 
969 
970 template<Index Log2Dim>
971 inline Coord
973 {
974  return (this->offsetToLocalCoord(n) + this->origin());
975 }
976 
977 
978 ////////////////////////////////////////
979 
980 
981 template<Index Log2Dim>
982 inline void
983 LeafNode<ValueMask, Log2Dim>::readTopology(std::istream& is, bool /*fromHalf*/)
984 {
985  mBuffer.mData.load(is);
986 }
987 
988 
989 template<Index Log2Dim>
990 inline void
991 LeafNode<ValueMask, Log2Dim>::writeTopology(std::ostream& os, bool /*toHalf*/) const
992 {
993  mBuffer.mData.save(os);
994 }
995 
996 
997 template<Index Log2Dim>
998 inline void
999 LeafNode<ValueMask, Log2Dim>::readBuffers(std::istream& is, const CoordBBox& clipBBox, bool fromHalf)
1000 {
1001  // Boolean LeafNodes don't currently implement lazy loading.
1002  // Instead, load the full buffer, then clip it.
1003 
1004  this->readBuffers(is, fromHalf);
1005 
1006  // Get this tree's background value.
1007  bool background = false;
1008  if (const void* bgPtr = io::getGridBackgroundValuePtr(is)) {
1009  background = *static_cast<const bool*>(bgPtr);
1010  }
1011  this->clip(clipBBox, background);
1012 }
1013 
1014 
1015 template<Index Log2Dim>
1016 inline void
1017 LeafNode<ValueMask, Log2Dim>::readBuffers(std::istream& is, bool /*fromHalf*/)
1018 {
1019  // Read in the value mask = buffer.
1020  mBuffer.mData.load(is);
1021  // Read in the origin.
1022  is.read(reinterpret_cast<char*>(&mOrigin), sizeof(Coord::ValueType) * 3);
1023 }
1024 
1025 
1026 template<Index Log2Dim>
1027 inline void
1028 LeafNode<ValueMask, Log2Dim>::writeBuffers(std::ostream& os, bool /*toHalf*/) const
1029 {
1030  // Write out the value mask = buffer.
1031  mBuffer.mData.save(os);
1032  // Write out the origin.
1033  os.write(reinterpret_cast<const char*>(&mOrigin), sizeof(Coord::ValueType) * 3);
1034 }
1035 
1036 
1037 ////////////////////////////////////////
1038 
1039 
1040 template<Index Log2Dim>
1041 inline bool
1043 {
1044  return mOrigin == other.mOrigin && mBuffer == other.mBuffer;
1045 }
1046 
1047 
1048 template<Index Log2Dim>
1049 inline bool
1051 {
1052  return !(this->operator==(other));
1053 }
1054 
1055 
1056 ////////////////////////////////////////
1057 
1058 
1059 template<Index Log2Dim>
1060 inline bool
1061 LeafNode<ValueMask, Log2Dim>::isConstant(bool& constValue, bool& state, bool) const
1062 {
1063  if (!mBuffer.mData.isConstant(state)) return false;
1064 
1065  constValue = state;
1066  return true;
1067 }
1068 
1069 
1070 ////////////////////////////////////////
1071 
1072 template<Index Log2Dim>
1073 inline bool
1075 {
1076  const Index countTrue = mBuffer.mData.countOn();
1077  return countTrue > (NUM_VALUES >> 1);
1078 }
1079 
1080 template<Index Log2Dim>
1081 inline Index
1083 {
1084  const Index countTrueOn = mBuffer.mData.countOn();
1085  state = true;//since value and state are the same for this specialization of the leaf node
1086  return countTrueOn;
1087 }
1088 
1089 template<Index Log2Dim>
1090 inline Index
1092 {
1093  const Index countFalseOff = mBuffer.mData.countOff();
1094  state = false;//since value and state are the same for this specialization of the leaf node
1095  return countFalseOff;
1096 }
1097 
1098 
1099 ////////////////////////////////////////
1100 
1101 
1102 template<Index Log2Dim>
1103 inline void
1104 LeafNode<ValueMask, Log2Dim>::addTile(Index /*level*/, const Coord& xyz, bool val, bool active)
1105 {
1106  this->addTile(this->coordToOffset(xyz), val, active);
1107 }
1108 
1109 template<Index Log2Dim>
1110 inline void
1111 LeafNode<ValueMask, Log2Dim>::addTile(Index offset, bool val, bool active)
1112 {
1113  assert(offset < SIZE);
1114  this->setValueOnly(offset, val);
1115  this->setActiveState(offset, active);
1116 }
1117 
1118 template<Index Log2Dim>
1119 template<typename AccessorT>
1120 inline void
1122  bool val, bool active, AccessorT&)
1123 {
1124  this->addTile(level, xyz, val, active);
1125 }
1126 
1127 
1128 ////////////////////////////////////////
1129 
1130 
1131 template<Index Log2Dim>
1132 inline const bool&
1134 {
1135  // This *CANNOT* use operator ? because Visual C++
1136  if (mBuffer.mData.isOn(this->coordToOffset(xyz))) return Buffer::sOn; else return Buffer::sOff;
1137 }
1138 
1139 
1140 template<Index Log2Dim>
1141 inline const bool&
1143 {
1144  assert(offset < SIZE);
1145  // This *CANNOT* use operator ? for Windows
1146  if (mBuffer.mData.isOn(offset)) return Buffer::sOn; else return Buffer::sOff;
1147 }
1148 
1149 
1150 template<Index Log2Dim>
1151 inline bool
1152 LeafNode<ValueMask, Log2Dim>::probeValue(const Coord& xyz, bool& val) const
1153 {
1154  const Index offset = this->coordToOffset(xyz);
1155  val = mBuffer.mData.isOn(offset);
1156  return val;
1157 }
1158 
1159 
1160 template<Index Log2Dim>
1161 inline void
1162 LeafNode<ValueMask, Log2Dim>::setValueOn(const Coord& xyz, bool val)
1163 {
1164  this->setValueOn(this->coordToOffset(xyz), val);
1165 }
1166 
1167 
1168 template<Index Log2Dim>
1169 inline void
1171 {
1172  assert(offset < SIZE);
1173  mBuffer.mData.set(offset, val);
1174 }
1175 
1176 
1177 template<Index Log2Dim>
1178 inline void
1180 {
1181  this->setValueOnly(this->coordToOffset(xyz), val);
1182 }
1183 
1184 
1185 template<Index Log2Dim>
1186 inline void
1188 {
1189  mBuffer.mData.set(this->coordToOffset(xyz), on);
1190 }
1191 
1192 
1193 template<Index Log2Dim>
1194 inline void
1196 {
1197  this->setValueOff(this->coordToOffset(xyz), val);
1198 }
1199 
1200 
1201 template<Index Log2Dim>
1202 inline void
1204 {
1205  assert(offset < SIZE);
1206  mBuffer.mData.set(offset, val);
1207 }
1208 
1209 
1210 template<Index Log2Dim>
1211 template<typename ModifyOp>
1212 inline void
1214 {
1215  bool val = mBuffer.mData.isOn(offset);
1216  op(val);
1217  mBuffer.mData.set(offset, val);
1218 }
1219 
1220 
1221 template<Index Log2Dim>
1222 template<typename ModifyOp>
1223 inline void
1224 LeafNode<ValueMask, Log2Dim>::modifyValue(const Coord& xyz, const ModifyOp& op)
1225 {
1226  this->modifyValue(this->coordToOffset(xyz), op);
1227 }
1228 
1229 
1230 template<Index Log2Dim>
1231 template<typename ModifyOp>
1232 inline void
1233 LeafNode<ValueMask, Log2Dim>::modifyValueAndActiveState(const Coord& xyz, const ModifyOp& op)
1234 {
1235  const Index offset = this->coordToOffset(xyz);
1236  bool val = mBuffer.mData.isOn(offset), state = val;
1237  op(val, state);
1238  mBuffer.mData.set(offset, val);
1239 }
1240 
1241 
1242 ////////////////////////////////////////
1243 
1244 
1245 template<Index Log2Dim>
1246 template<MergePolicy Policy>
1247 inline void
1248 LeafNode<ValueMask, Log2Dim>::merge(const LeafNode& other, bool /*bg*/, bool /*otherBG*/)
1249 {
1251  if (Policy == MERGE_NODES) return;
1252  mBuffer.mData |= other.mBuffer.mData;
1254 }
1255 
1256 template<Index Log2Dim>
1257 template<MergePolicy Policy>
1258 inline void
1260 {
1262  if (Policy != MERGE_ACTIVE_STATES_AND_NODES) return;
1263  if (tileValue) mBuffer.mData.setOn();
1265 }
1266 
1267 
1268 ////////////////////////////////////////
1269 
1270 
1271 template<Index Log2Dim>
1272 template<typename OtherType>
1273 inline void
1275 {
1276  mBuffer.mData |= other.valueMask();
1277 }
1278 
1279 
1280 template<Index Log2Dim>
1281 template<typename OtherType>
1282 inline void
1284  const bool&)
1285 {
1286  mBuffer.mData &= other.valueMask();
1287 }
1288 
1289 
1290 template<Index Log2Dim>
1291 template<typename OtherType>
1292 inline void
1294  const bool&)
1295 {
1296  mBuffer.mData &= !other.valueMask();
1297 }
1298 
1299 
1300 ////////////////////////////////////////
1301 
1302 
1303 template<Index Log2Dim>
1304 inline void
1305 LeafNode<ValueMask, Log2Dim>::clip(const CoordBBox& clipBBox, bool background)
1306 {
1307  CoordBBox nodeBBox = this->getNodeBoundingBox();
1308  if (!clipBBox.hasOverlap(nodeBBox)) {
1309  // This node lies completely outside the clipping region. Fill it with background tiles.
1310  this->fill(nodeBBox, background, /*active=*/false);
1311  } else if (clipBBox.isInside(nodeBBox)) {
1312  // This node lies completely inside the clipping region. Leave it intact.
1313  return;
1314  }
1315 
1316  // This node isn't completely contained inside the clipping region.
1317  // Set any voxels that lie outside the region to the background value.
1318 
1319  // Construct a boolean mask that is on inside the clipping region and off outside it.
1320  NodeMaskType mask;
1321  nodeBBox.intersect(clipBBox);
1322  Coord xyz;
1323  int &x = xyz.x(), &y = xyz.y(), &z = xyz.z();
1324  for (x = nodeBBox.min().x(); x <= nodeBBox.max().x(); ++x) {
1325  for (y = nodeBBox.min().y(); y <= nodeBBox.max().y(); ++y) {
1326  for (z = nodeBBox.min().z(); z <= nodeBBox.max().z(); ++z) {
1327  mask.setOn(static_cast<Index32>(this->coordToOffset(xyz)));
1328  }
1329  }
1330  }
1331 
1332  // Set voxels that lie in the inactive region of the mask (i.e., outside
1333  // the clipping region) to the background value.
1334  for (MaskOffIter maskIter = mask.beginOff(); maskIter; ++maskIter) {
1335  this->setValueOff(maskIter.pos(), background);
1336  }
1337 }
1338 
1339 
1340 ////////////////////////////////////////
1341 
1342 
1343 template<Index Log2Dim>
1344 inline void
1345 LeafNode<ValueMask, Log2Dim>::fill(const CoordBBox& bbox, bool value, bool)
1346 {
1347  auto clippedBBox = this->getNodeBoundingBox();
1348  clippedBBox.intersect(bbox);
1349  if (!clippedBBox) return;
1350 
1351  for (Int32 x = clippedBBox.min().x(); x <= clippedBBox.max().x(); ++x) {
1352  const Index offsetX = (x & (DIM-1u))<<2*Log2Dim;
1353  for (Int32 y = clippedBBox.min().y(); y <= clippedBBox.max().y(); ++y) {
1354  const Index offsetXY = offsetX + ((y & (DIM-1u))<< Log2Dim);
1355  for (Int32 z = clippedBBox.min().z(); z <= clippedBBox.max().z(); ++z) {
1356  const Index offset = offsetXY + (z & (DIM-1u));
1357  mBuffer.mData.set(offset, value);
1358  }
1359  }
1360  }
1361 }
1362 
1363 template<Index Log2Dim>
1364 inline void
1365 LeafNode<ValueMask, Log2Dim>::fill(const bool& value, bool)
1366 {
1367  mBuffer.fill(value);
1368 }
1369 
1370 
1371 ////////////////////////////////////////
1372 
1373 
1374 template<Index Log2Dim>
1375 template<typename DenseT>
1376 inline void
1377 LeafNode<ValueMask, Log2Dim>::copyToDense(const CoordBBox& bbox, DenseT& dense) const
1378 {
1379  using DenseValueType = typename DenseT::ValueType;
1380 
1381  const size_t xStride = dense.xStride(), yStride = dense.yStride(), zStride = dense.zStride();
1382  const Coord& min = dense.bbox().min();
1383  DenseValueType* t0 = dense.data() + zStride * (bbox.min()[2] - min[2]); // target array
1384  const Int32 n0 = bbox.min()[2] & (DIM-1u);
1385  for (Int32 x = bbox.min()[0], ex = bbox.max()[0] + 1; x < ex; ++x) {
1386  DenseValueType* t1 = t0 + xStride * (x - min[0]);
1387  const Int32 n1 = n0 + ((x & (DIM-1u)) << 2*LOG2DIM);
1388  for (Int32 y = bbox.min()[1], ey = bbox.max()[1] + 1; y < ey; ++y) {
1389  DenseValueType* t2 = t1 + yStride * (y - min[1]);
1390  Int32 n2 = n1 + ((y & (DIM-1u)) << LOG2DIM);
1391  for (Int32 z = bbox.min()[2], ez = bbox.max()[2] + 1; z < ez; ++z, t2 += zStride) {
1392  *t2 = DenseValueType(mBuffer.mData.isOn(n2++));
1393  }
1394  }
1395  }
1396 }
1397 
1398 
1399 template<Index Log2Dim>
1400 template<typename DenseT>
1401 inline void
1403  bool background, bool tolerance)
1404 {
1405  using DenseValueType = typename DenseT::ValueType;
1406  struct Local {
1407  inline static bool toBool(const DenseValueType& v) { return !math::isZero(v); }
1408  };
1409 
1410  const size_t xStride = dense.xStride(), yStride = dense.yStride(), zStride = dense.zStride();
1411  const Coord& min = dense.bbox().min();
1412  const DenseValueType* s0 = dense.data() + zStride * (bbox.min()[2] - min[2]); // source
1413  const Int32 n0 = bbox.min()[2] & (DIM-1u);
1414  for (Int32 x = bbox.min()[0], ex = bbox.max()[0] + 1; x < ex; ++x) {
1415  const DenseValueType* s1 = s0 + xStride * (x - min[0]);
1416  const Int32 n1 = n0 + ((x & (DIM-1u)) << 2*LOG2DIM);
1417  for (Int32 y = bbox.min()[1], ey = bbox.max()[1] + 1; y < ey; ++y) {
1418  const DenseValueType* s2 = s1 + yStride * (y - min[1]);
1419  Int32 n2 = n1 + ((y & (DIM-1u)) << LOG2DIM);
1420  for (Int32 z = bbox.min()[2], ez = bbox.max()[2]+1; z < ez; ++z, ++n2, s2 += zStride) {
1421  // Note: if tolerance is true (i.e., 1), then all boolean values compare equal.
1422  if (tolerance || (background == Local::toBool(*s2))) {
1423  mBuffer.mData.set(n2, background);
1424  } else {
1425  mBuffer.mData.set(n2, Local::toBool(*s2));
1426  }
1427  }
1428  }
1429  }
1430 }
1431 
1432 
1433 ////////////////////////////////////////
1434 
1435 
1436 template<Index Log2Dim>
1437 template<typename CombineOp>
1438 inline void
1440 {
1441  CombineArgs<bool> args;
1442  for (Index i = 0; i < SIZE; ++i) {
1443  bool result = false, aVal = mBuffer.mData.isOn(i), bVal = other.mBuffer.mData.isOn(i);
1444  op(args.setARef(aVal)
1445  .setAIsActive(aVal)
1446  .setBRef(bVal)
1447  .setBIsActive(bVal)
1448  .setResultRef(result));
1449  mBuffer.mData.set(i, result);
1450  }
1451 }
1452 
1453 
1454 template<Index Log2Dim>
1455 template<typename CombineOp>
1456 inline void
1457 LeafNode<ValueMask, Log2Dim>::combine(bool value, bool valueIsActive, CombineOp& op)
1458 {
1459  CombineArgs<bool> args;
1460  args.setBRef(value).setBIsActive(valueIsActive);
1461  for (Index i = 0; i < SIZE; ++i) {
1462  bool result = false, aVal = mBuffer.mData.isOn(i);
1463  op(args.setARef(aVal)
1464  .setAIsActive(aVal)
1465  .setResultRef(result));
1466  mBuffer.mData.set(i, result);
1467  }
1468 }
1469 
1470 
1471 ////////////////////////////////////////
1472 
1473 
1474 template<Index Log2Dim>
1475 template<typename CombineOp, typename OtherType>
1476 inline void
1477 LeafNode<ValueMask, Log2Dim>::combine2(const LeafNode& other, const OtherType& value,
1478  bool valueIsActive, CombineOp& op)
1479 {
1481  args.setBRef(value).setBIsActive(valueIsActive);
1482  for (Index i = 0; i < SIZE; ++i) {
1483  bool result = false, aVal = other.mBuffer.mData.isOn(i);
1484  op(args.setARef(aVal)
1485  .setAIsActive(aVal)
1486  .setResultRef(result));
1487  mBuffer.mData.set(i, result);
1488  }
1489 }
1490 
1491 
1492 template<Index Log2Dim>
1493 template<typename CombineOp, typename OtherNodeT>
1494 inline void
1495 LeafNode<ValueMask, Log2Dim>::combine2(bool value, const OtherNodeT& other,
1496  bool valueIsActive, CombineOp& op)
1497 {
1499  args.setARef(value).setAIsActive(valueIsActive);
1500  for (Index i = 0; i < SIZE; ++i) {
1501  bool result = false, bVal = other.mBuffer.mData.isOn(i);
1502  op(args.setBRef(bVal)
1503  .setBIsActive(bVal)
1504  .setResultRef(result));
1505  mBuffer.mData.set(i, result);
1506  }
1507 }
1508 
1509 
1510 template<Index Log2Dim>
1511 template<typename CombineOp, typename OtherNodeT>
1512 inline void
1513 LeafNode<ValueMask, Log2Dim>::combine2(const LeafNode& b0, const OtherNodeT& b1, CombineOp& op)
1514 {
1516  for (Index i = 0; i < SIZE; ++i) {
1517  bool result = false, b0Val = b0.mBuffer.mData.isOn(i), b1Val = b1.mBuffer.mData.isOn(i);
1518  op(args.setARef(b0Val)
1519  .setAIsActive(b0Val)
1520  .setBRef(b1Val)
1521  .setBIsActive(b1Val)
1522  .setResultRef(result));
1523  mBuffer.mData.set(i, result);
1524  }
1525 }
1526 
1527 
1528 ////////////////////////////////////////
1529 
1530 template<Index Log2Dim>
1531 template<typename BBoxOp>
1532 inline void
1534 {
1535  if (op.template descent<LEVEL>()) {
1536  for (ValueOnCIter i=this->cbeginValueOn(); i; ++i) {
1537  op.template operator()<LEVEL>(CoordBBox::createCube(i.getCoord(), 1));
1538  }
1539  } else {
1540  op.template operator()<LEVEL>(this->getNodeBoundingBox());
1541  }
1542 }
1543 
1544 
1545 template<Index Log2Dim>
1546 template<typename VisitorOp>
1547 inline void
1549 {
1550  doVisit<LeafNode, VisitorOp, ChildAllIter>(*this, op);
1551 }
1552 
1553 
1554 template<Index Log2Dim>
1555 template<typename VisitorOp>
1556 inline void
1558 {
1559  doVisit<const LeafNode, VisitorOp, ChildAllCIter>(*this, op);
1560 }
1561 
1562 
1563 template<Index Log2Dim>
1564 template<typename NodeT, typename VisitorOp, typename ChildAllIterT>
1565 inline void
1566 LeafNode<ValueMask, Log2Dim>::doVisit(NodeT& self, VisitorOp& op)
1567 {
1568  for (ChildAllIterT iter = self.beginChildAll(); iter; ++iter) {
1569  op(iter);
1570  }
1571 }
1572 
1573 
1574 ////////////////////////////////////////
1575 
1576 
1577 template<Index Log2Dim>
1578 template<typename OtherLeafNodeType, typename VisitorOp>
1579 inline void
1580 LeafNode<ValueMask, Log2Dim>::visit2Node(OtherLeafNodeType& other, VisitorOp& op)
1581 {
1582  doVisit2Node<LeafNode, OtherLeafNodeType, VisitorOp, ChildAllIter,
1583  typename OtherLeafNodeType::ChildAllIter>(*this, other, op);
1584 }
1585 
1586 
1587 template<Index Log2Dim>
1588 template<typename OtherLeafNodeType, typename VisitorOp>
1589 inline void
1590 LeafNode<ValueMask, Log2Dim>::visit2Node(OtherLeafNodeType& other, VisitorOp& op) const
1591 {
1592  doVisit2Node<const LeafNode, OtherLeafNodeType, VisitorOp, ChildAllCIter,
1593  typename OtherLeafNodeType::ChildAllCIter>(*this, other, op);
1594 }
1595 
1596 
1597 template<Index Log2Dim>
1598 template<
1599  typename NodeT,
1600  typename OtherNodeT,
1601  typename VisitorOp,
1602  typename ChildAllIterT,
1603  typename OtherChildAllIterT>
1604 inline void
1605 LeafNode<ValueMask, Log2Dim>::doVisit2Node(NodeT& self, OtherNodeT& other, VisitorOp& op)
1606 {
1607  // Allow the two nodes to have different ValueTypes, but not different dimensions.
1608  static_assert(OtherNodeT::SIZE == NodeT::SIZE,
1609  "can't visit nodes of different sizes simultaneously");
1610  static_assert(OtherNodeT::LEVEL == NodeT::LEVEL,
1611  "can't visit nodes at different tree levels simultaneously");
1612 
1613  ChildAllIterT iter = self.beginChildAll();
1614  OtherChildAllIterT otherIter = other.beginChildAll();
1615 
1616  for ( ; iter && otherIter; ++iter, ++otherIter) {
1617  op(iter, otherIter);
1618  }
1619 }
1620 
1621 
1622 ////////////////////////////////////////
1623 
1624 
1625 template<Index Log2Dim>
1626 template<typename IterT, typename VisitorOp>
1627 inline void
1628 LeafNode<ValueMask, Log2Dim>::visit2(IterT& otherIter, VisitorOp& op, bool otherIsLHS)
1629 {
1630  doVisit2<LeafNode, VisitorOp, ChildAllIter, IterT>(*this, otherIter, op, otherIsLHS);
1631 }
1632 
1633 
1634 template<Index Log2Dim>
1635 template<typename IterT, typename VisitorOp>
1636 inline void
1637 LeafNode<ValueMask, Log2Dim>::visit2(IterT& otherIter, VisitorOp& op, bool otherIsLHS) const
1638 {
1639  doVisit2<const LeafNode, VisitorOp, ChildAllCIter, IterT>(*this, otherIter, op, otherIsLHS);
1640 }
1641 
1642 
1643 template<Index Log2Dim>
1644 template<
1645  typename NodeT,
1646  typename VisitorOp,
1647  typename ChildAllIterT,
1648  typename OtherChildAllIterT>
1649 inline void
1650 LeafNode<ValueMask, Log2Dim>::doVisit2(NodeT& self, OtherChildAllIterT& otherIter,
1651  VisitorOp& op, bool otherIsLHS)
1652 {
1653  if (!otherIter) return;
1654 
1655  if (otherIsLHS) {
1656  for (ChildAllIterT iter = self.beginChildAll(); iter; ++iter) {
1657  op(otherIter, iter);
1658  }
1659  } else {
1660  for (ChildAllIterT iter = self.beginChildAll(); iter; ++iter) {
1661  op(iter, otherIter);
1662  }
1663  }
1664 }
1665 
1666 } // namespace tree
1667 } // namespace OPENVDB_VERSION_NAME
1668 } // namespace openvdb
1669 
1670 #endif // OPENVDB_TREE_LEAF_NODE_MASK_HAS_BEEN_INCLUDED
const Coord & origin() const
Return the grid index coordinates of this node&#39;s local origin.
Definition: LeafNodeMask.h:159
typename std::remove_const< UnsetItemT >::type NonConstValueType
Definition: Iterator.h:184
Buffer mBuffer
Bitmask representing the values AND state of voxels.
Definition: LeafNodeMask.h:741
const LeafNode * probeConstLeafAndCache(const Coord &, AccessorT &) const
Return a const pointer to this node.
Definition: LeafNodeMask.h:571
ValueIter< MaskOffIter, const LeafNode, const bool > ValueOffCIter
Definition: LeafNodeMask.h:655
ValueAllCIter cendValueAll() const
Definition: LeafNodeMask.h:681
static Index32 nonLeafCount()
Return the non-leaf count for this node, which is zero.
Definition: LeafNodeMask.h:118
const bool & getItem(Index pos) const
Definition: LeafNodeMask.h:601
void setValueOn(const Coord &xyz)
Mark the voxel at the given coordinates as active but don&#39;t change its value.
Definition: LeafNode.h:419
static Index getValueLevelAndCache(const Coord &, AccessorT &)
Return the LEVEL (=0) at which leaf node values reside.
Definition: LeafNodeMask.h:399
Buffer & buffer()
Definition: LeafNodeMask.h:198
ChildT * child
Definition: GridBuilder.h:1286
ChildOffCIter cendChildOff() const
Definition: LeafNodeMask.h:700
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:437
ValueOnCIter cendValueOn() const
Definition: LeafNodeMask.h:675
Index32 Index
Definition: Types.h:54
void setValue(bool value) const
Definition: LeafNodeMask.h:607
bool ValueType
Definition: LeafNodeMask.h:35
ValueIter< MaskDenseIter, const LeafNode, const bool > ValueAllCIter
Definition: LeafNodeMask.h:657
void setValueAndCache(const Coord &xyz, bool val, AccessorT &)
Change the value of the voxel at the given coordinates and mark it as active.
Definition: LeafNodeMask.h:345
void copyFromDense(const DenseT &dense, GridOrTreeT &sparse, const typename GridOrTreeT::ValueType &tolerance, bool serial=false)
Populate a sparse grid with the values of all of the voxels of a dense grid.
Definition: Dense.h:568
const Coord & origin() const
Return the grid index coordinates of this node&#39;s local origin.
Definition: LeafNode.h:173
void setValueMask(Index n, bool on)
Definition: LeafNodeMask.h:722
ValueOnIter beginValueOn()
Definition: LeafNodeMask.h:667
Index64 memUsage(const TreeT &tree, bool threaded=true)
Return the total amount of memory in bytes occupied by this tree.
Definition: Count.h:493
ValueIter< MaskOnIter, LeafNode, const bool > ValueOnIter
Definition: LeafNodeMask.h:652
static Index32 leafCount()
Return the leaf count for this node, which is one.
Definition: LeafNodeMask.h:114
bool isChildMaskOff(Index) const
Definition: LeafNodeMask.h:719
const LeafNode * probeConstLeaf(const Coord &) const
Return a const pointer to this node.
Definition: LeafNodeMask.h:569
ChildAllCIter cendChildAll() const
Definition: LeafNodeMask.h:703
bool isZero(const Type &x)
Return true if x is exactly equal to zero.
Definition: Math.h:337
void setValueOff(const Coord &xyz)
Mark the voxel at the given coordinates as inactive but don&#39;t change its value.
Definition: LeafNodeMask.h:241
General-purpose arithmetic and comparison routines, most of which accept arbitrary value types (or at...
void setValuesOn()
Mark all voxels as active but don&#39;t change their values.
Definition: LeafNodeMask.h:276
const LeafNode * probeLeafAndCache(const Coord &, AccessorT &) const
Return a const pointer to this node.
Definition: LeafNodeMask.h:568
void topologyIntersection(const LeafNode< OtherType, Log2Dim > &other, const ValueType &)
Intersect this node&#39;s set of active values with the active values of the other node, whose ValueType may be different. So a resulting voxel will be active only if both of the original voxels were active.
Definition: LeafNode.h:1723
void modifyItem(Index n, const ModifyOp &op) const
Definition: LeafNodeMask.h:611
const Buffer & buffer() const
Definition: LeafNodeMask.h:197
std::string str() const
Return a string representation of this node.
Definition: LeafNode.h:1039
Templated block class to hold specific data types and a fixed number of values determined by Log2Dim...
Definition: LeafNode.h:37
CoordBBox getNodeBoundingBox() const
Return the bounding box of this node, i.e., the full index space spanned by this leaf node...
Definition: LeafNode.h:167
NodeT * probeNode(const Coord &)
This function exists only to enable template instantiation.
Definition: LeafNodeMask.h:535
void negate()
Invert the bits of the voxels, i.e. states and values.
Definition: LeafNodeMask.h:449
void setValue(const Coord &xyz, bool val)
Set the value of the voxel at the given coordinates and mark the voxel as active. ...
Definition: LeafNodeMask.h:258
static Index getChildDim()
Return the dimension of child nodes of this LeafNode, which is one for voxels.
Definition: LeafNodeMask.h:112
CombineArgs & setBRef(const BValueType &b)
Redirect the B value to a new external source.
Definition: Types.h:584
ValueAllCIter endValueAll() const
Definition: LeafNodeMask.h:682
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...
DenseIter< LeafNode, bool > ChildAllIter
Definition: LeafNodeMask.h:662
void combine2(const LeafNode &other, const OtherType &, bool valueIsActive, CombineOp &)
Definition: LeafNode.h:1796
static Index numValues()
Return the total number of voxels represented by this LeafNode.
Definition: LeafNodeMask.h:106
SharedPtr< LeafNodeType > Ptr
Definition: LeafNodeMask.h:38
static Index getLevel()
Return the level of this node, which by definition is zero for LeafNodes.
Definition: LeafNodeMask.h:108
bool getItem(Index pos, void *&child, NonConstValueT &value) const
Definition: LeafNodeMask.h:637
void visit2Node(OtherLeafNodeType &other, VisitorOp &)
Definition: LeafNode.h:1904
ValueOffIter beginValueOff()
Definition: LeafNodeMask.h:670
ChildAllCIter cbeginChildAll() const
Definition: LeafNodeMask.h:693
static bool hasActiveTiles()
Return false since leaf nodes never contain tiles.
Definition: LeafNodeMask.h:286
ChildAllCIter endChildAll() const
Definition: LeafNodeMask.h:704
ValueIter< MaskOffIter, LeafNode, const bool > ValueOffIter
Definition: LeafNodeMask.h:654
void writeBuffers(std::ostream &os, bool toHalf=false) const
Write buffers to a stream.
Definition: LeafNode.h:1438
Definition: NodeMasks.h:239
const NodeMaskType & valueMask() const
Definition: LeafNodeMask.h:715
void addLeafAndCache(LeafNode *, AccessorT &)
This function exists only to enable template instantiation.
Definition: LeafNodeMask.h:531
void modifyValue(const ModifyOp &op) const
Definition: LeafNodeMask.h:614
void evalActiveBoundingBox(CoordBBox &bbox, bool visitVoxels=true) const
Definition: LeafNode.h:1485
bool isChildMaskOff() const
Definition: LeafNodeMask.h:720
Index64 memUsageIfLoaded() const
Definition: LeafNode.h:1475
void setTransientData(Index32 transientData)
Set the transient data value.
Definition: LeafNodeMask.h:176
ChildOnCIter cendChildOn() const
Definition: LeafNodeMask.h:697
void combine(const LeafNode &other, CombineOp &op)
Definition: LeafNode.h:1756
void setValueOnly(Index offset, bool val)
Set the value of the voxel at the given offset but don&#39;t change its active state. ...
Definition: LeafNodeMask.h:238
void modifyValueAndCache(const Coord &xyz, const ModifyOp &op, AccessorT &)
Apply a functor to the value of the voxel at the given coordinates and mark the voxel as active...
Definition: LeafNodeMask.h:365
ChildOnIter beginChildOn()
Definition: LeafNodeMask.h:689
void addTile(Index level, const Coord &, const ValueType &, bool)
Definition: LeafNode.h:1616
bool isValueOn(const Coord &xyz) const
Return true if the voxel at the given coordinates is active.
Definition: LeafNodeMask.h:281
void addLeaf(LeafNode *)
This function exists only to enable template instantiation.
Definition: LeafNodeMask.h:529
ChildIter< MaskOnIter, LeafNode > ChildOnIter
Definition: LeafNodeMask.h:658
void resetBackground(bool, bool)
no-op since for this template specialization voxel values and states are indistinguishable.
Definition: LeafNodeMask.h:446
ChildOnCIter cbeginChildOn() const
Definition: LeafNodeMask.h:687
bool operator==(const Vec3< T0 > &v0, const Vec3< T1 > &v1)
Equality operator, does exact floating point comparisons.
Definition: Vec3.h:477
NodeT * probeNodeAndCache(const Coord &, AccessorT &)
Return a pointer to this node.
Definition: LeafNodeMask.h:556
void setValueMask(const NodeMaskType &mask)
Definition: LeafNodeMask.h:717
void getOrigin(Int32 &x, Int32 &y, Int32 &z) const
Return the grid index coordinates of this node&#39;s local origin.
Definition: LeafNodeMask.h:161
bool isDense() const
Return true if this node only contains active voxels.
Definition: LeafNodeMask.h:132
typename NodeMaskType::OnIterator MaskOnIter
Definition: LeafNodeMask.h:586
const bool & getValue() const
Definition: LeafNodeMask.h:602
void visitActiveBBox(BBoxOp &) const
Calls the templated functor BBoxOp with bounding box information. An additional level argument is pro...
Definition: LeafNode.h:1857
~LeafNode()
Destructor.
Definition: LeafNode.h:1032
Index64 offVoxelCount() const
Return the number of inactive voxels.
Definition: LeafNodeMask.h:123
DenseIter< const LeafNode, const bool > ChildAllCIter
Definition: LeafNodeMask.h:663
ValueOnIter endValueOn()
Definition: LeafNodeMask.h:677
void setActiveStateAndCache(const Coord &xyz, bool on, AccessorT &)
Set the active state of the voxel at the given coordinates without changing its value.
Definition: LeafNodeMask.h:382
bool isConstant(ValueType &firstValue, bool &state, const ValueType &tolerance=zeroVal< ValueType >()) const
Definition: LeafNode.h:1511
void prune(const ValueType &=zeroVal< ValueType >())
This function exists only to enable template instantiation.
Definition: LeafNodeMask.h:528
int32_t Int32
Definition: Types.h:56
void fill(const ValueType &)
Populate this buffer with a constant value.
Definition: LeafBuffer.h:226
static Index64 offTileCount()
Definition: LeafNodeMask.h:127
void nodeCount(std::vector< Index32 > &) const
no-op
Definition: LeafNodeMask.h:116
bool probeValueAndCache(const Coord &xyz, bool &val, AccessorT &) const
Return true if the voxel at the given coordinates is active and return the voxel value in val...
Definition: LeafNodeMask.h:391
Tag dispatch class that distinguishes constructors during file input.
Definition: Types.h:650
static Index64 onTileCount()
Definition: LeafNodeMask.h:126
static Index getValueLevel(const Coord &)
Return the level (0) at which leaf node values reside.
Definition: LeafNodeMask.h:228
Definition: Types.h:469
LeafNode * touchLeafAndCache(const Coord &, AccessorT &)
Return a pointer to this node.
Definition: LeafNodeMask.h:551
const bool & getFirstValue() const
Return a const reference to the first entry in the buffer.
Definition: LeafNodeMask.h:404
const NodeT * probeConstNodeAndCache(const Coord &, AccessorT &) const
Return a const pointer to this node.
Definition: LeafNodeMask.h:573
BBox< Coord > CoordBBox
Definition: NanoVDB.h:1658
void addTileAndCache(Index, const Coord &, const ValueType &, bool, AccessorT &)
Definition: LeafNode.h:1633
void setOrigin(const Coord &origin)
Set the grid index coordinates of this node&#39;s local origin.
Definition: LeafNodeMask.h:156
void visit(VisitorOp &)
Definition: LeafNode.h:1872
void writeTopology(std::ostream &os, bool toHalf=false) const
Write out just the topology.
Definition: LeafNode.h:1318
ValueAllIter endValueAll()
Definition: LeafNodeMask.h:683
ValueAllIter beginValueAll()
Definition: LeafNodeMask.h:673
ChildIter< MaskOffIter, const LeafNode > ChildOffCIter
Definition: LeafNodeMask.h:661
uint32_t Index32
Definition: Types.h:52
void getOrigin(Coord &origin) const
Return the grid index coordinates of this node&#39;s local origin.
Definition: LeafNodeMask.h:160
LeafNode specialization for values of type ValueMask that encodes both the active states and the bool...
Definition: LeafNodeMask.h:30
void setValueOn(Index offset)
Mark the voxel at the given offset as active but don&#39;t change its value.
Definition: LeafNodeMask.h:253
void visit2(IterT &otherIter, VisitorOp &, bool otherIsLHS=false)
Definition: LeafNode.h:1952
This struct collects both input and output arguments to "grid combiner" functors used with the tree::...
Definition: Types.h:529
bool operator==(const LeafNode &other) const
Check for buffer, state and origin equivalence.
Definition: LeafNode.h:1455
DenseIter(const MaskDenseIter &iter, NodeT *parent)
Definition: LeafNodeMask.h:635
ValueOnCIter beginValueOn() const
Definition: LeafNodeMask.h:666
bool isValueMaskOff() const
Definition: LeafNodeMask.h:713
Coord offsetToGlobalCoord(Index n) const
Return the global coordinates for a linear table offset.
Definition: LeafNode.h:1076
const NodeT * probeConstNode(const Coord &) const
This function exists only to enable template instantiation.
Definition: LeafNodeMask.h:537
ValueOffCIter cendValueOff() const
Definition: LeafNodeMask.h:678
DenseIter< LeafNode, ValueType, ChildAll > ChildAllIter
Definition: LeafNode.h:296
LeafNode * touchLeaf(const Coord &)
Return a pointer to this node.
Definition: LeafNodeMask.h:549
ChildOffCIter cbeginChildOff() const
Definition: LeafNodeMask.h:690
ChildOnIter endChildOn()
Definition: LeafNodeMask.h:699
void unsetItem(Index pos, const ValueT &val) const
Definition: LeafNodeMask.h:648
bool isChildMaskOn(Index) const
Definition: LeafNodeMask.h:718
bool isInactive() const
Return true if all of this node&#39;s values are inactive.
Definition: LeafNodeMask.h:442
CoordBBox getNodeBoundingBox() const
Return the bounding box of this node, i.e., the full index space spanned by this leaf node...
Definition: LeafNodeMask.h:153
const NodeMaskType & valueMask() const
Definition: LeafNode.h:877
static const Index SIZE
Definition: LeafNode.h:53
ChildAllCIter beginChildAll() const
Definition: LeafNode.h:328
Definition: Exceptions.h:13
bool isValueOn(Index offset) const
Return true if the voxel at the given offset is active.
Definition: LeafNodeMask.h:283
void modifyValueAndActiveState(const Coord &xyz, const ModifyOp &op)
Apply a functor to the voxel at the given coordinates.
Definition: LeafNode.h:458
const std::enable_if<!VecTraits< T >::IsVec, T >::type & min(const T &a, const T &b)
Definition: Composite.h:103
ChildOffIter endChildOff()
Definition: LeafNodeMask.h:702
#define OPENVDB_NO_UNREACHABLE_CODE_WARNING_BEGIN
SIMD Intrinsic Headers.
Definition: Platform.h:115
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:1203
bool isValueMaskOn() const
Definition: LeafNodeMask.h:711
const NodeMaskType & getValueMask() const
Definition: LeafNodeMask.h:714
ChildOffCIter endChildOff() const
Definition: LeafNodeMask.h:701
ValueIter(const MaskIterT &iter, NodeT *parent)
Definition: LeafNodeMask.h:599
ValueT value
Definition: GridBuilder.h:1287
OffIterator beginOff() const
Definition: NodeMasks.h:354
void setValuesOff()
Mark all voxels as inactive but don&#39;t change their values.
Definition: LeafNodeMask.h:278
bool allocate()
Allocate memory for this node&#39;s buffer if it has not already been allocated.
Definition: LeafNodeMask.h:140
ValueOffCIter cbeginValueOff() const
Definition: LeafNodeMask.h:668
ValueType medianAll(ValueType *tmp=nullptr) const
Computes the median value of all the active AND inactive voxels in this node.
Definition: LeafNode.h:1547
NodeT * stealNode(const Coord &, const ValueType &, bool)
This function exists only to enable template instantiation.
Definition: LeafNodeMask.h:533
static Index dim()
Return the number of voxels in each dimension.
Definition: LeafNodeMask.h:102
ChildOnCIter beginChildOn() const
Definition: LeafNodeMask.h:688
void merge(const LeafNode &)
Definition: LeafNode.h:1666
bool isValueMaskOff(Index n) const
Definition: LeafNodeMask.h:712
Definition: NodeMasks.h:270
void readTopology(std::istream &is, bool fromHalf=false)
Read in just the topology.
Definition: LeafNode.h:1310
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:1273
bool isEmpty() const
Return true if this node has no active voxels.
Definition: LeafNodeMask.h:130
ValueIter< MaskDenseIter, LeafNode, const bool > ValueAllIter
Definition: LeafNodeMask.h:656
ChildAllCIter beginChildAll() const
Definition: LeafNodeMask.h:694
ChildIter< MaskOnIter, const LeafNode > ChildOnCIter
Definition: LeafNodeMask.h:659
NodeMaskType & getValueMask()
Definition: LeafNodeMask.h:716
typename BaseT::NonConstValueType NonConstValueT
Definition: LeafNodeMask.h:632
void topologyDifference(const LeafNode< OtherType, Log2Dim > &other, const ValueType &)
Difference this node&#39;s set of active values with the active values of the other node, whose ValueType may be different. So a resulting voxel will be active only if the original voxel is active in this LeafNode and inactive in the other LeafNode.
Definition: LeafNode.h:1732
static void doVisit(NodeT &, VisitorOp &)
Definition: LeafNode.h:1890
ChildOffCIter beginChildOff() const
Definition: LeafNodeMask.h:691
void setActiveState(const Coord &xyz, bool on)
Set the active state of the voxel at the given coordinates but don&#39;t change its value.
Definition: LeafNode.h:1137
void clip(const CoordBBox &, const ValueType &background)
Set all voxels that lie outside the given axis-aligned box to the background.
Definition: LeafNode.h:1163
ValueIter< MaskOnIter, const LeafNode, const bool > ValueOnCIter
Definition: LeafNodeMask.h:653
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:1503
Base class for dense iterators over internal and leaf nodes.
Definition: Iterator.h:178
CombineArgs & setARef(const AValueType &a)
Redirect the A value to a new external source.
Definition: Types.h:582
bool isValueOnAndCache(const Coord &xyz, AccessorT &) const
Return true if the voxel at the given coordinates is active.
Definition: LeafNodeMask.h:340
void voxelizeActiveTiles(bool=true)
No-op.
Definition: LeafNodeMask.h:457
ValueType * mData
Definition: LeafBuffer.h:126
bool probeValue(const Coord &xyz, ValueType &val) const
Return true if the voxel at the given coordinates is active.
Definition: LeafNode.h:1103
void setItem(Index pos, bool value) const
Definition: LeafNodeMask.h:605
void setValueOffAndCache(const Coord &xyz, bool value, AccessorT &)
Change the value of the voxel at the given coordinates and mark it as inactive.
Definition: LeafNodeMask.h:356
#define OPENVDB_NO_UNREACHABLE_CODE_WARNING_END
Definition: Platform.h:116
ChildIter< MaskOffIter, LeafNode > ChildOffIter
Definition: LeafNodeMask.h:660
void denseFill(const CoordBBox &bbox, bool value, bool=false)
Set all voxels within an axis-aligned box to the specified value.
Definition: LeafNodeMask.h:294
Index64 memUsage() const
Return the memory in bytes occupied by this node.
Definition: LeafNode.h:1465
void readBuffers(std::istream &is, bool fromHalf=false)
Read buffers from a stream.
Definition: LeafNode.h:1346
ValueOnCIter cbeginValueOn() const
Definition: LeafNodeMask.h:665
static Index size()
Return the total number of voxels represented by this LeafNode.
Definition: LeafNodeMask.h:104
ValueOffIter endValueOff()
Definition: LeafNodeMask.h:680
static void doVisit2Node(NodeT &self, OtherNodeT &other, VisitorOp &)
Definition: LeafNode.h:1929
void setOn(Index32 n)
Set the nth bit on.
Definition: NodeMasks.h:452
Base class for sparse iterators over internal and leaf nodes.
Definition: Iterator.h:114
LeafNode()
Default constructor.
Definition: LeafNode.h:936
void swap(Buffer &other)
Exchange this node&#39;s data buffer with the given data buffer without changing the active states of the...
Definition: LeafNodeMask.h:196
void setValueOff(Index offset)
Mark the voxel at the given offset as inactive but don&#39;t change its value.
Definition: LeafNodeMask.h:243
Index64 onVoxelCount() const
Return the number of active voxels.
Definition: LeafNodeMask.h:121
void topologyUnion(const LeafNode< OtherType, Log2Dim > &other, const bool preserveTiles=false)
Union this node&#39;s set of active values with the active values of the other node, whose ValueType may ...
Definition: LeafNode.h:1715
Index32 transientData() const
Return the transient data value.
Definition: LeafNodeMask.h:174
void setValueOnly(const Coord &xyz, const ValueType &val)
Set the value of the voxel at the given coordinates but don&#39;t change its active state.
Definition: LeafNode.h:1145
void setValueMaskOn(Index n)
Definition: LeafNodeMask.h:723
LeafNode * probeLeafAndCache(const Coord &, AccessorT &)
Return a pointer to this node.
Definition: LeafNodeMask.h:554
typename NodeMaskType::DenseIterator MaskDenseIter
Definition: LeafNodeMask.h:588
Index medianOn(ValueType &value, ValueType *tmp=nullptr) const
Computes the median value of all the active voxels in this node.
Definition: LeafNode.h:1565
void setValueOn(const Coord &xyz)
Mark the voxel at the given coordinates as active but don&#39;t change its value.
Definition: LeafNodeMask.h:251
ChildOffIter beginChildOff()
Definition: LeafNodeMask.h:692
Definition: Types.h:111
GridType::Ptr clip(const GridType &grid, const BBoxd &bbox, bool keepInterior=true)
Clip the given grid against a world-space bounding box and return a new grid containing the result...
Definition: Clip.h:352
void setValueOnlyAndCache(const Coord &xyz, bool val, AccessorT &)
Change the value of the voxel at the given coordinates but preserve its state.
Definition: LeafNodeMask.h:351
Coord mOrigin
Global grid index coordinates (x,y,z) of the local origin of this node.
Definition: LeafNodeMask.h:743
Bit mask for the internal and leaf nodes of VDB. This is a 64-bit implementation. ...
Definition: NodeMasks.h:307
ChildOnCIter endChildOn() const
Definition: LeafNodeMask.h:698
Index64 memUsageIfLoaded(const TreeT &tree, bool threaded=true)
Return the deserialized memory usage of this tree. This is not necessarily equal to the current memor...
Definition: Count.h:502
ValueAllCIter cbeginValueAll() const
Definition: LeafNodeMask.h:671
Index medianOff(ValueType &value, ValueType *tmp=nullptr) const
Computes the median value of all the inactive voxels in this node.
Definition: LeafNode.h:1589
LeafNode * probeLeaf(const Coord &)
Return a pointer to this node.
Definition: LeafNodeMask.h:552
static Index coordToOffset(const Coord &xyz)
Return the linear table offset of the given global or local coordinates.
Definition: LeafNode.h:1052
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:1246
void getNodes(ArrayT &) const
This function exists only to enable template instantiation.
Definition: LeafNodeMask.h:538
typename NodeMaskType::OffIterator MaskOffIter
Definition: LeafNodeMask.h:587
ChildAllIter beginChildAll()
Definition: LeafNodeMask.h:695
ChildIter(const MaskIterT &iter, NodeT *parent)
Definition: LeafNodeMask.h:623
const bool & getValueAndCache(const Coord &xyz, AccessorT &) const
Return the value of the voxel at the given coordinates.
Definition: LeafNodeMask.h:335
ValueAllCIter beginValueAll() const
Definition: LeafNodeMask.h:672
void setValueMaskOff(Index n)
Definition: LeafNodeMask.h:724
Definition: NodeMasks.h:208
Definition: PointDataGrid.h:170
ValueOffCIter endValueOff() const
Definition: LeafNodeMask.h:679
ValueOnCIter endValueOn() const
Definition: LeafNodeMask.h:676
void stealNodes(ArrayT &, const ValueType &, bool)
This function exists only to enable template instantiation.
Definition: LeafNodeMask.h:539
void setActiveState(Index offset, bool on)
Set the active state of the voxel at the given offset but don&#39;t change its value. ...
Definition: LeafNodeMask.h:233
DenseIter< const LeafNode, const ValueType, ChildAll > ChildAllCIter
Definition: LeafNode.h:297
void modifyValueAndActiveStateAndCache(const Coord &xyz, const ModifyOp &op, AccessorT &)
Definition: LeafNodeMask.h:373
std::shared_ptr< T > SharedPtr
Definition: Types.h:114
bool isAllocated() const
Return true if memory for this node&#39;s buffer has been allocated.
Definition: LeafNodeMask.h:136
static void evalNodeOrigin(Coord &xyz)
Compute the origin of the leaf node that contains the voxel with the given coordinates.
Definition: LeafNodeMask.h:727
const NodeMaskType & getValueMask() const
Definition: LeafNode.h:875
const bool & getLastValue() const
Return a const reference to the last entry in the buffer.
Definition: LeafNodeMask.h:408
static void doVisit2(NodeT &self, OtherChildAllIterT &, VisitorOp &, bool otherIsLHS)
Definition: LeafNode.h:1976
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h.in:116
bool operator!=(const LeafNode &other) const
Definition: LeafNode.h:203
Tag dispatch class that distinguishes topology copy constructors from deep copy constructors.
Definition: Types.h:644
static Index log2dim()
Return log2 of the size of the buffer storage.
Definition: LeafNodeMask.h:100
bool isValueMaskOn(Index n) const
Definition: LeafNodeMask.h:710
ValueType combine(const ValueType &v0, const ValueType &v1, const ValueType &v2, const openvdb::Vec3d &w)
Combine different value types.
Definition: AttributeTransferUtil.h:141
void copyToDense(const GridOrTreeT &sparse, DenseT &dense, bool serial=false)
Populate a dense grid with the values of voxels from a sparse grid, where the sparse grid intersects ...
Definition: Dense.h:421
void setValueOff(const Coord &xyz)
Mark the voxel at the given coordinates as inactive but don&#39;t change its value.
Definition: LeafNode.h:409
static void getNodeLog2Dims(std::vector< Index > &dims)
Append the Log2Dim of this LeafNode to the specified vector.
Definition: LeafNodeMask.h:110
Index64 offLeafVoxelCount() const
Definition: LeafNodeMask.h:125
uint64_t Index64
Definition: Types.h:53
Base class for iterators over internal and leaf nodes.
Definition: Iterator.h:29
bool operator!=(const Vec3< T0 > &v0, const Vec3< T1 > &v1)
Inequality operator, does exact floating point comparisons.
Definition: Vec3.h:485
ValueOffCIter beginValueOff() const
Definition: LeafNodeMask.h:669
static const Index LOG2DIM
Definition: LeafNode.h:48
ValueOnCIter cbeginValueOn() const
Definition: LeafNode.h:299
Index64 onLeafVoxelCount() const
Definition: LeafNodeMask.h:124
const LeafNode * probeLeaf(const Coord &) const
Return a const pointer to this node.
Definition: LeafNodeMask.h:566
const ValueType & getValue(const Coord &xyz) const
Return the value of the voxel at the given coordinates.
Definition: LeafNode.h:1087
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h.in:202
ChildAllIter endChildAll()
Definition: LeafNodeMask.h:705
static Coord offsetToLocalCoord(Index n)
Return the local coordinates for a linear table offset, where offset 0 has coordinates (0...
Definition: LeafNode.h:1062