GCC Code Coverage Report


Directory: ./
File: openvdb/openvdb/tree/LeafNodeBool.h
Date: 2022-07-25 17:40:05
Exec Total Coverage
Lines: 256 285 89.8%
Functions: 66 82 80.5%
Branches: 132 254 52.0%

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