• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef ROSEN_ENGINE_CORE_ANIMATION_RS_SPRING_INTERPOLATOR_H
17 #define ROSEN_ENGINE_CORE_ANIMATION_RS_SPRING_INTERPOLATOR_H
18 
19 #include "animation/rs_interpolator.h"
20 
21 namespace OHOS {
22 namespace Rosen {
23 class RSSpringInterpolator : public RSInterpolator {
24 public:
RSSpringInterpolator(float response,float dampingRatio,float initialVelocity)25     RSSpringInterpolator(float response, float dampingRatio, float initialVelocity)
26         // initialOffset: 1, minimumAmplitude: 0.001
27         : RSSpringInterpolator(response, dampingRatio, 1, 0.001, initialVelocity, 0)
28     {}
29 
~RSSpringInterpolator()30     ~RSSpringInterpolator() override {};
31 
Interpolate(float input)32     float Interpolate(float input) const override
33     {
34         return InterpolateImpl(input * duration_);
35     }
36     bool Marshalling(Parcel& parcel) const override;
37 #ifdef ROSEN_OHOS
38     static RSSpringInterpolator* Unmarshalling(Parcel& parcel);
39 #endif
40 protected:
41     explicit RSSpringInterpolator(float response, float dampingRatio, float initialOffset, float minimumAmplitude,
42         float initialVelocity, float duration);
43     void CalculateSpringParameters();
44     float InterpolateImpl(double seconds) const;
45 
46     float response_;
47     float dampingRatio_;
48     float initialVelocity_;
49     float initialOffset_;
50     float minimumAmplitude_;
51     float duration_;
52 
53 private:
54     void EstimateDuration();
55     double CalculateDisplacement(double mappedTime) const;
56 
57     // common intermediate coefficient
58     float coeffDecay_ { 0.0f };
59     float coeffScale_ { 0.0f };
60     // only for under-damped systems
61     float dampedAngularVelocity_ { 0.0f };
62     // only for over-damped systems
63     float coeffScaleMinus_ { 0.0f };
64     float coeffDecayMinus_ { 0.0f };
65 };
66 
67 class RSValueSpringInterpolator : public RSSpringInterpolator {
68 public:
RSValueSpringInterpolator(float response,float dampingRatio,float initialOffset,float minimumAmplitude)69     explicit RSValueSpringInterpolator(float response, float dampingRatio, float initialOffset, float minimumAmplitude)
70         // initialVelocity: 0, duration: 0
71         : RSSpringInterpolator(response, dampingRatio, initialOffset, minimumAmplitude, 0, 0)
72     {}
73 
~RSValueSpringInterpolator()74     ~RSValueSpringInterpolator() override {};
75 
76     void UpdateParameters(
77         float response, float dampingRatio, float initialVelocity, float initialOffset, float minimumAmplitude);
78 
79     float GetInstantaneousVelocity(int64_t microseconds) const;
80 
81     float InterpolateValue(int64_t microseconds) const;
82 
83     int64_t GetEstimatedDuration() const;
84 
SetReversed(bool isReversed)85     void SetReversed(bool isReversed)
86     {
87         isReversed_ = isReversed;
88     }
89 
90 private:
91     bool isReversed_ = false;
92 };
93 } // namespace Rosen
94 } // namespace OHOS
95 
96 #endif // ROSEN_ENGINE_CORE_ANIMATION_RS_SPRING_INTERPOLATOR_H
97