Debian Codegraph @

src/transition/curve.c

raw symbols SCIP
1 // SPDX-License-Identifier: MPL-2.0
2 // Copyright (c) Yuxuan Shui <[email protected]>
3
4 #include <assert.h>
5 #include <stdbool.h>
6 #include <stddef.h>
7
8 #include "compiler.h"
9 #include "utils/misc.h"
10 #include "utils/str.h"
11
12 #include "curve.h"
13
14 static double curve_sample_linear(const struct curve *this attr_unused, double progress) {
15 return progress;
16 }
17
18 static char *curve_linear_to_c(const struct curve * /*this*/) {
19 return strdup("{.type = CURVE_LINEAR},");
20 }
21
22 // Cubic bezier interpolator.
23 //
24 // Stolen from servo:
25 // https://searchfox.org/mozilla-central/rev/5da2d56d12/servo/components/style/bezier.rs
26
27 static inline double cubic_bezier_sample_x(const struct curve_cubic_bezier *self, double t) {
28 return ((self->ax * t + self->bx) * t + self->cx) * t;
29 }
30
31 static inline double cubic_bezier_sample_y(const struct curve_cubic_bezier *self, double t) {
32 return ((self->ay * t + self->by) * t + self->cy) * t;
33 }
34
35 static inline double
36 cubic_bezier_sample_derivative_x(const struct curve_cubic_bezier *self, double t) {
37 return (3.0 * self->ax * t + 2.0 * self->bx) * t + self->cx;
38 }
39
40 // Solve for the `t` in cubic bezier function that corresponds to `x`
41 static inline double cubic_bezier_solve_x(const struct curve_cubic_bezier *this, double x) {
42 static const int NEWTON_METHOD_ITERATIONS = 8;
43 double t = x;
44 // Fast path: try Newton's method.
45 for (int i = 0; i < NEWTON_METHOD_ITERATIONS; i++) {
46 double x2 = cubic_bezier_sample_x(this, t);
47 if (fabs(x2 - x) < 1e-7) {
48 return t;
49 }
50 double dx = cubic_bezier_sample_derivative_x(this, t);
51 if (fabs(dx) < 1e-6) {
52 break;
53 }
54 t -= (x2 - x) / dx;
55 }
56
57 // Slow path: Use bisection.
58 double low = 0.0, high = 1.0;
59 t = x;
60 while (high - low > 1e-7) {
61 double x2 = cubic_bezier_sample_x(this, t);
62 if (fabs(x2 - x) < 1e-7) {
63 return t;
64 }
65 if (x > x2) {
66 low = t;
67 } else {
68 high = t;
69 }
70 t = (high - low) / 2.0 + low;
71 }
72 return t;
73 }
74
75 static double
76 curve_sample_cubic_bezier(const struct curve_cubic_bezier *curve, double progress) {
77 assert(progress >= 0 && progress <= 1);
78 if (progress == 0 || progress == 1) {
79 return progress;
80 }
81 double t = cubic_bezier_solve_x(curve, progress);
82 return cubic_bezier_sample_y(curve, t);
83 }
84
85 static char *curve_cubic_bezier_to_c(const struct curve_cubic_bezier *curve) {
86 char *buf = NULL;
87 casprintf(&buf,
88 "{.type = CURVE_CUBIC_BEZIER, .bezier = { .ax = %a, .bx = %a, "
89 ".cx = %a, .ay = %a, .by = %a, .cy = %a }},",
90 curve->ax, curve->bx, curve->cx, curve->ay, curve->by, curve->cy);
91 return buf;
92 }
93
94 static double curve_sample_step(const struct curve_step *this, double progress) {
95 double y_steps = this->steps - 1 + this->jump_end + this->jump_start,
96 x_steps = this->steps;
97 if (progress == 1) {
98 return 1;
99 }
100 if (progress == 0) {
101 return this->jump_start ? 1 / y_steps : 0;
102 }
103
104 double scaled = progress * x_steps;
105 double quantized = this->jump_start ? ceil(scaled) : floor(scaled);
106 return quantized / y_steps;
107 }
108
109 static char *curve_step_to_c(const struct curve_step *this) {
110 char *buf = NULL;
111 casprintf(&buf,
112 "{.type = CURVE_STEP, .step = { .steps = %d, .jump_start = %s, "
113 ".jump_end = %s }},",
114 this->steps, this->jump_start ? "true" : "false",
115 this->jump_end ? "true" : "false");
116 return buf;
117 }
118
119 struct curve parse_linear(const char *str, const char **end, char **err) {
120 *end = str;
121 *err = NULL;
122 return CURVE_LINEAR_INIT;
123 }
124
125 struct curve parse_steps(const char *input_str, const char **out_end, char **err) {
126 const char *str = input_str;
127 *err = NULL;
128 if (*str != '(') {
129 casprintf(err, "Invalid steps %s.", str);
130 return CURVE_INVALID_INIT;
131 }
132 str += 1;
133 str = skip_space(str);
134 char *end;
135 auto steps = strtol(str, &end, 10);
136 if (end == str || steps > INT_MAX) {
137 casprintf(err, "Invalid step count at \"%s\".", str);
138 return CURVE_INVALID_INIT;
139 }
140 str = skip_space(end);
141 if (*str != ',') {
142 casprintf(err, "Invalid steps argument list \"%s\".", input_str);
143 return CURVE_INVALID_INIT;
144 }
145 str = skip_space(str + 1);
146 bool jump_start =
147 starts_with(str, "jump-start", true) || starts_with(str, "jump-both", true);
148 bool jump_end =
149 starts_with(str, "jump-end", true) || starts_with(str, "jump-both", true);
150 if (!jump_start && !jump_end && !starts_with(str, "jump-none", true)) {
151 casprintf(err, "Invalid jump setting for steps \"%s\".", str);
152 return CURVE_INVALID_INIT;
153 }
154 str += jump_start ? (jump_end ? 9 : 10) : (jump_end ? 8 : 9);
155 str = skip_space(str);
156 if (*str != ')') {
157 casprintf(err, "Invalid steps argument list \"%s\".", input_str);
158 return CURVE_INVALID_INIT;
159 }
160 *out_end = str + 1;
161 return curve_new_step((int)steps, jump_start, jump_end);
162 }
163
164 struct curve parse_cubic_bezier(const char *input_str, const char **out_end, char **err) {
165 double numbers[4];
166 const char *str = input_str;
167 if (*str != '(') {
168 casprintf(err, "Invalid cubic-bazier %s.", str);
169 return CURVE_INVALID_INIT;
170 }
171 str += 1;
172 for (int i = 0; i < 4; i++) {
173 str = skip_space(str);
174
175 const char *end = NULL;
176 numbers[i] = strtod_simple(str, &end);
177 if (end == str) {
178 casprintf(err, "Invalid number %s.", str);
179 return CURVE_INVALID_INIT;
180 }
181 str = skip_space(end);
182 const char expected = i == 3 ? ')' : ',';
183 if (*str != expected) {
184 casprintf(err, "Invalid cubic-bazier argument list %s.", input_str);
185 return CURVE_INVALID_INIT;
186 }
187 str += 1;
188 }
189 *out_end = str;
190 return curve_new_cubic_bezier(numbers[0], numbers[1], numbers[2], numbers[3]);
191 }
192
193 typedef struct curve (*curve_parser)(const char *str, const char **end, char **err);
194
195 static const struct {
196 curve_parser parse;
197 const char *name;
198 } curve_parsers[] = {
199 {parse_cubic_bezier, "cubic-bezier"},
200 {parse_linear, "linear"},
201 {parse_steps, "steps"},
202 };
203
204 struct curve curve_parse(const char *str, const char **end, char **err) {
205 str = skip_space(str);
206 for (size_t i = 0; i < ARR_SIZE(curve_parsers); i++) {
207 auto name_len = strlen(curve_parsers[i].name);
208 if (strncasecmp(str, curve_parsers[i].name, name_len) == 0) {
209 return curve_parsers[i].parse(str + name_len, end, err);
210 }
211 }
212 casprintf(err, "Unknown curve type \"%s\".", str);
213 return CURVE_INVALID_INIT;
214 }
215
216 double curve_sample(const struct curve *curve, double progress) {
217 switch (curve->type) {
218 case CURVE_LINEAR: return curve_sample_linear(curve, progress);
219 case CURVE_STEP: return curve_sample_step(&curve->step, progress);
220 case CURVE_CUBIC_BEZIER:
221 return curve_sample_cubic_bezier(&curve->bezier, progress);
222 case CURVE_INVALID:
223 default: unreachable();
224 }
225 }
226
227 char *curve_to_c(const struct curve *curve) {
228 switch (curve->type) {
229 case CURVE_LINEAR: return curve_linear_to_c(curve);
230 case CURVE_STEP: return curve_step_to_c(&curve->step);
231 case CURVE_CUBIC_BEZIER: return curve_cubic_bezier_to_c(&curve->bezier);
232 case CURVE_INVALID:
233 default: unreachable();
234 }
235 }