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