|
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
|
2 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
3 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
5 |
|
6 #ifndef NS_SMILKEYSPLINE_H_ |
|
7 #define NS_SMILKEYSPLINE_H_ |
|
8 |
|
9 /** |
|
10 * Utility class to provide scaling defined in a keySplines element. |
|
11 */ |
|
12 class nsSMILKeySpline |
|
13 { |
|
14 public: |
|
15 nsSMILKeySpline() { /* caller must call Init later */ } |
|
16 |
|
17 /** |
|
18 * Creates a new key spline control point description. |
|
19 * |
|
20 * aX1, etc. are the x1, y1, x2, y2 cubic Bezier control points as defined by |
|
21 * SMILANIM 3.2.3. They must each be in the range 0.0 <= x <= 1.0 |
|
22 */ |
|
23 nsSMILKeySpline(double aX1, double aY1, |
|
24 double aX2, double aY2) |
|
25 { |
|
26 Init(aX1, aY1, aX2, aY2); |
|
27 } |
|
28 |
|
29 double X1() const { return mX1; } |
|
30 double Y1() const { return mY1; } |
|
31 double X2() const { return mX2; } |
|
32 double Y2() const { return mY2; } |
|
33 |
|
34 void Init(double aX1, double aY1, |
|
35 double aX2, double aY2); |
|
36 |
|
37 /** |
|
38 * Gets the output (y) value for an input (x). |
|
39 * |
|
40 * @param aX The input x value. A floating-point number between 0 and |
|
41 * 1 (inclusive). |
|
42 */ |
|
43 double GetSplineValue(double aX) const; |
|
44 |
|
45 void GetSplineDerivativeValues(double aX, double& aDX, double& aDY) const; |
|
46 |
|
47 private: |
|
48 void |
|
49 CalcSampleValues(); |
|
50 |
|
51 /** |
|
52 * Returns x(t) given t, x1, and x2, or y(t) given t, y1, and y2. |
|
53 */ |
|
54 static double |
|
55 CalcBezier(double aT, double aA1, double aA2); |
|
56 |
|
57 /** |
|
58 * Returns dx/dt given t, x1, and x2, or dy/dt given t, y1, and y2. |
|
59 */ |
|
60 static double |
|
61 GetSlope(double aT, double aA1, double aA2); |
|
62 |
|
63 double |
|
64 GetTForX(double aX) const; |
|
65 |
|
66 double |
|
67 NewtonRaphsonIterate(double aX, double aGuessT) const; |
|
68 |
|
69 double |
|
70 BinarySubdivide(double aX, double aA, double aB) const; |
|
71 |
|
72 static double |
|
73 A(double aA1, double aA2) |
|
74 { |
|
75 return 1.0 - 3.0 * aA2 + 3.0 * aA1; |
|
76 } |
|
77 |
|
78 static double |
|
79 B(double aA1, double aA2) |
|
80 { |
|
81 return 3.0 * aA2 - 6.0 * aA1; |
|
82 } |
|
83 |
|
84 static double |
|
85 C(double aA1) |
|
86 { |
|
87 return 3.0 * aA1; |
|
88 } |
|
89 |
|
90 double mX1; |
|
91 double mY1; |
|
92 double mX2; |
|
93 double mY2; |
|
94 |
|
95 enum { kSplineTableSize = 11 }; |
|
96 double mSampleValues[kSplineTableSize]; |
|
97 |
|
98 static const double kSampleStepSize; |
|
99 }; |
|
100 |
|
101 #endif // NS_SMILKEYSPLINE_H_ |