10 #ifndef OPENVDB_TOOLS_LEVEL_SET_ADVECT_HAS_BEEN_INCLUDED 11 #define OPENVDB_TOOLS_LEVEL_SET_ADVECT_HAS_BEEN_INCLUDED 13 #include <tbb/parallel_for.h> 14 #include <tbb/parallel_reduce.h> 70 template<
typename GridT,
71 typename FieldT = EnrightField<typename GridT::ValueType>,
72 typename InterruptT = util::NullInterrupter>
86 mTracker(grid, interrupt), mField(field),
88 mTemporalScheme(math::
TVD_RK2) {}
104 return mTracker.getSpatialScheme();
108 mTracker.setSpatialScheme(scheme);
112 return mTracker.getTemporalScheme();
116 mTracker.setTemporalScheme(scheme);
151 Advect(
const Advect& other);
153 virtual ~Advect() {
if (mIsMaster) this->clearField(); }
158 void operator()(
const LeafRange& r)
const 160 if (mTask) mTask(const_cast<Advect*>(
this), r);
164 void cook(
const char* msg,
size_t swapBuffer = 0);
170 this->sample<false>(r, t0, t1);
174 this->sample<true>(r, t0, t1);
179 template <
int Nominator,
int Denominator>
190 typename std::function<void (Advect*, const LeafRange&)> mTask;
191 const bool mIsMaster;
194 template<math::BiasedGradientScheme SpatialScheme>
215 template<
typename Gr
idT,
typename FieldT,
typename InterruptT>
219 switch (mSpatialScheme) {
221 return this->advect1<math::FIRST_BIAS >(time0, time1);
223 return this->advect1<math::SECOND_BIAS >(time0, time1);
225 return this->advect1<math::THIRD_BIAS >(time0, time1);
227 return this->advect1<math::WENO5_BIAS >(time0, time1);
229 return this->advect1<math::HJWENO5_BIAS>(time0, time1);
237 template<
typename Gr
idT,
typename FieldT,
typename InterruptT>
238 template<math::BiasedGradientScheme SpatialScheme>
242 switch (mTemporalScheme) {
244 return this->advect2<SpatialScheme, math::TVD_RK1>(time0, time1);
246 return this->advect2<SpatialScheme, math::TVD_RK2>(time0, time1);
248 return this->advect2<SpatialScheme, math::TVD_RK3>(time0, time1);
256 template<
typename Gr
idT,
typename FieldT,
typename InterruptT>
257 template<math::BiasedGradientScheme SpatialScheme, math::TemporalIntegrationScheme TemporalScheme>
262 if (trans.
mapType() == math::UniformScaleMap::mapType()) {
263 return this->advect3<SpatialScheme, TemporalScheme, math::UniformScaleMap>(time0, time1);
264 }
else if (trans.
mapType() == math::UniformScaleTranslateMap::mapType()) {
265 return this->advect3<SpatialScheme, TemporalScheme, math::UniformScaleTranslateMap>(
267 }
else if (trans.
mapType() == math::UnitaryMap::mapType()) {
268 return this->advect3<SpatialScheme, TemporalScheme, math::UnitaryMap >(time0, time1);
269 }
else if (trans.
mapType() == math::TranslationMap::mapType()) {
270 return this->advect3<SpatialScheme, TemporalScheme, math::TranslationMap>(time0, time1);
278 template<
typename Gr
idT,
typename FieldT,
typename InterruptT>
286 Advect<MapT, SpatialScheme, TemporalScheme> tmp(*
this);
287 return tmp.advect(time0, time1);
294 template<
typename Gr
idT,
typename FieldT,
typename InterruptT>
306 , mMap(parent.mTracker.
grid().transform().template constMap<MapT>().
get())
313 template<
typename Gr
idT,
typename FieldT,
typename InterruptT>
321 Advect(
const Advect& other)
322 : mParent(other.mParent)
323 , mVelocity(other.mVelocity)
324 , mOffsets(other.mOffsets)
332 template<
typename Gr
idT,
typename FieldT,
typename InterruptT>
342 namespace ph = std::placeholders;
347 const bool isForward = time0 < time1;
348 while ((isForward ? time0<time1 : time0>time1) && mParent.mTracker.checkInterrupter()) {
354 const ValueType dt = this->sampleField(time0, time1);
358 switch(TemporalScheme) {
362 mTask = std::bind(&Advect::euler01, ph::_1, ph::_2, dt);
365 this->cook(
"Advecting level set using TVD_RK1", 1);
370 mTask = std::bind(&Advect::euler01, ph::_1, ph::_2, dt);
373 this->cook(
"Advecting level set using TVD_RK1 (step 1 of 2)", 1);
377 mTask = std::bind(&Advect::euler12, ph::_1, ph::_2, dt);
380 this->cook(
"Advecting level set using TVD_RK1 (step 2 of 2)", 1);
385 mTask = std::bind(&Advect::euler01, ph::_1, ph::_2, dt);
388 this->cook(
"Advecting level set using TVD_RK3 (step 1 of 3)", 1);
392 mTask = std::bind(&Advect::euler34, ph::_1, ph::_2, dt);
395 this->cook(
"Advecting level set using TVD_RK3 (step 2 of 3)", 2);
399 mTask = std::bind(&Advect::euler13, ph::_1, ph::_2, dt);
402 this->cook(
"Advecting level set using TVD_RK3 (step 3 of 3)", 2);
409 time0 += isForward ? dt : -dt;
411 mParent.mTracker.leafs().removeAuxBuffers();
414 mParent.mTracker.track();
420 template<
typename Gr
idT,
typename FieldT,
typename InterruptT>
425 inline typename GridT::ValueType
430 namespace ph = std::placeholders;
433 const size_t leafCount = mParent.mTracker.leafs().leafCount();
437 size_t size=0, voxelCount=mParent.mTracker.leafs().getPrefixSum(mOffsets, size, grainSize);
440 if (mParent.mField.transform() == mParent.mTracker.grid().transform()) {
441 mTask = std::bind(&Advect::sampleAligned, ph::_1, ph::_2, time0, time1);
443 mTask = std::bind(&Advect::sampleXformed, ph::_1, ph::_2, time0, time1);
445 assert(voxelCount == mParent.mTracker.grid().activeVoxelCount());
447 this->cook(
"Sampling advection field");
452 for (
size_t i = 0; i < voxelCount; ++i, ++v) {
461 const ValueType dt =
math::Abs(time1 - time0), dx = mParent.mTracker.voxelSize();
466 template<
typename Gr
idT,
typename FieldT,
typename InterruptT>
471 template<
bool Aligned>
477 const bool isForward = time0 < time1;
478 using VoxelIterT =
typename LeafType::ValueOnCIter;
479 const MapT& map = *mMap;
480 const FieldT field( mParent.mField );
481 mParent.mTracker.checkInterrupter();
482 for (
typename LeafRange::Iterator leafIter = range.begin(); leafIter; ++leafIter) {
483 VectorType* vel = mVelocity + mOffsets[ leafIter.pos() ];
484 for (VoxelIterT iter = leafIter->cbeginValueOn(); iter; ++iter, ++vel) {
486 const VectorType v = Aligned ? field(iter.getCoord(), time0) :
487 field(map.applyMap(iter.getCoord().asVec3d()), time0);
488 *vel = isForward ? v : -v;
495 template<
typename Gr
idT,
typename FieldT,
typename InterruptT>
512 template<
typename Gr
idT,
typename FieldT,
typename InterruptT>
520 cook(
const char* msg,
size_t swapBuffer)
524 const int grainSize = mParent.mTracker.getGrainSize();
525 const LeafRange range = mParent.mTracker.leafs().leafRange(grainSize);
527 grainSize == 0 ? (*this)(range) : tbb::parallel_for(range, *
this);
529 mParent.mTracker.leafs().swapLeafBuffer(swapBuffer, grainSize == 0);
531 mParent.mTracker.endInterrupter();
537 template<
typename Gr
idT,
typename FieldT,
typename InterruptT>
542 template <
int Nominator,
int Denominator>
549 using StencilT =
typename SchemeT::template ISStencil<GridType>::StencilType;
550 using VoxelIterT =
typename LeafType::ValueOnCIter;
556 mParent.mTracker.checkInterrupter();
557 const MapT& map = *mMap;
558 StencilT stencil(mParent.mTracker.grid());
559 for (
typename LeafRange::Iterator leafIter = range.begin(); leafIter; ++leafIter) {
560 const VectorType* vel = mVelocity + mOffsets[ leafIter.pos() ];
561 const ValueType* phi = leafIter.buffer(phiBuffer).data();
562 ValueType* result = leafIter.buffer(resultBuffer).data();
563 for (VoxelIterT voxelIter = leafIter->cbeginValueOn(); voxelIter; ++voxelIter, ++vel) {
564 const Index i = voxelIter.pos();
565 stencil.moveTo(voxelIter);
567 stencil.getValue() - dt * vel->dot(GradT::result(map, stencil, *vel));
568 result[i] = Nominator ? Alpha * phi[i] + Beta * a : a;
579 #ifdef OPENVDB_USE_EXPLICIT_INSTANTIATION 581 #ifdef OPENVDB_INSTANTIATE_LEVELSETADVECT 588 #endif // OPENVDB_USE_EXPLICIT_INSTANTIATION 595 #endif // OPENVDB_TOOLS_LEVEL_SET_ADVECT_HAS_BEEN_INCLUDED
Delta for small floating-point offsets.
Definition: Math.h:154
Performs multi-threaded interface tracking of narrow band level sets. This is the building-block for ...
Definition: FiniteDifference.h:170
#define OPENVDB_THROW(exception, message)
Definition: Exceptions.h:74
Defines two simple wrapper classes for advection velocity fields as well as VelocitySampler and Veloc...
Base class for interrupters.
Definition: NullInterrupter.h:25
Definition: FiniteDifference.h:171
bool isApproxZero(const Type &x)
Return true if x is equal to zero to within the default floating-point comparison tolerance...
Definition: Math.h:350
Definition: FiniteDifference.h:167
TemporalIntegrationScheme
Temporal integration schemes.
Definition: FiniteDifference.h:234
Definition: Exceptions.h:65
Biased gradient operators, defined with respect to the range-space of the map.
Definition: Operators.h:824
float Sqrt(float x)
Return the square root of a floating-point value.
Definition: Math.h:764
Definition: FiniteDifference.h:236
Definition: Exceptions.h:13
Definition: Operators.h:129
const Type & Max(const Type &a, const Type &b)
Return the maximum of two values.
Definition: Math.h:598
const Type & Min(const Type &a, const Type &b)
Return the minimum of two values.
Definition: Math.h:659
Definition: FiniteDifference.h:238
Index32 Index
Definition: Types.h:54
#define OPENVDB_INSTANTIATE_CLASS
Definition: version.h.in:143
Definition: FiniteDifference.h:237
Definition: FiniteDifference.h:168
Coord Abs(const Coord &xyz)
Definition: Coord.h:514
Definition: FiniteDifference.h:169
bool isZero(const Type &x)
Return true if x is exactly equal to zero.
Definition: Math.h:338
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h.in:116
BiasedGradientScheme
Biased Gradients are limited to non-centered differences.
Definition: FiniteDifference.h:165
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h.in:202