blob: fc0e8a059f225357f51d7aed58765b8162045386 [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
John Reckc8ac7752014-05-12 16:39:41 -070021#include <cmath>
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 Reckc8ac7752014-05-12 16:39:41 -070037float AccelerateInterpolator::interpolate(float input) {
38 if (mFactor == 1.0f) {
39 return input * input;
40 } else {
41 return pow(input, mDoubleFactor);
42 }
43}
44
45float AnticipateInterpolator::interpolate(float t) {
46 return t * t * ((mTension + 1) * t - mTension);
47}
48
49static float a(float t, float s) {
50 return t * t * ((s + 1) * t - s);
51}
52
53static float o(float t, float s) {
54 return t * t * ((s + 1) * t + s);
55}
56
57float AnticipateOvershootInterpolator::interpolate(float t) {
58 if (t < 0.5f) return 0.5f * a(t * 2.0f, mTension);
59 else return 0.5f * (o(t * 2.0f - 2.0f, mTension) + 2.0f);
60}
61
62static float bounce(float t) {
63 return t * t * 8.0f;
64}
65
66float BounceInterpolator::interpolate(float t) {
67 t *= 1.1226f;
68 if (t < 0.3535f) return bounce(t);
69 else if (t < 0.7408f) return bounce(t - 0.54719f) + 0.7f;
70 else if (t < 0.9644f) return bounce(t - 0.8526f) + 0.9f;
71 else return bounce(t - 1.0435f) + 0.95f;
72}
73
74float CycleInterpolator::interpolate(float input) {
75 return sinf(2 * mCycles * M_PI * input);
76}
77
78float DecelerateInterpolator::interpolate(float input) {
79 float result;
80 if (mFactor == 1.0f) {
81 result = 1.0f - (1.0f - input) * (1.0f - input);
82 } else {
83 result = 1.0f - pow((1.0f - input), 2 * mFactor);
84 }
85 return result;
86}
87
88float OvershootInterpolator::interpolate(float t) {
89 t -= 1.0f;
90 return t * t * ((mTension + 1) * t + mTension) + 1.0f;
91}
92
John Reck315c3292014-05-09 19:21:04 -070093LUTInterpolator::LUTInterpolator(float* values, size_t size) {
94 mValues = values;
95 mSize = size;
96}
97
98LUTInterpolator::~LUTInterpolator() {
99 delete mValues;
100 mValues = 0;
101}
102
103float LUTInterpolator::interpolate(float input) {
104 float lutpos = input * mSize;
105 if (lutpos >= (mSize - 1)) {
106 return mValues[mSize - 1];
107 }
108
109 float ipart, weight;
110 weight = modff(lutpos, &ipart);
111
112 int i1 = (int) ipart;
113 int i2 = MathUtils::min(i1 + 1, mSize - 1);
114
John Reck7aab5a12014-06-20 09:59:56 -0700115 LOG_ALWAYS_FATAL_IF(i1 < 0 || i2 < 0, "negatives in interpolation!"
116 " i1=%d, i2=%d, input=%f, lutpos=%f, size=%zu, values=%p, ipart=%f, weight=%f",
117 i1, i2, input, lutpos, mSize, mValues, ipart, weight);
118
John Reck315c3292014-05-09 19:21:04 -0700119 float v1 = mValues[i1];
120 float v2 = mValues[i2];
121
122 return MathUtils::lerp(v1, v2, weight);
123}
124
125
John Recke45b1fd2014-04-15 09:50:16 -0700126} /* namespace uirenderer */
127} /* namespace android */