9 #ifndef OPENVDB_TOOLS_GRID_OPERATORS_HAS_BEEN_INCLUDED
10 #define OPENVDB_TOOLS_GRID_OPERATORS_HAS_BEEN_INCLUDED
18 #include <tbb/parallel_for.h>
32 typedef typename VectorGridType::template ValueConverter<VecComponentValueT>::Type
Type;
41 typedef typename ScalarGridType::template ValueConverter<VectorValueT>::Type
Type;
51 template<
typename Gr
idType,
typename InterruptT>
inline
53 cpt(
const GridType& grid,
bool threaded, InterruptT* interrupt);
55 template<
typename Gr
idType,
typename MaskT,
typename InterruptT>
inline
57 cpt(
const GridType& grid,
const MaskT& mask,
bool threaded, InterruptT* interrupt);
59 template<
typename Gr
idType>
inline
61 cpt(
const GridType& grid,
bool threaded =
true)
63 return cpt<GridType, util::NullInterrupter>(grid, threaded,
nullptr);
66 template<
typename Gr
idType,
typename MaskT>
inline
67 typename ScalarToVectorConverter<GridType>::Type::Ptr
68 cpt(
const GridType& grid,
const MaskT& mask,
bool threaded =
true)
70 return cpt<GridType, MaskT, util::NullInterrupter>(grid, mask, threaded,
nullptr);
79 template<
typename Gr
idType,
typename InterruptT>
inline
80 typename GridType::Ptr
81 curl(
const GridType& grid,
bool threaded, InterruptT* interrupt);
83 template<
typename Gr
idType,
typename MaskT,
typename InterruptT>
inline
84 typename GridType::Ptr
85 curl(
const GridType& grid,
const MaskT& mask,
bool threaded, InterruptT* interrupt);
87 template<
typename Gr
idType>
inline
88 typename GridType::Ptr
89 curl(
const GridType& grid,
bool threaded =
true)
91 return curl<GridType, util::NullInterrupter>(grid, threaded,
nullptr);
94 template<
typename Gr
idType,
typename MaskT>
inline
95 typename GridType::Ptr
96 curl(
const GridType& grid,
const MaskT& mask,
bool threaded =
true)
98 return curl<GridType, MaskT, util::NullInterrupter>(grid, mask, threaded,
nullptr);
108 template<
typename Gr
idType,
typename InterruptT>
inline
109 typename VectorToScalarConverter<GridType>::Type::Ptr
110 divergence(
const GridType& grid,
bool threaded, InterruptT* interrupt);
112 template<
typename Gr
idType,
typename MaskT,
typename InterruptT>
inline
113 typename VectorToScalarConverter<GridType>::Type::Ptr
114 divergence(
const GridType& grid,
const MaskT& mask,
bool threaded, InterruptT* interrupt);
116 template<
typename Gr
idType>
inline
117 typename VectorToScalarConverter<GridType>::Type::Ptr
120 return divergence<GridType, util::NullInterrupter>(grid, threaded,
nullptr);
123 template<
typename Gr
idType,
typename MaskT>
inline
124 typename VectorToScalarConverter<GridType>::Type::Ptr
125 divergence(
const GridType& grid,
const MaskT& mask,
bool threaded =
true)
127 return divergence<GridType, MaskT, util::NullInterrupter>(grid, mask, threaded,
nullptr);
137 template<
typename Gr
idType,
typename InterruptT>
inline
138 typename ScalarToVectorConverter<GridType>::Type::Ptr
139 gradient(
const GridType& grid,
bool threaded, InterruptT* interrupt);
141 template<
typename Gr
idType,
typename MaskT,
typename InterruptT>
inline
142 typename ScalarToVectorConverter<GridType>::Type::Ptr
143 gradient(
const GridType& grid,
const MaskT& mask,
bool threaded, InterruptT* interrupt);
145 template<
typename Gr
idType>
inline
146 typename ScalarToVectorConverter<GridType>::Type::Ptr
147 gradient(
const GridType& grid,
bool threaded =
true)
149 return gradient<GridType, util::NullInterrupter>(grid, threaded,
nullptr);
152 template<
typename Gr
idType,
typename MaskT>
inline
153 typename ScalarToVectorConverter<GridType>::Type::Ptr
154 gradient(
const GridType& grid,
const MaskT& mask,
bool threaded =
true)
156 return gradient<GridType, MaskT, util::NullInterrupter>(grid, mask, threaded,
nullptr);
165 template<
typename Gr
idType,
typename InterruptT>
inline
166 typename GridType::Ptr
167 laplacian(
const GridType& grid,
bool threaded, InterruptT* interrupt);
169 template<
typename Gr
idType,
typename MaskT,
typename InterruptT>
inline
170 typename GridType::Ptr
171 laplacian(
const GridType& grid,
const MaskT& mask,
bool threaded, InterruptT* interrupt);
173 template<
typename Gr
idType>
inline
174 typename GridType::Ptr
177 return laplacian<GridType, util::NullInterrupter>(grid, threaded,
nullptr);
180 template<
typename Gr
idType,
typename MaskT>
inline
181 typename GridType::Ptr
182 laplacian(
const GridType& grid,
const MaskT mask,
bool threaded =
true)
184 return laplacian<GridType, MaskT, util::NullInterrupter>(grid, mask, threaded,
nullptr);
193 template<
typename Gr
idType,
typename InterruptT>
inline
194 typename GridType::Ptr
195 meanCurvature(
const GridType& grid,
bool threaded, InterruptT* interrupt);
197 template<
typename Gr
idType,
typename MaskT,
typename InterruptT>
inline
198 typename GridType::Ptr
199 meanCurvature(
const GridType& grid,
const MaskT& mask,
bool threaded, InterruptT* interrupt);
201 template<
typename Gr
idType>
inline
202 typename GridType::Ptr
205 return meanCurvature<GridType, util::NullInterrupter>(grid, threaded,
nullptr);
208 template<
typename Gr
idType,
typename MaskT>
inline
209 typename GridType::Ptr
210 meanCurvature(
const GridType& grid,
const MaskT& mask,
bool threaded =
true)
212 return meanCurvature<GridType, MaskT, util::NullInterrupter>(grid, mask, threaded,
nullptr);
222 template<
typename Gr
idType,
typename InterruptT>
inline
223 typename VectorToScalarConverter<GridType>::Type::Ptr
224 magnitude(
const GridType& grid,
bool threaded, InterruptT* interrupt);
226 template<
typename Gr
idType,
typename MaskT,
typename InterruptT>
inline
227 typename VectorToScalarConverter<GridType>::Type::Ptr
228 magnitude(
const GridType& grid,
const MaskT& mask,
bool threaded, InterruptT* interrupt);
230 template<
typename Gr
idType>
inline
231 typename VectorToScalarConverter<GridType>::Type::Ptr
234 return magnitude<GridType, util::NullInterrupter>(grid, threaded,
nullptr);
237 template<
typename Gr
idType,
typename MaskT>
inline
238 typename VectorToScalarConverter<GridType>::Type::Ptr
239 magnitude(
const GridType& grid,
const MaskT& mask,
bool threaded =
true)
241 return magnitude<GridType, MaskT, util::NullInterrupter>(grid, mask, threaded,
nullptr);
250 template<
typename Gr
idType,
typename InterruptT>
inline
251 typename GridType::Ptr
252 normalize(
const GridType& grid,
bool threaded, InterruptT* interrupt);
254 template<
typename Gr
idType,
typename MaskT,
typename InterruptT>
inline
255 typename GridType::Ptr
256 normalize(
const GridType& grid,
const MaskT& mask,
bool threaded, InterruptT* interrupt);
258 template<
typename Gr
idType>
inline
259 typename GridType::Ptr
262 return normalize<GridType, util::NullInterrupter>(grid, threaded,
nullptr);
265 template<
typename Gr
idType,
typename MaskT>
inline
266 typename GridType::Ptr
267 normalize(
const GridType& grid,
const MaskT& mask,
bool threaded =
true)
269 return normalize<GridType, MaskT, util::NullInterrupter>(grid, mask, threaded,
nullptr);
281 template<
typename Gr
idType>
294 typename MaskGridType,
306 GridOperator(
const InGridT& grid,
const MaskGridType* mask,
const MapT& map,
307 InterruptT* interrupt =
nullptr,
bool densify =
true)
308 : mAcc(grid.getConstAccessor())
310 , mInterrupt(interrupt)
319 typename OutGridT::Ptr
process(
bool threaded =
true)
321 if (mInterrupt) mInterrupt->start(
"Processing grid");
324 typename InGridT::TreeType tmp(mAcc.tree().background());
325 typename OutGridT::ValueType backg = OperatorT::result(mMap, tmp,
math::Coord(0));
333 if (mDensify) tree->voxelizeActiveTiles();
336 typename OutGridT::Ptr result(
new OutGridT(tree));
340 result->topologyIntersection(*mMask);
349 tbb::parallel_for(leafManager.
leafRange(), *
this);
356 using TileIter =
typename OutTreeT::ValueOnIter;
358 TileIter tileIter = tree->beginValueOn();
359 tileIter.setMaxDepth(tileIter.getLeafDepth() - 1);
362 auto tileOp = [
this, inAcc](
const TileIter& it) {
365 it.setValue(OperatorT::result(this->mMap, inAcc, it.getCoord()));
373 if (mDensify) tree->prune();
375 if (mInterrupt) mInterrupt->end();
388 for (
typename LeafManagerT::LeafRange::Iterator leaf=range.
begin(); leaf; ++leaf) {
389 for (
typename OutLeafT::ValueOnIter value=leaf->beginValueOn(); value; ++value) {
390 value.setValue(OperatorT::result(mMap, mAcc, value.getCoord()));
422 mInputGrid(grid), mInterrupt(interrupt), mMask(nullptr)
426 Cpt(
const InGridType& grid,
const MaskGridType& mask, InterruptT* interrupt =
nullptr):
427 mInputGrid(grid), mInterrupt(interrupt), mMask(&mask)
431 typename OutGridType::Ptr
process(
bool threaded =
true,
bool useWorldTransform =
true)
433 Functor functor(mInputGrid, mMask, threaded, useWorldTransform, mInterrupt);
436 return functor.mOutputGrid;
442 template<
typename MapT,
typename AccT>
443 static typename OutGridType::ValueType
444 result(
const MapT& map,
const AccT& acc,
const Coord& xyz)
451 template<
typename MapT,
typename AccT>
452 static typename OutGridType::ValueType
453 result(
const MapT& map,
const AccT& acc,
const Coord& xyz)
455 return math::CPT_RANGE<MapT, math::CD_2ND>::result(map, acc, xyz);
460 Functor(
const InGridType& grid,
const MaskGridType* mask,
461 bool threaded,
bool worldspace, InterruptT* interrupt)
462 : mThreaded(threaded)
463 , mWorldSpace(worldspace)
465 , mInterrupt(interrupt)
469 template<
typename MapT>
470 void operator()(
const MapT& map)
473 gridop::GridOperator<InGridType, MaskGridType, OutGridType, MapT, WsOpT, InterruptT>
474 op(mInputGrid, mMask, map, mInterrupt,
false);
475 mOutputGrid = op.process(mThreaded);
477 gridop::GridOperator<InGridType, MaskGridType, OutGridType, MapT, IsOpT, InterruptT>
478 op(mInputGrid, mMask, map, mInterrupt,
false);
479 mOutputGrid = op.process(mThreaded);
482 const bool mThreaded;
483 const bool mWorldSpace;
484 const InGridType& mInputGrid;
485 typename OutGridType::Ptr mOutputGrid;
486 InterruptT* mInterrupt;
487 const MaskGridType* mMask;
489 const InGridType& mInputGrid;
490 InterruptT* mInterrupt;
491 const MaskGridType* mMask;
501 typename MaskGridType =
typename gridop::ToMaskGrid<GridT>::Type,
502 typename InterruptT = util::NullInterrupter>
509 Curl(
const GridT& grid, InterruptT* interrupt =
nullptr):
510 mInputGrid(grid), mInterrupt(interrupt), mMask(nullptr)
514 Curl(
const GridT& grid,
const MaskGridType& mask, InterruptT* interrupt =
nullptr):
515 mInputGrid(grid), mInterrupt(interrupt), mMask(&mask)
519 typename GridT::Ptr
process(
bool threaded =
true)
521 Functor functor(mInputGrid, mMask, threaded, mInterrupt);
523 if (functor.mOutputGrid) functor.mOutputGrid->setVectorType(
VEC_COVARIANT);
524 return functor.mOutputGrid;
530 Functor(
const GridT& grid,
const MaskGridType* mask,
531 bool threaded, InterruptT* interrupt):
532 mThreaded(threaded), mInputGrid(grid), mInterrupt(interrupt), mMask(mask) {}
534 template<
typename MapT>
535 void operator()(
const MapT& map)
538 gridop::GridOperator<GridT, MaskGridType, GridT, MapT, OpT, InterruptT>
539 op(mInputGrid, mMask, map, mInterrupt);
540 mOutputGrid = op.process(mThreaded);
543 const bool mThreaded;
544 const GridT& mInputGrid;
545 typename GridT::Ptr mOutputGrid;
546 InterruptT* mInterrupt;
547 const MaskGridType* mMask;
550 const GridT& mInputGrid;
551 InterruptT* mInterrupt;
552 const MaskGridType* mMask;
562 typename MaskGridType =
typename gridop::ToMaskGrid<InGridT>::Type,
563 typename InterruptT = util::NullInterrupter>
570 Divergence(
const InGridT& grid, InterruptT* interrupt =
nullptr):
571 mInputGrid(grid), mInterrupt(interrupt), mMask(nullptr)
575 Divergence(
const InGridT& grid,
const MaskGridType& mask, InterruptT* interrupt =
nullptr):
576 mInputGrid(grid), mInterrupt(interrupt), mMask(&mask)
580 typename OutGridType::Ptr
process(
bool threaded =
true)
585 return functor.mOutputGrid;
589 return functor.mOutputGrid;
594 template<math::DScheme DiffScheme>
597 Functor(
const InGridT& grid,
const MaskGridType* mask,
598 bool threaded, InterruptT* interrupt):
599 mThreaded(threaded), mInputGrid(grid), mInterrupt(interrupt), mMask(mask) {}
601 template<
typename MapT>
606 op(mInputGrid, mMask, map, mInterrupt);
607 mOutputGrid = op.
process(mThreaded);
637 Gradient(
const InGridT& grid, InterruptT* interrupt =
nullptr):
638 mInputGrid(grid), mInterrupt(interrupt), mMask(nullptr)
642 Gradient(
const InGridT& grid,
const MaskGridType& mask, InterruptT* interrupt =
nullptr):
643 mInputGrid(grid), mInterrupt(interrupt), mMask(&mask)
647 typename OutGridType::Ptr
process(
bool threaded =
true)
649 Functor functor(mInputGrid, mMask, threaded, mInterrupt);
651 if (functor.mOutputGrid) functor.mOutputGrid->setVectorType(
VEC_COVARIANT);
652 return functor.mOutputGrid;
658 Functor(
const InGridT& grid,
const MaskGridType* mask,
659 bool threaded, InterruptT* interrupt):
660 mThreaded(threaded), mInputGrid(grid), mInterrupt(interrupt), mMask(mask) {}
662 template<
typename MapT>
667 op(mInputGrid, mMask, map, mInterrupt);
668 mOutputGrid = op.
process(mThreaded);
697 Laplacian(
const GridT& grid, InterruptT* interrupt =
nullptr):
698 mInputGrid(grid), mInterrupt(interrupt), mMask(nullptr)
702 Laplacian(
const GridT& grid,
const MaskGridType& mask, InterruptT* interrupt =
nullptr):
703 mInputGrid(grid), mInterrupt(interrupt), mMask(&mask)
707 typename GridT::Ptr
process(
bool threaded =
true)
709 Functor functor(mInputGrid, mMask, threaded, mInterrupt);
711 if (functor.mOutputGrid) functor.mOutputGrid->setVectorType(
VEC_COVARIANT);
712 return functor.mOutputGrid;
718 Functor(
const GridT& grid,
const MaskGridType* mask,
bool threaded, InterruptT* interrupt):
719 mThreaded(threaded), mInputGrid(grid), mInterrupt(interrupt), mMask(mask) {}
721 template<
typename MapT>
726 op(mInputGrid, mMask, map, mInterrupt);
727 mOutputGrid = op.
process(mThreaded);
757 mInputGrid(grid), mInterrupt(interrupt), mMask(nullptr)
761 MeanCurvature(
const GridT& grid,
const MaskGridType& mask, InterruptT* interrupt =
nullptr):
762 mInputGrid(grid), mInterrupt(interrupt), mMask(&mask)
766 typename GridT::Ptr
process(
bool threaded =
true)
768 Functor functor(mInputGrid, mMask, threaded, mInterrupt);
770 if (functor.mOutputGrid) functor.mOutputGrid->setVectorType(
VEC_COVARIANT);
771 return functor.mOutputGrid;
777 Functor(
const GridT& grid,
const MaskGridType* mask,
bool threaded, InterruptT* interrupt):
778 mThreaded(threaded), mInputGrid(grid), mInterrupt(interrupt), mMask(mask) {}
780 template<
typename MapT>
785 op(mInputGrid, mMask, map, mInterrupt);
786 mOutputGrid = op.
process(mThreaded);
816 mInputGrid(grid), mInterrupt(interrupt), mMask(nullptr)
821 mInputGrid(grid), mInterrupt(interrupt), mMask(&mask)
825 typename OutGridType::Ptr
process(
bool threaded =
true)
827 Functor functor(mInputGrid, mMask, threaded, mInterrupt);
829 return functor.mOutputGrid;
835 template<
typename MapT,
typename AccT>
836 static typename OutGridType::ValueType
837 result(
const MapT&,
const AccT& acc,
const Coord& xyz) {
return acc.getValue(xyz).length();}
841 Functor(
const InGridT& grid,
const MaskGridType* mask,
842 bool threaded, InterruptT* interrupt):
843 mThreaded(threaded), mInputGrid(grid), mInterrupt(interrupt), mMask(mask) {}
845 template<
typename MapT>
849 op(mInputGrid, mMask, map, mInterrupt,
false);
850 mOutputGrid = op.
process(mThreaded);
879 Normalize(
const GridT& grid, InterruptT* interrupt =
nullptr):
880 mInputGrid(grid), mInterrupt(interrupt), mMask(nullptr)
884 Normalize(
const GridT& grid,
const MaskGridType& mask, InterruptT* interrupt =
nullptr):
885 mInputGrid(grid), mInterrupt(interrupt), mMask(&mask)
889 typename GridT::Ptr
process(
bool threaded =
true)
891 Functor functor(mInputGrid, mMask, threaded, mInterrupt);
893 if (
typename GridT::Ptr outGrid = functor.mOutputGrid) {
894 const VecType vecType = mInputGrid.getVectorType();
898 outGrid->setVectorType(vecType);
901 return functor.mOutputGrid;
907 template<
typename MapT,
typename AccT>
908 static typename OutGridType::ValueType
911 typename OutGridType::ValueType vec = acc.getValue(xyz);
912 if ( !vec.normalize() ) vec.setZero();
918 Functor(
const GridT& grid,
const MaskGridType* mask,
bool threaded, InterruptT* interrupt):
919 mThreaded(threaded), mInputGrid(grid), mInterrupt(interrupt), mMask(mask) {}
921 template<
typename MapT>
925 op(mInputGrid, mMask, map, mInterrupt,
false);
926 mOutputGrid = op.
process(mThreaded);
945 template<
typename Gr
idType,
typename InterruptT>
inline
947 cpt(
const GridType& grid,
bool threaded, InterruptT* interrupt)
953 template<
typename Gr
idType,
typename MaskT,
typename InterruptT>
inline
954 typename ScalarToVectorConverter<GridType>::Type::Ptr
955 cpt(
const GridType& grid,
const MaskT& mask,
bool threaded, InterruptT* interrupt)
961 template<
typename Gr
idType,
typename InterruptT>
inline
962 typename GridType::Ptr
963 curl(
const GridType& grid,
bool threaded, InterruptT* interrupt)
969 template<
typename Gr
idType,
typename MaskT,
typename InterruptT>
inline
970 typename GridType::Ptr
971 curl(
const GridType& grid,
const MaskT& mask,
bool threaded, InterruptT* interrupt)
977 template<
typename Gr
idType,
typename InterruptT>
inline
978 typename VectorToScalarConverter<GridType>::Type::Ptr
979 divergence(
const GridType& grid,
bool threaded, InterruptT* interrupt)
986 template<
typename Gr
idType,
typename MaskT,
typename InterruptT>
inline
987 typename VectorToScalarConverter<GridType>::Type::Ptr
988 divergence(
const GridType& grid,
const MaskT& mask,
bool threaded, InterruptT* interrupt)
994 template<
typename Gr
idType,
typename InterruptT>
inline
995 typename ScalarToVectorConverter<GridType>::Type::Ptr
996 gradient(
const GridType& grid,
bool threaded, InterruptT* interrupt)
1003 template<
typename Gr
idType,
typename MaskT,
typename InterruptT>
inline
1004 typename ScalarToVectorConverter<GridType>::Type::Ptr
1005 gradient(
const GridType& grid,
const MaskT& mask,
bool threaded, InterruptT* interrupt)
1011 template<
typename Gr
idType,
typename InterruptT>
inline
1012 typename GridType::Ptr
1013 laplacian(
const GridType& grid,
bool threaded, InterruptT* interrupt)
1016 op(grid, interrupt);
1020 template<
typename Gr
idType,
typename MaskT,
typename InterruptT>
inline
1021 typename GridType::Ptr
1022 laplacian(
const GridType& grid,
const MaskT& mask,
bool threaded, InterruptT* interrupt)
1028 template<
typename Gr
idType,
typename InterruptT>
inline
1029 typename GridType::Ptr
1033 op(grid, interrupt);
1037 template<
typename Gr
idType,
typename MaskT,
typename InterruptT>
inline
1038 typename GridType::Ptr
1039 meanCurvature(
const GridType& grid,
const MaskT& mask,
bool threaded, InterruptT* interrupt)
1045 template<
typename Gr
idType,
typename InterruptT>
inline
1046 typename VectorToScalarConverter<GridType>::Type::Ptr
1047 magnitude(
const GridType& grid,
bool threaded, InterruptT* interrupt)
1050 op(grid, interrupt);
1054 template<
typename Gr
idType,
typename MaskT,
typename InterruptT>
inline
1055 typename VectorToScalarConverter<GridType>::Type::Ptr
1056 magnitude(
const GridType& grid,
const MaskT& mask,
bool threaded, InterruptT* interrupt)
1062 template<
typename Gr
idType,
typename InterruptT>
inline
1063 typename GridType::Ptr
1064 normalize(
const GridType& grid,
bool threaded, InterruptT* interrupt)
1067 op(grid, interrupt);
1071 template<
typename Gr
idType,
typename MaskT,
typename InterruptT>
inline
1072 typename GridType::Ptr
1073 normalize(
const GridType& grid,
const MaskT& mask,
bool threaded, InterruptT* interrupt)
1083 #endif // OPENVDB_TOOLS_GRID_OPERATORS_HAS_BEEN_INCLUDED