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 | |
| 17 | #define LOG_TAG "Interpolator" |
| 18 | |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 19 | #include "Interpolator.h" |
| 20 | |
| 21 | #include <math.h> |
John Reck | 315c329 | 2014-05-09 19:21:04 -0700 | [diff] [blame^] | 22 | #include <cutils/log.h> |
| 23 | |
| 24 | #include "utils/MathUtils.h" |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 25 | |
| 26 | namespace android { |
| 27 | namespace uirenderer { |
| 28 | |
| 29 | Interpolator* Interpolator::createDefaultInterpolator() { |
| 30 | return new AccelerateDecelerateInterpolator(); |
| 31 | } |
| 32 | |
| 33 | float AccelerateDecelerateInterpolator::interpolate(float input) { |
| 34 | return (float)(cosf((input + 1) * M_PI) / 2.0f) + 0.5f; |
| 35 | } |
| 36 | |
John Reck | 315c329 | 2014-05-09 19:21:04 -0700 | [diff] [blame^] | 37 | LUTInterpolator::LUTInterpolator(float* values, size_t size) { |
| 38 | mValues = values; |
| 39 | mSize = size; |
| 40 | } |
| 41 | |
| 42 | LUTInterpolator::~LUTInterpolator() { |
| 43 | delete mValues; |
| 44 | mValues = 0; |
| 45 | } |
| 46 | |
| 47 | float LUTInterpolator::interpolate(float input) { |
| 48 | float lutpos = input * mSize; |
| 49 | if (lutpos >= (mSize - 1)) { |
| 50 | return mValues[mSize - 1]; |
| 51 | } |
| 52 | |
| 53 | float ipart, weight; |
| 54 | weight = modff(lutpos, &ipart); |
| 55 | |
| 56 | int i1 = (int) ipart; |
| 57 | int i2 = MathUtils::min(i1 + 1, mSize - 1); |
| 58 | |
| 59 | float v1 = mValues[i1]; |
| 60 | float v2 = mValues[i2]; |
| 61 | |
| 62 | return MathUtils::lerp(v1, v2, weight); |
| 63 | } |
| 64 | |
| 65 | |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 66 | } /* namespace uirenderer */ |
| 67 | } /* namespace android */ |