OSSIA
Open Scenario System for Interactive Application
easing.hpp
Go to the documentation of this file.
1 #pragma once
2 #include <ossia/detail/config.hpp>
3 
4 #include <ossia/detail/math.hpp>
5 
6 #include <cmath>
7 
8 #include <string_view>
9 
32 namespace ossia
33 {
34 namespace easing
35 {
36 
37 struct ease
38 {
39  template <typename T, typename U, typename V>
40  constexpr T operator()(T a, U b, V t) const noexcept
41  {
42 #if defined(FP_FAST_FMA)
43  return ossia::fma(t, b, ossia::fma(-t, a, a));
44 #else
45  return (static_cast<T>(1) - t) * a + t * b;
46 #endif
47  }
48 };
49 
50 struct linear
51 {
52  static consteval std::string_view name() noexcept { return "linear"; }
53  template <typename T>
54  constexpr T operator()(T t) const noexcept
55  {
56  return t;
57  }
58 };
59 
60 template <typename V>
61 struct power
62 {
63  static consteval std::string_view name() noexcept { return "power"; }
64 
65  static constexpr V linear_gamma = 1.0;
66  V gamma = linear_gamma;
67 
68  template <typename T>
69  constexpr T operator()(T t) const noexcept
70  {
71  return std::pow(t, gamma);
72  }
73 };
74 
75 struct quadraticIn
76 {
77  static consteval std::string_view name() noexcept { return "quadraticIn"; }
78  template <typename T>
79  constexpr T operator()(T t) const noexcept
80  {
81  return ipow(t, 2);
82  }
83 };
84 
85 struct quadraticOut
86 {
87  static consteval std::string_view name() noexcept { return "quadraticOut"; }
88  template <typename T>
89  constexpr T operator()(T t) const noexcept
90  {
91  return -(t * (t - 2.));
92  }
93 };
94 
95 struct quadraticInOut
96 {
97  static consteval std::string_view name() noexcept { return "quadraticInOut"; }
98  template <typename T>
99  constexpr T operator()(T t) const noexcept
100  {
101  return (t < 0.5) ? 2. * t * t : (-2. * t * t) + (4. * t) - 1.;
102  }
103 };
104 
105 struct cubicIn
106 {
107  static consteval std::string_view name() noexcept { return "cubicIn"; }
108  template <typename T>
109  constexpr T operator()(T t) const noexcept
110  {
111  return ipow(t, 3);
112  }
113 };
114 
115 struct cubicOut
116 {
117  static consteval std::string_view name() noexcept { return "cubicOut"; }
118  template <typename T>
119  constexpr T operator()(T t) const noexcept
120  {
121  return ipow(t - 1., 3) + 1.;
122  }
123 };
124 
125 struct cubicInOut
126 {
127  static consteval std::string_view name() noexcept { return "cubicInOut"; }
128  template <typename T>
129  constexpr T operator()(T t) const noexcept
130  {
131  return (t < T(0.5)) ? 4. * ipow(t, 3) : 0.5 * ipow((2. * t) - 2, 3) + 1.;
132  }
133 };
134 
135 struct quarticIn
136 {
137  static consteval std::string_view name() noexcept { return "quarticIn"; }
138  template <typename T>
139  constexpr T operator()(T t) const noexcept
140  {
141  return ipow(t, 4);
142  }
143 };
144 
145 struct quarticOut
146 {
147  static consteval std::string_view name() noexcept { return "quarticOut"; }
148  template <typename T>
149  constexpr T operator()(T t) const noexcept
150  {
151  return ipow(t - 1., 3) * (1. - t) + 1.;
152  }
153 };
154 
155 struct quarticInOut
156 {
157  static consteval std::string_view name() noexcept { return "quarticInOut"; }
158  template <typename T>
159  constexpr T operator()(T t) const noexcept
160  {
161  return (t < 0.5) ? 8. * ipow(t, 4) : -8. * ipow(t - 1., 4) + 1.;
162  }
163 };
164 
165 struct quinticIn
166 {
167  static consteval std::string_view name() noexcept { return "quinticIn"; }
168  template <typename T>
169  constexpr T operator()(T t) const noexcept
170  {
171  return ipow(t, 5);
172  }
173 };
174 
175 struct quinticOut
176 {
177  static consteval std::string_view name() noexcept { return "quinticOut"; }
178  template <typename T>
179  constexpr T operator()(T t) const noexcept
180  {
181  return ipow(t - 1., 5) + 1.;
182  }
183 };
184 
185 struct quinticInOut
186 {
187  static consteval std::string_view name() noexcept { return "quinticInOut"; }
188  template <typename T>
189  constexpr T operator()(T t) const noexcept
190  {
191  return (t < 0.5) ? 16. * ipow(t, 5) : 0.5 * ipow((2. * t) - 2., 5) + 1.;
192  }
193 };
194 
195 struct sineIn
196 {
197  static consteval std::string_view name() noexcept { return "sineIn"; }
198  template <typename T>
199  constexpr T operator()(T t) const noexcept
200  {
201  return std::sin((t - 1.) * half_pi) + 1.;
202  }
203 };
204 
205 struct sineOut
206 {
207  static consteval std::string_view name() noexcept { return "sineOut"; }
208  template <typename T>
209  constexpr T operator()(T t) const noexcept
210  {
211  return std::sin(t * half_pi);
212  }
213 };
214 
215 struct sineInOut
216 {
217  static consteval std::string_view name() noexcept { return "sineInOut"; }
218  template <typename T>
219  constexpr T operator()(T t) const noexcept
220  {
221  return 0.5 * (1. - std::cos(t * pi));
222  }
223 };
224 
225 struct circularIn
226 {
227  static consteval std::string_view name() noexcept { return "circularIn"; }
228  template <typename T>
229  constexpr T operator()(T t) const noexcept
230  {
231  return 1. - std::sqrt(1. - (t * t));
232  }
233 };
234 
235 struct circularOut
236 {
237  static consteval std::string_view name() noexcept { return "circularOut"; }
238  template <typename T>
239  constexpr T operator()(T t) const noexcept
240  {
241  return std::sqrt((2. - t) * t);
242  }
243 };
244 
245 struct circularInOut
246 {
247  static consteval std::string_view name() noexcept { return "circularInOut"; }
248  template <typename T>
249  constexpr T operator()(T t) const noexcept
250  {
251  return (t < 0.5) ? 0.5 * (1 - std::sqrt(1 - 4. * (t * t)))
252  : 0.5 * (std::sqrt(-((2. * t) - 3.) * ((2. * t) - 1.)) + 1.);
253  }
254 };
255 
256 struct exponentialIn
257 {
258  static consteval std::string_view name() noexcept { return "exponentialIn"; }
259  template <typename T>
260  constexpr T operator()(T t) const
261  {
262  return (t <= 0) ? t : ossia::exp2(10. * (t - 1.));
263  }
264 };
265 
266 struct exponentialOut
267 {
268  static consteval std::string_view name() noexcept { return "exponentialOut"; }
269  template <typename T>
270  constexpr T operator()(T t) const noexcept
271  {
272  return (t >= 1.) ? t : 1. - ossia::exp2(-10. * t);
273  }
274 };
275 
276 struct exponentialInOut
277 {
278  static consteval std::string_view name() noexcept { return "exponentialInOut"; }
279  template <typename T>
280  constexpr T operator()(T t) const noexcept
281  {
282  return (t <= 0. || t >= 1.) ? t
283  : (t < 0.5) ? 0.5 * ossia::exp2((20. * t) - 10.)
284  : -0.5 * ossia::exp2((-20. * t) + 10.) + 1.;
285  }
286 };
287 
288 struct elasticIn
289 {
290  static consteval std::string_view name() noexcept { return "elasticIn"; }
291  template <typename T>
292  constexpr T operator()(T t) const noexcept
293  {
294  return std::sin(13. * half_pi * t) * ossia::exp2(10. * (t - 1.));
295  }
296 };
297 
298 struct elasticOut
299 {
300  static consteval std::string_view name() noexcept { return "elasticOut"; }
301  template <typename T>
302  constexpr T operator()(T t) const noexcept
303  {
304  return sin(-13. * half_pi * (t + 1.)) * ossia::exp2(-10. * t) + 1.;
305  }
306 };
307 
308 struct elasticInOut
309 {
310  static consteval std::string_view name() noexcept { return "elasticInOut"; }
311  template <typename T>
312  constexpr T operator()(T t) const noexcept
313  {
314  return (t < 0.5) ? 0.5 * std::sin(13. * half_pi * (2. * t))
315  * ossia::exp2(10. * ((2. * t) - 1.))
316  : 0.5
317  * (std::sin(-13. * half_pi * ((2. * t - 1) + 1))
318  * ossia::exp2(-10. * (2. * t - 1.))
319  + 2.);
320  }
321 };
322 
323 struct backIn
324 {
325  static consteval std::string_view name() noexcept { return "backIn"; }
326  template <typename T>
327  constexpr T operator()(T t) const noexcept
328  {
329  return ipow(t, 3) - t * std::sin(t * pi);
330  }
331 };
332 
333 struct backOut
334 {
335  static consteval std::string_view name() noexcept { return "backOut"; }
336  template <typename T>
337  constexpr T operator()(T t) const noexcept
338  {
339  return 1. - (ipow(1. - t, 3) - (1. - t) * std::sin((1. - t) * pi));
340  }
341 };
342 
343 struct backInOut
344 {
345  static consteval std::string_view name() noexcept { return "backInOut"; }
346  template <typename T>
347  constexpr T operator()(T t) const noexcept
348  {
349  return (t < 0.5) ? 0.5 * (ipow(2. * t, 3) - (2. * t) * std::sin((2. * t) * pi))
350  : 0.5
351  * (1.
352  - (ipow(1. - (2. * t - 1.), 3)
353  - (1. - (2. * t - 1.))
354  * std::sin((1. - (2. * t - 1.)) * pi)))
355  + 0.5;
356  }
357 };
358 
359 struct bounceOut
360 {
361  static consteval std::string_view name() noexcept { return "bounceOut"; }
362  template <typename T>
363  constexpr T operator()(T t) const noexcept
364  {
365  return t < 4. / 11. ? (121. * t * t) / 16.
366  : (t < 8. / 11.) ? (363. / 40. * t * t) - (99 / 10. * t) + 17 / 5.
367  : (t < 9. / 10.)
368  ? (4356. / 361. * t * t) - (35442. / 1805. * t) + 16061. / 1805.
369  : (54. / 5. * t * t) - (513. / 25. * t) + 268. / 25.;
370  }
371 };
372 
373 struct bounceIn
374 {
375  static consteval std::string_view name() noexcept { return "bounceIn"; }
376  template <typename T>
377  constexpr T operator()(T t) const noexcept
378  {
379  return 1. - bounceOut{}(1. - t);
380  }
381 };
382 
383 struct bounceInOut
384 {
385  static consteval std::string_view name() noexcept { return "bounceInOut"; }
386  template <typename T>
387  constexpr T operator()(T t) const noexcept
388  {
389  return t < 0.5 ? 0.5 * bounceIn{}(t * 2.) : 0.5 * bounceOut{}(t * 2. - 1.) + 0.5;
390  }
391 };
392 
393 struct perlinInOut
394 {
395  static consteval std::string_view name() noexcept { return "perlinInOut"; }
396  template <typename T>
397  constexpr T operator()(T t) const noexcept
398  {
399  return 6. * ipow(t, 5) - 15. * ipow(t, 4) + 10. * ipow(t, 3);
400  }
401 };
402 }
403 
404 template <typename Y, typename Easing>
405 struct curve_segment_ease
406 {
407  constexpr Y operator()(double ratio, Y start, Y end) const noexcept
408  {
409  return easing::ease{}(start, end, Easing{}(ratio));
410  }
411 };
412 
413 }
Definition: git_info.h:7