36 #ifndef OPENVDB_TOOLS_DENSE_HAS_BEEN_INCLUDED 37 #define OPENVDB_TOOLS_DENSE_HAS_BEEN_INCLUDED 45 #include <tbb/parallel_for.h> 62 template<
typename DenseT,
typename Gr
idOrTreeT>
65 const GridOrTreeT& sparse,
76 template<
typename DenseT,
typename Gr
idOrTreeT>
81 const typename GridOrTreeT::ValueType& tolerance,
100 template<
typename ValueT, MemoryLayout Layout>
class DenseBase;
105 template<
typename ValueT>
115 inline size_t coordToOffset(
size_t i,
size_t j,
size_t k)
const {
return i*mX + j*mY + k; }
123 const size_t x = n / mX;
125 const size_t y = n / mY;
152 template<
typename ValueT>
162 inline size_t coordToOffset(
size_t i,
size_t j,
size_t k)
const {
return i + j*mY + k*mZ; }
170 const size_t z = n / mZ;
172 const size_t y = n / mY;
208 template<
typename ValueT, MemoryLayout Layout = LayoutZYX>
247 if (BaseT::mBBox.empty()) {
270 inline ValueT*
data() {
return mData; }
274 inline const ValueT*
data()
const {
return mData; }
287 inline void setValue(
size_t offset,
const ValueT& value) { mData[offset] = value; }
290 const ValueT&
getValue(
size_t offset)
const {
return mData[offset]; }
293 ValueT&
getValue(
size_t offset) {
return mData[offset]; }
297 inline void setValue(
size_t i,
size_t j,
size_t k,
const ValueT& value)
299 mData[BaseT::coordToOffset(i,j,k)] = value;
305 inline const ValueT&
getValue(
size_t i,
size_t j,
size_t k)
const 307 return mData[BaseT::coordToOffset(i,j,k)];
313 inline ValueT&
getValue(
size_t i,
size_t j,
size_t k)
315 return mData[BaseT::coordToOffset(i,j,k)];
322 mData[this->coordToOffset(xyz)] = value;
329 return mData[this->coordToOffset(xyz)];
337 return mData[this->coordToOffset(xyz)];
341 inline void fill(
const ValueT& value)
343 size_t size = this->valueCount();
345 while(size--) *a++ = value;
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]));
365 return this->offsetToLocalCoord(n) + BaseT::mBBox.
min();
371 return sizeof(*this) + BaseT::mBBox.volume() *
sizeof(
ValueType);
376 void print(
const std::string& name =
"", std::ostream& os = std::cout)
const 378 const Coord dim = BaseT::mBBox.dim();
380 if (!name.empty()) os <<
" \"" << name <<
"\"";
382 os <<
" Dimensions of grid : " << dim[0] <<
" x " << dim[1] <<
" x " << dim[2] <<
"\n";
384 os <<
" Bounding box of voxels: " << BaseT::mBBox <<
"\n";
385 os <<
" Memory layout: " << (Layout ==
LayoutZYX ?
"ZYX (" :
"XYZ (dis")
386 <<
"similar to VDB)\n";
393 if (BaseT::mBBox.empty()) {
396 mArray.reset(
new ValueT[BaseT::mBBox.volume()]);
397 mData = mArray.get();
400 std::unique_ptr<ValueT[]> mArray;
413 template<
typename _TreeT,
typename _DenseT = Dense<
typename _TreeT::ValueType> >
419 using ValueT =
typename TreeT::ValueType;
422 : mRoot(&(tree.root())), mDense(&dense) {}
424 void copy(
bool serial =
false)
const 427 mRoot->copyToDense(mDense->bbox(), *mDense);
429 tbb::parallel_for(mDense->bbox(), *
this);
436 mRoot->copyToDense(bbox, *mDense);
440 const typename TreeT::RootNodeType* mRoot;
446 template<
typename DenseT,
typename Gr
idOrTreeT>
448 copyToDense(
const GridOrTreeT& sparse, DenseT& dense,
bool serial)
451 using TreeT =
typename Adapter::TreeType;
470 template<
typename _TreeT,
typename _DenseT = Dense<
typename _TreeT::ValueType> >
476 using ValueT =
typename TreeT::ValueType;
477 using LeafT =
typename TreeT::LeafNodeType;
484 mTolerance(tolerance),
485 mAccessor(tree.empty() ? nullptr : new
AccessorT(tree))
489 : mDense(other.mDense),
491 mBlocks(other.mBlocks),
492 mTolerance(other.mTolerance),
493 mAccessor(other.mAccessor.get() == nullptr ? nullptr : new
AccessorT(*mTree))
500 mBlocks =
new std::vector<Block>();
504 for (sub.min()[1] = bbox.
min()[1]; sub.
min()[1] <= bbox.
max()[1];
505 sub.
min()[1] = sub.
max()[1] + 1)
507 for (sub.min()[2] = bbox.
min()[2]; sub.
min()[2] <= bbox.
max()[2];
508 sub.
min()[2] = sub.
max()[2] + 1)
511 (sub.min()&(~(LeafT::DIM-1u))).offsetBy(LeafT::DIM-1u));
512 mBlocks->push_back(Block(sub));
519 (*this)(tbb::blocked_range<size_t>(0, mBlocks->size()));
521 tbb::parallel_for(tbb::blocked_range<size_t>(0, mBlocks->size()), *
this);
526 for (
size_t m=0, size = mBlocks->size(); m<size; ++m) {
527 Block& block = (*mBlocks)[m];
530 }
else if (block.tile.second) {
531 acc.
addTile(1, block.bbox.min(), block.tile.first,
true);
547 for (
size_t m=r.begin(), n=0, end = r.end(); m != end; ++m, ++n) {
549 Block& block = (*mBlocks)[m];
552 if (mAccessor.get() ==
nullptr) {
553 leaf->fill(mTree->background(),
false);
555 if (
const LeafT* target = mAccessor->probeConstLeaf(bbox.
min())) {
558 ValueT value = zeroVal<ValueT>();
559 bool state = mAccessor->probeValue(bbox.
min(), value);
560 leaf->fill(value, state);
564 leaf->copyFromDense(bbox, *mDense, mTree->background(), mTolerance);
566 if (!leaf->isConstant(block.tile.first, block.tile.second, mTolerance)) {
567 leaf->setOrigin(bbox.
min() & (~(LeafT::DIM - 1)));
580 std::pair<ValueT, bool> tile;
581 Block(
const CoordBBox& b) : bbox(b), leaf(nullptr) {}
584 const DenseT* mDense;
586 std::vector<Block>* mBlocks;
588 std::unique_ptr<AccessorT> mAccessor;
593 template<
typename DenseT,
typename Gr
idOrTreeT>
596 const typename GridOrTreeT::ValueType& tolerance,
bool serial)
599 using TreeT =
typename Adapter::TreeType;
609 #endif // OPENVDB_TOOLS_DENSE_HAS_BEEN_INCLUDED 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
FormattedInt< IntT > formattedInt(IntT n)
Definition: Formats.h:130
uint64_t Index64
Definition: Types.h:60
#define OPENVDB_THROW(exception, message)
Definition: Exceptions.h:109
const Coord & max() const
Definition: Coord.h:338
Signed (x, y, z) 32-bit integer coordinates.
Definition: Coord.h:51
Defined various multi-threaded utility functions for trees.
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h:136
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)
const Coord & min() const
Definition: Coord.h:337
Definition: Exceptions.h:40
Axis-aligned bounding box of signed integer coordinates.
Definition: Coord.h:264
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:375
Int32 ValueType
Definition: Coord.h:59
static Coord min()
Return the smallest possible coordinate.
Definition: Coord.h:70
std::shared_ptr< T > SharedPtr
Definition: Types.h:139
static Coord max()
Return the largest possible coordinate.
Definition: Coord.h:73
Definition: Exceptions.h:92
void addLeaf(LeafNodeT *leaf)
Add the specified leaf to this tree, possibly creating a child branch in the process....
Definition: ValueAccessor.h:367
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:188
void minComponent(const Coord &other)
Perform a component-wise minimum with the other Coord.
Definition: Coord.h:202