shumate/vector/shumate-vector-expression-interpolate.c
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * Copyright (C) 2021 James Westman <james@jwestman.net> | ||
| 3 | * | ||
| 4 | * This library is free software; you can redistribute it and/or | ||
| 5 | * modify it under the terms of the GNU Lesser General Public | ||
| 6 | * License as published by the Free Software Foundation; either | ||
| 7 | * version 2.1 of the License, or (at your option) any later version. | ||
| 8 | * | ||
| 9 | * This library is distributed in the hope that it will be useful, | ||
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| 12 | * Lesser General Public License for more details. | ||
| 13 | * | ||
| 14 | * You should have received a copy of the GNU Lesser General Public | ||
| 15 | * License along with this library; if not, see <https://www.gnu.org/licenses/>. | ||
| 16 | */ | ||
| 17 | |||
| 18 | #include "shumate-vector-renderer.h" | ||
| 19 | #include "shumate-vector-expression-interpolate-private.h" | ||
| 20 | #include "shumate-vector-expression-filter-private.h" | ||
| 21 | #include "shumate-vector-utils-private.h" | ||
| 22 | |||
| 23 | typedef struct { | ||
| 24 | double point; | ||
| 25 | ShumateVectorExpression *expr; | ||
| 26 | } Stop; | ||
| 27 | |||
| 28 | typedef enum { | ||
| 29 | STEP, | ||
| 30 | LINEAR, | ||
| 31 | EXPONENTIAL, | ||
| 32 | } InterpolationType; | ||
| 33 | |||
| 34 | struct _ShumateVectorExpressionInterpolate | ||
| 35 | { | ||
| 36 | ShumateVectorExpression parent_instance; | ||
| 37 | |||
| 38 | ShumateVectorExpression *input; | ||
| 39 | InterpolationType interpolation; | ||
| 40 | double base; | ||
| 41 | GPtrArray *stops; | ||
| 42 | }; | ||
| 43 | |||
| 44 |
4/6shumate_vector_expression_interpolate_class_intern_init:
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 2 times.
shumate_vector_expression_interpolate_get_type:
✓ Branch 2 → 3 taken 2 times.
✓ Branch 2 → 7 taken 18 times.
✓ Branch 4 → 5 taken 2 times.
✗ Branch 4 → 7 not taken.
|
24 | G_DEFINE_TYPE (ShumateVectorExpressionInterpolate, shumate_vector_expression_interpolate, SHUMATE_TYPE_VECTOR_EXPRESSION) |
| 45 | |||
| 46 | |||
| 47 | ShumateVectorExpression * | ||
| 48 | 6 | shumate_vector_expression_interpolate_from_json_obj (JsonObject *object, GError **error) | |
| 49 | { | ||
| 50 | 6 | g_autoptr(ShumateVectorExpressionInterpolate) self = g_object_new (SHUMATE_TYPE_VECTOR_EXPRESSION_INTERPOLATE, NULL); | |
| 51 | 6 | JsonNode *stops_node; | |
| 52 | |||
| 53 | 6 | self->interpolation = EXPONENTIAL; | |
| 54 | 6 | self->base = json_object_get_double_member_with_default (object, "base", 1.0); | |
| 55 | |||
| 56 |
1/2✓ Branch 6 → 7 taken 6 times.
✗ Branch 6 → 53 not taken.
|
6 | if ((stops_node = json_object_get_member (object, "stops"))) |
| 57 | { | ||
| 58 | 6 | JsonArray *stops; | |
| 59 | |||
| 60 |
1/2✓ Branch 8 → 9 taken 6 times.
✗ Branch 8 → 52 not taken.
|
6 | if (!shumate_vector_json_get_array (stops_node, &stops, error)) |
| 61 | ✗ | return NULL; | |
| 62 | |||
| 63 |
2/2✓ Branch 50 → 11 taken 20 times.
✓ Branch 50 → 51 taken 6 times.
|
26 | for (int i = 0, n = json_array_get_length (stops); i < n; i ++) |
| 64 | { | ||
| 65 | 20 | JsonNode *stop_node = json_array_get_element (stops, i); | |
| 66 | 20 | JsonArray *stop_array; | |
| 67 | 20 | Stop *stop; | |
| 68 | 20 | JsonNode *point_node; | |
| 69 | 20 | JsonNode *value_node; | |
| 70 | 20 | g_auto(GValue) gvalue = G_VALUE_INIT; | |
| 71 | 20 | g_auto(ShumateVectorValue) value = SHUMATE_VECTOR_VALUE_INIT; | |
| 72 | |||
| 73 |
1/2✓ Branch 13 → 14 taken 20 times.
✗ Branch 13 → 47 not taken.
|
20 | if (!shumate_vector_json_get_array (stop_node, &stop_array, error)) |
| 74 | return NULL; | ||
| 75 | |||
| 76 |
1/2✗ Branch 15 → 16 not taken.
✓ Branch 15 → 18 taken 20 times.
|
20 | if (json_array_get_length (stop_array) != 2) |
| 77 | { | ||
| 78 | ✗ | g_set_error (error, | |
| 79 | SHUMATE_STYLE_ERROR, | ||
| 80 | SHUMATE_STYLE_ERROR_INVALID_EXPRESSION, | ||
| 81 | "Expected element of \"stops\" to have exactly 2 elements"); | ||
| 82 | ✗ | return NULL; | |
| 83 | } | ||
| 84 | |||
| 85 | 20 | point_node = json_array_get_element (stop_array, 0); | |
| 86 | 20 | value_node = json_array_get_element (stop_array, 1); | |
| 87 | |||
| 88 |
1/2✓ Branch 21 → 22 taken 20 times.
✗ Branch 21 → 25 not taken.
|
20 | if (!JSON_NODE_HOLDS_VALUE (point_node) |
| 89 |
1/2✗ Branch 24 → 25 not taken.
✓ Branch 24 → 28 taken 20 times.
|
20 | || !g_value_type_transformable (json_node_get_value_type (point_node), G_TYPE_DOUBLE)) |
| 90 | { | ||
| 91 | ✗ | g_set_error (error, | |
| 92 | SHUMATE_STYLE_ERROR, | ||
| 93 | SHUMATE_STYLE_ERROR_INVALID_EXPRESSION, | ||
| 94 | "Expected element 1 of \"stops\" to be a number"); | ||
| 95 | ✗ | return NULL; | |
| 96 | } | ||
| 97 | |||
| 98 |
1/2✗ Branch 29 → 30 not taken.
✓ Branch 29 → 33 taken 20 times.
|
20 | if (!JSON_NODE_HOLDS_VALUE (value_node)) |
| 99 | { | ||
| 100 | ✗ | g_set_error (error, | |
| 101 | SHUMATE_STYLE_ERROR, | ||
| 102 | SHUMATE_STYLE_ERROR_INVALID_EXPRESSION, | ||
| 103 | "Expected element 2 of \"stops\" to be a literal value"); | ||
| 104 | ✗ | return NULL; | |
| 105 | } | ||
| 106 | |||
| 107 | 20 | json_node_get_value (value_node, &gvalue); | |
| 108 | |||
| 109 |
1/2✗ Branch 35 → 36 not taken.
✓ Branch 35 → 39 taken 20 times.
|
20 | if (!shumate_vector_value_set_from_g_value (&value, &gvalue)) |
| 110 | { | ||
| 111 | ✗ | g_set_error (error, | |
| 112 | SHUMATE_STYLE_ERROR, | ||
| 113 | SHUMATE_STYLE_ERROR_INVALID_EXPRESSION, | ||
| 114 | "Could not parse literal value"); | ||
| 115 | ✗ | return NULL; | |
| 116 | } | ||
| 117 | |||
| 118 | 20 | stop = g_new0 (Stop, 1); | |
| 119 | 20 | stop->point = json_node_get_double (point_node); | |
| 120 | 20 | stop->expr = shumate_vector_expression_filter_from_literal (&value); | |
| 121 | |||
| 122 | 20 | g_ptr_array_add (self->stops, stop); | |
| 123 | } | ||
| 124 | } | ||
| 125 | |||
| 126 | return (ShumateVectorExpression *)g_steal_pointer (&self); | ||
| 127 | } | ||
| 128 | |||
| 129 | static gboolean | ||
| 130 | 12 | add_stops (ShumateVectorExpressionInterpolate *self, | |
| 131 | JsonArray *array, | ||
| 132 | int start, | ||
| 133 | ShumateVectorExpressionContext *ctx, | ||
| 134 | GError **error) | ||
| 135 | { | ||
| 136 | 12 | double prev_point; | |
| 137 | |||
| 138 | /* Stops */ | ||
| 139 |
2/2✓ Branch 24 → 4 taken 28 times.
✓ Branch 24 → 25 taken 12 times.
|
40 | for (int i = start, n = json_array_get_length (array); i < n; i += 2) |
| 140 | { | ||
| 141 | 28 | g_auto(ShumateVectorValue) value = SHUMATE_VECTOR_VALUE_INIT; | |
| 142 | 28 | g_autoptr(ShumateVectorExpression) expr = NULL; | |
| 143 | 28 | Stop *stop; | |
| 144 | 28 | double point; | |
| 145 | |||
| 146 |
1/2✓ Branch 6 → 7 taken 28 times.
✗ Branch 6 → 22 not taken.
|
28 | if (!shumate_vector_value_set_from_json_literal (&value, json_array_get_element (array, i), error)) |
| 147 | return FALSE; | ||
| 148 | |||
| 149 |
1/2✗ Branch 8 → 9 not taken.
✓ Branch 8 → 11 taken 28 times.
|
28 | if (!shumate_vector_value_get_number (&value, &point)) |
| 150 | { | ||
| 151 | ✗ | g_set_error (error, | |
| 152 | SHUMATE_STYLE_ERROR, | ||
| 153 | SHUMATE_STYLE_ERROR_INVALID_EXPRESSION, | ||
| 154 | "Expected stop input to be a number"); | ||
| 155 | ✗ | return FALSE; | |
| 156 | } | ||
| 157 | |||
| 158 |
3/4✓ Branch 11 → 12 taken 16 times.
✓ Branch 11 → 15 taken 12 times.
✗ Branch 12 → 13 not taken.
✓ Branch 12 → 15 taken 16 times.
|
28 | if (i > start && point <= prev_point) |
| 159 | { | ||
| 160 | ✗ | g_set_error (error, | |
| 161 | SHUMATE_STYLE_ERROR, | ||
| 162 | SHUMATE_STYLE_ERROR_INVALID_EXPRESSION, | ||
| 163 | "Stop inputs must be in strictly ascending order"); | ||
| 164 | ✗ | return FALSE; | |
| 165 | } | ||
| 166 | 28 | prev_point = point; | |
| 167 | |||
| 168 | 28 | expr = shumate_vector_expression_filter_from_array_or_literal ( | |
| 169 | 28 | json_array_get_element (array, i + 1), | |
| 170 | ctx, | ||
| 171 | error | ||
| 172 | ); | ||
| 173 |
1/2✓ Branch 17 → 18 taken 28 times.
✗ Branch 17 → 22 not taken.
|
28 | if (!expr) |
| 174 | return FALSE; | ||
| 175 | |||
| 176 | 28 | stop = g_new0 (Stop, 1); | |
| 177 | 28 | stop->point = point; | |
| 178 | 28 | stop->expr = g_steal_pointer (&expr); | |
| 179 | 28 | g_ptr_array_add (self->stops, stop); | |
| 180 | } | ||
| 181 | |||
| 182 | return TRUE; | ||
| 183 | } | ||
| 184 | |||
| 185 | ShumateVectorExpression * | ||
| 186 | 4 | shumate_vector_expression_interpolate_from_json_array (JsonArray *array, | |
| 187 | ShumateVectorExpressionContext *ctx, | ||
| 188 | GError **error) | ||
| 189 | { | ||
| 190 | 4 | g_autoptr(ShumateVectorExpressionInterpolate) self = g_object_new (SHUMATE_TYPE_VECTOR_EXPRESSION_INTERPOLATE, NULL); | |
| 191 | 4 | JsonArray *interpolation; | |
| 192 | 4 | const char *interpolation_type; | |
| 193 | |||
| 194 |
1/2✗ Branch 5 → 6 not taken.
✓ Branch 5 → 9 taken 4 times.
|
4 | if (json_array_get_length (array) < 5) |
| 195 | { | ||
| 196 | ✗ | g_set_error (error, | |
| 197 | SHUMATE_STYLE_ERROR, | ||
| 198 | SHUMATE_STYLE_ERROR_INVALID_EXPRESSION, | ||
| 199 | "Operator `interpolate` expected at least 4 arguments"); | ||
| 200 | ✗ | return NULL; | |
| 201 | } | ||
| 202 | |||
| 203 |
1/2✗ Branch 10 → 11 not taken.
✓ Branch 10 → 14 taken 4 times.
|
4 | if (json_array_get_length (array) % 2 == 0) |
| 204 | { | ||
| 205 | ✗ | g_set_error (error, | |
| 206 | SHUMATE_STYLE_ERROR, | ||
| 207 | SHUMATE_STYLE_ERROR_INVALID_EXPRESSION, | ||
| 208 | "Operator `interpolate` expected an even number of arguments"); | ||
| 209 | ✗ | return NULL; | |
| 210 | } | ||
| 211 | |||
| 212 | /* Interpolation type */ | ||
| 213 |
1/2✓ Branch 16 → 17 taken 4 times.
✗ Branch 16 → 59 not taken.
|
4 | if (!shumate_vector_json_get_array (json_array_get_element (array, 1), &interpolation, error)) |
| 214 | return NULL; | ||
| 215 | |||
| 216 |
1/2✗ Branch 18 → 19 not taken.
✓ Branch 18 → 22 taken 4 times.
|
4 | if (json_array_get_length (interpolation) == 0) |
| 217 | { | ||
| 218 | ✗ | g_set_error (error, | |
| 219 | SHUMATE_STYLE_ERROR, | ||
| 220 | SHUMATE_STYLE_ERROR_INVALID_EXPRESSION, | ||
| 221 | "Expected an interpolation type"); | ||
| 222 | ✗ | return NULL; | |
| 223 | } | ||
| 224 | |||
| 225 |
1/2✓ Branch 24 → 25 taken 4 times.
✗ Branch 24 → 59 not taken.
|
4 | if (!shumate_vector_json_get_string (json_array_get_element (interpolation, 0), &interpolation_type, error)) |
| 226 | return NULL; | ||
| 227 | |||
| 228 |
1/2✓ Branch 26 → 27 taken 4 times.
✗ Branch 26 → 33 not taken.
|
4 | if (g_strcmp0 ("linear", interpolation_type) == 0) |
| 229 | { | ||
| 230 |
1/2✗ Branch 28 → 29 not taken.
✓ Branch 28 → 32 taken 4 times.
|
4 | if (json_array_get_length (interpolation) != 1) |
| 231 | { | ||
| 232 | ✗ | g_set_error (error, | |
| 233 | SHUMATE_STYLE_ERROR, | ||
| 234 | SHUMATE_STYLE_ERROR_INVALID_EXPRESSION, | ||
| 235 | "Interpolation type `linear` expected 0 arguments"); | ||
| 236 | ✗ | return NULL; | |
| 237 | } | ||
| 238 | |||
| 239 | 4 | self->interpolation = LINEAR; | |
| 240 | } | ||
| 241 | ✗ | else if (g_strcmp0 ("exponential", interpolation_type) == 0) | |
| 242 | { | ||
| 243 | ✗ | g_auto(ShumateVectorValue) value = SHUMATE_VECTOR_VALUE_INIT; | |
| 244 | |||
| 245 | ✗ | if (json_array_get_length (interpolation) != 2) | |
| 246 | { | ||
| 247 | ✗ | g_set_error (error, | |
| 248 | SHUMATE_STYLE_ERROR, | ||
| 249 | SHUMATE_STYLE_ERROR_INVALID_EXPRESSION, | ||
| 250 | "Interpolation type `exponential` expected 1 argument"); | ||
| 251 | ✗ | return NULL; | |
| 252 | } | ||
| 253 | |||
| 254 | ✗ | if (!shumate_vector_value_set_from_json_literal (&value, json_array_get_element (interpolation, 1), error)) | |
| 255 | return NULL; | ||
| 256 | |||
| 257 | ✗ | self->interpolation = EXPONENTIAL; | |
| 258 | ✗ | if (!shumate_vector_value_get_number (&value, &self->base)) | |
| 259 | { | ||
| 260 | ✗ | g_set_error (error, | |
| 261 | SHUMATE_STYLE_ERROR, | ||
| 262 | SHUMATE_STYLE_ERROR_INVALID_EXPRESSION, | ||
| 263 | "Expected argument of `exponential` to be a number"); | ||
| 264 | ✗ | return NULL; | |
| 265 | } | ||
| 266 | } | ||
| 267 | else | ||
| 268 | { | ||
| 269 | ✗ | g_set_error (error, | |
| 270 | SHUMATE_STYLE_ERROR, | ||
| 271 | SHUMATE_STYLE_ERROR_INVALID_EXPRESSION, | ||
| 272 | "Unknown interpolation type `%s`", | ||
| 273 | interpolation_type); | ||
| 274 | } | ||
| 275 | |||
| 276 | /* Input */ | ||
| 277 | 4 | self->input = shumate_vector_expression_filter_from_array_or_literal ( | |
| 278 | json_array_get_element (array, 2), | ||
| 279 | ctx, | ||
| 280 | error | ||
| 281 | ); | ||
| 282 |
1/2✓ Branch 56 → 57 taken 4 times.
✗ Branch 56 → 60 not taken.
|
4 | if (!self->input) |
| 283 | return NULL; | ||
| 284 | |||
| 285 | /* Stops */ | ||
| 286 |
1/2✗ Branch 58 → 59 not taken.
✓ Branch 58 → 61 taken 4 times.
|
4 | if (!add_stops (self, array, 3, ctx, error)) |
| 287 | return NULL; | ||
| 288 | |||
| 289 | return (ShumateVectorExpression *)g_steal_pointer (&self); | ||
| 290 | } | ||
| 291 | |||
| 292 | ShumateVectorExpression * | ||
| 293 | 8 | shumate_vector_expression_step_from_json_array (JsonArray *array, | |
| 294 | ShumateVectorExpressionContext *ctx, | ||
| 295 | GError **error) | ||
| 296 | { | ||
| 297 | 8 | g_autoptr(ShumateVectorExpressionInterpolate) self = g_object_new (SHUMATE_TYPE_VECTOR_EXPRESSION_INTERPOLATE, NULL); | |
| 298 | 8 | Stop *stop; | |
| 299 | |||
| 300 | 8 | self->interpolation = STEP; | |
| 301 | |||
| 302 |
1/2✗ Branch 5 → 6 not taken.
✓ Branch 5 → 9 taken 8 times.
|
8 | if (json_array_get_length (array) < 5) |
| 303 | { | ||
| 304 | ✗ | g_set_error (error, | |
| 305 | SHUMATE_STYLE_ERROR, | ||
| 306 | SHUMATE_STYLE_ERROR_INVALID_EXPRESSION, | ||
| 307 | "Operator `interpolate` expected at least 4 arguments"); | ||
| 308 | ✗ | return NULL; | |
| 309 | } | ||
| 310 | |||
| 311 |
1/2✗ Branch 10 → 11 not taken.
✓ Branch 10 → 14 taken 8 times.
|
8 | if (json_array_get_length (array) % 2 == 0) |
| 312 | { | ||
| 313 | ✗ | g_set_error (error, | |
| 314 | SHUMATE_STYLE_ERROR, | ||
| 315 | SHUMATE_STYLE_ERROR_INVALID_EXPRESSION, | ||
| 316 | "Operator `interpolate` expected an even number of arguments"); | ||
| 317 | ✗ | return NULL; | |
| 318 | } | ||
| 319 | |||
| 320 | /* Input */ | ||
| 321 | 8 | self->input = shumate_vector_expression_filter_from_array_or_literal ( | |
| 322 | json_array_get_element (array, 1), | ||
| 323 | ctx, | ||
| 324 | error | ||
| 325 | ); | ||
| 326 |
1/2✓ Branch 16 → 17 taken 8 times.
✗ Branch 16 → 24 not taken.
|
8 | if (!self->input) |
| 327 | return NULL; | ||
| 328 | |||
| 329 | /* First stop */ | ||
| 330 | 8 | stop = g_new0 (Stop, 1); | |
| 331 | 8 | g_ptr_array_add (self->stops, stop); | |
| 332 | 8 | stop->point = -G_MAXDOUBLE; | |
| 333 | 8 | stop->expr = shumate_vector_expression_filter_from_array_or_literal ( | |
| 334 | json_array_get_element (array, 2), | ||
| 335 | ctx, | ||
| 336 | error | ||
| 337 | ); | ||
| 338 |
1/2✓ Branch 21 → 22 taken 8 times.
✗ Branch 21 → 24 not taken.
|
8 | if (!stop->expr) |
| 339 | return NULL; | ||
| 340 | |||
| 341 | /* Stops */ | ||
| 342 |
1/2✗ Branch 23 → 24 not taken.
✓ Branch 23 → 25 taken 8 times.
|
8 | if (!add_stops (self, array, 3, ctx, error)) |
| 343 | return NULL; | ||
| 344 | |||
| 345 | return (ShumateVectorExpression *)g_steal_pointer (&self); | ||
| 346 | } | ||
| 347 | |||
| 348 | |||
| 349 | static void | ||
| 350 | 56 | stop_free (Stop *stop) | |
| 351 | { | ||
| 352 |
1/2✓ Branch 2 → 3 taken 56 times.
✗ Branch 2 → 4 not taken.
|
56 | g_clear_object (&stop->expr); |
| 353 |
1/2✗ Branch 4 → 5 not taken.
✓ Branch 4 → 6 taken 56 times.
|
56 | g_free (stop); |
| 354 | 56 | } | |
| 355 | |||
| 356 | |||
| 357 | static double | ||
| 358 | 28 | lerp_double (double a, double b, double pos) | |
| 359 | { | ||
| 360 | 28 | return (b - a) * pos + a; | |
| 361 | } | ||
| 362 | |||
| 363 | |||
| 364 | static void | ||
| 365 | 28 | lerp (ShumateVectorValue *last_value, ShumateVectorValue *next_value, double pos, ShumateVectorValue *out) | |
| 366 | { | ||
| 367 | 28 | gdouble last_double, next_double; | |
| 368 | 28 | GdkRGBA last_color, next_color; | |
| 369 | |||
| 370 |
2/2✓ Branch 3 → 4 taken 20 times.
✓ Branch 3 → 7 taken 8 times.
|
28 | if (shumate_vector_value_get_number (last_value, &last_double) |
| 371 |
1/2✓ Branch 5 → 6 taken 20 times.
✗ Branch 5 → 7 not taken.
|
20 | && shumate_vector_value_get_number (next_value, &next_double)) |
| 372 | 20 | shumate_vector_value_set_number (out, lerp_double (last_double, next_double, pos)); | |
| 373 |
1/2✓ Branch 8 → 9 taken 8 times.
✗ Branch 8 → 13 not taken.
|
8 | else if (shumate_vector_value_get_color (last_value, &last_color) |
| 374 |
1/2✓ Branch 10 → 11 taken 8 times.
✗ Branch 10 → 13 not taken.
|
8 | && shumate_vector_value_get_color (next_value, &next_color)) |
| 375 | 8 | { | |
| 376 | 8 | GdkRGBA color = { | |
| 377 | 8 | .red = lerp_double (last_color.red, next_color.red, pos), | |
| 378 | 8 | .green = lerp_double (last_color.green, next_color.green, pos), | |
| 379 | 8 | .blue = lerp_double (last_color.blue, next_color.blue, pos), | |
| 380 | 8 | .alpha = lerp_double (last_color.alpha, next_color.alpha, pos), | |
| 381 | }; | ||
| 382 | 8 | shumate_vector_value_set_color (out, &color); | |
| 383 | } | ||
| 384 | else | ||
| 385 | ✗ | shumate_vector_value_steal (last_value, out); | |
| 386 | 28 | } | |
| 387 | |||
| 388 | |||
| 389 | static void | ||
| 390 | ✗ | exp_interp (double last_point, | |
| 391 | double next_point, | ||
| 392 | ShumateVectorValue *last_value, | ||
| 393 | ShumateVectorValue *next_value, | ||
| 394 | double input, | ||
| 395 | double base, | ||
| 396 | ShumateVectorValue *dest) | ||
| 397 | { | ||
| 398 | ✗ | double diff = next_point - last_point; | |
| 399 | ✗ | double pos = input - last_point; | |
| 400 | |||
| 401 | ✗ | lerp (last_value, next_value, (pow (base, pos) - 1.0) / (pow (base, diff) - 1.0), dest); | |
| 402 | ✗ | } | |
| 403 | |||
| 404 | |||
| 405 | static void | ||
| 406 | 18 | shumate_vector_expression_interpolate_finalize (GObject *object) | |
| 407 | { | ||
| 408 | 18 | ShumateVectorExpressionInterpolate *self = (ShumateVectorExpressionInterpolate *)object; | |
| 409 | |||
| 410 |
2/2✓ Branch 2 → 3 taken 12 times.
✓ Branch 2 → 4 taken 6 times.
|
18 | g_clear_object (&self->input); |
| 411 |
1/2✓ Branch 4 → 5 taken 18 times.
✗ Branch 4 → 6 not taken.
|
18 | g_clear_pointer (&self->stops, g_ptr_array_unref); |
| 412 | |||
| 413 | 18 | G_OBJECT_CLASS (shumate_vector_expression_interpolate_parent_class)->finalize (object); | |
| 414 | 18 | } | |
| 415 | |||
| 416 | |||
| 417 | static gboolean | ||
| 418 | 52 | shumate_vector_expression_interpolate_eval (ShumateVectorExpression *expr, | |
| 419 | ShumateVectorRenderScope *scope, | ||
| 420 | ShumateVectorValue *out) | ||
| 421 | { | ||
| 422 | 52 | ShumateVectorExpressionInterpolate *self = (ShumateVectorExpressionInterpolate *)expr; | |
| 423 | 104 | g_auto(ShumateVectorValue) value = SHUMATE_VECTOR_VALUE_INIT; | |
| 424 | 52 | double input; | |
| 425 | 52 | Stop **stops = (Stop **)self->stops->pdata; | |
| 426 | 52 | guint n_stops = self->stops->len; | |
| 427 | |||
| 428 |
1/2✓ Branch 2 → 3 taken 52 times.
✗ Branch 2 → 32 not taken.
|
52 | if (n_stops == 0) |
| 429 | return FALSE; | ||
| 430 | |||
| 431 |
2/2✓ Branch 3 → 4 taken 30 times.
✓ Branch 3 → 8 taken 22 times.
|
52 | if (self->input != NULL) |
| 432 | { | ||
| 433 |
1/2✓ Branch 5 → 6 taken 30 times.
✗ Branch 5 → 32 not taken.
|
30 | if (!shumate_vector_expression_eval (self->input, scope, &value)) |
| 434 | return FALSE; | ||
| 435 |
1/2✓ Branch 7 → 9 taken 30 times.
✗ Branch 7 → 32 not taken.
|
30 | if (!shumate_vector_value_get_number (&value, &input)) |
| 436 | return FALSE; | ||
| 437 | } | ||
| 438 | else | ||
| 439 | 22 | input = scope->zoom_level; | |
| 440 | |||
| 441 |
2/2✓ Branch 9 → 10 taken 4 times.
✓ Branch 9 → 30 taken 48 times.
|
52 | if (input < stops[0]->point) |
| 442 | 4 | return shumate_vector_expression_eval (stops[0]->expr, scope, out); | |
| 443 | |||
| 444 |
2/2✓ Branch 30 → 11 taken 90 times.
✓ Branch 30 → 31 taken 16 times.
|
106 | for (int i = 1; i < n_stops; i ++) |
| 445 | { | ||
| 446 | 90 | Stop *last = stops[i - 1]; | |
| 447 | 90 | Stop *next = stops[i]; | |
| 448 |
3/4✓ Branch 11 → 12 taken 90 times.
✗ Branch 11 → 29 not taken.
✓ Branch 12 → 13 taken 32 times.
✓ Branch 12 → 29 taken 58 times.
|
90 | if (last->point <= input && input < next->point) |
| 449 | { | ||
| 450 | 32 | double pos_norm = (input - last->point) / (next->point - last->point); | |
| 451 | 32 | g_auto(ShumateVectorValue) last_value = SHUMATE_VECTOR_VALUE_INIT; | |
| 452 | 32 | g_auto(ShumateVectorValue) next_value = SHUMATE_VECTOR_VALUE_INIT; | |
| 453 | |||
| 454 |
1/2✓ Branch 14 → 15 taken 32 times.
✗ Branch 14 → 26 not taken.
|
32 | if (!shumate_vector_expression_eval (last->expr, scope, &last_value)) |
| 455 | return FALSE; | ||
| 456 |
1/2✓ Branch 16 → 17 taken 32 times.
✗ Branch 16 → 26 not taken.
|
32 | if (!shumate_vector_expression_eval (next->expr, scope, &next_value)) |
| 457 | return FALSE; | ||
| 458 | |||
| 459 |
3/4✓ Branch 17 → 18 taken 4 times.
✓ Branch 17 → 19 taken 14 times.
✓ Branch 17 → 21 taken 14 times.
✗ Branch 17 → 24 not taken.
|
32 | switch (self->interpolation) |
| 460 | { | ||
| 461 | 4 | case STEP: | |
| 462 | 4 | shumate_vector_value_steal (&last_value, out); | |
| 463 | 4 | return TRUE; | |
| 464 | 14 | case LINEAR: | |
| 465 | 14 | lerp (&last_value, &next_value, pos_norm, out); | |
| 466 | 14 | return TRUE; | |
| 467 | 14 | case EXPONENTIAL: | |
| 468 |
1/2✓ Branch 21 → 22 taken 14 times.
✗ Branch 21 → 23 not taken.
|
14 | if (self->base == 1.0) |
| 469 | 14 | lerp (&last_value, &next_value, pos_norm, out); | |
| 470 | else | ||
| 471 | ✗ | exp_interp (last->point, next->point, &last_value, &next_value, input, self->base, out); | |
| 472 | return TRUE; | ||
| 473 | ✗ | default: | |
| 474 | 32 | g_assert_not_reached (); | |
| 475 | } | ||
| 476 | } | ||
| 477 | } | ||
| 478 | |||
| 479 | 16 | return shumate_vector_expression_eval (stops[n_stops - 1]->expr, scope, out); | |
| 480 | } | ||
| 481 | |||
| 482 | |||
| 483 | static void | ||
| 484 | 2 | shumate_vector_expression_interpolate_class_init (ShumateVectorExpressionInterpolateClass *klass) | |
| 485 | { | ||
| 486 | 2 | GObjectClass *object_class = G_OBJECT_CLASS (klass); | |
| 487 | 2 | ShumateVectorExpressionClass *expr_class = SHUMATE_VECTOR_EXPRESSION_CLASS (klass); | |
| 488 | |||
| 489 | 2 | object_class->finalize = shumate_vector_expression_interpolate_finalize; | |
| 490 | 2 | expr_class->eval = shumate_vector_expression_interpolate_eval; | |
| 491 | } | ||
| 492 | |||
| 493 | static void | ||
| 494 | 18 | shumate_vector_expression_interpolate_init (ShumateVectorExpressionInterpolate *self) | |
| 495 | { | ||
| 496 | 18 | self->stops = g_ptr_array_new_with_free_func ((GDestroyNotify) stop_free); | |
| 497 | 18 | } | |
| 498 |