39 #ifndef OPENVDB_TOOLS_LEVEL_SET_ADVECT_HAS_BEEN_INCLUDED 40 #define OPENVDB_TOOLS_LEVEL_SET_ADVECT_HAS_BEEN_INCLUDED 42 #include <tbb/parallel_for.h> 43 #include <tbb/parallel_reduce.h> 44 #include <openvdb/Platform.h> 47 #include <openvdb/math/FiniteDifference.h> 48 #include <boost/math/constants/constants.hpp> 49 #include <openvdb/util/CpuTimer.h> 98 template<
typename GridT,
99 typename FieldT = EnrightField<typename GridT::ValueType>,
100 typename InterruptT = util::NullInterrupter>
114 mTracker(grid, interrupt), mField(field),
116 mTemporalScheme(math::
TVD_RK2) {}
157 size_t advect(ValueType time0, ValueType time1);
173 Advect(
const Advect& other);
175 virtual ~Advect() {
if (mIsMaster) this->clearField(); }
178 size_t advect(ValueType time0, ValueType time1);
180 void operator()(
const LeafRange& r)
const 182 if (mTask) mTask(const_cast<Advect*>(
this), r);
186 void cook(
const char* msg,
size_t swapBuffer = 0);
188 typename GridT::ValueType sampleField(ValueType time0, ValueType time1);
189 template <
bool Aligned>
void sample(
const LeafRange& r, ValueType t0, ValueType t1);
190 inline void sampleXformed(
const LeafRange& r, ValueType t0, ValueType t1)
192 this->sample<false>(r, t0, t1);
194 inline void sampleAligned(
const LeafRange& r, ValueType t0, ValueType t1)
196 this->sample<true>(r, t0, t1);
201 template <
int Nominator,
int Denominator>
202 void euler(
const LeafRange&, ValueType,
Index,
Index);
203 inline void euler01(
const LeafRange& r, ValueType t) {this->euler<0,1>(r, t, 0, 1);}
204 inline void euler12(
const LeafRange& r, ValueType t) {this->euler<1,2>(r, t, 1, 1);}
205 inline void euler34(
const LeafRange& r, ValueType t) {this->euler<3,4>(r, t, 1, 2);}
206 inline void euler13(
const LeafRange& r, ValueType t) {this->euler<1,3>(r, t, 1, 2);}
209 VectorType* mVelocity;
212 typename boost::function<void (Advect*, const LeafRange&)> mTask;
213 const bool mIsMaster;
216 template<math::BiasedGradientScheme SpatialScheme>
217 size_t advect1(ValueType time0, ValueType time1);
221 size_t advect2(ValueType time0, ValueType time1);
226 size_t advect3(ValueType time0, ValueType time1);
236 template<
typename Gr
idT,
typename FieldT,
typename InterruptT>
240 switch (mSpatialScheme) {
242 return this->advect1<math::FIRST_BIAS >(time0, time1);
244 return this->advect1<math::SECOND_BIAS >(time0, time1);
246 return this->advect1<math::THIRD_BIAS >(time0, time1);
248 return this->advect1<math::WENO5_BIAS >(time0, time1);
250 return this->advect1<math::HJWENO5_BIAS>(time0, time1);
257 template<
typename Gr
idT,
typename FieldT,
typename InterruptT>
258 template<math::BiasedGradientScheme SpatialScheme>
262 switch (mTemporalScheme) {
264 return this->advect2<SpatialScheme, math::TVD_RK1>(time0, time1);
266 return this->advect2<SpatialScheme, math::TVD_RK2>(time0, time1);
268 return this->advect2<SpatialScheme, math::TVD_RK3>(time0, time1);
275 template<
typename Gr
idT,
typename FieldT,
typename InterruptT>
282 if (trans.
mapType() == math::UniformScaleMap::mapType()) {
283 return this->advect3<SpatialScheme, TemporalScheme, math::UniformScaleMap>(time0, time1);
284 }
else if (trans.
mapType() == math::UniformScaleTranslateMap::mapType()) {
285 return this->advect3<SpatialScheme, TemporalScheme, math::UniformScaleTranslateMap>(time0, time1);
286 }
else if (trans.
mapType() == math::UnitaryMap::mapType()) {
287 return this->advect3<SpatialScheme, TemporalScheme, math::UnitaryMap >(time0, time1);
288 }
else if (trans.
mapType() == math::TranslationMap::mapType()) {
289 return this->advect3<SpatialScheme, TemporalScheme, math::TranslationMap>(time0, time1);
296 template<
typename Gr
idT,
typename FieldT,
typename InterruptT>
303 Advect<MapT, SpatialScheme, TemporalScheme> tmp(*
this);
304 return tmp.advect(time0, time1);
311 template<
typename Gr
idT,
typename FieldT,
typename InterruptT>
321 , mMap(parent.mTracker.
grid().transform().template constMap<MapT>().
get())
327 template<
typename Gr
idT,
typename FieldT,
typename InterruptT>
333 Advect(
const Advect& other)
334 : mParent(other.mParent)
335 , mVelocity(other.mVelocity)
336 , mOffsets(other.mOffsets)
343 template<
typename Gr
idT,
typename FieldT,
typename InterruptT>
354 const bool isForward = time0 < time1;
355 while ((isForward ? time0<time1 : time0>time1) && mParent.mTracker.checkInterrupter()) {
361 const ValueType dt = this->sampleField(time0, time1);
365 switch(TemporalScheme) {
369 mTask = boost::bind(&Advect::euler01, _1, _2, dt);
372 this->cook(
"Advecting level set using TVD_RK1", 1);
377 mTask = boost::bind(&Advect::euler01, _1, _2, dt);
380 this->cook(
"Advecting level set using TVD_RK1 (step 1 of 2)", 1);
384 mTask = boost::bind(&Advect::euler12, _1, _2, dt);
387 this->cook(
"Advecting level set using TVD_RK1 (step 2 of 2)", 1);
392 mTask = boost::bind(&Advect::euler01, _1, _2, dt);
395 this->cook(
"Advecting level set using TVD_RK3 (step 1 of 3)", 1);
399 mTask = boost::bind(&Advect::euler34, _1, _2, dt);
402 this->cook(
"Advecting level set using TVD_RK3 (step 2 of 3)", 2);
406 mTask = boost::bind(&Advect::euler13, _1, _2, dt);
409 this->cook(
"Advecting level set using TVD_RK3 (step 3 of 3)", 2);
416 time0 += isForward ? dt : -dt;
418 mParent.mTracker.leafs().removeAuxBuffers();
421 mParent.mTracker.track();
426 template<
typename Gr
idT,
typename FieldT,
typename InterruptT>
429 inline typename GridT::ValueType
435 const size_t leafCount = mParent.mTracker.leafs().leafCount();
439 size_t size=0, voxelCount=mParent.mTracker.leafs().getPreFixSum(mOffsets, size, grainSize);
442 if (mParent.mField.transform() == mParent.mTracker.grid().transform()) {
443 mTask = boost::bind(&Advect::sampleAligned, _1, _2, time0, time1);
445 mTask = boost::bind(&Advect::sampleXformed, _1, _2, time0, time1);
447 assert(voxelCount == mParent.mTracker.grid().activeVoxelCount());
449 this->cook(
"Sampling advection field");
454 for (
size_t i=0; i<voxelCount; ++i, ++v) maxAbsV =
math::Max(maxAbsV,
ValueType(v->lengthSqr()));
458 #ifndef _MSC_VER // Visual C++ doesn't guarantee thread-safe initialization of local statics 464 const ValueType dt =
math::Abs(time1 - time0), dx = mParent.mTracker.voxelSize();
468 template<
typename Gr
idT,
typename FieldT,
typename InterruptT>
471 template <
bool Aligned>
477 const bool isForward = time0 < time1;
478 typedef typename LeafType::ValueOnCIter VoxelIterT;
479 const MapT& map = *mMap;
480 const FieldT field( mParent.mField );
481 mParent.mTracker.checkInterrupter();
483 VectorType* vel = mVelocity + mOffsets[ leafIter.pos() ];
484 for (VoxelIterT iter = leafIter->cbeginValueOn(); iter; ++iter, ++vel) {
485 const VectorType v = Aligned ? field(iter.getCoord(), time0) :
486 field(map.applyMap(iter.getCoord().asVec3d()), time0);
487 *vel = isForward ? v : -v;
492 template<
typename Gr
idT,
typename FieldT,
typename InterruptT>
506 template<
typename Gr
idT,
typename FieldT,
typename InterruptT>
512 cook(
const char* msg,
size_t swapBuffer)
516 const int grainSize = mParent.mTracker.getGrainSize();
517 const LeafRange range = mParent.mTracker.leafs().leafRange(grainSize);
519 grainSize == 0 ? (*this)(range) : tbb::parallel_for(range, *
this);
521 mParent.mTracker.leafs().swapLeafBuffer(swapBuffer, grainSize == 0);
523 mParent.mTracker.endInterrupter();
528 template<
typename Gr
idT,
typename FieldT,
typename InterruptT>
531 template <
int Nominator,
int Denominator>
538 typedef typename SchemeT::template ISStencil<GridType>::StencilType StencilT;
539 typedef typename LeafType::ValueOnCIter VoxelIterT;
545 mParent.mTracker.checkInterrupter();
546 const MapT& map = *mMap;
547 StencilT stencil(mParent.mTracker.grid());
549 const VectorType* vel = mVelocity + mOffsets[ leafIter.pos() ];
550 const ValueType* phi = leafIter.buffer(phiBuffer).data();
551 ValueType* result = leafIter.buffer(resultBuffer).data();
552 for (VoxelIterT voxelIter = leafIter->cbeginValueOn(); voxelIter; ++voxelIter, ++vel) {
553 const Index i = voxelIter.pos();
554 stencil.moveTo(voxelIter);
555 const ValueType a = stencil.getValue() - dt * vel->dot(GradT::result(map, stencil, *vel));
556 result[i] = Nominator ? Alpha * phi[i] + Beta * a : a;
565 #endif // OPENVDB_TOOLS_LEVEL_SET_ADVECT_HAS_BEEN_INCLUDED
Coord Abs(const Coord &xyz)
Definition: Coord.h:247
Performs multi-threaded interface tracking of narrow band level sets. This is the building-block for ...
Definition: LeafManager.h:132
Defines two simple wrapper classes for advection velocity fields as well as VelocitySampler and Veloc...
void rebuildAuxBuffers(size_t auxBuffersPerLeaf, bool serial=false)
Change the number of auxiliary buffers.
Definition: LeafManager.h:314
Delta for small floating-point offsets.
Definition: Math.h:132
float Sqrt(float x)
Return the square root of a floating-point value.
Definition: Math.h:727
Definition: FiniteDifference.h:195
TemporalIntegrationScheme
Temporal integration schemes.
Definition: FiniteDifference.h:261
const Type & Min(const Type &a, const Type &b)
Return the minimum of two values.
Definition: Math.h:622
#define OPENVDB_THROW(exception, message)
Definition: Exceptions.h:97
Definition: FiniteDifference.h:194
Definition: FiniteDifference.h:263
bool isZero(const Type &x)
Return true if x is exactly equal to zero.
Definition: Math.h:324
Definition: FiniteDifference.h:198
Definition: Operators.h:152
Definition: FiniteDifference.h:197
Index32 Index
Definition: Types.h:58
#define OPENVDB_VERSION_NAME
Definition: version.h:43
Biased gradient operators, defined with respect to the range-space of the map.
Definition: Operators.h:827
Definition: Exceptions.h:39
BiasedGradientScheme
Biased Gradients are limited to non-centered differences.
Definition: FiniteDifference.h:192
Definition: FiniteDifference.h:265
Iterator begin() const
Definition: LeafManager.h:188
Definition: LeafManager.h:135
Definition: Exceptions.h:88
const Type & Max(const Type &a, const Type &b)
Return the maximum of two values.
Definition: Math.h:561
bool isApproxZero(const Type &x)
Return true if x is equal to zero to within the default floating-point comparison tolerance...
Definition: Math.h:336
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:71
Definition: FiniteDifference.h:196
Definition: FiniteDifference.h:264