1 /*
2  * Copyright (c) 2021-2023 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 "animation/rs_spring_interpolator.h"
17 
18 #include <algorithm>
19 #include <cmath>
20 
21 #include "platform/common/rs_log.h"
22 
23 namespace OHOS {
24 namespace Rosen {
25 
RSSpringInterpolator(float response,float dampingRatio,float initialVelocity)26 RSSpringInterpolator::RSSpringInterpolator(float response, float dampingRatio, float initialVelocity)
27     // initialOffset: 1, minimumAmplitude: 0.0001
28     : RSSpringModel<float>(response, dampingRatio, -1, initialVelocity, 0.0001)
29 {
30     estimatedDuration_ = EstimateDuration();
31 }
32 
RSSpringInterpolator(uint64_t id,float response,float dampingRatio,float initialVelocity)33 RSSpringInterpolator::RSSpringInterpolator(uint64_t id, float response, float dampingRatio, float initialVelocity)
34     // initialOffset: 1, minimumAmplitude: 0.0001
35     : RSSpringModel<float>(response, dampingRatio, -1, initialVelocity, 0.0001), RSInterpolator(id)
36 {
37     estimatedDuration_ = EstimateDuration();
38 }
39 
Marshalling(Parcel & parcel) const40 bool RSSpringInterpolator::Marshalling(Parcel& parcel) const
41 {
42     if (!parcel.WriteUint16(InterpolatorType::SPRING)) {
43         ROSEN_LOGE("RSSpringInterpolator::Marshalling, Write type failed");
44         return false;
45     }
46     if (!parcel.WriteUint64(id_)) {
47         ROSEN_LOGE("RSSpringInterpolator::Marshalling, Write id failed");
48         return false;
49     }
50     if (!(parcel.WriteFloat(response_) && parcel.WriteFloat(dampingRatio_) && parcel.WriteFloat(initialVelocity_))) {
51         ROSEN_LOGE("RSSpringInterpolator::Marshalling, Write value failed");
52         return false;
53     }
54     return true;
55 }
56 
Unmarshalling(Parcel & parcel)57 RSSpringInterpolator* RSSpringInterpolator::Unmarshalling(Parcel& parcel)
58 {
59     uint64_t id = parcel.ReadUint64();
60     float response, dampingRatio, initialVelocity;
61     if (!(parcel.ReadFloat(response) && parcel.ReadFloat(dampingRatio) && parcel.ReadFloat(initialVelocity))) {
62         ROSEN_LOGE("RSSpringInterpolator::Unmarshalling, SpringInterpolator failed");
63         return nullptr;
64     }
65     auto ret = new RSSpringInterpolator(id, response, dampingRatio, initialVelocity);
66     return ret;
67 }
68 
InterpolateImpl(float fraction) const69 float RSSpringInterpolator::InterpolateImpl(float fraction) const
70 {
71     if (fraction <= 0) {
72         return 0;
73     } else if (fraction >= 1.0f) {
74         return 1.0f;
75     }
76     // map [0, 1] to [0, duration], and calculate the output value
77     double mappedTime = fraction * estimatedDuration_;
78     return 1.0f + CalculateDisplacement(mappedTime);
79 }
80 } // namespace Rosen
81 } // namespace OHOS
82