OpenVDB 3.2.0

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