14 #ifndef OPENVDB_TOOLS_FILTER_HAS_BEEN_INCLUDED 15 #define OPENVDB_TOOLS_FILTER_HAS_BEEN_INCLUDED 26 #include <openvdb/thread/Threading.h> 29 #include <tbb/parallel_for.h> 30 #include <tbb/concurrent_vector.h> 34 #include <type_traits> 42 template<
typename GridT,
43 typename MaskT =
typename GridT::template ValueConverter<float>::Type,
44 typename InterruptT = util::NullInterrupter>
51 using LeafType =
typename TreeType::LeafNodeType;
55 using RangeType =
typename LeafManagerType::LeafRange;
57 static_assert(std::is_floating_point<AlphaType>::value,
58 "openvdb::tools::Filter requires a mask grid with floating-point values");
63 Filter(GridT& grid, InterruptT* interrupt =
nullptr)
66 , mInterrupter(interrupt)
80 , mInterrupter(other.mInterrupter)
82 , mGrainSize(other.mGrainSize)
83 , mMinMask(other.mMinMask)
84 , mMaxMask(other.mMaxMask)
85 , mInvertMask(other.mInvertMask)
86 , mTiles(other.mTiles) {}
134 void mean(
int width = 1,
int iterations = 1,
const MaskType* mask =
nullptr);
143 void gaussian(
int width = 1,
int iterations = 1,
const MaskType* mask =
nullptr);
151 void median(
int width = 1,
int iterations = 1,
const MaskType* mask =
nullptr);
164 if (mTask) mTask(const_cast<Filter*>(
this), range);
169 using LeafT =
typename TreeType::LeafNodeType;
170 using VoxelIterT =
typename LeafT::ValueOnIter;
171 using VoxelCIterT =
typename LeafT::ValueOnCIter;
173 using LeafIterT =
typename RangeType::Iterator;
178 template<
size_t Axis>
180 Avg(
const GridT* grid,
Int32 w): acc(grid->tree()), width(w), frac(1.f/
float(2*w+1)) {}
182 typename GridT::ConstAccessor acc;
188 template <
typename AvgT>
190 void doBoxX(
const RangeType& r,
Int32 w) { this->doBox<Avg<0> >(r,w); }
191 void doBoxY(
const RangeType& r,
Int32 w) { this->doBox<Avg<1> >(r,w); }
192 void doBoxZ(
const RangeType& r,
Int32 w) { this->doBox<Avg<2> >(r,w); }
199 typename std::function<void (Filter*, const RangeType&)> mTask;
200 InterruptT* mInterrupter;
213 namespace filter_internal {
215 template<
typename TreeT>
221 using MaskT =
typename TreeT::template ValueConverter<ValueMask>::Type;
223 Voxelizer(TreeT& tree,
const bool allNeighbors,
const size_t grainSize)
226 , mGrainSize(grainSize)
227 , mOp(tree, mVoxelTopology, allNeighbors ? 26 : 6) {}
235 int run(
const int width)
237 if (!mOp.tree().hasActiveTiles())
return 0;
240 for (
int i = 0; i < width; i += int(TreeT::LeafNodeType::DIM), ++count) {
241 if (i > 0) mManager->rebuild();
242 mManager->foreachBottomUp(mOp, mGrainSize > 0, mGrainSize);
243 mOp.tree().topologyUnion(mVoxelTopology);
257 mVoxelTopology.topologyUnion(mOp.tree());
258 mManager.reset(
new NodeManagerT(mOp.tree()));
262 struct CreateVoxelMask
264 using LeafT =
typename TreeT::LeafNodeType;
265 using RootT =
typename TreeT::RootNodeType;
267 CreateVoxelMask(TreeT& tree, MaskT& mask,
const size_t NN)
268 : mTree(tree), mVoxelTopology(mask), mNeighbors(NN) {}
270 TreeT& tree() {
return mTree; }
274 void operator()(
const LeafT&)
const { assert(
false); }
276 void operator()(
const RootT& node)
const 278 using ChildT =
typename RootT::ChildNodeType;
279 static constexpr
Int32 CHILDDIM =
Int32(ChildT::DIM);
280 static constexpr
Int32 LEAFDIM =
Int32(LeafT::DIM);
281 const Tester op(mTree, mNeighbors);
284 [&](
const Coord& ijk,
290 Int32& a = offset[axis1];
291 Int32& b = offset[axis2];
292 for (a = 0; a < CHILDDIM; a+=LEAFDIM) {
293 for (b = 0; b < CHILDDIM; b+=LEAFDIM) {
294 const Coord childijk = ijk + offset;
295 if (op.test(childijk, val)) {
296 mVoxelTopology.touchLeaf(childijk);
301 offset.reset(CHILDDIM-1);
302 for (a = 0; a < CHILDDIM; a+=LEAFDIM) {
303 for (b = 0; b < CHILDDIM; b+=LEAFDIM) {
304 const Coord childijk = ijk + offset;
305 if (op.test(childijk, val)) {
306 mVoxelTopology.touchLeaf(childijk);
312 for (
auto iter = node.cbeginValueOn(); iter; ++iter) {
313 const Coord& ijk = iter.getCoord();
316 step(ijk, 0, 1, *iter);
317 step(ijk, 0, 2, *iter);
318 step(ijk, 1, 2, *iter);
322 template<
typename NodeT>
323 void operator()(
const NodeT& node)
const 325 using ChildT =
typename NodeT::ChildNodeType;
326 static constexpr
Int32 CHILDDIM =
Int32(ChildT::DIM);
327 static constexpr
Int32 LEAFDIM =
Int32(LeafT::DIM);
335 std::vector<Coord>& coords)
338 Int32& a = offset[axis1];
339 Int32& b = offset[axis2];
340 for (a = 0; a < CHILDDIM; a+=LEAFDIM) {
341 for (b = 0; b < CHILDDIM; b+=LEAFDIM) {
342 const Coord childijk = ijk + offset;
343 if (op.test(childijk, val)) {
344 coords.emplace_back(childijk);
349 offset.reset(CHILDDIM-1);
350 for (a = 0; a < CHILDDIM; a+=LEAFDIM) {
351 for (b = 0; b < CHILDDIM; b+=LEAFDIM) {
352 const Coord childijk = ijk + offset;
353 if (op.test(childijk, val)) {
354 coords.emplace_back(childijk);
380 if (CHILDDIM == LEAFDIM) {
388 std::vector<char> flags(NodeT::NUM_VALUES,
char(0));
389 tbb::parallel_for(tbb::blocked_range<size_t>(0, NodeT::NUM_VALUES),
390 [&](
const tbb::blocked_range<size_t>& range) {
391 const Tester op(mTree, mNeighbors);
392 for (
size_t n = range.begin(), N = range.end(); n < N; ++n) {
393 if (node.isValueMaskOn(
Index(n))) {
395 const Coord ijk = node.offsetToGlobalCoord(
Index(n));
396 flags[n] = op.test(ijk, node.getValue(ijk));
403 for (
auto iter = flags.begin(); iter != flags.end(); ++iter, ++idx) {
404 if (*iter) mVoxelTopology.touchLeaf(node.offsetToGlobalCoord(idx));
414 tbb::concurrent_vector<Coord> nodes;
415 tbb::parallel_for(tbb::blocked_range<size_t>(0, NodeT::NUM_VALUES),
416 [&](
const tbb::blocked_range<size_t>& range)
418 const Tester op(mTree, mNeighbors);
419 std::vector<Coord> coords;
421 for (
size_t n = range.begin(), N = range.end(); n < N; ++n) {
422 if (!node.isValueMaskOn(
Index(n)))
continue;
424 const Coord ijk = node.offsetToGlobalCoord(
Index(n));
425 const auto& val = node.getValue(ijk);
428 step(op, ijk, 0, 1, val, coords);
429 step(op, ijk, 0, 2, val, coords);
430 step(op, ijk, 1, 2, val, coords);
433 if (!coords.empty()) {
434 std::copy(coords.begin(), coords.end(),
435 nodes.grow_by(coords.size()));
441 for (
const auto& coord : nodes) {
442 mVoxelTopology.touchLeaf(coord);
450 Tester(
const TreeT& tree,
const size_t NN)
451 : mAcc(tree), mNeighbors(NN) {}
453 inline bool test(
const Coord& ijk,
454 const typename TreeT::ValueType& val)
const 456 static constexpr
Int32 LEAFDIM =
Int32(LeafT::DIM);
458 for (
size_t i = 0; i < mNeighbors; ++i, ++NN) {
460 neighbor.x() *= LEAFDIM;
461 neighbor.y() *= LEAFDIM;
462 neighbor.z() *= LEAFDIM;
465 if (mAcc.getValue(neighbor) != val ||
466 mAcc.probeConstLeaf(neighbor)) {
474 const size_t mNeighbors;
479 MaskT& mVoxelTopology;
480 const size_t mNeighbors;
484 MaskT mVoxelTopology;
485 std::unique_ptr<NodeManagerT> mManager;
486 const size_t mGrainSize;
491 template<
typename T>
static inline void accum(T& sum, T addend) { sum += addend; }
493 inline void accum(
bool& sum,
bool addend) { sum = sum || addend; }
502 template<
typename Gr
idT,
typename MaskT,
typename InterruptT>
503 template<
size_t Axis>
504 inline typename GridT::ValueType
509 for (i -= width; i <= j; ++i) filter_internal::accum(sum, acc.getValue(xyz));
510 if constexpr(std::is_same<ValueType, bool>::value) {
511 return sum && frac > 0.0f;
524 template<
typename Gr
idT,
typename MaskT,
typename InterruptT>
528 if (iterations <= 0)
return;
531 const bool serial = mGrainSize == 0;
533 if (mInterrupter) mInterrupter->start(
"Applying mean filter");
535 std::unique_ptr<filter_internal::Voxelizer<TreeType>> voxelizer;
536 if (this->getProcessTiles()) {
539 const bool allNeighbors = iterations > 1;
542 voxelizer.reset(
new filter_internal::Voxelizer<TreeType>
543 (mGrid->tree(), allNeighbors, mGrainSize));
544 if (!voxelizer->run(w)) voxelizer.reset();
551 for (
int i=0; i<iterations && !this->
wasInterrupted(); ++i, dist+=w) {
552 if (i > 0 && voxelizer) {
555 const int remain = dist - iter * int(TreeType::LeafNodeType::DIM);
557 const int searches = voxelizer->run(remain);
558 if (searches == 0) voxelizer.reset();
559 else leafs.rebuild(serial);
564 mTask = std::bind(&Filter::doBoxX, std::placeholders::_1, std::placeholders::_2, w);
568 mTask = std::bind(&Filter::doBoxZ, std::placeholders::_1, std::placeholders::_2, w);
570 mTask = std::bind(&Filter::doBoxY, std::placeholders::_1, std::placeholders::_2, w);
574 if (mInterrupter) mInterrupter->end();
578 template<
typename Gr
idT,
typename MaskT,
typename InterruptT>
582 if (iterations <= 0)
return;
585 const bool serial = mGrainSize == 0;
587 if (mInterrupter) mInterrupter->start(
"Applying Gaussian filter");
589 std::unique_ptr<filter_internal::Voxelizer<TreeType>> voxelizer;
590 if (this->getProcessTiles()) {
593 const bool allNeighbors = iterations > 1;
597 voxelizer.reset(
new filter_internal::Voxelizer<TreeType>
598 (mGrid->tree(), allNeighbors, mGrainSize));
599 if (!voxelizer->run(w*4)) voxelizer.reset();
606 for (
int i=0; i<iterations; ++i, dist+=(w*4)) {
607 if (i > 0 && voxelizer) {
610 const int remain = dist - iter * int(TreeType::LeafNodeType::DIM);
612 const int searches = voxelizer->run(remain);
613 if (searches == 0) voxelizer.reset();
614 else leafs.rebuild(serial);
620 mTask = std::bind(&Filter::doBoxX, std::placeholders::_1, std::placeholders::_2, w);
624 mTask = std::bind(&Filter::doBoxZ, std::placeholders::_1, std::placeholders::_2, w);
626 mTask = std::bind(&Filter::doBoxY, std::placeholders::_1, std::placeholders::_2, w);
631 if (mInterrupter) mInterrupter->end();
635 template<
typename Gr
idT,
typename MaskT,
typename InterruptT>
639 if (iterations <= 0)
return;
642 const bool serial = mGrainSize == 0;
644 if (mInterrupter) mInterrupter->start(
"Applying median filter");
646 std::unique_ptr<filter_internal::Voxelizer<TreeType>> voxelizer;
647 if (this->getProcessTiles()) {
650 voxelizer.reset(
new filter_internal::Voxelizer<TreeType>
651 (mGrid->tree(),
true, mGrainSize));
652 if (!voxelizer->run(w)) voxelizer.reset();
657 mTask = std::bind(&Filter::doMedian, std::placeholders::_1, std::placeholders::_2, w);
661 for (
int i=0; i<iterations && !this->
wasInterrupted(); ++i, dist+=w) {
662 if (i > 0 && voxelizer) {
665 const int remain = dist - iter * int(TreeType::LeafNodeType::DIM);
667 const int searches = voxelizer->run(remain);
668 if (searches == 0) voxelizer.reset();
669 else leafs.rebuild(serial);
677 if (mInterrupter) mInterrupter->end();
681 template<
typename Gr
idT,
typename MaskT,
typename InterruptT>
687 if (mInterrupter) mInterrupter->start(
"Applying offset");
689 if (this->getProcessTiles()) {
693 NodeManagerT manager(mGrid->tree());
698 AlphaMaskT alpha(*mGrid, *mMask, mMinMask, mMaxMask, mInvertMask);
700 for (
auto iter = node.beginValueOn(); iter; ++iter) {
701 if (!alpha(iter.getCoord(), a, b))
continue;
703 iter.modifyValue([&](
ValueType& v) { v += a*value; });
709 manager.foreachBottomUp([&](
auto& node) {
711 for (
auto iter = node.beginValueOn(); iter; ++iter) {
712 iter.modifyValue([&](
ValueType& v) { v += value; });
719 mTask = std::bind(&Filter::doOffset, std::placeholders::_1, std::placeholders::_2, value);
722 if (mInterrupter) mInterrupter->end();
731 template<
typename Gr
idT,
typename MaskT,
typename InterruptT>
736 tbb::parallel_for(leafs.leafRange(mGrainSize), *
this);
738 (*this)(leafs.leafRange());
740 leafs.swapLeafBuffer(1, mGrainSize==0);
745 template<
typename Gr
idT,
typename MaskT,
typename InterruptT>
746 template <
typename AvgT>
754 AlphaMaskT alpha(*mGrid, *mMask, mMinMask, mMaxMask, mInvertMask);
755 for (LeafIterT leafIter=range.begin(); leafIter; ++leafIter) {
756 BufferT& buffer = leafIter.buffer(1);
757 for (VoxelCIterT iter = leafIter->cbeginValueOn(); iter; ++iter) {
758 const Coord xyz = iter.getCoord();
759 if (alpha(xyz, a, b)) {
761 const ValueType value(b*(*iter) + a*avg(xyz));
763 buffer.setValue(iter.pos(), value);
768 for (LeafIterT leafIter=range.begin(); leafIter; ++leafIter) {
769 BufferT& buffer = leafIter.buffer(1);
770 for (VoxelCIterT iter = leafIter->cbeginValueOn(); iter; ++iter) {
771 buffer.setValue(iter.pos(), avg(iter.getCoord()));
779 template<
typename Gr
idT,
typename MaskT,
typename InterruptT>
787 AlphaMaskT alpha(*mGrid, *mMask, mMinMask, mMaxMask, mInvertMask);
788 for (LeafIterT leafIter=range.begin(); leafIter; ++leafIter) {
789 BufferT& buffer = leafIter.buffer(1);
790 for (VoxelCIterT iter = leafIter->cbeginValueOn(); iter; ++iter) {
791 if (alpha(iter.getCoord(), a, b)) {
796 buffer.setValue(iter.pos(), value);
801 for (LeafIterT leafIter=range.begin(); leafIter; ++leafIter) {
802 BufferT& buffer = leafIter.buffer(1);
803 for (VoxelCIterT iter = leafIter->cbeginValueOn(); iter; ++iter) {
805 buffer.setValue(iter.pos(), stencil.
median());
813 template<
typename Gr
idT,
typename MaskT,
typename InterruptT>
820 AlphaMaskT alpha(*mGrid, *mMask, mMinMask, mMaxMask, mInvertMask);
821 for (LeafIterT leafIter=range.begin(); leafIter; ++leafIter) {
822 for (VoxelIterT iter = leafIter->beginValueOn(); iter; ++iter) {
823 if (alpha(iter.getCoord(), a, b)) {
827 iter.setValue(value);
832 for (LeafIterT leafIter=range.begin(); leafIter; ++leafIter) {
833 for (VoxelIterT iter = leafIter->beginValueOn(); iter; ++iter) {
834 iter.setValue(*iter + offset);
841 template<
typename Gr
idT,
typename MaskT,
typename InterruptT>
846 thread::cancelGroupExecution();
858 #ifdef OPENVDB_USE_EXPLICIT_INSTANTIATION 860 #ifdef OPENVDB_INSTANTIATE_FILTER 867 #endif // OPENVDB_USE_EXPLICIT_INSTANTIATION 874 #endif // OPENVDB_TOOLS_FILTER_HAS_BEEN_INCLUDED
To facilitate threading over the nodes of a tree, cache node pointers in linear arrays, one for each level of the tree.
Definition: NodeManager.h:30
Definition: Exceptions.h:65
typename CopyConstness< TreeType, NonConstBufferType >::Type BufferType
Definition: LeafManager.h:95
bool wasInterrupted(T *i, int percent=-1)
Definition: NullInterrupter.h:49
The Value Accessor Implementation and API methods. The majoirty of the API matches the API of a compa...
Definition: ValueAccessor.h:68
#define OPENVDB_THROW(exception, message)
Definition: Exceptions.h:74
General-purpose arithmetic and comparison routines, most of which accept arbitrary value types (or at...
void moveTo(const Coord &ijk)
Initialize the stencil buffer with the values of voxel (x, y, z) and its neighbors.
Definition: Stencils.h:1784
void foreachBottomUp(const NodeOp &op, bool threaded=true, size_t grainSize=1)
Threaded method that applies a user-supplied functor to all the nodes in the tree.
Definition: NodeManager.h:624
constexpr Coord COORD_OFFSETS[26]
coordinate offset table for neighboring voxels
Definition: Util.h:22
Axis
Definition: Math.h:901
Definition: Exceptions.h:13
ValueType median() const
Return the median value of the current stencil.
Definition: Stencils.h:121
Index32 Index
Definition: Types.h:54
Dense stencil of a given width.
Definition: Stencils.h:1764
#define OPENVDB_INSTANTIATE_CLASS
Definition: version.h.in:153
OPENVDB_AX_API void run(const char *ax, openvdb::GridBase &grid, const AttributeBindings &bindings={})
Run a full AX pipeline (parse, compile and execute) on a single OpenVDB Grid.
A LeafManager manages a linear array of pointers to a given tree's leaf nodes, as well as optional au...
NodeManager produces linear arrays of all tree nodes allowing for efficient threading and bottom-up p...
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h.in:121
int32_t Int32
Definition: Types.h:56
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h.in:212