25#ifndef OPENVDB_TOOLS_MULTIRESGRID_HAS_BEEN_INCLUDED
26#define OPENVDB_TOOLS_MULTIRESGRID_HAS_BEEN_INCLUDED
43#include <tbb/blocked_range.h>
44#include <tbb/enumerable_thread_specific.h>
45#include <tbb/parallel_for.h>
58template<
typename TreeType>
120 TreeType& tree(
size_t level);
125 const TreeType& constTree(
size_t level)
const;
130 TreePtr treePtr(
size_t level);
135 ConstTreePtr constTreePtr(
size_t level)
const;
171 ConstGridPtr grid(
size_t level)
const;
180 template<Index Order>
211 static Vec3R xyz(
const Coord& in_ijk,
size_t in_level,
size_t out_level);
212 static Vec3R xyz(
const Vec3R& in_xyz,
size_t in_level,
size_t out_level);
213 static Vec3R xyz(
const Vec3R& in_xyz,
double in_level,
double out_level);
230 template<Index Order>
232 template<Index Order>
242 template<Index Order>
252 template<Index Order>
263 ValueType prolongateVoxel(
const Coord& coords,
const size_t level)
const;
269 void prolongateActiveVoxels(
size_t destlevel,
size_t grainSize = 1);
277 ValueType restrictVoxel(
Coord ijk,
const size_t level,
bool useInjection =
false)
const;
285 void restrictActiveVoxels(
size_t destlevel,
size_t grainSize = 1);
288 void print(std::ostream& = std::cout,
int verboseLevel = 1)
const;
330 void topDownRestrict(
bool useInjection);
332 inline void initMeta();
345 template<Index Order>
349 template<
typename OpType>
struct CookOp;
352 std::vector<TreePtr> mTrees;
357template<
typename TreeType>
361 , mTransform(math::Transform::createLinearTransform( voxelSize ))
364 for (
size_t i=0; i<levels; ++i) mTrees[i] =
TreePtr(
new TreeType(background));
367template<
typename TreeType>
372 , mTransform( grid.transform().copy() )
375 mTrees[0].reset(
new TreeType(
grid.tree() ) );
376 mTrees[0]->voxelizeActiveTiles();
377 this->topDownRestrict(useInjection);
380template<
typename TreeType>
385 , mTransform( grid->transform().copy() )
388 mTrees[0] =
grid->treePtr();
389 mTrees[0]->voxelizeActiveTiles();
391 this->topDownRestrict(useInjection);
394template<
typename TreeType>
398 assert( level < mTrees.size() );
399 return *mTrees[level];
402template<
typename TreeType>
406 assert( level < mTrees.size() );
407 return *mTrees[level];
410template<
typename TreeType>
414 assert( level < mTrees.size() );
415 return mTrees[level];
418template<
typename TreeType>
422 assert( level < mTrees.size() );
423 return mTrees[level];
426template<
typename TreeType>
432 if (level>0) xform->preScale(
Real(1 << level) );
436 std::stringstream ss;
437 ss << this->getName() <<
"_level_" << level;
442template<
typename TreeType>
444grid(
size_t level)
const
449template<
typename TreeType>
452createGrid(
float level,
size_t grainSize)
const
454 assert( level >= 0.0f && level <=
float(mTrees.size()-1) );
458 xform->preScale(
math::Pow(2.0f, level) );
462 std::stringstream ss;
463 ss << this->getName() <<
"_level_" << level;
466 if (
size_t(floorf(level)) ==
size_t(ceilf(level)) ) {
467 grid->
setTree( this->constTree(
size_t(floorf(level))).copy() );
469 FractionOp<Order> tmp(*
this, grid->
tree(), level, grainSize);
479template<
typename TreeType>
484 for (
size_t level=0; level<mTrees.size(); ++level) grids->push_back(this->grid(level));
488template<
typename TreeType>
493 for (
size_t level=0; level<mTrees.size(); ++level) grids->push_back(this->grid(level));
497template<
typename TreeType>
499xyz(
const Coord& in_ijk,
size_t in_level,
size_t out_level)
504template<
typename TreeType>
506xyz(
const Vec3R& in_xyz,
size_t in_level,
size_t out_level)
508 return in_xyz *
Real(1 << in_level) /
Real(1 << out_level);
511template<
typename TreeType>
513xyz(
const Vec3R& in_xyz,
double in_level,
double out_level)
515 return in_xyz *
math::Pow(2.0, in_level - out_level);
519template<
typename TreeType>
524 assert( in_level >= 0 && in_level < mTrees.size() );
525 assert( out_level >= 0 && out_level < mTrees.size() );
530template<
typename TreeType>
535 assert( in_level >= 0 && in_level < mTrees.size() );
536 assert( out_level >= 0 && out_level < mTrees.size() );
541template<
typename TreeType>
546 assert( level >= 0.0 && level <=
double(mTrees.size()-1) );
547 const size_t level0 = size_t(floor(level)), level1 = size_t(ceil(level));
548 const ValueType v0 = this->
template sampleValue<Order>( ijk, 0, level0 );
549 if ( level0 == level1 )
return v0;
550 assert( level1 - level0 == 1 );
551 const ValueType v1 = this->
template sampleValue<Order>( ijk, 0, level1 );
558template<
typename TreeType>
563 assert( level >= 0.0 && level <=
double(mTrees.size()-1) );
564 const size_t level0 = size_t(floor(level)), level1 = size_t(ceil(level));
565 const ValueType v0 = this->
template sampleValue<Order>( xyz, 0, level0 );
566 if ( level0 == level1 )
return v0;
567 assert( level1 - level0 == 1 );
568 const ValueType v1 = this->
template sampleValue<Order>( xyz, 0, level1 );
575template<
typename TreeType>
579 assert( level+1 < mTrees.size() );
584template<
typename TreeType>
588 assert( destlevel < mTrees.size()-1 );
589 TreeType &fineTree = *mTrees[ destlevel ];
590 const TreeType &coarseTree = *mTrees[ destlevel+1 ];
591 CookOp<ProlongateOp> tmp( coarseTree, fineTree, grainSize );
594template<
typename TreeType>
598 assert( destlevel > 0 && destlevel < mTrees.size() );
599 const TreeType &fineTree = *mTrees[ destlevel-1 ];
600 if ( useInjection )
return fineTree.getValue(ijk<<1);
605template<
typename TreeType>
609 assert( destlevel > 0 && destlevel < mTrees.size() );
610 const TreeType &fineTree = *mTrees[ destlevel-1 ];
611 TreeType &coarseTree = *mTrees[ destlevel ];
612 CookOp<RestrictOp> tmp( fineTree, coarseTree, grainSize );
615template<
typename TreeType>
617print(std::ostream& os,
int verboseLevel)
const
619 os <<
"MultiResGrid with " << mTrees.size() <<
" levels\n";
620 for (
size_t i=0; i<mTrees.size(); ++i) {
621 os <<
"Level " << i <<
": ";
622 mTrees[i]->print(os, verboseLevel);
626 os <<
"Additional metadata:" << std::endl;
628 os <<
" " << it->first;
630 const std::string
value = it->second->str();
637 os <<
"Transform:" << std::endl;
638 transform().print(os,
" ");
642template<
typename TreeType>
646 const size_t levels = this->numLevels();
650 this->insertMeta(
"MultiResGrid_Levels",
Int64Metadata( levels ) );
653template<
typename TreeType>
654void MultiResGrid<TreeType>::
655topDownRestrict(
bool useInjection)
658 for (
size_t n=1; n<mTrees.size(); ++n) {
659 const TreeType &fineTree = *mTrees[n-1];
660 mTrees[n] = TreePtr(
new TreeType( fineTree.background() ) );
661 TreeType &coarseTree = *mTrees[n];
663 for (ValueOnCIter it = fineTree.cbeginValueOn(); it; ++it) {
664 const Coord ijk = it.getCoord();
665 if ( (ijk[0] & 1) || (ijk[1] & 1) || (ijk[2] & 1) )
continue;
666 coarseTree.setValue( ijk >> 1, *it );
669 MaskOp tmp(fineTree, coarseTree, 128);
670 this->restrictActiveVoxels(n, 64);
679template<
typename TreeType>
682 using MaskT =
typename TreeType::template ValueConverter<ValueMask>::Type;
683 using PoolType = tbb::enumerable_thread_specific<TreeType>;
686 using VoxelIterT =
typename ManagerT::LeafNodeType::ValueOnCIter;
688 MaskOp(
const TreeType& fineTree, TreeType& coarseTree,
size_t grainSize = 1)
689 : mPool(new
PoolType( coarseTree ) )
691 assert( coarseTree.empty() );
701 tbb::parallel_for(leafs.
leafRange( grainSize ), *
this);
704 using IterT =
typename PoolType::const_iterator;
705 for (IterT it=mPool->begin(); it!=mPool->end(); ++it) coarseTree.topologyUnion( *it );
710 Accessor coarseAcc( mPool->local() );
711 for (
typename RangeT::Iterator leafIter = range.begin(); leafIter; ++leafIter) {
712 for (
VoxelIterT voxelIter = leafIter->cbeginValueOn(); voxelIter; ++voxelIter) {
713 Coord ijk = voxelIter.getCoord();
714 if ( (ijk[2] & 1) || (ijk[1] & 1) || (ijk[0] & 1) )
continue;
722template<
typename TreeType>
726 using MaskT =
typename TreeType::template ValueConverter<ValueMask>::Type;
727 using PoolType = tbb::enumerable_thread_specific<MaskT>;
728 using PoolIterT =
typename PoolType::iterator;
731 using Range1 =
typename Manager1::LeafRange;
732 using Range2 =
typename Manager2::LeafRange;
737 size_t grainSize = 1)
740 , mTree0( &*(parent.mTrees[
size_t(floorf(level))]) )
741 , mTree1( &*(parent.mTrees[
size_t(ceilf(level))]) )
743 assert( midTree.empty() );
744 assert( mTree0 != mTree1 );
747 MaskT examplar(
false );
748 mPool =
new PoolType( examplar );
752 tbb::parallel_for( manager.
leafRange(grainSize), *
this );
756 tbb::parallel_for(tbb::blocked_range<PoolIterT>(mPool->begin(),mPool->end(),1), *
this);
759 for (PoolIterT it=mPool->begin(); it!=mPool->end(); ++it) midTree.topologyUnion( *it );
763 Manager2 manager( midTree );
764 tbb::parallel_for(manager.leafRange(grainSize), *
this);
767 void operator()(
const Range1& range)
const
769 using VoxelIter =
typename Manager1::LeafNodeType::ValueOnCIter;
780 for (
typename Range1::Iterator leafIter = range.begin(); leafIter; ++leafIter) {
781 for (VoxelIter voxelIter = leafIter->cbeginValueOn(); voxelIter; ++voxelIter) {
782 Coord ijk = voxelIter.getCoord();
784 const auto value0 = ijk[0] *
scale;
785 const auto value1 = ijk[1] *
scale;
786 const auto value2 = ijk[2] *
scale;
792 acc.setValueOn( ijk );
796 void operator()(
const tbb::blocked_range<PoolIterT>& range)
const
798 for (PoolIterT it=range.begin(); it!=range.end(); ++it) {
802 void operator()(
const Range2 &r)
const
804 using VoxelIter =
typename TreeType::LeafNodeType::ValueOnIter;
818 const float scale0 =
math::Pow( 2.0f, b );
819 const float scale1 =
math::Pow( 2.0f,-a );
820 ConstAccessor acc0( *mTree0 ), acc1( *mTree1 );
821 for (
typename Range2::Iterator leafIter = r.begin(); leafIter; ++leafIter) {
822 for (VoxelIter voxelIter = leafIter->beginValueOn(); voxelIter; ++voxelIter) {
823 const Vec3R xyz =
Vec3R( voxelIter.getCoord().data() );
827 const auto value0 = a*v0;
828 const auto value1 = b*v1;
830 voxelIter.setValue( ValueType(value0 + value1) );
836 const TreeType *mTree0, *mTree1;
840template<
typename TreeType>
841template<
typename OperatorType>
842struct MultiResGrid<TreeType>::CookOp
844 using ManagerT = tree::LeafManager<TreeType>;
845 using RangeT =
typename ManagerT::LeafRange;
847 CookOp(
const TreeType& srcTree, TreeType& dstTree,
size_t grainSize): acc(srcTree)
849 ManagerT leafs(dstTree);
850 tbb::parallel_for(leafs.leafRange(grainSize), *
this);
852 CookOp(
const CookOp &other): acc(other.acc.tree()) {}
854 void operator()(
const RangeT& range)
const
856 for (
auto leafIt = range.begin(); leafIt; ++leafIt) {
857 auto& phi = leafIt.buffer(0);
858 for (
auto voxelIt = leafIt->beginValueOn(); voxelIt; ++voxelIt) {
864 const ConstAccessor acc;
868template<
typename TreeType>
891 for (
int i=-1; i<=1; i+=2) {
892 for (
int j=-1; j<=1; j+=2) {
901template<
typename TreeType>
909 switch ( (ijk[0] & 1) | ((ijk[1] & 1) << 1) | ((ijk[2] & 1) << 2) ) {
939 for (
int i=-1; i<=1; i+=2) {
940 for (
int j=-1; j<=1; j+=2) {
954#ifdef OPENVDB_USE_EXPLICIT_INSTANTIATION
956#ifdef OPENVDB_INSTANTIATE_MULTIRESGRID
#define OPENVDB_INSTANTIATE_CLASS
Definition: ExplicitInstantiation.h:12
ValueT value
Definition: GridBuilder.h:1287
A LeafManager manages a linear array of pointers to a given tree's leaf nodes, as well as optional au...
General-purpose arithmetic and comparison routines, most of which accept arbitrary value types (or at...
Implementation of morphological dilation and erosion.
Defined various multi-threaded utility functions for trees.
Propagate the signs of distance values from the active voxels in the narrow band to the inactive valu...
void setName(const std::string &)
Specify a name for this grid.
void setTransform(math::Transform::Ptr)
Associate the given transform with this grid, in place of its existing transform.
Definition: Grid.h:1247
GridClass getGridClass() const
Return the class of volumetric data (level set, fog volume, etc.) that is stored in this grid.
static const char *const META_GRID_NAME
Definition: Grid.h:357
static const char *const META_GRID_CLASS
Definition: Grid.h:355
static GridClass stringToGridClass(const std::string &)
Return the class of volumetric data specified by the given string.
static std::string gridClassToString(GridClass)
Return the metadata string value for the given class of volumetric data.
Container class that associates a tree with a transform and metadata.
Definition: Grid.h:577
TreeType & tree()
Return a reference to this grid's tree, which might be shared with other grids.
Definition: Grid.h:917
void setTree(TreeBase::Ptr) override
Associate the given tree with this grid, in place of its existing tree.
Definition: Grid.h:1473
SharedPtr< const Grid > ConstPtr
Definition: Grid.h:580
SharedPtr< Grid > Ptr
Definition: Grid.h:579
static Ptr create()
Return a new grid with background value zero.
Definition: Grid.h:1323
Tag dispatch class that distinguishes topology copy constructors from deep copy constructors.
Definition: Types.h:564
Definition: Exceptions.h:65
Signed (x, y, z) 32-bit integer coordinates.
Definition: Coord.h:25
Coord offsetBy(Int32 dx, Int32 dy, Int32 dz) const
Definition: Coord.h:91
const Int32 * data() const
Definition: Coord.h:139
Definition: LeafManager.h:102
This class manages a linear array of pointers to a given tree's leaf nodes, as well as optional auxil...
Definition: LeafManager.h:85
LeafRange leafRange(size_t grainsize=1) const
Return a TBB-compatible LeafRange.
Definition: LeafManager.h:345
Definition: ValueAccessor.h:183
void setValueOn(const Coord &xyz, const ValueType &value)
Definition: ValueAccessor.h:255
const ValueType & getValue(const Coord &xyz) const
Return the value of the voxel at the given coordinates.
Definition: ValueAccessor.h:219
void print(const ast::Node &node, const bool numberStatements=true, std::ostream &os=std::cout, const char *indent=" ")
Writes a descriptive printout of a Node hierarchy into a target stream.
void run(const char *ax, openvdb::GridBase &grid)
Run a full AX pipeline (parse, compile and execute) on a single OpenVDB Grid.
OPENVDB_API uint32_t getGridClass(std::ios_base &)
Return the class (GRID_LEVEL_SET, GRID_UNKNOWN, etc.) of the grid currently being read from or writte...
float Round(float x)
Return x rounded to the nearest integer.
Definition: Math.h:822
Type Pow(Type x, int n)
Return xn.
Definition: Math.h:564
Type FractionalPart(Type x)
Return the fractional part of x.
Definition: Math.h:846
MatType scale(const Vec3< typename MatType::value_type > &s)
Return a matrix that scales by s.
Definition: Mat.h:637
std::vector< GridBase::Ptr > GridPtrVec
Definition: Grid.h:514
TypedMetadata< float > FloatMetadata
Definition: Metadata.h:360
double Real
Definition: Types.h:60
GridClass
Definition: Types.h:335
@ GRID_LEVEL_SET
Definition: Types.h:337
@ GRID_UNKNOWN
Definition: Types.h:336
SharedPtr< GridCPtrVec > GridCPtrVecPtr
Definition: Grid.h:522
SharedPtr< GridPtrVec > GridPtrVecPtr
Definition: Grid.h:517
TypedMetadata< std::string > StringMetadata
Definition: Metadata.h:363
math::Vec3< Real > Vec3R
Definition: Types.h:72
std::shared_ptr< T > SharedPtr
Definition: Types.h:114
TypedMetadata< int64_t > Int64Metadata
Definition: Metadata.h:362
std::vector< GridBase::ConstPtr > GridCPtrVec
Definition: Grid.h:519
openvdb::GridBase::Ptr GridPtr
Definition: Utils.h:35
Definition: Exceptions.h:13
#define OPENVDB_THROW(exception, message)
Definition: Exceptions.h:74
Defines various finite difference stencils by means of the "curiously recurring template pattern" on ...
NodeManager produces linear arrays of all tree nodes allowing for efficient threading and bottom-up p...
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h.in:116
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h.in:202