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 GetEstimatedDuration(); 34 35 protected: 36 RSSpringModel() = default; 37 void EstimateDuration(); 38 void CalculateSpringParameters(); 39 40 // physical parameters of spring-damper model 41 float response_ { 0.0f }; 42 float dampingRatio_ { 0.0f }; 43 RSAnimatableType initialOffset_; 44 RSAnimatableType initialVelocity_; 45 46 // estimated duration until the spring is at rest 47 float minimumAmplitudeRatio_ { 0.001f }; 48 float estimatedDuration_ { -1.0f }; 49 50 private: 51 // calculated intermediate coefficient 52 float coeffDecay_ { 0.0f }; 53 RSAnimatableType coeffScale_ {}; 54 float dampedAngularVelocity_ { 0.0f }; 55 RSAnimatableType coeffScaleAlt_ {}; 56 float coeffDecayAlt_ { 0.0f }; 57 }; 58 } // namespace Rosen 59 } // namespace OHOS 60 61 #endif // ROSEN_ENGINE_CORE_ANIMATION_RS_SPRING_MODEL_H 62