HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Tree.h
Go to the documentation of this file.
1 // Copyright Contributors to the OpenVDB Project
2 // SPDX-License-Identifier: MPL-2.0
3 
4 /// @file tree/Tree.h
5 
6 #ifndef OPENVDB_TREE_TREE_HAS_BEEN_INCLUDED
7 #define OPENVDB_TREE_TREE_HAS_BEEN_INCLUDED
8 
9 #include <openvdb/Types.h>
10 #include <openvdb/Metadata.h>
11 #include <openvdb/math/Math.h>
12 #include <openvdb/math/BBox.h>
13 #include <openvdb/util/Formats.h>
14 #include <openvdb/util/logging.h>
15 #include <openvdb/Platform.h>
16 #include "RootNode.h"
17 #include "InternalNode.h"
18 #include "LeafNode.h"
19 #include "TreeIterator.h"
20 #include "ValueAccessor.h"
21 #include <tbb/concurrent_hash_map.h>
22 #include <cstdint>
23 #include <iostream>
24 #include <mutex>
25 #include <sstream>
26 #include <vector>
27 
28 
29 namespace openvdb {
31 namespace OPENVDB_VERSION_NAME {
32 namespace tree {
33 
34 /// @brief Base class for typed trees
36 {
37 public:
40 
41  TreeBase() = default;
42  TreeBase(const TreeBase&) = default;
43  TreeBase& operator=(const TreeBase&) = delete; // disallow assignment
44  virtual ~TreeBase() = default;
45 
46  /// Return the name of this tree's type.
47  virtual const Name& type() const = 0;
48 
49  /// Return the name of the type of a voxel's value (e.g., "float" or "vec3d").
50  virtual Name valueType() const = 0;
51 
52  /// Return a pointer to a deep copy of this tree
53  virtual TreeBase::Ptr copy() const = 0;
54 
55  //
56  // Tree methods
57  //
58  /// @brief Return this tree's background value wrapped as metadata.
59  /// @note Query the metadata object for the value's type.
60  virtual Metadata::Ptr getBackgroundValue() const { return Metadata::Ptr(); }
61 
62  /// @brief Return in @a bbox the axis-aligned bounding box of all
63  /// active tiles and leaf nodes with active values.
64  /// @details This is faster than calling evalActiveVoxelBoundingBox,
65  /// which visits the individual active voxels, and hence
66  /// evalLeafBoundingBox produces a less tight, i.e. approximate, bbox.
67  /// @return @c false if the bounding box is empty (in which case
68  /// the bbox is set to its default value).
69  virtual bool evalLeafBoundingBox(CoordBBox& bbox) const = 0;
70 
71  /// @brief Return in @a dim the dimensions of the axis-aligned bounding box
72  /// of all leaf nodes.
73  /// @return @c false if the bounding box is empty.
74  virtual bool evalLeafDim(Coord& dim) const = 0;
75 
76  /// @brief Return in @a bbox the axis-aligned bounding box of all
77  /// active voxels and tiles.
78  /// @details This method produces a more accurate, i.e. tighter,
79  /// bounding box than evalLeafBoundingBox which is approximate but
80  /// faster.
81  /// @return @c false if the bounding box is empty (in which case
82  /// the bbox is set to its default value).
83  virtual bool evalActiveVoxelBoundingBox(CoordBBox& bbox) const = 0;
84 
85  /// @brief Return in @a dim the dimensions of the axis-aligned bounding box of all
86  /// active voxels. This is a tighter bounding box than the leaf node bounding box.
87  /// @return @c false if the bounding box is empty.
88  virtual bool evalActiveVoxelDim(Coord& dim) const = 0;
89 
90  virtual void getIndexRange(CoordBBox& bbox) const = 0;
91 
92  /// @brief Replace with background tiles any nodes whose voxel buffers
93  /// have not yet been allocated.
94  /// @details Typically, unallocated nodes are leaf nodes whose voxel buffers
95  /// are not yet resident in memory because delayed loading is in effect.
96  /// @sa readNonresidentBuffers, io::File::open
97  virtual void clipUnallocatedNodes() = 0;
98  /// Return the total number of unallocated leaf nodes residing in this tree.
99  virtual Index32 unallocatedLeafCount() const = 0;
100 
101 
102  //
103  // Statistics
104  //
105  /// @brief Return the depth of this tree.
106  ///
107  /// A tree with only a root node and leaf nodes has depth 2, for example.
108  virtual Index treeDepth() const = 0;
109  /// Return the number of leaf nodes.
110  virtual Index32 leafCount() const = 0;
111 #if OPENVDB_ABI_VERSION_NUMBER >= 7
112  /// Return a vector with node counts. The number of nodes of type NodeType
113  /// is given as element NodeType::LEVEL in the return vector. Thus, the size
114  /// of this vector corresponds to the height (or depth) of this tree.
115  virtual std::vector<Index32> nodeCount() const = 0;
116 #endif
117  /// Return the number of non-leaf nodes.
118  virtual Index32 nonLeafCount() const = 0;
119  /// Return the number of active voxels stored in leaf nodes.
120  virtual Index64 activeLeafVoxelCount() const = 0;
121  /// Return the number of inactive voxels stored in leaf nodes.
122  virtual Index64 inactiveLeafVoxelCount() const = 0;
123  /// Return the total number of active voxels.
124  virtual Index64 activeVoxelCount() const = 0;
125  /// Return the number of inactive voxels within the bounding box of all active voxels.
126  virtual Index64 inactiveVoxelCount() const = 0;
127  /// Return the total number of active tiles.
128  virtual Index64 activeTileCount() const = 0;
129 
130  /// Return the total amount of memory in bytes occupied by this tree.
131  virtual Index64 memUsage() const { return 0; }
132 
133 
134  //
135  // I/O methods
136  //
137  /// @brief Read the tree topology from a stream.
138  ///
139  /// This will read the tree structure and tile values, but not voxel data.
140  virtual void readTopology(std::istream&, bool saveFloatAsHalf = false);
141  /// @brief Write the tree topology to a stream.
142  ///
143  /// This will write the tree structure and tile values, but not voxel data.
144  virtual void writeTopology(std::ostream&, bool saveFloatAsHalf = false) const;
145 
146  /// Read all data buffers for this tree.
147  virtual void readBuffers(std::istream&, bool saveFloatAsHalf = false) = 0;
148  /// Read all of this tree's data buffers that intersect the given bounding box.
149  virtual void readBuffers(std::istream&, const CoordBBox&, bool saveFloatAsHalf = false) = 0;
150  /// @brief Read all of this tree's data buffers that are not yet resident in memory
151  /// (because delayed loading is in effect).
152  /// @details If this tree was read from a memory-mapped file, this operation
153  /// disconnects the tree from the file.
154  /// @sa clipUnallocatedNodes, io::File::open, io::MappedFile
155  virtual void readNonresidentBuffers() const = 0;
156  /// Write out all the data buffers for this tree.
157  virtual void writeBuffers(std::ostream&, bool saveFloatAsHalf = false) const = 0;
158 
159  /// @brief Print statistics, memory usage and other information about this tree.
160  /// @param os a stream to which to write textual information
161  /// @param verboseLevel 1: print tree configuration only;
162  /// 2: include node and voxel statistics;
163  /// 3: include memory usage;
164  /// 4: include minimum and maximum voxel values
165  /// @warning @a verboseLevel 4 forces loading of any unallocated nodes.
166  virtual void print(std::ostream& os = std::cout, int verboseLevel = 1) const;
167 };
168 
169 
170 ////////////////////////////////////////
171 
172 
173 template<typename _RootNodeType>
174 class Tree: public TreeBase
175 {
176 public:
179 
180  using RootNodeType = _RootNodeType;
181  using ValueType = typename RootNodeType::ValueType;
182  using BuildType = typename RootNodeType::BuildType;
183  using LeafNodeType = typename RootNodeType::LeafNodeType;
184 
185  static const Index DEPTH = RootNodeType::LEVEL + 1;
186 
187  /// @brief ValueConverter<T>::Type is the type of a tree having the same
188  /// hierarchy as this tree but a different value type, T.
189  ///
190  /// For example, FloatTree::ValueConverter<double>::Type is equivalent to DoubleTree.
191  /// @note If the source tree type is a template argument, it might be necessary
192  /// to write "typename SourceTree::template ValueConverter<T>::Type".
193  template<typename OtherValueType>
194  struct ValueConverter {
196  };
197 
198 
199  Tree() {}
200 
201  Tree& operator=(const Tree&) = delete; // disallow assignment
202 
203  /// Deep copy constructor
204  Tree(const Tree& other): TreeBase(other), mRoot(other.mRoot)
205  {
206  }
207 
208  /// @brief Value conversion deep copy constructor
209  ///
210  /// Deep copy a tree of the same configuration as this tree type but a different
211  /// ValueType, casting the other tree's values to this tree's ValueType.
212  /// @throw TypeError if the other tree's configuration doesn't match this tree's
213  /// or if this tree's ValueType is not constructible from the other tree's ValueType.
214  template<typename OtherRootType>
215  explicit Tree(const Tree<OtherRootType>& other): TreeBase(other), mRoot(other.root())
216  {
217  }
218 
219  /// @brief Topology copy constructor from a tree of a different type
220  ///
221  /// Copy the structure, i.e., the active states of tiles and voxels, of another
222  /// tree of a possibly different type, but don't copy any tile or voxel values.
223  /// Instead, initialize tiles and voxels with the given active and inactive values.
224  /// @param other a tree having (possibly) a different ValueType
225  /// @param inactiveValue background value for this tree, and the value to which
226  /// all inactive tiles and voxels are initialized
227  /// @param activeValue value to which active tiles and voxels are initialized
228  /// @throw TypeError if the other tree's configuration doesn't match this tree's.
229  template<typename OtherTreeType>
230  Tree(const OtherTreeType& other,
231  const ValueType& inactiveValue,
232  const ValueType& activeValue,
233  TopologyCopy):
234  TreeBase(other),
235  mRoot(other.root(), inactiveValue, activeValue, TopologyCopy())
236  {
237  }
238 
239  /// @brief Topology copy constructor from a tree of a different type
240  ///
241  /// @note This topology copy constructor is generally faster than
242  /// the one that takes both a foreground and a background value.
243  ///
244  /// Copy the structure, i.e., the active states of tiles and voxels, of another
245  /// tree of a possibly different type, but don't copy any tile or voxel values.
246  /// Instead, initialize tiles and voxels with the given background value.
247  /// @param other a tree having (possibly) a different ValueType
248  /// @param background the value to which tiles and voxels are initialized
249  /// @throw TypeError if the other tree's configuration doesn't match this tree's.
250  template<typename OtherTreeType>
251  Tree(const OtherTreeType& other, const ValueType& background, TopologyCopy):
252  TreeBase(other),
253  mRoot(other.root(), background, TopologyCopy())
254  {
255  }
256 
257  /// Empty tree constructor
258  Tree(const ValueType& background): mRoot(background) {}
259 
260  ~Tree() override { this->clear(); releaseAllAccessors(); }
261 
262  /// Return a pointer to a deep copy of this tree
263  TreeBase::Ptr copy() const override { return TreeBase::Ptr(new Tree(*this)); }
264 
265  /// Return the name of the type of a voxel's value (e.g., "float" or "vec3d")
266  Name valueType() const override { return typeNameAsString<ValueType>(); }
267 
268  /// Return the name of this type of tree.
269  static const Name& treeType();
270  /// Return the name of this type of tree.
271  const Name& type() const override { return this->treeType(); }
272 
273  bool operator==(const Tree&) const { OPENVDB_THROW(NotImplementedError, ""); }
274  bool operator!=(const Tree&) const { OPENVDB_THROW(NotImplementedError, ""); }
275 
276  //@{
277  /// Return this tree's root node.
278  RootNodeType& root() { return mRoot; }
279  const RootNodeType& root() const { return mRoot; }
280  //@}
281 
282 
283  //
284  // Tree methods
285  //
286  /// @brief Return @c true if the given tree has the same node and active value
287  /// topology as this tree, whether or not it has the same @c ValueType.
288  template<typename OtherRootNodeType>
289  bool hasSameTopology(const Tree<OtherRootNodeType>& other) const;
290 
291  bool evalLeafBoundingBox(CoordBBox& bbox) const override;
292  bool evalActiveVoxelBoundingBox(CoordBBox& bbox) const override;
293  bool evalActiveVoxelDim(Coord& dim) const override;
294  bool evalLeafDim(Coord& dim) const override;
295 
296  /// @brief Traverse the type hierarchy of nodes, and return, in @a dims, a list
297  /// of the Log2Dims of nodes in order from RootNode to LeafNode.
298  /// @note Because RootNodes are resizable, the RootNode Log2Dim is 0 for all trees.
299  static void getNodeLog2Dims(std::vector<Index>& dims);
300 
301 
302  //
303  // I/O methods
304  //
305  /// @brief Read the tree topology from a stream.
306  ///
307  /// This will read the tree structure and tile values, but not voxel data.
308  void readTopology(std::istream&, bool saveFloatAsHalf = false) override;
309  /// @brief Write the tree topology to a stream.
310  ///
311  /// This will write the tree structure and tile values, but not voxel data.
312  void writeTopology(std::ostream&, bool saveFloatAsHalf = false) const override;
313  /// Read all data buffers for this tree.
314  void readBuffers(std::istream&, bool saveFloatAsHalf = false) override;
315  /// Read all of this tree's data buffers that intersect the given bounding box.
316  void readBuffers(std::istream&, const CoordBBox&, bool saveFloatAsHalf = false) override;
317  /// @brief Read all of this tree's data buffers that are not yet resident in memory
318  /// (because delayed loading is in effect).
319  /// @details If this tree was read from a memory-mapped file, this operation
320  /// disconnects the tree from the file.
321  /// @sa clipUnallocatedNodes, io::File::open, io::MappedFile
322  void readNonresidentBuffers() const override;
323  /// Write out all data buffers for this tree.
324  void writeBuffers(std::ostream&, bool saveFloatAsHalf = false) const override;
325 
326  void print(std::ostream& os = std::cout, int verboseLevel = 1) const override;
327 
328 
329  //
330  // Statistics
331  //
332  /// @brief Return the depth of this tree.
333  ///
334  /// A tree with only a root node and leaf nodes has depth 2, for example.
335  Index treeDepth() const override { return DEPTH; }
336  /// Return the number of leaf nodes.
337  Index32 leafCount() const override { return mRoot.leafCount(); }
338 #if OPENVDB_ABI_VERSION_NUMBER >= 7
339  /// Return a vector with node counts. The number of nodes of type NodeType
340  /// is given as element NodeType::LEVEL in the return vector. Thus, the size
341  /// of this vector corresponds to the height (or depth) of this tree.
342  std::vector<Index32> nodeCount() const override
343  {
344  std::vector<Index32> vec(DEPTH, 0);
345  mRoot.nodeCount( vec );
346  return vec;// Named Return Value Optimization
347  }
348 #endif
349  /// Return the number of non-leaf nodes.
350  Index32 nonLeafCount() const override { return mRoot.nonLeafCount(); }
351  /// Return the number of active voxels stored in leaf nodes.
352  Index64 activeLeafVoxelCount() const override { return mRoot.onLeafVoxelCount(); }
353  /// Return the number of inactive voxels stored in leaf nodes.
354  Index64 inactiveLeafVoxelCount() const override { return mRoot.offLeafVoxelCount(); }
355  /// Return the total number of active voxels.
356  Index64 activeVoxelCount() const override { return mRoot.onVoxelCount(); }
357  /// Return the number of inactive voxels within the bounding box of all active voxels.
358  Index64 inactiveVoxelCount() const override;
359  /// Return the total number of active tiles.
360  Index64 activeTileCount() const override { return mRoot.onTileCount(); }
361 
362  /// Return the minimum and maximum active values in this tree.
363  void evalMinMax(ValueType &min, ValueType &max) const;
364 
365  Index64 memUsage() const override { return sizeof(*this) + mRoot.memUsage(); }
366 
367 
368  //
369  // Voxel access methods (using signed indexing)
370  //
371  /// Return the value of the voxel at the given coordinates.
372  const ValueType& getValue(const Coord& xyz) const;
373  /// @brief Return the value of the voxel at the given coordinates
374  /// and update the given accessor's node cache.
375  template<typename AccessT> const ValueType& getValue(const Coord& xyz, AccessT&) const;
376 
377  /// @brief Return the tree depth (0 = root) at which the value of voxel (x, y, z) resides.
378  /// @details If (x, y, z) isn't explicitly represented in the tree (i.e., it is
379  /// implicitly a background voxel), return -1.
380  int getValueDepth(const Coord& xyz) const;
381 
382  /// Set the active state of the voxel at the given coordinates but don't change its value.
383  void setActiveState(const Coord& xyz, bool on);
384  /// Set the value of the voxel at the given coordinates but don't change its active state.
385  void setValueOnly(const Coord& xyz, const ValueType& value);
386  /// Mark the voxel at the given coordinates as active but don't change its value.
387  void setValueOn(const Coord& xyz);
388  /// Set the value of the voxel at the given coordinates and mark the voxel as active.
389  void setValueOn(const Coord& xyz, const ValueType& value);
390  /// Set the value of the voxel at the given coordinates and mark the voxel as active.
391  void setValue(const Coord& xyz, const ValueType& value);
392  /// @brief Set the value of the voxel at the given coordinates, mark the voxel as active,
393  /// and update the given accessor's node cache.
394  template<typename AccessT> void setValue(const Coord& xyz, const ValueType& value, AccessT&);
395  /// Mark the voxel at the given coordinates as inactive but don't change its value.
396  void setValueOff(const Coord& xyz);
397  /// Set the value of the voxel at the given coordinates and mark the voxel as inactive.
398  void setValueOff(const Coord& xyz, const ValueType& value);
399 
400  /// @brief Apply a functor to the value of the voxel at the given coordinates
401  /// and mark the voxel as active.
402  /// @details Provided that the functor can be inlined, this is typically
403  /// significantly faster than calling getValue() followed by setValueOn().
404  /// @param xyz the coordinates of a voxel whose value is to be modified
405  /// @param op a functor of the form <tt>void op(ValueType&) const</tt> that modifies
406  /// its argument in place
407  /// @par Example:
408  /// @code
409  /// Coord xyz(1, 0, -2);
410  /// // Multiply the value of a voxel by a constant and mark the voxel as active.
411  /// floatTree.modifyValue(xyz, [](float& f) { f *= 0.25; }); // C++11
412  /// // Set the value of a voxel to the maximum of its current value and 0.25,
413  /// // and mark the voxel as active.
414  /// floatTree.modifyValue(xyz, [](float& f) { f = std::max(f, 0.25f); }); // C++11
415  /// @endcode
416  /// @note The functor is not guaranteed to be called only once.
417  /// @see tools::foreach()
418  template<typename ModifyOp>
419  void modifyValue(const Coord& xyz, const ModifyOp& op);
420 
421  /// @brief Apply a functor to the voxel at the given coordinates.
422  /// @details Provided that the functor can be inlined, this is typically
423  /// significantly faster than calling getValue() followed by setValue().
424  /// @param xyz the coordinates of a voxel to be modified
425  /// @param op a functor of the form <tt>void op(ValueType&, bool&) const</tt> that
426  /// modifies its arguments, a voxel's value and active state, in place
427  /// @par Example:
428  /// @code
429  /// Coord xyz(1, 0, -2);
430  /// // Multiply the value of a voxel by a constant and mark the voxel as inactive.
431  /// floatTree.modifyValueAndActiveState(xyz,
432  /// [](float& f, bool& b) { f *= 0.25; b = false; }); // C++11
433  /// // Set the value of a voxel to the maximum of its current value and 0.25,
434  /// // but don't change the voxel's active state.
435  /// floatTree.modifyValueAndActiveState(xyz,
436  /// [](float& f, bool&) { f = std::max(f, 0.25f); }); // C++11
437  /// @endcode
438  /// @note The functor is not guaranteed to be called only once.
439  /// @see tools::foreach()
440  template<typename ModifyOp>
441  void modifyValueAndActiveState(const Coord& xyz, const ModifyOp& op);
442 
443  /// @brief Get the value of the voxel at the given coordinates.
444  /// @return @c true if the value is active.
445  bool probeValue(const Coord& xyz, ValueType& value) const;
446 
447  /// Return @c true if the value at the given coordinates is active.
448  bool isValueOn(const Coord& xyz) const { return mRoot.isValueOn(xyz); }
449  /// Return @c true if the value at the given coordinates is inactive.
450  bool isValueOff(const Coord& xyz) const { return !this->isValueOn(xyz); }
451  /// Return @c true if this tree has any active tiles.
452  bool hasActiveTiles() const { return mRoot.hasActiveTiles(); }
453 
454  /// Set all voxels that lie outside the given axis-aligned box to the background.
455  void clip(const CoordBBox&);
456  /// @brief Replace with background tiles any nodes whose voxel buffers
457  /// have not yet been allocated.
458  /// @details Typically, unallocated nodes are leaf nodes whose voxel buffers
459  /// are not yet resident in memory because delayed loading is in effect.
460  /// @sa readNonresidentBuffers, io::File::open
461  void clipUnallocatedNodes() override;
462 
463  /// Return the total number of unallocated leaf nodes residing in this tree.
464  Index32 unallocatedLeafCount() const override;
465 
466  //@{
467  /// @brief Set all voxels within a given axis-aligned box to a constant value.
468  /// @param bbox inclusive coordinates of opposite corners of an axis-aligned box
469  /// @param value the value to which to set voxels within the box
470  /// @param active if true, mark voxels within the box as active,
471  /// otherwise mark them as inactive
472  /// @note This operation generates a sparse, but not always optimally sparse,
473  /// representation of the filled box. Follow fill operations with a prune()
474  /// operation for optimal sparseness.
475  void sparseFill(const CoordBBox& bbox, const ValueType& value, bool active = true);
476  void fill(const CoordBBox& bbox, const ValueType& value, bool active = true)
477  {
478  this->sparseFill(bbox, value, active);
479  }
480  //@}
481 
482  /// @brief Set all voxels within a given axis-aligned box to a constant value
483  /// and ensure that those voxels are all represented at the leaf level.
484  /// @param bbox inclusive coordinates of opposite corners of an axis-aligned box.
485  /// @param value the value to which to set voxels within the box.
486  /// @param active if true, mark voxels within the box as active,
487  /// otherwise mark them as inactive.
488  /// @sa voxelizeActiveTiles()
489  void denseFill(const CoordBBox& bbox, const ValueType& value, bool active = true);
490 
491  /// @brief Densify active tiles, i.e., replace them with leaf-level active voxels.
492  ///
493  /// @param threaded if true, this operation is multi-threaded (over the internal nodes).
494  ///
495  /// @warning This method can explode the tree's memory footprint, especially if it
496  /// contains active tiles at the upper levels (in particular the root level)!
497  ///
498  /// @sa denseFill()
499  void voxelizeActiveTiles(bool threaded = true);
500 
501  /// @brief Reduce the memory footprint of this tree by replacing with tiles
502  /// any nodes whose values are all the same (optionally to within a tolerance)
503  /// and have the same active state.
504  /// @warning Will soon be deprecated!
505  void prune(const ValueType& tolerance = zeroVal<ValueType>())
506  {
507  this->clearAllAccessors();
508  mRoot.prune(tolerance);
509  }
510 
511  /// @brief Add the given leaf node to this tree, creating a new branch if necessary.
512  /// If a leaf node with the same origin already exists, replace it.
513  ///
514  /// @warning Ownership of the leaf is transferred to the tree so
515  /// the client code should not attempt to delete the leaf pointer!
516  void addLeaf(LeafNodeType* leaf) { assert(leaf); mRoot.addLeaf(leaf); }
517 
518  /// @brief Add a tile containing voxel (x, y, z) at the specified tree level,
519  /// creating a new branch if necessary. Delete any existing lower-level nodes
520  /// that contain (x, y, z).
521  /// @note @a level must be less than this tree's depth.
522  void addTile(Index level, const Coord& xyz, const ValueType& value, bool active);
523 
524  /// @brief Return a pointer to the node of type @c NodeT that contains voxel (x, y, z)
525  /// and replace it with a tile of the specified value and state.
526  /// If no such node exists, leave the tree unchanged and return @c nullptr.
527  /// @note The caller takes ownership of the node and is responsible for deleting it.
528  template<typename NodeT>
529  NodeT* stealNode(const Coord& xyz, const ValueType& value, bool active);
530 
531  /// @brief Return a pointer to the leaf node that contains voxel (x, y, z).
532  /// If no such node exists, create one that preserves the values and
533  /// active states of all voxels.
534  /// @details Use this method to preallocate a static tree topology over which to
535  /// safely perform multithreaded processing.
536  LeafNodeType* touchLeaf(const Coord& xyz);
537 
538  //@{
539  /// @brief Return a pointer to the node of type @c NodeType that contains
540  /// voxel (x, y, z). If no such node exists, return @c nullptr.
541  template<typename NodeType> NodeType* probeNode(const Coord& xyz);
542  template<typename NodeType> const NodeType* probeConstNode(const Coord& xyz) const;
543  template<typename NodeType> const NodeType* probeNode(const Coord& xyz) const;
544  //@}
545 
546  //@{
547  /// @brief Return a pointer to the leaf node that contains voxel (x, y, z).
548  /// If no such node exists, return @c nullptr.
549  LeafNodeType* probeLeaf(const Coord& xyz);
550  const LeafNodeType* probeConstLeaf(const Coord& xyz) const;
551  const LeafNodeType* probeLeaf(const Coord& xyz) const { return this->probeConstLeaf(xyz); }
552  //@}
553 
554  //@{
555  /// @brief Adds all nodes of a certain type to a container with the following API:
556  /// @code
557  /// struct ArrayT {
558  /// using value_type = ...; // the type of node to be added to the array
559  /// void push_back(value_type nodePtr); // add a node to the array
560  /// };
561  /// @endcode
562  /// @details An example of a wrapper around a c-style array is:
563  /// @code
564  /// struct MyArray {
565  /// using value_type = LeafType*;
566  /// value_type* ptr;
567  /// MyArray(value_type* array) : ptr(array) {}
568  /// void push_back(value_type leaf) { *ptr++ = leaf; }
569  ///};
570  /// @endcode
571  /// @details An example that constructs a list of pointer to all leaf nodes is:
572  /// @code
573  /// std::vector<const LeafNodeType*> array;//most std contains have the required API
574  /// array.reserve(tree.leafCount());//this is a fast preallocation.
575  /// tree.getNodes(array);
576  /// @endcode
577  template<typename ArrayT> void getNodes(ArrayT& array) { mRoot.getNodes(array); }
578  template<typename ArrayT> void getNodes(ArrayT& array) const { mRoot.getNodes(array); }
579  //@}
580 
581  /// @brief Steals all nodes of a certain type from the tree and
582  /// adds them to a container with the following API:
583  /// @code
584  /// struct ArrayT {
585  /// using value_type = ...; // the type of node to be added to the array
586  /// void push_back(value_type nodePtr); // add a node to the array
587  /// };
588  /// @endcode
589  /// @details An example of a wrapper around a c-style array is:
590  /// @code
591  /// struct MyArray {
592  /// using value_type = LeafType*;
593  /// value_type* ptr;
594  /// MyArray(value_type* array) : ptr(array) {}
595  /// void push_back(value_type leaf) { *ptr++ = leaf; }
596  ///};
597  /// @endcode
598  /// @details An example that constructs a list of pointer to all leaf nodes is:
599  /// @code
600  /// std::vector<const LeafNodeType*> array;//most std contains have the required API
601  /// array.reserve(tree.leafCount());//this is a fast preallocation.
602  /// tree.stealNodes(array);
603  /// @endcode
604  template<typename ArrayT>
605  void stealNodes(ArrayT& array) { this->clearAllAccessors(); mRoot.stealNodes(array); }
606  template<typename ArrayT>
607  void stealNodes(ArrayT& array, const ValueType& value, bool state)
608  {
609  this->clearAllAccessors();
610  mRoot.stealNodes(array, value, state);
611  }
612 
613  //
614  // Aux methods
615  //
616  /// @brief Return @c true if this tree contains no nodes other than
617  /// the root node and no tiles other than background tiles.
618  bool empty() const { return mRoot.empty(); }
619 
620  /// Remove all tiles from this tree and all nodes other than the root node.
621  void clear();
622 
623  /// Clear all registered accessors.
624  void clearAllAccessors();
625 
626  //@{
627  /// @brief Register an accessor for this tree. Registered accessors are
628  /// automatically cleared whenever one of this tree's nodes is deleted.
631  //@}
632 
633  //@{
634  /// Dummy implementations
637  //@}
638 
639  //@{
640  /// Deregister an accessor so that it is no longer automatically cleared.
643  //@}
644 
645  //@{
646  /// Dummy implementations
649  //@}
650 
651  /// @brief Return this tree's background value wrapped as metadata.
652  /// @note Query the metadata object for the value's type.
653  Metadata::Ptr getBackgroundValue() const override;
654 
655  /// @brief Return this tree's background value.
656  ///
657  /// @note Use tools::changeBackground to efficiently modify the
658  /// background values. Else use tree.root().setBackground, which
659  /// is serial and hence slower.
660  const ValueType& background() const { return mRoot.background(); }
661 
662  /// Min and max are both inclusive.
663  void getIndexRange(CoordBBox& bbox) const override { mRoot.getIndexRange(bbox); }
664 
665  /// @brief Efficiently merge another tree into this tree using one of several schemes.
666  /// @details This operation is primarily intended to combine trees that are mostly
667  /// non-overlapping (for example, intermediate trees from computations that are
668  /// parallelized across disjoint regions of space).
669  /// @note This operation is not guaranteed to produce an optimally sparse tree.
670  /// Follow merge() with prune() for optimal sparseness.
671  /// @warning This operation always empties the other tree.
672  void merge(Tree& other, MergePolicy = MERGE_ACTIVE_STATES);
673 
674  /// @brief Union this tree's set of active values with the active values
675  /// of the other tree, whose @c ValueType may be different.
676  /// @details The resulting state of a value is active if the corresponding value
677  /// was already active OR if it is active in the other tree. Also, a resulting
678  /// value maps to a voxel if the corresponding value already mapped to a voxel
679  /// OR if it is a voxel in the other tree. Thus, a resulting value can only
680  /// map to a tile if the corresponding value already mapped to a tile
681  /// AND if it is a tile value in other tree.
682  ///
683  /// @note This operation modifies only active states, not values.
684  /// Specifically, active tiles and voxels in this tree are not changed, and
685  /// tiles or voxels that were inactive in this tree but active in the other tree
686  /// are marked as active in this tree but left with their original values.
687  template<typename OtherRootNodeType>
688  void topologyUnion(const Tree<OtherRootNodeType>& other);
689 
690  /// @brief Intersects this tree's set of active values with the active values
691  /// of the other tree, whose @c ValueType may be different.
692  /// @details The resulting state of a value is active only if the corresponding
693  /// value was already active AND if it is active in the other tree. Also, a
694  /// resulting value maps to a voxel if the corresponding value
695  /// already mapped to an active voxel in either of the two grids
696  /// and it maps to an active tile or voxel in the other grid.
697  ///
698  /// @note This operation can delete branches in this grid if they
699  /// overlap with inactive tiles in the other grid. Likewise active
700  /// voxels can be turned into unactive voxels resulting in leaf
701  /// nodes with no active values. Thus, it is recommended to
702  /// subsequently call tools::pruneInactive.
703  template<typename OtherRootNodeType>
705 
706  /// @brief Difference this tree's set of active values with the active values
707  /// of the other tree, whose @c ValueType may be different. So a
708  /// resulting voxel will be active only if the original voxel is
709  /// active in this tree and inactive in the other tree.
710  ///
711  /// @note This operation can delete branches in this grid if they
712  /// overlap with active tiles in the other grid. Likewise active
713  /// voxels can be turned into inactive voxels resulting in leaf
714  /// nodes with no active values. Thus, it is recommended to
715  /// subsequently call tools::pruneInactive.
716  template<typename OtherRootNodeType>
717  void topologyDifference(const Tree<OtherRootNodeType>& other);
718 
719  /// For a given function @c f, use sparse traversal to compute <tt>f(this, other)</tt>
720  /// over all corresponding pairs of values (tile or voxel) of this tree and the other tree
721  /// and store the result in this tree.
722  /// This method is typically more space-efficient than the two-tree combine2(),
723  /// since it moves rather than copies nodes from the other tree into this tree.
724  /// @note This operation always empties the other tree.
725  /// @param other a tree of the same type as this tree
726  /// @param op a functor of the form <tt>void op(const T& a, const T& b, T& result)</tt>,
727  /// where @c T is this tree's @c ValueType, that computes
728  /// <tt>result = f(a, b)</tt>
729  /// @param prune if true, prune the resulting tree one branch at a time (this is usually
730  /// more space-efficient than pruning the entire tree in one pass)
731  ///
732  /// @par Example:
733  /// Compute the per-voxel difference between two floating-point trees,
734  /// @c aTree and @c bTree, and store the result in @c aTree (leaving @c bTree empty).
735  /// @code
736  /// {
737  /// struct Local {
738  /// static inline void diff(const float& a, const float& b, float& result) {
739  /// result = a - b;
740  /// }
741  /// };
742  /// aTree.combine(bTree, Local::diff);
743  /// }
744  /// @endcode
745  ///
746  /// @par Example:
747  /// Compute <tt>f * a + (1 - f) * b</tt> over all voxels of two floating-point trees,
748  /// @c aTree and @c bTree, and store the result in @c aTree (leaving @c bTree empty).
749  /// @code
750  /// namespace {
751  /// struct Blend {
752  /// Blend(float f): frac(f) {}
753  /// inline void operator()(const float& a, const float& b, float& result) const {
754  /// result = frac * a + (1.0 - frac) * b;
755  /// }
756  /// float frac;
757  /// };
758  /// }
759  /// {
760  /// aTree.combine(bTree, Blend(0.25)); // 0.25 * a + 0.75 * b
761  /// }
762  /// @endcode
763  template<typename CombineOp>
764  void combine(Tree& other, CombineOp& op, bool prune = false);
765 #ifndef _MSC_VER
766  template<typename CombineOp>
767  void combine(Tree& other, const CombineOp& op, bool prune = false);
768 #endif
769 
770  /// Like combine(), but with
771  /// @param other a tree of the same type as this tree
772  /// @param op a functor of the form <tt>void op(CombineArgs<ValueType>& args)</tt> that
773  /// computes <tt>args.setResult(f(args.a(), args.b()))</tt> and, optionally,
774  /// <tt>args.setResultIsActive(g(args.aIsActive(), args.bIsActive()))</tt>
775  /// for some functions @c f and @c g
776  /// @param prune if true, prune the resulting tree one branch at a time (this is usually
777  /// more space-efficient than pruning the entire tree in one pass)
778  ///
779  /// This variant passes not only the @em a and @em b values but also the active states
780  /// of the @em a and @em b values to the functor, which may then return, by calling
781  /// @c args.setResultIsActive(), a computed active state for the result value.
782  /// By default, the result is active if either the @em a or the @em b value is active.
783  ///
784  /// @see openvdb/Types.h for the definition of the CombineArgs struct.
785  ///
786  /// @par Example:
787  /// Replace voxel values in floating-point @c aTree with corresponding values
788  /// from floating-point @c bTree (leaving @c bTree empty) wherever the @c bTree
789  /// values are larger. Also, preserve the active states of any transferred values.
790  /// @code
791  /// {
792  /// struct Local {
793  /// static inline void max(CombineArgs<float>& args) {
794  /// if (args.b() > args.a()) {
795  /// // Transfer the B value and its active state.
796  /// args.setResult(args.b());
797  /// args.setResultIsActive(args.bIsActive());
798  /// } else {
799  /// // Preserve the A value and its active state.
800  /// args.setResult(args.a());
801  /// args.setResultIsActive(args.aIsActive());
802  /// }
803  /// }
804  /// };
805  /// aTree.combineExtended(bTree, Local::max);
806  /// }
807  /// @endcode
808  template<typename ExtendedCombineOp>
809  void combineExtended(Tree& other, ExtendedCombineOp& op, bool prune = false);
810 #ifndef _MSC_VER
811  template<typename ExtendedCombineOp>
812  void combineExtended(Tree& other, const ExtendedCombineOp& op, bool prune = false);
813 #endif
814 
815  /// For a given function @c f, use sparse traversal to compute <tt>f(a, b)</tt> over all
816  /// corresponding pairs of values (tile or voxel) of trees A and B and store the result
817  /// in this tree.
818  /// @param a,b two trees with the same configuration (levels and node dimensions)
819  /// as this tree but with the B tree possibly having a different value type
820  /// @param op a functor of the form <tt>void op(const T1& a, const T2& b, T1& result)</tt>,
821  /// where @c T1 is this tree's and the A tree's @c ValueType and @c T2 is the
822  /// B tree's @c ValueType, that computes <tt>result = f(a, b)</tt>
823  /// @param prune if true, prune the resulting tree one branch at a time (this is usually
824  /// more space-efficient than pruning the entire tree in one pass)
825  ///
826  /// @throw TypeError if the B tree's configuration doesn't match this tree's
827  /// or if this tree's ValueType is not constructible from the B tree's ValueType.
828  ///
829  /// @par Example:
830  /// Compute the per-voxel difference between two floating-point trees,
831  /// @c aTree and @c bTree, and store the result in a third tree.
832  /// @code
833  /// {
834  /// struct Local {
835  /// static inline void diff(const float& a, const float& b, float& result) {
836  /// result = a - b;
837  /// }
838  /// };
839  /// FloatTree resultTree;
840  /// resultTree.combine2(aTree, bTree, Local::diff);
841  /// }
842  /// @endcode
843  template<typename CombineOp, typename OtherTreeType /*= Tree*/>
844  void combine2(const Tree& a, const OtherTreeType& b, CombineOp& op, bool prune = false);
845 #ifndef _MSC_VER
846  template<typename CombineOp, typename OtherTreeType /*= Tree*/>
847  void combine2(const Tree& a, const OtherTreeType& b, const CombineOp& op, bool prune = false);
848 #endif
849 
850  /// Like combine2(), but with
851  /// @param a,b two trees with the same configuration (levels and node dimensions)
852  /// as this tree but with the B tree possibly having a different value type
853  /// @param op a functor of the form <tt>void op(CombineArgs<T1, T2>& args)</tt>, where
854  /// @c T1 is this tree's and the A tree's @c ValueType and @c T2 is the B tree's
855  /// @c ValueType, that computes <tt>args.setResult(f(args.a(), args.b()))</tt>
856  /// and, optionally,
857  /// <tt>args.setResultIsActive(g(args.aIsActive(), args.bIsActive()))</tt>
858  /// for some functions @c f and @c g
859  /// @param prune if true, prune the resulting tree one branch at a time (this is usually
860  /// more space-efficient than pruning the entire tree in one pass)
861  /// This variant passes not only the @em a and @em b values but also the active states
862  /// of the @em a and @em b values to the functor, which may then return, by calling
863  /// <tt>args.setResultIsActive()</tt>, a computed active state for the result value.
864  /// By default, the result is active if either the @em a or the @em b value is active.
865  ///
866  /// @throw TypeError if the B tree's configuration doesn't match this tree's
867  /// or if this tree's ValueType is not constructible from the B tree's ValueType.
868  ///
869  /// @see openvdb/Types.h for the definition of the CombineArgs struct.
870  ///
871  /// @par Example:
872  /// Compute the per-voxel maximum values of two single-precision floating-point trees,
873  /// @c aTree and @c bTree, and store the result in a third tree. Set the active state
874  /// of each output value to that of the larger of the two input values.
875  /// @code
876  /// {
877  /// struct Local {
878  /// static inline void max(CombineArgs<float>& args) {
879  /// if (args.b() > args.a()) {
880  /// // Transfer the B value and its active state.
881  /// args.setResult(args.b());
882  /// args.setResultIsActive(args.bIsActive());
883  /// } else {
884  /// // Preserve the A value and its active state.
885  /// args.setResult(args.a());
886  /// args.setResultIsActive(args.aIsActive());
887  /// }
888  /// }
889  /// };
890  /// FloatTree aTree = ...;
891  /// FloatTree bTree = ...;
892  /// FloatTree resultTree;
893  /// resultTree.combine2Extended(aTree, bTree, Local::max);
894  /// }
895  /// @endcode
896  ///
897  /// @par Example:
898  /// Compute the per-voxel maximum values of a double-precision and a single-precision
899  /// floating-point tree, @c aTree and @c bTree, and store the result in a third,
900  /// double-precision tree. Set the active state of each output value to that of
901  /// the larger of the two input values.
902  /// @code
903  /// {
904  /// struct Local {
905  /// static inline void max(CombineArgs<double, float>& args) {
906  /// if (args.b() > args.a()) {
907  /// // Transfer the B value and its active state.
908  /// args.setResult(args.b());
909  /// args.setResultIsActive(args.bIsActive());
910  /// } else {
911  /// // Preserve the A value and its active state.
912  /// args.setResult(args.a());
913  /// args.setResultIsActive(args.aIsActive());
914  /// }
915  /// }
916  /// };
917  /// DoubleTree aTree = ...;
918  /// FloatTree bTree = ...;
919  /// DoubleTree resultTree;
920  /// resultTree.combine2Extended(aTree, bTree, Local::max);
921  /// }
922  /// @endcode
923  template<typename ExtendedCombineOp, typename OtherTreeType /*= Tree*/>
924  void combine2Extended(const Tree& a, const OtherTreeType& b, ExtendedCombineOp& op,
925  bool prune = false);
926 #ifndef _MSC_VER
927  template<typename ExtendedCombineOp, typename OtherTreeType /*= Tree*/>
928  void combine2Extended(const Tree& a, const OtherTreeType& b, const ExtendedCombineOp&,
929  bool prune = false);
930 #endif
931 
932  template<typename BBoxOp>
933  [[deprecated("Use DynamicNodeManager instead")]]
934  void visitActiveBBox(BBoxOp& op) const { mRoot.visitActiveBBox(op); }
935 
936  template<typename VisitorOp>
937  [[deprecated("Use DynamicNodeManager instead")]]
938  void visit(VisitorOp& op);
939  template<typename VisitorOp>
940  [[deprecated("Use DynamicNodeManager instead")]]
941  void visit(const VisitorOp& op);
942 
943  template<typename VisitorOp>
944  [[deprecated("Use DynamicNodeManager instead")]]
945  void visit(VisitorOp& op) const;
946  template<typename VisitorOp>
947  [[deprecated("Use DynamicNodeManager instead")]]
948  void visit(const VisitorOp& op) const;
949 
950  template<typename OtherTreeType, typename VisitorOp>
951  [[deprecated("Use DynamicNodeManager instead")]]
952  void visit2(OtherTreeType& other, VisitorOp& op);
953  template<typename OtherTreeType, typename VisitorOp>
954  [[deprecated("Use DynamicNodeManager instead")]]
955  void visit2(OtherTreeType& other, const VisitorOp& op);
956 
957  template<typename OtherTreeType, typename VisitorOp>
958  [[deprecated("Use DynamicNodeManager instead")]]
959  void visit2(OtherTreeType& other, VisitorOp& op) const;
960  template<typename OtherTreeType, typename VisitorOp>
961  [[deprecated("Use DynamicNodeManager instead")]]
962  void visit2(OtherTreeType& other, const VisitorOp& op) const;
963 
964 
965  //
966  // Iteration
967  //
968  //@{
969  /// Return an iterator over children of the root node.
970  typename RootNodeType::ChildOnCIter beginRootChildren() const { return mRoot.cbeginChildOn(); }
971  typename RootNodeType::ChildOnCIter cbeginRootChildren() const { return mRoot.cbeginChildOn(); }
972  typename RootNodeType::ChildOnIter beginRootChildren() { return mRoot.beginChildOn(); }
973  //@}
974 
975  //@{
976  /// Return an iterator over non-child entries of the root node's table.
977  typename RootNodeType::ChildOffCIter beginRootTiles() const { return mRoot.cbeginChildOff(); }
978  typename RootNodeType::ChildOffCIter cbeginRootTiles() const { return mRoot.cbeginChildOff(); }
979  typename RootNodeType::ChildOffIter beginRootTiles() { return mRoot.beginChildOff(); }
980  //@}
981 
982  //@{
983  /// Return an iterator over all entries of the root node's table.
984  typename RootNodeType::ChildAllCIter beginRootDense() const { return mRoot.cbeginChildAll(); }
985  typename RootNodeType::ChildAllCIter cbeginRootDense() const { return mRoot.cbeginChildAll(); }
986  typename RootNodeType::ChildAllIter beginRootDense() { return mRoot.beginChildAll(); }
987  //@}
988 
989 
990  //@{
991  /// Iterator over all nodes in this tree
994  //@}
995 
996  //@{
997  /// Iterator over all leaf nodes in this tree
1000  //@}
1001 
1002  //@{
1003  /// Return an iterator over all nodes in this tree.
1004  NodeIter beginNode() { return NodeIter(*this); }
1005  NodeCIter beginNode() const { return NodeCIter(*this); }
1006  NodeCIter cbeginNode() const { return NodeCIter(*this); }
1007  //@}
1008 
1009  //@{
1010  /// Return an iterator over all leaf nodes in this tree.
1011  LeafIter beginLeaf() { return LeafIter(*this); }
1012  LeafCIter beginLeaf() const { return LeafCIter(*this); }
1013  LeafCIter cbeginLeaf() const { return LeafCIter(*this); }
1014  //@}
1015 
1022 
1023  //@{
1024  /// Return an iterator over all values (tile and voxel) across all nodes.
1026  ValueAllCIter beginValueAll() const { return ValueAllCIter(*this); }
1027  ValueAllCIter cbeginValueAll() const { return ValueAllCIter(*this); }
1028  //@}
1029  //@{
1030  /// Return an iterator over active values (tile and voxel) across all nodes.
1031  ValueOnIter beginValueOn() { return ValueOnIter(*this); }
1032  ValueOnCIter beginValueOn() const { return ValueOnCIter(*this); }
1033  ValueOnCIter cbeginValueOn() const { return ValueOnCIter(*this); }
1034  //@}
1035  //@{
1036  /// Return an iterator over inactive values (tile and voxel) across all nodes.
1038  ValueOffCIter beginValueOff() const { return ValueOffCIter(*this); }
1039  ValueOffCIter cbeginValueOff() const { return ValueOffCIter(*this); }
1040  //@}
1041 
1042  /// @brief Return an iterator of type @c IterT (for example, begin<ValueOnIter>() is
1043  /// equivalent to beginValueOn()).
1044  template<typename IterT> IterT begin();
1045  /// @brief Return a const iterator of type CIterT (for example, cbegin<ValueOnCIter>()
1046  /// is equivalent to cbeginValueOn()).
1047  template<typename CIterT> CIterT cbegin() const;
1048 
1049 
1050 protected:
1051  using AccessorRegistry = tbb::concurrent_hash_map<ValueAccessorBase<Tree, true>*, bool>;
1052  using ConstAccessorRegistry = tbb::concurrent_hash_map<ValueAccessorBase<const Tree, true>*, bool>;
1053 
1054  /// @brief Notify all registered accessors, by calling ValueAccessor::release(),
1055  /// that this tree is about to be deleted.
1056  void releaseAllAccessors();
1057 
1058  // TBB body object used to deallocates nodes in parallel.
1059  template<typename NodeType>
1061  DeallocateNodes(std::vector<NodeType*>& nodes)
1062  : mNodes(nodes.empty() ? nullptr : &nodes.front()) { }
1063  void operator()(const tbb::blocked_range<size_t>& range) const {
1064  for (size_t n = range.begin(), N = range.end(); n < N; ++n) {
1065  delete mNodes[n]; mNodes[n] = nullptr;
1066  }
1067  }
1068  NodeType ** const mNodes;
1069  };
1070 
1071  //
1072  // Data members
1073  //
1074  RootNodeType mRoot; // root node of the tree
1077 
1078  static std::unique_ptr<const Name> sTreeTypeName;
1079 }; // end of Tree class
1080 
1081 template<typename _RootNodeType>
1082 std::unique_ptr<const Name> Tree<_RootNodeType>::sTreeTypeName;
1083 
1084 
1085 /// @brief Tree3<T, N1, N2>::Type is the type of a three-level tree
1086 /// (Root, Internal, Leaf) with value type T and
1087 /// internal and leaf node log dimensions N1 and N2, respectively.
1088 /// @note This is NOT the standard tree configuration (Tree4 is).
1089 template<typename T, Index N1=4, Index N2=3>
1090 struct Tree3 {
1092 };
1093 
1094 
1095 /// @brief Tree4<T, N1, N2, N3>::Type is the type of a four-level tree
1096 /// (Root, Internal, Internal, Leaf) with value type T and
1097 /// internal and leaf node log dimensions N1, N2 and N3, respectively.
1098 /// @note This is the standard tree configuration.
1099 template<typename T, Index N1=5, Index N2=4, Index N3=3>
1100 struct Tree4 {
1102 };
1103 
1104 /// @brief Tree5<T, N1, N2, N3, N4>::Type is the type of a five-level tree
1105 /// (Root, Internal, Internal, Internal, Leaf) with value type T and
1106 /// internal and leaf node log dimensions N1, N2, N3 and N4, respectively.
1107 /// @note This is NOT the standard tree configuration (Tree4 is).
1108 template<typename T, Index N1=6, Index N2=5, Index N3=4, Index N4=3>
1109 struct Tree5 {
1110  using Type =
1112 };
1113 
1114 
1115 ////////////////////////////////////////
1116 
1117 
1118 inline void
1119 TreeBase::readTopology(std::istream& is, bool /*saveFloatAsHalf*/)
1120 {
1121  int32_t bufferCount;
1122  is.read(reinterpret_cast<char*>(&bufferCount), sizeof(int32_t));
1123  if (bufferCount != 1) OPENVDB_LOG_WARN("multi-buffer trees are no longer supported");
1124 }
1125 
1126 
1127 inline void
1128 TreeBase::writeTopology(std::ostream& os, bool /*saveFloatAsHalf*/) const
1129 {
1130  int32_t bufferCount = 1;
1131  os.write(reinterpret_cast<char*>(&bufferCount), sizeof(int32_t));
1132 }
1133 
1134 
1135 inline void
1136 TreeBase::print(std::ostream& os, int /*verboseLevel*/) const
1137 {
1138  os << " Tree Type: " << type()
1139  << " Active Voxel Count: " << activeVoxelCount() << std::endl
1140  << " Active tile Count: " << activeTileCount() << std::endl
1141  << " Inactive Voxel Count: " << inactiveVoxelCount() << std::endl
1142  << " Leaf Node Count: " << leafCount() << std::endl
1143  << " Non-leaf Node Count: " << nonLeafCount() << std::endl;
1144 }
1145 
1146 
1147 ////////////////////////////////////////
1148 
1149 
1150 //
1151 // Type traits for tree iterators
1152 //
1153 
1154 /// @brief TreeIterTraits provides, for all tree iterators, a begin(tree) function
1155 /// that returns an iterator over a tree of arbitrary type.
1156 template<typename TreeT, typename IterT> struct TreeIterTraits;
1157 
1158 template<typename TreeT> struct TreeIterTraits<TreeT, typename TreeT::RootNodeType::ChildOnIter> {
1159  static typename TreeT::RootNodeType::ChildOnIter begin(TreeT& tree) {
1160  return tree.beginRootChildren();
1161  }
1162 };
1163 
1164 template<typename TreeT> struct TreeIterTraits<TreeT, typename TreeT::RootNodeType::ChildOnCIter> {
1165  static typename TreeT::RootNodeType::ChildOnCIter begin(const TreeT& tree) {
1166  return tree.cbeginRootChildren();
1167  }
1168 };
1169 
1170 template<typename TreeT> struct TreeIterTraits<TreeT, typename TreeT::RootNodeType::ChildOffIter> {
1171  static typename TreeT::RootNodeType::ChildOffIter begin(TreeT& tree) {
1172  return tree.beginRootTiles();
1173  }
1174 };
1175 
1176 template<typename TreeT> struct TreeIterTraits<TreeT, typename TreeT::RootNodeType::ChildOffCIter> {
1177  static typename TreeT::RootNodeType::ChildOffCIter begin(const TreeT& tree) {
1178  return tree.cbeginRootTiles();
1179  }
1180 };
1181 
1182 template<typename TreeT> struct TreeIterTraits<TreeT, typename TreeT::RootNodeType::ChildAllIter> {
1183  static typename TreeT::RootNodeType::ChildAllIter begin(TreeT& tree) {
1184  return tree.beginRootDense();
1185  }
1186 };
1187 
1188 template<typename TreeT> struct TreeIterTraits<TreeT, typename TreeT::RootNodeType::ChildAllCIter> {
1189  static typename TreeT::RootNodeType::ChildAllCIter begin(const TreeT& tree) {
1190  return tree.cbeginRootDense();
1191  }
1192 };
1193 
1194 template<typename TreeT> struct TreeIterTraits<TreeT, typename TreeT::NodeIter> {
1195  static typename TreeT::NodeIter begin(TreeT& tree) { return tree.beginNode(); }
1196 };
1197 
1198 template<typename TreeT> struct TreeIterTraits<TreeT, typename TreeT::NodeCIter> {
1199  static typename TreeT::NodeCIter begin(const TreeT& tree) { return tree.cbeginNode(); }
1200 };
1201 
1202 template<typename TreeT> struct TreeIterTraits<TreeT, typename TreeT::LeafIter> {
1203  static typename TreeT::LeafIter begin(TreeT& tree) { return tree.beginLeaf(); }
1204 };
1205 
1206 template<typename TreeT> struct TreeIterTraits<TreeT, typename TreeT::LeafCIter> {
1207  static typename TreeT::LeafCIter begin(const TreeT& tree) { return tree.cbeginLeaf(); }
1208 };
1209 
1210 template<typename TreeT> struct TreeIterTraits<TreeT, typename TreeT::ValueOnIter> {
1211  static typename TreeT::ValueOnIter begin(TreeT& tree) { return tree.beginValueOn(); }
1212 };
1213 
1214 template<typename TreeT> struct TreeIterTraits<TreeT, typename TreeT::ValueOnCIter> {
1215  static typename TreeT::ValueOnCIter begin(const TreeT& tree) { return tree.cbeginValueOn(); }
1216 };
1217 
1218 template<typename TreeT> struct TreeIterTraits<TreeT, typename TreeT::ValueOffIter> {
1219  static typename TreeT::ValueOffIter begin(TreeT& tree) { return tree.beginValueOff(); }
1220 };
1221 
1222 template<typename TreeT> struct TreeIterTraits<TreeT, typename TreeT::ValueOffCIter> {
1223  static typename TreeT::ValueOffCIter begin(const TreeT& tree) { return tree.cbeginValueOff(); }
1224 };
1225 
1226 template<typename TreeT> struct TreeIterTraits<TreeT, typename TreeT::ValueAllIter> {
1227  static typename TreeT::ValueAllIter begin(TreeT& tree) { return tree.beginValueAll(); }
1228 };
1229 
1230 template<typename TreeT> struct TreeIterTraits<TreeT, typename TreeT::ValueAllCIter> {
1231  static typename TreeT::ValueAllCIter begin(const TreeT& tree) { return tree.cbeginValueAll(); }
1232 };
1233 
1234 
1235 template<typename RootNodeType>
1236 template<typename IterT>
1237 inline IterT
1239 {
1240  return TreeIterTraits<Tree, IterT>::begin(*this);
1241 }
1242 
1243 
1244 template<typename RootNodeType>
1245 template<typename IterT>
1246 inline IterT
1248 {
1249  return TreeIterTraits<Tree, IterT>::begin(*this);
1250 }
1251 
1252 
1253 ////////////////////////////////////////
1254 
1255 
1256 template<typename RootNodeType>
1257 void
1258 Tree<RootNodeType>::readTopology(std::istream& is, bool saveFloatAsHalf)
1259 {
1260  this->clearAllAccessors();
1261  TreeBase::readTopology(is, saveFloatAsHalf);
1262  mRoot.readTopology(is, saveFloatAsHalf);
1263 }
1264 
1265 
1266 template<typename RootNodeType>
1267 void
1268 Tree<RootNodeType>::writeTopology(std::ostream& os, bool saveFloatAsHalf) const
1269 {
1270  TreeBase::writeTopology(os, saveFloatAsHalf);
1271  mRoot.writeTopology(os, saveFloatAsHalf);
1272 }
1273 
1274 
1275 template<typename RootNodeType>
1276 inline void
1277 Tree<RootNodeType>::readBuffers(std::istream &is, bool saveFloatAsHalf)
1278 {
1279  this->clearAllAccessors();
1280  mRoot.readBuffers(is, saveFloatAsHalf);
1281 }
1282 
1283 
1284 template<typename RootNodeType>
1285 inline void
1286 Tree<RootNodeType>::readBuffers(std::istream &is, const CoordBBox& bbox, bool saveFloatAsHalf)
1287 {
1288  this->clearAllAccessors();
1289  mRoot.readBuffers(is, bbox, saveFloatAsHalf);
1290 }
1291 
1292 
1293 template<typename RootNodeType>
1294 inline void
1296 {
1297  for (LeafCIter it = this->cbeginLeaf(); it; ++it) {
1298  // Retrieving the value of a leaf voxel forces loading of the leaf node's voxel buffer.
1299  it->getValue(Index(0));
1300  }
1301 }
1302 
1303 
1304 template<typename RootNodeType>
1305 inline void
1306 Tree<RootNodeType>::writeBuffers(std::ostream &os, bool saveFloatAsHalf) const
1307 {
1308  mRoot.writeBuffers(os, saveFloatAsHalf);
1309 }
1310 
1311 
1312 template<typename RootNodeType>
1313 inline void
1315 {
1316  std::vector<LeafNodeType*> leafnodes;
1317  this->stealNodes(leafnodes);
1318 
1319  tbb::parallel_for(tbb::blocked_range<size_t>(0, leafnodes.size()),
1320  DeallocateNodes<LeafNodeType>(leafnodes));
1321 
1322  std::vector<typename RootNodeType::ChildNodeType*> internalNodes;
1323  this->stealNodes(internalNodes);
1324 
1325  tbb::parallel_for(tbb::blocked_range<size_t>(0, internalNodes.size()),
1327 
1328  mRoot.clear();
1329 
1330  this->clearAllAccessors();
1331 }
1332 
1333 
1334 ////////////////////////////////////////
1335 
1336 
1337 template<typename RootNodeType>
1338 inline void
1340 {
1341  typename AccessorRegistry::accessor a;
1342  mAccessorRegistry.insert(a, &accessor);
1343 }
1344 
1345 
1346 template<typename RootNodeType>
1347 inline void
1349 {
1350  typename ConstAccessorRegistry::accessor a;
1351  mConstAccessorRegistry.insert(a, &accessor);
1352 }
1353 
1354 
1355 template<typename RootNodeType>
1356 inline void
1358 {
1359  mAccessorRegistry.erase(&accessor);
1360 }
1361 
1362 
1363 template<typename RootNodeType>
1364 inline void
1366 {
1367  mConstAccessorRegistry.erase(&accessor);
1368 }
1369 
1370 
1371 template<typename RootNodeType>
1372 inline void
1374 {
1375  for (typename AccessorRegistry::iterator it = mAccessorRegistry.begin();
1376  it != mAccessorRegistry.end(); ++it)
1377  {
1378  if (it->first) it->first->clear();
1379  }
1380 
1381  for (typename ConstAccessorRegistry::iterator it = mConstAccessorRegistry.begin();
1382  it != mConstAccessorRegistry.end(); ++it)
1383  {
1384  if (it->first) it->first->clear();
1385  }
1386 }
1387 
1388 
1389 template<typename RootNodeType>
1390 inline void
1392 {
1393  mAccessorRegistry.erase(nullptr);
1394  for (typename AccessorRegistry::iterator it = mAccessorRegistry.begin();
1395  it != mAccessorRegistry.end(); ++it)
1396  {
1397  it->first->release();
1398  }
1399  mAccessorRegistry.clear();
1400 
1401  mAccessorRegistry.erase(nullptr);
1402  for (typename ConstAccessorRegistry::iterator it = mConstAccessorRegistry.begin();
1403  it != mConstAccessorRegistry.end(); ++it)
1404  {
1405  it->first->release();
1406  }
1407  mConstAccessorRegistry.clear();
1408 }
1409 
1410 
1411 ////////////////////////////////////////
1412 
1413 
1414 template<typename RootNodeType>
1415 inline const typename RootNodeType::ValueType&
1416 Tree<RootNodeType>::getValue(const Coord& xyz) const
1417 {
1418  return mRoot.getValue(xyz);
1419 }
1420 
1421 
1422 template<typename RootNodeType>
1423 template<typename AccessT>
1424 inline const typename RootNodeType::ValueType&
1425 Tree<RootNodeType>::getValue(const Coord& xyz, AccessT& accessor) const
1426 {
1427  return accessor.getValue(xyz);
1428 }
1429 
1430 
1431 template<typename RootNodeType>
1432 inline int
1433 Tree<RootNodeType>::getValueDepth(const Coord& xyz) const
1434 {
1435  return mRoot.getValueDepth(xyz);
1436 }
1437 
1438 
1439 template<typename RootNodeType>
1440 inline void
1442 {
1443  mRoot.setValueOff(xyz);
1444 }
1445 
1446 
1447 template<typename RootNodeType>
1448 inline void
1450 {
1451  mRoot.setValueOff(xyz, value);
1452 }
1453 
1454 
1455 template<typename RootNodeType>
1456 inline void
1457 Tree<RootNodeType>::setActiveState(const Coord& xyz, bool on)
1458 {
1459  mRoot.setActiveState(xyz, on);
1460 }
1461 
1462 
1463 template<typename RootNodeType>
1464 inline void
1466 {
1467  mRoot.setValueOn(xyz, value);
1468 }
1469 
1470 template<typename RootNodeType>
1471 inline void
1473 {
1474  mRoot.setValueOnly(xyz, value);
1475 }
1476 
1477 template<typename RootNodeType>
1478 template<typename AccessT>
1479 inline void
1480 Tree<RootNodeType>::setValue(const Coord& xyz, const ValueType& value, AccessT& accessor)
1481 {
1482  accessor.setValue(xyz, value);
1483 }
1484 
1485 
1486 template<typename RootNodeType>
1487 inline void
1489 {
1490  mRoot.setActiveState(xyz, true);
1491 }
1492 
1493 
1494 template<typename RootNodeType>
1495 inline void
1497 {
1498  mRoot.setValueOn(xyz, value);
1499 }
1500 
1501 
1502 template<typename RootNodeType>
1503 template<typename ModifyOp>
1504 inline void
1505 Tree<RootNodeType>::modifyValue(const Coord& xyz, const ModifyOp& op)
1506 {
1507  mRoot.modifyValue(xyz, op);
1508 }
1509 
1510 
1511 template<typename RootNodeType>
1512 template<typename ModifyOp>
1513 inline void
1514 Tree<RootNodeType>::modifyValueAndActiveState(const Coord& xyz, const ModifyOp& op)
1515 {
1516  mRoot.modifyValueAndActiveState(xyz, op);
1517 }
1518 
1519 
1520 template<typename RootNodeType>
1521 inline bool
1523 {
1524  return mRoot.probeValue(xyz, value);
1525 }
1526 
1527 
1528 ////////////////////////////////////////
1529 
1530 
1531 template<typename RootNodeType>
1532 inline void
1534  const ValueType& value, bool active)
1535 {
1536  mRoot.addTile(level, xyz, value, active);
1537 }
1538 
1539 
1540 template<typename RootNodeType>
1541 template<typename NodeT>
1542 inline NodeT*
1543 Tree<RootNodeType>::stealNode(const Coord& xyz, const ValueType& value, bool active)
1544 {
1545  this->clearAllAccessors();
1546  return mRoot.template stealNode<NodeT>(xyz, value, active);
1547 }
1548 
1549 
1550 template<typename RootNodeType>
1551 inline typename RootNodeType::LeafNodeType*
1553 {
1554  return mRoot.touchLeaf(xyz);
1555 }
1556 
1557 
1558 template<typename RootNodeType>
1559 inline typename RootNodeType::LeafNodeType*
1561 {
1562  return mRoot.probeLeaf(xyz);
1563 }
1564 
1565 
1566 template<typename RootNodeType>
1567 inline const typename RootNodeType::LeafNodeType*
1568 Tree<RootNodeType>::probeConstLeaf(const Coord& xyz) const
1569 {
1570  return mRoot.probeConstLeaf(xyz);
1571 }
1572 
1573 
1574 template<typename RootNodeType>
1575 template<typename NodeType>
1576 inline NodeType*
1578 {
1579  return mRoot.template probeNode<NodeType>(xyz);
1580 }
1581 
1582 
1583 template<typename RootNodeType>
1584 template<typename NodeType>
1585 inline const NodeType*
1586 Tree<RootNodeType>::probeNode(const Coord& xyz) const
1587 {
1588  return this->template probeConstNode<NodeType>(xyz);
1589 }
1590 
1591 
1592 template<typename RootNodeType>
1593 template<typename NodeType>
1594 inline const NodeType*
1595 Tree<RootNodeType>::probeConstNode(const Coord& xyz) const
1596 {
1597  return mRoot.template probeConstNode<NodeType>(xyz);
1598 }
1599 
1600 
1601 ////////////////////////////////////////
1602 
1603 
1604 template<typename RootNodeType>
1605 inline void
1606 Tree<RootNodeType>::clip(const CoordBBox& bbox)
1607 {
1608  this->clearAllAccessors();
1609  return mRoot.clip(bbox);
1610 }
1611 
1612 
1613 template<typename RootNodeType>
1614 inline void
1616 {
1617  this->clearAllAccessors();
1618  for (LeafIter it = this->beginLeaf(); it; ) {
1619  const LeafNodeType* leaf = it.getLeaf();
1620  ++it; // advance the iterator before deleting the leaf node
1621  if (!leaf->isAllocated()) {
1622  this->addTile(/*level=*/0, leaf->origin(), this->background(), /*active=*/false);
1623  }
1624  }
1625 }
1626 
1627 template<typename RootNodeType>
1628 inline Index32
1630 {
1631  Index32 sum = 0;
1632  for (auto it = this->cbeginLeaf(); it; ++it) if (!it->isAllocated()) ++sum;
1633  return sum;
1634 }
1635 
1636 
1637 template<typename RootNodeType>
1638 inline void
1639 Tree<RootNodeType>::sparseFill(const CoordBBox& bbox, const ValueType& value, bool active)
1640 {
1641  this->clearAllAccessors();
1642  return mRoot.sparseFill(bbox, value, active);
1643 }
1644 
1645 
1646 template<typename RootNodeType>
1647 inline void
1648 Tree<RootNodeType>::denseFill(const CoordBBox& bbox, const ValueType& value, bool active)
1649 {
1650  this->clearAllAccessors();
1651  return mRoot.denseFill(bbox, value, active);
1652 }
1653 
1654 
1655 template<typename RootNodeType>
1656 inline void
1658 {
1659  this->clearAllAccessors();
1660  mRoot.voxelizeActiveTiles(threaded);
1661 }
1662 
1663 
1664 template<typename RootNodeType>
1667 {
1669  if (Metadata::isRegisteredType(valueType())) {
1670  using MetadataT = TypedMetadata<ValueType>;
1671  result = Metadata::createMetadata(valueType());
1672  if (result->typeName() == MetadataT::staticTypeName()) {
1673  MetadataT* m = static_cast<MetadataT*>(result.get());
1674  m->value() = mRoot.background();
1675  }
1676  }
1677  return result;
1678 }
1679 
1680 
1681 ////////////////////////////////////////
1682 
1683 
1684 template<typename RootNodeType>
1685 inline void
1687 {
1688  this->clearAllAccessors();
1689  other.clearAllAccessors();
1690  switch (policy) {
1691  case MERGE_ACTIVE_STATES:
1692  mRoot.template merge<MERGE_ACTIVE_STATES>(other.mRoot); break;
1693  case MERGE_NODES:
1694  mRoot.template merge<MERGE_NODES>(other.mRoot); break;
1696  mRoot.template merge<MERGE_ACTIVE_STATES_AND_NODES>(other.mRoot); break;
1697  }
1698 }
1699 
1700 
1701 template<typename RootNodeType>
1702 template<typename OtherRootNodeType>
1703 inline void
1705 {
1706  this->clearAllAccessors();
1707  mRoot.topologyUnion(other.root());
1708 }
1709 
1710 template<typename RootNodeType>
1711 template<typename OtherRootNodeType>
1712 inline void
1714 {
1715  this->clearAllAccessors();
1716  mRoot.topologyIntersection(other.root());
1717 }
1718 
1719 template<typename RootNodeType>
1720 template<typename OtherRootNodeType>
1721 inline void
1723 {
1724  this->clearAllAccessors();
1725  mRoot.topologyDifference(other.root());
1726 }
1727 
1728 ////////////////////////////////////////
1729 
1730 
1731 /// @brief Helper class to adapt a three-argument (a, b, result) CombineOp functor
1732 /// into a single-argument functor that accepts a CombineArgs struct
1733 template<typename AValueT, typename CombineOp, typename BValueT = AValueT>
1735 {
1736  CombineOpAdapter(CombineOp& _op): op(_op) {}
1737 
1739  op(args.a(), args.b(), args.result());
1740  }
1741 
1742  CombineOp& op;
1743 };
1744 
1745 
1746 template<typename RootNodeType>
1747 template<typename CombineOp>
1748 inline void
1749 Tree<RootNodeType>::combine(Tree& other, CombineOp& op, bool prune)
1750 {
1752  this->combineExtended(other, extendedOp, prune);
1753 }
1754 
1755 
1756 /// @internal This overload is needed (for ICC and GCC, but not for VC) to disambiguate
1757 /// code like this: <tt>aTree.combine(bTree, MyCombineOp(...))</tt>.
1758 #ifndef _MSC_VER
1759 template<typename RootNodeType>
1760 template<typename CombineOp>
1761 inline void
1762 Tree<RootNodeType>::combine(Tree& other, const CombineOp& op, bool prune)
1763 {
1765  this->combineExtended(other, extendedOp, prune);
1766 }
1767 #endif
1768 
1769 
1770 template<typename RootNodeType>
1771 template<typename ExtendedCombineOp>
1772 inline void
1773 Tree<RootNodeType>::combineExtended(Tree& other, ExtendedCombineOp& op, bool prune)
1774 {
1775  this->clearAllAccessors();
1776  mRoot.combine(other.root(), op, prune);
1777 }
1778 
1779 
1780 /// @internal This overload is needed (for ICC and GCC, but not for VC) to disambiguate
1781 /// code like this: <tt>aTree.combineExtended(bTree, MyCombineOp(...))</tt>.
1782 #ifndef _MSC_VER
1783 template<typename RootNodeType>
1784 template<typename ExtendedCombineOp>
1785 inline void
1786 Tree<RootNodeType>::combineExtended(Tree& other, const ExtendedCombineOp& op, bool prune)
1787 {
1788  this->clearAllAccessors();
1789  mRoot.template combine<const ExtendedCombineOp>(other.mRoot, op, prune);
1790 }
1791 #endif
1792 
1793 
1794 template<typename RootNodeType>
1795 template<typename CombineOp, typename OtherTreeType>
1796 inline void
1797 Tree<RootNodeType>::combine2(const Tree& a, const OtherTreeType& b, CombineOp& op, bool prune)
1798 {
1800  this->combine2Extended(a, b, extendedOp, prune);
1801 }
1802 
1803 
1804 /// @internal This overload is needed (for ICC and GCC, but not for VC) to disambiguate
1805 /// code like this: <tt>tree.combine2(aTree, bTree, MyCombineOp(...))</tt>.
1806 #ifndef _MSC_VER
1807 template<typename RootNodeType>
1808 template<typename CombineOp, typename OtherTreeType>
1809 inline void
1810 Tree<RootNodeType>::combine2(const Tree& a, const OtherTreeType& b, const CombineOp& op, bool prune)
1811 {
1813  this->combine2Extended(a, b, extendedOp, prune);
1814 }
1815 #endif
1816 
1817 
1818 template<typename RootNodeType>
1819 template<typename ExtendedCombineOp, typename OtherTreeType>
1820 inline void
1821 Tree<RootNodeType>::combine2Extended(const Tree& a, const OtherTreeType& b,
1822  ExtendedCombineOp& op, bool prune)
1823 {
1824  this->clearAllAccessors();
1825  mRoot.combine2(a.root(), b.root(), op, prune);
1826 }
1827 
1828 
1829 /// @internal This overload is needed (for ICC and GCC, but not for VC) to disambiguate
1830 /// code like the following, where the functor argument is a temporary:
1831 /// <tt>tree.combine2Extended(aTree, bTree, MyCombineOp(...))</tt>.
1832 #ifndef _MSC_VER
1833 template<typename RootNodeType>
1834 template<typename ExtendedCombineOp, typename OtherTreeType>
1835 inline void
1836 Tree<RootNodeType>::combine2Extended(const Tree& a, const OtherTreeType& b,
1837  const ExtendedCombineOp& op, bool prune)
1838 {
1839  this->clearAllAccessors();
1840  mRoot.template combine2<const ExtendedCombineOp>(a.root(), b.root(), op, prune);
1841 }
1842 #endif
1843 
1844 
1845 ////////////////////////////////////////
1846 
1847 
1848 template<typename RootNodeType>
1849 template<typename VisitorOp>
1850 inline void
1852 {
1853  this->clearAllAccessors();
1854  mRoot.template visit<VisitorOp>(op);
1855 }
1856 
1857 
1858 template<typename RootNodeType>
1859 template<typename VisitorOp>
1860 inline void
1861 Tree<RootNodeType>::visit(VisitorOp& op) const
1862 {
1863  mRoot.template visit<VisitorOp>(op);
1864 }
1865 
1866 
1867 /// @internal This overload is needed (for ICC and GCC, but not for VC) to disambiguate
1868 /// code like this: <tt>tree.visit(MyVisitorOp(...))</tt>.
1869 template<typename RootNodeType>
1870 template<typename VisitorOp>
1871 inline void
1872 Tree<RootNodeType>::visit(const VisitorOp& op)
1873 {
1874  this->clearAllAccessors();
1875  mRoot.template visit<const VisitorOp>(op);
1876 }
1877 
1878 
1879 /// @internal This overload is needed (for ICC and GCC, but not for VC) to disambiguate
1880 /// code like this: <tt>tree.visit(MyVisitorOp(...))</tt>.
1881 template<typename RootNodeType>
1882 template<typename VisitorOp>
1883 inline void
1884 Tree<RootNodeType>::visit(const VisitorOp& op) const
1885 {
1886  mRoot.template visit<const VisitorOp>(op);
1887 }
1888 
1889 
1890 ////////////////////////////////////////
1891 
1892 
1893 template<typename RootNodeType>
1894 template<typename OtherTreeType, typename VisitorOp>
1895 inline void
1896 Tree<RootNodeType>::visit2(OtherTreeType& other, VisitorOp& op)
1897 {
1898  this->clearAllAccessors();
1899  using OtherRootNodeType = typename OtherTreeType::RootNodeType;
1900  mRoot.template visit2<OtherRootNodeType, VisitorOp>(other.root(), op);
1901 }
1902 
1903 
1904 template<typename RootNodeType>
1905 template<typename OtherTreeType, typename VisitorOp>
1906 inline void
1907 Tree<RootNodeType>::visit2(OtherTreeType& other, VisitorOp& op) const
1908 {
1909  using OtherRootNodeType = typename OtherTreeType::RootNodeType;
1910  mRoot.template visit2<OtherRootNodeType, VisitorOp>(other.root(), op);
1911 }
1912 
1913 
1914 /// @internal This overload is needed (for ICC and GCC, but not for VC) to disambiguate
1915 /// code like this: <tt>aTree.visit2(bTree, MyVisitorOp(...))</tt>.
1916 template<typename RootNodeType>
1917 template<typename OtherTreeType, typename VisitorOp>
1918 inline void
1919 Tree<RootNodeType>::visit2(OtherTreeType& other, const VisitorOp& op)
1920 {
1921  this->clearAllAccessors();
1922  using OtherRootNodeType = typename OtherTreeType::RootNodeType;
1923  mRoot.template visit2<OtherRootNodeType, const VisitorOp>(other.root(), op);
1924 }
1925 
1926 
1927 /// @internal This overload is needed (for ICC and GCC, but not for VC) to disambiguate
1928 /// code like this: <tt>aTree.visit2(bTree, MyVisitorOp(...))</tt>.
1929 template<typename RootNodeType>
1930 template<typename OtherTreeType, typename VisitorOp>
1931 inline void
1932 Tree<RootNodeType>::visit2(OtherTreeType& other, const VisitorOp& op) const
1933 {
1934  using OtherRootNodeType = typename OtherTreeType::RootNodeType;
1935  mRoot.template visit2<OtherRootNodeType, const VisitorOp>(other.root(), op);
1936 }
1937 
1938 
1939 ////////////////////////////////////////
1940 
1941 
1942 template<typename RootNodeType>
1943 inline const Name&
1945 {
1946  static std::once_flag once;
1947  std::call_once(once, []()
1948  {
1949  std::vector<Index> dims;
1950  Tree::getNodeLog2Dims(dims);
1951  std::ostringstream ostr;
1952  ostr << "Tree_" << typeNameAsString<BuildType>();
1953  for (size_t i = 1, N = dims.size(); i < N; ++i) { // start from 1 to skip the RootNode
1954  ostr << "_" << dims[i];
1955  }
1956  sTreeTypeName.reset(new Name(ostr.str()));
1957  });
1958  return *sTreeTypeName;
1959 }
1960 
1961 
1962 template<typename RootNodeType>
1963 template<typename OtherRootNodeType>
1964 inline bool
1966 {
1967  return mRoot.hasSameTopology(other.root());
1968 }
1969 
1970 
1971 template<typename RootNodeType>
1972 Index64
1974 {
1975  Coord dim(0, 0, 0);
1976  this->evalActiveVoxelDim(dim);
1977  const Index64
1978  totalVoxels = dim.x() * dim.y() * dim.z(),
1979  activeVoxels = this->activeVoxelCount();
1980  assert(totalVoxels >= activeVoxels);
1981  return totalVoxels - activeVoxels;
1982 }
1983 
1984 
1985 template<typename RootNodeType>
1986 inline bool
1988 {
1989  bbox.reset(); // default invalid bbox
1990 
1991  if (this->empty()) return false; // empty
1992 
1993  mRoot.evalActiveBoundingBox(bbox, false);
1994 
1995  return !bbox.empty();
1996 }
1997 
1998 template<typename RootNodeType>
1999 inline bool
2001 {
2002  bbox.reset(); // default invalid bbox
2003 
2004  if (this->empty()) return false; // empty
2005 
2006  mRoot.evalActiveBoundingBox(bbox, true);
2007 
2008  return !bbox.empty();
2009 }
2010 
2011 
2012 template<typename RootNodeType>
2013 inline bool
2015 {
2016  CoordBBox bbox;
2017  bool notEmpty = this->evalActiveVoxelBoundingBox(bbox);
2018  dim = bbox.extents();
2019  return notEmpty;
2020 }
2021 
2022 
2023 template<typename RootNodeType>
2024 inline bool
2026 {
2027  CoordBBox bbox;
2028  bool notEmpty = this->evalLeafBoundingBox(bbox);
2029  dim = bbox.extents();
2030  return notEmpty;
2031 }
2032 
2033 
2034 template<typename RootNodeType>
2035 inline void
2037 {
2038  /// @todo optimize
2039  minVal = maxVal = zeroVal<ValueType>();
2040  if (ValueOnCIter iter = this->cbeginValueOn()) {
2041  minVal = maxVal = *iter;
2042  for (++iter; iter; ++iter) {
2043  const ValueType& val = *iter;
2044  if (math::cwiseLessThan(val, minVal)) minVal = val;
2045  if (math::cwiseGreaterThan(val, maxVal)) maxVal = val;
2046  }
2047  }
2048 }
2049 
2050 
2051 template<typename RootNodeType>
2052 inline void
2053 Tree<RootNodeType>::getNodeLog2Dims(std::vector<Index>& dims)
2054 {
2055  dims.clear();
2056  RootNodeType::getNodeLog2Dims(dims);
2057 }
2058 
2059 
2060 template<typename RootNodeType>
2061 inline void
2062 Tree<RootNodeType>::print(std::ostream& os, int verboseLevel) const
2063 {
2064  if (verboseLevel <= 0) return;
2065 
2066  /// @todo Consider using hboost::io::ios_precision_saver instead.
2067  struct OnExit {
2068  std::ostream& os;
2069  std::streamsize savedPrecision;
2070  OnExit(std::ostream& _os): os(_os), savedPrecision(os.precision()) {}
2071  ~OnExit() { os.precision(savedPrecision); }
2072  };
2073  OnExit restorePrecision(os);
2074 
2075  std::vector<Index> dims;
2076  Tree::getNodeLog2Dims(dims);// leaf is the last element
2077 
2078  os << "Information about Tree:\n"
2079  << " Type: " << this->type() << "\n";
2080 
2081  os << " Configuration:\n";
2082 
2083  if (verboseLevel <= 1) {
2084  // Print node types and sizes.
2085  os << " Root(" << mRoot.getTableSize() << ")";
2086  if (dims.size() > 1) {
2087  for (size_t i = 1, N = dims.size() - 1; i < N; ++i) {
2088  os << ", Internal(" << (1 << dims[i]) << "^3)";
2089  }
2090  os << ", Leaf(" << (1 << dims.back()) << "^3)\n";
2091  }
2092  os << " Background value: " << mRoot.background() << "\n";
2093  return;
2094  }
2095 
2096  // The following is tree information that is expensive to extract.
2097 
2098  ValueType minVal = zeroVal<ValueType>(), maxVal = zeroVal<ValueType>();
2099  if (verboseLevel > 3) {
2100  // This forces loading of all non-resident nodes.
2101  this->evalMinMax(minVal, maxVal);
2102  }
2103 
2104 #if OPENVDB_ABI_VERSION_NUMBER >= 7
2105  const auto nodeCount = this->nodeCount();//fast
2106  const Index32 leafCount = nodeCount.front();// leaf is the first element
2107 #else
2108  std::vector<Index64> nodeCount(dims.size());
2109  for (NodeCIter it = cbeginNode(); it; ++it) ++(nodeCount[it.getDepth()]);//slow
2110  const Index64 leafCount = *nodeCount.rbegin();// leaf is the last element
2111 #endif
2112  assert(dims.size() == nodeCount.size());
2113 
2114  Index64 totalNodeCount = 0;
2115  for (size_t i = 0; i < nodeCount.size(); ++i) totalNodeCount += nodeCount[i];
2116 
2117  // Print node types, counts and sizes.
2118  os << " Root(1 x " << mRoot.getTableSize() << ")";
2119  if (dims.size() >= 2) {
2120  for (size_t i = 1, N = dims.size() - 1; i < N; ++i) {
2121 #if OPENVDB_ABI_VERSION_NUMBER >= 7
2122  os << ", Internal(" << util::formattedInt(nodeCount[N - i]);
2123 #else
2124  os << ", Internal(" << util::formattedInt(nodeCount[i]);
2125 #endif
2126  os << " x " << (1 << dims[i]) << "^3)";
2127  }
2128  os << ", Leaf(" << util::formattedInt(leafCount);
2129  os << " x " << (1 << dims.back()) << "^3)\n";
2130  }
2131  os << " Background value: " << mRoot.background() << "\n";
2132 
2133  // Statistics of topology and values
2134 
2135  if (verboseLevel > 3) {
2136  os << " Min value: " << minVal << "\n";
2137  os << " Max value: " << maxVal << "\n";
2138  }
2139 
2140  const Index64
2141  numActiveVoxels = this->activeVoxelCount(),
2142  numActiveLeafVoxels = this->activeLeafVoxelCount(),
2143  numActiveTiles = this->activeTileCount();
2144 
2145  os << " Number of active voxels: " << util::formattedInt(numActiveVoxels) << "\n";
2146  os << " Number of active tiles: " << util::formattedInt(numActiveTiles) << "\n";
2147 
2148  Coord dim(0, 0, 0);
2149  Index64 totalVoxels = 0;
2150  if (numActiveVoxels) { // nonempty
2151  CoordBBox bbox;
2152  this->evalActiveVoxelBoundingBox(bbox);
2153  dim = bbox.extents();
2154  totalVoxels = dim.x() * uint64_t(dim.y()) * dim.z();
2155 
2156  os << " Bounding box of active voxels: " << bbox << "\n";
2157  os << " Dimensions of active voxels: "
2158  << dim[0] << " x " << dim[1] << " x " << dim[2] << "\n";
2159 
2160  const double activeRatio = (100.0 * double(numActiveVoxels)) / double(totalVoxels);
2161  os << " Percentage of active voxels: " << std::setprecision(3) << activeRatio << "%\n";
2162 
2163  if (leafCount > 0) {
2164  const double fillRatio = (100.0 * double(numActiveLeafVoxels))
2165  / (double(leafCount) * double(LeafNodeType::NUM_VOXELS));
2166  os << " Average leaf node fill ratio: " << fillRatio << "%\n";
2167  }
2168 
2169  if (verboseLevel > 2) {
2170  Index64 sum = 0;// count the number of unallocated leaf nodes
2171  for (auto it = this->cbeginLeaf(); it; ++it) if (!it->isAllocated()) ++sum;
2172  os << " Number of unallocated nodes: "
2173  << util::formattedInt(sum) << " ("
2174  << (100.0 * double(sum) / double(totalNodeCount)) << "%)\n";
2175  }
2176  } else {
2177  os << " Tree is empty!\n";
2178  }
2179  os << std::flush;
2180 
2181  if (verboseLevel == 2) return;
2182 
2183  // Memory footprint in bytes
2184  const Index64
2185  actualMem = this->memUsage(),
2186  denseMem = sizeof(ValueType) * totalVoxels,
2187  voxelsMem = sizeof(ValueType) * numActiveLeafVoxels;
2188  ///< @todo not accurate for BoolTree (and probably should count tile values)
2189 
2190  os << "Memory footprint:\n";
2191  util::printBytes(os, actualMem, " Actual: ");
2192  util::printBytes(os, voxelsMem, " Active leaf voxels: ");
2193 
2194  if (numActiveVoxels) {
2195  util::printBytes(os, denseMem, " Dense equivalent: ");
2196  os << " Actual footprint is " << (100.0 * double(actualMem) / double(denseMem))
2197  << "% of an equivalent dense volume\n";
2198  os << " Leaf voxel footprint is " << (100.0 * double(voxelsMem) / double(actualMem))
2199  << "% of actual footprint\n";
2200  }
2201 }
2202 
2203 } // namespace tree
2204 } // namespace OPENVDB_VERSION_NAME
2205 } // namespace openvdb
2206 
2207 #endif // OPENVDB_TREE_TREE_HAS_BEEN_INCLUDED
virtual Index64 activeTileCount() const =0
Return the total number of active tiles.
const AValueType & result() const
Get the output value.
Definition: Types.h:473
tbb::concurrent_hash_map< ValueAccessorBase< Tree, true > *, bool > AccessorRegistry
Definition: Tree.h:1051
TreeValueIteratorBase< const Tree, typename RootNodeType::ValueOnCIter > ValueOnCIter
Definition: Tree.h:1019
virtual void readTopology(std::istream &, bool saveFloatAsHalf=false)
Read the tree topology from a stream.
Definition: Tree.h:1119
ValueOffIter beginValueOff()
Return an iterator over inactive values (tile and voxel) across all nodes.
Definition: Tree.h:1037
void releaseAccessor(ValueAccessorBase< Tree, true > &) const
Deregister an accessor so that it is no longer automatically cleared.
Definition: Tree.h:1357
bool operator==(const Tree &) const
Definition: Tree.h:273
vint4 max(const vint4 &a, const vint4 &b)
Definition: simd.h:4703
void visit2(OtherTreeType &other, VisitorOp &op)
Definition: Tree.h:1896
virtual void writeTopology(std::ostream &, bool saveFloatAsHalf=false) const
Write the tree topology to a stream.
Definition: Tree.h:1128
void parallel_for(int64_t start, int64_t end, std::function< void(int64_t index)> &&task, parallel_options opt=parallel_options(0, Split_Y, 1))
Definition: parallel.h:153
bool hasSameTopology(const Tree< OtherRootNodeType > &other) const
Return true if the given tree has the same node and active value topology as this tree...
Definition: Tree.h:1965
GLenum GLint * range
Definition: glew.h:3500
Definition: ImfName.h:54
void writeBuffers(std::ostream &, bool saveFloatAsHalf=false) const override
Write out all data buffers for this tree.
Definition: Tree.h:1306
This struct collects both input and output arguments to "grid combiner" functors used with the tree::...
Definition: Types.h:428
Index64 inactiveVoxelCount() const override
Return the number of inactive voxels within the bounding box of all active voxels.
Definition: Tree.h:1973
void setValueOn(const Coord &xyz)
Mark the voxel at the given coordinates as active but don't change its value.
Definition: Tree.h:1488
#define OPENVDB_LOG_WARN(mesg)
Definition: logging.h:274
bool cwiseGreaterThan(const Mat< SIZE, T > &m0, const Mat< SIZE, T > &m1)
Definition: Mat.h:1044
virtual Index64 memUsage() const
Return the total amount of memory in bytes occupied by this tree.
Definition: Tree.h:131
LeafIteratorBase< Tree, typename RootNodeType::ChildOnIter > LeafIter
Iterator over all leaf nodes in this tree.
Definition: Tree.h:998
hboost::math::policies::policy< hboost::math::policies::domain_error< hboost::math::policies::ignore_error >, hboost::math::policies::pole_error< hboost::math::policies::ignore_error >, hboost::math::policies::overflow_error< hboost::math::policies::ignore_error >, hboost::math::policies::underflow_error< hboost::math::policies::ignore_error >, hboost::math::policies::denorm_error< hboost::math::policies::ignore_error >, hboost::math::policies::rounding_error< hboost::math::policies::ignore_error >, hboost::math::policies::evaluation_error< hboost::math::policies::ignore_error >, hboost::math::policies::indeterminate_result_error< hboost::math::policies::ignore_error > > policy
Definition: SYS_MathCbrt.h:35
void fill(const CoordBBox &bbox, const ValueType &value, bool active=true)
Set all voxels within a given axis-aligned box to a constant value.
Definition: Tree.h:476
void stealNodes(ArrayT &array, const ValueType &value, bool state)
Definition: Tree.h:607
LeafCIter cbeginLeaf() const
Return an iterator over all leaf nodes in this tree.
Definition: Tree.h:1013
void modifyValueAndActiveState(const Coord &xyz, const ModifyOp &op)
Apply a functor to the voxel at the given coordinates.
Definition: Tree.h:1514
void voxelizeActiveTiles(bool threaded=true)
Densify active tiles, i.e., replace them with leaf-level active voxels.
Definition: Tree.h:1657
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: Tree.h:1505
virtual Metadata::Ptr getBackgroundValue() const
Return this tree's background value wrapped as metadata.
Definition: Tree.h:60
void attachAccessor(ValueAccessorBase< Tree, false > &) const
Dummy implementations.
Definition: Tree.h:635
static const Name & treeType()
Return the name of this type of tree.
Definition: Tree.h:1944
TreeValueIteratorBase< Tree, typename RootNodeType::ValueOnIter > ValueOnIter
Definition: Tree.h:1018
FMT_CONSTEXPR auto begin(const C &c) -> decltype(c.begin())
Definition: format.h:251
const Args & args
Definition: printf.h:628
void addLeaf(LeafNodeType *leaf)
Add the given leaf node to this tree, creating a new branch if necessary. If a leaf node with the sam...
Definition: Tree.h:516
RootNodeType::ChildOffCIter cbeginRootTiles() const
Return an iterator over non-child entries of the root node's table.
Definition: Tree.h:978
static Metadata::Ptr createMetadata(const Name &typeName)
Create new metadata of the given type.
void stealNodes(ArrayT &array)
Steals all nodes of a certain type from the tree and adds them to a container with the following API:...
Definition: Tree.h:605
GLuint const GLfloat * val
Definition: glew.h:2794
void addTile(Index level, const Coord &xyz, const ValueType &value, bool active)
Add a tile containing voxel (x, y, z) at the specified tree level, creating a new branch if necessary...
Definition: Tree.h:1533
GLboolean GLboolean GLboolean GLboolean a
Definition: glew.h:9477
int getValueDepth(const Coord &xyz) const
Return the tree depth (0 = root) at which the value of voxel (x, y, z) resides.
Definition: Tree.h:1433
TreeIterTraits provides, for all tree iterators, a begin(tree) function that returns an iterator over...
Definition: Tree.h:1156
void sparseFill(const CoordBBox &bbox, const ValueType &value, bool active=true)
Set all voxels within a given axis-aligned box to a constant value.
Definition: Tree.h:1639
Base class for tree-traversal iterators over all nodes.
Definition: TreeIterator.h:935
ValueAllCIter beginValueAll() const
Return an iterator over all values (tile and voxel) across all nodes.
Definition: Tree.h:1026
DeallocateNodes(std::vector< NodeType * > &nodes)
Definition: Tree.h:1061
RootNodeType::ChildOffCIter beginRootTiles() const
Return an iterator over non-child entries of the root node's table.
Definition: Tree.h:977
NodeIteratorBase< Tree, typename RootNodeType::ChildOnIter > NodeIter
Iterator over all nodes in this tree.
Definition: Tree.h:992
Tree(const OtherTreeType &other, const ValueType &inactiveValue, const ValueType &activeValue, TopologyCopy)
Topology copy constructor from a tree of a different type.
Definition: Tree.h:230
RootNodeType::ChildAllCIter cbeginRootDense() const
Return an iterator over all entries of the root node's table.
Definition: Tree.h:985
RootNodeType & root()
Return this tree's root node.
Definition: Tree.h:278
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:167
const GLdouble * m
Definition: glew.h:9124
virtual Index64 activeVoxelCount() const =0
Return the total number of active voxels.
tree::TreeBase TreeBase
Definition: Grid.h:26
TreeValueIteratorBase< Tree, typename RootNodeType::ValueAllIter > ValueAllIter
Definition: Tree.h:1016
virtual Index32 leafCount() const =0
Return the number of leaf nodes.
TreeValueIteratorBase< const Tree, typename RootNodeType::ValueAllCIter > ValueAllCIter
Definition: Tree.h:1017
Tree(const ValueType &background)
Empty tree constructor.
Definition: Tree.h:258
bool isValueOff(const Coord &xyz) const
Return true if the value at the given coordinates is inactive.
Definition: Tree.h:450
Index32 unallocatedLeafCount() const override
Return the total number of unallocated leaf nodes residing in this tree.
Definition: Tree.h:1629
void readNonresidentBuffers() const override
Read all of this tree's data buffers that are not yet resident in memory (because delayed loading is ...
Definition: Tree.h:1295
void clip(const CoordBBox &)
Set all voxels that lie outside the given axis-aligned box to the background.
Definition: Tree.h:1606
Index64 memUsage() const override
Return the total amount of memory in bytes occupied by this tree.
Definition: Tree.h:365
ValueOffCIter cbeginValueOff() const
Return an iterator over inactive values (tile and voxel) across all nodes.
Definition: Tree.h:1039
RootNodeType::ChildOffIter beginRootTiles()
Return an iterator over non-child entries of the root node's table.
Definition: Tree.h:979
bool operator!=(const Tree &) const
Definition: Tree.h:274
void topologyIntersection(const Tree< OtherRootNodeType > &other)
Intersects this tree's set of active values with the active values of the other tree, whose ValueType may be different.
Definition: Tree.h:1713
void releaseAllAccessors()
Notify all registered accessors, by calling ValueAccessor::release(), that this tree is about to be d...
Definition: Tree.h:1391
virtual Index64 inactiveVoxelCount() const =0
Return the number of inactive voxels within the bounding box of all active voxels.
Tree4<T, N1, N2, N3>::Type is the type of a four-level tree (Root, Internal, Internal, Leaf) with value type T and internal and leaf node log dimensions N1, N2 and N3, respectively.
Definition: Tree.h:1100
bool evalActiveVoxelBoundingBox(CoordBBox &bbox) const override
Return in bbox the axis-aligned bounding box of all active voxels and tiles.
Definition: Tree.h:2000
ValueAllIter beginValueAll()
Return an iterator over all values (tile and voxel) across all nodes.
Definition: Tree.h:1025
Tree & operator=(const Tree &)=delete
void setValueOff(const Coord &xyz)
Mark the voxel at the given coordinates as inactive but don't change its value.
Definition: Tree.h:1441
std::shared_ptr< T > SharedPtr
Definition: Types.h:92
void clear()
Remove all tiles from this tree and all nodes other than the root node.
Definition: Tree.h:1314
void topologyDifference(const Tree< OtherRootNodeType > &other)
Difference this tree's set of active values with the active values of the other tree, whose ValueType may be different. So a resulting voxel will be active only if the original voxel is active in this tree and inactive in the other tree.
Definition: Tree.h:1722
SYS_FORCE_INLINE const_iterator end() const
const AValueType & a() const
Get the A input value.
Definition: Types.h:468
static bool isRegisteredType(const Name &typeName)
Return true if the given type is known by the metadata type registry.
RootNodeType::ChildOnCIter beginRootChildren() const
Return an iterator over children of the root node.
Definition: Tree.h:970
Name valueType() const override
Return the name of the type of a voxel's value (e.g., "float" or "vec3d")
Definition: Tree.h:266
Utility routines to output nicely-formatted numeric values.
void readBuffers(std::istream &, bool saveFloatAsHalf=false) override
Read all data buffers for this tree.
Definition: Tree.h:1277
const ValueType & background() const
Return this tree's background value.
Definition: Tree.h:660
tbb::concurrent_hash_map< ValueAccessorBase< const Tree, true > *, bool > ConstAccessorRegistry
Definition: Tree.h:1052
#define OPENVDB_API
Helper macros for defining library symbol visibility.
Definition: Platform.h:221
LeafCIter beginLeaf() const
Return an iterator over all leaf nodes in this tree.
Definition: Tree.h:1012
void getNodes(ArrayT &array)
Adds all nodes of a certain type to a container with the following API:
Definition: Tree.h:577
void operator()(CombineArgs< AValueT, BValueT > &args) const
Definition: Tree.h:1738
void print(std::ostream &os=std::cout, int verboseLevel=1) const override
Print statistics, memory usage and other information about this tree.
Definition: Tree.h:2062
void merge(Tree &other, MergePolicy=MERGE_ACTIVE_STATES)
Efficiently merge another tree into this tree using one of several schemes.
Definition: Tree.h:1686
Index64 activeTileCount() const override
Return the total number of active tiles.
Definition: Tree.h:360
Templated metadata class to hold specific types.
Definition: Metadata.h:121
LeafIter beginLeaf()
Return an iterator over all leaf nodes in this tree.
Definition: Tree.h:1011
General-purpose arithmetic and comparison routines, most of which accept arbitrary value types (or at...
TreeBase::Ptr copy() const override
Return a pointer to a deep copy of this tree.
Definition: Tree.h:263
Index64 activeLeafVoxelCount() const override
Return the number of active voxels stored in leaf nodes.
Definition: Tree.h:352
void releaseAccessor(ValueAccessorBase< Tree, false > &) const
Dummy implementations.
Definition: Tree.h:647
LeafIteratorBase< const Tree, typename RootNodeType::ChildOnCIter > LeafCIter
Iterator over all leaf nodes in this tree.
Definition: Tree.h:999
Index32 leafCount() const override
Return the number of leaf nodes.
Definition: Tree.h:337
void attachAccessor(ValueAccessorBase< Tree, true > &) const
Register an accessor for this tree. Registered accessors are automatically cleared whenever one of th...
Definition: Tree.h:1339
NodeCIter beginNode() const
Return an iterator over all nodes in this tree.
Definition: Tree.h:1005
bool evalActiveVoxelDim(Coord &dim) const override
Return in dim the dimensions of the axis-aligned bounding box of all active voxels. This is a tighter bounding box than the leaf node bounding box.
Definition: Tree.h:2014
ValueConverter<T>::Type is the type of a tree having the same hierarchy as this tree but a different ...
Definition: Tree.h:194
GLsizei n
Definition: glew.h:4040
const RootNodeType & root() const
Return this tree's root node.
Definition: Tree.h:279
Tree3<T, N1, N2>::Type is the type of a three-level tree (Root, Internal, Leaf) with value type T and...
Definition: Tree.h:1090
bool cwiseLessThan(const Mat< SIZE, T > &m0, const Mat< SIZE, T > &m1)
Definition: Mat.h:1030
Helper class to adapt a three-argument (a, b, result) CombineOp functor into a single-argument functo...
Definition: Tree.h:1734
bool probeValue(const Coord &xyz, ValueType &value) const
Get the value of the voxel at the given coordinates.
Definition: Tree.h:1522
ValueOffCIter beginValueOff() const
Return an iterator over inactive values (tile and voxel) across all nodes.
Definition: Tree.h:1038
const NodeType * probeConstNode(const Coord &xyz) const
Return a pointer to the node of type NodeType that contains voxel (x, y, z). If no such node exists...
Definition: Tree.h:1595
Index64 inactiveLeafVoxelCount() const override
Return the number of inactive voxels stored in leaf nodes.
Definition: Tree.h:354
const LeafNodeType * probeLeaf(const Coord &xyz) const
Return a pointer to the leaf node that contains voxel (x, y, z). If no such node exists, return nullptr.
Definition: Tree.h:551
void attachAccessor(ValueAccessorBase< const Tree, false > &) const
Dummy implementations.
Definition: Tree.h:636
void operator()(const tbb::blocked_range< size_t > &range) const
Definition: Tree.h:1063
This base class for ValueAccessors manages registration of an accessor with a tree so that the tree c...
Definition: ValueAccessor.h:84
void getNodes(ArrayT &array) const
Adds all nodes of a certain type to a container with the following API:
Definition: Tree.h:578
bool evalLeafBoundingBox(CoordBBox &bbox) const override
Return in bbox the axis-aligned bounding box of all active tiles and leaf nodes with active values...
Definition: Tree.h:1987
typename RootNodeType::ValueType ValueType
Definition: Tree.h:181
NodeIteratorBase< const Tree, typename RootNodeType::ChildOnCIter > NodeCIter
Iterator over all nodes in this tree.
Definition: Tree.h:993
TreeValueIteratorBase< Tree, typename RootNodeType::ValueOffIter > ValueOffIter
Definition: Tree.h:1020
The root node of an OpenVDB tree.
bool hasActiveTiles() const
Return true if this tree has any active tiles.
Definition: Tree.h:452
void releaseAccessor(ValueAccessorBase< const Tree, false > &) const
Dummy implementations.
Definition: Tree.h:648
void evalMinMax(ValueType &min, ValueType &max) const
Return the minimum and maximum active values in this tree.
Definition: Tree.h:2036
CIterT cbegin() const
Return a const iterator of type CIterT (for example, cbegin<ValueOnCIter>() is equivalent to cbeginVa...
ValueAllCIter cbeginValueAll() const
Return an iterator over all values (tile and voxel) across all nodes.
Definition: Tree.h:1027
Base class for typed trees.
Definition: Tree.h:35
GLdouble GLdouble GLdouble b
Definition: glew.h:9122
void topologyUnion(const Tree< OtherRootNodeType > &other)
Union this tree's set of active values with the active values of the other tree, whose ValueType may ...
Definition: Tree.h:1704
Index64 activeVoxelCount() const override
Return the total number of active voxels.
Definition: Tree.h:356
Index32 nonLeafCount() const override
Return the number of non-leaf nodes.
Definition: Tree.h:350
virtual Index32 nonLeafCount() const =0
Return the number of non-leaf nodes.
TreeValueIteratorBase< const Tree, typename RootNodeType::ValueOffCIter > ValueOffCIter
Definition: Tree.h:1021
static void getNodeLog2Dims(std::vector< Index > &dims)
Traverse the type hierarchy of nodes, and return, in dims, a list of the Log2Dims of nodes in order f...
Definition: Tree.h:2053
const ValueType & getValue(const Coord &xyz) const
Return the value of the voxel at the given coordinates.
Definition: Tree.h:1416
typename RootNodeType::LeafNodeType LeafNodeType
Definition: Tree.h:183
Tree(const Tree &other)
Deep copy constructor.
Definition: Tree.h:204
ConstAccessorRegistry mConstAccessorRegistry
Definition: Tree.h:1076
ValueOnCIter beginValueOn() const
Return an iterator over active values (tile and voxel) across all nodes.
Definition: Tree.h:1032
void readTopology(std::istream &, bool saveFloatAsHalf=false) override
Read the tree topology from a stream.
Definition: Tree.h:1258
virtual void print(std::ostream &os=std::cout, int verboseLevel=1) const
Print statistics, memory usage and other information about this tree.
Definition: Tree.h:1136
typename RootNodeType::BuildType BuildType
Definition: Tree.h:182
const LeafNodeType * probeConstLeaf(const Coord &xyz) const
Return a pointer to the leaf node that contains voxel (x, y, z). If no such node exists, return nullptr.
Definition: Tree.h:1568
OIIO_API bool copy(string_view from, string_view to, std::string &err)
ValueOnIter beginValueOn()
Return an iterator over active values (tile and voxel) across all nodes.
Definition: Tree.h:1031
Base class for tree-traversal iterators over tile and voxel values.
Definition: TreeIterator.h:616
void setValue(const Coord &xyz, const ValueType &value)
Set the value of the voxel at the given coordinates and mark the voxel as active. ...
Definition: Tree.h:1465
RootNodeType::ChildAllIter beginRootDense()
Return an iterator over all entries of the root node's table.
Definition: Tree.h:986
NodeCIter cbeginNode() const
Return an iterator over all nodes in this tree.
Definition: Tree.h:1006
RootNodeType::ChildOnIter beginRootChildren()
Return an iterator over children of the root node.
Definition: Tree.h:972
NodeT * stealNode(const Coord &xyz, const ValueType &value, bool active)
Return a pointer to the node of type NodeT that contains voxel (x, y, z) and replace it with a tile o...
Definition: Tree.h:1543
void getIndexRange(CoordBBox &bbox) const override
Min and max are both inclusive.
Definition: Tree.h:663
GA_API const UT_StringHolder N
FormattedInt< IntT > formattedInt(IntT n)
Definition: Formats.h:118
SharedPtr< const TreeBase > ConstPtr
Definition: Tree.h:39
static std::unique_ptr< const Name > sTreeTypeName
Definition: Tree.h:1078
void combine2Extended(const Tree &a, const OtherTreeType &b, ExtendedCombineOp &op, bool prune=false)
Definition: Tree.h:1821
RootNodeType::ChildOnCIter cbeginRootChildren() const
Return an iterator over children of the root node.
Definition: Tree.h:971
LeafNodeType * probeLeaf(const Coord &xyz)
Return a pointer to the leaf node that contains voxel (x, y, z). If no such node exists, return nullptr.
Definition: Tree.h:1560
void combine2(const Tree &a, const OtherTreeType &b, CombineOp &op, bool prune=false)
Definition: Tree.h:1797
GLuint64EXT * result
Definition: glew.h:14007
OPENVDB_API int printBytes(std::ostream &os, uint64_t bytes, const std::string &head="", const std::string &tail="\n", bool exact=false, int width=8, int precision=3)
NodeType * probeNode(const Coord &xyz)
Return a pointer to the node of type NodeType that contains voxel (x, y, z). If no such node exists...
Definition: Tree.h:1577
const Name & type() const override
Return the name of this type of tree.
Definition: Tree.h:271
GLenum array
Definition: glew.h:9066
void clipUnallocatedNodes() override
Replace with background tiles any nodes whose voxel buffers have not yet been allocated.
Definition: Tree.h:1615
RootNodeType::ChildAllCIter beginRootDense() const
Return an iterator over all entries of the root node's table.
Definition: Tree.h:984
Tag dispatch class that distinguishes topology copy constructors from deep copy constructors.
Definition: Types.h:542
const BValueType & b() const
Get the B input value.
Definition: Types.h:470
ValueOnCIter cbeginValueOn() const
Return an iterator over active values (tile and voxel) across all nodes.
Definition: Tree.h:1033
virtual const Name & type() const =0
Return the name of this tree's type.
void prune(TreeT &tree, typename TreeT::ValueType tolerance=zeroVal< typename TreeT::ValueType >(), bool threaded=true, size_t grainSize=1)
Reduce the memory footprint of a tree by replacing with tiles any nodes whose values are all the same...
Definition: Prune.h:334
Metadata::Ptr getBackgroundValue() const override
Return this tree's background value wrapped as metadata.
Definition: Tree.h:1666
NodeIter beginNode()
Return an iterator over all nodes in this tree.
Definition: Tree.h:1004
vint4 min(const vint4 &a, const vint4 &b)
Definition: simd.h:4694
bool isValueOn(const Coord &xyz) const
Return true if the value at the given coordinates is active.
Definition: Tree.h:448
bool evalLeafDim(Coord &dim) const override
Return in dim the dimensions of the axis-aligned bounding box of all leaf nodes.
Definition: Tree.h:2025
IterT begin()
Return an iterator of type IterT (for example, begin<ValueOnIter>() is equivalent to beginValueOn())...
Definition: Tree.h:1238
void prune(const ValueType &tolerance=zeroVal< ValueType >())
Reduce the memory footprint of this tree by replacing with tiles any nodes whose values are all the s...
Definition: Tree.h:505
LeafNodeType * touchLeaf(const Coord &xyz)
Return a pointer to the leaf node that contains voxel (x, y, z). If no such node exists, create one that preserves the values and active states of all voxels.
Definition: Tree.h:1552
Tree(const OtherTreeType &other, const ValueType &background, TopologyCopy)
Topology copy constructor from a tree of a different type.
Definition: Tree.h:251
bool empty() const
Return true if this tree contains no nodes other than the root node and no tiles other than backgroun...
Definition: Tree.h:618
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: Tree.h:1457
void denseFill(const CoordBBox &bbox, const ValueType &value, bool active=true)
Set all voxels within a given axis-aligned box to a constant value and ensure that those voxels are a...
Definition: Tree.h:1648
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h:113
GLsizei const GLfloat * value
Definition: glew.h:1849
void visitActiveBBox(BBoxOp &op) const
Definition: Tree.h:934
GLint level
Definition: glew.h:1252
void setValueOnly(const Coord &xyz, const ValueType &value)
Set the value of the voxel at the given coordinates but don't change its active state.
Definition: Tree.h:1472
Tree5<T, N1, N2, N3, N4>::Type is the type of a five-level tree (Root, Internal, Internal, Internal, Leaf) with value type T and internal and leaf node log dimensions N1, N2, N3 and N4, respectively.
Definition: Tree.h:1109
void combine(Tree &other, CombineOp &op, bool prune=false)
Definition: Tree.h:1749
Internal table nodes for OpenVDB trees.
Index treeDepth() const override
Return the depth of this tree.
Definition: Tree.h:335
#define OPENVDB_THROW(exception, message)
Definition: Exceptions.h:82
void combineExtended(Tree &other, ExtendedCombineOp &op, bool prune=false)
Definition: Tree.h:1773
void clearAllAccessors()
Clear all registered accessors.
Definition: Tree.h:1373
type
Definition: core.h:528
void writeTopology(std::ostream &, bool saveFloatAsHalf=false) const override
Write the tree topology to a stream.
Definition: Tree.h:1268
Base class for tree-traversal iterators over all leaf nodes (but not leaf voxels) ...
Tree(const Tree< OtherRootType > &other)
Value conversion deep copy constructor.
Definition: Tree.h:215
std::enable_if< internal::is_string< S >::value >::type print(std::basic_ostream< FMT_CHAR(S)> &os, const S &format_str, const Args &...args)
Definition: ostream.h:146