Go to the documentation of this file.
15 #ifndef OPENVDB_TREE_LEAFMANAGER_HAS_BEEN_INCLUDED
16 #define OPENVDB_TREE_LEAFMANAGER_HAS_BEEN_INCLUDED
20 #include <tbb/blocked_range.h>
21 #include <tbb/parallel_for.h>
22 #include <tbb/parallel_reduce.h>
25 #include <type_traits>
38 static const bool IsConstTree =
false;
41 template<
typename TreeT>
struct TreeTraits<const TreeT> {
42 static const bool IsConstTree =
true;
52 template<
typename ManagerT>
55 using RangeT =
typename ManagerT::RangeType;
56 using LeafT =
typename ManagerT::LeafType;
57 using BufT =
typename ManagerT::BufferType;
60 LeafT** leafs,
BufT* bufs,
size_t bufsPerLeaf)
62 for (
size_t n = r.begin(), m = r.end(), N = bufsPerLeaf; n != m; ++n) {
63 leafs[n]->swap(bufs[n * N + auxBufferIdx]);
83 template<
typename TreeT>
109 assert(this->isValid());
123 return mRange.mLeafManager.getBuffer(mPos, bufferIdx);
126 size_t pos()
const {
return mPos; }
128 bool isValid()
const {
return mPos>=mRange.mBegin && mPos<=mRange.mEnd; }
130 bool test()
const {
return mPos < mRange.mEnd; }
132 operator bool()
const {
return this->test(); }
134 bool empty()
const {
return !this->test(); }
137 return (mPos != other.mPos) || (&mRange != &other.mRange);
150 , mGrainSize(grainSize)
151 , mLeafManager(leafManager)
159 size_t size()
const {
return mEnd - mBegin; }
165 bool empty()
const {
return !(mBegin < mEnd);}
172 , mGrainSize(r.mGrainSize)
173 , mLeafManager(r.mLeafManager)
178 size_t mEnd, mBegin, mGrainSize;
184 size_t middle = r.mBegin + (r.mEnd - r.mBegin) / 2u;
196 , mAuxBuffersPerLeaf(auxBuffersPerLeaf)
198 this->rebuild(serial);
205 size_t auxBuffersPerLeaf=0,
bool serial=
false)
207 , mLeafCount(end-begin)
209 , mAuxBuffersPerLeaf(auxBuffersPerLeaf)
210 , mLeafPtrs(new
LeafType*[mLeafCount])
211 , mLeafs(mLeafPtrs.get())
213 size_t n = mLeafCount;
214 LeafType **target = mLeafs, **source = begin;
215 while (n--) *target++ = *source++;
216 if (auxBuffersPerLeaf) this->initAuxBuffers(serial);
224 , mLeafCount(other.mLeafCount)
225 , mAuxBufferCount(other.mAuxBufferCount)
226 , mAuxBuffersPerLeaf(other.mAuxBuffersPerLeaf)
227 , mLeafs(other.mLeafs)
228 , mAuxBuffers(other.mAuxBuffers)
242 this->initLeafArray(serial);
243 this->initAuxBuffers(serial);
246 void rebuild(
size_t auxBuffersPerLeaf,
bool serial=
false)
249 mAuxBuffersPerLeaf = auxBuffersPerLeaf;
250 this->rebuild(serial);
255 this->rebuild(serial);
260 mAuxBuffersPerLeaf = auxBuffersPerLeaf;
261 this->rebuild(serial);
264 void rebuildAuxBuffers(
size_t auxBuffersPerLeaf,
bool serial=
false)
270 mAuxBuffersPerLeaf = auxBuffersPerLeaf;
271 this->initAuxBuffers(serial);
279 this->removeAuxBuffers();
280 this->initLeafArray(serial);
295 return tbb::parallel_reduce(this->leafRange(),
Index64(0),
297 for (
const auto& leaf: range) { sum += leaf.onVoxelCount(); }
320 LeafType&
leaf(
size_t leafIdx)
const { assert(leafIdx<mLeafCount);
return *mLeafs[leafIdx]; }
334 assert(leafIdx < mLeafCount);
335 assert(bufferIdx == 0 || bufferIdx - 1 < mAuxBuffersPerLeaf);
336 return bufferIdx == 0 ? mLeafs[leafIdx]->buffer()
337 : mAuxBuffers[leafIdx * mAuxBuffersPerLeaf + bufferIdx - 1];
349 return LeafRange(0, mLeafCount, *
this, grainsize);
363 namespace ph = std::placeholders;
364 if (bufferIdx == 0 || bufferIdx > mAuxBuffersPerLeaf || this->isConstTree())
return false;
365 mTask = std::bind(&LeafManager::doSwapLeafBuffer, ph::_1, ph::_2, bufferIdx - 1);
366 this->cook(serial ? 0 : 512);
373 bool swapBuffer(
size_t bufferIdx1,
size_t bufferIdx2,
bool serial =
false)
375 namespace ph = std::placeholders;
376 const size_t b1 =
std::min(bufferIdx1, bufferIdx2);
377 const size_t b2 =
std::max(bufferIdx1, bufferIdx2);
378 if (b1 == b2 || b2 > mAuxBuffersPerLeaf)
return false;
380 if (this->isConstTree())
return false;
381 mTask = std::bind(&LeafManager::doSwapLeafBuffer, ph::_1, ph::_2, b2-1);
383 mTask = std::bind(&LeafManager::doSwapAuxBuffer, ph::_1, ph::_2, b1-1, b2-1);
385 this->cook(serial ? 0 : 512);
399 namespace ph = std::placeholders;
400 if (bufferIdx == 0 || bufferIdx > mAuxBuffersPerLeaf)
return false;
401 mTask = std::bind(&LeafManager::doSyncAuxBuffer, ph::_1, ph::_2, bufferIdx - 1);
402 this->cook(serial ? 0 : 64);
411 namespace ph = std::placeholders;
412 switch (mAuxBuffersPerLeaf) {
413 case 0:
return false;
414 case 1: mTask = std::bind(&LeafManager::doSyncAllBuffers1, ph::_1, ph::_2);
break;
415 case 2: mTask = std::bind(&LeafManager::doSyncAllBuffers2, ph::_1, ph::_2);
break;
416 default: mTask = std::bind(&LeafManager::doSyncAllBuffersN, ph::_1, ph::_2);
break;
418 this->cook(serial ? 0 : 64);
484 template<
typename LeafOp>
485 void foreach(
const LeafOp& op,
bool threaded =
true,
size_t grainSize=1)
487 LeafTransformer<LeafOp> transform(op);
488 transform.run(this->leafRange(grainSize), threaded);
533 template<
typename LeafOp>
534 void reduce(LeafOp& op,
bool threaded =
true,
size_t grainSize=1)
536 LeafReducer<LeafOp> transform(op);
537 transform.run(this->leafRange(grainSize), threaded);
540 template<
typename ArrayT>
541 [[deprecated(
"Use Tree::getNodes()")]]
void getNodes(ArrayT& array)
543 using T =
typename ArrayT::value_type;
544 static_assert(std::is_pointer<T>::value,
"argument to getNodes() must be a pointer array");
545 using LeafT =
typename std::conditional<std::is_const<
546 typename std::remove_pointer<T>::type>::value,
const LeafType,
LeafType>::type;
549 if (std::is_same<T, LeafT*>::value) {
550 array.resize(mLeafCount);
551 for (
size_t i=0; i<mLeafCount; ++i) array[i] = reinterpret_cast<T>(mLeafs[i]);
553 mTree->getNodes(array);
558 template<
typename ArrayT>
559 [[deprecated(
"Use Tree::getNodes()")]]
void getNodes(ArrayT& array)
const
561 using T =
typename ArrayT::value_type;
562 static_assert(std::is_pointer<T>::value,
"argument to getNodes() must be a pointer array");
563 static_assert(std::is_const<
typename std::remove_pointer<T>::type>::value,
564 "argument to getNodes() must be an array of const node pointers");
567 if (std::is_same<T, const LeafType*>::value) {
568 array.resize(mLeafCount);
569 for (
size_t i=0; i<mLeafCount; ++i) array[i] = reinterpret_cast<T>(mLeafs[i]);
571 mTree->getNodes(array);
587 size_t getPrefixSum(
size_t*& offsets,
size_t& size,
size_t grainSize=1)
const
589 if (offsets ==
nullptr || size < mLeafCount) {
591 offsets =
new size_t[mLeafCount];
595 if ( grainSize > 0 ) {
596 PrefixSum tmp(this->leafRange( grainSize ), offsets, prefix);
598 for (
size_t i=0; i<mLeafCount; ++i) {
600 prefix += mLeafs[i]->onVoxelCount();
612 if (mTask) mTask(
const_cast<LeafManager*
>(
this), r);
618 void initLeafArray(
bool serial =
false)
623 using NonConstLeafParentT =
typename NodeChainT::template Get<1>;
626 std::deque<LeafParentT*> leafParents;
627 mTree->getNodes(leafParents);
631 std::vector<Index32> leafCounts;
633 leafCounts.reserve(leafParents.size());
634 for (LeafParentT* leafParent : leafParents) {
635 leafCounts.push_back(leafParent->childCount());
638 leafCounts.resize(leafParents.size());
643 tbb::blocked_range<size_t>(0, leafParents.size(), 64),
644 [&](tbb::blocked_range<size_t>& range)
646 for (size_t i = range.begin(); i < range.end(); i++) {
647 leafCounts[i] = leafParents[i]->childCount();
655 for (
size_t i = 1; i < leafCounts.size(); i++) {
656 leafCounts[i] += leafCounts[i-1];
659 const size_t leafCount = leafCounts.empty() ? 0 : leafCounts.back();
663 if (leafCount != mLeafCount) {
665 mLeafPtrs.reset(
new LeafType*[leafCount]);
666 mLeafs = mLeafPtrs.get();
671 mLeafCount = leafCount;
674 if (mLeafCount == 0)
return;
679 LeafType** leafPtr = mLeafs;
680 for (LeafParentT* leafParent : leafParents) {
681 for (
auto iter = leafParent->beginChildOn(); iter; ++iter) {
682 *leafPtr++ = &iter.getValue();
687 tbb::blocked_range<size_t>(0, leafParents.size()),
688 [&](tbb::blocked_range<size_t>& range)
690 size_t i = range.begin();
691 LeafType** leafPtr = mLeafs;
692 if (i > 0) leafPtr += leafCounts[i-1];
693 for ( ; i < range.end(); i++) {
694 for (auto iter = leafParents[i]->beginChildOn(); iter; ++iter) {
695 *leafPtr++ = &iter.getValue();
703 void initAuxBuffers(
bool serial)
705 const size_t auxBufferCount = mLeafCount * mAuxBuffersPerLeaf;
706 if (auxBufferCount != mAuxBufferCount) {
707 if (auxBufferCount > 0) {
708 mAuxBufferPtrs.reset(
new NonConstBufferType[auxBufferCount]);
709 mAuxBuffers = mAuxBufferPtrs.get();
711 mAuxBufferPtrs.reset();
712 mAuxBuffers =
nullptr;
714 mAuxBufferCount = auxBufferCount;
716 this->syncAllBuffers(serial);
719 void cook(
size_t grainsize)
722 tbb::parallel_for(this->getRange(grainsize), *
this);
724 (*this)(this->getRange());
728 void doSwapLeafBuffer(
const RangeType& r,
size_t auxBufferIdx)
730 LeafManagerImpl<LeafManager>::doSwapLeafBuffer(
731 r, auxBufferIdx, mLeafs, mAuxBuffers, mAuxBuffersPerLeaf);
734 void doSwapAuxBuffer(
const RangeType& r,
size_t auxBufferIdx1,
size_t auxBufferIdx2)
736 for (
size_t N = mAuxBuffersPerLeaf, n = N*r.begin(), m = N*r.end(); n != m; n+=N) {
737 mAuxBuffers[n + auxBufferIdx1].swap(mAuxBuffers[n + auxBufferIdx2]);
741 void doSyncAuxBuffer(
const RangeType& r,
size_t auxBufferIdx)
743 for (
size_t n = r.begin(), m = r.end(), N = mAuxBuffersPerLeaf; n != m; ++n) {
744 mAuxBuffers[n*N + auxBufferIdx] = mLeafs[n]->buffer();
748 void doSyncAllBuffers1(
const RangeType& r)
750 for (
size_t n = r.begin(), m = r.end(); n != m; ++n) {
751 mAuxBuffers[n] = mLeafs[n]->buffer();
755 void doSyncAllBuffers2(
const RangeType& r)
757 for (
size_t n = r.begin(), m = r.end(); n != m; ++n) {
758 const BufferType& leafBuffer = mLeafs[n]->buffer();
759 mAuxBuffers[2*n ] = leafBuffer;
760 mAuxBuffers[2*n+1] = leafBuffer;
764 void doSyncAllBuffersN(
const RangeType& r)
766 for (
size_t n = r.begin(), m = r.end(), N = mAuxBuffersPerLeaf; n != m; ++n) {
767 const BufferType& leafBuffer = mLeafs[n]->buffer();
768 for (
size_t i=n*N, j=i+N; i!=j; ++i) mAuxBuffers[i] = leafBuffer;
774 template<
typename LeafOp>
775 struct LeafTransformer
777 LeafTransformer(
const LeafOp &leafOp) : mLeafOp(leafOp)
780 void run(
const LeafRange &range,
bool threaded)
const
782 threaded ? tbb::parallel_for(range, *
this) : (*this)(range);
784 void operator()(
const LeafRange &range)
const
786 for (
typename LeafRange::Iterator it = range.begin(); it; ++it) mLeafOp(*it, it.pos());
788 const LeafOp mLeafOp;
793 template<
typename LeafOp>
796 LeafReducer(LeafOp &leafOp) : mLeafOp(&leafOp)
799 LeafReducer(
const LeafReducer &other, tbb::split)
800 : mLeafOpPtr(
std::make_unique<LeafOp>(*(other.mLeafOp),
tbb::split()))
801 , mLeafOp(mLeafOpPtr.get())
804 void run(
const LeafRange& range,
bool threaded)
806 threaded ? tbb::parallel_reduce(range, *
this) : (*this)(range);
808 void operator()(
const LeafRange& range)
810 LeafOp &op = *mLeafOp;
811 for (
typename LeafRange::Iterator it = range.begin(); it; ++it) op(*it, it.pos());
813 void join(
const LeafReducer& other) { mLeafOp->join(*(other.mLeafOp)); }
814 std::unique_ptr<LeafOp> mLeafOpPtr;
815 LeafOp *mLeafOp =
nullptr;
821 PrefixSum(
const LeafRange& r,
size_t* offsets,
size_t& prefix)
824 tbb::parallel_for( r, *
this);
825 for (
size_t i=0, leafCount = r.size(); i<leafCount; ++i) {
826 size_t tmp = offsets[i];
831 inline void operator()(
const LeafRange& r)
const {
832 for (
typename LeafRange::Iterator i = r.begin(); i; ++i) {
833 mOffsets[i.pos()] = i->onVoxelCount();
839 using FuncType =
typename std::function<void (LeafManager*,
const RangeType&)>;
842 size_t mLeafCount, mAuxBufferCount, mAuxBuffersPerLeaf;
843 std::unique_ptr<LeafType*[]> mLeafPtrs;
844 LeafType** mLeafs =
nullptr;
845 std::unique_ptr<NonConstBufferType[]> mAuxBufferPtrs;
846 NonConstBufferType* mAuxBuffers =
nullptr;
847 FuncType mTask =
nullptr;
852 template<
typename TreeT>
871 #endif // OPENVDB_TREE_LEAFMANAGER_HAS_BEEN_INCLUDED
RangeType getRange(size_t grainsize=1) const
Return a tbb::blocked_range of leaf array indices.
Definition: LeafManager.h:344
void removeAuxBuffers()
Remove the auxiliary buffers, but don't rebuild the leaf array.
Definition: LeafManager.h:274
Index64 activeLeafVoxelCount() const
Return the number of active voxels in the leaf nodes.
Definition: LeafManager.h:293
The root node of an OpenVDB tree.
bool swapLeafBuffer(size_t bufferIdx, bool serial=false)
Swap each leaf node's buffer with the nth corresponding auxiliary buffer, where n = bufferIdx.
Definition: LeafManager.h:361
typename TreeT::LeafIter LeafIterType
Definition: LeafManager.h:39
size_t grainsize() const
Definition: LeafManager.h:161
void reduce(LeafOp &op, bool threaded=true, size_t grainSize=1)
Threaded method that applies a user-supplied functor to each leaf node in the LeafManager....
Definition: LeafManager.h:534
void getNodes(ArrayT &array)
Definition: LeafManager.h:541
void run(const char *ax, openvdb::GridBase &grid)
Run a full AX pipeline (parse, compile and execute) on a single OpenVDB Grid.
typename TreeType::LeafNodeType NonConstLeafType
Definition: LeafManager.h:90
static void doSwapLeafBuffer(const RangeT &, size_t, LeafT **, BufT *, size_t)
Definition: LeafManager.h:860
Definition: LeafManager.h:105
LeafRange leafRange(size_t grainsize=1) const
Return a TBB-compatible LeafRange.
Definition: LeafManager.h:347
typename CopyConstness< TreeType, NonConstBufferType >::Type BufferType
Definition: LeafManager.h:95
TreeT TreeType
Definition: LeafManager.h:87
typename LeafType::Buffer NonConstBufferType
Definition: LeafManager.h:94
Iterator begin() const
Definition: LeafManager.h:155
LeafType & operator*() const
Return a reference to the leaf node to which this iterator is pointing.
Definition: LeafManager.h:116
TreeType & tree()
Return a reference to the tree associated with this manager.
Definition: LeafManager.h:307
bool syncAllBuffers(bool serial=false)
Sync up all auxiliary buffers with their corresponding leaf node buffers.
Definition: LeafManager.h:409
LeafManager(TreeType &tree, size_t auxBuffersPerLeaf=0, bool serial=false)
Constructor from a tree reference and an auxiliary buffer count.
Definition: LeafManager.h:192
typename leafmgr::TreeTraits< TreeT >::LeafIterType LeafIterType
Definition: LeafManager.h:93
#define OPENVDB_THROW(exception, message)
Definition: openvdb/Exceptions.h:82
BufferType & buffer(size_t bufferIdx)
Return the nth buffer for the leaf node to which this iterator is pointing, where n = bufferIdx and n...
Definition: LeafManager.h:121
size_t auxBufferCount() const
Return the total number of allocated auxiliary buffers.
Definition: LeafManager.h:284
void rebuild(TreeType &tree, size_t auxBuffersPerLeaf, bool serial=false)
Definition: LeafManager.h:257
const LeafManager & leafManager() const
Definition: LeafManager.h:163
Iterator(const Iterator &)=default
LeafType & leaf(size_t leafIdx) const
Return a pointer to the leaf node at index leafIdx in the array.
Definition: LeafManager.h:320
tbb::blocked_range< size_t > RangeType
Definition: LeafManager.h:96
uint64_t Index64
Definition: openvdb/Types.h:31
Iterator & operator=(const Iterator &)=default
bool syncAuxBuffer(size_t bufferIdx, bool serial=false)
Sync up the specified auxiliary buffer with the corresponding leaf node buffer.
Definition: LeafManager.h:397
bool operator!=(const Iterator &other) const
Definition: LeafManager.h:135
LeafRange(size_t begin, size_t end, const LeafManager &leafManager, size_t grainSize=1)
Definition: LeafManager.h:147
typename TreeT::RootNodeType RootNodeType
Definition: LeafManager.h:89
const TreeType & tree() const
Return a const reference to tree associated with this manager.
Definition: LeafManager.h:304
BufferType & getBuffer(size_t leafIdx, size_t bufferIdx) const
Return the leaf or auxiliary buffer for the leaf node at index leafIdx. If bufferIdx is zero,...
Definition: LeafManager.h:332
typename ManagerT::RangeType RangeT
Definition: LeafManager.h:856
bool test() const
Return true if this iterator is not yet exhausted.
Definition: LeafManager.h:130
bool operator==(const Iterator &other) const
Definition: LeafManager.h:139
typename ManagerT::LeafType LeafT
Definition: LeafManager.h:56
void rebuild(TreeType &tree, bool serial=false)
Definition: LeafManager.h:252
Iterator & operator++()
Advance to the next leaf node.
Definition: LeafManager.h:114
void rebuild(bool serial=false)
(Re)initialize by resizing (if necessary) and repopulating the leaf array and by deleting existing au...
Definition: LeafManager.h:240
size_t size() const
Definition: LeafManager.h:159
This class manages a linear array of pointers to a given tree's leaf nodes, as well as optional auxil...
Definition: LeafManager.h:85
typename TreeT::LeafCIter LeafIterType
Definition: LeafManager.h:43
typename TreeT::ValueType ValueType
Definition: LeafManager.h:88
virtual ~LeafManager()
Definition: LeafManager.h:233
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:147
const LeafRange & leafRange() const
Definition: LeafManager.h:140
static void doSwapLeafBuffer(const RangeT &r, size_t auxBufferIdx, LeafT **leafs, BufT *bufs, size_t bufsPerLeaf)
Definition: LeafManager.h:59
Useful traits for Tree types.
Definition: LeafManager.h:37
typename std::remove_const< ToType >::type Type
Definition: openvdb/Types.h:299
LeafType LeafNodeType
Definition: LeafManager.h:92
Iterator end() const
Definition: LeafManager.h:157
void operator()(const RangeType &r) const
Used internally by tbb::parallel_for() - never call it directly!
Definition: LeafManager.h:610
typename ManagerT::BufferType BufT
Definition: LeafManager.h:57
bool empty() const
Definition: LeafManager.h:165
typename ManagerT::BufferType BufT
Definition: LeafManager.h:858
bool isConstTree() const
Return true if the tree associated with this manager is immutable.
Definition: LeafManager.h:316
size_t getPrefixSum(size_t *&offsets, size_t &size, size_t grainSize=1) const
Generate a linear array of prefix sums of offsets into the active voxels in the leafs....
Definition: LeafManager.h:587
LeafManager(const LeafManager &other)
Definition: LeafManager.h:222
Iterator(const LeafRange &range, size_t pos)
Definition: LeafManager.h:107
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h:95
typename CopyConstness< TreeType, NonConstLeafType >::Type LeafType
Definition: LeafManager.h:91
RootNodeType & root()
Return a reference to the root node associated with this manager.
Definition: LeafManager.h:313
void rebuildLeafArray(bool serial=false)
Remove the auxiliary buffers and rebuild the leaf array.
Definition: LeafManager.h:277
LeafType * operator->() const
Return a pointer to the leaf node to which this iterator is pointing.
Definition: LeafManager.h:118
bool empty() const
Return true if this iterator is exhausted.
Definition: LeafManager.h:134
typename SubtreeT::template Append< HeadT > Type
Definition: RootNode.h:993
LeafRange(LeafRange &r, tbb::split)
Definition: LeafManager.h:169
size_t pos() const
Return the index into the leaf array of the current leaf node.
Definition: LeafManager.h:126
Definition: openvdb/Exceptions.h:65
bool isValid() const
Return true if the position of this iterator is in a valid range.
Definition: LeafManager.h:128
size_t auxBuffersPerLeaf() const
Return the number of auxiliary buffers per leaf node.
Definition: LeafManager.h:286
Definition: openvdb/Exceptions.h:13
Definition: LeafManager.h:102
Definition: LeafManager.h:54
void getNodes(ArrayT &array) const
Definition: LeafManager.h:559
typename ManagerT::RangeType RangeT
Definition: LeafManager.h:55
size_t leafCount() const
Return the number of leaf nodes.
Definition: LeafManager.h:289
Index32 Index
Definition: openvdb/Types.h:32
bool swapBuffer(size_t bufferIdx1, size_t bufferIdx2, bool serial=false)
Swap any two buffers for each leaf node.
Definition: LeafManager.h:373
LeafManager(TreeType &tree, LeafType **begin, LeafType **end, size_t auxBuffersPerLeaf=0, bool serial=false)
Construct directly from an existing array of leafnodes.
Definition: LeafManager.h:204
const RootNodeType & root() const
Return a const reference to root node associated with this manager.
Definition: LeafManager.h:310
bool is_divisible() const
Definition: LeafManager.h:167
typename ManagerT::LeafType LeafT
Definition: LeafManager.h:857
Mat3< typename promote< T0, T1 >::type > operator*(const Mat3< T0 > &m0, const Mat3< T1 > &m1)
Multiply m0 by m1 and return the resulting matrix.
Definition: Mat3.h:604