-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzglEaseInterpolator.cpp
executable file
·415 lines (336 loc) · 8.16 KB
/
zglEaseInterpolator.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
/*
* Copyright (C) 2010 ZiiLABS Pte Ltd
* All Rights Reserved.
*
* This software and its associated documentation may contain some
* proprietary, confidential and trade secret information of
* ZiiLABS Pte Ltd and except as provided by written agreement with
* ZiiLABS Pte Ltd
*
* a) no part may be disclosed, distributed, reproduced, transmitted,
* transcribed, stored in a retrieval system, adapted or translated
* in any form or by any means electronic, mechanical, magnetic,
* optical, chemical, manual or otherwise,
*
* and
*
* b) the recipient is not entitled to discover through reverse
* engineering or reverse compiling or other such techniques or
* processes the trade secrets contained therein or in the
* documentation.
*/
#include <math.h>
#include "zglEaseInterpolator.h"
#include "zglBasicType.h"
typedef float (*getValueFunc)(float input);
/* quadratic */
static float ease_in_quad(float input);
static float ease_out_quad(float input);
static float ease_in_out_quad(float input);
/* cubic */
static float ease_in_cubic(float input);
static float ease_out_cubic(float input);
static float ease_in_out_cubic(float input);
/* quartic */
static float ease_in_quart(float input);
static float ease_out_quart(float input);
static float ease_in_out_quart(float input);
/* quintic */
static float ease_in_quint(float input);
static float ease_out_quint(float input);
static float ease_in_out_quint(float input);
/* sinusoidal */
static float ease_in_sine(float input);
static float ease_out_sine(float input);
static float ease_in_out_sine(float input);
/* exponential */
static float ease_in_expo(float input);
static float ease_out_expo(float input);
static float ease_in_out_expo(float input);
/* circular */
static float ease_in_circ(float input);
static float ease_out_circ(float input);
static float ease_in_out_circ(float input);
/* elastic */
static float ease_in_elastic(float input);
static float ease_out_elastic(float input);
static float ease_in_out_elastic(float input);
/* overshooting cubic */
static float ease_in_back(float input);
static float ease_out_back(float input);
static float ease_in_out_back(float input);
/* exponentially decaying parabolic */
static float ease_in_bound(float input);
static float ease_out_bound(float input);
static float ease_in_out_bound(float input);
static const struct {
int mode;
getValueFunc func;
} g_interpolates[] =
{
/* quadratic */
{ EASE_IN_QUAD, ease_in_quad },
{ EASE_OUT_QUAD, ease_out_quad },
{ EASE_IN_OUT_QUAD, ease_in_out_quad },
/* cubic */
{ EASE_IN_CUBIC, ease_in_cubic},
{ EASE_OUT_CUBIC, ease_out_cubic},
{ EASE_IN_OUT_CUBIC, ease_in_out_cubic},
/* quartic */
{ EASE_IN_QUART, ease_in_quart},
{ EASE_OUT_QUART, ease_out_quart},
{ EASE_IN_OUT_QUART, ease_in_out_quart},
/* quintic */
{ EASE_IN_QUINT, ease_in_quint},
{ EASE_OUT_QUINT, ease_out_quint},
{ EASE_IN_OUT_QUINT, ease_in_out_quint},
/* sinusoidal */
{ EASE_IN_SINE, ease_in_sine},
{ EASE_OUT_SINE, ease_out_sine},
{ EASE_IN_OUT_SINE, ease_in_out_sine},
/* exponential */
{ EASE_IN_EXPO, ease_in_expo},
{ EASE_OUT_EXPO, ease_out_expo},
{ EASE_IN_OUT_EXPO, ease_in_out_expo},
/* circular */
{ EASE_IN_CIRC, ease_in_circ},
{ EASE_OUT_CIRC, ease_out_circ},
{ EASE_IN_OUT_CIRC, ease_in_out_circ},
/* elastic */
{ EASE_IN_ELASTIC, ease_in_elastic },
{ EASE_OUT_ELASTIC, ease_out_elastic },
{ EASE_IN_OUT_ELASTIC, ease_in_out_elastic },
/* overshooting cubic */
{ EASE_IN_BACK, ease_in_back },
{ EASE_OUT_BACK, ease_out_back },
{ EASE_IN_OUT_BACK, ease_in_out_back },
/* exponentially decaying parabolic */
{ EASE_IN_BOUNCE, ease_in_bound },
{ EASE_OUT_BOUNCE, ease_out_bound },
{ EASE_IN_OUT_BOUNCE, ease_in_out_bound },
};
float zglEaseInterpolator::getValue(float input)
{
if(m_mode > EASE_MAX )
{
return input;
}
return g_interpolates[m_mode].func(input);
}
///////////////////////////////////////////////////////////////////////////////
/* quadratic */
static float ease_in_quad(float t)
{
return (float) t * t;
}
static float ease_out_quad(float t)
{
return -(float) t * (t - 2.0f);
}
static float ease_in_out_quad(float t)
{
double st = t * 2.0;
if (st < 1.0)
return (float) (0.5 * st * st);
st -= 1.0;
return (float) (-0.5 * (st * (st - 2) - 1));
}
/* cubic */
static float ease_in_cubic(float t)
{
return (float) t * t * t;
}
static float ease_out_cubic(float t)
{
float st = t - 1.0f;
return (float) st * st * st + 1.0f;
}
static float ease_in_out_cubic(float t)
{
float st = t * 2;
if (st < 1)
return 0.5 * st * st * st;
st -= 2.0f;
return 0.5 * (st * st * st + 2);
}
/* quartic */
static float ease_in_quart(float t)
{
return (float) t * t * t * t;
}
static float ease_out_quart(float t)
{
float st = t - 1.0f;
return -(float) (st * st * st * st - 1);
}
static float ease_in_out_quart(float t)
{
float st = t * 2;
if (st < 1)
return 0.5 * st * st * st * st;
st -= 2.0f;
return -0.5 * (st * st * st * st - 2);
}
/* quintic */
static float ease_in_quint(float t)
{
return (float) t * t * t * t * t;
}
static float ease_out_quint(float t)
{
float st = t - 1.0f;
return (float) (st * st * st * st * st + 1);
}
static float ease_in_out_quint(float t)
{
float st = t * 2;
if (st < 1)
return 0.5 * st * st * st * st * st;
st -= 2.0f;
return 0.5 * (st * st * st * st * st + 2);
}
/* sinusoidal */
static float ease_in_sine(float t)
{
return -(float) cos(t * ZGL_PI2) + 1.0f;
}
static float ease_out_sine(float t)
{
return (float) sin(t * ZGL_PI2);
}
static float ease_in_out_sine(float t)
{
return (float) (-0.5 * (cos(ZGL_PI * t) - 1));
}
/* exponential */
static float ease_in_expo(float t)
{
if (t == 0.0f)
return 0.0f;
return (float) pow(2, 10 * (t - 1.0f));
}
static float ease_out_expo(float t)
{
if (t == 1.0f)
return 1.0f;
return (float) (-pow(2, -10 * t) + 1);
}
static float ease_in_out_expo(float t)
{
if (t == 0.0f)
return 0.0f;
else if (t == 1.0f)
return 1.0f;
float st = t * 2;
if (st < 1.0f)
return 0.5f * pow(2, 10 * (st - 1.0f));
st -= 1.0f;
return 0.5f * (-pow(2, -10 * st) + 2);
}
/* circular */
static float ease_in_circ(float t)
{
return -1.0f * (sqrt(1 - t * t) - 1);
}
static float ease_out_circ(float t)
{
float st = t - 1.0f;
return (float)sqrt(1 - st * st);
}
static float ease_in_out_circ(float t)
{
float st = t * 2;
if (st < 1)
return -0.5f * (sqrt(1 - st * st) - 1);
st -= 2.0f;
return 0.5f * (sqrt(1 - st * st) + 1);
}
/* elastic */
static float ease_in_elastic(float t)
{
if( t == 1.0f) return 1.0f;
t -= 1.0f;
return -(pow(2, 10 * t) * sin((t * 10.0 / 3.0 - 0.25) * ZGL_2PI));
}
static float ease_out_elastic(float t)
{
if( t == 1.0f) return 1.0f;
return pow(2, -10 * t) * sin((t * 10.0 / 3.0 - 0.25) * ZGL_2PI) + 1.0f;
}
static float ease_in_out_elastic(float t)
{
float st = t * 2;
if(st < 1)
{
st -= 1;
return -0.5f * (pow(2, 10 * st) * sin( (st * 20.0f / 9 - 0.25f) * ZGL_2PI ) );
}
else
{
st -= 1;
return pow(2, -10 * st) * sin( (st * 20.0f / 9 - 0.25f) * ZGL_2PI ) * 0.5f + 1.0f;
}
return t;
}
/* overshooting cubic */
static float ease_in_back(float t)
{
return (float)(t * t * ((1.70158 + 1) * t - 1.70158));
}
static float ease_out_back(float t)
{
float st = t - 1.0f;
return st * st * ((1.70158 + 1) * st + 1.70158) + 1.0f;
}
static float ease_in_out_back(float t)
{
float st = t * 2;
double s = 1.70158 * 1.525;
if (st < 1.0f)
return 0.5f * (st * st * ((s + 1) * st - s));
st -= 2.0f;
return 0.5f * (st * st * ((s + 1) * st + s) + 2);
}
/* exponentially decaying parabolic */
static float ease_bound_internal(float t)
{
if (t < 1.0f / 2.75)
{
return 7.5625 * t * t;
}
else if (t < 2.0f / 2.75)
{
t -= (1.5f / 2.75);
return 7.5625 * t * t + 0.75;
}
else if (t < 2.5f / 2.75)
{
t -= (2.25f / 2.75);
return 7.5625 * t * t + 0.9375f;
}
else
{
t -= (2.625f / 2.75);
return 7.5625 * t * t + 0.984375f;
}
}
static float ease_in_bound(float t)
{
return 1.0f - ease_bound_internal(1.0f - t);
}
static float ease_out_bound(float t)
{
return ease_bound_internal(t);
}
static float ease_in_out_bound(float t)
{
float st = t * 2;
if( st < 1)
{
return (1.0f - ease_bound_internal(1.0f - st)) * 0.5f;
}
else
{
return ease_bound_internal(st - 1.0f) * 0.5f + 0.5f;
}
}