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) {}
151 Advect(
const Advect& other);
153 virtual ~Advect() {
if (mIsMaster) this->clearField(); }
160 if (mTask) mTask(const_cast<Advect*>(
this), r);
161 else OPENVDB_THROW(ValueError,
"task is undefined - don\'t call this method directly");
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);
231 OPENVDB_THROW(ValueError,
"Spatial difference scheme not supported!");
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);
250 OPENVDB_THROW(ValueError,
"Temporal integration scheme not supported!");
256 template<
typename Gr
idT,
typename FieldT,
typename InterruptT>
257 template<math::BiasedGradientScheme SpatialScheme, math::TemporalIntegrationScheme TemporalScheme>
259 LevelSetAdvection<GridT, FieldT, InterruptT>::advect2(
ValueType time0,
ValueType time1)
261 const math::Transform&
trans = mTracker.grid().transform();
263 return this->advect3<SpatialScheme, TemporalScheme, math::UniformScaleMap>(time0, time1);
265 return this->advect3<SpatialScheme, TemporalScheme, math::UniformScaleTranslateMap>(
268 return this->advect3<SpatialScheme, TemporalScheme, math::UnitaryMap >(time0, time1);
270 return this->advect3<SpatialScheme, TemporalScheme, math::TranslationMap>(time0, time1);
278 template<
typename Gr
idT,
typename FieldT,
typename InterruptT>
284 LevelSetAdvection<GridT, FieldT, InterruptT>::advect3(
ValueType time0,
ValueType time1)
286 Advect<MapT, SpatialScheme, TemporalScheme> tmp(*
this);
287 return tmp.advect(time0, time1);
294 template<
typename Gr
idT,
typename FieldT,
typename InterruptT>
300 LevelSetAdvection<GridT, FieldT, InterruptT>::
301 Advect<MapT, SpatialScheme, TemporalScheme>::
302 Advect(LevelSetAdvection& parent)
306 , mMap(parent.mTracker.grid().
transform().template constMap<MapT>().
get())
313 template<
typename Gr
idT,
typename FieldT,
typename InterruptT>
319 LevelSetAdvection<GridT, FieldT, InterruptT>::
320 Advect<MapT, SpatialScheme, TemporalScheme>::
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()) {
351 mParent.mTracker.leafs().rebuildAuxBuffers(TemporalScheme ==
math::TVD_RK3 ? 2 : 1);
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);
405 OPENVDB_THROW(ValueError,
"Temporal integration scheme not supported!");
409 time0 += isForward ? dt : -dt;
411 mParent.mTracker.leafs().removeAuxBuffers();
414 mParent.mTracker.track();
420 template<
typename Gr
idT,
typename FieldT,
typename InterruptT>
426 LevelSetAdvection<GridT, FieldT, InterruptT>::
427 Advect<MapT, SpatialScheme, TemporalScheme>::
430 namespace ph = std::placeholders;
432 const int grainSize = mParent.mTracker.getGrainSize();
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());
446 mVelocity =
new VectorType[ voxelCount ];
447 this->cook(
"Sampling advection field");
451 VectorType*
v = mVelocity;
452 for (
size_t i = 0; i < voxelCount; ++i, ++
v) {
459 TemporalScheme == math::
TVD_RK2 ? ValueType(0.9) :
460 ValueType(1.0))/math::
Sqrt(ValueType(3.0));
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>
473 LevelSetAdvection<GridT, FieldT, InterruptT>::
474 Advect<MapT, SpatialScheme, TemporalScheme>::
475 sample(
const LeafRange&
range, ValueType time0, ValueType time1)
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>
501 LevelSetAdvection<GridT, FieldT, InterruptT>::
502 Advect<MapT, SpatialScheme, TemporalScheme>::
512 template<
typename Gr
idT,
typename FieldT,
typename InterruptT>
518 LevelSetAdvection<GridT, FieldT, InterruptT>::
519 Advect<MapT, SpatialScheme, TemporalScheme>::
520 cook(
const char* msg,
size_t swapBuffer)
522 mParent.mTracker.startInterrupter( msg );
524 const int grainSize = mParent.mTracker.getGrainSize();
525 const LeafRange range = mParent.mTracker.leafs().leafRange(grainSize);
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>
544 LevelSetAdvection<GridT, FieldT, InterruptT>::
545 Advect<MapT, SpatialScheme, TemporalScheme>::
546 euler(
const LeafRange& range, ValueType dt,
Index phiBuffer,
Index resultBuffer)
548 using SchemeT = math::BIAS_SCHEME<SpatialScheme>;
549 using StencilT =
typename SchemeT::template ISStencil<GridType>::StencilType;
550 using VoxelIterT =
typename LeafType::ValueOnCIter;
551 using GradT = math::GradientBiased<MapT, SpatialScheme>;
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();
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
void parallel_for(int64_t start, int64_t end, std::function< void(int64_t index)> &&task, parallel_options opt=parallel_options(0, Split_Y, 1))
TemporalIntegrationScheme
Temporal integration schemes.
GLsizei const GLfloat * value
GLboolean GLboolean GLboolean GLboolean a
#define OPENVDB_USE_VERSION_NAMESPACE
**But if you need a result
BiasedGradientScheme
Biased Gradients are limited to non-centered differences.
#define OPENVDB_INSTANTIATE_CLASS
Coord Abs(const Coord &xyz)
GA_API const UT_StringHolder trans
float Sqrt(float x)
Return the square root of a floating-point value.
auto get(const UT_ARTIterator< T > &it) -> decltype(it.key())
Performs multi-threaded interface tracking of narrow band level sets. This is the building-block for ...
bool isApproxZero(const Type &x)
Return true if x is equal to zero to within the default floating-point comparison tolerance...
GA_API const UT_StringHolder transform
static Name mapType()
Return UnitaryMap.
Defines two simple wrapper classes for advection velocity fields as well as VelocitySampler and Veloc...
const Type & Min(const Type &a, const Type &b)
Return the minimum of two values.
GLint GLfloat GLint stencil
__hostdev__ float Sqrt(float x)
Return the square root of a floating-point value.
GA_API const UT_StringHolder Alpha
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
const Type & Max(const Type &a, const Type &b)
Return the maximum of two values.
bool isZero(const Type &x)
Return true if x is exactly equal to zero.
#define OPENVDB_THROW(exception, message)