29#ifndef OPENVDB_TOOLS_VALUETRANSFORMER_HAS_BEEN_INCLUDED
30#define OPENVDB_TOOLS_VALUETRANSFORMER_HAS_BEEN_INCLUDED
33#include <tbb/parallel_for.h>
34#include <tbb/parallel_reduce.h>
88template<
typename IterT,
typename XformOp>
89inline void foreach(
const IterT& iter, XformOp& op,
90 bool threaded =
true,
bool shareOp =
true);
92template<
typename IterT,
typename XformOp>
93inline void foreach(
const IterT& iter,
const XformOp& op,
94 bool threaded =
true,
bool shareOp =
true);
137template<
typename InIterT,
typename OutGr
idT,
typename XformOp>
139 XformOp& op,
bool threaded =
true,
bool shareOp =
true,
140 MergePolicy merge = MERGE_ACTIVE_STATES);
142template<
typename InIterT,
typename OutGr
idT,
typename XformOp>
144 const XformOp& op,
bool threaded =
true,
bool shareOp =
true,
145 MergePolicy merge = MERGE_ACTIVE_STATES);
192template<
typename IterT,
typename XformOp>
193inline void accumulate(
const IterT& iter, XformOp& op,
bool threaded =
true);
201template<
typename TreeT>
202void setValueOnMin(TreeT& tree,
const Coord& xyz,
const typename TreeT::ValueType& value);
209template<
typename TreeT>
210void setValueOnMax(TreeT& tree,
const Coord& xyz,
const typename TreeT::ValueType& value);
217template<
typename TreeT>
218void setValueOnSum(TreeT& tree,
const Coord& xyz,
const typename TreeT::ValueType& value);
225template<
typename TreeT>
226void setValueOnMult(TreeT& tree,
const Coord& xyz,
const typename TreeT::ValueType& value);
234template<
typename ValueType>
237 MinOp(
const ValueType& v): val(v) {}
238 inline void operator()(ValueType& v)
const { v = std::min<ValueType>(v, val); }
241template<
typename ValueType>
244 MaxOp(
const ValueType& v): val(v) {}
245 inline void operator()(ValueType& v)
const { v = std::max<ValueType>(v, val); }
248template<
typename ValueType>
251 SumOp(
const ValueType& v): val(v) {}
263template<
typename ValueType>
281template<
typename TreeT>
289template<
typename TreeT>
297template<
typename TreeT>
305template<
typename TreeT>
318template<
typename IterT,
typename OpT>
330 tbb::parallel_for(range, *
this);
344template<
typename IterT,
typename OpT>
355 mIter(other.mIter), mOp(*other.mOrigOp), mOrigOp(other.mOrigOp) {}
361 tbb::parallel_for(range, *
this);
372 OpT
const *
const mOrigOp;
378template<
typename IterT,
typename XformOp>
380foreach(
const IterT& iter, XformOp& op,
bool threaded,
bool shared)
387 Processor proc(iter, op);
388 proc.process(threaded);
392template<
typename IterT,
typename XformOp>
394foreach(
const IterT& iter,
const XformOp& op,
bool threaded,
bool )
407template<
typename InIterT,
typename OutTreeT,
typename OpT>
408class SharedOpTransformer
411 using InTreeT =
typename InIterT::TreeT;
413 using OutValueT =
typename OutTreeT::ValueType;
415 SharedOpTransformer(
const InIterT& inIter, OutTreeT& outTree, OpT& op,
MergePolicy merge):
418 mInputTree(inIter.getTree()),
419 mOutputTree(&outTree),
423 if (
static_cast<const void*
>(mInputTree) ==
static_cast<void*
>(mOutputTree)) {
425 " to transform a grid in place");
430 SharedOpTransformer(SharedOpTransformer& other, tbb::split):
432 mInputIter(other.mInputIter),
433 mInputTree(other.mInputTree),
434 mOutputTree(new OutTreeT(zeroVal<OutValueT>())),
436 mMergePolicy(other.mMergePolicy)
439 ~SharedOpTransformer()
445 mOutputTree =
nullptr;
449 void process(
bool threaded =
true)
451 if (!mInputTree || !mOutputTree)
return;
453 IterRange range(mInputIter);
458 tbb::parallel_reduce(range, *
this);
465 void operator()(
const IterRange& range)
const
467 if (!mOutputTree)
return;
469 typename tree::ValueAccessor<OutTreeT> outAccessor(*mOutputTree);
471 mOp(r.iterator(), outAccessor);
475 void join(
const SharedOpTransformer& other)
477 if (mOutputTree && other.mOutputTree) {
478 mOutputTree->merge(*other.mOutputTree, mMergePolicy);
485 const InTreeT* mInputTree;
486 OutTreeT* mOutputTree;
492template<
typename InIterT,
typename OutTreeT,
typename OpT>
493class CopyableOpTransformer
496 using InTreeT =
typename InIterT::TreeT;
497 using IterRange =
typename tree::IteratorRange<InIterT>;
498 using OutValueT =
typename OutTreeT::ValueType;
500 CopyableOpTransformer(
const InIterT& inIter, OutTreeT& outTree,
501 const OpT& op, MergePolicy merge):
504 mInputTree(inIter.getTree()),
505 mOutputTree(&outTree),
510 if (
static_cast<const void*
>(mInputTree) ==
static_cast<void*
>(mOutputTree)) {
512 " to transform a grid in place");
518 CopyableOpTransformer(CopyableOpTransformer& other, tbb::split):
520 mInputIter(other.mInputIter),
521 mInputTree(other.mInputTree),
522 mOutputTree(new OutTreeT(
zeroVal<OutValueT>())),
524 mOrigOp(other.mOrigOp),
525 mMergePolicy(other.mMergePolicy)
528 ~CopyableOpTransformer()
534 mOutputTree =
nullptr;
538 void process(
bool threaded =
true)
540 if (!mInputTree || !mOutputTree)
return;
542 IterRange range(mInputIter);
547 tbb::parallel_reduce(range, *
this);
554 void operator()(
const IterRange& range)
556 if (!mOutputTree)
return;
558 typename tree::ValueAccessor<OutTreeT> outAccessor(*mOutputTree);
560 mOp(r.iterator(), outAccessor);
564 void join(
const CopyableOpTransformer& other)
566 if (mOutputTree && other.mOutputTree) {
567 mOutputTree->merge(*other.mOutputTree, mMergePolicy);
574 const InTreeT* mInputTree;
575 OutTreeT* mOutputTree;
577 OpT
const *
const mOrigOp;
587template<
typename InIterT,
typename OutGr
idT,
typename XformOp>
593 using OutTreeT =
typename Adapter::TreeType;
595 using Processor =
typename valxform::SharedOpTransformer<InIterT, OutTreeT, XformOp>;
596 Processor proc(inIter, Adapter::tree(outGrid), op, merge);
597 proc.process(threaded);
599 using Processor =
typename valxform::CopyableOpTransformer<InIterT, OutTreeT, XformOp>;
600 Processor proc(inIter, Adapter::tree(outGrid), op, merge);
601 proc.process(threaded);
605template<
typename InIterT,
typename OutGr
idT,
typename XformOp>
611 using OutTreeT =
typename Adapter::TreeType;
613 using Processor =
typename valxform::SharedOpTransformer<InIterT, OutTreeT, const XformOp>;
614 Processor proc(inIter, Adapter::tree(outGrid), op, merge);
615 proc.process(threaded);
624template<
typename IterT,
typename OpT>
634 OpAccumulator(
const IterT& iter, OpT& op):
643 OpAccumulator(OpAccumulator& other, tbb::split):
646 mOp(new OpT(*other.mOrigOp)),
647 mOrigOp(other.mOrigOp)
650 ~OpAccumulator() {
if (mIsRoot)
delete mOrigOp;
else delete mOp; }
652 void process(
bool threaded =
true)
654 IterRange range(mIter);
656 tbb::parallel_reduce(range, *
this);
662 void operator()(
const IterRange& r) {
for (IterRange it(r); it.test(); ++it) (*mOp)(it.iterator()); }
664 void join(OpAccumulator& other) { mOp->join(*other.mOp); }
670 OpT
const *
const mOrigOp;
679template<
typename IterT,
typename XformOp>
681accumulate(
const IterT& iter, XformOp& op,
bool threaded)
683 typename valxform::OpAccumulator<IterT, XformOp> proc(iter, op);
684 proc.process(threaded);
693#ifdef OPENVDB_USE_EXPLICIT_INSTANTIATION
695#ifdef OPENVDB_INSTANTIATE_VALUETRANSFORMER
699#define _FUNCTION(TreeT) \
700 void setValueOnMin(TreeT&, const Coord&, const TreeT::ValueType&)
704#define _FUNCTION(TreeT) \
705 void setValueOnMax(TreeT&, const Coord&, const TreeT::ValueType&)
709#define _FUNCTION(TreeT) \
710 void setValueOnSum(TreeT&, const Coord&, const TreeT::ValueType&)
714#define _FUNCTION(TreeT) \
715 void setValueOnMult(TreeT&, const Coord&, const TreeT::ValueType&)
Signed (x, y, z) 32-bit integer coordinates.
Definition Coord.h:25
Definition TreeIterator.h:1303
#define OPENVDB_LOG_INFO(message)
Log an info message of the form 'someVar << "some text" << ...'.
Definition logging.h:254
PromoteType< ValueT >::Highest accumulate(const PointDataTreeT &points, const std::string &attribute, const FilterT &filter=NullFilter())
Evaluates the total value of a point attribute.
Definition PointStatisticsImpl.h:530
constexpr T zeroVal()
Return the value of type T that corresponds to zero.
Definition Math.h:70
MergePolicy
Definition Types.h:506
Definition Exceptions.h:13
This adapter allows code that is templated on a Tree type to accept either a Tree type or a Grid type...
Definition Grid.h:1060
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition version.h.in:121
#define OPENVDB_USE_VERSION_NAMESPACE
Definition version.h.in:212
#define OPENVDB_VOLUME_TREE_INSTANTIATE(Function)
Definition version.h.in:160