| 6 | * the License. You may obtain a copy of the License at |
| 7 | * http://www.mozilla.org/MPL/ |
| 8 | * |
| 9 | * Software distributed under the License is distributed on an "AS IS" basis, |
| 10 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License |
| 11 | * for the specific language governing rights and limitations under the |
| 12 | * License. |
| 13 | * |
| 14 | * The Original Code is the Mozilla SMIL module. |
| 15 | * |
| 16 | * The Initial Developer of the Original Code is Brian Birtles. |
| 17 | * Portions created by the Initial Developer are Copyright (C) 2005 |
| 18 | * the Initial Developer. All Rights Reserved. |
| 19 | * |
| 20 | * Contributor(s): |
| 21 | * Brian Birtles <birtles@gmail.com> |
| 22 | * |
| 23 | * Alternatively, the contents of this file may be used under the terms of |
| 24 | * either of the GNU General Public License Version 2 or later (the "GPL"), |
| 25 | * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), |
| 26 | * in which case the provisions of the GPL or the LGPL are applicable instead |
| 27 | * of those above. If you wish to allow use of your version of this file only |
| 28 | * under the terms of either the GPL or the LGPL, and not to allow others to |
| 29 | * use your version of this file under the terms of the MPL, indicate your |
| 30 | * decision by deleting the provisions above and replace them with the notice |
| 31 | * and other provisions required by the GPL or the LGPL. If you do not delete |
| 32 | * the provisions above, a recipient may use your version of this file under |
| 33 | * the terms of any one of the MPL, the GPL or the LGPL. |
| 34 | * |
| 35 | * ***** END LICENSE BLOCK ***** */ |
| 36 |
| 37 | #include "vinterpolator.h" |
| 38 | #include <cmath> |
| 39 |
| 40 | V_BEGIN_NAMESPACE |
| 41 |
| 42 | #define NEWTON_ITERATIONS 4 |
| 43 | #define NEWTON_MIN_SLOPE 0.02 |
| 44 | #define SUBDIVISION_PRECISION 0.0000001 |
| 45 | #define SUBDIVISION_MAX_ITERATIONS 10 |
| 46 |
| 47 | const float VInterpolator::kSampleStepSize = |
| 48 | 1.0 / float(VInterpolator::kSplineTableSize - 1); |
| 49 |
| 50 | void VInterpolator::init(float aX1, float aY1, float aX2, float aY2) |
| 51 | { |
| 52 | mX1 = aX1; |
| 53 | mY1 = aY1; |
| 54 | mX2 = aX2; |
| 55 | mY2 = aY2; |
| 56 |
| 57 | if (mX1 != mY1 || mX2 != mY2) CalcSampleValues(); |
| 58 | } |
| 59 |
| 60 | /*static*/ float VInterpolator::CalcBezier(float aT, float aA1, float aA2) |
| 61 | { |
| 62 | // use Horner's scheme to evaluate the Bezier polynomial |
| 63 | return ((A(aA1, aA2) * aT + B(aA1, aA2)) * aT + C(aA1)) * aT; |
| 64 | } |
| 65 |
| 66 | void VInterpolator::CalcSampleValues() |
| 67 | { |
| 68 | for (int i = 0; i < kSplineTableSize; ++i) { |
| 69 | mSampleValues[i] = CalcBezier(float(i) * kSampleStepSize, mX1, mX2); |
| 70 | } |
| 71 | } |
| 72 |
| 73 | float VInterpolator::GetSlope(float aT, float aA1, float aA2) |
| 74 | { |
| 75 | return 3.0 * A(aA1, aA2) * aT * aT + 2.0 * B(aA1, aA2) * aT + C(aA1); |
| 76 | } |
| 77 |
| 78 | float VInterpolator::value(float aX) const |
| 79 | { |
| 80 | if (mX1 == mY1 && mX2 == mY2) return aX; |
| 81 |
| 82 | return CalcBezier(GetTForX(aX), mY1, mY2); |
| 83 | } |
| 84 |
| 85 | float VInterpolator::GetTForX(float aX) const |
| 86 | { |
| 87 | // Find interval where t lies |
| 88 | float intervalStart = 0.0; |
| 89 | const float* currentSample = &mSampleValues[1]; |
| 90 | const float* const lastSample = &mSampleValues[kSplineTableSize - 1]; |
| 91 | for (; currentSample != lastSample && *currentSample <= aX; |
| 92 | ++currentSample) { |
| 93 | intervalStart += kSampleStepSize; |
| 94 | } |
| 95 | --currentSample; // t now lies between *currentSample and *currentSample+1 |
| 96 |
| 97 | // Interpolate to provide an initial guess for t |
| 98 | float dist = |
| 99 | (aX - *currentSample) / (*(currentSample + 1) - *currentSample); |
| 100 | float guessForT = intervalStart + dist * kSampleStepSize; |
| 101 |
| 102 | // Check the slope to see what strategy to use. If the slope is too small |
| 103 | // Newton-Raphson iteration won't converge on a root so we use bisection |
| 104 | // instead. |
| 105 | float initialSlope = GetSlope(guessForT, mX1, mX2); |
| 106 | if (initialSlope >= NEWTON_MIN_SLOPE) { |
| 107 | return NewtonRaphsonIterate(aX, guessForT); |
| 108 | } else if (initialSlope == 0.0) { |
| 109 | return guessForT; |
| 110 | } else { |
| 111 | return BinarySubdivide(aX, intervalStart, |
| 112 | intervalStart + kSampleStepSize); |
| 113 | } |
| 114 | } |
| 115 |
| 116 | float VInterpolator::NewtonRaphsonIterate(float aX, float aGuessT) const |
| 117 | { |
| 118 | // Refine guess with Newton-Raphson iteration |
| 119 | for (int i = 0; i < NEWTON_ITERATIONS; ++i) { |
| 120 | // We're trying to find where f(t) = aX, |
| 121 | // so we're actually looking for a root for: CalcBezier(t) - aX |
| 122 | float currentX = CalcBezier(aGuessT, mX1, mX2) - aX; |
| 123 | float currentSlope = GetSlope(aGuessT, mX1, mX2); |
| 124 |
| 125 | if (currentSlope == 0.0) return aGuessT; |
| 126 |
| 127 | aGuessT -= currentX / currentSlope; |
| 128 | } |
| 129 |
| 130 | return aGuessT; |
| 131 | } |
| 132 |
| 133 | float VInterpolator::BinarySubdivide(float aX, float aA, float aB) const |
| 134 | { |
| 135 | float currentX; |
| 136 | float currentT; |
| 137 | int i = 0; |
| 138 |
| 139 | do { |
| 140 | currentT = aA + (aB - aA) / 2.0; |
| 141 | currentX = CalcBezier(currentT, mX1, mX2) - aX; |
| 142 |
| 143 | if (currentX > 0.0) { |
| 144 | aB = currentT; |
| 145 | } else { |
| 146 | aA = currentT; |
| 147 | } |
| 148 | } while (fabs(currentX) > SUBDIVISION_PRECISION && |
| 149 | ++i < SUBDIVISION_MAX_ITERATIONS); |
| 150 |
| 151 | return currentT; |
| 152 | } |
| 153 |
| 154 | V_END_NAMESPACE |
| 155 |