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