OpenVDB: VelocityFields.h Source File

OpenVDB 8.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
34 namespace openvdb {
35 OPENVDB_USE_VERSION_NAMESPACE
36 namespace OPENVDB_VERSION_NAME {
37 namespace tools {
38
41 template <typename VelGridT, typename Interpolator = BoxSampler>
42 class DiscreteField
43 {
44 public:
45 typedef typename VelGridT::ValueType VectorType;
46 typedef typename VectorType::ValueType ValueType;
47 static_assert(std::is_floating_point<ValueType>::value,
48 "DiscreteField requires a floating point grid.");
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 static_assert(std::is_floating_point<ScalarT>::value,
107 "EnrightField requires a floating point grid.");
108
109 EnrightField() {}
110
114 math::Transform transform() const { return math::Transform(); }
115
118 inline VectorType operator() (const Vec3d& xyz, ValueType time) const;
119
121 inline VectorType operator() (const Coord& ijk, ValueType time) const
122 {
123 return (*this)(ijk.asVec3d(), time);
124 }
125 }; // end of EnrightField
126
127 template <typename ScalarT>
128 inline math::Vec3<ScalarT>
129 EnrightField<ScalarT>::operator() (const Vec3d& xyz, ValueType time) const
130 {
131 const ScalarT pi = math::pi<ScalarT>();
132 const ScalarT phase = pi / ScalarT(3);
133 const ScalarT Px = pi * ScalarT(xyz[0]), Py = pi * ScalarT(xyz[1]), Pz = pi * ScalarT(xyz[2]);
134 const ScalarT tr = math::Cos(ScalarT(time) * phase);
135 const ScalarT a = math::Sin(ScalarT(2)*Py);
136 const ScalarT b = -math::Sin(ScalarT(2)*Px);
137 const ScalarT c = math::Sin(ScalarT(2)*Pz);
138 return math::Vec3<ScalarT>(
139 tr * ( ScalarT(2) * math::Pow2(math::Sin(Px)) * a * c ),
140 tr * ( b * math::Pow2(math::Sin(Py)) * c ),
141 tr * ( b * a * math::Pow2(math::Sin(Pz)) ));
142 }
143
144
146
150 template<typename GridT = Vec3fGrid,
151 bool Staggered = false,
152 size_t Order = 1>
153 class VelocitySampler
154 {
155 public:
156 typedef typename GridT::ConstAccessor AccessorType;
157 typedef typename GridT::ValueType ValueType;
158
160 VelocitySampler(const GridT& grid):
161 mGrid(&grid),
162 mAcc(grid.getAccessor())
163 {
164 }
166 VelocitySampler(const VelocitySampler& other):
167 mGrid(other.mGrid),
168 mAcc(mGrid->getAccessor())
169 {
170 }
178 template <typename LocationType>
179 inline bool sample(const LocationType& world, ValueType& result) const
180 {
181 const Vec3R xyz = mGrid->worldToIndex(Vec3R(world[0], world[1], world[2]));
182 bool active = Sampler<Order, Staggered>::sample(mAcc, xyz, result);
183 return active;
184 }
185
191 template <typename LocationType>
192 inline ValueType sample(const LocationType& world) const
193 {
194 const Vec3R xyz = mGrid->worldToIndex(Vec3R(world[0], world[1], world[2]));
195 return Sampler<Order, Staggered>::sample(mAcc, xyz);
196 }
197
198 private:
199 // holding the Grids for the transforms
200 const GridT* mGrid; // Velocity vector field
201 AccessorType mAcc;
202 };// end of VelocitySampler class
203
205
212 template<typename GridT = Vec3fGrid,
213 bool Staggered = false,
214 size_t SampleOrder = 1>
215 class VelocityIntegrator
216 {
217 public:
218 typedef typename GridT::ValueType VecType;
219 typedef typename VecType::ValueType ElementType;
220
221 VelocityIntegrator(const GridT& velGrid):
222 mVelSampler(velGrid)
223 {
224 }
229 template<size_t OrderRK, typename LocationType>
230 inline void rungeKutta(const ElementType dt, LocationType& world) const
231 {
232 BOOST_STATIC_ASSERT(OrderRK <= 4);
233 VecType P(static_cast<ElementType>(world[0]),
234 static_cast<ElementType>(world[1]),
235 static_cast<ElementType>(world[2]));
236 // Note the if-branching below is optimized away at compile time
237 if (OrderRK == 0) {
238 return;// do nothing
239 } else if (OrderRK == 1) {
240 VecType V0;
241 mVelSampler.sample(P, V0);
242 P = dt * V0;
243 } else if (OrderRK == 2) {
244 VecType V0, V1;
245 mVelSampler.sample(P, V0);
246 mVelSampler.sample(P + ElementType(0.5) * dt * V0, V1);
247 P = dt * V1;
248 } else if (OrderRK == 3) {
249 VecType V0, V1, V2;
250 mVelSampler.sample(P, V0);
251 mVelSampler.sample(P + ElementType(0.5) * dt * V0, V1);
252 mVelSampler.sample(P + dt * (ElementType(2.0) * V1 - V0), V2);
253 P = dt * (V0 + ElementType(4.0) * V1 + V2) * ElementType(1.0 / 6.0);
254 } else if (OrderRK == 4) {
255 VecType V0, V1, V2, V3;
256 mVelSampler.sample(P, V0);
257 mVelSampler.sample(P + ElementType(0.5) * dt * V0, V1);
258 mVelSampler.sample(P + ElementType(0.5) * dt * V1, V2);
259 mVelSampler.sample(P + dt * V2, V3);
260 P = dt * (V0 + ElementType(2.0) * (V1 + V2) + V3) * ElementType(1.0 / 6.0);
261 }
262 typedef typename LocationType::ValueType OutType;
263 world += LocationType(static_cast<OutType>(P[0]),
264 static_cast<OutType>(P[1]),
265 static_cast<OutType>(P[2]));
266 }
267 private:
268 VelocitySampler<GridT, Staggered, SampleOrder> mVelSampler;
269 };// end of VelocityIntegrator class
270
271
272 } // namespace tools
273 } // namespace OPENVDB_VERSION_NAME
274 } // namespace openvdb
275
276 #endif // OPENVDB_TOOLS_VELOCITY_FIELDS_HAS_BEEN_INCLUDED
openvdb::v8_2::math::pi
constexpr T pi()
Pi constant taken from Boost to match old behaviour.
Definition: Math.h:118
openvdb::v8_2::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:192
openvdb::v8_2::math::Coord
Signed (x, y, z) 32-bit integer coordinates.
Definition: Coord.h:26
openvdb::v8_2::tools::EnrightField::EnrightField
EnrightField()
Definition: VelocityFields.h:109
openvdb::v8_2::tools::Sampler
Provises a unified interface for sampling, i.e. interpolation.
Definition: Interpolation.h:64
openvdb::v8_2::tools::DiscreteField::DiscreteField
DiscreteField(const VelGridT &vel)
Definition: VelocityFields.h:50
openvdb::v8_2::tools::DiscreteField::DiscreteField
DiscreteField(const DiscreteField &other)
Copy constructor.
Definition: VelocityFields.h:57
openvdb::v8_2::tools::VelocityIntegrator::VelocityIntegrator
VelocityIntegrator(const GridT &velGrid)
Definition: VelocityFields.h:221
openvdb::v8_2::tools::VelocitySampler::ValueType
GridT::ValueType ValueType
Definition: VelocityFields.h:157
openvdb::v8_2::math::Cos
float Cos(const float &x)
Return cos x.
Definition: Math.h:728
openvdb::v8_2::tools::DiscreteField::ValueType
VectorType::ValueType ValueType
Definition: VelocityFields.h:46
Platform.h
openvdb::v8_2::tools::VelocityIntegrator::VecType
GridT::ValueType VecType
Definition: VelocityFields.h:218
openvdb::v8_2::tools::DiscreteField
Thin wrapper class for a velocity grid.
Definition: VelocityFields.h:43
openvdb::v8_2::tools::DiscreteField::VectorType
VelGridT::ValueType VectorType
Definition: VelocityFields.h:45
openvdb::v8_2::tools::VelocitySampler::AccessorType
GridT::ConstAccessor AccessorType
Definition: VelocityFields.h:156
openvdb::v8_2::math::Vec3
Definition: Vec3.h:24
FiniteDifference.h
openvdb::v8_2::tools::VelocitySampler
Definition: VelocityFields.h:154
openvdb::v8_2::tools::VelocityIntegrator
Performs Runge-Kutta time integration of variable order in a static velocity field.
Definition: VelocityFields.h:216
openvdb::v8_2::tools::DiscreteField::transform
const math::Transform & transform() const
Definition: VelocityFields.h:66
openvdb::v8_2::tools::EnrightField::ValueType
ScalarT ValueType
Definition: VelocityFields.h:104
openvdb::v8_2::tools::VelocitySampler::VelocitySampler
VelocitySampler(const GridT &grid)
Constructor from a grid.
Definition: VelocityFields.h:160
Interpolation.h
openvdb::v8_2::Vec3R
math::Vec3< Real > Vec3R
Definition: openvdb/Types.h:50
OPENVDB_USE_VERSION_NAMESPACE
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:153
openvdb::v8_2::math::Pow2
Type Pow2(Type x)
Return x2.
Definition: Math.h:551
openvdb::v8_2::math::Vec3d
Vec3< double > Vec3d
Definition: Vec3.h:668
openvdb::v8_2::tools::EnrightField
Analytical, divergence-free and periodic velocity field.
Definition: VelocityFields.h:102
openvdb::v8_2::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:230
openvdb::v8_2::math::Transform
Definition: Transform.h:40
openvdb::v8_2::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:179
openvdb::v8_2::math::Sin
float Sin(const float &x)
Return sin x.
Definition: Math.h:719
OPENVDB_VERSION_NAME
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h:101
openvdb::v8_2::tools::EnrightField::VectorType
math::Vec3< ScalarT > VectorType
Definition: VelocityFields.h:105
openvdb::v8_2::tools::VelocityIntegrator::ElementType
VecType::ValueType ElementType
Definition: VelocityFields.h:219
openvdb::v8_2::Vec3fGrid
Vec3SGrid Vec3fGrid
Definition: openvdb.h:56
openvdb
Definition: openvdb/Exceptions.h:13
openvdb::v8_2::math::Coord::asVec3d
Vec3d asVec3d() const
Definition: Coord.h:144
openvdb.h
openvdb::v8_2::tools::EnrightField::transform
math::Transform transform() const
Definition: VelocityFields.h:114
openvdb::v8_2::tools::VelocitySampler::VelocitySampler
VelocitySampler(const VelocitySampler &other)
Copy-constructor.
Definition: VelocityFields.h:166