Blender  V3.3
easing.c
Go to the documentation of this file.
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright 2001 Robert Penner. All rights reserved. */
3 
8 #include "BLI_math_base.h"
9 
10 #include "BLI_easing.h" /* own include */
11 
12 #include "BLI_strict_flags.h"
13 
14 /* blend if (amplitude < fabsf(change) */
15 #define USE_ELASTIC_BLEND
16 
18  float time, float begin, float change, float duration, float overshoot)
19 {
20  time /= duration;
21  return change * time * time * ((overshoot + 1) * time - overshoot) + begin;
22 }
23 
25  float time, float begin, float change, float duration, float overshoot)
26 {
27  time = time / duration - 1;
28  return change * (time * time * ((overshoot + 1) * time + overshoot) + 1) + begin;
29 }
30 
32  float time, float begin, float change, float duration, float overshoot)
33 {
34  overshoot *= 1.525f;
35  if ((time /= duration / 2) < 1.0f) {
36  return change / 2 * (time * time * ((overshoot + 1) * time - overshoot)) + begin;
37  }
38  time -= 2.0f;
39  return change / 2 * (time * time * ((overshoot + 1) * time + overshoot) + 2) + begin;
40 }
41 
42 float BLI_easing_bounce_ease_out(float time, float begin, float change, float duration)
43 {
44  time /= duration;
45  if (time < (1 / 2.75f)) {
46  return change * (7.5625f * time * time) + begin;
47  }
48  if (time < (2 / 2.75f)) {
49  time -= (1.5f / 2.75f);
50  return change * ((7.5625f * time) * time + 0.75f) + begin;
51  }
52  if (time < (2.5f / 2.75f)) {
53  time -= (2.25f / 2.75f);
54  return change * ((7.5625f * time) * time + 0.9375f) + begin;
55  }
56  time -= (2.625f / 2.75f);
57  return change * ((7.5625f * time) * time + 0.984375f) + begin;
58 }
59 
60 float BLI_easing_bounce_ease_in(float time, float begin, float change, float duration)
61 {
62  return change - BLI_easing_bounce_ease_out(duration - time, 0, change, duration) + begin;
63 }
64 
65 float BLI_easing_bounce_ease_in_out(float time, float begin, float change, float duration)
66 {
67  if (time < duration / 2) {
68  return BLI_easing_bounce_ease_in(time * 2, 0, change, duration) * 0.5f + begin;
69  }
70  return BLI_easing_bounce_ease_out(time * 2 - duration, 0, change, duration) * 0.5f +
71  change * 0.5f + begin;
72 }
73 
74 float BLI_easing_circ_ease_in(float time, float begin, float change, float duration)
75 {
76  time /= duration;
77  return -change * (sqrtf(1 - time * time) - 1) + begin;
78 }
79 
80 float BLI_easing_circ_ease_out(float time, float begin, float change, float duration)
81 {
82  time = time / duration - 1;
83  return change * sqrtf(1 - time * time) + begin;
84 }
85 
86 float BLI_easing_circ_ease_in_out(float time, float begin, float change, float duration)
87 {
88  if ((time /= duration / 2) < 1.0f) {
89  return -change / 2 * (sqrtf(1 - time * time) - 1) + begin;
90  }
91  time -= 2.0f;
92  return change / 2 * (sqrtf(1 - time * time) + 1) + begin;
93 }
94 
95 float BLI_easing_cubic_ease_in(float time, float begin, float change, float duration)
96 {
97  time /= duration;
98  return change * time * time * time + begin;
99 }
100 
101 float BLI_easing_cubic_ease_out(float time, float begin, float change, float duration)
102 {
103  time = time / duration - 1;
104  return change * (time * time * time + 1) + begin;
105 }
106 
107 float BLI_easing_cubic_ease_in_out(float time, float begin, float change, float duration)
108 {
109  if ((time /= duration / 2) < 1.0f) {
110  return change / 2 * time * time * time + begin;
111  }
112  time -= 2.0f;
113  return change / 2 * (time * time * time + 2) + begin;
114 }
115 
116 #ifdef USE_ELASTIC_BLEND
121 static float elastic_blend(
122  float time, float change, float duration, float amplitude, float s, float f)
123 {
124  if (change) {
125  /* Looks like a magic number,
126  * but this is a part of the sine curve we need to blend from */
127  const float t = fabsf(s);
128  if (amplitude) {
129  f *= amplitude / fabsf(change);
130  }
131  else {
132  f = 0.0f;
133  }
134 
135  if (fabsf(time * duration) < t) {
136  float l = fabsf(time * duration) / t;
137  f = (f * l) + (1.0f - l);
138  }
139  }
140 
141  return f;
142 }
143 #endif
144 
146  float time, float begin, float change, float duration, float amplitude, float period)
147 {
148  float s;
149  float f = 1.0f;
150 
151  if (time == 0.0f) {
152  return begin;
153  }
154 
155  if ((time /= duration) == 1.0f) {
156  return begin + change;
157  }
158  time -= 1.0f;
159  if (!period) {
160  period = duration * 0.3f;
161  }
162  if (!amplitude || amplitude < fabsf(change)) {
163  s = period / 4;
164 #ifdef USE_ELASTIC_BLEND
165  f = elastic_blend(time, change, duration, amplitude, s, f);
166 #endif
167  amplitude = change;
168  }
169  else {
170  s = period / (2 * (float)M_PI) * asinf(change / amplitude);
171  }
172 
173  return (-f * (amplitude * powf(2, 10 * time) *
174  sinf((time * duration - s) * (2 * (float)M_PI) / period))) +
175  begin;
176 }
177 
179  float time, float begin, float change, float duration, float amplitude, float period)
180 {
181  float s;
182  float f = 1.0f;
183 
184  if (time == 0.0f) {
185  return begin;
186  }
187  if ((time /= duration) == 1.0f) {
188  return begin + change;
189  }
190  time = -time;
191  if (!period) {
192  period = duration * 0.3f;
193  }
194  if (!amplitude || amplitude < fabsf(change)) {
195  s = period / 4;
196 #ifdef USE_ELASTIC_BLEND
197  f = elastic_blend(time, change, duration, amplitude, s, f);
198 #endif
199  amplitude = change;
200  }
201  else {
202  s = period / (2 * (float)M_PI) * asinf(change / amplitude);
203  }
204 
205  return (f * (amplitude * powf(2, 10 * time) *
206  sinf((time * duration - s) * (2 * (float)M_PI) / period))) +
207  change + begin;
208 }
209 
211  float time, float begin, float change, float duration, float amplitude, float period)
212 {
213  float s;
214  float f = 1.0f;
215 
216  if (time == 0.0f) {
217  return begin;
218  }
219  if ((time /= duration / 2) == 2.0f) {
220  return begin + change;
221  }
222  time -= 1.0f;
223  if (!period) {
224  period = duration * (0.3f * 1.5f);
225  }
226  if (!amplitude || amplitude < fabsf(change)) {
227  s = period / 4;
228 #ifdef USE_ELASTIC_BLEND
229  f = elastic_blend(time, change, duration, amplitude, s, f);
230 #endif
231  amplitude = change;
232  }
233  else {
234  s = period / (2 * (float)M_PI) * asinf(change / amplitude);
235  }
236 
237  if (time < 0.0f) {
238  f *= -0.5f;
239  return (f * (amplitude * powf(2, 10 * time) *
240  sinf((time * duration - s) * (2 * (float)M_PI) / period))) +
241  begin;
242  }
243 
244  time = -time;
245  f *= 0.5f;
246  return (f * (amplitude * powf(2, 10 * time) *
247  sinf((time * duration - s) * (2 * (float)M_PI) / period))) +
248  change + begin;
249 }
250 
251 static const float pow_min = 0.0009765625f; /* = 2^(-10) */
252 static const float pow_scale = 1.0f / (1.0f - 0.0009765625f);
253 
254 float BLI_easing_expo_ease_in(float time, float begin, float change, float duration)
255 {
256  if (time == 0.0) {
257  return begin;
258  }
259  return change * (powf(2, 10 * (time / duration - 1)) - pow_min) * pow_scale + begin;
260 }
261 
262 float BLI_easing_expo_ease_out(float time, float begin, float change, float duration)
263 {
264  if (time == 0.0) {
265  return begin;
266  }
267  return change * (1 - (powf(2, -10 * time / duration) - pow_min) * pow_scale) + begin;
268 }
269 
270 float BLI_easing_expo_ease_in_out(float time, float begin, float change, float duration)
271 {
272  float duration_half = duration / 2.0f;
273  float change_half = change / 2.0f;
274  if (time <= duration_half) {
275  return BLI_easing_expo_ease_in(time, begin, change_half, duration_half);
276  }
278  time - duration_half, begin + change_half, change_half, duration_half);
279 }
280 
281 float BLI_easing_linear_ease(float time, float begin, float change, float duration)
282 {
283  return change * time / duration + begin;
284 }
285 
286 float BLI_easing_quad_ease_in(float time, float begin, float change, float duration)
287 {
288  time /= duration;
289  return change * time * time + begin;
290 }
291 
292 float BLI_easing_quad_ease_out(float time, float begin, float change, float duration)
293 {
294  time /= duration;
295  return -change * time * (time - 2) + begin;
296 }
297 
298 float BLI_easing_quad_ease_in_out(float time, float begin, float change, float duration)
299 {
300  if ((time /= duration / 2) < 1.0f) {
301  return change / 2 * time * time + begin;
302  }
303  time -= 1.0f;
304  return -change / 2 * (time * (time - 2) - 1) + begin;
305 }
306 
307 float BLI_easing_quart_ease_in(float time, float begin, float change, float duration)
308 {
309  time /= duration;
310  return change * time * time * time * time + begin;
311 }
312 
313 float BLI_easing_quart_ease_out(float time, float begin, float change, float duration)
314 {
315  time = time / duration - 1;
316  return -change * (time * time * time * time - 1) + begin;
317 }
318 
319 float BLI_easing_quart_ease_in_out(float time, float begin, float change, float duration)
320 {
321  if ((time /= duration / 2) < 1.0f) {
322  return change / 2 * time * time * time * time + begin;
323  }
324  time -= 2.0f;
325  return -change / 2 * (time * time * time * time - 2) + begin;
326 }
327 
328 float BLI_easing_quint_ease_in(float time, float begin, float change, float duration)
329 {
330  time /= duration;
331  return change * time * time * time * time * time + begin;
332 }
333 float BLI_easing_quint_ease_out(float time, float begin, float change, float duration)
334 {
335  time = time / duration - 1;
336  return change * (time * time * time * time * time + 1) + begin;
337 }
338 float BLI_easing_quint_ease_in_out(float time, float begin, float change, float duration)
339 {
340  if ((time /= duration / 2) < 1.0f) {
341  return change / 2 * time * time * time * time * time + begin;
342  }
343  time -= 2.0f;
344  return change / 2 * (time * time * time * time * time + 2) + begin;
345 }
346 
347 float BLI_easing_sine_ease_in(float time, float begin, float change, float duration)
348 {
349  return -change * cosf(time / duration * (float)M_PI_2) + change + begin;
350 }
351 
352 float BLI_easing_sine_ease_out(float time, float begin, float change, float duration)
353 {
354  return change * sinf(time / duration * (float)M_PI_2) + begin;
355 }
356 
357 float BLI_easing_sine_ease_in_out(float time, float begin, float change, float duration)
358 {
359  return -change / 2 * (cosf((float)M_PI * time / duration) - 1) + begin;
360 }
typedef float(TangentPoint)[2]
#define M_PI_2
Definition: BLI_math_base.h:23
#define M_PI
Definition: BLI_math_base.h:20
Strict compiler flags for areas of code we want to ensure don't do conversions without us knowing abo...
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum const void *lists _GL_VOID_RET _GL_VOID const GLdouble *equation _GL_VOID_RET _GL_VOID GLdouble GLdouble blue _GL_VOID_RET _GL_VOID GLfloat GLfloat blue _GL_VOID_RET _GL_VOID GLint GLint blue _GL_VOID_RET _GL_VOID GLshort GLshort blue _GL_VOID_RET _GL_VOID GLubyte GLubyte blue _GL_VOID_RET _GL_VOID GLuint GLuint blue _GL_VOID_RET _GL_VOID GLushort GLushort blue _GL_VOID_RET _GL_VOID GLbyte GLbyte GLbyte alpha _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble alpha _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat alpha _GL_VOID_RET _GL_VOID GLint GLint GLint alpha _GL_VOID_RET _GL_VOID GLshort GLshort GLshort alpha _GL_VOID_RET _GL_VOID GLubyte GLubyte GLubyte alpha _GL_VOID_RET _GL_VOID GLuint GLuint GLuint alpha _GL_VOID_RET _GL_VOID GLushort GLushort GLushort alpha _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLint GLsizei GLsizei GLenum type _GL_VOID_RET _GL_VOID GLsizei GLenum GLenum const void *pixels _GL_VOID_RET _GL_VOID const void *pointer _GL_VOID_RET _GL_VOID GLdouble v _GL_VOID_RET _GL_VOID GLfloat v _GL_VOID_RET _GL_VOID GLint GLint i2 _GL_VOID_RET _GL_VOID GLint j _GL_VOID_RET _GL_VOID GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble GLdouble GLdouble zFar _GL_VOID_RET _GL_UINT GLdouble *equation _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLenum GLfloat *v _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLfloat *values _GL_VOID_RET _GL_VOID GLushort *values _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLenum GLdouble *params _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_BOOL GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLenum GLfloat param _GL_VOID_RET _GL_VOID GLenum GLint param _GL_VOID_RET _GL_VOID GLushort pattern _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLint const GLdouble *points _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLint GLdouble GLdouble GLint GLint const GLdouble *points _GL_VOID_RET _GL_VOID GLdouble GLdouble u2 _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLdouble GLdouble v2 _GL_VOID_RET _GL_VOID GLenum GLfloat param _GL_VOID_RET _GL_VOID GLenum GLint param _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLdouble GLdouble nz _GL_VOID_RET _GL_VOID GLfloat GLfloat nz _GL_VOID_RET _GL_VOID GLint GLint nz _GL_VOID_RET _GL_VOID GLshort GLshort nz _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_VOID GLsizei const GLfloat *values _GL_VOID_RET _GL_VOID GLsizei const GLushort *values _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID const GLuint const GLclampf *priorities _GL_VOID_RET _GL_VOID GLdouble y _GL_VOID_RET _GL_VOID GLfloat y _GL_VOID_RET _GL_VOID GLint y _GL_VOID_RET _GL_VOID GLshort y _GL_VOID_RET _GL_VOID GLdouble GLdouble z _GL_VOID_RET _GL_VOID GLfloat GLfloat z _GL_VOID_RET _GL_VOID GLint GLint z _GL_VOID_RET _GL_VOID GLshort GLshort z _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble w _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat w _GL_VOID_RET _GL_VOID GLint GLint GLint w _GL_VOID_RET _GL_VOID GLshort GLshort GLshort w _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble y2 _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat y2 _GL_VOID_RET _GL_VOID GLint GLint GLint y2 _GL_VOID_RET _GL_VOID GLshort GLshort GLshort y2 _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble z _GL_VOID_RET _GL_VOID GLdouble GLdouble z _GL_VOID_RET _GL_VOID GLuint *buffer _GL_VOID_RET _GL_VOID GLdouble t _GL_VOID_RET _GL_VOID GLfloat t _GL_VOID_RET _GL_VOID GLint t _GL_VOID_RET _GL_VOID GLshort t _GL_VOID_RET _GL_VOID GLdouble t
ATTR_WARN_UNUSED_RESULT const BMLoop * l
#define sinf(x)
Definition: cuda/compat.h:102
#define cosf(x)
Definition: cuda/compat.h:101
#define powf(x, y)
Definition: cuda/compat.h:103
double time
float BLI_easing_sine_ease_in(float time, float begin, float change, float duration)
Definition: easing.c:347
float BLI_easing_back_ease_out(float time, float begin, float change, float duration, float overshoot)
Definition: easing.c:24
float BLI_easing_linear_ease(float time, float begin, float change, float duration)
Definition: easing.c:281
float BLI_easing_bounce_ease_in_out(float time, float begin, float change, float duration)
Definition: easing.c:65
float BLI_easing_quint_ease_out(float time, float begin, float change, float duration)
Definition: easing.c:333
float BLI_easing_quart_ease_in_out(float time, float begin, float change, float duration)
Definition: easing.c:319
float BLI_easing_circ_ease_in_out(float time, float begin, float change, float duration)
Definition: easing.c:86
float BLI_easing_quart_ease_out(float time, float begin, float change, float duration)
Definition: easing.c:313
float BLI_easing_bounce_ease_in(float time, float begin, float change, float duration)
Definition: easing.c:60
float BLI_easing_circ_ease_in(float time, float begin, float change, float duration)
Definition: easing.c:74
float BLI_easing_expo_ease_in(float time, float begin, float change, float duration)
Definition: easing.c:254
float BLI_easing_expo_ease_out(float time, float begin, float change, float duration)
Definition: easing.c:262
float BLI_easing_elastic_ease_in(float time, float begin, float change, float duration, float amplitude, float period)
Definition: easing.c:145
float BLI_easing_quad_ease_in_out(float time, float begin, float change, float duration)
Definition: easing.c:298
float BLI_easing_elastic_ease_out(float time, float begin, float change, float duration, float amplitude, float period)
Definition: easing.c:178
float BLI_easing_cubic_ease_in(float time, float begin, float change, float duration)
Definition: easing.c:95
float BLI_easing_elastic_ease_in_out(float time, float begin, float change, float duration, float amplitude, float period)
Definition: easing.c:210
float BLI_easing_quint_ease_in(float time, float begin, float change, float duration)
Definition: easing.c:328
float BLI_easing_sine_ease_out(float time, float begin, float change, float duration)
Definition: easing.c:352
float BLI_easing_bounce_ease_out(float time, float begin, float change, float duration)
Definition: easing.c:42
float BLI_easing_quad_ease_in(float time, float begin, float change, float duration)
Definition: easing.c:286
float BLI_easing_quad_ease_out(float time, float begin, float change, float duration)
Definition: easing.c:292
float BLI_easing_circ_ease_out(float time, float begin, float change, float duration)
Definition: easing.c:80
float BLI_easing_cubic_ease_out(float time, float begin, float change, float duration)
Definition: easing.c:101
float BLI_easing_back_ease_in(float time, float begin, float change, float duration, float overshoot)
Definition: easing.c:17
static float elastic_blend(float time, float change, float duration, float amplitude, float s, float f)
Definition: easing.c:121
float BLI_easing_quint_ease_in_out(float time, float begin, float change, float duration)
Definition: easing.c:338
float BLI_easing_expo_ease_in_out(float time, float begin, float change, float duration)
Definition: easing.c:270
static const float pow_min
Definition: easing.c:251
float BLI_easing_quart_ease_in(float time, float begin, float change, float duration)
Definition: easing.c:307
static const float pow_scale
Definition: easing.c:252
float BLI_easing_back_ease_in_out(float time, float begin, float change, float duration, float overshoot)
Definition: easing.c:31
float BLI_easing_cubic_ease_in_out(float time, float begin, float change, float duration)
Definition: easing.c:107
float BLI_easing_sine_ease_in_out(float time, float begin, float change, float duration)
Definition: easing.c:357
#define asinf(x)
Definition: metal/compat.h:221
#define fabsf(x)
Definition: metal/compat.h:219
#define sqrtf(x)
Definition: metal/compat.h:243