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