blob: b56648efc94988fc1f7d67e4bf7b39f25c7edcc6 [file] [log] [blame]
John Recke45b1fd2014-04-15 09:50:16 -07001/*
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 Reck315c3292014-05-09 19:21:04 -070016
17#define LOG_TAG "Interpolator"
18
John Recke45b1fd2014-04-15 09:50:16 -070019#include "Interpolator.h"
20
21#include <math.h>
John Reck315c3292014-05-09 19:21:04 -070022#include <cutils/log.h>
23
24#include "utils/MathUtils.h"
John Recke45b1fd2014-04-15 09:50:16 -070025
26namespace android {
27namespace uirenderer {
28
29Interpolator* Interpolator::createDefaultInterpolator() {
30 return new AccelerateDecelerateInterpolator();
31}
32
33float AccelerateDecelerateInterpolator::interpolate(float input) {
34 return (float)(cosf((input + 1) * M_PI) / 2.0f) + 0.5f;
35}
36
John Reck315c3292014-05-09 19:21:04 -070037LUTInterpolator::LUTInterpolator(float* values, size_t size) {
38 mValues = values;
39 mSize = size;
40}
41
42LUTInterpolator::~LUTInterpolator() {
43 delete mValues;
44 mValues = 0;
45}
46
47float 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 Recke45b1fd2014-04-15 09:50:16 -070066} /* namespace uirenderer */
67} /* namespace android */