36 #ifndef OPENVDB_TOOLS_DENSE_HAS_BEEN_INCLUDED
37 #define OPENVDB_TOOLS_DENSE_HAS_BEEN_INCLUDED
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_ptr.hpp>
56 template<
typename DenseT,
typename Gr
idOrTreeT>
59 const GridOrTreeT& sparse,
70 template<
typename DenseT,
typename Gr
idOrTreeT>
75 const typename GridOrTreeT::ValueType& tolerance,
94 template<
typename ValueT, MemoryLayout Layout>
class DenseBase;
99 template<
typename ValueT>
106 inline size_t coordToOffset(
size_t i,
size_t j,
size_t k)
const {
return i*mX + j*mY + k; }
122 DenseBase(
const CoordBBox& bbox) : mBBox(bbox), mY(bbox.dim()[2]), mX(mY*bbox.dim()[1]) {}
131 template<
typename ValueT>
138 inline size_t coordToOffset(
size_t i,
size_t j,
size_t k)
const {
return i + j*mY + k*mZ; }
154 DenseBase(
const CoordBBox& bbox) : mBBox(bbox), mY(bbox.dim()[0]), mZ(mY*bbox.dim()[1]) {}
172 template<
typename ValueT, MemoryLayout Layout = LayoutZYX>
192 Dense(
const CoordBBox& bbox,
const ValueT& value) :
BaseT(bbox)
207 Dense(
const CoordBBox& bbox, ValueT* data) :
BaseT(bbox), mData(data)
209 if (BaseT::mBBox.empty()) {
221 Dense(
const Coord& dim,
const Coord&
min = Coord(0))
232 inline ValueT*
data() {
return mData; }
236 inline const ValueT*
data()
const {
return mData; }
240 inline const CoordBBox&
bbox()
const {
return BaseT::mBBox; }
246 inline void setValue(
size_t offset,
const ValueT& value) { mData[offset] = value; }
249 const ValueT&
getValue(
size_t offset)
const {
return mData[offset]; }
253 inline void setValue(
size_t i,
size_t j,
size_t k,
const ValueT& value)
255 mData[BaseT::coordToOffset(i,j,k)] = value;
260 inline const ValueT&
getValue(
size_t i,
size_t j,
size_t k)
const
262 return mData[BaseT::coordToOffset(i,j,k)];
267 inline void setValue(
const Coord& xyz,
const ValueT& value)
269 mData[this->coordToOffset(xyz)] = value;
274 inline const ValueT&
getValue(
const Coord& xyz)
const
276 return mData[this->coordToOffset(xyz)];
280 inline void fill(
const ValueT& value)
282 size_t size = this->valueCount();
284 while(size--) *a++ = value;
295 assert(BaseT::mBBox.isInside(xyz));
296 return BaseT::coordToOffset(
size_t(xyz[0]-BaseT::mBBox.
min()[0]),
297 size_t(xyz[1]-BaseT::mBBox.
min()[1]),
298 size_t(xyz[2]-BaseT::mBBox.
min()[2]));
304 return sizeof(*this) + BaseT::mBBox.volume() *
sizeof(
ValueType);
312 if (BaseT::mBBox.empty()) {
315 mArray.reset(
new ValueT[BaseT::mBBox.volume()]);
316 mData = mArray.get();
319 boost::shared_array<ValueT> mArray;
332 template<
typename _TreeT,
typename _DenseT = Dense<
typename _TreeT::ValueType> >
338 typedef typename TreeT::ValueType
ValueT;
341 : mRoot(&(tree.root())), mDense(&dense) {}
343 void copy(
bool serial =
false)
const
346 mRoot->copyToDense(mDense->bbox(), *mDense);
348 tbb::parallel_for(mDense->bbox(), *
this);
355 mRoot->copyToDense(bbox, *mDense);
359 const typename TreeT::RootNodeType* mRoot;
365 template<
typename DenseT,
typename Gr
idOrTreeT>
367 copyToDense(
const GridOrTreeT& sparse, DenseT& dense,
bool serial)
370 typedef typename Adapter::TreeType TreeT;
389 template<
typename _TreeT,
typename _DenseT = Dense<
typename _TreeT::ValueType> >
395 typedef typename TreeT::ValueType
ValueT;
396 typedef typename TreeT::LeafNodeType
LeafT;
403 mTolerance(tolerance),
404 mAccessor(tree.empty() ? NULL : new
AccessorT(tree))
408 : mDense(other.mDense),
410 mBlocks(other.mBlocks),
411 mTolerance(other.mTolerance),
412 mAccessor(other.mAccessor.get() == NULL ? NULL : new
AccessorT(*mTree))
419 mBlocks =
new std::vector<Block>();
420 const CoordBBox& bbox = mDense->bbox();
422 for (CoordBBox sub=bbox; sub.min()[0] <= bbox.max()[0]; sub.min()[0] = sub.max()[0] + 1) {
423 for (sub.min()[1] = bbox.min()[1]; sub.min()[1] <= bbox.max()[1];
424 sub.min()[1] = sub.max()[1] + 1)
426 for (sub.min()[2] = bbox.min()[2]; sub.min()[2] <= bbox.max()[2];
427 sub.min()[2] = sub.max()[2] + 1)
430 (sub.min()&(~(LeafT::DIM-1u))).offsetBy(LeafT::DIM-1u));
431 mBlocks->push_back(Block(sub));
438 (*this)(tbb::blocked_range<size_t>(0, mBlocks->size()));
440 tbb::parallel_for(tbb::blocked_range<size_t>(0, mBlocks->size()), *
this);
445 for (
size_t m=0, size = mBlocks->size(); m<size; ++m) {
446 Block& block = (*mBlocks)[m];
449 }
else if (block.tile.second) {
450 acc.
addTile(1, block.bbox.min(), block.tile.first,
true);
456 mTree->root().pruneTiles(mTolerance);
466 for (
size_t m=r.begin(), n=0, end = r.end(); m != end; ++m, ++n) {
468 Block& block = (*mBlocks)[m];
469 const CoordBBox &bbox = block.bbox;
471 if (mAccessor.get() == NULL) {
472 leaf->fill(mTree->background(),
false);
474 if (
const LeafT* target = mAccessor->probeConstLeaf(bbox.min())) {
477 ValueT value = zeroVal<ValueT>();
478 bool state = mAccessor->probeValue(bbox.min(), value);
479 leaf->fill(value, state);
483 leaf->copyFromDense(bbox, *mDense, mTree->background(), mTolerance);
485 if (!leaf->isConstant(block.tile.first, block.tile.second, mTolerance)) {
486 leaf->setOrigin(bbox.min() & (~(LeafT::DIM - 1)));
499 std::pair<ValueT, bool> tile;
500 Block(
const CoordBBox& b) : bbox(b), leaf(NULL) {}
503 const DenseT* mDense;
505 std::vector<Block>* mBlocks;
507 boost::scoped_ptr<AccessorT> mAccessor;
512 template<
typename DenseT,
typename Gr
idOrTreeT>
515 const typename GridOrTreeT::ValueType& tolerance,
bool serial)
518 typedef typename Adapter::TreeType TreeT;
528 #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:328
OPENVDB_API Hermite min(const Hermite &, const Hermite &)
min and max operations done directly on the compressed data.
#define OPENVDB_THROW(exception, message)
Definition: Exceptions.h:97
Definition: Exceptions.h:88
Vec2< T > minComponent(const Vec2< T > &v1, const Vec2< T > &v2)
Return component-wise minimum of the two vectors.
Definition: Vec2.h:482
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
#define OPENVDB_VERSION_NAME
Definition: version.h:45
uint64_t Index64
Definition: Types.h:55
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:67
This adapter allows code that is templated on a Tree type to accept either a Tree type or a Grid type...
Definition: Grid.h:823