HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
CL_Jiggle.h
Go to the documentation of this file.
1 /*
2  * PROPRIETARY INFORMATION. This software is proprietary to
3  * Side Effects Software Inc., and is not to be reproduced,
4  * transmitted, or disclosed in any way without written permission.
5  *
6  * NAME: CL_Jiggle.h ( Clip Library, C++)
7  *
8  * COMMENTS:
9  * Applies a jiggle filter to input data
10  */
11 
12 
13 #ifndef __CL_Jiggle__
14 #define __CL_Jiggle__
15 
16 #include "CL_API.h"
17 #include <SYS/SYS_Math.h>
18 #include <SYS/SYS_Types.h>
19 #include <UT/UT_DMatrix4.h>
20 #include <UT/UT_Vector3.h>
21 
22 #include <algorithm>
23 
24 class UT_Interrupt;
25 
26 class CL_Jiggle
27 {
28 public:
29  /// Solve for a jiggle filter given input data as an array of UT_Vector3D
30  /// data and result must both be of length size
31  template <typename T, typename F>
32  static void solve(const UT_Vector3T<T> *data, UT_Vector3T<T> *result, int size,
33  fpreal spring_constant, fpreal damping_constant,
34  fpreal limit, UT_Vector3T<T> multiplier,
35  const F& interrupt);
36 
37  // Overload for no-interrupt version
38  template <typename T>
39  static void solve(const UT_Vector3T<T> *data, UT_Vector3T<T> *result, int size,
40  fpreal spring_constant, fpreal damping_constant,
41  fpreal limit, UT_Vector3T<T> multiplier)
42  {
43  solve(data, result, size,
44  spring_constant, damping_constant,
45  limit, multiplier, []() { return false; });
46  }
47 
48  /// Solve for a jiggle filter given input/output data as three separate arrays
49  /// for x,y,z components
50  /// data and result must both be of length size
51  template <typename T, typename F>
52  static void solve(const fpreal *data_x, const fpreal *data_y, const fpreal *data_z,
53  fpreal *result_x, fpreal *result_y, fpreal *result_z, int size,
54  fpreal spring_constant, fpreal damping_constant,
55  fpreal limit, UT_Vector3T<T> multiplier,
56  const F& interrupt, const UT_DMatrix4 *ref_xform = nullptr);
57 
58  // Overload for no-interrupt version
59  template <typename T, typename F>
60  static void solve(const fpreal *data_x, const fpreal *data_y, const fpreal *data_z,
61  fpreal *result_x, fpreal *result_y, fpreal *result_z, int size,
62  fpreal spring_constant, fpreal damping_constant,
63  fpreal limit, UT_Vector3T<T> multiplier,
64  const UT_DMatrix4 *ref_xform = nullptr)
65  {
66  solve(data_x, data_y, data_z, result_x, result_y, result_z, size,
67  spring_constant, damping_constant, limit, multiplier,
68  [](){ return false; }, ref_xform);
69  }
70 
71 private:
72  template<typename T>
73  static UT_Vector3D spring(const UT_Vector3T<T> &current, fpreal spring, fpreal decay,
74  const UT_Vector3T<T> &last, const UT_Vector3T<T> &vel)
75  {
76  return ((vel + (current - last) * spring) * (1.0 - decay));
77  }
78 
79  template<typename T>
80  static UT_Vector3T<T> simulate(UT_Vector3T<T> goal, fpreal k, fpreal d, fpreal limit,
81  const UT_Vector3T<T> &multiplier, const UT_DMatrix4 *ref_xform,
83  {
84  UT_Vector3T<T> curr, last, vel, newvel, newpos, diff, local_pos;
85  fpreal len, len2, limit2, f;
86 
87  curr = goal;
88  last = position;
89  vel = velocity;
90 
91  newvel = spring(curr, k, d, last, vel);
92  newpos = last + (newvel * 0.5);
93 
94  newvel = spring(curr, k, d, newpos, vel);
95  vel = newvel;
96  last += vel;
97 
98  diff = (curr - last);
99 
100  len = diff.length2();
101  limit2 = limit * limit;
102 
103  if(len > limit2)
104  {
105  len = sqrt(len);
106  len2 = 2.0 * limit - limit2 / len;
107  f = 1.0 - len2/len;
108 
109  position = last + (diff * f);
110  velocity = vel + (diff * f);
111  }
112  else
113  {
114  position = last;
115  velocity = vel;
116  }
117 
118  // Don't want to multiply position by ref_xform
119  local_pos = position;
120 
121  if (ref_xform)
122  {
123  local_pos *= (*ref_xform);
124  goal *= (*ref_xform);
125  }
126 
127  return (local_pos - goal) * multiplier + goal;
128  }
129 };
130 
131 template <typename T, typename F>
132 inline void
133 CL_Jiggle::solve(const fpreal *data_x, const fpreal *data_y, const fpreal *data_z,
134  fpreal *result_x, fpreal *result_y, fpreal *result_z, int size,
135  fpreal spring_constant, fpreal damping_constant,
136  fpreal limit, UT_Vector3T<T> multiplier,
137  const F& interrupt, const UT_DMatrix4 *ref_xform)
138 
139 {
140  UT_Vector3T<T> goal, result_vec;
141  UT_Vector3T<T> vel = { 0.0, 0.0, 0.0 };
142  UT_Vector3T<T> pos = { data_x[0], data_y[0], data_z[0] };
143 
144  for (exint j = 0; j < size; j++)
145  {
146  goal.assign(data_x[j], data_y[j], data_z[j]);
147  result_vec = simulate(goal, spring_constant, damping_constant, limit, multiplier,
148  (ref_xform == nullptr) ? nullptr : &(ref_xform[j]),
149  vel, pos);
150 
151  result_x[j] = result_vec.x();
152  result_y[j] = result_vec.y();
153  result_z[j] = result_vec.z();
154 
155  if (interrupt())
156  return;
157  }
158 }
159 
160 
161 template <typename T, typename F>
162 inline void
163 CL_Jiggle::solve(const UT_Vector3T<T> *data, UT_Vector3T<T> *result, int size,
164  fpreal spring_constant, fpreal damping_constant,
165  fpreal limit, UT_Vector3T<T> multiplier,
166  const F& interrupt)
167 {
168  UT_Vector3T<T> vel = { 0.0, 0.0, 0.0 };
169  UT_Vector3T<T> pos = data[0];
170 
171  for (exint i = 0; i < size; i++)
172  {
173  result[i] = simulate(data[i], spring_constant, damping_constant, limit,
174  multiplier, nullptr,
175  vel, pos);
176 
177  if (interrupt())
178  return;
179  }
180 }
181 
182 #endif
constexpr SYS_FORCE_INLINE T length2() const noexcept
Definition: UT_Vector3.h:356
vfloat4 sqrt(const vfloat4 &a)
Definition: simd.h:7481
constexpr SYS_FORCE_INLINE T & z() noexcept
Definition: UT_Vector3.h:667
int64 exint
Definition: SYS_Types.h:125
static void solve(const UT_Vector3T< T > *data, UT_Vector3T< T > *result, int size, fpreal spring_constant, fpreal damping_constant, fpreal limit, UT_Vector3T< T > multiplier, const F &interrupt)
Definition: CL_Jiggle.h:163
**But if you need a result
Definition: thread.h:613
3D Vector class.
GLfloat f
Definition: glcorearb.h:1926
static void solve(const fpreal *data_x, const fpreal *data_y, const fpreal *data_z, fpreal *result_x, fpreal *result_y, fpreal *result_z, int size, fpreal spring_constant, fpreal damping_constant, fpreal limit, UT_Vector3T< T > multiplier, const UT_DMatrix4 *ref_xform=nullptr)
Definition: CL_Jiggle.h:60
static void solve(const UT_Vector3T< T > *data, UT_Vector3T< T > *result, int size, fpreal spring_constant, fpreal damping_constant, fpreal limit, UT_Vector3T< T > multiplier)
Definition: CL_Jiggle.h:39
GLint j
Definition: glad.h:2733
__hostdev__ uint64_t last(uint32_t i) const
Definition: NanoVDB.h:5976
void assign(T xx=0.0f, T yy=0.0f, T zz=0.0f)
Set the values of the vector components.
Definition: UT_Vector3.h:694
GLsizeiptr size
Definition: glcorearb.h:664
fpreal64 fpreal
Definition: SYS_Types.h:277
SIM_API const UT_StringHolder position
constexpr SYS_FORCE_INLINE T & y() noexcept
Definition: UT_Vector3.h:665
Definition: format.h:895
constexpr SYS_FORCE_INLINE T & x() noexcept
Definition: UT_Vector3.h:663