RSeries astromech firmware
ServoEasing.h
Go to the documentation of this file.
1 #ifndef ServoEase_h
2 #define ServoEase_h
3 
4 #include "ServoDispatch.h"
5 
6 class Easing
7 {
8 public:
9  typedef float (*Method)(float);
10 
11  enum
12  {
45  };
46 
47  // Modeled after the line y = x
48  static float LinearInterpolation(float p)
49  {
50  return p;
51  }
52 
53  static float Continuous(float p)
54  {
55  return p;
56  }
57 
58  // Modeled after the parabola y = x^2
59  static float QuadraticEaseIn(float p)
60  {
61  return p * p;
62  }
63 
64  // Modeled after the parabola y = -x^2 + 2x
65  static float QuadraticEaseOut(float p)
66  {
67  return -(p * (p - 2));
68  }
69 
70  // Modeled after the piecewise quadratic
71  // y = (1/2)((2x)^2) ; [0, 0.5)
72  // y = -(1/2)((2x-1)*(2x-3) - 1) ; [0.5, 1]
73  static float QuadraticEaseInOut(float p)
74  {
75  if (p < 0.5)
76  {
77  return 2 * p * p;
78  }
79  else
80  {
81  return (-2 * p * p) + (4 * p) - 1;
82  }
83  }
84 
85  // Modeled after the cubic y = x^3
86  static float CubicEaseIn(float p)
87  {
88  return p * p * p;
89  }
90 
91  // Modeled after the cubic y = (x - 1)^3 + 1
92  static float CubicEaseOut(float p)
93  {
94  float f = (p - 1);
95  return f * f * f + 1;
96  }
97 
98  // Modeled after the piecewise cubic
99  // y = (1/2)((2x)^3) ; [0, 0.5)
100  // y = (1/2)((2x-2)^3 + 2) ; [0.5, 1]
101  static float CubicEaseInOut(float p)
102  {
103  if (p < 0.5)
104  {
105  return 4 * p * p * p;
106  }
107  else
108  {
109  float f = ((2 * p) - 2);
110  return 0.5 * f * f * f + 1;
111  }
112  }
113 
114  // Modeled after the quartic x^4
115  static float QuarticEaseIn(float p)
116  {
117  return p * p * p * p;
118  }
119 
120  // Modeled after the quartic y = 1 - (x - 1)^4
121  static float QuarticEaseOut(float p)
122  {
123  float f = (p - 1);
124  return f * f * f * (1 - p) + 1;
125  }
126 
127  // Modeled after the piecewise quartic
128  // y = (1/2)((2x)^4) ; [0, 0.5)
129  // y = -(1/2)((2x-2)^4 - 2) ; [0.5, 1]
130  static float QuarticEaseInOut(float p)
131  {
132  if (p < 0.5)
133  {
134  return 8 * p * p * p * p;
135  }
136  else
137  {
138  float f = (p - 1);
139  return -8 * f * f * f * f + 1;
140  }
141  }
142 
143  // Modeled after the quintic y = x^5
144  static float QuinticEaseIn(float p)
145  {
146  return p * p * p * p * p;
147  }
148 
149  // Modeled after the quintic y = (x - 1)^5 + 1
150  static float QuinticEaseOut(float p)
151  {
152  float f = (p - 1);
153  return f * f * f * f * f + 1;
154  }
155 
156  // Modeled after the piecewise quintic
157  // y = (1/2)((2x)^5) ; [0, 0.5)
158  // y = (1/2)((2x-2)^5 + 2) ; [0.5, 1]
159  static float QuinticEaseInOut(float p)
160  {
161  if (p < 0.5)
162  {
163  return 16 * p * p * p * p * p;
164  }
165  else
166  {
167  float f = ((2 * p) - 2);
168  return 0.5 * f * f * f * f * f + 1;
169  }
170  }
171 
172  // Modeled after quarter-cycle of sine wave
173  static float SineEaseIn(float p)
174  {
175  return sin((p - 1) * M_PI_2) + 1;
176  }
177 
178  // Modeled after quarter-cycle of sine wave (different phase)
179  static float SineEaseOut(float p)
180  {
181  return sin(p * M_PI_2);
182  }
183 
184  // Modeled after half sine wave
185  static float SineEaseInOut(float p)
186  {
187  return 0.5 * (1 - cos(p * M_PI));
188  }
189 
190  // Modeled after shifted quadrant IV of unit circle
191  static float CircularEaseIn(float p)
192  {
193  return 1 - sqrt(1 - (p * p));
194  }
195 
196  // Modeled after shifted quadrant II of unit circle
197  static float CircularEaseOut(float p)
198  {
199  return sqrt((2 - p) * p);
200  }
201 
202  // Modeled after the piecewise circular function
203  // y = (1/2)(1 - sqrt(1 - 4x^2)) ; [0, 0.5)
204  // y = (1/2)(sqrt(-(2x - 3)*(2x - 1)) + 1) ; [0.5, 1]
205  static float CircularEaseInOut(float p)
206  {
207  if (p < 0.5)
208  {
209  return 0.5 * (1 - sqrt(1 - 4 * (p * p)));
210  }
211  else
212  {
213  return 0.5 * (sqrt(-((2 * p) - 3) * ((2 * p) - 1)) + 1);
214  }
215  }
216 
217  // Modeled after the exponential function y = 2^(10(x - 1))
218  static float ExponentialEaseIn(float p)
219  {
220  return (p == 0.0) ? p : pow(2, 10 * (p - 1));
221  }
222 
223  // Modeled after the exponential function y = -2^(-10x) + 1
224  static float ExponentialEaseOut(float p)
225  {
226  return (p == 1.0) ? p : 1 - pow(2, -10 * p);
227  }
228 
229  // Modeled after the piecewise exponential
230  // y = (1/2)2^(10(2x - 1)) ; [0,0.5)
231  // y = -(1/2)*2^(-10(2x - 1))) + 1 ; [0.5,1]
232  static float ExponentialEaseInOut(float p)
233  {
234  if (p == 0.0 || p == 1.0)
235  {
236  return p;
237  }
238  else if (p < 0.5)
239  {
240  return 0.5 * pow(2, (20 * p) - 10);
241  }
242  return -0.5 * pow(2, (-20 * p) + 10) + 1;
243  }
244 
245  // Modeled after the damped sine wave y = sin(13pi/2*x)*pow(2, 10 * (x - 1))
246  static float ElasticEaseIn(float p)
247  {
248  return sin(13 * M_PI_2 * p) * pow(2, 10 * (p - 1));
249  }
250 
251  // Modeled after the damped sine wave y = sin(-13pi/2*(x + 1))*pow(2, -10x) + 1
252  static float ElasticEaseOut(float p)
253  {
254  return sin(-13 * M_PI_2 * (p + 1)) * pow(2, -10 * p) + 1;
255  }
256 
257  // Modeled after the piecewise exponentially-damped sine wave:
258  // y = (1/2)*sin(13pi/2*(2*x))*pow(2, 10 * ((2*x) - 1)) ; [0,0.5)
259  // y = (1/2)*(sin(-13pi/2*((2x-1)+1))*pow(2,-10(2*x-1)) + 2) ; [0.5, 1]
260  static float ElasticEaseInOut(float p)
261  {
262  if (p < 0.5)
263  {
264  return 0.5 * sin(13 * M_PI_2 * (2 * p)) * pow(2, 10 * ((2 * p) - 1));
265  }
266  else
267  {
268  return 0.5 * (sin(-13 * M_PI_2 * ((2 * p - 1) + 1)) * pow(2, -10 * (2 * p - 1)) + 2);
269  }
270  }
271 
272  // Modeled after the overshooting cubic y = x^3-x*sin(x*pi)
273  static float BackEaseIn(float p)
274  {
275  return p * p * p - p * sin(p * M_PI);
276  }
277 
278  // Modeled after overshooting cubic y = 1-((1-x)^3-(1-x)*sin((1-x)*pi))
279  static float BackEaseOut(float p)
280  {
281  float f = (1 - p);
282  return 1 - (f * f * f - f * sin(f * M_PI));
283  }
284 
285  // Modeled after the piecewise overshooting cubic function:
286  // y = (1/2)*((2x)^3-(2x)*sin(2*x*pi)) ; [0, 0.5)
287  // y = (1/2)*(1-((1-x)^3-(1-x)*sin((1-x)*pi))+1) ; [0.5, 1]
288  static float BackEaseInOut(float p)
289  {
290  if (p < 0.5)
291  {
292  float f = 2 * p;
293  return 0.5 * (f * f * f - f * sin(f * M_PI));
294  }
295  else
296  {
297  float f = (1 - (2*p - 1));
298  return 0.5 * (1 - (f * f * f - f * sin(f * M_PI))) + 0.5;
299  }
300  }
301 
302  static float BounceEaseIn(float p)
303  {
304  return 1 - BounceEaseOut(1 - p);
305  }
306 
307  static float BounceEaseOut(float p)
308  {
309  if (p < 4/11.0)
310  {
311  return (121 * p * p)/16.0;
312  }
313  else if (p < 8/11.0)
314  {
315  return (363/40.0 * p * p) - (99/10.0 * p) + 17/5.0;
316  }
317  else if (p < 9/10.0)
318  {
319  return (4356/361.0 * p * p) - (35442/1805.0 * p) + 16061/1805.0;
320  }
321  return (54/5.0 * p * p) - (513/25.0 * p) + 268/25.0;
322  }
323 
324  static float BounceEaseInOut(float p)
325  {
326  if (p < 0.5)
327  {
328  return 0.5 * BounceEaseIn(p*2);
329  }
330  else
331  {
332  return 0.5 * BounceEaseOut(p * 2 - 1) + 0.5;
333  }
334  }
335 
336  static Method getEasingMethod(uint8_t i)
337  {
338  switch (i)
339  {
341  return LinearInterpolation;
342  case kContinuous:
343  return Continuous;
344  case kQuadraticEaseIn:
345  return QuadraticEaseIn;
346  case kQuadraticEaseOut:
347  return QuadraticEaseOut;
348  case kQuadraticEaseInOut:
349  return QuadraticEaseInOut;
350  case kCubicEaseIn:
351  return CubicEaseIn;
352  case kCubicEaseOut:
353  return CubicEaseOut;
354  case kCubicEaseInOut:
355  return CubicEaseInOut;
356  case kQuarticEaseIn:
357  return QuarticEaseIn;
358  case kQuarticEaseOut:
359  return QuarticEaseOut;
360  case kQuarticEaseInOut:
361  return QuarticEaseInOut;
362  case kQuinticEaseIn:
363  return QuinticEaseIn;
364  case kQuinticEaseOut:
365  return QuinticEaseOut;
366  case kQuinticEaseInOut:
367  return QuinticEaseInOut;
368  case kSineEaseIn:
369  return SineEaseIn;
370  case kSineEaseOut:
371  return SineEaseOut;
372  case kSineEaseInOut:
373  return SineEaseInOut;
374  case kCircularEaseIn:
375  return CircularEaseIn;
376  case kCircularEaseOut:
377  return CircularEaseOut;
378  case kCircularEaseInOut:
379  return CircularEaseInOut;
380  case kExponentialEaseIn:
381  return ExponentialEaseIn;
382  case kExponentialEaseOut:
383  return ExponentialEaseOut;
385  return ExponentialEaseInOut;
386  case kElasticEaseIn:
387  return ElasticEaseIn;
388  case kElasticEaseOut:
389  return ElasticEaseOut;
390  case kElasticEaseInOut:
391  return ElasticEaseInOut;
392  case kBackEaseIn:
393  return BackEaseIn;
394  case kBackEaseOut:
395  return BackEaseOut;
396  case kBackEaseInOut:
397  return BackEaseInOut;
398  case kBounceEaseIn:
399  return BounceEaseIn;
400  case kBounceEaseOut:
401  return BounceEaseOut;
402  case kBounceEaseInOut:
403  return BounceEaseInOut;
404  }
405  return NULL;
406  }
407 };
408 
409 #endif
Easing::kCircularEaseOut
@ kCircularEaseOut
Definition: ServoEasing.h:31
Easing::BackEaseIn
static float BackEaseIn(float p)
Definition: ServoEasing.h:273
ServoDispatch.h
Easing::CircularEaseInOut
static float CircularEaseInOut(float p)
Definition: ServoEasing.h:205
Easing::kElasticEaseOut
@ kElasticEaseOut
Definition: ServoEasing.h:37
Easing::kExponentialEaseOut
@ kExponentialEaseOut
Definition: ServoEasing.h:34
Easing::kCubicEaseInOut
@ kCubicEaseInOut
Definition: ServoEasing.h:20
Easing::CubicEaseOut
static float CubicEaseOut(float p)
Definition: ServoEasing.h:92
Easing::kBounceEaseInOut
@ kBounceEaseInOut
Definition: ServoEasing.h:44
Easing::kQuadraticEaseInOut
@ kQuadraticEaseInOut
Definition: ServoEasing.h:17
Easing::kCubicEaseIn
@ kCubicEaseIn
Definition: ServoEasing.h:18
Easing::kBackEaseOut
@ kBackEaseOut
Definition: ServoEasing.h:40
Easing::kElasticEaseIn
@ kElasticEaseIn
Definition: ServoEasing.h:36
Easing::kBackEaseInOut
@ kBackEaseInOut
Definition: ServoEasing.h:41
Easing::ExponentialEaseOut
static float ExponentialEaseOut(float p)
Definition: ServoEasing.h:224
Easing::SineEaseIn
static float SineEaseIn(float p)
Definition: ServoEasing.h:173
Easing::kQuarticEaseInOut
@ kQuarticEaseInOut
Definition: ServoEasing.h:23
Easing::QuadraticEaseOut
static float QuadraticEaseOut(float p)
Definition: ServoEasing.h:65
Easing::Method
float(* Method)(float)
Definition: ServoEasing.h:9
Easing::CircularEaseOut
static float CircularEaseOut(float p)
Definition: ServoEasing.h:197
Easing::kSineEaseInOut
@ kSineEaseInOut
Definition: ServoEasing.h:29
Easing::QuarticEaseOut
static float QuarticEaseOut(float p)
Definition: ServoEasing.h:121
Easing::kQuinticEaseInOut
@ kQuinticEaseInOut
Definition: ServoEasing.h:26
Easing::QuarticEaseIn
static float QuarticEaseIn(float p)
Definition: ServoEasing.h:115
Easing::ElasticEaseIn
static float ElasticEaseIn(float p)
Definition: ServoEasing.h:246
Easing::BackEaseInOut
static float BackEaseInOut(float p)
Definition: ServoEasing.h:288
Easing::kExponentialEaseInOut
@ kExponentialEaseInOut
Definition: ServoEasing.h:35
Easing::QuadraticEaseInOut
static float QuadraticEaseInOut(float p)
Definition: ServoEasing.h:73
Easing::kQuinticEaseIn
@ kQuinticEaseIn
Definition: ServoEasing.h:24
Easing::CubicEaseInOut
static float CubicEaseInOut(float p)
Definition: ServoEasing.h:101
Easing::kBackEaseIn
@ kBackEaseIn
Definition: ServoEasing.h:39
Easing::QuinticEaseInOut
static float QuinticEaseInOut(float p)
Definition: ServoEasing.h:159
Easing::kQuadraticEaseIn
@ kQuadraticEaseIn
Definition: ServoEasing.h:15
Easing::QuarticEaseInOut
static float QuarticEaseInOut(float p)
Definition: ServoEasing.h:130
Easing::CircularEaseIn
static float CircularEaseIn(float p)
Definition: ServoEasing.h:191
Easing::ElasticEaseOut
static float ElasticEaseOut(float p)
Definition: ServoEasing.h:252
Easing::SineEaseOut
static float SineEaseOut(float p)
Definition: ServoEasing.h:179
Easing::BounceEaseOut
static float BounceEaseOut(float p)
Definition: ServoEasing.h:307
Easing::kQuarticEaseOut
@ kQuarticEaseOut
Definition: ServoEasing.h:22
Easing::kElasticEaseInOut
@ kElasticEaseInOut
Definition: ServoEasing.h:38
Easing::QuinticEaseIn
static float QuinticEaseIn(float p)
Definition: ServoEasing.h:144
Easing::kQuadraticEaseOut
@ kQuadraticEaseOut
Definition: ServoEasing.h:16
Easing::kCubicEaseOut
@ kCubicEaseOut
Definition: ServoEasing.h:19
Easing::SineEaseInOut
static float SineEaseInOut(float p)
Definition: ServoEasing.h:185
Easing::ExponentialEaseInOut
static float ExponentialEaseInOut(float p)
Definition: ServoEasing.h:232
Easing::BackEaseOut
static float BackEaseOut(float p)
Definition: ServoEasing.h:279
Easing::kQuinticEaseOut
@ kQuinticEaseOut
Definition: ServoEasing.h:25
Easing::kSineEaseIn
@ kSineEaseIn
Definition: ServoEasing.h:27
Easing::QuadraticEaseIn
static float QuadraticEaseIn(float p)
Definition: ServoEasing.h:59
Easing::kQuarticEaseIn
@ kQuarticEaseIn
Definition: ServoEasing.h:21
Easing::BounceEaseIn
static float BounceEaseIn(float p)
Definition: ServoEasing.h:302
Easing::kCircularEaseInOut
@ kCircularEaseInOut
Definition: ServoEasing.h:32
Easing::kCircularEaseIn
@ kCircularEaseIn
Definition: ServoEasing.h:30
Easing::Continuous
static float Continuous(float p)
Definition: ServoEasing.h:53
Easing::kBounceEaseOut
@ kBounceEaseOut
Definition: ServoEasing.h:43
Easing::ExponentialEaseIn
static float ExponentialEaseIn(float p)
Definition: ServoEasing.h:218
Easing::getEasingMethod
static Method getEasingMethod(uint8_t i)
Definition: ServoEasing.h:336
Easing::kSineEaseOut
@ kSineEaseOut
Definition: ServoEasing.h:28
Easing::kBounceEaseIn
@ kBounceEaseIn
Definition: ServoEasing.h:42
Easing::ElasticEaseInOut
static float ElasticEaseInOut(float p)
Definition: ServoEasing.h:260
Easing::kExponentialEaseIn
@ kExponentialEaseIn
Definition: ServoEasing.h:33
Easing::kContinuous
@ kContinuous
Definition: ServoEasing.h:14
Easing::QuinticEaseOut
static float QuinticEaseOut(float p)
Definition: ServoEasing.h:150
Easing::LinearInterpolation
static float LinearInterpolation(float p)
Definition: ServoEasing.h:48
Easing::CubicEaseIn
static float CubicEaseIn(float p)
Definition: ServoEasing.h:86
Easing
Definition: ServoEasing.h:6
Easing::BounceEaseInOut
static float BounceEaseInOut(float p)
Definition: ServoEasing.h:324
Easing::kLinearInterpolation
@ kLinearInterpolation
Definition: ServoEasing.h:13