12 #ifndef OPENVDB_TOOLS_FILTER_HAS_BEEN_INCLUDED
13 #define OPENVDB_TOOLS_FILTER_HAS_BEEN_INCLUDED
15 #include <tbb/parallel_for.h>
26 #include <type_traits>
37 template<
typename GridT,
38 typename MaskT =
typename GridT::template ValueConverter<float>::Type,
39 typename InterruptT = util::NullInterrupter>
46 using LeafType =
typename TreeType::LeafNodeType;
50 using RangeType =
typename LeafManagerType::LeafRange;
52 static_assert(std::is_floating_point<AlphaType>::value,
53 "openvdb::tools::Filter requires a mask grid with floating-point values");
58 Filter(GridT& grid, InterruptT* interrupt =
nullptr)
61 , mInterrupter(interrupt)
76 , mInterrupter(other.mInterrupter)
78 , mGrainSize(other.mGrainSize)
79 , mMinMask(other.mMinMask)
80 , mMaxMask(other.mMaxMask)
81 , mInvertMask(other.mInvertMask)
121 void mean(
int width = 1,
int iterations = 1,
const MaskType* mask =
nullptr);
130 void gaussian(
int width = 1,
int iterations = 1,
const MaskType* mask =
nullptr);
138 void median(
int width = 1,
int iterations = 1,
const MaskType* mask =
nullptr);
143 void offset(ValueType offset,
const MaskType* mask =
nullptr);
151 if (mTask) mTask(
const_cast<Filter*
>(
this), range);
156 using LeafT =
typename TreeType::LeafNodeType;
157 using VoxelIterT =
typename LeafT::ValueOnIter;
158 using VoxelCIterT =
typename LeafT::ValueOnCIter;
160 using LeafIterT =
typename RangeType::Iterator;
163 void cook(LeafManagerType& leafs);
165 template<
size_t Axis>
167 Avg(
const GridT* grid,
Int32 w): acc(grid->tree()), width(w), frac(1.f/float(2*w+1)) {}
168 inline ValueType operator()(
Coord xyz);
169 typename GridT::ConstAccessor acc;
175 template <
typename AvgT>
176 void doBox(
const RangeType& r,
Int32 w);
177 void doBoxX(
const RangeType& r,
Int32 w) { this->doBox<Avg<0> >(r,w); }
178 void doBoxY(
const RangeType& r,
Int32 w) { this->doBox<Avg<1> >(r,w); }
179 void doBoxZ(
const RangeType& r,
Int32 w) { this->doBox<Avg<2> >(r,w); }
180 void doMedian(
const RangeType&,
int);
181 void doOffset(
const RangeType&, ValueType);
186 typename std::function<void (Filter*,
const RangeType&)> mTask;
187 InterruptT* mInterrupter;
188 const MaskType* mMask;
190 AlphaType mMinMask, mMaxMask;
198 namespace filter_internal {
200 template<
typename T>
static inline void accum(T& sum, T addend) { sum += addend; }
202 inline void accum(
bool& sum,
bool addend) { sum = sum || addend; }
206 template<
typename Gr
idT,
typename MaskT,
typename InterruptT>
207 template<
size_t Axis>
208 inline typename GridT::ValueType
209 Filter<GridT, MaskT, InterruptT>::Avg<Axis>::operator()(
Coord xyz)
211 ValueType sum = zeroVal<ValueType>();
215 ValueType value =
static_cast<ValueType
>(sum * frac);
224 template<
typename Gr
idT,
typename MaskT,
typename InterruptT>
230 if (mInterrupter) mInterrupter->start(
"Applying mean filter");
237 mTask = std::bind(&Filter::doBoxX, std::placeholders::_1, std::placeholders::_2, w);
243 mTask = std::bind(&Filter::doBoxZ, std::placeholders::_1, std::placeholders::_2, w);
246 mTask = std::bind(&Filter::doBoxY, std::placeholders::_1, std::placeholders::_2, w);
250 if (mInterrupter) mInterrupter->end();
254 template<
typename Gr
idT,
typename MaskT,
typename InterruptT>
260 if (mInterrupter) mInterrupter->start(
"Applying Gaussian filter");
266 for (
int i=0; i<iterations; ++i) {
268 mTask = std::bind(&Filter::doBoxX, std::placeholders::_1, std::placeholders::_2, w);
274 mTask = std::bind(&Filter::doBoxZ, std::placeholders::_1, std::placeholders::_2, w);
277 mTask = std::bind(&Filter::doBoxY, std::placeholders::_1, std::placeholders::_2, w);
282 if (mInterrupter) mInterrupter->end();
286 template<
typename Gr
idT,
typename MaskT,
typename InterruptT>
292 if (mInterrupter) mInterrupter->start(
"Applying median filter");
296 mTask = std::bind(&Filter::doMedian,
297 std::placeholders::_1, std::placeholders::_2,
std::max(1, width));
298 for (
int i=0; i<iterations && !this->
wasInterrupted(); ++i) this->cook(leafs);
300 if (mInterrupter) mInterrupter->end();
304 template<
typename Gr
idT,
typename MaskT,
typename InterruptT>
310 if (mInterrupter) mInterrupter->start(
"Applying offset");
314 mTask = std::bind(&Filter::doOffset, std::placeholders::_1, std::placeholders::_2, value);
317 if (mInterrupter) mInterrupter->end();
326 template<
typename Gr
idT,
typename MaskT,
typename InterruptT>
331 tbb::parallel_for(leafs.leafRange(mGrainSize), *
this);
333 (*this)(leafs.leafRange());
335 leafs.swapLeafBuffer(1, mGrainSize==0);
340 template<
typename Gr
idT,
typename MaskT,
typename InterruptT>
341 template <
typename AvgT>
343 Filter<GridT, MaskT, InterruptT>::doBox(
const RangeType& range,
Int32 w)
348 typename AlphaMaskT::FloatType a, b;
349 AlphaMaskT alpha(*mGrid, *mMask, mMinMask, mMaxMask, mInvertMask);
350 for (LeafIterT leafIter=range.begin(); leafIter; ++leafIter) {
351 BufferT& buffer = leafIter.buffer(1);
352 for (VoxelCIterT iter = leafIter->cbeginValueOn(); iter; ++iter) {
353 const Coord xyz = iter.getCoord();
354 if (alpha(xyz, a, b)) {
356 const ValueType value(b*(*iter) + a*avg(xyz));
358 buffer.setValue(iter.pos(), value);
363 for (LeafIterT leafIter=range.begin(); leafIter; ++leafIter) {
364 BufferT& buffer = leafIter.buffer(1);
365 for (VoxelCIterT iter = leafIter->cbeginValueOn(); iter; ++iter) {
366 buffer.setValue(iter.pos(), avg(iter.getCoord()));
374 template<
typename Gr
idT,
typename MaskT,
typename InterruptT>
376 Filter<GridT, MaskT, InterruptT>::doMedian(
const RangeType& range,
int width)
379 typename math::DenseStencil<GridType> stencil(*mGrid, width);
381 typename AlphaMaskT::FloatType a, b;
382 AlphaMaskT alpha(*mGrid, *mMask, mMinMask, mMaxMask, mInvertMask);
383 for (LeafIterT leafIter=range.begin(); leafIter; ++leafIter) {
384 BufferT& buffer = leafIter.buffer(1);
385 for (VoxelCIterT iter = leafIter->cbeginValueOn(); iter; ++iter) {
386 if (alpha(iter.getCoord(), a, b)) {
387 stencil.moveTo(iter);
389 ValueType value(b*(*iter) + a*stencil.median());
391 buffer.setValue(iter.pos(), value);
396 for (LeafIterT leafIter=range.begin(); leafIter; ++leafIter) {
397 BufferT& buffer = leafIter.buffer(1);
398 for (VoxelCIterT iter = leafIter->cbeginValueOn(); iter; ++iter) {
399 stencil.moveTo(iter);
400 buffer.setValue(iter.pos(), stencil.median());
408 template<
typename Gr
idT,
typename MaskT,
typename InterruptT>
410 Filter<GridT, MaskT, InterruptT>::doOffset(
const RangeType& range, ValueType offset)
414 typename AlphaMaskT::FloatType a, b;
415 AlphaMaskT alpha(*mGrid, *mMask, mMinMask, mMaxMask, mInvertMask);
416 for (LeafIterT leafIter=range.begin(); leafIter; ++leafIter) {
417 for (VoxelIterT iter = leafIter->beginValueOn(); iter; ++iter) {
418 if (alpha(iter.getCoord(), a, b)) {
420 ValueType value(*iter + a*offset);
422 iter.setValue(value);
427 for (LeafIterT leafIter=range.begin(); leafIter; ++leafIter) {
428 for (VoxelIterT iter = leafIter->beginValueOn(); iter; ++iter) {
429 iter.setValue(*iter + offset);
436 template<
typename Gr
idT,
typename MaskT,
typename InterruptT>
441 tbb::task::self().cancel_group_execution();
A LeafManager manages a linear array of pointers to a given tree's leaf nodes, as well as optional au...
General-purpose arithmetic and comparison routines, most of which accept arbitrary value types (or at...
Defines various finite difference stencils by means of the "curiously recurring template pattern" on ...
Definition: openvdb/Exceptions.h:65
Signed (x, y, z) 32-bit integer coordinates.
Definition: Coord.h:26
This class manages a linear array of pointers to a given tree's leaf nodes, as well as optional auxil...
Definition: LeafManager.h:85
typename CopyConstness< TreeType, NonConstBufferType >::Type BufferType
Definition: LeafManager.h:95
Axis
Definition: Math.h:904
bool wasInterrupted(T *i, int percent=-1)
Definition: NullInterrupter.h:49
int32_t Int32
Definition: openvdb/Types.h:34
Definition: openvdb/Exceptions.h:13
#define OPENVDB_THROW(exception, message)
Definition: openvdb/Exceptions.h:74
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h:101
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:153