POPDecayAnimationInternal.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 "POPDecayAnimation.h"
  9. #import <cmath>
  10. #import "POPPropertyAnimationInternal.h"
  11. // minimal velocity factor before decay animation is considered complete, in units / s
  12. static CGFloat kPOPAnimationDecayMinimalVelocityFactor = 5.;
  13. // default decay animation deceleration
  14. static CGFloat kPOPAnimationDecayDecelerationDefault = 0.998;
  15. static void decay_position(CGFloat *x, CGFloat *v, NSUInteger count, CFTimeInterval dt, CGFloat deceleration)
  16. {
  17. dt *= 1000;
  18. // v0 = v / 1000
  19. // v = v0 * powf(deceleration, dt);
  20. // v = v * 1000;
  21. // x0 = x;
  22. // x = x0 + v0 * deceleration * (1 - powf(deceleration, dt)) / (1 - deceleration)
  23. float v0[count];
  24. float kv = powf(deceleration, dt);
  25. float kx = deceleration * (1 - kv) / (1 - deceleration);
  26. for (NSUInteger idx = 0; idx < count; idx++) {
  27. v0[idx] = v[idx] / 1000.;
  28. v[idx] = v0[idx] * kv * 1000.;
  29. x[idx] = x[idx] + v0[idx] * kx;
  30. }
  31. }
  32. struct _POPDecayAnimationState : _POPPropertyAnimationState
  33. {
  34. double deceleration;
  35. CFTimeInterval duration;
  36. _POPDecayAnimationState(id __unsafe_unretained anim) :
  37. _POPPropertyAnimationState(anim),
  38. deceleration(kPOPAnimationDecayDecelerationDefault),
  39. duration(0)
  40. {
  41. type = kPOPAnimationDecay;
  42. }
  43. bool isDone() {
  44. if (_POPPropertyAnimationState::isDone()) {
  45. return true;
  46. }
  47. CGFloat f = dynamicsThreshold * kPOPAnimationDecayMinimalVelocityFactor;
  48. const CGFloat *velocityValues = vec_data(velocityVec);
  49. for (NSUInteger idx = 0; idx < valueCount; idx++) {
  50. if (std::abs((velocityValues[idx])) >= f)
  51. return false;
  52. }
  53. return true;
  54. }
  55. void computeDuration() {
  56. // compute duration till threshold velocity
  57. Vector4r scaledVelocity = vector4(velocityVec) / 1000.;
  58. double k = dynamicsThreshold * kPOPAnimationDecayMinimalVelocityFactor / 1000.;
  59. double vx = k / scaledVelocity.x;
  60. double vy = k / scaledVelocity.y;
  61. double vz = k / scaledVelocity.z;
  62. double vw = k / scaledVelocity.w;
  63. double d = log(deceleration) * 1000.;
  64. duration = MAX(MAX(MAX(log(fabs(vx)) / d, log(fabs(vy)) / d), log(fabs(vz)) / d), log(fabs(vw)) / d);
  65. // ensure velocity threshold is exceeded
  66. if (std::isnan(duration) || duration < 0) {
  67. duration = 0;
  68. }
  69. }
  70. void computeToValue() {
  71. // to value assuming final velocity as a factor of dynamics threshold
  72. // derived from v' = v * d^dt used in decay_position
  73. // to compute the to value with maximal dt, p' = p + (v * d) / (1 - d)
  74. VectorRef fromValue = NULL != currentVec ? currentVec : fromVec;
  75. if (!fromValue) {
  76. return;
  77. }
  78. // ensure duration is computed
  79. if (0 == duration) {
  80. computeDuration();
  81. }
  82. // compute to value
  83. VectorRef toValue(Vector::new_vector(fromValue.get()));
  84. Vector4r velocity = velocityVec->vector4r();
  85. decay_position(toValue->data(), velocity.data(), valueCount, duration, deceleration);
  86. toVec = toValue;
  87. }
  88. bool advance(CFTimeInterval time, CFTimeInterval dt, id obj) {
  89. // advance past not yet initialized animations
  90. if (NULL == currentVec) {
  91. return false;
  92. }
  93. decay_position(currentVec->data(), velocityVec->data(), valueCount, dt, deceleration);
  94. // clamp to compute end value; avoid possibility of decaying past
  95. clampCurrentValue(kPOPAnimationClampEnd | clampMode);
  96. return true;
  97. }
  98. };
  99. typedef struct _POPDecayAnimationState POPDecayAnimationState;