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,
105  const NodeMaskType& mask,
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  /// Return the transient data value.
191  Index32 transientData() const { return mTransientData; }
192  /// Set the transient data value.
194 
195  /// Return a string representation of this node.
196  std::string str() const;
197 
198  /// @brief Return @c true if the given node (which may have a different @c ValueType
199  /// than this node) has the same active value topology as this node.
200  template<typename OtherType, Index OtherLog2Dim>
201  bool hasSameTopology(const LeafNode<OtherType, OtherLog2Dim>* other) const;
202 
203  /// Check for buffer equivalence by value.
204  bool operator==(const LeafNode&) const;
205  bool operator!=(const LeafNode&) const;
206 
207  //
208  // Buffer management
209  //
210  /// @brief Exchange this node's data buffer with the given data buffer
211  /// without changing the active states of the values.
212  void swap(Buffer& other) { mBuffer.swap(other); }
213  const Buffer& buffer() const { return mBuffer; }
214  Buffer& buffer() { return mBuffer; }
215 
216  //
217  // I/O methods
218  //
219  /// Read in just the topology.
220  void readTopology(std::istream&, bool fromHalf = false);
221  /// Write out just the topology.
222  void writeTopology(std::ostream&, bool toHalf = false) const;
223 
224  /// Read in the topology and the origin.
225  void readBuffers(std::istream&, bool fromHalf = false);
226  void readBuffers(std::istream& is, const CoordBBox&, bool fromHalf = false);
227  /// Write out the topology and the origin.
228  void writeBuffers(std::ostream&, bool toHalf = false) const;
229 
230  //
231  // Accessor methods
232  //
233  /// Return the value of the voxel at the given coordinates.
234  const bool& getValue(const Coord& xyz) const;
235  /// Return the value of the voxel at the given offset.
236  const bool& getValue(Index offset) const;
237 
238  /// @brief Return @c true if the voxel at the given coordinates is active.
239  /// @param xyz the coordinates of the voxel to be probed
240  /// @param[out] val the value of the voxel at the given coordinates
241  bool probeValue(const Coord& xyz, bool& val) const;
242 
243  /// Return the level (0) at which leaf node values reside.
244  static Index getValueLevel(const Coord&) { return LEVEL; }
245 
246  /// Set the active state of the voxel at the given coordinates but don't change its value.
247  void setActiveState(const Coord& xyz, bool on);
248  /// Set the active state of the voxel at the given offset but don't change its value.
249  void setActiveState(Index offset, bool on) { assert(offset<SIZE); mValueMask.set(offset, on); }
250 
251  /// Set the value of the voxel at the given coordinates but don't change its active state.
252  void setValueOnly(const Coord& xyz, bool val);
253  /// Set the value of the voxel at the given offset but don't change its active state.
254  void setValueOnly(Index offset, bool val) { assert(offset<SIZE); mBuffer.setValue(offset,val); }
255 
256  /// Mark the voxel at the given coordinates as inactive but don't change its value.
257  void setValueOff(const Coord& xyz) { mValueMask.setOff(this->coordToOffset(xyz)); }
258  /// Mark the voxel at the given offset as inactive but don't change its value.
259  void setValueOff(Index offset) { assert(offset < SIZE); mValueMask.setOff(offset); }
260 
261  /// Set the value of the voxel at the given coordinates and mark the voxel as inactive.
262  void setValueOff(const Coord& xyz, bool val);
263  /// Set the value of the voxel at the given offset and mark the voxel as inactive.
264  void setValueOff(Index offset, bool val);
265 
266  /// Mark the voxel at the given coordinates as active but don't change its value.
267  void setValueOn(const Coord& xyz) { mValueMask.setOn(this->coordToOffset(xyz)); }
268  /// Mark the voxel at the given offset as active but don't change its value.
269  void setValueOn(Index offset) { assert(offset < SIZE); mValueMask.setOn(offset); }
270 
271  /// Set the value of the voxel at the given coordinates and mark the voxel as active.
272  void setValueOn(const Coord& xyz, bool val);
273  /// Set the value of the voxel at the given coordinates and mark the voxel as active.
274  void setValue(const Coord& xyz, bool val) { this->setValueOn(xyz, val); }
275  /// Set the value of the voxel at the given offset and mark the voxel as active.
276  void setValueOn(Index offset, bool val);
277 
278  /// @brief Apply a functor to the value of the voxel at the given offset
279  /// and mark the voxel as active.
280  template<typename ModifyOp>
281  void modifyValue(Index offset, const ModifyOp& op);
282  /// @brief Apply a functor to the value of the voxel at the given coordinates
283  /// and mark the voxel as active.
284  template<typename ModifyOp>
285  void modifyValue(const Coord& xyz, const ModifyOp& op);
286 
287  /// Apply a functor to the voxel at the given coordinates.
288  template<typename ModifyOp>
289  void modifyValueAndActiveState(const Coord& xyz, const ModifyOp& op);
290 
291  /// Mark all voxels as active but don't change their values.
292  void setValuesOn() { mValueMask.setOn(); }
293  /// Mark all voxels as inactive but don't change their values.
294  void setValuesOff() { mValueMask.setOff(); }
295 
296  /// Return @c true if the voxel at the given coordinates is active.
297  bool isValueOn(const Coord& xyz) const { return mValueMask.isOn(this->coordToOffset(xyz)); }
298  /// Return @c true if the voxel at the given offset is active.
299  bool isValueOn(Index offset) const { assert(offset < SIZE); return mValueMask.isOn(offset); }
300 
301  /// Return @c false since leaf nodes never contain tiles.
302  static bool hasActiveTiles() { return false; }
303 
304  /// Set all voxels that lie outside the given axis-aligned box to the background.
305  void clip(const CoordBBox&, bool background);
306 
307  /// Set all voxels within an axis-aligned box to the specified value and active state.
308  void fill(const CoordBBox& bbox, bool value, bool active = true);
309  /// Set all voxels within an axis-aligned box to the specified value and active state.
310  void denseFill(const CoordBBox& bbox, bool val, bool on = true) { this->fill(bbox, val, on); }
311 
312  /// Set all voxels to the specified value but don't change their active states.
313  void fill(const bool& value);
314  /// Set all voxels to the specified value and active state.
315  void fill(const bool& value, bool active);
316 
317  /// @brief Copy into a dense grid the values of the voxels that lie within
318  /// a given bounding box.
319  ///
320  /// @param bbox inclusive bounding box of the voxels to be copied into the dense grid
321  /// @param dense dense grid with a stride in @e z of one (see tools::Dense
322  /// in tools/Dense.h for the required API)
323  ///
324  /// @note @a bbox is assumed to be identical to or contained in the coordinate domains
325  /// of both the dense grid and this node, i.e., no bounds checking is performed.
326  /// @note Consider using tools::CopyToDense in tools/Dense.h
327  /// instead of calling this method directly.
328  template<typename DenseT>
329  void copyToDense(const CoordBBox& bbox, DenseT& dense) const;
330 
331  /// @brief Copy from a dense grid into this node the values of the voxels
332  /// that lie within a given bounding box.
333  /// @details Only values that are different (by more than the given tolerance)
334  /// from the background value will be active. Other values are inactive
335  /// and truncated to the background value.
336  ///
337  /// @param bbox inclusive bounding box of the voxels to be copied into this node
338  /// @param dense dense grid with a stride in @e z of one (see tools::Dense
339  /// in tools/Dense.h for the required API)
340  /// @param background background value of the tree that this node belongs to
341  /// @param tolerance tolerance within which a value equals the background value
342  ///
343  /// @note @a bbox is assumed to be identical to or contained in the coordinate domains
344  /// of both the dense grid and this node, i.e., no bounds checking is performed.
345  /// @note Consider using tools::CopyFromDense in tools/Dense.h
346  /// instead of calling this method directly.
347  template<typename DenseT>
348  void copyFromDense(const CoordBBox& bbox, const DenseT& dense, bool background, bool tolerance);
349 
350  /// @brief Return the value of the voxel at the given coordinates.
351  /// @note Used internally by ValueAccessor.
352  template<typename AccessorT>
353  const bool& getValueAndCache(const Coord& xyz, AccessorT&) const {return this->getValue(xyz);}
354 
355  /// @brief Return @c true if the voxel at the given coordinates is active.
356  /// @note Used internally by ValueAccessor.
357  template<typename AccessorT>
358  bool isValueOnAndCache(const Coord& xyz, AccessorT&) const { return this->isValueOn(xyz); }
359 
360  /// @brief Change the value of the voxel at the given coordinates and mark it as active.
361  /// @note Used internally by ValueAccessor.
362  template<typename AccessorT>
363  void setValueAndCache(const Coord& xyz, bool val, AccessorT&) { this->setValueOn(xyz, val); }
364 
365  /// @brief Change the value of the voxel at the given coordinates
366  /// but preserve its state.
367  /// @note Used internally by ValueAccessor.
368  template<typename AccessorT>
369  void setValueOnlyAndCache(const Coord& xyz, bool val, AccessorT&) {this->setValueOnly(xyz,val);}
370 
371  /// @brief Change the value of the voxel at the given coordinates and mark it as inactive.
372  /// @note Used internally by ValueAccessor.
373  template<typename AccessorT>
374  void setValueOffAndCache(const Coord& xyz, bool value, AccessorT&)
375  {
376  this->setValueOff(xyz, value);
377  }
378 
379  /// @brief Apply a functor to the value of the voxel at the given coordinates
380  /// and mark the voxel as active.
381  /// @note Used internally by ValueAccessor.
382  template<typename ModifyOp, typename AccessorT>
383  void modifyValueAndCache(const Coord& xyz, const ModifyOp& op, AccessorT&)
384  {
385  this->modifyValue(xyz, op);
386  }
387 
388  /// Apply a functor to the voxel at the given coordinates.
389  /// @note Used internally by ValueAccessor.
390  template<typename ModifyOp, typename AccessorT>
391  void modifyValueAndActiveStateAndCache(const Coord& xyz, const ModifyOp& op, AccessorT&)
392  {
393  this->modifyValueAndActiveState(xyz, op);
394  }
395 
396  /// @brief Set the active state of the voxel at the given coordinates
397  /// without changing its value.
398  /// @note Used internally by ValueAccessor.
399  template<typename AccessorT>
400  void setActiveStateAndCache(const Coord& xyz, bool on, AccessorT&)
401  {
402  this->setActiveState(xyz, on);
403  }
404 
405  /// @brief Return @c true if the voxel at the given coordinates is active
406  /// and return the voxel value in @a val.
407  /// @note Used internally by ValueAccessor.
408  template<typename AccessorT>
409  bool probeValueAndCache(const Coord& xyz, bool& val, AccessorT&) const
410  {
411  return this->probeValue(xyz, val);
412  }
413 
414  /// @brief Return the LEVEL (=0) at which leaf node values reside.
415  /// @note Used internally by ValueAccessor.
416  template<typename AccessorT>
417  static Index getValueLevelAndCache(const Coord&, AccessorT&) { return LEVEL; }
418 
419  /// @brief Return a const reference to the first entry in the buffer.
420  /// @note Since it's actually a reference to a static data member
421  /// it should not be converted to a non-const pointer!
422  const bool& getFirstValue() const { if (mValueMask.isOn(0)) return Buffer::sOn; else return Buffer::sOff; }
423  /// @brief Return a const reference to the last entry in the buffer.
424  /// @note Since it's actually a reference to a static data member
425  /// it should not be converted to a non-const pointer!
426  const bool& getLastValue() const { if (mValueMask.isOn(SIZE-1)) return Buffer::sOn; else return Buffer::sOff; }
427 
428  /// Return @c true if all of this node's voxels have the same active state
429  /// and are equal to within the given tolerance, and return the value in
430  /// @a constValue and the active state in @a state.
431  bool isConstant(bool& constValue, bool& state, bool tolerance = 0) const;
432 
433  /// @brief Computes the median value of all the active and inactive voxels in this node.
434  /// @return The median value.
435  ///
436  /// @details The median for boolean values is defined as the mode
437  /// of the values, i.e. the value that occurs most often.
438  bool medianAll() const;
439 
440  /// @brief Computes the median value of all the active voxels in this node.
441  /// @return The number of active voxels.
442  /// @param value Updated with the median value of all the active voxels.
443  ///
444  /// @details The median for boolean values is defined as the mode
445  /// of the values, i.e. the value that occurs most often.
446  Index medianOn(ValueType &value) const;
447 
448  /// @brief Computes the median value of all the inactive voxels in this node.
449  /// @return The number of inactive voxels.
450  /// @param value Updated with the median value of all the inactive voxels.
451  ///
452  /// @details The median for boolean values is defined as the mode
453  /// of the values, i.e. the value that occurs most often.
454  Index medianOff(ValueType &value) const;
455 
456  /// Return @c true if all of this node's values are inactive.
457  bool isInactive() const { return mValueMask.isOff(); }
458 
459  void resetBackground(bool oldBackground, bool newBackground);
460 
461  void negate() { mBuffer.mData.toggle(); }
462 
463  template<MergePolicy Policy>
464  void merge(const LeafNode& other, bool bg = false, bool otherBG = false);
465  template<MergePolicy Policy> void merge(bool tileValue, bool tileActive);
466 
467  /// @brief No-op
468  /// @details This function exists only to enable template instantiation.
469  void voxelizeActiveTiles(bool = true) {}
470 
471  /// @brief Union this node's set of active values with the active values
472  /// of the other node, whose @c ValueType may be different. So a
473  /// resulting voxel will be active if either of the original voxels
474  /// were active.
475  ///
476  /// @note This operation modifies only active states, not values.
477  template<typename OtherType>
478  void topologyUnion(const LeafNode<OtherType, Log2Dim>& other, const bool preserveTiles = false);
479 
480  /// @brief Intersect this node's set of active values with the active values
481  /// of the other node, whose @c ValueType may be different. So a
482  /// resulting voxel will be active only if both of the original voxels
483  /// were active.
484  ///
485  /// @details The last dummy argument is required to match the signature
486  /// for InternalNode::topologyIntersection.
487  ///
488  /// @note This operation modifies only active states, not
489  /// values. Also note that this operation can result in all voxels
490  /// being inactive so consider subsequently calling prune.
491  template<typename OtherType>
492  void topologyIntersection(const LeafNode<OtherType, Log2Dim>& other, const bool&);
493 
494  /// @brief Difference this node's set of active values with the active values
495  /// of the other node, whose @c ValueType may be different. So a
496  /// resulting voxel will be active only if the original voxel is
497  /// active in this LeafNode and inactive in the other LeafNode.
498  ///
499  /// @details The last dummy argument is required to match the signature
500  /// for InternalNode::topologyDifference.
501  ///
502  /// @note This operation modifies only active states, not values.
503  /// Also, because it can deactivate all of this node's voxels,
504  /// consider subsequently calling prune.
505  template<typename OtherType>
506  void topologyDifference(const LeafNode<OtherType, Log2Dim>& other, const bool&);
507 
508  template<typename CombineOp>
509  void combine(const LeafNode& other, CombineOp& op);
510  template<typename CombineOp>
511  void combine(bool, bool valueIsActive, CombineOp& op);
512 
513  template<typename CombineOp, typename OtherType /*= bool*/>
514  void combine2(const LeafNode& other, const OtherType&, bool valueIsActive, CombineOp&);
515  template<typename CombineOp, typename OtherNodeT /*= LeafNode*/>
516  void combine2(bool, const OtherNodeT& other, bool valueIsActive, CombineOp&);
517  template<typename CombineOp, typename OtherNodeT /*= LeafNode*/>
518  void combine2(const LeafNode& b0, const OtherNodeT& b1, CombineOp&);
519 
520  //@{
521  /// This function exists only to enable template instantiation.
522  void prune(const ValueType& /*tolerance*/ = zeroVal<ValueType>()) {}
523  void addLeaf(LeafNode*) {}
524  template<typename AccessorT>
525  void addLeafAndCache(LeafNode*, AccessorT&) {}
526  template<typename NodeT>
527  NodeT* stealNode(const Coord&, const ValueType&, bool) { return nullptr; }
528  template<typename NodeT>
529  NodeT* probeNode(const Coord&) { return nullptr; }
530  template<typename NodeT>
531  const NodeT* probeConstNode(const Coord&) const { return nullptr; }
532  template<typename ArrayT> void getNodes(ArrayT&) const {}
533  template<typename ArrayT> void stealNodes(ArrayT&, const ValueType&, bool) {}
534  //@}
535 
536  void addTile(Index level, const Coord&, bool val, bool active);
537  void addTile(Index offset, bool val, bool active);
538  template<typename AccessorT>
539  void addTileAndCache(Index level, const Coord&, bool val, bool active, AccessorT&);
540 
541  //@{
542  /// @brief Return a pointer to this node.
543  LeafNode* touchLeaf(const Coord&) { return this; }
544  template<typename AccessorT>
545  LeafNode* touchLeafAndCache(const Coord&, AccessorT&) { return this; }
546  LeafNode* probeLeaf(const Coord&) { return this; }
547  template<typename AccessorT>
548  LeafNode* probeLeafAndCache(const Coord&, AccessorT&) { return this; }
549  template<typename NodeT, typename AccessorT>
550  NodeT* probeNodeAndCache(const Coord&, AccessorT&)
551  {
553  if (!(std::is_same<NodeT, LeafNode>::value)) return nullptr;
554  return reinterpret_cast<NodeT*>(this);
556  }
557  //@}
558  //@{
559  /// @brief Return a @const pointer to this node.
560  const LeafNode* probeLeaf(const Coord&) const { return this; }
561  template<typename AccessorT>
562  const LeafNode* probeLeafAndCache(const Coord&, AccessorT&) const { return this; }
563  const LeafNode* probeConstLeaf(const Coord&) const { return this; }
564  template<typename AccessorT>
565  const LeafNode* probeConstLeafAndCache(const Coord&, AccessorT&) const { return this; }
566  template<typename NodeT, typename AccessorT>
567  const NodeT* probeConstNodeAndCache(const Coord&, AccessorT&) const
568  {
570  if (!(std::is_same<NodeT, LeafNode>::value)) return nullptr;
571  return reinterpret_cast<const NodeT*>(this);
573  }
574  //@}
575 
576  //
577  // Iterators
578  //
579 protected:
583 
584  template<typename MaskIterT, typename NodeT, typename ValueT>
585  struct ValueIter:
586  // Derives from SparseIteratorBase, but can also be used as a dense iterator,
587  // if MaskIterT is a dense mask iterator type.
588  public SparseIteratorBase<MaskIterT, ValueIter<MaskIterT, NodeT, ValueT>, NodeT, ValueT>
589  {
591 
593  ValueIter(const MaskIterT& iter, NodeT* parent): BaseT(iter, parent) {}
594 
595  const bool& getItem(Index pos) const { return this->parent().getValue(pos); }
596  const bool& getValue() const { return this->getItem(this->pos()); }
597 
598  // Note: setItem() can't be called on const iterators.
599  void setItem(Index pos, bool value) const { this->parent().setValueOnly(pos, value); }
600  // Note: setValue() can't be called on const iterators.
601  void setValue(bool value) const { this->setItem(this->pos(), value); }
602 
603  // Note: modifyItem() can't be called on const iterators.
604  template<typename ModifyOp>
605  void modifyItem(Index n, const ModifyOp& op) const { this->parent().modifyValue(n, op); }
606  // Note: modifyValue() can't be called on const iterators.
607  template<typename ModifyOp>
608  void modifyValue(const ModifyOp& op) const { this->modifyItem(this->pos(), op); }
609  };
610 
611  /// Leaf nodes have no children, so their child iterators have no get/set accessors.
612  template<typename MaskIterT, typename NodeT>
613  struct ChildIter:
614  public SparseIteratorBase<MaskIterT, ChildIter<MaskIterT, NodeT>, NodeT, bool>
615  {
617  ChildIter(const MaskIterT& iter, NodeT* parent): SparseIteratorBase<
618  MaskIterT, ChildIter<MaskIterT, NodeT>, NodeT, bool>(iter, parent) {}
619  };
620 
621  template<typename NodeT, typename ValueT>
622  struct DenseIter: public DenseIteratorBase<
623  MaskDenseIter, DenseIter<NodeT, ValueT>, NodeT, /*ChildT=*/void, ValueT>
624  {
627 
629  DenseIter(const MaskDenseIter& iter, NodeT* parent): BaseT(iter, parent) {}
630 
631  bool getItem(Index pos, void*& child, NonConstValueT& value) const
632  {
633  value = this->parent().getValue(pos);
634  child = nullptr;
635  return false; // no child
636  }
637 
638  // Note: setItem() can't be called on const iterators.
639  //void setItem(Index pos, void* child) const {}
640 
641  // Note: unsetItem() can't be called on const iterators.
642  void unsetItem(Index pos, const ValueT& val) const {this->parent().setValueOnly(pos, val);}
643  };
644 
645 public:
646  using ValueOnIter = ValueIter<MaskOnIter, LeafNode, const bool>;
647  using ValueOnCIter = ValueIter<MaskOnIter, const LeafNode, const bool>;
648  using ValueOffIter = ValueIter<MaskOffIter, LeafNode, const bool>;
649  using ValueOffCIter = ValueIter<MaskOffIter, const LeafNode, const bool>;
650  using ValueAllIter = ValueIter<MaskDenseIter, LeafNode, const bool>;
651  using ValueAllCIter = ValueIter<MaskDenseIter, const LeafNode, const bool>;
652  using ChildOnIter = ChildIter<MaskOnIter, LeafNode>;
653  using ChildOnCIter = ChildIter<MaskOnIter, const LeafNode>;
654  using ChildOffIter = ChildIter<MaskOffIter, LeafNode>;
655  using ChildOffCIter = ChildIter<MaskOffIter, const LeafNode>;
656  using ChildAllIter = DenseIter<LeafNode, bool>;
657  using ChildAllCIter = DenseIter<const LeafNode, const bool>;
658 
659  ValueOnCIter cbeginValueOn() const { return ValueOnCIter(mValueMask.beginOn(), this); }
660  ValueOnCIter beginValueOn() const { return ValueOnCIter(mValueMask.beginOn(), this); }
661  ValueOnIter beginValueOn() { return ValueOnIter(mValueMask.beginOn(), this); }
662  ValueOffCIter cbeginValueOff() const { return ValueOffCIter(mValueMask.beginOff(), this); }
663  ValueOffCIter beginValueOff() const { return ValueOffCIter(mValueMask.beginOff(), this); }
664  ValueOffIter beginValueOff() { return ValueOffIter(mValueMask.beginOff(), this); }
665  ValueAllCIter cbeginValueAll() const { return ValueAllCIter(mValueMask.beginDense(), this); }
666  ValueAllCIter beginValueAll() const { return ValueAllCIter(mValueMask.beginDense(), this); }
667  ValueAllIter beginValueAll() { return ValueAllIter(mValueMask.beginDense(), this); }
668 
669  ValueOnCIter cendValueOn() const { return ValueOnCIter(mValueMask.endOn(), this); }
670  ValueOnCIter endValueOn() const { return ValueOnCIter(mValueMask.endOn(), this); }
671  ValueOnIter endValueOn() { return ValueOnIter(mValueMask.endOn(), this); }
672  ValueOffCIter cendValueOff() const { return ValueOffCIter(mValueMask.endOff(), this); }
673  ValueOffCIter endValueOff() const { return ValueOffCIter(mValueMask.endOff(), this); }
674  ValueOffIter endValueOff() { return ValueOffIter(mValueMask.endOff(), this); }
675  ValueAllCIter cendValueAll() const { return ValueAllCIter(mValueMask.endDense(), this); }
676  ValueAllCIter endValueAll() const { return ValueAllCIter(mValueMask.endDense(), this); }
677  ValueAllIter endValueAll() { return ValueAllIter(mValueMask.endDense(), this); }
678 
679  // Note that [c]beginChildOn() and [c]beginChildOff() actually return end iterators,
680  // because leaf nodes have no children.
681  ChildOnCIter cbeginChildOn() const { return ChildOnCIter(mValueMask.endOn(), this); }
682  ChildOnCIter beginChildOn() const { return ChildOnCIter(mValueMask.endOn(), this); }
683  ChildOnIter beginChildOn() { return ChildOnIter(mValueMask.endOn(), this); }
684  ChildOffCIter cbeginChildOff() const { return ChildOffCIter(mValueMask.endOff(), this); }
685  ChildOffCIter beginChildOff() const { return ChildOffCIter(mValueMask.endOff(), this); }
686  ChildOffIter beginChildOff() { return ChildOffIter(mValueMask.endOff(), this); }
687  ChildAllCIter cbeginChildAll() const { return ChildAllCIter(mValueMask.beginDense(), this); }
688  ChildAllCIter beginChildAll() const { return ChildAllCIter(mValueMask.beginDense(), this); }
689  ChildAllIter beginChildAll() { return ChildAllIter(mValueMask.beginDense(), this); }
690 
691  ChildOnCIter cendChildOn() const { return ChildOnCIter(mValueMask.endOn(), this); }
692  ChildOnCIter endChildOn() const { return ChildOnCIter(mValueMask.endOn(), this); }
693  ChildOnIter endChildOn() { return ChildOnIter(mValueMask.endOn(), this); }
694  ChildOffCIter cendChildOff() const { return ChildOffCIter(mValueMask.endOff(), this); }
695  ChildOffCIter endChildOff() const { return ChildOffCIter(mValueMask.endOff(), this); }
696  ChildOffIter endChildOff() { return ChildOffIter(mValueMask.endOff(), this); }
697  ChildAllCIter cendChildAll() const { return ChildAllCIter(mValueMask.endDense(), this); }
698  ChildAllCIter endChildAll() const { return ChildAllCIter(mValueMask.endDense(), this); }
699  ChildAllIter endChildAll() { return ChildAllIter(mValueMask.endDense(), this); }
700 
701  //
702  // Mask accessors
703  //
704  bool isValueMaskOn(Index n) const { return mValueMask.isOn(n); }
705  bool isValueMaskOn() const { return mValueMask.isOn(); }
706  bool isValueMaskOff(Index n) const { return mValueMask.isOff(n); }
707  bool isValueMaskOff() const { return mValueMask.isOff(); }
708  const NodeMaskType& getValueMask() const { return mValueMask; }
709  const NodeMaskType& valueMask() const { return mValueMask; }
710  NodeMaskType& getValueMask() { return mValueMask; }
711  void setValueMask(const NodeMaskType& mask) { mValueMask = mask; }
712  bool isChildMaskOn(Index) const { return false; } // leaf nodes have no children
713  bool isChildMaskOff(Index) const { return true; }
714  bool isChildMaskOff() const { return true; }
715 protected:
716  void setValueMask(Index n, bool on) { mValueMask.set(n, on); }
717  void setValueMaskOn(Index n) { mValueMask.setOn(n); }
718  void setValueMaskOff(Index n) { mValueMask.setOff(n); }
719 
720  /// Compute the origin of the leaf node that contains the voxel with the given coordinates.
721  static void evalNodeOrigin(Coord& xyz) { xyz &= ~(DIM - 1); }
722 
723  /// Bitmask that determines which voxels are active
725  /// Bitmask representing the values of voxels
727  /// Global grid index coordinates (x,y,z) of the local origin of this node
728  Coord mOrigin;
729  /// Transient data (not serialized)
730  Index32 mTransientData = 0;
731 
732 private:
733  /// @brief During topology-only construction, access is needed
734  /// to protected/private members of other template instances.
735  template<typename, Index> friend class LeafNode;
736 
737  friend struct ValueIter<MaskOnIter, LeafNode, bool>;
738  friend struct ValueIter<MaskOffIter, LeafNode, bool>;
739  friend struct ValueIter<MaskDenseIter, LeafNode, bool>;
740  friend struct ValueIter<MaskOnIter, const LeafNode, bool>;
741  friend struct ValueIter<MaskOffIter, const LeafNode, bool>;
742  friend struct ValueIter<MaskDenseIter, const LeafNode, bool>;
743 
744  //@{
745  /// Allow iterators to call mask accessor methods (see below).
746  /// @todo Make mask accessors public?
750  //@}
751 
752 }; // class LeafNode<bool>
753 
754 
755 ////////////////////////////////////////
756 
757 
758 template<Index Log2Dim>
759 inline
761  : mOrigin(0, 0, 0)
762 {
763 }
764 
765 
766 template<Index Log2Dim>
767 inline
768 LeafNode<bool, Log2Dim>::LeafNode(const Coord& xyz, bool value, bool active)
769  : mValueMask(active)
770  , mBuffer(value)
771  , mOrigin(xyz & (~(DIM - 1)))
772 {
773 }
774 
775 
776 template<Index Log2Dim>
777 inline
779  : mValueMask(active)
780  , mBuffer(value)
781  , mOrigin(xyz & (~(DIM - 1)))
782 {
783  /// @todo For now, this is identical to the non-PartialCreate constructor.
784  /// Consider modifying the Buffer class to allow it to be constructed
785  /// without allocating a bitmask.
786 }
787 
788 
789 template<Index Log2Dim>
790 inline
792  : mValueMask(other.valueMask())
793  , mBuffer(other.mBuffer)
794  , mOrigin(other.mOrigin)
795  , mTransientData(other.mTransientData)
796 {
797 }
798 
799 
800 // Copy-construct from a leaf node with the same configuration but a different ValueType.
801 template<Index Log2Dim>
802 template<typename ValueT>
803 inline
805  : mValueMask(other.valueMask())
806  , mOrigin(other.origin())
807  , mTransientData(other.mTransientData)
808 {
809  struct Local {
810  /// @todo Consider using a value conversion functor passed as an argument instead.
811  static inline bool convertValue(const ValueT& val) { return bool(val); }
812  };
813 
814  for (Index i = 0; i < SIZE; ++i) {
815  mBuffer.setValue(i, Local::convertValue(other.mBuffer[i]));
816  }
817 }
818 
819 
820 template<Index Log2Dim>
821 template<typename ValueT>
822 inline
824  bool background, TopologyCopy)
825  : mValueMask(other.valueMask())
826  , mBuffer(background)
827  , mOrigin(other.origin())
828  , mTransientData(other.mTransientData)
829 {
830 }
831 
832 
833 template<Index Log2Dim>
834 template<typename ValueT>
835 inline
837  : mValueMask(other.valueMask())
838  , mBuffer(other.valueMask())// value = active state
839  , mOrigin(other.origin())
840  , mTransientData(other.mTransientData)
841 {
842 }
843 
844 template<Index Log2Dim>
845 inline
847  const NodeMaskType& mask,
848  const Buffer& buff,
849  const Index32 trans)
850  : mValueMask(mask)
851  , mBuffer(buff)
852  , mOrigin(xyz & (~(DIM - 1)))
853  , mTransientData(trans)
854 {
855 }
856 
857 template<Index Log2Dim>
858 template<typename ValueT>
859 inline
861  bool offValue, bool onValue, TopologyCopy)
862  : mValueMask(other.valueMask())
863  , mBuffer(offValue)
864  , mOrigin(other.origin())
865  , mTransientData(other.mTransientData)
866 {
867  for (Index i = 0; i < SIZE; ++i) {
868  if (mValueMask.isOn(i)) {
869  mBuffer.setValue(i, onValue);
870  }
871  }
872 }
873 
874 
875 template<Index Log2Dim>
876 inline
878 {
879 }
880 
881 
882 ////////////////////////////////////////
883 
884 
885 template<Index Log2Dim>
886 inline Index64
888 {
889  // Use sizeof(*this) to capture alignment-related padding
890  return sizeof(*this);
891 }
892 
893 
894 template<Index Log2Dim>
895 inline Index64
897 {
898  // Use sizeof(*this) to capture alignment-related padding
899  return sizeof(*this);
900 }
901 
902 
903 template<Index Log2Dim>
904 inline void
906 {
907  CoordBBox this_bbox = this->getNodeBoundingBox();
908  if (bbox.isInside(this_bbox)) return;//this LeafNode is already enclosed in the bbox
909  if (ValueOnCIter iter = this->cbeginValueOn()) {//any active values?
910  if (visitVoxels) {//use voxel granularity?
911  this_bbox.reset();
912  for(; iter; ++iter) this_bbox.expand(this->offsetToLocalCoord(iter.pos()));
913  this_bbox.translate(this->origin());
914  }
915  bbox.expand(this_bbox);
916  }
917 }
918 
919 
920 template<Index Log2Dim>
921 template<typename OtherType, Index OtherLog2Dim>
922 inline bool
924 {
925  assert(other);
926  return (Log2Dim == OtherLog2Dim && mValueMask == other->getValueMask());
927 }
928 
929 
930 template<Index Log2Dim>
931 inline std::string
933 {
934  std::ostringstream ostr;
935  ostr << "LeafNode @" << mOrigin << ": ";
936  for (Index32 n = 0; n < SIZE; ++n) ostr << (mValueMask.isOn(n) ? '#' : '.');
937  return ostr.str();
938 }
939 
940 
941 ////////////////////////////////////////
942 
943 
944 template<Index Log2Dim>
945 inline Index
947 {
948  assert ((xyz[0] & (DIM-1u)) < DIM && (xyz[1] & (DIM-1u)) < DIM && (xyz[2] & (DIM-1u)) < DIM);
949  return ((xyz[0] & (DIM-1u)) << 2*Log2Dim)
950  + ((xyz[1] & (DIM-1u)) << Log2Dim)
951  + (xyz[2] & (DIM-1u));
952 }
953 
954 
955 template<Index Log2Dim>
956 inline Coord
958 {
959  assert(n < (1 << 3*Log2Dim));
960  Coord xyz;
961  xyz.setX(n >> 2*Log2Dim);
962  n &= ((1 << 2*Log2Dim) - 1);
963  xyz.setY(n >> Log2Dim);
964  xyz.setZ(n & ((1 << Log2Dim) - 1));
965  return xyz;
966 }
967 
968 
969 template<Index Log2Dim>
970 inline Coord
972 {
973  return (this->offsetToLocalCoord(n) + this->origin());
974 }
975 
976 
977 ////////////////////////////////////////
978 
979 
980 template<Index Log2Dim>
981 inline void
982 LeafNode<bool, Log2Dim>::readTopology(std::istream& is, bool /*fromHalf*/)
983 {
984  mValueMask.load(is);
985 }
986 
987 
988 template<Index Log2Dim>
989 inline void
990 LeafNode<bool, Log2Dim>::writeTopology(std::ostream& os, bool /*toHalf*/) const
991 {
992  mValueMask.save(os);
993 }
994 
995 
996 template<Index Log2Dim>
997 inline void
998 LeafNode<bool, Log2Dim>::readBuffers(std::istream& is, const CoordBBox& clipBBox, bool fromHalf)
999 {
1000  // Boolean LeafNodes don't currently implement lazy loading.
1001  // Instead, load the full buffer, then clip it.
1002 
1003  this->readBuffers(is, fromHalf);
1004 
1005  // Get this tree's background value.
1006  bool background = false;
1007  if (const void* bgPtr = io::getGridBackgroundValuePtr(is)) {
1008  background = *static_cast<const bool*>(bgPtr);
1009  }
1010  this->clip(clipBBox, background);
1011 }
1012 
1013 
1014 template<Index Log2Dim>
1015 inline void
1016 LeafNode<bool, Log2Dim>::readBuffers(std::istream& is, bool /*fromHalf*/)
1017 {
1018  // Read in the value mask.
1019  mValueMask.load(is);
1020  // Read in the origin.
1021  is.read(reinterpret_cast<char*>(&mOrigin), sizeof(Coord::ValueType) * 3);
1022 
1024  // Read in the mask for the voxel values.
1025  mBuffer.mData.load(is);
1026  } else {
1027  // Older files stored one or more bool arrays.
1028 
1029  // Read in the number of buffers, which should now always be one.
1030  int8_t numBuffers = 0;
1031  is.read(reinterpret_cast<char*>(&numBuffers), sizeof(int8_t));
1032 
1033  // Read in the buffer.
1034  // (Note: prior to the bool leaf optimization, buffers were always compressed.)
1035  std::unique_ptr<bool[]> buf{new bool[SIZE]};
1036  io::readData<bool>(is, buf.get(), SIZE, /*isCompressed=*/true);
1037 
1038  // Transfer values to mBuffer.
1039  mBuffer.mData.setOff();
1040  for (Index i = 0; i < SIZE; ++i) {
1041  if (buf[i]) mBuffer.mData.setOn(i);
1042  }
1043 
1044  if (numBuffers > 1) {
1045  // Read in and discard auxiliary buffers that were created with
1046  // earlier versions of the library.
1047  for (int i = 1; i < numBuffers; ++i) {
1048  io::readData<bool>(is, buf.get(), SIZE, /*isCompressed=*/true);
1049  }
1050  }
1051  }
1052 }
1053 
1054 
1055 template<Index Log2Dim>
1056 inline void
1057 LeafNode<bool, Log2Dim>::writeBuffers(std::ostream& os, bool /*toHalf*/) const
1058 {
1059  // Write out the value mask.
1060  mValueMask.save(os);
1061  // Write out the origin.
1062  os.write(reinterpret_cast<const char*>(&mOrigin), sizeof(Coord::ValueType) * 3);
1063  // Write out the voxel values.
1064  mBuffer.mData.save(os);
1065 }
1066 
1067 
1068 ////////////////////////////////////////
1069 
1070 
1071 template<Index Log2Dim>
1072 inline bool
1074 {
1075  return mOrigin == other.mOrigin &&
1076  mValueMask == other.valueMask() &&
1077  mBuffer == other.mBuffer;
1078 }
1079 
1080 
1081 template<Index Log2Dim>
1082 inline bool
1084 {
1085  return !(this->operator==(other));
1086 }
1087 
1088 
1089 ////////////////////////////////////////
1090 
1091 
1092 template<Index Log2Dim>
1093 inline bool
1094 LeafNode<bool, Log2Dim>::isConstant(bool& constValue, bool& state, bool tolerance) const
1095 {
1096  if (!mValueMask.isConstant(state)) return false;
1097 
1098  // Note: if tolerance is true (i.e., 1), then all boolean values compare equal.
1099  if (!tolerance && !(mBuffer.mData.isOn() || mBuffer.mData.isOff())) return false;
1100 
1101  constValue = mBuffer.mData.isOn();
1102  return true;
1103 }
1104 
1105 ////////////////////////////////////////
1106 
1107 template<Index Log2Dim>
1108 inline bool
1110 {
1111  const Index countTrue = mBuffer.mData.countOn();
1112  return countTrue > (NUM_VALUES >> 1);
1113 }
1114 
1115 template<Index Log2Dim>
1116 inline Index
1118 {
1119  const NodeMaskType tmp = mBuffer.mData & mValueMask;//both true and active
1120  const Index countTrueOn = tmp.countOn(), countOn = mValueMask.countOn();
1121  state = countTrueOn > (NUM_VALUES >> 1);
1122  return countOn;
1123 }
1124 
1125 template<Index Log2Dim>
1126 inline Index
1128 {
1129  const NodeMaskType tmp = mBuffer.mData & (!mValueMask);//both true and inactive
1130  const Index countTrueOff = tmp.countOn(), countOff = mValueMask.countOff();
1131  state = countTrueOff > (NUM_VALUES >> 1);
1132  return countOff;
1133 }
1134 
1135 ////////////////////////////////////////
1136 
1137 
1138 template<Index Log2Dim>
1139 inline void
1140 LeafNode<bool, Log2Dim>::addTile(Index /*level*/, const Coord& xyz, bool val, bool active)
1141 {
1142  this->addTile(this->coordToOffset(xyz), val, active);
1143 }
1144 
1145 template<Index Log2Dim>
1146 inline void
1148 {
1149  assert(offset < SIZE);
1150  this->setValueOnly(offset, val);
1151  this->setActiveState(offset, active);
1152 }
1153 
1154 template<Index Log2Dim>
1155 template<typename AccessorT>
1156 inline void
1158  bool val, bool active, AccessorT&)
1159 {
1160  this->addTile(level, xyz, val, active);
1161 }
1162 
1163 
1164 ////////////////////////////////////////
1165 
1166 
1167 template<Index Log2Dim>
1168 inline const bool&
1169 LeafNode<bool, Log2Dim>::getValue(const Coord& xyz) const
1170 {
1171  // This *CANNOT* use operator ? because Visual C++
1172  if (mBuffer.mData.isOn(this->coordToOffset(xyz))) return Buffer::sOn; else return Buffer::sOff;
1173 }
1174 
1175 
1176 template<Index Log2Dim>
1177 inline const bool&
1179 {
1180  assert(offset < SIZE);
1181  // This *CANNOT* use operator ? for Windows
1182  if (mBuffer.mData.isOn(offset)) return Buffer::sOn; else return Buffer::sOff;
1183 }
1184 
1185 
1186 template<Index Log2Dim>
1187 inline bool
1188 LeafNode<bool, Log2Dim>::probeValue(const Coord& xyz, bool& val) const
1189 {
1190  const Index offset = this->coordToOffset(xyz);
1191  val = mBuffer.mData.isOn(offset);
1192  return mValueMask.isOn(offset);
1193 }
1194 
1195 
1196 template<Index Log2Dim>
1197 inline void
1199 {
1200  this->setValueOn(this->coordToOffset(xyz), val);
1201 }
1202 
1203 
1204 template<Index Log2Dim>
1205 inline void
1207 {
1208  assert(offset < SIZE);
1209  mValueMask.setOn(offset);
1210  mBuffer.mData.set(offset, val);
1211 }
1212 
1213 
1214 template<Index Log2Dim>
1215 inline void
1217 {
1218  this->setValueOnly(this->coordToOffset(xyz), val);
1219 }
1220 
1221 
1222 template<Index Log2Dim>
1223 inline void
1225 {
1226  mValueMask.set(this->coordToOffset(xyz), on);
1227 }
1228 
1229 
1230 template<Index Log2Dim>
1231 inline void
1233 {
1234  this->setValueOff(this->coordToOffset(xyz), val);
1235 }
1236 
1237 
1238 template<Index Log2Dim>
1239 inline void
1241 {
1242  assert(offset < SIZE);
1243  mValueMask.setOff(offset);
1244  mBuffer.mData.set(offset, val);
1245 }
1246 
1247 
1248 template<Index Log2Dim>
1249 template<typename ModifyOp>
1250 inline void
1252 {
1253  bool val = mBuffer.mData.isOn(offset);
1254  op(val);
1255  mBuffer.mData.set(offset, val);
1256  mValueMask.setOn(offset);
1257 }
1258 
1259 
1260 template<Index Log2Dim>
1261 template<typename ModifyOp>
1262 inline void
1263 LeafNode<bool, Log2Dim>::modifyValue(const Coord& xyz, const ModifyOp& op)
1264 {
1265  this->modifyValue(this->coordToOffset(xyz), op);
1266 }
1267 
1268 
1269 template<Index Log2Dim>
1270 template<typename ModifyOp>
1271 inline void
1272 LeafNode<bool, Log2Dim>::modifyValueAndActiveState(const Coord& xyz, const ModifyOp& op)
1273 {
1274  const Index offset = this->coordToOffset(xyz);
1275  bool val = mBuffer.mData.isOn(offset), state = mValueMask.isOn(offset);
1276  op(val, state);
1277  mBuffer.mData.set(offset, val);
1278  mValueMask.set(offset, state);
1279 }
1280 
1281 
1282 ////////////////////////////////////////
1283 
1284 
1285 template<Index Log2Dim>
1286 inline void
1287 LeafNode<bool, Log2Dim>::resetBackground(bool oldBackground, bool newBackground)
1288 {
1289  if (newBackground != oldBackground) {
1290  // Flip mBuffer's background bits and zero its foreground bits.
1291  NodeMaskType bgMask = !(mBuffer.mData | mValueMask);
1292  // Overwrite mBuffer's background bits, leaving its foreground bits intact.
1293  mBuffer.mData = (mBuffer.mData & mValueMask) | bgMask;
1294  }
1295 }
1296 
1297 
1298 ////////////////////////////////////////
1299 
1300 
1301 template<Index Log2Dim>
1302 template<MergePolicy Policy>
1303 inline void
1304 LeafNode<bool, Log2Dim>::merge(const LeafNode& other, bool /*bg*/, bool /*otherBG*/)
1305 {
1307  if (Policy == MERGE_NODES) return;
1308  for (typename NodeMaskType::OnIterator iter = other.valueMask().beginOn(); iter; ++iter) {
1309  const Index n = iter.pos();
1310  if (mValueMask.isOff(n)) {
1311  mBuffer.mData.set(n, other.mBuffer.mData.isOn(n));
1312  mValueMask.setOn(n);
1313  }
1314  }
1316 }
1317 
1318 template<Index Log2Dim>
1319 template<MergePolicy Policy>
1320 inline void
1321 LeafNode<bool, Log2Dim>::merge(bool tileValue, bool tileActive)
1322 {
1324  if (Policy != MERGE_ACTIVE_STATES_AND_NODES) return;
1325  if (!tileActive) return;
1326  // Replace all inactive values with the active tile value.
1327  if (tileValue) mBuffer.mData |= !mValueMask; // -0=>1, +0=>0, -1=>1, +1=>1 (-,+ = off,on)
1328  else mBuffer.mData &= mValueMask; // -0=>0, +0=>0, -1=>0, +1=>1
1329  mValueMask.setOn();
1331 }
1332 
1333 
1334 ////////////////////////////////////////
1335 
1336 
1337 template<Index Log2Dim>
1338 template<typename OtherType>
1339 inline void
1341 {
1342  mValueMask |= other.valueMask();
1343 }
1344 
1345 
1346 template<Index Log2Dim>
1347 template<typename OtherType>
1348 inline void
1350  const bool&)
1351 {
1352  mValueMask &= other.valueMask();
1353 }
1354 
1355 
1356 template<Index Log2Dim>
1357 template<typename OtherType>
1358 inline void
1360  const bool&)
1361 {
1362  mValueMask &= !other.valueMask();
1363 }
1364 
1365 
1366 ////////////////////////////////////////
1367 
1368 
1369 template<Index Log2Dim>
1370 inline void
1371 LeafNode<bool, Log2Dim>::clip(const CoordBBox& clipBBox, bool background)
1372 {
1373  CoordBBox nodeBBox = this->getNodeBoundingBox();
1374  if (!clipBBox.hasOverlap(nodeBBox)) {
1375  // This node lies completely outside the clipping region. Fill it with background tiles.
1376  this->fill(nodeBBox, background, /*active=*/false);
1377  } else if (clipBBox.isInside(nodeBBox)) {
1378  // This node lies completely inside the clipping region. Leave it intact.
1379  return;
1380  }
1381 
1382  // This node isn't completely contained inside the clipping region.
1383  // Set any voxels that lie outside the region to the background value.
1384 
1385  // Construct a boolean mask that is on inside the clipping region and off outside it.
1387  nodeBBox.intersect(clipBBox);
1388  Coord xyz;
1389  int &x = xyz.x(), &y = xyz.y(), &z = xyz.z();
1390  for (x = nodeBBox.min().x(); x <= nodeBBox.max().x(); ++x) {
1391  for (y = nodeBBox.min().y(); y <= nodeBBox.max().y(); ++y) {
1392  for (z = nodeBBox.min().z(); z <= nodeBBox.max().z(); ++z) {
1393  mask.setOn(static_cast<Index32>(this->coordToOffset(xyz)));
1394  }
1395  }
1396  }
1397 
1398  // Set voxels that lie in the inactive region of the mask (i.e., outside
1399  // the clipping region) to the background value.
1400  for (MaskOffIter maskIter = mask.beginOff(); maskIter; ++maskIter) {
1401  this->setValueOff(maskIter.pos(), background);
1402  }
1403 }
1404 
1405 
1406 ////////////////////////////////////////
1407 
1408 
1409 template<Index Log2Dim>
1410 inline void
1412 {
1413  auto clippedBBox = this->getNodeBoundingBox();
1414  clippedBBox.intersect(bbox);
1415  if (!clippedBBox) return;
1416 
1417  for (Int32 x = clippedBBox.min().x(); x <= clippedBBox.max().x(); ++x) {
1418  const Index offsetX = (x & (DIM-1u))<<2*Log2Dim;
1419  for (Int32 y = clippedBBox.min().y(); y <= clippedBBox.max().y(); ++y) {
1420  const Index offsetXY = offsetX + ((y & (DIM-1u))<< Log2Dim);
1421  for (Int32 z = clippedBBox.min().z(); z <= clippedBBox.max().z(); ++z) {
1422  const Index offset = offsetXY + (z & (DIM-1u));
1423  mValueMask.set(offset, active);
1424  mBuffer.mData.set(offset, value);
1425  }
1426  }
1427  }
1428 }
1429 
1430 template<Index Log2Dim>
1431 inline void
1433 {
1434  mBuffer.fill(value);
1435 }
1436 
1437 template<Index Log2Dim>
1438 inline void
1440 {
1441  mBuffer.fill(value);
1442  mValueMask.set(active);
1443 }
1444 
1445 
1446 ////////////////////////////////////////
1447 
1448 
1449 template<Index Log2Dim>
1450 template<typename DenseT>
1451 inline void
1452 LeafNode<bool, Log2Dim>::copyToDense(const CoordBBox& bbox, DenseT& dense) const
1453 {
1454  using DenseValueType = typename DenseT::ValueType;
1455 
1456  const size_t xStride = dense.xStride(), yStride = dense.yStride(), zStride = dense.zStride();
1457  const Coord& min = dense.bbox().min();
1458  DenseValueType* t0 = dense.data() + zStride * (bbox.min()[2] - min[2]); // target array
1459  const Int32 n0 = bbox.min()[2] & (DIM-1u);
1460  for (Int32 x = bbox.min()[0], ex = bbox.max()[0] + 1; x < ex; ++x) {
1461  DenseValueType* t1 = t0 + xStride * (x - min[0]);
1462  const Int32 n1 = n0 + ((x & (DIM-1u)) << 2*LOG2DIM);
1463  for (Int32 y = bbox.min()[1], ey = bbox.max()[1] + 1; y < ey; ++y) {
1464  DenseValueType* t2 = t1 + yStride * (y - min[1]);
1465  Int32 n2 = n1 + ((y & (DIM-1u)) << LOG2DIM);
1466  for (Int32 z = bbox.min()[2], ez = bbox.max()[2] + 1; z < ez; ++z, t2 += zStride) {
1467  *t2 = DenseValueType(mBuffer.mData.isOn(n2++));
1468  }
1469  }
1470  }
1471 }
1472 
1473 
1474 template<Index Log2Dim>
1475 template<typename DenseT>
1476 inline void
1477 LeafNode<bool, Log2Dim>::copyFromDense(const CoordBBox& bbox, const DenseT& dense,
1478  bool background, bool tolerance)
1479 {
1480  using DenseValueType = typename DenseT::ValueType;
1481  struct Local {
1482  inline static bool toBool(const DenseValueType& v) { return !math::isZero(v); }
1483  };
1484 
1485  const size_t xStride = dense.xStride(), yStride = dense.yStride(), zStride = dense.zStride();
1486  const Coord& min = dense.bbox().min();
1487  const DenseValueType* s0 = dense.data() + zStride * (bbox.min()[2] - min[2]); // source
1488  const Int32 n0 = bbox.min()[2] & (DIM-1u);
1489  for (Int32 x = bbox.min()[0], ex = bbox.max()[0] + 1; x < ex; ++x) {
1490  const DenseValueType* s1 = s0 + xStride * (x - min[0]);
1491  const Int32 n1 = n0 + ((x & (DIM-1u)) << 2*LOG2DIM);
1492  for (Int32 y = bbox.min()[1], ey = bbox.max()[1] + 1; y < ey; ++y) {
1493  const DenseValueType* s2 = s1 + yStride * (y - min[1]);
1494  Int32 n2 = n1 + ((y & (DIM-1u)) << LOG2DIM);
1495  for (Int32 z = bbox.min()[2], ez = bbox.max()[2]+1; z < ez; ++z, ++n2, s2 += zStride) {
1496  // Note: if tolerance is true (i.e., 1), then all boolean values compare equal.
1497  if (tolerance || (background == Local::toBool(*s2))) {
1498  mValueMask.setOff(n2);
1499  mBuffer.mData.set(n2, background);
1500  } else {
1501  mValueMask.setOn(n2);
1502  mBuffer.mData.set(n2, Local::toBool(*s2));
1503  }
1504  }
1505  }
1506  }
1507 }
1508 
1509 
1510 ////////////////////////////////////////
1511 
1512 
1513 template<Index Log2Dim>
1514 template<typename CombineOp>
1515 inline void
1516 LeafNode<bool, Log2Dim>::combine(const LeafNode& other, CombineOp& op)
1517 {
1519  for (Index i = 0; i < SIZE; ++i) {
1520  bool result = false, aVal = mBuffer.mData.isOn(i), bVal = other.mBuffer.mData.isOn(i);
1521  op(args.setARef(aVal)
1522  .setAIsActive(mValueMask.isOn(i))
1523  .setBRef(bVal)
1524  .setBIsActive(other.valueMask().isOn(i))
1525  .setResultRef(result));
1526  mValueMask.set(i, args.resultIsActive());
1527  mBuffer.mData.set(i, result);
1528  }
1529 }
1530 
1531 
1532 template<Index Log2Dim>
1533 template<typename CombineOp>
1534 inline void
1535 LeafNode<bool, Log2Dim>::combine(bool value, bool valueIsActive, CombineOp& op)
1536 {
1538  args.setBRef(value).setBIsActive(valueIsActive);
1539  for (Index i = 0; i < SIZE; ++i) {
1540  bool result = false, aVal = mBuffer.mData.isOn(i);
1541  op(args.setARef(aVal)
1542  .setAIsActive(mValueMask.isOn(i))
1543  .setResultRef(result));
1544  mValueMask.set(i, args.resultIsActive());
1545  mBuffer.mData.set(i, result);
1546  }
1547 }
1548 
1549 
1550 ////////////////////////////////////////
1551 
1552 
1553 template<Index Log2Dim>
1554 template<typename CombineOp, typename OtherType>
1555 inline void
1556 LeafNode<bool, Log2Dim>::combine2(const LeafNode& other, const OtherType& value,
1557  bool valueIsActive, CombineOp& op)
1558 {
1560  args.setBRef(value).setBIsActive(valueIsActive);
1561  for (Index i = 0; i < SIZE; ++i) {
1562  bool result = false, aVal = other.mBuffer.mData.isOn(i);
1563  op(args.setARef(aVal)
1564  .setAIsActive(other.valueMask().isOn(i))
1565  .setResultRef(result));
1566  mValueMask.set(i, args.resultIsActive());
1567  mBuffer.mData.set(i, result);
1568  }
1569 }
1570 
1571 
1572 template<Index Log2Dim>
1573 template<typename CombineOp, typename OtherNodeT>
1574 inline void
1575 LeafNode<bool, Log2Dim>::combine2(bool value, const OtherNodeT& other,
1576  bool valueIsActive, CombineOp& op)
1577 {
1579  args.setARef(value).setAIsActive(valueIsActive);
1580  for (Index i = 0; i < SIZE; ++i) {
1581  bool result = false, bVal = other.mBuffer.mData.isOn(i);
1582  op(args.setBRef(bVal)
1583  .setBIsActive(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(const LeafNode& b0, const OtherNodeT& b1, CombineOp& op)
1595 {
1597  for (Index i = 0; i < SIZE; ++i) {
1598  // Default behavior: output voxel is active if either input voxel is active.
1599  mValueMask.set(i, b0.valueMask().isOn(i) || b1.valueMask().isOn(i));
1600 
1601  bool result = false, b0Val = b0.mBuffer.mData.isOn(i), b1Val = b1.mBuffer.mData.isOn(i);
1602  op(args.setARef(b0Val)
1603  .setAIsActive(b0.valueMask().isOn(i))
1604  .setBRef(b1Val)
1605  .setBIsActive(b1.valueMask().isOn(i))
1606  .setResultRef(result));
1607  mValueMask.set(i, args.resultIsActive());
1608  mBuffer.mData.set(i, result);
1609  }
1610 }
1611 
1612 
1613 } // namespace tree
1614 } // namespace OPENVDB_VERSION_NAME
1615 } // namespace openvdb
1616 
1617 #endif // OPENVDB_TREE_LEAF_NODE_BOOL_HAS_BEEN_INCLUDED
void getNodes(ArrayT &) const
This function exists only to enable template instantiation.
Definition: LeafNodeBool.h:532
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:374
void setValuesOff()
Mark all voxels as inactive but don't change their values.
Definition: LeafNodeBool.h:294
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:1679
void setValueOn(const Coord &xyz)
Mark the voxel at the given coordinates as active but don't change its value.
Definition: LeafNodeBool.h:267
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:292
bool isValueOn(const Coord &xyz) const
Return true if the voxel at the given coordinates is active.
Definition: LeafNodeBool.h:297
const NodeMaskType & valueMask() const
Definition: LeafNode.h:856
void setValueOff(Index offset)
Mark the voxel at the given offset as inactive but don't change its value.
Definition: LeafNodeBool.h:259
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:353
void readBuffers(std::istream &is, bool fromHalf=false)
Read buffers from a stream.
Definition: LeafNode.h:1304
ChildIter< MaskOffIterator, LeafNode, ChildOff > ChildOffIter
Definition: LeafNode.h:292
ValueIter< MaskOnIter, const LeafNode, const bool > ValueOnCIter
Definition: LeafNodeBool.h:647
void setValueOnly(const Coord &xyz, const ValueType &val)
Set the value of the voxel at the given coordinates but don't change its active state.
Definition: LeafNode.h:1103
void readTopology(std::istream &is, bool fromHalf=false)
Read in just the topology.
Definition: LeafNode.h:1268
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:363
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:1045
void setValueOn(const Coord &xyz)
Mark the voxel at the given coordinates as active but don't change its value.
Definition: LeafNode.h:417
void 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:1475
const LeafNode * probeConstLeaf(const Coord &) const
Return a pointer to this node.
Definition: LeafNodeBool.h:563
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:546
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:212
const LeafNode * probeConstLeafAndCache(const Coord &, AccessorT &) const
Return a pointer to this node.
Definition: LeafNodeBool.h:565
Index32 transientData() const
Return the transient data value.
Definition: LeafNodeBool.h:191
const bool & getLastValue() const
Return a const reference to the last entry in the buffer.
Definition: LeafNodeBool.h:426
LeafNode * touchLeaf(const Coord &)
Return a pointer to this node.
Definition: LeafNodeBool.h:543
GLint level
Definition: glcorearb.h:108
void addLeaf(LeafNode *)
This function exists only to enable template instantiation.
Definition: LeafNodeBool.h:523
Index64 memUsage() const
Return the memory in bytes occupied by this node.
Definition: LeafNode.h:1429
#define OPENVDB_DEPRECATED_MESSAGE(msg)
Definition: Platform.h:148
#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:728
NodeT * stealNode(const Coord &, const ValueType &, bool)
This function exists only to enable template instantiation.
Definition: LeafNodeBool.h:527
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:291
ChildIter< MaskOnIter, const LeafNode > ChildOnCIter
Definition: LeafNodeBool.h:653
**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:290
DenseIter< const LeafNode, const bool > ChildAllCIter
Definition: LeafNodeBool.h:657
void clip(const CoordBBox &, const ValueType &background)
Set all voxels that lie outside the given axis-aligned box to the background.
Definition: LeafNode.h:1121
**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:531
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:1687
const LeafNode * probeLeaf(const Coord &) const
Return a pointer to this node.
Definition: LeafNodeBool.h:560
Tag dispatch class that distinguishes constructors during file input.
Definition: Types.h:689
void setActiveState(const Coord &xyz, bool on)
Set the active state of the voxel at the given coordinates but don't change its value.
Definition: LeafNode.h:1095
ValueIter< MaskDenseIter, LeafNode, const bool > ValueAllIter
Definition: LeafNodeBool.h:650
ValueIter< MaskOffIterator, const LeafNode, const ValueType, ValueOff > ValueOffCIter
Definition: LeafNode.h:287
static Index getValueLevelAndCache(const Coord &, AccessorT &)
Return the LEVEL (=0) at which leaf node values reside.
Definition: LeafNodeBool.h:417
static Coord offsetToLocalCoord(Index n)
Return the local coordinates for a linear table offset, where offset 0 has coordinates (0...
Definition: LeafNode.h:1020
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:1402
ValueIter< MaskDenseIterator, LeafNode, const ValueType, ValueAll > ValueAllIter
Definition: LeafNode.h:288
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:299
bool isValueOn(const Coord &xyz) const
Return true if the voxel at the given coordinates is active.
Definition: LeafNode.h:475
void stealNodes(ArrayT &, const ValueType &, bool)
This function exists only to enable template instantiation.
Definition: LeafNodeBool.h:533
void modifyValue(Index offset, const ModifyOp &op)
Apply a functor to the value of the voxel at the given offset and mark the voxel as active...
Definition: LeafNode.h:435
const LeafNode * probeLeafAndCache(const Coord &, AccessorT &) const
Return a pointer to this node.
Definition: LeafNodeBool.h:562
GLdouble n
Definition: glcorearb.h:2008
OffMaskIterator< NodeMask > OffIterator
Definition: NodeMasks.h:349
Buffer mBuffer
Bitmask representing the values of voxels.
Definition: LeafNodeBool.h:726
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:646
void setValueOff(const Coord &xyz)
Mark the voxel at the given coordinates as inactive but don't change its value.
Definition: LeafNodeBool.h:257
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:1580
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:310
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:302
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:1696
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:284
void copyToDense(const CoordBBox &bbox, DenseT &dense) const
Copy into a dense grid the values of the voxels that lie within a given bounding box.
Definition: LeafNode.h:1204
void modifyItem(Index n, const ModifyOp &op) const
Definition: LeafNode.h:240
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:854
bool operator!=(const LeafNode &other) const
Definition: LeafNode.h:201
const bool & getFirstValue() const
Return a const reference to the first entry in the buffer.
Definition: LeafNodeBool.h:422
std::string str() const
Return a string representation of this node.
Definition: LeafNode.h:997
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:1609
ValueType medianAll(ValueType *tmp=nullptr) const
Computes the median value of all the active AND inactive voxels in this node.
Definition: LeafNode.h:1511
LeafNode * touchLeafAndCache(const Coord &, AccessorT &)
Return a pointer to this node.
Definition: LeafNodeBool.h:545
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:407
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:391
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:456
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:140
ValueIter< MaskOffIter, LeafNode, const bool > ValueOffIter
Definition: LeafNodeBool.h:648
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:383
Coord offsetToGlobalCoord(Index n) const
Return the global coordinates for a linear table offset.
Definition: LeafNode.h:1034
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:369
void combine2(const LeafNode &other, const OtherType &, bool valueIsActive, CombineOp &)
Definition: LeafNode.h:1760
Index medianOn(ValueType &value, ValueType *tmp=nullptr) const
Computes the median value of all the active voxels in this node.
Definition: LeafNode.h:1529
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:550
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:655
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:269
static void evalNodeOrigin(Coord &xyz)
Compute the origin of the leaf node that contains the voxel with the given coordinates.
Definition: LeafNodeBool.h:721
ValueIter< MaskOnIterator, const LeafNode, const ValueType, ValueOn > ValueOnCIter
Definition: LeafNode.h:285
void setItem(Index pos, const ValueT &value) const
Definition: LeafNode.h:228
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:249
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:1061
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:1720
void prune(const ValueType &=zeroVal< ValueType >())
This function exists only to enable template instantiation.
Definition: LeafNodeBool.h:522
const NodeT * probeConstNodeAndCache(const Coord &, AccessorT &) const
Return a pointer to this node.
Definition: LeafNodeBool.h:567
Index medianOff(ValueType &value, ValueType *tmp=nullptr) const
Computes the median value of all the inactive voxels in this node.
Definition: LeafNode.h:1553
void addTileAndCache(Index, const Coord &, const ValueType &, bool, AccessorT &)
Definition: LeafNode.h:1597
ValueIter< MaskOffIter, const LeafNode, const bool > ValueOffCIter
Definition: LeafNodeBool.h:649
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:651
NodeT * probeNode(const Coord &)
This function exists only to enable template instantiation.
Definition: LeafNodeBool.h:529
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:289
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:274
void setTransientData(Index32 transientData)
Set the transient data value.
Definition: LeafNodeBool.h:193
LeafNode * probeLeafAndCache(const Coord &, AccessorT &)
Return a pointer to this node.
Definition: LeafNodeBool.h:548
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:244
**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:457
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:1231
#define OPENVDB_NO_UNREACHABLE_CODE_WARNING_END
Definition: Platform.h:141
Definition: core.h:1131
bool getItem(Index pos, void *&child, NonConstValueT &value) const
Definition: LeafNodeBool.h:631
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:525
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:400
DenseIter< const LeafNode, const ValueType, ChildAll > ChildAllCIter
Definition: LeafNode.h:295
bool hasSameTopology(const LeafNode< OtherType, OtherLog2Dim > *other) const
Return true if the given node (which may have a different ValueType than this node) has the same acti...
Definition: LeafNode.h:1467
bool operator==(const LeafNode &other) const
Check for buffer, state and origin equivalence.
Definition: LeafNode.h:1419
#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:1449
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:254
static Index coordToOffset(const Coord &xyz)
Return the linear table offset of the given global or local coordinates.
Definition: LeafNode.h:1010
bool ValueType
Definition: NanoVDB.h:5729
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:409
NodeMaskType mValueMask
Bitmask that determines which voxels are active.
Definition: LeafNodeBool.h:724
ChildIter< MaskOffIterator, const LeafNode, ChildOff > ChildOffCIter
Definition: LeafNode.h:293
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:1276
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:358
DenseIter< LeafNode, ValueType, ChildAll > ChildAllIter
Definition: LeafNode.h:294
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:286
void fill(const CoordBBox &bbox, const ValueType &, bool active=true)
Set all voxels within an axis-aligned box to the specified value and active state.
Definition: LeafNode.h:1161
Index32 transientData() const
Return the transient data value.
Definition: LeafNode.h:187