]> git.mxchange.org Git - simgear.git/blob - simgear/props/easing_functions.cxx
Fixed a crash: the singleton needs to be instantiated the first time SGCommandMgr...
[simgear.git] / simgear / props / easing_functions.cxx
1 ///@file
2 /// Easing functions for property interpolation.
3 ///
4 /// Based on easing functions by Robert Penner
5 /// (http://www.robertpenner.com/easing)
6 //
7 // Copyright (C) 2013  Thomas Geymayer <tomgey@gmail.com>
8 //
9 // This library is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU Library General Public
11 // License as published by the Free Software Foundation; either
12 // version 2 of the License, or (at your option) any later version.
13 //
14 // This library is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17 // Library General Public License for more details.
18 //
19 // You should have received a copy of the GNU Library General Public
20 // License along with this library; if not, write to the Free Software
21 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301, USA
22
23 #include "easing_functions.hxx"
24 #include <simgear/math/SGMath.hxx>
25
26
27 namespace simgear
28 {
29
30   /// Simple linear easing.
31   double easingLinear(double t)
32   {
33     return t;
34   }
35
36   /// http://easings.net/#easeInSine
37   double easeInSine(double t)
38   {
39     return 1 - std::cos(t * M_PI_2);
40   }
41
42   /// http://easings.net/#easeOutSine
43   double easeOutSine(double t)
44   {
45     return std::sin(t * M_PI_2);
46   }
47
48   /// http://easings.net/#easeInOutSine
49   double easeInOutSine(double t)
50   {
51     return 0.5 - 0.5 * std::cos(t * M_PI);
52   }
53
54   template<easing_func_t easeIn, easing_func_t easeOut>
55   double easeInOut(double t)
56   {
57     if( (t *= 2) < 1 )
58       return 0.5 * (*easeIn)(t);
59     else
60       return 0.5 + 0.5 * (*easeOut)(t - 1);
61   }
62
63   template<size_t N, bool is_odd>
64   struct easeOutImpl;
65
66   /// http://easings.net/#easeOutCubic (N = 3)
67   /// http://easings.net/#easeOutQuint (N = 5)
68   template<size_t N>
69   struct easeOutImpl<N, true>
70   {
71     static double calc(double t)
72     {
73       return SGMiscd::pow<N>(t - 1) + 1;
74     }
75   };
76
77   /// http://easings.net/#easeOutQuad  (N = 2)
78   /// http://easings.net/#easeOutQuart (N = 4)
79   template<size_t N>
80   struct easeOutImpl<N, false>
81   {
82     static double calc(double t)
83     {
84       return -SGMiscd::pow<N>(t - 1) + 1;
85     }
86   };
87
88   /// http://easings.net/#easeOutQuad  (N = 2)
89   /// http://easings.net/#easeOutCubic (N = 3)
90   /// http://easings.net/#easeOutQuart (N = 4)
91   /// http://easings.net/#easeOutQuint (N = 5)
92   template<size_t N>
93   double easeOutPow(double t)
94   {
95     return easeOutImpl<N, N & 1>::calc(t);
96   }
97
98   /// http://easings.net/#easeInOutQuad  (N = 2)
99   /// http://easings.net/#easeInOutCubic (N = 3)
100   /// http://easings.net/#easeInOutQuart (N = 4)
101   /// http://easings.net/#easeInOutQuint (N = 5)
102   template<size_t N>
103   double easeInOutPow(double t)
104   {
105     return easeInOut<&SGMiscd::pow<N>, &easeOutPow<N> >(t);
106   }
107
108   /// http://easings.net/#easeInExpo
109   double easeInExpo(double t)
110   {
111     return (t == 0) ? 0 : std::pow(2, 10 * (t - 1));
112   }
113
114   /// http://easings.net/#easeOutExpo
115   double easeOutExpo(double t)
116   {
117     return (t == 1) ? 1 : 1 - std::pow(2, -10 * t);
118   }
119
120   /// http://easings.net/#easeInCirc
121   double easeInCirc(double t)
122   {
123     return 1 - std::sqrt(1 - SGMiscd::pow<2>(t));
124   }
125
126   /// http://easings.net/#easeOutCirc
127   double easeOutCirc(double t)
128   {
129     return std::sqrt(1 - SGMiscd::pow<2>(t - 1));
130   }
131
132   static const double ease_s = 1.70158;
133
134   /// http://easings.net/#easeInBack
135   double easeInBack(double t)
136   {
137
138     return SGMiscd::pow<2>(t) * ((ease_s + 1) * t - ease_s);
139   }
140
141   /// http://easings.net/#easeOutBack
142   double easeOutBack(double t)
143   {
144     t -= 1;
145     return SGMiscd::pow<2>(t) * ((ease_s + 1) * t + ease_s) + 1;
146   }
147
148   /// http://easings.net/#easeOutBack
149   double easeInElastic(double t)
150   {
151     if( t == 0 )
152       return 0;
153     if( t == 1 )
154       return 1;
155
156     t -= 1;
157     const double p = .3;
158     const double s = p * 0.25;
159
160     return -std::pow(2, 10 * t) * std::sin((t - s) * 2 * M_PI / p);
161   }
162
163   /// http://easings.net/#easeOutBack
164   double easeOutElastic(double t)
165   {
166     if( t == 0 )
167       return 0;
168     if( t == 1 )
169       return 1;
170
171     const double p = .3;
172     const double s = p * 0.25;
173
174     return std::pow(2, -10 * t) * std::sin((t - s) * 2 * M_PI / p) + 1;
175   }
176
177   /// http://easings.net/#easeOutBounce
178   double easeOutBounce(double t)
179   {
180     if( t < 1/2.75 )
181       return 7.5625 * SGMiscd::pow<2>(t);
182     else if( t < 2/2.75 )
183       return 7.5625 * SGMiscd::pow<2>(t - 1.5/2.75) + .75;
184     else if( t < 2.5/2.75 )
185       return 7.5625 * SGMiscd::pow<2>(t - 2.25/2.75) + .9375;
186     else
187       return 7.5625 * SGMiscd::pow<2>(t - 2.625/2.75) + .984375;
188   }
189
190   /// http://easings.net/#easeInBounce
191   double easeInBounce(double time)
192   {
193     return 1 - easeOutBounce(1 - time);
194   }
195
196 #define SG_ADD_EASING(name) {#name, &name},
197 #define SG_STR(str) #str
198 #define SG_ADD_EASING_IN_OUT(name)\
199           SG_ADD_EASING(easeIn##name)\
200           SG_ADD_EASING(easeOut##name)\
201           {SG_STR(easeInOut##name), &easeInOut<&easeIn##name, &easeOut##name>},
202
203   const EasingMapEntry easing_functions[] = {
204     {"linear", &easingLinear},
205     {"swing", &easeInOutSine},
206     SG_ADD_EASING_IN_OUT(Sine)
207     {"easeInQuad",    &SGMiscd::pow<2>},
208     {"easeInCubic",   &SGMiscd::pow<3>},
209     {"easeInQuart",   &SGMiscd::pow<4>},
210     {"easeInQuint",   &SGMiscd::pow<5>},
211     {"easeOutQuad",   &easeOutPow<2>},
212     {"easeOutCubic",  &easeOutPow<3>},
213     {"easeOutQuart",  &easeOutPow<4>},
214     {"easeOutQuint",  &easeOutPow<5>},
215     {"easeInOutQuad", &easeInOutPow<2>},
216     {"easeInOutCubic",&easeInOutPow<3>},
217     {"easeInOutQuart",&easeInOutPow<4>},
218     {"easeInOutQuint",&easeInOutPow<5>},
219     SG_ADD_EASING_IN_OUT(Expo)
220     SG_ADD_EASING_IN_OUT(Circ)
221     SG_ADD_EASING_IN_OUT(Back)
222     SG_ADD_EASING_IN_OUT(Elastic)
223     SG_ADD_EASING_IN_OUT(Bounce)
224     {0, 0}
225   };
226
227 #undef SG_ADD_EASING
228 #undef SG_STR
229 #undef SG_ADD_EASING_IN_OUT
230
231 } // namespace simgear