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);
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;
165 template<
size_t Axis>
167 Avg(
const GridT* grid,
Int32 w): acc(grid->tree()), width(w), frac(1.f/
float(2*w+1)) {}
169 typename GridT::ConstAccessor acc;
175 template <
typename AvgT>
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); }
186 typename std::function<void (Filter*, const RangeType&)> mTask;
187 InterruptT* mInterrupter;
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
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>
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>
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)) {
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) {
400 buffer.setValue(iter.pos(), stencil.
median());
408 template<
typename Gr
idT,
typename MaskT,
typename InterruptT>
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)) {
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();
451 #endif // OPENVDB_TOOLS_FILTER_HAS_BEEN_INCLUDED
int32_t Int32
Definition: openvdb/Types.h:34
Dense stencil of a given width.
Definition: Stencils.h:1761
#define OPENVDB_THROW(exception, message)
Definition: openvdb/Exceptions.h:82
General-purpose arithmetic and comparison routines, most of which accept arbitrary value types (or at...
typename CopyConstness< TreeType, NonConstBufferType >::Type BufferType
Definition: LeafManager.h:95
bool wasInterrupted(T *i, int percent=-1)
Definition: NullInterrupter.h:49
Definition: openvdb/Exceptions.h:65
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h:94
Axis
Definition: Math.h:894
Definition: openvdb/Exceptions.h:13
void moveTo(const Coord &ijk)
Initialize the stencil buffer with the values of voxel (x, y, z) and its neighbors.
Definition: Stencils.h:1781
This class manages a linear array of pointers to a given tree's leaf nodes, as well as optional auxil...
Definition: LeafManager.h:84
ValueType median() const
Return the median value of the current stencil.
Definition: Stencils.h:121
Defines various finite difference stencils by means of the "curiously recurring template pattern" on ...
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:146
A LeafManager manages a linear array of pointers to a given tree's leaf nodes, as well as optional au...