1 /*
2 * Copyright (c) 2020-2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "animator/interpolation.h"
17
18 #include "gfx_utils/graphic_math.h"
19
20 namespace OHOS {
21 /* B(t) = P0*(1-t)^3 + 3*P1*t*(1-t)^2 + 3*P2*t^2*(1-t) + P3*t^3 */
GetBezierInterpolation(int16_t t,int16_t u0,int16_t u1,int16_t u2,int16_t u3)22 int16_t Interpolation::GetBezierInterpolation(int16_t t, int16_t u0, int16_t u1, int16_t u2, int16_t u3)
23 {
24 int64_t invT = 1024 - t; // Intergerlize the standard equation, 1.0f is devided into 1024 parts
25 int64_t invT2 = invT * invT;
26 int64_t invT3 = invT2 * invT;
27 int64_t t2 = t * t;
28 int64_t t3 = t2 * t;
29
30 int64_t ret = invT3 * u0;
31 ret += BEZIER_COEFFICIENT * invT2 * t * u1;
32 ret += BEZIER_COEFFICIENT * invT * t2 * u2;
33 ret += t3 * u3;
34 ret = ret >> 30; // 30: cubic shift
35 return static_cast<int16_t>(ret);
36 }
37
38 /* B(t) = P0*(1-t)^3 + 3*P1*t*(1-t)^2 + 3*P2*t^2*(1-t) + P3*t^3 */
GetBezierInterpolation(float t,float u0,float u1,float u2,float u3)39 float Interpolation::GetBezierInterpolation(float t, float u0, float u1, float u2, float u3)
40 {
41 float invT = 1 - t;
42 float invT2 = invT * invT;
43 float invT3 = invT2 * invT;
44 float t2 = t * t;
45 float t3 = t2 * t;
46
47 float ret = invT3 * u0;
48 ret += BEZIER_COEFFICIENT * invT2 * t * u1;
49 ret += BEZIER_COEFFICIENT * invT * t2 * u2;
50 ret += t3 * u3;
51 return ret;
52 }
53
54 /* B(t) = 3(P1-P0)(1-t)^2 + 6(P2-P1)t(1-t) + 3(P3-P2)t^2 */
GetBezierDerivative(float t,float u0,float u1,float u2,float u3)55 float Interpolation::GetBezierDerivative(float t, float u0, float u1, float u2, float u3)
56 {
57 float invT = 1 - t;
58 float d0 = u1 - u0;
59 float d1 = u2 - u1;
60 float d2 = u3 - u2;
61 constexpr int8_t BESSEL_SQUARE_COEFFICIENT = (BEZIER_COEFFICIENT - 1) * BEZIER_COEFFICIENT;
62
63 float ret = BEZIER_COEFFICIENT * d0 * invT * invT;
64 ret += BESSEL_SQUARE_COEFFICIENT * d1 * invT * t;
65 ret += BEZIER_COEFFICIENT * d2 * t * t;
66 return ret;
67 }
68
GetBezierY(float x,float x1,float y1,float x2,float y2)69 float Interpolation::GetBezierY(float x, float x1, float y1, float x2, float y2)
70 {
71 /* P={x,y}; P0={0,0}; P1={x1,y1}; P2={x2,y2}; P3={1,1}
72 * P = P0*(1-t)^3 + 3*P1*t*(1-t)^2 + 3*P2*t^2*(1-t) + P3*t^3
73 */
74 float t = x;
75 float xt = GetBezierInterpolation(t, 0, x1, x2, 1);
76 /* Attention: precision must be carefully selected
77 * too small may lead to misconvergence and a decrease of performance
78 * too large may cause the curve rugged even make some points outlier */
79 constexpr float PRECISION = 0.05f; // 0.05f make serveral outliers near inflection point
80
81 /* Newton Method to solve t from x */
82 while (MATH_ABS(xt - x) > PRECISION) {
83 t = t + (x - xt) / GetBezierDerivative(t, 0, x1, x2, 1);
84 xt = GetBezierInterpolation(t, 0, x1, x2, 1);
85 }
86 return GetBezierInterpolation(t, 0, y1, y2, 1);
87 }
88 } // namespace OHOS
89