| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2008 Apple Inc. All Rights Reserved. | 2 * Copyright (C) 2008 Apple Inc. All Rights Reserved. |
| 3 * | 3 * |
| 4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions | 5 * modification, are permitted provided that the following conditions |
| 6 * are met: | 6 * are met: |
| 7 * 1. Redistributions of source code must retain the above copyright | 7 * 1. Redistributions of source code must retain the above copyright |
| 8 * notice, this list of conditions and the following disclaimer. | 8 * notice, this list of conditions and the following disclaimer. |
| 9 * 2. Redistributions in binary form must reproduce the above copyright | 9 * 2. Redistributions in binary form must reproduce the above copyright |
| 10 * notice, this list of conditions and the following disclaimer in the | 10 * notice, this list of conditions and the following disclaimer in the |
| (...skipping 11 matching lines...) Expand all Loading... |
| 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 24 */ | 24 */ |
| 25 | 25 |
| 26 #ifndef UnitBezier_h | 26 #ifndef UnitBezier_h |
| 27 #define UnitBezier_h | 27 #define UnitBezier_h |
| 28 | 28 |
| 29 #include "platform/PlatformExport.h" | 29 #include "platform/PlatformExport.h" |
| 30 #include "wtf/Allocator.h" | 30 #include "wtf/Allocator.h" |
| 31 #include "wtf/Assertions.h" | 31 #include "wtf/Assertions.h" |
| 32 #include <math.h> | 32 |
| 33 #include <algorithm> |
| 34 #include <cmath> |
| 33 | 35 |
| 34 namespace blink { | 36 namespace blink { |
| 35 | 37 |
| 36 struct UnitBezier { | 38 struct PLATFORM_EXPORT UnitBezier { |
| 37 USING_FAST_MALLOC(UnitBezier); | 39 USING_FAST_MALLOC(UnitBezier); |
| 38 public: | 40 public: |
| 39 UnitBezier(double p1x, double p1y, double p2x, double p2y) | 41 UnitBezier(double p1x, double p1y, double p2x, double p2y); |
| 40 { | |
| 41 // Calculate the polynomial coefficients, implicit first and last contro
l points are (0,0) and (1,1). | |
| 42 cx = 3.0 * p1x; | |
| 43 bx = 3.0 * (p2x - p1x) - cx; | |
| 44 ax = 1.0 - cx -bx; | |
| 45 | 42 |
| 46 cy = 3.0 * p1y; | 43 static const double kBezierEpsilon; |
| 47 by = 3.0 * (p2y - p1y) - cy; | |
| 48 ay = 1.0 - cy - by; | |
| 49 | 44 |
| 50 // End-point gradients are used to calculate timing function results | 45 double sampleCurveX(double t) const |
| 51 // outside the range [0, 1]. | |
| 52 // | |
| 53 // There are three possibilities for the gradient at each end: | |
| 54 // (1) the closest control point is not horizontally coincident with reg
ard to | |
| 55 // (0, 0) or (1, 1). In this case the line between the end point and | |
| 56 // the control point is tangent to the bezier at the end point. | |
| 57 // (2) the closest control point is coincident with the end point. In | |
| 58 // this case the line between the end point and the far control | |
| 59 // point is tangent to the bezier at the end point. | |
| 60 // (3) the closest control point is horizontally coincident with the end | |
| 61 // point, but vertically distinct. In this case the gradient at the | |
| 62 // end point is Infinite. However, this causes issues when | |
| 63 // interpolating. As a result, we break down to a simple case of | |
| 64 // 0 gradient under these conditions. | |
| 65 | |
| 66 if (p1x > 0) | |
| 67 m_startGradient = p1y / p1x; | |
| 68 else if (!p1y && p2x > 0) | |
| 69 m_startGradient = p2y / p2x; | |
| 70 else | |
| 71 m_startGradient = 0; | |
| 72 | |
| 73 if (p2x < 1) | |
| 74 m_endGradient = (p2y - 1) / (p2x - 1); | |
| 75 else if (p2x == 1 && p1x < 1) | |
| 76 m_endGradient = (p1y - 1) / (p1x - 1); | |
| 77 else | |
| 78 m_endGradient = 0; | |
| 79 } | |
| 80 | |
| 81 double sampleCurveX(double t) | |
| 82 { | 46 { |
| 83 // `ax t^3 + bx t^2 + cx t' expanded using Horner's rule. | 47 // `ax t^3 + bx t^2 + cx t' expanded using Horner's rule. |
| 84 return ((ax * t + bx) * t + cx) * t; | 48 return ((ax * t + bx) * t + cx) * t; |
| 85 } | 49 } |
| 86 | 50 |
| 87 double sampleCurveY(double t) | 51 double sampleCurveY(double t) const |
| 88 { | 52 { |
| 89 return ((ay * t + by) * t + cy) * t; | 53 return ((ay * t + by) * t + cy) * t; |
| 90 } | 54 } |
| 91 | 55 |
| 92 double sampleCurveDerivativeX(double t) | 56 double sampleCurveDerivativeX(double t) const |
| 93 { | 57 { |
| 94 return (3.0 * ax * t + 2.0 * bx) * t + cx; | 58 return (3.0 * ax * t + 2.0 * bx) * t + cx; |
| 95 } | 59 } |
| 96 | 60 |
| 61 double sampleCurveDerivativeY(double t) const |
| 62 { |
| 63 return (3.0 * ay * t + 2.0 * by) * t + cy; |
| 64 } |
| 65 |
| 97 // Given an x value, find a parametric value it came from. | 66 // Given an x value, find a parametric value it came from. |
| 98 double solveCurveX(double x, double epsilon) | 67 double solveCurveX(double x, double epsilon) const |
| 99 { | 68 { |
| 100 ASSERT(x >= 0.0); | 69 ASSERT(x >= 0.0); |
| 101 ASSERT(x <= 1.0); | 70 ASSERT(x <= 1.0); |
| 102 | 71 |
| 103 double t0; | 72 double t0; |
| 104 double t1; | 73 double t1; |
| 105 double t2; | 74 double t2; |
| 106 double x2; | 75 double x2; |
| 107 double d2; | 76 double d2; |
| 108 int i; | 77 int i; |
| (...skipping 22 matching lines...) Expand all Loading... |
| 131 t0 = t2; | 100 t0 = t2; |
| 132 else | 101 else |
| 133 t1 = t2; | 102 t1 = t2; |
| 134 t2 = (t1 - t0) * .5 + t0; | 103 t2 = (t1 - t0) * .5 + t0; |
| 135 } | 104 } |
| 136 | 105 |
| 137 // Failure. | 106 // Failure. |
| 138 return t2; | 107 return t2; |
| 139 } | 108 } |
| 140 | 109 |
| 110 // Evaluates y at the given x. |
| 111 double solve(double x) const |
| 112 { |
| 113 return solveWithEpsilon(x, kBezierEpsilon); |
| 114 } |
| 115 |
| 141 // Evaluates y at the given x. The epsilon parameter provides a hint as to t
he required | 116 // Evaluates y at the given x. The epsilon parameter provides a hint as to t
he required |
| 142 // accuracy and is not guaranteed. | 117 // accuracy and is not guaranteed. |
| 143 double solve(double x, double epsilon) | 118 double solveWithEpsilon(double x, double epsilon) const |
| 144 { | 119 { |
| 145 if (x < 0.0) | 120 if (x < 0.0) |
| 146 return 0.0 + m_startGradient * x; | 121 return 0.0 + m_startGradient * x; |
| 147 if (x > 1.0) | 122 if (x > 1.0) |
| 148 return 1.0 + m_endGradient * (x - 1.0); | 123 return 1.0 + m_endGradient * (x - 1.0); |
| 149 return sampleCurveY(solveCurveX(x, epsilon)); | 124 return sampleCurveY(solveCurveX(x, epsilon)); |
| 150 } | 125 } |
| 151 | 126 |
| 127 // Returns an approximation of dy/dx at the given x. |
| 128 double slope(double x) const |
| 129 { |
| 130 return slopeWithEpsilon(x, kBezierEpsilon); |
| 131 } |
| 132 |
| 133 double slopeWithEpsilon(double x, double epsilon) const |
| 134 { |
| 135 double t = solveCurveX(x, epsilon); |
| 136 double dx = sampleCurveDerivativeX(t); |
| 137 double dy = sampleCurveDerivativeY(t); |
| 138 return dy / dx; |
| 139 } |
| 140 |
| 141 // Sets |min| and |max| to the bezier's minimum and maximium y values in the |
| 142 // interval [0, 1]. |
| 143 void range(double* min, double* max) const |
| 144 { |
| 145 *min = m_rangeMin; |
| 146 *max = m_rangeMax; |
| 147 } |
| 148 |
| 152 private: | 149 private: |
| 150 void initCoefficients(double p1x, double p1y, double p2x, double p2y); |
| 151 void initGradients(double p1x, double p1y, double p2x, double p2y); |
| 152 void initRange(double p1y, double p2y); |
| 153 |
| 153 double ax; | 154 double ax; |
| 154 double bx; | 155 double bx; |
| 155 double cx; | 156 double cx; |
| 156 | 157 |
| 157 double ay; | 158 double ay; |
| 158 double by; | 159 double by; |
| 159 double cy; | 160 double cy; |
| 160 | 161 |
| 161 double m_startGradient; | 162 double m_startGradient; |
| 162 double m_endGradient; | 163 double m_endGradient; |
| 164 |
| 165 double m_rangeMin; |
| 166 double m_rangeMax; |
| 163 }; | 167 }; |
| 164 | 168 |
| 165 } // namespace blink | 169 } // namespace blink |
| 166 | 170 |
| 167 #endif // UnitBezier_h | 171 #endif // UnitBezier_h |
| OLD | NEW |