14 #ifndef OPENVDB_TOOLS_FILTER_HAS_BEEN_INCLUDED
15 #define OPENVDB_TOOLS_FILTER_HAS_BEEN_INCLUDED
29 #include <tbb/parallel_for.h>
30 #include <tbb/concurrent_vector.h>
34 #include <type_traits>
42 template<
typename GridT,
44 typename InterruptT = util::NullInterrupter>
51 using LeafType =
typename TreeType::LeafNodeType;
55 using RangeType =
typename LeafManagerType::LeafRange;
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) {}
118 if (!(min < max))
OPENVDB_THROW(ValueError,
"Invalid mask range (expects min < max)");
164 if (mTask) mTask(const_cast<Filter*>(
this), range);
165 else OPENVDB_THROW(ValueError,
"task is undefined - call median(), mean(), etc.");
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); }
196 bool wasInterrupted();
199 typename std::function<void (Filter*, const RangeType&)> mTask;
200 InterruptT* mInterrupter;
213 namespace filter_internal {
215 template<
typename TreeT>
220 using NodeManagerT = tree::NodeManager<TreeT, TreeT::RootNodeType::LEVEL-1>;
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,
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));
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;
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()) {
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,
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)) {
473 const tree::ValueAccessor<const TreeT> mAcc;
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>
509 for (i -= width; i <=
j; ++i) filter_internal::accum(sum, acc.getValue(xyz));
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();
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);
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());
700 for (
auto iter = node.beginValueOn(); iter; ++iter) {
701 if (!
alpha(iter.getCoord(),
a,
b))
continue;
709 manager.foreachBottomUp([&](
auto& node) {
711 for (
auto iter = node.beginValueOn(); iter; ++iter) {
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>
738 (*this)(leafs.leafRange());
740 leafs.swapLeafBuffer(1, mGrainSize==0);
745 template<
typename Gr
idT,
typename MaskT,
typename InterruptT>
746 template <
typename AvgT>
748 Filter<GridT, MaskT, InterruptT>::doBox(
const RangeType& range, Int32
w)
754 AlphaMaskT
alpha(*mGrid, *
mMask, mMinMask, mMaxMask, mInvertMask);
755 for (LeafIterT leafIter=range.begin(); leafIter; ++leafIter) {
757 for (VoxelCIterT iter = leafIter->cbeginValueOn(); iter; ++iter) {
758 const Coord xyz = iter.getCoord();
759 if (
alpha(xyz, a, b)) {
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>
781 Filter<GridT, MaskT, InterruptT>::doMedian(
const RangeType& range,
int width)
784 typename math::DenseStencil<GridType>
stencil(*mGrid, width);
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>
815 Filter<GridT, MaskT, InterruptT>::doOffset(
const RangeType& range,
ValueType offset)
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
void parallel_for(int64_t start, int64_t end, std::function< void(int64_t index)> &&task, parallel_options opt=parallel_options(0, Split_Y, 1))
typedef int(APIENTRYP RE_PFNGLXSWAPINTERVALSGIPROC)(int)
GA_API const UT_StringHolder dist
cvex test(vector P=0;int unbound=3;export float s=0;export vector Cf=0;)
typename CopyConstness< TreeType, NonConstBufferType >::Type BufferType
constexpr Coord COORD_OFFSETS[26]
coordinate offset table for neighboring voxels
OIIO_UTIL_API bool copy(string_view from, string_view to, std::string &err)
GLsizei const GLfloat * value
GLboolean GLboolean GLboolean GLboolean a
#define OPENVDB_USE_VERSION_NAMESPACE
ImageBuf OIIO_API min(Image_or_Const A, Image_or_Const B, ROI roi={}, int nthreads=0)
NodeManager produces linear arrays of all tree nodes allowing for efficient threading and bottom-up p...
SYS_FORCE_INLINE const_iterator end() const
To facilitate threading over the nodes of a tree, cache node pointers in linear arrays, one for each level of the tree.
IMATH_NAMESPACE::V2f float
#define OPENVDB_INSTANTIATE_CLASS
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.
General-purpose arithmetic and comparison routines, most of which accept arbitrary value types (or at...
GLfloat GLfloat GLfloat alpha
GLboolean GLboolean GLboolean b
buffer(size_t sz) FMT_NOEXCEPT
ImageBuf OIIO_API max(Image_or_Const A, Image_or_Const B, ROI roi={}, int nthreads=0)
GA_API const UT_StringHolder N
GLubyte GLubyte GLubyte GLubyte w
A LeafManager manages a linear array of pointers to a given tree's leaf nodes, as well as optional au...
GLint GLfloat GLint stencil
bool wasInterrupted(T *i, int percent=-1)
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
#define OPENVDB_THROW(exception, message)