4#include "openvdb/tree/TreeIterator.h"
5#include "openvdb/tree/Tree.h"
6#include "openvdb/tree/ValueAccessor.h"
9#if defined(MR_PARSING_FOR_PB11_BINDINGS) || defined(MR_COMPILING_PB11_BINDINGS)
12#include <openvdb/tools/ChangeBackground.h>
13#include <openvdb/tools/Prune.h>
14#include <openvdb/tools/SignedFloodFill.h>
26template <
class TreeT,
typename Transformer>
32 using ValueT =
typename TreeT::ValueType;
36 using LeafRange =
typename openvdb::tree::IteratorRange<LeafIterT>;
37 using TileRange =
typename openvdb::tree::IteratorRange<TileIterT>;
42 RangeProcessor(
const openvdb::math::CoordBBox& b,
const TreeT& inT, TreeT& outT,
const Transformer& xform ) :
43 mIsRoot( true ), mXform( xform ), mBBox( b ),
44 mInTree( inT ), mOutTree( &outT ), mInAcc( mInTree ), mOutAcc( *mOutTree )
47 RangeProcessor(
const openvdb::math::CoordBBox& b,
const TreeT& inTree,
const Transformer& xform ) :
48 mIsRoot( false ), mXform( xform ), mBBox( b ),
49 mInTree( inTree ), mOutTree( new TreeT( inTree.background() ) ),
50 mInAcc( mInTree ), mOutAcc( *mOutTree )
55 if ( !mIsRoot )
delete mOutTree;
61 mXform(
other.mXform ),
63 mInTree(
other.mInTree ),
64 mOutTree( new TreeT( mInTree.background() ) ),
67 mInterrupt(
other.mInterrupt )
78 if ( interrupt() )
break;
81 openvdb::math::CoordBBox bbox = i->getNodeBoundingBox();
83 bbox.intersect( mBBox );
87 for (
auto it = bbox.begin(); it != bbox.end(); ++it )
89 mXform( mInAcc, mOutAcc, *it );
101 if ( interrupt() )
break;
105 if ( !i.isTileValue() )
continue;
106 if ( !i.isValueOn() && openvdb::math::isApproxEqual( *i, mOutTree->background() ) )
continue;
108 openvdb::math::CoordBBox bbox;
109 i.getBoundingBox( bbox );
110 if ( !mBBox.empty() )
113 bbox = openvdb::math::CoordBBox(
114 openvdb::math::Coord::maxComponent( bbox.min(), mBBox.min() ),
115 openvdb::math::Coord::minComponent( bbox.max(), mBBox.max() ) );
119 for (
auto it = bbox.begin(); it != bbox.end(); ++it )
121 mXform( mInAcc, mOutAcc, *it );
130 if ( !interrupt() ) mOutTree->merge( *
other.mOutTree );
134 bool interrupt()
const {
return mInterrupt && mInterrupt(); }
138 openvdb::math::CoordBBox mBBox;
139 const TreeT& mInTree;
147template <
typename TreeT>
153 using ValueT =
typename TreeT::ValueType;
158 if ( inAcc.probeValue( coord, value ) )
159 outAcc.setValue( coord + shift_, value );
161 void setShift(
const openvdb::math::Coord& shift ) { shift_ = shift; }
163 openvdb::math::Coord shift_;
166template <
typename GredT>
169 using TreeT =
typename GredT::TreeType;
170 typename TreeT::Ptr outTreePtr = std::make_shared<TreeT>();
171 TreeT& inTree = grid.tree();
172 const auto gridClass = grid.getGridClass();
173 if (gridClass == openvdb::GRID_LEVEL_SET)
174 openvdb::tools::changeLevelSetBackground( *outTreePtr, inTree.background() );
176 openvdb::math::CoordBBox bbox = grid.evalActiveVoxelBoundingBox();
177 if ( bbox.empty() || bbox.min() == openvdb::math::Coord() )
184 RangeProc proc( bbox, inTree, *outTreePtr, xform );
186 if ( gridClass != openvdb::GRID_LEVEL_SET )
191 typename RangeProc::TileIterT tileIter = inTree.cbeginValueAll();
192 tileIter.setMaxDepth( tileIter.getLeafDepth() - 1 );
193 typename RangeProc::TileRange tileRange( tileIter );
194 tbb::parallel_reduce( tileRange, proc );
197 typename RangeProc::LeafRange leafRange( inTree.cbeginLeaf() );
198 tbb::parallel_reduce( leafRange, proc );
200 if ( gridClass == openvdb::GRID_LEVEL_SET )
202 openvdb::tools::pruneLevelSet( *outTreePtr );
203 openvdb::tools::signedFloodFill( *outTreePtr );
206 grid.setTree( outTreePtr );
225 progressThreadId_ = std::this_thread::get_id();
229 void add(
size_t l,
size_t t )
232 counter_.fetch_add( l );
234 counter_.fetch_add( t );
239 if ( !cb_ || progressThreadId_ != std::this_thread::get_id() )
241 return cb_(
float( counter_.load() ) /
float( size_ ) );
244 std::atomic<size_t> counter_{ 0 };
247 std::thread::id progressThreadId_;
256template <
typename TreeT,
typename Proc>
263 using ValueT =
typename TreeT::ValueType;
267 using LeafRange =
typename openvdb::tree::IteratorRange<LeafIterT>;
268 using TileRange =
typename openvdb::tree::IteratorRange<TileIterT>;
270 using TreeAccessor =
typename openvdb::tree::ValueAccessor<const TreeT>;
273 mProc( proc ), mBBox( b ), mInTree( inT ), mInAcc( mInTree )
279 mBBox(
other.mBBox ),
280 mInTree(
other.mInTree ),
282 mInterrupt(
other.mInterrupt ),
283 mCanceled{
other.mCanceled },
284 mProgress(
other.mProgress )
295 size_t leafCountLast = 0;
298 if ( interrupt() )
break;
299 if ( !( leafCount & 0x400 ) )
301 if ( !setProgress( leafCount - leafCountLast, tileCount ) )
304 leafCountLast = leafCount;
308 openvdb::math::CoordBBox bbox = i->getNodeBoundingBox();
309 if ( !mBBox.empty() )
310 bbox.intersect( mBBox );
314 mProc.action( i, mInAcc, bbox );
318 setProgress( leafCount - leafCountLast, tileCount );
326 size_t tileCountLast = 0;
329 if ( interrupt() )
break;
330 if ( !( tileCount & 0x400 ) )
332 if ( !setProgress( leafCount, tileCount - tileCountLast ) )
335 tileCountLast = tileCount;
340 if ( !i.isTileValue() )
continue;
341 if ( !i.isValueOn() )
continue;
343 openvdb::math::CoordBBox bbox;
344 i.getBoundingBox( bbox );
345 if ( !mBBox.empty() )
346 bbox.intersect( mBBox );
350 mProc.action( i, mInAcc, bbox );
354 setProgress( leafCount, tileCount - tileCountLast );
367 bool interrupt()
const
369 return mCanceled || ( mInterrupt && mInterrupt() );
371 bool setProgress(
size_t l,
size_t t )
375 mProgress->add( l, t );
376 if ( !mProgress->reportProgress() )
381 openvdb::math::CoordBBox mBBox;
382 const TreeT& mInTree;
385 bool mCanceled{
false };
386 std::shared_ptr<RangeProgress> mProgress;
388 size_t leafCount = 0;
389 size_t tileCount = 0;
402template<
typename TreeT>
406 using ValueT =
typename TreeT::ValueType;
436template <
typename Gr
idT>
439 using TreeT =
typename GridT::TreeType;
443 RangeProcessC calcCount( grid.evalActiveVoxelBoundingBox(), grid.tree(), proc );
445 typename RangeProcessC::TileIterT tileIter = grid.tree().cbeginValueAll();
446 tileIter.setMaxDepth( tileIter.getLeafDepth() - 1 );
447 typename RangeProcessC::TileRange tileRange( tileIter );
448 tbb::parallel_reduce( tileRange, calcCount );
449 typename RangeProcessC::LeafRange leafRange( grid.tree().cbeginLeaf() );
450 tbb::parallel_reduce( leafRange, calcCount );
452 return calcCount.mProc.size;
functor to calculate tile and leaf valid nodes count
Definition MROpenVDBHelper.h:404
typename TreeT::LeafCIter LeafIterT
Definition MROpenVDBHelper.h:408
typename TreeT::ValueAllCIter TileIterT
Definition MROpenVDBHelper.h:409
openvdb::tree::ValueAccessor< const TreeT > TreeAccessor
Definition MROpenVDBHelper.h:407
void action(const TileIterT &, const TreeAccessor &, const openvdb::math::CoordBBox &)
Definition MROpenVDBHelper.h:422
RangeCounter()
Definition MROpenVDBHelper.h:411
typename TreeT::ValueType ValueT
Definition MROpenVDBHelper.h:406
void action(const LeafIterT &, const TreeAccessor &, const openvdb::math::CoordBBox &)
Definition MROpenVDBHelper.h:417
void join(const RangeCounter &other)
Definition MROpenVDBHelper.h:427
RangeCounter(const RangeCounter &)
Definition MROpenVDBHelper.h:414
RangeSize size
Definition MROpenVDBHelper.h:433
Class to use in tbb::parallel_reduce for tree operations that do not require an output tree.
Definition MROpenVDBHelper.h:258
typename TreeT::ValueType ValueT
Definition MROpenVDBHelper.h:263
std::shared_ptr< RangeProgress > ProgressHolder
Definition MROpenVDBHelper.h:261
Proc mProc
Definition MROpenVDBHelper.h:365
typename TreeT::LeafCIter LeafIterT
Definition MROpenVDBHelper.h:265
void operator()(const LeafRange &rCRef)
Transform each leaf node in the given range.
Definition MROpenVDBHelper.h:291
RangeProcessorSingle(const openvdb::math::CoordBBox &b, const TreeT &inT, const Proc &proc)
Definition MROpenVDBHelper.h:272
std::function< bool(void)> InterruptFunc
Definition MROpenVDBHelper.h:260
void setProgressHolder(ProgressHolder progressHolder)
Definition MROpenVDBHelper.h:288
typename openvdb::tree::IteratorRange< LeafIterT > LeafRange
Definition MROpenVDBHelper.h:267
RangeProcessorSingle(RangeProcessorSingle &other, tbb::split)
Splitting constructor: don't copy the original processor's output tree.
Definition MROpenVDBHelper.h:277
typename openvdb::tree::IteratorRange< TileIterT > TileRange
Definition MROpenVDBHelper.h:268
typename openvdb::tree::ValueAccessor< const TreeT > TreeAccessor
Definition MROpenVDBHelper.h:270
void operator()(const TileRange &rCRef)
Transform each non-background tile in the given range.
Definition MROpenVDBHelper.h:322
typename TreeT::ValueAllCIter TileIterT
Definition MROpenVDBHelper.h:266
void join(RangeProcessorSingle &other)
Merge another processor's output tree into this processor's tree.
Definition MROpenVDBHelper.h:358
void setInterrupt(const InterruptFunc &f)
Definition MROpenVDBHelper.h:287
Class to use in tbb::parallel_reduce for openvdb::tree transformation.
Definition MROpenVDBHelper.h:28
void setInterrupt(const InterruptFunc &f)
Definition MROpenVDBHelper.h:70
typename TreeT::LeafCIter LeafIterT
Definition MROpenVDBHelper.h:34
typename TreeT::ValueType ValueT
Definition MROpenVDBHelper.h:32
void operator()(const TileRange &rCRef)
Transform each non-background tile in the given range.
Definition MROpenVDBHelper.h:96
RangeProcessor(RangeProcessor &other, tbb::split)
Splitting constructor: don't copy the original processor's output tree.
Definition MROpenVDBHelper.h:59
typename openvdb::tree::ValueAccessor< const TreeT > InTreeAccessor
Definition MROpenVDBHelper.h:39
void join(RangeProcessor &other)
Merge another processor's output tree into this processor's tree.
Definition MROpenVDBHelper.h:128
typename openvdb::tree::IteratorRange< LeafIterT > LeafRange
Definition MROpenVDBHelper.h:36
typename TreeT::ValueAllCIter TileIterT
Definition MROpenVDBHelper.h:35
typename openvdb::tree::ValueAccessor< TreeT > OutTreeAccessor
Definition MROpenVDBHelper.h:40
~RangeProcessor()
Definition MROpenVDBHelper.h:53
std::function< bool(void)> InterruptFunc
Definition MROpenVDBHelper.h:30
typename openvdb::tree::IteratorRange< TileIterT > TileRange
Definition MROpenVDBHelper.h:37
RangeProcessor(const openvdb::math::CoordBBox &b, const TreeT &inTree, const Transformer &xform)
Definition MROpenVDBHelper.h:47
void operator()(const LeafRange &rCRef)
Transform each leaf node in the given range.
Definition MROpenVDBHelper.h:73
RangeProcessor(const openvdb::math::CoordBBox &b, const TreeT &inT, TreeT &outT, const Transformer &xform)
Definition MROpenVDBHelper.h:42
Definition MROpenVDBHelper.h:212
bool reportProgress() const
Definition MROpenVDBHelper.h:237
void add(size_t l, size_t t)
Definition MROpenVDBHelper.h:229
Mode
Definition MROpenVDBHelper.h:216
RangeProgress(ProgressCallback cb, size_t size, Mode mode)
Definition MROpenVDBHelper.h:220
std::function< bool(float)> ProgressCallback
Definition MRMesh/MRMeshFwd.h:576
Definition MRCameraOrientationPlugin.h:8
ImVec2 size(const ViewportRectangle &rect)
Definition MRViewport.h:32
void translateToZero(GredT &grid)
Definition MROpenVDBHelper.h:167
RangeSize calculateRangeSize(const GridT &grid)
Definition MROpenVDBHelper.h:437
Definition MROpenVDBHelper.h:394
size_t leaf
Definition MROpenVDBHelper.h:395
size_t tile
Definition MROpenVDBHelper.h:396