OpenVDB  3.2.0
GridOperators.h
Go to the documentation of this file.
1 //
3 // Copyright (c) 2012-2016 DreamWorks Animation LLC
4 //
5 // All rights reserved. This software is distributed under the
6 // Mozilla Public License 2.0 ( http://www.mozilla.org/MPL/2.0/ )
7 //
8 // Redistributions of source code must retain the above copyright
9 // and license notice and the following restrictions and disclaimer.
10 //
11 // * Neither the name of DreamWorks Animation nor the names of
12 // its contributors may be used to endorse or promote products derived
13 // from this software without specific prior written permission.
14 //
15 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY INDIRECT, INCIDENTAL,
20 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 // IN NO EVENT SHALL THE COPYRIGHT HOLDERS' AND CONTRIBUTORS' AGGREGATE
27 // LIABILITY FOR ALL CLAIMS REGARDLESS OF THEIR BASIS EXCEED US$250.00.
28 //
30 //
35 
36 #ifndef OPENVDB_TOOLS_GRID_OPERATORS_HAS_BEEN_INCLUDED
37 #define OPENVDB_TOOLS_GRID_OPERATORS_HAS_BEEN_INCLUDED
38 
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>
45 
46 
47 namespace openvdb {
49 namespace OPENVDB_VERSION_NAME {
50 namespace tools {
51 
56 template<typename VectorGridType> struct VectorToScalarConverter {
57  typedef typename VectorGridType::ValueType::value_type VecComponentValueT;
58  typedef typename VectorGridType::template ValueConverter<VecComponentValueT>::Type Type;
59 };
60 
65 template<typename ScalarGridType> struct ScalarToVectorConverter {
67  typedef typename ScalarGridType::template ValueConverter<VectorValueT>::Type Type;
68 };
69 
70 
81 template<typename GridType, typename InterruptT> inline
83 cpt(const GridType& grid, bool threaded, InterruptT* interrupt);
84 
85 template<typename GridType, typename MaskT, typename InterruptT> inline
87 cpt(const GridType& grid, const MaskT& mask, bool threaded, InterruptT* interrupt);
88 
89 template<typename GridType> inline
91 cpt(const GridType& grid, bool threaded = true)
92 {
93  return cpt<GridType, util::NullInterrupter>(grid, threaded, NULL);
94 }
95 
96 template<typename GridType, typename MaskT> inline
98 cpt(const GridType& grid, const MaskT& mask, bool threaded = true)
99 {
100  return cpt<GridType, MaskT, util::NullInterrupter>(grid, mask, threaded, NULL);
101 }
102 
103 
109 template<typename GridType, typename InterruptT> inline
110 typename GridType::Ptr
111 curl(const GridType& grid, bool threaded, InterruptT* interrupt);
112 
113 template<typename GridType, typename MaskT, typename InterruptT> inline
114 typename GridType::Ptr
115 curl(const GridType& grid, const MaskT& mask, bool threaded, InterruptT* interrupt);
116 
117 template<typename GridType> inline
118 typename GridType::Ptr
119 curl(const GridType& grid, bool threaded = true)
120 {
121  return curl<GridType, util::NullInterrupter>(grid, threaded, NULL);
122 }
123 
124 template<typename GridType, typename MaskT> inline
125 typename GridType::Ptr
126 curl(const GridType& grid, const MaskT& mask, bool threaded = true)
127 {
128  return curl<GridType, MaskT, util::NullInterrupter>(grid, mask, threaded, NULL);
129 }
130 
131 
138 template<typename GridType, typename InterruptT> inline
140 divergence(const GridType& grid, bool threaded, InterruptT* interrupt);
141 
142 template<typename GridType, typename MaskT, typename InterruptT> inline
144 divergence(const GridType& grid, const MaskT& mask, bool threaded, InterruptT* interrupt);
145 
146 template<typename GridType> inline
148 divergence(const GridType& grid, bool threaded = true)
149 {
150  return divergence<GridType, util::NullInterrupter>(grid, threaded, NULL);
151 }
152 
153 template<typename GridType, typename MaskT> inline
155 divergence(const GridType& grid, const MaskT& mask, bool threaded = true)
156 {
157  return divergence<GridType, MaskT, util::NullInterrupter>(grid, mask, threaded, NULL);
158 }
159 
160 
167 template<typename GridType, typename InterruptT> inline
169 gradient(const GridType& grid, bool threaded, InterruptT* interrupt);
170 
171 template<typename GridType, typename MaskT, typename InterruptT> inline
173 gradient(const GridType& grid, const MaskT& mask, bool threaded, InterruptT* interrupt);
174 
175 template<typename GridType> inline
177 gradient(const GridType& grid, bool threaded = true)
178 {
179  return gradient<GridType, util::NullInterrupter>(grid, threaded, NULL);
180 }
181 
182 template<typename GridType, typename MaskT> inline
184 gradient(const GridType& grid, const MaskT& mask, bool threaded = true)
185 {
186  return gradient<GridType, MaskT, util::NullInterrupter>(grid, mask, threaded, NULL);
187 }
188 
189 
195 template<typename GridType, typename InterruptT> inline
196 typename GridType::Ptr
197 laplacian(const GridType& grid, bool threaded, InterruptT* interrupt);
198 
199 template<typename GridType, typename MaskT, typename InterruptT> inline
200 typename GridType::Ptr
201 laplacian(const GridType& grid, const MaskT& mask, bool threaded, InterruptT* interrupt);
202 
203 template<typename GridType> inline
204 typename GridType::Ptr
205 laplacian(const GridType& grid, bool threaded = true)
206 {
207  return laplacian<GridType, util::NullInterrupter>(grid, threaded, NULL);
208 }
209 
210 template<typename GridType, typename MaskT> inline
211 typename GridType::Ptr
212 laplacian(const GridType& grid, const MaskT mask, bool threaded = true)
213 {
214  return laplacian<GridType, MaskT, util::NullInterrupter>(grid, mask, threaded, NULL);
215 }
216 
217 
223 template<typename GridType, typename InterruptT> inline
224 typename GridType::Ptr
225 meanCurvature(const GridType& grid, bool threaded, InterruptT* interrupt);
226 
227 template<typename GridType, typename MaskT, typename InterruptT> inline
228 typename GridType::Ptr
229 meanCurvature(const GridType& grid, const MaskT& mask, bool threaded, InterruptT* interrupt);
230 
231 template<typename GridType> inline
232 typename GridType::Ptr
233 meanCurvature(const GridType& grid, bool threaded = true)
234 {
235  return meanCurvature<GridType, util::NullInterrupter>(grid, threaded, NULL);
236 }
237 
238 template<typename GridType, typename MaskT> inline
239 typename GridType::Ptr
240 meanCurvature(const GridType& grid, const MaskT& mask, bool threaded = true)
241 {
242  return meanCurvature<GridType, MaskT, util::NullInterrupter>(grid, mask, threaded, NULL);
243 }
244 
245 
252 template<typename GridType, typename InterruptT> inline
254 magnitude(const GridType& grid, bool threaded, InterruptT* interrupt);
255 
256 template<typename GridType, typename MaskT, typename InterruptT> inline
258 magnitude(const GridType& grid, const MaskT& mask, bool threaded, InterruptT* interrupt);
259 
260 template<typename GridType> inline
262 magnitude(const GridType& grid, bool threaded = true)
263 {
264  return magnitude<GridType, util::NullInterrupter>(grid, threaded, NULL);
265 }
266 
267 template<typename GridType, typename MaskT> inline
269 magnitude(const GridType& grid, const MaskT& mask, bool threaded = true)
270 {
271  return magnitude<GridType, MaskT, util::NullInterrupter>(grid, mask, threaded, NULL);
272 }
273 
274 
280 template<typename GridType, typename InterruptT> inline
281 typename GridType::Ptr
282 normalize(const GridType& grid, bool threaded, InterruptT* interrupt);
283 
284 template<typename GridType, typename MaskT, typename InterruptT> inline
285 typename GridType::Ptr
286 normalize(const GridType& grid, const MaskT& mask, bool threaded, InterruptT* interrupt);
287 
288 template<typename GridType> inline
289 typename GridType::Ptr
290 normalize(const GridType& grid, bool threaded = true)
291 {
292  return normalize<GridType, util::NullInterrupter>(grid, threaded, NULL);
293 }
294 
295 template<typename GridType, typename MaskT> inline
296 typename GridType::Ptr
297 normalize(const GridType& grid, const MaskT& mask, bool threaded = true)
298 {
299  return normalize<GridType, MaskT, util::NullInterrupter>(grid, mask, threaded, NULL);
300 }
301 
302 
304 
305 
306 namespace gridop {
307 
311 template<typename GridType>
312 struct ToMaskGrid {
314 };
315 
316 
326 template<
327  typename InGridT,
328  typename MaskGridType,
329  typename OutGridT,
330  typename MapT,
331  typename OperatorT,
332  typename InterruptT = util::NullInterrupter>
334 {
335 public:
336  typedef typename OutGridT::TreeType OutTreeT;
337  typedef typename OutTreeT::LeafNodeType OutLeafT;
339 
340  GridOperator(const InGridT& grid, const MaskGridType* mask, const MapT& map,
341  InterruptT* interrupt = NULL):
342  mAcc(grid.getConstAccessor()), mMap(map), mInterrupt(interrupt), mMask(mask)
343  {
344  }
345 
346  virtual ~GridOperator() {}
347  typename OutGridT::Ptr process(bool threaded = true)
348  {
349  if (mInterrupt) mInterrupt->start("Processing grid");
350 
351  // Derive background value of the output grid
352  typename InGridT::TreeType tmp(mAcc.tree().background());
353  typename OutGridT::ValueType backg = OperatorT::result(mMap, tmp, math::Coord(0));
354 
355  // output tree = topology copy of input tree!
356  typename OutTreeT::Ptr tree(new OutTreeT(mAcc.tree(), backg, TopologyCopy()));
357 
358 
359  // create grid with output tree and unit transform
360  typename OutGridT::Ptr result(new OutGridT(tree));
361 
362  // Modify the solution area if a mask was supplied.
363  if (mMask) {
364  result->topologyIntersection(*mMask);
365  }
366 
367  // transform of output grid = transform of input grid
368  result->setTransform(math::Transform::Ptr(new math::Transform( mMap.copy() )));
369 
370  LeafManagerT leafManager(*tree);
371 
372  if (threaded) {
373  tbb::parallel_for(leafManager.leafRange(), *this);
374  } else {
375  (*this)(leafManager.leafRange());
376  }
377 
378  if (mInterrupt) mInterrupt->end();
379  return result;
380  }
381 
388  void operator()(const typename LeafManagerT::LeafRange& range) const
389  {
390  if (util::wasInterrupted(mInterrupt)) tbb::task::self().cancel_group_execution();
391 
392  for (typename LeafManagerT::LeafRange::Iterator leaf=range.begin(); leaf; ++leaf) {
393  for (typename OutLeafT::ValueOnIter value=leaf->beginValueOn(); value; ++value) {
394  value.setValue(OperatorT::result(mMap, mAcc, value.getCoord()));
395  }
396  }
397  }
398 
399 protected:
400  typedef typename InGridT::ConstAccessor AccessorT;
401  mutable AccessorT mAcc;
402  const MapT& mMap;
403  InterruptT* mInterrupt;
404  const MaskGridType* mMask;
405 }; // end of GridOperator class
406 
407 } // namespace gridop
408 
409 
411 
412 
414 template<
415  typename InGridT,
416  typename MaskGridType = typename gridop::ToMaskGrid<InGridT>::Type,
417  typename InterruptT = util::NullInterrupter>
418 class Cpt
419 {
420 public:
421  typedef InGridT InGridType;
423 
424  Cpt(const InGridType& grid, InterruptT* interrupt = NULL):
425  mInputGrid(grid), mInterrupt(interrupt), mMask(NULL)
426  {
427  }
428 
429  Cpt(const InGridType& grid, const MaskGridType& mask, InterruptT* interrupt = NULL):
430  mInputGrid(grid), mInterrupt(interrupt), mMask(&mask)
431  {
432  }
433 
434  typename OutGridType::Ptr process(bool threaded = true, bool useWorldTransform = true)
435  {
436  Functor functor(mInputGrid, mMask, threaded, useWorldTransform, mInterrupt);
437  processTypedMap(mInputGrid.transform(), functor);
438  if (functor.mOutputGrid) functor.mOutputGrid->setVectorType(VEC_CONTRAVARIANT_ABSOLUTE);
439  return functor.mOutputGrid;
440  }
441 
442 private:
443  struct IsOpT
444  {
445  template<typename MapT, typename AccT>
446  static typename OutGridType::ValueType
447  result(const MapT& map, const AccT& acc, const Coord& xyz)
448  {
449  return math::CPT<MapT, math::CD_2ND>::result(map, acc, xyz);
450  }
451  };
452  struct WsOpT
453  {
454  template<typename MapT, typename AccT>
455  static typename OutGridType::ValueType
456  result(const MapT& map, const AccT& acc, const Coord& xyz)
457  {
458  return math::CPT_RANGE<MapT, math::CD_2ND>::result(map, acc, xyz);
459  }
460  };
461  struct Functor
462  {
463  Functor(const InGridType& grid, const MaskGridType* mask,
464  bool threaded, bool worldspace, InterruptT* interrupt)
465  : mThreaded(threaded)
466  , mWorldSpace(worldspace)
467  , mInputGrid(grid)
468  , mInterrupt(interrupt)
469  , mMask(mask)
470  {}
471 
472  template<typename MapT>
473  void operator()(const MapT& map)
474  {
475  if (mWorldSpace) {
477  op(mInputGrid, mMask, map, mInterrupt);
478  mOutputGrid = op.process(mThreaded); // cache the result
479  } else {
481  op(mInputGrid, mMask, map, mInterrupt);
482  mOutputGrid = op.process(mThreaded); // cache the result
483  }
484  }
485  const bool mThreaded;
486  const bool mWorldSpace;
487  const InGridType& mInputGrid;
488  typename OutGridType::Ptr mOutputGrid;
489  InterruptT* mInterrupt;
490  const MaskGridType* mMask;
491  };
492  const InGridType& mInputGrid;
493  InterruptT* mInterrupt;
494  const MaskGridType* mMask;
495 }; // end of Cpt class
496 
497 
499 
500 
502 template<
503  typename GridT,
504  typename MaskGridType = typename gridop::ToMaskGrid<GridT>::Type,
505  typename InterruptT = util::NullInterrupter>
506 class Curl
507 {
508 public:
509  typedef GridT InGridType;
510  typedef GridT OutGridType;
511 
512  Curl(const GridT& grid, InterruptT* interrupt = NULL):
513  mInputGrid(grid), mInterrupt(interrupt), mMask(NULL)
514  {
515  }
516 
517  Curl(const GridT& grid, const MaskGridType& mask, InterruptT* interrupt = NULL):
518  mInputGrid(grid), mInterrupt(interrupt), mMask(&mask)
519  {
520  }
521 
522  typename GridT::Ptr process(bool threaded = true)
523  {
524  Functor functor(mInputGrid, mMask, threaded, mInterrupt);
525  processTypedMap(mInputGrid.transform(), functor);
526  if (functor.mOutputGrid) functor.mOutputGrid->setVectorType(VEC_COVARIANT);
527  return functor.mOutputGrid;
528  }
529 
530 private:
531  struct Functor
532  {
533  Functor(const GridT& grid, const MaskGridType* mask,
534  bool threaded, InterruptT* interrupt):
535  mThreaded(threaded), mInputGrid(grid), mInterrupt(interrupt), mMask(mask) {}
536 
537  template<typename MapT>
538  void operator()(const MapT& map)
539  {
540  typedef math::Curl<MapT, math::CD_2ND> OpT;
542  op(mInputGrid, mMask, map, mInterrupt);
543  mOutputGrid = op.process(mThreaded); // cache the result
544  }
545 
546  const bool mThreaded;
547  const GridT& mInputGrid;
548  typename GridT::Ptr mOutputGrid;
549  InterruptT* mInterrupt;
550  const MaskGridType* mMask;
551  }; // Private Functor
552 
553  const GridT& mInputGrid;
554  InterruptT* mInterrupt;
555  const MaskGridType* mMask;
556 }; // end of Curl class
557 
558 
560 
561 
563 template<
564  typename InGridT,
565  typename MaskGridType = typename gridop::ToMaskGrid<InGridT>::Type,
566  typename InterruptT = util::NullInterrupter>
568 {
569 public:
570  typedef InGridT InGridType;
572 
573  Divergence(const InGridT& grid, InterruptT* interrupt = NULL):
574  mInputGrid(grid), mInterrupt(interrupt), mMask(NULL)
575  {
576  }
577 
578  Divergence(const InGridT& grid, const MaskGridType& mask, InterruptT* interrupt = NULL):
579  mInputGrid(grid), mInterrupt(interrupt), mMask(&mask)
580  {
581  }
582 
583  typename OutGridType::Ptr process(bool threaded = true)
584  {
585  if (mInputGrid.getGridClass() == GRID_STAGGERED) {
586  Functor<math::FD_1ST> functor(mInputGrid, mMask, threaded, mInterrupt);
587  processTypedMap(mInputGrid.transform(), functor);
588  return functor.mOutputGrid;
589  } else {
590  Functor<math::CD_2ND> functor(mInputGrid, mMask, threaded, mInterrupt);
591  processTypedMap(mInputGrid.transform(), functor);
592  return functor.mOutputGrid;
593  }
594  }
595 
596 protected:
597  template<math::DScheme DiffScheme>
598  struct Functor
599  {
600  Functor(const InGridT& grid, const MaskGridType* mask,
601  bool threaded, InterruptT* interrupt):
602  mThreaded(threaded), mInputGrid(grid), mInterrupt(interrupt), mMask(mask) {}
603 
604  template<typename MapT>
605  void operator()(const MapT& map)
606  {
609  op(mInputGrid, mMask, map, mInterrupt);
610  mOutputGrid = op.process(mThreaded); // cache the result
611  }
612 
613  const bool mThreaded;
614  const InGridType& mInputGrid;
615  typename OutGridType::Ptr mOutputGrid;
616  InterruptT* mInterrupt;
617  const MaskGridType* mMask;
618  }; // Private Functor
619 
620  const InGridType& mInputGrid;
621  InterruptT* mInterrupt;
622  const MaskGridType* mMask;
623 }; // end of Divergence class
624 
625 
627 
628 
630 template<
631  typename InGridT,
632  typename MaskGridType = typename gridop::ToMaskGrid<InGridT>::Type,
633  typename InterruptT = util::NullInterrupter>
634 class Gradient
635 {
636 public:
637  typedef InGridT InGridType;
639 
640  Gradient(const InGridT& grid, InterruptT* interrupt = NULL):
641  mInputGrid(grid), mInterrupt(interrupt), mMask(NULL)
642  {
643  }
644 
645  Gradient(const InGridT& grid, const MaskGridType& mask, InterruptT* interrupt = NULL):
646  mInputGrid(grid), mInterrupt(interrupt), mMask(&mask)
647  {
648  }
649 
650  typename OutGridType::Ptr process(bool threaded = true)
651  {
652  Functor functor(mInputGrid, mMask, threaded, mInterrupt);
653  processTypedMap(mInputGrid.transform(), functor);
654  if (functor.mOutputGrid) functor.mOutputGrid->setVectorType(VEC_COVARIANT);
655  return functor.mOutputGrid;
656  }
657 
658 protected:
659  struct Functor
660  {
661  Functor(const InGridT& grid, const MaskGridType* mask,
662  bool threaded, InterruptT* interrupt):
663  mThreaded(threaded), mInputGrid(grid), mInterrupt(interrupt), mMask(mask) {}
664 
665  template<typename MapT>
666  void operator()(const MapT& map)
667  {
670  op(mInputGrid, mMask, map, mInterrupt);
671  mOutputGrid = op.process(mThreaded); // cache the result
672  }
673 
674  const bool mThreaded;
675  const InGridT& mInputGrid;
676  typename OutGridType::Ptr mOutputGrid;
677  InterruptT* mInterrupt;
678  const MaskGridType* mMask;
679  }; // Private Functor
680 
681  const InGridT& mInputGrid;
682  InterruptT* mInterrupt;
683  const MaskGridType* mMask;
684 }; // end of Gradient class
685 
686 
688 
689 
690 template<
691  typename GridT,
692  typename MaskGridType = typename gridop::ToMaskGrid<GridT>::Type,
693  typename InterruptT = util::NullInterrupter>
695 {
696 public:
697  typedef GridT InGridType;
698  typedef GridT OutGridType;
699 
700  Laplacian(const GridT& grid, InterruptT* interrupt = NULL):
701  mInputGrid(grid), mInterrupt(interrupt), mMask(NULL)
702  {
703  }
704 
705  Laplacian(const GridT& grid, const MaskGridType& mask, InterruptT* interrupt = NULL):
706  mInputGrid(grid), mInterrupt(interrupt), mMask(&mask)
707  {
708  }
709 
710  typename GridT::Ptr process(bool threaded = true)
711  {
712  Functor functor(mInputGrid, mMask, threaded, mInterrupt);
713  processTypedMap(mInputGrid.transform(), functor);
714  if (functor.mOutputGrid) functor.mOutputGrid->setVectorType(VEC_COVARIANT);
715  return functor.mOutputGrid;
716  }
717 
718 protected:
719  struct Functor
720  {
721  Functor(const GridT& grid, const MaskGridType* mask, bool threaded, InterruptT* interrupt):
722  mThreaded(threaded), mInputGrid(grid), mInterrupt(interrupt), mMask(mask) {}
723 
724  template<typename MapT>
725  void operator()(const MapT& map)
726  {
729  op(mInputGrid, mMask, map);
730  mOutputGrid = op.process(mThreaded); // cache the result
731  }
732 
733  const bool mThreaded;
734  const GridT& mInputGrid;
735  typename GridT::Ptr mOutputGrid;
736  InterruptT* mInterrupt;
737  const MaskGridType* mMask;
738  }; // Private Functor
739 
740  const GridT& mInputGrid;
741  InterruptT* mInterrupt;
742  const MaskGridType* mMask;
743 }; // end of Laplacian class
744 
745 
747 
748 
749 template<
750  typename GridT,
751  typename MaskGridType = typename gridop::ToMaskGrid<GridT>::Type,
752  typename InterruptT = util::NullInterrupter>
754 {
755 public:
756  typedef GridT InGridType;
757  typedef GridT OutGridType;
758 
759  MeanCurvature(const GridT& grid, InterruptT* interrupt = NULL):
760  mInputGrid(grid), mInterrupt(interrupt), mMask(NULL)
761  {
762  }
763 
764  MeanCurvature(const GridT& grid, const MaskGridType& mask, InterruptT* interrupt = NULL):
765  mInputGrid(grid), mInterrupt(interrupt), mMask(&mask)
766  {
767  }
768 
769  typename GridT::Ptr process(bool threaded = true)
770  {
771  Functor functor(mInputGrid, mMask, threaded, mInterrupt);
772  processTypedMap(mInputGrid.transform(), functor);
773  if (functor.mOutputGrid) functor.mOutputGrid->setVectorType(VEC_COVARIANT);
774  return functor.mOutputGrid;
775  }
776 
777 protected:
778  struct Functor
779  {
780  Functor(const GridT& grid, const MaskGridType* mask, bool threaded, InterruptT* interrupt):
781  mThreaded(threaded), mInputGrid(grid), mInterrupt(interrupt), mMask(mask) {}
782 
783  template<typename MapT>
784  void operator()(const MapT& map)
785  {
788  op(mInputGrid, mMask, map);
789  mOutputGrid = op.process(mThreaded); // cache the result
790  }
791 
792  const bool mThreaded;
793  const GridT& mInputGrid;
794  typename GridT::Ptr mOutputGrid;
795  InterruptT* mInterrupt;
796  const MaskGridType* mMask;
797  }; // Private Functor
798 
799  const GridT& mInputGrid;
800  InterruptT* mInterrupt;
801  const MaskGridType* mMask;
802 }; // end of MeanCurvature class
803 
804 
806 
807 
808 template<
809  typename InGridT,
810  typename MaskGridType = typename gridop::ToMaskGrid<InGridT>::Type,
811  typename InterruptT = util::NullInterrupter>
813 {
814 public:
815  typedef InGridT InGridType;
817 
818  Magnitude(const InGridType& grid, InterruptT* interrupt = NULL):
819  mInputGrid(grid), mInterrupt(interrupt), mMask(NULL)
820  {
821  }
822 
823  Magnitude(const InGridType& grid, const MaskGridType& mask, InterruptT* interrupt = NULL):
824  mInputGrid(grid), mInterrupt(interrupt), mMask(&mask)
825  {
826  }
827 
828  typename OutGridType::Ptr process(bool threaded = true)
829  {
830  Functor functor(mInputGrid, mMask, threaded, mInterrupt);
831  processTypedMap(mInputGrid.transform(), functor);
832  return functor.mOutputGrid;
833  }
834 
835 protected:
836  struct OpT
837  {
838  template<typename MapT, typename AccT>
839  static typename OutGridType::ValueType
840  result(const MapT&, const AccT& acc, const Coord& xyz) { return acc.getValue(xyz).length();}
841  };
842  struct Functor
843  {
844  Functor(const InGridT& grid, const MaskGridType* mask,
845  bool threaded, InterruptT* interrupt):
846  mThreaded(threaded), mInputGrid(grid), mInterrupt(interrupt), mMask(mask) {}
847 
848  template<typename MapT>
849  void operator()(const MapT& map)
850  {
852  op(mInputGrid, mMask, map);
853  mOutputGrid = op.process(mThreaded); // cache the result
854  }
855 
856  const bool mThreaded;
857  const InGridType& mInputGrid;
858  typename OutGridType::Ptr mOutputGrid;
859  InterruptT* mInterrupt;
860  const MaskGridType* mMask;
861  }; // Private Functor
862 
863  const InGridType& mInputGrid;
864  InterruptT* mInterrupt;
865  const MaskGridType* mMask;
866 }; // end of Magnitude class
867 
868 
870 
871 
872 template<
873  typename GridT,
874  typename MaskGridType = typename gridop::ToMaskGrid<GridT>::Type,
875  typename InterruptT = util::NullInterrupter>
877 {
878 public:
879  typedef GridT InGridType;
880  typedef GridT OutGridType;
881 
882  Normalize(const GridT& grid, InterruptT* interrupt = NULL):
883  mInputGrid(grid), mInterrupt(interrupt), mMask(NULL)
884  {
885  }
886 
887  Normalize(const GridT& grid, const MaskGridType& mask, InterruptT* interrupt = NULL):
888  mInputGrid(grid), mInterrupt(interrupt), mMask(&mask)
889  {
890  }
891 
892  typename GridT::Ptr process(bool threaded = true)
893  {
894  Functor functor(mInputGrid, mMask, threaded, mInterrupt);
895  processTypedMap(mInputGrid.transform(), functor);
896  if (typename GridT::Ptr outGrid = functor.mOutputGrid) {
897  const VecType vecType = mInputGrid.getVectorType();
898  if (vecType == VEC_COVARIANT) {
899  outGrid->setVectorType(VEC_COVARIANT_NORMALIZE);
900  } else {
901  outGrid->setVectorType(vecType);
902  }
903  }
904  return functor.mOutputGrid;
905  }
906 
907 protected:
908  struct OpT
909  {
910  template<typename MapT, typename AccT>
911  static typename OutGridType::ValueType
912  result(const MapT&, const AccT& acc, const Coord& xyz)
913  {
914  typename OutGridType::ValueType vec = acc.getValue(xyz);
915  if ( !vec.normalize() ) vec.setZero();
916  return vec;
917  }
918  };
919  struct Functor
920  {
921  Functor(const GridT& grid, const MaskGridType* mask, bool threaded, InterruptT* interrupt):
922  mThreaded(threaded), mInputGrid(grid), mInterrupt(interrupt), mMask(mask) {}
923 
924  template<typename MapT>
925  void operator()(const MapT& map)
926  {
928  op(mInputGrid, mMask,map);
929  mOutputGrid = op.process(mThreaded); // cache the result
930  }
931 
932  const bool mThreaded;
933  const GridT& mInputGrid;
934  typename GridT::Ptr mOutputGrid;
935  InterruptT* mInterrupt;
936  const MaskGridType* mMask;
937  }; // Private Functor
938 
939  const GridT& mInputGrid;
940  InterruptT* mInterrupt;
941  const MaskGridType* mMask;
942 }; // end of Normalize class
943 
944 
946 
947 
948 template<typename GridType, typename InterruptT> inline
950 cpt(const GridType& grid, bool threaded, InterruptT* interrupt)
951 {
952  Cpt<GridType, typename gridop::ToMaskGrid<GridType>::Type, InterruptT> op(grid, interrupt);
953  return op.process(threaded);
954 }
955 
956 template<typename GridType, typename MaskT, typename InterruptT> inline
958 cpt(const GridType& grid, const MaskT& mask, bool threaded, InterruptT* interrupt)
959 {
960  Cpt<GridType, MaskT, InterruptT> op(grid, mask, interrupt);
961  return op.process(threaded);
962 }
963 
964 template<typename GridType, typename InterruptT> inline
965 typename GridType::Ptr
966 curl(const GridType& grid, bool threaded, InterruptT* interrupt)
967 {
968  Curl<GridType, typename gridop::ToMaskGrid<GridType>::Type, InterruptT> op(grid, interrupt);
969  return op.process(threaded);
970 }
971 
972 template<typename GridType, typename MaskT, typename InterruptT> inline
973 typename GridType::Ptr
974 curl(const GridType& grid, const MaskT& mask, bool threaded, InterruptT* interrupt)
975 {
976  Curl<GridType, MaskT, InterruptT> op(grid, mask, interrupt);
977  return op.process(threaded);
978 }
979 
980 template<typename GridType, typename InterruptT> inline
982 divergence(const GridType& grid, bool threaded, InterruptT* interrupt)
983 {
985  op(grid, interrupt);
986  return op.process(threaded);
987 }
988 
989 template<typename GridType, typename MaskT, typename InterruptT> inline
991 divergence(const GridType& grid, const MaskT& mask, bool threaded, InterruptT* interrupt)
992 {
993  Divergence<GridType, MaskT, InterruptT> op(grid, mask, interrupt);
994  return op.process(threaded);
995 }
996 
997 template<typename GridType, typename InterruptT> inline
999 gradient(const GridType& grid, bool threaded, InterruptT* interrupt)
1000 {
1002  op(grid, interrupt);
1003  return op.process(threaded);
1004 }
1005 
1006 template<typename GridType, typename MaskT, typename InterruptT> inline
1008 gradient(const GridType& grid, const MaskT& mask, bool threaded, InterruptT* interrupt)
1009 {
1010  Gradient<GridType, MaskT, InterruptT> op(grid, mask, interrupt);
1011  return op.process(threaded);
1012 }
1013 
1014 template<typename GridType, typename InterruptT> inline
1015 typename GridType::Ptr
1016 laplacian(const GridType& grid, bool threaded, InterruptT* interrupt)
1017 {
1019  op(grid, interrupt);
1020  return op.process(threaded);
1021 }
1022 
1023 template<typename GridType, typename MaskT, typename InterruptT> inline
1024 typename GridType::Ptr
1025 laplacian(const GridType& grid, const MaskT& mask, bool threaded, InterruptT* interrupt)
1026 {
1027  Laplacian<GridType, MaskT, InterruptT> op(grid, mask, interrupt);
1028  return op.process(threaded);
1029 }
1030 
1031 template<typename GridType, typename InterruptT> inline
1032 typename GridType::Ptr
1033 meanCurvature(const GridType& grid, bool threaded, InterruptT* interrupt)
1034 {
1036  op(grid, interrupt);
1037  return op.process(threaded);
1038 }
1039 
1040 template<typename GridType, typename MaskT, typename InterruptT> inline
1041 typename GridType::Ptr
1042 meanCurvature(const GridType& grid, const MaskT& mask, bool threaded, InterruptT* interrupt)
1043 {
1044  MeanCurvature<GridType, MaskT, InterruptT> op(grid, mask, interrupt);
1045  return op.process(threaded);
1046 }
1047 
1048 template<typename GridType, typename InterruptT> inline
1050 magnitude(const GridType& grid, bool threaded, InterruptT* interrupt)
1051 {
1053  op(grid, interrupt);
1054  return op.process(threaded);
1055 }
1056 
1057 template<typename GridType, typename MaskT, typename InterruptT> inline
1059 magnitude(const GridType& grid, const MaskT& mask, bool threaded, InterruptT* interrupt)
1060 {
1061  Magnitude<GridType, MaskT, InterruptT> op(grid, mask, interrupt);
1062  return op.process(threaded);
1063 }
1064 
1065 template<typename GridType, typename InterruptT> inline
1066 typename GridType::Ptr
1067 normalize(const GridType& grid, bool threaded, InterruptT* interrupt)
1068 {
1070  op(grid, interrupt);
1071  return op.process(threaded);
1072 }
1073 
1074 template<typename GridType, typename MaskT, typename InterruptT> inline
1075 typename GridType::Ptr
1076 normalize(const GridType& grid, const MaskT& mask, bool threaded, InterruptT* interrupt)
1077 {
1078  Normalize<GridType, MaskT, InterruptT> op(grid, mask, interrupt);
1079  return op.process(threaded);
1080 }
1081 
1082 } // namespace tools
1083 } // namespace OPENVDB_VERSION_NAME
1084 } // namespace openvdb
1085 
1086 #endif // OPENVDB_TOOLS_GRID_OPERATORS_HAS_BEEN_INCLUDED
1087 
1088 // Copyright (c) 2012-2016 DreamWorks Animation LLC
1089 // All rights reserved. This software is distributed under the
1090 // Mozilla Public License 2.0 ( http://www.mozilla.org/MPL/2.0/ )
GridT::Ptr process(bool threaded=true)
Definition: GridOperators.h:710
Definition: GridOperators.h:836
const MaskGridType * mMask
Definition: GridOperators.h:860
Magnitude(const InGridType &grid, InterruptT *interrupt=NULL)
Definition: GridOperators.h:818
Functor(const GridT &grid, const MaskGridType *mask, bool threaded, InterruptT *interrupt)
Definition: GridOperators.h:780
ScalarToVectorConverter< InGridT >::Type OutGridType
Definition: GridOperators.h:422
VectorGridType::template ValueConverter< VecComponentValueT >::Type Type
Definition: GridOperators.h:58
const InGridType & mInputGrid
Definition: GridOperators.h:614
Normalize(const GridT &grid, InterruptT *interrupt=NULL)
Definition: GridOperators.h:882
InterruptT * mInterrupt
Definition: GridOperators.h:741
Grid< typename GridType::TreeType::template ValueConverter< ValueMask >::Type > Type
Definition: GridOperators.h:313
VectorToScalarConverter< GridType >::Type::Ptr divergence(const GridType &grid, const MaskT &mask, bool threaded=true)
Definition: GridOperators.h:155
Functor(const GridT &grid, const MaskGridType *mask, bool threaded, InterruptT *interrupt)
Definition: GridOperators.h:721
math::Vec3< typename ScalarGridType::ValueType > VectorValueT
Definition: GridOperators.h:66
MeanCurvature(const GridT &grid, InterruptT *interrupt=NULL)
Definition: GridOperators.h:759
OutTreeT::LeafNodeType OutLeafT
Definition: GridOperators.h:337
OutGridT::TreeType OutTreeT
Definition: GridOperators.h:336
LeafRange leafRange(size_t grainsize=1) const
Return a TBB-compatible LeafRange.
Definition: LeafManager.h:381
Dummy NOOP interrupter class defining interface.
Definition: NullInterrupter.h:52
Compute the curl of a vector-valued grid using differencing of various orders in the space defined by...
Definition: Operators.h:1277
GridType::Ptr laplacian(const GridType &grid, const MaskT mask, bool threaded=true)
Definition: GridOperators.h:212
Definition: Types.h:442
const InGridType & mInputGrid
Definition: GridOperators.h:620
InterruptT * mInterrupt
Definition: GridOperators.h:677
VectorToScalarConverter< InGridT >::Type OutGridType
Definition: GridOperators.h:571
Divergence(const InGridT &grid, const MaskGridType &mask, InterruptT *interrupt=NULL)
Definition: GridOperators.h:578
const InGridType & mInputGrid
Definition: GridOperators.h:863
GridT::Ptr process(bool threaded=true)
Definition: GridOperators.h:522
Definition: Mat.h:146
VecType
Definition: Types.h:241
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:268
ScalarToVectorConverter< InGridT >::Type OutGridType
Definition: GridOperators.h:638
Magnitude(const InGridType &grid, const MaskGridType &mask, InterruptT *interrupt=NULL)
Definition: GridOperators.h:823
Definition: GridOperators.h:812
Definition: GridOperators.h:908
const GridT & mInputGrid
Definition: GridOperators.h:799
OutGridType::Ptr mOutputGrid
Definition: GridOperators.h:858
OutGridType::Ptr process(bool threaded=true, bool useWorldTransform=true)
Definition: GridOperators.h:434
This class manages a linear array of pointers to a given tree&#39;s leaf nodes, as well as optional auxil...
Definition: LeafManager.h:115
tree::LeafManager< OutTreeT > LeafManagerT
Definition: GridOperators.h:338
Signed (x, y, z) 32-bit integer coordinates.
Definition: Coord.h:47
static OutGridType::ValueType result(const MapT &, const AccT &acc, const Coord &xyz)
Definition: GridOperators.h:912
const GridT & mInputGrid
Definition: GridOperators.h:933
Curl(const GridT &grid, const MaskGridType &mask, InterruptT *interrupt=NULL)
Definition: GridOperators.h:517
Definition: GridOperators.h:719
Functor(const GridT &grid, const MaskGridType *mask, bool threaded, InterruptT *interrupt)
Definition: GridOperators.h:921
MeanCurvature(const GridT &grid, const MaskGridType &mask, InterruptT *interrupt=NULL)
Definition: GridOperators.h:764
const bool mThreaded
Definition: GridOperators.h:733
InterruptT * mInterrupt
Definition: GridOperators.h:859
void operator()(const MapT &map)
Definition: GridOperators.h:925
GridT::Ptr process(bool threaded=true)
Definition: GridOperators.h:892
const MaskGridType * mMask
Definition: GridOperators.h:683
InterruptT * mInterrupt
Definition: GridOperators.h:940
const bool mThreaded
Definition: GridOperators.h:792
const GridT & mInputGrid
Definition: GridOperators.h:939
ScalarGridType::template ValueConverter< VectorValueT >::Type Type
Definition: GridOperators.h:67
OutGridType::Ptr mOutputGrid
Definition: GridOperators.h:615
InterruptT * mInterrupt
Definition: GridOperators.h:621
GridType::Ptr normalize(const GridType &grid, const MaskT &mask, bool threaded=true)
Definition: GridOperators.h:297
InterruptT * mInterrupt
Definition: GridOperators.h:403
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:952
GridT::Ptr process(bool threaded=true)
Definition: GridOperators.h:769
static OutGridType::ValueType result(const MapT &, const AccT &acc, const Coord &xyz)
Definition: GridOperators.h:840
ScalarToVectorConverter< GridType >::Type::Ptr gradient(const GridType &grid, const MaskT &mask, bool threaded=true)
Definition: GridOperators.h:184
GridT OutGridType
Definition: GridOperators.h:757
InGridT InGridType
Definition: GridOperators.h:815
VectorToScalarConverter<VectorGridType>::Type is the type of a grid having the same tree configuratio...
Definition: GridOperators.h:56
InterruptT * mInterrupt
Definition: GridOperators.h:736
InterruptT * mInterrupt
Definition: GridOperators.h:616
virtual ~GridOperator()
Definition: GridOperators.h:346
InGridT InGridType
Definition: GridOperators.h:570
void operator()(const MapT &map)
Definition: GridOperators.h:725
OutGridType::Ptr mOutputGrid
Definition: GridOperators.h:676
ScalarToVectorConverter< GridType >::Type::Ptr cpt(const GridType &grid, const MaskT &mask, bool threaded=true)
Definition: GridOperators.h:98
void operator()(const MapT &map)
Definition: GridOperators.h:605
#define OPENVDB_VERSION_NAME
Definition: version.h:43
Definition: GridOperators.h:753
const GridT & mInputGrid
Definition: GridOperators.h:740
Cpt(const InGridType &grid, InterruptT *interrupt=NULL)
Definition: GridOperators.h:424
InterruptT * mInterrupt
Definition: GridOperators.h:800
GridT InGridType
Definition: GridOperators.h:509
AccessorT mAcc
Definition: GridOperators.h:401
Definition: GridOperators.h:659
GridT InGridType
Definition: GridOperators.h:879
GridT OutGridType
Definition: GridOperators.h:698
const MaskGridType * mMask
Definition: GridOperators.h:936
Compute the Laplacian at a given location in a grid using finite differencing of various orders...
Definition: Operators.h:1422
VectorToScalarConverter< GridType >::Type::Ptr magnitude(const GridType &grid, const MaskT &mask, bool threaded=true)
Definition: GridOperators.h:269
InGridT InGridType
Definition: GridOperators.h:637
Calculate an axis-aligned bounding box in index space from a bounding sphere in world space...
Definition: Transform.h:66
const GridT & mInputGrid
Definition: GridOperators.h:734
InterruptT * mInterrupt
Definition: GridOperators.h:795
Definition: Exceptions.h:39
const MaskGridType * mMask
Definition: GridOperators.h:737
InterruptT * mInterrupt
Definition: GridOperators.h:935
Normalize(const GridT &grid, const MaskGridType &mask, InterruptT *interrupt=NULL)
Definition: GridOperators.h:887
Definition: GridOperators.h:919
Laplacian(const GridT &grid, const MaskGridType &mask, InterruptT *interrupt=NULL)
Definition: GridOperators.h:705
Compute the closest-point transform to a level set.
Definition: Operators.h:1709
Definition: Types.h:243
GridT InGridType
Definition: GridOperators.h:756
OutGridT::Ptr process(bool threaded=true)
Definition: GridOperators.h:347
Definition: GridOperators.h:842
const MaskGridType * mMask
Definition: GridOperators.h:678
VectorToScalarConverter< InGridT >::Type OutGridType
Definition: GridOperators.h:816
Functor(const InGridT &grid, const MaskGridType *mask, bool threaded, InterruptT *interrupt)
Definition: GridOperators.h:661
void operator()(const typename LeafManagerT::LeafRange &range) const
Iterate sequentially over LeafNodes and voxels in the output grid and compute the Laplacian using a v...
Definition: GridOperators.h:388
Iterator begin() const
Definition: LeafManager.h:188
GridT InGridType
Definition: GridOperators.h:697
Compute the gradient of a scalar grid.
Definition: GridOperators.h:634
InterruptT * mInterrupt
Definition: GridOperators.h:864
GridT::Ptr mOutputGrid
Definition: GridOperators.h:735
Definition: GridOperators.h:778
Center difference gradient operators, defined with respect to the range-space of the map...
Definition: Operators.h:642
const MaskGridType * mMask
Definition: GridOperators.h:801
const bool mThreaded
Definition: GridOperators.h:932
Compute the closest-point transform of a scalar grid.
Definition: GridOperators.h:418
const MaskGridType * mMask
Definition: GridOperators.h:796
Definition: Types.h:215
Curl(const GridT &grid, InterruptT *interrupt=NULL)
Definition: GridOperators.h:512
const bool mThreaded
Definition: GridOperators.h:674
const GridT & mInputGrid
Definition: GridOperators.h:793
const InGridT & mInputGrid
Definition: GridOperators.h:675
Laplacian(const GridT &grid, InterruptT *interrupt=NULL)
Definition: GridOperators.h:700
Compute the closest-point transform to a level set.
Definition: Operators.h:1656
void operator()(const MapT &map)
Definition: GridOperators.h:849
ToMaskGrid<T>::Type is the type of a grid having the same tree hierarchy as grid type T but a value e...
Definition: GridOperators.h:312
Cpt(const InGridType &grid, const MaskGridType &mask, InterruptT *interrupt=NULL)
Definition: GridOperators.h:429
const InGridType & mInputGrid
Definition: GridOperators.h:857
ScalarToVectorConverter<ScalarGridType>::Type is the type of a grid having the same tree configuratio...
Definition: GridOperators.h:65
GridT OutGridType
Definition: GridOperators.h:880
Functor(const InGridT &grid, const MaskGridType *mask, bool threaded, InterruptT *interrupt)
Definition: GridOperators.h:600
GridType::Ptr meanCurvature(const GridType &grid, const MaskT &mask, bool threaded=true)
Definition: GridOperators.h:240
Functor(const InGridT &grid, const MaskGridType *mask, bool threaded, InterruptT *interrupt)
Definition: GridOperators.h:844
void operator()(const MapT &map)
Definition: GridOperators.h:784
const MapT & mMap
Definition: GridOperators.h:402
InGridT InGridType
Definition: GridOperators.h:421
const MaskGridType * mMask
Definition: GridOperators.h:617
Gradient(const InGridT &grid, InterruptT *interrupt=NULL)
Definition: GridOperators.h:640
Definition: GridOperators.h:876
OutGridType::Ptr process(bool threaded=true)
Definition: GridOperators.h:828
Compute the curl of a vector grid.
Definition: GridOperators.h:506
OutGridType::Ptr process(bool threaded=true)
Definition: GridOperators.h:583
GridType::Ptr curl(const GridType &grid, const MaskT &mask, bool threaded=true)
Definition: GridOperators.h:126
OutGridType::Ptr process(bool threaded=true)
Definition: GridOperators.h:650
GridT::Ptr mOutputGrid
Definition: GridOperators.h:794
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:71
GridT::Ptr mOutputGrid
Definition: GridOperators.h:934
Definition: GridOperators.h:694
Definition: GridOperators.h:598
InterruptT * mInterrupt
Definition: GridOperators.h:682
Gradient(const InGridT &grid, const MaskGridType &mask, InterruptT *interrupt=NULL)
Definition: GridOperators.h:645
Compute the mean curvature.
Definition: Operators.h:1751
GridOperator(const InGridT &grid, const MaskGridType *mask, const MapT &map, InterruptT *interrupt=NULL)
Definition: GridOperators.h:340
const InGridT & mInputGrid
Definition: GridOperators.h:681
boost::shared_ptr< Transform > Ptr
Definition: Transform.h:69
const bool mThreaded
Definition: GridOperators.h:856
const MaskGridType * mMask
Definition: GridOperators.h:742
void operator()(const MapT &map)
Definition: GridOperators.h:666
Divergence(const InGridT &grid, InterruptT *interrupt=NULL)
Definition: GridOperators.h:573
Container class that associates a tree with a transform and metadata.
Definition: Grid.h:54
const bool mThreaded
Definition: GridOperators.h:613
GridT OutGridType
Definition: GridOperators.h:510
Apply an operator on an input grid to produce an output grid with the same topology but a possibly di...
Definition: GridOperators.h:333
Compute the divergence of a vector grid.
Definition: GridOperators.h:567
const MaskGridType * mMask
Definition: GridOperators.h:622
VectorGridType::ValueType::value_type VecComponentValueT
Definition: GridOperators.h:57
bool wasInterrupted(T *i, int percent=-1)
Definition: NullInterrupter.h:76
const MaskGridType * mMask
Definition: GridOperators.h:941
const MaskGridType * mMask
Definition: GridOperators.h:865
const MaskGridType * mMask
Definition: GridOperators.h:404
InGridT::ConstAccessor AccessorT
Definition: GridOperators.h:400