From 462a48feb2604ecc066e50c4aa64e0e9ea5a19c6 Mon Sep 17 00:00:00 2001 From: Kimon Tsinteris Date: Wed, 7 May 2014 15:31:07 -0700 Subject: [PATCH] namespace all exported math C functions; fix #56 --- pop-tests/POPBasicAnimationTests.mm | 8 +++++++- pop-tests/POPSpringAnimationTests.mm | 4 ++++ pop/POPAnimationExtras.mm | 16 ++++++++-------- pop/POPBasicAnimationInternal.h | 4 ++-- pop/POPMath.h | 17 +++++++++-------- pop/POPMath.mm | 14 +++++++------- pop/POPPropertyAnimationInternal.h | 2 +- 7 files changed, 38 insertions(+), 27 deletions(-) diff --git a/pop-tests/POPBasicAnimationTests.mm b/pop-tests/POPBasicAnimationTests.mm index a59460c1..408b83ae 100644 --- a/pop-tests/POPBasicAnimationTests.mm +++ b/pop-tests/POPBasicAnimationTests.mm @@ -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]; diff --git a/pop-tests/POPSpringAnimationTests.mm b/pop-tests/POPSpringAnimationTests.mm index c48726b2..03a46d18 100644 --- a/pop-tests/POPSpringAnimationTests.mm +++ b/pop-tests/POPSpringAnimationTests.mm @@ -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 diff --git a/pop/POPAnimationExtras.mm b/pop/POPAnimationExtras.mm index d7abbfe6..941aaaf4 100644 --- a/pop/POPAnimationExtras.mm +++ b/pop/POPAnimationExtras.mm @@ -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); @@ -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 diff --git a/pop/POPBasicAnimationInternal.h b/pop/POPBasicAnimationInternal.h index be465e05..c7779e32 100644 --- a/pop/POPBasicAnimationInternal.h +++ b/pop/POPBasicAnimationInternal.h @@ -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); @@ -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) { diff --git a/pop/POPMath.h b/pop/POPMath.h index ae92994c..877a8e1f 100644 --- a/pop/POPMath.h +++ b/pop/POPMath.h @@ -10,7 +10,8 @@ #import #import -#import +#import "POPDefines.h" +#import "POPVector.h" NS_INLINE CGFloat sqrtr(CGFloat f) { @@ -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); diff --git a/pop/POPMath.mm b/pop/POPMath.mm index dc494573..0b86bbe9 100644 --- a/pop/POPMath.mm +++ b/pop/POPMath.mm @@ -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)); } @@ -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); } @@ -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.) { @@ -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); diff --git a/pop/POPPropertyAnimationInternal.h b/pop/POPPropertyAnimationInternal.h index ff742ff5..cccab38e 100644 --- a/pop/POPPropertyAnimationInternal.h +++ b/pop/POPPropertyAnimationInternal.h @@ -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;