36 #ifndef OPENVDB_TOOLS_DENSE_HAS_BEEN_INCLUDED 37 #define OPENVDB_TOOLS_DENSE_HAS_BEEN_INCLUDED 44 #include <tbb/parallel_for.h> 45 #include <boost/scoped_array.hpp> 46 #include <boost/scoped_ptr.hpp> 59 template<
typename DenseT,
typename Gr
idOrTreeT>
62 const GridOrTreeT& sparse,
73 template<
typename DenseT,
typename Gr
idOrTreeT>
78 const typename GridOrTreeT::ValueType& tolerance,
97 template<
typename ValueT, MemoryLayout Layout>
class DenseBase;
102 template<
typename ValueT>
112 inline size_t coordToOffset(
size_t i,
size_t j,
size_t k)
const {
return i*mX + j*mY + k; }
120 const size_t x = n / mX;
122 const size_t y = n / mY;
149 template<
typename ValueT>
159 inline size_t coordToOffset(
size_t i,
size_t j,
size_t k)
const {
return i + j*mY + k*mZ; }
167 const size_t z = n / mZ;
169 const size_t y = n / mY;
205 template<
typename ValueT, MemoryLayout Layout = LayoutZYX>
211 typedef boost::shared_ptr<Dense>
Ptr;
244 if (BaseT::mBBox.empty()) {
267 inline ValueT*
data() {
return mData; }
271 inline const ValueT*
data()
const {
return mData; }
284 inline void setValue(
size_t offset,
const ValueT& value) { mData[offset] = value; }
287 const ValueT&
getValue(
size_t offset)
const {
return mData[offset]; }
290 ValueT&
getValue(
size_t offset) {
return mData[offset]; }
294 inline void setValue(
size_t i,
size_t j,
size_t k,
const ValueT& value)
296 mData[BaseT::coordToOffset(i,j,k)] = value;
301 inline const ValueT&
getValue(
size_t i,
size_t j,
size_t k)
const 303 return mData[BaseT::coordToOffset(i,j,k)];
308 inline ValueT&
getValue(
size_t i,
size_t j,
size_t k)
310 return mData[BaseT::coordToOffset(i,j,k)];
317 mData[this->coordToOffset(xyz)] = value;
324 return mData[this->coordToOffset(xyz)];
331 return mData[this->coordToOffset(xyz)];
335 inline void fill(
const ValueT& value)
337 size_t size = this->valueCount();
339 while(size--) *a++ = value;
350 assert(BaseT::mBBox.isInside(xyz));
351 return BaseT::coordToOffset(
size_t(xyz[0]-BaseT::mBBox.
min()[0]),
352 size_t(xyz[1]-BaseT::mBBox.
min()[1]),
353 size_t(xyz[2]-BaseT::mBBox.
min()[2]));
359 return this->offsetToLocalCoord(n) + BaseT::mBBox.
min();
365 return sizeof(*this) + BaseT::mBBox.volume() *
sizeof(ValueType);
370 void print(
const std::string& name =
"", std::ostream& os = std::cout)
const 372 const Coord dim = BaseT::mBBox.dim();
374 if (!name.empty()) os <<
" \"" << name <<
"\"";
376 os <<
" Dimensions of grid : " << dim[0] <<
" x " << dim[1] <<
" x " << dim[2] <<
"\n";
378 os <<
" Bounding box of voxels: " << BaseT::mBBox <<
"\n";
379 os <<
" Memory layout: " << (Layout == LayoutZYX ?
"ZYX (" :
"XYZ (dis")
380 <<
"similar to VDB)\n";
388 if (BaseT::mBBox.empty()) {
391 mArray.reset(
new ValueT[BaseT::mBBox.volume()]);
392 mData = mArray.get();
395 boost::scoped_array<ValueT> mArray;
408 template<
typename _TreeT,
typename _DenseT = Dense<
typename _TreeT::ValueType> >
414 typedef typename TreeT::ValueType
ValueT;
417 : mRoot(&(tree.root())), mDense(&dense) {}
419 void copy(
bool serial =
false)
const 422 mRoot->copyToDense(mDense->bbox(), *mDense);
424 tbb::parallel_for(mDense->bbox(), *
this);
431 mRoot->copyToDense(bbox, *mDense);
435 const typename TreeT::RootNodeType* mRoot;
441 template<
typename DenseT,
typename Gr
idOrTreeT>
443 copyToDense(
const GridOrTreeT& sparse, DenseT& dense,
bool serial)
446 typedef typename Adapter::TreeType TreeT;
465 template<
typename _TreeT,
typename _DenseT = Dense<
typename _TreeT::ValueType> >
471 typedef typename TreeT::ValueType
ValueT;
472 typedef typename TreeT::LeafNodeType
LeafT;
479 mTolerance(tolerance),
480 mAccessor(tree.empty() ? NULL : new AccessorT(tree))
484 : mDense(other.mDense),
486 mBlocks(other.mBlocks),
487 mTolerance(other.mTolerance),
488 mAccessor(other.mAccessor.get() == NULL ? NULL : new AccessorT(*mTree))
495 mBlocks =
new std::vector<Block>();
499 for (sub.min()[1] = bbox.min()[1]; sub.min()[1] <= bbox.max()[1];
500 sub.min()[1] = sub.max()[1] + 1)
502 for (sub.min()[2] = bbox.min()[2]; sub.min()[2] <= bbox.max()[2];
503 sub.min()[2] = sub.max()[2] + 1)
506 (sub.min()&(~(LeafT::DIM-1u))).offsetBy(LeafT::DIM-1u));
507 mBlocks->push_back(Block(sub));
514 (*this)(tbb::blocked_range<size_t>(0, mBlocks->size()));
516 tbb::parallel_for(tbb::blocked_range<size_t>(0, mBlocks->size()), *
this);
521 for (
size_t m=0, size = mBlocks->size(); m<size; ++m) {
522 Block& block = (*mBlocks)[m];
525 }
else if (block.tile.second) {
526 acc.
addTile(1, block.bbox.min(), block.tile.first,
true);
540 LeafT* leaf =
new LeafT();
542 for (
size_t m=r.begin(), n=0, end = r.end(); m != end; ++m, ++n) {
544 Block& block = (*mBlocks)[m];
547 if (mAccessor.get() == NULL) {
548 leaf->fill(mTree->background(),
false);
550 if (
const LeafT* target = mAccessor->probeConstLeaf(bbox.
min())) {
553 ValueT value = zeroVal<ValueT>();
554 bool state = mAccessor->probeValue(bbox.
min(), value);
555 leaf->fill(value, state);
559 leaf->copyFromDense(bbox, *mDense, mTree->background(), mTolerance);
561 if (!leaf->isConstant(block.tile.first, block.tile.second, mTolerance)) {
562 leaf->setOrigin(bbox.
min() & (~(LeafT::DIM - 1)));
575 std::pair<ValueT, bool> tile;
576 Block(
const CoordBBox& b) : bbox(b), leaf(NULL) {}
579 const DenseT* mDense;
581 std::vector<Block>* mBlocks;
583 boost::scoped_ptr<AccessorT> mAccessor;
588 template<
typename DenseT,
typename Gr
idOrTreeT>
591 const typename GridOrTreeT::ValueType& tolerance,
bool serial)
594 typedef typename Adapter::TreeType TreeT;
604 #endif // OPENVDB_TOOLS_DENSE_HAS_BEEN_INCLUDED
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:374
static Coord max()
Return the largest possible coordinate.
Definition: Coord.h:72
void minComponent(const Coord &other)
Perform a component-wise minimum with the other Coord.
Definition: Coord.h:191
#define OPENVDB_THROW(exception, message)
Definition: Exceptions.h:97
Int32 ValueType
Definition: Coord.h:55
Signed (x, y, z) 32-bit integer coordinates.
Definition: Coord.h:47
FormattedInt< IntT > formattedInt(IntT n)
Definition: Formats.h:130
Defined various multi-threaded utility functions for trees.
uint64_t Index64
Definition: Types.h:57
#define OPENVDB_VERSION_NAME
Definition: version.h:43
This adapter allows code that is templated on a Tree type to accept either a Tree type or a Grid type...
Definition: Grid.h:898
OPENVDB_API int printBytes(std::ostream &os, uint64_t bytes, const std::string &head="", const std::string &tail="\, bool exact=false, int width=8, int precision=3)
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:382
const Coord & min() const
Definition: Coord.h:332
static Coord min()
Return the smallest possible coordinate.
Definition: Coord.h:69
const Coord & max() const
Definition: Coord.h:333
Axis-aligned bounding box of signed integer coordinates.
Definition: Coord.h:259
Definition: Exceptions.h:88
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:71