Skip to content

Commit

Permalink
namespace all exported math C functions; fix facebookarchive#56
Browse files Browse the repository at this point in the history
  • Loading branch information
Kimon Tsinteris committed May 7, 2014
1 parent 065ba80 commit 462a48f
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 27 deletions.
8 changes: 7 additions & 1 deletion pop-tests/POPBasicAnimationTests.mm
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,15 @@ @implementation POPBasicAnimationTests
- (void)testColorInterpolation
{
POPBasicAnimation *anim = [POPBasicAnimation animationWithPropertyNamed:kPOPLayerBackgroundColor];

#if TARGET_OS_IPHONE
anim.fromValue = [UIColor whiteColor];
anim.toValue = [UIColor redColor];

#else
anim.fromValue = [NSColor whiteColor];
anim.toValue = [NSColor redColor];
#endif

POPAnimationTracer *tracer = anim.tracer;
[tracer start];

Expand Down
4 changes: 4 additions & 0 deletions pop-tests/POPSpringAnimationTests.mm
Original file line number Diff line number Diff line change
Expand Up @@ -554,7 +554,11 @@ - (void)testNilColor
STAssertTrue(fromColor, @"unexpected value %p", fromColor);

// verify from color clear
#if TARGET_OS_IPHONE
POPAssertColorEqual(fromColor, [UIColor clearColor].CGColor);
#else
POPAssertColorEqual(fromColor, [NSColor clearColor].CGColor);
#endif
}

- (void)testExcessiveJumpInTime
Expand Down
16 changes: 8 additions & 8 deletions pop/POPAnimationExtras.mm
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,13 @@ @implementation POPSpringAnimation (POPAnimationExtras)

