Chromium Code Reviews| Index: third_party/WebKit/Source/platform/animation/UnitBezier.h |
| diff --git a/third_party/WebKit/Source/platform/animation/UnitBezier.h b/third_party/WebKit/Source/platform/animation/UnitBezier.h |
| index f5a5ade7fdc67e3cf0015fdfc0d9b480aebe55a6..c6584fe1ea04e6646a57316e59bcb90bfa414efc 100644 |
| --- a/third_party/WebKit/Source/platform/animation/UnitBezier.h |
| +++ b/third_party/WebKit/Source/platform/animation/UnitBezier.h |
| @@ -29,73 +29,42 @@ |
| #include "platform/PlatformExport.h" |
| #include "wtf/Allocator.h" |
| #include "wtf/Assertions.h" |
| -#include <math.h> |
| + |
| +#include <algorithm> |
| +#include <cmath> |
| namespace blink { |
| -struct UnitBezier { |
| +struct PLATFORM_EXPORT UnitBezier { |
| USING_FAST_MALLOC(UnitBezier); |
| public: |
| - UnitBezier(double p1x, double p1y, double p2x, double p2y) |
| - { |
| - // Calculate the polynomial coefficients, implicit first and last control points are (0,0) and (1,1). |
| - cx = 3.0 * p1x; |
| - bx = 3.0 * (p2x - p1x) - cx; |
| - ax = 1.0 - cx -bx; |
| - |
| - cy = 3.0 * p1y; |
| - by = 3.0 * (p2y - p1y) - cy; |
| - ay = 1.0 - cy - by; |
| - |
| - // End-point gradients are used to calculate timing function results |
| - // outside the range [0, 1]. |
| - // |
| - // There are three possibilities for the gradient at each end: |
| - // (1) the closest control point is not horizontally coincident with regard to |
| - // (0, 0) or (1, 1). In this case the line between the end point and |
| - // the control point is tangent to the bezier at the end point. |
| - // (2) the closest control point is coincident with the end point. In |
| - // this case the line between the end point and the far control |
| - // point is tangent to the bezier at the end point. |
| - // (3) the closest control point is horizontally coincident with the end |
| - // point, but vertically distinct. In this case the gradient at the |
| - // end point is Infinite. However, this causes issues when |
| - // interpolating. As a result, we break down to a simple case of |
| - // 0 gradient under these conditions. |
| - |
| - if (p1x > 0) |
| - m_startGradient = p1y / p1x; |
| - else if (!p1y && p2x > 0) |
| - m_startGradient = p2y / p2x; |
| - else |
| - m_startGradient = 0; |
| - |
| - if (p2x < 1) |
| - m_endGradient = (p2y - 1) / (p2x - 1); |
| - else if (p2x == 1 && p1x < 1) |
| - m_endGradient = (p1y - 1) / (p1x - 1); |
| - else |
| - m_endGradient = 0; |
| - } |
| + UnitBezier(double p1x, double p1y, double p2x, double p2y); |
| - double sampleCurveX(double t) |
| + 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
|
| + |
| + double sampleCurveX(double t) const |
| { |
| // `ax t^3 + bx t^2 + cx t' expanded using Horner's rule. |
| return ((ax * t + bx) * t + cx) * t; |
| } |
| - double sampleCurveY(double t) |
| + double sampleCurveY(double t) const |
| { |
| return ((ay * t + by) * t + cy) * t; |
| } |
| - double sampleCurveDerivativeX(double t) |
| + double sampleCurveDerivativeX(double t) const |
| { |
| return (3.0 * ax * t + 2.0 * bx) * t + cx; |
| } |
| + double sampleCurveDerivativeY(double t) const |
| + { |
| + return (3.0 * ay * t + 2.0 * by) * t + cy; |
| + } |
| + |
| // Given an x value, find a parametric value it came from. |
| - double solveCurveX(double x, double epsilon) |
| + double solveCurveX(double x, double epsilon) const |
| { |
| ASSERT(x >= 0.0); |
| ASSERT(x <= 1.0); |
| @@ -140,7 +109,12 @@ public: |
| // Evaluates y at the given x. The epsilon parameter provides a hint as to the required |
| // accuracy and is not guaranteed. |
| - double solve(double x, double epsilon) |
| + double solve(double x) const |
| + { |
| + return solveWithEpsilon(x, kBezierEpsilon); |
| + } |
| + |
| + double solveWithEpsilon(double x, double epsilon) const |
| { |
| if (x < 0.0) |
| return 0.0 + m_startGradient * x; |
| @@ -149,7 +123,33 @@ public: |
| return sampleCurveY(solveCurveX(x, epsilon)); |
| } |
| + // Returns an approximation of dy/dx at the given x. |
| + double slope(double x) const |
| + { |
| + return slopeWithEpsilon(x, kBezierEpsilon); |
| + } |
| + |
| + double slopeWithEpsilon(double x, double epsilon) const |
| + { |
| + double t = solveCurveX(x, epsilon); |
| + double dx = sampleCurveDerivativeX(t); |
| + double dy = sampleCurveDerivativeY(t); |
| + return dy / dx; |
| + } |
| + |
| + // Sets |min| and |max| to the bezier's minimum and maximium y values in the |
| + // interval [0, 1]. |
| + void range(double* min, double* max) const |
| + { |
| + *min = m_rangeMin; |
| + *max = m_rangeMax; |
| + } |
| + |
| private: |
| + void InitCoefficients(double p1x, double p1y, double p2x, double p2y); |
| + void InitGradients(double p1x, double p1y, double p2x, double p2y); |
| + 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.
|
| + |
| double ax; |
| double bx; |
| double cx; |
| @@ -160,6 +160,9 @@ private: |
| double m_startGradient; |
| double m_endGradient; |
| + |
| + double m_rangeMin; |
| + double m_rangeMax; |
| }; |
| } // namespace blink |