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