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);
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;
192 template<
size_t Axis>
194 Avg(
const GridT* grid,
Int32 w): acc(grid->tree()), width(w), frac(1.f/
float(2*w+1)) {}
196 typename GridT::ConstAccessor acc;
202 template <
typename AvgT>
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); }
213 typename std::function<void (Filter*, const RangeType&)> mTask;
214 InterruptT* mInterrupter;
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
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>
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>
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)) {
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) {
421 buffer.setValue(iter.pos(), stencil.
median());
429 template<
typename Gr
idT,
typename MaskT,
typename InterruptT>
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)) {
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
Dense stencil of a given width.
Definition: Stencils.h:1648
General-purpose arithmetic and comparison routines, most of which accept arbitrary value types (or at...
#define OPENVDB_THROW(exception, message)
Definition: Exceptions.h:109
Signed (x, y, z) 32-bit integer coordinates.
Definition: Coord.h:52
void moveTo(const Coord &ijk)
Initialize the stencil buffer with the values of voxel (x, y, z) and its neighbors.
Definition: Stencils.h:1668
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h:128
typename CopyConstness< TreeType, NonConstBufferType >::Type BufferType
Definition: LeafManager.h:120
Definition: Exceptions.h:40
Axis
Definition: Math.h:876
ValueType median() const
Return the median value of the current stencil.
Definition: Stencils.h:147
This class manages a linear array of pointers to a given tree's leaf nodes, as well as optional auxil...
Definition: LeafManager.h:109
Defines various finite difference stencils by means of the "curiously recurring template pattern" on ...
Definition: Exceptions.h:92
int32_t Int32
Definition: Types.h:63
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:180
A LeafManager manages a linear array of pointers to a given tree's leaf nodes, as well as optional au...
bool wasInterrupted(T *i, int percent=-1)
Definition: NullInterrupter.h:76