/src/mozilla-central/dom/smil/nsSMILKeySpline.cpp
Line | Count | Source (jump to first uncovered line) |
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 | | #include "nsSMILKeySpline.h" |
8 | | #include <stdint.h> |
9 | | #include <math.h> |
10 | | |
11 | 0 | #define NEWTON_ITERATIONS 4 |
12 | 0 | #define NEWTON_MIN_SLOPE 0.02 |
13 | 0 | #define SUBDIVISION_PRECISION 0.0000001 |
14 | 0 | #define SUBDIVISION_MAX_ITERATIONS 10 |
15 | | |
16 | | const double nsSMILKeySpline::kSampleStepSize = |
17 | | 1.0 / double(kSplineTableSize - 1); |
18 | | |
19 | | void |
20 | | nsSMILKeySpline::Init(double aX1, |
21 | | double aY1, |
22 | | double aX2, |
23 | | double aY2) |
24 | 0 | { |
25 | 0 | mX1 = aX1; |
26 | 0 | mY1 = aY1; |
27 | 0 | mX2 = aX2; |
28 | 0 | mY2 = aY2; |
29 | 0 |
|
30 | 0 | if (mX1 != mY1 || mX2 != mY2) |
31 | 0 | CalcSampleValues(); |
32 | 0 | } |
33 | | |
34 | | double |
35 | | nsSMILKeySpline::GetSplineValue(double aX) const |
36 | 0 | { |
37 | 0 | if (mX1 == mY1 && mX2 == mY2) |
38 | 0 | return aX; |
39 | 0 | |
40 | 0 | return CalcBezier(GetTForX(aX), mY1, mY2); |
41 | 0 | } |
42 | | |
43 | | void |
44 | | nsSMILKeySpline::GetSplineDerivativeValues(double aX, double& aDX, double& aDY) const |
45 | 0 | { |
46 | 0 | double t = GetTForX(aX); |
47 | 0 | aDX = GetSlope(t, mX1, mX2); |
48 | 0 | aDY = GetSlope(t, mY1, mY2); |
49 | 0 | } |
50 | | |
51 | | void |
52 | | nsSMILKeySpline::CalcSampleValues() |
53 | 0 | { |
54 | 0 | for (uint32_t i = 0; i < kSplineTableSize; ++i) { |
55 | 0 | mSampleValues[i] = CalcBezier(double(i) * kSampleStepSize, mX1, mX2); |
56 | 0 | } |
57 | 0 | } |
58 | | |
59 | | /*static*/ double |
60 | | nsSMILKeySpline::CalcBezier(double aT, |
61 | | double aA1, |
62 | | double aA2) |
63 | 0 | { |
64 | 0 | // use Horner's scheme to evaluate the Bezier polynomial |
65 | 0 | return ((A(aA1, aA2)*aT + B(aA1, aA2))*aT + C(aA1))*aT; |
66 | 0 | } |
67 | | |
68 | | /*static*/ double |
69 | | nsSMILKeySpline::GetSlope(double aT, |
70 | | double aA1, |
71 | | double aA2) |
72 | 0 | { |
73 | 0 | return 3.0 * A(aA1, aA2)*aT*aT + 2.0 * B(aA1, aA2) * aT + C(aA1); |
74 | 0 | } |
75 | | |
76 | | double |
77 | | nsSMILKeySpline::GetTForX(double aX) const |
78 | 0 | { |
79 | 0 | // Early return when aX == 1.0 to avoid floating-point inaccuracies. |
80 | 0 | if (aX == 1.0) { |
81 | 0 | return 1.0; |
82 | 0 | } |
83 | 0 | // Find interval where t lies |
84 | 0 | double intervalStart = 0.0; |
85 | 0 | const double* currentSample = &mSampleValues[1]; |
86 | 0 | const double* const lastSample = &mSampleValues[kSplineTableSize - 1]; |
87 | 0 | for (; currentSample != lastSample && *currentSample <= aX; |
88 | 0 | ++currentSample) { |
89 | 0 | intervalStart += kSampleStepSize; |
90 | 0 | } |
91 | 0 | --currentSample; // t now lies between *currentSample and *currentSample+1 |
92 | 0 |
|
93 | 0 | // Interpolate to provide an initial guess for t |
94 | 0 | double dist = (aX - *currentSample) / |
95 | 0 | (*(currentSample+1) - *currentSample); |
96 | 0 | double guessForT = intervalStart + dist * kSampleStepSize; |
97 | 0 |
|
98 | 0 | // Check the slope to see what strategy to use. If the slope is too small |
99 | 0 | // Newton-Raphson iteration won't converge on a root so we use bisection |
100 | 0 | // instead. |
101 | 0 | double initialSlope = GetSlope(guessForT, mX1, mX2); |
102 | 0 | if (initialSlope >= NEWTON_MIN_SLOPE) { |
103 | 0 | return NewtonRaphsonIterate(aX, guessForT); |
104 | 0 | } else if (initialSlope == 0.0) { |
105 | 0 | return guessForT; |
106 | 0 | } else { |
107 | 0 | return BinarySubdivide(aX, intervalStart, intervalStart + kSampleStepSize); |
108 | 0 | } |
109 | 0 | } |
110 | | |
111 | | double |
112 | | nsSMILKeySpline::NewtonRaphsonIterate(double aX, double aGuessT) const |
113 | 0 | { |
114 | 0 | // Refine guess with Newton-Raphson iteration |
115 | 0 | for (uint32_t i = 0; i < NEWTON_ITERATIONS; ++i) { |
116 | 0 | // We're trying to find where f(t) = aX, |
117 | 0 | // so we're actually looking for a root for: CalcBezier(t) - aX |
118 | 0 | double currentX = CalcBezier(aGuessT, mX1, mX2) - aX; |
119 | 0 | double currentSlope = GetSlope(aGuessT, mX1, mX2); |
120 | 0 |
|
121 | 0 | if (currentSlope == 0.0) |
122 | 0 | return aGuessT; |
123 | 0 | |
124 | 0 | aGuessT -= currentX / currentSlope; |
125 | 0 | } |
126 | 0 |
|
127 | 0 | return aGuessT; |
128 | 0 | } |
129 | | |
130 | | double |
131 | | nsSMILKeySpline::BinarySubdivide(double aX, double aA, double aB) const |
132 | 0 | { |
133 | 0 | double currentX; |
134 | 0 | double currentT; |
135 | 0 | uint32_t i = 0; |
136 | 0 |
|
137 | 0 | do |
138 | 0 | { |
139 | 0 | currentT = aA + (aB - aA) / 2.0; |
140 | 0 | currentX = CalcBezier(currentT, mX1, mX2) - aX; |
141 | 0 |
|
142 | 0 | if (currentX > 0.0) { |
143 | 0 | aB = currentT; |
144 | 0 | } else { |
145 | 0 | aA = currentT; |
146 | 0 | } |
147 | 0 | } while (fabs(currentX) > SUBDIVISION_PRECISION |
148 | 0 | && ++i < SUBDIVISION_MAX_ITERATIONS); |
149 | 0 |
|
150 | 0 | return currentT; |
151 | 0 | } |