39 #ifndef OPENVDB_TOOLS_LEVEL_SET_TRACKER_HAS_BEEN_INCLUDED 40 #define OPENVDB_TOOLS_LEVEL_SET_TRACKER_HAS_BEEN_INCLUDED 42 #include <tbb/parallel_for.h> 43 #include <boost/bind.hpp> 44 #include <boost/function.hpp> 45 #include <boost/type_traits/is_floating_point.hpp> 66 template<
typename Gr
idT,
typename InterruptT = util::NullInterrupter>
72 typedef typename TreeType::LeafNodeType
LeafType;
77 typedef typename TreeType::template ValueConverter<ValueMask>::Type
MaskTreeType;
78 BOOST_STATIC_ASSERT(boost::is_floating_point<ValueType>::value);
85 : spatialScheme(s), temporalScheme(t), normCount(n), grainSize(g) {}
100 template <
typename MaskType>
104 void normalize() { this->normalize<MaskTreeType>(NULL); }
128 void dilate(
int iterations = 1);
132 void erode(
int iterations = 1);
176 void startInterrupter(
const char* msg);
178 void endInterrupter();
181 bool checkInterrupter();
183 const GridType&
grid()
const {
return *mGrid; }
185 LeafManagerType&
leafs() {
return *mLeafs; }
187 const LeafManagerType&
leafs()
const {
return *mLeafs; }
201 void operator()(
const LeafRange& r)
const;
212 typedef typename SchemeT::template ISStencil<GridType>::StencilType StencilT;
213 typedef typename MaskT::LeafNodeType MaskLeafT;
214 typedef typename MaskLeafT::ValueOnCIter MaskIterT;
215 typedef typename LeafType::ValueOnCIter VoxelIterT;
218 void operator()(
const LeafRange& r)
const {mTask(const_cast<Normalizer*>(
this), r);}
219 void cook(
const char* msg,
int swapBuffer=0);
220 template <
int Nominator,
int Denominator>
221 void euler(
const LeafRange& range,
Index phiBuffer,
Index resultBuffer);
222 inline void euler01(
const LeafRange& r) {this->euler<0,1>(r, 0, 1);}
223 inline void euler12(
const LeafRange& r) {this->euler<1,2>(r, 1, 1);}
224 inline void euler34(
const LeafRange& r) {this->euler<3,4>(r, 1, 2);}
225 inline void euler13(
const LeafRange& r) {this->euler<1,3>(r, 1, 2);}
226 template <
int Nominator,
int Denominator>
227 void eval(StencilT& stencil,
const ValueType* phi, ValueType* result,
Index n)
const;
230 const ValueType mDt, mInvDx;
231 typename boost::function<void (Normalizer*, const LeafRange&)> mTask;
234 template<math::BiasedGradientScheme SpatialScheme,
typename MaskT>
235 void normalize1(
const MaskT* mask);
239 void normalize2(
const MaskT* mask);
246 LeafManagerType* mLeafs;
247 InterruptT* mInterrupter;
252 template<
typename Gr
idT,
typename InterruptT>
256 mLeafs(new LeafManagerType(grid.tree())),
257 mInterrupter(interrupt),
258 mDx(static_cast<ValueType>(grid.voxelSize()[0])),
261 if ( !grid.hasUniformVoxels() ) {
263 "The transform must have uniform scale for the LevelSetTracker to function");
267 "LevelSetTracker expected a level set, got a grid of class \"" 268 + grid.gridClassToString(grid.getGridClass())
269 +
"\" [hint: Grid::setGridClass(openvdb::GRID_LEVEL_SET)]");
273 template<
typename Gr
idT,
typename InterruptT>
292 template<
typename Gr
idT,
typename InterruptT>
307 template<
typename Gr
idT,
typename InterruptT>
313 for (
int i=0; i < iterations; ++i) {
318 for (
int i=0; i < iterations; ++i) {
319 MaskTreeType mask0(mGrid->tree(),
false,
TopologyCopy());
322 MaskTreeType mask(mGrid->tree(),
false,
TopologyCopy());
323 mask.topologyDifference(mask0);
329 template<
typename Gr
idT,
typename InterruptT>
336 const ValueType background = mGrid->background() - iterations*mDx;
340 template<
typename Gr
idT,
typename InterruptT>
346 const int wNew =
static_cast<int>(halfWidth);
348 this->
dilate(wNew - wOld);
349 }
else if (wOld > wNew) {
350 this->
erode(wOld - wNew);
355 template<
typename Gr
idT,
typename InterruptT>
360 if (mInterrupter) mInterrupter->start(msg);
363 template<
typename Gr
idT,
typename InterruptT>
368 if (mInterrupter) mInterrupter->end();
371 template<
typename Gr
idT,
typename InterruptT>
377 tbb::task::self().cancel_group_execution();
383 template<
typename Gr
idT,
typename InterruptT>
384 template<
typename MaskT>
391 this->normalize1<math::FIRST_BIAS , MaskT>(mask);
break;
393 this->normalize1<math::SECOND_BIAS, MaskT>(mask);
break;
395 this->normalize1<math::THIRD_BIAS, MaskT>(mask);
break;
397 this->normalize1<math::WENO5_BIAS, MaskT>(mask);
break;
399 this->normalize1<math::HJWENO5_BIAS, MaskT>(mask);
break;
405 template<
typename Gr
idT,
typename InterruptT>
406 template<math::BiasedGradientScheme SpatialScheme,
typename MaskT>
413 this->normalize2<SpatialScheme, math::TVD_RK1, MaskT>(mask);
break;
415 this->normalize2<SpatialScheme, math::TVD_RK2, MaskT>(mask);
break;
417 this->normalize2<SpatialScheme, math::TVD_RK3, MaskT>(mask);
break;
423 template<
typename Gr
idT,
typename InterruptT>
431 Normalizer<SpatialScheme, TemporalScheme, MaskT> tmp(*
this, mask);
437 template<
typename Gr
idT,
typename InterruptT>
443 const LeafRange range = mTracker.leafs().leafRange(grainSize);
446 tbb::parallel_for(range, *
this);
453 template<
typename Gr
idT,
typename InterruptT>
458 typedef typename LeafType::ValueOnIter VoxelIterT;
460 const ValueType gamma = mTracker.mGrid->background();
463 LeafType &leaf = *leafIter;
464 for (VoxelIterT iter = leaf.beginValueOn(); iter; ++iter) {
465 const ValueType val = *iter;
467 leaf.setValueOff(iter.pos(), -gamma);
468 else if (val >= gamma)
469 leaf.setValueOff(iter.pos(), gamma);
476 template<
typename Gr
idT,
typename InterruptT>
493 template<
typename Gr
idT,
typename InterruptT>
503 mTracker.mLeafs->rebuildAuxBuffers(TemporalScheme ==
math::TVD_RK3 ? 2 : 1);
505 for (
int n=0, e=mTracker.getNormCount(); n < e; ++n) {
508 switch(TemporalScheme) {
512 mTask = boost::bind(&Normalizer::euler01, _1, _2);
515 this->cook(
"Normalizing level set using TVD_RK1", 1);
520 mTask = boost::bind(&Normalizer::euler01, _1, _2);
523 this->cook(
"Normalizing level set using TVD_RK1 (step 1 of 2)", 1);
527 mTask = boost::bind(&Normalizer::euler12, _1, _2);
530 this->cook(
"Normalizing level set using TVD_RK1 (step 2 of 2)", 1);
535 mTask = boost::bind(&Normalizer::euler01, _1, _2);
538 this->cook(
"Normalizing level set using TVD_RK3 (step 1 of 3)", 1);
542 mTask = boost::bind(&Normalizer::euler34, _1, _2);
545 this->cook(
"Normalizing level set using TVD_RK3 (step 2 of 3)", 2);
549 mTask = boost::bind(&Normalizer::euler13, _1, _2);
552 this->cook(
"Normalizing level set using TVD_RK3 (step 3 of 3)", 2);
559 mTracker.mLeafs->removeAuxBuffers();
564 template<
typename Gr
idT,
typename InterruptT>
571 cook(
const char* msg,
int swapBuffer)
575 const int grainSize = mTracker.getGrainSize();
576 const LeafRange range = mTracker.leafs().leafRange(grainSize);
578 grainSize>0 ? tbb::parallel_for(range, *
this) : (*this)(range);
580 mTracker.leafs().swapLeafBuffer(swapBuffer, grainSize==0);
582 mTracker.endInterrupter();
585 template<
typename Gr
idT,
typename InterruptT>
589 template <
int Nominator,
int Denominator>
593 eval(StencilT& stencil,
const ValueType* phi, ValueType* result,
Index n)
const 597 static const ValueType beta =
ValueType(1) - alpha;
599 const ValueType normSqGradPhi = GradientT::result(stencil);
600 const ValueType phi0 = stencil.getValue();
603 v = phi0 - mDt * v * (
math::Sqrt(normSqGradPhi) * mInvDx - 1.0f);
604 result[n] = Nominator ? alpha * phi[n] + beta * v : v;
607 template<
typename Gr
idT,
typename InterruptT>
611 template <
int Nominator,
int Denominator>
617 typedef typename LeafType::ValueOnCIter VoxelIterT;
621 StencilT stencil(mTracker.grid());
624 const ValueType* phi = leafIter.buffer(phiBuffer).data();
625 ValueType* result = leafIter.buffer(resultBuffer).data();
627 for (VoxelIterT iter = leafIter->cbeginValueOn(); iter; ++iter) {
628 stencil.moveTo(iter);
629 this->eval<Nominator, Denominator>(stencil, phi, result, iter.pos());
631 }
else if (
const MaskLeafT* mask = mMask->probeLeaf(leafIter->origin())) {
632 const ValueType* phi0 = leafIter->buffer().data();
633 for (MaskIterT iter = mask->cbeginValueOn(); iter; ++iter) {
634 const Index i = iter.pos();
635 stencil.moveTo(iter.getCoord(), phi0[i]);
636 this->eval<Nominator, Denominator>(stencil, phi, result, i);
646 #endif // OPENVDB_TOOLS_LEVEL_SET_TRACKER_HAS_BEEN_INCLUDED
Definition: LeafManager.h:132
Definition: Operators.h:253
General-purpose arithmetic and comparison routines, most of which accept arbitrary value types (or at...
float Sqrt(float x)
Return the square root of a floating-point value.
Definition: Math.h:727
Definition: FiniteDifference.h:195
TemporalIntegrationScheme
Temporal integration schemes.
Definition: FiniteDifference.h:261
void rebuildLeafArray()
Remove the auxiliary buffers and rebuild the leaf array.
Definition: LeafManager.h:323
Type Pow2(Type x)
Return .
Definition: Math.h:514
#define OPENVDB_THROW(exception, message)
Definition: Exceptions.h:97
Definition: FiniteDifference.h:194
Efficient multi-threaded replacement of the background values in tree.
static const Real LEVEL_SET_HALF_WIDTH
Definition: Types.h:219
Definition: FiniteDifference.h:263
This class manages a linear array of pointers to a given tree's leaf nodes, as well as optional auxil...
Definition: LeafManager.h:115
Definition: FiniteDifference.h:198
Definition: Operators.h:152
Definition: FiniteDifference.h:197
Defined various multi-threaded utility functions for trees.
Index32 Index
Definition: Types.h:58
static T value()
Definition: Math.h:125
#define OPENVDB_VERSION_NAME
Definition: version.h:43
Definition: Exceptions.h:86
CopyConstness< TreeType, NonConstBufferType >::Type BufferType
Definition: LeafManager.h:126
Implementation of morphological dilation and erosion.
Definition: Exceptions.h:39
BiasedGradientScheme
Biased Gradients are limited to non-centered differences.
Definition: FiniteDifference.h:192
Definition: FiniteDifference.h:265
Iterator begin() const
Definition: LeafManager.h:188
float RoundDown(float x)
Return x rounded down to the nearest integer.
Definition: Math.h:769
Definition: LeafManager.h:135
Definition: Exceptions.h:88
Defines various finite difference stencils by means of the "curiously recurring template pattern" on ...
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:71
A LeafManager manages a linear array of pointers to a given tree's leaf nodes, as well as optional au...
Definition: FiniteDifference.h:196
Definition: FiniteDifference.h:264
bool wasInterrupted(T *i, int percent=-1)
Definition: NullInterrupter.h:76