OpenVDB  4.0.1
LevelSetFilter.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 //
41 
42 #ifndef OPENVDB_TOOLS_LEVELSETFILTER_HAS_BEEN_INCLUDED
43 #define OPENVDB_TOOLS_LEVELSETFILTER_HAS_BEEN_INCLUDED
44 
45 #include <assert.h>
46 #include <boost/type_traits/is_floating_point.hpp>
47 #include "LevelSetTracker.h"
48 #include "Interpolation.h"
49 
50 namespace openvdb {
52 namespace OPENVDB_VERSION_NAME {
53 namespace tools {
54 
61 template<typename GridT,
62  typename MaskT = typename GridT::template ValueConverter<float>::Type,
63  typename InterruptT = util::NullInterrupter>
64 class LevelSetFilter : public LevelSetTracker<GridT, InterruptT>
65 {
66 public:
68  typedef GridT GridType;
69  typedef MaskT MaskType;
70  typedef typename GridType::TreeType TreeType;
71  typedef typename TreeType::ValueType ValueType;
72  typedef typename MaskType::ValueType AlphaType;
73  BOOST_STATIC_ASSERT(boost::is_floating_point<AlphaType>::value);
74 
78  LevelSetFilter(GridType& grid, InterruptT* interrupt = nullptr)
79  : BaseType(grid, interrupt)
80  , mMinMask(0)
81  , mMaxMask(1)
82  , mInvertMask(false)
83  {
84  }
86  virtual ~LevelSetFilter() {}
87 
90  AlphaType minMask() const { return mMinMask; }
93  AlphaType maxMask() const { return mMaxMask; }
101  void setMaskRange(AlphaType min, AlphaType max)
102  {
103  if (!(min < max)) OPENVDB_THROW(ValueError, "Invalid mask range (expects min < max)");
104  mMinMask = min;
105  mMaxMask = max;
106  }
107 
110  bool isMaskInverted() const { return mInvertMask; }
113  void invertMask(bool invert=true) { mInvertMask = invert; }
114 
117  void meanCurvature(const MaskType* mask = nullptr)
118  {
119  Filter f(this, mask); f.meanCurvature();
120  }
121 
124  void laplacian(const MaskType* mask = nullptr)
125  {
126  Filter f(this, mask); f.laplacian();
127  }
128 
135  void gaussian(int width = 1, const MaskType* mask = nullptr)
136  {
137  Filter f(this, mask); f.gaussian(width);
138  }
139 
143  void offset(ValueType offset, const MaskType* mask = nullptr)
144  {
145  Filter f(this, mask); f.offset(offset);
146  }
147 
154  void median(int width = 1, const MaskType* mask = nullptr)
155  {
156  Filter f(this, mask); f.median(width);
157  }
158 
164  void mean(int width = 1, const MaskType* mask = nullptr)
165  {
166  Filter f(this, mask); f.mean(width);
167  }
168 
169 private:
170  // disallow copy construction and copy by assignment!
171  LevelSetFilter(const LevelSetFilter&);// not implemented
172  LevelSetFilter& operator=(const LevelSetFilter&);// not implemented
173 
174  // Private struct that implements all the filtering.
175  struct Filter
176  {
177  typedef typename TreeType::LeafNodeType LeafT;
178  typedef typename LeafT::ValueOnIter VoxelIterT;
179  typedef typename LeafT::ValueOnCIter VoxelCIterT;
180  typedef typename tree::LeafManager<TreeType>::BufferType BufferT;
182  typedef typename LeafRange::Iterator LeafIterT;
183  typedef tools::AlphaMask<GridT, MaskT> AlphaMaskT;
184 
185  Filter(LevelSetFilter* parent, const MaskType* mask) : mParent(parent), mMask(mask) {}
186  Filter(const Filter&) = default;
187  virtual ~Filter() {}
188 
189  void box(int width);
190  void median(int width);
191  void mean(int width);
192  void gaussian(int width);
193  void laplacian();
194  void meanCurvature();
195  void offset(ValueType value);
196  void operator()(const LeafRange& r) const
197  {
198  if (mTask) mTask(const_cast<Filter*>(this), r);
199  else OPENVDB_THROW(ValueError, "task is undefined - don\'t call this method directly");
200  }
201  void cook(bool swap)
202  {
203  const int n = mParent->getGrainSize();
204  if (n>0) {
205  tbb::parallel_for(mParent->leafs().leafRange(n), *this);
206  } else {
207  (*this)(mParent->leafs().leafRange());
208  }
209  if (swap) mParent->leafs().swapLeafBuffer(1, n==0);
210  }
211 
212  template <size_t Axis>
213  struct Avg {
214  Avg(const GridT& grid, Int32 w) :
215  acc(grid.tree()), width(w), frac(1/ValueType(2*w+1)) {}
216  inline ValueType operator()(Coord xyz)
217  {
218  ValueType sum = zeroVal<ValueType>();
219  Int32& i = xyz[Axis], j = i + width;
220  for (i -= width; i <= j; ++i) sum += acc.getValue(xyz);
221  return sum*frac;
222  }
223  typename GridT::ConstAccessor acc;
224  const Int32 width;
225  const ValueType frac;
226  };
227 
228  template <typename AvgT>
229  void box( const LeafRange& r, Int32 w);
230 
231  void boxX(const LeafRange& r, Int32 w) { this->box<Avg<0> >(r,w); }
232  void boxZ(const LeafRange& r, Int32 w) { this->box<Avg<1> >(r,w); }
233  void boxY(const LeafRange& r, Int32 w) { this->box<Avg<2> >(r,w); }
234 
235  void median(const LeafRange&, int);
236  void meanCurvature(const LeafRange&);
237  void laplacian(const LeafRange&);
238  void offset(const LeafRange&, ValueType);
239 
240  LevelSetFilter* mParent;
241  const MaskType* mMask;
242  typename boost::function<void (Filter*, const LeafRange&)> mTask;
243  }; // end of private Filter struct
244 
245  AlphaType mMinMask, mMaxMask;
246  bool mInvertMask;
247 
248 }; // end of LevelSetFilter class
249 
250 
252 
253 template<typename GridT, typename MaskT, typename InterruptT>
254 inline void
256 Filter::median(int width)
257 {
258  mParent->startInterrupter("Median-value flow of level set");
259 
260  mParent->leafs().rebuildAuxBuffers(1, mParent->getGrainSize()==0);
261 
262  mTask = boost::bind(&Filter::median, _1, _2, std::max(1, width));
263  this->cook(true);
264 
265  mParent->track();
266 
267  mParent->endInterrupter();
268 }
269 
270 template<typename GridT, typename MaskT, typename InterruptT>
271 inline void
273 Filter::mean(int width)
274 {
275  mParent->startInterrupter("Mean-value flow of level set");
276 
277  this->box(width);
278 
279  mParent->endInterrupter();
280 }
281 
282 template<typename GridT, typename MaskT, typename InterruptT>
283 inline void
285 Filter::gaussian(int width)
286 {
287  mParent->startInterrupter("Gaussian flow of level set");
288 
289  for (int n=0; n<4; ++n) this->box(width);
290 
291  mParent->endInterrupter();
292 }
293 
294 template<typename GridT, typename MaskT, typename InterruptT>
295 inline void
297 Filter::box(int width)
298 {
299  mParent->leafs().rebuildAuxBuffers(1, mParent->getGrainSize()==0);
300 
301  width = std::max(1, width);
302 
303  mTask = boost::bind(&Filter::boxX, _1, _2, width);
304  this->cook(true);
305 
306  mTask = boost::bind(&Filter::boxY, _1, _2, width);
307  this->cook(true);
308 
309  mTask = boost::bind(&Filter::boxZ, _1, _2, width);
310  this->cook(true);
311 
312  mParent->track();
313 }
314 
315 template<typename GridT, typename MaskT, typename InterruptT>
316 inline void
319 {
320  mParent->startInterrupter("Mean-curvature flow of level set");
321 
322  mParent->leafs().rebuildAuxBuffers(1, mParent->getGrainSize()==0);
323 
324  mTask = boost::bind(&Filter::meanCurvature, _1, _2);
325  this->cook(true);
326 
327  mParent->track();
328 
329  mParent->endInterrupter();
330 }
331 
332 template<typename GridT, typename MaskT, typename InterruptT>
333 inline void
336 {
337  mParent->startInterrupter("Laplacian flow of level set");
338 
339  mParent->leafs().rebuildAuxBuffers(1, mParent->getGrainSize()==0);
340 
341  mTask = boost::bind(&Filter::laplacian, _1, _2);
342  this->cook(true);
343 
344  mParent->track();
345 
346  mParent->endInterrupter();
347 }
348 
349 template<typename GridT, typename MaskT, typename InterruptT>
350 inline void
353 {
354  mParent->startInterrupter("Offsetting level set");
355 
356  mParent->leafs().removeAuxBuffers();// no auxiliary buffers required
357 
358  const ValueType CFL = ValueType(0.5) * mParent->voxelSize(), offset = openvdb::math::Abs(value);
359  ValueType dist = 0.0;
360  while (offset-dist > ValueType(0.001)*CFL && mParent->checkInterrupter()) {
361  const ValueType delta = openvdb::math::Min(offset-dist, CFL);
362  dist += delta;
363 
364  mTask = boost::bind(&Filter::offset, _1, _2, copysign(delta, value));
365  this->cook(false);
366 
367  mParent->track();
368  }
369 
370  mParent->endInterrupter();
371 }
372 
373 
375 
377 template<typename GridT, typename MaskT, typename InterruptT>
378 inline void
380 Filter::meanCurvature(const LeafRange& range)
381 {
382  mParent->checkInterrupter();
383  //const float CFL = 0.9f, dt = CFL * mDx * mDx / 6.0f;
384  const ValueType dx = mParent->voxelSize(), dt = math::Pow2(dx) / ValueType(3.0);
385  math::CurvatureStencil<GridType> stencil(mParent->grid(), dx);
386  if (mMask) {
387  typename AlphaMaskT::FloatType a, b;
388  AlphaMaskT alpha(mParent->grid(), *mMask, mParent->minMask(),
389  mParent->maxMask(), mParent->isMaskInverted());
390  for (LeafIterT leafIter=range.begin(); leafIter; ++leafIter) {
391  ValueType* buffer = leafIter.buffer(1).data();
392  for (VoxelCIterT iter = leafIter->cbeginValueOn(); iter; ++iter) {
393  if (alpha(iter.getCoord(), a, b)) {
394  stencil.moveTo(iter);
395  const ValueType phi0 = *iter, phi1 = phi0 + dt*stencil.meanCurvatureNormGrad();
396  buffer[iter.pos()] = b * phi0 + a * phi1;
397  }
398  }
399  }
400  } else {
401  for (LeafIterT leafIter=range.begin(); leafIter; ++leafIter) {
402  ValueType* buffer = leafIter.buffer(1).data();
403  for (VoxelCIterT iter = leafIter->cbeginValueOn(); iter; ++iter) {
404  stencil.moveTo(iter);
405  buffer[iter.pos()] = *iter + dt*stencil.meanCurvatureNormGrad();
406  }
407  }
408  }
409 }
410 
418 template<typename GridT, typename MaskT, typename InterruptT>
419 inline void
421 Filter::laplacian(const LeafRange& range)
422 {
423  mParent->checkInterrupter();
424  //const float CFL = 0.9f, half_dt = CFL * mDx * mDx / 12.0f;
425  const ValueType dx = mParent->voxelSize(), dt = math::Pow2(dx) / ValueType(6.0);
426  math::GradStencil<GridType> stencil(mParent->grid(), dx);
427  if (mMask) {
428  typename AlphaMaskT::FloatType a, b;
429  AlphaMaskT alpha(mParent->grid(), *mMask, mParent->minMask(),
430  mParent->maxMask(), mParent->isMaskInverted());
431  for (LeafIterT leafIter=range.begin(); leafIter; ++leafIter) {
432  ValueType* buffer = leafIter.buffer(1).data();
433  for (VoxelCIterT iter = leafIter->cbeginValueOn(); iter; ++iter) {
434  if (alpha(iter.getCoord(), a, b)) {
435  stencil.moveTo(iter);
436  const ValueType phi0 = *iter, phi1 = phi0 + dt*stencil.laplacian();
437  buffer[iter.pos()] = b * phi0 + a * phi1;
438  }
439  }
440  }
441  } else {
442  for (LeafIterT leafIter=range.begin(); leafIter; ++leafIter) {
443  ValueType* buffer = leafIter.buffer(1).data();
444  for (VoxelCIterT iter = leafIter->cbeginValueOn(); iter; ++iter) {
445  stencil.moveTo(iter);
446  buffer[iter.pos()] = *iter + dt*stencil.laplacian();
447  }
448  }
449  }
450 }
451 
453 template<typename GridT, typename MaskT, typename InterruptT>
454 inline void
456 Filter::offset(const LeafRange& range, ValueType offset)
457 {
458  mParent->checkInterrupter();
459  if (mMask) {
460  typename AlphaMaskT::FloatType a, b;
461  AlphaMaskT alpha(mParent->grid(), *mMask, mParent->minMask(),
462  mParent->maxMask(), mParent->isMaskInverted());
463  for (LeafIterT leafIter=range.begin(); leafIter; ++leafIter) {
464  for (VoxelIterT iter = leafIter->beginValueOn(); iter; ++iter) {
465  if (alpha(iter.getCoord(), a, b)) iter.setValue(*iter + a*offset);
466  }
467  }
468  } else {
469  for (LeafIterT leafIter=range.begin(); leafIter; ++leafIter) {
470  for (VoxelIterT iter = leafIter->beginValueOn(); iter; ++iter) {
471  iter.setValue(*iter + offset);
472  }
473  }
474  }
475 }
476 
478 template<typename GridT, typename MaskT, typename InterruptT>
479 inline void
481 Filter::median(const LeafRange& range, int width)
482 {
483  mParent->checkInterrupter();
484  typename math::DenseStencil<GridType> stencil(mParent->grid(), width);//creates local cache!
485  if (mMask) {
486  typename AlphaMaskT::FloatType a, b;
487  AlphaMaskT alpha(mParent->grid(), *mMask, mParent->minMask(),
488  mParent->maxMask(), mParent->isMaskInverted());
489  for (LeafIterT leafIter=range.begin(); leafIter; ++leafIter) {
490  ValueType* buffer = leafIter.buffer(1).data();
491  for (VoxelCIterT iter = leafIter->cbeginValueOn(); iter; ++iter) {
492  if (alpha(iter.getCoord(), a, b)) {
493  stencil.moveTo(iter);
494  buffer[iter.pos()] = b * (*iter) + a * stencil.median();
495  }
496  }
497  }
498  } else {
499  for (LeafIterT leafIter=range.begin(); leafIter; ++leafIter) {
500  ValueType* buffer = leafIter.buffer(1).data();
501  for (VoxelCIterT iter = leafIter->cbeginValueOn(); iter; ++iter) {
502  stencil.moveTo(iter);
503  buffer[iter.pos()] = stencil.median();
504  }
505  }
506  }
507 }
508 
510 template<typename GridT, typename MaskT, typename InterruptT>
511 template <typename AvgT>
512 inline void
514 Filter::box(const LeafRange& range, Int32 w)
515 {
516  mParent->checkInterrupter();
517  AvgT avg(mParent->grid(), w);
518  if (mMask) {
519  typename AlphaMaskT::FloatType a, b;
520  AlphaMaskT alpha(mParent->grid(), *mMask, mParent->minMask(),
521  mParent->maxMask(), mParent->isMaskInverted());
522  for (LeafIterT leafIter=range.begin(); leafIter; ++leafIter) {
523  ValueType* buffer = leafIter.buffer(1).data();
524  for (VoxelCIterT iter = leafIter->cbeginValueOn(); iter; ++iter) {
525  const Coord xyz = iter.getCoord();
526  if (alpha(xyz, a, b)) buffer[iter.pos()] = b * (*iter)+ a * avg(xyz);
527  }
528  }
529  } else {
530  for (LeafIterT leafIter=range.begin(); leafIter; ++leafIter) {
531  ValueType* buffer = leafIter.buffer(1).data();
532  for (VoxelCIterT iter = leafIter->cbeginValueOn(); iter; ++iter) {
533  buffer[iter.pos()] = avg(iter.getCoord());
534  }
535  }
536  }
537 }
538 
539 } // namespace tools
540 } // namespace OPENVDB_VERSION_NAME
541 } // namespace openvdb
542 
543 #endif // OPENVDB_TOOLS_LEVELSETFILTER_HAS_BEEN_INCLUDED
544 
545 // Copyright (c) 2012-2017 DreamWorks Animation LLC
546 // All rights reserved. This software is distributed under the
547 // Mozilla Public License 2.0 ( http://www.mozilla.org/MPL/2.0/ )
Performs multi-threaded interface tracking of narrow band level sets. This is the building-block for ...
bool isMaskInverted() const
Return true if the mask is inverted, i.e. min->max in the original mask maps to 1->0 in the inverted ...
Definition: LevelSetFilter.h:110
Definition: Stencils.h:1205
Avg(const GridT &grid, Int32 w)
Definition: LevelSetFilter.h:214
Coord Abs(const Coord &xyz)
Definition: Coord.h:254
const Type & Min(const Type &a, const Type &b)
Return the minimum of two values.
Definition: Math.h:622
#define OPENVDB_THROW(exception, message)
Definition: Exceptions.h:101
Definition: Interpolation.h:570
void invertMask(bool invert=true)
Invert the optional mask, i.e. min->max in the original mask maps to 1->0 in the inverted alpha mask...
Definition: LevelSetFilter.h:113
virtual ~LevelSetFilter()
Default destructor.
Definition: LevelSetFilter.h:86
void startInterrupter(const char *msg)
Definition: LevelSetTracker.h:359
void median(int width=1, const MaskType *mask=nullptr)
One iteration of median-value flow of the level set.
Definition: LevelSetFilter.h:154
MaskT MaskType
Definition: LevelSetFilter.h:69
const ValueType frac
Definition: LevelSetFilter.h:225
const boost::disable_if_c< VecTraits< T >::IsVec, T >::type & min(const T &a, const T &b)
Definition: Composite.h:128
#define OPENVDB_VERSION_NAME
Definition: version.h:43
FloatT FloatType
Definition: Interpolation.h:578
void gaussian(int width=1, const MaskType *mask=nullptr)
One iteration of a fast separable Gaussian filter.
Definition: LevelSetFilter.h:135
Definition: Exceptions.h:39
void mean(int width=1, const MaskType *mask=nullptr)
One iteration of mean-value flow of the level set.
Definition: LevelSetFilter.h:164
const boost::disable_if_c< VecTraits< T >::IsVec, T >::type & max(const T &a, const T &b)
Definition: Composite.h:132
GridT::ConstAccessor acc
Definition: LevelSetFilter.h:223
AlphaType minMask() const
Return the minimum value of the mask to be used for the derivation of a smooth alpha value...
Definition: LevelSetFilter.h:90
GridType::TreeType TreeType
Definition: LevelSetFilter.h:70
void rebuildAuxBuffers(size_t auxBuffersPerLeaf, bool serial=false)
Change the number of auxiliary buffers.
Definition: LeafManager.h:312
void meanCurvature(const MaskType *mask=nullptr)
One iteration of mean-curvature flow of the level set.
Definition: LevelSetFilter.h:117
Definition: Exceptions.h:92
Definition: Stencils.h:1479
ValueType operator()(Coord xyz)
Definition: LevelSetFilter.h:216
void laplacian(const MaskType *mask=nullptr)
One iteration of Laplacian flow of the level set.
Definition: LevelSetFilter.h:124
Axis
Definition: Math.h:856
void offset(ValueType offset, const MaskType *mask=nullptr)
Offset the level set by the specified (world) distance.
Definition: LevelSetFilter.h:143
Performs multi-threaded interface tracking of narrow band level sets.
Definition: LevelSetTracker.h:67
GridType::Ptr meanCurvature(const GridType &grid, bool threaded, InterruptT *interrupt)
Compute the mean curvature of the given grid.
Definition: GridOperators.h:1057
LevelSetFilter(GridType &grid, InterruptT *interrupt=nullptr)
Main constructor from a grid.
Definition: LevelSetFilter.h:78
LeafManagerType & leafs()
Definition: LevelSetTracker.h:186
Filtering (e.g. diffusion) of narrow-band level sets. An optional scalar field can be used to produce...
Definition: LevelSetFilter.h:64
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:71
MaskType::ValueType AlphaType
Definition: LevelSetFilter.h:72
TreeType::ValueType ValueType
Definition: LevelSetFilter.h:71
LevelSetTracker< GridT, InterruptT > BaseType
Definition: LevelSetFilter.h:67
int32_t Int32
Definition: Types.h:59
Dense stencil of a given width.
Definition: Stencils.h:1617
GridType::Ptr laplacian(const GridType &grid, bool threaded, InterruptT *interrupt)
Compute the Laplacian of the given scalar grid.
Definition: GridOperators.h:1040
void setMaskRange(AlphaType min, AlphaType max)
Define the range for the (optional) scalar mask.
Definition: LevelSetFilter.h:101
Iterator begin() const
Definition: LeafManager.h:186
AlphaType maxMask() const
Return the maximum value of the mask to be used for the derivation of a smooth alpha value...
Definition: LevelSetFilter.h:93
CopyConstness< TreeType, NonConstBufferType >::Type BufferType
Definition: LeafManager.h:126
GridT GridType
Definition: LevelSetFilter.h:68
bool checkInterrupter()
Definition: LevelSetTracker.h:375
Type Pow2(Type x)
Return .
Definition: Math.h:514
const Int32 width
Definition: LevelSetFilter.h:224