blob: 0e62d7780780914ec292b058bb3ed0f34a86e2c9 [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
Andreas Gampe1e196742014-11-10 15:23:43 -080017// LOG_TAG is being provided by the Makefile, reset.
18#ifdef LOG_TAG
19#undef LOG_TAG
20#endif
John Reck315c3292014-05-09 19:21:04 -070021#define LOG_TAG "Interpolator"
22
John Recke45b1fd2014-04-15 09:50:16 -070023#include "Interpolator.h"
24
John Reckc8ac7752014-05-12 16:39:41 -070025#include <cmath>
John Reck315c3292014-05-09 19:21:04 -070026#include <cutils/log.h>
27
28#include "utils/MathUtils.h"
John Recke45b1fd2014-04-15 09:50:16 -070029
30namespace android {
31namespace uirenderer {
32
33Interpolator* Interpolator::createDefaultInterpolator() {
34 return new AccelerateDecelerateInterpolator();
35}
36
37float AccelerateDecelerateInterpolator::interpolate(float input) {
38 return (float)(cosf((input + 1) * M_PI) / 2.0f) + 0.5f;
39}
40
John Reckc8ac7752014-05-12 16:39:41 -070041float AccelerateInterpolator::interpolate(float input) {
42 if (mFactor == 1.0f) {
43 return input * input;
44 } else {
45 return pow(input, mDoubleFactor);
46 }
47}
48
49float AnticipateInterpolator::interpolate(float t) {
50 return t * t * ((mTension + 1) * t - mTension);
51}
52
53static float a(float t, float s) {
54 return t * t * ((s + 1) * t - s);
55}
56
57static float o(float t, float s) {
58 return t * t * ((s + 1) * t + s);
59}
60
61float AnticipateOvershootInterpolator::interpolate(float t) {
62 if (t < 0.5f) return 0.5f * a(t * 2.0f, mTension);
63 else return 0.5f * (o(t * 2.0f - 2.0f, mTension) + 2.0f);
64}
65
66static float bounce(float t) {
67 return t * t * 8.0f;
68}
69
70float BounceInterpolator::interpolate(float t) {
71 t *= 1.1226f;
72 if (t < 0.3535f) return bounce(t);
73 else if (t < 0.7408f) return bounce(t - 0.54719f) + 0.7f;
74 else if (t < 0.9644f) return bounce(t - 0.8526f) + 0.9f;
75 else return bounce(t - 1.0435f) + 0.95f;
76}
77
78float CycleInterpolator::interpolate(float input) {
79 return sinf(2 * mCycles * M_PI * input);
80}
81
82float DecelerateInterpolator::interpolate(float input) {
83 float result;
84 if (mFactor == 1.0f) {
85 result = 1.0f - (1.0f - input) * (1.0f - input);
86 } else {
87 result = 1.0f - pow((1.0f - input), 2 * mFactor);
88 }
89 return result;
90}
91
92float OvershootInterpolator::interpolate(float t) {
93 t -= 1.0f;
94 return t * t * ((mTension + 1) * t + mTension) + 1.0f;
95}
96
John Reck315c3292014-05-09 19:21:04 -070097LUTInterpolator::LUTInterpolator(float* values, size_t size) {
98 mValues = values;
99 mSize = size;
100}
101
102LUTInterpolator::~LUTInterpolator() {
103 delete mValues;
104 mValues = 0;
105}
106
107float LUTInterpolator::interpolate(float input) {
108 float lutpos = input * mSize;
109 if (lutpos >= (mSize - 1)) {
110 return mValues[mSize - 1];
111 }
112
113 float ipart, weight;
114 weight = modff(lutpos, &ipart);
115
116 int i1 = (int) ipart;
ztenghuic50a03d2014-08-21 13:47:54 -0700117 int i2 = MathUtils::min(i1 + 1, (int) mSize - 1);
John Reck315c3292014-05-09 19:21:04 -0700118
John Reck7aab5a12014-06-20 09:59:56 -0700119 LOG_ALWAYS_FATAL_IF(i1 < 0 || i2 < 0, "negatives in interpolation!"
120 " i1=%d, i2=%d, input=%f, lutpos=%f, size=%zu, values=%p, ipart=%f, weight=%f",
121 i1, i2, input, lutpos, mSize, mValues, ipart, weight);
122
John Reck315c3292014-05-09 19:21:04 -0700123 float v1 = mValues[i1];
124 float v2 = mValues[i2];
125
126 return MathUtils::lerp(v1, v2, weight);
127}
128
129
John Recke45b1fd2014-04-15 09:50:16 -0700130} /* namespace uirenderer */
131} /* namespace android */