36 #ifndef OPENVDB_TOOLS_GRID_OPERATORS_HAS_BEEN_INCLUDED
37 #define OPENVDB_TOOLS_GRID_OPERATORS_HAS_BEEN_INCLUDED
39 #include <openvdb/Grid.h>
40 #include <openvdb/math/Operators.h>
41 #include <openvdb/util/NullInterrupter.h>
42 #include <openvdb/tree/LeafManager.h>
43 #include <openvdb/tree/ValueAccessor.h>
44 #include <tbb/parallel_for.h>
58 typedef typename VectorGridType::template ValueConverter<VecComponentValueT>::Type
Type;
67 typedef typename ScalarGridType::template ValueConverter<VectorValueT>::Type
Type;
81 template<
typename Gr
idType,
typename InterruptT>
inline
83 cpt(
const GridType& grid,
bool threaded, InterruptT* interrupt);
85 template<
typename Gr
idType,
typename MaskT,
typename InterruptT>
inline
87 cpt(
const GridType& grid,
const MaskT& mask,
bool threaded, InterruptT* interrupt);
90 template<
typename Gr
idType>
inline
92 cpt(
const GridType& grid,
bool threaded =
true)
94 return cpt<GridType, util::NullInterrupter>(grid, threaded, NULL);
98 template<
typename Gr
idType,
typename MaskT>
inline
99 typename ScalarToVectorConverter<GridType>::Type::Ptr
100 cpt(
const GridType& grid,
const MaskT& mask,
bool threaded =
true)
102 return cpt<GridType, MaskT, util::NullInterrupter>(grid, mask, threaded, NULL);
111 template<
typename Gr
idType,
typename InterruptT>
inline
112 typename GridType::Ptr
113 curl(
const GridType& grid,
bool threaded, InterruptT* interrupt);
115 template<
typename Gr
idType,
typename MaskT,
typename InterruptT>
inline
116 typename GridType::Ptr
117 curl(
const GridType& grid,
const MaskT& mask,
bool threaded, InterruptT* interrupt);
119 template<
typename Gr
idType>
inline
120 typename GridType::Ptr
121 curl(
const GridType& grid,
bool threaded =
true)
123 return curl<GridType, util::NullInterrupter>(grid, threaded, NULL);
126 template<
typename Gr
idType,
typename MaskT>
inline
127 typename GridType::Ptr
128 curl(
const GridType& grid,
const MaskT& mask,
bool threaded =
true)
130 return curl<GridType, MaskT, util::NullInterrupter>(grid, mask, threaded, NULL);
139 template<
typename Gr
idType,
typename InterruptT>
inline
140 typename VectorToScalarConverter<GridType>::Type::Ptr
141 divergence(
const GridType& grid,
bool threaded, InterruptT* interrupt);
143 template<
typename Gr
idType,
typename MaskT,
typename InterruptT>
inline
144 typename VectorToScalarConverter<GridType>::Type::Ptr
145 divergence(
const GridType& grid,
const MaskT& mask,
bool threaded, InterruptT* interrupt);
147 template<
typename Gr
idType>
inline
148 typename VectorToScalarConverter<GridType>::Type::Ptr
151 return divergence<GridType, util::NullInterrupter>(grid, threaded, NULL);
154 template<
typename Gr
idType,
typename MaskT>
inline
155 typename VectorToScalarConverter<GridType>::Type::Ptr
156 divergence(
const GridType& grid,
const MaskT& mask,
bool threaded =
true)
158 return divergence<GridType, MaskT, util::NullInterrupter>(grid, mask, threaded, NULL);
167 template<
typename Gr
idType,
typename InterruptT>
inline
168 typename ScalarToVectorConverter<GridType>::Type::Ptr
169 gradient(
const GridType& grid,
bool threaded, InterruptT* interrupt);
171 template<
typename Gr
idType,
typename MaskT,
typename InterruptT>
inline
172 typename ScalarToVectorConverter<GridType>::Type::Ptr
173 gradient(
const GridType& grid,
const MaskT& mask,
bool threaded, InterruptT* interrupt);
175 template<
typename Gr
idType>
inline
176 typename ScalarToVectorConverter<GridType>::Type::Ptr
177 gradient(
const GridType& grid,
bool threaded =
true)
179 return gradient<GridType, util::NullInterrupter>(grid, threaded, NULL);
183 template<
typename Gr
idType,
typename MaskT>
inline
184 typename ScalarToVectorConverter<GridType>::Type::Ptr
185 gradient(
const GridType& grid,
const MaskT& mask,
bool threaded =
true)
187 return gradient<GridType, MaskT, util::NullInterrupter>(grid, mask, threaded, NULL);
196 template<
typename Gr
idType,
typename InterruptT>
inline
197 typename GridType::Ptr
198 laplacian(
const GridType& grid,
bool threaded, InterruptT* interrupt);
200 template<
typename Gr
idType,
typename MaskT,
typename InterruptT>
inline
201 typename GridType::Ptr
202 laplacian(
const GridType& grid,
const MaskT& mask,
bool threaded, InterruptT* interrupt);
204 template<
typename Gr
idType>
inline
205 typename GridType::Ptr
208 return laplacian<GridType, util::NullInterrupter>(grid, threaded, NULL);
211 template<
typename Gr
idType,
typename MaskT>
inline
212 typename GridType::Ptr
213 laplacian(
const GridType& grid,
const MaskT mask,
bool threaded =
true)
215 return laplacian<GridType, MaskT, util::NullInterrupter>(grid, mask, threaded, NULL);
223 template<
typename Gr
idType,
typename InterruptT>
inline
224 typename GridType::Ptr
225 meanCurvature(
const GridType& grid,
bool threaded, InterruptT* interrupt);
227 template<
typename Gr
idType,
typename MaskT,
typename InterruptT>
inline
228 typename GridType::Ptr
229 meanCurvature(
const GridType& grid,
const MaskT& mask,
bool threaded, InterruptT* interrupt);
231 template<
typename Gr
idType>
inline
232 typename GridType::Ptr
235 return meanCurvature<GridType, util::NullInterrupter>(grid, threaded, NULL);
238 template<
typename Gr
idType,
typename MaskT>
inline
239 typename GridType::Ptr
240 meanCurvature(
const GridType& grid,
const MaskT& mask,
bool threaded =
true)
242 return meanCurvature<GridType, MaskT, util::NullInterrupter>(grid, mask, threaded, NULL);
251 template<
typename Gr
idType,
typename InterruptT>
inline
252 typename VectorToScalarConverter<GridType>::Type::Ptr
253 magnitude(
const GridType& grid,
bool threaded, InterruptT* interrupt);
255 template<
typename Gr
idType,
typename MaskT,
typename InterruptT>
inline
256 typename VectorToScalarConverter<GridType>::Type::Ptr
257 magnitude(
const GridType& grid,
const MaskT& mask,
bool threaded, InterruptT* interrupt);
259 template<
typename Gr
idType>
inline
260 typename VectorToScalarConverter<GridType>::Type::Ptr
263 return magnitude<GridType, util::NullInterrupter>(grid, threaded, NULL);
266 template<
typename Gr
idType,
typename MaskT>
inline
267 typename VectorToScalarConverter<GridType>::Type::Ptr
268 magnitude(
const GridType& grid,
const MaskT& mask,
bool threaded =
true)
270 return magnitude<GridType, MaskT, util::NullInterrupter>(grid, mask, threaded, NULL);
278 template<
typename Gr
idType,
typename InterruptT>
inline
279 typename GridType::Ptr
280 normalize(
const GridType& grid,
bool threaded, InterruptT* interrupt);
282 template<
typename Gr
idType,
typename MaskT,
typename InterruptT>
inline
283 typename GridType::Ptr
284 normalize(
const GridType& grid,
const MaskT& mask,
bool threaded, InterruptT* interrupt);
286 template<
typename Gr
idType>
inline
287 typename GridType::Ptr
290 return normalize<GridType, util::NullInterrupter>(grid, threaded, NULL);
293 template<
typename Gr
idType,
typename MaskT>
inline
294 typename GridType::Ptr
295 normalize(
const GridType& grid,
const MaskT& mask,
bool threaded =
true)
297 return normalize<GridType, MaskT, util::NullInterrupter>(grid, mask, threaded, NULL);
314 template<
typename InGr
idT,
typename MaskGr
idType,
typename OutGr
idT,
typename MapT,
typename OperatorT,
typename InterruptT = util::NullInterrupter>
318 typedef typename OutGridT::TreeType OutTreeT;
319 typedef typename OutTreeT::LeafNodeType OutLeafT;
322 GridOperator(
const InGridT& grid,
const MaskGridType* mask,
const MapT& map, InterruptT* interrupt = NULL):
327 virtual ~GridOperator() {}
328 typename OutGridT::Ptr process(
bool threaded =
true)
333 typename InGridT::TreeType tmp(
mAcc.tree().background());
334 typename OutGridT::ValueType backg = OperatorT::result(
mMap, tmp, math::Coord(0));
337 typename OutTreeT::Ptr tree(
new OutTreeT(
mAcc.tree(), backg, TopologyCopy()));
341 typename OutGridT::Ptr result(
new OutGridT(tree));
345 result->topologyIntersection(*
mMask);
349 result->setTransform(math::Transform::Ptr(
new math::Transform(
mMap.copy() )));
351 LeafManagerT leafManager(*tree);
354 tbb::parallel_for(leafManager.leafRange(), *
this);
356 (*this)(leafManager.leafRange());
369 void operator()(
const typename LeafManagerT::LeafRange& range)
const
373 for (
typename LeafManagerT::LeafRange::Iterator leaf=range.begin(); leaf; ++leaf) {
374 for (
typename OutLeafT::ValueOnIter value=leaf->beginValueOn(); value; ++value) {
375 value.setValue(OperatorT::result(
mMap,
mAcc, value.getCoord()));
382 typedef typename InGridT::ConstAccessor AccessorT;
396 template<
typename InGr
idT,
typename MaskGr
idType = BoolGr
id,
typename InterruptT = util::NullInterrupter>
409 Cpt(
const InGridType& grid,
const MaskGridType& mask, InterruptT* interrupt = NULL):
415 typename OutGridType::Ptr
process(
bool threaded =
true,
bool useWorldTransform =
true)
417 Functor functor(mInputGrid,
mMask, threaded, useWorldTransform,
mInterrupt);
419 return functor.mOutputGrid;
424 template<
typename MapT,
typename AccT>
425 static typename OutGridType::ValueType
426 result(
const MapT& map,
const AccT& acc,
const Coord& xyz)
433 template<
typename MapT,
typename AccT>
434 static typename OutGridType::ValueType
435 result(
const MapT& map,
const AccT& acc,
const Coord& xyz)
442 Functor(
const InGridType& grid,
const MaskGridType* mask,
bool threaded,
bool worldspace, InterruptT* interrupt):
443 mThreaded(threaded), mWorldSpace(worldspace), mInputGrid(grid),
mInterrupt(interrupt),
mMask(mask){}
444 template<
typename MapT>
445 void operator()(
const MapT& map)
448 GridOperator<InGridType, MaskGridType, OutGridType, MapT, WsOpT, InterruptT> op(mInputGrid,
mMask, map,
mInterrupt);
449 mOutputGrid = op.process(mThreaded);
451 GridOperator<InGridType, MaskGridType, OutGridType, MapT, IsOpT, InterruptT> op(mInputGrid,
mMask, map,
mInterrupt);
452 mOutputGrid = op.process(mThreaded);
455 const bool mThreaded;
456 const bool mWorldSpace;
457 const InGridType& mInputGrid;
458 typename OutGridType::Ptr mOutputGrid;
460 const MaskGridType*
mMask;
462 const InGridType& mInputGrid;
464 const MaskGridType*
mMask;
472 template<
typename Gr
idT,
typename MaskGr
idType = BoolGr
id,
typename InterruptT = util::NullInterrupter>
478 Curl(
const GridT& grid, InterruptT* interrupt = NULL):
482 Curl(
const GridT& grid,
const MaskGridType& mask, InterruptT* interrupt = NULL):
487 typename GridT::Ptr
process(
bool threaded =
true)
491 return functor.mOutputGrid;
497 Functor(
const GridT& grid,
const MaskGridType* mask,
498 bool threaded, InterruptT* interrupt):
499 mThreaded(threaded), mInputGrid(grid),
mInterrupt(interrupt),
mMask(mask){}
500 template<
typename MapT>
501 void operator()(
const MapT& map)
504 GridOperator<GridT, MaskGridType, GridT, MapT, OpT, InterruptT> op(mInputGrid,
mMask, map,
mInterrupt);
505 mOutputGrid = op.process(mThreaded);
507 const bool mThreaded;
508 const GridT& mInputGrid;
509 typename GridT::Ptr mOutputGrid;
511 const MaskGridType*
mMask;
514 const GridT& mInputGrid;
516 const MaskGridType*
mMask;
524 template<
typename InGr
idT,
typename MaskGr
idType = BoolGr
id,
typename InterruptT = util::NullInterrupter>
531 Divergence(
const InGridT& grid, InterruptT* interrupt = NULL):
536 Divergence(
const InGridT& grid,
const MaskGridType& mask, InterruptT* interrupt = NULL):
540 typename OutGridType::Ptr
process(
bool threaded =
true)
545 return functor.mOutputGrid;
550 return functor.mOutputGrid;
555 template<math::DScheme DiffScheme>
558 Functor(
const InGridT& grid,
const MaskGridType* mask,
bool threaded, InterruptT* interrupt):
559 mThreaded(threaded), mInputGrid(grid),
mInterrupt(interrupt),
mMask(mask) {}
560 template<
typename MapT>
564 GridOperator<InGridType, MaskGridType, OutGridType, MapT, OpT, InterruptT> op(mInputGrid,
mMask, map,
mInterrupt);
565 mOutputGrid = op.process(mThreaded);
584 template<
typename InGr
idT,
typename MaskGr
idType = BoolGr
id,
typename InterruptT = util::NullInterrupter>
591 Gradient(
const InGridT& grid, InterruptT* interrupt = NULL):
595 Gradient(
const InGridT& grid,
const MaskGridType& mask, InterruptT* interrupt = NULL):
599 typename OutGridType::Ptr
process(
bool threaded =
true)
603 return functor.mOutputGrid;
609 Functor(
const InGridT& grid,
const MaskGridType* mask,
bool threaded, InterruptT* interrupt):
610 mThreaded(threaded), mInputGrid(grid),
mInterrupt(interrupt),
mMask(mask) {}
611 template<
typename MapT>
615 GridOperator<InGridType, MaskGridType, OutGridType, MapT, OpT, InterruptT> op(mInputGrid,
mMask, map,
mInterrupt);
616 mOutputGrid = op.process(mThreaded);
635 template<
typename Gr
idT,
typename MaskGr
idType = BoolGr
id,
typename InterruptT = util::NullInterrupter>
641 Laplacian(
const GridT& grid, InterruptT* interrupt = NULL):
646 Laplacian(
const GridT& grid,
const MaskGridType& mask, InterruptT* interrupt = NULL):
650 typename GridT::Ptr
process(
bool threaded =
true)
654 return functor.mOutputGrid;
660 Functor(
const GridT& grid,
const MaskGridType* mask,
bool threaded, InterruptT* interrupt):
661 mThreaded(threaded), mInputGrid(grid),
mInterrupt(interrupt),
mMask(mask) {}
662 template<
typename MapT>
666 GridOperator<GridT, MaskGridType, GridT, MapT, OpT, InterruptT> op(mInputGrid,
mMask, map);
667 mOutputGrid = op.process(mThreaded);
685 template<
typename Gr
idT,
typename MaskGr
idType = BoolGr
id,
typename InterruptT = util::NullInterrupter>
695 MeanCurvature(
const GridT& grid,
const MaskGridType& mask, InterruptT* interrupt = NULL):
700 typename GridT::Ptr
process(
bool threaded =
true)
704 return functor.mOutputGrid;
710 Functor(
const GridT& grid,
const MaskGridType* mask,
bool threaded, InterruptT* interrupt):
711 mThreaded(threaded), mInputGrid(grid),
mInterrupt(interrupt),
mMask(mask) {}
712 template<
typename MapT>
716 GridOperator<GridT, MaskGridType, GridT, MapT, OpT, InterruptT> op(mInputGrid,
mMask, map);
717 mOutputGrid = op.process(mThreaded);
735 template<
typename InGr
idT,
typename MaskGr
idType = BoolGr
id,
typename InterruptT = util::NullInterrupter>
749 typename OutGridType::Ptr
process(
bool threaded =
true)
753 return functor.mOutputGrid;
759 template<
typename MapT,
typename AccT>
760 static typename OutGridType::ValueType
761 result(
const MapT&,
const AccT& acc,
const Coord& xyz) {
return acc.getValue(xyz).length();}
765 Functor(
const InGridT& grid,
const MaskGridType* mask,
bool threaded, InterruptT* interrupt):
766 mThreaded(threaded), mInputGrid(grid),
mInterrupt(interrupt),
mMask(mask) {}
767 template<
typename MapT>
770 GridOperator<InGridType, MaskGridType, OutGridType, MapT, OpT, InterruptT> op(mInputGrid,
mMask, map);
771 mOutputGrid = op.process(mThreaded);
789 template<
typename Gr
idT,
typename MaskGr
idType = BoolGr
id,
typename InterruptT = util::NullInterrupter>
795 Normalize(
const GridT& grid, InterruptT* interrupt = NULL):
799 Normalize(
const GridT& grid,
const MaskGridType& mask, InterruptT* interrupt = NULL):
804 typename GridT::Ptr
process(
bool threaded =
true)
808 return functor.mOutputGrid;
814 template<
typename MapT,
typename AccT>
815 static typename OutGridType::ValueType
816 result(
const MapT&,
const AccT& acc,
const Coord& xyz)
818 typename OutGridType::ValueType vec = acc.getValue(xyz);
819 if ( !vec.normalize() ) vec.setZero();
825 Functor(
const GridT& grid,
const MaskGridType* mask,
bool threaded, InterruptT* interrupt):
826 mThreaded(threaded), mInputGrid(grid),
mInterrupt(interrupt),
mMask(mask) {}
827 template<
typename MapT>
830 GridOperator<GridT, MaskGridType, GridT, MapT, OpT, InterruptT> op(mInputGrid,
mMask,map);
831 mOutputGrid = op.process(mThreaded);
849 template<
typename Gr
idType,
typename InterruptT>
inline
851 cpt(
const GridType& grid,
bool threaded, InterruptT* interrupt)
857 template<
typename Gr
idType,
typename MaskT,
typename InterruptT>
inline
858 typename ScalarToVectorConverter<GridType>::Type::Ptr
859 cpt(
const GridType& grid,
const MaskT& mask,
bool threaded, InterruptT* interrupt)
865 template<
typename Gr
idType,
typename InterruptT>
inline
866 typename GridType::Ptr
867 curl(
const GridType& grid,
bool threaded, InterruptT* interrupt)
874 template<
typename Gr
idType,
typename MaskT,
typename InterruptT>
inline
875 typename GridType::Ptr
876 curl(
const GridType& grid,
const MaskT& mask,
bool threaded, InterruptT* interrupt)
882 template<
typename Gr
idType,
typename InterruptT>
inline
883 typename VectorToScalarConverter<GridType>::Type::Ptr
884 divergence(
const GridType& grid,
bool threaded, InterruptT* interrupt)
890 template<
typename Gr
idType,
typename MaskT,
typename InterruptT>
inline
891 typename VectorToScalarConverter<GridType>::Type::Ptr
892 divergence(
const GridType& grid,
const MaskT& mask,
bool threaded, InterruptT* interrupt)
898 template<
typename Gr
idType,
typename InterruptT>
inline
899 typename ScalarToVectorConverter<GridType>::Type::Ptr
900 gradient(
const GridType& grid,
bool threaded, InterruptT* interrupt)
906 template<
typename Gr
idType,
typename MaskT,
typename InterruptT>
inline
907 typename ScalarToVectorConverter<GridType>::Type::Ptr
908 gradient(
const GridType& grid,
const MaskT& mask,
bool threaded, InterruptT* interrupt)
914 template<
typename Gr
idType,
typename InterruptT>
inline
915 typename GridType::Ptr
916 laplacian(
const GridType& grid,
bool threaded, InterruptT* interrupt)
922 template<
typename Gr
idType,
typename MaskT,
typename InterruptT>
inline
923 typename GridType::Ptr
924 laplacian(
const GridType& grid,
const MaskT& mask,
bool threaded, InterruptT* interrupt)
930 template<
typename Gr
idType,
typename InterruptT>
inline
931 typename GridType::Ptr
938 template<
typename Gr
idType,
typename MaskT,
typename InterruptT>
inline
939 typename GridType::Ptr
940 meanCurvature(
const GridType& grid,
const MaskT& mask,
bool threaded, InterruptT* interrupt)
946 template<
typename Gr
idType,
typename InterruptT>
inline
947 typename VectorToScalarConverter<GridType>::Type::Ptr
948 magnitude(
const GridType& grid,
bool threaded, InterruptT* interrupt)
954 template<
typename Gr
idType,
typename MaskT,
typename InterruptT>
inline
955 typename VectorToScalarConverter<GridType>::Type::Ptr
956 magnitude(
const GridType& grid,
const MaskT& mask,
bool threaded, InterruptT* interrupt)
962 template<
typename Gr
idType,
typename InterruptT>
inline
963 typename GridType::Ptr
964 normalize(
const GridType& grid,
bool threaded, InterruptT* interrupt)
970 template<
typename Gr
idType,
typename MaskT,
typename InterruptT>
inline
971 typename GridType::Ptr
972 normalize(
const GridType& grid,
const MaskT& mask,
bool threaded, InterruptT* interrupt)
982 #endif // OPENVDB_TOOLS_GRID_OPERATORS_HAS_BEEN_INCLUDED
const MapT & mMap
Definition: GridOperators.h:384
Compute the mean curvature.
Definition: Operators.h:1738
AccessorT mAcc
Definition: GridOperators.h:383
bool processTypedMap(TransformType &transform, OpType &op)
Utility function that, given a generic map pointer, calls a functor on the fully-resoved map...
Definition: Transform.h:253
Compute the Laplacian at a given location in a grid using finite differencing of various orders...
Definition: Operators.h:1409
Compute the divergence of a vector-valued grid using differencing of various orders, the result defined with respect to the range-space of the map.
Definition: Operators.h:919
#define OPENVDB_VERSION_NAME
Definition: version.h:45
const MaskGridType * mMask
Definition: GridOperators.h:386
Compute the closest-point transform to a level set.
Definition: Operators.h:1696
InterruptT * mInterrupt
Definition: GridOperators.h:385
Compute the closest-point transform to a level set.
Definition: Operators.h:1643
Compute the curl of a vector-valued grid using differencing of various orders in the space defined by...
Definition: Operators.h:1264
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:67
This class manages a linear array of pointers to a given tree's leaf nodes, as well as optional auxil...
Definition: LeafManager.h:109
Center difference gradient operators, defined with respect to the range-space of the map...
Definition: Operators.h:609
bool wasInterrupted(T *i, int percent=-1)
Definition: NullInterrupter.h:76