OpenVDB  2.1.0
Filter.h
Go to the documentation of this file.
1 //
3 // Copyright (c) 2012-2013 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_FILTER_HAS_BEEN_INCLUDED
40 #define OPENVDB_TOOLS_FILTER_HAS_BEEN_INCLUDED
41 
42 #include <tbb/parallel_reduce.h>
43 #include <tbb/parallel_for.h>
44 #include <boost/bind.hpp>
45 #include <boost/function.hpp>
46 #include <boost/type_traits/is_floating_point.hpp>
47 #include <openvdb/Types.h>
48 #include <openvdb/math/Math.h>
49 #include <openvdb/math/Stencils.h>
50 #include <openvdb/math/Transform.h>
51 #include <openvdb/tree/LeafManager.h>
52 #include <openvdb/util/NullInterrupter.h>
53 #include <openvdb/Grid.h>
54 #include "Interpolation.h"
55 
56 namespace openvdb {
58 namespace OPENVDB_VERSION_NAME {
59 namespace tools {
60 
64 template<typename GridT,
65  typename MaskT = typename GridT::template ValueConverter<float>::Type,
66  typename InterruptT = util::NullInterrupter>
67 class Filter
68 {
69 public:
70  typedef GridT GridType;
71  typedef MaskT MaskType;
72  typedef typename GridType::TreeType TreeType;
73  typedef typename TreeType::LeafNodeType LeafType;
74  typedef typename GridType::ValueType ValueType;
75  typedef typename MaskType::ValueType AlphaType;
79  BOOST_STATIC_ASSERT(boost::is_floating_point<AlphaType>::value);
80 
84  Filter(GridT& grid, InterruptT* interrupt = NULL)
85  : mGrid(&grid)
86  , mTask(0)
87  , mInterrupter(interrupt)
88  , mMask(NULL)
89  , mGrainSize(1)
90  , mMinMask(0)
91  , mMaxMask(1)
92  , mInvertMask(false)
93  {
94  }
95 
99  Filter(const Filter& other)
100  : mGrid(other.mGrid)
101  , mTask(other.mTask)
102  , mInterrupter(other.mInterrupter)
103  , mMask(other.mMask)
104  , mGrainSize(other.mGrainSize)
105  , mMinMask(other.mMinMask)
106  , mMaxMask(other.mMaxMask)
107  , mInvertMask(other.mInvertMask)
108  {
109  }
110 
112  int getGrainSize() const { return mGrainSize; }
115  void setGrainSize(int grainsize) { mGrainSize = grainsize; }
116 
119  AlphaType minMask() const { return mMinMask; }
122  AlphaType maxMask() const { return mMaxMask; }
130  {
131  if (!(min < max)) OPENVDB_THROW(ValueError, "Invalid mask range (expects min < max)");
132  mMinMask = min;
133  mMaxMask = max;
134  }
135 
138  bool isMaskInverted() const { return mInvertMask; }
141  void invertMask(bool invert=true) { mInvertMask = invert; }
142 
147  void mean(int width = 1, int iterations = 1, const MaskType* mask = NULL);
148 
156  void gaussian(int width = 1, int iterations = 1, const MaskType* mask = NULL);
157 
164  void median(int width = 1, int iterations = 1, const MaskType* mask = NULL);
165 
169  void offset(ValueType offset, const MaskType* mask = NULL);
170 
175  void operator()(const RangeType& range) const
176  {
177  if (mTask) mTask(const_cast<Filter*>(this), range);
178  else OPENVDB_THROW(ValueError, "task is undefined - call median(), mean(), etc.");
179  }
180 
181 private:
182  typedef typename TreeType::LeafNodeType LeafT;
183  typedef typename LeafT::ValueOnIter VoxelIterT;
184  typedef typename LeafT::ValueOnCIter VoxelCIterT;
185  typedef typename tree::LeafManager<TreeType>::BufferType BufferT;
186  typedef typename RangeType::Iterator LeafIterT;
187 
188  void cook(LeafManagerType& leafs);
189 
190  // Private class to derive the normalized alpha mask
191  struct AlphaMask
192  {
193  AlphaMask(const GridType& grid, const MaskType& mask,
194  AlphaType min, AlphaType max, bool invert)
195  : mSampler(mask, grid), mMin(min), mInvNorm(1/(max-min)), mInvert(invert)
196  {
197  assert(min < max);
198  }
199  inline bool operator()(const Coord& xyz, AlphaType& a, AlphaType& b) const
200  {
201  a = mSampler(xyz);
202  const AlphaType t = (a-mMin)*mInvNorm;
203  a = t > 0 ? t < 1 ? (3-2*t)*t*t : 1 : 0;//smooth mapping to 0->1
204  b = 1 - a;
205  if (mInvert) std::swap(a,b);
206  return a>0;
207  }
209  const AlphaType mMin, mInvNorm;
210  const bool mInvert;
211  };
212 
213  template <size_t Axis>
214  struct Avg {
215  Avg(const GridT* grid, Int32 w) :
216  acc(grid->tree()), width(w), frac(1/ValueType(2*w+1)) {}
217  ValueType operator()(Coord xyz) {
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  // Private filter methods called by tbb::parallel_for threads
229  template <typename AvgT>
230  void doBox( const RangeType& r, Int32 w);
231  void doBoxX(const RangeType& r, Int32 w) { this->doBox<Avg<0> >(r,w); }
232  void doBoxZ(const RangeType& r, Int32 w) { this->doBox<Avg<1> >(r,w); }
233  void doBoxY(const RangeType& r, Int32 w) { this->doBox<Avg<2> >(r,w); }
234  void doMedian(const RangeType&, int);
235  void doOffset(const RangeType&, ValueType);
237  bool wasInterrupted();
238 
239  GridType* mGrid;
240  typename boost::function<void (Filter*, const RangeType&)> mTask;
241  InterruptT* mInterrupter;
242  const MaskType* mMask;
243  int mGrainSize;
244  AlphaType mMinMask, mMaxMask;
245  bool mInvertMask;
246 }; // end of Filter class
247 
249 
250 template<typename GridT, typename MaskT, typename InterruptT>
251 inline void
252 Filter<GridT, MaskT, InterruptT>::mean(int width, int iterations, const MaskType* mask)
253 {
254  mMask = mask;
255 
256  if (mInterrupter) mInterrupter->start("Applying mean filter");
257 
258  const int w = std::max(1, width);
259 
260  LeafManagerType leafs(mGrid->tree(), 1, mGrainSize==0);
261 
262  for (int i=0; i<iterations && !this->wasInterrupted(); ++i) {
263  mTask = boost::bind(&Filter::doBoxX, _1, _2, w);
264  this->cook(leafs);
265 
266  mTask = boost::bind(&Filter::doBoxY, _1, _2, w);
267  this->cook(leafs);
268 
269  mTask = boost::bind(&Filter::doBoxZ, _1, _2, w);
270  this->cook(leafs);
271  }
272 
273  if (mInterrupter) mInterrupter->end();
274 }
275 
276 template<typename GridT, typename MaskT, typename InterruptT>
277 inline void
278 Filter<GridT, MaskT, InterruptT>::gaussian(int width, int iterations, const MaskType* mask)
279 {
280  mMask = mask;
281 
282  if (mInterrupter) mInterrupter->start("Applying gaussian filter");
283 
284  const int w = std::max(1, width);
285 
286  LeafManagerType leafs(mGrid->tree(), 1, mGrainSize==0);
287 
288  for (int i=0; i<iterations; ++i) {
289  for (int n=0; n<4 && !this->wasInterrupted(); ++n) {
290  mTask = boost::bind(&Filter::doBoxX, _1, _2, w);
291  this->cook(leafs);
292 
293  mTask = boost::bind(&Filter::doBoxY, _1, _2, w);
294  this->cook(leafs);
295 
296  mTask = boost::bind(&Filter::doBoxZ, _1, _2, w);
297  this->cook(leafs);
298  }
299  }
300 
301  if (mInterrupter) mInterrupter->end();
302 }
303 
304 
305 template<typename GridT, typename MaskT, typename InterruptT>
306 inline void
307 Filter<GridT, MaskT, InterruptT>::median(int width, int iterations, const MaskType* mask)
308 {
309  mMask = mask;
310 
311  if (mInterrupter) mInterrupter->start("Applying median filter");
312 
313  LeafManagerType leafs(mGrid->tree(), 1, mGrainSize==0);
314 
315  mTask = boost::bind(&Filter::doMedian, _1, _2, std::max(1, width));
316  for (int i=0; i<iterations && !this->wasInterrupted(); ++i) this->cook(leafs);
317 
318  if (mInterrupter) mInterrupter->end();
319 }
320 
321 template<typename GridT, typename MaskT, typename InterruptT>
322 inline void
324 {
325  mMask = mask;
326 
327  if (mInterrupter) mInterrupter->start("Applying offset");
328 
329  LeafManagerType leafs(mGrid->tree(), 0, mGrainSize==0);
330 
331  mTask = boost::bind(&Filter::doOffset, _1, _2, value);
332  this->cook(leafs);
333 
334  if (mInterrupter) mInterrupter->end();
335 }
336 
338 
339 
342 template<typename GridT, typename MaskT, typename InterruptT>
343 inline void
344 Filter<GridT, MaskT, InterruptT>::cook(LeafManagerType& leafs)
345 {
346  if (mGrainSize>0) {
347  tbb::parallel_for(leafs.leafRange(mGrainSize), *this);
348  } else {
349  (*this)(leafs.leafRange());
350  }
351  leafs.swapLeafBuffer(1, mGrainSize==0);
352 }
353 
355 template<typename GridT, typename MaskT, typename InterruptT>
356 template <typename AvgT>
357 inline void
358 Filter<GridT, MaskT, InterruptT>::doBox(const RangeType& range, Int32 w)
359 {
360  this->wasInterrupted();
361  AvgT avg(mGrid, w);
362  if (mMask) {
363  AlphaType a, b;
364  AlphaMask alpha(*mGrid, *mMask, mMinMask, mMaxMask, mInvertMask);
365  for (LeafIterT leafIter=range.begin(); leafIter; ++leafIter) {
366  BufferT& buffer = leafIter.buffer(1);
367  for (VoxelCIterT iter = leafIter->cbeginValueOn(); iter; ++iter) {
368  const Coord xyz = iter.getCoord();
369  if (alpha(xyz, a, b)) {
370  buffer.setValue(iter.pos(), ValueType(b*(*iter) + a*avg(xyz)));
371  }
372  }
373  }
374  } else {
375  for (LeafIterT leafIter=range.begin(); leafIter; ++leafIter) {
376  BufferT& buffer = leafIter.buffer(1);
377  for (VoxelCIterT iter = leafIter->cbeginValueOn(); iter; ++iter) {
378  buffer.setValue(iter.pos(), avg(iter.getCoord()));
379  }
380  }
381  }
382 }
383 
385 template<typename GridT, typename MaskT, typename InterruptT>
386 inline void
387 Filter<GridT, MaskT, InterruptT>::doMedian(const RangeType& range, int width)
388 {
389  this->wasInterrupted();
390  typename math::DenseStencil<GridType> stencil(*mGrid, width);//creates local cache!
391  if (mMask) {
392  AlphaType a, b;
393  AlphaMask alpha(*mGrid, *mMask, mMinMask, mMaxMask, mInvertMask);
394  for (LeafIterT leafIter=range.begin(); leafIter; ++leafIter) {
395  BufferT& buffer = leafIter.buffer(1);
396  for (VoxelCIterT iter = leafIter->cbeginValueOn(); iter; ++iter) {
397  if (alpha(iter.getCoord(), a, b)) {
398  stencil.moveTo(iter);
399  buffer.setValue(iter.pos(), ValueType(b*(*iter) + a*stencil.median()));
400  }
401  }
402  }
403  } else {
404  for (LeafIterT leafIter=range.begin(); leafIter; ++leafIter) {
405  BufferT& buffer = leafIter.buffer(1);
406  for (VoxelCIterT iter = leafIter->cbeginValueOn(); iter; ++iter) {
407  stencil.moveTo(iter);
408  buffer.setValue(iter.pos(), stencil.median());
409  }
410  }
411  }
412 }
413 
415 template<typename GridT, typename MaskT, typename InterruptT>
416 inline void
417 Filter<GridT, MaskT, InterruptT>::doOffset(const RangeType& range, ValueType offset)
418 {
419  this->wasInterrupted();
420  if (mMask) {
421  AlphaType a, b;
422  AlphaMask alpha(*mGrid, *mMask, mMinMask, mMaxMask, mInvertMask);
423  for (LeafIterT leafIter=range.begin(); leafIter; ++leafIter) {
424  for (VoxelIterT iter = leafIter->beginValueOn(); iter; ++iter) {
425  if (alpha(iter.getCoord(), a, b)) iter.setValue(ValueType(*iter + a*offset));
426  }
427  }
428  } else {
429  for (LeafIterT leafIter=range.begin(); leafIter; ++leafIter) {
430  for (VoxelIterT iter = leafIter->beginValueOn(); iter; ++iter) {
431  iter.setValue(*iter + offset);
432  }
433  }
434  }
435 }
436 
437 template<typename GridT, typename MaskT, typename InterruptT>
438 inline bool
440 {
441  if (util::wasInterrupted(mInterrupter)) {
442  tbb::task::self().cancel_group_execution();
443  return true;
444  }
445  return false;
446 }
447 
448 } // namespace tools
449 } // namespace OPENVDB_VERSION_NAME
450 } // namespace openvdb
451 
452 #endif // OPENVDB_TOOLS_FILTER_HAS_BEEN_INCLUDED
453 
454 // Copyright (c) 2012-2013 DreamWorks Animation LLC
455 // All rights reserved. This software is distributed under the
456 // Mozilla Public License 2.0 ( http://www.mozilla.org/MPL/2.0/ )
Filter(GridT &grid, InterruptT *interrupt=NULL)
Definition: Filter.h:84
OPENVDB_API Hermite min(const Hermite &, const Hermite &)
min and max operations done directly on the compressed data.
void operator()(const RangeType &range) const
Used internally by tbb::parallel_for()
Definition: Filter.h:175
#define OPENVDB_THROW(exception, message)
Definition: Exceptions.h:97
Definition: Exceptions.h:88
CopyConstness< TreeType, NonConstBufferType >::Type BufferType
Definition: LeafManager.h:117
void setGrainSize(int grainsize)
Set the grain-size used for multi-threading.
Definition: Filter.h:115
Volume filtering (e.g., diffusion) with optional alpha masking.
Definition: Filter.h:67
GridType::ValueType ValueType
Definition: Filter.h:74
tree::LeafManager< TreeType > LeafManagerType
Definition: Filter.h:76
LeafManagerType::LeafRange RangeType
Definition: Filter.h:77
#define OPENVDB_VERSION_NAME
Definition: version.h:45
This is a simple convenience class that allows for sampling from a source grid into the index space o...
Definition: Interpolation.h:385
const MaskGridType * mMask
Definition: GridOperators.h:386
int32_t Int32
Definition: Types.h:58
int getGrainSize() const
Definition: Filter.h:112
LeafManagerType::BufferType BufferType
Definition: Filter.h:78
GridT GridType
Definition: Filter.h:70
OPENVDB_API Hermite max(const Hermite &, const Hermite &)
min and max operations done directly on the compressed data.
MaskT MaskType
Definition: Filter.h:71
void invertMask(bool invert=true)
Invert the optional mask, i.e. min-&gt;max in the original mask maps to 1-&gt;0 in the inverted alpha mask...
Definition: Filter.h:141
bool isMaskInverted() const
Return true if the mask is inverted, i.e. min-&gt;max in the original mask maps to 1-&gt;0 in the inverted ...
Definition: Filter.h:138
Axis
Definition: Math.h:769
AlphaType minMask() const
Return the minimum value of the mask to be used for the derivation of a smooth alpha value...
Definition: Filter.h:119
Filter(const Filter &other)
Shallow copy constructor called by tbb::parallel_for() threads during filtering.
Definition: Filter.h:99
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:67
This class manages a linear array of pointers to a given tree&#39;s leaf nodes, as well as optional auxil...
Definition: LeafManager.h:109
MaskType::ValueType AlphaType
Definition: Filter.h:75
AlphaType maxMask() const
Return the maximum value of the mask to be used for the derivation of a smooth alpha value...
Definition: Filter.h:122
bool wasInterrupted(T *i, int percent=-1)
Definition: NullInterrupter.h:76
TreeType::LeafNodeType LeafType
Definition: Filter.h:73
void setMaskRange(AlphaType min, AlphaType max)
Define the range for the (optional) scalar mask.
Definition: Filter.h:129
GridType::TreeType TreeType
Definition: Filter.h:72