OpenVDB: VelocityFields.h Source File

OpenVDB 7.0.0

VelocityFields.h
Go to the documentation of this file.
1 // Copyright Contributors to the OpenVDB Project
2 // SPDX-License-Identifier: MPL-2.0
3 //
5 //
24
25 #ifndef OPENVDB_TOOLS_VELOCITY_FIELDS_HAS_BEEN_INCLUDED
26 #define OPENVDB_TOOLS_VELOCITY_FIELDS_HAS_BEEN_INCLUDED
27
28 #include <tbb/parallel_reduce.h>
29 #include <openvdb/Platform.h>
30 #include <openvdb/openvdb.h>
31 #include "Interpolation.h" // for Sampler, etc.
32 #include <openvdb/math/FiniteDifference.h>
33 #include <boost/math/constants/constants.hpp>
34
35 namespace openvdb {
36 OPENVDB_USE_VERSION_NAMESPACE
37 namespace OPENVDB_VERSION_NAME {
38 namespace tools {
39
42 template <typename VelGridT, typename Interpolator = BoxSampler>
43 class DiscreteField
44 {
45 public:
46 typedef typename VelGridT::ValueType VectorType;
47 typedef typename VectorType::ValueType ValueType;
48 BOOST_STATIC_ASSERT(boost::is_floating_point<ValueType>::value);
49
50 DiscreteField(const VelGridT &vel)
51 : mAccessor(vel.tree())
52 , mTransform(&vel.transform())
53 {
54 }
55
57 DiscreteField(const DiscreteField& other)
58 : mAccessor(other.mAccessor.tree())
59 , mTransform(other.mTransform)
60 {
61 }
62
66 const math::Transform& transform() const { return *mTransform; }
67
72 inline VectorType operator() (const Vec3d& xyz, ValueType/*dummy time*/) const
73 {
74 return Interpolator::sample(mAccessor, mTransform->worldToIndex(xyz));
75 }
76
81 inline VectorType operator() (const Coord& ijk, ValueType/*dummy time*/) const
82 {
83 return mAccessor.getValue(ijk);
84 }
85
86 private:
87 const typename VelGridT::ConstAccessor mAccessor;//Not thread-safe
88 const math::Transform* mTransform;
89
90 }; // end of DiscreteField
91
93
100 template <typename ScalarT = float>
101 class EnrightField
102 {
103 public:
104 typedef ScalarT ValueType;
105 typedef math::Vec3<ScalarT> VectorType;
106 BOOST_STATIC_ASSERT(boost::is_floating_point<ScalarT>::value);
107
108 EnrightField() {}
109
113 math::Transform transform() const { return math::Transform(); }
114
117 inline VectorType operator() (const Vec3d& xyz, ValueType time) const;
118
120 inline VectorType operator() (const Coord& ijk, ValueType time) const
121 {
122 return (*this)(ijk.asVec3d(), time);
123 }
124 }; // end of EnrightField
125
126 template <typename ScalarT>
127 inline math::Vec3<ScalarT>
128 EnrightField<ScalarT>::operator() (const Vec3d& xyz, ValueType time) const
129 {
130 const ScalarT pi = boost::math::constants::pi<ScalarT>();
131 const ScalarT phase = pi / ScalarT(3);
132 const ScalarT Px = pi * ScalarT(xyz[0]), Py = pi * ScalarT(xyz[1]), Pz = pi * ScalarT(xyz[2]);
133 const ScalarT tr = math::Cos(ScalarT(time) * phase);
134 const ScalarT a = math::Sin(ScalarT(2)*Py);
135 const ScalarT b = -math::Sin(ScalarT(2)*Px);
136 const ScalarT c = math::Sin(ScalarT(2)*Pz);
137 return math::Vec3<ScalarT>(
138 tr * ( ScalarT(2) * math::Pow2(math::Sin(Px)) * a * c ),
139 tr * ( b * math::Pow2(math::Sin(Py)) * c ),
140 tr * ( b * a * math::Pow2(math::Sin(Pz)) ));
141 }
142
143
145
149 template<typename GridT = Vec3fGrid,
150 bool Staggered = false,
151 size_t Order = 1>
152 class VelocitySampler
153 {
154 public:
155 typedef typename GridT::ConstAccessor AccessorType;
156 typedef typename GridT::ValueType ValueType;
157
159 VelocitySampler(const GridT& grid):
160 mGrid(&grid),
161 mAcc(grid.getAccessor())
162 {
163 }
165 VelocitySampler(const VelocitySampler& other):
166 mGrid(other.mGrid),
167 mAcc(mGrid->getAccessor())
168 {
169 }
177 template <typename LocationType>
178 inline bool sample(const LocationType& world, ValueType& result) const
179 {
180 const Vec3R xyz = mGrid->worldToIndex(Vec3R(world[0], world[1], world[2]));
181 bool active = Sampler<Order, Staggered>::sample(mAcc, xyz, result);
182 return active;
183 }
184
190 template <typename LocationType>
191 inline ValueType sample(const LocationType& world) const
192 {
193 const Vec3R xyz = mGrid->worldToIndex(Vec3R(world[0], world[1], world[2]));
194 return Sampler<Order, Staggered>::sample(mAcc, xyz);
195 }
196
197 private:
198 // holding the Grids for the transforms
199 const GridT* mGrid; // Velocity vector field
200 AccessorType mAcc;
201 };// end of VelocitySampler class
202
204
211 template<typename GridT = Vec3fGrid,
212 bool Staggered = false,
213 size_t SampleOrder = 1>
214 class VelocityIntegrator
215 {
216 public:
217 typedef typename GridT::ValueType VecType;
218 typedef typename VecType::ValueType ElementType;
219
220 VelocityIntegrator(const GridT& velGrid):
221 mVelSampler(velGrid)
222 {
223 }
228 template<size_t OrderRK, typename LocationType>
229 inline void rungeKutta(const ElementType dt, LocationType& world) const
230 {
231 BOOST_STATIC_ASSERT(OrderRK <= 4);
232 VecType P(static_cast<ElementType>(world[0]),
233 static_cast<ElementType>(world[1]),
234 static_cast<ElementType>(world[2]));
235 // Note the if-branching below is optimized away at compile time
236 if (OrderRK == 0) {
237 return;// do nothing
238 } else if (OrderRK == 1) {
239 VecType V0;
240 mVelSampler.sample(P, V0);
241 P = dt * V0;
242 } else if (OrderRK == 2) {
243 VecType V0, V1;
244 mVelSampler.sample(P, V0);
245 mVelSampler.sample(P + ElementType(0.5) * dt * V0, V1);
246 P = dt * V1;
247 } else if (OrderRK == 3) {
248 VecType V0, V1, V2;
249 mVelSampler.sample(P, V0);
250 mVelSampler.sample(P + ElementType(0.5) * dt * V0, V1);
251 mVelSampler.sample(P + dt * (ElementType(2.0) * V1 - V0), V2);
252 P = dt * (V0 + ElementType(4.0) * V1 + V2) * ElementType(1.0 / 6.0);
253 } else if (OrderRK == 4) {
254 VecType V0, V1, V2, V3;
255 mVelSampler.sample(P, V0);
256 mVelSampler.sample(P + ElementType(0.5) * dt * V0, V1);
257 mVelSampler.sample(P + ElementType(0.5) * dt * V1, V2);
258 mVelSampler.sample(P + dt * V2, V3);
259 P = dt * (V0 + ElementType(2.0) * (V1 + V2) + V3) * ElementType(1.0 / 6.0);
260 }
261 typedef typename LocationType::ValueType OutType;
262 world += LocationType(static_cast<OutType>(P[0]),
263 static_cast<OutType>(P[1]),
264 static_cast<OutType>(P[2]));
265 }
266 private:
267 VelocitySampler<GridT, Staggered, SampleOrder> mVelSampler;
268 };// end of VelocityIntegrator class
269
270
271 } // namespace tools
272 } // namespace OPENVDB_VERSION_NAME
273 } // namespace openvdb
274
275 #endif // OPENVDB_TOOLS_VELOCITY_FIELDS_HAS_BEEN_INCLUDED
openvdb::v7_0::tools::VelocitySampler::AccessorType
GridT::ConstAccessor AccessorType
Definition: VelocityFields.h:155
openvdb::v7_0::Vec3fGrid
Vec3SGrid Vec3fGrid
Definition: openvdb.h:56
openvdb::v7_0::tools::DiscreteField::VectorType
VelGridT::ValueType VectorType
Definition: VelocityFields.h:46
openvdb::v7_0::Vec3R
math::Vec3< Real > Vec3R
Definition: Types.h:49
openvdb::v7_0::math::Coord
Signed (x, y, z) 32-bit integer coordinates.
Definition: Coord.h:26
openvdb::v7_0::tools::VelocitySampler
Definition: VelocityFields.h:153
openvdb::v7_0::tools::Sampler
Provises a unified interface for sampling, i.e. interpolation.
Definition: Interpolation.h:64
openvdb::v7_0::tools::DiscreteField::transform
const math::Transform & transform() const
Definition: VelocityFields.h:66
openvdb::v7_0::tools::EnrightField::transform
math::Transform transform() const
Definition: VelocityFields.h:113
openvdb::v7_0::tools::VelocityIntegrator::VecType
GridT::ValueType VecType
Definition: VelocityFields.h:217
openvdb::v7_0::tools::VelocityIntegrator
Performs Runge-Kutta time integration of variable order in a static velocity field.
Definition: VelocityFields.h:215
openvdb::v7_0::tools::VelocitySampler::VelocitySampler
VelocitySampler(const VelocitySampler &other)
Copy-constructor.
Definition: VelocityFields.h:165
openvdb::v7_0::tools::VelocityIntegrator::VelocityIntegrator
VelocityIntegrator(const GridT &velGrid)
Definition: VelocityFields.h:220
Platform.h
openvdb::v7_0::math::Cos
float Cos(const float &x)
Return cos x.
Definition: Math.h:672
openvdb::v7_0::tools::VelocitySampler::VelocitySampler
VelocitySampler(const GridT &grid)
Constructor from a grid.
Definition: VelocityFields.h:159
openvdb::v7_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:178
openvdb::v7_0::math::Vec3d
Vec3< double > Vec3d
Definition: Vec3.h:662
openvdb::v7_0::math::Pow2
Type Pow2(Type x)
Return x2.
Definition: Math.h:495
openvdb::v7_0::math::Sin
float Sin(const float &x)
Return sin x.
Definition: Math.h:663
FiniteDifference.h
openvdb::v7_0::tools::EnrightField::ValueType
ScalarT ValueType
Definition: VelocityFields.h:104
openvdb::v7_0::tools::EnrightField::VectorType
math::Vec3< ScalarT > VectorType
Definition: VelocityFields.h:105
openvdb::v7_0::math::Coord::asVec3d
Vec3d asVec3d() const
Definition: Coord.h:144
openvdb::v7_0::tools::DiscreteField::DiscreteField
DiscreteField(const VelGridT &vel)
Definition: VelocityFields.h:50
openvdb::v7_0::tools::EnrightField
Analytical, divergence-free and periodic velocity field.
Definition: VelocityFields.h:102
openvdb::v7_0::tools::DiscreteField::DiscreteField
DiscreteField(const DiscreteField &other)
Copy constructor.
Definition: VelocityFields.h:57
openvdb::v7_0::math::Vec3
Definition: Mat.h:170
Interpolation.h
openvdb::v7_0::tools::VelocitySampler::ValueType
GridT::ValueType ValueType
Definition: VelocityFields.h:156
openvdb::v7_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:229
OPENVDB_USE_VERSION_NAMESPACE
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:154
openvdb::v7_0::tools::DiscreteField::ValueType
VectorType::ValueType ValueType
Definition: VelocityFields.h:47
openvdb::v7_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:191
openvdb::v7_0::tools::DiscreteField::BOOST_STATIC_ASSERT
BOOST_STATIC_ASSERT(boost::is_floating_point< ValueType >::value)
openvdb::v7_0::tools::DiscreteField
Thin wrapper class for a velocity grid.
Definition: VelocityFields.h:44
openvdb::v7_0::tools::VelocityIntegrator::ElementType
VecType::ValueType ElementType
Definition: VelocityFields.h:218
openvdb::v7_0::math::Transform
Definition: Transform.h:40
openvdb::v7_0::tools::EnrightField::EnrightField
EnrightField()
Definition: VelocityFields.h:108
OPENVDB_VERSION_NAME
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h:102
openvdb
Definition: Exceptions.h:13
openvdb::v7_0::tools::EnrightField::BOOST_STATIC_ASSERT
BOOST_STATIC_ASSERT(boost::is_floating_point< ScalarT >::value)
openvdb.h