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 divided 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
35 uint64_t uret = (ret < 0) ? (-ret) : ret;
36 int16_t value = static_cast<int16_t>(uret >> 30); // 30: cubic shift
37 return (ret < 0) ? (-value) : value;
38 }
39
40 /* 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)41 float Interpolation::GetBezierInterpolation(float t, float u0, float u1, float u2, float u3)
42 {
43 float invT = 1 - t;
44 float invT2 = invT * invT;
45 float invT3 = invT2 * invT;
46 float t2 = t * t;
47 float t3 = t2 * t;
48
49 float ret = invT3 * u0;
50 ret += BEZIER_COEFFICIENT * invT2 * t * u1;
51 ret += BEZIER_COEFFICIENT * invT * t2 * u2;
52 ret += t3 * u3;
53 return ret;
54 }
55
56 /* 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)57 float Interpolation::GetBezierDerivative(float t, float u0, float u1, float u2, float u3)
58 {
59 float invT = 1 - t;
60 float d0 = u1 - u0;
61 float d1 = u2 - u1;
62 float d2 = u3 - u2;
63 constexpr int8_t BESSEL_SQUARE_COEFFICIENT = (BEZIER_COEFFICIENT - 1) * BEZIER_COEFFICIENT;
64
65 float ret = BEZIER_COEFFICIENT * d0 * invT * invT;
66 ret += BESSEL_SQUARE_COEFFICIENT * d1 * invT * t;
67 ret += BEZIER_COEFFICIENT * d2 * t * t;
68 return ret;
69 }
70
GetBezierY(float x,float x1,float y1,float x2,float y2)71 float Interpolation::GetBezierY(float x, float x1, float y1, float x2, float y2)
72 {
73 /* P={x,y}; P0={0,0}; P1={x1,y1}; P2={x2,y2}; P3={1,1}
74 * P = P0*(1-t)^3 + 3*P1*t*(1-t)^2 + 3*P2*t^2*(1-t) + P3*t^3
75 */
76 float t = x;
77 float xt = GetBezierInterpolation(t, 0, x1, x2, 1);
78 /* Attention: precision must be carefully selected
79 * too small may lead to misconvergence and a decrease of performance
80 * too large may cause the curve rugged even make some points outlier */
81 constexpr float PRECISION = 0.05f; // 0.05f make several outliers near inflection point
82 int8_t iterationCnt = 10; // iterate at most 10 times
83
84 /* Newton Method to solve t from x */
85 while ((MATH_ABS(xt - x) > PRECISION) && (iterationCnt-- > 0)) {
86 t = t + (x - xt) / GetBezierDerivative(t, 0, x1, x2, 1);
87 xt = GetBezierInterpolation(t, 0, x1, x2, 1);
88 }
89 return GetBezierInterpolation(t, 0, y1, y2, 1);
90 }
91 } // namespace OHOS
92