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 | |
Chris Craik | 9db58c0 | 2015-08-19 15:19:18 -0700 | [diff] [blame] | 19 | #include <algorithm> |
Mark Salyzyn | 52eb4e0 | 2016-09-28 16:15:30 -0700 | [diff] [blame] | 20 | |
| 21 | #include <log/log.h> |
| 22 | |
| 23 | #include "utils/MathUtils.h" |
Chris Craik | 9db58c0 | 2015-08-19 15:19:18 -0700 | [diff] [blame] | 24 | |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 25 | namespace android { |
| 26 | namespace uirenderer { |
| 27 | |
| 28 | Interpolator* Interpolator::createDefaultInterpolator() { |
| 29 | return new AccelerateDecelerateInterpolator(); |
| 30 | } |
| 31 | |
| 32 | float AccelerateDecelerateInterpolator::interpolate(float input) { |
| 33 | return (float)(cosf((input + 1) * M_PI) / 2.0f) + 0.5f; |
| 34 | } |
| 35 | |
John Reck | c8ac775 | 2014-05-12 16:39:41 -0700 | [diff] [blame] | 36 | float AccelerateInterpolator::interpolate(float input) { |
| 37 | if (mFactor == 1.0f) { |
| 38 | return input * input; |
| 39 | } else { |
| 40 | return pow(input, mDoubleFactor); |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | float AnticipateInterpolator::interpolate(float t) { |
| 45 | return t * t * ((mTension + 1) * t - mTension); |
| 46 | } |
| 47 | |
| 48 | static float a(float t, float s) { |
| 49 | return t * t * ((s + 1) * t - s); |
| 50 | } |
| 51 | |
| 52 | static float o(float t, float s) { |
| 53 | return t * t * ((s + 1) * t + s); |
| 54 | } |
| 55 | |
| 56 | float AnticipateOvershootInterpolator::interpolate(float t) { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 57 | if (t < 0.5f) |
| 58 | return 0.5f * a(t * 2.0f, mTension); |
| 59 | else |
| 60 | return 0.5f * (o(t * 2.0f - 2.0f, mTension) + 2.0f); |
John Reck | c8ac775 | 2014-05-12 16:39:41 -0700 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | static float bounce(float t) { |
| 64 | return t * t * 8.0f; |
| 65 | } |
| 66 | |
| 67 | float BounceInterpolator::interpolate(float t) { |
| 68 | t *= 1.1226f; |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 69 | if (t < 0.3535f) |
| 70 | return bounce(t); |
| 71 | else if (t < 0.7408f) |
| 72 | return bounce(t - 0.54719f) + 0.7f; |
| 73 | else if (t < 0.9644f) |
| 74 | return bounce(t - 0.8526f) + 0.9f; |
| 75 | else |
| 76 | return bounce(t - 1.0435f) + 0.95f; |
John Reck | c8ac775 | 2014-05-12 16:39:41 -0700 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | float CycleInterpolator::interpolate(float input) { |
| 80 | return sinf(2 * mCycles * M_PI * input); |
| 81 | } |
| 82 | |
| 83 | float DecelerateInterpolator::interpolate(float input) { |
| 84 | float result; |
| 85 | if (mFactor == 1.0f) { |
| 86 | result = 1.0f - (1.0f - input) * (1.0f - input); |
| 87 | } else { |
| 88 | result = 1.0f - pow((1.0f - input), 2 * mFactor); |
| 89 | } |
| 90 | return result; |
| 91 | } |
| 92 | |
| 93 | float OvershootInterpolator::interpolate(float t) { |
| 94 | t -= 1.0f; |
| 95 | return t * t * ((mTension + 1) * t + mTension) + 1.0f; |
| 96 | } |
| 97 | |
Doris Liu | 9e9eeee | 2016-11-29 14:48:25 -0800 | [diff] [blame] | 98 | float PathInterpolator::interpolate(float t) { |
| 99 | if (t <= 0) { |
| 100 | return 0; |
| 101 | } else if (t >= 1) { |
| 102 | return 1; |
| 103 | } |
| 104 | // Do a binary search for the correct x to interpolate between. |
| 105 | size_t startIndex = 0; |
| 106 | size_t endIndex = mX.size() - 1; |
| 107 | |
| 108 | while (endIndex > startIndex + 1) { |
| 109 | int midIndex = (startIndex + endIndex) / 2; |
| 110 | if (t < mX[midIndex]) { |
| 111 | endIndex = midIndex; |
| 112 | } else { |
| 113 | startIndex = midIndex; |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | float xRange = mX[endIndex] - mX[startIndex]; |
| 118 | if (xRange == 0) { |
| 119 | return mY[startIndex]; |
| 120 | } |
| 121 | |
| 122 | float tInRange = t - mX[startIndex]; |
| 123 | float fraction = tInRange / xRange; |
| 124 | |
| 125 | float startY = mY[startIndex]; |
| 126 | float endY = mY[endIndex]; |
| 127 | return startY + (fraction * (endY - startY)); |
Doris Liu | 9e9eeee | 2016-11-29 14:48:25 -0800 | [diff] [blame] | 128 | } |
| 129 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 130 | LUTInterpolator::LUTInterpolator(float* values, size_t size) : mValues(values), mSize(size) {} |
John Reck | 315c329 | 2014-05-09 19:21:04 -0700 | [diff] [blame] | 131 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 132 | LUTInterpolator::~LUTInterpolator() {} |
John Reck | 315c329 | 2014-05-09 19:21:04 -0700 | [diff] [blame] | 133 | |
| 134 | float LUTInterpolator::interpolate(float input) { |
Doris Liu | 0940834 | 2016-11-29 13:04:49 -0800 | [diff] [blame] | 135 | // lut position should only be at the end of the table when input is 1f. |
| 136 | float lutpos = input * (mSize - 1); |
John Reck | 315c329 | 2014-05-09 19:21:04 -0700 | [diff] [blame] | 137 | if (lutpos >= (mSize - 1)) { |
| 138 | return mValues[mSize - 1]; |
| 139 | } |
| 140 | |
| 141 | float ipart, weight; |
| 142 | weight = modff(lutpos, &ipart); |
| 143 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 144 | int i1 = (int)ipart; |
| 145 | int i2 = std::min(i1 + 1, (int)mSize - 1); |
John Reck | 315c329 | 2014-05-09 19:21:04 -0700 | [diff] [blame] | 146 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 147 | LOG_ALWAYS_FATAL_IF( |
| 148 | i1 < 0 || i2 < 0, |
| 149 | "negatives in interpolation!" |
John Reck | 7aab5a1 | 2014-06-20 09:59:56 -0700 | [diff] [blame] | 150 | " 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] | 151 | i1, i2, input, lutpos, mSize, mValues.get(), ipart, weight); |
John Reck | 7aab5a1 | 2014-06-20 09:59:56 -0700 | [diff] [blame] | 152 | |
John Reck | 315c329 | 2014-05-09 19:21:04 -0700 | [diff] [blame] | 153 | float v1 = mValues[i1]; |
| 154 | float v2 = mValues[i2]; |
| 155 | |
| 156 | return MathUtils::lerp(v1, v2, weight); |
| 157 | } |
| 158 | |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 159 | } /* namespace uirenderer */ |
| 160 | } /* namespace android */ |