+ (void)convertBounciness:(CGFloat)bounciness speed:(CGFloat)speed toTension:(CGFloat *)outTension friction:(CGFloat *)outFriction mass:(CGFloat *)outMass
{
double b = normalize(bounciness / POPBouncy3NormalizationScale, 0, POPBouncy3NormalizationRange);
b = project_normal(b, POPBouncy3BouncinessNormalizedMin, POPBouncy3BouncinessNormalizedMax);
double b = POPNormalize(bounciness / POPBouncy3NormalizationScale, 0, POPBouncy3NormalizationRange);
b = POPProjectNormal(b, POPBouncy3BouncinessNormalizedMin, POPBouncy3BouncinessNormalizedMax);

double s = normalize(speed / POPBouncy3NormalizationScale, 0, POPBouncy3NormalizationRange);
double s = POPNormalize(speed / POPBouncy3NormalizationScale, 0, POPBouncy3NormalizationRange);

CGFloat tension = project_normal(s, POPBouncy3SpeedNormalizedMin, POPBouncy3SpeedNormalizedMax);
CGFloat friction = quadratic_out_interpolation(b, b3_nobounce(tension), POPBouncy3FrictionInterpolationMax);
CGFloat tension = POPProjectNormal(s, POPBouncy3SpeedNormalizedMin, POPBouncy3SpeedNormalizedMax);
CGFloat friction = POPQuadraticOutInterpolation(b, POPBouncy3NoBounce(tension), POPBouncy3FrictionInterpolationMax);

tension = POP_ANIMATION_TENSION_FOR_QC_TENSION(tension);
friction = POP_ANIMATION_FRICTION_FOR_QC_FRICTION(friction);
Expand All @@ -83,13 +83,13 @@ + (void)convertTension:(CGFloat)tension friction:(CGFloat)friction toBounciness:
CGFloat qcTension = QC_TENSION_FOR_POP_ANIMATION_TENSION(tension);

// Friction is a function of bounciness and tension, according to the following:
// friction = quadratic_out_interpolation(b, b3_nobounce(tension), POPBouncy3FrictionInterpolationMax);
// friction = POPQuadraticOutInterpolation(b, POPBouncy3NoBounce(tension), POPBouncy3FrictionInterpolationMax);
// Solve for bounciness, given a tension and friction.

CGFloat nobounceTension = b3_nobounce(qcTension);
CGFloat nobounceTension = POPBouncy3NoBounce(qcTension);
CGFloat bounciness1, bounciness2;

quadratic_solve((nobounceTension - POPBouncy3FrictionInterpolationMax), // a
POPQuadraticSolve((nobounceTension - POPBouncy3FrictionInterpolationMax), // a
2 * (POPBouncy3FrictionInterpolationMax - nobounceTension), // b
(nobounceTension - qcFriction), // c
bounciness1, // x1
Expand Down
4 changes: 2 additions & 2 deletions pop/POPBasicAnimationInternal.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ static void interpolate(POPValueType valueType, NSUInteger count, const CGFloat
case kPOPValueSize:
case kPOPValueRect:
case kPOPValueColor:
interpolate_vector(count, outVec, fromVec, toVec, p);
POPInterpolateVector(count, outVec, fromVec, toVec, p);
break;
default:
NSCAssert(false, @"unhandled type %d", valueType);
Expand Down Expand Up @@ -74,7 +74,7 @@ struct _POPBasicAnimationState : _POPPropertyAnimationState
CFTimeInterval t = MIN(time - startTime, duration) / duration;

// solve for normalized time, aka progresss [0, 1]
double p = timing_function_solve(timingControlPoints, t, SOLVE_EPS(duration));
double p = POPTimingFunctionSolve(timingControlPoints, t, SOLVE_EPS(duration));

// interpolate and advance
if (p != progress) {
Expand Down
17 changes: 9 additions & 8 deletions pop/POPMath.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
#import <Foundation/Foundation.h>
#import <CoreGraphics/CoreGraphics.h>

#import <POP/POPDefines.h>
#import "POPDefines.h"
#import "POPVector.h"

NS_INLINE CGFloat sqrtr(CGFloat f)
{
Expand All @@ -34,21 +35,21 @@ NS_INLINE CGFloat POPSubRound(CGFloat f, CGFloat sub)

#define _EQLF_(x, y, epsilon) (fabsf ((x) - (y)) < epsilon)

extern void interpolate_vector(NSUInteger count, CGFloat *dst, const CGFloat *from, const CGFloat *to, double f);
extern void POPInterpolateVector(NSUInteger count, CGFloat *dst, const CGFloat *from, const CGFloat *to, double f);

extern double timing_function_solve(const double vec[4], double t, double eps);
extern double POPTimingFunctionSolve(const double vec[4], double t, double eps);

// quadratic mapping of t [0, 1] to [start, end]
extern double quadratic_out_interpolation(double t, double start, double end);
extern double POPQuadraticOutInterpolation(double t, double start, double end);

// normalize value to [0, 1] based on its range [startValue, endValue]
extern double normalize(double value, double startValue, double endValue);
extern double POPNormalize(double value, double startValue, double endValue);

// project a normalized value [0, 1] to a given range [start, end]
extern double project_normal(double n, double start, double end);
extern double POPProjectNormal(double n, double start, double end);

// solve a quadratic equation of the form a * x^2 + b * x + c = 0
extern void quadratic_solve(CGFloat a, CGFloat b, CGFloat c, CGFloat &x1, CGFloat &x2);
extern void POPQuadraticSolve(CGFloat a, CGFloat b, CGFloat c, CGFloat &x1, CGFloat &x2);

// for a given tension return the bouncy 3 friction that produces no bounce
extern double b3_nobounce(double tension);
extern double POPBouncy3NoBounce(double tension);
14 changes: 7 additions & 7 deletions pop/POPMath.mm
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,25 @@
#import "UnitBezier.h"
#import "POPAnimationPrivate.h"

void interpolate_vector(NSUInteger count, CGFloat *dst, const CGFloat *from, const CGFloat *to, double f)
void POPInterpolateVector(NSUInteger count, CGFloat *dst, const CGFloat *from, const CGFloat *to, double f)
{
for (NSUInteger idx = 0; idx < count; idx++) {
dst[idx] = MIX(from[idx], to[idx], f);
}
}

double timing_function_solve(const double vec[4], double t, double eps)
double POPTimingFunctionSolve(const double vec[4], double t, double eps)
{
WebCore::UnitBezier bezier(vec[0], vec[1], vec[2], vec[3]);
return bezier.solve(t, eps);
}

double normalize(double value, double startValue, double endValue)
double POPNormalize(double value, double startValue, double endValue)
{
return (value - startValue) / (endValue - startValue);
}

double project_normal(double n, double start, double end)
double POPProjectNormal(double n, double start, double end)
{
return start + (n * (end - start));
}
Expand All @@ -39,7 +39,7 @@ static double linear_interpolation(double t, double start, double end)
return t * end + (1.f - t) * start;
}

double quadratic_out_interpolation(double t, double start, double end)
double POPQuadraticOutInterpolation(double t, double start, double end)
{
return linear_interpolation(2*t - t*t, start, end);
}
Expand All @@ -59,7 +59,7 @@ static double b3_friction3(double x)
return (0.00000045 * pow(x, 3)) - (0.000332 * pow(x, 2)) + 0.1078 * x + 5.84;
}

double b3_nobounce(double tension)
double POPBouncy3NoBounce(double tension)
{
double friction = 0;
if (tension <= 18.) {
Expand All @@ -74,7 +74,7 @@ static double b3_friction3(double x)
return friction;
}

void quadratic_solve(CGFloat a, CGFloat b, CGFloat c, CGFloat &x1, CGFloat &x2)
void POPQuadraticSolve(CGFloat a, CGFloat b, CGFloat c, CGFloat &x1, CGFloat &x2)
{
CGFloat discriminant = sqrt(b * b - 4 * a * c);
x1 = (-b + discriminant) / (2 * a);
Expand Down
2 changes: 1 addition & 1 deletion pop/POPPropertyAnimationInternal.h
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ struct _POPPropertyAnimationState : _POPAnimationState
*outVec = *toVec;
}
} else {
interpolate_vector(count, vec_data(outVec), vec_data(fromVec), vec_data(toVec), progress);
POPInterpolateVector(count, vec_data(outVec), vec_data(fromVec), vec_data(toVec), progress);
}

currentVec = outVec;
Expand Down

0 comments on commit 462a48f

Please sign in to comment.