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/spring_curve.h"
17 #include "core/common/container.h"
18 
19 namespace OHOS::Ace {
20 namespace {
21 
22 constexpr float DEFAULT_VALUE_THRESHOLD = 0.001f;
23 constexpr float VELOCITY_THRESHOLD_RATIO = 25.0f;
24 constexpr float DEFAULT_START_POSITION = 0.0f;
25 constexpr float DEFAULT_END_POSITION = 1.0f;
26 constexpr int32_t DEFAULT_ESTIMATE_STEPS = 100;
27 constexpr float FRACTION_PARAMETER_MAX = 1.0f;
28 constexpr float FRACTION_PARAMETER_MIN = 0.0f;
29 constexpr float MAX_ESTIMATE_DURATION = 1000.0f;
30 constexpr float HALF = 0.5f;
31 
32 } // namespace
33 
SpringCurve(float velocity,float mass,float stiffness,float damping)34 SpringCurve::SpringCurve(float velocity, float mass, float stiffness, float damping)
35     : velocity_(velocity), mass_(mass), stiffness_(stiffness), damping_(damping)
36 {
37     property_ = AceType::MakeRefPtr<SpringProperty>(mass_, stiffness_, damping_);
38     valueThreshold_ = (DEFAULT_END_POSITION - DEFAULT_START_POSITION) * DEFAULT_VALUE_THRESHOLD;
39     SetEndPosition(DEFAULT_END_POSITION, velocity_);
40 }
41 
SetEndPosition(float endPosition,float startVelocity)42 void SpringCurve::SetEndPosition(float endPosition, float startVelocity)
43 {
44     startPosition_ = 0.0f;
45     endPosition_ = endPosition;
46     currentVelocity_ = startVelocity;
47     currentPosition_ = startPosition_;
48     velocityThreshold_ = valueThreshold_ * VELOCITY_THRESHOLD_RATIO;
49     if (Container::LessThanAPIVersion(PlatformVersion::VERSION_TEN)) {
50         solution_ = SpringModel::Build(endPosition_, startVelocity, property_);
51     } else {
52         solution_ = SpringModel::Build(startPosition_ - endPosition_, startVelocity, property_);
53     }
54     if (!solution_) {
55         LOGW("Create springCurve error, %{public}s", ToString().c_str());
56         return;
57     }
58     InitEstimateDuration();
59 }
60 
InitEstimateDuration()61 void SpringCurve::InitEstimateDuration()
62 {
63     float velocity = 0.0f;
64     float time = 1.0f / DEFAULT_ESTIMATE_STEPS;
65     if (Container::LessThanAPIVersion(PlatformVersion::VERSION_TEN)) {
66         float position = 0.0f;
67         for (int32_t i = 1; i < DEFAULT_ESTIMATE_STEPS; ++i) {
68             position = endPosition_ - solution_->Position(time * i);
69             velocity = solution_->Velocity(time * i);
70             if (NearEqual(position, endPosition_, valueThreshold_) && NearZero(velocity, velocityThreshold_)) {
71                 estimateDuration_ = time * i;
72                 break;
73             }
74         }
75         return;
76     }
77     // Binary search to estimate duration
78     float minDuration = 0.0f;
79     float maxDuration = MAX_ESTIMATE_DURATION;
80     float positionChange = 0.0f;
81     while (maxDuration - minDuration >= time) {
82         auto duration = (minDuration + maxDuration) * HALF;
83         positionChange = solution_->Position(duration);
84         velocity = solution_->Velocity(duration);
85         if (NearZero(positionChange, valueThreshold_) && NearZero(velocity, velocityThreshold_)) {
86             maxDuration = duration;
87         } else {
88             minDuration = duration;
89         }
90     }
91     estimateDuration_ = maxDuration;
92 }
93 
MoveInternal(float time)94 float SpringCurve::MoveInternal(float time)
95 {
96     if (time < FRACTION_PARAMETER_MIN || time > FRACTION_PARAMETER_MAX) {
97         LOGE("SpringCurve MoveInternal: time is less than 0 or larger than 1, return 1");
98         return FRACTION_PARAMETER_MAX;
99     }
100     CHECK_NULL_RETURN(solution_, endPosition_);
101     if (Container::LessThanAPIVersion(PlatformVersion::VERSION_TEN)) {
102         currentPosition_ = endPosition_ - solution_->Position(time * estimateDuration_);
103     } else {
104         currentPosition_ = endPosition_ + solution_->Position(time * estimateDuration_);
105     }
106     currentVelocity_ = solution_->Velocity(time * estimateDuration_);
107     if (NearEqual(currentPosition_, endPosition_, valueThreshold_) &&
108         NearZero(currentVelocity_, velocityThreshold_)) {
109         currentPosition_ = endPosition_;
110         currentVelocity_ = 0.0f;
111     }
112     return currentPosition_;
113 }
114 
ToString()115 const std::string SpringCurve::ToString()
116 {
117     std::string curveString("spring");
118     std::string comma(",");
119     curveString.append(std::string("(") + std::to_string(velocity_) + comma + std::to_string(mass_)
120         + comma + std::to_string(stiffness_) + comma + std::to_string(damping_) + std::string(")"));
121     return curveString;
122 }
123 
124 } // namespace OHOS::Ace