12 #ifndef OPENVDB_TOOLS_LEVEL_SET_TRACKER_HAS_BEEN_INCLUDED 13 #define OPENVDB_TOOLS_LEVEL_SET_TRACKER_HAS_BEEN_INCLUDED 15 #include <tbb/parallel_for.h> 30 #include <type_traits> 54 template<
typename Gr
idT,
typename InterruptT = util::NullInterrupter>
62 using LeafType =
typename TreeType::LeafNodeType;
65 using LeafRange =
typename LeafManagerType::LeafRange;
67 using MaskTreeType =
typename TreeType::template ValueConverter<ValueMask>::Type;
68 static_assert(std::is_floating_point<ValueType>::value,
69 "LevelSetTracker requires a level set grid with floating-point values");
76 : spatialScheme(s), temporalScheme(t), normCount(n), grainSize(g) {}
91 template <
typename MaskType>
95 void normalize() { this->normalize<MaskTreeType>(
nullptr); }
122 void dilate(
int iterations = 1);
126 void erode(
int iterations = 1);
178 void startInterrupter(
const char* msg);
180 void endInterrupter();
183 bool checkInterrupter();
198 template<TrimMode Trimming>
203 void operator()(
const LeafRange& r)
const;
214 using StencilT =
typename SchemeT::template ISStencil<GridType>::StencilType;
215 using MaskLeafT =
typename MaskT::LeafNodeType;
216 using MaskIterT =
typename MaskLeafT::ValueOnCIter;
217 using VoxelIterT =
typename LeafType::ValueOnCIter;
221 void operator()(
const LeafRange& r)
const {mTask(const_cast<Normalizer*>(
this), r);}
222 void cook(
const char* msg,
int swapBuffer=0);
223 template <
int Nominator,
int Denominator>
225 inline void euler01(
const LeafRange& r) {this->euler<0,1>(r, 0, 1);}
226 inline void euler12(
const LeafRange& r) {this->euler<1,2>(r, 1, 1);}
227 inline void euler34(
const LeafRange& r) {this->euler<3,4>(r, 1, 2);}
228 inline void euler13(
const LeafRange& r) {this->euler<1,3>(r, 1, 2);}
229 template <
int Nominator,
int Denominator>
234 typename std::function<void (Normalizer*, const LeafRange&)> mTask;
237 template<math::BiasedGradientScheme SpatialScheme,
typename MaskT>
238 void normalize1(
const MaskT* mask);
242 void normalize2(
const MaskT* mask);
250 InterruptT* mInterrupter;
253 TrimMode mTrimMode = TrimMode::kAll;
256 template<
typename Gr
idT,
typename InterruptT>
261 mInterrupter(interrupt),
262 mDx(static_cast<
ValueType>(grid.voxelSize()[0])),
265 if ( !grid.hasUniformVoxels() ) {
267 "The transform must have uniform scale for the LevelSetTracker to function");
271 "LevelSetTracker expected a level set, got a grid of class \"" 272 + grid.gridClassToString(grid.getGridClass())
273 +
"\" [hint: Grid::setGridClass(openvdb::GRID_LEVEL_SET)]");
277 template<
typename Gr
idT,
typename InterruptT>
286 case TrimMode::kNone:
break;
287 case TrimMode::kInterior: Trim<TrimMode::kInterior>(*this).trim();
break;
288 case TrimMode::kExterior: Trim<TrimMode::kExterior>(*this).trim();
break;
289 case TrimMode::kAll: Trim<TrimMode::kAll>(*this).trim();
break;
296 mLeafs->rebuildLeafArray();
300 template<
typename Gr
idT,
typename InterruptT>
315 template<
typename Gr
idT,
typename InterruptT>
321 for (
int i=0; i < iterations; ++i) {
326 for (
int i=0; i < iterations; ++i) {
331 mask.topologyDifference(mask0);
337 template<
typename Gr
idT,
typename InterruptT>
343 mLeafs->rebuildLeafArray();
348 template<
typename Gr
idT,
typename InterruptT>
354 const int wNew =
static_cast<int>(halfWidth);
356 this->
dilate(wNew - wOld);
357 }
else if (wOld > wNew) {
358 this->
erode(wOld - wNew);
363 template<
typename Gr
idT,
typename InterruptT>
368 if (mInterrupter) mInterrupter->start(msg);
371 template<
typename Gr
idT,
typename InterruptT>
376 if (mInterrupter) mInterrupter->end();
379 template<
typename Gr
idT,
typename InterruptT>
385 tbb::task::self().cancel_group_execution();
391 template<
typename Gr
idT,
typename InterruptT>
392 template<
typename MaskT>
399 this->normalize1<math::FIRST_BIAS , MaskT>(mask);
break;
401 this->normalize1<math::SECOND_BIAS, MaskT>(mask);
break;
403 this->normalize1<math::THIRD_BIAS, MaskT>(mask);
break;
405 this->normalize1<math::WENO5_BIAS, MaskT>(mask);
break;
407 this->normalize1<math::HJWENO5_BIAS, MaskT>(mask);
break;
414 template<
typename Gr
idT,
typename InterruptT>
415 template<math::BiasedGradientScheme SpatialScheme,
typename MaskT>
422 this->normalize2<SpatialScheme, math::TVD_RK1, MaskT>(mask);
break;
424 this->normalize2<SpatialScheme, math::TVD_RK2, MaskT>(mask);
break;
426 this->normalize2<SpatialScheme, math::TVD_RK3, MaskT>(mask);
break;
433 template<
typename Gr
idT,
typename InterruptT>
441 Normalizer<SpatialScheme, TemporalScheme, MaskT> tmp(*
this, mask);
449 template<
typename Gr
idT,
typename InterruptT>
450 template<lstrack::TrimMode Trimming>
455 if (Trimming != TrimMode::kNone) {
456 const int grainSize = mTracker.getGrainSize();
457 const LeafRange range = mTracker.leafs().leafRange(grainSize);
460 tbb::parallel_for(range, *
this);
470 template<
typename Gr
idT,
typename InterruptT>
471 template<lstrack::TrimMode Trimming>
476 const ValueType gamma = mTracker.mGrid->background();
479 for (
auto leafIter = range.begin(); leafIter; ++leafIter) {
480 auto& leaf = *leafIter;
481 for (
auto iter = leaf.beginValueOn(); iter; ++iter) {
482 const auto val = *iter;
484 case TrimMode::kNone:
486 case TrimMode::kInterior:
487 if (val <= -gamma) { leaf.setValueOff(iter.pos(), -gamma); }
489 case TrimMode::kExterior:
490 if (val >= gamma) { leaf.setValueOff(iter.pos(), gamma); }
494 leaf.setValueOff(iter.pos(), -gamma);
495 }
else if (val >= gamma) {
496 leaf.setValueOff(iter.pos(), gamma);
508 template<
typename Gr
idT,
typename InterruptT>
525 template<
typename Gr
idT,
typename InterruptT>
534 namespace ph = std::placeholders;
537 mTracker.mLeafs->rebuildAuxBuffers(TemporalScheme ==
math::TVD_RK3 ? 2 : 1);
539 for (
int n=0, e=mTracker.getNormCount(); n < e; ++n) {
542 switch(TemporalScheme) {
546 mTask = std::bind(&Normalizer::euler01, ph::_1, ph::_2);
549 this->cook(
"Normalizing level set using TVD_RK1", 1);
554 mTask = std::bind(&Normalizer::euler01, ph::_1, ph::_2);
557 this->cook(
"Normalizing level set using TVD_RK1 (step 1 of 2)", 1);
561 mTask = std::bind(&Normalizer::euler12, ph::_1, ph::_2);
564 this->cook(
"Normalizing level set using TVD_RK1 (step 2 of 2)", 1);
569 mTask = std::bind(&Normalizer::euler01, ph::_1, ph::_2);
572 this->cook(
"Normalizing level set using TVD_RK3 (step 1 of 3)", 1);
576 mTask = std::bind(&Normalizer::euler34, ph::_1, ph::_2);
579 this->cook(
"Normalizing level set using TVD_RK3 (step 2 of 3)", 2);
583 mTask = std::bind(&Normalizer::euler13, ph::_1, ph::_2);
586 this->cook(
"Normalizing level set using TVD_RK3 (step 3 of 3)", 2);
594 mTracker.mLeafs->removeAuxBuffers();
599 template<
typename Gr
idT,
typename InterruptT>
606 cook(
const char* msg,
int swapBuffer)
610 const int grainSize = mTracker.getGrainSize();
611 const LeafRange range = mTracker.leafs().leafRange(grainSize);
613 grainSize>0 ? tbb::parallel_for(range, *
this) : (*this)(range);
615 mTracker.leafs().swapLeafBuffer(swapBuffer, grainSize==0);
617 mTracker.endInterrupter();
620 template<
typename Gr
idT,
typename InterruptT>
624 template <
int Nominator,
int Denominator>
634 const ValueType normSqGradPhi = GradientT::result(stencil);
635 const ValueType phi0 = stencil.getValue();
638 v = phi0 - mDt * v * (
math::Sqrt(normSqGradPhi) * mInvDx - 1.0f);
639 result[n] = Nominator ? alpha * phi[n] + beta * v : v;
642 template<
typename Gr
idT,
typename InterruptT>
646 template <
int Nominator,
int Denominator>
654 StencilT stencil(mTracker.grid());
656 for (
typename LeafRange::Iterator leafIter = range.begin(); leafIter; ++leafIter) {
657 const ValueType* phi = leafIter.buffer(phiBuffer).data();
658 ValueType* result = leafIter.buffer(resultBuffer).data();
659 if (mMask ==
nullptr) {
660 for (
auto iter = leafIter->cbeginValueOn(); iter; ++iter) {
661 stencil.moveTo(iter);
662 this->eval<Nominator, Denominator>(stencil, phi, result, iter.pos());
664 }
else if (
const MaskLeafT* mask = mMask->probeLeaf(leafIter->origin())) {
665 const ValueType* phi0 = leafIter->buffer().data();
666 for (MaskIterT iter = mask->cbeginValueOn(); iter; ++iter) {
667 const Index i = iter.pos();
668 stencil.moveTo(iter.getCoord(), phi0[i]);
669 this->eval<Nominator, Denominator>(stencil, phi, result, i);
679 #endif // OPENVDB_TOOLS_LEVEL_SET_TRACKER_HAS_BEEN_INCLUDED
TemporalIntegrationScheme
Temporal integration schemes.
Definition: FiniteDifference.h:234
#define OPENVDB_THROW(exception, message)
Definition: openvdb/Exceptions.h:82
General-purpose arithmetic and comparison routines, most of which accept arbitrary value types (or at...
static T value()
Definition: Math.h:137
Definition: FiniteDifference.h:169
Efficient multi-threaded replacement of the background values in tree.
bool wasInterrupted(T *i, int percent=-1)
Definition: NullInterrupter.h:49
Definition: Operators.h:126
Definition: openvdb/Types.h:315
Definition: FiniteDifference.h:167
Definition: FiniteDifference.h:236
Definition: Operators.h:227
Defined various multi-threaded utility functions for trees.
Definition: openvdb/Exceptions.h:65
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h:94
float Sqrt(float x)
Return the square root of a floating-point value.
Definition: Math.h:754
Type Pow2(Type x)
Return x2.
Definition: Math.h:541
Definition: FiniteDifference.h:235
Implementation of morphological dilation and erosion.
Definition: openvdb/Exceptions.h:13
Definition: FiniteDifference.h:166
Definition: FiniteDifference.h:237
Definition: FiniteDifference.h:238
Definition: FiniteDifference.h:168
This class manages a linear array of pointers to a given tree's leaf nodes, as well as optional auxil...
Definition: LeafManager.h:84
Definition: FiniteDifference.h:171
Defines various finite difference stencils by means of the "curiously recurring template pattern" on ...
BiasedGradientScheme
Biased Gradients are limited to non-centered differences.
Definition: FiniteDifference.h:165
Tag dispatch class that distinguishes topology copy constructors from deep copy constructors.
Definition: openvdb/Types.h:542
Definition: openvdb/Exceptions.h:63
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:146
static const Real LEVEL_SET_HALF_WIDTH
Definition: openvdb/Types.h:321
A LeafManager manages a linear array of pointers to a given tree's leaf nodes, as well as optional au...
Definition: FiniteDifference.h:170
Index32 Index
Definition: openvdb/Types.h:32
float RoundDown(float x)
Return x rounded down to the nearest integer.
Definition: Math.h:796