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