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