/usr/include/openvdb/tools/LevelSetTracker.h is in libopenvdb-dev 3.1.0-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 | ///////////////////////////////////////////////////////////////////////////
//
// Copyright (c) 2012-2015 DreamWorks Animation LLC
//
// All rights reserved. This software is distributed under the
// Mozilla Public License 2.0 ( http://www.mozilla.org/MPL/2.0/ )
//
// Redistributions of source code must retain the above copyright
// and license notice and the following restrictions and disclaimer.
//
// * Neither the name of DreamWorks Animation nor the names of
// its contributors may be used to endorse or promote products derived
// from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// IN NO EVENT SHALL THE COPYRIGHT HOLDERS' AND CONTRIBUTORS' AGGREGATE
// LIABILITY FOR ALL CLAIMS REGARDLESS OF THEIR BASIS EXCEED US$250.00.
//
///////////////////////////////////////////////////////////////////////////
//
/// @author Ken Museth
///
/// @file LevelSetTracker.h
///
/// @brief Performs multi-threaded interface tracking of narrow band
/// level sets. This is the building-block for most level set
/// computations that involve dynamic topology, e.g. advection.
#ifndef OPENVDB_TOOLS_LEVEL_SET_TRACKER_HAS_BEEN_INCLUDED
#define OPENVDB_TOOLS_LEVEL_SET_TRACKER_HAS_BEEN_INCLUDED
#include <tbb/parallel_for.h>
#include <boost/bind.hpp>
#include <boost/function.hpp>
#include <boost/type_traits/is_floating_point.hpp>
#include <openvdb/Types.h>
#include <openvdb/math/Math.h>
#include <openvdb/math/FiniteDifference.h>
#include <openvdb/math/Operators.h>
#include <openvdb/math/Stencils.h>
#include <openvdb/math/Transform.h>
#include <openvdb/Grid.h>
#include <openvdb/util/NullInterrupter.h>
#include <openvdb/tree/ValueAccessor.h>
#include <openvdb/tree/LeafManager.h>
#include "ChangeBackground.h"// for changeLevelSetBackground
#include "Morphology.h"//for dilateVoxels
#include "Prune.h"// for pruneLevelSet
namespace openvdb {
OPENVDB_USE_VERSION_NAMESPACE
namespace OPENVDB_VERSION_NAME {
namespace tools {
/// @brief Performs multi-threaded interface tracking of narrow band level sets
template<typename GridT, typename InterruptT = util::NullInterrupter>
class LevelSetTracker
{
public:
typedef GridT GridType;
typedef typename GridT::TreeType TreeType;
typedef typename TreeType::LeafNodeType LeafType;
typedef typename TreeType::ValueType ValueType;
typedef typename tree::LeafManager<TreeType> LeafManagerType; // leafs + buffers
typedef typename LeafManagerType::LeafRange LeafRange;
typedef typename LeafManagerType::BufferType BufferType;
typedef typename TreeType::template ValueConverter<bool>::Type BoolMaskType;
BOOST_STATIC_ASSERT(boost::is_floating_point<ValueType>::value);
/// Lightweight struct that stores the state of the LevelSetTracker
struct 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)
: spatialScheme(s), temporalScheme(t), normCount(n), grainSize(g) {}
math::BiasedGradientScheme spatialScheme;
math::TemporalIntegrationScheme temporalScheme;
int normCount;// Number of iterations of normalization
int grainSize;
};
/// @brief Main constructor
/// @throw RuntimeError if the grid is not a level set
LevelSetTracker(GridT& grid, InterruptT* interrupt = NULL);
virtual ~LevelSetTracker() { delete mLeafs; }
/// @brief Iterative normalization, i.e. solving the Eikonal equation
/// @note The mask it optional and by default it is ignored.
template <typename MaskType>
void normalize(const MaskType* mask);
/// @brief Iterative normalization, i.e. solving the Eikonal equation
void normalize() { this->normalize<BoolMaskType>(NULL); }
/// @brief Track the level set interface, i.e. rebuild and normalize the
/// narrow band of the level set.
void track();
/// @brief Remove voxels that are outside the narrow band. (substep of track)
void prune();
/// @brief Fast but approximate dilation of the narrow band - one
/// layer at a time. Normally we recommend using the resize method below
/// which internally calls dilate (or erode) with the correct
/// number of @a iterations to achieve the desired half voxel width
/// of the narrow band (3 is recomended for most level set applications).
///
/// @note Since many level set applications perform
/// interface-tracking, which in turn rebuilds the narrow-band
/// accurately, this dilate method can often be used with a
/// single iterations of low-order re-normalization. This
/// effectively allows very narrow bands to be created from points
/// or polygons (e.g. with a half voxel width of 1), followed by a
/// fast but approximate dilation (typically with a half voxel
/// width of 3). This can be significantly faster than generating
/// the final width of the narrow band from points or polygons.
void dilate(int iterations = 1);
/// @brief Erodes the width of the narrow-band and update the background values
/// @throw ValueError if @a iterations is larger than the current half-width.
void erode(int iterations = 1);
/// @brief Resize the width of the narrow band, i.e. perform
/// dilation and renormalization or erosion as required.
bool resize(Index halfWidth = static_cast<Index>(LEVEL_SET_HALF_WIDTH));
/// @brief Return the half width of the narrow band in floating-point voxel units.
ValueType getHalfWidth() const { return mGrid->background()/mDx; }
/// @brief Return the state of the tracker (see struct defined above)
State getState() const { return mState; }
/// @brief Set the state of the tracker (see struct defined above)
void setState(const State& s) { mState =s; }
/// @return the spatial finite difference scheme
math::BiasedGradientScheme getSpatialScheme() const { return mState.spatialScheme; }
/// @brief Set the spatial finite difference scheme
void setSpatialScheme(math::BiasedGradientScheme scheme) { mState.spatialScheme = scheme; }
/// @return the temporal integration scheme
math::TemporalIntegrationScheme getTemporalScheme() const { return mState.temporalScheme; }
/// @brief Set the spatial finite difference scheme
void setTemporalScheme(math::TemporalIntegrationScheme scheme) { mState.temporalScheme = scheme;}
/// @return The number of normalizations performed per track or
/// normalize call.
int getNormCount() const { return mState.normCount; }
/// @brief Set the number of normalizations performed per track or
/// normalize call.
void setNormCount(int n) { mState.normCount = n; }
/// @return the grain-size used for multi-threading
int getGrainSize() const { return mState.grainSize; }
/// @brief Set the grain-size used for multi-threading.
/// @note A grainsize of 0 or less disables multi-threading!
void setGrainSize(int grainsize) { mState.grainSize = grainsize; }
ValueType voxelSize() const { return mDx; }
void startInterrupter(const char* msg);
void endInterrupter();
/// @return false if the process was interrupted
bool checkInterrupter();
const GridType& grid() const { return *mGrid; }
LeafManagerType& leafs() { return *mLeafs; }
const LeafManagerType& leafs() const { return *mLeafs; }
private:
// disallow copy construction and copy by assignment!
LevelSetTracker(const LevelSetTracker&);// not implemented
LevelSetTracker& operator=(const LevelSetTracker&);// not implemented
// Private class to perform multi-threaded trimming of
// voxels that are too far away from the zero-crossing.
struct Trim
{
Trim(LevelSetTracker& tracker) : mTracker(tracker) {}
void trim();
void operator()(const LeafRange& r) const;
LevelSetTracker& mTracker;
};// Trim
// Private struct to perform multi-threaded normalization
template<math::BiasedGradientScheme SpatialScheme,
math::TemporalIntegrationScheme TemporalScheme,
typename MaskT>
struct Normalizer
{
typedef math::BIAS_SCHEME<SpatialScheme> SchemeT;
typedef typename SchemeT::template ISStencil<GridType>::StencilType StencilT;
typedef typename MaskT::LeafNodeType MaskLeafT;
typedef typename MaskLeafT::ValueOnCIter MaskIterT;
typedef typename LeafType::ValueOnCIter VoxelIterT;
Normalizer(LevelSetTracker& tracker, const MaskT* mask);
void normalize();
void operator()(const LeafRange& r) const {mTask(const_cast<Normalizer*>(this), r);}
void cook(int swapBuffer=0);
template <int Nominator, int Denominator>
void euler(const LeafRange& range, Index phiBuffer, Index resultBuffer);
inline void euler01(const LeafRange& r) {this->euler<0,1>(r, 0, 1);}
inline void euler12(const LeafRange& r) {this->euler<1,2>(r, 1, 1);}
inline void euler34(const LeafRange& r) {this->euler<3,4>(r, 1, 2);}
inline void euler13(const LeafRange& r) {this->euler<1,3>(r, 1, 2);}
template <int Nominator, int Denominator>
void eval(StencilT& stencil, const ValueType* phi, ValueType* result, Index n) const;
LevelSetTracker& mTracker;
const MaskT* mMask;
const ValueType mDt, mInvDx;
typename boost::function<void (Normalizer*, const LeafRange&)> mTask;
}; // Normalizer struct
template<math::BiasedGradientScheme SpatialScheme, typename MaskT>
void normalize1(const MaskT* mask);
template<math::BiasedGradientScheme SpatialScheme,
math::TemporalIntegrationScheme TemporalScheme, typename MaskT>
void normalize2(const MaskT* mask);
// Throughout the methods below mLeafs is always assumed to contain
// a list of the current LeafNodes! The auxiliary buffers on the
// other hand always have to be allocated locally, since some
// methods need them and others don't!
GridType* mGrid;
LeafManagerType* mLeafs;
InterruptT* mInterrupter;
const ValueType mDx;
State mState;
}; // end of LevelSetTracker class
template<typename GridT, typename InterruptT>
LevelSetTracker<GridT, InterruptT>::
LevelSetTracker(GridT& grid, InterruptT* interrupt):
mGrid(&grid),
mLeafs(new LeafManagerType(grid.tree())),
mInterrupter(interrupt),
mDx(static_cast<ValueType>(grid.voxelSize()[0])),
mState()
{
if ( !grid.hasUniformVoxels() ) {
OPENVDB_THROW(RuntimeError,
"The transform must have uniform scale for the LevelSetTracker to function");
}
if ( grid.getGridClass() != GRID_LEVEL_SET) {
OPENVDB_THROW(RuntimeError,
"LevelSetTracker expected a level set, got a grid of class \""
+ grid.gridClassToString(grid.getGridClass())
+ "\" [hint: Grid::setGridClass(openvdb::GRID_LEVEL_SET)]");
}
}
template<typename GridT, typename InterruptT>
inline void
LevelSetTracker<GridT, InterruptT>::
prune()
{
this->startInterrupter("Pruning Level Set");
// Prune voxels that are too far away from the zero-crossing
Trim t(*this);
t.trim();
// Remove inactive nodes from tree
tools::pruneLevelSet(mGrid->tree());
// The tree topology has changes so rebuild the list of leafs
mLeafs->rebuildLeafArray();
this->endInterrupter();
}
template<typename GridT, typename InterruptT>
inline void
LevelSetTracker<GridT, InterruptT>::
track()
{
// Dilate narrow-band (this also rebuilds the leaf array!)
tools::dilateVoxels(*mLeafs);
// Compute signed distances in dilated narrow-band
this->normalize();
// Remove voxels that are outside the narrow band
this->prune();
}
template<typename GridT, typename InterruptT>
inline void
LevelSetTracker<GridT, InterruptT>::
dilate(int iterations)
{
if (this->getNormCount() == 0) {
for (int i=0; i < iterations; ++i) {
tools::dilateVoxels(*mLeafs);
mLeafs->rebuildLeafArray();
tools::changeLevelSetBackground(this->leafs(), mDx + mGrid->background());
}
} else {
for (int i=0; i < iterations; ++i) {
BoolMaskType mask0(mGrid->tree(), false, TopologyCopy());
tools::dilateVoxels(*mLeafs);
mLeafs->rebuildLeafArray();
tools::changeLevelSetBackground(this->leafs(), mDx + mGrid->background());
BoolMaskType mask(mGrid->tree(), false, TopologyCopy());
mask.topologyDifference(mask0);
this->normalize(&mask);
}
}
}
template<typename GridT, typename InterruptT>
inline void
LevelSetTracker<GridT, InterruptT>::
erode(int iterations)
{
tools::erodeVoxels(*mLeafs, iterations);
mLeafs->rebuildLeafArray();
const ValueType background = mGrid->background() - iterations*mDx;
tools::changeLevelSetBackground(this->leafs(), background);
}
template<typename GridT, typename InterruptT>
inline bool
LevelSetTracker<GridT, InterruptT>::
resize(Index halfWidth)
{
const int wOld = static_cast<int>(math::RoundDown(this->getHalfWidth()));
const int wNew = static_cast<int>(halfWidth);
if (wOld < wNew) {
this->dilate(wNew - wOld);
} else if (wOld > wNew) {
this->erode(wOld - wNew);
}
return wOld != wNew;
}
template<typename GridT, typename InterruptT>
inline void
LevelSetTracker<GridT, InterruptT>::
startInterrupter(const char* msg)
{
if (mInterrupter) mInterrupter->start(msg);
}
template<typename GridT, typename InterruptT>
inline void
LevelSetTracker<GridT, InterruptT>::
endInterrupter()
{
if (mInterrupter) mInterrupter->end();
}
template<typename GridT, typename InterruptT>
inline bool
LevelSetTracker<GridT, InterruptT>::
checkInterrupter()
{
if (util::wasInterrupted(mInterrupter)) {
tbb::task::self().cancel_group_execution();
return false;
}
return true;
}
template<typename GridT, typename InterruptT>
template<typename MaskT>
inline void
LevelSetTracker<GridT, InterruptT>::
normalize(const MaskT* mask)
{
switch (this->getSpatialScheme()) {
case math::FIRST_BIAS:
this->normalize1<math::FIRST_BIAS , MaskT>(mask); break;
case math::SECOND_BIAS:
this->normalize1<math::SECOND_BIAS, MaskT>(mask); break;
case math::THIRD_BIAS:
this->normalize1<math::THIRD_BIAS, MaskT>(mask); break;
case math::WENO5_BIAS:
this->normalize1<math::WENO5_BIAS, MaskT>(mask); break;
case math::HJWENO5_BIAS:
this->normalize1<math::HJWENO5_BIAS, MaskT>(mask); break;
default:
OPENVDB_THROW(ValueError, "Spatial difference scheme not supported!");
}
}
template<typename GridT, typename InterruptT>
template<math::BiasedGradientScheme SpatialScheme, typename MaskT>
inline void
LevelSetTracker<GridT, InterruptT>::
normalize1(const MaskT* mask)
{
switch (this->getTemporalScheme()) {
case math::TVD_RK1:
this->normalize2<SpatialScheme, math::TVD_RK1, MaskT>(mask); break;
case math::TVD_RK2:
this->normalize2<SpatialScheme, math::TVD_RK2, MaskT>(mask); break;
case math::TVD_RK3:
this->normalize2<SpatialScheme, math::TVD_RK3, MaskT>(mask); break;
default:
OPENVDB_THROW(ValueError, "Temporal integration scheme not supported!");
}
}
template<typename GridT, typename InterruptT>
template<math::BiasedGradientScheme SpatialScheme,
math::TemporalIntegrationScheme TemporalScheme,
typename MaskT>
inline void
LevelSetTracker<GridT, InterruptT>::
normalize2(const MaskT* mask)
{
Normalizer<SpatialScheme, TemporalScheme, MaskT> tmp(*this, mask);
tmp.normalize();
}
////////////////////////////////////////////////////////////////////////////
template<typename GridT, typename InterruptT>
inline void
LevelSetTracker<GridT, InterruptT>::
Trim::trim()
{
const int grainSize = mTracker.getGrainSize();
const LeafRange range = mTracker.leafs().leafRange(grainSize);
if (grainSize>0) {
tbb::parallel_for(range, *this);
} else {
(*this)(range);
}
}
/// Prunes away voxels that have moved outside the narrow band
template<typename GridT, typename InterruptT>
inline void
LevelSetTracker<GridT, InterruptT>::
Trim::operator()(const LeafRange& range) const
{
typedef typename LeafType::ValueOnIter VoxelIterT;
mTracker.checkInterrupter();
const ValueType gamma = mTracker.mGrid->background();
for (typename LeafRange::Iterator leafIter = range.begin(); leafIter; ++leafIter) {
LeafType &leaf = *leafIter;
for (VoxelIterT iter = leaf.beginValueOn(); iter; ++iter) {
const ValueType val = *iter;
if (val <= -gamma)
leaf.setValueOff(iter.pos(), -gamma);
else if (val >= gamma)
leaf.setValueOff(iter.pos(), gamma);
}
}
}
////////////////////////////////////////////////////////////////////////////
template<typename GridT, typename InterruptT>
template<math::BiasedGradientScheme SpatialScheme,
math::TemporalIntegrationScheme TemporalScheme,
typename MaskT>
inline
LevelSetTracker<GridT, InterruptT>::
Normalizer<SpatialScheme, TemporalScheme, MaskT>::
Normalizer(LevelSetTracker& tracker, const MaskT* mask)
: mTracker(tracker)
, mMask(mask)
, mDt(tracker.voxelSize()*(TemporalScheme == math::TVD_RK1 ? 0.3f :
TemporalScheme == math::TVD_RK2 ? 0.9f : 1.0f))
, mInvDx(1.0f/tracker.voxelSize())
, mTask(0)
{
}
template<typename GridT, typename InterruptT>
template<math::BiasedGradientScheme SpatialScheme,
math::TemporalIntegrationScheme TemporalScheme,
typename MaskT>
inline void
LevelSetTracker<GridT, InterruptT>::
Normalizer<SpatialScheme, TemporalScheme, MaskT>::
normalize()
{
/// Make sure we have enough temporal auxiliary buffers
mTracker.mLeafs->rebuildAuxBuffers(TemporalScheme == math::TVD_RK3 ? 2 : 1);
for (int n=0, e=mTracker.getNormCount(); n < e; ++n) {
OPENVDB_NO_UNREACHABLE_CODE_WARNING_BEGIN
switch(TemporalScheme) {//switch is resolved at compile-time
case math::TVD_RK1:
// Perform one explicit Euler step: t1 = t0 + dt
// Phi_t1(0) = Phi_t0(0) - dt * VdotG_t0(1)
mTask = boost::bind(&Normalizer::euler01, _1, _2);
// Cook and swap buffer 0 and 1 such that Phi_t1(0) and Phi_t0(1)
this->cook(1);
break;
case math::TVD_RK2:
// Perform one explicit Euler step: t1 = t0 + dt
// Phi_t1(1) = Phi_t0(0) - dt * VdotG_t0(1)
mTask = boost::bind(&Normalizer::euler01, _1, _2);
// Cook and swap buffer 0 and 1 such that Phi_t1(0) and Phi_t0(1)
this->cook(1);
// Convex combine explicit Euler step: t2 = t0 + dt
// Phi_t2(1) = 1/2 * Phi_t0(1) + 1/2 * (Phi_t1(0) - dt * V.Grad_t1(0))
mTask = boost::bind(&Normalizer::euler12, _1, _2);
// Cook and swap buffer 0 and 1 such that Phi_t2(0) and Phi_t1(1)
this->cook(1);
break;
case math::TVD_RK3:
// Perform one explicit Euler step: t1 = t0 + dt
// Phi_t1(1) = Phi_t0(0) - dt * VdotG_t0(1)
mTask = boost::bind(&Normalizer::euler01, _1, _2);
// Cook and swap buffer 0 and 1 such that Phi_t1(0) and Phi_t0(1)
this->cook(1);
// Convex combine explicit Euler step: t2 = t0 + dt/2
// Phi_t2(2) = 3/4 * Phi_t0(1) + 1/4 * (Phi_t1(0) - dt * V.Grad_t1(0))
mTask = boost::bind(&Normalizer::euler34, _1, _2);
// Cook and swap buffer 0 and 2 such that Phi_t2(0) and Phi_t1(2)
this->cook(2);
// Convex combine explicit Euler step: t3 = t0 + dt
// Phi_t3(2) = 1/3 * Phi_t0(1) + 2/3 * (Phi_t2(0) - dt * V.Grad_t2(0)
mTask = boost::bind(&Normalizer::euler13, _1, _2);
// Cook and swap buffer 0 and 2 such that Phi_t3(0) and Phi_t2(2)
this->cook(2);
break;
default:
OPENVDB_THROW(ValueError, "Temporal integration scheme not supported!");
}
OPENVDB_NO_UNREACHABLE_CODE_WARNING_END
}
mTracker.mLeafs->removeAuxBuffers();
}
/// Private method to perform the task (serial or threaded) and
/// subsequently swap the leaf buffers.
template<typename GridT, typename InterruptT>
template<math::BiasedGradientScheme SpatialScheme,
math::TemporalIntegrationScheme TemporalScheme,
typename MaskT>
inline void
LevelSetTracker<GridT, InterruptT>::
Normalizer<SpatialScheme, TemporalScheme, MaskT>::
cook(int swapBuffer)
{
mTracker.startInterrupter("Normalizing Level Set");
const int grainSize = mTracker.getGrainSize();
const LeafRange range = mTracker.leafs().leafRange(grainSize);
if (grainSize>0) {
tbb::parallel_for(range, *this);
} else {
(*this)(range);
}
mTracker.leafs().swapLeafBuffer(swapBuffer, grainSize==0);
mTracker.endInterrupter();
}
template<typename GridT, typename InterruptT>
template<math::BiasedGradientScheme SpatialScheme,
math::TemporalIntegrationScheme TemporalScheme,
typename MaskT>
template <int Nominator, int Denominator>
inline void
LevelSetTracker<GridT, InterruptT>::
Normalizer<SpatialScheme, TemporalScheme, MaskT>::
eval(StencilT& stencil, const ValueType* phi, ValueType* result, Index n) const
{
typedef typename math::ISGradientNormSqrd<SpatialScheme> GradientT;
static const ValueType alpha = ValueType(Nominator)/ValueType(Denominator);
static const ValueType beta = ValueType(1) - alpha;
const ValueType normSqGradPhi = GradientT::result(stencil);
const ValueType phi0 = stencil.getValue();
ValueType v = phi0 / ( math::Sqrt(math::Pow2(phi0) + normSqGradPhi) +
math::Tolerance<ValueType>::value() );
v = phi0 - mDt * v * (math::Sqrt(normSqGradPhi) * mInvDx - 1.0f);
result[n] = Nominator ? alpha * phi[n] + beta * v : v;
}
template<typename GridT, typename InterruptT>
template<math::BiasedGradientScheme SpatialScheme,
math::TemporalIntegrationScheme TemporalScheme,
typename MaskT>
template <int Nominator, int Denominator>
inline void
LevelSetTracker<GridT,InterruptT>::
Normalizer<SpatialScheme, TemporalScheme, MaskT>::
euler(const LeafRange& range, Index phiBuffer, Index resultBuffer)
{
typedef typename LeafType::ValueOnCIter VoxelIterT;
mTracker.checkInterrupter();
StencilT stencil(mTracker.grid());
for (typename LeafRange::Iterator leafIter = range.begin(); leafIter; ++leafIter) {
const ValueType* phi = leafIter.buffer(phiBuffer).data();
ValueType* result = leafIter.buffer(resultBuffer).data();
if (mMask == NULL) {
for (VoxelIterT iter = leafIter->cbeginValueOn(); iter; ++iter) {
stencil.moveTo(iter);
this->eval<Nominator, Denominator>(stencil, phi, result, iter.pos());
}//loop over active voxels in the leaf of the level set
} else if (const MaskLeafT* mask = mMask->probeLeaf(leafIter->origin())) {
const ValueType* phi0 = leafIter->buffer().data();
for (MaskIterT iter = mask->cbeginValueOn(); iter; ++iter) {
const Index i = iter.pos();
stencil.moveTo(iter.getCoord(), phi0[i]);
this->eval<Nominator, Denominator>(stencil, phi, result, i);
}//loop over active voxels in the leaf of the mask
}
}//loop over leafs of the level set
}
} // namespace tools
} // namespace OPENVDB_VERSION_NAME
} // namespace openvdb
#endif // OPENVDB_TOOLS_LEVEL_SET_TRACKER_HAS_BEEN_INCLUDED
// Copyright (c) 2012-2015 DreamWorks Animation LLC
// All rights reserved. This software is distributed under the
// Mozilla Public License 2.0 ( http://www.mozilla.org/MPL/2.0/ )
|