OpenVDB  2.1.0
GridOperators.h
Go to the documentation of this file.
1 //
3 // Copyright (c) 2012-2013 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 
90 template<typename GridType> inline
92 cpt(const GridType& grid, bool threaded = true)
93 {
94  return cpt<GridType, util::NullInterrupter>(grid, threaded, NULL);
95 }
96 
97 
98 template<typename GridType, typename MaskT> inline
99 typename ScalarToVectorConverter<GridType>::Type::Ptr
100 cpt(const GridType& grid, const MaskT& mask, bool threaded = true)
101 {
102  return cpt<GridType, MaskT, util::NullInterrupter>(grid, mask, threaded, NULL);
103 }
104 
105 
111 template<typename GridType, typename InterruptT> inline
112 typename GridType::Ptr
113 curl(const GridType& grid, bool threaded, InterruptT* interrupt);
114 
115 template<typename GridType, typename MaskT, typename InterruptT> inline
116 typename GridType::Ptr
117 curl(const GridType& grid, const MaskT& mask, bool threaded, InterruptT* interrupt);
118 
119 template<typename GridType> inline
120 typename GridType::Ptr
121 curl(const GridType& grid, bool threaded = true)
122 {
123  return curl<GridType, util::NullInterrupter>(grid, threaded, NULL);
124 }
125 
126 template<typename GridType, typename MaskT> inline
127 typename GridType::Ptr
128 curl(const GridType& grid, const MaskT& mask, bool threaded = true)
129 {
130  return curl<GridType, MaskT, util::NullInterrupter>(grid, mask, threaded, NULL);
131 }
132 
139 template<typename GridType, typename InterruptT> inline
140 typename VectorToScalarConverter<GridType>::Type::Ptr
141 divergence(const GridType& grid, bool threaded, InterruptT* interrupt);
142 
143 template<typename GridType, typename MaskT, typename InterruptT> inline
144 typename VectorToScalarConverter<GridType>::Type::Ptr
145 divergence(const GridType& grid, const MaskT& mask, bool threaded, InterruptT* interrupt);
146 
147 template<typename GridType> inline
148 typename VectorToScalarConverter<GridType>::Type::Ptr
149 divergence(const GridType& grid, bool threaded = true)
150 {
151  return divergence<GridType, util::NullInterrupter>(grid, threaded, NULL);
152 }
153 
154 template<typename GridType, typename MaskT> inline
155 typename VectorToScalarConverter<GridType>::Type::Ptr
156 divergence(const GridType& grid, const MaskT& mask, bool threaded = true)
157 {
158  return divergence<GridType, MaskT, util::NullInterrupter>(grid, mask, threaded, NULL);
159 }
160 
167 template<typename GridType, typename InterruptT> inline
168 typename ScalarToVectorConverter<GridType>::Type::Ptr
169 gradient(const GridType& grid, bool threaded, InterruptT* interrupt);
170 
171 template<typename GridType, typename MaskT, typename InterruptT> inline
172 typename ScalarToVectorConverter<GridType>::Type::Ptr
173 gradient(const GridType& grid, const MaskT& mask, bool threaded, InterruptT* interrupt);
174 
175 template<typename GridType> inline
176 typename ScalarToVectorConverter<GridType>::Type::Ptr
177 gradient(const GridType& grid, bool threaded = true)
178 {
179  return gradient<GridType, util::NullInterrupter>(grid, threaded, NULL);
180 }
181 
182 
183 template<typename GridType, typename MaskT> inline
184 typename ScalarToVectorConverter<GridType>::Type::Ptr
185 gradient(const GridType& grid, const MaskT& mask, bool threaded = true)
186 {
187  return gradient<GridType, MaskT, util::NullInterrupter>(grid, mask, threaded, NULL);
188 }
189 
190 
196 template<typename GridType, typename InterruptT> inline
197 typename GridType::Ptr
198 laplacian(const GridType& grid, bool threaded, InterruptT* interrupt);
199 
200 template<typename GridType, typename MaskT, typename InterruptT> inline
201 typename GridType::Ptr
202 laplacian(const GridType& grid, const MaskT& mask, bool threaded, InterruptT* interrupt);
203 
204 template<typename GridType> inline
205 typename GridType::Ptr
206 laplacian(const GridType& grid, bool threaded = true)
207 {
208  return laplacian<GridType, util::NullInterrupter>(grid, threaded, NULL);
209 }
210 
211 template<typename GridType, typename MaskT> inline
212 typename GridType::Ptr
213 laplacian(const GridType& grid, const MaskT mask, bool threaded = true)
214 {
215  return laplacian<GridType, MaskT, util::NullInterrupter>(grid, mask, threaded, NULL);
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 
251 template<typename GridType, typename InterruptT> inline
252 typename VectorToScalarConverter<GridType>::Type::Ptr
253 magnitude(const GridType& grid, bool threaded, InterruptT* interrupt);
254 
255 template<typename GridType, typename MaskT, typename InterruptT> inline
256 typename VectorToScalarConverter<GridType>::Type::Ptr
257 magnitude(const GridType& grid, const MaskT& mask, bool threaded, InterruptT* interrupt);
258 
259 template<typename GridType> inline
260 typename VectorToScalarConverter<GridType>::Type::Ptr
261 magnitude(const GridType& grid, bool threaded = true)
262 {
263  return magnitude<GridType, util::NullInterrupter>(grid, threaded, NULL);
264 }
265 
266 template<typename GridType, typename MaskT> inline
267 typename VectorToScalarConverter<GridType>::Type::Ptr
268 magnitude(const GridType& grid, const MaskT& mask, bool threaded = true)
269 {
270  return magnitude<GridType, MaskT, util::NullInterrupter>(grid, mask, threaded, NULL);
271 }
272 
278 template<typename GridType, typename InterruptT> inline
279 typename GridType::Ptr
280 normalize(const GridType& grid, bool threaded, InterruptT* interrupt);
281 
282 template<typename GridType, typename MaskT, typename InterruptT> inline
283 typename GridType::Ptr
284 normalize(const GridType& grid, const MaskT& mask, bool threaded, InterruptT* interrupt);
285 
286 template<typename GridType> inline
287 typename GridType::Ptr
288 normalize(const GridType& grid, bool threaded = true)
289 {
290  return normalize<GridType, util::NullInterrupter>(grid, threaded, NULL);
291 }
292 
293 template<typename GridType, typename MaskT> inline
294 typename GridType::Ptr
295 normalize(const GridType& grid, const MaskT& mask, bool threaded = true)
296 {
297  return normalize<GridType, MaskT, util::NullInterrupter>(grid, mask, threaded, NULL);
298 }
299 
301 
302 
303 namespace {
304 
314 template<typename InGridT, typename MaskGridType, typename OutGridT, typename MapT, typename OperatorT, typename InterruptT = util::NullInterrupter>
315 class GridOperator
316 {
317 public:
318  typedef typename OutGridT::TreeType OutTreeT;
319  typedef typename OutTreeT::LeafNodeType OutLeafT;
320  typedef typename tree::LeafManager<OutTreeT> LeafManagerT;
321 
322  GridOperator(const InGridT& grid, const MaskGridType* mask, const MapT& map, InterruptT* interrupt = NULL):
323  mAcc(grid.getConstAccessor()), mMap(map), mInterrupt(interrupt), mMask(mask)
324  {
325  }
326 
327  virtual ~GridOperator() {}
328  typename OutGridT::Ptr process(bool threaded = true)
329  {
330  if (mInterrupt) mInterrupt->start("Processing grid");
331 
332  // Derive background value of the output grid
333  typename InGridT::TreeType tmp(mAcc.tree().background());
334  typename OutGridT::ValueType backg = OperatorT::result(mMap, tmp, math::Coord(0));
335 
336  // output tree = topology copy of input tree!
337  typename OutTreeT::Ptr tree(new OutTreeT(mAcc.tree(), backg, TopologyCopy()));
338 
339 
340  // create grid with output tree and unit transform
341  typename OutGridT::Ptr result(new OutGridT(tree));
342 
343  // Modify the solution area if a mask was supplied.
344  if (mMask) {
345  result->topologyIntersection(*mMask);
346  }
347 
348  // transform of output grid = transform of input grid
349  result->setTransform(math::Transform::Ptr(new math::Transform( mMap.copy() )));
350 
351  LeafManagerT leafManager(*tree);
352 
353  if (threaded) {
354  tbb::parallel_for(leafManager.leafRange(), *this);
355  } else {
356  (*this)(leafManager.leafRange());
357  }
358 
359  if (mInterrupt) mInterrupt->end();
360  return result;
361  }
362 
369  void operator()(const typename LeafManagerT::LeafRange& range) const
370  {
371  if (util::wasInterrupted(mInterrupt)) tbb::task::self().cancel_group_execution();
372 
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()));
376  }
377  }
378  }
379 
380 protected:
381 
382  typedef typename InGridT::ConstAccessor AccessorT;
383  mutable AccessorT mAcc;
384  const MapT& mMap;
385  InterruptT* mInterrupt;
386  const MaskGridType* mMask;
387 }; // end of GridOperator class
388 
389 } //end of anonymous namespaceo
390 
391 
393 
394 
396 template<typename InGridT, typename MaskGridType = BoolGrid, typename InterruptT = util::NullInterrupter>
397 class Cpt
398 {
399 public:
400  typedef InGridT InGridType;
402 
403  Cpt(const InGridType& grid, InterruptT* interrupt = NULL):
404  mInputGrid(grid), mInterrupt(interrupt), mMask(NULL)
405  {
406  }
407 
408 
409  Cpt(const InGridType& grid, const MaskGridType& mask, InterruptT* interrupt = NULL):
410  mInputGrid(grid), mInterrupt(interrupt), mMask(&mask)
411  {
412  }
413 
414 
415  typename OutGridType::Ptr process(bool threaded = true, bool useWorldTransform = true)
416  {
417  Functor functor(mInputGrid, mMask, threaded, useWorldTransform, mInterrupt);
418  processTypedMap(mInputGrid.transform(), functor);
419  return functor.mOutputGrid;
420  }
421 private:
422  struct IsOpT
423  {
424  template<typename MapT, typename AccT>
425  static typename OutGridType::ValueType
426  result(const MapT& map, const AccT& acc, const Coord& xyz)
427  {
428  return math::CPT<MapT, math::CD_2ND>::result(map, acc, xyz);
429  }
430  };
431  struct WsOpT
432  {
433  template<typename MapT, typename AccT>
434  static typename OutGridType::ValueType
435  result(const MapT& map, const AccT& acc, const Coord& xyz)
436  {
437  return math::CPT_RANGE<MapT, math::CD_2ND>::result(map, acc, xyz);
438  }
439  };
440  struct Functor
441  {
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)
446  {
447  if (mWorldSpace) {
448  GridOperator<InGridType, MaskGridType, OutGridType, MapT, WsOpT, InterruptT> op(mInputGrid, mMask, map, mInterrupt);
449  mOutputGrid = op.process(mThreaded); // cache the result
450  } else {
451  GridOperator<InGridType, MaskGridType, OutGridType, MapT, IsOpT, InterruptT> op(mInputGrid, mMask, map, mInterrupt);
452  mOutputGrid = op.process(mThreaded); // cache the result
453  }
454  }
455  const bool mThreaded;
456  const bool mWorldSpace;
457  const InGridType& mInputGrid;
458  typename OutGridType::Ptr mOutputGrid;
459  InterruptT* mInterrupt;
460  const MaskGridType* mMask;
461  };
462  const InGridType& mInputGrid;
463  InterruptT* mInterrupt;
464  const MaskGridType* mMask;
465 }; // end of Cpt class
466 
467 
469 
470 
472 template<typename GridT, typename MaskGridType = BoolGrid, typename InterruptT = util::NullInterrupter>
473 class Curl
474 {
475 public:
476  typedef GridT InGridType;
477  typedef GridT OutGridType;
478  Curl(const GridT& grid, InterruptT* interrupt = NULL):
479  mInputGrid(grid), mInterrupt(interrupt), mMask(NULL)
480  {
481  }
482  Curl(const GridT& grid, const MaskGridType& mask, InterruptT* interrupt = NULL):
483  mInputGrid(grid), mInterrupt(interrupt), mMask(&mask)
484  {
485  }
486 
487  typename GridT::Ptr process(bool threaded = true)
488  {
489  Functor functor(mInputGrid, mMask, threaded, mInterrupt);
490  processTypedMap(mInputGrid.transform(), functor);
491  return functor.mOutputGrid;
492  }
493 
494 private:
495  struct Functor
496  {
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)
502  {
503  typedef math::Curl<MapT, math::CD_2ND> OpT;
504  GridOperator<GridT, MaskGridType, GridT, MapT, OpT, InterruptT> op(mInputGrid, mMask, map, mInterrupt);
505  mOutputGrid = op.process(mThreaded); // cache the result
506  }
507  const bool mThreaded;
508  const GridT& mInputGrid;
509  typename GridT::Ptr mOutputGrid;
510  InterruptT* mInterrupt;
511  const MaskGridType* mMask;
512  }; // Private Functor
513 
514  const GridT& mInputGrid;
515  InterruptT* mInterrupt;
516  const MaskGridType* mMask;
517 }; // end of Curl class
518 
519 
521 
522 
524 template<typename InGridT, typename MaskGridType = BoolGrid, typename InterruptT = util::NullInterrupter>
526 {
527 public:
528  typedef InGridT InGridType;
530 
531  Divergence(const InGridT& grid, InterruptT* interrupt = NULL):
532  mInputGrid(grid), mInterrupt(interrupt), mMask(NULL)
533  {
534  }
535 
536  Divergence(const InGridT& grid, const MaskGridType& mask, InterruptT* interrupt = NULL):
537  mInputGrid(grid), mInterrupt(interrupt), mMask(&mask)
538  {
539  }
540  typename OutGridType::Ptr process(bool threaded = true)
541  {
542  if( mInputGrid.getGridClass() == GRID_STAGGERED ) {
543  Functor<math::FD_1ST> functor(mInputGrid, mMask, threaded, mInterrupt);
544  processTypedMap(mInputGrid.transform(), functor);
545  return functor.mOutputGrid;
546  }
547  else {
548  Functor<math::CD_2ND> functor(mInputGrid, mMask, threaded, mInterrupt);
549  processTypedMap(mInputGrid.transform(), functor);
550  return functor.mOutputGrid;
551  }
552  }
553 
554 protected:
555  template<math::DScheme DiffScheme>
556  struct Functor
557  {
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>
561  void operator()(const MapT& map)
562  {
564  GridOperator<InGridType, MaskGridType, OutGridType, MapT, OpT, InterruptT> op(mInputGrid, mMask, map, mInterrupt);
565  mOutputGrid = op.process(mThreaded); // cache the result
566  }
567  const bool mThreaded;
569  typename OutGridType::Ptr mOutputGrid;
570  InterruptT* mInterrupt;
571  const MaskGridType* mMask;
572  }; // Private Functor
573 
575  InterruptT* mInterrupt;
576  const MaskGridType* mMask;
577 }; // end of Divergence class
578 
579 
581 
582 
584 template<typename InGridT, typename MaskGridType = BoolGrid, typename InterruptT = util::NullInterrupter>
585 class Gradient
586 {
587 public:
588  typedef InGridT InGridType;
590 
591  Gradient(const InGridT& grid, InterruptT* interrupt = NULL):
592  mInputGrid(grid), mInterrupt(interrupt), mMask(NULL)
593  {
594  }
595  Gradient(const InGridT& grid, const MaskGridType& mask, InterruptT* interrupt = NULL):
596  mInputGrid(grid), mInterrupt(interrupt), mMask(&mask)
597  {
598  }
599  typename OutGridType::Ptr process(bool threaded = true)
600  {
601  Functor functor(mInputGrid, mMask, threaded, mInterrupt);
602  processTypedMap(mInputGrid.transform(), functor);
603  return functor.mOutputGrid;
604  }
605 
606 protected:
607  struct Functor
608  {
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>
612  void operator()(const MapT& map)
613  {
615  GridOperator<InGridType, MaskGridType, OutGridType, MapT, OpT, InterruptT> op(mInputGrid, mMask, map, mInterrupt);
616  mOutputGrid = op.process(mThreaded); // cache the result
617  }
618  const bool mThreaded;
619  const InGridT& mInputGrid;
620  typename OutGridType::Ptr mOutputGrid;
621  InterruptT* mInterrupt;
622  const MaskGridType* mMask;
623  }; // Private Functor
624 
625  const InGridT& mInputGrid;
626  InterruptT* mInterrupt;
627  const MaskGridType* mMask;
628 }; // end of Gradient class
629 
630 
632 
633 
635 template<typename GridT, typename MaskGridType = BoolGrid, typename InterruptT = util::NullInterrupter>
637 {
638 public:
639  typedef GridT InGridType;
640  typedef GridT OutGridType;
641  Laplacian(const GridT& grid, InterruptT* interrupt = NULL):
642  mInputGrid(grid), mInterrupt(interrupt), mMask(NULL)
643  {
644  }
645 
646  Laplacian(const GridT& grid, const MaskGridType& mask, InterruptT* interrupt = NULL):
647  mInputGrid(grid), mInterrupt(interrupt), mMask(&mask)
648  {
649  }
650  typename GridT::Ptr process(bool threaded = true)
651  {
652  Functor functor(mInputGrid, mMask, threaded, mInterrupt);
653  processTypedMap(mInputGrid.transform(), functor);
654  return functor.mOutputGrid;
655  }
656 
657 protected:
658  struct Functor
659  {
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>
663  void operator()(const MapT& map)
664  {
666  GridOperator<GridT, MaskGridType, GridT, MapT, OpT, InterruptT> op(mInputGrid, mMask, map);
667  mOutputGrid = op.process(mThreaded); // cache the result
668  }
669  const bool mThreaded;
670  const GridT& mInputGrid;
671  typename GridT::Ptr mOutputGrid;
672  InterruptT* mInterrupt;
673  const MaskGridType* mMask;
674  }; // Private Functor
675 
676  const GridT& mInputGrid;
677  InterruptT* mInterrupt;
678  const MaskGridType* mMask;
679 }; // end of Laplacian class
680 
681 
683 
684 
685 template<typename GridT, typename MaskGridType = BoolGrid, typename InterruptT = util::NullInterrupter>
687 {
688 public:
689  typedef GridT InGridType;
690  typedef GridT OutGridType;
691  MeanCurvature(const GridT& grid, InterruptT* interrupt = NULL):
692  mInputGrid(grid), mInterrupt(interrupt), mMask(NULL)
693  {
694  }
695  MeanCurvature(const GridT& grid, const MaskGridType& mask, InterruptT* interrupt = NULL):
696  mInputGrid(grid), mInterrupt(interrupt), mMask(&mask)
697  {
698  }
699 
700  typename GridT::Ptr process(bool threaded = true)
701  {
702  Functor functor(mInputGrid, mMask, threaded, mInterrupt);
703  processTypedMap(mInputGrid.transform(), functor);
704  return functor.mOutputGrid;
705  }
706 
707 protected:
708  struct Functor
709  {
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>
713  void operator()(const MapT& map)
714  {
716  GridOperator<GridT, MaskGridType, GridT, MapT, OpT, InterruptT> op(mInputGrid, mMask, map);
717  mOutputGrid = op.process(mThreaded); // cache the result
718  }
719  const bool mThreaded;
720  const GridT& mInputGrid;
721  typename GridT::Ptr mOutputGrid;
722  InterruptT* mInterrupt;
723  const MaskGridType* mMask;
724  }; // Private Functor
725 
726  const GridT& mInputGrid;
727  InterruptT* mInterrupt;
728  const MaskGridType* mMask;
729 }; // end of MeanCurvature class
730 
731 
733 
734 
735 template<typename InGridT, typename MaskGridType = BoolGrid, typename InterruptT = util::NullInterrupter>
737 {
738 public:
739  typedef InGridT InGridType;
741  Magnitude(const InGridType& grid, InterruptT* interrupt = NULL):
742  mInputGrid(grid), mInterrupt(interrupt), mMask(NULL)
743  {
744  }
745  Magnitude(const InGridType& grid, const MaskGridType& mask, InterruptT* interrupt = NULL):
746  mInputGrid(grid), mInterrupt(interrupt), mMask(&mask)
747  {
748  }
749  typename OutGridType::Ptr process(bool threaded = true)
750  {
751  Functor functor(mInputGrid, mMask, threaded, mInterrupt);
752  processTypedMap(mInputGrid.transform(), functor);
753  return functor.mOutputGrid;
754  }
755 
756 protected:
757  struct OpT
758  {
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();}
762  };
763  struct Functor
764  {
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>
768  void operator()(const MapT& map)
769  {
770  GridOperator<InGridType, MaskGridType, OutGridType, MapT, OpT, InterruptT> op(mInputGrid, mMask, map);
771  mOutputGrid = op.process(mThreaded); // cache the result
772  }
773  const bool mThreaded;
775  typename OutGridType::Ptr mOutputGrid;
776  InterruptT* mInterrupt;
777  const MaskGridType* mMask;
778  }; // Private Functor
779 
781  InterruptT* mInterrupt;
782  const MaskGridType* mMask;
783 }; // end of Magnitude class
784 
785 
787 
788 
789 template<typename GridT, typename MaskGridType = BoolGrid, typename InterruptT = util::NullInterrupter>
791 {
792 public:
793  typedef GridT InGridType;
794  typedef GridT OutGridType;
795  Normalize(const GridT& grid, InterruptT* interrupt = NULL):
796  mInputGrid(grid), mInterrupt(interrupt), mMask(NULL)
797  {
798  }
799  Normalize(const GridT& grid, const MaskGridType& mask, InterruptT* interrupt = NULL):
800  mInputGrid(grid), mInterrupt(interrupt), mMask(&mask)
801  {
802  }
803 
804  typename GridT::Ptr process(bool threaded = true)
805  {
806  Functor functor(mInputGrid, mMask, threaded, mInterrupt);
807  processTypedMap(mInputGrid.transform(), functor);
808  return functor.mOutputGrid;
809  }
810 
811 protected:
812  struct OpT
813  {
814  template<typename MapT, typename AccT>
815  static typename OutGridType::ValueType
816  result(const MapT&, const AccT& acc, const Coord& xyz)
817  {
818  typename OutGridType::ValueType vec = acc.getValue(xyz);
819  if ( !vec.normalize() ) vec.setZero();
820  return vec;
821  }
822  };
823  struct Functor
824  {
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>
828  void operator()(const MapT& map)
829  {
830  GridOperator<GridT, MaskGridType, GridT, MapT, OpT, InterruptT> op(mInputGrid, mMask,map);
831  mOutputGrid = op.process(mThreaded); // cache the result
832  }
833  const bool mThreaded;
834  const GridT& mInputGrid;
835  typename GridT::Ptr mOutputGrid;
836  InterruptT* mInterrupt;
837  const MaskGridType* mMask;
838  }; // Private Functor
839 
840  const GridT& mInputGrid;
841  InterruptT* mInterrupt;
842  const MaskGridType* mMask;
843 }; // end of Normalize class
844 
845 
847 
848 
849 template<typename GridType, typename InterruptT> inline
851 cpt(const GridType& grid, bool threaded, InterruptT* interrupt)
852 {
853  Cpt<GridType, BoolGrid, InterruptT> op(grid, interrupt);
854  return op.process(threaded);
855 }
856 
857 template<typename GridType, typename MaskT, typename InterruptT> inline
858 typename ScalarToVectorConverter<GridType>::Type::Ptr
859 cpt(const GridType& grid, const MaskT& mask, bool threaded, InterruptT* interrupt)
860 {
861  Cpt<GridType, MaskT, InterruptT> op(grid, mask, interrupt);
862  return op.process(threaded);
863 }
864 
865 template<typename GridType, typename InterruptT> inline
866 typename GridType::Ptr
867 curl(const GridType& grid, bool threaded, InterruptT* interrupt)
868 {
869  Curl<GridType, BoolGrid, InterruptT> op(grid, interrupt);
870  return op.process(threaded);
871 }
872 
873 
874 template<typename GridType, typename MaskT, typename InterruptT> inline
875 typename GridType::Ptr
876 curl(const GridType& grid, const MaskT& mask, bool threaded, InterruptT* interrupt)
877 {
878  Curl<GridType, MaskT, InterruptT> op(grid, mask, interrupt);
879  return op.process(threaded);
880 }
881 
882 template<typename GridType, typename InterruptT> inline
883 typename VectorToScalarConverter<GridType>::Type::Ptr
884 divergence(const GridType& grid, bool threaded, InterruptT* interrupt)
885 {
886  Divergence<GridType, BoolGrid, InterruptT> op(grid, interrupt);
887  return op.process(threaded);
888 }
889 
890 template<typename GridType, typename MaskT, typename InterruptT> inline
891 typename VectorToScalarConverter<GridType>::Type::Ptr
892 divergence(const GridType& grid, const MaskT& mask, bool threaded, InterruptT* interrupt)
893 {
894  Divergence<GridType, MaskT, InterruptT> op(grid, mask, interrupt);
895  return op.process(threaded);
896 }
897 
898 template<typename GridType, typename InterruptT> inline
899 typename ScalarToVectorConverter<GridType>::Type::Ptr
900 gradient(const GridType& grid, bool threaded, InterruptT* interrupt)
901 {
902  Gradient<GridType, BoolGrid, InterruptT> op(grid, interrupt);
903  return op.process(threaded);
904 }
905 
906 template<typename GridType, typename MaskT, typename InterruptT> inline
907 typename ScalarToVectorConverter<GridType>::Type::Ptr
908 gradient(const GridType& grid, const MaskT& mask, bool threaded, InterruptT* interrupt)
909 {
910  Gradient<GridType, MaskT, InterruptT> op(grid, mask, interrupt);
911  return op.process(threaded);
912 }
913 
914 template<typename GridType, typename InterruptT> inline
915 typename GridType::Ptr
916 laplacian(const GridType& grid, bool threaded, InterruptT* interrupt)
917 {
918  Laplacian<GridType, BoolGrid, InterruptT> op(grid, interrupt);
919  return op.process(threaded);
920 }
921 
922 template<typename GridType, typename MaskT, typename InterruptT> inline
923 typename GridType::Ptr
924 laplacian(const GridType& grid, const MaskT& mask, bool threaded, InterruptT* interrupt)
925 {
926  Laplacian<GridType, MaskT, InterruptT> op(grid, mask, interrupt);
927  return op.process(threaded);
928 }
929 
930 template<typename GridType, typename InterruptT> inline
931 typename GridType::Ptr
932 meanCurvature(const GridType& grid, bool threaded, InterruptT* interrupt)
933 {
935  return op.process(threaded);
936 }
937 
938 template<typename GridType, typename MaskT, typename InterruptT> inline
939 typename GridType::Ptr
940 meanCurvature(const GridType& grid, const MaskT& mask, bool threaded, InterruptT* interrupt)
941 {
942  MeanCurvature<GridType, MaskT, InterruptT> op(grid, mask, interrupt);
943  return op.process(threaded);
944 }
945 
946 template<typename GridType, typename InterruptT> inline
947 typename VectorToScalarConverter<GridType>::Type::Ptr
948 magnitude(const GridType& grid, bool threaded, InterruptT* interrupt)
949 {
950  Magnitude<GridType, BoolGrid, InterruptT> op(grid, interrupt);
951  return op.process(threaded);
952 }
953 
954 template<typename GridType, typename MaskT, typename InterruptT> inline
955 typename VectorToScalarConverter<GridType>::Type::Ptr
956 magnitude(const GridType& grid, const MaskT& mask, bool threaded, InterruptT* interrupt)
957 {
958  Magnitude<GridType, MaskT, InterruptT> op(grid, mask, interrupt);
959  return op.process(threaded);
960 }
961 
962 template<typename GridType, typename InterruptT> inline
963 typename GridType::Ptr
964 normalize(const GridType& grid, bool threaded, InterruptT* interrupt)
965 {
966  Normalize<GridType, BoolGrid, InterruptT> op(grid, interrupt);
967  return op.process(threaded);
968 }
969 
970 template<typename GridType, typename MaskT, typename InterruptT> inline
971 typename GridType::Ptr
972 normalize(const GridType& grid, const MaskT& mask, bool threaded, InterruptT* interrupt)
973 {
974  Normalize<GridType, MaskT, InterruptT> op(grid, mask, interrupt);
975  return op.process(threaded);
976 }
977 
978 } // namespace tools
979 } // namespace OPENVDB_VERSION_NAME
980 } // namespace openvdb
981 
982 #endif // OPENVDB_TOOLS_GRID_OPERATORS_HAS_BEEN_INCLUDED
983 
984 // Copyright (c) 2012-2013 DreamWorks Animation LLC
985 // All rights reserved. This software is distributed under the
986 // Mozilla Public License 2.0 ( http://www.mozilla.org/MPL/2.0/ )
ScalarToVectorConverter< GridType >::Type::Ptr gradient(const GridType &grid, bool threaded, InterruptT *interrupt)
Compute the gradient of the given scalar grid.
Definition: GridOperators.h:900
const GridT & mInputGrid
Definition: GridOperators.h:726
const GridT & mInputGrid
Definition: GridOperators.h:670
GridT::Ptr mOutputGrid
Definition: GridOperators.h:835
Definition: GridOperators.h:812
ScalarToVectorConverter< InGridT >::Type OutGridType
Definition: GridOperators.h:589
void operator()(const MapT &map)
Definition: GridOperators.h:663
const InGridT & mInputGrid
Definition: GridOperators.h:619
Definition: GridOperators.h:736
GridT OutGridType
Definition: GridOperators.h:690
OutGridType::Ptr process(bool threaded=true, bool useWorldTransform=true)
Definition: GridOperators.h:415
Definition: GridOperators.h:607
Definition: Mat.h:150
const GridT & mInputGrid
Definition: GridOperators.h:834
Functor(const GridT &grid, const MaskGridType *mask, bool threaded, InterruptT *interrupt)
Definition: GridOperators.h:660
Gradient(const InGridT &grid, InterruptT *interrupt=NULL)
Definition: GridOperators.h:591
const MapT & mMap
Definition: GridOperators.h:384
const MaskGridType * mMask
Definition: GridOperators.h:673
InGridT InGridType
Definition: GridOperators.h:400
Definition: GridOperators.h:790
void operator()(const MapT &map)
Definition: GridOperators.h:713
GridT InGridType
Definition: GridOperators.h:639
InterruptT * mInterrupt
Definition: GridOperators.h:575
Compute the mean curvature.
Definition: Operators.h:1738
const MaskGridType * mMask
Definition: GridOperators.h:576
VectorToScalarConverter< GridType >::Type::Ptr magnitude(const GridType &grid, bool threaded, InterruptT *interrupt)
Compute the magnitudes of the vectors of the given vector-valued grid.
Definition: GridOperators.h:948
const bool mThreaded
Definition: GridOperators.h:567
Definition: GridOperators.h:763
InterruptT * mInterrupt
Definition: GridOperators.h:727
GridT::Ptr mOutputGrid
Definition: GridOperators.h:671
Divergence(const InGridT &grid, InterruptT *interrupt=NULL)
Definition: GridOperators.h:531
Computes the Laplacian of a scalar grid.
Definition: GridOperators.h:636
const bool mThreaded
Definition: GridOperators.h:719
ScalarToVectorConverter&lt;ScalarGridType&gt;::Type is the type of a grid having the same tree configuratio...
Definition: GridOperators.h:65
const MaskGridType * mMask
Definition: GridOperators.h:723
void operator()(const MapT &map)
Definition: GridOperators.h:612
Functor(const InGridT &grid, const MaskGridType *mask, bool threaded, InterruptT *interrupt)
Definition: GridOperators.h:765
InterruptT * mInterrupt
Definition: GridOperators.h:781
Definition: Types.h:139
Laplacian(const GridT &grid, InterruptT *interrupt=NULL)
Definition: GridOperators.h:641
Functor(const GridT &grid, const MaskGridType *mask, bool threaded, InterruptT *interrupt)
Definition: GridOperators.h:825
InterruptT * mInterrupt
Definition: GridOperators.h:836
Functor(const InGridT &grid, const MaskGridType *mask, bool threaded, InterruptT *interrupt)
Definition: GridOperators.h:609
const bool mThreaded
Definition: GridOperators.h:669
const MaskGridType * mMask
Definition: GridOperators.h:571
AccessorT mAcc
Definition: GridOperators.h:383
Definition: GridOperators.h:556
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
const InGridType & mInputGrid
Definition: GridOperators.h:774
OutGridType::Ptr mOutputGrid
Definition: GridOperators.h:569
InterruptT * mInterrupt
Definition: GridOperators.h:570
ScalarToVectorConverter< GridType >::Type::Ptr cpt(const GridType &grid, bool threaded, InterruptT *interrupt)
Compute the Closest-Point Transform (CPT) from a distance field.
Definition: GridOperators.h:851
Definition: GridOperators.h:757
const MaskGridType * mMask
Definition: GridOperators.h:782
VectorToScalarConverter< InGridT >::Type OutGridType
Definition: GridOperators.h:740
InGridT InGridType
Definition: GridOperators.h:588
GridT OutGridType
Definition: GridOperators.h:794
const MaskGridType * mMask
Definition: GridOperators.h:678
OutGridType::Ptr process(bool threaded=true)
Definition: GridOperators.h:749
GridType::Ptr normalize(const GridType &grid, bool threaded, InterruptT *interrupt)
Normalize the vectors of the given vector-valued grid.
Definition: GridOperators.h:964
VectorToScalarConverter&lt;VectorGridType&gt;::Type is the type of a grid having the same tree configuratio...
Definition: GridOperators.h:56
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
GridT::Ptr process(bool threaded=true)
Definition: GridOperators.h:650
const MaskGridType * mMask
Definition: GridOperators.h:837
Functor(const GridT &grid, const MaskGridType *mask, bool threaded, InterruptT *interrupt)
Definition: GridOperators.h:710
const InGridType & mInputGrid
Definition: GridOperators.h:780
void operator()(const MapT &map)
Definition: GridOperators.h:768
const MaskGridType * mMask
Definition: GridOperators.h:627
#define OPENVDB_VERSION_NAME
Definition: version.h:45
const InGridType & mInputGrid
Definition: GridOperators.h:574
VectorToScalarConverter< GridType >::Type::Ptr divergence(const GridType &grid, bool threaded, InterruptT *interrupt)
Compute the divergence of the given vector-valued grid.
Definition: GridOperators.h:884
const MaskGridType * mMask
Definition: GridOperators.h:622
InterruptT * mInterrupt
Definition: GridOperators.h:626
InterruptT * mInterrupt
Definition: GridOperators.h:677
const MaskGridType * mMask
Definition: GridOperators.h:777
InterruptT * mInterrupt
Definition: GridOperators.h:776
const MaskGridType * mMask
Definition: GridOperators.h:386
Definition: GridOperators.h:823
const InGridT & mInputGrid
Definition: GridOperators.h:625
Curl(const GridT &grid, InterruptT *interrupt=NULL)
Definition: GridOperators.h:478
Compute the closest-point transform to a level set.
Definition: Operators.h:1696
InterruptT * mInterrupt
Definition: GridOperators.h:385
InterruptT * mInterrupt
Definition: GridOperators.h:722
Compute the curl of a scalar grid.
Definition: GridOperators.h:473
Magnitude(const InGridType &grid, const MaskGridType &mask, InterruptT *interrupt=NULL)
Definition: GridOperators.h:745
void operator()(const MapT &map)
Definition: GridOperators.h:561
Compute the closest-point transform to a level set.
Definition: Operators.h:1643
Definition: GridOperators.h:658
const InGridType & mInputGrid
Definition: GridOperators.h:568
GridType::Ptr meanCurvature(const GridType &grid, bool threaded, InterruptT *interrupt)
Compute the mean curvature of the given grid.
Definition: GridOperators.h:932
InterruptT * mInterrupt
Definition: GridOperators.h:841
Gradient(const InGridT &grid, const MaskGridType &mask, InterruptT *interrupt=NULL)
Definition: GridOperators.h:595
const MaskGridType * mMask
Definition: GridOperators.h:842
InGridT InGridType
Definition: GridOperators.h:528
GridT InGridType
Definition: GridOperators.h:689
Compute the curl of a vector-valued grid using differencing of various orders in the space defined by...
Definition: Operators.h:1264
Normalize(const GridT &grid, InterruptT *interrupt=NULL)
Definition: GridOperators.h:795
InGridT InGridType
Definition: GridOperators.h:739
Cpt(const InGridType &grid, InterruptT *interrupt=NULL)
Definition: GridOperators.h:403
GridT::Ptr process(bool threaded=true)
Definition: GridOperators.h:487
VectorGridType::template ValueConverter< VecComponentValueT >::Type Type
Definition: GridOperators.h:58
Definition: GridOperators.h:708
const MaskGridType * mMask
Definition: GridOperators.h:728
static OutGridType::ValueType result(const MapT &, const AccT &acc, const Coord &xyz)
Definition: GridOperators.h:816
Divergence(const InGridT &grid, const MaskGridType &mask, InterruptT *interrupt=NULL)
Definition: GridOperators.h:536
const bool mThreaded
Definition: GridOperators.h:833
OutGridType::Ptr mOutputGrid
Definition: GridOperators.h:775
MeanCurvature(const GridT &grid, const MaskGridType &mask, InterruptT *interrupt=NULL)
Definition: GridOperators.h:695
ScalarToVectorConverter< InGridT >::Type OutGridType
Definition: GridOperators.h:401
Compute the closest-point transform of a scalar grid.
Definition: GridOperators.h:397
math::Vec3< typename ScalarGridType::ValueType > VectorValueT
Definition: GridOperators.h:66
InterruptT * mInterrupt
Definition: GridOperators.h:621
Definition: GridOperators.h:686
Functor(const InGridT &grid, const MaskGridType *mask, bool threaded, InterruptT *interrupt)
Definition: GridOperators.h:558
InterruptT * mInterrupt
Definition: GridOperators.h:672
Magnitude(const InGridType &grid, InterruptT *interrupt=NULL)
Definition: GridOperators.h:741
GridT::Ptr process(bool threaded=true)
Definition: GridOperators.h:804
ScalarGridType::template ValueConverter< VectorValueT >::Type Type
Definition: GridOperators.h:67
Cpt(const InGridType &grid, const MaskGridType &mask, InterruptT *interrupt=NULL)
Definition: GridOperators.h:409
GridT InGridType
Definition: GridOperators.h:476
static OutGridType::ValueType result(const MapT &, const AccT &acc, const Coord &xyz)
Definition: GridOperators.h:761
OutGridType::Ptr process(bool threaded=true)
Definition: GridOperators.h:599
GridT::Ptr mOutputGrid
Definition: GridOperators.h:721
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:67
VectorGridType::ValueType::value_type VecComponentValueT
Definition: GridOperators.h:57
GridT InGridType
Definition: GridOperators.h:793
Computes the Gradient of a scalar grid.
Definition: GridOperators.h:585
OutGridType::Ptr mOutputGrid
Definition: GridOperators.h:620
const bool mThreaded
Definition: GridOperators.h:773
const GridT & mInputGrid
Definition: GridOperators.h:840
This class manages a linear array of pointers to a given tree&#39;s leaf nodes, as well as optional auxil...
Definition: LeafManager.h:109
VectorToScalarConverter< InGridT >::Type OutGridType
Definition: GridOperators.h:529
void operator()(const MapT &map)
Definition: GridOperators.h:828
OutGridType::Ptr process(bool threaded=true)
Definition: GridOperators.h:540
Normalize(const GridT &grid, const MaskGridType &mask, InterruptT *interrupt=NULL)
Definition: GridOperators.h:799
GridType::Ptr curl(const GridType &grid, bool threaded, InterruptT *interrupt)
Compute the curl of the given vector-valued grid.
Definition: GridOperators.h:867
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
Curl(const GridT &grid, const MaskGridType &mask, InterruptT *interrupt=NULL)
Definition: GridOperators.h:482
GridType::Ptr laplacian(const GridType &grid, bool threaded, InterruptT *interrupt)
Compute the Laplacian of the given scalar grid.
Definition: GridOperators.h:916
const bool mThreaded
Definition: GridOperators.h:618
MeanCurvature(const GridT &grid, InterruptT *interrupt=NULL)
Definition: GridOperators.h:691
GridT::Ptr process(bool threaded=true)
Definition: GridOperators.h:700
GridT OutGridType
Definition: GridOperators.h:640
const GridT & mInputGrid
Definition: GridOperators.h:676
const GridT & mInputGrid
Definition: GridOperators.h:720
Computes the Divergence of a scalar grid.
Definition: GridOperators.h:525
GridT OutGridType
Definition: GridOperators.h:477
Laplacian(const GridT &grid, const MaskGridType &mask, InterruptT *interrupt=NULL)
Definition: GridOperators.h:646