39 #ifndef OPENVDB_TOOLS_FILTER_HAS_BEEN_INCLUDED
40 #define OPENVDB_TOOLS_FILTER_HAS_BEEN_INCLUDED
42 #include <tbb/parallel_for.h>
53 #include <type_traits>
64 template<
typename GridT,
65 typename MaskT =
typename GridT::template ValueConverter<float>::Type,
66 typename InterruptT = util::NullInterrupter>
73 using LeafType =
typename TreeType::LeafNodeType;
77 using RangeType =
typename LeafManagerType::LeafRange;
79 static_assert(std::is_floating_point<AlphaType>::value,
80 "openvdb::tools::Filter requires a mask grid with floating-point values");
85 Filter(GridT& grid, InterruptT* interrupt =
nullptr)
88 , mInterrupter(interrupt)
103 , mInterrupter(other.mInterrupter)
105 , mGrainSize(other.mGrainSize)
106 , mMinMask(other.mMinMask)
107 , mMaxMask(other.mMaxMask)
108 , mInvertMask(other.mInvertMask)
148 void mean(
int width = 1,
int iterations = 1,
const MaskType* mask =
nullptr);
157 void gaussian(
int width = 1,
int iterations = 1,
const MaskType* mask =
nullptr);
165 void median(
int width = 1,
int iterations = 1,
const MaskType* mask =
nullptr);
170 void offset(ValueType offset,
const MaskType* mask =
nullptr);
178 if (mTask) mTask(const_cast<Filter*>(
this), range);
183 using LeafT =
typename TreeType::LeafNodeType;
184 using VoxelIterT =
typename LeafT::ValueOnIter;
185 using VoxelCIterT =
typename LeafT::ValueOnCIter;
187 using LeafIterT =
typename RangeType::Iterator;
190 void cook(LeafManagerType& leafs);
192 template<
size_t Axis>
194 Avg(
const GridT* grid,
Int32 w): acc(grid->tree()), width(w), frac(1.f/float(2*w+1)) {}
195 inline ValueType operator()(
Coord xyz);
196 typename GridT::ConstAccessor acc;
202 template <
typename AvgT>
203 void doBox(
const RangeType& r,
Int32 w);
204 void doBoxX(
const RangeType& r,
Int32 w) { this->doBox<Avg<0> >(r,w); }
205 void doBoxZ(
const RangeType& r,
Int32 w) { this->doBox<Avg<1> >(r,w); }
206 void doBoxY(
const RangeType& r,
Int32 w) { this->doBox<Avg<2> >(r,w); }
207 void doMedian(
const RangeType&,
int);
208 void doOffset(
const RangeType&, ValueType);
213 typename std::function<void (Filter*,
const RangeType&)> mTask;
214 InterruptT* mInterrupter;
215 const MaskType* mMask;
217 AlphaType mMinMask, mMaxMask;
225 namespace filter_internal {
227 template<
typename T>
static inline void accum(T& sum, T addend) { sum += addend; }
229 inline void accum(
bool& sum,
bool addend) { sum = sum || addend; }
233 template<
typename Gr
idT,
typename MaskT,
typename InterruptT>
234 template<
size_t Axis>
235 inline typename GridT::ValueType
236 Filter<GridT, MaskT, InterruptT>::Avg<Axis>::operator()(
Coord xyz)
238 ValueType sum = zeroVal<ValueType>();
242 ValueType value = static_cast<ValueType>(sum * frac);
251 template<
typename Gr
idT,
typename MaskT,
typename InterruptT>
257 if (mInterrupter) mInterrupter->start(
"Applying mean filter");
264 mTask = std::bind(&Filter::doBoxX, std::placeholders::_1, std::placeholders::_2, w);
267 mTask = std::bind(&Filter::doBoxY, std::placeholders::_1, std::placeholders::_2, w);
270 mTask = std::bind(&Filter::doBoxZ, std::placeholders::_1, std::placeholders::_2, w);
274 if (mInterrupter) mInterrupter->end();
278 template<
typename Gr
idT,
typename MaskT,
typename InterruptT>
284 if (mInterrupter) mInterrupter->start(
"Applying Gaussian filter");
290 for (
int i=0; i<iterations; ++i) {
292 mTask = std::bind(&Filter::doBoxX, std::placeholders::_1, std::placeholders::_2, w);
295 mTask = std::bind(&Filter::doBoxY, std::placeholders::_1, std::placeholders::_2, w);
298 mTask = std::bind(&Filter::doBoxZ, std::placeholders::_1, std::placeholders::_2, w);
303 if (mInterrupter) mInterrupter->end();
307 template<
typename Gr
idT,
typename MaskT,
typename InterruptT>
313 if (mInterrupter) mInterrupter->start(
"Applying median filter");
317 mTask = std::bind(&Filter::doMedian,
318 std::placeholders::_1, std::placeholders::_2,
std::max(1, width));
319 for (
int i=0; i<iterations && !this->
wasInterrupted(); ++i) this->cook(leafs);
321 if (mInterrupter) mInterrupter->end();
325 template<
typename Gr
idT,
typename MaskT,
typename InterruptT>
331 if (mInterrupter) mInterrupter->start(
"Applying offset");
335 mTask = std::bind(&Filter::doOffset, std::placeholders::_1, std::placeholders::_2, value);
338 if (mInterrupter) mInterrupter->end();
347 template<
typename Gr
idT,
typename MaskT,
typename InterruptT>
352 tbb::parallel_for(leafs.leafRange(mGrainSize), *
this);
354 (*this)(leafs.leafRange());
356 leafs.swapLeafBuffer(1, mGrainSize==0);
361 template<
typename Gr
idT,
typename MaskT,
typename InterruptT>
362 template <
typename AvgT>
364 Filter<GridT, MaskT, InterruptT>::doBox(
const RangeType& range,
Int32 w)
369 typename AlphaMaskT::FloatType a, b;
370 AlphaMaskT alpha(*mGrid, *mMask, mMinMask, mMaxMask, mInvertMask);
371 for (LeafIterT leafIter=range.begin(); leafIter; ++leafIter) {
372 BufferT& buffer = leafIter.buffer(1);
373 for (VoxelCIterT iter = leafIter->cbeginValueOn(); iter; ++iter) {
374 const Coord xyz = iter.getCoord();
375 if (alpha(xyz, a, b)) {
377 const ValueType value(b*(*iter) + a*avg(xyz));
379 buffer.setValue(iter.pos(), value);
384 for (LeafIterT leafIter=range.begin(); leafIter; ++leafIter) {
385 BufferT& buffer = leafIter.buffer(1);
386 for (VoxelCIterT iter = leafIter->cbeginValueOn(); iter; ++iter) {
387 buffer.setValue(iter.pos(), avg(iter.getCoord()));
395 template<
typename Gr
idT,
typename MaskT,
typename InterruptT>
397 Filter<GridT, MaskT, InterruptT>::doMedian(
const RangeType& range,
int width)
400 typename math::DenseStencil<GridType> stencil(*mGrid, width);
402 typename AlphaMaskT::FloatType a, b;
403 AlphaMaskT alpha(*mGrid, *mMask, mMinMask, mMaxMask, mInvertMask);
404 for (LeafIterT leafIter=range.begin(); leafIter; ++leafIter) {
405 BufferT& buffer = leafIter.buffer(1);
406 for (VoxelCIterT iter = leafIter->cbeginValueOn(); iter; ++iter) {
407 if (alpha(iter.getCoord(), a, b)) {
408 stencil.moveTo(iter);
410 ValueType value(b*(*iter) + a*stencil.median());
412 buffer.setValue(iter.pos(), value);
417 for (LeafIterT leafIter=range.begin(); leafIter; ++leafIter) {
418 BufferT& buffer = leafIter.buffer(1);
419 for (VoxelCIterT iter = leafIter->cbeginValueOn(); iter; ++iter) {
420 stencil.moveTo(iter);
421 buffer.setValue(iter.pos(), stencil.median());
429 template<
typename Gr
idT,
typename MaskT,
typename InterruptT>
431 Filter<GridT, MaskT, InterruptT>::doOffset(
const RangeType& range, ValueType offset)
435 typename AlphaMaskT::FloatType a, b;
436 AlphaMaskT alpha(*mGrid, *mMask, mMinMask, mMaxMask, mInvertMask);
437 for (LeafIterT leafIter=range.begin(); leafIter; ++leafIter) {
438 for (VoxelIterT iter = leafIter->beginValueOn(); iter; ++iter) {
439 if (alpha(iter.getCoord(), a, b)) {
441 ValueType value(*iter + a*offset);
443 iter.setValue(value);
448 for (LeafIterT leafIter=range.begin(); leafIter; ++leafIter) {
449 for (VoxelIterT iter = leafIter->beginValueOn(); iter; ++iter) {
450 iter.setValue(*iter + offset);
457 template<
typename Gr
idT,
typename MaskT,
typename InterruptT>
462 tbb::task::self().cancel_group_execution();
472 #endif // OPENVDB_TOOLS_FILTER_HAS_BEEN_INCLUDED