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