OpenVDB  7.0.0
Dense.h
Go to the documentation of this file.
1 // Copyright Contributors to the OpenVDB Project
2 // SPDX-License-Identifier: MPL-2.0
3 
8 
9 #ifndef OPENVDB_TOOLS_DENSE_HAS_BEEN_INCLUDED
10 #define OPENVDB_TOOLS_DENSE_HAS_BEEN_INCLUDED
11 
12 #include <openvdb/Types.h>
13 #include <openvdb/Grid.h>
15 #include <openvdb/Exceptions.h>
16 #include <openvdb/util/Formats.h>
17 #include "Prune.h"
18 #include <tbb/parallel_for.h>
19 #include <iostream>
20 #include <memory>
21 #include <string>
22 #include <utility> // for std::pair
23 #include <vector>
24 
25 namespace openvdb {
27 namespace OPENVDB_VERSION_NAME {
28 namespace tools {
29 
35 template<typename DenseT, typename GridOrTreeT>
36 void
38  const GridOrTreeT& sparse,
39  DenseT& dense,
40  bool serial = false);
41 
42 
49 template<typename DenseT, typename GridOrTreeT>
50 void
52  const DenseT& dense,
53  GridOrTreeT& sparse,
54  const typename GridOrTreeT::ValueType& tolerance,
55  bool serial = false);
56 
57 
59 
69 
73 template<typename ValueT, MemoryLayout Layout> class DenseBase;
74 
78 template<typename ValueT>
79 class DenseBase<ValueT, LayoutZYX>
80 {
81 public:
88  inline size_t coordToOffset(size_t i, size_t j, size_t k) const { return i*mX + j*mY + k; }
89 
94  inline Coord offsetToLocalCoord(size_t n) const
95  {
96  const size_t x = n / mX;
97  n -= mX*x;
98  const size_t y = n / mY;
99  return Coord(Coord::ValueType(x), Coord::ValueType(y), Coord::ValueType(n - mY*y));
100  }
101 
104  inline size_t xStride() const { return mX; }
105 
108  inline size_t yStride() const { return mY; }
109 
112  static size_t zStride() { return 1; }
113 
114 protected:
116  DenseBase(const CoordBBox& bbox) : mBBox(bbox), mY(bbox.dim()[2]), mX(mY*bbox.dim()[1]) {}
117 
118  const CoordBBox mBBox;//signed coordinates of the domain represented by the grid
119  const size_t mY, mX;//strides in the y and x direction
120 };// end of DenseBase<ValueT, LayoutZYX>
121 
125 template<typename ValueT>
126 class DenseBase<ValueT, LayoutXYZ>
127 {
128 public:
135  inline size_t coordToOffset(size_t i, size_t j, size_t k) const { return i + j*mY + k*mZ; }
136 
141  inline Coord offsetToLocalCoord(size_t n) const
142  {
143  const size_t z = n / mZ;
144  n -= mZ*z;
145  const size_t y = n / mY;
146  return Coord(Coord::ValueType(n - mY*y), Coord::ValueType(y), Coord::ValueType(z));
147  }
148 
151  static size_t xStride() { return 1; }
152 
155  inline size_t yStride() const { return mY; }
156 
159  inline size_t zStride() const { return mZ; }
160 
161 protected:
163  DenseBase(const CoordBBox& bbox) : mBBox(bbox), mY(bbox.dim()[0]), mZ(mY*bbox.dim()[1]) {}
164 
165  const CoordBBox mBBox;//signed coordinates of the domain represented by the grid
166  const size_t mY, mZ;//strides in the y and z direction
167 };// end of DenseBase<ValueT, LayoutXYZ>
168 
181 template<typename ValueT, MemoryLayout Layout = LayoutZYX>
182 class Dense : public DenseBase<ValueT, Layout>
183 {
184 public:
185  using ValueType = ValueT;
189 
195  Dense(const CoordBBox& bbox) : BaseT(bbox) { this->init(); }
196 
203  Dense(const CoordBBox& bbox, const ValueT& value) : BaseT(bbox)
204  {
205  this->init();
206  this->fill(value);
207  }
208 
218  Dense(const CoordBBox& bbox, ValueT* data) : BaseT(bbox), mData(data)
219  {
220  if (BaseT::mBBox.empty()) {
221  OPENVDB_THROW(ValueError, "can't construct a dense grid with an empty bounding box");
222  }
223  }
224 
232  Dense(const Coord& dim, const Coord& min = Coord(0))
233  : BaseT(CoordBBox(min, min+dim.offsetBy(-1)))
234  {
235  this->init();
236  }
237 
239  static MemoryLayout memoryLayout() { return Layout; }
240 
243  inline ValueT* data() { return mData; }
244 
247  inline const ValueT* data() const { return mData; }
248 
251  inline const CoordBBox& bbox() const { return BaseT::mBBox; }
252 
254  inline const Coord& origin() const { return BaseT::mBBox.min(); }
255 
257  inline Index64 valueCount() const { return BaseT::mBBox.volume(); }
258 
260  inline void setValue(size_t offset, const ValueT& value) { mData[offset] = value; }
261 
263  const ValueT& getValue(size_t offset) const { return mData[offset]; }
264 
266  ValueT& getValue(size_t offset) { return mData[offset]; }
267 
270  inline void setValue(size_t i, size_t j, size_t k, const ValueT& value)
271  {
272  mData[BaseT::coordToOffset(i,j,k)] = value;
273  }
274 
278  inline const ValueT& getValue(size_t i, size_t j, size_t k) const
279  {
280  return mData[BaseT::coordToOffset(i,j,k)];
281  }
282 
286  inline ValueT& getValue(size_t i, size_t j, size_t k)
287  {
288  return mData[BaseT::coordToOffset(i,j,k)];
289  }
290 
293  inline void setValue(const Coord& xyz, const ValueT& value)
294  {
295  mData[this->coordToOffset(xyz)] = value;
296  }
297 
300  inline const ValueT& getValue(const Coord& xyz) const
301  {
302  return mData[this->coordToOffset(xyz)];
303  }
304 
308  inline ValueT& getValue(const Coord& xyz)
309  {
310  return mData[this->coordToOffset(xyz)];
311  }
312 
314  inline void fill(const ValueT& value)
315  {
316  size_t size = this->valueCount();
317  ValueT* a = mData;
318  while(size--) *a++ = value;
319  }
320 
327  inline size_t coordToOffset(const Coord& xyz) const
328  {
329  assert(BaseT::mBBox.isInside(xyz));
330  return BaseT::coordToOffset(size_t(xyz[0]-BaseT::mBBox.min()[0]),
331  size_t(xyz[1]-BaseT::mBBox.min()[1]),
332  size_t(xyz[2]-BaseT::mBBox.min()[2]));
333  }
334 
336  inline Coord offsetToCoord(size_t n) const
337  {
338  return this->offsetToLocalCoord(n) + BaseT::mBBox.min();
339  }
340 
342  inline Index64 memUsage() const
343  {
344  return sizeof(*this) + BaseT::mBBox.volume() * sizeof(ValueType);
345  }
346 
349  void print(const std::string& name = "", std::ostream& os = std::cout) const
350  {
351  const Coord dim = BaseT::mBBox.dim();
352  os << "Dense Grid";
353  if (!name.empty()) os << " \"" << name << "\"";
354  util::printBytes(os, this->memUsage(), ":\n Memory footprint: ");
355  os << " Dimensions of grid : " << dim[0] << " x " << dim[1] << " x " << dim[2] << "\n";
356  os << " Number of voxels: " << util::formattedInt(this->valueCount()) << "\n";
357  os << " Bounding box of voxels: " << BaseT::mBBox << "\n";
358  os << " Memory layout: " << (Layout == LayoutZYX ? "ZYX (" : "XYZ (dis")
359  << "similar to VDB)\n";
360  }
361 
362 private:
364  void init()
365  {
366  if (BaseT::mBBox.empty()) {
367  OPENVDB_THROW(ValueError, "can't construct a dense grid with an empty bounding box");
368  }
369  mArray.reset(new ValueT[BaseT::mBBox.volume()]);
370  mData = mArray.get();
371  }
372 
373  std::unique_ptr<ValueT[]> mArray;
374  ValueT* mData;//raw c-style pointer to values
375 };// end of Dense
376 
378 
379 
386 template<typename _TreeT, typename _DenseT = Dense<typename _TreeT::ValueType> >
388 {
389 public:
390  using DenseT = _DenseT;
391  using TreeT = _TreeT;
392  using ValueT = typename TreeT::ValueType;
393 
394  CopyToDense(const TreeT& tree, DenseT& dense)
395  : mRoot(&(tree.root())), mDense(&dense) {}
396 
397  void copy(bool serial = false) const
398  {
399  if (serial) {
400  mRoot->copyToDense(mDense->bbox(), *mDense);
401  } else {
402  tbb::parallel_for(mDense->bbox(), *this);
403  }
404  }
405 
407  void operator()(const CoordBBox& bbox) const
408  {
409  mRoot->copyToDense(bbox, *mDense);
410  }
411 
412 private:
413  const typename TreeT::RootNodeType* mRoot;
414  DenseT* mDense;
415 };// CopyToDense
416 
417 
418 // Convenient wrapper function for the CopyToDense class
419 template<typename DenseT, typename GridOrTreeT>
420 void
421 copyToDense(const GridOrTreeT& sparse, DenseT& dense, bool serial)
422 {
423  using Adapter = TreeAdapter<GridOrTreeT>;
424  using TreeT = typename Adapter::TreeType;
425 
426  CopyToDense<TreeT, DenseT> op(Adapter::constTree(sparse), dense);
427  op.copy(serial);
428 }
429 
430 
432 
433 
443 template<typename _TreeT, typename _DenseT = Dense<typename _TreeT::ValueType> >
445 {
446 public:
447  using DenseT = _DenseT;
448  using TreeT = _TreeT;
449  using ValueT = typename TreeT::ValueType;
450  using LeafT = typename TreeT::LeafNodeType;
452 
453  CopyFromDense(const DenseT& dense, TreeT& tree, const ValueT& tolerance)
454  : mDense(&dense),
455  mTree(&tree),
456  mBlocks(nullptr),
457  mTolerance(tolerance),
458  mAccessor(tree.empty() ? nullptr : new AccessorT(tree))
459  {
460  }
462  : mDense(other.mDense),
463  mTree(other.mTree),
464  mBlocks(other.mBlocks),
465  mTolerance(other.mTolerance),
466  mAccessor(other.mAccessor.get() == nullptr ? nullptr : new AccessorT(*mTree))
467  {
468  }
469 
471  void copy(bool serial = false)
472  {
473  mBlocks = new std::vector<Block>();
474  const CoordBBox& bbox = mDense->bbox();
475  // Pre-process: Construct a list of blocks aligned with (potential) leaf nodes
476  for (CoordBBox sub=bbox; sub.min()[0] <= bbox.max()[0]; sub.min()[0] = sub.max()[0] + 1) {
477  for (sub.min()[1] = bbox.min()[1]; sub.min()[1] <= bbox.max()[1];
478  sub.min()[1] = sub.max()[1] + 1)
479  {
480  for (sub.min()[2] = bbox.min()[2]; sub.min()[2] <= bbox.max()[2];
481  sub.min()[2] = sub.max()[2] + 1)
482  {
483  sub.max() = Coord::minComponent(bbox.max(),
484  (sub.min()&(~(LeafT::DIM-1u))).offsetBy(LeafT::DIM-1u));
485  mBlocks->push_back(Block(sub));
486  }
487  }
488  }
489 
490  // Multi-threaded process: Convert dense grid into leaf nodes and tiles
491  if (serial) {
492  (*this)(tbb::blocked_range<size_t>(0, mBlocks->size()));
493  } else {
494  tbb::parallel_for(tbb::blocked_range<size_t>(0, mBlocks->size()), *this);
495  }
496 
497  // Post-process: Insert leaf nodes and tiles into the tree, and prune the tiles only!
498  tree::ValueAccessor<TreeT> acc(*mTree);
499  for (size_t m=0, size = mBlocks->size(); m<size; ++m) {
500  Block& block = (*mBlocks)[m];
501  if (block.leaf) {
502  acc.addLeaf(block.leaf);
503  } else if (block.tile.second) {//only background tiles are inactive
504  acc.addTile(1, block.bbox.min(), block.tile.first, true);//leaf tile
505  }
506  }
507  delete mBlocks;
508  mBlocks = nullptr;
509 
510  tools::pruneTiles(*mTree, mTolerance);//multi-threaded
511  }
512 
515  void operator()(const tbb::blocked_range<size_t> &r) const
516  {
517  assert(mBlocks);
518  LeafT* leaf = new LeafT();
519 
520  for (size_t m=r.begin(), n=0, end = r.end(); m != end; ++m, ++n) {
521 
522  Block& block = (*mBlocks)[m];
523  const CoordBBox &bbox = block.bbox;
524 
525  if (mAccessor.get() == nullptr) {//i.e. empty target tree
526  leaf->fill(mTree->background(), false);
527  } else {//account for existing leaf nodes in the target tree
528  if (const LeafT* target = mAccessor->probeConstLeaf(bbox.min())) {
529  (*leaf) = (*target);
530  } else {
531  ValueT value = zeroVal<ValueT>();
532  bool state = mAccessor->probeValue(bbox.min(), value);
533  leaf->fill(value, state);
534  }
535  }
536 
537  leaf->copyFromDense(bbox, *mDense, mTree->background(), mTolerance);
538 
539  if (!leaf->isConstant(block.tile.first, block.tile.second, mTolerance)) {
540  leaf->setOrigin(bbox.min() & (~(LeafT::DIM - 1)));
541  block.leaf = leaf;
542  leaf = new LeafT();
543  }
544  }// loop over blocks
545 
546  delete leaf;
547  }
548 
549 private:
550  struct Block {
551  CoordBBox bbox;
552  LeafT* leaf;
553  std::pair<ValueT, bool> tile;
554  Block(const CoordBBox& b) : bbox(b), leaf(nullptr) {}
555  };
556 
557  const DenseT* mDense;
558  TreeT* mTree;
559  std::vector<Block>* mBlocks;
560  ValueT mTolerance;
561  std::unique_ptr<AccessorT> mAccessor;
562 };// CopyFromDense
563 
564 
565 // Convenient wrapper function for the CopyFromDense class
566 template<typename DenseT, typename GridOrTreeT>
567 void
568 copyFromDense(const DenseT& dense, GridOrTreeT& sparse,
569  const typename GridOrTreeT::ValueType& tolerance, bool serial)
570 {
571  using Adapter = TreeAdapter<GridOrTreeT>;
572  using TreeT = typename Adapter::TreeType;
573 
574  CopyFromDense<TreeT, DenseT> op(dense, Adapter::tree(sparse), tolerance);
575  op.copy(serial);
576 }
577 
578 } // namespace tools
579 } // namespace OPENVDB_VERSION_NAME
580 } // namespace openvdb
581 
582 #endif // OPENVDB_TOOLS_DENSE_HAS_BEEN_INCLUDED
const CoordBBox & bbox() const
Return the bounding box of the signed index domain of this grid.
Definition: Dense.h:251
Dense(const CoordBBox &bbox, const ValueT &value)
Construct a dense grid with a given range of coordinates and initial value.
Definition: Dense.h:203
Index64 valueCount() const
Return the number of voxels contained in this grid.
Definition: Dense.h:257
Utility routines to output nicely-formatted numeric values.
size_t coordToOffset(size_t i, size_t j, size_t k) const
Return the linear offset into this grid&#39;s value array given by unsigned coordinates (i...
Definition: Dense.h:135
DenseBase(const CoordBBox &bbox)
Protected constructor so as to prevent direct instantiation.
Definition: Dense.h:116
size_t zStride() const
Return the stride of the array in the y direction ( = dimX*dimY).
Definition: Dense.h:159
ValueT ValueType
Definition: Dense.h:185
ValueT * data()
Return a raw pointer to this grid&#39;s value array.
Definition: Dense.h:243
const Coord & origin() const
Return the grid&#39;s origin in index coordinates.
Definition: Dense.h:254
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:568
#define OPENVDB_THROW(exception, message)
Definition: Exceptions.h:82
std::shared_ptr< T > SharedPtr
Definition: Types.h:91
void setValue(size_t offset, const ValueT &value)
Set the value of the voxel at the given array offset.
Definition: Dense.h:260
Dense is a simple dense grid API used by the CopyToDense and CopyFromDense classes defined below...
Definition: Dense.h:182
size_t yStride() const
Return the stride of the array in the y direction ( = dimX).
Definition: Dense.h:155
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:349
CopyToDense(const TreeT &tree, DenseT &dense)
Definition: Dense.h:394
static size_t zStride()
Return the stride of the array in the z direction ( = 1).
Definition: Dense.h:112
SharedPtr< const Dense > ConstPtr
Definition: Dense.h:188
Coord offsetToLocalCoord(size_t n) const
Return the index coordinate corresponding to the specified linear offset.
Definition: Dense.h:141
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:421
Defined various multi-threaded utility functions for trees.
_DenseT DenseT
Definition: Dense.h:447
Definition: Exceptions.h:65
void setValue(const Coord &xyz, const ValueT &value)
Set the value of the voxel at the given signed coordinates.
Definition: Dense.h:293
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:300
typename TreeT::ValueType ValueT
Definition: Dense.h:392
Coord offsetToLocalCoord(size_t n) const
Return the local coordinate corresponding to the specified linear offset.
Definition: Dense.h:94
CopyFromDense(const CopyFromDense &other)
Definition: Dense.h:461
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h:102
MemoryLayout
Definition: Dense.h:68
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:344
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)
_DenseT DenseT
Definition: Dense.h:390
Coord offsetToCoord(size_t n) const
Return the global coordinate corresponding to the specified linear offset.
Definition: Dense.h:336
Copy the values from a dense grid into an OpenVDB tree.
Definition: Dense.h:444
const CoordBBox mBBox
Definition: Dense.h:165
Definition: Dense.h:68
ValueT & getValue(const Coord &xyz)
Return a non-const reference to the value of the voxel at the given signed coordinates.
Definition: Dense.h:308
ValueT & getValue(size_t offset)
Return a non-const reference to the value of the voxel at the given array offset. ...
Definition: Dense.h:266
void operator()(const tbb::blocked_range< size_t > &r) const
Public method called by tbb::parallel_for.
Definition: Dense.h:515
This adapter allows code that is templated on a Tree type to accept either a Tree type or a Grid type...
Definition: Grid.h:1065
Base class for Dense which is defined below.
Definition: Dense.h:73
SharedPtr< Dense > Ptr
Definition: Dense.h:187
static MemoryLayout memoryLayout()
Return the memory layout for this grid (see above for definitions).
Definition: Dense.h:239
Dense(const CoordBBox &bbox)
Construct a dense grid with a given range of coordinates.
Definition: Dense.h:195
Definition: Exceptions.h:13
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:348
typename TreeT::ValueType ValueT
Definition: Dense.h:449
Dense(const CoordBBox &bbox, ValueT *data)
Construct a dense grid that wraps an external array.
Definition: Dense.h:218
const ValueT * data() const
Return a raw pointer to this grid&#39;s value array.
Definition: Dense.h:247
const size_t mZ
Definition: Dense.h:166
void copy(bool serial=false) const
Definition: Dense.h:397
FormattedInt< IntT > formattedInt(IntT n)
Definition: Formats.h:118
const CoordBBox mBBox
Definition: Dense.h:118
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:286
void operator()(const CoordBBox &bbox) const
Public method called by tbb::parallel_for.
Definition: Dense.h:407
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:263
size_t yStride() const
Return the stride of the array in the y direction ( = dimZ).
Definition: Dense.h:108
DenseBase(const CoordBBox &bbox)
Protected constructor so as to prevent direct instantiation.
Definition: Dense.h:163
size_t coordToOffset(size_t i, size_t j, size_t k) const
Return the linear offset into this grid&#39;s value array given by unsigned coordinates (i...
Definition: Dense.h:88
Copy an OpenVDB tree into an existing dense grid.
Definition: Dense.h:387
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:278
void fill(const ValueT &value)
Fill this grid with a constant value.
Definition: Dense.h:314
const std::enable_if<!VecTraits< T >::IsVec, T >::type & min(const T &a, const T &b)
Definition: Composite.h:102
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:270
size_t coordToOffset(const Coord &xyz) const
Return the linear offset into this grid&#39;s value array given by the specified signed coordinates...
Definition: Dense.h:327
uint64_t Index64
Definition: Types.h:30
size_t xStride() const
Return the stride of the array in the x direction ( = dimY*dimZ).
Definition: Dense.h:104
Definition: Dense.h:68
static size_t xStride()
Return the stride of the array in the x direction ( = 1).
Definition: Dense.h:151
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:340
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:154
Index64 memUsage() const
Return the memory footprint of this Dense grid in bytes.
Definition: Dense.h:342
_TreeT TreeT
Definition: Dense.h:391
typename TreeT::LeafNodeType LeafT
Definition: Dense.h:450
Dense(const Coord &dim, const Coord &min=Coord(0))
Construct a dense grid with a given origin and dimensions.
Definition: Dense.h:232
Vec2< T > minComponent(const Vec2< T > &v1, const Vec2< T > &v2)
Return component-wise minimum of the two vectors.
Definition: Vec2.h:503
CopyFromDense(const DenseT &dense, TreeT &tree, const ValueT &tolerance)
Definition: Dense.h:453
_TreeT TreeT
Definition: Dense.h:448
void copy(bool serial=false)
Copy values from the dense grid to the sparse tree.
Definition: Dense.h:471
const size_t mY
Definition: Dense.h:119