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.001
28 : RSSpringModel<float>(response, dampingRatio, -1, initialVelocity, 0.001)
29 {
30 EstimateDuration();
31 }
32
Marshalling(Parcel & parcel) const33 bool RSSpringInterpolator::Marshalling(Parcel& parcel) const
34 {
35 if (!parcel.WriteUint16(InterpolatorType::SPRING)) {
36 ROSEN_LOGE("RSSpringInterpolator::Marshalling, Write type failed");
37 return false;
38 }
39 if (!(parcel.WriteFloat(response_) && parcel.WriteFloat(dampingRatio_) && parcel.WriteFloat(initialVelocity_))) {
40 ROSEN_LOGE("RSSpringInterpolator::Marshalling, Write value failed");
41 return false;
42 }
43 return true;
44 }
45
Unmarshalling(Parcel & parcel)46 RSSpringInterpolator* RSSpringInterpolator::Unmarshalling(Parcel& parcel)
47 {
48 float response, dampingRatio, initialVelocity;
49 if (!(parcel.ReadFloat(response) && parcel.ReadFloat(dampingRatio) && parcel.ReadFloat(initialVelocity))) {
50 ROSEN_LOGE("RSSpringInterpolator::Unmarshalling, SpringInterpolator failed");
51 return nullptr;
52 }
53 auto ret = new RSSpringInterpolator(response, dampingRatio, initialVelocity);
54 return ret;
55 }
56
Interpolate(float fraction) const57 float RSSpringInterpolator::Interpolate(float fraction) const
58 {
59 if (fraction <= 0) {
60 return 0;
61 } else if (fraction >= 1.0f) {
62 return 1.0f;
63 }
64 // map [0, 1] to [0, duration], and calculate the output value
65 double mappedTime = fraction * estimatedDuration_;
66 return 1.0f + CalculateDisplacement(mappedTime);
67 }
68 } // namespace Rosen
69 } // namespace OHOS
70