HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
VelocityFields.h
Go to the documentation of this file.
1 ///////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (c) 2012-2017 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 //
29 ///////////////////////////////////////////////////////////////////////////
30 //
31 ///////////////////////////////////////////////////////////////////////////
32 //
33 /// @author Ken Museth
34 ///
35 /// @file VelocityFields.h
36 ///
37 /// @brief Defines two simple wrapper classes for advection velocity
38 /// fields as well as VelocitySampler and VelocityIntegrator
39 ///
40 ///
41 /// @details DiscreteField wraps a velocity grid and EnrightField is mostly
42 /// intended for debugging (it's an analytical divergence free and
43 /// periodic field). They both share the same API required by the
44 /// LevelSetAdvection class defined in LevelSetAdvect.h. Thus, any
45 /// class with this API should work with LevelSetAdvection.
46 ///
47 /// @warning Note the Field wrapper classes below always assume the velocity
48 /// is represented in the world-frame of reference. For DiscreteField
49 /// this implies the input grid must contain velocities in world
50 /// coordinates.
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.
59 #include <hboost/math/constants/constants.hpp>
60 
61 namespace openvdb {
63 namespace OPENVDB_VERSION_NAME {
64 namespace tools {
65 
66 /// @brief Thin wrapper class for a velocity grid
67 /// @note Consider replacing BoxSampler with StaggeredBoxSampler
68 template <typename VelGridT, typename Interpolator = BoxSampler>
70 {
71 public:
72  typedef typename VelGridT::ValueType VectorType;
73  typedef typename VectorType::ValueType ValueType;
75 
76  DiscreteField(const VelGridT &vel)
77  : mAccessor(vel.tree())
78  , mTransform(&vel.transform())
79  {
80  }
81 
82  /// @brief Copy constructor
84  : mAccessor(other.mAccessor.tree())
85  , mTransform(other.mTransform)
86  {
87  }
88 
89  /// @return const reference to the transform between world and index space
90  /// @note Use this method to determine if a client grid is
91  /// aligned with the coordinate space of the velocity grid.
92  const math::Transform& transform() const { return *mTransform; }
93 
94  /// @return the interpolated velocity at the world space position xyz
95  ///
96  /// @warning Not threadsafe since it uses a ValueAccessor! So use
97  /// one instance per thread (which is fine since its lightweight).
98  inline VectorType operator() (const Vec3d& xyz, ValueType/*dummy time*/) const
99  {
100  return Interpolator::sample(mAccessor, mTransform->worldToIndex(xyz));
101  }
102 
103  /// @return the velocity at the coordinate space position ijk
104  ///
105  /// @warning Not threadsafe since it uses a ValueAccessor! So use
106  /// one instance per thread (which is fine since its lightweight).
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 
118 ///////////////////////////////////////////////////////////////////////
119 
120 /// @brief Analytical, divergence-free and periodic velocity field
121 /// @note Primarily intended for debugging!
122 /// @warning This analytical velocity only produce meaningful values
123 /// in the unit box in world space. In other words make sure any level
124 /// set surface is fully enclosed in the axis aligned bounding box
125 /// spanning 0->1 in world units.
126 template <typename ScalarT = float>
128 {
129 public:
130  typedef ScalarT ValueType;
133 
135 
136  /// @return const reference to the identity transform between world and index space
137  /// @note Use this method to determine if a client grid is
138  /// aligned with the coordinate space of this velocity field
140 
141  /// @return the velocity in world units, evaluated at the world
142  /// position xyz and at the specified time
143  inline VectorType operator() (const Vec3d& xyz, ValueType time) const;
144 
145  /// @return the velocity at the coordinate space position ijk
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>
155 {
156  const ScalarT pi = hboost::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 
170 ///////////////////////////////////////////////////////////////////////
171 
172 /// Class to hold a Vec3 field interpreted as a velocity field.
173 /// Primarily exists to provide a method(s) that integrate a passive
174 /// point forward in the velocity field for a single time-step (dt)
175 template<typename GridT = Vec3fGrid,
176  bool Staggered = false,
177  size_t Order = 1>
179 {
180 public:
181  typedef typename GridT::ConstAccessor AccessorType;
182  typedef typename GridT::ValueType ValueType;
183 
184  /// @brief Constructor from a grid
185  VelocitySampler(const GridT& grid):
186  mGrid(&grid),
187  mAcc(grid.getAccessor())
188  {
189  }
190  /// @brief Copy-constructor
192  mGrid(other.mGrid),
193  mAcc(mGrid->getAccessor())
194  {
195  }
196  /// @brief Samples the velocity at world position onto result. Supports both
197  /// staggered (i.e. MAC) and collocated velocity grids.
198  ///
199  /// @return @c true if any one of the sampled values is active.
200  ///
201  /// @warning Not threadsafe since it uses a ValueAccessor! So use
202  /// one instance per thread (which is fine since its lightweight).
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 
211  /// @brief Samples the velocity at world position onto result. Supports both
212  /// staggered (i.e. MAC) and co-located velocity grids.
213  ///
214  /// @warning Not threadsafe since it uses a ValueAccessor! So use
215  /// one instance per thread (which is fine since its lightweight).
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 
229 ///////////////////////////////////////////////////////////////////////
230 
231 /// @brief Performs Runge-Kutta time integration of variable order in
232 /// a static velocity field.
233 ///
234 /// @note Note that the order of the velocity sampling is controlled
235 /// with the SampleOrder template parameter, which defaults
236 /// to one, i.e. a tri-linear interpolation kernel.
237 template<typename GridT = Vec3fGrid,
238  bool Staggered = false,
239  size_t SampleOrder = 1>
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  }
250  /// @brief Variable order Runge-Kutta time integration for a single time step
251  ///
252  /// @param dt Time sub-step for the Runge-Kutte integrator of order OrderRK
253  /// @param world Location in world space coordinates (both input and output)
254  template<size_t OrderRK, typename LocationType>
255  inline void rungeKutta(const ElementType dt, LocationType& world) const
256  {
257  HBOOST_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:
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-2017 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/ )
void rungeKutta(const ElementType dt, LocationType &world) const
Variable order Runge-Kutta time integration for a single time step.
VectorType operator()(const Vec3d &xyz, ValueType) const
HBOOST_STATIC_ASSERT(hboost::is_floating_point< ScalarT >::value)
DiscreteField(const DiscreteField &other)
Copy constructor.
math::Vec3< Real > Vec3R
Definition: Types.h:75
Type Pow2(Type x)
Return .
Definition: Math.h:514
static bool sample(const TreeT &inTree, const Vec3R &inCoord, typename TreeT::ValueType &result)
Sample inTree at the floating-point index coordinate inCoord and store the result in result...
Thin wrapper class for a velocity grid.
GLboolean GLboolean GLboolean GLboolean a
Definition: glcorearb.h:1221
float Cos(const float &x)
Return .
Definition: Math.h:691
VelocitySampler(const GridT &grid)
Constructor from a grid.
VectorType operator()(const Vec3d &xyz, ValueType time) const
Calculate an axis-aligned bounding box in index space from a bounding sphere in world space...
Definition: Transform.h:66
#define OPENVDB_VERSION_NAME
Definition: version.h:43
Performs Runge-Kutta time integration of variable order in a static velocity field.
HBOOST_STATIC_ASSERT(hboost::is_floating_point< ValueType >::value)
VelocitySampler(const VelocitySampler &other)
Copy-constructor.
GLboolean GLboolean GLboolean b
Definition: glcorearb.h:1221
ValueType sample(const LocationType &world) const
Samples the velocity at world position onto result. Supports both staggered (i.e. MAC) and co-located...
GLsizei const GLfloat * value
Definition: glcorearb.h:823
float Sin(const float &x)
Return .
Definition: Math.h:682
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:71
Vec3d worldToIndex(const Vec3d &xyz) const
Apply this transformation to the given coordinates.
Definition: Transform.h:137
bool sample(const LocationType &world, ValueType &result) const
Samples the velocity at world position onto result. Supports both staggered (i.e. MAC) and collocated...
Analytical, divergence-free and periodic velocity field.
const math::Transform & transform() const