1 /*
2  * Copyright (c) 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 "core/animation/cubic_curve.h"
17 
18 namespace OHOS::Ace {
19 namespace {
20 
21 constexpr float FRACTION_PARAMETER_MAX = 1.0f;
22 constexpr float FRACTION_PARAMETER_MIN = 0.0f;
23 
24 }   // namespace
CubicCurve(float x0,float y0,float x1,float y1)25 CubicCurve::CubicCurve(float x0, float y0, float x1, float y1)
26     : x0_(x0), y0_(y0), x1_(x1), y1_(y1)
27 {}
28 
MoveInternal(float time)29 float CubicCurve::MoveInternal(float time)
30 {
31     if (std::isnan(time)) {
32         TAG_LOGW(AceLogTag::ACE_ANIMATION, "CubicCurve MoveInternal: time is nan, return 1");
33         return FRACTION_PARAMETER_MAX;
34     }
35     if (time < FRACTION_PARAMETER_MIN || time > FRACTION_PARAMETER_MAX) {
36         TAG_LOGI(AceLogTag::ACE_ANIMATION, "CubicCurve MoveInternal: time is less than 0 or larger than 1, return 1");
37         return FRACTION_PARAMETER_MAX;
38     }
39     // let P0 = (0,0), P3 = (1,1)
40     float start = 0.0f;
41     float end = 1.0f;
42     while (true) {
43         float midpoint = (start + end) / 2;
44         float estimate = CalculateCubic(x0_, x1_, midpoint);
45         if (NearEqual(time, estimate, cubicErrorBound_)) {
46             return CalculateCubic(y0_, y1_, midpoint);
47         }
48 
49         if (estimate < time) {
50             start = midpoint;
51         } else {
52             end = midpoint;
53         }
54     }
55 }
56 
ToString()57 const std::string CubicCurve::ToString()
58 {
59     std::string curveString("cubic-bezier");
60     std::string comma(",");
61     curveString.append(std::string("(") + std::to_string(x0_) + comma + std::to_string(y0_)
62         + comma + std::to_string(x1_) + comma + std::to_string(y1_) + std::string(")"));
63     return curveString;
64 }
65 
IsEqual(const RefPtr<Curve> & curve) const66 bool CubicCurve::IsEqual(const RefPtr<Curve>& curve) const
67 {
68     auto other = DynamicCast<CubicCurve>(curve);
69     if (!other) {
70         return false;
71     }
72     return NearEqual(other->GetX0(), x0_) && NearEqual(other->GetY0(), y0_)
73         && NearEqual(other->GetX1(), x1_) && NearEqual(other->GetY1(), y1_);
74 }
75 
CalculateCubic(float a,float b,float m)76 float CubicCurve::CalculateCubic(float a, float b, float m)
77 {
78     return 3.0f * a * (1.0f - m) * (1.0f - m) * m + 3.0f * b * (1.0f - m) * m * m + m * m * m;
79 }
80 
81 } // namespace OHOS::Ace
82