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