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