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 | |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 17 | #include "Interpolator.h" |
| 18 | |
John Reck | 315c329 | 2014-05-09 19:21:04 -0700 | [diff] [blame] | 19 | #include "utils/MathUtils.h" |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 20 | |
Chris Craik | 9db58c0 | 2015-08-19 15:19:18 -0700 | [diff] [blame] | 21 | #include <algorithm> |
| 22 | #include <cutils/log.h> |
| 23 | |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 24 | namespace android { |
| 25 | namespace uirenderer { |
| 26 | |
| 27 | Interpolator* Interpolator::createDefaultInterpolator() { |
| 28 | return new AccelerateDecelerateInterpolator(); |
| 29 | } |
| 30 | |
| 31 | float AccelerateDecelerateInterpolator::interpolate(float input) { |
| 32 | return (float)(cosf((input + 1) * M_PI) / 2.0f) + 0.5f; |
| 33 | } |
| 34 | |
John Reck | c8ac775 | 2014-05-12 16:39:41 -0700 | [diff] [blame] | 35 | float AccelerateInterpolator::interpolate(float input) { |
| 36 | if (mFactor == 1.0f) { |
| 37 | return input * input; |
| 38 | } else { |
| 39 | return pow(input, mDoubleFactor); |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | float AnticipateInterpolator::interpolate(float t) { |
| 44 | return t * t * ((mTension + 1) * t - mTension); |
| 45 | } |
| 46 | |
| 47 | static float a(float t, float s) { |
| 48 | return t * t * ((s + 1) * t - s); |
| 49 | } |
| 50 | |
| 51 | static float o(float t, float s) { |
| 52 | return t * t * ((s + 1) * t + s); |
| 53 | } |
| 54 | |
| 55 | float AnticipateOvershootInterpolator::interpolate(float t) { |
| 56 | if (t < 0.5f) return 0.5f * a(t * 2.0f, mTension); |
| 57 | else return 0.5f * (o(t * 2.0f - 2.0f, mTension) + 2.0f); |
| 58 | } |
| 59 | |
| 60 | static float bounce(float t) { |
| 61 | return t * t * 8.0f; |
| 62 | } |
| 63 | |
| 64 | float BounceInterpolator::interpolate(float t) { |
| 65 | t *= 1.1226f; |
| 66 | if (t < 0.3535f) return bounce(t); |
| 67 | else if (t < 0.7408f) return bounce(t - 0.54719f) + 0.7f; |
| 68 | else if (t < 0.9644f) return bounce(t - 0.8526f) + 0.9f; |
| 69 | else return bounce(t - 1.0435f) + 0.95f; |
| 70 | } |
| 71 | |
| 72 | float CycleInterpolator::interpolate(float input) { |
| 73 | return sinf(2 * mCycles * M_PI * input); |
| 74 | } |
| 75 | |
| 76 | float DecelerateInterpolator::interpolate(float input) { |
| 77 | float result; |
| 78 | if (mFactor == 1.0f) { |
| 79 | result = 1.0f - (1.0f - input) * (1.0f - input); |
| 80 | } else { |
| 81 | result = 1.0f - pow((1.0f - input), 2 * mFactor); |
| 82 | } |
| 83 | return result; |
| 84 | } |
| 85 | |
| 86 | float OvershootInterpolator::interpolate(float t) { |
| 87 | t -= 1.0f; |
| 88 | return t * t * ((mTension + 1) * t + mTension) + 1.0f; |
| 89 | } |
| 90 | |
Chris Craik | 51d6a3d | 2014-12-22 17:16:56 -0800 | [diff] [blame] | 91 | LUTInterpolator::LUTInterpolator(float* values, size_t size) |
| 92 | : mValues(values) |
| 93 | , mSize(size) { |
John Reck | 315c329 | 2014-05-09 19:21:04 -0700 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | LUTInterpolator::~LUTInterpolator() { |
John Reck | 315c329 | 2014-05-09 19:21:04 -0700 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | float LUTInterpolator::interpolate(float input) { |
| 100 | float lutpos = input * mSize; |
| 101 | if (lutpos >= (mSize - 1)) { |
| 102 | return mValues[mSize - 1]; |
| 103 | } |
| 104 | |
| 105 | float ipart, weight; |
| 106 | weight = modff(lutpos, &ipart); |
| 107 | |
| 108 | int i1 = (int) ipart; |
Chris Craik | 9db58c0 | 2015-08-19 15:19:18 -0700 | [diff] [blame] | 109 | int i2 = std::min(i1 + 1, (int) mSize - 1); |
John Reck | 315c329 | 2014-05-09 19:21:04 -0700 | [diff] [blame] | 110 | |
John Reck | 7aab5a1 | 2014-06-20 09:59:56 -0700 | [diff] [blame] | 111 | LOG_ALWAYS_FATAL_IF(i1 < 0 || i2 < 0, "negatives in interpolation!" |
| 112 | " i1=%d, i2=%d, input=%f, lutpos=%f, size=%zu, values=%p, ipart=%f, weight=%f", |
Chris Craik | 51d6a3d | 2014-12-22 17:16:56 -0800 | [diff] [blame] | 113 | i1, i2, input, lutpos, mSize, mValues.get(), ipart, weight); |
John Reck | 7aab5a1 | 2014-06-20 09:59:56 -0700 | [diff] [blame] | 114 | |
John Reck | 315c329 | 2014-05-09 19:21:04 -0700 | [diff] [blame] | 115 | float v1 = mValues[i1]; |
| 116 | float v2 = mValues[i2]; |
| 117 | |
| 118 | return MathUtils::lerp(v1, v2, weight); |
| 119 | } |
| 120 | |
| 121 | |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 122 | } /* namespace uirenderer */ |
| 123 | } /* namespace android */ |