OpenVDB: VelocityFields.h Source File

OpenVDB 5.1.0

VelocityFields.h
Go to the documentation of this file.
1 //
3 // Copyright (c) 2012-2018 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 //
32 //
51
52 #ifndef OPENVDB_TOOLS_VELOCITY_FIELDS_HAS_BEEN_INCLUDED
53 #define OPENVDB_TOOLS_VELOCITY_FIELDS_HAS_BEEN_INCLUDED
54
55 #include <tbb/parallel_reduce.h>
56 #include <openvdb/Platform.h>
57 #include <openvdb/openvdb.h>
58 #include "Interpolation.h" // for Sampler, etc.
59 #include <openvdb/math/FiniteDifference.h>
60 #include <boost/math/constants/constants.hpp>
61
62 namespace openvdb {
63 OPENVDB_USE_VERSION_NAMESPACE
64 namespace OPENVDB_VERSION_NAME {
65 namespace tools {
66
69 template <typename VelGridT, typename Interpolator = BoxSampler>
70 class DiscreteField
71 {
72 public:
73 typedef typename VelGridT::ValueType VectorType;
74 typedef typename VectorType::ValueType ValueType;
75 BOOST_STATIC_ASSERT(boost::is_floating_point<ValueType>::value);
76
77 DiscreteField(const VelGridT &vel)
78 : mAccessor(vel.tree())
79 , mTransform(&vel.transform())
80 {
81 }
82
84 DiscreteField(const DiscreteField& other)
85 : mAccessor(other.mAccessor.tree())
86 , mTransform(other.mTransform)
87 {
88 }
89
93 const math::Transform& transform() const { return *mTransform; }
94
99 inline VectorType operator() (const Vec3d& xyz, ValueType/*dummy time*/) const
100 {
101 return Interpolator::sample(mAccessor, mTransform->worldToIndex(xyz));
102 }
103
108 inline VectorType operator() (const Coord& ijk, ValueType/*dummy time*/) const
109 {
110 return mAccessor.getValue(ijk);
111 }
112
113 private:
114 const typename VelGridT::ConstAccessor mAccessor;//Not thread-safe
115 const math::Transform* mTransform;
116
117 }; // end of DiscreteField
118
120
127 template <typename ScalarT = float>
128 class EnrightField
129 {
130 public:
131 typedef ScalarT ValueType;
132 typedef math::Vec3<ScalarT> VectorType;
133 BOOST_STATIC_ASSERT(boost::is_floating_point<ScalarT>::value);
134
135 EnrightField() {}
136
140 math::Transform transform() const { return math::Transform(); }
141
144 inline VectorType operator() (const Vec3d& xyz, ValueType time) const;
145
147 inline VectorType operator() (const Coord& ijk, ValueType time) const
148 {
149 return (*this)(ijk.asVec3d(), time);
150 }
151 }; // end of EnrightField
152
153 template <typename ScalarT>
154 inline math::Vec3<ScalarT>
155 EnrightField<ScalarT>::operator() (const Vec3d& xyz, ValueType time) const
156 {
157 const ScalarT pi = boost::math::constants::pi<ScalarT>();
158 const ScalarT phase = pi / ScalarT(3);
159 const ScalarT Px = pi * ScalarT(xyz[0]), Py = pi * ScalarT(xyz[1]), Pz = pi * ScalarT(xyz[2]);
160 const ScalarT tr = math::Cos(ScalarT(time) * phase);
161 const ScalarT a = math::Sin(ScalarT(2)*Py);
162 const ScalarT b = -math::Sin(ScalarT(2)*Px);
163 const ScalarT c = math::Sin(ScalarT(2)*Pz);
164 return math::Vec3<ScalarT>(
165 tr * ( ScalarT(2) * math::Pow2(math::Sin(Px)) * a * c ),
166 tr * ( b * math::Pow2(math::Sin(Py)) * c ),
167 tr * ( b * a * math::Pow2(math::Sin(Pz)) ));
168 }
169
170
172
176 template<typename GridT = Vec3fGrid,
177 bool Staggered = false,
178 size_t Order = 1>
179 class VelocitySampler
180 {
181 public:
182 typedef typename GridT::ConstAccessor AccessorType;
183 typedef typename GridT::ValueType ValueType;
184
186 VelocitySampler(const GridT& grid):
187 mGrid(&grid),
188 mAcc(grid.getAccessor())
189 {
190 }
192 VelocitySampler(const VelocitySampler& other):
193 mGrid(other.mGrid),
194 mAcc(mGrid->getAccessor())
195 {
196 }
204 template <typename LocationType>
205 inline bool sample(const LocationType& world, ValueType& result) const
206 {
207 const Vec3R xyz = mGrid->worldToIndex(Vec3R(world[0], world[1], world[2]));
208 bool active = Sampler<Order, Staggered>::sample(mAcc, xyz, result);
209 return active;
210 }
211
217 template <typename LocationType>
218 inline ValueType sample(const LocationType& world) const
219 {
220 const Vec3R xyz = mGrid->worldToIndex(Vec3R(world[0], world[1], world[2]));
221 return Sampler<Order, Staggered>::sample(mAcc, xyz);
222 }
223
224 private:
225 // holding the Grids for the transforms
226 const GridT* mGrid; // Velocity vector field
227 AccessorType mAcc;
228 };// end of VelocitySampler class
229
231
238 template<typename GridT = Vec3fGrid,
239 bool Staggered = false,
240 size_t SampleOrder = 1>
241 class VelocityIntegrator
242 {
243 public:
244 typedef typename GridT::ValueType VecType;
245 typedef typename VecType::ValueType ElementType;
246
247 VelocityIntegrator(const GridT& velGrid):
248 mVelSampler(velGrid)
249 {
250 }
255 template<size_t OrderRK, typename LocationType>
256 inline void rungeKutta(const ElementType dt, LocationType& world) const
257 {
258 BOOST_STATIC_ASSERT(OrderRK <= 4);
259 VecType P(static_cast<ElementType>(world[0]),
260 static_cast<ElementType>(world[1]),
261 static_cast<ElementType>(world[2]));
262 // Note the if-branching below is optimized away at compile time
263 if (OrderRK == 0) {
264 return;// do nothing
265 } else if (OrderRK == 1) {
266 VecType V0;
267 mVelSampler.sample(P, V0);
268 P = dt * V0;
269 } else if (OrderRK == 2) {
270 VecType V0, V1;
271 mVelSampler.sample(P, V0);
272 mVelSampler.sample(P + ElementType(0.5) * dt * V0, V1);
273 P = dt * V1;
274 } else if (OrderRK == 3) {
275 VecType V0, V1, V2;
276 mVelSampler.sample(P, V0);
277 mVelSampler.sample(P + ElementType(0.5) * dt * V0, V1);
278 mVelSampler.sample(P + dt * (ElementType(2.0) * V1 - V0), V2);
279 P = dt * (V0 + ElementType(4.0) * V1 + V2) * ElementType(1.0 / 6.0);
280 } else if (OrderRK == 4) {
281 VecType V0, V1, V2, V3;
282 mVelSampler.sample(P, V0);
283 mVelSampler.sample(P + ElementType(0.5) * dt * V0, V1);
284 mVelSampler.sample(P + ElementType(0.5) * dt * V1, V2);
285 mVelSampler.sample(P + dt * V2, V3);
286 P = dt * (V0 + ElementType(2.0) * (V1 + V2) + V3) * ElementType(1.0 / 6.0);
287 }
288 typedef typename LocationType::ValueType OutType;
289 world += LocationType(static_cast<OutType>(P[0]),
290 static_cast<OutType>(P[1]),
291 static_cast<OutType>(P[2]));
292 }
293 private:
294 VelocitySampler<GridT, Staggered, SampleOrder> mVelSampler;
295 };// end of VelocityIntegrator class
296
297
298 } // namespace tools
299 } // namespace OPENVDB_VERSION_NAME
300 } // namespace openvdb
301
302 #endif // OPENVDB_TOOLS_VELOCITY_FIELDS_HAS_BEEN_INCLUDED
303
304 // Copyright (c) 2012-2018 DreamWorks Animation LLC
305 // All rights reserved. This software is distributed under the
306 // Mozilla Public License 2.0 ( http://www.mozilla.org/MPL/2.0/ )
FiniteDifference.h
openvdb::v5_1::tools::VelocityIntegrator
Performs Runge-Kutta time integration of variable order in a static velocity field.
Definition: VelocityFields.h:241
openvdb::v5_1::tools::VelocityIntegrator::rungeKutta
void rungeKutta(const ElementType dt, LocationType &world) const
Variable order Runge-Kutta time integration for a single time step.
Definition: VelocityFields.h:256
openvdb::v5_1::tools::DiscreteField
Thin wrapper class for a velocity grid.
Definition: VelocityFields.h:70
openvdb::v5_1::math::Cos
float Cos(const float &x)
Return cos x.
Definition: Math.h:679
openvdb::v5_1::tools::VelocitySampler::VelocitySampler
VelocitySampler(const VelocitySampler &other)
Copy-constructor.
Definition: VelocityFields.h:192
openvdb.h
openvdb::v5_1::tools::VelocitySampler::sample
ValueType sample(const LocationType &world) const
Samples the velocity at world position onto result. Supports both staggered (i.e. MAC) and co-located...
Definition: VelocityFields.h:218
openvdb::v5_1::tools::EnrightField
Analytical, divergence-free and periodic velocity field.
Definition: VelocityFields.h:128
openvdb::v5_1::tools::DiscreteField::VectorType
VelGridT::ValueType VectorType
Definition: VelocityFields.h:73
openvdb::v5_1::tools::DiscreteField::ValueType
VectorType::ValueType ValueType
Definition: VelocityFields.h:74
openvdb::v5_1::math::Vec3
Definition: Mat.h:197
openvdb::v5_1::math::Pow2
Type Pow2(Type x)
Return x2.
Definition: Math.h:502
openvdb::v5_1::tools::EnrightField::EnrightField
EnrightField()
Definition: VelocityFields.h:135
openvdb::v5_1::tools::Sampler
Provises a unified interface for sampling, i.e. interpolation.
Definition: Interpolation.h:90
openvdb::v5_1::tools::VelocitySampler::ValueType
GridT::ValueType ValueType
Definition: VelocityFields.h:183
openvdb::v5_1::math::Sin
float Sin(const float &x)
Return sin x.
Definition: Math.h:670
openvdb::v5_1::tools::EnrightField::transform
math::Transform transform() const
Definition: VelocityFields.h:140
openvdb::v5_1::tools::EnrightField::VectorType
math::Vec3< ScalarT > VectorType
Definition: VelocityFields.h:132
openvdb::v5_1::tools::VelocitySampler::VelocitySampler
VelocitySampler(const GridT &grid)
Constructor from a grid.
Definition: VelocityFields.h:186
OPENVDB_VERSION_NAME
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h:136
openvdb::v5_1::tools::VelocitySampler::AccessorType
GridT::ConstAccessor AccessorType
Definition: VelocityFields.h:182
openvdb::v5_1::Vec3fGrid
Vec3SGrid Vec3fGrid
Definition: openvdb.h:83
openvdb::v5_1::math::Transform
Definition: Transform.h:66
openvdb::v5_1::math::Vec3d
Vec3< double > Vec3d
Definition: Vec3.h:679
openvdb
Definition: Exceptions.h:40
openvdb::v5_1::tools::VelocityIntegrator::VecType
GridT::ValueType VecType
Definition: VelocityFields.h:244
openvdb::v5_1::tools::DiscreteField::DiscreteField
DiscreteField(const DiscreteField &other)
Copy constructor.
Definition: VelocityFields.h:84
openvdb::v5_1::tools::VelocityIntegrator::ElementType
VecType::ValueType ElementType
Definition: VelocityFields.h:245
openvdb::v5_1::tools::VelocitySampler::sample
bool sample(const LocationType &world, ValueType &result) const
Samples the velocity at world position onto result. Supports both staggered (i.e. MAC) and collocated...
Definition: VelocityFields.h:205
openvdb::v5_1::tools::DiscreteField::transform
const math::Transform & transform() const
Definition: VelocityFields.h:93
openvdb::v5_1::Vec3R
math::Vec3< Real > Vec3R
Definition: Types.h:79
openvdb::v5_1::tools::VelocitySampler
Definition: VelocityFields.h:179
openvdb::v5_1::tools::DiscreteField::DiscreteField
DiscreteField(const VelGridT &vel)
Definition: VelocityFields.h:77
Interpolation.h
openvdb::v5_1::math::Coord
Signed (x, y, z) 32-bit integer coordinates.
Definition: Coord.h:51
openvdb::v5_1::tools::VelocityIntegrator::VelocityIntegrator
VelocityIntegrator(const GridT &velGrid)
Definition: VelocityFields.h:247
OPENVDB_USE_VERSION_NAMESPACE
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:188
openvdb::v5_1::math::Coord::asVec3d
Vec3d asVec3d() const
Definition: Coord.h:170
openvdb::v5_1::tools::EnrightField::ValueType
ScalarT ValueType
Definition: VelocityFields.h:131
Platform.h