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
18 namespace OHOS::Ace {
19 namespace {
20
21 constexpr float DEFAULT_VALUE_THRESHOLD = 0.001f;
22 constexpr float VELOCITY_THRESHOLD_RATIO = 25.0f;
23 constexpr float DEFAULT_START_POSITION = 0.0f;
24 constexpr float DEFAULT_END_POSITION = 1.0f;
25 constexpr int32_t DEFAULT_ESTIMATE_STEPS = 100;
26 constexpr float FRACTION_PARAMETER_MAX = 1.0f;
27 constexpr float FRACTION_PARAMETER_MIN = 0.0f;
28
29 } // namespace
30
SpringCurve(float velocity,float mass,float stiffness,float damping)31 SpringCurve::SpringCurve(float velocity, float mass, float stiffness, float damping)
32 : velocity_(velocity), mass_(mass), stiffness_(stiffness), damping_(damping)
33 {
34 property_ = AceType::MakeRefPtr<SpringProperty>(mass_, stiffness_, damping_);
35 valueThreshold_ = (DEFAULT_END_POSITION - DEFAULT_START_POSITION) * DEFAULT_VALUE_THRESHOLD;
36 SetEndPosition(DEFAULT_END_POSITION, velocity_);
37 }
38
SetEndPosition(float endPosition,float startVelocity)39 void SpringCurve::SetEndPosition(float endPosition, float startVelocity)
40 {
41 startPosition_ = 0.0f;
42 endPosition_ = endPosition;
43 currentVelocity_ = startVelocity;
44 currentPosition_ = startPosition_;
45 velocityThreshold_ = valueThreshold_ * VELOCITY_THRESHOLD_RATIO;
46 solution_ = SpringModel::Build(endPosition_, startVelocity, property_);
47 InitEstimateDuration();
48 }
49
InitEstimateDuration()50 void SpringCurve::InitEstimateDuration()
51 {
52 float position = 0.0f;
53 float velocity = 0.0f;
54 float time = 1.0f / DEFAULT_ESTIMATE_STEPS;
55 for (int32_t i = 1; i < DEFAULT_ESTIMATE_STEPS; ++i) {
56 position = endPosition_ - solution_->Position(time * i);
57 velocity = solution_->Velocity(time * i);
58 if (NearEqual(position, endPosition_, valueThreshold_) && NearZero(velocity, velocityThreshold_)) {
59 estimateDuration_ = time * i;
60 break;
61 }
62 }
63 }
64
MoveInternal(float time)65 float SpringCurve::MoveInternal(float time)
66 {
67 if (time < FRACTION_PARAMETER_MIN || time > FRACTION_PARAMETER_MAX) {
68 LOGE("SpringCurve MoveInternal: time is less than 0 or larger than 1, return 1");
69 return FRACTION_PARAMETER_MAX;
70 }
71 currentPosition_ = endPosition_ - solution_->Position(time * estimateDuration_);
72 currentVelocity_ = solution_->Velocity(time * estimateDuration_);
73 if (NearEqual(currentPosition_, endPosition_, valueThreshold_) &&
74 NearZero(currentVelocity_, velocityThreshold_)) {
75 currentPosition_ = endPosition_;
76 currentVelocity_ = 0.0f;
77 }
78 return currentPosition_;
79 }
80
ToString()81 const std::string SpringCurve::ToString()
82 {
83 std::string curveString("spring");
84 std::string comma(",");
85 curveString.append(std::string("(") + std::to_string(velocity_) + comma + std::to_string(mass_)
86 + comma + std::to_string(stiffness_) + comma + std::to_string(damping_) + std::string(")"));
87 return curveString;
88 }
89
90 } // namespace OHOS::Ace