blob: b5a33afccdc780b54042b18b47737ea23a572502 [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
John Recke45b1fd2014-04-15 09:50:16 -070017#include "Interpolator.h"
18
Chris Craik9db58c02015-08-19 15:19:18 -070019#include <algorithm>
Mark Salyzyn52eb4e02016-09-28 16:15:30 -070020
21#include <log/log.h>
22
23#include "utils/MathUtils.h"
Chris Craik9db58c02015-08-19 15:19:18 -070024
John Recke45b1fd2014-04-15 09:50:16 -070025namespace android {
26namespace uirenderer {
27
28Interpolator* Interpolator::createDefaultInterpolator() {
29 return new AccelerateDecelerateInterpolator();
30}
31
32float AccelerateDecelerateInterpolator::interpolate(float input) {
33 return (float)(cosf((input + 1) * M_PI) / 2.0f) + 0.5f;
34}
35
John Reckc8ac7752014-05-12 16:39:41 -070036float AccelerateInterpolator::interpolate(float input) {
37 if (mFactor == 1.0f) {
38 return input * input;
39 } else {
40 return pow(input, mDoubleFactor);
41 }
42}
43
44float AnticipateInterpolator::interpolate(float t) {
45 return t * t * ((mTension + 1) * t - mTension);
46}
47
48static float a(float t, float s) {
49 return t * t * ((s + 1) * t - s);
50}
51
52static float o(float t, float s) {
53 return t * t * ((s + 1) * t + s);
54}
55
56float AnticipateOvershootInterpolator::interpolate(float t) {
57 if (t < 0.5f) return 0.5f * a(t * 2.0f, mTension);
58 else return 0.5f * (o(t * 2.0f - 2.0f, mTension) + 2.0f);
59}
60
61static float bounce(float t) {
62 return t * t * 8.0f;
63}
64
65float BounceInterpolator::interpolate(float t) {
66 t *= 1.1226f;
67 if (t < 0.3535f) return bounce(t);
68 else if (t < 0.7408f) return bounce(t - 0.54719f) + 0.7f;
69 else if (t < 0.9644f) return bounce(t - 0.8526f) + 0.9f;
70 else return bounce(t - 1.0435f) + 0.95f;
71}
72
73float CycleInterpolator::interpolate(float input) {
74 return sinf(2 * mCycles * M_PI * input);
75}
76
77float DecelerateInterpolator::interpolate(float input) {
78 float result;
79 if (mFactor == 1.0f) {
80 result = 1.0f - (1.0f - input) * (1.0f - input);
81 } else {
82 result = 1.0f - pow((1.0f - input), 2 * mFactor);
83 }
84 return result;
85}
86
87float OvershootInterpolator::interpolate(float t) {
88 t -= 1.0f;
89 return t * t * ((mTension + 1) * t + mTension) + 1.0f;
90}
91
Chris Craik51d6a3d2014-12-22 17:16:56 -080092LUTInterpolator::LUTInterpolator(float* values, size_t size)
93 : mValues(values)
94 , mSize(size) {
John Reck315c3292014-05-09 19:21:04 -070095}
96
97LUTInterpolator::~LUTInterpolator() {
John Reck315c3292014-05-09 19:21:04 -070098}
99
100float LUTInterpolator::interpolate(float input) {
Doris Liu09408342016-11-29 13:04:49 -0800101 // lut position should only be at the end of the table when input is 1f.
102 float lutpos = input * (mSize - 1);
John Reck315c3292014-05-09 19:21:04 -0700103 if (lutpos >= (mSize - 1)) {
104 return mValues[mSize - 1];
105 }
106
107 float ipart, weight;
108 weight = modff(lutpos, &ipart);
109
110 int i1 = (int) ipart;
Chris Craik9db58c02015-08-19 15:19:18 -0700111 int i2 = std::min(i1 + 1, (int) mSize - 1);
John Reck315c3292014-05-09 19:21:04 -0700112
John Reck7aab5a12014-06-20 09:59:56 -0700113 LOG_ALWAYS_FATAL_IF(i1 < 0 || i2 < 0, "negatives in interpolation!"
114 " i1=%d, i2=%d, input=%f, lutpos=%f, size=%zu, values=%p, ipart=%f, weight=%f",
Chris Craik51d6a3d2014-12-22 17:16:56 -0800115 i1, i2, input, lutpos, mSize, mValues.get(), ipart, weight);
John Reck7aab5a12014-06-20 09:59:56 -0700116
John Reck315c3292014-05-09 19:21:04 -0700117 float v1 = mValues[i1];
118 float v2 = mValues[i2];
119
120 return MathUtils::lerp(v1, v2, weight);
121}
122
123
John Recke45b1fd2014-04-15 09:50:16 -0700124} /* namespace uirenderer */
125} /* namespace android */