HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Dense.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 /// @file Dense.h
32 ///
33 /// @brief This file defines a simple dense grid and efficient
34 /// converters to and from VDB grids.
35 
36 #ifndef OPENVDB_TOOLS_DENSE_HAS_BEEN_INCLUDED
37 #define OPENVDB_TOOLS_DENSE_HAS_BEEN_INCLUDED
38 
39 #include <openvdb/Types.h>
40 #include <openvdb/Grid.h>
42 #include <openvdb/Exceptions.h>
43 #include <openvdb/util/Formats.h>
44 #include "Prune.h"
45 #include <tbb/parallel_for.h>
46 #include <iostream>
47 #include <memory>
48 #include <string>
49 #include <utility> // for std::pair
50 #include <vector>
51 
52 namespace openvdb {
54 namespace OPENVDB_VERSION_NAME {
55 namespace tools {
56 
57 /// @brief Populate a dense grid with the values of voxels from a sparse grid,
58 /// where the sparse grid intersects the dense grid.
59 /// @param sparse an OpenVDB grid or tree from which to copy values
60 /// @param dense the dense grid into which to copy values
61 /// @param serial if false, process voxels in parallel
62 template<typename DenseT, typename GridOrTreeT>
63 void
65  const GridOrTreeT& sparse,
66  DenseT& dense,
67  bool serial = false);
68 
69 
70 /// @brief Populate a sparse grid with the values of all of the voxels of a dense grid.
71 /// @param dense the dense grid from which to copy values
72 /// @param sparse an OpenVDB grid or tree into which to copy values
73 /// @param tolerance values in the dense grid that are within this tolerance of the sparse
74 /// grid's background value become inactive background voxels or tiles in the sparse grid
75 /// @param serial if false, process voxels in parallel
76 template<typename DenseT, typename GridOrTreeT>
77 void
79  const DenseT& dense,
80  GridOrTreeT& sparse,
81  const typename GridOrTreeT::ValueType& tolerance,
82  bool serial = false);
83 
84 
85 ////////////////////////////////////////
86 
87 /// We currently support the following two 3D memory layouts for dense
88 /// volumes: XYZ, i.e. x is the fastest moving index, and ZYX, i.e. z
89 /// is the fastest moving index. The ZYX memory layout leads to nested
90 /// for-loops of the order x, y, z, which we find to be the most
91 /// intuitive. Hence, ZYX is the layout used throughout VDB. However,
92 /// other data structures, e.g. Houdini and Maya, employ the XYZ
93 /// layout. Clearly a dense volume with the ZYX layout converts more
94 /// efficiently to a VDB, but we support both for convenience.
96 
97 /// @brief Base class for Dense which is defined below.
98 /// @note The constructor of this class is protected to prevent direct
99 /// instantiation.
100 template<typename ValueT, MemoryLayout Layout> class DenseBase;
101 
102 /// @brief Partial template specialization of DenseBase.
103 /// @note ZYX is the memory-layout in VDB. It leads to nested
104 /// for-loops of the order x, y, z which we find to be the most intuitive.
105 template<typename ValueT>
106 class DenseBase<ValueT, LayoutZYX>
107 {
108 public:
109  /// @brief Return the linear offset into this grid's value array given by
110  /// unsigned coordinates (i, j, k), i.e., coordinates relative to
111  /// the origin of this grid's bounding box.
112  ///
113  /// @warning The input coordinates are assume to be relative to
114  /// the grid's origin, i.e. minimum of its index bounding box!
115  inline size_t coordToOffset(size_t i, size_t j, size_t k) const { return i*mX + j*mY + k; }
116 
117  /// @brief Return the local coordinate corresponding to the specified linear offset.
118  ///
119  /// @warning The returned coordinate is relative to the origin of this
120  /// grid's bounding box so add dense.origin() to get absolute coordinates.
121  inline Coord offsetToLocalCoord(size_t n) const
122  {
123  const size_t x = n / mX;
124  n -= mX*x;
125  const size_t y = n / mY;
126  return Coord(Coord::ValueType(x), Coord::ValueType(y), Coord::ValueType(n - mY*y));
127  }
128 
129  /// @brief Return the stride of the array in the x direction ( = dimY*dimZ).
130  /// @note This method is required by both CopyToDense and CopyFromDense.
131  inline size_t xStride() const { return mX; }
132 
133  /// @brief Return the stride of the array in the y direction ( = dimZ).
134  /// @note This method is required by both CopyToDense and CopyFromDense.
135  inline size_t yStride() const { return mY; }
136 
137  /// @brief Return the stride of the array in the z direction ( = 1).
138  /// @note This method is required by both CopyToDense and CopyFromDense.
139  static size_t zStride() { return 1; }
140 
141 protected:
142  /// Protected constructor so as to prevent direct instantiation
143  DenseBase(const CoordBBox& bbox) : mBBox(bbox), mY(bbox.dim()[2]), mX(mY*bbox.dim()[1]) {}
144 
145  const CoordBBox mBBox;//signed coordinates of the domain represented by the grid
146  const size_t mY, mX;//strides in the y and x direction
147 };// end of DenseBase<ValueT, LayoutZYX>
148 
149 /// @brief Partial template specialization of DenseBase.
150 /// @note This is the memory-layout employed in Houdini and Maya. It leads
151 /// to nested for-loops of the order z, y, x.
152 template<typename ValueT>
153 class DenseBase<ValueT, LayoutXYZ>
154 {
155 public:
156  /// @brief Return the linear offset into this grid's value array given by
157  /// unsigned coordinates (i, j, k), i.e., coordinates relative to
158  /// the origin of this grid's bounding box.
159  ///
160  /// @warning The input coordinates are assume to be relative to
161  /// the grid's origin, i.e. minimum of its index bounding box!
162  inline size_t coordToOffset(size_t i, size_t j, size_t k) const { return i + j*mY + k*mZ; }
163 
164  /// @brief Return the index coordinate corresponding to the specified linear offset.
165  ///
166  /// @warning The returned coordinate is relative to the origin of this
167  /// grid's bounding box so add dense.origin() to get absolute coordinates.
168  inline Coord offsetToLocalCoord(size_t n) const
169  {
170  const size_t z = n / mZ;
171  n -= mZ*z;
172  const size_t y = n / mY;
173  return Coord(Coord::ValueType(n - mY*y), Coord::ValueType(y), Coord::ValueType(z));
174  }
175 
176  /// @brief Return the stride of the array in the x direction ( = 1).
177  /// @note This method is required by both CopyToDense and CopyFromDense.
178  static size_t xStride() { return 1; }
179 
180  /// @brief Return the stride of the array in the y direction ( = dimX).
181  /// @note This method is required by both CopyToDense and CopyFromDense.
182  inline size_t yStride() const { return mY; }
183 
184  /// @brief Return the stride of the array in the y direction ( = dimX*dimY).
185  /// @note This method is required by both CopyToDense and CopyFromDense.
186  inline size_t zStride() const { return mZ; }
187 
188 protected:
189  /// Protected constructor so as to prevent direct instantiation
190  DenseBase(const CoordBBox& bbox) : mBBox(bbox), mY(bbox.dim()[0]), mZ(mY*bbox.dim()[1]) {}
191 
192  const CoordBBox mBBox;//signed coordinates of the domain represented by the grid
193  const size_t mY, mZ;//strides in the y and z direction
194 };// end of DenseBase<ValueT, LayoutXYZ>
195 
196 /// @brief Dense is a simple dense grid API used by the CopyToDense and
197 /// CopyFromDense classes defined below.
198 /// @details Use the Dense class to efficiently produce a dense in-memory
199 /// representation of an OpenVDB grid. However, be aware that a dense grid
200 /// could have a memory footprint that is orders of magnitude larger than
201 /// the sparse grid from which it originates.
202 ///
203 /// @note This class can be used as a simple wrapper for existing dense grid
204 /// classes if they provide access to the raw data array.
205 /// @note This implementation allows for the 3D memory layout to be
206 /// defined by the MemoryLayout template parameter (see above for definition).
207 /// The default memory layout is ZYX since that's the layout used by OpenVDB grids.
208 template<typename ValueT, MemoryLayout Layout = LayoutZYX>
209 class Dense : public DenseBase<ValueT, Layout>
210 {
211 public:
212  using ValueType = ValueT;
216 
217  /// @brief Construct a dense grid with a given range of coordinates.
218  ///
219  /// @param bbox the bounding box of the (signed) coordinate range of this grid
220  /// @throw ValueError if the bounding box is empty.
221  /// @note The min and max coordinates of the bounding box are inclusive.
222  Dense(const CoordBBox& bbox) : BaseT(bbox) { this->init(); }
223 
224  /// @brief Construct a dense grid with a given range of coordinates and initial value
225  ///
226  /// @param bbox the bounding box of the (signed) coordinate range of this grid
227  /// @param value the initial value of the grid.
228  /// @throw ValueError if the bounding box is empty.
229  /// @note The min and max coordinates of the bounding box are inclusive.
230  Dense(const CoordBBox& bbox, const ValueT& value) : BaseT(bbox)
231  {
232  this->init();
233  this->fill(value);
234  }
235 
236  /// @brief Construct a dense grid that wraps an external array.
237  ///
238  /// @param bbox the bounding box of the (signed) coordinate range of this grid
239  /// @param data a raw C-style array whose size is commensurate with
240  /// the coordinate domain of @a bbox
241  ///
242  /// @note The data array is assumed to have a stride of one in the @e z direction.
243  /// @throw ValueError if the bounding box is empty.
244  /// @note The min and max coordinates of the bounding box are inclusive.
245  Dense(const CoordBBox& bbox, ValueT* data) : BaseT(bbox), mData(data)
246  {
247  if (BaseT::mBBox.empty()) {
248  OPENVDB_THROW(ValueError, "can't construct a dense grid with an empty bounding box");
249  }
250  }
251 
252  /// @brief Construct a dense grid with a given origin and dimensions.
253  ///
254  /// @param dim the desired dimensions of the grid
255  /// @param min the signed coordinates of the first voxel in the dense grid
256  /// @throw ValueError if any of the dimensions are zero.
257  /// @note The @a min coordinate is inclusive, and the max coordinate will be
258  /// @a min + @a dim - 1.
259  Dense(const Coord& dim, const Coord& min = Coord(0))
260  : BaseT(CoordBBox(min, min+dim.offsetBy(-1)))
261  {
262  this->init();
263  }
264 
265  /// @brief Return the memory layout for this grid (see above for definitions).
266  static MemoryLayout memoryLayout() { return Layout; }
267 
268  /// @brief Return a raw pointer to this grid's value array.
269  /// @note This method is required by CopyToDense.
270  inline ValueT* data() { return mData; }
271 
272  /// @brief Return a raw pointer to this grid's value array.
273  /// @note This method is required by CopyFromDense.
274  inline const ValueT* data() const { return mData; }
275 
276  /// @brief Return the bounding box of the signed index domain of this grid.
277  /// @note This method is required by both CopyToDense and CopyFromDense.
278  inline const CoordBBox& bbox() const { return BaseT::mBBox; }
279 
280  /// Return the grid's origin in index coordinates.
281  inline const Coord& origin() const { return BaseT::mBBox.min(); }
282 
283  /// @brief Return the number of voxels contained in this grid.
284  inline Index64 valueCount() const { return BaseT::mBBox.volume(); }
285 
286  /// @brief Set the value of the voxel at the given array offset.
287  inline void setValue(size_t offset, const ValueT& value) { mData[offset] = value; }
288 
289  /// @brief Return a const reference to the value of the voxel at the given array offset.
290  const ValueT& getValue(size_t offset) const { return mData[offset]; }
291 
292  /// @brief Return a non-const reference to the value of the voxel at the given array offset.
293  ValueT& getValue(size_t offset) { return mData[offset]; }
294 
295  /// @brief Set the value of the voxel at unsigned index coordinates (i, j, k).
296  /// @note This is somewhat slower than using an array offset.
297  inline void setValue(size_t i, size_t j, size_t k, const ValueT& value)
298  {
299  mData[BaseT::coordToOffset(i,j,k)] = value;
300  }
301 
302  /// @brief Return a const reference to the value of the voxel
303  /// at unsigned index coordinates (i, j, k).
304  /// @note This is somewhat slower than using an array offset.
305  inline const ValueT& getValue(size_t i, size_t j, size_t k) const
306  {
307  return mData[BaseT::coordToOffset(i,j,k)];
308  }
309 
310  /// @brief Return a non-const reference to the value of the voxel
311  /// at unsigned index coordinates (i, j, k).
312  /// @note This is somewhat slower than using an array offset.
313  inline ValueT& getValue(size_t i, size_t j, size_t k)
314  {
315  return mData[BaseT::coordToOffset(i,j,k)];
316  }
317 
318  /// @brief Set the value of the voxel at the given signed coordinates.
319  /// @note This is slower than using either an array offset or unsigned index coordinates.
320  inline void setValue(const Coord& xyz, const ValueT& value)
321  {
322  mData[this->coordToOffset(xyz)] = value;
323  }
324 
325  /// @brief Return a const reference to the value of the voxel at the given signed coordinates.
326  /// @note This is slower than using either an array offset or unsigned index coordinates.
327  inline const ValueT& getValue(const Coord& xyz) const
328  {
329  return mData[this->coordToOffset(xyz)];
330  }
331 
332  /// @brief Return a non-const reference to the value of the voxel
333  /// at the given signed coordinates.
334  /// @note This is slower than using either an array offset or unsigned index coordinates.
335  inline ValueT& getValue(const Coord& xyz)
336  {
337  return mData[this->coordToOffset(xyz)];
338  }
339 
340  /// @brief Fill this grid with a constant value.
341  inline void fill(const ValueT& value)
342  {
343  size_t size = this->valueCount();
344  ValueT* a = mData;
345  while(size--) *a++ = value;
346  }
347 
348  /// @brief Return the linear offset into this grid's value array given by
349  /// the specified signed coordinates, i.e., coordinates in the space of
350  /// this grid's bounding box.
351  ///
352  /// @note This method reflects the fact that we assume the same
353  /// layout of values as an OpenVDB grid, i.e., the fastest coordinate is @e z.
354  inline size_t coordToOffset(const Coord& xyz) const
355  {
356  assert(BaseT::mBBox.isInside(xyz));
357  return BaseT::coordToOffset(size_t(xyz[0]-BaseT::mBBox.min()[0]),
358  size_t(xyz[1]-BaseT::mBBox.min()[1]),
359  size_t(xyz[2]-BaseT::mBBox.min()[2]));
360  }
361 
362  /// @brief Return the global coordinate corresponding to the specified linear offset.
363  inline Coord offsetToCoord(size_t n) const
364  {
365  return this->offsetToLocalCoord(n) + BaseT::mBBox.min();
366  }
367 
368  /// @brief Return the memory footprint of this Dense grid in bytes.
369  inline Index64 memUsage() const
370  {
371  return sizeof(*this) + BaseT::mBBox.volume() * sizeof(ValueType);
372  }
373 
374  /// @brief Output a human-readable description of this grid to the
375  /// specified stream.
376  void print(const std::string& name = "", std::ostream& os = std::cout) const
377  {
378  const Coord dim = BaseT::mBBox.dim();
379  os << "Dense Grid";
380  if (!name.empty()) os << " \"" << name << "\"";
381  util::printBytes(os, this->memUsage(), ":\n Memory footprint: ");
382  os << " Dimensions of grid : " << dim[0] << " x " << dim[1] << " x " << dim[2] << "\n";
383  os << " Number of voxels: " << util::formattedInt(this->valueCount()) << "\n";
384  os << " Bounding box of voxels: " << BaseT::mBBox << "\n";
385  os << " Memory layout: " << (Layout == LayoutZYX ? "ZYX (" : "XYZ (dis")
386  << "similar to VDB)\n";
387  }
388 
389 private:
390  /// @brief Private method to initialize the dense value array.
391  void init()
392  {
393  if (BaseT::mBBox.empty()) {
394  OPENVDB_THROW(ValueError, "can't construct a dense grid with an empty bounding box");
395  }
396  mArray.reset(new ValueT[BaseT::mBBox.volume()]);
397  mData = mArray.get();
398  }
399 
400  std::unique_ptr<ValueT[]> mArray;
401  ValueT* mData;//raw c-style pointer to values
402 };// end of Dense
403 
404 ////////////////////////////////////////
405 
406 
407 /// @brief Copy an OpenVDB tree into an existing dense grid.
408 ///
409 /// @note Only voxels that intersect the dense grid's bounding box are copied
410 /// from the OpenVDB tree. But both active and inactive voxels are copied,
411 /// so all existing values in the dense grid are overwritten, regardless of
412 /// the OpenVDB tree's topology.
413 template<typename _TreeT, typename _DenseT = Dense<typename _TreeT::ValueType> >
415 {
416 public:
417  using DenseT = _DenseT;
418  using TreeT = _TreeT;
419  using ValueT = typename TreeT::ValueType;
420 
421  CopyToDense(const TreeT& tree, DenseT& dense)
422  : mRoot(&(tree.root())), mDense(&dense) {}
423 
424  void copy(bool serial = false) const
425  {
426  if (serial) {
427  mRoot->copyToDense(mDense->bbox(), *mDense);
428  } else {
429  tbb::parallel_for(mDense->bbox(), *this);
430  }
431  }
432 
433  /// @brief Public method called by tbb::parallel_for
434  void operator()(const CoordBBox& bbox) const
435  {
436  mRoot->copyToDense(bbox, *mDense);
437  }
438 
439 private:
440  const typename TreeT::RootNodeType* mRoot;
441  DenseT* mDense;
442 };// CopyToDense
443 
444 
445 // Convenient wrapper function for the CopyToDense class
446 template<typename DenseT, typename GridOrTreeT>
447 void
448 copyToDense(const GridOrTreeT& sparse, DenseT& dense, bool serial)
449 {
450  using Adapter = TreeAdapter<GridOrTreeT>;
451  using TreeT = typename Adapter::TreeType;
452 
453  CopyToDense<TreeT, DenseT> op(Adapter::constTree(sparse), dense);
454  op.copy(serial);
455 }
456 
457 
458 ////////////////////////////////////////
459 
460 
461 /// @brief Copy the values from a dense grid into an OpenVDB tree.
462 ///
463 /// @details Values in the dense grid that are within a tolerance of
464 /// the background value are truncated to inactive background voxels or tiles.
465 /// This allows the tree to form a sparse representation of the dense grid.
466 ///
467 /// @note Since this class allocates leaf nodes concurrently it is recommended
468 /// to use a scalable implementation of @c new like the one provided by TBB,
469 /// rather than the mutex-protected standard library @c new.
470 template<typename _TreeT, typename _DenseT = Dense<typename _TreeT::ValueType> >
472 {
473 public:
474  using DenseT = _DenseT;
475  using TreeT = _TreeT;
476  using ValueT = typename TreeT::ValueType;
477  using LeafT = typename TreeT::LeafNodeType;
479 
480  CopyFromDense(const DenseT& dense, TreeT& tree, const ValueT& tolerance)
481  : mDense(&dense),
482  mTree(&tree),
483  mBlocks(nullptr),
484  mTolerance(tolerance),
485  mAccessor(tree.empty() ? nullptr : new AccessorT(tree))
486  {
487  }
489  : mDense(other.mDense),
490  mTree(other.mTree),
491  mBlocks(other.mBlocks),
492  mTolerance(other.mTolerance),
493  mAccessor(other.mAccessor.get() == nullptr ? nullptr : new AccessorT(*mTree))
494  {
495  }
496 
497  /// @brief Copy values from the dense grid to the sparse tree.
498  void copy(bool serial = false)
499  {
500  mBlocks = new std::vector<Block>();
501  const CoordBBox& bbox = mDense->bbox();
502  // Pre-process: Construct a list of blocks aligned with (potential) leaf nodes
503  for (CoordBBox sub=bbox; sub.min()[0] <= bbox.max()[0]; sub.min()[0] = sub.max()[0] + 1) {
504  for (sub.min()[1] = bbox.min()[1]; sub.min()[1] <= bbox.max()[1];
505  sub.min()[1] = sub.max()[1] + 1)
506  {
507  for (sub.min()[2] = bbox.min()[2]; sub.min()[2] <= bbox.max()[2];
508  sub.min()[2] = sub.max()[2] + 1)
509  {
510  sub.max() = Coord::minComponent(bbox.max(),
511  (sub.min()&(~(LeafT::DIM-1u))).offsetBy(LeafT::DIM-1u));
512  mBlocks->push_back(Block(sub));
513  }
514  }
515  }
516 
517  // Multi-threaded process: Convert dense grid into leaf nodes and tiles
518  if (serial) {
519  (*this)(tbb::blocked_range<size_t>(0, mBlocks->size()));
520  } else {
521  tbb::parallel_for(tbb::blocked_range<size_t>(0, mBlocks->size()), *this);
522  }
523 
524  // Post-process: Insert leaf nodes and tiles into the tree, and prune the tiles only!
525  tree::ValueAccessor<TreeT> acc(*mTree);
526  for (size_t m=0, size = mBlocks->size(); m<size; ++m) {
527  Block& block = (*mBlocks)[m];
528  if (block.leaf) {
529  acc.addLeaf(block.leaf);
530  } else if (block.tile.second) {//only background tiles are inactive
531  acc.addTile(1, block.bbox.min(), block.tile.first, true);//leaf tile
532  }
533  }
534  delete mBlocks;
535  mBlocks = nullptr;
536 
537  tools::pruneTiles(*mTree, mTolerance);//multi-threaded
538  }
539 
540  /// @brief Public method called by tbb::parallel_for
541  /// @warning Never call this method directly!
542  void operator()(const tbb::blocked_range<size_t> &r) const
543  {
544  assert(mBlocks);
545  LeafT* leaf = new LeafT();
546 
547  for (size_t m=r.begin(), n=0, end = r.end(); m != end; ++m, ++n) {
548 
549  Block& block = (*mBlocks)[m];
550  const CoordBBox &bbox = block.bbox;
551 
552  if (mAccessor.get() == nullptr) {//i.e. empty target tree
553  leaf->fill(mTree->background(), false);
554  } else {//account for existing leaf nodes in the target tree
555  if (const LeafT* target = mAccessor->probeConstLeaf(bbox.min())) {
556  (*leaf) = (*target);
557  } else {
558  ValueT value = zeroVal<ValueT>();
559  bool state = mAccessor->probeValue(bbox.min(), value);
560  leaf->fill(value, state);
561  }
562  }
563 
564  leaf->copyFromDense(bbox, *mDense, mTree->background(), mTolerance);
565 
566  if (!leaf->isConstant(block.tile.first, block.tile.second, mTolerance)) {
567  leaf->setOrigin(bbox.min() & (~(LeafT::DIM - 1)));
568  block.leaf = leaf;
569  leaf = new LeafT();
570  }
571  }// loop over blocks
572 
573  delete leaf;
574  }
575 
576 private:
577  struct Block {
578  CoordBBox bbox;
579  LeafT* leaf;
580  std::pair<ValueT, bool> tile;
581  Block(const CoordBBox& b) : bbox(b), leaf(nullptr) {}
582  };
583 
584  const DenseT* mDense;
585  TreeT* mTree;
586  std::vector<Block>* mBlocks;
587  ValueT mTolerance;
588  std::unique_ptr<AccessorT> mAccessor;
589 };// CopyFromDense
590 
591 
592 // Convenient wrapper function for the CopyFromDense class
593 template<typename DenseT, typename GridOrTreeT>
594 void
595 copyFromDense(const DenseT& dense, GridOrTreeT& sparse,
596  const typename GridOrTreeT::ValueType& tolerance, bool serial)
597 {
598  using Adapter = TreeAdapter<GridOrTreeT>;
599  using TreeT = typename Adapter::TreeType;
600 
601  CopyFromDense<TreeT, DenseT> op(dense, Adapter::tree(sparse), tolerance);
602  op.copy(serial);
603 }
604 
605 } // namespace tools
606 } // namespace OPENVDB_VERSION_NAME
607 } // namespace openvdb
608 
609 #endif // OPENVDB_TOOLS_DENSE_HAS_BEEN_INCLUDED
610 
611 // Copyright (c) 2012-2018 DreamWorks Animation LLC
612 // All rights reserved. This software is distributed under the
613 // Mozilla Public License 2.0 ( http://www.mozilla.org/MPL/2.0/ )
Vec2< T > minComponent(const Vec2< T > &v1, const Vec2< T > &v2)
Return component-wise minimum of the two vectors.
Definition: Vec2.h:530
CopyFromDense(const CopyFromDense &other)
Definition: Dense.h:488
void operator()(const CoordBBox &bbox) const
Public method called by tbb::parallel_for.
Definition: Dense.h:434
Index64 memUsage() const
Return the memory footprint of this Dense grid in bytes.
Definition: Dense.h:369
SharedPtr< const Dense > ConstPtr
Definition: Dense.h:215
void fill(const ValueT &value)
Fill this grid with a constant value.
Definition: Dense.h:341
void copyToDense(const GridOrTreeT &sparse, DenseT &dense, bool serial=false)
Populate a dense grid with the values of voxels from a sparse grid, where the sparse grid intersects ...
Definition: Dense.h:448
void pruneTiles(TreeT &tree, typename TreeT::ValueType tolerance=zeroVal< typename TreeT::ValueType >(), bool threaded=true, size_t grainSize=1)
Reduce the memory footprint of a tree by replacing with tiles any non-leaf nodes whose values are all...
Definition: Prune.h:371
Coord offsetToLocalCoord(size_t n) const
Return the index coordinate corresponding to the specified linear offset.
Definition: Dense.h:168
GLsizei const GLchar *const * string
Definition: glcorearb.h:813
Dense(const CoordBBox &bbox, ValueT *data)
Construct a dense grid that wraps an external array.
Definition: Dense.h:245
size_t coordToOffset(size_t i, size_t j, size_t k) const
Return the linear offset into this grid's value array given by unsigned coordinates (i...
Definition: Dense.h:115
size_t zStride() const
Return the stride of the array in the y direction ( = dimX*dimY).
Definition: Dense.h:186
size_t coordToOffset(size_t i, size_t j, size_t k) const
Return the linear offset into this grid's value array given by unsigned coordinates (i...
Definition: Dense.h:162
Index64 valueCount() const
Return the number of voxels contained in this grid.
Definition: Dense.h:284
const ValueT * data() const
Return a raw pointer to this grid's value array.
Definition: Dense.h:274
GLdouble GLdouble GLdouble z
Definition: glcorearb.h:847
Coord offsetToLocalCoord(size_t n) const
Return the local coordinate corresponding to the specified linear offset.
Definition: Dense.h:121
GLboolean GLboolean GLboolean GLboolean a
Definition: glcorearb.h:1221
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:189
void copy(bool serial=false) const
Definition: Dense.h:424
GLint y
Definition: glcorearb.h:102
const Coord & origin() const
Return the grid's origin in index coordinates.
Definition: Dense.h:281
void setValue(size_t offset, const ValueT &value)
Set the value of the voxel at the given array offset.
Definition: Dense.h:287
png_uint_32 i
Definition: png.h:2877
const ValueT & getValue(size_t i, size_t j, size_t k) const
Return a const reference to the value of the voxel at unsigned index coordinates (i, j, k).
Definition: Dense.h:305
size_t coordToOffset(const Coord &xyz) const
Return the linear offset into this grid's value array given by the specified signed coordinates...
Definition: Dense.h:354
void setValue(const Coord &xyz, const ValueT &value)
Set the value of the voxel at the given signed coordinates.
Definition: Dense.h:320
GLsizeiptr size
Definition: glcorearb.h:663
This adapter allows code that is templated on a Tree type to accept either a Tree type or a Grid type...
Definition: Grid.h:944
void addLeaf(LeafNodeT *leaf)
Add the specified leaf to this tree, possibly creating a child branch in the process. If the leaf node already exists, replace it.
Dense(const Coord &dim, const Coord &min=Coord(0))
Construct a dense grid with a given origin and dimensions.
Definition: Dense.h:259
std::shared_ptr< T > SharedPtr
Definition: Types.h:139
DenseBase(const CoordBBox &bbox)
Protected constructor so as to prevent direct instantiation.
Definition: Dense.h:143
void addTile(Index level, const Coord &xyz, const ValueType &value, bool state)
Add a tile at the specified tree level that contains voxel (x, y, z), possibly deleting existing node...
static size_t xStride()
Return the stride of the array in the x direction ( = 1).
Definition: Dense.h:178
static MemoryLayout memoryLayout()
Return the memory layout for this grid (see above for definitions).
Definition: Dense.h:266
GLdouble n
Definition: glcorearb.h:2007
Utility routines to output nicely-formatted numeric values.
size_t xStride() const
Return the stride of the array in the x direction ( = dimY*dimZ).
Definition: Dense.h:131
static size_t zStride()
Return the stride of the array in the z direction ( = 1).
Definition: Dense.h:139
Base class for Dense which is defined below.
Definition: Dense.h:100
void operator()(const tbb::blocked_range< size_t > &r) const
Public method called by tbb::parallel_for.
Definition: Dense.h:542
typename TreeT::LeafNodeType LeafT
Definition: Dense.h:477
GLuint GLuint end
Definition: glcorearb.h:474
size_t yStride() const
Return the stride of the array in the y direction ( = dimZ).
Definition: Dense.h:135
GLintptr offset
Definition: glcorearb.h:664
Defined various multi-threaded utility functions for trees.
Dense is a simple dense grid API used by the CopyToDense and CopyFromDense classes defined below...
Definition: Dense.h:209
ValueT & getValue(const Coord &xyz)
Return a non-const reference to the value of the voxel at the given signed coordinates.
Definition: Dense.h:335
CopyFromDense(const DenseT &dense, TreeT &tree, const ValueT &tolerance)
Definition: Dense.h:480
CopyToDense(const TreeT &tree, DenseT &dense)
Definition: Dense.h:421
GLenum target
Definition: glcorearb.h:1666
GLboolean * data
Definition: glcorearb.h:130
GLuint const GLchar * name
Definition: glcorearb.h:785
GLboolean GLboolean GLboolean b
Definition: glcorearb.h:1221
size_t yStride() const
Return the stride of the array in the y direction ( = dimX).
Definition: Dense.h:182
DenseBase(const CoordBBox &bbox)
Protected constructor so as to prevent direct instantiation.
Definition: Dense.h:190
void copyFromDense(const DenseT &dense, GridOrTreeT &sparse, const typename GridOrTreeT::ValueType &tolerance, bool serial=false)
Populate a sparse grid with the values of all of the voxels of a dense grid.
Definition: Dense.h:595
GLsizei const GLfloat * value
Definition: glcorearb.h:823
void copy(bool serial=false)
Copy values from the dense grid to the sparse tree.
Definition: Dense.h:498
ValueT & getValue(size_t i, size_t j, size_t k)
Return a non-const reference to the value of the voxel at unsigned index coordinates (i...
Definition: Dense.h:313
Dense(const CoordBBox &bbox)
Construct a dense grid with a given range of coordinates.
Definition: Dense.h:222
const ValueT & getValue(size_t offset) const
Return a const reference to the value of the voxel at the given array offset.
Definition: Dense.h:290
GLint GLenum GLint x
Definition: glcorearb.h:408
FormattedInt< IntT > formattedInt(IntT n)
Definition: Formats.h:130
OPENVDB_API int printBytes(std::ostream &os, uint64_t bytes, const std::string &head="", const std::string &tail="\n", bool exact=false, int width=8, int precision=3)
Coord offsetToCoord(size_t n) const
Return the global coordinate corresponding to the specified linear offset.
Definition: Dense.h:363
GLboolean r
Definition: glcorearb.h:1221
ValueT & getValue(size_t offset)
Return a non-const reference to the value of the voxel at the given array offset. ...
Definition: Dense.h:293
Copy an OpenVDB tree into an existing dense grid.
Definition: Dense.h:414
const CoordBBox & bbox() const
Return the bounding box of the signed index domain of this grid.
Definition: Dense.h:278
void print(const std::string &name="", std::ostream &os=std::cout) const
Output a human-readable description of this grid to the specified stream.
Definition: Dense.h:376
void setValue(size_t i, size_t j, size_t k, const ValueT &value)
Set the value of the voxel at unsigned index coordinates (i, j, k).
Definition: Dense.h:297
const std::enable_if<!VecTraits< T >::IsVec, T >::type & min(const T &a, const T &b)
Definition: Composite.h:129
Copy the values from a dense grid into an OpenVDB tree.
Definition: Dense.h:471
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h:135
ValueT * data()
Return a raw pointer to this grid's value array.
Definition: Dense.h:270
Dense(const CoordBBox &bbox, const ValueT &value)
Construct a dense grid with a given range of coordinates and initial value.
Definition: Dense.h:230
const ValueT & getValue(const Coord &xyz) const
Return a const reference to the value of the voxel at the given signed coordinates.
Definition: Dense.h:327
#define OPENVDB_THROW(exception, message)
Definition: Exceptions.h:109