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>
52 template<
typename ValueT>
class Dense;
60 template<
typename DenseT,
typename Gr
idOrTreeT>
63 const GridOrTreeT& sparse,
74 template<
typename DenseT,
typename Gr
idOrTreeT>
79 const typename GridOrTreeT::ValueType& tolerance,
97 template<
typename ValueT>
109 : mBBox(bbox), mY(bbox.dim()[2]), mX(mY*bbox.dim()[1])
120 Dense(
const CoordBBox& bbox,
const ValueT& value)
121 : mBBox(bbox), mY(bbox.dim()[2]), mX(mY*bbox.dim()[1])
136 Dense(
const CoordBBox& bbox, ValueT* data)
137 : mBBox(bbox), mData(data), mY(mBBox.dim()[2]), mX(mY*mBBox.dim()[1])
151 Dense(
const Coord& dim,
const Coord&
min = Coord(0))
152 : mBBox(
min,
min+dim.offsetBy(-1)), mY(mBBox.dim()[2]), mX(mY*mBBox.dim()[1])
159 ValueT*
data() {
return mData; }
163 const ValueT*
data()
const {
return mData; }
167 const CoordBBox&
bbox()
const {
return mBBox; }
181 void setValue(
size_t offset,
const ValueT& value) { mData[offset] = value; }
184 const ValueT&
getValue(
size_t offset)
const {
return mData[offset]; }
188 void setValue(
size_t i,
size_t j,
size_t k,
const ValueT& value)
190 mData[this->coordToOffset(i,j,k)] = value;
195 const ValueT&
getValue(
size_t i,
size_t j,
size_t k)
const
197 return mData[this->coordToOffset(i,j,k)];
202 void setValue(
const Coord& xyz,
const ValueT& value)
204 mData[this->coordToOffset(xyz)] = value;
211 return mData[this->coordToOffset(xyz)];
217 size_t size = this->valueCount();
219 while(size--) *a++ = value;
230 return k + j*mY + i*mX;
241 assert(mBBox.isInside(xyz));
242 return this->coordToOffset(
size_t(xyz[0]-mBBox.min()[0]),
243 size_t(xyz[1]-mBBox.min()[1]),
244 size_t(xyz[2]-mBBox.min()[2]));
255 mArray.reset(
new ValueT[mBBox.volume()]);
256 mData = mArray.get();
259 const CoordBBox mBBox;
260 boost::shared_array<ValueT> mArray;
275 template<
typename _TreeT,
typename _DenseT = Dense<
typename _TreeT::ValueType> >
281 typedef typename TreeT::ValueType
ValueT;
284 : mRoot(&(tree.root())), mDense(&dense) {}
286 void copy(
bool serial =
false)
const
289 mRoot->copyToDense(mDense->bbox(), *mDense);
291 tbb::parallel_for(mDense->bbox(), *
this);
298 mRoot->copyToDense(bbox, *mDense);
302 const typename TreeT::RootNodeType* mRoot;
308 template<
typename DenseT,
typename Gr
idOrTreeT>
310 copyToDense(
const GridOrTreeT& sparse, DenseT& dense,
bool serial)
313 typedef typename Adapter::TreeType TreeT;
332 template<
typename _TreeT,
typename _DenseT = Dense<
typename _TreeT::ValueType> >
338 typedef typename TreeT::ValueType
ValueT;
339 typedef typename TreeT::LeafNodeType
LeafT;
346 mTolerance(tolerance),
347 mAccessor(tree.empty() ? NULL : new
AccessorT(tree))
351 : mDense(other.mDense),
353 mBlocks(other.mBlocks),
354 mTolerance(other.mTolerance),
355 mAccessor(other.mAccessor.get() == NULL ? NULL : new
AccessorT(*mTree))
362 mBlocks =
new std::vector<Block>();
363 const CoordBBox& bbox = mDense->bbox();
365 for (CoordBBox sub=bbox; sub.min()[0] <= bbox.max()[0]; sub.min()[0] = sub.max()[0] + 1) {
366 for (sub.min()[1] = bbox.min()[1]; sub.min()[1] <= bbox.max()[1];
367 sub.min()[1] = sub.max()[1] + 1)
369 for (sub.min()[2] = bbox.min()[2]; sub.min()[2] <= bbox.max()[2];
370 sub.min()[2] = sub.max()[2] + 1)
373 (sub.min()&(~(LeafT::DIM-1u))).offsetBy(LeafT::DIM-1u));
374 mBlocks->push_back(Block(sub));
381 (*this)(tbb::blocked_range<size_t>(0, mBlocks->size()));
383 tbb::parallel_for(tbb::blocked_range<size_t>(0, mBlocks->size()), *
this);
388 for (
size_t m=0, size = mBlocks->size(); m<size; ++m) {
389 Block& block = (*mBlocks)[m];
392 }
else if (block.tile.second) {
393 acc.
addTile(1, block.bbox.min(), block.tile.first,
true);
399 mTree->root().pruneTiles(mTolerance);
409 for (
size_t m=r.begin(), n=0, end = r.end(); m != end; ++m, ++n) {
411 Block& block = (*mBlocks)[m];
412 const CoordBBox &bbox = block.bbox;
414 if (mAccessor.get() == NULL) {
415 leaf->fill(mTree->background(),
false);
417 if (
const LeafT* target = mAccessor->probeConstLeaf(bbox.min())) {
420 ValueT value = zeroVal<ValueT>();
421 bool state = mAccessor->probeValue(bbox.min(), value);
422 leaf->fill(value, state);
426 leaf->copyFromDense(bbox, *mDense, mTree->background(), mTolerance);
428 if (!leaf->isConstant(block.tile.first, block.tile.second, mTolerance)) {
429 leaf->setOrigin(bbox.min() & (~(LeafT::DIM - 1)));
442 std::pair<ValueT, bool> tile;
443 Block(
const CoordBBox& b) : bbox(b), leaf(NULL) {}
446 const DenseT* mDense;
448 std::vector<Block>* mBlocks;
450 boost::scoped_ptr<AccessorT> mAccessor;
455 template<
typename DenseT,
typename Gr
idOrTreeT>
458 const typename GridOrTreeT::ValueType& tolerance,
bool serial)
461 typedef typename Adapter::TreeType TreeT;
471 #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
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
#define OPENVDB_THROW(exception, message)
Definition: Exceptions.h:97
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
OPENVDB_API Hermite min(const Hermite &, const Hermite &)
min and max operations done directly on the compressed data.
Definition: Exceptions.h:88
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:56
uint64_t Index64
Definition: Types.h:55