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