33 #ifndef OPENVDB_TOOLS_GRID_OPERATORS_HAS_BEEN_INCLUDED
34 #define OPENVDB_TOOLS_GRID_OPERATORS_HAS_BEEN_INCLUDED
36 #include <openvdb/Grid.h>
37 #include <openvdb/math/Operators.h>
38 #include <openvdb/math/Stencils.h>
39 #include <openvdb/tree/LeafManager.h>
40 #include <openvdb/tree/ValueAccessor.h>
41 #include <tbb/parallel_for.h>
42 #include <tbb/blocked_range.h>
56 typedef typename VectorGridType::template ValueConverter<VecComponentValueT>::Type
Type;
65 typedef typename ScalarGridType::template ValueConverter<VectorValueT>::Type
Type;
71 template<
typename Gr
idType>
inline
72 typename GridType::Ptr
73 curl(
const GridType& grid,
bool threaded =
true);
79 template<
typename Gr
idType>
inline
81 divergence(
const GridType& grid,
bool threaded =
true);
87 template<
typename Gr
idType>
inline
89 gradient(
const GridType& grid,
bool threaded =
true);
94 template<
typename Gr
idType>
inline
95 typename GridType::Ptr
96 laplacian(
const GridType& grid,
bool threaded =
true);
101 template<
typename Gr
idType>
inline
102 typename GridType::Ptr
108 template<
typename Gr
idType>
inline
109 typename GridType::Ptr
110 magnitude(
const GridType& grid,
bool threaded =
true);
115 template<
typename Gr
idType>
inline
116 typename GridType::Ptr
117 normalize(
const GridType& grid,
bool threaded =
true);
132 template<
typename InGr
idT,
typename OutGr
idT,
typename MapT,
typename OperatorT>
136 typedef typename OutGridT::TreeType OutTreeT;
137 typedef typename OutTreeT::LeafNodeType OutLeafT;
139 typedef typename LeafManagerT::RangeType RangeT;
141 GridOperator(
const InGridT& grid,
const MapT& map):
145 virtual ~GridOperator() {}
146 typename OutGridT::Ptr process(
bool threaded =
true)
149 typename InGridT::TreeType tmp(
mAcc.getTree()->getBackground());
150 typename OutGridT::ValueType backg = OperatorT::result(
mMap, tmp,
math::Coord(0));
152 typename OutTreeT::Ptr tree(
new OutTreeT(*
mAcc.getTree(), backg,
TopologyCopy()));
154 typename OutGridT::Ptr result(
new OutGridT(tree));
158 mLeafs =
new LeafManagerT(*tree);
160 tbb::parallel_for(
mLeafs->getRange(), *
this);
162 (*this)(
mLeafs->getRange());
175 void operator()(
const RangeT& range)
const
178 for (
size_t n=range.begin(), e=range.end(); n != e; ++n) {
179 OutLeafT& leaf =
mLeafs->leaf(n);
180 for (
typename OutLeafT::ValueOnIter iter = leaf.beginValueOn(); iter; ++iter) {
181 iter.setValue(OperatorT::result(
mMap,
mAcc, iter.getCoord()));
187 typedef typename InGridT::ConstAccessor AccessorT;
201 template<
typename Gr
idT>
207 Curl(
const GridT& grid): mInputGrid(grid) {}
209 typename GridT::Ptr process(
bool threaded =
true)
211 Functor functor(mInputGrid, threaded);
219 Functor(
const GridT& grid,
bool threaded): mThreaded(threaded), mInputGrid(grid) {}
220 template<
typename MapT>
221 void operator()(
const MapT& map)
224 GridOperator<GridT, GridT, MapT, OpT> op(mInputGrid, map);
225 mOutputGrid = op.process(mThreaded);
240 template<
typename InGr
idT>
249 typename OutGridType::Ptr process(
bool threaded =
true)
264 template<math::DScheme DiffScheme>
267 Functor(
const InGridT& grid,
bool threaded): mThreaded(threaded), mInputGrid(grid) {}
268 template<
typename MapT>
269 void operator()(
const MapT& map)
272 GridOperator<InGridType, OutGridType, MapT, OpT> op(mInputGrid, map);
273 mOutputGrid = op.process(mThreaded);
288 template<
typename InGr
idT>
297 typename OutGridType::Ptr process(
bool threaded =
true)
299 Functor functor(mInputGrid, threaded);
307 Functor(
const InGridT& grid,
bool threaded): mThreaded(threaded), mInputGrid(grid) {}
308 template<
typename MapT>
309 void operator()(
const MapT& map)
312 GridOperator<InGridType, OutGridType, MapT, OpT> op(mInputGrid, map);
313 mOutputGrid = op.process(mThreaded);
328 template<
typename Gr
idT>
336 typename GridT::Ptr process(
bool threaded =
true)
338 Functor functor(mInputGrid, threaded);
346 Functor(
const GridT& grid,
bool threaded): mThreaded(threaded), mInputGrid(grid) {}
347 template<
typename MapT>
348 void operator()(
const MapT& map)
351 GridOperator<GridT, GridT, MapT, OpT> op(mInputGrid, map);
352 mOutputGrid = op.process(mThreaded);
366 template<
typename Gr
idT>
374 typename GridT::Ptr process(
bool threaded =
true)
376 Functor functor(mInputGrid, threaded);
384 Functor(
const GridT& grid,
bool threaded): mThreaded(threaded), mInputGrid(grid) {}
385 template<
typename MapT>
386 void operator()(
const MapT& map)
389 GridOperator<GridT, GridT, MapT, OpT> op(mInputGrid, map);
390 mOutputGrid = op.process(mThreaded);
403 template<
typename InGr
idT>
411 typename OutGridType::Ptr process(
bool threaded =
true)
413 Functor functor(mInputGrid, threaded);
421 template<
typename MapT,
typename AccT>
422 static typename OutGridType::ValueType
423 result(
const MapT&,
const AccT& acc,
const Coord& xyz) {
return acc.getValue(xyz).length();}
427 Functor(
const InGridT& grid,
bool threaded): mThreaded(threaded), mInputGrid(grid) {}
428 template<
typename MapT>
429 void operator()(
const MapT& map)
431 GridOperator<InGridType, OutGridType, MapT, OpT> op(mInputGrid, map);
432 mOutputGrid = op.process(mThreaded);
446 template<
typename Gr
idT>
454 typename GridT::Ptr process(
bool threaded =
true)
456 Functor functor(mInputGrid, threaded);
464 template<
typename MapT,
typename AccT>
465 static typename OutGridType::ValueType
466 result(
const MapT&,
const AccT& acc,
const Coord& xyz)
468 typename OutGridType::ValueType vec = acc.getValue(xyz);
469 if ( !vec.normalize() ) vec.setZero();
475 Functor(
const GridT& grid,
bool threaded): mThreaded(threaded), mInputGrid(grid) {}
476 template<
typename MapT>
477 void operator()(
const MapT& map)
479 GridOperator<GridT, GridT, MapT, OpT> op(mInputGrid, map);
480 mOutputGrid = op.process(mThreaded);
493 template<
typename Gr
idType>
inline
494 typename GridType::Ptr
495 curl(
const GridType& grid,
bool threaded)
501 template<
typename Gr
idType>
inline
502 typename VectorToScalarConverter<GridType>::Type::Ptr
509 template<
typename Gr
idType>
inline
510 typename ScalarToVectorConverter<GridType>::Type::Ptr
517 template<
typename Gr
idType>
inline
518 typename GridType::Ptr
525 template<
typename Gr
idType>
inline
526 typename GridType::Ptr
533 template<
typename Gr
idType>
inline
534 typename GridType::Ptr
541 template<
typename Gr
idType>
inline
542 typename GridType::Ptr
553 #endif // OPENVDB_TOOLS_GRID_OPERATORS_HAS_BEEN_INCLUDED