12 #ifndef OPENVDB_TOOLS_LEVEL_SET_TRACKER_HAS_BEEN_INCLUDED 13 #define OPENVDB_TOOLS_LEVEL_SET_TRACKER_HAS_BEEN_INCLUDED 23 #include <openvdb/thread/Threading.h> 30 #include <tbb/parallel_for.h> 33 #include <type_traits> 56 template<
typename Gr
idT,
typename InterruptT = util::NullInterrupter>
64 using LeafType =
typename TreeType::LeafNodeType;
67 using LeafRange =
typename LeafManagerType::LeafRange;
69 using MaskTreeType =
typename TreeType::template ValueConverter<ValueMask>::Type;
70 static_assert(std::is_floating_point<ValueType>::value,
71 "LevelSetTracker requires a level set grid with floating-point values");
78 : spatialScheme(s), temporalScheme(t), normCount(n), grainSize(g) {}
93 template <
typename MaskType>
97 void normalize() { this->normalize<MaskTreeType>(
nullptr); }
124 void dilate(
int iterations = 1);
128 void erode(
int iterations = 1);
180 void startInterrupter(
const char* msg);
182 void endInterrupter();
185 bool checkInterrupter();
200 template<TrimMode Trimming>
205 void operator()(
const LeafRange& r)
const;
216 using StencilT =
typename SchemeT::template ISStencil<GridType>::StencilType;
217 using MaskLeafT =
typename MaskT::LeafNodeType;
218 using MaskIterT =
typename MaskLeafT::ValueOnCIter;
219 using VoxelIterT =
typename LeafType::ValueOnCIter;
223 void operator()(
const LeafRange& r)
const {mTask(const_cast<Normalizer*>(
this), r);}
224 void cook(
const char* msg,
int swapBuffer=0);
225 template <
int Nominator,
int Denominator>
227 inline void euler01(
const LeafRange& r) {this->euler<0,1>(r, 0, 1);}
228 inline void euler12(
const LeafRange& r) {this->euler<1,2>(r, 1, 1);}
229 inline void euler34(
const LeafRange& r) {this->euler<3,4>(r, 1, 2);}
230 inline void euler13(
const LeafRange& r) {this->euler<1,3>(r, 1, 2);}
231 template <
int Nominator,
int Denominator>
236 typename std::function<void (Normalizer*, const LeafRange&)> mTask;
239 template<math::BiasedGradientScheme SpatialScheme,
typename MaskT>
240 void normalize1(
const MaskT* mask);
244 void normalize2(
const MaskT* mask);
252 InterruptT* mInterrupter;
255 TrimMode mTrimMode = TrimMode::kAll;
258 template<
typename Gr
idT,
typename InterruptT>
263 mInterrupter(interrupt),
264 mDx(static_cast<
ValueType>(grid.voxelSize()[0])),
267 if ( !grid.hasUniformVoxels() ) {
269 "The transform must have uniform scale for the LevelSetTracker to function");
273 "LevelSetTracker expected a level set, got a grid of class \"" 274 + grid.gridClassToString(grid.getGridClass())
275 +
"\" [hint: Grid::setGridClass(openvdb::GRID_LEVEL_SET)]");
279 template<
typename Gr
idT,
typename InterruptT>
288 case TrimMode::kNone:
break;
289 case TrimMode::kInterior: Trim<TrimMode::kInterior>(*this).trim();
break;
290 case TrimMode::kExterior: Trim<TrimMode::kExterior>(*this).trim();
break;
291 case TrimMode::kAll: Trim<TrimMode::kAll>(*this).trim();
break;
298 mLeafs->rebuildLeafArray();
302 template<
typename Gr
idT,
typename InterruptT>
317 template<
typename Gr
idT,
typename InterruptT>
323 for (
int i=0; i < iterations; ++i) {
328 for (
int i=0; i < iterations; ++i) {
333 mask.topologyDifference(mask0);
339 template<
typename Gr
idT,
typename InterruptT>
346 mLeafs->rebuildLeafArray();
351 template<
typename Gr
idT,
typename InterruptT>
357 const int wNew =
static_cast<int>(halfWidth);
359 this->
dilate(wNew - wOld);
360 }
else if (wOld > wNew) {
361 this->
erode(wOld - wNew);
366 template<
typename Gr
idT,
typename InterruptT>
371 if (mInterrupter) mInterrupter->start(msg);
374 template<
typename Gr
idT,
typename InterruptT>
379 if (mInterrupter) mInterrupter->end();
382 template<
typename Gr
idT,
typename InterruptT>
388 thread::cancelGroupExecution();
394 template<
typename Gr
idT,
typename InterruptT>
395 template<
typename MaskT>
402 this->normalize1<math::FIRST_BIAS , MaskT>(mask);
break;
404 this->normalize1<math::SECOND_BIAS, MaskT>(mask);
break;
406 this->normalize1<math::THIRD_BIAS, MaskT>(mask);
break;
408 this->normalize1<math::WENO5_BIAS, MaskT>(mask);
break;
410 this->normalize1<math::HJWENO5_BIAS, MaskT>(mask);
break;
417 template<
typename Gr
idT,
typename InterruptT>
418 template<math::BiasedGradientScheme SpatialScheme,
typename MaskT>
425 this->normalize2<SpatialScheme, math::TVD_RK1, MaskT>(mask);
break;
427 this->normalize2<SpatialScheme, math::TVD_RK2, MaskT>(mask);
break;
429 this->normalize2<SpatialScheme, math::TVD_RK3, MaskT>(mask);
break;
436 template<
typename Gr
idT,
typename InterruptT>
444 Normalizer<SpatialScheme, TemporalScheme, MaskT> tmp(*
this, mask);
452 template<
typename Gr
idT,
typename InterruptT>
453 template<lstrack::TrimMode Trimming>
458 if (Trimming != TrimMode::kNone) {
459 const int grainSize = mTracker.getGrainSize();
460 const LeafRange range = mTracker.leafs().leafRange(grainSize);
463 tbb::parallel_for(range, *
this);
473 template<
typename Gr
idT,
typename InterruptT>
474 template<lstrack::TrimMode Trimming>
479 const ValueType gamma = mTracker.mGrid->background();
482 for (
auto leafIter = range.begin(); leafIter; ++leafIter) {
483 auto& leaf = *leafIter;
484 for (
auto iter = leaf.beginValueOn(); iter; ++iter) {
485 const auto val = *iter;
487 case TrimMode::kNone:
489 case TrimMode::kInterior:
490 if (val <= -gamma) { leaf.setValueOff(iter.pos(), -gamma); }
492 case TrimMode::kExterior:
493 if (val >= gamma) { leaf.setValueOff(iter.pos(), gamma); }
497 leaf.setValueOff(iter.pos(), -gamma);
498 }
else if (val >= gamma) {
499 leaf.setValueOff(iter.pos(), gamma);
511 template<
typename Gr
idT,
typename InterruptT>
528 template<
typename Gr
idT,
typename InterruptT>
537 namespace ph = std::placeholders;
540 mTracker.mLeafs->rebuildAuxBuffers(TemporalScheme ==
math::TVD_RK3 ? 2 : 1);
542 for (
int n=0, e=mTracker.getNormCount(); n < e; ++n) {
545 switch(TemporalScheme) {
549 mTask = std::bind(&Normalizer::euler01, ph::_1, ph::_2);
552 this->cook(
"Normalizing level set using TVD_RK1", 1);
557 mTask = std::bind(&Normalizer::euler01, ph::_1, ph::_2);
560 this->cook(
"Normalizing level set using TVD_RK1 (step 1 of 2)", 1);
564 mTask = std::bind(&Normalizer::euler12, ph::_1, ph::_2);
567 this->cook(
"Normalizing level set using TVD_RK1 (step 2 of 2)", 1);
572 mTask = std::bind(&Normalizer::euler01, ph::_1, ph::_2);
575 this->cook(
"Normalizing level set using TVD_RK3 (step 1 of 3)", 1);
579 mTask = std::bind(&Normalizer::euler34, ph::_1, ph::_2);
582 this->cook(
"Normalizing level set using TVD_RK3 (step 2 of 3)", 2);
586 mTask = std::bind(&Normalizer::euler13, ph::_1, ph::_2);
589 this->cook(
"Normalizing level set using TVD_RK3 (step 3 of 3)", 2);
597 mTracker.mLeafs->removeAuxBuffers();
602 template<
typename Gr
idT,
typename InterruptT>
609 cook(
const char* msg,
int swapBuffer)
613 const int grainSize = mTracker.getGrainSize();
614 const LeafRange range = mTracker.leafs().leafRange(grainSize);
616 grainSize>0 ? tbb::parallel_for(range, *
this) : (*this)(range);
618 mTracker.leafs().swapLeafBuffer(swapBuffer, grainSize==0);
620 mTracker.endInterrupter();
623 template<
typename Gr
idT,
typename InterruptT>
627 template <
int Nominator,
int Denominator>
637 const ValueType normSqGradPhi = GradientT::result(stencil);
638 const ValueType phi0 = stencil.getValue();
641 v = phi0 - mDt * v * (
math::Sqrt(normSqGradPhi) * mInvDx - 1.0f);
642 result[n] = Nominator ? alpha * phi[n] + beta * v : v;
645 template<
typename Gr
idT,
typename InterruptT>
649 template <
int Nominator,
int Denominator>
657 StencilT stencil(mTracker.grid());
659 for (
typename LeafRange::Iterator leafIter = range.begin(); leafIter; ++leafIter) {
660 const ValueType* phi = leafIter.buffer(phiBuffer).data();
661 ValueType* result = leafIter.buffer(resultBuffer).data();
662 if (mMask ==
nullptr) {
663 for (
auto iter = leafIter->cbeginValueOn(); iter; ++iter) {
664 stencil.moveTo(iter);
665 this->eval<Nominator, Denominator>(stencil, phi, result, iter.pos());
667 }
else if (
const MaskLeafT* mask = mMask->probeLeaf(leafIter->origin())) {
668 const ValueType* phi0 = leafIter->buffer().data();
669 for (MaskIterT iter = mask->cbeginValueOn(); iter; ++iter) {
670 const Index i = iter.pos();
671 stencil.moveTo(iter.getCoord(), phi0[i]);
672 this->eval<Nominator, Denominator>(stencil, phi, result, i);
684 #ifdef OPENVDB_USE_EXPLICIT_INSTANTIATION 686 #ifdef OPENVDB_INSTANTIATE_LEVELSETTRACKER 693 #endif // OPENVDB_USE_EXPLICIT_INSTANTIATION 700 #endif // OPENVDB_TOOLS_LEVEL_SET_TRACKER_HAS_BEEN_INCLUDED void trim(std::string &s)
Definition: Name.h:83
Type Pow2(Type x)
Return x2.
Definition: Math.h:548
#define OPENVDB_THROW(exception, message)
Definition: Exceptions.h:74
General-purpose arithmetic and comparison routines, most of which accept arbitrary value types (or at...
Definition: Operators.h:230
BiasedGradientScheme
Biased Gradients are limited to non-centered differences.
Definition: FiniteDifference.h:164
float RoundDown(float x)
Return x rounded down to the nearest integer.
Definition: Math.h:803
Efficient multi-threaded replacement of the background values in tree.
Index32 Index
Definition: Types.h:54
bool wasInterrupted(T *i, int percent=-1)
Definition: NullInterrupter.h:49
Defined various multi-threaded utility functions for trees.
Definition: Exceptions.h:65
static const Real LEVEL_SET_HALF_WIDTH
Definition: Types.h:461
TemporalIntegrationScheme
Temporal integration schemes.
Definition: FiniteDifference.h:233
Definition: FiniteDifference.h:165
Definition: FiniteDifference.h:169
Implementation of morphological dilation and erosion.
Definition: Exceptions.h:13
static T value()
Definition: Math.h:148
#define OPENVDB_INSTANTIATE_CLASS
Definition: version.h.in:158
ValueAccessors are designed to help accelerate accesses into the OpenVDB Tree structures by storing c...
Definition: FiniteDifference.h:168
Definition: FiniteDifference.h:237
Definition: FiniteDifference.h:236
Definition: FiniteDifference.h:235
Definition: FiniteDifference.h:166
Definition: FiniteDifference.h:170
Tag dispatch class that distinguishes topology copy constructors from deep copy constructors.
Definition: Types.h:683
A LeafManager manages a linear array of pointers to a given tree's leaf nodes, as well as optional au...
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h.in:121
float Sqrt(float x)
Return the square root of a floating-point value.
Definition: Math.h:761
Definition: Operators.h:129
Definition: FiniteDifference.h:234
Definition: FiniteDifference.h:167
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h.in:218
Definition: Exceptions.h:63