• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <cmath>
22 #include <cutils/log.h>
23 
24 #include "utils/MathUtils.h"
25 
26 namespace android {
27 namespace uirenderer {
28 
createDefaultInterpolator()29 Interpolator* Interpolator::createDefaultInterpolator() {
30     return new AccelerateDecelerateInterpolator();
31 }
32 
interpolate(float input)33 float AccelerateDecelerateInterpolator::interpolate(float input) {
34     return (float)(cosf((input + 1) * M_PI) / 2.0f) + 0.5f;
35 }
36 
interpolate(float input)37 float AccelerateInterpolator::interpolate(float input) {
38     if (mFactor == 1.0f) {
39         return input * input;
40     } else {
41         return pow(input, mDoubleFactor);
42     }
43 }
44 
interpolate(float t)45 float AnticipateInterpolator::interpolate(float t) {
46     return t * t * ((mTension + 1) * t - mTension);
47 }
48 
a(float t,float s)49 static float a(float t, float s) {
50     return t * t * ((s + 1) * t - s);
51 }
52 
o(float t,float s)53 static float o(float t, float s) {
54     return t * t * ((s + 1) * t + s);
55 }
56 
interpolate(float t)57 float 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 
bounce(float t)62 static float bounce(float t) {
63     return t * t * 8.0f;
64 }
65 
interpolate(float t)66 float 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 
interpolate(float input)74 float CycleInterpolator::interpolate(float input) {
75     return sinf(2 * mCycles * M_PI * input);
76 }
77 
interpolate(float input)78 float 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 
interpolate(float t)88 float OvershootInterpolator::interpolate(float t) {
89     t -= 1.0f;
90     return t * t * ((mTension + 1) * t + mTension) + 1.0f;
91 }
92 
LUTInterpolator(float * values,size_t size)93 LUTInterpolator::LUTInterpolator(float* values, size_t size) {
94     mValues = values;
95     mSize = size;
96 }
97 
~LUTInterpolator()98 LUTInterpolator::~LUTInterpolator() {
99     delete mValues;
100     mValues = 0;
101 }
102 
interpolate(float input)103 float 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, (int) mSize - 1);
114 
115     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 
119     float v1 = mValues[i1];
120     float v2 = mValues[i2];
121 
122     return MathUtils::lerp(v1, v2, weight);
123 }
124 
125 
126 } /* namespace uirenderer */
127 } /* namespace android */
128