Skip to content

Commit

Permalink
support computing decay duration independent of fromValue; fix facebo…
Browse files Browse the repository at this point in the history
  • Loading branch information
Kimon Tsinteris committed May 2, 2014
1 parent ad03259 commit 3b6eb6e
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 3 deletions.
25 changes: 25 additions & 0 deletions pop-tests/POPDecayAnimationTests.mm
Original file line number Diff line number Diff line change
Expand Up @@ -459,4 +459,29 @@ - (void)testEndValueOnReuse
STAssertTrue(lastValue > toValue, @"unexpected last value:%f");
}

- (void)testComputedProperties
{
POPDecayAnimation *anim = [POPDecayAnimation animationWithPropertyNamed:kPOPLayerPositionX];

// set velocity, test duration
anim.velocity = @(100);
CGFloat d1 = anim.duration;
STAssertTrue(d1 > 0, @"unexpected duration %@", anim);

// set velocity, test duration
anim.velocity = @(1000);
CGFloat d2 = anim.duration;
STAssertTrue(d2 > d1, @"unexpected duration %@", anim);

// set from value, test to value
anim.fromValue = @(0);
CGFloat p1 = [anim.toValue floatValue];
STAssertTrue(p1 > [anim.fromValue floatValue], @"unexpected to value %@", anim);

// set from value, test to value
anim.fromValue = @(10000);
CGFloat p2 = [anim.toValue floatValue];
STAssertTrue(p2 > [anim.fromValue floatValue] && p2 > p1, @"unexpected to value %@", anim);
}

@end
9 changes: 6 additions & 3 deletions pop/POPDecayAnimation.mm
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@ - (void)setVelocity:(id)aValue
- (void)_ensureComputedProperties
{
if (NULL == __state->toVec) {
__state->computeDestinationValues();
__state->computeDuration();
__state->computeToValue();
}
}

Expand All @@ -113,11 +114,13 @@ - (void)_appendDescription:(NSMutableString *)s debug:(BOOL)debug
{
[super _appendDescription:s debug:debug];

if (NULL != __state->toVec)
if (0 != self.duration) {
[s appendFormat:@"; duration = %f", self.duration];
}

if (__state->deceleration)
if (__state->deceleration) {
[s appendFormat:@"; deceleration = %f", __state->deceleration];
}
}

@end
40 changes: 40 additions & 0 deletions pop/POPDecayAnimationInternal.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,46 @@ struct _POPDecayAnimationState : _POPPropertyAnimationState

}

void computeDuration() {

// compute duration till threshold velocity
Vector4r scaledVelocity = vector4(velocityVec) / 1000.;

double k = dynamicsThreshold * kPOPAnimationDecayMinimalVelocityFactor / 1000.;
double vx = k / scaledVelocity.x;
double vy = k / scaledVelocity.y;
double vz = k / scaledVelocity.z;
double vw = k / scaledVelocity.w;
double d = log(deceleration) * 1000.;
duration = MAX(MAX(MAX(log(fabs(vx)) / d, log(fabs(vy)) / d), log(fabs(vz)) / d), log(fabs(vw)) / d);

// ensure velocity threshold is exceeded
if (isnan(duration) || duration < 0) {
duration = 0;
}
}

void computeToValue() {
// to value assuming final velocity as a factor of dynamics threshold
// derived from v' = v * d^dt used in decay_position
// to compute the to value with maximal dt, p' = p + (v * d) / (1 - d)
VectorRef fromValue = NULL != currentVec ? currentVec : fromVec;
if (!fromValue) {
return;
}

// ensure duration is computed
if (0 == duration) {
computeDuration();
}

// compute to value
VectorRef toValue(Vector::new_vector(fromValue.get()));
Vector4r velocity = velocityVec->vector4r();
decay_position(toValue->data(), velocity.data(), valueCount, duration, deceleration);
toVec = toValue;
}

void computeDestinationValues() {
// to value assuming final velocity as a factor of dynamics threshold
// derived from v' = v * d^dt used in decay_position
Expand Down

0 comments on commit 3b6eb6e

Please sign in to comment.