OpenVDB  3.0.0
Dense.h
Go to the documentation of this file.
1 //
3 // Copyright (c) 2012-2014 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 //
30 //
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>
41 #include <openvdb/tree/ValueAccessor.h>
42 #include <openvdb/Exceptions.h>
43 #include <tbb/parallel_for.h>
44 #include <boost/scoped_array.hpp>
45 #include <boost/scoped_ptr.hpp>
46 #include "Prune.h"
47 
48 namespace openvdb {
50 namespace OPENVDB_VERSION_NAME {
51 namespace tools {
52 
58 template<typename DenseT, typename GridOrTreeT>
59 void
61  const GridOrTreeT& sparse,
62  DenseT& dense,
63  bool serial = false);
64 
65 
72 template<typename DenseT, typename GridOrTreeT>
73 void
75  const DenseT& dense,
76  GridOrTreeT& sparse,
77  const typename GridOrTreeT::ValueType& tolerance,
78  bool serial = false);
79 
80 
82 
92 
96 template<typename ValueT, MemoryLayout Layout> class DenseBase;
97 
101 template<typename ValueT>
102 class DenseBase<ValueT, LayoutZYX>
103 {
104 public:
108  inline size_t coordToOffset(size_t i, size_t j, size_t k) const { return i*mX + j*mY + k; }
109 
112  inline size_t xStride() const { return mX; }
113 
116  inline size_t yStride() const { return mY; }
117 
120  static size_t zStride() { return 1; }
121 
122 protected:
124  DenseBase(const CoordBBox& bbox) : mBBox(bbox), mY(bbox.dim()[2]), mX(mY*bbox.dim()[1]) {}
125 
126  const CoordBBox mBBox;//signed coordinates of the domain represented by the grid
127  const size_t mY, mX;//strides in the y and x direction
128 };// end of DenseBase<ValueT, LayoutZYX>
129 
133 template<typename ValueT>
134 class DenseBase<ValueT, LayoutXYZ>
135 {
136 public:
140  inline size_t coordToOffset(size_t i, size_t j, size_t k) const { return i + j*mY + k*mZ; }
141 
144  static size_t xStride() { return 1; }
145 
148  inline size_t yStride() const { return mY; }
149 
152  inline size_t zStride() const { return mZ; }
153 
154 protected:
156  DenseBase(const CoordBBox& bbox) : mBBox(bbox), mY(bbox.dim()[0]), mZ(mY*bbox.dim()[1]) {}
157 
158  const CoordBBox mBBox;//signed coordinates of the domain represented by the grid
159  const size_t mY, mZ;//strides in the y and z direction
160 };// end of DenseBase<ValueT, LayoutXYZ>
161 
174 template<typename ValueT, MemoryLayout Layout = LayoutZYX>
175 class Dense : public DenseBase<ValueT, Layout>
176 {
177 public:
178  typedef ValueT ValueType;
180 
186  Dense(const CoordBBox& bbox) : BaseT(bbox) { this->init(); }
187 
194  Dense(const CoordBBox& bbox, const ValueT& value) : BaseT(bbox)
195  {
196  this->init();
197  this->fill(value);
198  }
199 
209  Dense(const CoordBBox& bbox, ValueT* data) : BaseT(bbox), mData(data)
210  {
211  if (BaseT::mBBox.empty()) {
212  OPENVDB_THROW(ValueError, "can't construct a dense grid with an empty bounding box");
213  }
214  }
215 
223  Dense(const Coord& dim, const Coord& min = Coord(0))
224  : BaseT(CoordBBox(min, min+dim.offsetBy(-1)))
225  {
226  this->init();
227  }
228 
230  static MemoryLayout memoryLayout() { return Layout; }
231 
234  inline ValueT* data() { return mData; }
235 
238  inline const ValueT* data() const { return mData; }
239 
242  inline const CoordBBox& bbox() const { return BaseT::mBBox; }
243 
245  inline Index64 valueCount() const { return BaseT::mBBox.volume(); }
246 
248  inline void setValue(size_t offset, const ValueT& value) { mData[offset] = value; }
249 
251  const ValueT& getValue(size_t offset) const { return mData[offset]; }
252 
255  inline void setValue(size_t i, size_t j, size_t k, const ValueT& value)
256  {
257  mData[BaseT::coordToOffset(i,j,k)] = value;
258  }
259 
262  inline const ValueT& getValue(size_t i, size_t j, size_t k) const
263  {
264  return mData[BaseT::coordToOffset(i,j,k)];
265  }
266 
269  inline void setValue(const Coord& xyz, const ValueT& value)
270  {
271  mData[this->coordToOffset(xyz)] = value;
272  }
273 
276  inline const ValueT& getValue(const Coord& xyz) const
277  {
278  return mData[this->coordToOffset(xyz)];
279  }
280 
282  inline void fill(const ValueT& value)
283  {
284  size_t size = this->valueCount();
285  ValueT* a = mData;
286  while(size--) *a++ = value;
287  }
288 
295  inline size_t coordToOffset(Coord xyz) const
296  {
297  assert(BaseT::mBBox.isInside(xyz));
298  return BaseT::coordToOffset(size_t(xyz[0]-BaseT::mBBox.min()[0]),
299  size_t(xyz[1]-BaseT::mBBox.min()[1]),
300  size_t(xyz[2]-BaseT::mBBox.min()[2]));
301  }
302 
304  inline Index64 memUsage() const
305  {
306  return sizeof(*this) + BaseT::mBBox.volume() * sizeof(ValueType);
307  }
308 
309 private:
310 
312  void init()
313  {
314  if (BaseT::mBBox.empty()) {
315  OPENVDB_THROW(ValueError, "can't construct a dense grid with an empty bounding box");
316  }
317  mArray.reset(new ValueT[BaseT::mBBox.volume()]);
318  mData = mArray.get();
319  }
320 
321  boost::scoped_array<ValueT> mArray;
322  ValueT* mData;//raw c-style pointer to values
323 };// end of Dense
324 
326 
327 
334 template<typename _TreeT, typename _DenseT = Dense<typename _TreeT::ValueType> >
336 {
337 public:
338  typedef _DenseT DenseT;
339  typedef _TreeT TreeT;
340  typedef typename TreeT::ValueType ValueT;
341 
342  CopyToDense(const TreeT& tree, DenseT& dense)
343  : mRoot(&(tree.root())), mDense(&dense) {}
344 
345  void copy(bool serial = false) const
346  {
347  if (serial) {
348  mRoot->copyToDense(mDense->bbox(), *mDense);
349  } else {
350  tbb::parallel_for(mDense->bbox(), *this);
351  }
352  }
353 
355  void operator()(const CoordBBox& bbox) const
356  {
357  mRoot->copyToDense(bbox, *mDense);
358  }
359 
360 private:
361  const typename TreeT::RootNodeType* mRoot;
362  DenseT* mDense;
363 };// CopyToDense
364 
365 
366 // Convenient wrapper function for the CopyToDense class
367 template<typename DenseT, typename GridOrTreeT>
368 void
369 copyToDense(const GridOrTreeT& sparse, DenseT& dense, bool serial)
370 {
371  typedef TreeAdapter<GridOrTreeT> Adapter;
372  typedef typename Adapter::TreeType TreeT;
373 
374  CopyToDense<TreeT, DenseT> op(Adapter::constTree(sparse), dense);
375  op.copy(serial);
376 }
377 
378 
380 
381 
391 template<typename _TreeT, typename _DenseT = Dense<typename _TreeT::ValueType> >
393 {
394 public:
395  typedef _DenseT DenseT;
396  typedef _TreeT TreeT;
397  typedef typename TreeT::ValueType ValueT;
398  typedef typename TreeT::LeafNodeType LeafT;
400 
401  CopyFromDense(const DenseT& dense, TreeT& tree, const ValueT& tolerance)
402  : mDense(&dense),
403  mTree(&tree),
404  mBlocks(NULL),
405  mTolerance(tolerance),
406  mAccessor(tree.empty() ? NULL : new AccessorT(tree))
407  {
408  }
410  : mDense(other.mDense),
411  mTree(other.mTree),
412  mBlocks(other.mBlocks),
413  mTolerance(other.mTolerance),
414  mAccessor(other.mAccessor.get() == NULL ? NULL : new AccessorT(*mTree))
415  {
416  }
417 
419  void copy(bool serial = false)
420  {
421  mBlocks = new std::vector<Block>();
422  const CoordBBox& bbox = mDense->bbox();
423  // Pre-process: Construct a list of blocks alligned with (potential) leaf nodes
424  for (CoordBBox sub=bbox; sub.min()[0] <= bbox.max()[0]; sub.min()[0] = sub.max()[0] + 1) {
425  for (sub.min()[1] = bbox.min()[1]; sub.min()[1] <= bbox.max()[1];
426  sub.min()[1] = sub.max()[1] + 1)
427  {
428  for (sub.min()[2] = bbox.min()[2]; sub.min()[2] <= bbox.max()[2];
429  sub.min()[2] = sub.max()[2] + 1)
430  {
431  sub.max() = Coord::minComponent(bbox.max(),
432  (sub.min()&(~(LeafT::DIM-1u))).offsetBy(LeafT::DIM-1u));
433  mBlocks->push_back(Block(sub));
434  }
435  }
436  }
437 
438  // Multi-threaded process: Convert dense grid into leaf nodes and tiles
439  if (serial) {
440  (*this)(tbb::blocked_range<size_t>(0, mBlocks->size()));
441  } else {
442  tbb::parallel_for(tbb::blocked_range<size_t>(0, mBlocks->size()), *this);
443  }
444 
445  // Post-process: Insert leaf nodes and tiles into the tree, and prune the tiles only!
446  tree::ValueAccessor<TreeT> acc(*mTree);
447  for (size_t m=0, size = mBlocks->size(); m<size; ++m) {
448  Block& block = (*mBlocks)[m];
449  if (block.leaf) {
450  acc.addLeaf(block.leaf);
451  } else if (block.tile.second) {//only background tiles are inactive
452  acc.addTile(1, block.bbox.min(), block.tile.first, true);//leaf tile
453  }
454  }
455  delete mBlocks;
456  mBlocks = NULL;
457 
458  tools::pruneTiles(*mTree, mTolerance);
459  //mTree->root().pruneTiles(mTolerance);
460  }
461 
464  void operator()(const tbb::blocked_range<size_t> &r) const
465  {
466  assert(mBlocks);
467  LeafT* leaf = new LeafT();
468 
469  for (size_t m=r.begin(), n=0, end = r.end(); m != end; ++m, ++n) {
470 
471  Block& block = (*mBlocks)[m];
472  const CoordBBox &bbox = block.bbox;
473 
474  if (mAccessor.get() == NULL) {//i.e. empty target tree
475  leaf->fill(mTree->background(), false);
476  } else {//account for existing leaf nodes in the target tree
477  if (const LeafT* target = mAccessor->probeConstLeaf(bbox.min())) {
478  (*leaf) = (*target);
479  } else {
480  ValueT value = zeroVal<ValueT>();
481  bool state = mAccessor->probeValue(bbox.min(), value);
482  leaf->fill(value, state);
483  }
484  }
485 
486  leaf->copyFromDense(bbox, *mDense, mTree->background(), mTolerance);
487 
488  if (!leaf->isConstant(block.tile.first, block.tile.second, mTolerance)) {
489  leaf->setOrigin(bbox.min() & (~(LeafT::DIM - 1)));
490  block.leaf = leaf;
491  leaf = new LeafT();
492  }
493  }// loop over blocks
494 
495  delete leaf;
496  }
497 
498 private:
499  struct Block {
500  CoordBBox bbox;
501  LeafT* leaf;
502  std::pair<ValueT, bool> tile;
503  Block(const CoordBBox& b) : bbox(b), leaf(NULL) {}
504  };
505 
506  const DenseT* mDense;
507  TreeT* mTree;
508  std::vector<Block>* mBlocks;
509  ValueT mTolerance;
510  boost::scoped_ptr<AccessorT> mAccessor;
511 };// CopyFromDense
512 
513 
514 // Convenient wrapper function for the CopyFromDense class
515 template<typename DenseT, typename GridOrTreeT>
516 void
517 copyFromDense(const DenseT& dense, GridOrTreeT& sparse,
518  const typename GridOrTreeT::ValueType& tolerance, bool serial)
519 {
520  typedef TreeAdapter<GridOrTreeT> Adapter;
521  typedef typename Adapter::TreeType TreeT;
522 
523  CopyFromDense<TreeT, DenseT> op(dense, Adapter::tree(sparse), tolerance);
524  op.copy(serial);
525 }
526 
527 } // namespace tools
528 } // namespace OPENVDB_VERSION_NAME
529 } // namespace openvdb
530 
531 #endif // OPENVDB_TOOLS_DENSE_HAS_BEEN_INCLUDED
532 
533 // Copyright (c) 2012-2014 DreamWorks Animation LLC
534 // All rights reserved. This software is distributed under the
535 // Mozilla Public License 2.0 ( http://www.mozilla.org/MPL/2.0/ )
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.
Definition: ValueAccessor.h:328
Index64 valueCount() const
Return the number of voxels contained in this grid.
Definition: Dense.h:245
Index64 memUsage() const
Return the memory footprint of this Dense grid in bytes.
Definition: Dense.h:304
_TreeT TreeT
Definition: Dense.h:339
Vec2< T > minComponent(const Vec2< T > &v1, const Vec2< T > &v2)
Return component-wise minimum of the two vectors.
Definition: Vec2.h:497
Definition: Dense.h:91
Copy an OpenVDB tree into an existing dense grid.
Definition: Dense.h:335
uint64_t Index64
Definition: Types.h:58
const CoordBBox mBBox
Definition: Dense.h:126
const CoordBBox mBBox
Definition: Dense.h:158
ValueT ValueType
Definition: Dense.h:178
void copy(bool serial=false) const
Definition: Dense.h:345
const ValueT & getValue(size_t i, size_t j, size_t k) const
Return the value of the voxel at unsigned index coordinates (i, j, k).
Definition: Dense.h:262
MemoryLayout
Definition: Dense.h:91
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:517
#define OPENVDB_THROW(exception, message)
Definition: Exceptions.h:97
const ValueT * data() const
Return a raw pointer to this grid's value array.
Definition: Dense.h:238
CopyFromDense(const DenseT &dense, TreeT &tree, const ValueT &tolerance)
Definition: Dense.h:401
TreeT::ValueType ValueT
Definition: Dense.h:397
_DenseT DenseT
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:369
void copy(bool serial=false)
Copy values from the dense grid to the sparse tree.
Definition: Dense.h:419
DenseBase(const CoordBBox &bbox)
Protected constructor so as to prevent direct instantiation.
Definition: Dense.h:156
CopyToDense(const TreeT &tree, DenseT &dense)
Definition: Dense.h:342
const ValueT & getValue(size_t offset) const
Return the value of the voxel at the given array offset.
Definition: Dense.h:251
static size_t zStride()
Return the stride of the array in the z direction ( = 1).
Definition: Dense.h:120
Defined various multi-threaded utility functions for trees.
size_t yStride() const
Return the stride of the array in the y direction ( = dimZ).
Definition: Dense.h:116
Definition: Dense.h:91
Dense(const CoordBBox &bbox, ValueT *data)
Construct a dense grid that wraps an external array.
Definition: Dense.h:209
Dense(const CoordBBox &bbox)
Construct a dense grid with a given range of coordinates.
Definition: Dense.h:186
TreeT::ValueType ValueT
Definition: Dense.h:340
TreeT::LeafNodeType LeafT
Definition: Dense.h:398
#define OPENVDB_VERSION_NAME
Definition: version.h:43
ValueT * data()
Return a raw pointer to this grid's value array.
Definition: Dense.h:234
DenseBase(const CoordBBox &bbox)
Protected constructor so as to prevent direct instantiation.
Definition: Dense.h:124
Dense is a simple dense grid API used by the CopyToDense and CopyFromDense classes defined below...
Definition: Dense.h:175
void operator()(const CoordBBox &bbox) const
Public method called by tbb::parallel_for.
Definition: Dense.h:355
Dense(const Coord &dim, const Coord &min=Coord(0))
Construct a dense grid with a given origin and dimensions.
Definition: Dense.h:223
_DenseT DenseT
Definition: Dense.h:395
DenseBase< ValueT, Layout > BaseT
Definition: Dense.h:179
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:140
Copy the values from a dense grid into an OpenVDB tree.
Definition: Dense.h:392
Definition: Exceptions.h:39
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...
Definition: ValueAccessor.h:336
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:324
void setValue(const Coord &xyz, const ValueT &value)
Set the value of the voxel at the given signed coordinates.
Definition: Dense.h:269
OPENVDB_API Hermite min(const Hermite &, const Hermite &)
min and max operations done directly on the compressed data.
void fill(const ValueT &value)
Fill this grid with a constant value.
Definition: Dense.h:282
static size_t xStride()
Return the stride of the array in the x direction ( = 1).
Definition: Dense.h:144
Dense(const CoordBBox &bbox, const ValueT &value)
Construct a dense grid with a given range of coordinates and initial value.
Definition: Dense.h:194
const ValueT & getValue(const Coord &xyz) const
Return the value of the voxel at the given signed coordinates.
Definition: Dense.h:276
Base class for Dense which is defined below.
Definition: Dense.h:96
Definition: Exceptions.h:88
_TreeT TreeT
Definition: Dense.h:396
tree::ValueAccessor< TreeT > AccessorT
Definition: Dense.h:399
size_t coordToOffset(Coord xyz) const
Return the linear offset into this grid's value array given by the specified signed coordinates...
Definition: Dense.h:295
const CoordBBox & bbox() const
Return the bounding box of the signed index domain of this grid.
Definition: Dense.h:242
size_t yStride() const
Return the stride of the array in the y direction ( = dimX).
Definition: Dense.h:148
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:255
This adapter allows code that is templated on a Tree type to accept either a Tree type or a Grid type...
Definition: Grid.h:858
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:108
size_t xStride() const
Return the stride of the array in the x direction ( = dimY*dimZ).
Definition: Dense.h:112
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:71
void operator()(const tbb::blocked_range< size_t > &r) const
Public method called by tbb::parallel_for.
Definition: Dense.h:464
size_t zStride() const
Return the stride of the array in the y direction ( = dimX*dimY).
Definition: Dense.h:152
CopyFromDense(const CopyFromDense &other)
Definition: Dense.h:409
static MemoryLayout memoryLayout()
Return the memory layout for this grid (see above for definitions).
Definition: Dense.h:230
void setValue(size_t offset, const ValueT &value)
Set the value of the voxel at the given array offset.
Definition: Dense.h:248