OpenVDB: LevelSetTracker.h Source File

OpenVDB 4.0.2

LevelSetTracker.h
Go to the documentation of this file.
1 //
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 //
30
38
39 #ifndef OPENVDB_TOOLS_LEVEL_SET_TRACKER_HAS_BEEN_INCLUDED
40 #define OPENVDB_TOOLS_LEVEL_SET_TRACKER_HAS_BEEN_INCLUDED
41
42 #include <tbb/parallel_for.h>
43 #include <boost/bind.hpp>
44 #include <boost/function.hpp>
45 #include <type_traits>
46 #include <openvdb/Types.h>
47 #include <openvdb/math/Math.h>
48 #include <openvdb/math/FiniteDifference.h>
49 #include <openvdb/math/Operators.h>
50 #include <openvdb/math/Stencils.h>
51 #include <openvdb/math/Transform.h>
52 #include <openvdb/Grid.h>
53 #include <openvdb/util/NullInterrupter.h>
54 #include <openvdb/tree/ValueAccessor.h>
55 #include <openvdb/tree/LeafManager.h>
56 #include "ChangeBackground.h"// for changeLevelSetBackground
57 #include "Morphology.h"//for dilateActiveValues
58 #include "Prune.h"// for pruneLevelSet
59
60 namespace openvdb {
61 OPENVDB_USE_VERSION_NAMESPACE
62 namespace OPENVDB_VERSION_NAME {
63 namespace tools {
64
66 template<typename GridT, typename InterruptT = util::NullInterrupter>
67 class LevelSetTracker
68 {
69 public:
70 typedef GridT GridType;
71 typedef typename GridT::TreeType TreeType;
72 typedef typename TreeType::LeafNodeType LeafType;
73 typedef typename TreeType::ValueType ValueType;
74 typedef typename tree::LeafManager<TreeType> LeafManagerType; // leafs + buffers
75 typedef typename LeafManagerType::LeafRange LeafRange;
76 typedef typename LeafManagerType::BufferType BufferType;
77 typedef typename TreeType::template ValueConverter<ValueMask>::Type MaskTreeType;
78 static_assert(std::is_floating_point<ValueType>::value,
79 "LevelSetTracker requires a level set grid with floating-point values");
80
82 struct State {
83 State(math::BiasedGradientScheme s = math::HJWENO5_BIAS,
84 math::TemporalIntegrationScheme t = math::TVD_RK1,
85 int n = static_cast<int>(LEVEL_SET_HALF_WIDTH), int g = 1)
86 : spatialScheme(s), temporalScheme(t), normCount(n), grainSize(g) {}
87 math::BiasedGradientScheme spatialScheme;
88 math::TemporalIntegrationScheme temporalScheme;
89 int normCount;// Number of iterations of normalization
90 int grainSize;
91 };
92
95 LevelSetTracker(GridT& grid, InterruptT* interrupt = nullptr);
96
97 virtual ~LevelSetTracker() { delete mLeafs; }
98
101 template <typename MaskType>
102 void normalize(const MaskType* mask);
103
105 void normalize() { this->normalize<MaskTreeType>(nullptr); }
106
109 void track();
110
112 void prune();
113
129 void dilate(int iterations = 1);
130
133 void erode(int iterations = 1);
134
137 bool resize(Index halfWidth = static_cast<Index>(LEVEL_SET_HALF_WIDTH));
138
140 ValueType getHalfWidth() const { return mGrid->background()/mDx; }
141
143 State getState() const { return mState; }
144
146 void setState(const State& s) { mState =s; }
147
149 math::BiasedGradientScheme getSpatialScheme() const { return mState.spatialScheme; }
150
152 void setSpatialScheme(math::BiasedGradientScheme scheme) { mState.spatialScheme = scheme; }
153
155 math::TemporalIntegrationScheme getTemporalScheme() const { return mState.temporalScheme; }
156
158 void setTemporalScheme(math::TemporalIntegrationScheme scheme) { mState.temporalScheme = scheme;}
159
162 int getNormCount() const { return mState.normCount; }
163
166 void setNormCount(int n) { mState.normCount = n; }
167
169 int getGrainSize() const { return mState.grainSize; }
170
173 void setGrainSize(int grainsize) { mState.grainSize = grainsize; }
174
175 ValueType voxelSize() const { return mDx; }
176
177 void startInterrupter(const char* msg);
178
179 void endInterrupter();
180
182 bool checkInterrupter();
183
184 const GridType& grid() const { return *mGrid; }
185
186 LeafManagerType& leafs() { return *mLeafs; }
187
188 const LeafManagerType& leafs() const { return *mLeafs; }
189
190 private:
191
192 // disallow copy construction and copy by assignment!
193 LevelSetTracker(const LevelSetTracker&);// not implemented
194 LevelSetTracker& operator=(const LevelSetTracker&);// not implemented
195
196 // Private class to perform multi-threaded trimming of
197 // voxels that are too far away from the zero-crossing.
198 struct Trim
199 {
200 Trim(LevelSetTracker& tracker) : mTracker(tracker) {}
201 void trim();
202 void operator()(const LeafRange& r) const;
203 LevelSetTracker& mTracker;
204 };// Trim
205
206 // Private struct to perform multi-threaded normalization
207 template<math::BiasedGradientScheme SpatialScheme,
208 math::TemporalIntegrationScheme TemporalScheme,
209 typename MaskT>
210 struct Normalizer
211 {
212 typedef math::BIAS_SCHEME<SpatialScheme> SchemeT;
213 typedef typename SchemeT::template ISStencil<GridType>::StencilType StencilT;
214 typedef typename MaskT::LeafNodeType MaskLeafT;
215 typedef typename MaskLeafT::ValueOnCIter MaskIterT;
216 typedef typename LeafType::ValueOnCIter VoxelIterT;
217 Normalizer(LevelSetTracker& tracker, const MaskT* mask);
218 void normalize();
219 void operator()(const LeafRange& r) const {mTask(const_cast<Normalizer*>(this), r);}
220 void cook(const char* msg, int swapBuffer=0);
221 template <int Nominator, int Denominator>
222 void euler(const LeafRange& range, Index phiBuffer, Index resultBuffer);
223 inline void euler01(const LeafRange& r) {this->euler<0,1>(r, 0, 1);}
224 inline void euler12(const LeafRange& r) {this->euler<1,2>(r, 1, 1);}
225 inline void euler34(const LeafRange& r) {this->euler<3,4>(r, 1, 2);}
226 inline void euler13(const LeafRange& r) {this->euler<1,3>(r, 1, 2);}
227 template <int Nominator, int Denominator>
228 void eval(StencilT& stencil, const ValueType* phi, ValueType* result, Index n) const;
229 LevelSetTracker& mTracker;
230 const MaskT* mMask;
231 const ValueType mDt, mInvDx;
232 typename boost::function<void (Normalizer*, const LeafRange&)> mTask;
233 }; // Normalizer struct
234
235 template<math::BiasedGradientScheme SpatialScheme, typename MaskT>
236 void normalize1(const MaskT* mask);
237
238 template<math::BiasedGradientScheme SpatialScheme,
239 math::TemporalIntegrationScheme TemporalScheme, typename MaskT>
240 void normalize2(const MaskT* mask);
241
242 // Throughout the methods below mLeafs is always assumed to contain
243 // a list of the current LeafNodes! The auxiliary buffers on the
244 // other hand always have to be allocated locally, since some
245 // methods need them and others don't!
246 GridType* mGrid;
247 LeafManagerType* mLeafs;
248 InterruptT* mInterrupter;
249 const ValueType mDx;
250 State mState;
251 }; // end of LevelSetTracker class
252
253 template<typename GridT, typename InterruptT>
254 LevelSetTracker<GridT, InterruptT>::
255 LevelSetTracker(GridT& grid, InterruptT* interrupt):
256 mGrid(&grid),
257 mLeafs(new LeafManagerType(grid.tree())),
258 mInterrupter(interrupt),
259 mDx(static_cast<ValueType>(grid.voxelSize()[0])),
260 mState()
261 {
262 if ( !grid.hasUniformVoxels() ) {
263 OPENVDB_THROW(RuntimeError,
264 "The transform must have uniform scale for the LevelSetTracker to function");
265 }
266 if ( grid.getGridClass() != GRID_LEVEL_SET) {
267 OPENVDB_THROW(RuntimeError,
268 "LevelSetTracker expected a level set, got a grid of class \""
269 + grid.gridClassToString(grid.getGridClass())
270 + "\" [hint: Grid::setGridClass(openvdb::GRID_LEVEL_SET)]");
271 }
272 }
273
274 template<typename GridT, typename InterruptT>
275 inline void
276 LevelSetTracker<GridT, InterruptT>::
277 prune()
278 {
279 this->startInterrupter("Pruning Level Set");
280
281 // Prune voxels that are too far away from the zero-crossing
282 Trim t(*this);
283 t.trim();
284
285 // Remove inactive nodes from tree
286 tools::pruneLevelSet(mGrid->tree());
287
288 // The tree topology has changes so rebuild the list of leafs
289 mLeafs->rebuildLeafArray();
290 this->endInterrupter();
291 }
292
293 template<typename GridT, typename InterruptT>
294 inline void
295 LevelSetTracker<GridT, InterruptT>::
296 track()
297 {
298 // Dilate narrow-band (this also rebuilds the leaf array!)
299 tools::dilateActiveValues( *mLeafs, 1, tools::NN_FACE, tools::IGNORE_TILES);
300
301 // Compute signed distances in dilated narrow-band
302 this->normalize();
303
304 // Remove voxels that are outside the narrow band
305 this->prune();
306 }
307
308 template<typename GridT, typename InterruptT>
309 inline void
310 LevelSetTracker<GridT, InterruptT>::
311 dilate(int iterations)
312 {
313 if (this->getNormCount() == 0) {
314 for (int i=0; i < iterations; ++i) {
315 tools::dilateActiveValues( *mLeafs, 1, tools::NN_FACE, tools::IGNORE_TILES);
316 tools::changeLevelSetBackground(this->leafs(), mDx + mGrid->background());
317 }
318 } else {
319 for (int i=0; i < iterations; ++i) {
320 MaskTreeType mask0(mGrid->tree(), false, TopologyCopy());
321 tools::dilateActiveValues( *mLeafs, 1, tools::NN_FACE, tools::IGNORE_TILES);
322 tools::changeLevelSetBackground(this->leafs(), mDx + mGrid->background());
323 MaskTreeType mask(mGrid->tree(), false, TopologyCopy());
324 mask.topologyDifference(mask0);
325 this->normalize(&mask);
326 }
327 }
328 }
329
330 template<typename GridT, typename InterruptT>
331 inline void
332 LevelSetTracker<GridT, InterruptT>::
333 erode(int iterations)
334 {
335 tools::erodeVoxels(*mLeafs, iterations);
336 mLeafs->rebuildLeafArray();
337 const ValueType background = mGrid->background() - iterations*mDx;
338 tools::changeLevelSetBackground(this->leafs(), background);
339 }
340
341 template<typename GridT, typename InterruptT>
342 inline bool
343 LevelSetTracker<GridT, InterruptT>::
344 resize(Index halfWidth)
345 {
346 const int wOld = static_cast<int>(math::RoundDown(this->getHalfWidth()));
347 const int wNew = static_cast<int>(halfWidth);
348 if (wOld < wNew) {
349 this->dilate(wNew - wOld);
350 } else if (wOld > wNew) {
351 this->erode(wOld - wNew);
352 }
353 return wOld != wNew;
354 }
355
356 template<typename GridT, typename InterruptT>
357 inline void
358 LevelSetTracker<GridT, InterruptT>::
359 startInterrupter(const char* msg)
360 {
361 if (mInterrupter) mInterrupter->start(msg);
362 }
363
364 template<typename GridT, typename InterruptT>
365 inline void
366 LevelSetTracker<GridT, InterruptT>::
367 endInterrupter()
368 {
369 if (mInterrupter) mInterrupter->end();
370 }
371
372 template<typename GridT, typename InterruptT>
373 inline bool
374 LevelSetTracker<GridT, InterruptT>::
375 checkInterrupter()
376 {
377 if (util::wasInterrupted(mInterrupter)) {
378 tbb::task::self().cancel_group_execution();
379 return false;
380 }
381 return true;
382 }
383
384 template<typename GridT, typename InterruptT>
385 template<typename MaskT>
386 inline void
387 LevelSetTracker<GridT, InterruptT>::
388 normalize(const MaskT* mask)
389 {
390 switch (this->getSpatialScheme()) {
391 case math::FIRST_BIAS:
392 this->normalize1<math::FIRST_BIAS , MaskT>(mask); break;
393 case math::SECOND_BIAS:
394 this->normalize1<math::SECOND_BIAS, MaskT>(mask); break;
395 case math::THIRD_BIAS:
396 this->normalize1<math::THIRD_BIAS, MaskT>(mask); break;
397 case math::WENO5_BIAS:
398 this->normalize1<math::WENO5_BIAS, MaskT>(mask); break;
399 case math::HJWENO5_BIAS:
400 this->normalize1<math::HJWENO5_BIAS, MaskT>(mask); break;
401 case math::UNKNOWN_BIAS:
402 default:
403 OPENVDB_THROW(ValueError, "Spatial difference scheme not supported!");
404 }
405 }
406
407 template<typename GridT, typename InterruptT>
408 template<math::BiasedGradientScheme SpatialScheme, typename MaskT>
409 inline void
410 LevelSetTracker<GridT, InterruptT>::
411 normalize1(const MaskT* mask)
412 {
413 switch (this->getTemporalScheme()) {
414 case math::TVD_RK1:
415 this->normalize2<SpatialScheme, math::TVD_RK1, MaskT>(mask); break;
416 case math::TVD_RK2:
417 this->normalize2<SpatialScheme, math::TVD_RK2, MaskT>(mask); break;
418 case math::TVD_RK3:
419 this->normalize2<SpatialScheme, math::TVD_RK3, MaskT>(mask); break;
420 case math::UNKNOWN_TIS:
421 default:
422 OPENVDB_THROW(ValueError, "Temporal integration scheme not supported!");
423 }
424 }
425
426 template<typename GridT, typename InterruptT>
427 template<math::BiasedGradientScheme SpatialScheme,
428 math::TemporalIntegrationScheme TemporalScheme,
429 typename MaskT>
430 inline void
431 LevelSetTracker<GridT, InterruptT>::
432 normalize2(const MaskT* mask)
433 {
434 Normalizer<SpatialScheme, TemporalScheme, MaskT> tmp(*this, mask);
435 tmp.normalize();
436 }
437
439
440 template<typename GridT, typename InterruptT>
441 inline void
442 LevelSetTracker<GridT, InterruptT>::
443 Trim::trim()
444 {
445 const int grainSize = mTracker.getGrainSize();
446 const LeafRange range = mTracker.leafs().leafRange(grainSize);
447
448 if (grainSize>0) {
449 tbb::parallel_for(range, *this);
450 } else {
451 (*this)(range);
452 }
453 }
454
456 template<typename GridT, typename InterruptT>
457 inline void
458 LevelSetTracker<GridT, InterruptT>::
459 Trim::operator()(const LeafRange& range) const
460 {
461 typedef typename LeafType::ValueOnIter VoxelIterT;
462 mTracker.checkInterrupter();
463 const ValueType gamma = mTracker.mGrid->background();
464
465 for (typename LeafRange::Iterator leafIter = range.begin(); leafIter; ++leafIter) {
466 LeafType &leaf = *leafIter;
467 for (VoxelIterT iter = leaf.beginValueOn(); iter; ++iter) {
468 const ValueType val = *iter;
469 if (val <= -gamma)
470 leaf.setValueOff(iter.pos(), -gamma);
471 else if (val >= gamma)
472 leaf.setValueOff(iter.pos(), gamma);
473 }
474 }
475 }
476
478
479 template<typename GridT, typename InterruptT>
480 template<math::BiasedGradientScheme SpatialScheme,
481 math::TemporalIntegrationScheme TemporalScheme,
482 typename MaskT>
483 inline
484 LevelSetTracker<GridT, InterruptT>::
485 Normalizer<SpatialScheme, TemporalScheme, MaskT>::
486 Normalizer(LevelSetTracker& tracker, const MaskT* mask)
487 : mTracker(tracker)
488 , mMask(mask)
489 , mDt(tracker.voxelSize()*(TemporalScheme == math::TVD_RK1 ? 0.3f :
490 TemporalScheme == math::TVD_RK2 ? 0.9f : 1.0f))
491 , mInvDx(1.0f/tracker.voxelSize())
492 , mTask(0)
493 {
494 }
495
496 template<typename GridT, typename InterruptT>
497 template<math::BiasedGradientScheme SpatialScheme,
498 math::TemporalIntegrationScheme TemporalScheme,
499 typename MaskT>
500 inline void
501 LevelSetTracker<GridT, InterruptT>::
502 Normalizer<SpatialScheme, TemporalScheme, MaskT>::
503 normalize()
504 {
506 mTracker.mLeafs->rebuildAuxBuffers(TemporalScheme == math::TVD_RK3 ? 2 : 1);
507
508 for (int n=0, e=mTracker.getNormCount(); n < e; ++n) {
509
510 OPENVDB_NO_UNREACHABLE_CODE_WARNING_BEGIN
511 switch(TemporalScheme) {//switch is resolved at compile-time
512 case math::TVD_RK1:
513 // Perform one explicit Euler step: t1 = t0 + dt
514 // Phi_t1(0) = Phi_t0(0) - dt * VdotG_t0(1)
515 mTask = boost::bind(&Normalizer::euler01, _1, _2);
516
517 // Cook and swap buffer 0 and 1 such that Phi_t1(0) and Phi_t0(1)
518 this->cook("Normalizing level set using TVD_RK1", 1);
519 break;
520 case math::TVD_RK2:
521 // Perform one explicit Euler step: t1 = t0 + dt
522 // Phi_t1(1) = Phi_t0(0) - dt * VdotG_t0(1)
523 mTask = boost::bind(&Normalizer::euler01, _1, _2);
524
525 // Cook and swap buffer 0 and 1 such that Phi_t1(0) and Phi_t0(1)
526 this->cook("Normalizing level set using TVD_RK1 (step 1 of 2)", 1);
527
528 // Convex combine explicit Euler step: t2 = t0 + dt
529 // Phi_t2(1) = 1/2 * Phi_t0(1) + 1/2 * (Phi_t1(0) - dt * V.Grad_t1(0))
530 mTask = boost::bind(&Normalizer::euler12, _1, _2);
531
532 // Cook and swap buffer 0 and 1 such that Phi_t2(0) and Phi_t1(1)
533 this->cook("Normalizing level set using TVD_RK1 (step 2 of 2)", 1);
534 break;
535 case math::TVD_RK3:
536 // Perform one explicit Euler step: t1 = t0 + dt
537 // Phi_t1(1) = Phi_t0(0) - dt * VdotG_t0(1)
538 mTask = boost::bind(&Normalizer::euler01, _1, _2);
539
540 // Cook and swap buffer 0 and 1 such that Phi_t1(0) and Phi_t0(1)
541 this->cook("Normalizing level set using TVD_RK3 (step 1 of 3)", 1);
542
543 // Convex combine explicit Euler step: t2 = t0 + dt/2
544 // Phi_t2(2) = 3/4 * Phi_t0(1) + 1/4 * (Phi_t1(0) - dt * V.Grad_t1(0))
545 mTask = boost::bind(&Normalizer::euler34, _1, _2);
546
547 // Cook and swap buffer 0 and 2 such that Phi_t2(0) and Phi_t1(2)
548 this->cook("Normalizing level set using TVD_RK3 (step 2 of 3)", 2);
549
550 // Convex combine explicit Euler step: t3 = t0 + dt
551 // Phi_t3(2) = 1/3 * Phi_t0(1) + 2/3 * (Phi_t2(0) - dt * V.Grad_t2(0)
552 mTask = boost::bind(&Normalizer::euler13, _1, _2);
553
554 // Cook and swap buffer 0 and 2 such that Phi_t3(0) and Phi_t2(2)
555 this->cook("Normalizing level set using TVD_RK3 (step 3 of 3)", 2);
556 break;
557 case math::UNKNOWN_TIS:
558 default:
559 OPENVDB_THROW(ValueError, "Temporal integration scheme not supported!");
560 }
561 OPENVDB_NO_UNREACHABLE_CODE_WARNING_END
562 }
563 mTracker.mLeafs->removeAuxBuffers();
564 }
565
568 template<typename GridT, typename InterruptT>
569 template<math::BiasedGradientScheme SpatialScheme,
570 math::TemporalIntegrationScheme TemporalScheme,
571 typename MaskT>
572 inline void
573 LevelSetTracker<GridT, InterruptT>::
574 Normalizer<SpatialScheme, TemporalScheme, MaskT>::
575 cook(const char* msg, int swapBuffer)
576 {
577 mTracker.startInterrupter( msg );
578
579 const int grainSize = mTracker.getGrainSize();
580 const LeafRange range = mTracker.leafs().leafRange(grainSize);
581
582 grainSize>0 ? tbb::parallel_for(range, *this) : (*this)(range);
583
584 mTracker.leafs().swapLeafBuffer(swapBuffer, grainSize==0);
585
586 mTracker.endInterrupter();
587 }
588
589 template<typename GridT, typename InterruptT>
590 template<math::BiasedGradientScheme SpatialScheme,
591 math::TemporalIntegrationScheme TemporalScheme,
592 typename MaskT>
593 template <int Nominator, int Denominator>
594 inline void
595 LevelSetTracker<GridT, InterruptT>::
596 Normalizer<SpatialScheme, TemporalScheme, MaskT>::
597 eval(StencilT& stencil, const ValueType* phi, ValueType* result, Index n) const
598 {
599 typedef typename math::ISGradientNormSqrd<SpatialScheme> GradientT;
600 static const ValueType alpha = ValueType(Nominator)/ValueType(Denominator);
601 static const ValueType beta = ValueType(1) - alpha;
602
603 const ValueType normSqGradPhi = GradientT::result(stencil);
604 const ValueType phi0 = stencil.getValue();
605 ValueType v = phi0 / ( math::Sqrt(math::Pow2(phi0) + normSqGradPhi) +
606 math::Tolerance<ValueType>::value() );
607 v = phi0 - mDt * v * (math::Sqrt(normSqGradPhi) * mInvDx - 1.0f);
608 result[n] = Nominator ? alpha * phi[n] + beta * v : v;
609 }
610
611 template<typename GridT, typename InterruptT>
612 template<math::BiasedGradientScheme SpatialScheme,
613 math::TemporalIntegrationScheme TemporalScheme,
614 typename MaskT>
615 template <int Nominator, int Denominator>
616 inline void
617 LevelSetTracker<GridT,InterruptT>::
618 Normalizer<SpatialScheme, TemporalScheme, MaskT>::
619 euler(const LeafRange& range, Index phiBuffer, Index resultBuffer)
620 {
621 typedef typename LeafType::ValueOnCIter VoxelIterT;
622
623 mTracker.checkInterrupter();
624
625 StencilT stencil(mTracker.grid());
626
627 for (typename LeafRange::Iterator leafIter = range.begin(); leafIter; ++leafIter) {
628 const ValueType* phi = leafIter.buffer(phiBuffer).data();
629 ValueType* result = leafIter.buffer(resultBuffer).data();
630 if (mMask == nullptr) {
631 for (VoxelIterT iter = leafIter->cbeginValueOn(); iter; ++iter) {
632 stencil.moveTo(iter);
633 this->eval<Nominator, Denominator>(stencil, phi, result, iter.pos());
634 }//loop over active voxels in the leaf of the level set
635 } else if (const MaskLeafT* mask = mMask->probeLeaf(leafIter->origin())) {
636 const ValueType* phi0 = leafIter->buffer().data();
637 for (MaskIterT iter = mask->cbeginValueOn(); iter; ++iter) {
638 const Index i = iter.pos();
639 stencil.moveTo(iter.getCoord(), phi0[i]);
640 this->eval<Nominator, Denominator>(stencil, phi, result, i);
641 }//loop over active voxels in the leaf of the mask
642 }
643 }//loop over leafs of the level set
644 }
645
646 } // namespace tools
647 } // namespace OPENVDB_VERSION_NAME
648 } // namespace openvdb
649
650 #endif // OPENVDB_TOOLS_LEVEL_SET_TRACKER_HAS_BEEN_INCLUDED
651
652 // Copyright (c) 2012-2017 DreamWorks Animation LLC
653 // All rights reserved. This software is distributed under the
654 // Mozilla Public License 2.0 ( http://www.mozilla.org/MPL/2.0/ )
openvdb::v4_0_2::math::TVD_RK2
Definition: FiniteDifference.h:264
FiniteDifference.h
openvdb::v4_0_2::math::TVD_RK1
Definition: FiniteDifference.h:263
openvdb::v4_0_2::math::SECOND_BIAS
Definition: FiniteDifference.h:195
Types.h
openvdb::v4_0_2::tools::LevelSetTracker::grid
const GridType & grid() const
Definition: LevelSetTracker.h:184
NullInterrupter.h
openvdb::v4_0_2::math::TemporalIntegrationScheme
TemporalIntegrationScheme
Temporal integration schemes.
Definition: FiniteDifference.h:261
Math.h
General-purpose arithmetic and comparison routines, most of which accept arbitrary value types (or at...
openvdb::v4_0_2::tools::LevelSetTracker::setState
void setState(const State &s)
Set the state of the tracker (see struct defined above)
Definition: LevelSetTracker.h:146
openvdb::v4_0_2::math::HJWENO5_BIAS
Definition: FiniteDifference.h:198
openvdb::v4_0_2::tools::LevelSetTracker::getState
State getState() const
Return the state of the tracker (see struct defined above)
Definition: LevelSetTracker.h:143
openvdb::v4_0_2::tools::LevelSetTracker::setSpatialScheme
void setSpatialScheme(math::BiasedGradientScheme scheme)
Set the spatial finite difference scheme.
Definition: LevelSetTracker.h:152
openvdb::v4_0_2::tools::normalize
GridType::Ptr normalize(const GridType &grid, bool threaded, InterruptT *interrupt)
Normalize the vectors of the given vector-valued grid.
Definition: GridOperators.h:1091
OPENVDB_THROW
#define OPENVDB_THROW(exception, message)
Definition: Exceptions.h:101
openvdb::v4_0_2::math::FIRST_BIAS
Definition: FiniteDifference.h:194
openvdb::v4_0_2::tools::LevelSetTracker::getNormCount
int getNormCount() const
Definition: LevelSetTracker.h:162
openvdb::v4_0_2::tools::LevelSetTracker::leafs
const LeafManagerType & leafs() const
Definition: LevelSetTracker.h:188
openvdb::v4_0_2::tools::LevelSetTracker::setNormCount
void setNormCount(int n)
Set the number of normalizations performed per track or normalize call.
Definition: LevelSetTracker.h:166
ChangeBackground.h
Efficient multi-threaded replacement of the background values in tree.
i
tbb::atomic< Index32 > i
Definition: LeafBuffer.h:71
openvdb::v4_0_2::tools::LevelSetTracker::LeafType
TreeType::LeafNodeType LeafType
Definition: LevelSetTracker.h:72
openvdb::v4_0_2::tools::LevelSetTracker::getSpatialScheme
math::BiasedGradientScheme getSpatialScheme() const
Definition: LevelSetTracker.h:149
openvdb::v4_0_2::tools::NN_FACE
Definition: Morphology.h:85
openvdb::v4_0_2::tools::LevelSetTracker::getHalfWidth
ValueType getHalfWidth() const
Return the half width of the narrow band in floating-point voxel units.
Definition: LevelSetTracker.h:140
openvdb::v4_0_2::tools::LevelSetTracker::prune
void prune()
Remove voxels that are outside the narrow band. (substep of track)
Definition: LevelSetTracker.h:277
openvdb::v4_0_2::tools::LevelSetTracker::State::temporalScheme
math::TemporalIntegrationScheme temporalScheme
Definition: LevelSetTracker.h:88
Transform.h
openvdb::v4_0_2::math::UNKNOWN_BIAS
Definition: FiniteDifference.h:193
openvdb::v4_0_2::Index
Index32 Index
Definition: Types.h:57
openvdb::v4_0_2::tools::LevelSetTracker::getTemporalScheme
math::TemporalIntegrationScheme getTemporalScheme() const
Definition: LevelSetTracker.h:155
openvdb::v4_0_2::tools::pruneLevelSet
void pruneLevelSet(TreeT &tree, bool threaded=true, size_t grainSize=1)
Reduce the memory footprint of a tree by replacing nodes whose values are all inactive with inactive ...
Definition: Prune.h:416
openvdb::v4_0_2::tools::LevelSetTracker::startInterrupter
void startInterrupter(const char *msg)
Definition: LevelSetTracker.h:359
openvdb::v4_0_2::tools::LevelSetTracker::erode
void erode(int iterations=1)
Erodes the width of the narrow-band and update the background values.
Definition: LevelSetTracker.h:333
Prune.h
Defined various multi-threaded utility functions for trees.
openvdb::v4_0_2::tree::LeafManager::LeafRange
Definition: LeafManager.h:127
openvdb::v4_0_2::math::TVD_RK3
Definition: FiniteDifference.h:265
openvdb::v4_0_2::util::wasInterrupted
bool wasInterrupted(T *i, int percent=-1)
Definition: NullInterrupter.h:76
openvdb::v4_0_2::tools::LevelSetTracker::normalize
void normalize()
Iterative normalization, i.e. solving the Eikonal equation.
Definition: LevelSetTracker.h:105
openvdb::v4_0_2::tools::prune
void prune(TreeT &tree, typename TreeT::ValueType tolerance=zeroVal< typename TreeT::ValueType >(), bool threaded=true, size_t grainSize=1)
Reduce the memory footprint of a tree by replacing with tiles any nodes whose values are all the same...
Definition: Prune.h:361
openvdb::v4_0_2::math::BIAS_SCHEME
Definition: Operators.h:152
OPENVDB_VERSION_NAME
#define OPENVDB_VERSION_NAME
Definition: version.h:43
openvdb::v4_0_2::tools::dilateActiveValues
void dilateActiveValues(TreeType &tree, int iterations=1, NearestNeighbors nn=NN_FACE, TilePolicy mode=PRESERVE_TILES)
Topologically dilate all active values (i.e. both voxels and tiles) in a tree using one of three near...
Definition: Morphology.h:1078
openvdb::v4_0_2::tree::LeafManager::LeafRange::Iterator
Definition: LeafManager.h:130
openvdb::v4_0_2::tree::LeafManager::BufferType
typename CopyConstness< TreeType, NonConstBufferType >::Type BufferType
Definition: LeafManager.h:121
Morphology.h
Implementation of morphological dilation and erosion.
openvdb
Definition: Exceptions.h:39
openvdb::v4_0_2::tools::LevelSetTracker::dilate
void dilate(int iterations=1)
Fast but approximate dilation of the narrow band - one layer at a time. Normally we recommend using t...
Definition: LevelSetTracker.h:311
OPENVDB_NO_UNREACHABLE_CODE_WARNING_BEGIN
#define OPENVDB_NO_UNREACHABLE_CODE_WARNING_BEGIN
Definition: Platform.h:129
openvdb::v4_0_2::tools::LevelSetTracker::resize
bool resize(Index halfWidth=static_cast< Index >(LEVEL_SET_HALF_WIDTH))
Resize the width of the narrow band, i.e. perform dilation and renormalization or erosion as required...
Definition: LevelSetTracker.h:344
openvdb::v4_0_2::tools::LevelSetTracker::State::normCount
int normCount
Definition: LevelSetTracker.h:89
openvdb::v4_0_2::tree::LeafManager::rebuildLeafArray
void rebuildLeafArray()
Remove the auxiliary buffers and rebuild the leaf array.
Definition: LeafManager.h:316
openvdb::v4_0_2::tools::changeLevelSetBackground
void changeLevelSetBackground(TreeOrLeafManagerT &tree, const typename TreeOrLeafManagerT::ValueType &halfWidth, bool threaded=true, size_t grainSize=32)
Replace the background value in all the nodes of a floating-point tree containing a symmetric narrow-...
Definition: ChangeBackground.h:260
openvdb::v4_0_2::ValueError
Definition: Exceptions.h:92
openvdb::v4_0_2::tools::LevelSetTracker::State
Lightweight struct that stores the state of the LevelSetTracker.
Definition: LevelSetTracker.h:82
openvdb::v4_0_2::tree::LeafManager
This class manages a linear array of pointers to a given tree&#39;s leaf nodes, as well as optional auxil...
Definition: LeafManager.h:110
openvdb::v4_0_2::math::RoundDown
float RoundDown(float x)
Return x rounded down to the nearest integer.
Definition: Math.h:753
ValueAccessor.h
openvdb::v4_0_2::tools::LevelSetTracker::getGrainSize
int getGrainSize() const
Definition: LevelSetTracker.h:169
openvdb::v4_0_2::math::ISGradientNormSqrd
Definition: Operators.h:253
openvdb::v4_0_2::LEVEL_SET_HALF_WIDTH
static const Real LEVEL_SET_HALF_WIDTH
Definition: Types.h:270
openvdb::v4_0_2::tree::LeafManager::LeafRange::begin
Iterator begin() const
Definition: LeafManager.h:181
openvdb::v4_0_2::tools::LevelSetTracker::MaskTreeType
TreeType::template ValueConverter< ValueMask >::Type MaskTreeType
Definition: LevelSetTracker.h:77
openvdb::v4_0_2::tools::LevelSetTracker::State::spatialScheme
math::BiasedGradientScheme spatialScheme
Definition: LevelSetTracker.h:87
OPENVDB_NO_UNREACHABLE_CODE_WARNING_END
#define OPENVDB_NO_UNREACHABLE_CODE_WARNING_END
Definition: Platform.h:130
openvdb::v4_0_2::math::WENO5_BIAS
Definition: FiniteDifference.h:197
openvdb::v4_0_2::tools::LevelSetTracker::endInterrupter
void endInterrupter()
Definition: LevelSetTracker.h:367
openvdb::v4_0_2::tools::IGNORE_TILES
Definition: Morphology.h:100
openvdb::v4_0_2::tools::LevelSetTracker::TreeType
GridT::TreeType TreeType
Definition: LevelSetTracker.h:71
Grid.h
openvdb::v4_0_2::tools::LevelSetTracker::voxelSize
ValueType voxelSize() const
Definition: LevelSetTracker.h:175
openvdb::v4_0_2::TopologyCopy
Tag dispatch class that distinguishes topology copy constructors from deep copy constructors.
Definition: Types.h:503
openvdb::v4_0_2::math::BiasedGradientScheme
BiasedGradientScheme
Biased Gradients are limited to non-centered differences.
Definition: FiniteDifference.h:192
openvdb::v4_0_2::math::Sqrt
float Sqrt(float x)
Return the square root of a floating-point value.
Definition: Math.h:711
openvdb::v4_0_2::tools::LevelSetTracker::setTemporalScheme
void setTemporalScheme(math::TemporalIntegrationScheme scheme)
Set the spatial finite difference scheme.
Definition: LevelSetTracker.h:158
openvdb::v4_0_2::math::THIRD_BIAS
Definition: FiniteDifference.h:196
Stencils.h
Defines various finite difference stencils by means of the "curiously recurring template pattern" on ...
openvdb::v4_0_2::RuntimeError
Definition: Exceptions.h:90
openvdb::v4_0_2::math::Pow2
Type Pow2(Type x)
Return .
Definition: Math.h:498
openvdb::v4_0_2::tools::LevelSetTracker::leafs
LeafManagerType & leafs()
Definition: LevelSetTracker.h:186
OPENVDB_USE_VERSION_NAMESPACE
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:71
openvdb::v4_0_2::tools::LevelSetTracker
Performs multi-threaded interface tracking of narrow band level sets.
Definition: LevelSetTracker.h:67
LeafManager.h
A LeafManager manages a linear array of pointers to a given tree&#39;s leaf nodes, as well as optional au...
openvdb::v4_0_2::tools::LevelSetTracker::~LevelSetTracker
virtual ~LevelSetTracker()
Definition: LevelSetTracker.h:97
openvdb::v4_0_2::tools::erodeVoxels
void erodeVoxels(TreeType &tree, int iterations=1, NearestNeighbors nn=NN_FACE)
Topologically erode all leaf-level active voxels in the given tree.
Definition: Morphology.h:877
openvdb::v4_0_2::tools::LevelSetTracker::track
void track()
Track the level set interface, i.e. rebuild and normalize the narrow band of the level set...
Definition: LevelSetTracker.h:296
openvdb::v4_0_2::tools::LevelSetTracker::BufferType
LeafManagerType::BufferType BufferType
Definition: LevelSetTracker.h:76
openvdb::v4_0_2::math::Tolerance::value
static T value()
Definition: Math.h:117
openvdb::v4_0_2::tools::LevelSetTracker::State::State
State(math::BiasedGradientScheme s=math::HJWENO5_BIAS, math::TemporalIntegrationScheme t=math::TVD_RK1, int n=static_cast< int >(LEVEL_SET_HALF_WIDTH), int g=1)
Definition: LevelSetTracker.h:83
openvdb::v4_0_2::tools::LevelSetTracker::ValueType
TreeType::ValueType ValueType
Definition: LevelSetTracker.h:73
openvdb::v4_0_2::tools::LevelSetTracker::GridType
GridT GridType
Definition: LevelSetTracker.h:70
openvdb::v4_0_2::tools::LevelSetTracker::State::grainSize
int grainSize
Definition: LevelSetTracker.h:90
openvdb::v4_0_2::math::UNKNOWN_TIS
Definition: FiniteDifference.h:262
openvdb::v4_0_2::GRID_LEVEL_SET
Definition: Types.h:264
openvdb::v4_0_2::tools::LevelSetTracker::setGrainSize
void setGrainSize(int grainsize)
Set the grain-size used for multi-threading.
Definition: LevelSetTracker.h:173
openvdb::v4_0_2::tools::LevelSetTracker::LeafManagerType
tree::LeafManager< TreeType > LeafManagerType
Definition: LevelSetTracker.h:74
openvdb::v4_0_2::tools::LevelSetTracker::LeafRange
LeafManagerType::LeafRange LeafRange
Definition: LevelSetTracker.h:75
openvdb::v4_0_2::tools::LevelSetTracker::checkInterrupter
bool checkInterrupter()
Definition: LevelSetTracker.h:375
Operators.h