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> 71 template<
typename GridT,
72 typename FieldT = EnrightField<typename GridT::ValueType>,
73 typename InterruptT = util::NullInterrupter>
87 mTracker(grid, interrupt), mField(field),
89 mTemporalScheme(math::
TVD_RK2) {}
105 return mTracker.getSpatialScheme();
109 mTracker.setSpatialScheme(scheme);
113 return mTracker.getTemporalScheme();
117 mTracker.setTemporalScheme(scheme);
152 Advect(
const Advect& other);
154 virtual ~Advect() {
if (mIsMaster) this->clearField(); }
159 void operator()(
const LeafRange& r)
const 161 if (mTask) mTask(const_cast<Advect*>(
this), r);
165 void cook(
const char* msg,
size_t swapBuffer = 0);
171 this->sample<false>(r, t0, t1);
175 this->sample<true>(r, t0, t1);
180 template <
int Nominator,
int Denominator>
191 typename std::function<void (Advect*, const LeafRange&)> mTask;
192 const bool mIsMaster;
195 template<math::BiasedGradientScheme SpatialScheme>
216 template<
typename Gr
idT,
typename FieldT,
typename InterruptT>
220 switch (mSpatialScheme) {
222 return this->advect1<math::FIRST_BIAS >(time0, time1);
224 return this->advect1<math::SECOND_BIAS >(time0, time1);
226 return this->advect1<math::THIRD_BIAS >(time0, time1);
228 return this->advect1<math::WENO5_BIAS >(time0, time1);
230 return this->advect1<math::HJWENO5_BIAS>(time0, time1);
238 template<
typename Gr
idT,
typename FieldT,
typename InterruptT>
239 template<math::BiasedGradientScheme SpatialScheme>
243 switch (mTemporalScheme) {
245 return this->advect2<SpatialScheme, math::TVD_RK1>(time0, time1);
247 return this->advect2<SpatialScheme, math::TVD_RK2>(time0, time1);
249 return this->advect2<SpatialScheme, math::TVD_RK3>(time0, time1);
257 template<
typename Gr
idT,
typename FieldT,
typename InterruptT>
258 template<math::BiasedGradientScheme SpatialScheme, math::TemporalIntegrationScheme TemporalScheme>
263 if (trans.
mapType() == math::UniformScaleMap::mapType()) {
264 return this->advect3<SpatialScheme, TemporalScheme, math::UniformScaleMap>(time0, time1);
265 }
else if (trans.
mapType() == math::UniformScaleTranslateMap::mapType()) {
266 return this->advect3<SpatialScheme, TemporalScheme, math::UniformScaleTranslateMap>(
268 }
else if (trans.
mapType() == math::UnitaryMap::mapType()) {
269 return this->advect3<SpatialScheme, TemporalScheme, math::UnitaryMap >(time0, time1);
270 }
else if (trans.
mapType() == math::TranslationMap::mapType()) {
271 return this->advect3<SpatialScheme, TemporalScheme, math::TranslationMap>(time0, time1);
279 template<
typename Gr
idT,
typename FieldT,
typename InterruptT>
287 Advect<MapT, SpatialScheme, TemporalScheme> tmp(*
this);
288 return tmp.advect(time0, time1);
295 template<
typename Gr
idT,
typename FieldT,
typename InterruptT>
307 , mMap(parent.mTracker.
grid().transform().template constMap<MapT>().
get())
314 template<
typename Gr
idT,
typename FieldT,
typename InterruptT>
322 Advect(
const Advect& other)
323 : mParent(other.mParent)
324 , mVelocity(other.mVelocity)
325 , mOffsets(other.mOffsets)
333 template<
typename Gr
idT,
typename FieldT,
typename InterruptT>
343 namespace ph = std::placeholders;
348 const bool isForward = time0 < time1;
349 while ((isForward ? time0<time1 : time0>time1) && mParent.mTracker.checkInterrupter()) {
355 const ValueType dt = this->sampleField(time0, time1);
359 switch(TemporalScheme) {
363 mTask = std::bind(&Advect::euler01, ph::_1, ph::_2, dt);
366 this->cook(
"Advecting level set using TVD_RK1", 1);
371 mTask = std::bind(&Advect::euler01, ph::_1, ph::_2, dt);
374 this->cook(
"Advecting level set using TVD_RK1 (step 1 of 2)", 1);
378 mTask = std::bind(&Advect::euler12, ph::_1, ph::_2, dt);
381 this->cook(
"Advecting level set using TVD_RK1 (step 2 of 2)", 1);
386 mTask = std::bind(&Advect::euler01, ph::_1, ph::_2, dt);
389 this->cook(
"Advecting level set using TVD_RK3 (step 1 of 3)", 1);
393 mTask = std::bind(&Advect::euler34, ph::_1, ph::_2, dt);
396 this->cook(
"Advecting level set using TVD_RK3 (step 2 of 3)", 2);
400 mTask = std::bind(&Advect::euler13, ph::_1, ph::_2, dt);
403 this->cook(
"Advecting level set using TVD_RK3 (step 3 of 3)", 2);
410 time0 += isForward ? dt : -dt;
412 mParent.mTracker.leafs().removeAuxBuffers();
415 mParent.mTracker.track();
421 template<
typename Gr
idT,
typename FieldT,
typename InterruptT>
426 inline typename GridT::ValueType
431 namespace ph = std::placeholders;
434 const size_t leafCount = mParent.mTracker.leafs().leafCount();
438 size_t size=0, voxelCount=mParent.mTracker.leafs().getPrefixSum(mOffsets, size, grainSize);
441 if (mParent.mField.transform() == mParent.mTracker.grid().transform()) {
442 mTask = std::bind(&Advect::sampleAligned, ph::_1, ph::_2, time0, time1);
444 mTask = std::bind(&Advect::sampleXformed, ph::_1, ph::_2, time0, time1);
446 OPENVDB_ASSERT(voxelCount == mParent.mTracker.grid().activeVoxelCount());
448 this->cook(
"Sampling advection field");
453 for (
size_t i = 0; i < voxelCount; ++i, ++v) {
462 const ValueType dt =
math::Abs(time1 - time0), dx = mParent.mTracker.voxelSize();
467 template<
typename Gr
idT,
typename FieldT,
typename InterruptT>
472 template<
bool Aligned>
478 const bool isForward = time0 < time1;
479 using VoxelIterT =
typename LeafType::ValueOnCIter;
480 const MapT& map = *mMap;
481 const FieldT field( mParent.mField );
482 mParent.mTracker.checkInterrupter();
483 for (
typename LeafRange::Iterator leafIter = range.begin(); leafIter; ++leafIter) {
484 VectorType* vel = mVelocity + mOffsets[ leafIter.pos() ];
485 for (VoxelIterT iter = leafIter->cbeginValueOn(); iter; ++iter, ++vel) {
487 const VectorType v = Aligned ? field(iter.getCoord(), time0) :
488 field(map.applyMap(iter.getCoord().asVec3d()), time0);
489 *vel = isForward ? v : -v;
496 template<
typename Gr
idT,
typename FieldT,
typename InterruptT>
513 template<
typename Gr
idT,
typename FieldT,
typename InterruptT>
521 cook(
const char* msg,
size_t swapBuffer)
525 const int grainSize = mParent.mTracker.getGrainSize();
526 const LeafRange range = mParent.mTracker.leafs().leafRange(grainSize);
528 grainSize == 0 ? (*this)(range) : tbb::parallel_for(range, *
this);
530 mParent.mTracker.leafs().swapLeafBuffer(swapBuffer, grainSize == 0);
532 mParent.mTracker.endInterrupter();
538 template<
typename Gr
idT,
typename FieldT,
typename InterruptT>
543 template <
int Nominator,
int Denominator>
550 using StencilT =
typename SchemeT::template ISStencil<GridType>::StencilType;
551 using VoxelIterT =
typename LeafType::ValueOnCIter;
557 mParent.mTracker.checkInterrupter();
558 const MapT& map = *mMap;
559 StencilT stencil(mParent.mTracker.grid());
560 for (
typename LeafRange::Iterator leafIter = range.begin(); leafIter; ++leafIter) {
561 const VectorType* vel = mVelocity + mOffsets[ leafIter.pos() ];
562 const ValueType* phi = leafIter.buffer(phiBuffer).data();
563 ValueType* result = leafIter.buffer(resultBuffer).data();
564 for (VoxelIterT voxelIter = leafIter->cbeginValueOn(); voxelIter; ++voxelIter, ++vel) {
565 const Index i = voxelIter.pos();
566 stencil.moveTo(voxelIter);
568 stencil.getValue() - dt * vel->dot(GradT::result(map, stencil, *vel));
569 result[i] = Nominator ? Alpha * phi[i] + Beta * a : a;
580 #ifdef OPENVDB_USE_EXPLICIT_INSTANTIATION 582 #ifdef OPENVDB_INSTANTIATE_LEVELSETADVECT 589 #endif // OPENVDB_USE_EXPLICIT_INSTANTIATION 596 #endif // OPENVDB_TOOLS_LEVEL_SET_ADVECT_HAS_BEEN_INCLUDED
Performs multi-threaded interface tracking of narrow band level sets. This is the building-block for ...
#define OPENVDB_THROW(exception, message)
Definition: Exceptions.h:74
Coord Abs(const Coord &xyz)
Definition: Coord.h:518
Defines two simple wrapper classes for advection velocity fields as well as VelocitySampler and Veloc...
BiasedGradientScheme
Biased Gradients are limited to non-centered differences.
Definition: FiniteDifference.h:164
const Type & Max(const Type &a, const Type &b)
Return the maximum of two values.
Definition: Math.h:595
Index32 Index
Definition: Types.h:54
const Type & Min(const Type &a, const Type &b)
Return the minimum of two values.
Definition: Math.h:656
Base class for interrupters.
Definition: NullInterrupter.h:25
Definition: Exceptions.h:65
bool isApproxZero(const Type &x)
Return true if x is equal to zero to within the default floating-point comparison tolerance...
Definition: Math.h:349
TemporalIntegrationScheme
Temporal integration schemes.
Definition: FiniteDifference.h:233
bool isZero(const Type &x)
Return true if x is exactly equal to zero.
Definition: Math.h:337
#define OPENVDB_ASSERT(X)
Definition: Assert.h:41
Definition: FiniteDifference.h:169
Definition: Exceptions.h:13
#define OPENVDB_INSTANTIATE_CLASS
Definition: version.h.in:158
Definition: FiniteDifference.h:168
Definition: FiniteDifference.h:237
Definition: FiniteDifference.h:236
Definition: FiniteDifference.h:235
Definition: FiniteDifference.h:166
Biased gradient operators, defined with respect to the range-space of the map.
Definition: Operators.h:824
Delta for small floating-point offsets.
Definition: Math.h:155
Definition: FiniteDifference.h:170
#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:167
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h.in:218