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 FOUNDATION_ACE_FRAMEWORKS_CORE_ANIMATION_SPRING_MODEL_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_ANIMATION_SPRING_MODEL_H 18 19 #include "base/memory/ace_type.h" 20 21 namespace OHOS::Ace { 22 23 enum class SpringModelType { 24 CRITICAL_DAMPED, // critical damped calculation model 25 UNDER_DAMPED, // under damped calculation model 26 OVER_DAMPED, // over damping calculation model 27 }; 28 29 class SpringProperty : public AceType { 30 DECLARE_ACE_TYPE(SpringProperty, AceType); 31 32 public: SpringProperty(double mass,double stiffness,double damping)33 SpringProperty(double mass, double stiffness, double damping) 34 : mass_(mass), stiffness_(stiffness), damping_(damping) 35 {} 36 37 SpringProperty() = default; 38 39 ~SpringProperty() override = default; 40 41 void SetMass(double mass); 42 43 double Mass() const; 44 45 void SetStiffness(double stiffness); 46 47 double Stiffness() const; 48 49 void SetDamping(double damping); 50 51 double Damping() const; 52 53 bool IsValid() const; 54 55 private: 56 // Default stiffness of spring. 57 static constexpr double DEFAULT_STIFFNESS = 228.0; 58 // Default damping of spring. 59 static constexpr double DEFAULT_DAMPING = 30.0; 60 // Default mass of spring 61 static constexpr double DEFAULT_MASS = 1.0; 62 63 // the mass of the spring. 64 double mass_ = DEFAULT_MASS; 65 // the stiffness of spring, generally, a spring stiffness is constant. 66 double stiffness_ = DEFAULT_STIFFNESS; 67 // damping ratio of spring 68 double damping_ = DEFAULT_DAMPING; 69 }; 70 71 class SpringModel : public AceType { 72 DECLARE_ACE_TYPE(SpringModel, AceType); 73 74 public: 75 // calculate position, the unit of time is second. 76 virtual double Position(double time) const = 0; 77 78 // calculate velocity, the unit of time is second. 79 virtual double Velocity(double time) const = 0; 80 81 // get current calculation type. 82 virtual SpringModelType GetType() const = 0; 83 84 /** 85 * Judge the type of the spring and get the calculation according to the type. 86 */ 87 static RefPtr<SpringModel> Build(double distance, double velocity, const RefPtr<SpringProperty>& spring); 88 }; 89 90 // Critical Damped calculation model. 91 class CriticalDampedModel : public SpringModel { 92 DECLARE_ACE_TYPE(CriticalDampedModel, SpringModel); 93 94 public: 95 CriticalDampedModel(double distance, double velocity, const RefPtr<SpringProperty>& spring); 96 97 ~CriticalDampedModel() override = default; 98 99 double Position(double time) const override; 100 101 double Velocity(double time) const override; 102 103 SpringModelType GetType() const override; 104 105 private: 106 double r_ = 0.0; 107 double c1_ = 0.0; 108 double c2_ = 0.0; 109 }; 110 111 // Overdamping calculation model. 112 class OverdampedModel : public SpringModel { 113 DECLARE_ACE_TYPE(OverdampedModel, SpringModel); 114 115 public: 116 OverdampedModel(double distance, double velocity, const RefPtr<SpringProperty>& spring); 117 118 ~OverdampedModel() override = default; 119 120 double Position(double time) const override; 121 122 double Velocity(double time) const override; 123 124 SpringModelType GetType() const override; 125 126 private: 127 double r1_ = 0.0; 128 double r2_ = 0.0; 129 double c1_ = 0.0; 130 double c2_ = 0.0; 131 }; 132 133 // Underdamped calculation model 134 class UnderdampedModel : public SpringModel { 135 DECLARE_ACE_TYPE(UnderdampedModel, SpringModel); 136 137 public: 138 UnderdampedModel(double distance, double velocity, const RefPtr<SpringProperty>& spring); 139 140 ~UnderdampedModel() override = default; 141 142 double Position(double time) const override; 143 144 double Velocity(double time) const override; 145 146 SpringModelType GetType() const override; 147 148 private: 149 double w_ = 0.0; 150 double r_ = 0.0; 151 double c1_ = 0.0; 152 double c2_ = 0.0; 153 }; 154 155 } // namespace OHOS::Ace 156 157 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_ANIMATION_SPRING_MODEL_H 158