POPSpringSolver.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /**
  2. Copyright (c) 2014-present, Facebook, Inc.
  3. All rights reserved.
  4. This source code is licensed under the BSD-style license found in the
  5. LICENSE file in the root directory of this source tree. An additional grant
  6. of patent rights can be found in the PATENTS file in the same directory.
  7. */
  8. #import <Foundation/Foundation.h>
  9. #import "POPVector.h"
  10. namespace POP {
  11. template <typename T>
  12. struct SSState
  13. {
  14. T p;
  15. T v;
  16. };
  17. template <typename T>
  18. struct SSDerivative
  19. {
  20. T dp;
  21. T dv;
  22. };
  23. typedef SSState<Vector4d> SSState4d;
  24. typedef SSDerivative<Vector4d> SSDerivative4d;
  25. const CFTimeInterval solverDt = 0.001f;
  26. const CFTimeInterval maxSolverDt = 30.0f;
  27. /**
  28. Templated spring solver class.
  29. */
  30. template <typename T>
  31. class SpringSolver
  32. {
  33. double _k; // stiffness
  34. double _b; // dampening
  35. double _m; // mass
  36. double _tp; // threshold
  37. double _tv; // threshold velocity
  38. double _ta; // threshold acceleration
  39. CFTimeInterval _accumulatedTime;
  40. SSState<T> _lastState;
  41. T _lastDv;
  42. bool _started;
  43. public:
  44. SpringSolver(double k, double b, double m = 1) : _k(k), _b(b), _m(m), _started(false)
  45. {
  46. _accumulatedTime = 0;
  47. _lastState.p = T::Zero();
  48. _lastState.v = T::Zero();
  49. _lastDv = T::Zero();
  50. setThreshold(1.);
  51. }
  52. ~SpringSolver()
  53. {
  54. }
  55. bool started()
  56. {
  57. return _started;
  58. }
  59. void setConstants(double k, double b, double m)
  60. {
  61. _k = k;
  62. _b = b;
  63. _m = m;
  64. }
  65. void setThreshold(double t)
  66. {
  67. _tp = t / 2; // half a unit
  68. _tv = 25.0 * t; // 5 units per second, squared for comparison
  69. _ta = 625.0 * t * t; // 5 units per second squared, squared for comparison
  70. }
  71. T acceleration(const SSState<T> &state, double t)
  72. {
  73. return state.p*(-_k/_m) - state.v*(_b/_m);
  74. }
  75. SSDerivative<T> evaluate(const SSState<T> &initial, double t)
  76. {
  77. SSDerivative<T> output;
  78. output.dp = initial.v;
  79. output.dv = acceleration(initial, t);
  80. return output;
  81. }
  82. SSDerivative<T> evaluate(const SSState<T> &initial, double t, double dt, const SSDerivative<T> &d)
  83. {
  84. SSState<T> state;
  85. state.p = initial.p + d.dp*dt;
  86. state.v = initial.v + d.dv*dt;
  87. SSDerivative<T> output;
  88. output.dp = state.v;
  89. output.dv = acceleration(state, t+dt);
  90. return output;
  91. }
  92. void integrate(SSState<T> &state, double t, double dt)
  93. {
  94. SSDerivative<T> a = evaluate(state, t);
  95. SSDerivative<T> b = evaluate(state, t, dt*0.5, a);
  96. SSDerivative<T> c = evaluate(state, t, dt*0.5, b);
  97. SSDerivative<T> d = evaluate(state, t, dt, c);
  98. T dpdt = (a.dp + (b.dp + c.dp)*2.0 + d.dp) * (1.0/6.0);
  99. T dvdt = (a.dv + (b.dv + c.dv)*2.0 + d.dv) * (1.0/6.0);
  100. state.p = state.p + dpdt*dt;
  101. state.v = state.v + dvdt*dt;
  102. _lastDv = dvdt;
  103. }
  104. SSState<T> interpolate(const SSState<T> &previous, const SSState<T> &current, double alpha)
  105. {
  106. SSState<T> state;
  107. state.p = current.p*alpha + previous.p*(1-alpha);
  108. state.v = current.v*alpha + previous.v*(1-alpha);
  109. return state;
  110. }
  111. void advance(SSState<T> &state, double t, double dt)
  112. {
  113. _started = true;
  114. if (dt > maxSolverDt) {
  115. // excessive time step, force shut down
  116. _lastDv = _lastState.v = _lastState.p = T::Zero();
  117. } else {
  118. _accumulatedTime += dt;
  119. SSState<T> previousState = state, currentState = state;
  120. while (_accumulatedTime >= solverDt) {
  121. previousState = currentState;
  122. this->integrate(currentState, t, solverDt);
  123. t += solverDt;
  124. _accumulatedTime -= solverDt;
  125. }
  126. CFTimeInterval alpha = _accumulatedTime / solverDt;
  127. _lastState = state = this->interpolate(previousState, currentState, alpha);
  128. }
  129. }
  130. bool hasConverged()
  131. {
  132. if (!_started) {
  133. return false;
  134. }
  135. for (size_t idx = 0; idx < _lastState.p.size(); idx++) {
  136. if (fabs(_lastState.p(idx)) >= _tp) {
  137. return false;
  138. }
  139. }
  140. return (_lastState.v.squaredNorm() < _tv) && (_lastDv.squaredNorm() < _ta);
  141. }
  142. void reset()
  143. {
  144. _accumulatedTime = 0;
  145. _lastState.p = T::Zero();
  146. _lastState.v = T::Zero();
  147. _lastDv = T::Zero();
  148. _started = false;
  149. }
  150. };
  151. /**
  152. Convenience spring solver type definitions.
  153. */
  154. typedef SpringSolver<Vector2d> SpringSolver2d;
  155. typedef SpringSolver<Vector3d> SpringSolver3d;
  156. typedef SpringSolver<Vector4d> SpringSolver4d;
  157. }