2 /// Easing functions for property interpolation.
4 /// Based on easing functions by Robert Penner
5 /// (http://www.robertpenner.com/easing)
7 // Copyright (C) 2013 Thomas Geymayer <tomgey@gmail.com>
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.
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.
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
23 #include "easing_functions.hxx"
29 // TODO move somewhere to math
31 double pow(double base)
33 return base * pow<N - 1>(base);
42 /// Simple linear easing.
43 double easingLinear(double t)
48 /// http://easings.net/#easeInSine
49 double easeInSine(double t)
51 return 1 - std::cos(t * M_PI_2);
54 /// http://easings.net/#easeOutSine
55 double easeOutSine(double t)
57 return std::sin(t * M_PI_2);
60 /// http://easings.net/#easeInOutSine
61 double easeInOutSine(double t)
63 return 0.5 - 0.5 * std::cos(t * M_PI);
66 template<easing_func_t easeIn, easing_func_t easeOut>
67 double easeInOut(double t)
70 return 0.5 * (*easeIn)(t);
72 return 0.5 + 0.5 * (*easeOut)(t - 1);
75 template<size_t N, bool is_odd>
78 /// http://easings.net/#easeOutCubic (N = 3)
79 /// http://easings.net/#easeOutQuint (N = 5)
81 struct easeOutImpl<N, true>
83 static double calc(double t)
85 return pow<N>(t - 1) + 1;
89 /// http://easings.net/#easeOutQuad (N = 2)
90 /// http://easings.net/#easeOutQuart (N = 4)
92 struct easeOutImpl<N, false>
94 static double calc(double t)
96 return -pow<N>(t - 1) + 1;
100 /// http://easings.net/#easeOutQuad (N = 2)
101 /// http://easings.net/#easeOutCubic (N = 3)
102 /// http://easings.net/#easeOutQuart (N = 4)
103 /// http://easings.net/#easeOutQuint (N = 5)
105 double easeOutPow(double t)
107 return easeOutImpl<N, N & 1>::calc(t);
110 /// http://easings.net/#easeInOutQuad (N = 2)
111 /// http://easings.net/#easeInOutCubic (N = 3)
112 /// http://easings.net/#easeInOutQuart (N = 4)
113 /// http://easings.net/#easeInOutQuint (N = 5)
115 double easeInOutPow(double t)
117 return easeInOut<&pow<N>, &easeOutPow<N> >(t);
120 /// http://easings.net/#easeInExpo
121 double easeInExpo(double t)
123 return (t == 0) ? 0 : std::pow(2, 10 * (t - 1));
126 /// http://easings.net/#easeOutExpo
127 double easeOutExpo(double t)
129 return (t == 1) ? 1 : 1 - std::pow(2, -10 * t);
132 /// http://easings.net/#easeInCirc
133 double easeInCirc(double t)
135 return 1 - std::sqrt(1 - pow<2>(t));
138 /// http://easings.net/#easeOutCirc
139 double easeOutCirc(double t)
141 return std::sqrt(1 - pow<2>(t - 1));
144 static const double ease_s = 1.70158;
146 /// http://easings.net/#easeInBack
147 double easeInBack(double t)
150 return pow<2>(t) * ((ease_s + 1) * t - ease_s);
153 /// http://easings.net/#easeOutBack
154 double easeOutBack(double t)
157 return pow<2>(t) * ((ease_s + 1) * t + ease_s) + 1;
160 /// http://easings.net/#easeOutBack
161 double easeInElastic(double t)
170 const double s = p * 0.25;
172 return -std::pow(2, 10 * t) * std::sin((t - s) * 2 * M_PI / p);
175 /// http://easings.net/#easeOutBack
176 double easeOutElastic(double t)
184 const double s = p * 0.25;
186 return std::pow(2, -10 * t) * std::sin((t - s) * 2 * M_PI / p) + 1;
189 /// http://easings.net/#easeOutBounce
190 double easeOutBounce(double t)
193 return 7.5625 * pow<2>(t);
194 else if( t < 2/2.75 )
195 return 7.5625 * pow<2>(t - 1.5/2.75) + .75;
196 else if( t < 2.5/2.75 )
197 return 7.5625 * pow<2>(t - 2.25/2.75) + .9375;
199 return 7.5625 * pow<2>(t - 2.625/2.75) + .984375;
202 /// http://easings.net/#easeInBounce
203 double easeInBounce(double time)
205 return 1 - easeOutBounce(1 - time);
208 #define SG_ADD_EASING(name) {#name, &name},
209 #define SG_STR(str) #str
210 #define SG_ADD_EASING_IN_OUT(name)\
211 SG_ADD_EASING(easeIn##name)\
212 SG_ADD_EASING(easeOut##name)\
213 {SG_STR(easeInOut##name), &easeInOut<&easeIn##name, &easeOut##name>},
215 const EasingMapEntry easing_functions[] = {
216 {"linear", &easingLinear},
217 {"swing", &easeInOutSine},
218 SG_ADD_EASING_IN_OUT(Sine)
219 {"easeInQuad", &pow<2>},
220 {"easeInCubic", &pow<3>},
221 {"easeInQuart", &pow<4>},
222 {"easeInQuint", &pow<5>},
223 {"easeOutQuad", &easeOutPow<2>},
224 {"easeOutCubic", &easeOutPow<3>},
225 {"easeOutQuart", &easeOutPow<4>},
226 {"easeOutQuint", &easeOutPow<5>},
227 {"easeInOutQuad", &easeInOutPow<2>},
228 {"easeInOutCubic",&easeInOutPow<3>},
229 {"easeInOutQuart",&easeInOutPow<4>},
230 {"easeInOutQuint",&easeInOutPow<5>},
231 SG_ADD_EASING_IN_OUT(Expo)
232 SG_ADD_EASING_IN_OUT(Circ)
233 SG_ADD_EASING_IN_OUT(Back)
234 SG_ADD_EASING_IN_OUT(Elastic)
235 SG_ADD_EASING_IN_OUT(Bounce)
241 #undef SG_ADD_EASING_IN_OUT
243 } // namespace simgear