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