9 #ifndef OPENVDB_TOOLS_DENSE_HAS_BEEN_INCLUDED 10 #define OPENVDB_TOOLS_DENSE_HAS_BEEN_INCLUDED 18 #include <tbb/parallel_for.h> 35 template<
typename DenseT,
typename Gr
idOrTreeT>
38 const GridOrTreeT& sparse,
49 template<
typename DenseT,
typename Gr
idOrTreeT>
54 const typename GridOrTreeT::ValueType& tolerance,
73 template<
typename ValueT, MemoryLayout Layout>
class DenseBase;
78 template<
typename ValueT>
88 inline size_t coordToOffset(
size_t i,
size_t j,
size_t k)
const {
return i*mX + j*mY + k; }
96 const size_t x = n / mX;
98 const size_t y = n / mY;
99 return Coord(Coord::ValueType(x), Coord::ValueType(y), Coord::ValueType(n - mY*y));
125 template<
typename ValueT>
135 inline size_t coordToOffset(
size_t i,
size_t j,
size_t k)
const {
return i + j*mY + k*mZ; }
143 const size_t z = n / mZ;
145 const size_t y = n / mY;
146 return Coord(Coord::ValueType(n - mY*y), Coord::ValueType(y), Coord::ValueType(z));
181 template<
typename ValueT, MemoryLayout Layout = LayoutZYX>
220 if (BaseT::mBBox.empty()) {
232 Dense(
const Coord& dim,
const Coord&
min = Coord(0))
243 inline ValueT*
data() {
return mData; }
247 inline const ValueT*
data()
const {
return mData; }
254 inline const Coord&
origin()
const {
return BaseT::mBBox.min(); }
263 const ValueT&
getValue(
size_t offset)
const {
return mData[offset]; }
266 ValueT&
getValue(
size_t offset) {
return mData[offset]; }
272 mData[BaseT::coordToOffset(i,j,k)] =
value;
278 inline const ValueT&
getValue(
size_t i,
size_t j,
size_t k)
const 280 return mData[BaseT::coordToOffset(i,j,k)];
286 inline ValueT&
getValue(
size_t i,
size_t j,
size_t k)
288 return mData[BaseT::coordToOffset(i,j,k)];
295 mData[this->coordToOffset(xyz)] =
value;
300 inline const ValueT&
getValue(
const Coord& xyz)
const 302 return mData[this->coordToOffset(xyz)];
310 return mData[this->coordToOffset(xyz)];
316 size_t size = this->valueCount();
318 while(size--) *a++ =
value;
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]));
338 return this->offsetToLocalCoord(n) + BaseT::mBBox.min();
344 return sizeof(*this) + BaseT::mBBox.volume() *
sizeof(
ValueType);
349 void print(
const std::string& name =
"", std::ostream& os = std::cout)
const 351 const Coord dim = BaseT::mBBox.dim();
353 if (!name.empty()) os <<
" \"" << name <<
"\"";
355 os <<
" Dimensions of grid : " << dim[0] <<
" x " << dim[1] <<
" x " << dim[2] <<
"\n";
357 os <<
" Bounding box of voxels: " << BaseT::mBBox <<
"\n";
358 os <<
" Memory layout: " << (Layout == LayoutZYX ?
"ZYX (" :
"XYZ (dis")
359 <<
"similar to VDB)\n";
366 if (BaseT::mBBox.empty()) {
369 mArray.reset(
new ValueT[BaseT::mBBox.volume()]);
370 mData = mArray.get();
373 std::unique_ptr<ValueT[]> mArray;
386 template<
typename _TreeT,
typename _DenseT = Dense<
typename _TreeT::ValueType> >
392 using ValueT =
typename TreeT::ValueType;
395 : mRoot(&(tree.root())), mDense(&dense) {}
397 void copy(
bool serial =
false)
const 400 mRoot->copyToDense(mDense->bbox(), *mDense);
402 tbb::parallel_for(mDense->bbox(), *
this);
409 mRoot->copyToDense(bbox, *mDense);
413 const typename TreeT::RootNodeType* mRoot;
419 template<
typename DenseT,
typename Gr
idOrTreeT>
421 copyToDense(
const GridOrTreeT& sparse, DenseT& dense,
bool serial)
424 using TreeT =
typename Adapter::TreeType;
443 template<
typename _TreeT,
typename _DenseT = Dense<
typename _TreeT::ValueType> >
449 using ValueT =
typename TreeT::ValueType;
450 using LeafT =
typename TreeT::LeafNodeType;
457 mTolerance(tolerance),
458 mAccessor(tree.empty() ? nullptr : new
AccessorT(tree))
462 : mDense(other.mDense),
464 mBlocks(other.mBlocks),
465 mTolerance(other.mTolerance),
466 mAccessor(other.mAccessor.get() == nullptr ? nullptr : new
AccessorT(*mTree))
473 mBlocks =
new std::vector<Block>();
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)
480 for (sub.min()[2] = bbox.min()[2]; sub.min()[2] <= bbox.max()[2];
481 sub.min()[2] = sub.max()[2] + 1)
484 (sub.min()&(~(LeafT::DIM-1u))).offsetBy(LeafT::DIM-1u));
485 mBlocks->push_back(Block(sub));
492 (*this)(tbb::blocked_range<size_t>(0, mBlocks->size()));
494 tbb::parallel_for(tbb::blocked_range<size_t>(0, mBlocks->size()), *
this);
499 for (
size_t m=0, size = mBlocks->size(); m<size; ++m) {
500 Block& block = (*mBlocks)[m];
503 }
else if (block.tile.second) {
504 acc.
addTile(1, block.bbox.min(), block.tile.first,
true);
520 for (
size_t m=r.begin(), n=0, end = r.end(); m != end; ++m, ++n) {
522 Block& block = (*mBlocks)[m];
525 if (mAccessor.get() ==
nullptr) {
526 leaf->fill(mTree->background(),
false);
528 if (
const LeafT* target = mAccessor->probeConstLeaf(bbox.min())) {
532 bool state = mAccessor->probeValue(bbox.min(),
value);
533 leaf->fill(value, state);
537 leaf->copyFromDense(bbox, *mDense, mTree->background(), mTolerance);
539 if (!leaf->isConstant(block.tile.first, block.tile.second, mTolerance)) {
540 leaf->setOrigin(bbox.min() & (~(LeafT::DIM - 1)));
553 std::pair<ValueT, bool> tile;
554 Block(
const CoordBBox& b) : bbox(b), leaf(
nullptr) {}
559 std::vector<Block>* mBlocks;
561 std::unique_ptr<AccessorT> mAccessor;
566 template<
typename DenseT,
typename Gr
idOrTreeT>
569 const typename GridOrTreeT::ValueType& tolerance,
bool serial)
572 using TreeT =
typename Adapter::TreeType;
582 #endif // OPENVDB_TOOLS_DENSE_HAS_BEEN_INCLUDED
#define OPENVDB_THROW(exception, message)
Definition: Exceptions.h:74
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)
std::shared_ptr< T > SharedPtr
Definition: Types.h:114
Definition: Exceptions.h:65
Defined various multi-threaded utility functions for trees.
BBox< Coord > CoordBBox
Definition: NanoVDB.h:1658
uint64_t Index64
Definition: Types.h:53
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:337
Definition: Exceptions.h:13
ValueT value
Definition: GridBuilder.h:1287
FormattedInt< IntT > formattedInt(IntT n)
Definition: Formats.h:118
Vec2< T > minComponent(const Vec2< T > &v1, const Vec2< T > &v2)
Return component-wise minimum of the two vectors.
Definition: Vec2.h:508
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:329
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h.in:116
This adapter allows code that is templated on a Tree type to accept either a Tree type or a Grid type...
Definition: Grid.h:1070
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h.in:202