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"
24 #include <simgear/math/SGMath.hxx>
30 /// Simple linear easing.
31 double easingLinear(double t)
36 /// http://easings.net/#easeInSine
37 double easeInSine(double t)
39 return 1 - std::cos(t * M_PI_2);
42 /// http://easings.net/#easeOutSine
43 double easeOutSine(double t)
45 return std::sin(t * M_PI_2);
48 /// http://easings.net/#easeInOutSine
49 double easeInOutSine(double t)
51 return 0.5 - 0.5 * std::cos(t * M_PI);
54 template<easing_func_t easeIn, easing_func_t easeOut>
55 double easeInOut(double t)
58 return 0.5 * (*easeIn)(t);
60 return 0.5 + 0.5 * (*easeOut)(t - 1);
63 template<size_t N, bool is_odd>
66 /// http://easings.net/#easeOutCubic (N = 3)
67 /// http://easings.net/#easeOutQuint (N = 5)
69 struct easeOutImpl<N, true>
71 static double calc(double t)
73 return SGMiscd::pow<N>(t - 1) + 1;
77 /// http://easings.net/#easeOutQuad (N = 2)
78 /// http://easings.net/#easeOutQuart (N = 4)
80 struct easeOutImpl<N, false>
82 static double calc(double t)
84 return -SGMiscd::pow<N>(t - 1) + 1;
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)
93 double easeOutPow(double t)
95 return easeOutImpl<N, N & 1>::calc(t);
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)
103 double easeInOutPow(double t)
105 return easeInOut<&SGMiscd::pow<N>, &easeOutPow<N> >(t);
108 /// http://easings.net/#easeInExpo
109 double easeInExpo(double t)
111 return (t == 0) ? 0 : std::pow(2, 10 * (t - 1));
114 /// http://easings.net/#easeOutExpo
115 double easeOutExpo(double t)
117 return (t == 1) ? 1 : 1 - std::pow(2, -10 * t);
120 /// http://easings.net/#easeInCirc
121 double easeInCirc(double t)
123 return 1 - std::sqrt(1 - SGMiscd::pow<2>(t));
126 /// http://easings.net/#easeOutCirc
127 double easeOutCirc(double t)
129 return std::sqrt(1 - SGMiscd::pow<2>(t - 1));
132 static const double ease_s = 1.70158;
134 /// http://easings.net/#easeInBack
135 double easeInBack(double t)
138 return SGMiscd::pow<2>(t) * ((ease_s + 1) * t - ease_s);
141 /// http://easings.net/#easeOutBack
142 double easeOutBack(double t)
145 return SGMiscd::pow<2>(t) * ((ease_s + 1) * t + ease_s) + 1;
148 /// http://easings.net/#easeOutBack
149 double easeInElastic(double t)
158 const double s = p * 0.25;
160 return -std::pow(2, 10 * t) * std::sin((t - s) * 2 * M_PI / p);
163 /// http://easings.net/#easeOutBack
164 double easeOutElastic(double t)
172 const double s = p * 0.25;
174 return std::pow(2, -10 * t) * std::sin((t - s) * 2 * M_PI / p) + 1;
177 /// http://easings.net/#easeOutBounce
178 double easeOutBounce(double t)
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;
187 return 7.5625 * SGMiscd::pow<2>(t - 2.625/2.75) + .984375;
190 /// http://easings.net/#easeInBounce
191 double easeInBounce(double time)
193 return 1 - easeOutBounce(1 - time);
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>},
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)
229 #undef SG_ADD_EASING_IN_OUT
231 } // namespace simgear