Interpolator.cpp revision 315c329544d7c593d1072b071cbb92d9afe74021
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 */
16
17#define LOG_TAG "Interpolator"
18
19#include "Interpolator.h"
20
21#include <math.h>
22#include <cutils/log.h>
23
24#include "utils/MathUtils.h"
25
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
37LUTInterpolator::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
66} /* namespace uirenderer */
67} /* namespace android */
68