1 /* 2 * Copyright (c) 2022-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 #ifndef ROSEN_ENGINE_CORE_ANIMATION_RS_SPRING_MODEL_H 17 #define ROSEN_ENGINE_CORE_ANIMATION_RS_SPRING_MODEL_H 18 19 #include "common/rs_macros.h" 20 21 namespace OHOS { 22 namespace Rosen { 23 // RSAnimatableType should have following operators: + - *float == 24 template<typename RSAnimatableType> 25 class RSB_EXPORT RSSpringModel { 26 public: 27 explicit RSSpringModel(float response, float dampingRatio, const RSAnimatableType& initialOffset, 28 const RSAnimatableType& initialVelocity, float minimumAmplitude); 29 30 virtual ~RSSpringModel() = default; 31 32 RSAnimatableType CalculateDisplacement(double time) const; 33 float EstimateDuration() const; 34 35 protected: 36 RSSpringModel() = default; 37 void CalculateSpringParameters(); 38 39 // physical parameters of spring-damper model 40 float response_ { 0.0f }; 41 float dampingRatio_ { 0.0f }; 42 RSAnimatableType initialOffset_; 43 RSAnimatableType initialVelocity_; 44 45 // estimated duration until the spring is at rest 46 float minimumAmplitudeRatio_ { 0.001f }; 47 48 private: EstimateDurationForUnderDampedModel()49 float EstimateDurationForUnderDampedModel() const 50 { 51 return 0.0f; 52 } EstimateDurationForCriticalDampedModel()53 float EstimateDurationForCriticalDampedModel() const 54 { 55 return 0.0f; 56 } EstimateDurationForOverDampedModel()57 float EstimateDurationForOverDampedModel() const 58 { 59 return 0.0f; 60 } BinarySearchTime(float left,float right,RSAnimatableType target)61 float BinarySearchTime(float left, float right, RSAnimatableType target) const 62 { 63 return 0.0f; 64 } 65 // calculated intermediate coefficient 66 float coeffDecay_ { 0.0f }; 67 RSAnimatableType coeffScale_ {}; 68 float dampedAngularVelocity_ { 0.0f }; 69 RSAnimatableType coeffScaleAlt_ {}; 70 float coeffDecayAlt_ { 0.0f }; 71 }; 72 73 // only used in interpolatingSpring animation 74 template<> 75 RSB_EXPORT float RSSpringModel<float>::EstimateDuration() const; 76 template<> 77 RSB_EXPORT float RSSpringModel<float>::BinarySearchTime(float left, float right, float target) const; 78 template<> 79 RSB_EXPORT float RSSpringModel<float>::EstimateDurationForUnderDampedModel() const; 80 template<> 81 RSB_EXPORT float RSSpringModel<float>::EstimateDurationForCriticalDampedModel() const; 82 template<> 83 RSB_EXPORT float RSSpringModel<float>::EstimateDurationForOverDampedModel() const; 84 } // namespace Rosen 85 } // namespace OHOS 86 87 #endif // ROSEN_ENGINE_CORE_ANIMATION_RS_SPRING_MODEL_H 88