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 #include "Interpolator.h"
18
19 #include <algorithm>
20
21 #include <log/log.h>
22
23 #include "utils/MathUtils.h"
24
25 namespace android {
26 namespace uirenderer {
27
createDefaultInterpolator()28 Interpolator* Interpolator::createDefaultInterpolator() {
29 return new AccelerateDecelerateInterpolator();
30 }
31
interpolate(float input)32 float AccelerateDecelerateInterpolator::interpolate(float input) {
33 return (float)(cosf((input + 1) * M_PI) / 2.0f) + 0.5f;
34 }
35
interpolate(float input)36 float AccelerateInterpolator::interpolate(float input) {
37 if (mFactor == 1.0f) {
38 return input * input;
39 } else {
40 return pow(input, mDoubleFactor);
41 }
42 }
43
interpolate(float t)44 float AnticipateInterpolator::interpolate(float t) {
45 return t * t * ((mTension + 1) * t - mTension);
46 }
47
a(float t,float s)48 static float a(float t, float s) {
49 return t * t * ((s + 1) * t - s);
50 }
51
o(float t,float s)52 static float o(float t, float s) {
53 return t * t * ((s + 1) * t + s);
54 }
55
interpolate(float t)56 float 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
bounce(float t)61 static float bounce(float t) {
62 return t * t * 8.0f;
63 }
64
interpolate(float t)65 float 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
interpolate(float input)73 float CycleInterpolator::interpolate(float input) {
74 return sinf(2 * mCycles * M_PI * input);
75 }
76
interpolate(float input)77 float 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
interpolate(float t)87 float OvershootInterpolator::interpolate(float t) {
88 t -= 1.0f;
89 return t * t * ((mTension + 1) * t + mTension) + 1.0f;
90 }
91
interpolate(float t)92 float PathInterpolator::interpolate(float t) {
93 if (t <= 0) {
94 return 0;
95 } else if (t >= 1) {
96 return 1;
97 }
98 // Do a binary search for the correct x to interpolate between.
99 size_t startIndex = 0;
100 size_t endIndex = mX.size() - 1;
101
102 while (endIndex > startIndex + 1) {
103 int midIndex = (startIndex + endIndex) / 2;
104 if (t < mX[midIndex]) {
105 endIndex = midIndex;
106 } else {
107 startIndex = midIndex;
108 }
109 }
110
111 float xRange = mX[endIndex] - mX[startIndex];
112 if (xRange == 0) {
113 return mY[startIndex];
114 }
115
116 float tInRange = t - mX[startIndex];
117 float fraction = tInRange / xRange;
118
119 float startY = mY[startIndex];
120 float endY = mY[endIndex];
121 return startY + (fraction * (endY - startY));
122
123 }
124
LUTInterpolator(float * values,size_t size)125 LUTInterpolator::LUTInterpolator(float* values, size_t size)
126 : mValues(values)
127 , mSize(size) {
128 }
129
~LUTInterpolator()130 LUTInterpolator::~LUTInterpolator() {
131 }
132
interpolate(float input)133 float LUTInterpolator::interpolate(float input) {
134 // lut position should only be at the end of the table when input is 1f.
135 float lutpos = input * (mSize - 1);
136 if (lutpos >= (mSize - 1)) {
137 return mValues[mSize - 1];
138 }
139
140 float ipart, weight;
141 weight = modff(lutpos, &ipart);
142
143 int i1 = (int) ipart;
144 int i2 = std::min(i1 + 1, (int) mSize - 1);
145
146 LOG_ALWAYS_FATAL_IF(i1 < 0 || i2 < 0, "negatives in interpolation!"
147 " i1=%d, i2=%d, input=%f, lutpos=%f, size=%zu, values=%p, ipart=%f, weight=%f",
148 i1, i2, input, lutpos, mSize, mValues.get(), ipart, weight);
149
150 float v1 = mValues[i1];
151 float v2 = mValues[i2];
152
153 return MathUtils::lerp(v1, v2, weight);
154 }
155
156
157 } /* namespace uirenderer */
158 } /* namespace android */
159