39 #ifndef OPENVDB_TOOLS_FILTER_HAS_BEEN_INCLUDED
40 #define OPENVDB_TOOLS_FILTER_HAS_BEEN_INCLUDED
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>
64 template<
typename GridT,
65 typename MaskT =
typename GridT::template ValueConverter<float>::Type,
66 typename InterruptT = util::NullInterrupter>
73 typedef typename TreeType::LeafNodeType
LeafType;
79 BOOST_STATIC_ASSERT(boost::is_floating_point<AlphaType>::value);
84 Filter(GridT& grid, InterruptT* interrupt = NULL)
87 , mInterrupter(interrupt)
102 , mInterrupter(other.mInterrupter)
104 , mGrainSize(other.mGrainSize)
105 , mMinMask(other.mMinMask)
106 , mMaxMask(other.mMaxMask)
107 , mInvertMask(other.mInvertMask)
147 void mean(
int width = 1,
int iterations = 1,
const MaskType* mask = NULL);
156 void gaussian(
int width = 1,
int iterations = 1,
const MaskType* mask = NULL);
164 void median(
int width = 1,
int iterations = 1,
const MaskType* mask = NULL);
169 void offset(ValueType offset,
const MaskType* mask = NULL);
177 if (mTask) mTask(const_cast<Filter*>(
this), range);
182 typedef typename TreeType::LeafNodeType LeafT;
183 typedef typename LeafT::ValueOnIter VoxelIterT;
184 typedef typename LeafT::ValueOnCIter VoxelCIterT;
186 typedef typename RangeType::Iterator LeafIterT;
188 void cook(LeafManagerType& leafs);
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)
199 inline bool operator()(
const Coord& xyz, AlphaType& a, AlphaType& b)
const
202 const AlphaType t = (a-mMin)*mInvNorm;
203 a = t > 0 ? t < 1 ? (3-2*t)*t*t : 1 : 0;
205 if (mInvert) std::swap(a,b);
209 const AlphaType mMin, mInvNorm;
213 template <
size_t Axis>
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>();
220 for (i -= width; i <= j; ++i) sum += acc.getValue(xyz);
223 typename GridT::ConstAccessor acc;
225 const ValueType frac;
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);
240 typename boost::function<void (Filter*, const RangeType&)> mTask;
241 InterruptT* mInterrupter;
242 const MaskType*
mMask;
244 AlphaType mMinMask, mMaxMask;
250 template<
typename Gr
idT,
typename MaskT,
typename InterruptT>
256 if (mInterrupter) mInterrupter->start(
"Applying mean filter");
263 mTask = boost::bind(&Filter::doBoxX, _1, _2, w);
266 mTask = boost::bind(&Filter::doBoxY, _1, _2, w);
269 mTask = boost::bind(&Filter::doBoxZ, _1, _2, w);
273 if (mInterrupter) mInterrupter->end();
276 template<
typename Gr
idT,
typename MaskT,
typename InterruptT>
282 if (mInterrupter) mInterrupter->start(
"Applying gaussian filter");
288 for (
int i=0; i<iterations; ++i) {
290 mTask = boost::bind(&Filter::doBoxX, _1, _2, w);
293 mTask = boost::bind(&Filter::doBoxY, _1, _2, w);
296 mTask = boost::bind(&Filter::doBoxZ, _1, _2, w);
301 if (mInterrupter) mInterrupter->end();
305 template<
typename Gr
idT,
typename MaskT,
typename InterruptT>
311 if (mInterrupter) mInterrupter->start(
"Applying median filter");
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);
318 if (mInterrupter) mInterrupter->end();
321 template<
typename Gr
idT,
typename MaskT,
typename InterruptT>
327 if (mInterrupter) mInterrupter->start(
"Applying offset");
331 mTask = boost::bind(&Filter::doOffset, _1, _2, value);
334 if (mInterrupter) mInterrupter->end();
342 template<
typename Gr
idT,
typename MaskT,
typename InterruptT>
347 tbb::parallel_for(leafs.leafRange(mGrainSize), *
this);
349 (*this)(leafs.leafRange());
351 leafs.swapLeafBuffer(1, mGrainSize==0);
355 template<
typename Gr
idT,
typename MaskT,
typename InterruptT>
356 template <
typename AvgT>
358 Filter<GridT, MaskT, InterruptT>::doBox(
const RangeType& range,
Int32 w)
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)));
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()));
385 template<
typename Gr
idT,
typename MaskT,
typename InterruptT>
387 Filter<GridT, MaskT, InterruptT>::doMedian(
const RangeType& range,
int width)
390 typename math::DenseStencil<GridType> stencil(*mGrid, width);
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()));
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());
415 template<
typename Gr
idT,
typename MaskT,
typename InterruptT>
417 Filter<GridT, MaskT, InterruptT>::doOffset(
const RangeType& range, ValueType offset)
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));
429 for (LeafIterT leafIter=range.begin(); leafIter; ++leafIter) {
430 for (VoxelIterT iter = leafIter->beginValueOn(); iter; ++iter) {
431 iter.setValue(*iter + offset);
437 template<
typename Gr
idT,
typename MaskT,
typename InterruptT>
442 tbb::task::self().cancel_group_execution();
452 #endif // OPENVDB_TOOLS_FILTER_HAS_BEEN_INCLUDED
OPENVDB_API Hermite min(const Hermite &, const Hermite &)
min and max operations done directly on the compressed data.
#define OPENVDB_THROW(exception, message)
Definition: Exceptions.h:97
Definition: Exceptions.h:88
CopyConstness< TreeType, NonConstBufferType >::Type BufferType
Definition: LeafManager.h:117
#define OPENVDB_VERSION_NAME
Definition: version.h:45
const MaskGridType * mMask
Definition: GridOperators.h:386
int32_t Int32
Definition: Types.h:58
OPENVDB_API Hermite max(const Hermite &, const Hermite &)
min and max operations done directly on the compressed data.
Definition: LeafManager.h:122
Axis
Definition: Math.h:769
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:67
This class manages a linear array of pointers to a given tree's leaf nodes, as well as optional auxil...
Definition: LeafManager.h:109
bool wasInterrupted(T *i, int percent=-1)
Definition: NullInterrupter.h:76