Plasma Engine  2.0
Loading...
Searching...
No Matches
CurveFunctions_inl.h
1#pragma once
2
3#include <Foundation/Math/Math.h>
4
5namespace plMath
6{
7 PL_ALWAYS_INLINE double GetCurveValue_Linear(double t)
8 {
9 return t;
10 }
11
12 PL_ALWAYS_INLINE double GetCurveValue_ConstantZero(double t)
13 {
14 return 0.0;
15 }
16
17 PL_ALWAYS_INLINE double GetCurveValue_ConstantOne(double t)
18 {
19 return 1.0;
20 }
21
22 PL_ALWAYS_INLINE double GetCurveValue_EaseInSine(double t)
23 {
24 return 1.0 - cos((t * plMath::Pi<double>()) / 2.0);
25 }
26
27 PL_ALWAYS_INLINE double GetCurveValue_EaseOutSine(double t)
28 {
29 return sin((t * plMath::Pi<double>()) / 2.0);
30 }
31
32 PL_ALWAYS_INLINE double GetCurveValue_EaseInOutSine(double t)
33 {
34 return -(cos(plMath::Pi<double>() * t) - 1.0) / 2.0;
35 }
36
37 PL_ALWAYS_INLINE double GetCurveValue_EaseInQuad(double t)
38 {
39 return t * t;
40 }
41
42 PL_ALWAYS_INLINE double GetCurveValue_EaseOutQuad(double t)
43 {
44 return 1.0 - (1.0 - t) * (1.0 - t);
45 }
46
47 PL_ALWAYS_INLINE double GetCurveValue_EaseInOutQuad(double t)
48 {
49 return t < 0.5 ? 2.0 * t * t : 1.0 - plMath::Pow(-2.0 * t + 2, 2.0) / 2;
50 }
51
52 PL_ALWAYS_INLINE double GetCurveValue_EaseInCubic(double t)
53 {
54 return t * t * t;
55 }
56
57 PL_ALWAYS_INLINE double GetCurveValue_EaseOutCubic(double t)
58 {
59 return 1.0 - pow(1 - t, 3.0);
60 }
61
62
63 PL_ALWAYS_INLINE double GetCurveValue_EaseInOutCubic(double t)
64 {
65 return t < 0.5 ? 4.0 * t * t * t : 1.0 - plMath::Pow(-2.0 * t + 2.0, 3.0) / 2.0;
66 }
67
68
69 PL_ALWAYS_INLINE double GetCurveValue_EaseInQuartic(double t)
70 {
71 return t * t * t * t;
72 }
73
74
75 PL_ALWAYS_INLINE double GetCurveValue_EaseOutQuartic(double t)
76 {
77 return 1.0 - plMath::Pow(1.0 - t, 4.0);
78 }
79
80
81 PL_ALWAYS_INLINE double GetCurveValue_EaseInOutQuartic(double t)
82 {
83 return t < 0.5 ? 8.0 * t * t * t * t : 1.0 - plMath::Pow(-2.0 * t + 2.0, 4.0) / 2.0;
84 }
85
86
87 PL_ALWAYS_INLINE double GetCurveValue_EaseInQuintic(double t)
88 {
89 return t * t * t * t * t;
90 }
91
92
93 PL_ALWAYS_INLINE double GetCurveValue_EaseOutQuintic(double t)
94 {
95 return 1.0 - plMath::Pow(1.0 - t, 5.0);
96 }
97
98
99 PL_ALWAYS_INLINE double GetCurveValue_EaseInOutQuintic(double t)
100 {
101 return t < 0.5 ? 16.0 * t * t * t * t * t : 1.0 - plMath::Pow(-2.0 * t + 2.0, 5.0) / 2.0;
102 }
103
104
105 PL_ALWAYS_INLINE double GetCurveValue_EaseInExpo(double t)
106 {
107 return t == 0 ? 0 : plMath::Pow(2.0, 10.0 * t - 10.0);
108 }
109
110
111 PL_ALWAYS_INLINE double GetCurveValue_EaseOutExpo(double t)
112 {
113 return t == 1.0 ? 1.0 : 1.0 - plMath::Pow(2.0, -10.0 * t);
114 }
115
116
117 PL_ALWAYS_INLINE double GetCurveValue_EaseInOutExpo(double t)
118 {
119 if (t == 0.0)
120 {
121 return 0.0;
122 }
123 else if (t == 1.0)
124 {
125 return 1.0;
126 }
127 else
128 {
129 return t < 0.5 ? plMath::Pow(2.0, 20.0 * t - 10.0) / 2.0
130 : (2.0 - plMath::Pow(2.0, -20.0 * t + 10.0)) / 2.0;
131 }
132 }
133
134
135 PL_ALWAYS_INLINE double GetCurveValue_EaseInCirc(double t)
136 {
137 return 1.0 - plMath::Sqrt(1.0 - plMath::Pow(t, 2.0));
138 }
139
140
141 PL_ALWAYS_INLINE double GetCurveValue_EaseOutCirc(double t)
142 {
143 return plMath::Sqrt(1.0 - plMath::Pow(t - 1.0, 2.0));
144 }
145
146
147 PL_ALWAYS_INLINE double GetCurveValue_EaseInOutCirc(double t)
148 {
149 return t < 0.5
150 ? (1.0 - plMath::Sqrt(1.0 - plMath::Pow(2.0 * t, 2.0))) / 2.0
151 : (plMath::Sqrt(1.0 - plMath::Pow(-2.0 * t + 2.0, 2.0)) + 1.0) / 2.0;
152 }
153
154
155 PL_ALWAYS_INLINE double GetCurveValue_EaseInBack(double t)
156 {
157 return 2.70158 * t * t * t - 1.70158 * t * t;
158 }
159
160
161 PL_ALWAYS_INLINE double GetCurveValue_EaseOutBack(double t)
162 {
163 return 1 + 2.70158 * plMath::Pow(t - 1.0, 3.0) + 1.70158 * plMath::Pow(t - 1.0, 2.0);
164 }
165
166
167 PL_ALWAYS_INLINE double GetCurveValue_EaseInOutBack(double t)
168 {
169 return t < 0.5
170 ? (plMath::Pow(2.0 * t, 2.0) * (((1.70158 * 1.525) + 1.0) * 2 * t - (1.70158 * 1.525))) / 2.0
171 : (plMath::Pow(2.0 * t - 2.0, 2.0) * (((1.70158 * 1.525) + 1.0) * (t * 2.0 - 2.0) + (1.70158 * 1.525)) + 2.0) / 2.0;
172 }
173
174
175 PL_ALWAYS_INLINE double GetCurveValue_EaseInElastic(double t)
176 {
177 if (t == 0.0)
178 {
179 return 0.0;
180 }
181 else if (t == 1.0)
182 {
183 return 1.0;
184 }
185 else
186 {
187 return -plMath::Pow(2.0, 10.0 * t - 10.0) * sin((t * 10.0 - 10.75) * ((2.0 * plMath::Pi<double>()) / 3.0));
188 }
189 }
190
191
192 PL_ALWAYS_INLINE double GetCurveValue_EaseOutElastic(double t)
193 {
194 if (t == 0.0)
195 {
196 return 0.0;
197 }
198 else if (t == 1.0)
199 {
200 return 1.0;
201 }
202 else
203 {
204 return plMath::Pow(2.0, -10.0 * t) * sin((t * 10.0 - 0.75) * ((2.0 * plMath::Pi<double>()) / 3.0)) + 1.0;
205 }
206 }
207
208 PL_ALWAYS_INLINE double GetCurveValue_EaseInOutElastic(double t)
209 {
210 if (t == 0.0)
211 {
212 return 0.0;
213 }
214 else if (t == 1.0)
215 {
216 return 1.0;
217 }
218 else
219 {
220 return t < 0.5
221 ? -(plMath::Pow(2.0, 20.0 * t - 10.0) * sin((20.0 * t - 11.125) * ((2 * plMath::Pi<double>()) / 4.5))) / 2.0
222 : (plMath::Pow(2.0, -20.0 * t + 10.0) * sin((20.0 * t - 11.125) * ((2 * plMath::Pi<double>()) / 4.5))) / 2.0 + 1.0;
223 }
224 }
225
226 PL_ALWAYS_INLINE double GetCurveValue_EaseInBounce(double t)
227 {
228 return 1.0 - GetCurveValue_EaseOutBounce(1.0 - t);
229 }
230
231 PL_ALWAYS_INLINE double GetCurveValue_EaseOutBounce(double t)
232 {
233 if (t < 1.0 / 2.75)
234 {
235 return 7.5625 * t * t;
236 }
237 else if (t < 2.0 / 2.75)
238 {
239 t -= 1.5 / 2.75;
240 return 7.5625 * t * t + 0.75;
241 }
242 else if (t < 2.5 / 2.75)
243 {
244 t -= 2.25 / 2.75;
245 return 7.5625 * t * t + 0.9375;
246 }
247 else
248 {
249 t -= 2.625 / 2.75;
250 return 7.5625 * t * t + 0.984375;
251 }
252 }
253
254 PL_ALWAYS_INLINE double GetCurveValue_EaseInOutBounce(double t)
255 {
256 return t < 0.5
257 ? (1.0 - GetCurveValue_EaseOutBounce(1.0 - 2.0 * t)) / 2.0
258 : (1.0 + GetCurveValue_EaseOutBounce(2.0 * t - 1.0)) / 2.0;
259 }
260
261 PL_ALWAYS_INLINE double GetCurveValue_Conical(double t)
262 {
263 if (t < 0.2)
264 {
265 return 1.0f - plMath::Pow(1.0 - (t * 5.0), 4.0);
266 }
267 else
268 {
269 t = (t - 0.2) / 0.8; // normalize to 0-1 range
270
271 return 1.0 - plMath::Pow(t, 2.0);
272 }
273 }
274
275 PL_ALWAYS_INLINE double GetCurveValue_FadeInHoldFadeOut(double t)
276 {
277 if (t < 0.2)
278 {
279 return 1.0f - plMath::Pow(1.0 - (t * 5.0), 3.0);
280 }
281 else if (t > 0.8)
282 {
283 return 1.0 - plMath::Pow((t - 0.8) * 5.0, 3.0);
284 }
285 else
286 {
287 return 1.0;
288 }
289 }
290
291 PL_ALWAYS_INLINE double GetCurveValue_FadeInFadeOut(double t)
292 {
293 if (t < 0.5)
294 {
295 return 1.0f - plMath::Pow(1.0 - (t * 2.0), 3.0);
296 }
297 else
298 {
299 return 1.0 - plMath::Pow((t - 0.5) * 2.0, 3.0);
300 }
301 }
302
303 PL_ALWAYS_INLINE double GetCurveValue_Bell(double t)
304 {
305 if (t < 0.25)
306 {
307 return (plMath::Pow((t * 4.0), 3.0)) * 0.5;
308 }
309 else if (t < 0.5)
310 {
311 return (1.0f - plMath::Pow(1.0 - ((t - 0.25) * 4.0), 3.0)) * 0.5 + 0.5;
312 }
313 else if (t < 0.75)
314 {
315 return (1.0f - plMath::Pow(((t - 0.5) * 4.0), 3.0)) * 0.5 + 0.5;
316 }
317 else
318 {
319 return (plMath::Pow(1.0 - ((t - 0.75) * 4.0), 3.0)) * 0.5;
320 }
321 }
322} // namespace plMath
323
324// static
325inline double plCurveFunction::GetValue(Enum function, double x)
326{
327 switch (function)
328 {
329 case Linear:
330 return plMath::GetCurveValue_Linear(x);
331 case ConstantZero:
332 return plMath::GetCurveValue_ConstantZero(x);
333 case ConstantOne:
334 return plMath::GetCurveValue_ConstantOne(x);
335 case EaseInSine:
336 return plMath::GetCurveValue_EaseInSine(x);
337 case EaseOutSine:
338 return plMath::GetCurveValue_EaseOutSine(x);
339 case EaseInOutSine:
340 return plMath::GetCurveValue_EaseInOutSine(x);
341 case EaseInQuad:
342 return plMath::GetCurveValue_EaseInQuad(x);
343 case EaseOutQuad:
344 return plMath::GetCurveValue_EaseOutQuad(x);
345 case EaseInOutQuad:
346 return plMath::GetCurveValue_EaseInOutQuad(x);
347 case EaseInCubic:
348 return plMath::GetCurveValue_EaseInCubic(x);
349 case EaseOutCubic:
350 return plMath::GetCurveValue_EaseOutCubic(x);
351 case EaseInOutCubic:
352 return plMath::GetCurveValue_EaseInOutCubic(x);
353 case EaseInQuartic:
354 return plMath::GetCurveValue_EaseInQuartic(x);
355 case EaseOutQuartic:
356 return plMath::GetCurveValue_EaseOutQuartic(x);
357 case EaseInOutQuartic:
358 return plMath::GetCurveValue_EaseInOutQuartic(x);
359 case EaseInQuintic:
360 return plMath::GetCurveValue_EaseInQuintic(x);
361 case EaseOutQuintic:
362 return plMath::GetCurveValue_EaseOutQuintic(x);
363 case EaseInOutQuintic:
364 return plMath::GetCurveValue_EaseInOutQuintic(x);
365 case EaseInExpo:
366 return plMath::GetCurveValue_EaseInExpo(x);
367 case EaseOutExpo:
368 return plMath::GetCurveValue_EaseOutExpo(x);
369 case EaseInOutExpo:
370 return plMath::GetCurveValue_EaseInOutExpo(x);
371 case EaseInCirc:
372 return plMath::GetCurveValue_EaseInCirc(x);
373 case EaseOutCirc:
374 return plMath::GetCurveValue_EaseOutCirc(x);
375 case EaseInOutCirc:
376 return plMath::GetCurveValue_EaseInOutCirc(x);
377 case EaseInBack:
378 return plMath::GetCurveValue_EaseInBack(x);
379 case EaseOutBack:
380 return plMath::GetCurveValue_EaseOutBack(x);
381 case EaseInOutBack:
382 return plMath::GetCurveValue_EaseInOutBack(x);
383 case EaseInElastic:
384 return plMath::GetCurveValue_EaseInElastic(x);
385 case EaseOutElastic:
386 return plMath::GetCurveValue_EaseOutElastic(x);
387 case EaseInOutElastic:
388 return plMath::GetCurveValue_EaseInOutElastic(x);
389 case EaseInBounce:
390 return plMath::GetCurveValue_EaseInBounce(x);
391 case EaseOutBounce:
392 return plMath::GetCurveValue_EaseOutBounce(x);
393 case EaseInOutBounce:
394 return plMath::GetCurveValue_EaseInOutBounce(x);
395 case Conical:
396 return plMath::GetCurveValue_Conical(x);
397 case FadeInHoldFadeOut:
398 return plMath::GetCurveValue_FadeInHoldFadeOut(x);
399 case FadeInFadeOut:
400 return plMath::GetCurveValue_FadeInFadeOut(x);
401 case Bell:
402 return plMath::GetCurveValue_Bell(x);
403
404 PL_DEFAULT_CASE_NOT_IMPLEMENTED;
405 }
406
407 return 0.0;
408}
409
410// static
411inline double plCurveFunction::GetValue(Enum function, double x, bool bInverse)
412{
413 double value = GetValue(function, x);
414
415 return bInverse ? (1.0 - value) : value;
416}
This namespace provides common math-functionality as functions.
Definition Constants.h:6
constexpr TYPE Pi()
Returns the natural constant Pi.
PL_ALWAYS_INLINE double Sqrt(double f)
Returns the square root of f.
Definition MathDouble_inl.h:99
static double GetValue(Enum function, double x)
Helper function that returns the function value at the given x coordinate.
Definition CurveFunctions_inl.h:325
Enum
Definition CurveFunctions.h:19
@ EaseInOutBack
Values exceed the 0-1 range briefly.
Definition CurveFunctions.h:55
@ EaseInElastic
Values exceed the 0-1 range briefly.
Definition CurveFunctions.h:57
@ EaseOutBack
Values exceed the 0-1 range briefly.
Definition CurveFunctions.h:54
@ EaseOutElastic
Values exceed the 0-1 range briefly.
Definition CurveFunctions.h:58
@ EaseInOutElastic
Values exceed the 0-1 range briefly.
Definition CurveFunctions.h:59
@ EaseInBack
Values exceed the 0-1 range briefly.
Definition CurveFunctions.h:53