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>
58 template<
typename DenseT,
typename Gr
idOrTreeT>
61 const GridOrTreeT& sparse,
72 template<
typename DenseT,
typename Gr
idOrTreeT>
77 const typename GridOrTreeT::ValueType& tolerance,
96 template<
typename ValueT, MemoryLayout Layout>
class DenseBase;
101 template<
typename ValueT>
108 inline size_t coordToOffset(
size_t i,
size_t j,
size_t k)
const {
return i*mX + j*mY + k; }
124 DenseBase(
const CoordBBox& bbox) : mBBox(bbox), mY(bbox.dim()[2]), mX(mY*bbox.dim()[1]) {}
133 template<
typename ValueT>
140 inline size_t coordToOffset(
size_t i,
size_t j,
size_t k)
const {
return i + j*mY + k*mZ; }
156 DenseBase(
const CoordBBox& bbox) : mBBox(bbox), mY(bbox.dim()[0]), mZ(mY*bbox.dim()[1]) {}
174 template<
typename ValueT, MemoryLayout Layout = LayoutZYX>
186 Dense(
const CoordBBox& bbox) : BaseT(bbox) { this->init(); }
194 Dense(
const CoordBBox& bbox,
const ValueT& value) : BaseT(bbox)
209 Dense(
const CoordBBox& bbox, ValueT* data) : BaseT(bbox), mData(data)
211 if (BaseT::mBBox.empty()) {
223 Dense(
const Coord& dim,
const Coord&
min = Coord(0))
224 : BaseT(CoordBBox(
min,
min+dim.offsetBy(-1)))
234 inline ValueT*
data() {
return mData; }
238 inline const ValueT*
data()
const {
return mData; }
242 inline const CoordBBox&
bbox()
const {
return BaseT::mBBox; }
248 inline void setValue(
size_t offset,
const ValueT& value) { mData[offset] = value; }
251 const ValueT&
getValue(
size_t offset)
const {
return mData[offset]; }
255 inline void setValue(
size_t i,
size_t j,
size_t k,
const ValueT& value)
257 mData[BaseT::coordToOffset(i,j,k)] = value;
262 inline const ValueT&
getValue(
size_t i,
size_t j,
size_t k)
const
264 return mData[BaseT::coordToOffset(i,j,k)];
269 inline void setValue(
const Coord& xyz,
const ValueT& value)
271 mData[this->coordToOffset(xyz)] = value;
276 inline const ValueT&
getValue(
const Coord& xyz)
const
278 return mData[this->coordToOffset(xyz)];
282 inline void fill(
const ValueT& value)
284 size_t size = this->valueCount();
286 while(size--) *a++ = value;
297 assert(BaseT::mBBox.isInside(xyz));
298 return BaseT::coordToOffset(
size_t(xyz[0]-BaseT::mBBox.
min()[0]),
299 size_t(xyz[1]-BaseT::mBBox.
min()[1]),
300 size_t(xyz[2]-BaseT::mBBox.
min()[2]));
306 return sizeof(*this) + BaseT::mBBox.volume() *
sizeof(ValueType);
314 if (BaseT::mBBox.empty()) {
317 mArray.reset(
new ValueT[BaseT::mBBox.volume()]);
318 mData = mArray.get();
321 boost::scoped_array<ValueT> mArray;
334 template<
typename _TreeT,
typename _DenseT = Dense<
typename _TreeT::ValueType> >
340 typedef typename TreeT::ValueType
ValueT;
343 : mRoot(&(tree.root())), mDense(&dense) {}
345 void copy(
bool serial =
false)
const
348 mRoot->copyToDense(mDense->bbox(), *mDense);
350 tbb::parallel_for(mDense->bbox(), *
this);
357 mRoot->copyToDense(bbox, *mDense);
361 const typename TreeT::RootNodeType* mRoot;
367 template<
typename DenseT,
typename Gr
idOrTreeT>
369 copyToDense(
const GridOrTreeT& sparse, DenseT& dense,
bool serial)
372 typedef typename Adapter::TreeType TreeT;
391 template<
typename _TreeT,
typename _DenseT = Dense<
typename _TreeT::ValueType> >
397 typedef typename TreeT::ValueType
ValueT;
398 typedef typename TreeT::LeafNodeType
LeafT;
405 mTolerance(tolerance),
406 mAccessor(tree.empty() ? NULL : new AccessorT(tree))
410 : mDense(other.mDense),
412 mBlocks(other.mBlocks),
413 mTolerance(other.mTolerance),
414 mAccessor(other.mAccessor.get() == NULL ? NULL : new AccessorT(*mTree))
421 mBlocks =
new std::vector<Block>();
422 const CoordBBox& bbox = mDense->bbox();
424 for (CoordBBox sub=bbox; sub.min()[0] <= bbox.max()[0]; sub.min()[0] = sub.max()[0] + 1) {
425 for (sub.min()[1] = bbox.min()[1]; sub.min()[1] <= bbox.max()[1];
426 sub.min()[1] = sub.max()[1] + 1)
428 for (sub.min()[2] = bbox.min()[2]; sub.min()[2] <= bbox.max()[2];
429 sub.min()[2] = sub.max()[2] + 1)
432 (sub.min()&(~(LeafT::DIM-1u))).offsetBy(LeafT::DIM-1u));
433 mBlocks->push_back(Block(sub));
440 (*this)(tbb::blocked_range<size_t>(0, mBlocks->size()));
442 tbb::parallel_for(tbb::blocked_range<size_t>(0, mBlocks->size()), *
this);
447 for (
size_t m=0, size = mBlocks->size(); m<size; ++m) {
448 Block& block = (*mBlocks)[m];
451 }
else if (block.tile.second) {
452 acc.
addTile(1, block.bbox.min(), block.tile.first,
true);
467 LeafT* leaf =
new LeafT();
469 for (
size_t m=r.begin(), n=0, end = r.end(); m != end; ++m, ++n) {
471 Block& block = (*mBlocks)[m];
472 const CoordBBox &bbox = block.bbox;
474 if (mAccessor.get() == NULL) {
475 leaf->fill(mTree->background(),
false);
477 if (
const LeafT* target = mAccessor->probeConstLeaf(bbox.min())) {
480 ValueT value = zeroVal<ValueT>();
481 bool state = mAccessor->probeValue(bbox.min(), value);
482 leaf->fill(value, state);
486 leaf->copyFromDense(bbox, *mDense, mTree->background(), mTolerance);
488 if (!leaf->isConstant(block.tile.first, block.tile.second, mTolerance)) {
489 leaf->setOrigin(bbox.min() & (~(LeafT::DIM - 1)));
502 std::pair<ValueT, bool> tile;
503 Block(
const CoordBBox& b) : bbox(b), leaf(NULL) {}
506 const DenseT* mDense;
508 std::vector<Block>* mBlocks;
510 boost::scoped_ptr<AccessorT> mAccessor;
515 template<
typename DenseT,
typename Gr
idOrTreeT>
518 const typename GridOrTreeT::ValueType& tolerance,
bool serial)
521 typedef typename Adapter::TreeType TreeT;
531 #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
Vec2< T > minComponent(const Vec2< T > &v1, const Vec2< T > &v2)
Return component-wise minimum of the two vectors.
Definition: Vec2.h:497
uint64_t Index64
Definition: Types.h:58
#define OPENVDB_THROW(exception, message)
Definition: Exceptions.h:97
Defined various multi-threaded utility functions for trees.
#define OPENVDB_VERSION_NAME
Definition: version.h:43
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:336
OPENVDB_API Hermite min(const Hermite &, const Hermite &)
min and max operations done directly on the compressed data.
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:858
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:71