OpenVDB 13.0.1
Loading...
Searching...
No Matches
InternalNode.h
Go to the documentation of this file.
1// Copyright Contributors to the OpenVDB Project
2// SPDX-License-Identifier: Apache-2.0
3//
4/// @file InternalNode.h
5///
6/// @brief Internal table nodes for OpenVDB trees
7
8#ifndef OPENVDB_TREE_INTERNALNODE_HAS_BEEN_INCLUDED
9#define OPENVDB_TREE_INTERNALNODE_HAS_BEEN_INCLUDED
10
11#include <openvdb/Platform.h>
13#include <openvdb/util/Assert.h>
14#include <openvdb/io/Compression.h> // for io::readCompressedValues(), etc.
15#include <openvdb/math/Math.h> // for math::isExactlyEqual(), etc.
16#include <openvdb/version.h>
17#include <openvdb/Types.h>
18#include "Iterator.h"
19#include "NodeUnion.h"
20#include <tbb/parallel_for.h>
21#include <memory>
22#include <type_traits>
23
24
25namespace openvdb {
27namespace OPENVDB_VERSION_NAME {
28namespace tree {
29
30template<typename, Index, typename> struct SameInternalConfig; // forward declaration
31
32
33template<typename _ChildNodeType, Index Log2Dim>
35{
36public:
37 using ChildNodeType = _ChildNodeType;
38 using LeafNodeType = typename ChildNodeType::LeafNodeType;
39 using ValueType = typename ChildNodeType::ValueType;
40 using BuildType = typename ChildNodeType::BuildType;
43
44 static const Index
45 LOG2DIM = Log2Dim, // log2 of tile count in one dimension
46 TOTAL = Log2Dim + ChildNodeType::TOTAL, // log2 of voxel count in one dimension
47 DIM = 1 << TOTAL, // total voxel count in one dimension
48 NUM_VALUES = 1 << (3 * Log2Dim), // total voxel count represented by this node
49 LEVEL = 1 + ChildNodeType::LEVEL; // level 0 = leaf
50 static const Index64
51 NUM_VOXELS = uint64_t(1) << (3 * TOTAL); // total voxel count represented by this node
52
53 /// @brief ValueConverter<T>::Type is the type of an InternalNode having the same
54 /// child hierarchy and dimensions as this node but a different value type, T.
55 template<typename OtherValueType>
57 using Type = InternalNode<typename ChildNodeType::template ValueConverter<
58 OtherValueType>::Type, Log2Dim>;
59 };
60
61 /// @brief SameConfiguration<OtherNodeType>::value is @c true if and only if OtherNodeType
62 /// is the type of an InternalNode with the same dimensions as this node and whose
63 /// ChildNodeType has the same configuration as this node's ChildNodeType.
64 template<typename OtherNodeType>
69
70
71 /// @brief Default constructor
72 /// @warning The resulting InternalNode is uninitialized
74
75 /// @brief Constructor of an InternalNode with dense inactive tiles of the specified value.
76 /// @param offValue Background value used for inactive values
77 explicit InternalNode(const ValueType& offValue);
78
79 /// @brief Constructs an InternalNode with dense tiles
80 /// @param origin The location in index space of the fist tile value
81 /// @param fillValue Value assigned to all the tiles
82 /// @param active State assigned to all the tiles
83 InternalNode(const Coord& origin, const ValueType& fillValue, bool active = false);
84
85 InternalNode(PartialCreate, const Coord&, const ValueType& fillValue, bool active = false);
86
87 /// @brief Deep copy constructor
88 ///
89 /// @note This method is multi-threaded!
91
92 /// @brief Value conversion copy constructor
93 ///
94 /// @note This method is multi-threaded!
95 template<typename OtherChildNodeType>
97
98 /// @brief Topology copy constructor
99 ///
100 /// @note This method is multi-threaded!
101 template<typename OtherChildNodeType>
103 const ValueType& background, TopologyCopy);
104
105 /// @brief Topology copy constructor
106 ///
107 /// @note This method is multi-threaded!
108 template<typename OtherChildNodeType>
110 const ValueType& offValue, const ValueType& onValue, TopologyCopy);
111
113
114protected:
118
119 // Type tags to disambiguate template instantiations
120 struct ValueOn {}; struct ValueOff {}; struct ValueAll {};
121 struct ChildOn {}; struct ChildOff {}; struct ChildAll {};
122
123 // The following class templates implement the iterator interfaces specified in Iterator.h
124 // by providing getItem(), setItem() and/or modifyItem() methods.
125
126 // Sparse iterator that visits child nodes of an InternalNode
127 template<typename NodeT, typename ChildT, typename MaskIterT, typename TagT>
129 MaskIterT, ChildIter<NodeT, ChildT, MaskIterT, TagT>, NodeT, ChildT>
130 {
132 ChildIter(const MaskIterT& iter, NodeT* parent): SparseIteratorBase<
133 MaskIterT, ChildIter<NodeT, ChildT, MaskIterT, TagT>, NodeT, ChildT>(iter, parent) {}
134
135 ChildT& getItem(Index pos) const
136 {
138 return *(this->parent().getChildNode(pos));
139 }
140
141 // Note: setItem() can't be called on const iterators.
142 void setItem(Index pos, const ChildT& c) const { this->parent().resetChildNode(pos, &c); }
143
144 // Note: modifyItem() isn't implemented, since it's not useful for child node pointers.
145 };// ChildIter
146
147 // Sparse iterator that visits tile values of an InternalNode
148 template<typename NodeT, typename ValueT, typename MaskIterT, typename TagT>
150 MaskIterT, ValueIter<NodeT, ValueT, MaskIterT, TagT>, NodeT, ValueT>
151 {
153 ValueIter(const MaskIterT& iter, NodeT* parent): SparseIteratorBase<
154 MaskIterT, ValueIter<NodeT, ValueT, MaskIterT, TagT>, NodeT, ValueT>(iter, parent) {}
155
156 const ValueT& getItem(Index pos) const { return this->parent().mNodes[pos].getValue(); }
157
158 // Note: setItem() can't be called on const iterators.
159 void setItem(Index pos, const ValueT& v) const { this->parent().mNodes[pos].setValue(v); }
160
161 // Note: modifyItem() can't be called on const iterators.
162 template<typename ModifyOp>
163 void modifyItem(Index pos, const ModifyOp& op) const
164 {
165 op(this->parent().mNodes[pos].getValue());
166 }
167 };// ValueIter
168
169 // Dense iterator that visits both tiles and child nodes of an InternalNode
170 template<typename NodeT, typename ChildT, typename ValueT, typename TagT>
172 MaskDenseIterator, DenseIter<NodeT, ChildT, ValueT, TagT>, NodeT, ChildT, ValueT>
173 {
176
180
181 bool getItem(Index pos, ChildT*& child, NonConstValueT& value) const
182 {
183 if (this->parent().isChildMaskOn(pos)) {
184 child = this->parent().getChildNode(pos);
185 return true;
186 }
187 child = nullptr;
188 value = this->parent().mNodes[pos].getValue();
189 return false;
190 }
191
192 // Note: setItem() can't be called on const iterators.
193 void setItem(Index pos, ChildT* child) const
194 {
195 this->parent().resetChildNode(pos, child);
196 }
197
198 // Note: unsetItem() can't be called on const iterators.
199 void unsetItem(Index pos, const ValueT& value) const
200 {
201 this->parent().unsetChildNode(pos, value);
202 }
203 };// DenseIter
204
205public:
206 // Iterators (see Iterator.h for usage)
207 using ChildOnIter = ChildIter<InternalNode, ChildNodeType, MaskOnIterator, ChildOn>;
208 using ChildOnCIter = ChildIter<const InternalNode,const ChildNodeType,MaskOnIterator,ChildOn>;
209 using ChildOffIter = ValueIter<InternalNode, const ValueType, MaskOffIterator, ChildOff>;
210 using ChildOffCIter = ValueIter<const InternalNode,const ValueType,MaskOffIterator,ChildOff>;
211 using ChildAllIter = DenseIter<InternalNode, ChildNodeType, ValueType, ChildAll>;
212 using ChildAllCIter = DenseIter<const InternalNode,const ChildNodeType, ValueType, ChildAll>;
213
214 using ValueOnIter = ValueIter<InternalNode, const ValueType, MaskOnIterator, ValueOn>;
215 using ValueOnCIter = ValueIter<const InternalNode, const ValueType, MaskOnIterator, ValueOn>;
216 using ValueOffIter = ValueIter<InternalNode, const ValueType, MaskOffIterator, ValueOff>;
217 using ValueOffCIter = ValueIter<const InternalNode,const ValueType,MaskOffIterator,ValueOff>;
218 using ValueAllIter = ValueIter<InternalNode, const ValueType, MaskOffIterator, ValueAll>;
219 using ValueAllCIter = ValueIter<const InternalNode,const ValueType,MaskOffIterator,ValueAll>;
220
221 ChildOnCIter cbeginChildOn() const { return ChildOnCIter(mChildMask.beginOn(), this); }
222 ChildOffCIter cbeginChildOff() const { return ChildOffCIter(mChildMask.beginOff(), this); }
223 ChildAllCIter cbeginChildAll() const { return ChildAllCIter(mChildMask.beginDense(), this); }
227 ChildOnIter beginChildOn() { return ChildOnIter(mChildMask.beginOn(), this); }
228 ChildOffIter beginChildOff() { return ChildOffIter(mChildMask.beginOff(), this); }
229 ChildAllIter beginChildAll() { return ChildAllIter(mChildMask.beginDense(), this); }
230
231 ValueOnCIter cbeginValueOn() const { return ValueOnCIter(mValueMask.beginOn(), this); }
232 /// @warning This iterator will also visit child nodes so use isChildMaskOn to skip them!
233 ValueOffCIter cbeginValueOff() const { return ValueOffCIter(mValueMask.beginOff(), this); }
234 ValueAllCIter cbeginValueAll() const { return ValueAllCIter(mChildMask.beginOff(), this); }
236 /// @warning This iterator will also visit child nodes so use isChildMaskOn to skip them!
239 ValueOnIter beginValueOn() { return ValueOnIter(mValueMask.beginOn(), this); }
240 /// @warning This iterator will also visit child nodes so use isChildMaskOn to skip them!
241 ValueOffIter beginValueOff() { return ValueOffIter(mValueMask.beginOff(), this); }
242 ValueAllIter beginValueAll() { return ValueAllIter(mChildMask.beginOff(), this); }
243
244
245 /// @return The dimension of this InternalNode
246 /// @details The number of voxels in one coordinate direction covered by this node
247 static Index dim() { return DIM; }
248 /// @return The level of this node
249 /// @details Level 0 is by definition the level of the leaf nodes
250 static Index getLevel() { return LEVEL; }
251 /// @brief Populated an std::vector with the dimension of all the
252 /// nodes in the branch starting with this node.
253 static void getNodeLog2Dims(std::vector<Index>& dims);
254 /// @return The dimension of the child nodes of this node.
255 /// @details The number of voxels in one coordinate direction
256 /// covered by a child node of this node.
257 static Index getChildDim() { return ChildNodeType::DIM; }
258
259 /// Return the linear table offset of the given global or local coordinates.
260 static Index coordToOffset(const Coord& xyz);
261 /// @brief Return the local coordinates for a linear table offset,
262 /// where offset 0 has coordinates (0, 0, 0).
263 static void offsetToLocalCoord(Index n, Coord& xyz);
264 /// Return the global coordinates for a linear table offset.
266
267 /// Return the grid index coordinates of this node's local origin.
268 const Coord& origin() const { return mOrigin; }
269 /// Set the grid index coordinates of this node's local origin.
270 void setOrigin(const Coord& origin) { mOrigin = origin; }
271
272 /// Return the transient data value.
274 /// Set the transient data value.
276
279 void nodeCount(std::vector<Index64> &vec) const;
280 OPENVDB_DEPRECATED_MESSAGE("Use input type std::vector<Index64> for nodeCount.")
281 void nodeCount(std::vector<Index32> &vec) const;
288
289 /// Return the total amount of memory in bytes occupied by this node and its children.
291
292 /// @brief Expand the specified bounding box so that it includes the active tiles
293 /// of this internal node as well as all the active values in its child nodes.
294 /// If visitVoxels is false LeafNodes will be approximated as dense, i.e. with all
295 /// voxels active. Else the individual active voxels are visited to produce a tight bbox.
296 void evalActiveBoundingBox(CoordBBox& bbox, bool visitVoxels = true) const;
297
298 /// @brief Return the bounding box of this node, i.e., the full index space
299 /// spanned by the node regardless of its content.
301
302 /// @return True if this node contains no child nodes.
303 bool isEmpty() const { return mChildMask.isOff(); }
304
305 /// Return @c true if all of this node's table entries have the same active state
306 /// and the same constant value to within the given tolerance,
307 /// and return that value in @a firstValue and the active state in @a state.
308 ///
309 /// @note This method also returns @c false if this node contains any child nodes.
310 bool isConstant(ValueType& firstValue, bool& state,
311 const ValueType& tolerance = zeroVal<ValueType>()) const;
312
313 /// Return @c true if all of this node's tables entries have
314 /// the same active @a state and the range of its values satisfy
315 /// (@a maxValue - @a minValue) <= @a tolerance.
316 ///
317 /// @param minValue Is updated with the minimum of all values IF method
318 /// returns @c true. Else the value is undefined!
319 /// @param maxValue Is updated with the maximum of all values IF method
320 /// returns @c true. Else the value is undefined!
321 /// @param state Is updated with the state of all values IF method
322 /// returns @c true. Else the value is undefined!
323 /// @param tolerance The tolerance used to determine if values are
324 /// approximately constant.
325 ///
326 /// @note This method also returns @c false if this node contains any child nodes.
327 bool isConstant(ValueType& minValue, ValueType& maxValue,
328 bool& state, const ValueType& tolerance = zeroVal<ValueType>()) const;
329
330 /// Return @c true if this node has no children and only contains inactive values.
331 bool isInactive() const { return this->isChildMaskOff() && this->isValueMaskOff(); }
332
333 /// Return @c true if the voxel at the given coordinates is active.
334 bool isValueOn(const Coord& xyz) const;
335 /// Return @c true if the voxel at the given offset is active.
336 bool isValueOn(Index offset) const { OPENVDB_ASSERT(offset < NUM_VALUES); return mValueMask.isOn(offset); }
337 /// Return @c true if the voxel at the given coordinates is inactive.
338 bool isValueOff(const Coord& xyz) const;
339 /// Return @c true if the voxel at the given offset is inactive.
340 bool isValueOff(Index offset) const { OPENVDB_ASSERT(offset < NUM_VALUES); return mValueMask.isOff(offset); }
341
342 /// Return @c true if this node or any of its child nodes have any active tiles.
343 bool hasActiveTiles() const;
344
345 const ValueType& getValue(const Coord& xyz) const;
346 bool probeValue(const Coord& xyz, ValueType& value) const;
347
348 /// @brief Return the level of the tree (0 = leaf) at which the value
349 /// at the given coordinates resides.
350 Index getValueLevel(const Coord& xyz) const;
351
352 /// @brief If the first entry in this node's table is a tile, return the tile's value.
353 /// Otherwise, return the result of calling getFirstValue() on the child.
354 const ValueType& getFirstValue() const;
355 /// @brief If the last entry in this node's table is a tile, return the tile's value.
356 /// Otherwise, return the result of calling getLastValue() on the child.
357 const ValueType& getLastValue() const;
358
359 /// Set the active state of the voxel at the given coordinates but don't change its value.
360 void setActiveState(const Coord& xyz, bool on);
361 /// Set the value of the voxel at the given coordinates but don't change its active state.
362 void setValueOnly(const Coord& xyz, const ValueType& value);
363 /// Mark the voxel at the given coordinates as active but don't change its value.
364 void setValueOn(const Coord& xyz);
365 /// Set the value of the voxel at the given coordinates and mark the voxel as active.
366 void setValueOn(const Coord& xyz, const ValueType& value);
367 /// Mark the voxel at the given coordinates as inactive but don't change its value.
368 void setValueOff(const Coord& xyz);
369 /// Set the value of the voxel at the given coordinates and mark the voxel as inactive.
370 void setValueOff(const Coord& xyz, const ValueType& value);
371
372 /// @brief Apply a functor to the value of the voxel at the given coordinates
373 /// and mark the voxel as active.
374 template<typename ModifyOp>
375 void modifyValue(const Coord& xyz, const ModifyOp& op);
376 /// Apply a functor to the voxel at the given coordinates.
377 template<typename ModifyOp>
378 void modifyValueAndActiveState(const Coord& xyz, const ModifyOp& op);
379
380 /// Return the value of the voxel at the given coordinates and, if necessary, update
381 /// the accessor with pointers to the nodes along the path from the root node to
382 /// the node containing the voxel.
383 /// @note Used internally by ValueAccessor.
384 template<typename AccessorT>
385 const ValueType& getValueAndCache(const Coord& xyz, AccessorT&) const;
386
387 /// Return @c true if the voxel at the given coordinates is active and, if necessary,
388 /// update the accessor with pointers to the nodes along the path from the root node
389 /// to the node containing the voxel.
390 /// @note Used internally by ValueAccessor.
391 template<typename AccessorT>
392 bool isValueOnAndCache(const Coord& xyz, AccessorT&) const;
393
394 /// Change the value of the voxel at the given coordinates and mark it as active.
395 /// If necessary, update the accessor with pointers to the nodes along the path
396 /// from the root node to the node containing the voxel.
397 /// @note Used internally by ValueAccessor.
398 template<typename AccessorT>
399 void setValueAndCache(const Coord& xyz, const ValueType& value, AccessorT&);
400
401 /// Set the value of the voxel at the given coordinate but preserves its active state.
402 /// If necessary, update the accessor with pointers to the nodes along the path
403 /// from the root node to the node containing the voxel.
404 /// @note Used internally by ValueAccessor.
405 template<typename AccessorT>
406 void setValueOnlyAndCache(const Coord& xyz, const ValueType& value, AccessorT&);
407
408 /// @brief Apply a functor to the value of the voxel at the given coordinates
409 /// and mark the voxel as active.
410 /// If necessary, update the accessor with pointers to the nodes along the path
411 /// from the root node to the node containing the voxel.
412 /// @note Used internally by ValueAccessor.
413 template<typename ModifyOp, typename AccessorT>
414 void modifyValueAndCache(const Coord& xyz, const ModifyOp& op, AccessorT&);
415
416 /// Apply a functor to the voxel at the given coordinates.
417 /// If necessary, update the accessor with pointers to the nodes along the path
418 /// from the root node to the node containing the voxel.
419 /// @note Used internally by ValueAccessor.
420 template<typename ModifyOp, typename AccessorT>
421 void modifyValueAndActiveStateAndCache(const Coord& xyz, const ModifyOp& op, AccessorT&);
422
423 /// Change the value of the voxel at the given coordinates and mark it as inactive.
424 /// If necessary, update the accessor with pointers to the nodes along the path
425 /// from the root node to the node containing the voxel.
426 /// @note Used internally by ValueAccessor.
427 template<typename AccessorT>
428 void setValueOffAndCache(const Coord& xyz, const ValueType& value, AccessorT&);
429
430 /// Set the active state of the voxel at the given coordinates without changing its value.
431 /// If necessary, update the accessor with pointers to the nodes along the path
432 /// from the root node to the node containing the voxel.
433 /// @note Used internally by ValueAccessor.
434 template<typename AccessorT>
435 void setActiveStateAndCache(const Coord& xyz, bool on, AccessorT&);
436
437 /// Return, in @a value, the value of the voxel at the given coordinates and,
438 /// if necessary, update the accessor with pointers to the nodes along
439 /// the path from the root node to the node containing the voxel.
440 /// @return @c true if the voxel at the given coordinates is active
441 /// @note Used internally by ValueAccessor.
442 template<typename AccessorT>
443 bool probeValueAndCache(const Coord& xyz, ValueType& value, AccessorT&) const;
444
445 /// @brief Return the level of the tree (0 = leaf) at which the value
446 /// at the given coordinates resides.
447 ///
448 /// If necessary, update the accessor with pointers to the nodes along the path
449 /// from the root node to the node containing the voxel.
450 /// @note Used internally by ValueAccessor.
451 template<typename AccessorT>
452 Index getValueLevelAndCache(const Coord& xyz, AccessorT&) const;
453
454 /// Mark all values (both tiles and voxels) as active.
456
457 //
458 // I/O
459 //
460 void writeTopology(std::ostream&, bool toHalf = false) const;
461 void readTopology(std::istream&, bool fromHalf = false);
462 void writeBuffers(std::ostream&, bool toHalf = false) const;
463 void readBuffers(std::istream&, bool fromHalf = false);
464 void readBuffers(std::istream&, const CoordBBox&, bool fromHalf = false);
465
466
467 //
468 // Unsafe methods
469 //
470 // WARNING: For improved performance, these unsafe methods do not check the value or
471 // child masks. If used incorrectly, at best they will leave the InternalNode in an
472 // invalid state and at worst cause the application to crash. Always use the safer
473 // alternative method(s) unless you really know what you're doing.
474 // Enabling OpenVDB asserts will catch where assumptions are incorrectly invalidated.
475
476 /// @brief Return the tile value at offset.
477 /// @note Use getValue(const Coord&) for a safer alternative.
478 /// @warning This method should only be used by experts seeking low-level optimizations.
479 const ValueType& getValueUnsafe(Index offset) const;
480 /// @brief Return the tile value and active state at offset.
481 /// @note Use probeValue(const Coord&, ValueType&) for a safer alternative.
482 /// @warning This method should only be used by experts seeking low-level optimizations.
483 bool getValueUnsafe(Index offset, ValueType& value) const;
484
485 /// @brief Return the child node at offset.
486 /// @note Use probeChild(const Coord&) for a safer alternative.
487 /// @warning This method should only be used by experts seeking low-level optimizations.
489 /// @brief Return the child node at offset.
490 /// @note Use probeConstChild(const Coord&) for a safer alternative.
491 /// @warning This method should only be used by experts seeking low-level optimizations.
493 /// @brief Return the child node at offset.
494 /// @note Use probeChild(const Coord&) for a safer alternative.
495 /// @warning This method should only be used by experts seeking low-level optimizations.
496 const ChildNodeType* getChildUnsafe(Index offset) const;
497
498 /// @brief Set the tile active state at offset but don't change its value.
499 /// @note Use setActiveState(const Coord&, bool) for a safer alternative.
500 /// @warning This method should only be used by experts seeking low-level optimizations.
501 void setActiveStateUnsafe(Index offset, bool on);
502 /// @brief Set the tile value at offset but don't change its value.
503 /// @note Use setValueOnly(const Coord&, const ValueType&) for a safer alternative.
504 /// @warning This method should only be used by experts seeking low-level optimizations.
505 void setValueOnlyUnsafe(Index offset, const ValueType& value);
506 /// @brief Mark the tile active at offset but don't change its value.
507 /// @note Use setValueOn(const Coord&) for a safer alternative.
508 /// @warning This method should only be used by experts seeking low-level optimizations.
510 /// @brief Set the tile value at offset and mark the voxel as active.
511 /// @note Use setValueOn(const Coord&, const ValueType&) for a safer alternative.
512 /// @warning This method should only be used by experts seeking low-level optimizations.
513 void setValueOnUnsafe(Index offset, const ValueType& value);
514 /// @brief Mark the tile inactive at offset but don't change its value.
515 /// @note Use setValueOff(const Coord&) for a safer alternative.
516 /// @warning This method should only be used by experts seeking low-level optimizations.
518 /// @brief Set the tile value at offset and mark the voxel as inactive.
519 /// @note Use setValueOff(const Coord&, const ValueType&) for a safer alternative.
520 /// @warning This method should only be used by experts seeking low-level optimizations.
521 void setValueOffUnsafe(Index offset, const ValueType& value);
522
523 /// @brief Set the active/inactive value mask.
524 /// @note Use setValueOn(const Coord&)/setValueOff(const Coord&) for a safer alternative.
525 /// @warning This method should only be used by experts seeking low-level optimizations.
527
528 /// @brief Replace a tile at offset with the given child node.
529 /// @note Use addChild(ChildNodeType*) for a safer alternative.
530 /// @warning This method should only be used by experts seeking low-level optimizations.
531 void setChildUnsafe(Index offset, ChildNodeType* child);
532 /// @brief Replace a child node at offset with the given child node.
533 /// @note Use addChild(ChildNodeType*) for a safer alternative.
534 /// @warning This method should only be used by experts seeking low-level optimizations.
536 /// @brief Replace a child node at offset with the given value and active state.
537 /// @note Use addChild(ChildNodeType*) for a safer alternative.
538 /// @warning This method should only be used by experts seeking low-level optimizations.
539 ChildNodeType* stealChildUnsafe(Index offset, const ValueType& value, bool active);
540 /// @brief Delete a child node at offset and replace with the given value and active state.
541 /// @note Use addTile(Index, const ValueType&, bool) for a safer alternative.
542 /// @warning This method should only be used by experts seeking low-level optimizations.
543 void deleteChildUnsafe(Index offset, const ValueType& value, bool active);
544
545 //
546 // Aux methods
547 //
548
549 /// Change the sign of all the values represented in this node and its child nodes.
550 void negate();
551
552 /// @brief Set all voxels within a given axis-aligned box to a constant value.
553 /// @param bbox inclusive coordinates of opposite corners of an axis-aligned box
554 /// @param value the value to which to set voxels within the box
555 /// @param active if true, mark voxels within the box as active,
556 /// otherwise mark them as inactive
557 /// @note This operation generates a sparse, but not always optimally sparse,
558 /// representation of the filled box. Follow fill operations with a prune()
559 /// operation for optimal sparseness.
560 void fill(const CoordBBox& bbox, const ValueType& value, bool active = true);
561
562 /// @brief Set all voxels within a given axis-aligned box to a constant value
563 /// and ensure that those voxels are all represented at the leaf level.
564 /// @param bbox inclusive coordinates of opposite corners of an axis-aligned box.
565 /// @param value the value to which to set voxels within the box.
566 /// @param active if true, mark voxels within the box as active,
567 /// otherwise mark them as inactive.
568 /// @sa voxelizeActiveTiles()
569 void denseFill(const CoordBBox& bbox, const ValueType& value, bool active = true);
570
571 /// @brief Densify active tiles, i.e., replace them with leaf-level active voxels.
572 /// @param threaded if true, this operation is multi-threaded (over the internal nodes).
573 /// @sa denseFill()
574 void voxelizeActiveTiles(bool threaded = true);
575
576 /// @brief Copy into a dense grid the values of the voxels that lie within
577 /// a given bounding box.
578 /// @param bbox inclusive bounding box of the voxels to be copied into the dense grid
579 /// @param dense dense grid with a stride in @e z of one (see tools::Dense
580 /// in tools/Dense.h for the required API)
581 /// @note @a bbox is assumed to be identical to or contained in the coordinate domains
582 /// of both the dense grid and this node, i.e., no bounds checking is performed.
583 template<typename DenseT>
584 void copyToDense(const CoordBBox& bbox, DenseT& dense) const;
585
586 /// @brief Efficiently merge another tree into this tree using one of several schemes.
587 /// @warning This operation cannibalizes the other tree.
588 template<MergePolicy Policy>
589 void merge(InternalNode& other, const ValueType& background, const ValueType& otherBackground);
590
591 /// @brief Merge, using one of several schemes, this node (and its descendants)
592 /// with a tile of the same dimensions and the given value and active state.
593 template<MergePolicy Policy> void merge(const ValueType& tileValue, bool tileActive);
594
595 /// @brief Union this branch's set of active values with the other branch's
596 /// active values. The value type of the other branch can be different.
597 /// @details The resulting state of a value is active if the corresponding value
598 /// was already active OR if it is active in the other tree. Also, a resulting
599 /// value maps to a voxel if the corresponding value already mapped to a voxel
600 /// OR if it is a voxel in the other tree. Thus, a resulting value can only
601 /// map to a tile if the corresponding value already mapped to a tile
602 /// AND if it is a tile value in other tree.
603 ///
604 /// Specifically, active tiles and voxels in this branch are not changed, and
605 /// tiles or voxels that were inactive in this branch but active in the other branch
606 /// are marked as active in this branch but left with their original values.
607 template<typename OtherChildNodeType>
608 void topologyUnion(const InternalNode<OtherChildNodeType, Log2Dim>& other, const bool preserveTiles = false);
609
610 /// @brief Intersects this tree's set of active values with the active values
611 /// of the other tree, whose @c ValueType may be different.
612 /// @details The resulting state of a value is active only if the corresponding
613 /// value was already active AND if it is active in the other tree. Also, a
614 /// resulting value maps to a voxel if the corresponding value
615 /// already mapped to an active voxel in either of the two grids
616 /// and it maps to an active tile or voxel in the other grid.
617 ///
618 /// @note This operation can delete branches in this grid if they
619 /// overlap with inactive tiles in the other grid. Likewise active
620 /// voxels can be turned into inactive voxels resulting in leaf
621 /// nodes with no active values. Thus, it is recommended to
622 /// subsequently call prune.
623 template<typename OtherChildNodeType>
625 const ValueType& background);
626
627 /// @brief Difference this node's set of active values with the active values
628 /// of the other node, whose @c ValueType may be different. So a
629 /// resulting voxel will be active only if the original voxel is
630 /// active in this node and inactive in the other node.
631 ///
632 /// @details The last dummy argument is required to match the signature
633 /// for InternalNode::topologyDifference.
634 ///
635 /// @note This operation modifies only active states, not
636 /// values. Also note that this operation can result in all voxels
637 /// being inactive so consider subsequently calling prune.
638 template<typename OtherChildNodeType>
640 const ValueType& background);
641
642 template<typename CombineOp>
643 void combine(InternalNode& other, CombineOp&);
644 template<typename CombineOp>
645 void combine(const ValueType& value, bool valueIsActive, CombineOp&);
646
647 template<typename CombineOp, typename OtherNodeType /*= InternalNode*/>
648 void combine2(const InternalNode& other0, const OtherNodeType& other1, CombineOp&);
649 template<typename CombineOp, typename OtherNodeType /*= InternalNode*/>
650 void combine2(const ValueType& value, const OtherNodeType& other, bool valIsActive, CombineOp&);
651 template<typename CombineOp, typename OtherValueType>
652 void combine2(const InternalNode& other, const OtherValueType&, bool valIsActive, CombineOp&);
653
654 /// Set all voxels that lie outside the given axis-aligned box to the background.
655 void clip(const CoordBBox&, const ValueType& background);
656
657 /// @brief Reduce the memory footprint of this tree by replacing with tiles
658 /// any nodes whose values are all the same (optionally to within a tolerance)
659 /// and have the same active state.
660 void prune(const ValueType& tolerance = zeroVal<ValueType>());
661
662 /// @brief Add the specified leaf to this node, possibly creating a child branch
663 /// in the process. If the leaf node already exists, replace it.
665
666 /// @brief Same as addLeaf() except, if necessary, update the accessor with pointers
667 /// to the nodes along the path from the root node to the node containing the coordinate.
668 template<typename AccessorT>
669 void addLeafAndCache(LeafNodeType* leaf, AccessorT&);
670
671 /// @brief Return a pointer to the node of type @c NodeT that contains voxel (x, y, z)
672 /// and replace it with a tile of the specified value and state.
673 /// If no such node exists, leave the tree unchanged and return @c nullptr.
674 ///
675 /// @note The caller takes ownership of the node and is responsible for deleting it.
676 ///
677 /// @warning Since this method potentially removes nodes and branches of the tree,
678 /// it is important to clear the caches of all ValueAccessors associated with this tree.
679 template<typename NodeT>
680 NodeT* stealNode(const Coord& xyz, const ValueType& value, bool state);
681
682 /// @brief Add the given child node at this level deducing the offset from it's origin.
683 /// If a child node with this offset already exists, delete the old node and add the
684 /// new node in its place (i.e. ownership of the new child node is transferred to
685 /// this InternalNode)
686 /// @return @c true if inserting the child has been successful, otherwise the caller
687 /// retains ownership of the node and is responsible for deleting it.
689
690 /// @brief Add a tile at the specified tree level that contains voxel (x, y, z),
691 /// possibly creating a parent branch or deleting a child branch in the process.
692 void addTile(Index level, const Coord& xyz, const ValueType& value, bool state);
693
694 /// @brief Delete any existing child branch at the specified offset and add a tile.
695 void addTile(Index offset, const ValueType& value, bool state);
696
697 /// @brief Same as addTile() except, if necessary, update the accessor with pointers
698 /// to the nodes along the path from the root node to the node containing (x, y, z).
699 template<typename AccessorT>
700 void addTileAndCache(Index level, const Coord& xyz, const ValueType&, bool state, AccessorT&);
701
702 //@{
703 /// @brief Return a pointer to the node that contains voxel (x, y, z).
704 /// If no such node exists, return nullptr.
705 template<typename NodeType> NodeType* probeNode(const Coord& xyz);
706 template<typename NodeType> const NodeType* probeConstNode(const Coord& xyz) const;
707 template<typename NodeType> const NodeType* probeNode(const Coord& xyz) const { return this->probeConstNode<NodeType>(xyz); }
708 //@}
709
710 //@{
711 /// @brief Return a pointer to the child node that contains voxel (x, y, z).
712 /// If no such node exists, return nullptr.
714 const ChildNodeType* probeConstChild(const Coord& xyz) const;
715 const ChildNodeType* probeChild(const Coord& xyz) const { return this->probeConstChild(xyz); }
716 //@}
717
718 //@{
719 /// @brief Return a pointer to the child node that contains voxel (x, y, z).
720 /// If no such node exists, return nullptr.
721 ChildNodeType* probeChild(const Coord& xyz, ValueType& value, bool& active);
722 const ChildNodeType* probeConstChild(const Coord& xyz, ValueType& value, bool& active) const;
723 const ChildNodeType* probeChild(const Coord& xyz, ValueType& value, bool& active) const { return this->probeConstChild(xyz, value, active); }
724 //@}
725
726 //@{
727 /// @brief Return a pointer to the child node for a specific offset.
728 /// If no such node exists, return nullptr.
729 /// @warning This method should only be used by experts seeking low-level optimizations.
730 /// @note Out-of-bounds memory access attempts will wrap around using modulo indexing.
733 const ChildNodeType* probeChildUnsafe(Index offset) const { return this->probeConstChildUnsafe(offset); }
734 //@}
735
736 //@{
737 /// @brief Return a pointer to the child node for a specific offset.
738 /// If no such node exists, return nullptr.
739 /// @warning This method should only be used by experts seeking low-level optimizations.
740 /// @note Out-of-bounds memory access attempts will wrap around using modulo indexing.
741 ChildNodeType* probeChildUnsafe(Index offset, ValueType& value, bool& active);
742 const ChildNodeType* probeConstChildUnsafe(Index offset, ValueType& value, bool& active) const;
743 const ChildNodeType* probeChildUnsafe(Index offset, ValueType& value, bool& active) const { return this->probeConstChildUnsafe(offset, value, active); }
744 //@}
745
746 //@{
747 /// @brief Same as probeNode() except, if necessary, update the accessor with pointers
748 /// to the nodes along the path from the root node to the node containing (x, y, z).
749 template<typename NodeType, typename AccessorT>
750 NodeType* probeNodeAndCache(const Coord& xyz, AccessorT&);
751 template<typename NodeType, typename AccessorT>
752 const NodeType* probeConstNodeAndCache(const Coord& xyz, AccessorT&) const;
753 //@}
754
755 //@{
756 /// @brief Return a pointer to the leaf node that contains voxel (x, y, z).
757 /// If no such node exists, return @c nullptr.
759 const LeafNodeType* probeConstLeaf(const Coord& xyz) const;
760 const LeafNodeType* probeLeaf(const Coord& xyz) const;
761 //@}
762
763 //@{
764 /// @brief Same as probeLeaf() except, if necessary, update the accessor with pointers
765 /// to the nodes along the path from the root node to the node containing (x, y, z).
766 template<typename AccessorT>
767 LeafNodeType* probeLeafAndCache(const Coord& xyz, AccessorT& acc);
768 template<typename AccessorT>
769 const LeafNodeType* probeConstLeafAndCache(const Coord& xyz, AccessorT& acc) const;
770 template<typename AccessorT>
771 const LeafNodeType* probeLeafAndCache(const Coord& xyz, AccessorT& acc) const;
772 //@}
773
774 /// @brief Return the leaf node that contains voxel (x, y, z).
775 /// If no such node exists, create one, but preserve the values and
776 /// active states of all voxels.
777 ///
778 /// @details Use this method to preallocate a static tree topology
779 /// over which to safely perform multithreaded processing.
781
782 /// @brief Same as touchLeaf() except, if necessary, update the accessor with pointers
783 /// to the nodes along the path from the root node to the node containing the coordinate.
784 template<typename AccessorT>
785 LeafNodeType* touchLeafAndCache(const Coord& xyz, AccessorT&);
786
787 //@{
788 /// @brief Adds all nodes of a certain type to a container with the following API:
789 /// @code
790 /// struct ArrayT {
791 /// using value_type = ...;// defines the type of nodes to be added to the array
792 /// void push_back(value_type nodePtr);// method that add nodes to the array
793 /// };
794 /// @endcode
795 /// @details An example of a wrapper around a c-style array is:
796 /// @code
797 /// struct MyArray {
798 /// using value_type = LeafType*;
799 /// value_type* ptr;
800 /// MyArray(value_type* array) : ptr(array) {}
801 /// void push_back(value_type leaf) { *ptr++ = leaf; }
802 ///};
803 /// @endcode
804 /// @details An example that constructs a list of pointer to all leaf nodes is:
805 /// @code
806 /// std::vector<const LeafNodeType*> array;//most std contains have the required API
807 /// array.reserve(tree.leafCount());//this is a fast preallocation.
808 /// tree.getNodes(array);
809 /// @endcode
810 template<typename ArrayT>
811 void getNodes(ArrayT& array);
812 template<typename ArrayT>
813 void getNodes(ArrayT& array) const;
814 //@}
815
816 /// @brief Steals all nodes of a certain type from the tree and
817 /// adds them to a container with the following API:
818 /// @code
819 /// struct ArrayT {
820 /// using value_type = ...;// defines the type of nodes to be added to the array
821 /// void push_back(value_type nodePtr);// method that add nodes to the array
822 /// };
823 /// @endcode
824 /// @details An example of a wrapper around a c-style array is:
825 /// @code
826 /// struct MyArray {
827 /// using value_type = LeafType*;
828 /// value_type* ptr;
829 /// MyArray(value_type* array) : ptr(array) {}
830 /// void push_back(value_type leaf) { *ptr++ = leaf; }
831 ///};
832 /// @endcode
833 /// @details An example that constructs a list of pointer to all leaf nodes is:
834 /// @code
835 /// std::vector<const LeafNodeType*> array;//most std contains have the required API
836 /// array.reserve(tree.leafCount());//this is a fast preallocation.
837 /// tree.stealNodes(array);
838 /// @endcode
839 template<typename ArrayT>
840 void stealNodes(ArrayT& array, const ValueType& value, bool state);
841
842 /// @brief Change inactive tiles or voxels with value oldBackground to newBackground
843 /// or -oldBackground to -newBackground. Active values are unchanged.
844 void resetBackground(const ValueType& oldBackground, const ValueType& newBackground);
845
846 /// @brief Return @c true if the given tree branch has the same node and active value
847 /// topology as this tree branch (but possibly a different @c ValueType).
848 template<typename OtherChildNodeType, Index OtherLog2Dim>
850
851protected:
852 //@{
853 /// Allow iterators to call mask accessor methods (setValueMask(), setChildMask(), etc.).
854 /// @todo Make mask accessors public?
858 //@}
859
860 /// @brief During topology-only construction, access is needed
861 /// to protected/private members of other template instances.
862 template<typename, Index> friend class InternalNode;
863
864 // Mask accessors
865public:
866 bool isValueMaskOn(Index n) const { return mValueMask.isOn(n); }
867 bool isValueMaskOn() const { return mValueMask.isOn(); }
868 bool isValueMaskOff(Index n) const { return mValueMask.isOff(n); }
869 bool isValueMaskOff() const { return mValueMask.isOff(); }
870 bool isChildMaskOn(Index n) const { return mChildMask.isOn(n); }
871 bool isChildMaskOff(Index n) const { return mChildMask.isOff(n); }
872 bool isChildMaskOff() const { return mChildMask.isOff(); }
873 const NodeMaskType& getValueMask() const { return mValueMask; }
874 const NodeMaskType& getChildMask() const { return mChildMask; }
876 {
878 mask |= mChildMask;
879 mask.toggle();
880 return mask;
881 }
882 const UnionType* getTable() const { return mNodes; }
883protected:
884 //@{
885 /// Use a mask accessor to ensure consistency between the child and value masks;
886 /// i.e., the value mask should always be off wherever the child mask is on.
887 void setValueMask(Index n, bool on) { mValueMask.set(n, mChildMask.isOn(n) ? false : on); }
888 //@}
889
890 void makeChildNodeEmpty(Index n, const ValueType& value);
891 void setChildNode( Index i, ChildNodeType* child);//assumes a tile
892 void resetChildNode(Index i, ChildNodeType* child);//checks for an existing child
894
895 ///@{
896 /// @brief Returns a pointer to the child node at the linear offset n.
897 /// @warning This protected method assumes that a child node exists at
898 /// the specified linear offset!
901 ///@}
902
903 ///@{
904 /// @brief Protected member classes for recursive multi-threading
905 struct VoxelizeActiveTiles;
906 template<typename OtherInternalNode> struct DeepCopy;
907 template<typename OtherInternalNode> struct TopologyCopy1;
908 template<typename OtherInternalNode> struct TopologyCopy2;
909 template<typename OtherInternalNode> struct TopologyUnion;
910 template<typename OtherInternalNode> struct TopologyDifference;
911 template<typename OtherInternalNode> struct TopologyIntersection;
912 ///@}
913
916 /// Global grid index coordinates (x,y,z) of the local origin of this node
918 /// Transient data (not serialized)
920}; // class InternalNode
921
922
923////////////////////////////////////////
924
925
926//@{
927/// Helper metafunction used to implement InternalNode::SameConfiguration
928/// (which, as an inner class, can't be independently specialized)
929template<typename ChildT1, Index Dim1, typename NodeT2>
931 static const bool value = false;
932};
933
934template<typename ChildT1, Index Dim1, typename ChildT2>
935struct SameInternalConfig<ChildT1, Dim1, InternalNode<ChildT2, Dim1> > {
936 static const bool value = ChildT1::template SameConfiguration<ChildT2>::value;
937};
938//@}
939
940
941////////////////////////////////////////
942
943
944template<typename ChildT, Index Log2Dim>
945inline
947{
948 for (Index i = 0; i < NUM_VALUES; ++i) mNodes[i].setValue(background);
949}
950
951
952template<typename ChildT, Index Log2Dim>
953inline
955 mOrigin(origin[0] & ~(DIM - 1), // zero out the low-order bits
956 origin[1] & ~(DIM - 1),
957 origin[2] & ~(DIM - 1))
958{
959 if (active) mValueMask.setOn();
960 for (Index i = 0; i < NUM_VALUES; ++i) mNodes[i].setValue(val);
961}
962
963
964// For InternalNodes, the PartialCreate constructor is identical to its
965// non-PartialCreate counterpart.
966template<typename ChildT, Index Log2Dim>
967inline
969 const Coord& origin, const ValueType& val, bool active)
970 : mOrigin(origin[0] & ~(DIM-1), origin[1] & ~(DIM-1), origin[2] & ~(DIM-1))
971{
972 if (active) mValueMask.setOn();
973 for (Index i = 0; i < NUM_VALUES; ++i) mNodes[i].setValue(val);
974}
975
976template<typename ChildT, Index Log2Dim>
977template<typename OtherInternalNode>
978struct InternalNode<ChildT, Log2Dim>::DeepCopy
979{
980 DeepCopy(const OtherInternalNode* source, InternalNode* target) : s(source), t(target) {
981 tbb::parallel_for(tbb::blocked_range<Index>(0, NUM_VALUES), *this);
982 //(*this)(tbb::blocked_range<Index>(0, NUM_VALUES));//serial
983 }
984 void operator()(const tbb::blocked_range<Index> &r) const {
985 for (Index i = r.begin(), end=r.end(); i!=end; ++i) {
986 if (s->mChildMask.isOff(i)) {
987 t->mNodes[i].setValue(ValueType(s->mNodes[i].getValue()));
988 } else {
989 t->mNodes[i].setChild(new ChildNodeType(*(s->mNodes[i].getChild())));
990 }
991 }
992 }
993 const OtherInternalNode* s;
995};// DeepCopy
996
997template<typename ChildT, Index Log2Dim>
998inline
1007
1008
1009// Copy-construct from a node with the same configuration but a different ValueType.
1010template<typename ChildT, Index Log2Dim>
1011template<typename OtherChildNodeType>
1012inline
1021
1022template<typename ChildT, Index Log2Dim>
1023template<typename OtherInternalNode>
1024struct InternalNode<ChildT, Log2Dim>::TopologyCopy1
1025{
1026 TopologyCopy1(const OtherInternalNode* source, InternalNode* target,
1027 const ValueType& background) : s(source), t(target), b(background) {
1028 tbb::parallel_for(tbb::blocked_range<Index>(0, NUM_VALUES), *this);
1029 //(*this)(tbb::blocked_range<Index>(0, NUM_VALUES));//serial
1030 }
1031 void operator()(const tbb::blocked_range<Index> &r) const {
1032 for (Index i = r.begin(), end=r.end(); i!=end; ++i) {
1033 if (s->isChildMaskOn(i)) {
1034 t->mNodes[i].setChild(new ChildNodeType(*(s->mNodes[i].getChild()),
1035 b, TopologyCopy()));
1036 } else {
1037 t->mNodes[i].setValue(b);
1038 }
1039 }
1040 }
1041 const OtherInternalNode* s;
1043 const ValueType &b;
1044};// TopologyCopy1
1045
1046template<typename ChildT, Index Log2Dim>
1047template<typename OtherChildNodeType>
1048inline
1058
1059template<typename ChildT, Index Log2Dim>
1060template<typename OtherInternalNode>
1061struct InternalNode<ChildT, Log2Dim>::TopologyCopy2
1062{
1063 TopologyCopy2(const OtherInternalNode* source, InternalNode* target,
1064 const ValueType& offValue, const ValueType& onValue)
1065 : s(source), t(target), offV(offValue), onV(onValue) {
1066 tbb::parallel_for(tbb::blocked_range<Index>(0, NUM_VALUES), *this);
1067 }
1068 void operator()(const tbb::blocked_range<Index> &r) const {
1069 for (Index i = r.begin(), end=r.end(); i!=end; ++i) {
1070 if (s->isChildMaskOn(i)) {
1071 t->mNodes[i].setChild(new ChildNodeType(*(s->mNodes[i].getChild()),
1072 offV, onV, TopologyCopy()));
1073 } else {
1074 t->mNodes[i].setValue(s->isValueMaskOn(i) ? onV : offV);
1075 }
1076 }
1077 }
1078 const OtherInternalNode* s;
1081 };// TopologyCopy2
1082
1083template<typename ChildT, Index Log2Dim>
1084template<typename OtherChildNodeType>
1085inline
1087 const ValueType& offValue,
1088 const ValueType& onValue, TopologyCopy)
1089 : mChildMask(other.mChildMask)
1090 , mValueMask(other.mValueMask)
1091 , mOrigin(other.mOrigin)
1093{
1094 TopologyCopy2<InternalNode<OtherChildNodeType, Log2Dim> > tmp(&other, this, offValue, onValue);
1095}
1096
1097
1098template<typename ChildT, Index Log2Dim>
1099inline
1101{
1102 for (ChildOnIter iter = this->beginChildOn(); iter; ++iter) {
1103 delete mNodes[iter.pos()].getChild();
1104 }
1105}
1106
1107
1108////////////////////////////////////////
1109
1110
1111template<typename ChildT, Index Log2Dim>
1112inline Index64
1114{
1115 if (ChildNodeType::getLevel() == 0) return mChildMask.countOn();
1116 Index64 sum = 0;
1117 for (ChildOnCIter iter = this->cbeginChildOn(); iter; ++iter) {
1118 sum += iter->leafCount();
1119 }
1120 return sum;
1121}
1122
1123template<typename ChildT, Index Log2Dim>
1124inline void
1125InternalNode<ChildT, Log2Dim>::nodeCount(std::vector<Index64> &vec) const
1126{
1127 OPENVDB_ASSERT(vec.size() > ChildNodeType::LEVEL);
1128 const auto count = mChildMask.countOn();
1129 if (ChildNodeType::LEVEL > 0 && count > 0) {
1130 for (auto iter = this->cbeginChildOn(); iter; ++iter) iter->nodeCount(vec);
1131 }
1132 vec[ChildNodeType::LEVEL] += count;
1133}
1134
1135template<typename ChildT, Index Log2Dim>
1136inline void
1137InternalNode<ChildT, Log2Dim>::nodeCount(std::vector<Index32> &vec) const
1138{
1139 OPENVDB_ASSERT(vec.size() > ChildNodeType::LEVEL);
1140 const auto count = mChildMask.countOn();
1141 if (ChildNodeType::LEVEL > 0 && count > 0) {
1142 for (auto iter = this->cbeginChildOn(); iter; ++iter) {
1144 iter->nodeCount(vec);
1146 }
1147 }
1148 vec[ChildNodeType::LEVEL] += count;
1149}
1150
1151
1152template<typename ChildT, Index Log2Dim>
1153inline Index64
1155{
1156 Index64 sum = 1;
1157 if (ChildNodeType::getLevel() == 0) return sum;
1158 for (ChildOnCIter iter = this->cbeginChildOn(); iter; ++iter) {
1159 sum += iter->nonLeafCount();
1160 }
1161 return sum;
1162}
1163
1164
1165template<typename ChildT, Index Log2Dim>
1166inline Index32
1168{
1169 return this->getChildMask().countOn();
1170}
1171
1172
1173template<typename ChildT, Index Log2Dim>
1174inline Index64
1176{
1177 Index64 sum = ChildT::NUM_VOXELS * mValueMask.countOn();
1178 for (ChildOnCIter iter = this->cbeginChildOn(); iter; ++iter) {
1179 sum += iter->onVoxelCount();
1180 }
1181 return sum;
1182}
1183
1184
1185template<typename ChildT, Index Log2Dim>
1186inline Index64
1188{
1189 Index64 sum = ChildT::NUM_VOXELS * (NUM_VALUES-mValueMask.countOn()-mChildMask.countOn());
1190 for (ChildOnCIter iter = this->cbeginChildOn(); iter; ++iter) {
1191 sum += iter->offVoxelCount();
1192 }
1193 return sum;
1194}
1195
1196
1197template<typename ChildT, Index Log2Dim>
1198inline Index64
1200{
1201 Index64 sum = 0;
1202 for (ChildOnCIter iter = this->beginChildOn(); iter; ++iter) {
1203 sum += mNodes[iter.pos()].getChild()->onLeafVoxelCount();
1204 }
1205 return sum;
1206}
1207
1208
1209template<typename ChildT, Index Log2Dim>
1210inline Index64
1212{
1213 Index64 sum = 0;
1214 for (ChildOnCIter iter = this->beginChildOn(); iter; ++iter) {
1215 sum += mNodes[iter.pos()].getChild()->offLeafVoxelCount();
1216 }
1217 return sum;
1218}
1219
1220template<typename ChildT, Index Log2Dim>
1221inline Index64
1223{
1224 Index64 sum = mValueMask.countOn();
1225 for (ChildOnCIter iter = this->cbeginChildOn(); LEVEL>1 && iter; ++iter) {
1226 sum += iter->onTileCount();
1227 }
1228 return sum;
1229}
1230
1231template<typename ChildT, Index Log2Dim>
1232inline Index64
1234{
1235 Index64 sum = NUM_VALUES * sizeof(UnionType) + mChildMask.memUsage()
1236 + mValueMask.memUsage() + sizeof(mOrigin);
1237 for (ChildOnCIter iter = this->cbeginChildOn(); iter; ++iter) {
1238 sum += iter->memUsage();
1239 }
1240 return sum;
1241}
1242
1243
1244template<typename ChildT, Index Log2Dim>
1245inline void
1247{
1248 if (bbox.isInside(this->getNodeBoundingBox())) return;
1249
1250 for (ValueOnCIter i = this->cbeginValueOn(); i; ++i) {
1251 bbox.expand(i.getCoord(), ChildT::DIM);
1252 }
1253 for (ChildOnCIter i = this->cbeginChildOn(); i; ++i) {
1254 i->evalActiveBoundingBox(bbox, visitVoxels);
1255 }
1256}
1257
1258
1259////////////////////////////////////////
1260
1261
1262template<typename ChildT, Index Log2Dim>
1263inline void
1265{
1266 bool state = false;
1267 ValueType value = zeroVal<ValueType>();
1268 for (ChildOnIter iter = this->beginChildOn(); iter; ++iter) {
1269 const Index i = iter.pos();
1270 ChildT* child = mNodes[i].getChild();
1271 child->prune(tolerance);
1272 if (child->isConstant(value, state, tolerance)) {
1273 delete child;
1274 mChildMask.setOff(i);
1275 mValueMask.set(i, state);
1276 mNodes[i].setValue(value);
1277 }
1278 }
1279}
1280
1281
1282////////////////////////////////////////
1283
1284
1285template<typename ChildT, Index Log2Dim>
1286template<typename NodeT>
1287inline NodeT*
1288InternalNode<ChildT, Log2Dim>::stealNode(const Coord& xyz, const ValueType& value, bool state)
1289{
1290 if ((NodeT::LEVEL == ChildT::LEVEL && !(std::is_same<NodeT, ChildT>::value)) ||
1291 NodeT::LEVEL > ChildT::LEVEL) return nullptr;
1293 const Index n = this->coordToOffset(xyz);
1294 if (mChildMask.isOff(n)) return nullptr;
1295 ChildT* child = mNodes[n].getChild();
1296 if (std::is_same<NodeT, ChildT>::value) {
1297 mChildMask.setOff(n);
1298 mValueMask.set(n, state);
1299 mNodes[n].setValue(value);
1300 }
1301 return (std::is_same<NodeT, ChildT>::value)
1302 ? reinterpret_cast<NodeT*>(child)
1303 : child->template stealNode<NodeT>(xyz, value, state);
1305}
1306
1307
1308////////////////////////////////////////
1309
1310
1311template<typename ChildT, Index Log2Dim>
1312template<typename NodeT>
1313inline NodeT*
1315{
1316 if ((NodeT::LEVEL == ChildT::LEVEL && !(std::is_same<NodeT, ChildT>::value)) ||
1317 NodeT::LEVEL > ChildT::LEVEL) return nullptr;
1319 const Index n = this->coordToOffset(xyz);
1320 if (mChildMask.isOff(n)) return nullptr;
1321 ChildT* child = mNodes[n].getChild();
1322 return (std::is_same<NodeT, ChildT>::value)
1323 ? reinterpret_cast<NodeT*>(child)
1324 : child->template probeNode<NodeT>(xyz);
1326}
1327
1328
1329template<typename ChildT, Index Log2Dim>
1330template<typename NodeT, typename AccessorT>
1331inline NodeT*
1333{
1334 if ((NodeT::LEVEL == ChildT::LEVEL && !(std::is_same<NodeT, ChildT>::value)) ||
1335 NodeT::LEVEL > ChildT::LEVEL) return nullptr;
1337 const Index n = this->coordToOffset(xyz);
1338 if (mChildMask.isOff(n)) return nullptr;
1339 ChildT* child = mNodes[n].getChild();
1340 acc.insert(xyz, child);
1341 return (std::is_same<NodeT, ChildT>::value)
1342 ? reinterpret_cast<NodeT*>(child)
1343 : child->template probeNodeAndCache<NodeT>(xyz, acc);
1345}
1346
1347
1348template<typename ChildT, Index Log2Dim>
1349template<typename NodeT>
1350inline const NodeT*
1352{
1353 if ((NodeT::LEVEL == ChildT::LEVEL && !(std::is_same<NodeT, ChildT>::value)) ||
1354 NodeT::LEVEL > ChildT::LEVEL) return nullptr;
1356 const Index n = this->coordToOffset(xyz);
1357 if (mChildMask.isOff(n)) return nullptr;
1358 const ChildT* child = mNodes[n].getChild();
1359 return (std::is_same<NodeT, ChildT>::value)
1360 ? reinterpret_cast<const NodeT*>(child)
1361 : child->template probeConstNode<NodeT>(xyz);
1363}
1364
1365
1366template<typename ChildT, Index Log2Dim>
1367template<typename NodeT, typename AccessorT>
1368inline const NodeT*
1370{
1371 if ((NodeT::LEVEL == ChildT::LEVEL && !(std::is_same<NodeT, ChildT>::value)) ||
1372 NodeT::LEVEL > ChildT::LEVEL) return nullptr;
1374 const Index n = this->coordToOffset(xyz);
1375 if (mChildMask.isOff(n)) return nullptr;
1376 const ChildT* child = mNodes[n].getChild();
1377 acc.insert(xyz, child);
1378 return (std::is_same<NodeT, ChildT>::value)
1379 ? reinterpret_cast<const NodeT*>(child)
1380 : child->template probeConstNodeAndCache<NodeT>(xyz, acc);
1382}
1383
1384
1385////////////////////////////////////////
1386
1387
1388template<typename ChildT, Index Log2Dim>
1389inline ChildT*
1391{
1392 const Index n = this->coordToOffset(xyz);
1393 return this->probeChildUnsafe(n);
1394}
1395
1396template<typename ChildT, Index Log2Dim>
1397inline const ChildT*
1399{
1400 const Index n = this->coordToOffset(xyz);
1401 return this->probeConstChildUnsafe(n);
1402}
1403
1404template<typename ChildT, Index Log2Dim>
1405inline ChildT*
1407{
1408 const Index n = this->coordToOffset(xyz);
1409 return this->probeChildUnsafe(n, value, active);
1410}
1411
1412template<typename ChildT, Index Log2Dim>
1413inline const ChildT*
1415{
1416 const Index n = this->coordToOffset(xyz);
1417 return this->probeConstChildUnsafe(n, value, active);
1418}
1419
1420template<typename ChildT, Index Log2Dim>
1421inline ChildT*
1423{
1424 OPENVDB_ASSERT(offset < NUM_VALUES);
1425 if (mChildMask.isOn(offset)) return mNodes[offset].getChild();
1426 return nullptr;
1427}
1428
1429template<typename ChildT, Index Log2Dim>
1430inline const ChildT*
1432{
1433 OPENVDB_ASSERT(offset < NUM_VALUES);
1434 if (mChildMask.isOn(offset)) return mNodes[offset].getChild();
1435 return nullptr;
1436}
1437
1438template<typename ChildT, Index Log2Dim>
1439inline ChildT*
1441{
1442 OPENVDB_ASSERT(offset < NUM_VALUES);
1443 if (mChildMask.isOn(offset)) return mNodes[offset].getChild();
1444 value = mNodes[offset].getValue();
1445 active = mValueMask.isOn(offset);
1446 return nullptr;
1447}
1448
1449template<typename ChildT, Index Log2Dim>
1450inline const ChildT*
1452{
1453 OPENVDB_ASSERT(offset < NUM_VALUES);
1454 if (mChildMask.isOn(offset)) return mNodes[offset].getChild();
1455 value = mNodes[offset].getValue();
1456 active = mValueMask.isOn(offset);
1457 return nullptr;
1458}
1459
1460
1461////////////////////////////////////////
1462
1463
1464template<typename ChildT, Index Log2Dim>
1465inline typename ChildT::LeafNodeType*
1467{
1468 return this->template probeNode<LeafNodeType>(xyz);
1469}
1470
1471
1472template<typename ChildT, Index Log2Dim>
1473template<typename AccessorT>
1474inline typename ChildT::LeafNodeType*
1476{
1477 return this->template probeNodeAndCache<LeafNodeType>(xyz, acc);
1478}
1479
1480
1481template<typename ChildT, Index Log2Dim>
1482template<typename AccessorT>
1483inline const typename ChildT::LeafNodeType*
1485{
1486 return this->probeConstLeafAndCache(xyz, acc);
1487}
1488
1489
1490template<typename ChildT, Index Log2Dim>
1491inline const typename ChildT::LeafNodeType*
1493{
1494 return this->template probeConstNode<LeafNodeType>(xyz);
1495}
1496
1497
1498template<typename ChildT, Index Log2Dim>
1499template<typename AccessorT>
1500inline const typename ChildT::LeafNodeType*
1502{
1503 return this->template probeConstNodeAndCache<LeafNodeType>(xyz, acc);
1504}
1505
1506
1507////////////////////////////////////////
1508
1509
1510template<typename ChildT, Index Log2Dim>
1511inline void
1513{
1514 OPENVDB_ASSERT(leaf != nullptr);
1515 const Coord& xyz = leaf->origin();
1516 const Index n = this->coordToOffset(xyz);
1517 ChildT* child = nullptr;
1518 if (mChildMask.isOff(n)) {
1519 if (ChildT::LEVEL>0) {
1520 child = new ChildT(xyz, mNodes[n].getValue(), mValueMask.isOn(n));
1521 } else {
1522 child = reinterpret_cast<ChildT*>(leaf);
1523 }
1524 this->setChildNode(n, child);
1525 } else {
1526 if (ChildT::LEVEL>0) {
1527 child = mNodes[n].getChild();
1528 } else {
1529 delete mNodes[n].getChild();
1530 child = reinterpret_cast<ChildT*>(leaf);
1531 mNodes[n].setChild(child);
1532 }
1533 }
1534 child->addLeaf(leaf);
1535}
1536
1537
1538template<typename ChildT, Index Log2Dim>
1539template<typename AccessorT>
1540inline void
1542{
1543 OPENVDB_ASSERT(leaf != nullptr);
1544 const Coord& xyz = leaf->origin();
1545 const Index n = this->coordToOffset(xyz);
1546 ChildT* child = nullptr;
1547 if (mChildMask.isOff(n)) {
1548 if (ChildT::LEVEL>0) {
1549 child = new ChildT(xyz, mNodes[n].getValue(), mValueMask.isOn(n));
1550 acc.insert(xyz, child);//we only cache internal nodes
1551 } else {
1552 child = reinterpret_cast<ChildT*>(leaf);
1553 }
1554 this->setChildNode(n, child);
1555 } else {
1556 if (ChildT::LEVEL>0) {
1557 child = mNodes[n].getChild();
1558 acc.insert(xyz, child);//we only cache internal nodes
1559 } else {
1560 delete mNodes[n].getChild();
1561 child = reinterpret_cast<ChildT*>(leaf);
1562 mNodes[n].setChild(child);
1563 }
1564 }
1565 child->addLeafAndCache(leaf, acc);
1566}
1567
1568
1569////////////////////////////////////////
1570
1571
1572template<typename ChildT, Index Log2Dim>
1573inline bool
1575{
1576 OPENVDB_ASSERT(child);
1577 const Coord& xyz = child->origin();
1578 // verify that the child belongs in this internal node
1579 if (Coord((xyz & ~(DIM-1))) != this->origin()) return false;
1580 // compute the offset and insert the child node
1581 const Index n = this->coordToOffset(xyz);
1582 // this also deletes an existing child node
1583 this->resetChildNode(n, child);
1584 return true;
1585}
1586
1587
1588template<typename ChildT, Index Log2Dim>
1589inline void
1591{
1593 this->makeChildNodeEmpty(n, value);
1594 mValueMask.set(n, state);
1595}
1596
1597
1598template<typename ChildT, Index Log2Dim>
1599inline void
1601 const ValueType& value, bool state)
1602{
1603 if (LEVEL >= level) {
1604 const Index n = this->coordToOffset(xyz);
1605 if (mChildMask.isOff(n)) {// tile case
1606 if (LEVEL > level) {
1607 ChildT* child = new ChildT(xyz, mNodes[n].getValue(), mValueMask.isOn(n));
1608 this->setChildNode(n, child);
1609 child->addTile(level, xyz, value, state);
1610 } else {
1611 mValueMask.set(n, state);
1612 mNodes[n].setValue(value);
1613 }
1614 } else {// child branch case
1615 ChildT* child = mNodes[n].getChild();
1616 if (LEVEL > level) {
1617 child->addTile(level, xyz, value, state);
1618 } else {
1619 delete child;
1620 mChildMask.setOff(n);
1621 mValueMask.set(n, state);
1622 mNodes[n].setValue(value);
1623 }
1624 }
1625 }
1626}
1627
1628
1629template<typename ChildT, Index Log2Dim>
1630template<typename AccessorT>
1631inline void
1633 const ValueType& value, bool state, AccessorT& acc)
1634{
1635 if (LEVEL >= level) {
1636 const Index n = this->coordToOffset(xyz);
1637 if (mChildMask.isOff(n)) {// tile case
1638 if (LEVEL > level) {
1639 ChildT* child = new ChildT(xyz, mNodes[n].getValue(), mValueMask.isOn(n));
1640 this->setChildNode(n, child);
1641 acc.insert(xyz, child);
1642 child->addTileAndCache(level, xyz, value, state, acc);
1643 } else {
1644 mValueMask.set(n, state);
1645 mNodes[n].setValue(value);
1646 }
1647 } else {// child branch case
1648 ChildT* child = mNodes[n].getChild();
1649 if (LEVEL > level) {
1650 acc.insert(xyz, child);
1651 child->addTileAndCache(level, xyz, value, state, acc);
1652 } else {
1653 delete child;
1654 mChildMask.setOff(n);
1655 mValueMask.set(n, state);
1656 mNodes[n].setValue(value);
1657 }
1658 }
1659 }
1660}
1661
1662
1663////////////////////////////////////////
1664
1665
1666template<typename ChildT, Index Log2Dim>
1667inline typename ChildT::LeafNodeType*
1669{
1670 const Index n = this->coordToOffset(xyz);
1671 ChildT* child = nullptr;
1672 if (mChildMask.isOff(n)) {
1673 child = new ChildT(xyz, mNodes[n].getValue(), mValueMask.isOn(n));
1674 this->setChildNode(n, child);
1675 } else {
1676 child = mNodes[n].getChild();
1677 }
1678 return child->touchLeaf(xyz);
1679}
1680
1681
1682template<typename ChildT, Index Log2Dim>
1683template<typename AccessorT>
1684inline typename ChildT::LeafNodeType*
1686{
1687 const Index n = this->coordToOffset(xyz);
1688 if (mChildMask.isOff(n)) {
1689 this->setChildNode(n, new ChildNodeType(xyz, mNodes[n].getValue(), mValueMask.isOn(n)));
1690 }
1691 acc.insert(xyz, mNodes[n].getChild());
1692 return mNodes[n].getChild()->touchLeafAndCache(xyz, acc);
1693}
1694
1695
1696////////////////////////////////////////
1697
1698
1699template<typename ChildT, Index Log2Dim>
1700inline bool
1702 const ValueType& tolerance) const
1703{
1704 if (!mChildMask.isOff() || !mValueMask.isConstant(state)) return false;// early termination
1705
1706 firstValue = mNodes[0].getValue();
1707 for (Index i = 1; i < NUM_VALUES; ++i) {
1708 if (!math::isApproxEqual(mNodes[i].getValue(), firstValue, tolerance)) {
1709 return false; // early termination
1710 }
1711 }
1712 return true;
1713}
1714
1715
1716////////////////////////////////////////
1717
1718
1719template<typename ChildT, Index Log2Dim>
1720inline bool
1722 ValueType& maxValue,
1723 bool& state,
1724 const ValueType& tolerance) const
1725{
1726 if (!mChildMask.isOff() || !mValueMask.isConstant(state)) return false;// early termination
1727 minValue = maxValue = mNodes[0].getValue();
1728 for (Index i = 1; i < NUM_VALUES; ++i) {
1729 const ValueType& v = mNodes[i].getValue();
1730 if (math::cwiseLessThan(v, minValue)) {
1731 if (math::cwiseGreaterThan((maxValue - v), tolerance)) return false;// early termination
1732 minValue = v;
1733 } else if (math::cwiseGreaterThan(v, maxValue)) {
1734 if (math::cwiseGreaterThan((v - minValue), tolerance)) return false;// early termination
1735 maxValue = v;
1736 }
1737 }
1738 return true;
1739}
1740
1741
1742////////////////////////////////////////
1743
1744
1745template<typename ChildT, Index Log2Dim>
1746inline bool
1748{
1750 const bool anyActiveTiles = !mValueMask.isOff();
1751 if (LEVEL==1 || anyActiveTiles) return anyActiveTiles;
1752 for (ChildOnCIter iter = this->cbeginChildOn(); iter; ++iter) {
1753 if (iter->hasActiveTiles()) return true;
1754 }
1755 return false;
1757}
1758
1759
1760template<typename ChildT, Index Log2Dim>
1761inline bool
1763{
1764 const Index n = this->coordToOffset(xyz);
1765 return this->isChildMaskOff(n) ? this->isValueMaskOn(n)
1766 : mNodes[n].getChild()->isValueOn(xyz);
1767}
1768
1769template<typename ChildT, Index Log2Dim>
1770inline bool
1772{
1773 const Index n = this->coordToOffset(xyz);
1774 return this->isChildMaskOff(n) ? this->isValueMaskOn(n)
1775 : mNodes[n].getChild()->isValueOff(xyz);
1776}
1777
1778template<typename ChildT, Index Log2Dim>
1779template<typename AccessorT>
1780inline bool
1782{
1783 const Index n = this->coordToOffset(xyz);
1784 if (this->isChildMaskOff(n)) return this->isValueMaskOn(n);
1785 acc.insert(xyz, mNodes[n].getChild());
1786 return mNodes[n].getChild()->isValueOnAndCache(xyz, acc);
1787}
1788
1789
1790template<typename ChildT, Index Log2Dim>
1791inline const typename ChildT::ValueType&
1793{
1794 const Index n = this->coordToOffset(xyz);
1795 return this->isChildMaskOff(n) ? mNodes[n].getValue()
1796 : mNodes[n].getChild()->getValue(xyz);
1797}
1798
1799template<typename ChildT, Index Log2Dim>
1800template<typename AccessorT>
1801inline const typename ChildT::ValueType&
1803{
1804 const Index n = this->coordToOffset(xyz);
1805 if (this->isChildMaskOn(n)) {
1806 acc.insert(xyz, mNodes[n].getChild());
1807 return mNodes[n].getChild()->getValueAndCache(xyz, acc);
1808 }
1809 return mNodes[n].getValue();
1810}
1811
1812
1813template<typename ChildT, Index Log2Dim>
1814inline Index
1816{
1817 const Index n = this->coordToOffset(xyz);
1818 return this->isChildMaskOff(n) ? LEVEL : mNodes[n].getChild()->getValueLevel(xyz);
1819}
1820
1821template<typename ChildT, Index Log2Dim>
1822template<typename AccessorT>
1823inline Index
1825{
1826 const Index n = this->coordToOffset(xyz);
1827 if (this->isChildMaskOn(n)) {
1828 acc.insert(xyz, mNodes[n].getChild());
1829 return mNodes[n].getChild()->getValueLevelAndCache(xyz, acc);
1830 }
1831 return LEVEL;
1832}
1833
1834
1835template<typename ChildT, Index Log2Dim>
1836inline bool
1838{
1839 const Index n = this->coordToOffset(xyz);
1840 if (this->isChildMaskOff(n)) {
1841 value = mNodes[n].getValue();
1842 return this->isValueMaskOn(n);
1843 }
1844 return mNodes[n].getChild()->probeValue(xyz, value);
1845}
1846
1847template<typename ChildT, Index Log2Dim>
1848template<typename AccessorT>
1849inline bool
1851 ValueType& value, AccessorT& acc) const
1852{
1853 const Index n = this->coordToOffset(xyz);
1854 if (this->isChildMaskOn(n)) {
1855 acc.insert(xyz, mNodes[n].getChild());
1856 return mNodes[n].getChild()->probeValueAndCache(xyz, value, acc);
1857 }
1858 value = mNodes[n].getValue();
1859 return this->isValueMaskOn(n);
1860}
1861
1862
1863template<typename ChildT, Index Log2Dim>
1864inline void
1866{
1867 const Index n = this->coordToOffset(xyz);
1868 bool hasChild = this->isChildMaskOn(n);
1869 if (!hasChild && this->isValueMaskOn(n)) {
1870 // If the voxel belongs to a constant tile that is active,
1871 // a child subtree must be constructed.
1872 hasChild = true;
1873 this->setChildNode(n, new ChildNodeType(xyz, mNodes[n].getValue(), /*active=*/true));
1874 }
1875 if (hasChild) mNodes[n].getChild()->setValueOff(xyz);
1876}
1877
1878
1879template<typename ChildT, Index Log2Dim>
1880inline void
1882{
1883 const Index n = this->coordToOffset(xyz);
1884 bool hasChild = this->isChildMaskOn(n);
1885 if (!hasChild && !this->isValueMaskOn(n)) {
1886 // If the voxel belongs to a constant tile that is inactive,
1887 // a child subtree must be constructed.
1888 hasChild = true;
1889 this->setChildNode(n, new ChildNodeType(xyz, mNodes[n].getValue(), /*active=*/false));
1890 }
1891 if (hasChild) mNodes[n].getChild()->setValueOn(xyz);
1892}
1893
1894
1895template<typename ChildT, Index Log2Dim>
1896inline void
1898{
1899 const Index n = InternalNode::coordToOffset(xyz);
1900 bool hasChild = this->isChildMaskOn(n);
1901 if (!hasChild) {
1902 const bool active = this->isValueMaskOn(n);
1903 if (active || !math::isExactlyEqual(mNodes[n].getValue(), value)) {
1904 // If the voxel belongs to a tile that is either active or that
1905 // has a constant value that is different from the one provided,
1906 // a child subtree must be constructed.
1907 hasChild = true;
1908 this->setChildNode(n, new ChildNodeType(xyz, mNodes[n].getValue(), active));
1909 }
1910 }
1911 if (hasChild) mNodes[n].getChild()->setValueOff(xyz, value);
1912}
1913
1914template<typename ChildT, Index Log2Dim>
1915template<typename AccessorT>
1916inline void
1918 const ValueType& value, AccessorT& acc)
1919{
1920 const Index n = InternalNode::coordToOffset(xyz);
1921 bool hasChild = this->isChildMaskOn(n);
1922 if (!hasChild) {
1923 const bool active = this->isValueMaskOn(n);
1924 if (active || !math::isExactlyEqual(mNodes[n].getValue(), value)) {
1925 // If the voxel belongs to a tile that is either active or that
1926 // has a constant value that is different from the one provided,
1927 // a child subtree must be constructed.
1928 hasChild = true;
1929 this->setChildNode(n, new ChildNodeType(xyz, mNodes[n].getValue(), active));
1930 }
1931 }
1932 if (hasChild) {
1933 ChildT* child = mNodes[n].getChild();
1934 acc.insert(xyz, child);
1935 child->setValueOffAndCache(xyz, value, acc);
1936 }
1937}
1938
1939
1940template<typename ChildT, Index Log2Dim>
1941inline void
1943{
1944 const Index n = this->coordToOffset(xyz);
1945 bool hasChild = this->isChildMaskOn(n);
1946 if (!hasChild) {
1947 const bool active = this->isValueMaskOn(n); // tile's active state
1948 if (!active || !math::isExactlyEqual(mNodes[n].getValue(), value)) {
1949 // If the voxel belongs to a tile that is either inactive or that
1950 // has a constant value that is different from the one provided,
1951 // a child subtree must be constructed.
1952 hasChild = true;
1953 this->setChildNode(n, new ChildNodeType(xyz, mNodes[n].getValue(), active));
1954 }
1955 }
1956 if (hasChild) mNodes[n].getChild()->setValueOn(xyz, value);
1957}
1958
1959template<typename ChildT, Index Log2Dim>
1960template<typename AccessorT>
1961inline void
1963 const ValueType& value, AccessorT& acc)
1964{
1965 const Index n = this->coordToOffset(xyz);
1966 bool hasChild = this->isChildMaskOn(n);
1967 if (!hasChild) {
1968 const bool active = this->isValueMaskOn(n);
1969 if (!active || !math::isExactlyEqual(mNodes[n].getValue(), value)) {
1970 // If the voxel belongs to a tile that is either inactive or that
1971 // has a constant value that is different from the one provided,
1972 // a child subtree must be constructed.
1973 hasChild = true;
1974 this->setChildNode(n, new ChildNodeType(xyz, mNodes[n].getValue(), active));
1975 }
1976 }
1977 if (hasChild) {
1978 acc.insert(xyz, mNodes[n].getChild());
1979 mNodes[n].getChild()->setValueAndCache(xyz, value, acc);
1980 }
1981}
1982
1983
1984template<typename ChildT, Index Log2Dim>
1985inline void
1987{
1988 const Index n = this->coordToOffset(xyz);
1989 bool hasChild = this->isChildMaskOn(n);
1990 if (!hasChild && !math::isExactlyEqual(mNodes[n].getValue(), value)) {
1991 // If the voxel has a tile value that is different from the one provided,
1992 // a child subtree must be constructed.
1993 const bool active = this->isValueMaskOn(n);
1994 hasChild = true;
1995 this->setChildNode(n, new ChildNodeType(xyz, mNodes[n].getValue(), active));
1996 }
1997 if (hasChild) mNodes[n].getChild()->setValueOnly(xyz, value);
1998}
1999
2000template<typename ChildT, Index Log2Dim>
2001template<typename AccessorT>
2002inline void
2004 const ValueType& value, AccessorT& acc)
2005{
2006 const Index n = this->coordToOffset(xyz);
2007 bool hasChild = this->isChildMaskOn(n);
2008 if (!hasChild && !math::isExactlyEqual(mNodes[n].getValue(), value)) {
2009 // If the voxel has a tile value that is different from the one provided,
2010 // a child subtree must be constructed.
2011 const bool active = this->isValueMaskOn(n);
2012 hasChild = true;
2013 this->setChildNode(n, new ChildNodeType(xyz, mNodes[n].getValue(), active));
2014 }
2015 if (hasChild) {
2016 acc.insert(xyz, mNodes[n].getChild());
2017 mNodes[n].getChild()->setValueOnlyAndCache(xyz, value, acc);
2018 }
2019}
2020
2021
2022template<typename ChildT, Index Log2Dim>
2023inline void
2025{
2026 const Index n = this->coordToOffset(xyz);
2027 bool hasChild = this->isChildMaskOn(n);
2028 if (!hasChild) {
2029 if (on != this->isValueMaskOn(n)) {
2030 // If the voxel belongs to a tile with the wrong active state,
2031 // then a child subtree must be constructed.
2032 // 'on' is the voxel's new state, therefore '!on' is the tile's current state
2033 hasChild = true;
2034 this->setChildNode(n, new ChildNodeType(xyz, mNodes[n].getValue(), !on));
2035 }
2036 }
2037 if (hasChild) mNodes[n].getChild()->setActiveState(xyz, on);
2038}
2039
2040template<typename ChildT, Index Log2Dim>
2041template<typename AccessorT>
2042inline void
2044{
2045 const Index n = this->coordToOffset(xyz);
2046 bool hasChild = this->isChildMaskOn(n);
2047 if (!hasChild) {
2048 if (on != this->isValueMaskOn(n)) {
2049 // If the voxel belongs to a tile with the wrong active state,
2050 // then a child subtree must be constructed.
2051 // 'on' is the voxel's new state, therefore '!on' is the tile's current state
2052 hasChild = true;
2053 this->setChildNode(n, new ChildNodeType(xyz, mNodes[n].getValue(), !on));
2054 }
2055 }
2056 if (hasChild) {
2057 ChildT* child = mNodes[n].getChild();
2058 acc.insert(xyz, child);
2059 child->setActiveStateAndCache(xyz, on, acc);
2060 }
2061}
2062
2063
2064template<typename ChildT, Index Log2Dim>
2065inline void
2067{
2069 for (ChildOnIter iter = this->beginChildOn(); iter; ++iter) {
2070 mNodes[iter.pos()].getChild()->setValuesOn();
2071 }
2072}
2073
2074
2075template<typename ChildT, Index Log2Dim>
2076template<typename ModifyOp>
2077inline void
2079{
2080 const Index n = InternalNode::coordToOffset(xyz);
2081 bool hasChild = this->isChildMaskOn(n);
2082 if (!hasChild) {
2083 // Need to create a child if the tile is inactive,
2084 // in order to activate voxel (x, y, z).
2085 const bool active = this->isValueMaskOn(n);
2086 bool createChild = !active;
2087 if (!createChild) {
2088 // Need to create a child if applying the functor
2089 // to the tile value produces a different value.
2090 const ValueType& tileVal = mNodes[n].getValue();
2091 ValueType modifiedVal = tileVal;
2092 op(modifiedVal);
2093 createChild = !math::isExactlyEqual(tileVal, modifiedVal);
2094 }
2095 if (createChild) {
2096 hasChild = true;
2097 this->setChildNode(n, new ChildNodeType(xyz, mNodes[n].getValue(), active));
2098 }
2099 }
2100 if (hasChild) mNodes[n].getChild()->modifyValue(xyz, op);
2101}
2102
2103template<typename ChildT, Index Log2Dim>
2104template<typename ModifyOp, typename AccessorT>
2105inline void
2107 AccessorT& acc)
2108{
2109 const Index n = InternalNode::coordToOffset(xyz);
2110 bool hasChild = this->isChildMaskOn(n);
2111 if (!hasChild) {
2112 // Need to create a child if the tile is inactive,
2113 // in order to activate voxel (x, y, z).
2114 const bool active = this->isValueMaskOn(n);
2115 bool createChild = !active;
2116 if (!createChild) {
2117 // Need to create a child if applying the functor
2118 // to the tile value produces a different value.
2119 const ValueType& tileVal = mNodes[n].getValue();
2120 ValueType modifiedVal = tileVal;
2121 op(modifiedVal);
2122 createChild = !math::isExactlyEqual(tileVal, modifiedVal);
2123 }
2124 if (createChild) {
2125 hasChild = true;
2126 this->setChildNode(n, new ChildNodeType(xyz, mNodes[n].getValue(), active));
2127 }
2128 }
2129 if (hasChild) {
2130 ChildNodeType* child = mNodes[n].getChild();
2131 acc.insert(xyz, child);
2132 child->modifyValueAndCache(xyz, op, acc);
2133 }
2134}
2135
2136
2137template<typename ChildT, Index Log2Dim>
2138template<typename ModifyOp>
2139inline void
2141{
2142 const Index n = InternalNode::coordToOffset(xyz);
2143 bool hasChild = this->isChildMaskOn(n);
2144 if (!hasChild) {
2145 const bool tileState = this->isValueMaskOn(n);
2146 const ValueType& tileVal = mNodes[n].getValue();
2147 bool modifiedState = !tileState;
2148 ValueType modifiedVal = tileVal;
2149 op(modifiedVal, modifiedState);
2150 // Need to create a child if applying the functor to the tile
2151 // produces a different value or active state.
2152 if (modifiedState != tileState || !math::isExactlyEqual(modifiedVal, tileVal)) {
2153 hasChild = true;
2154 this->setChildNode(n, new ChildNodeType(xyz, tileVal, tileState));
2155 }
2156 }
2157 if (hasChild) mNodes[n].getChild()->modifyValueAndActiveState(xyz, op);
2158}
2159
2160template<typename ChildT, Index Log2Dim>
2161template<typename ModifyOp, typename AccessorT>
2162inline void
2164 const Coord& xyz, const ModifyOp& op, AccessorT& acc)
2165{
2166 const Index n = InternalNode::coordToOffset(xyz);
2167 bool hasChild = this->isChildMaskOn(n);
2168 if (!hasChild) {
2169 const bool tileState = this->isValueMaskOn(n);
2170 const ValueType& tileVal = mNodes[n].getValue();
2171 bool modifiedState = !tileState;
2172 ValueType modifiedVal = tileVal;
2173 op(modifiedVal, modifiedState);
2174 // Need to create a child if applying the functor to the tile
2175 // produces a different value or active state.
2176 if (modifiedState != tileState || !math::isExactlyEqual(modifiedVal, tileVal)) {
2177 hasChild = true;
2178 this->setChildNode(n, new ChildNodeType(xyz, tileVal, tileState));
2179 }
2180 }
2181 if (hasChild) {
2182 ChildNodeType* child = mNodes[n].getChild();
2183 acc.insert(xyz, child);
2184 child->modifyValueAndActiveStateAndCache(xyz, op, acc);
2185 }
2186}
2187
2188
2189////////////////////////////////////////
2190
2191
2192template<typename ChildT, Index Log2Dim>
2193inline void
2195{
2196 CoordBBox nodeBBox = this->getNodeBoundingBox();
2197 if (!clipBBox.hasOverlap(nodeBBox)) {
2198 // This node lies completely outside the clipping region. Fill it with background tiles.
2199 this->fill(nodeBBox, background, /*active=*/false);
2200 } else if (clipBBox.isInside(nodeBBox)) {
2201 // This node lies completely inside the clipping region. Leave it intact.
2202 return;
2203 }
2204
2205 // This node isn't completely contained inside the clipping region.
2206 // Clip tiles and children, and replace any that lie outside the region
2207 // with background tiles.
2208
2209 for (Index pos = 0; pos < NUM_VALUES; ++pos) {
2210 const Coord xyz = this->offsetToGlobalCoord(pos); // tile or child origin
2211 CoordBBox tileBBox(xyz, xyz.offsetBy(ChildT::DIM - 1)); // tile or child bounds
2212 if (!clipBBox.hasOverlap(tileBBox)) {
2213 // This table entry lies completely outside the clipping region.
2214 // Replace it with a background tile.
2215 this->makeChildNodeEmpty(pos, background);
2216 mValueMask.setOff(pos);
2217 } else if (!clipBBox.isInside(tileBBox)) {
2218 // This table entry does not lie completely inside the clipping region
2219 // and must be clipped.
2220 if (this->isChildMaskOn(pos)) {
2221 mNodes[pos].getChild()->clip(clipBBox, background);
2222 } else {
2223 // Replace this tile with a background tile, then fill the clip region
2224 // with the tile's original value. (This might create a child branch.)
2225 tileBBox.intersect(clipBBox);
2226 const ValueType val = mNodes[pos].getValue();
2227 const bool on = this->isValueMaskOn(pos);
2228 mNodes[pos].setValue(background);
2229 mValueMask.setOff(pos);
2230 this->fill(tileBBox, val, on);
2231 }
2232 } else {
2233 // This table entry lies completely inside the clipping region. Leave it intact.
2234 }
2235 }
2236}
2237
2238
2239////////////////////////////////////////
2240
2241
2242template<typename ChildT, Index Log2Dim>
2243inline void
2244InternalNode<ChildT, Log2Dim>::fill(const CoordBBox& bbox, const ValueType& value, bool active)
2245{
2246 auto clippedBBox = this->getNodeBoundingBox();
2247 clippedBBox.intersect(bbox);
2248 if (!clippedBBox) return;
2249
2250 // Iterate over the fill region in axis-aligned, tile-sized chunks.
2251 // (The first and last chunks along each axis might be smaller than a tile.)
2252 Coord xyz, tileMin, tileMax;
2253 for (int x = clippedBBox.min().x(); x <= clippedBBox.max().x(); x = tileMax.x() + 1) {
2254 xyz.setX(x);
2255 for (int y = clippedBBox.min().y(); y <= clippedBBox.max().y(); y = tileMax.y() + 1) {
2256 xyz.setY(y);
2257 for (int z = clippedBBox.min().z(); z <= clippedBBox.max().z(); z = tileMax.z() + 1) {
2258 xyz.setZ(z);
2259
2260 // Get the bounds of the tile that contains voxel (x, y, z).
2261 const Index n = this->coordToOffset(xyz);
2262 tileMin = this->offsetToGlobalCoord(n);
2263 tileMax = tileMin.offsetBy(ChildT::DIM - 1);
2264
2265 if (xyz != tileMin || Coord::lessThan(clippedBBox.max(), tileMax)) {
2266 // If the box defined by (xyz, clippedBBox.max()) doesn't completely enclose
2267 // the tile to which xyz belongs, create a child node (or retrieve
2268 // the existing one).
2269 ChildT* child = nullptr;
2270 if (this->isChildMaskOff(n)) {
2271 // Replace the tile with a newly-created child that is initialized
2272 // with the tile's value and active state.
2273 child = new ChildT{xyz, mNodes[n].getValue(), this->isValueMaskOn(n)};
2274 this->setChildNode(n, child);
2275 } else {
2276 child = mNodes[n].getChild();
2277 }
2278
2279 // Forward the fill request to the child.
2280 if (child) {
2281 const Coord tmp = Coord::minComponent(clippedBBox.max(), tileMax);
2282 child->fill(CoordBBox(xyz, tmp), value, active);
2283 }
2284
2285 } else {
2286 // If the box given by (xyz, clippedBBox.max()) completely encloses
2287 // the tile to which xyz belongs, create the tile (if it
2288 // doesn't already exist) and give it the fill value.
2289 this->makeChildNodeEmpty(n, value);
2290 mValueMask.set(n, active);
2291 }
2292 }
2293 }
2294 }
2295}
2296
2297
2298template<typename ChildT, Index Log2Dim>
2299inline void
2300InternalNode<ChildT, Log2Dim>::denseFill(const CoordBBox& bbox, const ValueType& value, bool active)
2301{
2302 auto clippedBBox = this->getNodeBoundingBox();
2303 clippedBBox.intersect(bbox);
2304 if (!clippedBBox) return;
2305
2306 // Iterate over the fill region in axis-aligned, tile-sized chunks.
2307 // (The first and last chunks along each axis might be smaller than a tile.)
2308 Coord xyz, tileMin, tileMax;
2309 for (int x = clippedBBox.min().x(); x <= clippedBBox.max().x(); x = tileMax.x() + 1) {
2310 xyz.setX(x);
2311 for (int y = clippedBBox.min().y(); y <= clippedBBox.max().y(); y = tileMax.y() + 1) {
2312 xyz.setY(y);
2313 for (int z = clippedBBox.min().z(); z <= clippedBBox.max().z(); z = tileMax.z() + 1) {
2314 xyz.setZ(z);
2315
2316 // Get the table index of the tile that contains voxel (x, y, z).
2317 const auto n = this->coordToOffset(xyz);
2318
2319 // Retrieve the child node at index n, or replace the tile at index n with a child.
2320 ChildT* child = nullptr;
2321 if (this->isChildMaskOn(n)) {
2322 child = mNodes[n].getChild();
2323 } else {
2324 // Replace the tile with a newly-created child that is filled
2325 // with the tile's value and active state.
2326 child = new ChildT{xyz, mNodes[n].getValue(), this->isValueMaskOn(n)};
2327 this->setChildNode(n, child);
2328 }
2329
2330 // Get the bounds of the tile that contains voxel (x, y, z).
2331 tileMin = this->offsetToGlobalCoord(n);
2332 tileMax = tileMin.offsetBy(ChildT::DIM - 1);
2333
2334 // Forward the fill request to the child.
2335 child->denseFill(CoordBBox{xyz, clippedBBox.max()}, value, active);
2336 }
2337 }
2338 }
2339}
2340
2341
2342////////////////////////////////////////
2343
2344
2345template<typename ChildT, Index Log2Dim>
2346template<typename DenseT>
2347inline void
2349{
2350 using DenseValueType = typename DenseT::ValueType;
2351
2352 const size_t xStride = dense.xStride(), yStride = dense.yStride(), zStride = dense.zStride();
2353 const Coord& min = dense.bbox().min();
2354 for (Coord xyz = bbox.min(), max; xyz[0] <= bbox.max()[0]; xyz[0] = max[0] + 1) {
2355 for (xyz[1] = bbox.min()[1]; xyz[1] <= bbox.max()[1]; xyz[1] = max[1] + 1) {
2356 for (xyz[2] = bbox.min()[2]; xyz[2] <= bbox.max()[2]; xyz[2] = max[2] + 1) {
2357 const Index n = this->coordToOffset(xyz);
2358 // Get max coordinates of the child node that contains voxel xyz.
2359 max = this->offsetToGlobalCoord(n).offsetBy(ChildT::DIM-1);
2360
2361 // Get the bbox of the interection of bbox and the child node
2362 CoordBBox sub(xyz, Coord::minComponent(bbox.max(), max));
2363
2364 if (this->isChildMaskOn(n)) {//is a child
2365 mNodes[n].getChild()->copyToDense(sub, dense);
2366 } else {//a tile value
2367 const ValueType value = mNodes[n].getValue();
2368 sub.translate(-min);
2369 DenseValueType* a0 = dense.data() + zStride*sub.min()[2];
2370 for (Int32 x=sub.min()[0], ex=sub.max()[0]+1; x<ex; ++x) {
2371 DenseValueType* a1 = a0 + x*xStride;
2372 for (Int32 y=sub.min()[1], ey=sub.max()[1]+1; y<ey; ++y) {
2373 DenseValueType* a2 = a1 + y*yStride;
2374 for (Int32 z = sub.min()[2], ez = sub.max()[2]+1;
2375 z < ez; ++z, a2 += zStride)
2376 {
2377 *a2 = DenseValueType(value);
2378 }
2379 }
2380 }
2381 }
2382 }
2383 }
2384 }
2385}
2386
2387
2388////////////////////////////////////////
2389
2390
2391template<typename ChildT, Index Log2Dim>
2392inline void
2393InternalNode<ChildT, Log2Dim>::writeTopology(std::ostream& os, bool toHalf) const
2394{
2395 mChildMask.save(os);
2396 mValueMask.save(os);
2397
2398 {
2399 // Copy all of this node's values into an array.
2400 std::unique_ptr<ValueType[]> valuePtr(new ValueType[NUM_VALUES]);
2401 ValueType* values = valuePtr.get();
2402 const ValueType zero = zeroVal<ValueType>();
2403 for (Index i = 0; i < NUM_VALUES; ++i) {
2404 values[i] = (mChildMask.isOff(i) ? mNodes[i].getValue() : zero);
2405 }
2406 // Compress (optionally) and write out the contents of the array.
2408 }
2409 // Write out the child nodes in order.
2410 for (ChildOnCIter iter = this->cbeginChildOn(); iter; ++iter) {
2411 iter->writeTopology(os, toHalf);
2412 }
2413}
2414
2415
2416template<typename ChildT, Index Log2Dim>
2417inline void
2418InternalNode<ChildT, Log2Dim>::readTopology(std::istream& is, bool fromHalf)
2419{
2421
2423 : *static_cast<const ValueType*>(io::getGridBackgroundValuePtr(is)));
2424
2425 mChildMask.load(is);
2426 mValueMask.load(is);
2427
2428 const bool oldVersion =
2430 const Index numValues = (oldVersion ? mChildMask.countOff() : NUM_VALUES);
2431 {
2432 // Read in (and uncompress, if necessary) all of this node's values
2433 // into a contiguous array.
2434 std::unique_ptr<ValueType[]> valuePtr(new ValueType[numValues]);
2435 ValueType* values = valuePtr.get();
2436 io::readCompressedValues(is, values, numValues, mValueMask, fromHalf);
2437
2438 // Copy values from the array into this node's table.
2439 if (oldVersion) {
2440 Index n = 0;
2441 for (ValueAllIter iter = this->beginValueAll(); iter; ++iter) {
2442 mNodes[iter.pos()].setValue(values[n++]);
2443 }
2444 OPENVDB_ASSERT(n == numValues);
2445 } else {
2446 for (ValueAllIter iter = this->beginValueAll(); iter; ++iter) {
2447 mNodes[iter.pos()].setValue(values[iter.pos()]);
2448 }
2449 }
2450 }
2451
2452 // Read in all child nodes and insert them into the table at their proper locations.
2453 for (ChildOnIter iter = this->beginChildOn(); iter; ++iter) {
2454 ChildNodeType* child = new ChildNodeType(PartialCreate(), iter.getCoord(), background);
2455 mNodes[iter.pos()].setChild(child);
2456 child->readTopology(is, fromHalf);
2457 }
2458}
2459
2460
2461////////////////////////////////////////
2462
2463
2464template<typename ChildT, Index Log2Dim>
2465inline const typename ChildT::ValueType&
2467{
2468 return (this->isChildMaskOn(0) ? mNodes[0].getChild()->getFirstValue() : mNodes[0].getValue());
2469}
2470
2471
2472template<typename ChildT, Index Log2Dim>
2473inline const typename ChildT::ValueType&
2475{
2476 const Index n = NUM_VALUES - 1;
2477 return (this->isChildMaskOn(n) ? mNodes[n].getChild()->getLastValue() : mNodes[n].getValue());
2478}
2479
2480
2481////////////////////////////////////////
2482
2483
2484template<typename ChildT, Index Log2Dim>
2485inline const typename ChildT::ValueType&
2487{
2489 OPENVDB_ASSERT(mChildMask.isOff(n));
2490 return mNodes[n].getValue();
2491}
2492
2493template<typename ChildT, Index Log2Dim>
2494inline bool
2496{
2498 OPENVDB_ASSERT(mChildMask.isOff(n));
2499 value = mNodes[n].getValue();
2500 return mValueMask.isOn(n);
2501}
2502
2503template<typename ChildT, Index Log2Dim>
2504inline ChildT*
2506{
2508 OPENVDB_ASSERT(mChildMask.isOn(n));
2509 return mNodes[n].getChild();
2510}
2511
2512template<typename ChildT, Index Log2Dim>
2513inline const ChildT*
2515{
2517 OPENVDB_ASSERT(mChildMask.isOn(n));
2518 return mNodes[n].getChild();
2519}
2520
2521template<typename ChildT, Index Log2Dim>
2522inline const ChildT*
2527
2528template<typename ChildT, Index Log2Dim>
2529inline void
2536
2537template<typename ChildT, Index Log2Dim>
2538inline void
2540{
2542 OPENVDB_ASSERT(mChildMask.isOff(n));
2543 mNodes[n].setValue(value);
2544}
2545
2546template<typename ChildT, Index Log2Dim>
2547inline void
2554
2555template<typename ChildT, Index Log2Dim>
2556inline void
2558{
2560 OPENVDB_ASSERT(mChildMask.isOff(n));
2561 mNodes[n].setValue(value);
2562 mValueMask.setOn(n);
2563}
2564
2565template<typename ChildT, Index Log2Dim>
2566inline void
2573
2574template<typename ChildT, Index Log2Dim>
2575inline void
2577{
2579 OPENVDB_ASSERT(mChildMask.isOff(n));
2580 mNodes[n].setValue(value);
2581 mValueMask.setOff(n);
2582}
2583
2584template<typename ChildT, Index Log2Dim>
2585inline void
2591
2592template<typename ChildT, Index Log2Dim>
2593inline void
2595{
2596 // replace tile with child
2598 OPENVDB_ASSERT(mChildMask.isOff(n));
2599 mNodes[n].setChild(child);
2600 mChildMask.setOn(n);
2601 mValueMask.setOff(n);
2602}
2603
2604template<typename ChildT, Index Log2Dim>
2605inline void
2607{
2608 // replace child with child
2609 OPENVDB_ASSERT(child);
2611 OPENVDB_ASSERT(mChildMask.isOn(n));
2612 delete mNodes[n].getChild();
2613 mNodes[n].setChild(child);
2614}
2615
2616template<typename ChildT, Index Log2Dim>
2617inline ChildT*
2619{
2620 // replace child with tile (and return child)
2622 OPENVDB_ASSERT(mChildMask.isOn(n));
2623 auto* child = mNodes[n].getChild();
2624 mChildMask.setOff(n);
2625 mValueMask.set(n, active);
2626 mNodes[n].setValue(value);
2627 return child;
2628}
2629
2630template<typename ChildT, Index Log2Dim>
2631inline void
2633{
2634 // replace child with tile (and delete child)
2635 delete this->stealChildUnsafe(n, value, active);
2636}
2637
2638
2639////////////////////////////////////////
2640
2641
2642template<typename ChildT, Index Log2Dim>
2643inline void
2645{
2646 for (Index i = 0; i < NUM_VALUES; ++i) {
2647 if (this->isChildMaskOn(i)) {
2648 mNodes[i].getChild()->negate();
2649 } else {
2650 mNodes[i].setValue(math::negative(mNodes[i].getValue()));
2651 }
2652 }
2653
2654}
2655
2656
2657////////////////////////////////////////
2658
2659
2660template<typename ChildT, Index Log2Dim>
2662{
2664 //(*this)(tbb::blocked_range<Index>(0, NUM_VALUES));//single thread for debugging
2665 tbb::parallel_for(tbb::blocked_range<Index>(0, NUM_VALUES), *this);
2666
2667 node.mChildMask |= node.mValueMask;
2668 node.mValueMask.setOff();
2669 }
2670 void operator()(const tbb::blocked_range<Index> &r) const
2671 {
2672 for (Index i = r.begin(), end=r.end(); i!=end; ++i) {
2673 if (mNode->mChildMask.isOn(i)) {// Loop over node's child nodes
2674 mNode->mNodes[i].getChild()->voxelizeActiveTiles(true);
2675 } else if (mNode->mValueMask.isOn(i)) {// Loop over node's active tiles
2676 const Coord &ijk = mNode->offsetToGlobalCoord(i);
2677 ChildNodeType *child = new ChildNodeType(ijk, mNode->mNodes[i].getValue(), true);
2678 child->voxelizeActiveTiles(true);
2679 mNode->mNodes[i].setChild(child);
2680 }
2681 }
2682 }
2684};// VoxelizeActiveTiles
2685
2686template<typename ChildT, Index Log2Dim>
2687inline void
2689{
2690 if (threaded) {
2691 VoxelizeActiveTiles tmp(*this);
2692 } else {
2693 for (ValueOnIter iter = this->beginValueOn(); iter; ++iter) {
2694 this->setChildNode(iter.pos(),
2695 new ChildNodeType(iter.getCoord(), iter.getValue(), true));
2696 }
2697 for (ChildOnIter iter = this->beginChildOn(); iter; ++iter)
2698 iter->voxelizeActiveTiles(false);
2699 }
2700}
2701
2702
2703////////////////////////////////////////
2704
2705
2706template<typename ChildT, Index Log2Dim>
2707template<MergePolicy Policy>
2708inline void
2710 const ValueType& background, const ValueType& otherBackground)
2711{
2713
2714 switch (Policy) {
2715
2717 default:
2718 {
2719 for (ChildOnIter iter = other.beginChildOn(); iter; ++iter) {
2720 const Index n = iter.pos();
2721 if (mChildMask.isOn(n)) {
2722 // Merge this node's child with the other node's child.
2723 mNodes[n].getChild()->template merge<MERGE_ACTIVE_STATES>(*iter,
2724 background, otherBackground);
2725 } else if (mValueMask.isOff(n)) {
2726 // Replace this node's inactive tile with the other node's child
2727 // and replace the other node's child with a tile of undefined value
2728 // (which is okay since the other tree is assumed to be cannibalized
2729 // in the process of merging).
2730 ChildNodeType* child = other.mNodes[n].getChild();
2731 other.mChildMask.setOff(n);
2732 child->resetBackground(otherBackground, background);
2733 this->setChildNode(n, child);
2734 }
2735 }
2736
2737 // Copy active tile values.
2738 for (ValueOnCIter iter = other.cbeginValueOn(); iter; ++iter) {
2739 const Index n = iter.pos();
2740 if (mValueMask.isOff(n)) {
2741 // Replace this node's child or inactive tile with the other node's active tile.
2742 this->makeChildNodeEmpty(n, iter.getValue());
2743 mValueMask.setOn(n);
2744 }
2745 }
2746 break;
2747 }
2748
2749 case MERGE_NODES:
2750 {
2751 for (ChildOnIter iter = other.beginChildOn(); iter; ++iter) {
2752 const Index n = iter.pos();
2753 if (mChildMask.isOn(n)) {
2754 // Merge this node's child with the other node's child.
2755 mNodes[n].getChild()->template merge<Policy>(*iter, background, otherBackground);
2756 } else {
2757 // Replace this node's tile (regardless of its active state) with
2758 // the other node's child and replace the other node's child with
2759 // a tile of undefined value (which is okay since the other tree
2760 // is assumed to be cannibalized in the process of merging).
2761 ChildNodeType* child = other.mNodes[n].getChild();
2762 other.mChildMask.setOff(n);
2763 child->resetBackground(otherBackground, background);
2764 this->setChildNode(n, child);
2765 }
2766 }
2767 break;
2768 }
2769
2771 {
2772 // Transfer children from the other tree to this tree.
2773 for (ChildOnIter iter = other.beginChildOn(); iter; ++iter) {
2774 const Index n = iter.pos();
2775 if (mChildMask.isOn(n)) {
2776 // Merge this node's child with the other node's child.
2777 mNodes[n].getChild()->template merge<Policy>(*iter, background, otherBackground);
2778 } else {
2779 // Replace this node's tile with the other node's child, leaving the other
2780 // node with an inactive tile of undefined value (which is okay since
2781 // the other tree is assumed to be cannibalized in the process of merging).
2782 ChildNodeType* child = other.mNodes[n].getChild();
2783 other.mChildMask.setOff(n);
2784 child->resetBackground(otherBackground, background);
2785 if (mValueMask.isOn(n)) {
2786 // Merge the child with this node's active tile.
2787 child->template merge<Policy>(mNodes[n].getValue(), /*on=*/true);
2788 mValueMask.setOff(n);
2789 }
2790 mChildMask.setOn(n);
2791 mNodes[n].setChild(child);
2792 }
2793 }
2794
2795 // Merge active tiles into this tree.
2796 for (ValueOnCIter iter = other.cbeginValueOn(); iter; ++iter) {
2797 const Index n = iter.pos();
2798 if (mChildMask.isOn(n)) {
2799 // Merge the other node's active tile into this node's child.
2800 mNodes[n].getChild()->template merge<Policy>(iter.getValue(), /*on=*/true);
2801 } else if (mValueMask.isOff(n)) {
2802 // Replace this node's inactive tile with the other node's active tile.
2803 mNodes[n].setValue(iter.getValue());
2804 mValueMask.setOn(n);
2805 }
2806 }
2807 break;
2808 }
2809
2810 }
2812}
2813
2814
2815template<typename ChildT, Index Log2Dim>
2816template<MergePolicy Policy>
2817inline void
2818InternalNode<ChildT, Log2Dim>::merge(const ValueType& tileValue, bool tileActive)
2819{
2821
2822 if (Policy != MERGE_ACTIVE_STATES_AND_NODES) return;
2823
2824 // For MERGE_ACTIVE_STATES_AND_NODES, inactive tiles in the other tree are ignored.
2825 if (!tileActive) return;
2826
2827 // Iterate over this node's children and inactive tiles.
2828 for (ValueOffIter iter = this->beginValueOff(); iter; ++iter) {
2829 const Index n = iter.pos();
2830 if (mChildMask.isOn(n)) {
2831 // Merge the other node's active tile into this node's child.
2832 mNodes[n].getChild()->template merge<Policy>(tileValue, /*on=*/true);
2833 } else {
2834 // Replace this node's inactive tile with the other node's active tile.
2835 iter.setValue(tileValue);
2836 mValueMask.setOn(n);
2837 }
2838 }
2840}
2841
2842
2843////////////////////////////////////////
2844
2845
2846template<typename ChildT, Index Log2Dim>
2847template<typename OtherInternalNode>
2849{
2850 using W = typename NodeMaskType::Word;
2851 struct A { inline void operator()(W &tV, const W& sV, const W& tC) const
2852 { tV = (tV | sV) & ~tC; }
2853 };
2854 TopologyUnion(const OtherInternalNode* source, InternalNode* target, const bool preserveTiles)
2855 : s(source), t(target), mPreserveTiles(preserveTiles) {
2856 //(*this)(tbb::blocked_range<Index>(0, NUM_VALUES));//single thread for debugging
2857 tbb::parallel_for(tbb::blocked_range<Index>(0, NUM_VALUES), *this);
2858
2859 // Bit processing is done in a single thread!
2860 if (!mPreserveTiles) t->mChildMask |= s->mChildMask;//serial but very fast bitwise post-process
2861 else t->mChildMask |= (s->mChildMask & !t->mValueMask);
2862
2863 A op;
2864 t->mValueMask.foreach(s->mValueMask, t->mChildMask, op);
2865 OPENVDB_ASSERT((t->mValueMask & t->mChildMask).isOff());//no overlapping active tiles or child nodes
2866 }
2867 void operator()(const tbb::blocked_range<Index> &r) const {
2868 for (Index i = r.begin(), end=r.end(); i!=end; ++i) {
2869 if (s->mChildMask.isOn(i)) {// Loop over other node's child nodes
2870 const typename OtherInternalNode::ChildNodeType& other = *(s->mNodes[i].getChild());
2871 if (t->mChildMask.isOn(i)) {//this has a child node
2872 t->mNodes[i].getChild()->topologyUnion(other, mPreserveTiles);
2873 } else {// this is a tile so replace it with a child branch with identical topology
2874 if (!mPreserveTiles || t->mValueMask.isOff(i)) { // force child topology
2875 ChildT* child = new ChildT(other, t->mNodes[i].getValue(), TopologyCopy());
2876 if (t->mValueMask.isOn(i)) child->setValuesOn();//activate all values
2877 t->mNodes[i].setChild(child);
2878 }
2879 }
2880 } else if (s->mValueMask.isOn(i) && t->mChildMask.isOn(i)) {
2881 t->mNodes[i].getChild()->setValuesOn();
2882 }
2883 }
2884 }
2885 const OtherInternalNode* s;
2887 const bool mPreserveTiles;
2888};// TopologyUnion
2889
2890template<typename ChildT, Index Log2Dim>
2891template<typename OtherChildT>
2892inline void
2894{
2895 TopologyUnion<InternalNode<OtherChildT, Log2Dim> > tmp(&other, this, preserveTiles);
2896}
2897
2898template<typename ChildT, Index Log2Dim>
2899template<typename OtherInternalNode>
2900struct InternalNode<ChildT, Log2Dim>::TopologyIntersection
2901{
2902 using W = typename NodeMaskType::Word;
2903 struct A { inline void operator()(W &tC, const W& sC, const W& sV, const W& tV) const
2904 { tC = (tC & (sC | sV)) | (tV & sC); }
2905 };
2906 TopologyIntersection(const OtherInternalNode* source, InternalNode* target,
2907 const ValueType& background) : s(source), t(target), b(background) {
2908 //(*this)(tbb::blocked_range<Index>(0, NUM_VALUES));//single thread for debugging
2909 tbb::parallel_for(tbb::blocked_range<Index>(0, NUM_VALUES), *this);
2910
2911 // Bit processing is done in a single thread!
2912 A op;
2913 t->mChildMask.foreach(s->mChildMask, s->mValueMask, t->mValueMask, op);
2914
2915 t->mValueMask &= s->mValueMask;
2916 OPENVDB_ASSERT((t->mValueMask & t->mChildMask).isOff());//no overlapping active tiles or child nodes
2917 }
2918 void operator()(const tbb::blocked_range<Index> &r) const {
2919 for (Index i = r.begin(), end=r.end(); i!=end; ++i) {
2920 if (t->mChildMask.isOn(i)) {// Loop over this node's child nodes
2921 ChildT* child = t->mNodes[i].getChild();
2922 if (s->mChildMask.isOn(i)) {//other also has a child node
2923 child->topologyIntersection(*(s->mNodes[i].getChild()), b);
2924 } else if (s->mValueMask.isOff(i)) {//other is an inactive tile
2925 delete child;//convert child to an inactive tile
2926 t->mNodes[i].setValue(b);
2927 }
2928 } else if (t->mValueMask.isOn(i) && s->mChildMask.isOn(i)) {//active tile -> a branch
2929 t->mNodes[i].setChild(new ChildT(*(s->mNodes[i].getChild()),
2930 t->mNodes[i].getValue(), TopologyCopy()));
2931 }
2932 }
2933 }
2934 const OtherInternalNode* s;
2936 const ValueType& b;
2937};// TopologyIntersection
2938
2939template<typename ChildT, Index Log2Dim>
2940template<typename OtherChildT>
2941inline void
2943 const InternalNode<OtherChildT, Log2Dim>& other, const ValueType& background)
2944{
2945 TopologyIntersection<InternalNode<OtherChildT, Log2Dim> > tmp(&other, this, background);
2946}
2947
2948template<typename ChildT, Index Log2Dim>
2949template<typename OtherInternalNode>
2950struct InternalNode<ChildT, Log2Dim>::TopologyDifference
2951{
2952 using W = typename NodeMaskType::Word;
2953 struct A {inline void operator()(W &tC, const W& sC, const W& sV, const W& tV) const
2954 { tC = (tC & (sC | ~sV)) | (tV & sC); }
2955 };
2956 struct B {inline void operator()(W &tV, const W& sC, const W& sV, const W& tC) const
2957 { tV &= ~((tC & sV) | (sC | sV)); }
2958 };
2959 TopologyDifference(const OtherInternalNode* source, InternalNode* target,
2960 const ValueType& background) : s(source), t(target), b(background) {
2961 //(*this)(tbb::blocked_range<Index>(0, NUM_VALUES));//single thread for debugging
2962 tbb::parallel_for(tbb::blocked_range<Index>(0, NUM_VALUES), *this);
2963
2964 // Bit processing is done in a single thread!
2965 const NodeMaskType oldChildMask(t->mChildMask);//important to avoid cross pollution
2966 A op1;
2967 t->mChildMask.foreach(s->mChildMask, s->mValueMask, t->mValueMask, op1);
2968
2969 B op2;
2970 t->mValueMask.foreach(t->mChildMask, s->mValueMask, oldChildMask, op2);
2971 OPENVDB_ASSERT((t->mValueMask & t->mChildMask).isOff());//no overlapping active tiles or child nodes
2972 }
2973 void operator()(const tbb::blocked_range<Index> &r) const {
2974 for (Index i = r.begin(), end=r.end(); i!=end; ++i) {
2975 if (t->mChildMask.isOn(i)) {// Loop over this node's child nodes
2976 ChildT* child = t->mNodes[i].getChild();
2977 if (s->mChildMask.isOn(i)) {
2978 child->topologyDifference(*(s->mNodes[i].getChild()), b);
2979 } else if (s->mValueMask.isOn(i)) {
2980 delete child;//convert child to an inactive tile
2981 t->mNodes[i].setValue(b);
2982 }
2983 } else if (t->mValueMask.isOn(i)) {//this is an active tile
2984 if (s->mChildMask.isOn(i)) {
2985 const typename OtherInternalNode::ChildNodeType& other =
2986 *(s->mNodes[i].getChild());
2987 ChildT* child = new ChildT(other.origin(), t->mNodes[i].getValue(), true);
2988 child->topologyDifference(other, b);
2989 t->mNodes[i].setChild(child);//replace the active tile with a child branch
2990 }
2991 }
2992 }
2993 }
2994 const OtherInternalNode* s;
2996 const ValueType& b;
2997};// TopologyDifference
2998
2999template<typename ChildT, Index Log2Dim>
3000template<typename OtherChildT>
3001inline void
3003 const ValueType& background)
3004{
3005 TopologyDifference<InternalNode<OtherChildT, Log2Dim> > tmp(&other, this, background);
3006}
3007
3008
3009////////////////////////////////////////
3010
3011
3012template<typename ChildT, Index Log2Dim>
3013template<typename CombineOp>
3014inline void
3016{
3017 const ValueType zero = zeroVal<ValueType>();
3018
3020
3021 for (Index i = 0; i < NUM_VALUES; ++i) {
3022 if (this->isChildMaskOff(i) && other.isChildMaskOff(i)) {
3023 // Both this node and the other node have constant values (tiles).
3024 // Combine the two values and store the result as this node's new tile value.
3025 op(args.setARef(mNodes[i].getValue())
3027 .setBRef(other.mNodes[i].getValue())
3028 .setBIsActive(other.isValueMaskOn(i)));
3029 mNodes[i].setValue(args.result());
3030 mValueMask.set(i, args.resultIsActive());
3031 } else if (this->isChildMaskOn(i) && other.isChildMaskOff(i)) {
3032 // Combine this node's child with the other node's constant value.
3033 ChildNodeType* child = mNodes[i].getChild();
3034 OPENVDB_ASSERT(child);
3035 if (child) {
3036 child->combine(other.mNodes[i].getValue(), other.isValueMaskOn(i), op);
3037 }
3038 } else if (this->isChildMaskOff(i) && other.isChildMaskOn(i)) {
3039 // Combine this node's constant value with the other node's child.
3040 ChildNodeType* child = other.mNodes[i].getChild();
3041 OPENVDB_ASSERT(child);
3042 if (child) {
3043 // Combine this node's constant value with the other node's child,
3044 // but use a new functor in which the A and B values are swapped,
3045 // since the constant value is the A value, not the B value.
3047 child->combine(mNodes[i].getValue(), isValueMaskOn(i), swappedOp);
3048
3049 // Steal the other node's child.
3050 other.mChildMask.setOff(i);
3051 other.mNodes[i].setValue(zero);
3052 this->setChildNode(i, child);
3053 }
3054
3055 } else /*if (isChildMaskOn(i) && other.isChildMaskOn(i))*/ {
3056 // Combine this node's child with the other node's child.
3058 *child = mNodes[i].getChild(),
3059 *otherChild = other.mNodes[i].getChild();
3060 OPENVDB_ASSERT(child);
3061 OPENVDB_ASSERT(otherChild);
3062 if (child && otherChild) {
3063 child->combine(*otherChild, op);
3064 }
3065 }
3066 }
3067}
3068
3069
3070template<typename ChildT, Index Log2Dim>
3071template<typename CombineOp>
3072inline void
3073InternalNode<ChildT, Log2Dim>::combine(const ValueType& value, bool valueIsActive, CombineOp& op)
3074{
3076
3077 for (Index i = 0; i < NUM_VALUES; ++i) {
3078 if (this->isChildMaskOff(i)) {
3079 // Combine this node's constant value with the given constant value.
3080 op(args.setARef(mNodes[i].getValue())
3082 .setBRef(value)
3083 .setBIsActive(valueIsActive));
3084 mNodes[i].setValue(args.result());
3085 mValueMask.set(i, args.resultIsActive());
3086 } else /*if (isChildMaskOn(i))*/ {
3087 // Combine this node's child with the given constant value.
3088 ChildNodeType* child = mNodes[i].getChild();
3089 OPENVDB_ASSERT(child);
3090 if (child) child->combine(value, valueIsActive, op);
3091 }
3092 }
3093}
3094
3095
3096////////////////////////////////////////
3097
3098
3099template<typename ChildT, Index Log2Dim>
3100template<typename CombineOp, typename OtherNodeType>
3101inline void
3102InternalNode<ChildT, Log2Dim>::combine2(const InternalNode& other0, const OtherNodeType& other1,
3103 CombineOp& op)
3104{
3106
3107 for (Index i = 0; i < NUM_VALUES; ++i) {
3108 if (other0.isChildMaskOff(i) && other1.isChildMaskOff(i)) {
3109 op(args.setARef(other0.mNodes[i].getValue())
3110 .setAIsActive(other0.isValueMaskOn(i))
3111 .setBRef(other1.mNodes[i].getValue())
3112 .setBIsActive(other1.isValueMaskOn(i)));
3113 // Replace child i with a constant value.
3114 this->makeChildNodeEmpty(i, args.result());
3115 mValueMask.set(i, args.resultIsActive());
3116 } else {
3117 if (this->isChildMaskOff(i)) {
3118 // Add a new child with the same coordinates, etc. as the other node's child.
3119 const Coord& childOrigin = other0.isChildMaskOn(i)
3120 ? other0.mNodes[i].getChild()->origin()
3121 : other1.mNodes[i].getChild()->origin();
3122 this->setChildNode(i, new ChildNodeType(childOrigin, mNodes[i].getValue()));
3123 }
3124
3125 if (other0.isChildMaskOff(i)) {
3126 // Combine node1's child with node0's constant value
3127 // and write the result into child i.
3128 mNodes[i].getChild()->combine2(other0.mNodes[i].getValue(),
3129 *other1.mNodes[i].getChild(), other0.isValueMaskOn(i), op);
3130 } else if (other1.isChildMaskOff(i)) {
3131 // Combine node0's child with node1's constant value
3132 // and write the result into child i.
3133 mNodes[i].getChild()->combine2(*other0.mNodes[i].getChild(),
3134 other1.mNodes[i].getValue(), other1.isValueMaskOn(i), op);
3135 } else {
3136 // Combine node0's child with node1's child
3137 // and write the result into child i.
3138 mNodes[i].getChild()->combine2(*other0.mNodes[i].getChild(),
3139 *other1.mNodes[i].getChild(), op);
3140 }
3141 }
3142 }
3143}
3144
3145
3146template<typename ChildT, Index Log2Dim>
3147template<typename CombineOp, typename OtherNodeType>
3148inline void
3149InternalNode<ChildT, Log2Dim>::combine2(const ValueType& value, const OtherNodeType& other,
3150 bool valueIsActive, CombineOp& op)
3151{
3153
3154 for (Index i = 0; i < NUM_VALUES; ++i) {
3155 if (other.isChildMaskOff(i)) {
3156 op(args.setARef(value)
3157 .setAIsActive(valueIsActive)
3158 .setBRef(other.mNodes[i].getValue())
3159 .setBIsActive(other.isValueMaskOn(i)));
3160 // Replace child i with a constant value.
3161 this->makeChildNodeEmpty(i, args.result());
3162 mValueMask.set(i, args.resultIsActive());
3163 } else {
3164 typename OtherNodeType::ChildNodeType* otherChild = other.mNodes[i].getChild();
3165 OPENVDB_ASSERT(otherChild);
3166 if (this->isChildMaskOff(i)) {
3167 // Add a new child with the same coordinates, etc.
3168 // as the other node's child.
3169 this->setChildNode(i, new ChildNodeType(*otherChild));
3170 }
3171 // Combine the other node's child with a constant value
3172 // and write the result into child i.
3173 mNodes[i].getChild()->combine2(value, *otherChild, valueIsActive, op);
3174 }
3175 }
3176}
3177
3178
3179template<typename ChildT, Index Log2Dim>
3180template<typename CombineOp, typename OtherValueType>
3181inline void
3182InternalNode<ChildT, Log2Dim>::combine2(const InternalNode& other, const OtherValueType& value,
3183 bool valueIsActive, CombineOp& op)
3184{
3186
3187 for (Index i = 0; i < NUM_VALUES; ++i) {
3188 if (other.isChildMaskOff(i)) {
3189 op(args.setARef(other.mNodes[i].getValue())
3190 .setAIsActive(other.isValueMaskOn(i))
3191 .setBRef(value)
3192 .setBIsActive(valueIsActive));
3193 // Replace child i with a constant value.
3194 this->makeChildNodeEmpty(i, args.result());
3195 mValueMask.set(i, args.resultIsActive());
3196 } else {
3197 ChildNodeType* otherChild = other.mNodes[i].getChild();
3198 OPENVDB_ASSERT(otherChild);
3199 if (this->isChildMaskOff(i)) {
3200 // Add a new child with the same coordinates, etc. as the other node's child.
3201 this->setChildNode(i,
3202 new ChildNodeType(otherChild->origin(), mNodes[i].getValue()));
3203 }
3204 // Combine the other node's child with a constant value
3205 // and write the result into child i.
3206 mNodes[i].getChild()->combine2(*otherChild, value, valueIsActive, op);
3207 }
3208 }
3209}
3210
3211
3212////////////////////////////////////////
3213
3214
3215template<typename ChildT, Index Log2Dim>
3216inline void
3217InternalNode<ChildT, Log2Dim>::writeBuffers(std::ostream& os, bool toHalf) const
3218{
3219 for (ChildOnCIter iter = this->cbeginChildOn(); iter; ++iter) {
3220 iter->writeBuffers(os, toHalf);
3221 }
3222}
3223
3224
3225template<typename ChildT, Index Log2Dim>
3226inline void
3227InternalNode<ChildT, Log2Dim>::readBuffers(std::istream& is, bool fromHalf)
3228{
3229 for (ChildOnIter iter = this->beginChildOn(); iter; ++iter) {
3230 iter->readBuffers(is, fromHalf);
3231 }
3232}
3233
3234
3235template<typename ChildT, Index Log2Dim>
3236inline void
3238 const CoordBBox& clipBBox, bool fromHalf)
3239{
3240 for (ChildOnIter iter = this->beginChildOn(); iter; ++iter) {
3241 // Stream in the branch rooted at this child.
3242 // (We can't skip over children that lie outside the clipping region,
3243 // because buffers are serialized in depth-first order and need to be
3244 // unserialized in the same order.)
3245 iter->readBuffers(is, clipBBox, fromHalf);
3246 }
3247
3248 // Get this tree's background value.
3249 ValueType background = zeroVal<ValueType>();
3250 if (const void* bgPtr = io::getGridBackgroundValuePtr(is)) {
3251 background = *static_cast<const ValueType*>(bgPtr);
3252 }
3253 this->clip(clipBBox, background);
3254}
3255
3256
3257////////////////////////////////////////
3258
3259
3260template<typename ChildT, Index Log2Dim>
3261void
3263{
3264 dims.push_back(Log2Dim);
3265 ChildNodeType::getNodeLog2Dims(dims);
3266}
3267
3268
3269template<typename ChildT, Index Log2Dim>
3270inline void
3272{
3273 OPENVDB_ASSERT(n<(1<<3*Log2Dim));
3274 xyz.setX(n >> 2*Log2Dim);
3275 n &= ((1<<2*Log2Dim)-1);
3276 xyz.setY(n >> Log2Dim);
3277 xyz.setZ(n & ((1<<Log2Dim)-1));
3278}
3279
3280
3281template<typename ChildT, Index Log2Dim>
3282inline Index
3284{
3285 return (((xyz[0] & (DIM-1u)) >> ChildNodeType::TOTAL) << 2*Log2Dim)
3286 + (((xyz[1] & (DIM-1u)) >> ChildNodeType::TOTAL) << Log2Dim)
3287 + ((xyz[2] & (DIM-1u)) >> ChildNodeType::TOTAL);
3288}
3289
3290
3291template<typename ChildT, Index Log2Dim>
3292inline Coord
3294{
3295 Coord local;
3296 this->offsetToLocalCoord(n, local);
3297 local <<= ChildT::TOTAL;
3298 return local + this->origin();
3299}
3300
3301
3302////////////////////////////////////////
3303
3304
3305template<typename ChildT, Index Log2Dim>
3306template<typename ArrayT>
3307inline void
3309{
3310 using T = typename ArrayT::value_type;
3311 static_assert(std::is_pointer<T>::value, "argument to getNodes() must be a pointer array");
3312 using ArrayChildT = typename std::conditional<
3313 std::is_const<typename std::remove_pointer<T>::type>::value, const ChildT, ChildT>::type;
3314 for (ChildOnIter iter = this->beginChildOn(); iter; ++iter) {
3316 if (std::is_same<T, ArrayChildT*>::value) {
3317 array.push_back(reinterpret_cast<T>(mNodes[iter.pos()].getChild()));
3318 } else {
3319 iter->getNodes(array);//descent
3320 }
3322 }
3323}
3324
3325template<typename ChildT, Index Log2Dim>
3326template<typename ArrayT>
3327inline void
3329{
3330 using T = typename ArrayT::value_type;
3331 static_assert(std::is_pointer<T>::value, "argument to getNodes() must be a pointer array");
3332 static_assert(std::is_const<typename std::remove_pointer<T>::type>::value,
3333 "argument to getNodes() must be an array of const node pointers");
3334 for (ChildOnCIter iter = this->cbeginChildOn(); iter; ++iter) {
3336 if (std::is_same<T, const ChildT*>::value) {
3337 array.push_back(reinterpret_cast<T>(mNodes[iter.pos()].getChild()));
3338 } else {
3339 iter->getNodes(array);//descent
3340 }
3342 }
3343}
3344
3345
3346////////////////////////////////////////
3347
3348
3349template<typename ChildT, Index Log2Dim>
3350template<typename ArrayT>
3351inline void
3352InternalNode<ChildT, Log2Dim>::stealNodes(ArrayT& array, const ValueType& value, bool state)
3353{
3354 using T = typename ArrayT::value_type;
3355 static_assert(std::is_pointer<T>::value, "argument to stealNodes() must be a pointer array");
3356 using ArrayChildT = typename std::conditional<
3357 std::is_const<typename std::remove_pointer<T>::type>::value, const ChildT, ChildT>::type;
3359 for (ChildOnIter iter = this->beginChildOn(); iter; ++iter) {
3360 const Index n = iter.pos();
3361 if (std::is_same<T, ArrayChildT*>::value) {
3362 array.push_back(reinterpret_cast<T>(mNodes[n].getChild()));
3363 mValueMask.set(n, state);
3364 mNodes[n].setValue(value);
3365 } else {
3366 iter->stealNodes(array, value, state);//descent
3367 }
3368 }
3369 if (std::is_same<T, ArrayChildT*>::value) mChildMask.setOff();
3371}
3372
3373
3374////////////////////////////////////////
3375
3376
3377template<typename ChildT, Index Log2Dim>
3378inline void
3380 const ValueType& newBackground)
3381{
3382 if (math::isExactlyEqual(oldBackground, newBackground)) return;
3383 for (Index i = 0; i < NUM_VALUES; ++i) {
3384 if (this->isChildMaskOn(i)) {
3385 mNodes[i].getChild()->resetBackground(oldBackground, newBackground);
3386 } else if (this->isValueMaskOff(i)) {
3387 if (math::isApproxEqual(mNodes[i].getValue(), oldBackground)) {
3388 mNodes[i].setValue(newBackground);
3389 } else if (math::isApproxEqual(mNodes[i].getValue(), math::negative(oldBackground))) {
3390 mNodes[i].setValue(math::negative(newBackground));
3391 }
3392 }
3393 }
3394}
3395
3396template<typename ChildT, Index Log2Dim>
3397template<typename OtherChildNodeType, Index OtherLog2Dim>
3398inline bool
3401{
3402 if (Log2Dim != OtherLog2Dim || mChildMask != other->mChildMask ||
3403 mValueMask != other->mValueMask) return false;
3404 for (ChildOnCIter iter = this->cbeginChildOn(); iter; ++iter) {
3405 if (!iter->hasSameTopology(other->mNodes[iter.pos()].getChild())) return false;
3406 }
3407 return true;
3408}
3409
3410
3411template<typename ChildT, Index Log2Dim>
3412inline void
3414{
3415 OPENVDB_ASSERT(child);
3416 if (this->isChildMaskOn(i)) {
3417 delete mNodes[i].getChild();
3418 } else {
3419 mChildMask.setOn(i);
3420 mValueMask.setOff(i);
3421 }
3422 mNodes[i].setChild(child);
3423}
3424
3425template<typename ChildT, Index Log2Dim>
3426inline void
3428{
3429 OPENVDB_ASSERT(child);
3430 OPENVDB_ASSERT(mChildMask.isOff(i));
3431 mChildMask.setOn(i);
3432 mValueMask.setOff(i);
3433 mNodes[i].setChild(child);
3434}
3435
3436
3437template<typename ChildT, Index Log2Dim>
3438inline ChildT*
3440{
3441 if (this->isChildMaskOff(i)) {
3442 mNodes[i].setValue(value);
3443 return nullptr;
3444 }
3445 ChildNodeType* child = mNodes[i].getChild();
3446 mChildMask.setOff(i);
3447 mNodes[i].setValue(value);
3448 return child;
3449}
3450
3451
3452template<typename ChildT, Index Log2Dim>
3453inline void
3455{
3456 delete this->unsetChildNode(n, value);
3457}
3458
3459template<typename ChildT, Index Log2Dim>
3460inline ChildT*
3462{
3463 OPENVDB_ASSERT(this->isChildMaskOn(n));
3464 return mNodes[n].getChild();
3465}
3466
3467
3468template<typename ChildT, Index Log2Dim>
3469inline const ChildT*
3471{
3472 OPENVDB_ASSERT(this->isChildMaskOn(n));
3473 return mNodes[n].getChild();
3474}
3475
3476} // namespace tree
3477} // namespace OPENVDB_VERSION_NAME
3478} // namespace openvdb
3479
3480#endif // OPENVDB_TREE_INTERNALNODE_HAS_BEEN_INCLUDED
#define OPENVDB_ASSERT(X)
Definition Assert.h:41
General-purpose arithmetic and comparison routines, most of which accept arbitrary value types (or at...
#define OPENVDB_NO_DEPRECATION_WARNING_END
Definition Platform.h:205
#define OPENVDB_NO_DEPRECATION_WARNING_BEGIN
Bracket code with OPENVDB_NO_DEPRECATION_WARNING_BEGIN/_END, to inhibit warnings about deprecated cod...
Definition Platform.h:204
#define OPENVDB_NO_UNREACHABLE_CODE_WARNING_END
Definition Platform.h:151
#define OPENVDB_NO_UNREACHABLE_CODE_WARNING_BEGIN
Definition Platform.h:150
#define OPENVDB_DEPRECATED_MESSAGE(msg)
Definition Platform.h:158
This struct collects both input and output arguments to "grid combiner" functors used with the tree::...
Definition Types.h:640
CombineArgs & setARef(const AValueType &a)
Redirect the A value to a new external source.
Definition Types.h:692
const AValueType & result() const
Get the output value.
Definition Types.h:684
CombineArgs & setBIsActive(bool b)
Set the active state of the B value.
Definition Types.h:708
CombineArgs & setBRef(const BValueType &b)
Redirect the B value to a new external source.
Definition Types.h:694
bool resultIsActive() const
Definition Types.h:703
CombineArgs & setAIsActive(bool b)
Set the active state of the A value.
Definition Types.h:706
static CoordBBox createCube(const Coord &min, ValueType dim)
Definition Coord.h:316
void minComponent(const Coord &other)
Perform a component-wise minimum with the other Coord.
Definition Coord.h:176
static bool lessThan(const Coord &a, const Coord &b)
Definition Coord.h:209
Tag dispatch class that distinguishes constructors during file input.
Definition Types.h:760
Tag dispatch class that distinguishes topology copy constructors from deep copy constructors.
Definition Types.h:754
Axis-aligned bounding box of signed integer coordinates.
Definition Coord.h:252
void translate(const Coord &t)
Translate this bounding box by (tx, ty, tz).
Definition Coord.h:461
void expand(ValueType padding)
Pad this bounding box with the specified padding.
Definition Coord.h:421
const Coord & min() const
Definition Coord.h:324
bool hasOverlap(const CoordBBox &b) const
Return true if the given bounding box overlaps with this bounding box.
Definition Coord.h:415
const Coord & max() const
Definition Coord.h:325
bool isInside(const Coord &xyz) const
Return true if point (x, y, z) is inside this bounding box.
Definition Coord.h:403
void intersect(const CoordBBox &bbox)
Intersect this bounding box with the given bounding box.
Definition Coord.h:447
Signed (x, y, z) 32-bit integer coordinates.
Definition Coord.h:26
Int32 y() const
Definition Coord.h:132
Coord offsetBy(Int32 dx, Int32 dy, Int32 dz) const
Definition Coord.h:92
Int32 x() const
Definition Coord.h:131
Coord & setZ(Int32 z)
Definition Coord.h:82
Coord & setY(Int32 y)
Definition Coord.h:81
static Coord max()
Return the largest possible coordinate.
Definition Coord.h:47
Int32 z() const
Definition Coord.h:133
Coord & setX(Int32 x)
Definition Coord.h:80
Definition InternalNode.h:35
void setValueAndCache(const Coord &xyz, const ValueType &value, AccessorT &)
Definition InternalNode.h:1962
ChildNodeType * getChildUnsafe(Index offset)
Return the child node at offset.
Definition InternalNode.h:2505
bool isValueOn(Index offset) const
Return true if the voxel at the given offset is active.
Definition InternalNode.h:336
void merge(InternalNode &other, const ValueType &background, const ValueType &otherBackground)
Efficiently merge another tree into this tree using one of several schemes.
Definition InternalNode.h:2709
ChildOnCIter cbeginChildOn() const
Definition InternalNode.h:221
const ValueType & getFirstValue() const
If the first entry in this node's table is a tile, return the tile's value. Otherwise,...
Definition InternalNode.h:2466
CoordBBox getNodeBoundingBox() const
Definition InternalNode.h:300
bool isConstant(ValueType &minValue, ValueType &maxValue, bool &state, const ValueType &tolerance=zeroVal< ValueType >()) const
Definition InternalNode.h:1721
ValueIter< const InternalNode, const ValueType, MaskOffIterator, ValueOff > ValueOffCIter
Definition InternalNode.h:217
ChildNodeType * probeChildUnsafe(Index offset, ValueType &value, bool &active)
Return a pointer to the child node for a specific offset. If no such node exists, return nullptr.
Definition InternalNode.h:1440
ChildOnCIter beginChildOn() const
Definition InternalNode.h:224
ValueIter< InternalNode, const ValueType, MaskOffIterator, ValueAll > ValueAllIter
Definition InternalNode.h:218
const ChildNodeType * probeConstChildUnsafe(Index offset) const
Definition InternalNode.h:1431
ChildOnIter beginChildOn()
Definition InternalNode.h:227
NodeType * probeNode(const Coord &xyz)
Return a pointer to the node that contains voxel (x, y, z). If no such node exists,...
const NodeType * probeNode(const Coord &xyz) const
Definition InternalNode.h:707
bool isChildMaskOff(Index n) const
Definition InternalNode.h:871
bool isValueOn(const Coord &xyz) const
Return true if the voxel at the given coordinates is active.
Definition InternalNode.h:1762
const ValueType & getValueUnsafe(Index offset) const
Return the tile value at offset.
Definition InternalNode.h:2486
const ChildNodeType * probeChildUnsafe(Index offset, ValueType &value, bool &active) const
Definition InternalNode.h:743
void writeTopology(std::ostream &, bool toHalf=false) const
Definition InternalNode.h:2393
util::NodeMask< Log2Dim > NodeMaskType
Definition InternalNode.h:42
void copyToDense(const CoordBBox &bbox, DenseT &dense) const
Copy into a dense grid the values of the voxels that lie within a given bounding box.
Definition InternalNode.h:2348
void setChildNode(Index i, ChildNodeType *child)
Definition InternalNode.h:3427
bool isChildMaskOff() const
Definition InternalNode.h:872
ValueOffCIter cbeginValueOff() const
Definition InternalNode.h:233
LeafNodeType * probeLeafAndCache(const Coord &xyz, AccessorT &acc)
Same as probeLeaf() except, if necessary, update the accessor with pointers to the nodes along the pa...
Index32 transientData() const
Return the transient data value.
Definition InternalNode.h:273
InternalNode(const InternalNode< OtherChildNodeType, Log2Dim > &other)
Value conversion copy constructor.
Definition InternalNode.h:1013
InternalNode(const Coord &origin, const ValueType &fillValue, bool active=false)
Constructs an InternalNode with dense tiles.
Definition InternalNode.h:954
void addLeafAndCache(LeafNodeType *leaf, AccessorT &)
Same as addLeaf() except, if necessary, update the accessor with pointers to the nodes along the path...
Definition InternalNode.h:1541
static Index getChildDim()
Definition InternalNode.h:257
const NodeMaskType & getChildMask() const
Definition InternalNode.h:874
void topologyIntersection(const InternalNode< OtherChildNodeType, Log2Dim > &other, const ValueType &background)
Intersects this tree's set of active values with the active values of the other tree,...
NodeUnion< ValueType, ChildNodeType > UnionType
Definition InternalNode.h:41
bool isValueMaskOff() const
Definition InternalNode.h:869
InternalNode(const InternalNode< OtherChildNodeType, Log2Dim > &other, const ValueType &offValue, const ValueType &onValue, TopologyCopy)
Topology copy constructor.
Definition InternalNode.h:1086
void getNodes(ArrayT &array)
Adds all nodes of a certain type to a container with the following API:
Definition InternalNode.h:3308
bool isValueMaskOn() const
Definition InternalNode.h:867
Index64 nonLeafCount() const
Definition InternalNode.h:1154
NodeT * stealNode(const Coord &xyz, const ValueType &value, bool state)
Return a pointer to the node of type NodeT that contains voxel (x, y, z) and replace it with a tile o...
Definition InternalNode.h:1288
void voxelizeActiveTiles(bool threaded=true)
Densify active tiles, i.e., replace them with leaf-level active voxels.
Definition InternalNode.h:2688
InternalNode()
Default constructor.
Definition InternalNode.h:73
bool isChildMaskOn(Index n) const
Definition InternalNode.h:870
~InternalNode()
Definition InternalNode.h:1100
const LeafNodeType * probeConstLeafAndCache(const Coord &xyz, AccessorT &acc) const
NodeMaskType getValueOffMask() const
Definition InternalNode.h:875
void merge(const ValueType &tileValue, bool tileActive)
Merge, using one of several schemes, this node (and its descendants) with a tile of the same dimensio...
Definition InternalNode.h:2818
LeafNodeType * probeLeaf(const Coord &xyz)
Return a pointer to the leaf node that contains voxel (x, y, z). If no such node exists,...
Definition InternalNode.h:1466
bool getValueUnsafe(Index offset, ValueType &value) const
Return the tile value and active state at offset.
Definition InternalNode.h:2495
ValueAllCIter cbeginValueAll() const
Definition InternalNode.h:234
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 InternalNode.h:1264
ValueIter< const InternalNode, const ValueType, MaskOffIterator, ValueAll > ValueAllCIter
Definition InternalNode.h:219
ValueOnCIter beginValueOn() const
Definition InternalNode.h:235
void setValueOnlyUnsafe(Index offset, const ValueType &value)
Set the tile value at offset but don't change its value.
Definition InternalNode.h:2539
const ChildNodeType * probeConstChild(const Coord &xyz, ValueType &value, bool &active) const
Definition InternalNode.h:1414
void combine2(const InternalNode &other, const OtherValueType &, bool valIsActive, CombineOp &)
Definition InternalNode.h:3182
void resetBackground(const ValueType &oldBackground, const ValueType &newBackground)
Change inactive tiles or voxels with value oldBackground to newBackground or -oldBackground to -newBa...
Definition InternalNode.h:3379
void addTile(Index level, const Coord &xyz, const ValueType &value, bool state)
Add a tile at the specified tree level that contains voxel (x, y, z), possibly creating a parent bran...
Definition InternalNode.h:1600
void combine2(const ValueType &value, const OtherNodeType &other, bool valIsActive, CombineOp &)
Definition InternalNode.h:3149
void setOrigin(const Coord &origin)
Set the grid index coordinates of this node's local origin.
Definition InternalNode.h:270
const Coord & origin() const
Definition InternalNode.h:268
const ChildNodeType * probeConstChildUnsafe(Index offset, ValueType &value, bool &active) const
Definition InternalNode.h:1451
bool isInactive() const
Return true if this node has no children and only contains inactive values.
Definition InternalNode.h:331
void modifyValueAndActiveState(const Coord &xyz, const ModifyOp &op)
Apply a functor to the voxel at the given coordinates.
Definition InternalNode.h:2140
void setValueOnlyAndCache(const Coord &xyz, const ValueType &value, AccessorT &)
Definition InternalNode.h:2003
void combine2(const InternalNode &other0, const OtherNodeType &other1, CombineOp &)
Definition InternalNode.h:3102
friend class InternalNode
During topology-only construction, access is needed to protected/private members of other template in...
Definition InternalNode.h:862
bool isValueMaskOff(Index n) const
Definition InternalNode.h:868
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 InternalNode.h:1986
const LeafNodeType * probeConstLeaf(const Coord &xyz) const
Definition InternalNode.h:1492
void deleteChildUnsafe(Index offset, const ValueType &value, bool active)
Delete a child node at offset and replace with the given value and active state.
Definition InternalNode.h:2632
ChildNodeType * probeChild(const Coord &xyz)
Return a pointer to the child node that contains voxel (x, y, z). If no such node exists,...
Definition InternalNode.h:1390
Index getValueLevelAndCache(const Coord &xyz, AccessorT &) const
Return the level of the tree (0 = leaf) at which the value at the given coordinates resides.
Definition InternalNode.h:1824
const NodeType * probeConstNodeAndCache(const Coord &xyz, AccessorT &) const
void setActiveStateUnsafe(Index offset, bool on)
Set the tile active state at offset but don't change its value.
Definition InternalNode.h:2530
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 InternalNode.h:2244
ValueOffCIter beginValueOff() const
Definition InternalNode.h:237
void setValueOffAndCache(const Coord &xyz, const ValueType &value, AccessorT &)
Definition InternalNode.h:1917
void addLeaf(LeafNodeType *leaf)
Add the specified leaf to this node, possibly creating a child branch in the process....
Definition InternalNode.h:1512
InternalNode(const InternalNode< OtherChildNodeType, Log2Dim > &other, const ValueType &background, TopologyCopy)
Topology copy constructor.
Definition InternalNode.h:1049
const ValueType & getValueAndCache(const Coord &xyz, AccessorT &) const
ChildAllCIter cbeginChildAll() const
Definition InternalNode.h:223
void clip(const CoordBBox &, const ValueType &background)
Set all voxels that lie outside the given axis-aligned box to the background.
Definition InternalNode.h:2194
Index64 leafCount() const
Definition InternalNode.h:1113
ChildAllIter beginChildAll()
Definition InternalNode.h:229
ValueIter< InternalNode, const ValueType, MaskOnIterator, ValueOn > ValueOnIter
Definition InternalNode.h:214
static Index getLevel()
Definition InternalNode.h:250
void setValueOff(const Coord &xyz, const ValueType &value)
Set the value of the voxel at the given coordinates and mark the voxel as inactive.
Definition InternalNode.h:1897
bool isValueOnAndCache(const Coord &xyz, AccessorT &) const
Definition InternalNode.h:1781
const ChildNodeType * probeChildUnsafe(Index offset) const
Definition InternalNode.h:733
void topologyDifference(const InternalNode< OtherChildNodeType, Log2Dim > &other, const ValueType &background)
Difference this node's set of active values with the active values of the other node,...
static const Index DIM
Definition InternalNode.h:47
ChildNodeType * stealChildUnsafe(Index offset, const ValueType &value, bool active)
Replace a child node at offset with the given value and active state.
Definition InternalNode.h:2618
bool probeValue(const Coord &xyz, ValueType &value) const
Definition InternalNode.h:1837
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 InternalNode.h:2024
ValueOnIter beginValueOn()
Definition InternalNode.h:239
void modifyValueAndCache(const Coord &xyz, const ModifyOp &op, AccessorT &)
Apply a functor to the value of the voxel at the given coordinates and mark the voxel as active....
Definition InternalNode.h:2106
static const Index LEVEL
Definition InternalNode.h:49
void setValueMaskUnsafe(const NodeMaskType &mask)
Set the active/inactive value mask.
Definition InternalNode.h:2586
void topologyUnion(const InternalNode< OtherChildNodeType, Log2Dim > &other, const bool preserveTiles=false)
Union this branch's set of active values with the other branch's active values. The value type of the...
void resetChildUnsafe(Index offset, ChildNodeType *child)
Replace a child node at offset with the given child node.
Definition InternalNode.h:2606
ChildOffCIter cbeginChildOff() const
Definition InternalNode.h:222
ChildOffIter beginChildOff()
Definition InternalNode.h:228
ChildIter< InternalNode, ChildNodeType, MaskOnIterator, ChildOn > ChildOnIter
Definition InternalNode.h:207
ChildOffCIter beginChildOff() const
Definition InternalNode.h:225
static Index coordToOffset(const Coord &xyz)
Return the linear table offset of the given global or local coordinates.
Definition InternalNode.h:3283
void addTileAndCache(Index level, const Coord &xyz, const ValueType &, bool state, AccessorT &)
Same as addTile() except, if necessary, update the accessor with pointers to the nodes along the path...
Definition InternalNode.h:1632
typename ChildNodeType::LeafNodeType LeafNodeType
Definition InternalNode.h:38
void setValueOff(const Coord &xyz)
Mark the voxel at the given coordinates as inactive but don't change its value.
Definition InternalNode.h:1865
NodeType * probeNodeAndCache(const Coord &xyz, AccessorT &)
Same as probeNode() except, if necessary, update the accessor with pointers to the nodes along the pa...
void writeBuffers(std::ostream &, bool toHalf=false) const
Definition InternalNode.h:3217
void nodeCount(std::vector< Index64 > &vec) const
Definition InternalNode.h:1125
ValueOnCIter cbeginValueOn() const
Definition InternalNode.h:231
typename ChildNodeType::ValueType ValueType
Definition InternalNode.h:39
const LeafNodeType * probeLeaf(const Coord &xyz) const
void setValueOnUnsafe(Index offset)
Mark the tile active at offset but don't change its value.
Definition InternalNode.h:2548
void setValueOnUnsafe(Index offset, const ValueType &value)
Set the tile value at offset and mark the voxel as active.
Definition InternalNode.h:2557
void resetChildNode(Index i, ChildNodeType *child)
Definition InternalNode.h:3413
void readBuffers(std::istream &, const CoordBBox &, bool fromHalf=false)
Definition InternalNode.h:3237
DenseIter< const InternalNode, const ChildNodeType, ValueType, ChildAll > ChildAllCIter
Definition InternalNode.h:212
Index getValueLevel(const Coord &xyz) const
Return the level of the tree (0 = leaf) at which the value at the given coordinates resides.
Definition InternalNode.h:1815
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 InternalNode.h:2078
bool hasSameTopology(const InternalNode< OtherChildNodeType, OtherLog2Dim > *other) const
Return true if the given tree branch has the same node and active value topology as this tree branch ...
Definition InternalNode.h:3399
void setValueMask(Index n, bool on)
Definition InternalNode.h:887
const ChildNodeType * getChildUnsafe(Index offset) const
Return the child node at offset.
Definition InternalNode.h:2523
typename NodeMaskType::OffIterator MaskOffIterator
Definition InternalNode.h:116
ChildIter< const InternalNode, const ChildNodeType, MaskOnIterator, ChildOn > ChildOnCIter
Definition InternalNode.h:208
bool isConstant(ValueType &firstValue, bool &state, const ValueType &tolerance=zeroVal< ValueType >()) const
Definition InternalNode.h:1701
LeafNodeType * touchLeaf(const Coord &xyz)
Return the leaf node that contains voxel (x, y, z). If no such node exists, create one,...
Definition InternalNode.h:1668
void getNodes(ArrayT &array) const
Definition InternalNode.h:3328
const ChildNodeType * getConstChildUnsafe(Index offset) const
Return the child node at offset.
Definition InternalNode.h:2514
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 InternalNode.h:2300
static const Index NUM_VALUES
Definition InternalNode.h:48
ValueIter< InternalNode, const ValueType, MaskOffIterator, ChildOff > ChildOffIter
Definition InternalNode.h:209
UnionType mNodes[NUM_VALUES]
Definition InternalNode.h:914
void negate()
Change the sign of all the values represented in this node and its child nodes.
Definition InternalNode.h:2644
void readTopology(std::istream &, bool fromHalf=false)
Definition InternalNode.h:2418
Coord offsetToGlobalCoord(Index n) const
Return the global coordinates for a linear table offset.
Definition InternalNode.h:3293
ValueIter< const InternalNode, const ValueType, MaskOffIterator, ChildOff > ChildOffCIter
Definition InternalNode.h:210
ChildAllCIter beginChildAll() const
Definition InternalNode.h:226
void setActiveStateAndCache(const Coord &xyz, bool on, AccessorT &)
Definition InternalNode.h:2043
void setValuesOn()
Mark all values (both tiles and voxels) as active.
Definition InternalNode.h:2066
void makeChildNodeEmpty(Index n, const ValueType &value)
Definition InternalNode.h:3454
void setTransientData(Index32 transientData)
Set the transient data value.
Definition InternalNode.h:275
typename ChildNodeType::BuildType BuildType
Definition InternalNode.h:40
LeafNodeType * touchLeafAndCache(const Coord &xyz, AccessorT &)
Same as touchLeaf() except, if necessary, update the accessor with pointers to the nodes along the pa...
void stealNodes(ArrayT &array, const ValueType &value, bool state)
Steals all nodes of a certain type from the tree and adds them to a container with the following API:
Definition InternalNode.h:3352
ChildNodeType * getChildNode(Index n)
Returns a pointer to the child node at the linear offset n.
Definition InternalNode.h:3461
typename NodeMaskType::OnIterator MaskOnIterator
Definition InternalNode.h:115
const LeafNodeType * probeLeafAndCache(const Coord &xyz, AccessorT &acc) const
bool probeValueAndCache(const Coord &xyz, ValueType &value, AccessorT &) const
Definition InternalNode.h:1850
static const Index LOG2DIM
Definition InternalNode.h:45
bool addChild(ChildNodeType *child)
Add the given child node at this level deducing the offset from it's origin. If a child node with thi...
Definition InternalNode.h:1574
ChildNodeType * probeChildUnsafe(Index offset)
Return a pointer to the child node for a specific offset. If no such node exists, return nullptr.
Definition InternalNode.h:1422
DenseIter< InternalNode, ChildNodeType, ValueType, ChildAll > ChildAllIter
Definition InternalNode.h:211
void evalActiveBoundingBox(CoordBBox &bbox, bool visitVoxels=true) const
ValueIter< const InternalNode, const ValueType, MaskOnIterator, ValueOn > ValueOnCIter
Definition InternalNode.h:215
const ChildNodeType * probeChild(const Coord &xyz) const
Definition InternalNode.h:715
bool isEmpty() const
Definition InternalNode.h:303
InternalNode(const ValueType &offValue)
Constructor of an InternalNode with dense inactive tiles of the specified value.
Definition InternalNode.h:946
const UnionType * getTable() const
Definition InternalNode.h:882
ChildNodeType * probeChild(const Coord &xyz, ValueType &value, bool &active)
Return a pointer to the child node that contains voxel (x, y, z). If no such node exists,...
Definition InternalNode.h:1406
static void getNodeLog2Dims(std::vector< Index > &dims)
Populated an std::vector with the dimension of all the nodes in the branch starting with this node.
Definition InternalNode.h:3262
ValueOffIter beginValueOff()
Definition InternalNode.h:241
void setValueOn(const Coord &xyz)
Mark the voxel at the given coordinates as active but don't change its value.
Definition InternalNode.h:1881
ValueIter< InternalNode, const ValueType, MaskOffIterator, ValueOff > ValueOffIter
Definition InternalNode.h:216
const ChildNodeType * probeConstChild(const Coord &xyz) const
Definition InternalNode.h:1398
typename SubtreeT::Back ChildNodeType
Definition InternalNode.h:37
static void offsetToLocalCoord(Index n, Coord &xyz)
Return the local coordinates for a linear table offset, where offset 0 has coordinates (0,...
Definition InternalNode.h:3271
const NodeMaskType & getValueMask() const
Definition InternalNode.h:873
static const Index TOTAL
Definition InternalNode.h:46
const ChildNodeType * getChildNode(Index n) const
Returns a pointer to the child node at the linear offset n.
Definition InternalNode.h:3470
const NodeType * probeConstNode(const Coord &xyz) const
bool hasActiveTiles() const
Return true if this node or any of its child nodes have any active tiles.
Definition InternalNode.h:1747
void combine(InternalNode &other, CombineOp &)
Definition InternalNode.h:3015
InternalNode(const InternalNode &)
Deep copy constructor.
Definition InternalNode.h:999
void setChildUnsafe(Index offset, ChildNodeType *child)
Replace a tile at offset with the given child node.
Definition InternalNode.h:2594
const ValueType & getValue(const Coord &xyz) const
Definition InternalNode.h:1792
void setValueOn(const Coord &xyz, const ValueType &value)
Set the value of the voxel at the given coordinates and mark the voxel as active.
Definition InternalNode.h:1942
bool isValueOff(const Coord &xyz) const
Return true if the voxel at the given coordinates is inactive.
Definition InternalNode.h:1771
static const Index64 NUM_VOXELS
Definition InternalNode.h:51
void setValueOffUnsafe(Index offset)
Mark the tile inactive at offset but don't change its value.
Definition InternalNode.h:2567
ChildNodeType * unsetChildNode(Index i, const ValueType &value)
Definition InternalNode.h:3439
const ChildNodeType * probeChild(const Coord &xyz, ValueType &value, bool &active) const
Definition InternalNode.h:723
void readBuffers(std::istream &, bool fromHalf=false)
Definition InternalNode.h:3227
typename NodeMaskType::DenseIterator MaskDenseIterator
Definition InternalNode.h:117
void modifyValueAndActiveStateAndCache(const Coord &xyz, const ModifyOp &op, AccessorT &)
Definition InternalNode.h:2163
bool isValueOff(Index offset) const
Return true if the voxel at the given offset is inactive.
Definition InternalNode.h:340
ValueAllCIter beginValueAll() const
Definition InternalNode.h:238
static Index dim()
Definition InternalNode.h:247
void setValueOffUnsafe(Index offset, const ValueType &value)
Set the tile value at offset and mark the voxel as inactive.
Definition InternalNode.h:2576
InternalNode(PartialCreate, const Coord &, const ValueType &fillValue, bool active=false)
Definition InternalNode.h:968
void addTile(Index offset, const ValueType &value, bool state)
Delete any existing child branch at the specified offset and add a tile.
Definition InternalNode.h:1590
bool isValueMaskOn(Index n) const
Definition InternalNode.h:866
ValueAllIter beginValueAll()
Definition InternalNode.h:242
void combine(const ValueType &value, bool valueIsActive, CombineOp &)
Definition InternalNode.h:3073
const ValueType & getLastValue() const
If the last entry in this node's table is a tile, return the tile's value. Otherwise,...
Definition InternalNode.h:2474
Base class for iterators over internal and leaf nodes.
Definition Iterator.h:30
Default implementation of a NodeUnion that stores the child pointer and the value separately (i....
Definition NodeUnion.h:32
const ValueT & getValue() const
Definition NodeUnion.h:43
ChildT * getChild() const
Definition NodeUnion.h:40
void setValue(const ValueT &val)
Definition NodeUnion.h:45
Bit mask for the internal and leaf nodes of VDB. This is a 64-bit implementation.
Definition NodeMasks.h:309
Index64 Word
Definition NodeMasks.h:317
DenseMaskIterator< NodeMask > DenseIterator
Definition NodeMasks.h:351
void toggle(Index32 n)
Toggle the state of the nth bit.
Definition NodeMasks.h:484
OnMaskIterator< NodeMask > OnIterator
Definition NodeMasks.h:349
void setOff(Index32 n)
Set the nth bit off.
Definition NodeMasks.h:458
OffMaskIterator< NodeMask > OffIterator
Definition NodeMasks.h:350
void readCompressedValues(std::istream &is, ValueT *destBuf, Index destCount, const MaskT &valueMask, bool fromHalf)
Definition Compression.h:466
void checkFormatVersion(std::ios_base &)
Throws an IoError if the file format version number is not supported.
uint32_t getFormatVersion(std::ios_base &)
Return the file format version number associated with the given input stream.
const void * getGridBackgroundValuePtr(std::ios_base &)
Return a pointer to the background value of the grid currently being read from or written to the give...
void writeCompressedValues(std::ostream &os, const ValueT *srcBuf, Index srcCount, const MaskT &valueMask, const MaskT &childMask, bool toHalf)
Definition Compression.h:648
bool cwiseLessThan(const Mat< SIZE, T > &m0, const Mat< SIZE, T > &m1)
Definition Mat.h:1015
bool isApproxEqual(const Type &a, const Type &b, const Type &tolerance)
Return true if a is equal to b to within the given tolerance.
Definition Math.h:428
T negative(const T &val)
Return the unary negation of the given value.
Definition Math.h:139
bool isExactlyEqual(const T0 &a, const T1 &b)
Return true if a is exactly equal to b.
Definition Math.h:465
bool cwiseGreaterThan(const Mat< SIZE, T > &m0, const Mat< SIZE, T > &m1)
Definition Mat.h:1029
Definition TreeIterator.h:30
Index32 Index
Definition Types.h:34
constexpr T zeroVal()
Return the value of type T that corresponds to zero.
Definition Math.h:71
@ OPENVDB_FILE_VERSION_NODE_MASK_COMPRESSION
Definition version.h.in:328
uint32_t Index32
Definition Types.h:32
int32_t Int32
Definition Types.h:36
uint64_t Index64
Definition Types.h:33
@ MERGE_ACTIVE_STATES
Definition Types.h:578
@ MERGE_NODES
Definition Types.h:579
@ MERGE_ACTIVE_STATES_AND_NODES
Definition Types.h:580
Definition Exceptions.h:13
Definition Coord.h:590
Definition Types.h:731
typename std::remove_const< ValueT >::type NonConstValueType
Definition Iterator.h:184
ChildIter(const MaskIterT &iter, NodeT *parent)
Definition InternalNode.h:132
ChildIter()
Definition InternalNode.h:131
ChildT & getItem(Index pos) const
Definition InternalNode.h:135
void setItem(Index pos, const ChildT &c) const
Definition InternalNode.h:142
Definition InternalNode.h:121
DeepCopy(const OtherInternalNode *source, InternalNode *target)
Definition InternalNode.h:980
InternalNode * t
Definition InternalNode.h:994
const OtherInternalNode * s
Definition InternalNode.h:993
void operator()(const tbb::blocked_range< Index > &r) const
Definition InternalNode.h:984
DenseIteratorBase< MaskDenseIterator, DenseIter, NodeT, ChildT, ValueT > BaseT
Definition InternalNode.h:174
DenseIter(const MaskDenseIterator &iter, NodeT *parent)
Definition InternalNode.h:178
void unsetItem(Index pos, const ValueT &value) const
Definition InternalNode.h:199
void setItem(Index pos, ChildT *child) const
Definition InternalNode.h:193
DenseIter()
Definition InternalNode.h:177
bool getItem(Index pos, ChildT *&child, NonConstValueT &value) const
Definition InternalNode.h:181
typename BaseT::NonConstValueType NonConstValueT
Definition InternalNode.h:175
SameConfiguration<OtherNodeType>::value is true if and only if OtherNodeType is the type of an Intern...
Definition InternalNode.h:65
static const bool value
Definition InternalNode.h:66
TopologyCopy1(const OtherInternalNode *source, InternalNode *target, const ValueType &background)
Definition InternalNode.h:1026
InternalNode * t
Definition InternalNode.h:1042
const OtherInternalNode * s
Definition InternalNode.h:1041
const ValueType & b
Definition InternalNode.h:1043
void operator()(const tbb::blocked_range< Index > &r) const
Definition InternalNode.h:1031
const ValueType & offV
Definition InternalNode.h:1080
TopologyCopy2(const OtherInternalNode *source, InternalNode *target, const ValueType &offValue, const ValueType &onValue)
Definition InternalNode.h:1063
InternalNode * t
Definition InternalNode.h:1079
const OtherInternalNode * s
Definition InternalNode.h:1078
const ValueType & onV
Definition InternalNode.h:1080
void operator()(const tbb::blocked_range< Index > &r) const
Definition InternalNode.h:1068
void operator()(W &tC, const W &sC, const W &sV, const W &tV) const
Definition InternalNode.h:2953
void operator()(W &tV, const W &sC, const W &sV, const W &tC) const
Definition InternalNode.h:2956
TopologyDifference(const OtherInternalNode *source, InternalNode *target, const ValueType &background)
Definition InternalNode.h:2959
typename NodeMaskType::Word W
Definition InternalNode.h:2952
InternalNode * t
Definition InternalNode.h:2995
const OtherInternalNode * s
Definition InternalNode.h:2994
const ValueType & b
Definition InternalNode.h:2996
void operator()(const tbb::blocked_range< Index > &r) const
Definition InternalNode.h:2973
void operator()(W &tC, const W &sC, const W &sV, const W &tV) const
Definition InternalNode.h:2903
TopologyIntersection(const OtherInternalNode *source, InternalNode *target, const ValueType &background)
Definition InternalNode.h:2906
typename NodeMaskType::Word W
Definition InternalNode.h:2902
InternalNode * t
Definition InternalNode.h:2935
const OtherInternalNode * s
Definition InternalNode.h:2934
const ValueType & b
Definition InternalNode.h:2936
void operator()(const tbb::blocked_range< Index > &r) const
Definition InternalNode.h:2918
void operator()(W &tV, const W &sV, const W &tC) const
Definition InternalNode.h:2851
typename NodeMaskType::Word W
Definition InternalNode.h:2850
InternalNode * t
Definition InternalNode.h:2886
const bool mPreserveTiles
Definition InternalNode.h:2887
const OtherInternalNode * s
Definition InternalNode.h:2885
void operator()(const tbb::blocked_range< Index > &r) const
Definition InternalNode.h:2867
TopologyUnion(const OtherInternalNode *source, InternalNode *target, const bool preserveTiles)
Definition InternalNode.h:2854
ValueConverter<T>::Type is the type of an InternalNode having the same child hierarchy and dimensions...
Definition InternalNode.h:56
InternalNode< typename ChildNodeType::template ValueConverter< OtherValueType >::Type, Log2Dim > Type
Definition InternalNode.h:57
void modifyItem(Index pos, const ModifyOp &op) const
Definition InternalNode.h:163
ValueIter(const MaskIterT &iter, NodeT *parent)
Definition InternalNode.h:153
const ValueT & getItem(Index pos) const
Definition InternalNode.h:156
ValueIter()
Definition InternalNode.h:152
void setItem(Index pos, const ValueT &v) const
Definition InternalNode.h:159
Definition InternalNode.h:120
InternalNode * mNode
Definition InternalNode.h:2683
void operator()(const tbb::blocked_range< Index > &r) const
Definition InternalNode.h:2670
VoxelizeActiveTiles(InternalNode &node)
Definition InternalNode.h:2663
Definition InternalNode.h:930
static const bool value
Definition InternalNode.h:931
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition version.h.in:121
#define OPENVDB_USE_VERSION_NAMESPACE
Definition version.h.in:284