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