OpenVDB 10.1.0
Loading...
Searching...
No Matches
LeafNodeBool.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_LEAF_NODE_BOOL_HAS_BEEN_INCLUDED
5#define OPENVDB_TREE_LEAF_NODE_BOOL_HAS_BEEN_INCLUDED
6
7#include <openvdb/Types.h>
8#include <openvdb/io/Compression.h> // for io::readData(), etc.
9#include <openvdb/math/Math.h> // for math::isZero()
11#include "LeafNode.h"
12#include "Iterator.h"
13#include <iostream>
14#include <sstream>
15#include <string>
16#include <type_traits>
17#include <vector>
18
19
20namespace openvdb {
22namespace OPENVDB_VERSION_NAME {
23namespace tree {
24
25/// @brief LeafNode specialization for values of type bool that stores both
26/// the active states and the values of (2^Log2Dim)^3 voxels as bit masks
27template<Index Log2Dim>
28class LeafNode<bool, Log2Dim>
29{
30public:
32 using BuildType = bool;
33 using ValueType = bool;
37
38 // These static declarations must be on separate lines to avoid VC9 compiler errors.
39 static const Index LOG2DIM = Log2Dim; // needed by parent nodes
40 static const Index TOTAL = Log2Dim; // needed by parent nodes
41 static const Index DIM = 1 << TOTAL; // dimension along one coordinate direction
42 static const Index NUM_VALUES = 1 << 3 * Log2Dim;
43 static const Index NUM_VOXELS = NUM_VALUES; // total number of voxels represented by this node
44 static const Index SIZE = NUM_VALUES;
45 static const Index LEVEL = 0; // level 0 = leaf
46
47 /// @brief ValueConverter<T>::Type is the type of a LeafNode having the same
48 /// dimensions as this node but a different value type, T.
49 template<typename ValueType>
50 struct ValueConverter { using Type = LeafNode<ValueType, Log2Dim>; };
51
52 /// @brief SameConfiguration<OtherNodeType>::value is @c true if and only if
53 /// OtherNodeType is the type of a LeafNode with the same dimensions as this node.
54 template<typename OtherNodeType>
55 struct SameConfiguration {
57 };
58
59
60 /// Default constructor
61 LeafNode();
62
63 /// Constructor
64 /// @param xyz the coordinates of a voxel that lies within the node
65 /// @param value the initial value for all of this node's voxels
66 /// @param active the active state to which to initialize all voxels
67 explicit LeafNode(const Coord& xyz, bool value = false, bool active = false);
68
69 /// "Partial creation" constructor used during file input
70 LeafNode(PartialCreate, const Coord& xyz, bool value = false, bool active = false);
71
72 /// Deep copy constructor
73 LeafNode(const LeafNode&);
74
75 /// Deep assignment operator
76 LeafNode& operator=(const LeafNode&) = default;
77
78 /// Value conversion copy constructor
79 template<typename OtherValueType>
81
82 /// @brief Deprecated topology copy constructor
83 /// @note This constructor initialises the bool buffer to the ValueMask
84 /// states (i.e. value will be true if the active state is on and
85 /// vice-versa). This is not really a "TopologyCopy" and is therefor
86 /// deprecated. Use the explicit mask/buffer constructor instead:
87 /// @code
88 /// // build new leaf node with the mask of 'a', but with the mask of
89 /// // 'b' as the the new value buffer.
90 /// const LeafNode a = ... ;
91 /// const LeafNode b = ... ;
92 /// const LeafNode copy(a.origin(), /*mask=*/a.getValueMask(),
93 /// /*buff=*/b.getValueMask());
94 /// @endcode
95 template<typename ValueType>
96 OPENVDB_DEPRECATED_MESSAGE("Use LeafNodeBool component constructor.")
97 LeafNode(const LeafNode<ValueType, Log2Dim>& other, TopologyCopy);
98
99 /// @brief Construct a LeafNodeBool with its individual components
100 /// @param xyz Leaf origin
101 /// @param mask The ValueMask to copy
102 /// @param buff The LeafBuffer to copy (also constructable from a ValueMask)
103 /// @param trans Transient data (ignored until ABI 9)
104 LeafNode(const Coord& xyz,
105 const NodeMaskType& mask,
106 const Buffer& buff,
107 const Index32 trans = 0);
108
109 //@{
110 /// @brief Topology copy constructors
111 template<typename ValueType>
112 LeafNode(const LeafNode<ValueType, Log2Dim>& other, bool offValue, bool onValue, TopologyCopy);
113 template<typename ValueType>
114 LeafNode(const LeafNode<ValueType, Log2Dim>& other, bool background, TopologyCopy);
115 //@}
116
117 /// Destructor
118 ~LeafNode();
119
120 //
121 // Statistics
122 //
123 /// Return log2 of the size of the buffer storage.
124 static Index log2dim() { return Log2Dim; }
125 /// Return the number of voxels in each dimension.
126 static Index dim() { return DIM; }
127 static Index size() { return SIZE; }
128 static Index numValues() { return SIZE; }
129 static Index getLevel() { return LEVEL; }
130 static void getNodeLog2Dims(std::vector<Index>& dims) { dims.push_back(Log2Dim); }
131 static Index getChildDim() { return 1; }
132
133 static Index32 leafCount() { return 1; }
134 /// no-op
135 void nodeCount(std::vector<Index32> &) const {}
136 static Index32 nonLeafCount() { return 0; }
137
138 /// Return the number of active voxels.
139 Index64 onVoxelCount() const { return mValueMask.countOn(); }
140 /// Return the number of inactive voxels.
141 Index64 offVoxelCount() const { return mValueMask.countOff(); }
142 Index64 onLeafVoxelCount() const { return onVoxelCount(); }
143 Index64 offLeafVoxelCount() const { return offVoxelCount(); }
144 static Index64 onTileCount() { return 0; }
145 static Index64 offTileCount() { return 0; }
146
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 only contains active voxels.
150 bool isDense() const { return mValueMask.isOn(); }
151 /// @brief Return @c true if memory for this node's buffer has been allocated.
152 /// @details Currently, boolean leaf nodes don't support partial creation,
153 /// so this always returns @c true.
154 bool isAllocated() const { return true; }
155 /// @brief Allocate memory for this node's buffer if it has not already been allocated.
156 /// @details Currently, boolean leaf nodes don't support partial creation,
157 /// so this has no effect.
158 bool allocate() { return true; }
159
160 /// Return the memory in bytes occupied by this node.
161 Index64 memUsage() const;
162 Index64 memUsageIfLoaded() const;
163
164 /// Expand the given bounding box so that it includes this leaf node's active voxels.
165 /// If visitVoxels is false this LeafNode will be approximated as dense, i.e. with all
166 /// voxels active. Else the individual active voxels are visited to produce a tight bbox.
167 void evalActiveBoundingBox(CoordBBox& bbox, bool visitVoxels = true) const;
168
169 /// @brief Return the bounding box of this node, i.e., the full index space
170 /// spanned by this leaf node.
171 CoordBBox getNodeBoundingBox() const { return CoordBBox::createCube(mOrigin, DIM); }
172
173 /// Set the grid index coordinates of this node's local origin.
174 void setOrigin(const Coord& origin) { mOrigin = origin; }
175 //@{
176 /// Return the grid index coordinates of this node's local origin.
177 const Coord& origin() const { return mOrigin; }
178 void getOrigin(Coord& origin) const { origin = mOrigin; }
179 void getOrigin(Int32& x, Int32& y, Int32& z) const { mOrigin.asXYZ(x, y, z); }
180 //@}
181
182 /// Return the linear table offset of the given global or local coordinates.
183 static Index coordToOffset(const Coord& xyz);
184 /// @brief Return the local coordinates for a linear table offset,
185 /// where offset 0 has coordinates (0, 0, 0).
186 static Coord offsetToLocalCoord(Index n);
187 /// Return the global coordinates for a linear table offset.
188 Coord offsetToGlobalCoord(Index n) const;
189
190#if OPENVDB_ABI_VERSION_NUMBER >= 9
191 /// Return the transient data value.
192 Index32 transientData() const { return mTransientData; }
193 /// Set the transient data value.
194 void setTransientData(Index32 transientData) { mTransientData = transientData; }
195#endif
196
197 /// Return a string representation of this node.
198 std::string str() const;
199
200 /// @brief Return @c true if the given node (which may have a different @c ValueType
201 /// than this node) has the same active value topology as this node.
202 template<typename OtherType, Index OtherLog2Dim>
203 bool hasSameTopology(const LeafNode<OtherType, OtherLog2Dim>* other) const;
204
205 /// Check for buffer equivalence by value.
206 bool operator==(const LeafNode&) const;
207 bool operator!=(const LeafNode&) const;
208
209 //
210 // Buffer management
211 //
212 /// @brief Exchange this node's data buffer with the given data buffer
213 /// without changing the active states of the values.
214 void swap(Buffer& other) { mBuffer.swap(other); }
215 const Buffer& buffer() const { return mBuffer; }
216 Buffer& buffer() { return mBuffer; }
217
218 //
219 // I/O methods
220 //
221 /// Read in just the topology.
222 void readTopology(std::istream&, bool fromHalf = false);
223 /// Write out just the topology.
224 void writeTopology(std::ostream&, bool toHalf = false) const;
225
226 /// Read in the topology and the origin.
227 void readBuffers(std::istream&, bool fromHalf = false);
228 void readBuffers(std::istream& is, const CoordBBox&, bool fromHalf = false);
229 /// Write out the topology and the origin.
230 void writeBuffers(std::ostream&, bool toHalf = false) const;
231
232 //
233 // Accessor methods
234 //
235 /// Return the value of the voxel at the given coordinates.
236 const bool& getValue(const Coord& xyz) const;
237 /// Return the value of the voxel at the given offset.
238 const bool& getValue(Index offset) const;
239
240 /// @brief Return @c true if the voxel at the given coordinates is active.
241 /// @param xyz the coordinates of the voxel to be probed
242 /// @param[out] val the value of the voxel at the given coordinates
243 bool probeValue(const Coord& xyz, bool& val) const;
244
245 /// Return the level (0) at which leaf node values reside.
246 static Index getValueLevel(const Coord&) { return LEVEL; }
247
248 /// Set the active state of the voxel at the given coordinates but don't change its value.
249 void setActiveState(const Coord& xyz, bool on);
250 /// Set the active state of the voxel at the given offset but don't change its value.
251 void setActiveState(Index offset, bool on) { assert(offset<SIZE); mValueMask.set(offset, on); }
252
253 /// Set the value of the voxel at the given coordinates but don't change its active state.
254 void setValueOnly(const Coord& xyz, bool val);
255 /// Set the value of the voxel at the given offset but don't change its active state.
256 void setValueOnly(Index offset, bool val) { assert(offset<SIZE); mBuffer.setValue(offset,val); }
257
258 /// Mark the voxel at the given coordinates as inactive but don't change its value.
259 void setValueOff(const Coord& xyz) { mValueMask.setOff(this->coordToOffset(xyz)); }
260 /// Mark the voxel at the given offset as inactive but don't change its value.
261 void setValueOff(Index offset) { assert(offset < SIZE); mValueMask.setOff(offset); }
262
263 /// Set the value of the voxel at the given coordinates and mark the voxel as inactive.
264 void setValueOff(const Coord& xyz, bool val);
265 /// Set the value of the voxel at the given offset and mark the voxel as inactive.
266 void setValueOff(Index offset, bool val);
267
268 /// Mark the voxel at the given coordinates as active but don't change its value.
269 void setValueOn(const Coord& xyz) { mValueMask.setOn(this->coordToOffset(xyz)); }
270 /// Mark the voxel at the given offset as active but don't change its value.
271 void setValueOn(Index offset) { assert(offset < SIZE); mValueMask.setOn(offset); }
272
273 /// Set the value of the voxel at the given coordinates and mark the voxel as active.
274 void setValueOn(const Coord& xyz, bool val);
275 /// Set the value of the voxel at the given coordinates and mark the voxel as active.
276 void setValue(const Coord& xyz, bool val) { this->setValueOn(xyz, val); }
277 /// Set the value of the voxel at the given offset and mark the voxel as active.
278 void setValueOn(Index offset, bool val);
279
280 /// @brief Apply a functor to the value of the voxel at the given offset
281 /// and mark the voxel as active.
282 template<typename ModifyOp>
283 void modifyValue(Index offset, const ModifyOp& op);
284 /// @brief Apply a functor to the value of the voxel at the given coordinates
285 /// and mark the voxel as active.
286 template<typename ModifyOp>
287 void modifyValue(const Coord& xyz, const ModifyOp& op);
288
289 /// Apply a functor to the voxel at the given coordinates.
290 template<typename ModifyOp>
291 void modifyValueAndActiveState(const Coord& xyz, const ModifyOp& op);
292
293 /// Mark all voxels as active but don't change their values.
294 void setValuesOn() { mValueMask.setOn(); }
295 /// Mark all voxels as inactive but don't change their values.
296 void setValuesOff() { mValueMask.setOff(); }
297
298 /// Return @c true if the voxel at the given coordinates is active.
299 bool isValueOn(const Coord& xyz) const { return mValueMask.isOn(this->coordToOffset(xyz)); }
300 /// Return @c true if the voxel at the given offset is active.
301 bool isValueOn(Index offset) const { assert(offset < SIZE); return mValueMask.isOn(offset); }
302
303 /// Return @c false since leaf nodes never contain tiles.
304 static bool hasActiveTiles() { return false; }
305
306 /// Set all voxels that lie outside the given axis-aligned box to the background.
307 void clip(const CoordBBox&, bool background);
308
309 /// Set all voxels within an axis-aligned box to the specified value and active state.
310 void fill(const CoordBBox& bbox, bool value, bool active = true);
311 /// Set all voxels within an axis-aligned box to the specified value and active state.
312 void denseFill(const CoordBBox& bbox, bool val, bool on = true) { this->fill(bbox, val, on); }
313
314 /// Set all voxels to the specified value but don't change their active states.
315 void fill(const bool& value);
316 /// Set all voxels to the specified value and active state.
317 void fill(const bool& value, bool active);
318
319 /// @brief Copy into a dense grid the values of the voxels that lie within
320 /// a given bounding box.
321 ///
322 /// @param bbox inclusive bounding box of the voxels to be copied into the dense grid
323 /// @param dense dense grid with a stride in @e z of one (see tools::Dense
324 /// in tools/Dense.h for the required API)
325 ///
326 /// @note @a bbox is assumed to be identical to or contained in the coordinate domains
327 /// of both the dense grid and this node, i.e., no bounds checking is performed.
328 /// @note Consider using tools::CopyToDense in tools/Dense.h
329 /// instead of calling this method directly.
330 template<typename DenseT>
331 void copyToDense(const CoordBBox& bbox, DenseT& dense) const;
332
333 /// @brief Copy from a dense grid into this node the values of the voxels
334 /// that lie within a given bounding box.
335 /// @details Only values that are different (by more than the given tolerance)
336 /// from the background value will be active. Other values are inactive
337 /// and truncated to the background value.
338 ///
339 /// @param bbox inclusive bounding box of the voxels to be copied into this node
340 /// @param dense dense grid with a stride in @e z of one (see tools::Dense
341 /// in tools/Dense.h for the required API)
342 /// @param background background value of the tree that this node belongs to
343 /// @param tolerance tolerance within which a value equals the background value
344 ///
345 /// @note @a bbox is assumed to be identical to or contained in the coordinate domains
346 /// of both the dense grid and this node, i.e., no bounds checking is performed.
347 /// @note Consider using tools::CopyFromDense in tools/Dense.h
348 /// instead of calling this method directly.
349 template<typename DenseT>
350 void copyFromDense(const CoordBBox& bbox, const DenseT& dense, bool background, bool tolerance);
351
352 /// @brief Return the value of the voxel at the given coordinates.
353 /// @note Used internally by ValueAccessor.
354 template<typename AccessorT>
355 const bool& getValueAndCache(const Coord& xyz, AccessorT&) const {return this->getValue(xyz);}
356
357 /// @brief Return @c true if the voxel at the given coordinates is active.
358 /// @note Used internally by ValueAccessor.
359 template<typename AccessorT>
360 bool isValueOnAndCache(const Coord& xyz, AccessorT&) const { return this->isValueOn(xyz); }
361
362 /// @brief Change the value of the voxel at the given coordinates and mark it as active.
363 /// @note Used internally by ValueAccessor.
364 template<typename AccessorT>
365 void setValueAndCache(const Coord& xyz, bool val, AccessorT&) { this->setValueOn(xyz, val); }
366
367 /// @brief Change the value of the voxel at the given coordinates
368 /// but preserve its state.
369 /// @note Used internally by ValueAccessor.
370 template<typename AccessorT>
371 void setValueOnlyAndCache(const Coord& xyz, bool val, AccessorT&) {this->setValueOnly(xyz,val);}
372
373 /// @brief Change the value of the voxel at the given coordinates and mark it as inactive.
374 /// @note Used internally by ValueAccessor.
375 template<typename AccessorT>
376 void setValueOffAndCache(const Coord& xyz, bool value, AccessorT&)
377 {
378 this->setValueOff(xyz, value);
379 }
380
381 /// @brief Apply a functor to the value of the voxel at the given coordinates
382 /// and mark the voxel as active.
383 /// @note Used internally by ValueAccessor.
384 template<typename ModifyOp, typename AccessorT>
385 void modifyValueAndCache(const Coord& xyz, const ModifyOp& op, AccessorT&)
386 {
387 this->modifyValue(xyz, op);
388 }
389
390 /// Apply a functor to the voxel at the given coordinates.
391 /// @note Used internally by ValueAccessor.
392 template<typename ModifyOp, typename AccessorT>
393 void modifyValueAndActiveStateAndCache(const Coord& xyz, const ModifyOp& op, AccessorT&)
394 {
395 this->modifyValueAndActiveState(xyz, op);
396 }
397
398 /// @brief Set the active state of the voxel at the given coordinates
399 /// without changing its value.
400 /// @note Used internally by ValueAccessor.
401 template<typename AccessorT>
402 void setActiveStateAndCache(const Coord& xyz, bool on, AccessorT&)
403 {
404 this->setActiveState(xyz, on);
405 }
406
407 /// @brief Return @c true if the voxel at the given coordinates is active
408 /// and return the voxel value in @a val.
409 /// @note Used internally by ValueAccessor.
410 template<typename AccessorT>
411 bool probeValueAndCache(const Coord& xyz, bool& val, AccessorT&) const
412 {
413 return this->probeValue(xyz, val);
414 }
415
416 /// @brief Return the LEVEL (=0) at which leaf node values reside.
417 /// @note Used internally by ValueAccessor.
418 template<typename AccessorT>
419 static Index getValueLevelAndCache(const Coord&, AccessorT&) { return LEVEL; }
420
421 /// @brief Return a const reference to the first entry in the buffer.
422 /// @note Since it's actually a reference to a static data member
423 /// it should not be converted to a non-const pointer!
424 const bool& getFirstValue() const { if (mValueMask.isOn(0)) return Buffer::sOn; else return Buffer::sOff; }
425 /// @brief Return a const reference to the last entry in the buffer.
426 /// @note Since it's actually a reference to a static data member
427 /// it should not be converted to a non-const pointer!
428 const bool& getLastValue() const { if (mValueMask.isOn(SIZE-1)) return Buffer::sOn; else return Buffer::sOff; }
429
430 /// Return @c true if all of this node's voxels have the same active state
431 /// and are equal to within the given tolerance, and return the value in
432 /// @a constValue and the active state in @a state.
433 bool isConstant(bool& constValue, bool& state, bool tolerance = 0) const;
434
435 /// @brief Computes the median value of all the active and inactive voxels in this node.
436 /// @return The median value.
437 ///
438 /// @details The median for boolean values is defined as the mode
439 /// of the values, i.e. the value that occurs most often.
440 bool medianAll() const;
441
442 /// @brief Computes the median value of all the active voxels in this node.
443 /// @return The number of active voxels.
444 /// @param value Updated with the median value of all the active voxels.
445 ///
446 /// @details The median for boolean values is defined as the mode
447 /// of the values, i.e. the value that occurs most often.
448 Index medianOn(ValueType &value) const;
449
450 /// @brief Computes the median value of all the inactive voxels in this node.
451 /// @return The number of inactive voxels.
452 /// @param value Updated with the median value of all the inactive voxels.
453 ///
454 /// @details The median for boolean values is defined as the mode
455 /// of the values, i.e. the value that occurs most often.
456 Index medianOff(ValueType &value) const;
457
458 /// Return @c true if all of this node's values are inactive.
459 bool isInactive() const { return mValueMask.isOff(); }
460
461 void resetBackground(bool oldBackground, bool newBackground);
462
463 void negate() { mBuffer.mData.toggle(); }
464
465 template<MergePolicy Policy>
466 void merge(const LeafNode& other, bool bg = false, bool otherBG = false);
467 template<MergePolicy Policy> void merge(bool tileValue, bool tileActive);
468
469 /// @brief No-op
470 /// @details This function exists only to enable template instantiation.
471 void voxelizeActiveTiles(bool = true) {}
472
473 /// @brief Union this node's set of active values with the active values
474 /// of the other node, whose @c ValueType may be different. So a
475 /// resulting voxel will be active if either of the original voxels
476 /// were active.
477 ///
478 /// @note This operation modifies only active states, not values.
479 template<typename OtherType>
480 void topologyUnion(const LeafNode<OtherType, Log2Dim>& other, const bool preserveTiles = false);
481
482 /// @brief Intersect this node's set of active values with the active values
483 /// of the other node, whose @c ValueType may be different. So a
484 /// resulting voxel will be active only if both of the original voxels
485 /// were active.
486 ///
487 /// @details The last dummy argument is required to match the signature
488 /// for InternalNode::topologyIntersection.
489 ///
490 /// @note This operation modifies only active states, not
491 /// values. Also note that this operation can result in all voxels
492 /// being inactive so consider subsequently calling prune.
493 template<typename OtherType>
494 void topologyIntersection(const LeafNode<OtherType, Log2Dim>& other, const bool&);
495
496 /// @brief Difference this node's set of active values with the active values
497 /// of the other node, whose @c ValueType may be different. So a
498 /// resulting voxel will be active only if the original voxel is
499 /// active in this LeafNode and inactive in the other LeafNode.
500 ///
501 /// @details The last dummy argument is required to match the signature
502 /// for InternalNode::topologyDifference.
503 ///
504 /// @note This operation modifies only active states, not values.
505 /// Also, because it can deactivate all of this node's voxels,
506 /// consider subsequently calling prune.
507 template<typename OtherType>
508 void topologyDifference(const LeafNode<OtherType, Log2Dim>& other, const bool&);
509
510 template<typename CombineOp>
511 void combine(const LeafNode& other, CombineOp& op);
512 template<typename CombineOp>
513 void combine(bool, bool valueIsActive, CombineOp& op);
514
515 template<typename CombineOp, typename OtherType /*= bool*/>
516 void combine2(const LeafNode& other, const OtherType&, bool valueIsActive, CombineOp&);
517 template<typename CombineOp, typename OtherNodeT /*= LeafNode*/>
518 void combine2(bool, const OtherNodeT& other, bool valueIsActive, CombineOp&);
519 template<typename CombineOp, typename OtherNodeT /*= LeafNode*/>
520 void combine2(const LeafNode& b0, const OtherNodeT& b1, CombineOp&);
521
522 //@{
523 /// This function exists only to enable template instantiation.
524 void prune(const ValueType& /*tolerance*/ = zeroVal<ValueType>()) {}
526 template<typename AccessorT>
527 void addLeafAndCache(LeafNode*, AccessorT&) {}
528 template<typename NodeT>
529 NodeT* stealNode(const Coord&, const ValueType&, bool) { return nullptr; }
530 template<typename NodeT>
531 NodeT* probeNode(const Coord&) { return nullptr; }
532 template<typename NodeT>
533 const NodeT* probeConstNode(const Coord&) const { return nullptr; }
534 template<typename ArrayT> void getNodes(ArrayT&) const {}
535 template<typename ArrayT> void stealNodes(ArrayT&, const ValueType&, bool) {}
536 //@}
537
538 void addTile(Index level, const Coord&, bool val, bool active);
539 void addTile(Index offset, bool val, bool active);
540 template<typename AccessorT>
541 void addTileAndCache(Index level, const Coord&, bool val, bool active, AccessorT&);
542
543 //@{
544 /// @brief Return a pointer to this node.
545 LeafNode* touchLeaf(const Coord&) { return this; }
546 template<typename AccessorT>
547 LeafNode* touchLeafAndCache(const Coord&, AccessorT&) { return this; }
548 LeafNode* probeLeaf(const Coord&) { return this; }
549 template<typename AccessorT>
550 LeafNode* probeLeafAndCache(const Coord&, AccessorT&) { return this; }
551 template<typename NodeT, typename AccessorT>
552 NodeT* probeNodeAndCache(const Coord&, AccessorT&)
553 {
555 if (!(std::is_same<NodeT, LeafNode>::value)) return nullptr;
556 return reinterpret_cast<NodeT*>(this);
558 }
559 //@}
560 //@{
561 /// @brief Return a @const pointer to this node.
562 const LeafNode* probeLeaf(const Coord&) const { return this; }
563 template<typename AccessorT>
564 const LeafNode* probeLeafAndCache(const Coord&, AccessorT&) const { return this; }
565 const LeafNode* probeConstLeaf(const Coord&) const { return this; }
566 template<typename AccessorT>
567 const LeafNode* probeConstLeafAndCache(const Coord&, AccessorT&) const { return this; }
568 template<typename NodeT, typename AccessorT>
569 const NodeT* probeConstNodeAndCache(const Coord&, AccessorT&) const
570 {
572 if (!(std::is_same<NodeT, LeafNode>::value)) return nullptr;
573 return reinterpret_cast<const NodeT*>(this);
575 }
576 //@}
577
578 //
579 // Iterators
580 //
581protected:
585
586 template<typename MaskIterT, typename NodeT, typename ValueT>
587 struct ValueIter:
588 // Derives from SparseIteratorBase, but can also be used as a dense iterator,
589 // if MaskIterT is a dense mask iterator type.
590 public SparseIteratorBase<MaskIterT, ValueIter<MaskIterT, NodeT, ValueT>, NodeT, ValueT>
591 {
593
595 ValueIter(const MaskIterT& iter, NodeT* parent): BaseT(iter, parent) {}
596
597 const bool& getItem(Index pos) const { return this->parent().getValue(pos); }
598 const bool& getValue() const { return this->getItem(this->pos()); }
599
600 // Note: setItem() can't be called on const iterators.
601 void setItem(Index pos, bool value) const { this->parent().setValueOnly(pos, value); }
602 // Note: setValue() can't be called on const iterators.
603 void setValue(bool value) const { this->setItem(this->pos(), value); }
604
605 // Note: modifyItem() can't be called on const iterators.
606 template<typename ModifyOp>
607 void modifyItem(Index n, const ModifyOp& op) const { this->parent().modifyValue(n, op); }
608 // Note: modifyValue() can't be called on const iterators.
609 template<typename ModifyOp>
610 void modifyValue(const ModifyOp& op) const { this->modifyItem(this->pos(), op); }
611 };
612
613 /// Leaf nodes have no children, so their child iterators have no get/set accessors.
614 template<typename MaskIterT, typename NodeT>
615 struct ChildIter:
616 public SparseIteratorBase<MaskIterT, ChildIter<MaskIterT, NodeT>, NodeT, bool>
617 {
619 ChildIter(const MaskIterT& iter, NodeT* parent): SparseIteratorBase<
620 MaskIterT, ChildIter<MaskIterT, NodeT>, NodeT, bool>(iter, parent) {}
621 };
622
623 template<typename NodeT, typename ValueT>
624 struct DenseIter: public DenseIteratorBase<
625 MaskDenseIter, DenseIter<NodeT, ValueT>, NodeT, /*ChildT=*/void, ValueT>
626 {
629
631 DenseIter(const MaskDenseIter& iter, NodeT* parent): BaseT(iter, parent) {}
632
633 bool getItem(Index pos, void*& child, NonConstValueT& value) const
634 {
635 value = this->parent().getValue(pos);
636 child = nullptr;
637 return false; // no child
638 }
639
640 // Note: setItem() can't be called on const iterators.
641 //void setItem(Index pos, void* child) const {}
642
643 // Note: unsetItem() can't be called on const iterators.
644 void unsetItem(Index pos, const ValueT& val) const {this->parent().setValueOnly(pos, val);}
645 };
646
647public:
648 using ValueOnIter = ValueIter<MaskOnIter, LeafNode, const bool>;
649 using ValueOnCIter = ValueIter<MaskOnIter, const LeafNode, const bool>;
650 using ValueOffIter = ValueIter<MaskOffIter, LeafNode, const bool>;
651 using ValueOffCIter = ValueIter<MaskOffIter, const LeafNode, const bool>;
652 using ValueAllIter = ValueIter<MaskDenseIter, LeafNode, const bool>;
653 using ValueAllCIter = ValueIter<MaskDenseIter, const LeafNode, const bool>;
654 using ChildOnIter = ChildIter<MaskOnIter, LeafNode>;
655 using ChildOnCIter = ChildIter<MaskOnIter, const LeafNode>;
656 using ChildOffIter = ChildIter<MaskOffIter, LeafNode>;
657 using ChildOffCIter = ChildIter<MaskOffIter, const LeafNode>;
658 using ChildAllIter = DenseIter<LeafNode, bool>;
659 using ChildAllCIter = DenseIter<const LeafNode, const bool>;
660
661 ValueOnCIter cbeginValueOn() const { return ValueOnCIter(mValueMask.beginOn(), this); }
662 ValueOnCIter beginValueOn() const { return ValueOnCIter(mValueMask.beginOn(), this); }
663 ValueOnIter beginValueOn() { return ValueOnIter(mValueMask.beginOn(), this); }
664 ValueOffCIter cbeginValueOff() const { return ValueOffCIter(mValueMask.beginOff(), this); }
665 ValueOffCIter beginValueOff() const { return ValueOffCIter(mValueMask.beginOff(), this); }
666 ValueOffIter beginValueOff() { return ValueOffIter(mValueMask.beginOff(), this); }
667 ValueAllCIter cbeginValueAll() const { return ValueAllCIter(mValueMask.beginDense(), this); }
668 ValueAllCIter beginValueAll() const { return ValueAllCIter(mValueMask.beginDense(), this); }
669 ValueAllIter beginValueAll() { return ValueAllIter(mValueMask.beginDense(), this); }
670
671 ValueOnCIter cendValueOn() const { return ValueOnCIter(mValueMask.endOn(), this); }
672 ValueOnCIter endValueOn() const { return ValueOnCIter(mValueMask.endOn(), this); }
673 ValueOnIter endValueOn() { return ValueOnIter(mValueMask.endOn(), this); }
674 ValueOffCIter cendValueOff() const { return ValueOffCIter(mValueMask.endOff(), this); }
675 ValueOffCIter endValueOff() const { return ValueOffCIter(mValueMask.endOff(), this); }
676 ValueOffIter endValueOff() { return ValueOffIter(mValueMask.endOff(), this); }
677 ValueAllCIter cendValueAll() const { return ValueAllCIter(mValueMask.endDense(), this); }
678 ValueAllCIter endValueAll() const { return ValueAllCIter(mValueMask.endDense(), this); }
679 ValueAllIter endValueAll() { return ValueAllIter(mValueMask.endDense(), this); }
680
681 // Note that [c]beginChildOn() and [c]beginChildOff() actually return end iterators,
682 // because leaf nodes have no children.
683 ChildOnCIter cbeginChildOn() const { return ChildOnCIter(mValueMask.endOn(), this); }
684 ChildOnCIter beginChildOn() const { return ChildOnCIter(mValueMask.endOn(), this); }
685 ChildOnIter beginChildOn() { return ChildOnIter(mValueMask.endOn(), this); }
686 ChildOffCIter cbeginChildOff() const { return ChildOffCIter(mValueMask.endOff(), this); }
687 ChildOffCIter beginChildOff() const { return ChildOffCIter(mValueMask.endOff(), this); }
688 ChildOffIter beginChildOff() { return ChildOffIter(mValueMask.endOff(), this); }
689 ChildAllCIter cbeginChildAll() const { return ChildAllCIter(mValueMask.beginDense(), this); }
690 ChildAllCIter beginChildAll() const { return ChildAllCIter(mValueMask.beginDense(), this); }
691 ChildAllIter beginChildAll() { return ChildAllIter(mValueMask.beginDense(), this); }
692
693 ChildOnCIter cendChildOn() const { return ChildOnCIter(mValueMask.endOn(), this); }
694 ChildOnCIter endChildOn() const { return ChildOnCIter(mValueMask.endOn(), this); }
695 ChildOnIter endChildOn() { return ChildOnIter(mValueMask.endOn(), this); }
696 ChildOffCIter cendChildOff() const { return ChildOffCIter(mValueMask.endOff(), this); }
697 ChildOffCIter endChildOff() const { return ChildOffCIter(mValueMask.endOff(), this); }
698 ChildOffIter endChildOff() { return ChildOffIter(mValueMask.endOff(), this); }
699 ChildAllCIter cendChildAll() const { return ChildAllCIter(mValueMask.endDense(), this); }
700 ChildAllCIter endChildAll() const { return ChildAllCIter(mValueMask.endDense(), this); }
701 ChildAllIter endChildAll() { return ChildAllIter(mValueMask.endDense(), this); }
702
703 //
704 // Mask accessors
705 //
706 bool isValueMaskOn(Index n) const { return mValueMask.isOn(n); }
707 bool isValueMaskOn() const { return mValueMask.isOn(); }
708 bool isValueMaskOff(Index n) const { return mValueMask.isOff(n); }
709 bool isValueMaskOff() const { return mValueMask.isOff(); }
710 const NodeMaskType& getValueMask() const { return mValueMask; }
711 const NodeMaskType& valueMask() const { return mValueMask; }
712 NodeMaskType& getValueMask() { return mValueMask; }
713 void setValueMask(const NodeMaskType& mask) { mValueMask = mask; }
714 bool isChildMaskOn(Index) const { return false; } // leaf nodes have no children
715 bool isChildMaskOff(Index) const { return true; }
716 bool isChildMaskOff() const { return true; }
717protected:
718 void setValueMask(Index n, bool on) { mValueMask.set(n, on); }
719 void setValueMaskOn(Index n) { mValueMask.setOn(n); }
720 void setValueMaskOff(Index n) { mValueMask.setOff(n); }
721
722 /// Compute the origin of the leaf node that contains the voxel with the given coordinates.
723 static void evalNodeOrigin(Coord& xyz) { xyz &= ~(DIM - 1); }
724
725 /// Bitmask that determines which voxels are active
727 /// Bitmask representing the values of voxels
729 /// Global grid index coordinates (x,y,z) of the local origin of this node
731#if OPENVDB_ABI_VERSION_NUMBER >= 9
732 /// Transient data (not serialized)
733 Index32 mTransientData = 0;
734#endif
735
736private:
737 /// @brief During topology-only construction, access is needed
738 /// to protected/private members of other template instances.
739 template<typename, Index> friend class LeafNode;
740
741 friend struct ValueIter<MaskOnIter, LeafNode, bool>;
742 friend struct ValueIter<MaskOffIter, LeafNode, bool>;
743 friend struct ValueIter<MaskDenseIter, LeafNode, bool>;
744 friend struct ValueIter<MaskOnIter, const LeafNode, bool>;
745 friend struct ValueIter<MaskOffIter, const LeafNode, bool>;
746 friend struct ValueIter<MaskDenseIter, const LeafNode, bool>;
747
748 //@{
749 /// Allow iterators to call mask accessor methods (see below).
750 /// @todo Make mask accessors public?
751 friend class IteratorBase<MaskOnIter, LeafNode>;
752 friend class IteratorBase<MaskOffIter, LeafNode>;
753 friend class IteratorBase<MaskDenseIter, LeafNode>;
754 //@}
755
756}; // class LeafNode<bool>
757
758
759////////////////////////////////////////
760
761
762template<Index Log2Dim>
763inline
765 : mOrigin(0, 0, 0)
766{
767}
768
769
770template<Index Log2Dim>
771inline
772LeafNode<bool, Log2Dim>::LeafNode(const Coord& xyz, bool value, bool active)
773 : mValueMask(active)
774 , mBuffer(value)
775 , mOrigin(xyz & (~(DIM - 1)))
776{
777}
778
779
780template<Index Log2Dim>
781inline
783 : mValueMask(active)
784 , mBuffer(value)
785 , mOrigin(xyz & (~(DIM - 1)))
786{
787 /// @todo For now, this is identical to the non-PartialCreate constructor.
788 /// Consider modifying the Buffer class to allow it to be constructed
789 /// without allocating a bitmask.
790}
791
792
793template<Index Log2Dim>
794inline
796 : mValueMask(other.valueMask())
797 , mBuffer(other.mBuffer)
798 , mOrigin(other.mOrigin)
799#if OPENVDB_ABI_VERSION_NUMBER >= 9
800 , mTransientData(other.mTransientData)
801#endif
802{
803}
804
805
806// Copy-construct from a leaf node with the same configuration but a different ValueType.
807template<Index Log2Dim>
808template<typename ValueT>
809inline
811 : mValueMask(other.valueMask())
812 , mOrigin(other.origin())
813#if OPENVDB_ABI_VERSION_NUMBER >= 9
814 , mTransientData(other.mTransientData)
815#endif
816{
817 struct Local {
818 /// @todo Consider using a value conversion functor passed as an argument instead.
819 static inline bool convertValue(const ValueT& val) { return bool(val); }
820 };
821
822 for (Index i = 0; i < SIZE; ++i) {
823 mBuffer.setValue(i, Local::convertValue(other.mBuffer[i]));
824 }
825}
826
827
828template<Index Log2Dim>
829template<typename ValueT>
830inline
832 bool background, TopologyCopy)
833 : mValueMask(other.valueMask())
834 , mBuffer(background)
835 , mOrigin(other.origin())
836#if OPENVDB_ABI_VERSION_NUMBER >= 9
837 , mTransientData(other.mTransientData)
838#endif
839{
840}
841
842
843template<Index Log2Dim>
844template<typename ValueT>
845inline
847 : mValueMask(other.valueMask())
848 , mBuffer(other.valueMask())// value = active state
849 , mOrigin(other.origin())
850#if OPENVDB_ABI_VERSION_NUMBER >= 9
851 , mTransientData(other.mTransientData)
852#endif
853{
854}
855
856template<Index Log2Dim>
857inline
859 const NodeMaskType& mask,
860 const Buffer& buff,
861#if OPENVDB_ABI_VERSION_NUMBER < 9
862 [[maybe_unused]]
863#endif
864 const Index32 trans)
865 : mValueMask(mask)
866 , mBuffer(buff)
867 , mOrigin(xyz & (~(DIM - 1)))
868#if OPENVDB_ABI_VERSION_NUMBER >= 9
869 , mTransientData(trans)
870#endif
871{
872}
873
874template<Index Log2Dim>
875template<typename ValueT>
876inline
878 bool offValue, bool onValue, TopologyCopy)
879 : mValueMask(other.valueMask())
880 , mBuffer(offValue)
881 , mOrigin(other.origin())
882#if OPENVDB_ABI_VERSION_NUMBER >= 9
883 , mTransientData(other.mTransientData)
884#endif
885{
886 for (Index i = 0; i < SIZE; ++i) {
887 if (mValueMask.isOn(i)) {
888 mBuffer.setValue(i, onValue);
889 }
890 }
891}
892
893
894template<Index Log2Dim>
895inline
899
900
901////////////////////////////////////////
902
903
904template<Index Log2Dim>
905inline Index64
907{
908 // Use sizeof(*this) to capture alignment-related padding
909 return sizeof(*this);
910}
911
912
913template<Index Log2Dim>
914inline Index64
916{
917 // Use sizeof(*this) to capture alignment-related padding
918 return sizeof(*this);
919}
920
921
922template<Index Log2Dim>
923inline void
925{
926 CoordBBox this_bbox = this->getNodeBoundingBox();
927 if (bbox.isInside(this_bbox)) return;//this LeafNode is already enclosed in the bbox
928 if (ValueOnCIter iter = this->cbeginValueOn()) {//any active values?
929 if (visitVoxels) {//use voxel granularity?
930 this_bbox.reset();
931 for(; iter; ++iter) this_bbox.expand(this->offsetToLocalCoord(iter.pos()));
932 this_bbox.translate(this->origin());
933 }
934 bbox.expand(this_bbox);
935 }
936}
937
938
939template<Index Log2Dim>
940template<typename OtherType, Index OtherLog2Dim>
941inline bool
943{
944 assert(other);
945 return (Log2Dim == OtherLog2Dim && mValueMask == other->getValueMask());
946}
947
948
949template<Index Log2Dim>
950inline std::string
952{
953 std::ostringstream ostr;
954 ostr << "LeafNode @" << mOrigin << ": ";
955 for (Index32 n = 0; n < SIZE; ++n) ostr << (mValueMask.isOn(n) ? '#' : '.');
956 return ostr.str();
957}
958
959
960////////////////////////////////////////
961
962
963template<Index Log2Dim>
964inline Index
966{
967 assert ((xyz[0] & (DIM-1u)) < DIM && (xyz[1] & (DIM-1u)) < DIM && (xyz[2] & (DIM-1u)) < DIM);
968 return ((xyz[0] & (DIM-1u)) << 2*Log2Dim)
969 + ((xyz[1] & (DIM-1u)) << Log2Dim)
970 + (xyz[2] & (DIM-1u));
971}
972
973
974template<Index Log2Dim>
975inline Coord
977{
978 assert(n < (1 << 3*Log2Dim));
979 Coord xyz;
980 xyz.setX(n >> 2*Log2Dim);
981 n &= ((1 << 2*Log2Dim) - 1);
982 xyz.setY(n >> Log2Dim);
983 xyz.setZ(n & ((1 << Log2Dim) - 1));
984 return xyz;
985}
986
987
988template<Index Log2Dim>
989inline Coord
991{
992 return (this->offsetToLocalCoord(n) + this->origin());
993}
994
995
996////////////////////////////////////////
997
998
999template<Index Log2Dim>
1000inline void
1001LeafNode<bool, Log2Dim>::readTopology(std::istream& is, bool /*fromHalf*/)
1002{
1003 mValueMask.load(is);
1004}
1005
1006
1007template<Index Log2Dim>
1008inline void
1009LeafNode<bool, Log2Dim>::writeTopology(std::ostream& os, bool /*toHalf*/) const
1010{
1011 mValueMask.save(os);
1012}
1013
1014
1015template<Index Log2Dim>
1016inline void
1017LeafNode<bool, Log2Dim>::readBuffers(std::istream& is, const CoordBBox& clipBBox, bool fromHalf)
1018{
1019 // Boolean LeafNodes don't currently implement lazy loading.
1020 // Instead, load the full buffer, then clip it.
1021
1022 this->readBuffers(is, fromHalf);
1023
1024 // Get this tree's background value.
1025 bool background = false;
1026 if (const void* bgPtr = io::getGridBackgroundValuePtr(is)) {
1027 background = *static_cast<const bool*>(bgPtr);
1028 }
1029 this->clip(clipBBox, background);
1030}
1031
1032
1033template<Index Log2Dim>
1034inline void
1035LeafNode<bool, Log2Dim>::readBuffers(std::istream& is, bool /*fromHalf*/)
1036{
1037 // Read in the value mask.
1038 mValueMask.load(is);
1039 // Read in the origin.
1040 is.read(reinterpret_cast<char*>(&mOrigin), sizeof(Coord::ValueType) * 3);
1041
1043 // Read in the mask for the voxel values.
1044 mBuffer.mData.load(is);
1045 } else {
1046 // Older files stored one or more bool arrays.
1047
1048 // Read in the number of buffers, which should now always be one.
1049 int8_t numBuffers = 0;
1050 is.read(reinterpret_cast<char*>(&numBuffers), sizeof(int8_t));
1051
1052 // Read in the buffer.
1053 // (Note: prior to the bool leaf optimization, buffers were always compressed.)
1054 std::unique_ptr<bool[]> buf{new bool[SIZE]};
1055 io::readData<bool>(is, buf.get(), SIZE, /*isCompressed=*/true);
1056
1057 // Transfer values to mBuffer.
1058 mBuffer.mData.setOff();
1059 for (Index i = 0; i < SIZE; ++i) {
1060 if (buf[i]) mBuffer.mData.setOn(i);
1061 }
1062
1063 if (numBuffers > 1) {
1064 // Read in and discard auxiliary buffers that were created with
1065 // earlier versions of the library.
1066 for (int i = 1; i < numBuffers; ++i) {
1067 io::readData<bool>(is, buf.get(), SIZE, /*isCompressed=*/true);
1068 }
1069 }
1070 }
1071}
1072
1073
1074template<Index Log2Dim>
1075inline void
1076LeafNode<bool, Log2Dim>::writeBuffers(std::ostream& os, bool /*toHalf*/) const
1077{
1078 // Write out the value mask.
1079 mValueMask.save(os);
1080 // Write out the origin.
1081 os.write(reinterpret_cast<const char*>(&mOrigin), sizeof(Coord::ValueType) * 3);
1082 // Write out the voxel values.
1083 mBuffer.mData.save(os);
1084}
1085
1086
1087////////////////////////////////////////
1088
1089
1090template<Index Log2Dim>
1091inline bool
1093{
1094 return mOrigin == other.mOrigin &&
1095 mValueMask == other.valueMask() &&
1096 mBuffer == other.mBuffer;
1097}
1098
1099
1100template<Index Log2Dim>
1101inline bool
1103{
1104 return !(this->operator==(other));
1105}
1106
1107
1108////////////////////////////////////////
1109
1110
1111template<Index Log2Dim>
1112inline bool
1113LeafNode<bool, Log2Dim>::isConstant(bool& constValue, bool& state, bool tolerance) const
1114{
1115 if (!mValueMask.isConstant(state)) return false;
1116
1117 // Note: if tolerance is true (i.e., 1), then all boolean values compare equal.
1118 if (!tolerance && !(mBuffer.mData.isOn() || mBuffer.mData.isOff())) return false;
1119
1120 constValue = mBuffer.mData.isOn();
1121 return true;
1122}
1123
1124////////////////////////////////////////
1125
1126template<Index Log2Dim>
1127inline bool
1129{
1130 const Index countTrue = mBuffer.mData.countOn();
1131 return countTrue > (NUM_VALUES >> 1);
1132}
1133
1134template<Index Log2Dim>
1135inline Index
1137{
1138 const NodeMaskType tmp = mBuffer.mData & mValueMask;//both true and active
1139 const Index countTrueOn = tmp.countOn(), countOn = mValueMask.countOn();
1140 state = countTrueOn > (NUM_VALUES >> 1);
1141 return countOn;
1142}
1143
1144template<Index Log2Dim>
1145inline Index
1147{
1148 const NodeMaskType tmp = mBuffer.mData & (!mValueMask);//both true and inactive
1149 const Index countTrueOff = tmp.countOn(), countOff = mValueMask.countOff();
1150 state = countTrueOff > (NUM_VALUES >> 1);
1151 return countOff;
1152}
1153
1154////////////////////////////////////////
1155
1156
1157template<Index Log2Dim>
1158inline void
1159LeafNode<bool, Log2Dim>::addTile(Index /*level*/, const Coord& xyz, bool val, bool active)
1160{
1161 this->addTile(this->coordToOffset(xyz), val, active);
1162}
1163
1164template<Index Log2Dim>
1165inline void
1166LeafNode<bool, Log2Dim>::addTile(Index offset, bool val, bool active)
1167{
1168 assert(offset < SIZE);
1169 this->setValueOnly(offset, val);
1170 this->setActiveState(offset, active);
1171}
1172
1173template<Index Log2Dim>
1174template<typename AccessorT>
1175inline void
1177 bool val, bool active, AccessorT&)
1178{
1179 this->addTile(level, xyz, val, active);
1180}
1181
1182
1183////////////////////////////////////////
1184
1185
1186template<Index Log2Dim>
1187inline const bool&
1189{
1190 // This *CANNOT* use operator ? because Visual C++
1191 if (mBuffer.mData.isOn(this->coordToOffset(xyz))) return Buffer::sOn; else return Buffer::sOff;
1192}
1193
1194
1195template<Index Log2Dim>
1196inline const bool&
1198{
1199 assert(offset < SIZE);
1200 // This *CANNOT* use operator ? for Windows
1201 if (mBuffer.mData.isOn(offset)) return Buffer::sOn; else return Buffer::sOff;
1202}
1203
1204
1205template<Index Log2Dim>
1206inline bool
1207LeafNode<bool, Log2Dim>::probeValue(const Coord& xyz, bool& val) const
1208{
1209 const Index offset = this->coordToOffset(xyz);
1210 val = mBuffer.mData.isOn(offset);
1211 return mValueMask.isOn(offset);
1212}
1213
1214
1215template<Index Log2Dim>
1216inline void
1218{
1219 this->setValueOn(this->coordToOffset(xyz), val);
1220}
1221
1222
1223template<Index Log2Dim>
1224inline void
1226{
1227 assert(offset < SIZE);
1228 mValueMask.setOn(offset);
1229 mBuffer.mData.set(offset, val);
1230}
1231
1232
1233template<Index Log2Dim>
1234inline void
1236{
1237 this->setValueOnly(this->coordToOffset(xyz), val);
1238}
1239
1240
1241template<Index Log2Dim>
1242inline void
1244{
1245 mValueMask.set(this->coordToOffset(xyz), on);
1246}
1247
1248
1249template<Index Log2Dim>
1250inline void
1252{
1253 this->setValueOff(this->coordToOffset(xyz), val);
1254}
1255
1256
1257template<Index Log2Dim>
1258inline void
1260{
1261 assert(offset < SIZE);
1262 mValueMask.setOff(offset);
1263 mBuffer.mData.set(offset, val);
1264}
1265
1266
1267template<Index Log2Dim>
1268template<typename ModifyOp>
1269inline void
1271{
1272 bool val = mBuffer.mData.isOn(offset);
1273 op(val);
1274 mBuffer.mData.set(offset, val);
1275 mValueMask.setOn(offset);
1276}
1277
1278
1279template<Index Log2Dim>
1280template<typename ModifyOp>
1281inline void
1282LeafNode<bool, Log2Dim>::modifyValue(const Coord& xyz, const ModifyOp& op)
1283{
1284 this->modifyValue(this->coordToOffset(xyz), op);
1285}
1286
1287
1288template<Index Log2Dim>
1289template<typename ModifyOp>
1290inline void
1292{
1293 const Index offset = this->coordToOffset(xyz);
1294 bool val = mBuffer.mData.isOn(offset), state = mValueMask.isOn(offset);
1295 op(val, state);
1296 mBuffer.mData.set(offset, val);
1297 mValueMask.set(offset, state);
1298}
1299
1300
1301////////////////////////////////////////
1302
1303
1304template<Index Log2Dim>
1305inline void
1306LeafNode<bool, Log2Dim>::resetBackground(bool oldBackground, bool newBackground)
1307{
1308 if (newBackground != oldBackground) {
1309 // Flip mBuffer's background bits and zero its foreground bits.
1310 NodeMaskType bgMask = !(mBuffer.mData | mValueMask);
1311 // Overwrite mBuffer's background bits, leaving its foreground bits intact.
1312 mBuffer.mData = (mBuffer.mData & mValueMask) | bgMask;
1313 }
1314}
1315
1316
1317////////////////////////////////////////
1318
1319
1320template<Index Log2Dim>
1321template<MergePolicy Policy>
1322inline void
1323LeafNode<bool, Log2Dim>::merge(const LeafNode& other, bool /*bg*/, bool /*otherBG*/)
1324{
1326 if (Policy == MERGE_NODES) return;
1327 for (typename NodeMaskType::OnIterator iter = other.valueMask().beginOn(); iter; ++iter) {
1328 const Index n = iter.pos();
1329 if (mValueMask.isOff(n)) {
1330 mBuffer.mData.set(n, other.mBuffer.mData.isOn(n));
1331 mValueMask.setOn(n);
1332 }
1333 }
1335}
1336
1337template<Index Log2Dim>
1338template<MergePolicy Policy>
1339inline void
1340LeafNode<bool, Log2Dim>::merge(bool tileValue, bool tileActive)
1341{
1343 if (Policy != MERGE_ACTIVE_STATES_AND_NODES) return;
1344 if (!tileActive) return;
1345 // Replace all inactive values with the active tile value.
1346 if (tileValue) mBuffer.mData |= !mValueMask; // -0=>1, +0=>0, -1=>1, +1=>1 (-,+ = off,on)
1347 else mBuffer.mData &= mValueMask; // -0=>0, +0=>0, -1=>0, +1=>1
1348 mValueMask.setOn();
1350}
1351
1352
1353////////////////////////////////////////
1354
1355
1356template<Index Log2Dim>
1357template<typename OtherType>
1358inline void
1360{
1361 mValueMask |= other.valueMask();
1362}
1363
1364
1365template<Index Log2Dim>
1366template<typename OtherType>
1367inline void
1369 const bool&)
1370{
1371 mValueMask &= other.valueMask();
1372}
1373
1374
1375template<Index Log2Dim>
1376template<typename OtherType>
1377inline void
1379 const bool&)
1380{
1381 mValueMask &= !other.valueMask();
1382}
1383
1384
1385////////////////////////////////////////
1386
1387
1388template<Index Log2Dim>
1389inline void
1390LeafNode<bool, Log2Dim>::clip(const CoordBBox& clipBBox, bool background)
1391{
1392 CoordBBox nodeBBox = this->getNodeBoundingBox();
1393 if (!clipBBox.hasOverlap(nodeBBox)) {
1394 // This node lies completely outside the clipping region. Fill it with background tiles.
1395 this->fill(nodeBBox, background, /*active=*/false);
1396 } else if (clipBBox.isInside(nodeBBox)) {
1397 // This node lies completely inside the clipping region. Leave it intact.
1398 return;
1399 }
1400
1401 // This node isn't completely contained inside the clipping region.
1402 // Set any voxels that lie outside the region to the background value.
1403
1404 // Construct a boolean mask that is on inside the clipping region and off outside it.
1405 NodeMaskType mask;
1406 nodeBBox.intersect(clipBBox);
1407 Coord xyz;
1408 int &x = xyz.x(), &y = xyz.y(), &z = xyz.z();
1409 for (x = nodeBBox.min().x(); x <= nodeBBox.max().x(); ++x) {
1410 for (y = nodeBBox.min().y(); y <= nodeBBox.max().y(); ++y) {
1411 for (z = nodeBBox.min().z(); z <= nodeBBox.max().z(); ++z) {
1412 mask.setOn(static_cast<Index32>(this->coordToOffset(xyz)));
1413 }
1414 }
1415 }
1416
1417 // Set voxels that lie in the inactive region of the mask (i.e., outside
1418 // the clipping region) to the background value.
1419 for (MaskOffIter maskIter = mask.beginOff(); maskIter; ++maskIter) {
1420 this->setValueOff(maskIter.pos(), background);
1421 }
1422}
1423
1424
1425////////////////////////////////////////
1426
1427
1428template<Index Log2Dim>
1429inline void
1430LeafNode<bool, Log2Dim>::fill(const CoordBBox& bbox, bool value, bool active)
1431{
1432 auto clippedBBox = this->getNodeBoundingBox();
1433 clippedBBox.intersect(bbox);
1434 if (!clippedBBox) return;
1435
1436 for (Int32 x = clippedBBox.min().x(); x <= clippedBBox.max().x(); ++x) {
1437 const Index offsetX = (x & (DIM-1u))<<2*Log2Dim;
1438 for (Int32 y = clippedBBox.min().y(); y <= clippedBBox.max().y(); ++y) {
1439 const Index offsetXY = offsetX + ((y & (DIM-1u))<< Log2Dim);
1440 for (Int32 z = clippedBBox.min().z(); z <= clippedBBox.max().z(); ++z) {
1441 const Index offset = offsetXY + (z & (DIM-1u));
1442 mValueMask.set(offset, active);
1443 mBuffer.mData.set(offset, value);
1444 }
1445 }
1446 }
1447}
1448
1449template<Index Log2Dim>
1450inline void
1452{
1453 mBuffer.fill(value);
1454}
1455
1456template<Index Log2Dim>
1457inline void
1458LeafNode<bool, Log2Dim>::fill(const bool& value, bool active)
1459{
1460 mBuffer.fill(value);
1461 mValueMask.set(active);
1462}
1463
1464
1465////////////////////////////////////////
1466
1467
1468template<Index Log2Dim>
1469template<typename DenseT>
1470inline void
1471LeafNode<bool, Log2Dim>::copyToDense(const CoordBBox& bbox, DenseT& dense) const
1472{
1473 using DenseValueType = typename DenseT::ValueType;
1474
1475 const size_t xStride = dense.xStride(), yStride = dense.yStride(), zStride = dense.zStride();
1476 const Coord& min = dense.bbox().min();
1477 DenseValueType* t0 = dense.data() + zStride * (bbox.min()[2] - min[2]); // target array
1478 const Int32 n0 = bbox.min()[2] & (DIM-1u);
1479 for (Int32 x = bbox.min()[0], ex = bbox.max()[0] + 1; x < ex; ++x) {
1480 DenseValueType* t1 = t0 + xStride * (x - min[0]);
1481 const Int32 n1 = n0 + ((x & (DIM-1u)) << 2*LOG2DIM);
1482 for (Int32 y = bbox.min()[1], ey = bbox.max()[1] + 1; y < ey; ++y) {
1483 DenseValueType* t2 = t1 + yStride * (y - min[1]);
1484 Int32 n2 = n1 + ((y & (DIM-1u)) << LOG2DIM);
1485 for (Int32 z = bbox.min()[2], ez = bbox.max()[2] + 1; z < ez; ++z, t2 += zStride) {
1486 *t2 = DenseValueType(mBuffer.mData.isOn(n2++));
1487 }
1488 }
1489 }
1490}
1491
1492
1493template<Index Log2Dim>
1494template<typename DenseT>
1495inline void
1496LeafNode<bool, Log2Dim>::copyFromDense(const CoordBBox& bbox, const DenseT& dense,
1497 bool background, bool tolerance)
1498{
1499 using DenseValueType = typename DenseT::ValueType;
1500 struct Local {
1501 inline static bool toBool(const DenseValueType& v) { return !math::isZero(v); }
1502 };
1503
1504 const size_t xStride = dense.xStride(), yStride = dense.yStride(), zStride = dense.zStride();
1505 const Coord& min = dense.bbox().min();
1506 const DenseValueType* s0 = dense.data() + zStride * (bbox.min()[2] - min[2]); // source
1507 const Int32 n0 = bbox.min()[2] & (DIM-1u);
1508 for (Int32 x = bbox.min()[0], ex = bbox.max()[0] + 1; x < ex; ++x) {
1509 const DenseValueType* s1 = s0 + xStride * (x - min[0]);
1510 const Int32 n1 = n0 + ((x & (DIM-1u)) << 2*LOG2DIM);
1511 for (Int32 y = bbox.min()[1], ey = bbox.max()[1] + 1; y < ey; ++y) {
1512 const DenseValueType* s2 = s1 + yStride * (y - min[1]);
1513 Int32 n2 = n1 + ((y & (DIM-1u)) << LOG2DIM);
1514 for (Int32 z = bbox.min()[2], ez = bbox.max()[2]+1; z < ez; ++z, ++n2, s2 += zStride) {
1515 // Note: if tolerance is true (i.e., 1), then all boolean values compare equal.
1516 if (tolerance || (background == Local::toBool(*s2))) {
1517 mValueMask.setOff(n2);
1518 mBuffer.mData.set(n2, background);
1519 } else {
1520 mValueMask.setOn(n2);
1521 mBuffer.mData.set(n2, Local::toBool(*s2));
1522 }
1523 }
1524 }
1525 }
1526}
1527
1528
1529////////////////////////////////////////
1530
1531
1532template<Index Log2Dim>
1533template<typename CombineOp>
1534inline void
1535LeafNode<bool, Log2Dim>::combine(const LeafNode& other, CombineOp& op)
1536{
1537 CombineArgs<bool> args;
1538 for (Index i = 0; i < SIZE; ++i) {
1539 bool result = false, aVal = mBuffer.mData.isOn(i), bVal = other.mBuffer.mData.isOn(i);
1540 op(args.setARef(aVal)
1541 .setAIsActive(mValueMask.isOn(i))
1542 .setBRef(bVal)
1543 .setBIsActive(other.valueMask().isOn(i))
1544 .setResultRef(result));
1545 mValueMask.set(i, args.resultIsActive());
1546 mBuffer.mData.set(i, result);
1547 }
1548}
1549
1550
1551template<Index Log2Dim>
1552template<typename CombineOp>
1553inline void
1554LeafNode<bool, Log2Dim>::combine(bool value, bool valueIsActive, CombineOp& op)
1555{
1556 CombineArgs<bool> args;
1557 args.setBRef(value).setBIsActive(valueIsActive);
1558 for (Index i = 0; i < SIZE; ++i) {
1559 bool result = false, aVal = mBuffer.mData.isOn(i);
1560 op(args.setARef(aVal)
1561 .setAIsActive(mValueMask.isOn(i))
1562 .setResultRef(result));
1563 mValueMask.set(i, args.resultIsActive());
1564 mBuffer.mData.set(i, result);
1565 }
1566}
1567
1568
1569////////////////////////////////////////
1570
1571
1572template<Index Log2Dim>
1573template<typename CombineOp, typename OtherType>
1574inline void
1575LeafNode<bool, Log2Dim>::combine2(const LeafNode& other, const OtherType& value,
1576 bool valueIsActive, CombineOp& op)
1577{
1579 args.setBRef(value).setBIsActive(valueIsActive);
1580 for (Index i = 0; i < SIZE; ++i) {
1581 bool result = false, aVal = other.mBuffer.mData.isOn(i);
1582 op(args.setARef(aVal)
1583 .setAIsActive(other.valueMask().isOn(i))
1584 .setResultRef(result));
1585 mValueMask.set(i, args.resultIsActive());
1586 mBuffer.mData.set(i, result);
1587 }
1588}
1589
1590
1591template<Index Log2Dim>
1592template<typename CombineOp, typename OtherNodeT>
1593inline void
1594LeafNode<bool, Log2Dim>::combine2(bool value, const OtherNodeT& other,
1595 bool valueIsActive, CombineOp& op)
1596{
1598 args.setARef(value).setAIsActive(valueIsActive);
1599 for (Index i = 0; i < SIZE; ++i) {
1600 bool result = false, bVal = other.mBuffer.mData.isOn(i);
1601 op(args.setBRef(bVal)
1602 .setBIsActive(other.valueMask().isOn(i))
1603 .setResultRef(result));
1604 mValueMask.set(i, args.resultIsActive());
1605 mBuffer.mData.set(i, result);
1606 }
1607}
1608
1609
1610template<Index Log2Dim>
1611template<typename CombineOp, typename OtherNodeT>
1612inline void
1613LeafNode<bool, Log2Dim>::combine2(const LeafNode& b0, const OtherNodeT& b1, CombineOp& op)
1614{
1616 for (Index i = 0; i < SIZE; ++i) {
1617 // Default behavior: output voxel is active if either input voxel is active.
1618 mValueMask.set(i, b0.valueMask().isOn(i) || b1.valueMask().isOn(i));
1619
1620 bool result = false, b0Val = b0.mBuffer.mData.isOn(i), b1Val = b1.mBuffer.mData.isOn(i);
1621 op(args.setARef(b0Val)
1622 .setAIsActive(b0.valueMask().isOn(i))
1623 .setBRef(b1Val)
1624 .setBIsActive(b1.valueMask().isOn(i))
1625 .setResultRef(result));
1626 mValueMask.set(i, args.resultIsActive());
1627 mBuffer.mData.set(i, result);
1628 }
1629}
1630
1631
1632} // namespace tree
1633} // namespace OPENVDB_VERSION_NAME
1634} // namespace openvdb
1635
1636#endif // OPENVDB_TREE_LEAF_NODE_BOOL_HAS_BEEN_INCLUDED
ValueT value
Definition GridBuilder.h:1290
ChildT * child
Definition GridBuilder.h:1289
General-purpose arithmetic and comparison routines, most of which accept arbitrary value types (or at...
#define OPENVDB_NO_UNREACHABLE_CODE_WARNING_END
Definition Platform.h:147
#define OPENVDB_NO_UNREACHABLE_CODE_WARNING_BEGIN
Definition Platform.h:146
#define OPENVDB_DEPRECATED_MESSAGE(msg)
Definition Platform.h:154
This struct collects both input and output arguments to "grid combiner" functors used with the tree::...
Definition Types.h:569
CombineArgs & setARef(const AValueType &a)
Redirect the A value to a new external source.
Definition Types.h:621
CombineArgs & setBIsActive(bool b)
Set the active state of the B value.
Definition Types.h:637
CombineArgs & setResultRef(AValueType &val)
Redirect the result value to a new external destination.
Definition Types.h:625
CombineArgs & setBRef(const BValueType &b)
Redirect the B value to a new external source.
Definition Types.h:623
bool resultIsActive() const
Definition Types.h:632
CombineArgs & setAIsActive(bool b)
Set the active state of the A value.
Definition Types.h:635
Tag dispatch class that distinguishes constructors during file input.
Definition Types.h:689
Tag dispatch class that distinguishes topology copy constructors from deep copy constructors.
Definition Types.h:683
Axis-aligned bounding box of signed integer coordinates.
Definition Coord.h:251
void translate(const Coord &t)
Translate this bounding box by (tx, ty, tz).
Definition Coord.h:460
void expand(ValueType padding)
Pad this bounding box with the specified padding.
Definition Coord.h:420
const Coord & min() const
Definition Coord.h:323
bool hasOverlap(const CoordBBox &b) const
Return true if the given bounding box overlaps with this bounding box.
Definition Coord.h:414
const Coord & max() const
Definition Coord.h:324
bool isInside(const Coord &xyz) const
Return true if point (x, y, z) is inside this bounding box.
Definition Coord.h:402
void intersect(const CoordBBox &bbox)
Intersect this bounding box with the given bounding box.
Definition Coord.h:446
void reset()
Definition Coord.h:329
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
void setValue(Index i, const ValueType &)
Set the i'th value of this buffer to the specified value.
Definition LeafBuffer.h:232
LeafNode specialization for values of type bool that stores both the active states and the values of ...
Definition LeafNodeBool.h:29
void stealNodes(ArrayT &, const ValueType &, bool)
Definition LeafNodeBool.h:535
LeafNode & operator=(const LeafNode &)=default
Deep assignment operator.
bool isValueOn(Index offset) const
Return true if the voxel at the given offset is active.
Definition LeafNodeBool.h:301
static Index64 onTileCount()
Definition LeafNodeBool.h:144
void getOrigin(Int32 &x, Int32 &y, Int32 &z) const
Definition LeafNodeBool.h:179
ChildOnCIter cbeginChildOn() const
Definition LeafNodeBool.h:683
bool BuildType
Definition LeafNodeBool.h:32
CoordBBox getNodeBoundingBox() const
Return the bounding box of this node, i.e., the full index space spanned by this leaf node.
Definition LeafNodeBool.h:171
NodeMaskType & getValueMask()
Definition LeafNodeBool.h:712
void setValueOn(Index offset)
Mark the voxel at the given offset as active but don't change its value.
Definition LeafNodeBool.h:271
bool isChildMaskOn(Index) const
Definition LeafNodeBool.h:714
ChildOnCIter beginChildOn() const
Definition LeafNodeBool.h:684
ChildOnIter beginChildOn()
Definition LeafNodeBool.h:685
bool isValueOn(const Coord &xyz) const
Return true if the voxel at the given coordinates is active.
Definition LeafNodeBool.h:299
ValueOnIter endValueOn()
Definition LeafNodeBool.h:673
bool isChildMaskOff() const
Definition LeafNodeBool.h:716
ValueOffCIter cbeginValueOff() const
Definition LeafNodeBool.h:664
Index32 transientData() const
Return the transient data value.
Definition LeafNodeBool.h:192
SharedPtr< LeafNodeType > Ptr
Definition LeafNodeBool.h:36
static Index getChildDim()
Definition LeafNodeBool.h:131
void setValueMask(const NodeMaskType &mask)
Definition LeafNodeBool.h:713
ChildOnIter endChildOn()
Definition LeafNodeBool.h:695
ValueAllIter endValueAll()
Definition LeafNodeBool.h:679
LeafNode * touchLeaf(const Coord &)
Return a pointer to this node.
Definition LeafNodeBool.h:545
LeafNode * probeLeaf(const Coord &)
Definition LeafNodeBool.h:548
bool isValueMaskOff() const
Definition LeafNodeBool.h:709
void prune(const ValueType &=zeroVal< ValueType >())
This function exists only to enable template instantiation.
Definition LeafNodeBool.h:524
bool isValueMaskOn() const
Definition LeafNodeBool.h:707
DenseIter< const LeafNode, const bool > ChildAllCIter
Definition LeafNodeBool.h:659
void getNodes(ArrayT &) const
Definition LeafNodeBool.h:534
NodeMaskType mValueMask
Bitmask that determines which voxels are active.
Definition LeafNodeBool.h:726
void setValuesOff()
Mark all voxels as inactive but don't change their values.
Definition LeafNodeBool.h:296
ValueAllCIter endValueAll() const
Definition LeafNodeBool.h:678
ChildIter< MaskOnIter, const LeafNode > ChildOnCIter
Definition LeafNodeBool.h:655
Index64 onLeafVoxelCount() const
Definition LeafNodeBool.h:142
void setValueOnly(Index offset, bool val)
Set the value of the voxel at the given offset but don't change its active state.
Definition LeafNodeBool.h:256
ChildOffCIter endChildOff() const
Definition LeafNodeBool.h:697
ValueAllCIter cbeginValueAll() const
Definition LeafNodeBool.h:667
NodeT * probeNode(const Coord &)
Definition LeafNodeBool.h:531
ValueOnCIter beginValueOn() const
Definition LeafNodeBool.h:662
void denseFill(const CoordBBox &bbox, bool val, bool on=true)
Set all voxels within an axis-aligned box to the specified value and active state.
Definition LeafNodeBool.h:312
static void evalNodeOrigin(Coord &xyz)
Compute the origin of the leaf node that contains the voxel with the given coordinates.
Definition LeafNodeBool.h:723
const Buffer & buffer() const
Definition LeafNodeBool.h:215
LeafNode * probeLeafAndCache(const Coord &, AccessorT &)
Definition LeafNodeBool.h:550
void setValueMaskOn(Index n)
Definition LeafNodeBool.h:719
Index64 offLeafVoxelCount() const
Definition LeafNodeBool.h:143
const LeafNode * probeLeaf(const Coord &) const
Return a const pointer to this node.
Definition LeafNodeBool.h:562
void setOrigin(const Coord &origin)
Set the grid index coordinates of this node's local origin.
Definition LeafNodeBool.h:174
const Coord & origin() const
Return the grid index coordinates of this node's local origin.
Definition LeafNodeBool.h:177
static Index getValueLevel(const Coord &)
Return the level (0) at which leaf node values reside.
Definition LeafNodeBool.h:246
typename NodeMaskType::OnIterator MaskOnIter
Definition LeafNodeBool.h:582
bool isInactive() const
Return true if all of this node's values are inactive.
Definition LeafNodeBool.h:459
Buffer mBuffer
Bitmask representing the values of voxels.
Definition LeafNodeBool.h:728
ValueIter< MaskOnIter, const LeafNode, const bool > ValueOnCIter
Definition LeafNodeBool.h:649
bool isValueMaskOff(Index n) const
Definition LeafNodeBool.h:708
ValueOnCIter cendValueOn() const
Definition LeafNodeBool.h:671
bool isAllocated() const
Return true if memory for this node's buffer has been allocated.
Definition LeafNodeBool.h:154
static Index getValueLevelAndCache(const Coord &, AccessorT &)
Return the LEVEL (=0) at which leaf node values reside.
Definition LeafNodeBool.h:419
static Index numValues()
Definition LeafNodeBool.h:128
ValueOffCIter beginValueOff() const
Definition LeafNodeBool.h:665
ValueIter< MaskOffIter, LeafNode, const bool > ValueOffIter
Definition LeafNodeBool.h:650
ChildAllCIter cbeginChildAll() const
Definition LeafNodeBool.h:689
ChildOffIter endChildOff()
Definition LeafNodeBool.h:698
ChildAllIter beginChildAll()
Definition LeafNodeBool.h:691
static Index getLevel()
Definition LeafNodeBool.h:129
bool isValueOnAndCache(const Coord &xyz, AccessorT &) const
Return true if the voxel at the given coordinates is active.
Definition LeafNodeBool.h:360
void addLeaf(LeafNode *)
Definition LeafNodeBool.h:525
ValueOnIter beginValueOn()
Definition LeafNodeBool.h:663
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 LeafNodeBool.h:385
void setValueOffAndCache(const Coord &xyz, bool value, AccessorT &)
Change the value of the voxel at the given coordinates and mark it as inactive.
Definition LeafNodeBool.h:376
ChildIter< MaskOffIter, const LeafNode > ChildOffCIter
Definition LeafNodeBool.h:657
NodeT * probeNodeAndCache(const Coord &, AccessorT &)
Definition LeafNodeBool.h:552
ValueIter< MaskDenseIter, LeafNode, const bool > ValueAllIter
Definition LeafNodeBool.h:652
ChildOffCIter cbeginChildOff() const
Definition LeafNodeBool.h:686
ChildOffIter beginChildOff()
Definition LeafNodeBool.h:688
bool isChildMaskOff(Index) const
Definition LeafNodeBool.h:715
Index64 onVoxelCount() const
Return the number of active voxels.
Definition LeafNodeBool.h:139
typename NodeMaskType::OffIterator MaskOffIter
Definition LeafNodeBool.h:583
ChildOffCIter beginChildOff() const
Definition LeafNodeBool.h:687
static Index64 offTileCount()
Definition LeafNodeBool.h:145
void setValueOff(const Coord &xyz)
Mark the voxel at the given coordinates as inactive but don't change its value.
Definition LeafNodeBool.h:259
bool ValueType
Definition LeafNodeBool.h:33
ValueOffIter endValueOff()
Definition LeafNodeBool.h:676
void setValueOff(Index offset)
Mark the voxel at the given offset as inactive but don't change its value.
Definition LeafNodeBool.h:261
const LeafNode * probeConstLeafAndCache(const Coord &, AccessorT &) const
Definition LeafNodeBool.h:567
ChildAllCIter endChildAll() const
Definition LeafNodeBool.h:700
const NodeT * probeConstNodeAndCache(const Coord &, AccessorT &) const
Definition LeafNodeBool.h:569
ValueOnCIter cbeginValueOn() const
Definition LeafNodeBool.h:661
static void getNodeLog2Dims(std::vector< Index > &dims)
Definition LeafNodeBool.h:130
ChildOnCIter endChildOn() const
Definition LeafNodeBool.h:694
const LeafNode * probeConstLeaf(const Coord &) const
Definition LeafNodeBool.h:565
static Index32 nonLeafCount()
Definition LeafNodeBool.h:136
bool probeValueAndCache(const Coord &xyz, bool &val, AccessorT &) const
Return true if the voxel at the given coordinates is active and return the voxel value in val.
Definition LeafNodeBool.h:411
ChildOnCIter cendChildOn() const
Definition LeafNodeBool.h:693
static bool hasActiveTiles()
Return false since leaf nodes never contain tiles.
Definition LeafNodeBool.h:304
ChildAllCIter cendChildAll() const
Definition LeafNodeBool.h:699
ValueIter< MaskOnIter, LeafNode, const bool > ValueOnIter
Definition LeafNodeBool.h:648
void setValueOnlyAndCache(const Coord &xyz, bool val, AccessorT &)
Change the value of the voxel at the given coordinates but preserve its state.
Definition LeafNodeBool.h:371
DenseIter< LeafNode, bool > ChildAllIter
Definition LeafNodeBool.h:658
ChildAllIter endChildAll()
Definition LeafNodeBool.h:701
void setValueMask(Index n, bool on)
Definition LeafNodeBool.h:718
const NodeMaskType & valueMask() const
Definition LeafNodeBool.h:711
Index64 offVoxelCount() const
Return the number of inactive voxels.
Definition LeafNodeBool.h:141
void swap(Buffer &other)
Exchange this node's data buffer with the given data buffer without changing the active states of the...
Definition LeafNodeBool.h:214
ValueAllCIter cendValueAll() const
Definition LeafNodeBool.h:677
ChildIter< MaskOnIter, LeafNode > ChildOnIter
Definition LeafNodeBool.h:654
void negate()
Definition LeafNodeBool.h:463
ChildAllCIter beginChildAll() const
Definition LeafNodeBool.h:690
const LeafNode * probeLeafAndCache(const Coord &, AccessorT &) const
Definition LeafNodeBool.h:564
void setActiveStateAndCache(const Coord &xyz, bool on, AccessorT &)
Set the active state of the voxel at the given coordinates without changing its value.
Definition LeafNodeBool.h:402
void getOrigin(Coord &origin) const
Definition LeafNodeBool.h:178
void setValuesOn()
Mark all voxels as active but don't change their values.
Definition LeafNodeBool.h:294
void setTransientData(Index32 transientData)
Set the transient data value.
Definition LeafNodeBool.h:194
void nodeCount(std::vector< Index32 > &) const
no-op
Definition LeafNodeBool.h:135
static Index size()
Definition LeafNodeBool.h:127
ChildOffCIter cendChildOff() const
Definition LeafNodeBool.h:696
typename NodeMaskType::DenseIterator MaskDenseIter
Definition LeafNodeBool.h:584
bool isEmpty() const
Return true if this node has no active voxels.
Definition LeafNodeBool.h:148
void setValueAndCache(const Coord &xyz, bool val, AccessorT &)
Change the value of the voxel at the given coordinates and mark it as active.
Definition LeafNodeBool.h:365
ValueOffIter beginValueOff()
Definition LeafNodeBool.h:666
const NodeT * probeConstNode(const Coord &) const
Definition LeafNodeBool.h:533
void setValueOn(const Coord &xyz)
Mark the voxel at the given coordinates as active but don't change its value.
Definition LeafNodeBool.h:269
Buffer & buffer()
Definition LeafNodeBool.h:216
void setActiveState(Index offset, bool on)
Set the active state of the voxel at the given offset but don't change its value.
Definition LeafNodeBool.h:251
static Index32 leafCount()
Definition LeafNodeBool.h:133
bool allocate()
Allocate memory for this node's buffer if it has not already been allocated.
Definition LeafNodeBool.h:158
const NodeMaskType & getValueMask() const
Definition LeafNodeBool.h:710
const bool & getFirstValue() const
Return a const reference to the first entry in the buffer.
Definition LeafNodeBool.h:424
void addLeafAndCache(LeafNode *, AccessorT &)
Definition LeafNodeBool.h:527
const bool & getLastValue() const
Return a const reference to the last entry in the buffer.
Definition LeafNodeBool.h:428
ValueOffCIter cendValueOff() const
Definition LeafNodeBool.h:674
ValueIter< MaskDenseIter, const LeafNode, const bool > ValueAllCIter
Definition LeafNodeBool.h:653
const bool & getValueAndCache(const Coord &xyz, AccessorT &) const
Return the value of the voxel at the given coordinates.
Definition LeafNodeBool.h:355
void setValueMaskOff(Index n)
Definition LeafNodeBool.h:720
bool isDense() const
Return true if this node only contains active voxels.
Definition LeafNodeBool.h:150
ValueOffCIter endValueOff() const
Definition LeafNodeBool.h:675
NodeT * stealNode(const Coord &, const ValueType &, bool)
Definition LeafNodeBool.h:529
void setValue(const Coord &xyz, bool val)
Set the value of the voxel at the given coordinates and mark the voxel as active.
Definition LeafNodeBool.h:276
ValueOnCIter endValueOn() const
Definition LeafNodeBool.h:672
void voxelizeActiveTiles(bool=true)
No-op.
Definition LeafNodeBool.h:471
void modifyValueAndActiveStateAndCache(const Coord &xyz, const ModifyOp &op, AccessorT &)
Definition LeafNodeBool.h:393
LeafNode * touchLeafAndCache(const Coord &, AccessorT &)
Definition LeafNodeBool.h:547
ValueAllCIter beginValueAll() const
Definition LeafNodeBool.h:668
Coord mOrigin
Global grid index coordinates (x,y,z) of the local origin of this node.
Definition LeafNodeBool.h:730
static Index dim()
Return the number of voxels in each dimension.
Definition LeafNodeBool.h:126
ChildIter< MaskOffIter, LeafNode > ChildOffIter
Definition LeafNodeBool.h:656
ValueIter< MaskOffIter, const LeafNode, const bool > ValueOffCIter
Definition LeafNodeBool.h:651
bool isValueMaskOn(Index n) const
Definition LeafNodeBool.h:706
ValueAllIter beginValueAll()
Definition LeafNodeBool.h:669
LeafNode(const LeafNode< OtherValueType, Log2Dim > &other)
Value conversion copy constructor.
Templated block class to hold specific data types and a fixed number of values determined by Log2Dim....
Definition LeafNode.h:38
static Coord offsetToLocalCoord(Index n)
Return the local coordinates for a linear table offset, where offset 0 has coordinates (0,...
Definition LeafNode.h:1032
void writeTopology(std::ostream &os, bool toHalf=false) const
Write out just the topology.
Definition LeafNode.h:1288
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:1216
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:1243
const ValueType & getValue(const Coord &xyz) const
Return the value of the voxel at the given coordinates.
Definition LeafNode.h:1057
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:1115
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:1708
Index medianOff(ValueType &value, ValueType *tmp=nullptr) const
Computes the median value of all the inactive voxels in this node.
Definition LeafNode.h:1565
ValueType medianAll(ValueType *tmp=nullptr) const
Computes the median value of all the active AND inactive voxels in this node.
Definition LeafNode.h:1523
~LeafNode()
Destructor.
Definition LeafNode.h:1002
void readTopology(std::istream &is, bool fromHalf=false)
Read in just the topology.
Definition LeafNode.h:1280
Index medianOn(ValueType &value, ValueType *tmp=nullptr) const
Computes the median value of all the active voxels in this node.
Definition LeafNode.h:1541
void addTile(Index level, const Coord &, const ValueType &, bool)
Definition LeafNode.h:1592
void resetBackground(const ValueType &oldBackground, const ValueType &newBackground)
Replace inactive occurrences of oldBackground with newBackground, and inactive occurrences of -oldBac...
Definition LeafNode.h:1621
void modifyValueAndActiveState(const Coord &xyz, const ModifyOp &op)
Apply a functor to the voxel at the given coordinates.
Definition LeafNode.h:458
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:1699
void clip(const CoordBBox &, const ValueType &background)
Set all voxels that lie outside the given axis-aligned box to the background.
Definition LeafNode.h:1133
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:1107
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:1691
static Index coordToOffset(const Coord &xyz)
Return the linear table offset of the given global or local coordinates.
Definition LeafNode.h:1022
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:1479
void writeBuffers(std::ostream &os, bool toHalf=false) const
Write buffers to a stream.
Definition LeafNode.h:1414
void combine(const LeafNode &other, CombineOp &op)
Definition LeafNode.h:1732
Index64 memUsageIfLoaded() const
Definition LeafNode.h:1451
void combine2(const LeafNode &other, const OtherType &, bool valueIsActive, CombineOp &)
Definition LeafNode.h:1772
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:1173
const NodeMaskType & valueMask() const
Definition LeafNode.h:858
void readBuffers(std::istream &is, bool fromHalf=false)
Read buffers from a stream.
Definition LeafNode.h:1316
bool isConstant(ValueType &firstValue, bool &state, const ValueType &tolerance=zeroVal< ValueType >()) const
Definition LeafNode.h:1487
friend class LeafNode
Definition LeafNode.h:835
Coord offsetToGlobalCoord(Index n) const
Return the global coordinates for a linear table offset.
Definition LeafNode.h:1046
bool operator==(const LeafNode &other) const
Check for buffer, state and origin equivalence.
Definition LeafNode.h:1431
static const Index SIZE
Definition LeafNode.h:53
void evalActiveBoundingBox(CoordBBox &bbox, bool visitVoxels=true) const
Definition LeafNode.h:1461
void merge(const LeafNode &)
Definition LeafNode.h:1642
void setValueOn(const Coord &xyz)
Mark the voxel at the given coordinates as active but don't change its value.
Definition LeafNode.h:419
const NodeMaskType & getValueMask() const
Definition LeafNode.h:856
void addTileAndCache(Index, const Coord &, const ValueType &, bool, AccessorT &)
Definition LeafNode.h:1609
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
Index64 memUsage() const
Return the memory in bytes occupied by this node.
Definition LeafNode.h:1441
std::string str() const
Return a string representation of this node.
Definition LeafNode.h:1009
bool probeValue(const Coord &xyz, ValueType &val) const
Return true if the voxel at the given coordinates is active.
Definition LeafNode.h:1073
Definition NodeMasks.h:271
Bit mask for the internal and leaf nodes of VDB. This is a 64-bit implementation.
Definition NodeMasks.h:308
Index32 countOn() const
Return the total number of on bits.
Definition NodeMasks.h:443
OnIterator beginOn() const
Definition NodeMasks.h:352
OffIterator beginOff() const
Definition NodeMasks.h:354
void set(Index32 n, bool On)
Set the nth bit to the specified state.
Definition NodeMasks.h:462
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
OPENVDB_API uint32_t getFormatVersion(std::ios_base &)
Return the file format version number associated with the given input stream.
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...
Index32 Index
Definition Types.h:54
uint32_t Index32
Definition Types.h:52
@ OPENVDB_FILE_VERSION_BOOL_LEAF_OPTIMIZATION
Definition version.h.in:250
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:508
@ MERGE_ACTIVE_STATES_AND_NODES
Definition Types.h:509
Definition Exceptions.h:13
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
ChildIter(const MaskIterT &iter, NodeT *parent)
Definition LeafNodeBool.h:619
bool getItem(Index pos, void *&child, NonConstValueT &value) const
Definition LeafNodeBool.h:633
DenseIter(const MaskDenseIter &iter, NodeT *parent)
Definition LeafNodeBool.h:631
typename BaseT::NonConstValueType NonConstValueT
Definition LeafNodeBool.h:628
void unsetItem(Index pos, const ValueT &val) const
Definition LeafNodeBool.h:644
void setItem(Index pos, bool value) const
Definition LeafNodeBool.h:601
void modifyValue(const ModifyOp &op) const
Definition LeafNodeBool.h:610
ValueIter(const MaskIterT &iter, NodeT *parent)
Definition LeafNodeBool.h:595
const bool & getValue() const
Definition LeafNodeBool.h:598
const bool & getItem(Index pos) const
Definition LeafNodeBool.h:597
void setValue(bool value) const
Definition LeafNodeBool.h:603
void modifyItem(Index n, const ModifyOp &op) const
Definition LeafNodeBool.h:607
Definition LeafNode.h:894
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:121
#define OPENVDB_USE_VERSION_NAMESPACE
Definition version.h.in:212