Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(94)

Side by Side Diff: third_party/WebKit/Source/platform/animation/UnitBezier.h

Issue 1819783002: Blink Platform UnitBezier: Implement Slope and Range methods. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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;
alancutter (OOO until 2018) 2016/03/21 07:12:56 This should stay in the cpp file since it's not ex
loyso (OOO) 2016/03/22 03:12:50 It is used in inline functions in this header. Als
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 24 matching lines...) Expand all
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
141 // Evaluates y at the given x. The epsilon parameter provides a hint as to t he required 110 // Evaluates y at the given x. The epsilon parameter provides a hint as to t he required
142 // accuracy and is not guaranteed. 111 // accuracy and is not guaranteed.
143 double solve(double x, double epsilon) 112 double solve(double x) const
113 {
114 return solveWithEpsilon(x, kBezierEpsilon);
115 }
116
117 double solveWithEpsilon(double x, double epsilon) const
144 { 118 {
145 if (x < 0.0) 119 if (x < 0.0)
146 return 0.0 + m_startGradient * x; 120 return 0.0 + m_startGradient * x;
147 if (x > 1.0) 121 if (x > 1.0)
148 return 1.0 + m_endGradient * (x - 1.0); 122 return 1.0 + m_endGradient * (x - 1.0);
149 return sampleCurveY(solveCurveX(x, epsilon)); 123 return sampleCurveY(solveCurveX(x, epsilon));
150 } 124 }
151 125
126 // Returns an approximation of dy/dx at the given x.
127 double slope(double x) const
128 {
129 return slopeWithEpsilon(x, kBezierEpsilon);
130 }
131
132 double slopeWithEpsilon(double x, double epsilon) const
133 {
134 double t = solveCurveX(x, epsilon);
135 double dx = sampleCurveDerivativeX(t);
136 double dy = sampleCurveDerivativeY(t);
137 return dy / dx;
138 }
139
140 // Sets |min| and |max| to the bezier's minimum and maximium y values in the
141 // interval [0, 1].
142 void range(double* min, double* max) const
143 {
144 *min = m_rangeMin;
145 *max = m_rangeMax;
146 }
147
152 private: 148 private:
149 void InitCoefficients(double p1x, double p1y, double p2x, double p2y);
150 void InitGradients(double p1x, double p1y, double p2x, double p2y);
151 void InitRange(double p1y, double p2y);
alancutter (OOO until 2018) 2016/03/21 07:12:56 Lower case i to conform to Blink coding style.
loyso (OOO) 2016/03/22 03:12:50 Done.
152
153 double ax; 153 double ax;
154 double bx; 154 double bx;
155 double cx; 155 double cx;
156 156
157 double ay; 157 double ay;
158 double by; 158 double by;
159 double cy; 159 double cy;
160 160
161 double m_startGradient; 161 double m_startGradient;
162 double m_endGradient; 162 double m_endGradient;
163
164 double m_rangeMin;
165 double m_rangeMax;
163 }; 166 };
164 167
165 } // namespace blink 168 } // namespace blink
166 169
167 #endif // UnitBezier_h 170 #endif // UnitBezier_h
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698