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_array.hpp>
45 #include <boost/scoped_ptr.hpp>
57 template<
typename DenseT,
typename Gr
idOrTreeT>
60 const GridOrTreeT& sparse,
71 template<
typename DenseT,
typename Gr
idOrTreeT>
76 const typename GridOrTreeT::ValueType& tolerance,
95 template<
typename ValueT, MemoryLayout Layout>
class DenseBase;
100 template<
typename ValueT>
107 inline size_t coordToOffset(
size_t i,
size_t j,
size_t k)
const {
return i*mX + j*mY + k; }
123 DenseBase(
const CoordBBox& bbox) : mBBox(bbox), mY(bbox.dim()[2]), mX(mY*bbox.dim()[1]) {}
132 template<
typename ValueT>
139 inline size_t coordToOffset(
size_t i,
size_t j,
size_t k)
const {
return i + j*mY + k*mZ; }
155 DenseBase(
const CoordBBox& bbox) : mBBox(bbox), mY(bbox.dim()[0]), mZ(mY*bbox.dim()[1]) {}
173 template<
typename ValueT, MemoryLayout Layout = LayoutZYX>
193 Dense(
const CoordBBox& bbox,
const ValueT& value) :
BaseT(bbox)
208 Dense(
const CoordBBox& bbox, ValueT* data) :
BaseT(bbox), mData(data)
210 if (BaseT::mBBox.empty()) {
222 Dense(
const Coord& dim,
const Coord&
min = Coord(0))
233 inline ValueT*
data() {
return mData; }
237 inline const ValueT*
data()
const {
return mData; }
241 inline const CoordBBox&
bbox()
const {
return BaseT::mBBox; }
247 inline void setValue(
size_t offset,
const ValueT& value) { mData[offset] = value; }
250 const ValueT&
getValue(
size_t offset)
const {
return mData[offset]; }
254 inline void setValue(
size_t i,
size_t j,
size_t k,
const ValueT& value)
256 mData[BaseT::coordToOffset(i,j,k)] = value;
261 inline const ValueT&
getValue(
size_t i,
size_t j,
size_t k)
const
263 return mData[BaseT::coordToOffset(i,j,k)];
268 inline void setValue(
const Coord& xyz,
const ValueT& value)
270 mData[this->coordToOffset(xyz)] = value;
275 inline const ValueT&
getValue(
const Coord& xyz)
const
277 return mData[this->coordToOffset(xyz)];
281 inline void fill(
const ValueT& value)
283 size_t size = this->valueCount();
285 while(size--) *a++ = value;
296 assert(BaseT::mBBox.isInside(xyz));
297 return BaseT::coordToOffset(
size_t(xyz[0]-BaseT::mBBox.
min()[0]),
298 size_t(xyz[1]-BaseT::mBBox.
min()[1]),
299 size_t(xyz[2]-BaseT::mBBox.
min()[2]));
305 return sizeof(*this) + BaseT::mBBox.volume() *
sizeof(
ValueType);
313 if (BaseT::mBBox.empty()) {
316 mArray.reset(
new ValueT[BaseT::mBBox.volume()]);
317 mData = mArray.get();
320 boost::scoped_array<ValueT> mArray;
333 template<
typename _TreeT,
typename _DenseT = Dense<
typename _TreeT::ValueType> >
339 typedef typename TreeT::ValueType
ValueT;
342 : mRoot(&(tree.root())), mDense(&dense) {}
344 void copy(
bool serial =
false)
const
347 mRoot->copyToDense(mDense->bbox(), *mDense);
349 tbb::parallel_for(mDense->bbox(), *
this);
356 mRoot->copyToDense(bbox, *mDense);
360 const typename TreeT::RootNodeType* mRoot;
366 template<
typename DenseT,
typename Gr
idOrTreeT>
368 copyToDense(
const GridOrTreeT& sparse, DenseT& dense,
bool serial)
371 typedef typename Adapter::TreeType TreeT;
390 template<
typename _TreeT,
typename _DenseT = Dense<
typename _TreeT::ValueType> >
396 typedef typename TreeT::ValueType
ValueT;
397 typedef typename TreeT::LeafNodeType
LeafT;
404 mTolerance(tolerance),
405 mAccessor(tree.empty() ? NULL : new
AccessorT(tree))
409 : mDense(other.mDense),
411 mBlocks(other.mBlocks),
412 mTolerance(other.mTolerance),
413 mAccessor(other.mAccessor.get() == NULL ? NULL : new
AccessorT(*mTree))
420 mBlocks =
new std::vector<Block>();
421 const CoordBBox& bbox = mDense->bbox();
423 for (CoordBBox sub=bbox; sub.min()[0] <= bbox.max()[0]; sub.min()[0] = sub.max()[0] + 1) {
424 for (sub.min()[1] = bbox.min()[1]; sub.min()[1] <= bbox.max()[1];
425 sub.min()[1] = sub.max()[1] + 1)
427 for (sub.min()[2] = bbox.min()[2]; sub.min()[2] <= bbox.max()[2];
428 sub.min()[2] = sub.max()[2] + 1)
431 (sub.min()&(~(LeafT::DIM-1u))).offsetBy(LeafT::DIM-1u));
432 mBlocks->push_back(Block(sub));
439 (*this)(tbb::blocked_range<size_t>(0, mBlocks->size()));
441 tbb::parallel_for(tbb::blocked_range<size_t>(0, mBlocks->size()), *
this);
446 for (
size_t m=0, size = mBlocks->size(); m<size; ++m) {
447 Block& block = (*mBlocks)[m];
450 }
else if (block.tile.second) {
451 acc.
addTile(1, block.bbox.min(), block.tile.first,
true);
457 mTree->root().pruneTiles(mTolerance);
467 for (
size_t m=r.begin(), n=0, end = r.end(); m != end; ++m, ++n) {
469 Block& block = (*mBlocks)[m];
470 const CoordBBox &bbox = block.bbox;
472 if (mAccessor.get() == NULL) {
473 leaf->fill(mTree->background(),
false);
475 if (
const LeafT* target = mAccessor->probeConstLeaf(bbox.min())) {
478 ValueT value = zeroVal<ValueT>();
479 bool state = mAccessor->probeValue(bbox.min(), value);
480 leaf->fill(value, state);
484 leaf->copyFromDense(bbox, *mDense, mTree->background(), mTolerance);
486 if (!leaf->isConstant(block.tile.first, block.tile.second, mTolerance)) {
487 leaf->setOrigin(bbox.min() & (~(LeafT::DIM - 1)));
500 std::pair<ValueT, bool> tile;
501 Block(
const CoordBBox& b) : bbox(b), leaf(NULL) {}
504 const DenseT* mDense;
506 std::vector<Block>* mBlocks;
508 boost::scoped_ptr<AccessorT> mAccessor;
513 template<
typename DenseT,
typename Gr
idOrTreeT>
516 const typename GridOrTreeT::ValueType& tolerance,
bool serial)
519 typedef typename Adapter::TreeType TreeT;
529 #endif // OPENVDB_TOOLS_DENSE_HAS_BEEN_INCLUDED
OPENVDB_API Hermite min(const Hermite &, const Hermite &)
min and max operations done directly on the compressed data.
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
#define OPENVDB_THROW(exception, message)
Definition: Exceptions.h:97
Definition: Exceptions.h:88
This adapter allows code that is templated on a Tree type to accept either a Tree type or a Grid type...
Definition: Grid.h:837
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
uint64_t Index64
Definition: Types.h:56
#define OPENVDB_VERSION_NAME
Definition: version.h:45
Vec2< T > minComponent(const Vec2< T > &v1, const Vec2< T > &v2)
Return component-wise minimum of the two vectors.
Definition: Vec2.h:497
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:67