HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
LeafNode.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_LEAFNODE_HAS_BEEN_INCLUDED
5 #define OPENVDB_TREE_LEAFNODE_HAS_BEEN_INCLUDED
6 
7 #include <openvdb/Types.h>
9 #include <openvdb/io/Compression.h> // for io::readData(), etc.
10 #include "Iterator.h"
11 #include "LeafBuffer.h"
12 #include <algorithm> // for std::nth_element()
13 #include <iostream>
14 #include <memory>
15 #include <sstream>
16 #include <string>
17 #include <type_traits>
18 #include <vector>
19 
20 
21 class TestLeaf;
22 template<typename> class TestLeafIO;
23 
24 namespace openvdb {
26 namespace OPENVDB_VERSION_NAME {
27 namespace tree {
28 
29 template<Index, typename> struct SameLeafConfig; // forward declaration
30 
31 
32 /// @brief Templated block class to hold specific data types and a fixed
33 /// number of values determined by Log2Dim. The actual coordinate
34 /// dimension of the block is 2^Log2Dim, i.e. Log2Dim=3 corresponds to
35 /// a LeafNode that spans a 8^3 block.
36 template<typename T, Index Log2Dim>
37 class LeafNode
38 {
39 public:
40  using BuildType = T;
41  using ValueType = T;
46 
47  static const Index
48  LOG2DIM = Log2Dim, // needed by parent nodes
49  TOTAL = Log2Dim, // needed by parent nodes
50  DIM = 1 << TOTAL, // dimension along one coordinate direction
51  NUM_VALUES = 1 << 3 * Log2Dim,
52  NUM_VOXELS = NUM_VALUES, // total number of voxels represented by this node
54  LEVEL = 0; // level 0 = leaf
55 
56  /// @brief ValueConverter<T>::Type is the type of a LeafNode having the same
57  /// dimensions as this node but a different value type, T.
58  template<typename OtherValueType>
60 
61  /// @brief SameConfiguration<OtherNodeType>::value is @c true if and only if
62  /// OtherNodeType is the type of a LeafNode with the same dimensions as this node.
63  template<typename OtherNodeType>
66  };
67 
68 
69  /// Default constructor
70  LeafNode();
71 
72  /// @brief Constructor
73  /// @param coords the grid index coordinates of a voxel
74  /// @param value a value with which to fill the buffer
75  /// @param active the active state to which to initialize all voxels
76  explicit LeafNode(const Coord& coords,
77  const ValueType& value = zeroVal<ValueType>(),
78  bool active = false);
79 
80  /// @brief "Partial creation" constructor used during file input
81  /// @param coords the grid index coordinates of a voxel
82  /// @param value a value with which to fill the buffer
83  /// @param active the active state to which to initialize all voxels
84  /// @details This constructor does not allocate memory for voxel values.
86  const Coord& coords,
87  const ValueType& value = zeroVal<ValueType>(),
88  bool active = false);
89 
90  /// Deep copy constructor
91  LeafNode(const LeafNode&);
92 
93  /// Deep assignment operator
94  LeafNode& operator=(const LeafNode&) = default;
95 
96  /// Value conversion copy constructor
97  template<typename OtherValueType>
98  explicit LeafNode(const LeafNode<OtherValueType, Log2Dim>& other);
99 
100  /// Topology copy constructor
101  template<typename OtherValueType>
103  const ValueType& offValue, const ValueType& onValue, TopologyCopy);
104 
105  /// Topology copy constructor
106  template<typename OtherValueType>
108  const ValueType& background, TopologyCopy);
109 
110  /// Destructor.
111  ~LeafNode();
112 
113  //
114  // Statistics
115  //
116  /// Return log2 of the dimension of this LeafNode, e.g. 3 if dimensions are 8^3
117  static Index log2dim() { return Log2Dim; }
118  /// Return the number of voxels in each coordinate dimension.
119  static Index dim() { return DIM; }
120  /// Return the total number of voxels represented by this LeafNode
121  static Index size() { return SIZE; }
122  /// Return the total number of voxels represented by this LeafNode
123  static Index numValues() { return SIZE; }
124  /// Return the level of this node, which by definition is zero for LeafNodes
125  static Index getLevel() { return LEVEL; }
126  /// Append the Log2Dim of this LeafNode to the specified vector
127  static void getNodeLog2Dims(std::vector<Index>& dims) { dims.push_back(Log2Dim); }
128  /// Return the dimension of child nodes of this LeafNode, which is one for voxels.
129  static Index getChildDim() { return 1; }
130  /// Return the leaf count for this node, which is one.
131  static Index32 leafCount() { return 1; }
132  /// no-op
133  void nodeCount(std::vector<Index32> &) const {}
134  /// Return the non-leaf count for this node, which is zero.
135  static Index32 nonLeafCount() { return 0; }
136  /// Return the child count for this node, which is zero.
137  static Index32 childCount() { return 0; }
138 
139  /// Return the number of voxels marked On.
140  Index64 onVoxelCount() const { return mValueMask.countOn(); }
141  /// Return the number of voxels marked Off.
142  Index64 offVoxelCount() const { return mValueMask.countOff(); }
143  Index64 onLeafVoxelCount() const { return onVoxelCount(); }
145  static Index64 onTileCount() { return 0; }
146  static Index64 offTileCount() { return 0; }
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 contains only active voxels.
150  bool isDense() const { return mValueMask.isOn(); }
151  /// Return @c true if memory for this node's buffer has been allocated.
152  bool isAllocated() const { return !mBuffer.isOutOfCore() && !mBuffer.empty(); }
153  /// Allocate memory for this node's buffer if it has not already been allocated.
154  bool allocate() { return mBuffer.allocate(); }
155 
156  /// Return the memory in bytes occupied by this node.
157  Index64 memUsage() const;
158 
159  /// Expand the given bounding box so that it includes this leaf node's active voxels.
160  /// If visitVoxels is false this LeafNode will be approximated as dense, i.e. with all
161  /// voxels active. Else the individual active voxels are visited to produce a tight bbox.
162  void evalActiveBoundingBox(CoordBBox& bbox, bool visitVoxels = true) const;
163 
164  /// @brief Return the bounding box of this node, i.e., the full index space
165  /// spanned by this leaf node.
166  CoordBBox getNodeBoundingBox() const { return CoordBBox::createCube(mOrigin, DIM); }
167 
168  /// Set the grid index coordinates of this node's local origin.
169  void setOrigin(const Coord& origin) { mOrigin = origin; }
170  //@{
171  /// Return the grid index coordinates of this node's local origin.
172  const Coord& origin() const { return mOrigin; }
173  void getOrigin(Coord& origin) const { origin = mOrigin; }
174  void getOrigin(Int32& x, Int32& y, Int32& z) const { mOrigin.asXYZ(x, y, z); }
175  //@}
176 
177  /// Return the linear table offset of the given global or local coordinates.
178  static Index coordToOffset(const Coord& xyz);
179  /// @brief Return the local coordinates for a linear table offset,
180  /// where offset 0 has coordinates (0, 0, 0).
181  static Coord offsetToLocalCoord(Index n);
182  /// Return the global coordinates for a linear table offset.
183  Coord offsetToGlobalCoord(Index n) const;
184 
185  /// Return a string representation of this node.
186  std::string str() const;
187 
188  /// @brief Return @c true if the given node (which may have a different @c ValueType
189  /// than this node) has the same active value topology as this node.
190  template<typename OtherType, Index OtherLog2Dim>
191  bool hasSameTopology(const LeafNode<OtherType, OtherLog2Dim>* other) const;
192 
193  /// Check for buffer, state and origin equivalence.
194  bool operator==(const LeafNode& other) const;
195  bool operator!=(const LeafNode& other) const { return !(other == *this); }
196 
197 protected:
201 
202  // Type tags to disambiguate template instantiations
203  struct ValueOn {}; struct ValueOff {}; struct ValueAll {};
204  struct ChildOn {}; struct ChildOff {}; struct ChildAll {};
205 
206  template<typename MaskIterT, typename NodeT, typename ValueT, typename TagT>
207  struct ValueIter:
208  // Derives from SparseIteratorBase, but can also be used as a dense iterator,
209  // if MaskIterT is a dense mask iterator type.
210  public SparseIteratorBase<
211  MaskIterT, ValueIter<MaskIterT, NodeT, ValueT, TagT>, NodeT, ValueT>
212  {
214 
216  ValueIter(const MaskIterT& iter, NodeT* parent): BaseT(iter, parent) {}
217 
218  ValueT& getItem(Index pos) const { return this->parent().getValue(pos); }
219  ValueT& getValue() const { return this->parent().getValue(this->pos()); }
220 
221  // Note: setItem() can't be called on const iterators.
222  void setItem(Index pos, const ValueT& value) const
223  {
224  this->parent().setValueOnly(pos, value);
225  }
226  // Note: setValue() can't be called on const iterators.
227  void setValue(const ValueT& value) const
228  {
229  this->parent().setValueOnly(this->pos(), value);
230  }
231 
232  // Note: modifyItem() can't be called on const iterators.
233  template<typename ModifyOp>
234  void modifyItem(Index n, const ModifyOp& op) const { this->parent().modifyValue(n, op); }
235  // Note: modifyValue() can't be called on const iterators.
236  template<typename ModifyOp>
237  void modifyValue(const ModifyOp& op) const { this->parent().modifyValue(this->pos(), op); }
238  };
239 
240  /// Leaf nodes have no children, so their child iterators have no get/set accessors.
241  template<typename MaskIterT, typename NodeT, typename TagT>
242  struct ChildIter:
243  public SparseIteratorBase<MaskIterT, ChildIter<MaskIterT, NodeT, TagT>, NodeT, ValueType>
244  {
246  ChildIter(const MaskIterT& iter, NodeT* parent): SparseIteratorBase<
247  MaskIterT, ChildIter<MaskIterT, NodeT, TagT>, NodeT, ValueType>(iter, parent) {}
248  };
249 
250  template<typename NodeT, typename ValueT, typename TagT>
251  struct DenseIter: public DenseIteratorBase<
252  MaskDenseIterator, DenseIter<NodeT, ValueT, TagT>, NodeT, /*ChildT=*/void, ValueT>
253  {
256 
258  DenseIter(const MaskDenseIterator& iter, NodeT* parent): BaseT(iter, parent) {}
259 
260  bool getItem(Index pos, void*& child, NonConstValueT& value) const
261  {
262  value = this->parent().getValue(pos);
263  child = nullptr;
264  return false; // no child
265  }
266 
267  // Note: setItem() can't be called on const iterators.
268  //void setItem(Index pos, void* child) const {}
269 
270  // Note: unsetItem() can't be called on const iterators.
271  void unsetItem(Index pos, const ValueT& value) const
272  {
273  this->parent().setValueOnly(pos, value);
274  }
275  };
276 
277 public:
290 
291  ValueOnCIter cbeginValueOn() const { return ValueOnCIter(mValueMask.beginOn(), this); }
292  ValueOnCIter beginValueOn() const { return ValueOnCIter(mValueMask.beginOn(), this); }
293  ValueOnIter beginValueOn() { return ValueOnIter(mValueMask.beginOn(), this); }
294  ValueOffCIter cbeginValueOff() const { return ValueOffCIter(mValueMask.beginOff(), this); }
295  ValueOffCIter beginValueOff() const { return ValueOffCIter(mValueMask.beginOff(), this); }
296  ValueOffIter beginValueOff() { return ValueOffIter(mValueMask.beginOff(), this); }
297  ValueAllCIter cbeginValueAll() const { return ValueAllCIter(mValueMask.beginDense(), this); }
298  ValueAllCIter beginValueAll() const { return ValueAllCIter(mValueMask.beginDense(), this); }
299  ValueAllIter beginValueAll() { return ValueAllIter(mValueMask.beginDense(), this); }
300 
301  ValueOnCIter cendValueOn() const { return ValueOnCIter(mValueMask.endOn(), this); }
302  ValueOnCIter endValueOn() const { return ValueOnCIter(mValueMask.endOn(), this); }
303  ValueOnIter endValueOn() { return ValueOnIter(mValueMask.endOn(), this); }
304  ValueOffCIter cendValueOff() const { return ValueOffCIter(mValueMask.endOff(), this); }
305  ValueOffCIter endValueOff() const { return ValueOffCIter(mValueMask.endOff(), this); }
306  ValueOffIter endValueOff() { return ValueOffIter(mValueMask.endOff(), this); }
307  ValueAllCIter cendValueAll() const { return ValueAllCIter(mValueMask.endDense(), this); }
308  ValueAllCIter endValueAll() const { return ValueAllCIter(mValueMask.endDense(), this); }
309  ValueAllIter endValueAll() { return ValueAllIter(mValueMask.endDense(), this); }
310 
311  // Note that [c]beginChildOn() and [c]beginChildOff() actually return end iterators,
312  // because leaf nodes have no children.
313  ChildOnCIter cbeginChildOn() const { return ChildOnCIter(mValueMask.endOn(), this); }
314  ChildOnCIter beginChildOn() const { return ChildOnCIter(mValueMask.endOn(), this); }
315  ChildOnIter beginChildOn() { return ChildOnIter(mValueMask.endOn(), this); }
316  ChildOffCIter cbeginChildOff() const { return ChildOffCIter(mValueMask.endOff(), this); }
317  ChildOffCIter beginChildOff() const { return ChildOffCIter(mValueMask.endOff(), this); }
318  ChildOffIter beginChildOff() { return ChildOffIter(mValueMask.endOff(), this); }
319  ChildAllCIter cbeginChildAll() const { return ChildAllCIter(mValueMask.beginDense(), this); }
320  ChildAllCIter beginChildAll() const { return ChildAllCIter(mValueMask.beginDense(), this); }
321  ChildAllIter beginChildAll() { return ChildAllIter(mValueMask.beginDense(), this); }
322 
323  ChildOnCIter cendChildOn() const { return ChildOnCIter(mValueMask.endOn(), this); }
324  ChildOnCIter endChildOn() const { return ChildOnCIter(mValueMask.endOn(), this); }
325  ChildOnIter endChildOn() { return ChildOnIter(mValueMask.endOn(), this); }
326  ChildOffCIter cendChildOff() const { return ChildOffCIter(mValueMask.endOff(), this); }
327  ChildOffCIter endChildOff() const { return ChildOffCIter(mValueMask.endOff(), this); }
328  ChildOffIter endChildOff() { return ChildOffIter(mValueMask.endOff(), this); }
329  ChildAllCIter cendChildAll() const { return ChildAllCIter(mValueMask.endDense(), this); }
330  ChildAllCIter endChildAll() const { return ChildAllCIter(mValueMask.endDense(), this); }
331  ChildAllIter endChildAll() { return ChildAllIter(mValueMask.endDense(), this); }
332 
333  //
334  // Buffer management
335  //
336  /// @brief Exchange this node's data buffer with the given data buffer
337  /// without changing the active states of the values.
338  void swap(Buffer& other) { mBuffer.swap(other); }
339  const Buffer& buffer() const { return mBuffer; }
340  Buffer& buffer() { return mBuffer; }
341 
342  //
343  // I/O methods
344  //
345  /// @brief Read in just the topology.
346  /// @param is the stream from which to read
347  /// @param fromHalf if true, floating-point input values are assumed to be 16-bit
348  void readTopology(std::istream& is, bool fromHalf = false);
349  /// @brief Write out just the topology.
350  /// @param os the stream to which to write
351  /// @param toHalf if true, output floating-point values as 16-bit half floats
352  void writeTopology(std::ostream& os, bool toHalf = false) const;
353 
354  /// @brief Read buffers from a stream.
355  /// @param is the stream from which to read
356  /// @param fromHalf if true, floating-point input values are assumed to be 16-bit
357  void readBuffers(std::istream& is, bool fromHalf = false);
358  /// @brief Read buffers that intersect the given bounding box.
359  /// @param is the stream from which to read
360  /// @param bbox an index-space bounding box
361  /// @param fromHalf if true, floating-point input values are assumed to be 16-bit
362  void readBuffers(std::istream& is, const CoordBBox& bbox, bool fromHalf = false);
363  /// @brief Write buffers to a stream.
364  /// @param os the stream to which to write
365  /// @param toHalf if true, output floating-point values as 16-bit half floats
366  void writeBuffers(std::ostream& os, bool toHalf = false) const;
367 
368  size_t streamingSize(bool toHalf = false) const;
369 
370  //
371  // Accessor methods
372  //
373  /// Return the value of the voxel at the given coordinates.
374  const ValueType& getValue(const Coord& xyz) const;
375  /// Return the value of the voxel at the given linear offset.
376  const ValueType& getValue(Index offset) const;
377 
378  /// @brief Return @c true if the voxel at the given coordinates is active.
379  /// @param xyz the coordinates of the voxel to be probed
380  /// @param[out] val the value of the voxel at the given coordinates
381  bool probeValue(const Coord& xyz, ValueType& val) const;
382  /// @brief Return @c true if the voxel at the given offset is active.
383  /// @param offset the linear offset of the voxel to be probed
384  /// @param[out] val the value of the voxel at the given coordinates
385  bool probeValue(Index offset, ValueType& val) const;
386 
387  /// Return the level (i.e., 0) at which leaf node values reside.
388  static Index getValueLevel(const Coord&) { return LEVEL; }
389 
390  /// Set the active state of the voxel at the given coordinates but don't change its value.
391  void setActiveState(const Coord& xyz, bool on);
392  /// Set the active state of the voxel at the given offset but don't change its value.
393  void setActiveState(Index offset, bool on) { assert(offset<SIZE); mValueMask.set(offset, on); }
394 
395  /// Set the value of the voxel at the given coordinates but don't change its active state.
396  void setValueOnly(const Coord& xyz, const ValueType& val);
397  /// Set the value of the voxel at the given offset but don't change its active state.
398  void setValueOnly(Index offset, const ValueType& val);
399 
400  /// Mark the voxel at the given coordinates as inactive but don't change its value.
401  void setValueOff(const Coord& xyz) { mValueMask.setOff(LeafNode::coordToOffset(xyz)); }
402  /// Mark the voxel at the given offset as inactive but don't change its value.
403  void setValueOff(Index offset) { assert(offset < SIZE); mValueMask.setOff(offset); }
404 
405  /// Set the value of the voxel at the given coordinates and mark the voxel as inactive.
406  void setValueOff(const Coord& xyz, const ValueType& val);
407  /// Set the value of the voxel at the given offset and mark the voxel as inactive.
408  void setValueOff(Index offset, const ValueType& val);
409 
410  /// Mark the voxel at the given coordinates as active but don't change its value.
411  void setValueOn(const Coord& xyz) { mValueMask.setOn(LeafNode::coordToOffset(xyz)); }
412  /// Mark the voxel at the given offset as active but don't change its value.
413  void setValueOn(Index offset) { assert(offset < SIZE); mValueMask.setOn(offset); }
414  /// Set the value of the voxel at the given coordinates and mark the voxel as active.
415  void setValueOn(const Coord& xyz, const ValueType& val) {
416  this->setValueOn(LeafNode::coordToOffset(xyz), val);
417  }
418  /// Set the value of the voxel at the given coordinates and mark the voxel as active.
419  void setValue(const Coord& xyz, const ValueType& val) { this->setValueOn(xyz, val); }
420  /// Set the value of the voxel at the given offset and mark the voxel as active.
422  mBuffer.setValue(offset, val);
423  mValueMask.setOn(offset);
424  }
425 
426  /// @brief Apply a functor to the value of the voxel at the given offset
427  /// and mark the voxel as active.
428  template<typename ModifyOp>
429  void modifyValue(Index offset, const ModifyOp& op)
430  {
431  mBuffer.loadValues();
432  if (!mBuffer.empty()) {
433  // in-place modify value
434  ValueType& val = const_cast<ValueType&>(mBuffer[offset]);
435  op(val);
436  mValueMask.setOn(offset);
437  }
438  }
439 
440  /// @brief Apply a functor to the value of the voxel at the given coordinates
441  /// and mark the voxel as active.
442  template<typename ModifyOp>
443  void modifyValue(const Coord& xyz, const ModifyOp& op)
444  {
445  this->modifyValue(this->coordToOffset(xyz), op);
446  }
447 
448  /// Apply a functor to the voxel at the given coordinates.
449  template<typename ModifyOp>
450  void modifyValueAndActiveState(const Coord& xyz, const ModifyOp& op)
451  {
452  mBuffer.loadValues();
453  if (!mBuffer.empty()) {
454  const Index offset = this->coordToOffset(xyz);
455  bool state = mValueMask.isOn(offset);
456  // in-place modify value
457  ValueType& val = const_cast<ValueType&>(mBuffer[offset]);
458  op(val, state);
459  mValueMask.set(offset, state);
460  }
461  }
462 
463  /// Mark all voxels as active but don't change their values.
464  void setValuesOn() { mValueMask.setOn(); }
465  /// Mark all voxels as inactive but don't change their values.
466  void setValuesOff() { mValueMask.setOff(); }
467 
468  /// Return @c true if the voxel at the given coordinates is active.
469  bool isValueOn(const Coord& xyz) const {return this->isValueOn(LeafNode::coordToOffset(xyz));}
470  /// Return @c true if the voxel at the given offset is active.
471  bool isValueOn(Index offset) const { return mValueMask.isOn(offset); }
472 
473  /// Return @c false since leaf nodes never contain tiles.
474  static bool hasActiveTiles() { return false; }
475 
476  /// Set all voxels that lie outside the given axis-aligned box to the background.
477  void clip(const CoordBBox&, const ValueType& background);
478 
479  /// Set all voxels within an axis-aligned box to the specified value and active state.
480  void fill(const CoordBBox& bbox, const ValueType&, bool active = true);
481  /// Set all voxels within an axis-aligned box to the specified value and active state.
482  void denseFill(const CoordBBox& bbox, const ValueType& value, bool active = true)
483  {
484  this->fill(bbox, value, active);
485  }
486 
487  /// Set all voxels to the specified value but don't change their active states.
488  void fill(const ValueType& value);
489  /// Set all voxels to the specified value and active state.
490  void fill(const ValueType& value, bool active);
491 
492  /// @brief Copy into a dense grid the values of the voxels that lie within
493  /// a given bounding box.
494  ///
495  /// @param bbox inclusive bounding box of the voxels to be copied into the dense grid
496  /// @param dense dense grid with a stride in @e z of one (see tools::Dense
497  /// in tools/Dense.h for the required API)
498  ///
499  /// @note @a bbox is assumed to be identical to or contained in the coordinate domains
500  /// of both the dense grid and this node, i.e., no bounds checking is performed.
501  /// @note Consider using tools::CopyToDense in tools/Dense.h
502  /// instead of calling this method directly.
503  template<typename DenseT>
504  void copyToDense(const CoordBBox& bbox, DenseT& dense) const;
505 
506  /// @brief Copy from a dense grid into this node the values of the voxels
507  /// that lie within a given bounding box.
508  /// @details Only values that are different (by more than the given tolerance)
509  /// from the background value will be active. Other values are inactive
510  /// and truncated to the background value.
511  ///
512  /// @param bbox inclusive bounding box of the voxels to be copied into this node
513  /// @param dense dense grid with a stride in @e z of one (see tools::Dense
514  /// in tools/Dense.h for the required API)
515  /// @param background background value of the tree that this node belongs to
516  /// @param tolerance tolerance within which a value equals the background value
517  ///
518  /// @note @a bbox is assumed to be identical to or contained in the coordinate domains
519  /// of both the dense grid and this node, i.e., no bounds checking is performed.
520  /// @note Consider using tools::CopyFromDense in tools/Dense.h
521  /// instead of calling this method directly.
522  template<typename DenseT>
523  void copyFromDense(const CoordBBox& bbox, const DenseT& dense,
524  const ValueType& background, const ValueType& tolerance);
525 
526  /// @brief Return the value of the voxel at the given coordinates.
527  /// @note Used internally by ValueAccessor.
528  template<typename AccessorT>
529  const ValueType& getValueAndCache(const Coord& xyz, AccessorT&) const
530  {
531  return this->getValue(xyz);
532  }
533 
534  /// @brief Return @c true if the voxel at the given coordinates is active.
535  /// @note Used internally by ValueAccessor.
536  template<typename AccessorT>
537  bool isValueOnAndCache(const Coord& xyz, AccessorT&) const { return this->isValueOn(xyz); }
538 
539  /// @brief Change the value of the voxel at the given coordinates and mark it as active.
540  /// @note Used internally by ValueAccessor.
541  template<typename AccessorT>
542  void setValueAndCache(const Coord& xyz, const ValueType& val, AccessorT&)
543  {
544  this->setValueOn(xyz, val);
545  }
546 
547  /// @brief Change the value of the voxel at the given coordinates
548  /// but preserve its state.
549  /// @note Used internally by ValueAccessor.
550  template<typename AccessorT>
551  void setValueOnlyAndCache(const Coord& xyz, const ValueType& val, AccessorT&)
552  {
553  this->setValueOnly(xyz, val);
554  }
555 
556  /// @brief Apply a functor to the value of the voxel at the given coordinates
557  /// and mark the voxel as active.
558  /// @note Used internally by ValueAccessor.
559  template<typename ModifyOp, typename AccessorT>
560  void modifyValueAndCache(const Coord& xyz, const ModifyOp& op, AccessorT&)
561  {
562  this->modifyValue(xyz, op);
563  }
564 
565  /// Apply a functor to the voxel at the given coordinates.
566  /// @note Used internally by ValueAccessor.
567  template<typename ModifyOp, typename AccessorT>
568  void modifyValueAndActiveStateAndCache(const Coord& xyz, const ModifyOp& op, AccessorT&)
569  {
570  this->modifyValueAndActiveState(xyz, op);
571  }
572 
573  /// @brief Change the value of the voxel at the given coordinates and mark it as inactive.
574  /// @note Used internally by ValueAccessor.
575  template<typename AccessorT>
576  void setValueOffAndCache(const Coord& xyz, const ValueType& value, AccessorT&)
577  {
578  this->setValueOff(xyz, value);
579  }
580 
581  /// @brief Set the active state of the voxel at the given coordinates
582  /// without changing its value.
583  /// @note Used internally by ValueAccessor.
584  template<typename AccessorT>
585  void setActiveStateAndCache(const Coord& xyz, bool on, AccessorT&)
586  {
587  this->setActiveState(xyz, on);
588  }
589 
590  /// @brief Return @c true if the voxel at the given coordinates is active
591  /// and return the voxel value in @a val.
592  /// @note Used internally by ValueAccessor.
593  template<typename AccessorT>
594  bool probeValueAndCache(const Coord& xyz, ValueType& val, AccessorT&) const
595  {
596  return this->probeValue(xyz, val);
597  }
598 
599  /// @brief Return the value of the voxel at the given coordinates and return
600  /// its active state and level (i.e., 0) in @a state and @a level.
601  /// @note Used internally by ValueAccessor.
602  template<typename AccessorT>
603  const ValueType& getValue(const Coord& xyz, bool& state, int& level, AccessorT&) const
604  {
605  const Index offset = this->coordToOffset(xyz);
606  state = mValueMask.isOn(offset);
607  level = LEVEL;
608  return mBuffer[offset];
609  }
610 
611  /// @brief Return the LEVEL (=0) at which leaf node values reside.
612  /// @note Used internally by ValueAccessor (note last argument is a dummy).
613  template<typename AccessorT>
614  static Index getValueLevelAndCache(const Coord&, AccessorT&) { return LEVEL; }
615 
616  /// @brief Return a const reference to the first value in the buffer.
617  /// @note Though it is potentially risky you can convert this
618  /// to a non-const pointer by means of const_case<ValueType*>&.
619  const ValueType& getFirstValue() const { return mBuffer[0]; }
620  /// Return a const reference to the last value in the buffer.
621  const ValueType& getLastValue() const { return mBuffer[SIZE - 1]; }
622 
623  /// @brief Replace inactive occurrences of @a oldBackground with @a newBackground,
624  /// and inactive occurrences of @a -oldBackground with @a -newBackground.
625  void resetBackground(const ValueType& oldBackground, const ValueType& newBackground);
626 
627  void negate();
628 
629  /// @brief No-op
630  /// @details This function exists only to enable template instantiation.
631  void voxelizeActiveTiles(bool = true) {}
632 
633  template<MergePolicy Policy> void merge(const LeafNode&);
634  template<MergePolicy Policy> void merge(const ValueType& tileValue, bool tileActive);
635  template<MergePolicy Policy>
636  void merge(const LeafNode& other, const ValueType& /*bg*/, const ValueType& /*otherBG*/);
637 
638  /// @brief Union this node's set of active values with the active values
639  /// of the other node, whose @c ValueType may be different. So a
640  /// resulting voxel will be active if either of the original voxels
641  /// were active.
642  ///
643  /// @note This operation modifies only active states, not values.
644  template<typename OtherType>
645  void topologyUnion(const LeafNode<OtherType, Log2Dim>& other);
646 
647  /// @brief Intersect this node's set of active values with the active values
648  /// of the other node, whose @c ValueType may be different. So a
649  /// resulting voxel will be active only if both of the original voxels
650  /// were active.
651  ///
652  /// @details The last dummy argument is required to match the signature
653  /// for InternalNode::topologyIntersection.
654  ///
655  /// @note This operation modifies only active states, not
656  /// values. Also note that this operation can result in all voxels
657  /// being inactive so consider subsequnetly calling prune.
658  template<typename OtherType>
660 
661  /// @brief Difference this node's set of active values with the active values
662  /// of the other node, whose @c ValueType may be different. So a
663  /// resulting voxel will be active only if the original voxel is
664  /// active in this LeafNode and inactive in the other LeafNode.
665  ///
666  /// @details The last dummy argument is required to match the signature
667  /// for InternalNode::topologyDifference.
668  ///
669  /// @note This operation modifies only active states, not values.
670  /// Also, because it can deactivate all of this node's voxels,
671  /// consider subsequently calling prune.
672  template<typename OtherType>
673  void topologyDifference(const LeafNode<OtherType, Log2Dim>& other, const ValueType&);
674 
675  template<typename CombineOp>
676  void combine(const LeafNode& other, CombineOp& op);
677  template<typename CombineOp>
678  void combine(const ValueType& value, bool valueIsActive, CombineOp& op);
679 
680  template<typename CombineOp, typename OtherType /*= ValueType*/>
681  void combine2(const LeafNode& other, const OtherType&, bool valueIsActive, CombineOp&);
682  template<typename CombineOp, typename OtherNodeT /*= LeafNode*/>
683  void combine2(const ValueType&, const OtherNodeT& other, bool valueIsActive, CombineOp&);
684  template<typename CombineOp, typename OtherNodeT /*= LeafNode*/>
685  void combine2(const LeafNode& b0, const OtherNodeT& b1, CombineOp&);
686 
687  /// @brief Calls the templated functor BBoxOp with bounding box
688  /// information. An additional level argument is provided to the
689  /// callback.
690  ///
691  /// @note The bounding boxes are guarenteed to be non-overlapping.
692  template<typename BBoxOp> void visitActiveBBox(BBoxOp&) const;
693 
694  template<typename VisitorOp> void visit(VisitorOp&);
695  template<typename VisitorOp> void visit(VisitorOp&) const;
696 
697  template<typename OtherLeafNodeType, typename VisitorOp>
698  void visit2Node(OtherLeafNodeType& other, VisitorOp&);
699  template<typename OtherLeafNodeType, typename VisitorOp>
700  void visit2Node(OtherLeafNodeType& other, VisitorOp&) const;
701  template<typename IterT, typename VisitorOp>
702  void visit2(IterT& otherIter, VisitorOp&, bool otherIsLHS = false);
703  template<typename IterT, typename VisitorOp>
704  void visit2(IterT& otherIter, VisitorOp&, bool otherIsLHS = false) const;
705 
706  //@{
707  /// This function exists only to enable template instantiation.
708  void prune(const ValueType& /*tolerance*/ = zeroVal<ValueType>()) {}
709  void addLeaf(LeafNode*) {}
710  template<typename AccessorT>
711  void addLeafAndCache(LeafNode*, AccessorT&) {}
712  template<typename NodeT>
713  NodeT* stealNode(const Coord&, const ValueType&, bool) { return nullptr; }
714  template<typename NodeT>
715  NodeT* probeNode(const Coord&) { return nullptr; }
716  template<typename NodeT>
717  const NodeT* probeConstNode(const Coord&) const { return nullptr; }
718  template<typename ArrayT> void getNodes(ArrayT&) const {}
719  template<typename ArrayT> void stealNodes(ArrayT&, const ValueType&, bool) {}
720  //@}
721 
722  void addTile(Index level, const Coord&, const ValueType&, bool);
723  void addTile(Index offset, const ValueType&, bool);
724  template<typename AccessorT>
725  void addTileAndCache(Index, const Coord&, const ValueType&, bool, AccessorT&);
726 
727  //@{
728  /// @brief Return a pointer to this node.
729  LeafNode* touchLeaf(const Coord&) { return this; }
730  template<typename AccessorT>
731  LeafNode* touchLeafAndCache(const Coord&, AccessorT&) { return this; }
732  template<typename NodeT, typename AccessorT>
733  NodeT* probeNodeAndCache(const Coord&, AccessorT&)
734  {
736  if (!(std::is_same<NodeT, LeafNode>::value)) return nullptr;
737  return reinterpret_cast<NodeT*>(this);
739  }
740  LeafNode* probeLeaf(const Coord&) { return this; }
741  template<typename AccessorT>
742  LeafNode* probeLeafAndCache(const Coord&, AccessorT&) { return this; }
743  //@}
744  //@{
745  /// @brief Return a @const pointer to this node.
746  const LeafNode* probeConstLeaf(const Coord&) const { return this; }
747  template<typename AccessorT>
748  const LeafNode* probeConstLeafAndCache(const Coord&, AccessorT&) const { return this; }
749  template<typename AccessorT>
750  const LeafNode* probeLeafAndCache(const Coord&, AccessorT&) const { return this; }
751  const LeafNode* probeLeaf(const Coord&) const { return this; }
752  template<typename NodeT, typename AccessorT>
753  const NodeT* probeConstNodeAndCache(const Coord&, AccessorT&) const
754  {
756  if (!(std::is_same<NodeT, LeafNode>::value)) return nullptr;
757  return reinterpret_cast<const NodeT*>(this);
759  }
760  //@}
761 
762  /// Return @c true if all of this node's values have the same active state
763  /// and are in the range this->getFirstValue() +/- @a tolerance.
764  ///
765  ///
766  /// @param firstValue Is updated with the first value of this leaf node.
767  /// @param state Is updated with the state of all values IF method
768  /// returns @c true. Else the value is undefined!
769  /// @param tolerance The tolerance used to determine if values are
770  /// approximatly equal to the for value.
771  bool isConstant(ValueType& firstValue, bool& state,
772  const ValueType& tolerance = zeroVal<ValueType>()) const;
773 
774  /// Return @c true if all of this node's values have the same active state
775  /// and the range (@a maxValue - @a minValue) < @a tolerance.
776  ///
777  /// @param minValue Is updated with the minimum of all values IF method
778  /// returns @c true. Else the value is undefined!
779  /// @param maxValue Is updated with the maximum of all values IF method
780  /// returns @c true. Else the value is undefined!
781  /// @param state Is updated with the state of all values IF method
782  /// returns @c true. Else the value is undefined!
783  /// @param tolerance The tolerance used to determine if values are
784  /// approximatly constant.
785  bool isConstant(ValueType& minValue, ValueType& maxValue,
786  bool& state, const ValueType& tolerance = zeroVal<ValueType>()) const;
787 
788 
789  /// @brief Computes the median value of all the active AND inactive voxels in this node.
790  /// @return The median value of all values in this node.
791  ///
792  /// @param tmp Optional temporary storage that can hold at least NUM_VALUES values
793  /// Use of this temporary storage can improve performance
794  /// when this method is called multiple times.
795  ///
796  /// @note If tmp = this->buffer().data() then the median
797  /// value is computed very efficiently (in place) but
798  /// the voxel values in this node are re-shuffeled!
799  ///
800  /// @warning If tmp != nullptr then it is the responsibility of
801  /// the client code that it points to enough memory to
802  /// hold NUM_VALUES elements of type ValueType.
803  ValueType medianAll(ValueType *tmp = nullptr) const;
804 
805  /// @brief Computes the median value of all the active voxels in this node.
806  /// @return The number of active voxels.
807  ///
808  /// @param value If the return value is non zero @a value is updated
809  /// with the median value.
810  ///
811  /// @param tmp Optional temporary storage that can hold at least
812  /// as many values as there are active voxels in this node.
813  /// Use of this temporary storage can improve performance
814  /// when this method is called multiple times.
815  ///
816  /// @warning If tmp != nullptr then it is the responsibility of
817  /// the client code that it points to enough memory to
818  /// hold the number of active voxels of type ValueType.
819  Index medianOn(ValueType &value, ValueType *tmp = nullptr) const;
820 
821  /// @brief Computes the median value of all the inactive voxels in this node.
822  /// @return The number of inactive voxels.
823  ///
824  /// @param value If the return value is non zero @a value is updated
825  /// with the median value.
826  ///
827  /// @param tmp Optional temporary storage that can hold at least
828  /// as many values as there are inactive voxels in this node.
829  /// Use of this temporary storage can improve performance
830  /// when this method is called multiple times.
831  ///
832  /// @warning If tmp != nullptr then it is the responsibility of
833  /// the client code that it points to enough memory to
834  /// hold the number of inactive voxels of type ValueType.
835  Index medianOff(ValueType &value, ValueType *tmp = nullptr) const;
836 
837  /// Return @c true if all of this node's values are inactive.
838  bool isInactive() const { return mValueMask.isOff(); }
839 
840 protected:
841  friend class ::TestLeaf;
842  template<typename> friend class ::TestLeafIO;
843 
844  // During topology-only construction, access is needed
845  // to protected/private members of other template instances.
846  template<typename, Index> friend class LeafNode;
847 
854 
855  // Allow iterators to call mask accessor methods (see below).
856  /// @todo Make mask accessors public?
860 
861  // Mask accessors
862 public:
863  bool isValueMaskOn(Index n) const { return mValueMask.isOn(n); }
864  bool isValueMaskOn() const { return mValueMask.isOn(); }
865  bool isValueMaskOff(Index n) const { return mValueMask.isOff(n); }
866  bool isValueMaskOff() const { return mValueMask.isOff(); }
867  const NodeMaskType& getValueMask() const { return mValueMask; }
868  NodeMaskType& getValueMask() { return mValueMask; }
869  const NodeMaskType& valueMask() const { return mValueMask; }
870  void setValueMask(const NodeMaskType& mask) { mValueMask = mask; }
871  bool isChildMaskOn(Index) const { return false; } // leaf nodes have no children
872  bool isChildMaskOff(Index) const { return true; }
873  bool isChildMaskOff() const { return true; }
874 protected:
875  void setValueMask(Index n, bool on) { mValueMask.set(n, on); }
876  void setValueMaskOn(Index n) { mValueMask.setOn(n); }
877  void setValueMaskOff(Index n) { mValueMask.setOff(n); }
878 
879  inline void skipCompressedValues(bool seekable, std::istream&, bool fromHalf);
880 
881  /// Compute the origin of the leaf node that contains the voxel with the given coordinates.
882  static void evalNodeOrigin(Coord& xyz) { xyz &= ~(DIM - 1); }
883 
884  template<typename NodeT, typename VisitorOp, typename ChildAllIterT>
885  static inline void doVisit(NodeT&, VisitorOp&);
886 
887  template<typename NodeT, typename OtherNodeT, typename VisitorOp,
888  typename ChildAllIterT, typename OtherChildAllIterT>
889  static inline void doVisit2Node(NodeT& self, OtherNodeT& other, VisitorOp&);
890 
891  template<typename NodeT, typename VisitorOp,
892  typename ChildAllIterT, typename OtherChildAllIterT>
893  static inline void doVisit2(NodeT& self, OtherChildAllIterT&, VisitorOp&, bool otherIsLHS);
894 
895 private:
896  /// Buffer containing the actual data values
897  Buffer mBuffer;
898  /// Bitmask that determines which voxels are active
899  NodeMaskType mValueMask;
900  /// Global grid index coordinates (x,y,z) of the local origin of this node
901  Coord mOrigin;
902 }; // end of LeafNode class
903 
904 
905 ////////////////////////////////////////
906 
907 
908 //@{
909 /// Helper metafunction used to implement LeafNode::SameConfiguration
910 /// (which, as an inner class, can't be independently specialized)
911 template<Index Dim1, typename NodeT2>
912 struct SameLeafConfig { static const bool value = false; };
913 
914 template<Index Dim1, typename T2>
915 struct SameLeafConfig<Dim1, LeafNode<T2, Dim1> > { static const bool value = true; };
916 //@}
917 
918 
919 ////////////////////////////////////////
920 
921 
922 template<typename T, Index Log2Dim>
923 inline
925  mValueMask(),//default is off!
926  mOrigin(0, 0, 0)
927 {
928 }
929 
930 
931 template<typename T, Index Log2Dim>
932 inline
933 LeafNode<T, Log2Dim>::LeafNode(const Coord& xyz, const ValueType& val, bool active):
934  mBuffer(val),
935  mValueMask(active),
936  mOrigin(xyz & (~(DIM - 1)))
937 {
938 }
939 
940 
941 template<typename T, Index Log2Dim>
942 inline
943 LeafNode<T, Log2Dim>::LeafNode(PartialCreate, const Coord& xyz, const ValueType& val, bool active):
944  mBuffer(PartialCreate(), val),
945  mValueMask(active),
946  mOrigin(xyz & (~(DIM - 1)))
947 {
948 }
949 
950 
951 template<typename T, Index Log2Dim>
952 inline
954  mBuffer(other.mBuffer),
955  mValueMask(other.valueMask()),
956  mOrigin(other.mOrigin)
957 {
958 }
959 
960 
961 // Copy-construct from a leaf node with the same configuration but a different ValueType.
962 template<typename T, Index Log2Dim>
963 template<typename OtherValueType>
964 inline
966  mValueMask(other.valueMask()),
967  mOrigin(other.mOrigin)
968 {
969  struct Local {
970  /// @todo Consider using a value conversion functor passed as an argument instead.
971  static inline ValueType convertValue(const OtherValueType& val) { return ValueType(val); }
972  };
973 
974  for (Index i = 0; i < SIZE; ++i) {
975  mBuffer[i] = Local::convertValue(other.mBuffer[i]);
976  }
977 }
978 
979 
980 template<typename T, Index Log2Dim>
981 template<typename OtherValueType>
982 inline
984  const ValueType& background, TopologyCopy):
985  mBuffer(background),
986  mValueMask(other.valueMask()),
987  mOrigin(other.mOrigin)
988 {
989 }
990 
991 
992 template<typename T, Index Log2Dim>
993 template<typename OtherValueType>
994 inline
996  const ValueType& offValue, const ValueType& onValue, TopologyCopy):
997  mValueMask(other.valueMask()),
998  mOrigin(other.mOrigin)
999 {
1000  for (Index i = 0; i < SIZE; ++i) {
1001  mBuffer[i] = (mValueMask.isOn(i) ? onValue : offValue);
1002  }
1003 }
1004 
1005 
1006 template<typename T, Index Log2Dim>
1007 inline
1009 {
1010 }
1011 
1012 
1013 template<typename T, Index Log2Dim>
1014 inline std::string
1016 {
1017  std::ostringstream ostr;
1018  ostr << "LeafNode @" << mOrigin << ": " << mBuffer;
1019  return ostr.str();
1020 }
1021 
1022 
1023 ////////////////////////////////////////
1024 
1025 
1026 template<typename T, Index Log2Dim>
1027 inline Index
1029 {
1030  assert ((xyz[0] & (DIM-1u)) < DIM && (xyz[1] & (DIM-1u)) < DIM && (xyz[2] & (DIM-1u)) < DIM);
1031  return ((xyz[0] & (DIM-1u)) << 2*Log2Dim)
1032  + ((xyz[1] & (DIM-1u)) << Log2Dim)
1033  + (xyz[2] & (DIM-1u));
1034 }
1035 
1036 template<typename T, Index Log2Dim>
1037 inline Coord
1039 {
1040  assert(n<(1<< 3*Log2Dim));
1041  Coord xyz;
1042  xyz.setX(n >> 2*Log2Dim);
1043  n &= ((1<<2*Log2Dim)-1);
1044  xyz.setY(n >> Log2Dim);
1045  xyz.setZ(n & ((1<<Log2Dim)-1));
1046  return xyz;
1047 }
1048 
1049 
1050 template<typename T, Index Log2Dim>
1051 inline Coord
1053 {
1054  return (this->offsetToLocalCoord(n) + this->origin());
1055 }
1056 
1057 
1058 ////////////////////////////////////////
1059 
1060 
1061 template<typename ValueT, Index Log2Dim>
1062 inline const ValueT&
1064 {
1065  return this->getValue(LeafNode::coordToOffset(xyz));
1066 }
1067 
1068 template<typename ValueT, Index Log2Dim>
1069 inline const ValueT&
1071 {
1072  assert(offset < SIZE);
1073  return mBuffer[offset];
1074 }
1075 
1076 
1077 template<typename T, Index Log2Dim>
1078 inline bool
1080 {
1081  return this->probeValue(LeafNode::coordToOffset(xyz), val);
1082 }
1083 
1084 template<typename T, Index Log2Dim>
1085 inline bool
1087 {
1088  assert(offset < SIZE);
1089  val = mBuffer[offset];
1090  return mValueMask.isOn(offset);
1091 }
1092 
1093 
1094 template<typename T, Index Log2Dim>
1095 inline void
1097 {
1098  this->setValueOff(LeafNode::coordToOffset(xyz), val);
1099 }
1100 
1101 template<typename T, Index Log2Dim>
1102 inline void
1104 {
1105  assert(offset < SIZE);
1106  mBuffer.setValue(offset, val);
1107  mValueMask.setOff(offset);
1108 }
1109 
1110 
1111 template<typename T, Index Log2Dim>
1112 inline void
1113 LeafNode<T, Log2Dim>::setActiveState(const Coord& xyz, bool on)
1114 {
1115  mValueMask.set(this->coordToOffset(xyz), on);
1116 }
1117 
1118 
1119 template<typename T, Index Log2Dim>
1120 inline void
1122 {
1123  this->setValueOnly(LeafNode::coordToOffset(xyz), val);
1124 }
1125 
1126 template<typename T, Index Log2Dim>
1127 inline void
1129 {
1130  assert(offset<SIZE); mBuffer.setValue(offset, val);
1131 }
1132 
1133 
1134 ////////////////////////////////////////
1135 
1136 
1137 template<typename T, Index Log2Dim>
1138 inline void
1139 LeafNode<T, Log2Dim>::clip(const CoordBBox& clipBBox, const T& background)
1140 {
1141  CoordBBox nodeBBox = this->getNodeBoundingBox();
1142  if (!clipBBox.hasOverlap(nodeBBox)) {
1143  // This node lies completely outside the clipping region. Fill it with the background.
1144  this->fill(background, /*active=*/false);
1145  } else if (clipBBox.isInside(nodeBBox)) {
1146  // This node lies completely inside the clipping region. Leave it intact.
1147  return;
1148  }
1149 
1150  // This node isn't completely contained inside the clipping region.
1151  // Set any voxels that lie outside the region to the background value.
1152 
1153  // Construct a boolean mask that is on inside the clipping region and off outside it.
1155  nodeBBox.intersect(clipBBox);
1156  Coord xyz;
1157  int &x = xyz.x(), &y = xyz.y(), &z = xyz.z();
1158  for (x = nodeBBox.min().x(); x <= nodeBBox.max().x(); ++x) {
1159  for (y = nodeBBox.min().y(); y <= nodeBBox.max().y(); ++y) {
1160  for (z = nodeBBox.min().z(); z <= nodeBBox.max().z(); ++z) {
1161  mask.setOn(static_cast<Index32>(this->coordToOffset(xyz)));
1162  }
1163  }
1164  }
1165 
1166  // Set voxels that lie in the inactive region of the mask (i.e., outside
1167  // the clipping region) to the background value.
1168  for (MaskOffIterator maskIter = mask.beginOff(); maskIter; ++maskIter) {
1169  this->setValueOff(maskIter.pos(), background);
1170  }
1171 }
1172 
1173 
1174 ////////////////////////////////////////
1175 
1176 
1177 template<typename T, Index Log2Dim>
1178 inline void
1179 LeafNode<T, Log2Dim>::fill(const CoordBBox& bbox, const ValueType& value, bool active)
1180 {
1181  if (!this->allocate()) return;
1182 
1183  auto clippedBBox = this->getNodeBoundingBox();
1184  clippedBBox.intersect(bbox);
1185  if (!clippedBBox) return;
1186 
1187  for (Int32 x = clippedBBox.min().x(); x <= clippedBBox.max().x(); ++x) {
1188  const Index offsetX = (x & (DIM-1u)) << 2*Log2Dim;
1189  for (Int32 y = clippedBBox.min().y(); y <= clippedBBox.max().y(); ++y) {
1190  const Index offsetXY = offsetX + ((y & (DIM-1u)) << Log2Dim);
1191  for (Int32 z = clippedBBox.min().z(); z <= clippedBBox.max().z(); ++z) {
1192  const Index offset = offsetXY + (z & (DIM-1u));
1193  mBuffer[offset] = value;
1194  mValueMask.set(offset, active);
1195  }
1196  }
1197  }
1198 }
1199 
1200 template<typename T, Index Log2Dim>
1201 inline void
1203 {
1204  mBuffer.fill(value);
1205 }
1206 
1207 template<typename T, Index Log2Dim>
1208 inline void
1210 {
1211  mBuffer.fill(value);
1212  mValueMask.set(active);
1213 }
1214 
1215 
1216 ////////////////////////////////////////
1217 
1218 
1219 template<typename T, Index Log2Dim>
1220 template<typename DenseT>
1221 inline void
1222 LeafNode<T, Log2Dim>::copyToDense(const CoordBBox& bbox, DenseT& dense) const
1223 {
1224  mBuffer.loadValues();
1225 
1226  using DenseValueType = typename DenseT::ValueType;
1227 
1228  const size_t xStride = dense.xStride(), yStride = dense.yStride(), zStride = dense.zStride();
1229  const Coord& min = dense.bbox().min();
1230  DenseValueType* t0 = dense.data() + zStride * (bbox.min()[2] - min[2]); // target array
1231  const T* s0 = &mBuffer[bbox.min()[2] & (DIM-1u)]; // source array
1232  for (Int32 x = bbox.min()[0], ex = bbox.max()[0] + 1; x < ex; ++x) {
1233  DenseValueType* t1 = t0 + xStride * (x - min[0]);
1234  const T* s1 = s0 + ((x & (DIM-1u)) << 2*Log2Dim);
1235  for (Int32 y = bbox.min()[1], ey = bbox.max()[1] + 1; y < ey; ++y) {
1236  DenseValueType* t2 = t1 + yStride * (y - min[1]);
1237  const T* s2 = s1 + ((y & (DIM-1u)) << Log2Dim);
1238  for (Int32 z = bbox.min()[2], ez = bbox.max()[2] + 1; z < ez; ++z, t2 += zStride) {
1239  *t2 = DenseValueType(*s2++);
1240  }
1241  }
1242  }
1243 }
1244 
1245 
1246 template<typename T, Index Log2Dim>
1247 template<typename DenseT>
1248 inline void
1249 LeafNode<T, Log2Dim>::copyFromDense(const CoordBBox& bbox, const DenseT& dense,
1250  const ValueType& background, const ValueType& tolerance)
1251 {
1252  if (!this->allocate()) return;
1253 
1254  using DenseValueType = typename DenseT::ValueType;
1255 
1256  const size_t xStride = dense.xStride(), yStride = dense.yStride(), zStride = dense.zStride();
1257  const Coord& min = dense.bbox().min();
1258 
1259  const DenseValueType* s0 = dense.data() + zStride * (bbox.min()[2] - min[2]); // source
1260  const Int32 n0 = bbox.min()[2] & (DIM-1u);
1261  for (Int32 x = bbox.min()[0], ex = bbox.max()[0]+1; x < ex; ++x) {
1262  const DenseValueType* s1 = s0 + xStride * (x - min[0]);
1263  const Int32 n1 = n0 + ((x & (DIM-1u)) << 2*LOG2DIM);
1264  for (Int32 y = bbox.min()[1], ey = bbox.max()[1]+1; y < ey; ++y) {
1265  const DenseValueType* s2 = s1 + yStride * (y - min[1]);
1266  Int32 n2 = n1 + ((y & (DIM-1u)) << LOG2DIM);
1267  for (Int32 z = bbox.min()[2], ez = bbox.max()[2]+1; z < ez; ++z, ++n2, s2 += zStride) {
1268  if (math::isApproxEqual(background, ValueType(*s2), tolerance)) {
1269  mValueMask.setOff(n2);
1270  mBuffer[n2] = background;
1271  } else {
1272  mValueMask.setOn(n2);
1273  mBuffer[n2] = ValueType(*s2);
1274  }
1275  }
1276  }
1277  }
1278 }
1279 
1280 
1281 ////////////////////////////////////////
1282 
1283 
1284 template<typename T, Index Log2Dim>
1285 inline void
1286 LeafNode<T, Log2Dim>::readTopology(std::istream& is, bool /*fromHalf*/)
1287 {
1288  mValueMask.load(is);
1289 }
1290 
1291 
1292 template<typename T, Index Log2Dim>
1293 inline void
1294 LeafNode<T, Log2Dim>::writeTopology(std::ostream& os, bool /*toHalf*/) const
1295 {
1296  mValueMask.save(os);
1297 }
1298 
1299 
1300 ////////////////////////////////////////
1301 
1302 
1303 
1304 template<typename T, Index Log2Dim>
1305 inline void
1306 LeafNode<T,Log2Dim>::skipCompressedValues(bool seekable, std::istream& is, bool fromHalf)
1307 {
1308  if (seekable) {
1309  // Seek over voxel values.
1310  io::readCompressedValues<ValueType, NodeMaskType>(
1311  is, nullptr, SIZE, mValueMask, fromHalf);
1312  } else {
1313  // Read and discard voxel values.
1314  Buffer temp;
1315  io::readCompressedValues(is, temp.mData, SIZE, mValueMask, fromHalf);
1316  }
1317 }
1318 
1319 
1320 template<typename T, Index Log2Dim>
1321 inline void
1322 LeafNode<T,Log2Dim>::readBuffers(std::istream& is, bool fromHalf)
1323 {
1324  this->readBuffers(is, CoordBBox::inf(), fromHalf);
1325 }
1326 
1327 
1328 template<typename T, Index Log2Dim>
1329 inline void
1330 LeafNode<T,Log2Dim>::readBuffers(std::istream& is, const CoordBBox& clipBBox, bool fromHalf)
1331 {
1333  const bool seekable = meta && meta->seekable();
1334 
1335  std::streamoff maskpos = is.tellg();
1336 
1337  if (seekable) {
1338  // Seek over the value mask.
1339  mValueMask.seek(is);
1340  } else {
1341  // Read in the value mask.
1342  mValueMask.load(is);
1343  }
1344 
1345  int8_t numBuffers = 1;
1347  // Read in the origin.
1348  is.read(reinterpret_cast<char*>(&mOrigin), sizeof(Coord::ValueType) * 3);
1349 
1350  // Read in the number of buffers, which should now always be one.
1351  is.read(reinterpret_cast<char*>(&numBuffers), sizeof(int8_t));
1352  }
1353 
1354  CoordBBox nodeBBox = this->getNodeBoundingBox();
1355  if (!clipBBox.hasOverlap(nodeBBox)) {
1356  // This node lies completely outside the clipping region.
1357  skipCompressedValues(seekable, is, fromHalf);
1358  mValueMask.setOff();
1359  mBuffer.setOutOfCore(false);
1360  } else {
1361  // If this node lies completely inside the clipping region and it is being read
1362  // from a memory-mapped file, delay loading of its buffer until the buffer
1363  // is actually accessed. (If this node requires clipping, its buffer
1364  // must be accessed and therefore must be loaded.)
1365  io::MappedFile::Ptr mappedFile = io::getMappedFilePtr(is);
1366  const bool delayLoad = ((mappedFile.get() != nullptr) && clipBBox.isInside(nodeBBox));
1367 
1368  if (delayLoad) {
1369  mBuffer.setOutOfCore(true);
1370  mBuffer.mFileInfo = new typename Buffer::FileInfo;
1371  mBuffer.mFileInfo->meta = meta;
1372  mBuffer.mFileInfo->bufpos = is.tellg();
1373  mBuffer.mFileInfo->mapping = mappedFile;
1374  // Save the offset to the value mask, because the in-memory copy
1375  // might change before the value buffer gets read.
1376  mBuffer.mFileInfo->maskpos = maskpos;
1377  // Skip over voxel values.
1378  skipCompressedValues(seekable, is, fromHalf);
1379  } else {
1380  mBuffer.allocate();
1381  io::readCompressedValues(is, mBuffer.mData, SIZE, mValueMask, fromHalf);
1382  mBuffer.setOutOfCore(false);
1383 
1384  // Get this tree's background value.
1385  T background = zeroVal<T>();
1386  if (const void* bgPtr = io::getGridBackgroundValuePtr(is)) {
1387  background = *static_cast<const T*>(bgPtr);
1388  }
1389  this->clip(clipBBox, background);
1390  }
1391  }
1392 
1393  if (numBuffers > 1) {
1394  // Read in and discard auxiliary buffers that were created with earlier
1395  // versions of the library. (Auxiliary buffers are not mask compressed.)
1396  const bool zipped = io::getDataCompression(is) & io::COMPRESS_ZIP;
1397  Buffer temp;
1398  for (int i = 1; i < numBuffers; ++i) {
1399  if (fromHalf) {
1401  } else {
1402  io::readData<T>(is, temp.mData, SIZE, zipped);
1403  }
1404  }
1405  }
1406 
1407  // increment the leaf number
1408  if (meta) meta->setLeaf(meta->leaf() + 1);
1409 }
1410 
1411 
1412 template<typename T, Index Log2Dim>
1413 inline void
1414 LeafNode<T, Log2Dim>::writeBuffers(std::ostream& os, bool toHalf) const
1415 {
1416  // Write out the value mask.
1417  mValueMask.save(os);
1418 
1419  mBuffer.loadValues();
1420 
1421  io::writeCompressedValues(os, mBuffer.mData, SIZE,
1422  mValueMask, /*childMask=*/NodeMaskType(), toHalf);
1423 }
1424 
1425 
1426 ////////////////////////////////////////
1427 
1428 
1429 template<typename T, Index Log2Dim>
1430 inline bool
1432 {
1433  return mOrigin == other.mOrigin &&
1434  mValueMask == other.valueMask() &&
1435  mBuffer == other.mBuffer;
1436 }
1437 
1438 
1439 template<typename T, Index Log2Dim>
1440 inline Index64
1442 {
1443  // Use sizeof(*this) to capture alignment-related padding
1444  // (but note that sizeof(*this) includes sizeof(mBuffer)).
1445  return sizeof(*this) + mBuffer.memUsage() - sizeof(mBuffer);
1446 }
1447 
1448 
1449 template<typename T, Index Log2Dim>
1450 inline void
1451 LeafNode<T, Log2Dim>::evalActiveBoundingBox(CoordBBox& bbox, bool visitVoxels) const
1452 {
1453  CoordBBox this_bbox = this->getNodeBoundingBox();
1454  if (bbox.isInside(this_bbox)) return;//this LeafNode is already enclosed in the bbox
1455  if (ValueOnCIter iter = this->cbeginValueOn()) {//any active values?
1456  if (visitVoxels) {//use voxel granularity?
1457  this_bbox.reset();
1458  for(; iter; ++iter) this_bbox.expand(this->offsetToLocalCoord(iter.pos()));
1459  this_bbox.translate(this->origin());
1460  }
1461  bbox.expand(this_bbox);
1462  }
1463 }
1464 
1465 
1466 template<typename T, Index Log2Dim>
1467 template<typename OtherType, Index OtherLog2Dim>
1468 inline bool
1470 {
1471  assert(other);
1472  return (Log2Dim == OtherLog2Dim && mValueMask == other->getValueMask());
1473 }
1474 
1475 template<typename T, Index Log2Dim>
1476 inline bool
1478  bool& state,
1479  const ValueType& tolerance) const
1480 {
1481  if (!mValueMask.isConstant(state)) return false;// early termination
1482  firstValue = mBuffer[0];
1483  for (Index i = 1; i < SIZE; ++i) {
1484  if ( !math::isApproxEqual(mBuffer[i], firstValue, tolerance) ) return false;// early termination
1485  }
1486  return true;
1487 }
1488 
1489 template<typename T, Index Log2Dim>
1490 inline bool
1492  ValueType& maxValue,
1493  bool& state,
1494  const ValueType& tolerance) const
1495 {
1496  if (!mValueMask.isConstant(state)) return false;// early termination
1497  minValue = maxValue = mBuffer[0];
1498  for (Index i = 1; i < SIZE; ++i) {
1499  const T& v = mBuffer[i];
1500  if (v < minValue) {
1501  if ((maxValue - v) > tolerance) return false;// early termination
1502  minValue = v;
1503  } else if (v > maxValue) {
1504  if ((v - minValue) > tolerance) return false;// early termination
1505  maxValue = v;
1506  }
1507  }
1508  return true;
1509 }
1510 
1511 template<typename T, Index Log2Dim>
1512 inline T
1514 {
1515  std::unique_ptr<T[]> data(nullptr);
1516  if (tmp == nullptr) {//allocate temporary storage
1517  data.reset(new T[NUM_VALUES]);
1518  tmp = data.get();
1519  }
1520  if (tmp != mBuffer.data()) {
1521  const T* src = mBuffer.data();
1522  for (T* dst = tmp; dst-tmp < NUM_VALUES;) *dst++ = *src++;
1523  }
1524  static const size_t midpoint = (NUM_VALUES - 1) >> 1;
1525  std::nth_element(tmp, tmp + midpoint, tmp + NUM_VALUES);
1526  return tmp[midpoint];
1527 }
1528 
1529 template<typename T, Index Log2Dim>
1530 inline Index
1532 {
1533  const Index count = mValueMask.countOn();
1534  if (count == NUM_VALUES) {//special case: all voxels are active
1535  value = this->medianAll(tmp);
1536  return NUM_VALUES;
1537  } else if (count == 0) {
1538  return 0;
1539  }
1540  std::unique_ptr<T[]> data(nullptr);
1541  if (tmp == nullptr) {//allocate temporary storage
1542  data.reset(new T[count]);// 0 < count < NUM_VALUES
1543  tmp = data.get();
1544  }
1545  for (auto iter=this->cbeginValueOn(); iter; ++iter) *tmp++ = *iter;
1546  T *begin = tmp - count;
1547  const size_t midpoint = (count - 1) >> 1;
1548  std::nth_element(begin, begin + midpoint, tmp);
1549  value = begin[midpoint];
1550  return count;
1551 }
1552 
1553 template<typename T, Index Log2Dim>
1554 inline Index
1556 {
1557  const Index count = mValueMask.countOff();
1558  if (count == NUM_VALUES) {//special case: all voxels are inactive
1559  value = this->medianAll(tmp);
1560  return NUM_VALUES;
1561  } else if (count == 0) {
1562  return 0;
1563  }
1564  std::unique_ptr<T[]> data(nullptr);
1565  if (tmp == nullptr) {//allocate temporary storage
1566  data.reset(new T[count]);// 0 < count < NUM_VALUES
1567  tmp = data.get();
1568  }
1569  for (auto iter=this->cbeginValueOff(); iter; ++iter) *tmp++ = *iter;
1570  T *begin = tmp - count;
1571  const size_t midpoint = (count - 1) >> 1;
1572  std::nth_element(begin, begin + midpoint, tmp);
1573  value = begin[midpoint];
1574  return count;
1575 }
1576 
1577 ////////////////////////////////////////
1578 
1579 
1580 template<typename T, Index Log2Dim>
1581 inline void
1582 LeafNode<T, Log2Dim>::addTile(Index /*level*/, const Coord& xyz, const ValueType& val, bool active)
1583 {
1584  this->addTile(this->coordToOffset(xyz), val, active);
1585 }
1586 
1587 template<typename T, Index Log2Dim>
1588 inline void
1590 {
1591  assert(offset < SIZE);
1592  setValueOnly(offset, val);
1593  setActiveState(offset, active);
1594 }
1595 
1596 template<typename T, Index Log2Dim>
1597 template<typename AccessorT>
1598 inline void
1600  const ValueType& val, bool active, AccessorT&)
1601 {
1602  this->addTile(level, xyz, val, active);
1603 }
1604 
1605 
1606 ////////////////////////////////////////
1607 
1608 
1609 template<typename T, Index Log2Dim>
1610 inline void
1612  const ValueType& newBackground)
1613 {
1614  if (!this->allocate()) return;
1615 
1616  typename NodeMaskType::OffIterator iter;
1617  // For all inactive values...
1618  for (iter = this->mValueMask.beginOff(); iter; ++iter) {
1619  ValueType &inactiveValue = mBuffer[iter.pos()];
1620  if (math::isApproxEqual(inactiveValue, oldBackground)) {
1621  inactiveValue = newBackground;
1622  } else if (math::isApproxEqual(inactiveValue, math::negative(oldBackground))) {
1623  inactiveValue = math::negative(newBackground);
1624  }
1625  }
1626 }
1627 
1628 
1629 template<typename T, Index Log2Dim>
1630 template<MergePolicy Policy>
1631 inline void
1633 {
1634  if (!this->allocate()) return;
1635 
1637  if (Policy == MERGE_NODES) return;
1638  typename NodeMaskType::OnIterator iter = other.valueMask().beginOn();
1639  for (; iter; ++iter) {
1640  const Index n = iter.pos();
1641  if (mValueMask.isOff(n)) {
1642  mBuffer[n] = other.mBuffer[n];
1643  mValueMask.setOn(n);
1644  }
1645  }
1647 }
1648 
1649 template<typename T, Index Log2Dim>
1650 template<MergePolicy Policy>
1651 inline void
1653  const ValueType& /*bg*/, const ValueType& /*otherBG*/)
1654 {
1655  this->template merge<Policy>(other);
1656 }
1657 
1658 template<typename T, Index Log2Dim>
1659 template<MergePolicy Policy>
1660 inline void
1661 LeafNode<T, Log2Dim>::merge(const ValueType& tileValue, bool tileActive)
1662 {
1663  if (!this->allocate()) return;
1664 
1666  if (Policy != MERGE_ACTIVE_STATES_AND_NODES) return;
1667  if (!tileActive) return;
1668  // Replace all inactive values with the active tile value.
1669  for (typename NodeMaskType::OffIterator iter = mValueMask.beginOff(); iter; ++iter) {
1670  const Index n = iter.pos();
1671  mBuffer[n] = tileValue;
1672  mValueMask.setOn(n);
1673  }
1675 }
1676 
1677 
1678 template<typename T, Index Log2Dim>
1679 template<typename OtherType>
1680 inline void
1682 {
1683  mValueMask |= other.valueMask();
1684 }
1685 
1686 template<typename T, Index Log2Dim>
1687 template<typename OtherType>
1688 inline void
1690  const ValueType&)
1691 {
1692  mValueMask &= other.valueMask();
1693 }
1694 
1695 template<typename T, Index Log2Dim>
1696 template<typename OtherType>
1697 inline void
1699  const ValueType&)
1700 {
1701  mValueMask &= !other.valueMask();
1702 }
1703 
1704 template<typename T, Index Log2Dim>
1705 inline void
1707 {
1708  if (!this->allocate()) return;
1709 
1710  for (Index i = 0; i < SIZE; ++i) {
1711  mBuffer[i] = -mBuffer[i];
1712  }
1713 }
1714 
1715 
1716 ////////////////////////////////////////
1717 
1718 
1719 template<typename T, Index Log2Dim>
1720 template<typename CombineOp>
1721 inline void
1722 LeafNode<T, Log2Dim>::combine(const LeafNode& other, CombineOp& op)
1723 {
1724  if (!this->allocate()) return;
1725 
1727  for (Index i = 0; i < SIZE; ++i) {
1728  op(args.setARef(mBuffer[i])
1729  .setAIsActive(mValueMask.isOn(i))
1730  .setBRef(other.mBuffer[i])
1731  .setBIsActive(other.valueMask().isOn(i))
1732  .setResultRef(mBuffer[i]));
1733  mValueMask.set(i, args.resultIsActive());
1734  }
1735 }
1736 
1737 
1738 template<typename T, Index Log2Dim>
1739 template<typename CombineOp>
1740 inline void
1741 LeafNode<T, Log2Dim>::combine(const ValueType& value, bool valueIsActive, CombineOp& op)
1742 {
1743  if (!this->allocate()) return;
1744 
1746  args.setBRef(value).setBIsActive(valueIsActive);
1747  for (Index i = 0; i < SIZE; ++i) {
1748  op(args.setARef(mBuffer[i])
1749  .setAIsActive(mValueMask.isOn(i))
1750  .setResultRef(mBuffer[i]));
1751  mValueMask.set(i, args.resultIsActive());
1752  }
1753 }
1754 
1755 
1756 ////////////////////////////////////////
1757 
1758 
1759 template<typename T, Index Log2Dim>
1760 template<typename CombineOp, typename OtherType>
1761 inline void
1762 LeafNode<T, Log2Dim>::combine2(const LeafNode& other, const OtherType& value,
1763  bool valueIsActive, CombineOp& op)
1764 {
1765  if (!this->allocate()) return;
1766 
1768  args.setBRef(value).setBIsActive(valueIsActive);
1769  for (Index i = 0; i < SIZE; ++i) {
1770  op(args.setARef(other.mBuffer[i])
1771  .setAIsActive(other.valueMask().isOn(i))
1772  .setResultRef(mBuffer[i]));
1773  mValueMask.set(i, args.resultIsActive());
1774  }
1775 }
1776 
1777 
1778 template<typename T, Index Log2Dim>
1779 template<typename CombineOp, typename OtherNodeT>
1780 inline void
1781 LeafNode<T, Log2Dim>::combine2(const ValueType& value, const OtherNodeT& other,
1782  bool valueIsActive, CombineOp& op)
1783 {
1784  if (!this->allocate()) return;
1785 
1787  args.setARef(value).setAIsActive(valueIsActive);
1788  for (Index i = 0; i < SIZE; ++i) {
1789  op(args.setBRef(other.mBuffer[i])
1790  .setBIsActive(other.valueMask().isOn(i))
1791  .setResultRef(mBuffer[i]));
1792  mValueMask.set(i, args.resultIsActive());
1793  }
1794 }
1795 
1796 
1797 template<typename T, Index Log2Dim>
1798 template<typename CombineOp, typename OtherNodeT>
1799 inline void
1800 LeafNode<T, Log2Dim>::combine2(const LeafNode& b0, const OtherNodeT& b1, CombineOp& op)
1801 {
1802  if (!this->allocate()) return;
1803 
1805  for (Index i = 0; i < SIZE; ++i) {
1806  mValueMask.set(i, b0.valueMask().isOn(i) || b1.valueMask().isOn(i));
1807  op(args.setARef(b0.mBuffer[i])
1808  .setAIsActive(b0.valueMask().isOn(i))
1809  .setBRef(b1.mBuffer[i])
1810  .setBIsActive(b1.valueMask().isOn(i))
1811  .setResultRef(mBuffer[i]));
1812  mValueMask.set(i, args.resultIsActive());
1813  }
1814 }
1815 
1816 
1817 ////////////////////////////////////////
1818 
1819 
1820 template<typename T, Index Log2Dim>
1821 template<typename BBoxOp>
1822 inline void
1824 {
1825  if (op.template descent<LEVEL>()) {
1826  for (ValueOnCIter i=this->cbeginValueOn(); i; ++i) {
1827  op.template operator()<LEVEL>(CoordBBox::createCube(i.getCoord(), 1));
1828  }
1829  } else {
1830  op.template operator()<LEVEL>(this->getNodeBoundingBox());
1831  }
1832 }
1833 
1834 
1835 template<typename T, Index Log2Dim>
1836 template<typename VisitorOp>
1837 inline void
1839 {
1840  doVisit<LeafNode, VisitorOp, ChildAllIter>(*this, op);
1841 }
1842 
1843 
1844 template<typename T, Index Log2Dim>
1845 template<typename VisitorOp>
1846 inline void
1847 LeafNode<T, Log2Dim>::visit(VisitorOp& op) const
1848 {
1849  doVisit<const LeafNode, VisitorOp, ChildAllCIter>(*this, op);
1850 }
1851 
1852 
1853 template<typename T, Index Log2Dim>
1854 template<typename NodeT, typename VisitorOp, typename ChildAllIterT>
1855 inline void
1856 LeafNode<T, Log2Dim>::doVisit(NodeT& self, VisitorOp& op)
1857 {
1858  for (ChildAllIterT iter = self.beginChildAll(); iter; ++iter) {
1859  op(iter);
1860  }
1861 }
1862 
1863 
1864 ////////////////////////////////////////
1865 
1866 
1867 template<typename T, Index Log2Dim>
1868 template<typename OtherLeafNodeType, typename VisitorOp>
1869 inline void
1870 LeafNode<T, Log2Dim>::visit2Node(OtherLeafNodeType& other, VisitorOp& op)
1871 {
1872  doVisit2Node<LeafNode, OtherLeafNodeType, VisitorOp, ChildAllIter,
1873  typename OtherLeafNodeType::ChildAllIter>(*this, other, op);
1874 }
1875 
1876 
1877 template<typename T, Index Log2Dim>
1878 template<typename OtherLeafNodeType, typename VisitorOp>
1879 inline void
1880 LeafNode<T, Log2Dim>::visit2Node(OtherLeafNodeType& other, VisitorOp& op) const
1881 {
1882  doVisit2Node<const LeafNode, OtherLeafNodeType, VisitorOp, ChildAllCIter,
1883  typename OtherLeafNodeType::ChildAllCIter>(*this, other, op);
1884 }
1885 
1886 
1887 template<typename T, Index Log2Dim>
1888 template<
1889  typename NodeT,
1890  typename OtherNodeT,
1891  typename VisitorOp,
1892  typename ChildAllIterT,
1893  typename OtherChildAllIterT>
1894 inline void
1895 LeafNode<T, Log2Dim>::doVisit2Node(NodeT& self, OtherNodeT& other, VisitorOp& op)
1896 {
1897  // Allow the two nodes to have different ValueTypes, but not different dimensions.
1898  static_assert(OtherNodeT::SIZE == NodeT::SIZE,
1899  "can't visit nodes of different sizes simultaneously");
1900  static_assert(OtherNodeT::LEVEL == NodeT::LEVEL,
1901  "can't visit nodes at different tree levels simultaneously");
1902 
1903  ChildAllIterT iter = self.beginChildAll();
1904  OtherChildAllIterT otherIter = other.beginChildAll();
1905 
1906  for ( ; iter && otherIter; ++iter, ++otherIter) {
1907  op(iter, otherIter);
1908  }
1909 }
1910 
1911 
1912 ////////////////////////////////////////
1913 
1914 
1915 template<typename T, Index Log2Dim>
1916 template<typename IterT, typename VisitorOp>
1917 inline void
1918 LeafNode<T, Log2Dim>::visit2(IterT& otherIter, VisitorOp& op, bool otherIsLHS)
1919 {
1920  doVisit2<LeafNode, VisitorOp, ChildAllIter, IterT>(
1921  *this, otherIter, op, otherIsLHS);
1922 }
1923 
1924 
1925 template<typename T, Index Log2Dim>
1926 template<typename IterT, typename VisitorOp>
1927 inline void
1928 LeafNode<T, Log2Dim>::visit2(IterT& otherIter, VisitorOp& op, bool otherIsLHS) const
1929 {
1930  doVisit2<const LeafNode, VisitorOp, ChildAllCIter, IterT>(
1931  *this, otherIter, op, otherIsLHS);
1932 }
1933 
1934 
1935 template<typename T, Index Log2Dim>
1936 template<
1937  typename NodeT,
1938  typename VisitorOp,
1939  typename ChildAllIterT,
1940  typename OtherChildAllIterT>
1941 inline void
1942 LeafNode<T, Log2Dim>::doVisit2(NodeT& self, OtherChildAllIterT& otherIter,
1943  VisitorOp& op, bool otherIsLHS)
1944 {
1945  if (!otherIter) return;
1946 
1947  if (otherIsLHS) {
1948  for (ChildAllIterT iter = self.beginChildAll(); iter; ++iter) {
1949  op(otherIter, iter);
1950  }
1951  } else {
1952  for (ChildAllIterT iter = self.beginChildAll(); iter; ++iter) {
1953  op(iter, otherIter);
1954  }
1955  }
1956 }
1957 
1958 
1959 ////////////////////////////////////////
1960 
1961 
1962 template<typename T, Index Log2Dim>
1963 inline std::ostream&
1964 operator<<(std::ostream& os, const typename LeafNode<T, Log2Dim>::Buffer& buf)
1965 {
1966  for (Index32 i = 0, N = buf.size(); i < N; ++i) os << buf.mData[i] << ", ";
1967  return os;
1968 }
1969 
1970 } // namespace tree
1971 } // namespace OPENVDB_VERSION_NAME
1972 } // namespace openvdb
1973 
1974 
1975 ////////////////////////////////////////
1976 
1977 
1978 // Specialization for LeafNodes of type bool
1979 #include "LeafNodeBool.h"
1980 
1981 // Specialization for LeafNodes with mask information only
1982 #include "LeafNodeMask.h"
1983 
1984 #endif // OPENVDB_TREE_LEAFNODE_HAS_BEEN_INCLUDED
typename NodeMaskType::OnIterator MaskOnIterator
Definition: LeafNode.h:198
NodeT * stealNode(const Coord &, const ValueType &, bool)
This function exists only to enable template instantiation.
Definition: LeafNode.h:713
void modifyValue(const Coord &xyz, const ModifyOp &op)
Apply a functor to the value of the voxel at the given coordinates and mark the voxel as active...
Definition: LeafNode.h:443
OPENVDB_API SharedPtr< MappedFile > getMappedFilePtr(std::ios_base &)
Return a shared pointer to the memory-mapped file with which the given stream is associated, or a null pointer if the stream is not associated with a memory-mapped file.
Leaf nodes have no children, so their child iterators have no get/set accessors.
Definition: LeafNode.h:242
void stealNodes(ArrayT &, const ValueType &, bool)
This function exists only to enable template instantiation.
Definition: LeafNode.h:719
This struct collects both input and output arguments to "grid combiner" functors used with the tree::...
Definition: Types.h:931
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...
GLenum src
Definition: glew.h:2410
void setValueOff(Index offset)
Mark the voxel at the given offset as inactive but don't change its value.
Definition: LeafNode.h:403
const NodeMaskType & valueMask() const
Definition: LeafNode.h:869
const ValueType & getFirstValue() const
Return a const reference to the first value in the buffer.
Definition: LeafNode.h:619
Index32 countOn() const
Return the total number of on bits.
Definition: NodeMasks.h:443
void readBuffers(std::istream &is, bool fromHalf=false)
Read buffers from a stream.
Definition: LeafNode.h:1322
bool isValueOnAndCache(const Coord &xyz, AccessorT &) const
Return true if the voxel at the given coordinates is active.
Definition: LeafNode.h:537
ChildIter< MaskOffIterator, LeafNode, ChildOff > ChildOffIter
Definition: LeafNode.h:286
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:1121
void readTopology(std::istream &is, bool fromHalf=false)
Read in just the topology.
Definition: LeafNode.h:1286
void setValueAndCache(const Coord &xyz, const ValueType &val, AccessorT &)
Change the value of the voxel at the given coordinates and mark it as active.
Definition: LeafNode.h:542
void setActiveState(Index offset, bool on)
Set the active state of the voxel at the given offset but don't change its value. ...
Definition: LeafNode.h:393
const ValueType & getValue(const Coord &xyz) const
Return the value of the voxel at the given coordinates.
Definition: LeafNode.h:1063
static bool hasActiveTiles()
Return false since leaf nodes never contain tiles.
Definition: LeafNode.h:474
FMT_CONSTEXPR auto begin(const C &c) -> decltype(c.begin())
Definition: format.h:251
const Args & args
Definition: printf.h:628
void setValueOn(const Coord &xyz)
Mark the voxel at the given coordinates as active but don't change its value.
Definition: LeafNode.h:411
T negative(const T &val)
Return the unary negation of the given value.
Definition: Math.h:90
bool isDense() const
Return true if this node contains only active voxels.
Definition: LeafNode.h:150
bool isConstant(ValueType &firstValue, bool &state, const ValueType &tolerance=zeroVal< ValueType >()) const
Definition: LeafNode.h:1477
LeafNode * probeLeafAndCache(const Coord &, AccessorT &)
Return a pointer to this node.
Definition: LeafNode.h:742
ImageBuf OIIO_API fill(cspan< float > values, ROI roi, int nthreads=0)
GLuint const GLfloat * val
Definition: glew.h:2794
void writeCompressedValues(std::ostream &os, ValueT *srcBuf, Index srcCount, const MaskT &valueMask, const MaskT &childMask, bool toHalf)
Definition: Compression.h:645
static void doVisit(NodeT &, VisitorOp &)
Definition: LeafNode.h:1856
void setValueOn(Index offset)
Mark the voxel at the given offset as active but don't change its value.
Definition: LeafNode.h:413
OPENVDB_API uint32_t getDataCompression(std::ios_base &)
Return a bitwise OR of compression option flags (COMPRESS_ZIP, COMPRESS_ACTIVE_MASK, etc.) specifying whether and how input data is compressed or output data should be compressed.
static Index32 childCount()
Return the child count for this node, which is zero.
Definition: LeafNode.h:137
GLenum GLuint coords
Definition: glew.h:7906
void setOff(Index32 n)
Set the nth bit off.
Definition: NodeMasks.h:457
Index pos() const
Identical to offset.
Definition: Iterator.h:60
const LeafNode * probeConstLeafAndCache(const Coord &, AccessorT &) const
Return a pointer to this node.
Definition: LeafNode.h:748
bool getItem(Index pos, void *&child, NonConstValueT &value) const
Definition: LeafNode.h:260
Index64 memUsage() const
Return the memory in bytes occupied by this node.
Definition: LeafNode.h:1441
static Index size()
Return the total number of voxels represented by this LeafNode.
Definition: LeafNode.h:121
ValueIter(const MaskIterT &iter, NodeT *parent)
Definition: LeafNode.h:216
util::NodeMask< Log2Dim > NodeMaskType
Definition: LeafNode.h:44
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:166
SameConfiguration<OtherNodeType>::value is true if and only if OtherNodeType is the type of a LeafNod...
Definition: LeafNode.h:64
bool isValueOn(Index offset) const
Return true if the voxel at the given offset is active.
Definition: LeafNode.h:471
Base class for iterators over internal and leaf nodes.
Definition: Iterator.h:29
ChildIter< MaskOnIterator, const LeafNode, ChildOn > ChildOnCIter
Definition: LeafNode.h:285
const NodeT * probeConstNodeAndCache(const Coord &, AccessorT &) const
Return a pointer to this node.
Definition: LeafNode.h:753
ChildIter< MaskOnIterator, LeafNode, ChildOn > ChildOnIter
Definition: LeafNode.h:284
SharedPtr< MappedFile > Ptr
Definition: io.h:136
const GLdouble * v
Definition: glew.h:1391
void clip(const CoordBBox &, const ValueType &background)
Set all voxels that lie outside the given axis-aligned box to the background.
Definition: LeafNode.h:1139
GLenum GLint GLuint mask
Definition: glew.h:1845
bool empty() const
Return true if memory for this buffer has not yet been allocated.
Definition: LeafBuffer.h:84
void topologyUnion(const LeafNode< OtherType, Log2Dim > &other)
Union this node's set of active values with the active values of the other node, whose ValueType may ...
Definition: LeafNode.h:1681
NodeT & parent() const
Return a reference to the node over which this iterator is iterating.
Definition: Iterator.h:50
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:1689
void read(T &in, bool &v)
Definition: ImfXdr.h:611
LeafBuffer< ValueType, Log2Dim > Buffer
Definition: LeafNode.h:42
bool probeValueAndCache(const Coord &xyz, ValueType &val, AccessorT &) const
Return true if the voxel at the given coordinates is active and return the voxel value in val...
Definition: LeafNode.h:594
bool isEmpty() const
Return true if this node has no active voxels.
Definition: LeafNode.h:148
NodeT * probeNode(const Coord &)
This function exists only to enable template instantiation.
Definition: LeafNode.h:715
Tag dispatch class that distinguishes constructors during file input.
Definition: Types.h:1047
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:1113
void setValuesOn()
Mark all voxels as active but don't change their values.
Definition: LeafNode.h:464
ValueIter< MaskOffIterator, const LeafNode, const ValueType, ValueOff > ValueOffCIter
Definition: LeafNode.h:281
void setValueOffAndCache(const Coord &xyz, const ValueType &value, AccessorT &)
Change the value of the voxel at the given coordinates and mark it as inactive.
Definition: LeafNode.h:576
static Index getLevel()
Return the level of this node, which by definition is zero for LeafNodes.
Definition: LeafNode.h:125
static Coord offsetToLocalCoord(Index n)
Return the local coordinates for a linear table offset, where offset 0 has coordinates (0...
Definition: LeafNode.h:1038
static Index numValues()
Return the total number of voxels represented by this LeafNode.
Definition: LeafNode.h:123
Index32 countOff() const
Return the total number of on bits.
Definition: NodeMasks.h:450
static void evalNodeOrigin(Coord &xyz)
Compute the origin of the leaf node that contains the voxel with the given coordinates.
Definition: LeafNode.h:882
void writeBuffers(std::ostream &os, bool toHalf=false) const
Write buffers to a stream.
Definition: LeafNode.h:1414
ValueIter< MaskDenseIterator, LeafNode, const ValueType, ValueAll > ValueAllIter
Definition: LeafNode.h:282
GLdouble GLdouble z
Definition: glew.h:1559
void denseFill(const CoordBBox &bbox, const ValueType &value, bool active=true)
Set all voxels within an axis-aligned box to the specified value and active state.
Definition: LeafNode.h:482
void readCompressedValues(std::istream &is, ValueT *destBuf, Index destCount, const MaskT &valueMask, bool fromHalf)
Definition: Compression.h:465
void getOrigin(Int32 &x, Int32 &y, Int32 &z) const
Return the grid index coordinates of this node's local origin.
Definition: LeafNode.h:174
std::shared_ptr< T > SharedPtr
Definition: Types.h:91
const LeafNode * probeConstLeaf(const Coord &) const
Return a pointer to this node.
Definition: LeafNode.h:746
bool isValueOn(const Coord &xyz) const
Return true if the voxel at the given coordinates is active.
Definition: LeafNode.h:469
const ValueType & getLastValue() const
Return a const reference to the last value in the buffer.
Definition: LeafNode.h:621
void addLeaf(LeafNode *)
This function exists only to enable template instantiation.
Definition: LeafNode.h:709
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:429
void nodeCount(std::vector< Index32 > &) const
no-op
Definition: LeafNode.h:133
void addLeafAndCache(LeafNode *, AccessorT &)
This function exists only to enable template instantiation.
Definition: LeafNode.h:711
void setOrigin(const Coord &origin)
Set the grid index coordinates of this node's local origin.
Definition: LeafNode.h:169
OffMaskIterator< NodeMask > OffIterator
Definition: NodeMasks.h:349
static Index log2dim()
Return log2 of the dimension of this LeafNode, e.g. 3 if dimensions are 8^3.
Definition: LeafNode.h:117
GLint GLint GLint GLint GLint x
Definition: glew.h:1252
GLint GLint GLint GLint GLint GLint y
Definition: glew.h:1252
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: LeafNode.h:560
void setActiveStateAndCache(const Coord &xyz, bool on, AccessorT &)
Set the active state of the voxel at the given coordinates without changing its value.
Definition: LeafNode.h:585
NodeT * probeNodeAndCache(const Coord &, AccessorT &)
Return a pointer to this node.
Definition: LeafNode.h:733
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:275
*But if you need a or simply need to know when the task has note that the like this
Definition: thread.h:639
void addTile(Index level, const Coord &, const ValueType &, bool)
Definition: LeafNode.h:1582
Index64 onVoxelCount() const
Return the number of voxels marked On.
Definition: LeafNode.h:140
GLint GLenum GLsizei GLint GLsizei const void * data
Definition: glew.h:1379
Templated block class to hold specific data types and a fixed number of values determined by Log2Dim...
Definition: LeafNode.h:37
LeafNode * touchLeaf(const Coord &)
Return a pointer to this node.
Definition: LeafNode.h:729
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:1698
bool allocate()
Allocate memory for this buffer if it has not already been allocated.
Definition: LeafBuffer.h:86
ValueIter< MaskOnIterator, LeafNode, const ValueType, ValueOn > ValueOnIter
Definition: LeafNode.h:278
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:1222
void modifyItem(Index n, const ModifyOp &op) const
Definition: LeafNode.h:234
DenseIter(const MaskDenseIterator &iter, NodeT *parent)
Definition: LeafNode.h:258
GLsizei n
Definition: glew.h:4040
GLenum GLenum dst
Definition: glew.h:2410
const NodeMaskType & getValueMask() const
Definition: LeafNode.h:867
bool operator!=(const LeafNode &other) const
Definition: LeafNode.h:195
std::string str() const
Return a string representation of this node.
Definition: LeafNode.h:1015
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:1611
ValueType medianAll(ValueType *tmp=nullptr) const
Computes the median value of all the active AND inactive voxels in this node.
Definition: LeafNode.h:1513
static Index getChildDim()
Return the dimension of child nodes of this LeafNode, which is one for voxels.
Definition: LeafNode.h:129
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:401
typename BaseT::NonConstValueType NonConstValueT
Definition: LeafNode.h:255
Index64 offVoxelCount() const
Return the number of voxels marked Off.
Definition: LeafNode.h:142
typename NodeMaskType::DenseIterator MaskDenseIterator
Definition: LeafNode.h:200
static Index getValueLevelAndCache(const Coord &, AccessorT &)
Return the LEVEL (=0) at which leaf node values reside.
Definition: LeafNode.h:614
static Index getValueLevel(const Coord &)
Return the level (i.e., 0) at which leaf node values reside.
Definition: LeafNode.h:388
const value_t * data() const
Return a pointer to the underlying scalar type.
Definition: simd.h:947
const Coord & origin() const
Return the grid index coordinates of this node's local origin.
Definition: LeafNode.h:172
void modifyValueAndActiveState(const Coord &xyz, const ModifyOp &op)
Apply a functor to the voxel at the given coordinates.
Definition: LeafNode.h:450
void getOrigin(Coord &origin) const
Return the grid index coordinates of this node's local origin.
Definition: LeafNode.h:173
#define OPENVDB_NO_UNREACHABLE_CODE_WARNING_BEGIN
SIMD Intrinsic Headers.
Definition: Platform.h:114
static Index dim()
Return the number of voxels in each coordinate dimension.
Definition: LeafNode.h:119
bool isApproxEqual(const Type &a, const Type &b)
Return true if a is equal to b to within the default floating-point comparison tolerance.
Definition: Math.h:371
static Index32 leafCount()
Return the leaf count for this node, which is one.
Definition: LeafNode.h:131
Coord offsetToGlobalCoord(Index n) const
Return the global coordinates for a linear table offset.
Definition: LeafNode.h:1052
Base class for sparse iterators over internal and leaf nodes.
Definition: Iterator.h:114
void combine2(const LeafNode &other, const OtherType &, bool valueIsActive, CombineOp &)
Definition: LeafNode.h:1762
Index medianOn(ValueType &value, ValueType *tmp=nullptr) const
Computes the median value of all the active voxels in this node.
Definition: LeafNode.h:1531
void setValueOnlyAndCache(const Coord &xyz, const ValueType &val, AccessorT &)
Change the value of the voxel at the given coordinates but preserve its state.
Definition: LeafNode.h:551
CoordBBox getNodeBoundingBox() const
Return the bounding box of this node, i.e., the full index space spanned by this leaf node...
Definition: LeafNode.h:166
void visit2Node(OtherLeafNodeType &other, VisitorOp &)
Definition: LeafNode.h:1870
Base class for dense iterators over internal and leaf nodes.
Definition: Iterator.h:178
GLsizei const GLchar *const * string
Definition: glew.h:1844
void modifyValueAndActiveStateAndCache(const Coord &xyz, const ModifyOp &op, AccessorT &)
Definition: LeafNode.h:568
Allocator::value_type * allocate(Allocator &alloc, std::size_t n)
Definition: format.h:282
void prune(const ValueType &=zeroVal< ValueType >())
This function exists only to enable template instantiation.
Definition: LeafNode.h:708
IMATH_INTERNAL_NAMESPACE_HEADER_ENTER T clip(const T &p, const Box< T > &box)
Definition: ImathBoxAlgo.h:89
DenseMaskIterator< NodeMask > DenseIterator
Definition: NodeMasks.h:350
ValueIter< MaskOnIterator, const LeafNode, const ValueType, ValueOn > ValueOnCIter
Definition: LeafNode.h:279
void setItem(Index pos, const ValueT &value) const
Definition: LeafNode.h:222
const ValueType & getValueAndCache(const Coord &xyz, AccessorT &) const
Return the value of the voxel at the given coordinates.
Definition: LeafNode.h:529
bool probeValue(const Coord &xyz, ValueType &val) const
Return true if the voxel at the given coordinates is active.
Definition: LeafNode.h:1079
CombineArgs & setBRef(const BValueType &b)
Redirect the B value to a new external source.
Definition: Types.h:986
void combine(const LeafNode &other, CombineOp &op)
Definition: LeafNode.h:1722
void setValueOn(const Coord &xyz, const ValueType &val)
Set the value of the voxel at the given coordinates and mark the voxel as active. ...
Definition: LeafNode.h:415
Index medianOff(ValueType &value, ValueType *tmp=nullptr) const
Computes the median value of all the inactive voxels in this node.
Definition: LeafNode.h:1555
void addTileAndCache(Index, const Coord &, const ValueType &, bool, AccessorT &)
Definition: LeafNode.h:1599
bool isOutOfCore() const
Return true if this buffer's values have not yet been read from disk.
Definition: LeafBuffer.h:82
GLuint GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat s1
Definition: glew.h:12681
GLuint GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat t1
Definition: glew.h:12681
ValueIter< MaskDenseIterator, const LeafNode, const ValueType, ValueAll > ValueAllCIter
Definition: LeafNode.h:283
OnMaskIterator< NodeMask > OnIterator
Definition: NodeMasks.h:348
const NodeT * probeConstNode(const Coord &) const
This function exists only to enable template instantiation.
Definition: LeafNode.h:717
GLuint GLuint GLsizei count
Definition: glew.h:1253
const LeafNode * probeLeafAndCache(const Coord &, AccessorT &) const
Return a pointer to this node.
Definition: LeafNode.h:750
const ValueType & getValue(const Coord &xyz, bool &state, int &level, AccessorT &) const
Return the value of the voxel at the given coordinates and return its active state and level (i...
Definition: LeafNode.h:603
void setValueMask(const NodeMaskType &mask)
Definition: LeafNode.h:870
GA_API const UT_StringHolder N
void skipCompressedValues(bool seekable, std::istream &, bool fromHalf)
Definition: LeafNode.h:1306
bool isInactive() const
Return true if all of this node's values are inactive.
Definition: LeafNode.h:838
static void getNodeLog2Dims(std::vector< Index > &dims)
Append the Log2Dim of this LeafNode to the specified vector.
Definition: LeafNode.h:127
void setValueOn(Index offset, const ValueType &val)
Set the value of the voxel at the given offset and mark the voxel as active.
Definition: LeafNode.h:421
ValueConverter<T>::Type is the type of a LeafNode having the same dimensions as this node but a diffe...
Definition: LeafNode.h:59
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:984
OPENVDB_API SharedPtr< StreamMetadata > getStreamMetadataPtr(std::ios_base &)
Return a shared pointer to an object that stores metadata (file format, compression scheme...
void setValue(const Coord &xyz, const ValueType &val)
Set the value of the voxel at the given coordinates and mark the voxel as active. ...
Definition: LeafNode.h:419
void unsetItem(Index pos, const ValueT &value) const
Definition: LeafNode.h:271
#define SIZE
Definition: simple.C:40
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:1249
#define OPENVDB_NO_UNREACHABLE_CODE_WARNING_END
Definition: Platform.h:115
size_t streamingSize(bool toHalf=false) const
void visit2(IterT &otherIter, VisitorOp &, bool otherIsLHS=false)
Definition: LeafNode.h:1918
Tag dispatch class that distinguishes topology copy constructors from deep copy constructors.
Definition: Types.h:1045
bool isAllocated() const
Return true if memory for this node's buffer has been allocated.
Definition: LeafNode.h:152
GLuint GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat s0
Definition: glew.h:12681
void visitActiveBBox(BBoxOp &) const
Calls the templated functor BBoxOp with bounding box information. An additional level argument is pro...
Definition: LeafNode.h:1823
void setValuesOff()
Mark all voxels as inactive but don't change their values.
Definition: LeafNode.h:466
GLuint GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat t0
Definition: glew.h:12681
void voxelizeActiveTiles(bool=true)
No-op.
Definition: LeafNode.h:631
#define const
Definition: zconf.h:214
ChildIter(const MaskIterT &iter, NodeT *parent)
Definition: LeafNode.h:246
void setValue(Index i, const ValueType &)
Set the i'th value of this buffer to the specified value.
Definition: LeafBuffer.h:212
DenseIter< const LeafNode, const ValueType, ChildAll > ChildAllCIter
Definition: LeafNode.h:289
vint4 min(const vint4 &a, const vint4 &b)
Definition: simd.h:4694
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:1469
bool operator==(const LeafNode &other) const
Check for buffer, state and origin equivalence.
Definition: LeafNode.h:1431
GLenum GLuint GLsizei const GLchar * buf
Definition: glew.h:2580
typename NodeMaskType::OffIterator MaskOffIterator
Definition: LeafNode.h:199
bool allocate()
Allocate memory for this node's buffer if it has not already been allocated.
Definition: LeafNode.h:154
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h:112
GLsizei const GLfloat * value
Definition: glew.h:1849
void evalActiveBoundingBox(CoordBBox &bbox, bool visitVoxels=true) const
Definition: LeafNode.h:1451
static Index coordToOffset(const Coord &xyz)
Return the linear table offset of the given global or local coordinates.
Definition: LeafNode.h:1028
void getNodes(ArrayT &) const
This function exists only to enable template instantiation.
Definition: LeafNode.h:718
OPENVDB_API uint32_t getFormatVersion(std::ios_base &)
Return the file format version number associated with the given input stream.
GLint level
Definition: glew.h:1252
static void doVisit2(NodeT &self, OtherChildAllIterT &, VisitorOp &, bool otherIsLHS)
Definition: LeafNode.h:1942
LeafNode * probeLeaf(const Coord &)
Return a pointer to this node.
Definition: LeafNode.h:740
ChildIter< MaskOffIterator, const LeafNode, ChildOff > ChildOffCIter
Definition: LeafNode.h:287
const LeafNode * probeLeaf(const Coord &) const
Return a pointer to this node.
Definition: LeafNode.h:751
void writeTopology(std::ostream &os, bool toHalf=false) const
Write out just the topology.
Definition: LeafNode.h:1294
bool isOff(Index32 n) const
Return true if the nth bit is off.
Definition: NodeMasks.h:508
static Index32 nonLeafCount()
Return the non-leaf count for this node, which is zero.
Definition: LeafNode.h:135
DenseIter< LeafNode, ValueType, ChildAll > ChildAllIter
Definition: LeafNode.h:288
ValueIter< MaskOffIterator, LeafNode, const ValueType, ValueOff > ValueOffIter
Definition: LeafNode.h:280
void swap(Buffer &other)
Exchange this node's data buffer with the given data buffer without changing the active states of the...
Definition: LeafNode.h:338
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:1179
static void doVisit2Node(NodeT &self, OtherNodeT &other, VisitorOp &)
Definition: LeafNode.h:1895
GLintptr offset
Definition: glew.h:1682
LeafNode * touchLeafAndCache(const Coord &, AccessorT &)
Return a pointer to this node.
Definition: LeafNode.h:731