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