HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
LevelSetAdvect.h
Go to the documentation of this file.
1 ///////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (c) 2012-2018 DreamWorks Animation LLC
4 //
5 // All rights reserved. This software is distributed under the
6 // Mozilla Public License 2.0 ( http://www.mozilla.org/MPL/2.0/ )
7 //
8 // Redistributions of source code must retain the above copyright
9 // and license notice and the following restrictions and disclaimer.
10 //
11 // * Neither the name of DreamWorks Animation nor the names of
12 // its contributors may be used to endorse or promote products derived
13 // from this software without specific prior written permission.
14 //
15 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY INDIRECT, INCIDENTAL,
20 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 // IN NO EVENT SHALL THE COPYRIGHT HOLDERS' AND CONTRIBUTORS' AGGREGATE
27 // LIABILITY FOR ALL CLAIMS REGARDLESS OF THEIR BASIS EXCEED US$250.00.
28 //
29 ///////////////////////////////////////////////////////////////////////////
30 
31 /// @author Ken Museth
32 ///
33 /// @file tools/LevelSetAdvect.h
34 ///
35 /// @brief Hyperbolic advection of narrow-band level sets
36 
37 #ifndef OPENVDB_TOOLS_LEVEL_SET_ADVECT_HAS_BEEN_INCLUDED
38 #define OPENVDB_TOOLS_LEVEL_SET_ADVECT_HAS_BEEN_INCLUDED
39 
40 #include <tbb/parallel_for.h>
41 #include <tbb/parallel_reduce.h>
42 #include <openvdb/Platform.h>
43 #include "LevelSetTracker.h"
44 #include "VelocityFields.h" // for EnrightField
46 //#include <openvdb/util/CpuTimer.h>
47 #include <functional>
48 
49 
50 namespace openvdb {
52 namespace OPENVDB_VERSION_NAME {
53 namespace tools {
54 
55 /// @brief Hyperbolic advection of narrow-band level sets in an
56 /// external velocity field
57 ///
58 /// The @c FieldType template argument below refers to any functor
59 /// with the following interface (see tools/VelocityFields.h
60 /// for examples):
61 ///
62 /// @code
63 /// class VelocityField {
64 /// ...
65 /// public:
66 /// openvdb::VectorType operator() (const openvdb::Coord& xyz, ValueType time) const;
67 /// ...
68 /// };
69 /// @endcode
70 ///
71 /// @note The functor method returns the velocity field at coordinate
72 /// position xyz of the advection grid, and for the specified
73 /// time. Note that since the velocity is returned in the local
74 /// coordinate space of the grid that is being advected, the functor
75 /// typically depends on the transformation of that grid. This design
76 /// is chosen for performance reasons. Finally we will assume that the
77 /// functor method is NOT threadsafe (typically uses a ValueAccessor)
78 /// and that its lightweight enough that we can copy it per thread.
79 ///
80 /// The @c InterruptType template argument below refers to any class
81 /// with the following interface:
82 /// @code
83 /// class Interrupter {
84 /// ...
85 /// public:
86 /// void start(const char* name = nullptr) // called when computations begin
87 /// void end() // called when computations end
88 /// bool wasInterrupted(int percent=-1) // return true to break computation
89 ///};
90 /// @endcode
91 ///
92 /// @note If no template argument is provided for this InterruptType
93 /// the util::NullInterrupter is used which implies that all
94 /// interrupter calls are no-ops (i.e. incurs no computational overhead).
95 ///
96 
97 template<typename GridT,
98  typename FieldT = EnrightField<typename GridT::ValueType>,
99  typename InterruptT = util::NullInterrupter>
101 {
102 public:
103  using GridType = GridT;
105  using LeafRange = typename TrackerT::LeafRange;
106  using LeafType = typename TrackerT::LeafType;
108  using ValueType = typename TrackerT::ValueType;
109  using VectorType = typename FieldT::VectorType;
110 
111  /// Main constructor
112  LevelSetAdvection(GridT& grid, const FieldT& field, InterruptT* interrupt = nullptr):
113  mTracker(grid, interrupt), mField(field),
114  mSpatialScheme(math::HJWENO5_BIAS),
115  mTemporalScheme(math::TVD_RK2) {}
116 
117  virtual ~LevelSetAdvection() {}
118 
119  /// @brief Return the spatial finite difference scheme
120  math::BiasedGradientScheme getSpatialScheme() const { return mSpatialScheme; }
121  /// @brief Set the spatial finite difference scheme
122  void setSpatialScheme(math::BiasedGradientScheme scheme) { mSpatialScheme = scheme; }
123 
124  /// @brief Return the temporal integration scheme
125  math::TemporalIntegrationScheme getTemporalScheme() const { return mTemporalScheme; }
126  /// @brief Set the spatial finite difference scheme
127  void setTemporalScheme(math::TemporalIntegrationScheme scheme) { mTemporalScheme = scheme; }
128 
129  /// @brief Return the spatial finite difference scheme
131  return mTracker.getSpatialScheme();
132  }
133  /// @brief Set the spatial finite difference scheme
135  mTracker.setSpatialScheme(scheme);
136  }
137  /// @brief Return the temporal integration scheme
139  return mTracker.getTemporalScheme();
140  }
141  /// @brief Set the spatial finite difference scheme
143  mTracker.setTemporalScheme(scheme);
144  }
145 
146  /// @brief Return The number of normalizations performed per track or
147  /// normalize call.
148  int getNormCount() const { return mTracker.getNormCount(); }
149  /// @brief Set the number of normalizations performed per track or
150  /// normalize call.
151  void setNormCount(int n) { mTracker.setNormCount(n); }
152 
153  /// @brief Return the grain-size used for multi-threading
154  int getGrainSize() const { return mTracker.getGrainSize(); }
155  /// @brief Set the grain-size used for multi-threading.
156  /// @note A grain size of 0 or less disables multi-threading!
157  void setGrainSize(int grainsize) { mTracker.setGrainSize(grainsize); }
158 
159  /// Advect the level set from its current time, time0, to its
160  /// final time, time1. If time0>time1 backward advection is performed.
161  ///
162  /// @return number of CFL iterations used to advect from time0 to time1
163  size_t advect(ValueType time0, ValueType time1);
164 
165 private:
166  // disallow copy construction and copy by assinment!
167  LevelSetAdvection(const LevelSetAdvection&);// not implemented
168  LevelSetAdvection& operator=(const LevelSetAdvection&);// not implemented
169 
170  // This templated private struct implements all the level set magic.
171  template<typename MapT, math::BiasedGradientScheme SpatialScheme,
172  math::TemporalIntegrationScheme TemporalScheme>
173  struct Advect
174  {
175  /// Main constructor
176  Advect(LevelSetAdvection& parent);
177  /// Shallow copy constructor called by tbb::parallel_for() threads
178  Advect(const Advect& other);
179  /// Destructor
180  virtual ~Advect() { if (mIsMaster) this->clearField(); }
181  /// Advect the level set from its current time, time0, to its final time, time1.
182  /// @return number of CFL iterations
183  size_t advect(ValueType time0, ValueType time1);
184  /// Used internally by tbb::parallel_for()
185  void operator()(const LeafRange& r) const
186  {
187  if (mTask) mTask(const_cast<Advect*>(this), r);
188  else OPENVDB_THROW(ValueError, "task is undefined - don\'t call this method directly");
189  }
190  /// method calling tbb
191  void cook(const char* msg, size_t swapBuffer = 0);
192  /// Sample field and return the CFL time step
193  typename GridT::ValueType sampleField(ValueType time0, ValueType time1);
194  template <bool Aligned> void sample(const LeafRange& r, ValueType t0, ValueType t1);
195  inline void sampleXformed(const LeafRange& r, ValueType t0, ValueType t1)
196  {
197  this->sample<false>(r, t0, t1);
198  }
199  inline void sampleAligned(const LeafRange& r, ValueType t0, ValueType t1)
200  {
201  this->sample<true>(r, t0, t1);
202  }
203  void clearField();
204  // Convex combination of Phi and a forward Euler advection steps:
205  // Phi(result) = alpha * Phi(phi) + (1-alpha) * (Phi(0) - dt * Speed(speed)*|Grad[Phi(0)]|);
206  template <int Nominator, int Denominator>
207  void euler(const LeafRange&, ValueType, Index, Index);
208  inline void euler01(const LeafRange& r, ValueType t) {this->euler<0,1>(r, t, 0, 1);}
209  inline void euler12(const LeafRange& r, ValueType t) {this->euler<1,2>(r, t, 1, 1);}
210  inline void euler34(const LeafRange& r, ValueType t) {this->euler<3,4>(r, t, 1, 2);}
211  inline void euler13(const LeafRange& r, ValueType t) {this->euler<1,3>(r, t, 1, 2);}
212 
213  LevelSetAdvection& mParent;
214  VectorType* mVelocity;
215  size_t* mOffsets;
216  const MapT* mMap;
217  typename std::function<void (Advect*, const LeafRange&)> mTask;
218  const bool mIsMaster;
219  }; // end of private Advect struct
220 
221  template<math::BiasedGradientScheme SpatialScheme>
222  size_t advect1(ValueType time0, ValueType time1);
223 
224  template<math::BiasedGradientScheme SpatialScheme,
225  math::TemporalIntegrationScheme TemporalScheme>
226  size_t advect2(ValueType time0, ValueType time1);
227 
228  template<math::BiasedGradientScheme SpatialScheme,
229  math::TemporalIntegrationScheme TemporalScheme,
230  typename MapType>
231  size_t advect3(ValueType time0, ValueType time1);
232 
233  TrackerT mTracker;
234  //each thread needs a deep copy of the field since it might contain a ValueAccessor
235  const FieldT mField;
236  math::BiasedGradientScheme mSpatialScheme;
237  math::TemporalIntegrationScheme mTemporalScheme;
238 
239 };//end of LevelSetAdvection
240 
241 
242 template<typename GridT, typename FieldT, typename InterruptT>
243 inline size_t
245 {
246  switch (mSpatialScheme) {
247  case math::FIRST_BIAS:
248  return this->advect1<math::FIRST_BIAS >(time0, time1);
249  case math::SECOND_BIAS:
250  return this->advect1<math::SECOND_BIAS >(time0, time1);
251  case math::THIRD_BIAS:
252  return this->advect1<math::THIRD_BIAS >(time0, time1);
253  case math::WENO5_BIAS:
254  return this->advect1<math::WENO5_BIAS >(time0, time1);
255  case math::HJWENO5_BIAS:
256  return this->advect1<math::HJWENO5_BIAS>(time0, time1);
257  default:
258  OPENVDB_THROW(ValueError, "Spatial difference scheme not supported!");
259  }
260  return 0;
261 }
262 
263 
264 template<typename GridT, typename FieldT, typename InterruptT>
265 template<math::BiasedGradientScheme SpatialScheme>
266 inline size_t
267 LevelSetAdvection<GridT, FieldT, InterruptT>::advect1(ValueType time0, ValueType time1)
268 {
269  switch (mTemporalScheme) {
270  case math::TVD_RK1:
271  return this->advect2<SpatialScheme, math::TVD_RK1>(time0, time1);
272  case math::TVD_RK2:
273  return this->advect2<SpatialScheme, math::TVD_RK2>(time0, time1);
274  case math::TVD_RK3:
275  return this->advect2<SpatialScheme, math::TVD_RK3>(time0, time1);
276  default:
277  OPENVDB_THROW(ValueError, "Temporal integration scheme not supported!");
278  }
279  return 0;
280 }
281 
282 
283 template<typename GridT, typename FieldT, typename InterruptT>
284 template<math::BiasedGradientScheme SpatialScheme, math::TemporalIntegrationScheme TemporalScheme>
285 inline size_t
286 LevelSetAdvection<GridT, FieldT, InterruptT>::advect2(ValueType time0, ValueType time1)
287 {
288  const math::Transform& trans = mTracker.grid().transform();
289  if (trans.mapType() == math::UniformScaleMap::mapType()) {
290  return this->advect3<SpatialScheme, TemporalScheme, math::UniformScaleMap>(time0, time1);
291  } else if (trans.mapType() == math::UniformScaleTranslateMap::mapType()) {
292  return this->advect3<SpatialScheme, TemporalScheme, math::UniformScaleTranslateMap>(
293  time0, time1);
294  } else if (trans.mapType() == math::UnitaryMap::mapType()) {
295  return this->advect3<SpatialScheme, TemporalScheme, math::UnitaryMap >(time0, time1);
296  } else if (trans.mapType() == math::TranslationMap::mapType()) {
297  return this->advect3<SpatialScheme, TemporalScheme, math::TranslationMap>(time0, time1);
298  } else {
299  OPENVDB_THROW(ValueError, "MapType not supported!");
300  }
301  return 0;
302 }
303 
304 
305 template<typename GridT, typename FieldT, typename InterruptT>
306 template<
307  math::BiasedGradientScheme SpatialScheme,
308  math::TemporalIntegrationScheme TemporalScheme,
309  typename MapT>
310 inline size_t
311 LevelSetAdvection<GridT, FieldT, InterruptT>::advect3(ValueType time0, ValueType time1)
312 {
313  Advect<MapT, SpatialScheme, TemporalScheme> tmp(*this);
314  return tmp.advect(time0, time1);
315 }
316 
317 
318 ///////////////////////////////////////////////////////////////////////
319 
320 
321 template<typename GridT, typename FieldT, typename InterruptT>
322 template<
323  typename MapT,
324  math::BiasedGradientScheme SpatialScheme,
325  math::TemporalIntegrationScheme TemporalScheme>
326 inline
327 LevelSetAdvection<GridT, FieldT, InterruptT>::
328 Advect<MapT, SpatialScheme, TemporalScheme>::
329 Advect(LevelSetAdvection& parent)
330  : mParent(parent)
331  , mVelocity(nullptr)
332  , mOffsets(nullptr)
333  , mMap(parent.mTracker.grid().transform().template constMap<MapT>().get())
334  , mTask(0)
335  , mIsMaster(true)
336 {
337 }
338 
339 
340 template<typename GridT, typename FieldT, typename InterruptT>
341 template<
342  typename MapT,
343  math::BiasedGradientScheme SpatialScheme,
344  math::TemporalIntegrationScheme TemporalScheme>
345 inline
346 LevelSetAdvection<GridT, FieldT, InterruptT>::
347 Advect<MapT, SpatialScheme, TemporalScheme>::
348 Advect(const Advect& other)
349  : mParent(other.mParent)
350  , mVelocity(other.mVelocity)
351  , mOffsets(other.mOffsets)
352  , mMap(other.mMap)
353  , mTask(other.mTask)
354  , mIsMaster(false)
355 {
356 }
357 
358 
359 template<typename GridT, typename FieldT, typename InterruptT>
360 template<
361  typename MapT,
362  math::BiasedGradientScheme SpatialScheme,
363  math::TemporalIntegrationScheme TemporalScheme>
364 inline size_t
367 advect(ValueType time0, ValueType time1)
368 {
369  namespace ph = std::placeholders;
370 
371  //util::CpuTimer timer;
372  size_t countCFL = 0;
373  if ( math::isZero(time0 - time1) ) return countCFL;
374  const bool isForward = time0 < time1;
375  while ((isForward ? time0<time1 : time0>time1) && mParent.mTracker.checkInterrupter()) {
376  /// Make sure we have enough temporal auxiliary buffers
377  //timer.start( "\nallocate buffers" );
378  mParent.mTracker.leafs().rebuildAuxBuffers(TemporalScheme == math::TVD_RK3 ? 2 : 1);
379  //timer.stop();
380 
381  const ValueType dt = this->sampleField(time0, time1);
382  if ( math::isZero(dt) ) break;//V is essentially zero so terminate
383 
384  OPENVDB_NO_UNREACHABLE_CODE_WARNING_BEGIN //switch is resolved at compile-time
385  switch(TemporalScheme) {
386  case math::TVD_RK1:
387  // Perform one explicit Euler step: t1 = t0 + dt
388  // Phi_t1(1) = Phi_t0(0) - dt * VdotG_t0(0)
389  mTask = std::bind(&Advect::euler01, ph::_1, ph::_2, dt);
390 
391  // Cook and swap buffer 0 and 1 such that Phi_t1(0) and Phi_t0(1)
392  this->cook("Advecting level set using TVD_RK1", 1);
393  break;
394  case math::TVD_RK2:
395  // Perform one explicit Euler step: t1 = t0 + dt
396  // Phi_t1(1) = Phi_t0(0) - dt * VdotG_t0(0)
397  mTask = std::bind(&Advect::euler01, ph::_1, ph::_2, dt);
398 
399  // Cook and swap buffer 0 and 1 such that Phi_t1(0) and Phi_t0(1)
400  this->cook("Advecting level set using TVD_RK1 (step 1 of 2)", 1);
401 
402  // Convex combine explict Euler step: t2 = t0 + dt
403  // Phi_t2(1) = 1/2 * Phi_t0(1) + 1/2 * (Phi_t1(0) - dt * V.Grad_t1(0))
404  mTask = std::bind(&Advect::euler12, ph::_1, ph::_2, dt);
405 
406  // Cook and swap buffer 0 and 1 such that Phi_t2(0) and Phi_t1(1)
407  this->cook("Advecting level set using TVD_RK1 (step 2 of 2)", 1);
408  break;
409  case math::TVD_RK3:
410  // Perform one explicit Euler step: t1 = t0 + dt
411  // Phi_t1(1) = Phi_t0(0) - dt * VdotG_t0(0)
412  mTask = std::bind(&Advect::euler01, ph::_1, ph::_2, dt);
413 
414  // Cook and swap buffer 0 and 1 such that Phi_t1(0) and Phi_t0(1)
415  this->cook("Advecting level set using TVD_RK3 (step 1 of 3)", 1);
416 
417  // Convex combine explict Euler step: t2 = t0 + dt/2
418  // Phi_t2(2) = 3/4 * Phi_t0(1) + 1/4 * (Phi_t1(0) - dt * V.Grad_t1(0))
419  mTask = std::bind(&Advect::euler34, ph::_1, ph::_2, dt);
420 
421  // Cook and swap buffer 0 and 2 such that Phi_t2(0) and Phi_t1(2)
422  this->cook("Advecting level set using TVD_RK3 (step 2 of 3)", 2);
423 
424  // Convex combine explict Euler step: t3 = t0 + dt
425  // Phi_t3(2) = 1/3 * Phi_t0(1) + 2/3 * (Phi_t2(0) - dt * V.Grad_t2(0)
426  mTask = std::bind(&Advect::euler13, ph::_1, ph::_2, dt);
427 
428  // Cook and swap buffer 0 and 2 such that Phi_t3(0) and Phi_t2(2)
429  this->cook("Advecting level set using TVD_RK3 (step 3 of 3)", 2);
430  break;
431  default:
432  OPENVDB_THROW(ValueError, "Temporal integration scheme not supported!");
433  }//end of compile-time resolved switch
435 
436  time0 += isForward ? dt : -dt;
437  ++countCFL;
438  mParent.mTracker.leafs().removeAuxBuffers();
439  this->clearField();
440  /// Track the narrow band
441  mParent.mTracker.track();
442  }//end wile-loop over time
443  return countCFL;//number of CLF propagation steps
444 }
445 
446 
447 template<typename GridT, typename FieldT, typename InterruptT>
448 template<
449  typename MapT,
450  math::BiasedGradientScheme SpatialScheme,
451  math::TemporalIntegrationScheme TemporalScheme>
452 inline typename GridT::ValueType
453 LevelSetAdvection<GridT, FieldT, InterruptT>::
454 Advect<MapT, SpatialScheme, TemporalScheme>::
455 sampleField(ValueType time0, ValueType time1)
456 {
457  namespace ph = std::placeholders;
458 
459  const int grainSize = mParent.mTracker.getGrainSize();
460  const size_t leafCount = mParent.mTracker.leafs().leafCount();
461  if (leafCount==0) return ValueType(0.0);
462 
463  // Compute the prefix sum of offsets to active voxels
464  size_t size=0, voxelCount=mParent.mTracker.leafs().getPrefixSum(mOffsets, size, grainSize);
465 
466  // Sample the velocity field
467  if (mParent.mField.transform() == mParent.mTracker.grid().transform()) {
468  mTask = std::bind(&Advect::sampleAligned, ph::_1, ph::_2, time0, time1);
469  } else {
470  mTask = std::bind(&Advect::sampleXformed, ph::_1, ph::_2, time0, time1);
471  }
472  assert(voxelCount == mParent.mTracker.grid().activeVoxelCount());
473  mVelocity = new VectorType[ voxelCount ];
474  this->cook("Sampling advection field");
475 
476  // Find the extrema of the magnitude of the velocities
477  ValueType maxAbsV = 0;
478  VectorType* v = mVelocity;
479  for (size_t i = 0; i < voxelCount; ++i, ++v) {
480  maxAbsV = math::Max(maxAbsV, ValueType(v->lengthSqr()));
481  }
482 
483  // Compute the CFL number
484  if (math::isApproxZero(maxAbsV, math::Delta<ValueType>::value())) return ValueType(0);
485 #ifndef _MSC_VER // Visual C++ doesn't guarantee thread-safe initialization of local statics
486  static
487 #endif
488  const ValueType CFL = (TemporalScheme == math::TVD_RK1 ? ValueType(0.3) :
489  TemporalScheme == math::TVD_RK2 ? ValueType(0.9) :
490  ValueType(1.0))/math::Sqrt(ValueType(3.0));
491  const ValueType dt = math::Abs(time1 - time0), dx = mParent.mTracker.voxelSize();
492  return math::Min(dt, ValueType(CFL*dx/math::Sqrt(maxAbsV)));
493 }
494 
495 
496 template<typename GridT, typename FieldT, typename InterruptT>
497 template<
498  typename MapT,
499  math::BiasedGradientScheme SpatialScheme,
500  math::TemporalIntegrationScheme TemporalScheme>
501 template<bool Aligned>
502 inline void
503 LevelSetAdvection<GridT, FieldT, InterruptT>::
504 Advect<MapT, SpatialScheme, TemporalScheme>::
505 sample(const LeafRange& range, ValueType time0, ValueType time1)
506 {
507  const bool isForward = time0 < time1;
508  using VoxelIterT = typename LeafType::ValueOnCIter;
509  const MapT& map = *mMap;
510  const FieldT field( mParent.mField );
511  mParent.mTracker.checkInterrupter();
512  for (typename LeafRange::Iterator leafIter = range.begin(); leafIter; ++leafIter) {
513  VectorType* vel = mVelocity + mOffsets[ leafIter.pos() ];
514  for (VoxelIterT iter = leafIter->cbeginValueOn(); iter; ++iter, ++vel) {
515  const VectorType v = Aligned ? field(iter.getCoord(), time0) ://resolved at compile time
516  field(map.applyMap(iter.getCoord().asVec3d()), time0);
517  *vel = isForward ? v : -v;
518  }
519  }
520 }
521 
522 
523 template<typename GridT, typename FieldT, typename InterruptT>
524 template<
525  typename MapT,
526  math::BiasedGradientScheme SpatialScheme,
527  math::TemporalIntegrationScheme TemporalScheme>
528 inline void
529 LevelSetAdvection<GridT, FieldT, InterruptT>::
530 Advect<MapT, SpatialScheme, TemporalScheme>::
531 clearField()
532 {
533  delete [] mOffsets;
534  delete [] mVelocity;
535  mOffsets = nullptr;
536  mVelocity = nullptr;
537 }
538 
539 
540 template<typename GridT, typename FieldT, typename InterruptT>
541 template<
542  typename MapT,
543  math::BiasedGradientScheme SpatialScheme,
544  math::TemporalIntegrationScheme TemporalScheme>
545 inline void
546 LevelSetAdvection<GridT, FieldT, InterruptT>::
547 Advect<MapT, SpatialScheme, TemporalScheme>::
548 cook(const char* msg, size_t swapBuffer)
549 {
550  mParent.mTracker.startInterrupter( msg );
551 
552  const int grainSize = mParent.mTracker.getGrainSize();
553  const LeafRange range = mParent.mTracker.leafs().leafRange(grainSize);
554 
555  grainSize == 0 ? (*this)(range) : tbb::parallel_for(range, *this);
556 
557  mParent.mTracker.leafs().swapLeafBuffer(swapBuffer, grainSize == 0);
558 
559  mParent.mTracker.endInterrupter();
560 }
561 
562 
563 // Convex combination of Phi and a forward Euler advection steps:
564 // Phi(result) = alpha * Phi(phi) + (1-alpha) * (Phi(0) - dt * V.Grad(0));
565 template<typename GridT, typename FieldT, typename InterruptT>
566 template<
567  typename MapT,
568  math::BiasedGradientScheme SpatialScheme,
569  math::TemporalIntegrationScheme TemporalScheme>
570 template <int Nominator, int Denominator>
571 inline void
572 LevelSetAdvection<GridT, FieldT, InterruptT>::
573 Advect<MapT, SpatialScheme, TemporalScheme>::
574 euler(const LeafRange& range, ValueType dt, Index phiBuffer, Index resultBuffer)
575 {
576  using SchemeT = math::BIAS_SCHEME<SpatialScheme>;
577  using StencilT = typename SchemeT::template ISStencil<GridType>::StencilType;
578  using VoxelIterT = typename LeafType::ValueOnCIter;
579  using GradT = math::GradientBiased<MapT, SpatialScheme>;
580 
581  static const ValueType Alpha = ValueType(Nominator)/ValueType(Denominator);
582  static const ValueType Beta = ValueType(1) - Alpha;
583 
584  mParent.mTracker.checkInterrupter();
585  const MapT& map = *mMap;
586  StencilT stencil(mParent.mTracker.grid());
587  for (typename LeafRange::Iterator leafIter = range.begin(); leafIter; ++leafIter) {
588  const VectorType* vel = mVelocity + mOffsets[ leafIter.pos() ];
589  const ValueType* phi = leafIter.buffer(phiBuffer).data();
590  ValueType* result = leafIter.buffer(resultBuffer).data();
591  for (VoxelIterT voxelIter = leafIter->cbeginValueOn(); voxelIter; ++voxelIter, ++vel) {
592  const Index i = voxelIter.pos();
593  stencil.moveTo(voxelIter);
594  const ValueType a =
595  stencil.getValue() - dt * vel->dot(GradT::result(map, stencil, *vel));
596  result[i] = Nominator ? Alpha * phi[i] + Beta * a : a;
597  }//loop over active voxels in the leaf of the mask
598  }//loop over leafs of the level set
599 }
600 
601 } // namespace tools
602 } // namespace OPENVDB_VERSION_NAME
603 } // namespace openvdb
604 
605 #endif // OPENVDB_TOOLS_LEVEL_SET_ADVECT_HAS_BEEN_INCLUDED
606 
607 // Copyright (c) 2012-2018 DreamWorks Animation LLC
608 // All rights reserved. This software is distributed under the
609 // Mozilla Public License 2.0 ( http://www.mozilla.org/MPL/2.0/ )
TemporalIntegrationScheme
Temporal integration schemes.
GLenum GLint * range
Definition: glcorearb.h:1924
LevelSetAdvection(GridT &grid, const FieldT &field, InterruptT *interrupt=nullptr)
Main constructor.
size_t advect(ValueType time0, ValueType time1)
void setTemporalScheme(math::TemporalIntegrationScheme s)
Set the spatial finite difference scheme.
const GLdouble * v
Definition: glcorearb.h:836
void setGrainSize(int grainsize)
Set the grain-size used for multi-threading.
GLboolean GLboolean GLboolean GLboolean a
Definition: glcorearb.h:1221
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:189
Performs multi-threaded interface tracking of narrow band level sets.
png_uint_32 i
Definition: png.h:2877
math::TemporalIntegrationScheme getTemporalScheme() const
GLsizeiptr size
Definition: glcorearb.h:663
BiasedGradientScheme
Biased Gradients are limited to non-centered differences.
png_infop png_bytep * trans
Definition: png.h:2520
void setNormCount(int n)
Set the number of normalizations performed per track or normalize call.
GLdouble n
Definition: glcorearb.h:2007
math::BiasedGradientScheme getSpatialScheme() const
Coord Abs(const Coord &xyz)
Definition: Coord.h:513
void setSpatialScheme(math::BiasedGradientScheme scheme)
Set the spatial finite difference scheme.
int getNormCount() const
Return The number of normalizations performed per track or normalize call.
float Sqrt(float x)
Return the square root of a floating-point value.
Definition: Math.h:715
void setTrackerSpatialScheme(math::BiasedGradientScheme scheme)
Set the spatial finite difference scheme.
void setNormCount(int n)
Set the number of normalizations performed per track or normalize call.
void setSpatialScheme(math::BiasedGradientScheme s)
Set the spatial finite difference scheme.
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...
Definition: Math.h:320
GA_API const UT_StringHolder transform
#define OPENVDB_NO_UNREACHABLE_CODE_WARNING_BEGIN
Definition: Platform.h:129
GLint GLfloat GLint stencil
Definition: glcorearb.h:1277
math::BiasedGradientScheme getSpatialScheme() const
Return the spatial finite difference scheme.
void setGrainSize(int grainsize)
Set the grain-size used for multi-threading.
math::TemporalIntegrationScheme getTrackerTemporalScheme() const
Return the temporal integration scheme.
static Name mapType()
Return UnitaryMap.
Definition: Maps.h:1731
GLsizei const GLfloat * value
Definition: glcorearb.h:823
Defines two simple wrapper classes for advection velocity fields as well as VelocitySampler and Veloc...
typename LeafManagerType::BufferType BufferType
math::BiasedGradientScheme getTrackerSpatialScheme() const
Return the spatial finite difference scheme.
const Type & Min(const Type &a, const Type &b)
Return the minimum of two values.
Definition: Math.h:610
#define OPENVDB_NO_UNREACHABLE_CODE_WARNING_END
Definition: Platform.h:130
typename LeafManagerType::LeafRange LeafRange
GLboolean r
Definition: glcorearb.h:1221
int getGrainSize() const
Return the grain-size used for multi-threading.
GA_API const UT_StringHolder Alpha
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h:135
Hyperbolic advection of narrow-band level sets in an external velocity field.
const Type & Max(const Type &a, const Type &b)
Return the maximum of two values.
Definition: Math.h:549
math::TemporalIntegrationScheme getTemporalScheme() const
Return the temporal integration scheme.
LevelSetTracker< GridT, InterruptT > TrackerT
bool isZero(const Type &x)
Return true if x is exactly equal to zero.
Definition: Math.h:308
#define OPENVDB_THROW(exception, message)
Definition: Exceptions.h:109
void setTrackerTemporalScheme(math::TemporalIntegrationScheme scheme)
Set the spatial finite difference scheme.
void setTemporalScheme(math::TemporalIntegrationScheme scheme)
Set the spatial finite difference scheme.