9 #ifndef OPENVDB_TOOLS_DENSE_HAS_BEEN_INCLUDED 10 #define OPENVDB_TOOLS_DENSE_HAS_BEEN_INCLUDED 19 #include <tbb/parallel_for.h> 36 template<
typename DenseT,
typename Gr
idOrTreeT>
39 const GridOrTreeT& sparse,
50 template<
typename DenseT,
typename Gr
idOrTreeT>
55 const typename GridOrTreeT::ValueType& tolerance,
74 template<
typename ValueT, MemoryLayout Layout>
class DenseBase;
79 template<
typename ValueT>
89 inline size_t coordToOffset(
size_t i,
size_t j,
size_t k)
const {
return i*mX + j*mY + k; }
97 const size_t x = n / mX;
99 const size_t y = n / mY;
100 return Coord(Coord::ValueType(x), Coord::ValueType(y), Coord::ValueType(n - mY*y));
117 DenseBase(
const CoordBBox& bbox) : mBBox(bbox), mY(bbox.dim()[2]), mX(mY*bbox.dim()[1]) {}
126 template<
typename ValueT>
136 inline size_t coordToOffset(
size_t i,
size_t j,
size_t k)
const {
return i + j*mY + k*mZ; }
144 const size_t z = n / mZ;
146 const size_t y = n / mY;
147 return Coord(Coord::ValueType(n - mY*y), Coord::ValueType(y), Coord::ValueType(z));
164 DenseBase(
const CoordBBox& bbox) : mBBox(bbox), mY(bbox.dim()[0]), mZ(mY*bbox.dim()[1]) {}
182 template<
typename ValueT, MemoryLayout Layout = LayoutZYX>
204 Dense(
const CoordBBox& bbox,
const ValueT& value) :
BaseT(bbox)
219 Dense(
const CoordBBox& bbox, ValueT* data) :
BaseT(bbox), mData(data)
221 if (BaseT::mBBox.
empty()) {
233 Dense(
const Coord& dim,
const Coord&
min = Coord(0))
244 inline ValueT*
data() {
return mData; }
248 inline const ValueT*
data()
const {
return mData; }
252 inline const CoordBBox&
bbox()
const {
return BaseT::mBBox; }
255 inline const Coord&
origin()
const {
return BaseT::mBBox.min(); }
261 inline void setValue(
size_t offset,
const ValueT& value) { mData[offset] = value; }
264 const ValueT&
getValue(
size_t offset)
const {
return mData[offset]; }
267 ValueT&
getValue(
size_t offset) {
return mData[offset]; }
271 inline void setValue(
size_t i,
size_t j,
size_t k,
const ValueT& value)
273 mData[BaseT::coordToOffset(i,j,k)] = value;
279 inline const ValueT&
getValue(
size_t i,
size_t j,
size_t k)
const 281 return mData[BaseT::coordToOffset(i,j,k)];
287 inline ValueT&
getValue(
size_t i,
size_t j,
size_t k)
289 return mData[BaseT::coordToOffset(i,j,k)];
294 inline void setValue(
const Coord& xyz,
const ValueT& value)
296 mData[this->coordToOffset(xyz)] = value;
301 inline const ValueT&
getValue(
const Coord& xyz)
const 303 return mData[this->coordToOffset(xyz)];
311 return mData[this->coordToOffset(xyz)];
315 inline void fill(
const ValueT& value)
317 size_t size = this->valueCount();
319 while(size--) *a++ = value;
331 return BaseT::coordToOffset(
size_t(xyz[0]-BaseT::mBBox.
min()[0]),
332 size_t(xyz[1]-BaseT::mBBox.
min()[1]),
333 size_t(xyz[2]-BaseT::mBBox.
min()[2]));
339 return this->offsetToLocalCoord(n) + BaseT::mBBox.min();
345 return sizeof(*this) + BaseT::mBBox.volume() *
sizeof(
ValueType);
350 void print(
const std::string& name =
"", std::ostream& os = std::cout)
const 352 const Coord dim = BaseT::mBBox.dim();
354 if (!name.empty()) os <<
" \"" << name <<
"\"";
356 os <<
" Dimensions of grid : " << dim[0] <<
" x " << dim[1] <<
" x " << dim[2] <<
"\n";
358 os <<
" Bounding box of voxels: " << BaseT::mBBox <<
"\n";
359 os <<
" Memory layout: " << (Layout == LayoutZYX ?
"ZYX (" :
"XYZ (dis")
360 <<
"similar to VDB)\n";
367 if (BaseT::mBBox.
empty()) {
370 mArray.reset(
new ValueT[BaseT::mBBox.volume()]);
371 mData = mArray.get();
374 std::unique_ptr<ValueT[]> mArray;
387 template<
typename _TreeT,
typename _DenseT = Dense<
typename _TreeT::ValueType> >
393 using ValueT =
typename TreeT::ValueType;
396 : mRoot(&(tree.root())), mDense(&dense) {}
398 void copy(
bool serial =
false)
const 401 mRoot->copyToDense(mDense->bbox(), *mDense);
403 tbb::parallel_for(mDense->bbox(), *
this);
410 mRoot->copyToDense(bbox, *mDense);
414 const typename TreeT::RootNodeType* mRoot;
420 template<
typename DenseT,
typename Gr
idOrTreeT>
422 copyToDense(
const GridOrTreeT& sparse, DenseT& dense,
bool serial)
425 using TreeT =
typename Adapter::TreeType;
444 template<
typename _TreeT,
typename _DenseT = Dense<
typename _TreeT::ValueType> >
450 using ValueT =
typename TreeT::ValueType;
451 using LeafT =
typename TreeT::LeafNodeType;
458 mTolerance(tolerance),
463 : mDense(other.mDense),
465 mBlocks(other.mBlocks),
466 mTolerance(other.mTolerance),
467 mAccessor(other.mAccessor.get() == nullptr ? nullptr : new
AccessorT(*mTree))
474 mBlocks =
new std::vector<Block>();
475 const CoordBBox& bbox = mDense->bbox();
477 for (CoordBBox sub=bbox; sub.min()[0] <= bbox.max()[0]; sub.min()[0] = sub.max()[0] + 1) {
478 for (sub.min()[1] = bbox.min()[1]; sub.min()[1] <= bbox.max()[1];
479 sub.min()[1] = sub.max()[1] + 1)
481 for (sub.min()[2] = bbox.min()[2]; sub.min()[2] <= bbox.max()[2];
482 sub.min()[2] = sub.max()[2] + 1)
485 (sub.min()&(~(LeafT::DIM-1u))).offsetBy(LeafT::DIM-1u));
486 mBlocks->push_back(Block(sub));
493 (*this)(tbb::blocked_range<size_t>(0, mBlocks->size()));
495 tbb::parallel_for(tbb::blocked_range<size_t>(0, mBlocks->size()), *
this);
500 for (
size_t m=0, size = mBlocks->size(); m<size; ++m) {
501 Block& block = (*mBlocks)[m];
504 }
else if (block.tile.second) {
505 acc.
addTile(1, block.bbox.min(), block.tile.first,
true);
521 for (
size_t m=r.begin(), n=0, end = r.end(); m != end; ++m, ++n) {
523 Block& block = (*mBlocks)[m];
524 const CoordBBox &bbox = block.bbox;
526 if (mAccessor.get() ==
nullptr) {
527 leaf->fill(mTree->background(),
false);
529 if (
const LeafT* target = mAccessor->probeConstLeaf(bbox.min())) {
532 ValueT value = zeroVal<ValueT>();
533 bool state = mAccessor->probeValue(bbox.min(), value);
534 leaf->fill(value, state);
538 leaf->copyFromDense(bbox, *mDense, mTree->background(), mTolerance);
540 if (!leaf->isConstant(block.tile.first, block.tile.second, mTolerance)) {
541 leaf->setOrigin(bbox.min() & (~(LeafT::DIM - 1)));
554 std::pair<ValueT, bool> tile;
555 Block(
const CoordBBox& b) : bbox(b), leaf(
nullptr) {}
560 std::vector<Block>* mBlocks;
562 std::unique_ptr<AccessorT> mAccessor;
567 template<
typename DenseT,
typename Gr
idOrTreeT>
570 const typename GridOrTreeT::ValueType& tolerance,
bool serial)
573 using TreeT =
typename Adapter::TreeType;
583 #endif // OPENVDB_TOOLS_DENSE_HAS_BEEN_INCLUDED
The Value Accessor Implementation and API methods. The majoirty of the API matches the API of a compa...
Definition: ValueAccessor.h:68
#define OPENVDB_THROW(exception, message)
Definition: Exceptions.h:74
uint64_t Index64
Definition: Types.h:53
Defined various multi-threaded utility functions for trees.
Definition: Exceptions.h:65
std::shared_ptr< T > SharedPtr
Definition: Types.h:114
#define OPENVDB_ASSERT(X)
Definition: Assert.h:41
This adapter allows code that is templated on a Tree type to accept either a Tree type or a Grid type...
Definition: Grid.h:1059
bool empty(const char *str)
tests if a c-string str is empty, that is its first value is '\0'
Definition: Util.h:144
Definition: Exceptions.h:13
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:729
OPENVDB_API int printBytes(std::ostream &os, uint64_t bytes, const std::string &head="", const std::string &tail="\n", bool exact=false, int width=8, int precision=3)
ValueAccessors are designed to help accelerate accesses into the OpenVDB Tree structures by storing c...
void addTile(Index level, const Coord &xyz, const ValueType &value, bool state)
Add a tile at the specified tree level that contains the coordinate xyz, possibly deleting existing n...
Definition: ValueAccessor.h:754
FormattedInt< IntT > formattedInt(IntT n)
Definition: Formats.h:118
Vec2< T > minComponent(const Vec2< T > &v1, const Vec2< T > &v2)
Return component-wise minimum of the two vectors.
Definition: Vec2.h:504
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h.in:121
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h.in:218