OpenVDB  0.104.0
Cpt.h
Go to the documentation of this file.
1 
2 //
3 // Copyright (c) 2012 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 //
33 
34 #ifndef OPENVDB_TOOLS_CPT_HAS_BEEN_INCLUDED
35 #define OPENVDB_TOOLS_CPT_HAS_BEEN_INCLUDED
36 
37 #include <openvdb/Grid.h>
38 #include <openvdb/math/Operators.h>
39 #include <tbb/parallel_reduce.h>
40 
41 
42 namespace openvdb {
44 namespace OPENVDB_VERSION_NAME {
45 namespace tools {
46 
54 template<typename InGridType, typename MapType, math::DScheme Scheme>
55 class CptImpl
56 {
57 public:
58  typedef typename InGridType::TreeType InTreeType;
59  typedef typename InGridType::ValueType Real;
61  typedef typename InTreeType::LeafNodeType LeafType;
63  typedef typename InTreeType::template ValueConverter<Vec3Type>::Type OutTreeType;
65 
66  CptImpl(const InGridType& grid, const MapType& map):
67  mInputGrid(&grid),
68  mOutputGrid(OutGridType::create(Vec3Type(0, 0, 0))),
69  mMap(&map)
70  {
71  }
72 
74  CptImpl(const CptImpl& other, tbb::split):
75  mInputGrid(other.mInputGrid),
76  mOutputGrid(OutGridType::create(Vec3Type(0, 0, 0))), // new local grid
77  mMap(other.mMap),
78  mUseWorldTransform(other.mUseWorldTransform)
79  {
80  }
81 
82  ~CptImpl() {}
83 
84  typename OutGridType::Ptr process(bool threaded = true, bool useWorldTransform = true)
85  {
86  mUseWorldTransform = useWorldTransform;
87 
88  RangeType range(mInputGrid->tree().cbeginLeaf());
89  if (threaded) {
90  tbb::parallel_reduce(range, *this);
91  } else {
92  (*this)(range);
93  }
94  return mOutputGrid;
95  }
96 
97  void operator()(RangeType& range)
98  {
99  const bool useWorldTransform = mUseWorldTransform;
100  typename InGridType::ConstAccessor inAccessor = mInputGrid->getConstAccessor();
101  typename OutGridType::Accessor outAccessor = mOutputGrid->getAccessor();
102 
103 
104  if (useWorldTransform) { // the results are recorded in world space
105  for ( ; range; ++range) {
106  for (typename LeafType::ValueOnCIter v = range.iterator()->beginValueOn(); v; ++v) {
107  const Coord ijk = v.getCoord();
108  Vec3Type cpt = math::CPT_RANGE<MapType, Scheme>::result(*mMap, inAccessor, ijk);
109  outAccessor.setValue(ijk, cpt);
110 
111  }
112  }
113  } else { // the results are recorded in index space
114  for ( ; range; ++range) {
115  for (typename LeafType::ValueOnCIter v = range.iterator()->beginValueOn(); v; ++v) {
116  const Coord ijk = v.getCoord();
117  Vec3Type cpt = math::CPT<MapType, Scheme>::result(*mMap, inAccessor, ijk);
118  outAccessor.setValue(ijk, cpt);
119  }
120  }
121  }
122  }
123 
124  void join(const CptImpl& other) { mOutputGrid->merge(*other.mOutputGrid); }
125 
126 protected:
127  const InGridType* mInputGrid;
129  const MapType* mMap;
131 }; // end of CptImp class
132 
133 
134 template<typename InGridT>
136 {
137 public:
138  typedef InGridT InGridType;
139  typedef typename InGridT::ValueType Real;
140  typedef typename InGridT::TreeType InTreeType;
142  typedef typename InTreeType::template ValueConverter<Vec3Type>::Type OutTreeType;
144 
145  CptFunctor(const InGridType& grid, bool threaded, bool worldspace):
146  mInputGrid(&grid), mThreaded(threaded), mWorldSpace(worldspace)
147  {
148  }
149 
150  template<typename MapType>
151  void operator()(const MapType& map)
152  {
153  CptImpl<InGridType, MapType, math::CD_2ND> cptImpl(*mInputGrid, map);
154  mOutputGrid = cptImpl.process(mThreaded, mWorldSpace); // cache the result
155  }
156 
157  // for output of the result
158  typename OutGridType::Ptr result() { return mOutputGrid; }
159 
160 private:
161  const InGridType* mInputGrid;
162  typename OutGridType::Ptr mOutputGrid;
163  bool mThreaded;
164  bool mWorldSpace;
165 };
166 
167 
175 template<typename InGridType>
176 class Cpt
177 {
178 public:
179  typedef typename InGridType::TreeType InTreeType;
180  typedef typename InGridType::ValueType Real;
182  typedef typename InTreeType::template ValueConverter<Vec3Type>::Type OutTreeType;
184 
185  Cpt(const InGridType& grid): mInputGrid(&grid) {}
186  ~Cpt() {}
187 
188  typename OutGridType::Ptr process(bool threaded = true, bool useWorldTransform = true)
189  {
190  CptFunctor<InGridType> cptFunctor(*mInputGrid, threaded, useWorldTransform);
191  processTypedMap(mInputGrid->transform(), cptFunctor);
192  return cptFunctor.result();
193  }
194 
195 protected:
196  const InGridType* mInputGrid;
197 }; // end of Cpt class
198 
199 } // namespace tools
200 } // namespace OPENVDB_VERSION_NAME
201 } // namespace openvdb
202 
203 #endif // OPENVDB_TOOLS_CPT_HAS_BEEN_INCLUDED
204 
205 // Copyright (c) 2012 DreamWorks Animation LLC
206 // All rights reserved. This software is distributed under the
207 // Mozilla Public License 2.0 ( http://www.mozilla.org/MPL/2.0/ )