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