John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
John Reck | 315c329 | 2014-05-09 19:21:04 -0700 | [diff] [blame] | 16 | |
Andreas Gampe | 1e19674 | 2014-11-10 15:23:43 -0800 | [diff] [blame] | 17 | // LOG_TAG is being provided by the Makefile, reset. |
| 18 | #ifdef LOG_TAG |
| 19 | #undef LOG_TAG |
| 20 | #endif |
John Reck | 315c329 | 2014-05-09 19:21:04 -0700 | [diff] [blame] | 21 | #define LOG_TAG "Interpolator" |
| 22 | |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 23 | #include "Interpolator.h" |
| 24 | |
John Reck | c8ac775 | 2014-05-12 16:39:41 -0700 | [diff] [blame] | 25 | #include <cmath> |
John Reck | 315c329 | 2014-05-09 19:21:04 -0700 | [diff] [blame] | 26 | #include <cutils/log.h> |
| 27 | |
| 28 | #include "utils/MathUtils.h" |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 29 | |
| 30 | namespace android { |
| 31 | namespace uirenderer { |
| 32 | |
| 33 | Interpolator* Interpolator::createDefaultInterpolator() { |
| 34 | return new AccelerateDecelerateInterpolator(); |
| 35 | } |
| 36 | |
| 37 | float AccelerateDecelerateInterpolator::interpolate(float input) { |
| 38 | return (float)(cosf((input + 1) * M_PI) / 2.0f) + 0.5f; |
| 39 | } |
| 40 | |
John Reck | c8ac775 | 2014-05-12 16:39:41 -0700 | [diff] [blame] | 41 | float AccelerateInterpolator::interpolate(float input) { |
| 42 | if (mFactor == 1.0f) { |
| 43 | return input * input; |
| 44 | } else { |
| 45 | return pow(input, mDoubleFactor); |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | float AnticipateInterpolator::interpolate(float t) { |
| 50 | return t * t * ((mTension + 1) * t - mTension); |
| 51 | } |
| 52 | |
| 53 | static float a(float t, float s) { |
| 54 | return t * t * ((s + 1) * t - s); |
| 55 | } |
| 56 | |
| 57 | static float o(float t, float s) { |
| 58 | return t * t * ((s + 1) * t + s); |
| 59 | } |
| 60 | |
| 61 | float AnticipateOvershootInterpolator::interpolate(float t) { |
| 62 | if (t < 0.5f) return 0.5f * a(t * 2.0f, mTension); |
| 63 | else return 0.5f * (o(t * 2.0f - 2.0f, mTension) + 2.0f); |
| 64 | } |
| 65 | |
| 66 | static float bounce(float t) { |
| 67 | return t * t * 8.0f; |
| 68 | } |
| 69 | |
| 70 | float BounceInterpolator::interpolate(float t) { |
| 71 | t *= 1.1226f; |
| 72 | if (t < 0.3535f) return bounce(t); |
| 73 | else if (t < 0.7408f) return bounce(t - 0.54719f) + 0.7f; |
| 74 | else if (t < 0.9644f) return bounce(t - 0.8526f) + 0.9f; |
| 75 | else return bounce(t - 1.0435f) + 0.95f; |
| 76 | } |
| 77 | |
| 78 | float CycleInterpolator::interpolate(float input) { |
| 79 | return sinf(2 * mCycles * M_PI * input); |
| 80 | } |
| 81 | |
| 82 | float DecelerateInterpolator::interpolate(float input) { |
| 83 | float result; |
| 84 | if (mFactor == 1.0f) { |
| 85 | result = 1.0f - (1.0f - input) * (1.0f - input); |
| 86 | } else { |
| 87 | result = 1.0f - pow((1.0f - input), 2 * mFactor); |
| 88 | } |
| 89 | return result; |
| 90 | } |
| 91 | |
| 92 | float OvershootInterpolator::interpolate(float t) { |
| 93 | t -= 1.0f; |
| 94 | return t * t * ((mTension + 1) * t + mTension) + 1.0f; |
| 95 | } |
| 96 | |
John Reck | 315c329 | 2014-05-09 19:21:04 -0700 | [diff] [blame] | 97 | LUTInterpolator::LUTInterpolator(float* values, size_t size) { |
| 98 | mValues = values; |
| 99 | mSize = size; |
| 100 | } |
| 101 | |
| 102 | LUTInterpolator::~LUTInterpolator() { |
| 103 | delete mValues; |
| 104 | mValues = 0; |
| 105 | } |
| 106 | |
| 107 | float LUTInterpolator::interpolate(float input) { |
| 108 | float lutpos = input * mSize; |
| 109 | if (lutpos >= (mSize - 1)) { |
| 110 | return mValues[mSize - 1]; |
| 111 | } |
| 112 | |
| 113 | float ipart, weight; |
| 114 | weight = modff(lutpos, &ipart); |
| 115 | |
| 116 | int i1 = (int) ipart; |
ztenghui | c50a03d | 2014-08-21 13:47:54 -0700 | [diff] [blame] | 117 | int i2 = MathUtils::min(i1 + 1, (int) mSize - 1); |
John Reck | 315c329 | 2014-05-09 19:21:04 -0700 | [diff] [blame] | 118 | |
John Reck | 7aab5a1 | 2014-06-20 09:59:56 -0700 | [diff] [blame] | 119 | LOG_ALWAYS_FATAL_IF(i1 < 0 || i2 < 0, "negatives in interpolation!" |
| 120 | " i1=%d, i2=%d, input=%f, lutpos=%f, size=%zu, values=%p, ipart=%f, weight=%f", |
| 121 | i1, i2, input, lutpos, mSize, mValues, ipart, weight); |
| 122 | |
John Reck | 315c329 | 2014-05-09 19:21:04 -0700 | [diff] [blame] | 123 | float v1 = mValues[i1]; |
| 124 | float v2 = mValues[i2]; |
| 125 | |
| 126 | return MathUtils::lerp(v1, v2, weight); |
| 127 | } |
| 128 | |
| 129 | |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 130 | } /* namespace uirenderer */ |
| 131 | } /* namespace android */ |