1/// @ref gtx_easing
2/// @file glm/gtx/easing.inl
3
4#include <cmath>
5
6namespace glm{
7
8	template <typename genType>
9	GLM_FUNC_QUALIFIER genType linearInterpolation(genType const& a)
10	{
11		// Only defined in [0, 1]
12		assert(a >= zero<genType>());
13		assert(a <= one<genType>());
14
15		return a;
16	}
17
18	template <typename genType>
19	GLM_FUNC_QUALIFIER genType quadraticEaseIn(genType const& a)
20	{
21		// Only defined in [0, 1]
22		assert(a >= zero<genType>());
23		assert(a <= one<genType>());
24
25		return a * a;
26	}
27
28	template <typename genType>
29	GLM_FUNC_QUALIFIER genType quadraticEaseOut(genType const& a)
30	{
31		// Only defined in [0, 1]
32		assert(a >= zero<genType>());
33		assert(a <= one<genType>());
34
35		return -(a * (a - static_cast<genType>(2)));
36	}
37
38	template <typename genType>
39	GLM_FUNC_QUALIFIER genType quadraticEaseInOut(genType const& a)
40	{
41		// Only defined in [0, 1]
42		assert(a >= zero<genType>());
43		assert(a <= one<genType>());
44
45		if(a < static_cast<genType>(0.5))
46		{
47			return static_cast<genType>(2) * a * a;
48		}
49		else
50		{
51			return (-static_cast<genType>(2) * a * a) + (4 * a) - one<genType>();
52		}
53	}
54
55	template <typename genType>
56	GLM_FUNC_QUALIFIER genType cubicEaseIn(genType const& a)
57	{
58		// Only defined in [0, 1]
59		assert(a >= zero<genType>());
60		assert(a <= one<genType>());
61
62		return a * a * a;
63	}
64
65	template <typename genType>
66	GLM_FUNC_QUALIFIER genType cubicEaseOut(genType const& a)
67	{
68		// Only defined in [0, 1]
69		assert(a >= zero<genType>());
70		assert(a <= one<genType>());
71
72		genType const f = a - one<genType>();
73		return f * f * f + one<genType>();
74	}
75
76	template <typename genType>
77	GLM_FUNC_QUALIFIER genType cubicEaseInOut(genType const& a)
78	{
79		// Only defined in [0, 1]
80		assert(a >= zero<genType>());
81		assert(a <= one<genType>());
82
83		if (a < static_cast<genType>(0.5))
84		{
85			return static_cast<genType>(4) * a * a * a;
86		}
87		else
88		{
89			genType const f = ((static_cast<genType>(2) * a) - static_cast<genType>(2));
90			return static_cast<genType>(0.5) * f * f * f + one<genType>();
91		}
92	}
93
94	template <typename genType>
95	GLM_FUNC_QUALIFIER genType quarticEaseIn(genType const& a)
96	{
97		// Only defined in [0, 1]
98		assert(a >= zero<genType>());
99		assert(a <= one<genType>());
100
101		return a * a * a * a;
102	}
103
104	template <typename genType>
105	GLM_FUNC_QUALIFIER genType quarticEaseOut(genType const& a)
106	{
107		// Only defined in [0, 1]
108		assert(a >= zero<genType>());
109		assert(a <= one<genType>());
110
111		genType const f = (a - one<genType>());
112		return f * f * f * (one<genType>() - a) + one<genType>();
113	}
114
115	template <typename genType>
116	GLM_FUNC_QUALIFIER genType quarticEaseInOut(genType const& a)
117	{
118		// Only defined in [0, 1]
119		assert(a >= zero<genType>());
120		assert(a <= one<genType>());
121
122		if(a < static_cast<genType>(0.5))
123		{
124			return static_cast<genType>(8) * a * a * a * a;
125		}
126		else
127		{
128			genType const f = (a - one<genType>());
129			return -static_cast<genType>(8) * f * f * f * f + one<genType>();
130		}
131	}
132
133	template <typename genType>
134	GLM_FUNC_QUALIFIER genType quinticEaseIn(genType const& a)
135	{
136		// Only defined in [0, 1]
137		assert(a >= zero<genType>());
138		assert(a <= one<genType>());
139
140		return a * a * a * a * a;
141	}
142
143	template <typename genType>
144	GLM_FUNC_QUALIFIER genType quinticEaseOut(genType const& a)
145	{
146		// Only defined in [0, 1]
147		assert(a >= zero<genType>());
148		assert(a <= one<genType>());
149
150		genType const f = (a - one<genType>());
151		return f * f * f * f * f + one<genType>();
152	}
153
154	template <typename genType>
155	GLM_FUNC_QUALIFIER genType quinticEaseInOut(genType const& a)
156	{
157		// Only defined in [0, 1]
158		assert(a >= zero<genType>());
159		assert(a <= one<genType>());
160
161		if(a < static_cast<genType>(0.5))
162		{
163			return static_cast<genType>(16) * a * a * a * a * a;
164		}
165		else
166		{
167			genType const f = ((static_cast<genType>(2) * a) - static_cast<genType>(2));
168			return static_cast<genType>(0.5) * f * f * f * f * f + one<genType>();
169		}
170	}
171
172	template <typename genType>
173	GLM_FUNC_QUALIFIER genType sineEaseIn(genType const& a)
174	{
175		// Only defined in [0, 1]
176		assert(a >= zero<genType>());
177		assert(a <= one<genType>());
178
179		return sin((a - one<genType>()) * half_pi<genType>()) + one<genType>();
180	}
181
182	template <typename genType>
183	GLM_FUNC_QUALIFIER genType sineEaseOut(genType const& a)
184	{
185		// Only defined in [0, 1]
186		assert(a >= zero<genType>());
187		assert(a <= one<genType>());
188
189		return sin(a * half_pi<genType>());
190	}
191
192	template <typename genType>
193	GLM_FUNC_QUALIFIER genType sineEaseInOut(genType const& a)
194	{
195		// Only defined in [0, 1]
196		assert(a >= zero<genType>());
197		assert(a <= one<genType>());
198
199		return static_cast<genType>(0.5) * (one<genType>() - cos(a * pi<genType>()));
200	}
201
202	template <typename genType>
203	GLM_FUNC_QUALIFIER genType circularEaseIn(genType const& a)
204	{
205		// Only defined in [0, 1]
206		assert(a >= zero<genType>());
207		assert(a <= one<genType>());
208
209		return one<genType>() - sqrt(one<genType>() - (a * a));
210	}
211
212	template <typename genType>
213	GLM_FUNC_QUALIFIER genType circularEaseOut(genType const& a)
214	{
215		// Only defined in [0, 1]
216		assert(a >= zero<genType>());
217		assert(a <= one<genType>());
218
219		return sqrt((static_cast<genType>(2) - a) * a);
220	}
221
222	template <typename genType>
223	GLM_FUNC_QUALIFIER genType circularEaseInOut(genType const& a)
224	{
225		// Only defined in [0, 1]
226		assert(a >= zero<genType>());
227		assert(a <= one<genType>());
228
229		if(a < static_cast<genType>(0.5))
230		{
231			return static_cast<genType>(0.5) * (one<genType>() - std::sqrt(one<genType>() - static_cast<genType>(4) * (a * a)));
232		}
233		else
234		{
235			return static_cast<genType>(0.5) * (std::sqrt(-((static_cast<genType>(2) * a) - static_cast<genType>(3)) * ((static_cast<genType>(2) * a) - one<genType>())) + one<genType>());
236		}
237	}
238
239	template <typename genType>
240	GLM_FUNC_QUALIFIER genType exponentialEaseIn(genType const& a)
241	{
242		// Only defined in [0, 1]
243		assert(a >= zero<genType>());
244		assert(a <= one<genType>());
245
246		if(a <= zero<genType>())
247			return a;
248		else
249		{
250			genType const Complementary = a - one<genType>();
251			genType const Two = static_cast<genType>(2);
252
253			return glm::pow(Two, Complementary * static_cast<genType>(10));
254		}
255	}
256
257	template <typename genType>
258	GLM_FUNC_QUALIFIER genType exponentialEaseOut(genType const& a)
259	{
260		// Only defined in [0, 1]
261		assert(a >= zero<genType>());
262		assert(a <= one<genType>());
263
264		if(a >= one<genType>())
265			return a;
266		else
267		{
268			return one<genType>() - glm::pow(static_cast<genType>(2), -static_cast<genType>(10) * a);
269		}
270	}
271
272	template <typename genType>
273	GLM_FUNC_QUALIFIER genType exponentialEaseInOut(genType const& a)
274	{
275		// Only defined in [0, 1]
276		assert(a >= zero<genType>());
277		assert(a <= one<genType>());
278
279		if(a < static_cast<genType>(0.5))
280			return static_cast<genType>(0.5) * glm::pow(static_cast<genType>(2), (static_cast<genType>(20) * a) - static_cast<genType>(10));
281		else
282			return -static_cast<genType>(0.5) * glm::pow(static_cast<genType>(2), (-static_cast<genType>(20) * a) + static_cast<genType>(10)) + one<genType>();
283	}
284
285	template <typename genType>
286	GLM_FUNC_QUALIFIER genType elasticEaseIn(genType const& a)
287	{
288		// Only defined in [0, 1]
289		assert(a >= zero<genType>());
290		assert(a <= one<genType>());
291
292		return std::sin(static_cast<genType>(13) * half_pi<genType>() * a) * glm::pow(static_cast<genType>(2), static_cast<genType>(10) * (a - one<genType>()));
293	}
294
295	template <typename genType>
296	GLM_FUNC_QUALIFIER genType elasticEaseOut(genType const& a)
297	{
298		// Only defined in [0, 1]
299		assert(a >= zero<genType>());
300		assert(a <= one<genType>());
301
302		return std::sin(-static_cast<genType>(13) * half_pi<genType>() * (a + one<genType>())) * glm::pow(static_cast<genType>(2), -static_cast<genType>(10) * a) + one<genType>();
303	}
304
305	template <typename genType>
306	GLM_FUNC_QUALIFIER genType elasticEaseInOut(genType const& a)
307	{
308		// Only defined in [0, 1]
309		assert(a >= zero<genType>());
310		assert(a <= one<genType>());
311
312		if(a < static_cast<genType>(0.5))
313			return static_cast<genType>(0.5) * std::sin(static_cast<genType>(13) * half_pi<genType>() * (static_cast<genType>(2) * a)) * glm::pow(static_cast<genType>(2), static_cast<genType>(10) * ((static_cast<genType>(2) * a) - one<genType>()));
314		else
315			return static_cast<genType>(0.5) * (std::sin(-static_cast<genType>(13) * half_pi<genType>() * ((static_cast<genType>(2) * a - one<genType>()) + one<genType>())) * glm::pow(static_cast<genType>(2), -static_cast<genType>(10) * (static_cast<genType>(2) * a - one<genType>())) + static_cast<genType>(2));
316	}
317
318	template <typename genType>
319	GLM_FUNC_QUALIFIER genType backEaseIn(genType const& a, genType const& o)
320	{
321		// Only defined in [0, 1]
322		assert(a >= zero<genType>());
323		assert(a <= one<genType>());
324
325		genType z = ((o + one<genType>()) * a) - o;
326		return (a * a * z);
327	}
328
329	template <typename genType>
330	GLM_FUNC_QUALIFIER genType backEaseOut(genType const& a, genType const& o)
331	{
332		// Only defined in [0, 1]
333		assert(a >= zero<genType>());
334		assert(a <= one<genType>());
335
336		genType n = a - one<genType>();
337		genType z = ((o + one<genType>()) * n) + o;
338		return (n * n * z) + one<genType>();
339	}
340
341	template <typename genType>
342	GLM_FUNC_QUALIFIER genType backEaseInOut(genType const& a, genType const& o)
343	{
344		// Only defined in [0, 1]
345		assert(a >= zero<genType>());
346		assert(a <= one<genType>());
347
348		genType s = o * static_cast<genType>(1.525);
349		genType x = static_cast<genType>(0.5);
350		genType n = a / static_cast<genType>(0.5);
351
352		if (n < static_cast<genType>(1))
353		{
354			genType z = ((s + static_cast<genType>(1)) * n) - s;
355			genType m = n * n * z;
356			return x * m;
357		}
358		else
359		{
360			n -= static_cast<genType>(2);
361			genType z = ((s + static_cast<genType>(1)) * n) + s;
362			genType m = (n*n*z) + static_cast<genType>(2);
363			return x * m;
364		}
365	}
366
367	template <typename genType>
368	GLM_FUNC_QUALIFIER genType backEaseIn(genType const& a)
369	{
370		return backEaseIn(a, static_cast<genType>(1.70158));
371	}
372
373	template <typename genType>
374	GLM_FUNC_QUALIFIER genType backEaseOut(genType const& a)
375	{
376		return backEaseOut(a, static_cast<genType>(1.70158));
377	}
378
379	template <typename genType>
380	GLM_FUNC_QUALIFIER genType backEaseInOut(genType const& a)
381	{
382		return backEaseInOut(a, static_cast<genType>(1.70158));
383	}
384
385	template <typename genType>
386	GLM_FUNC_QUALIFIER genType bounceEaseOut(genType const& a)
387	{
388		// Only defined in [0, 1]
389		assert(a >= zero<genType>());
390		assert(a <= one<genType>());
391
392		if(a < static_cast<genType>(4.0 / 11.0))
393		{
394			return (static_cast<genType>(121) * a * a) / static_cast<genType>(16);
395		}
396		else if(a < static_cast<genType>(8.0 / 11.0))
397		{
398			return (static_cast<genType>(363.0 / 40.0) * a * a) - (static_cast<genType>(99.0 / 10.0) * a) + static_cast<genType>(17.0 / 5.0);
399		}
400		else if(a < static_cast<genType>(9.0 / 10.0))
401		{
402			return (static_cast<genType>(4356.0 / 361.0) * a * a) - (static_cast<genType>(35442.0 / 1805.0) * a) + static_cast<genType>(16061.0 / 1805.0);
403		}
404		else
405		{
406			return (static_cast<genType>(54.0 / 5.0) * a * a) - (static_cast<genType>(513.0 / 25.0) * a) + static_cast<genType>(268.0 / 25.0);
407		}
408	}
409
410	template <typename genType>
411	GLM_FUNC_QUALIFIER genType bounceEaseIn(genType const& a)
412	{
413		// Only defined in [0, 1]
414		assert(a >= zero<genType>());
415		assert(a <= one<genType>());
416
417		return one<genType>() - bounceEaseOut(one<genType>() - a);
418	}
419
420	template <typename genType>
421	GLM_FUNC_QUALIFIER genType bounceEaseInOut(genType const& a)
422	{
423		// Only defined in [0, 1]
424		assert(a >= zero<genType>());
425		assert(a <= one<genType>());
426
427		if(a < static_cast<genType>(0.5))
428		{
429			return static_cast<genType>(0.5) * (one<genType>() - bounceEaseOut(a * static_cast<genType>(2)));
430		}
431		else
432		{
433			return static_cast<genType>(0.5) * bounceEaseOut(a * static_cast<genType>(2) - one<genType>()) + static_cast<genType>(0.5);
434		}
435	}
436
437}//namespace glm
438