OpenVDB  3.0.0
Filter.h
Go to the documentation of this file.
1 //
3 // Copyright (c) 2012-2014 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; }
129  void setMaskRange(AlphaType min, AlphaType max)
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  typedef tools::AlphaMask<GridT, MaskT> AlphaMaskT;
188 
189  void cook(LeafManagerType& leafs);
190 
191  template<size_t Axis>
192  struct Avg {
193  Avg(const GridT* grid, Int32 w): acc(grid->tree()), width(w), frac(1.f/float(2*w+1)) {}
194  inline ValueType operator()(Coord xyz);
195  typename GridT::ConstAccessor acc;
196  const Int32 width;
197  const float frac;
198  };
199 
200  // Private filter methods called by tbb::parallel_for threads
201  template <typename AvgT>
202  void doBox( const RangeType& r, Int32 w);
203  void doBoxX(const RangeType& r, Int32 w) { this->doBox<Avg<0> >(r,w); }
204  void doBoxZ(const RangeType& r, Int32 w) { this->doBox<Avg<1> >(r,w); }
205  void doBoxY(const RangeType& r, Int32 w) { this->doBox<Avg<2> >(r,w); }
206  void doMedian(const RangeType&, int);
207  void doOffset(const RangeType&, ValueType);
209  bool wasInterrupted();
210 
211  GridType* mGrid;
212  typename boost::function<void (Filter*, const RangeType&)> mTask;
213  InterruptT* mInterrupter;
214  const MaskType* mMask;
215  int mGrainSize;
216  AlphaType mMinMask, mMaxMask;
217  bool mInvertMask;
218 }; // end of Filter class
219 
220 
222 
223 
224 namespace filter_internal {
225 // Helper function for Filter::Avg::operator()
226 template<typename T> static inline void accum(T& sum, T addend) { sum += addend; }
227 // Overload for bool ValueType
228 inline void accum(bool& sum, bool addend) { sum = sum || addend; }
229 }
230 
231 
232 template<typename GridT, typename MaskT, typename InterruptT>
233 template<size_t Axis>
234 inline typename GridT::ValueType
235 Filter<GridT, MaskT, InterruptT>::Avg<Axis>::operator()(Coord xyz)
236 {
237  ValueType sum = zeroVal<ValueType>();
238  Int32 &i = xyz[Axis], j = i + width;
239  for (i -= width; i <= j; ++i) filter_internal::accum(sum, acc.getValue(xyz));
240  return static_cast<ValueType>(sum * frac);
241 }
242 
243 
245 
246 
247 template<typename GridT, typename MaskT, typename InterruptT>
248 inline void
249 Filter<GridT, MaskT, InterruptT>::mean(int width, int iterations, const MaskType* mask)
250 {
251  mMask = mask;
252 
253  if (mInterrupter) mInterrupter->start("Applying mean filter");
254 
255  const int w = std::max(1, width);
256 
257  LeafManagerType leafs(mGrid->tree(), 1, mGrainSize==0);
258 
259  for (int i=0; i<iterations && !this->wasInterrupted(); ++i) {
260  mTask = boost::bind(&Filter::doBoxX, _1, _2, w);
261  this->cook(leafs);
262 
263  mTask = boost::bind(&Filter::doBoxY, _1, _2, w);
264  this->cook(leafs);
265 
266  mTask = boost::bind(&Filter::doBoxZ, _1, _2, w);
267  this->cook(leafs);
268  }
269 
270  if (mInterrupter) mInterrupter->end();
271 }
272 
273 
274 template<typename GridT, typename MaskT, typename InterruptT>
275 inline void
276 Filter<GridT, MaskT, InterruptT>::gaussian(int width, int iterations, const MaskType* mask)
277 {
278  mMask = mask;
279 
280  if (mInterrupter) mInterrupter->start("Applying gaussian filter");
281 
282  const int w = std::max(1, width);
283 
284  LeafManagerType leafs(mGrid->tree(), 1, mGrainSize==0);
285 
286  for (int i=0; i<iterations; ++i) {
287  for (int n=0; n<4 && !this->wasInterrupted(); ++n) {
288  mTask = boost::bind(&Filter::doBoxX, _1, _2, w);
289  this->cook(leafs);
290 
291  mTask = boost::bind(&Filter::doBoxY, _1, _2, w);
292  this->cook(leafs);
293 
294  mTask = boost::bind(&Filter::doBoxZ, _1, _2, w);
295  this->cook(leafs);
296  }
297  }
298 
299  if (mInterrupter) mInterrupter->end();
300 }
301 
302 
303 template<typename GridT, typename MaskT, typename InterruptT>
304 inline void
305 Filter<GridT, MaskT, InterruptT>::median(int width, int iterations, const MaskType* mask)
306 {
307  mMask = mask;
308 
309  if (mInterrupter) mInterrupter->start("Applying median filter");
310 
311  LeafManagerType leafs(mGrid->tree(), 1, mGrainSize==0);
312 
313  mTask = boost::bind(&Filter::doMedian, _1, _2, std::max(1, width));
314  for (int i=0; i<iterations && !this->wasInterrupted(); ++i) this->cook(leafs);
315 
316  if (mInterrupter) mInterrupter->end();
317 }
318 
319 
320 template<typename GridT, typename MaskT, typename InterruptT>
321 inline void
323 {
324  mMask = mask;
325 
326  if (mInterrupter) mInterrupter->start("Applying offset");
327 
328  LeafManagerType leafs(mGrid->tree(), 0, mGrainSize==0);
329 
330  mTask = boost::bind(&Filter::doOffset, _1, _2, value);
331  this->cook(leafs);
332 
333  if (mInterrupter) mInterrupter->end();
334 }
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 
354 
356 template<typename GridT, typename MaskT, typename InterruptT>
357 template <typename AvgT>
358 inline void
359 Filter<GridT, MaskT, InterruptT>::doBox(const RangeType& range, Int32 w)
360 {
361  this->wasInterrupted();
362  AvgT avg(mGrid, w);
363  if (mMask) {
364  typename AlphaMaskT::FloatType a, b;
365  AlphaMaskT alpha(*mGrid, *mMask, mMinMask, mMaxMask, mInvertMask);
366  for (LeafIterT leafIter=range.begin(); leafIter; ++leafIter) {
367  BufferT& buffer = leafIter.buffer(1);
368  for (VoxelCIterT iter = leafIter->cbeginValueOn(); iter; ++iter) {
369  const Coord xyz = iter.getCoord();
370  if (alpha(xyz, a, b)) {
371  buffer.setValue(iter.pos(), ValueType(b*(*iter) + a*avg(xyz)));
372  }
373  }
374  }
375  } else {
376  for (LeafIterT leafIter=range.begin(); leafIter; ++leafIter) {
377  BufferT& buffer = leafIter.buffer(1);
378  for (VoxelCIterT iter = leafIter->cbeginValueOn(); iter; ++iter) {
379  buffer.setValue(iter.pos(), avg(iter.getCoord()));
380  }
381  }
382  }
383 }
384 
385 
387 template<typename GridT, typename MaskT, typename InterruptT>
388 inline void
389 Filter<GridT, MaskT, InterruptT>::doMedian(const RangeType& range, int width)
390 {
391  this->wasInterrupted();
392  typename math::DenseStencil<GridType> stencil(*mGrid, width);//creates local cache!
393  if (mMask) {
394  typename AlphaMaskT::FloatType a, b;
395  AlphaMaskT alpha(*mGrid, *mMask, mMinMask, mMaxMask, mInvertMask);
396  for (LeafIterT leafIter=range.begin(); leafIter; ++leafIter) {
397  BufferT& buffer = leafIter.buffer(1);
398  for (VoxelCIterT iter = leafIter->cbeginValueOn(); iter; ++iter) {
399  if (alpha(iter.getCoord(), a, b)) {
400  stencil.moveTo(iter);
401  buffer.setValue(iter.pos(), ValueType(b*(*iter) + a*stencil.median()));
402  }
403  }
404  }
405  } else {
406  for (LeafIterT leafIter=range.begin(); leafIter; ++leafIter) {
407  BufferT& buffer = leafIter.buffer(1);
408  for (VoxelCIterT iter = leafIter->cbeginValueOn(); iter; ++iter) {
409  stencil.moveTo(iter);
410  buffer.setValue(iter.pos(), stencil.median());
411  }
412  }
413  }
414 }
415 
416 
418 template<typename GridT, typename MaskT, typename InterruptT>
419 inline void
420 Filter<GridT, MaskT, InterruptT>::doOffset(const RangeType& range, ValueType offset)
421 {
422  this->wasInterrupted();
423  if (mMask) {
424  typename AlphaMaskT::FloatType a, b;
425  AlphaMaskT alpha(*mGrid, *mMask, mMinMask, mMaxMask, mInvertMask);
426  for (LeafIterT leafIter=range.begin(); leafIter; ++leafIter) {
427  for (VoxelIterT iter = leafIter->beginValueOn(); iter; ++iter) {
428  if (alpha(iter.getCoord(), a, b)) iter.setValue(ValueType(*iter + a*offset));
429  }
430  }
431  } else {
432  for (LeafIterT leafIter=range.begin(); leafIter; ++leafIter) {
433  for (VoxelIterT iter = leafIter->beginValueOn(); iter; ++iter) {
434  iter.setValue(*iter + offset);
435  }
436  }
437  }
438 }
439 
440 
441 template<typename GridT, typename MaskT, typename InterruptT>
442 inline bool
444 {
445  if (util::wasInterrupted(mInterrupter)) {
446  tbb::task::self().cancel_group_execution();
447  return true;
448  }
449  return false;
450 }
451 
452 } // namespace tools
453 } // namespace OPENVDB_VERSION_NAME
454 } // namespace openvdb
455 
456 #endif // OPENVDB_TOOLS_FILTER_HAS_BEEN_INCLUDED
457 
458 // Copyright (c) 2012-2014 DreamWorks Animation LLC
459 // All rights reserved. This software is distributed under the
460 // Mozilla Public License 2.0 ( http://www.mozilla.org/MPL/2.0/ )
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
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: Filter.h:138
Filter(GridT &grid, InterruptT *interrupt=NULL)
Definition: Filter.h:84
#define OPENVDB_THROW(exception, message)
Definition: Exceptions.h:97
void operator()(const RangeType &range) const
Used internally by tbb::parallel_for()
Definition: Filter.h:175
Definition: Interpolation.h:481
This class manages a linear array of pointers to a given tree's leaf nodes, as well as optional auxil...
Definition: LeafManager.h:109
TreeType::LeafNodeType LeafType
Definition: Filter.h:73
int getGrainSize() const
Definition: Filter.h:112
LeafManagerType::LeafRange RangeType
Definition: Filter.h:77
int32_t Int32
Definition: Types.h:61
void setGrainSize(int grainsize)
Set the grain-size used for multi-threading.
Definition: Filter.h:115
bool wasInterrupted(T *i, int percent=-1)
Definition: NullInterrupter.h:76
tree::LeafManager< TreeType > LeafManagerType
Definition: Filter.h:76
#define OPENVDB_VERSION_NAME
Definition: version.h:43
LeafManagerType::BufferType BufferType
Definition: Filter.h:78
GridT GridType
Definition: Filter.h:70
GridType::ValueType ValueType
Definition: Filter.h:74
Definition: Exceptions.h:39
OPENVDB_API Hermite min(const Hermite &, const Hermite &)
min and max operations done directly on the compressed data.
MaskType::ValueType AlphaType
Definition: Filter.h:75
void setMaskRange(AlphaType min, AlphaType max)
Define the range for the (optional) scalar mask.
Definition: Filter.h:129
Volume filtering (e.g., diffusion) with optional alpha masking.
Definition: Filter.h:67
Axis
Definition: Math.h:822
GridType::TreeType TreeType
Definition: Filter.h:72
Definition: Exceptions.h:88
MaskT MaskType
Definition: Filter.h:71
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: Filter.h:141
OPENVDB_API Hermite max(const Hermite &, const Hermite &)
min and max operations done directly on the compressed data.
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
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:71
void accum(bool &sum, bool addend)
Definition: Filter.h:228
CopyConstness< TreeType, NonConstBufferType >::Type BufferType
Definition: LeafManager.h:120