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