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