scena 1.7.2

A Rust-native scene-graph renderer with typed scene state, glTF assets, and explicit prepare/render lifecycles.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
use serde_json::Value;

use super::super::diagnostic;
use super::{validate_known_fields, validate_target};
use crate::scene::recipe::SceneRecipeDiagnosticV1;

const QUALITY_FIELDS: &[&str] = &[
    "profile",
    "exposure",
    "contrast",
    "noise",
    "text",
    "line",
    "geometry",
    "reflection",
    "area_light",
    "grounding",
    "depth_of_field",
];
const QUALITY_EXPOSURE_FIELDS: &[&str] = &["max_low_clip_fraction", "max_high_clip_fraction"];
const QUALITY_CONTRAST_FIELDS: &[&str] = &["min_luminance_range", "min_sobel_energy"];
const QUALITY_NOISE_FIELDS: &[&str] = &["max_outlier_fraction"];
const QUALITY_TEXT_FIELDS: &[&str] = &[
    "min_ink_coverage",
    "max_ink_isolation",
    "min_intermediate_edge_fraction",
    "max_background_luminance_range",
    "max_background_mean_delta",
];
const QUALITY_LINE_FIELDS: &[&str] = &["min_intermediate_edge_fraction", "max_straightness_error"];
const QUALITY_GEOMETRY_FIELDS: &[&str] = &["min_intermediate_edge_fraction"];
const QUALITY_REFLECTION_FIELDS: &[&str] = &[
    "target",
    "min_luminance_range",
    "min_sobel_energy",
    "min_chroma_range",
    "max_firefly_fraction",
    "min_bright_fraction",
    "min_dark_fraction",
];
const QUALITY_AREA_LIGHT_FIELDS: &[&str] = &[
    "target",
    "min_shadow_contrast",
    "min_penumbra_width_px",
    "min_penumbra_luma_levels",
    "min_emitter_extent_meters",
];
const QUALITY_GROUNDING_FIELDS: &[&str] = &["target", "min_contact_shadow_delta"];
const QUALITY_DEPTH_OF_FIELD_FIELDS: &[&str] = &[
    "target",
    "background_target",
    "min_source_background_sobel",
    "min_background_sobel_drop",
    "min_background_sobel_drop_fraction",
    "max_focal_mean_delta",
];
const QUALITY_REFLECTION_THRESHOLD_FIELDS: &[&str] = &[
    "min_luminance_range",
    "min_sobel_energy",
    "min_chroma_range",
    "max_firefly_fraction",
    "min_bright_fraction",
    "min_dark_fraction",
];
pub(super) const REFERENCE_FIELDS: &[&str] = &["id", "image", "metric", "mean_max", "min_ssim"];

pub(super) fn validate_quality(
    value: Option<&Value>,
    diagnostics: &mut Vec<SceneRecipeDiagnosticV1>,
) {
    let Some(value) = value else {
        return;
    };
    let Some(object) = value.as_object() else {
        diagnostics.push(diagnostic(
            "invalid_expect",
            "error",
            "$.expect.expect_quality",
            "expect_quality must be an object",
            "emit expect_quality:{profile,exposure,contrast,noise,text,line,geometry,reflection,area_light,grounding,depth_of_field}",
            None,
            false,
        ));
        return;
    };
    validate_known_fields(
        "$.expect.expect_quality",
        object.keys().map(String::as_str),
        QUALITY_FIELDS,
        diagnostics,
    );
    match object.get("profile").and_then(Value::as_str) {
        Some("product" | "documentation" | "cad" | "dashboard" | "twin") => {}
        _ => diagnostics.push(diagnostic(
            "invalid_expect",
            "error",
            "$.expect.expect_quality.profile",
            "quality profile must be one of product, documentation, cad, dashboard, twin",
            "choose the profile that matches the intended application",
            None,
            false,
        )),
    }
    validate_threshold_object(
        object.get("exposure"),
        "$.expect.expect_quality.exposure",
        QUALITY_EXPOSURE_FIELDS,
        diagnostics,
    );
    validate_threshold_object(
        object.get("contrast"),
        "$.expect.expect_quality.contrast",
        QUALITY_CONTRAST_FIELDS,
        diagnostics,
    );
    validate_threshold_object(
        object.get("noise"),
        "$.expect.expect_quality.noise",
        QUALITY_NOISE_FIELDS,
        diagnostics,
    );
    validate_threshold_object(
        object.get("text"),
        "$.expect.expect_quality.text",
        QUALITY_TEXT_FIELDS,
        diagnostics,
    );
    validate_threshold_object(
        object.get("line"),
        "$.expect.expect_quality.line",
        QUALITY_LINE_FIELDS,
        diagnostics,
    );
    validate_threshold_object(
        object.get("geometry"),
        "$.expect.expect_quality.geometry",
        QUALITY_GEOMETRY_FIELDS,
        diagnostics,
    );
    validate_reflection_quality(object.get("reflection"), diagnostics);
    validate_area_light_quality(object.get("area_light"), diagnostics);
    validate_grounding_quality(object.get("grounding"), diagnostics);
    validate_depth_of_field_quality(object.get("depth_of_field"), diagnostics);
}

pub(super) fn validate_reference_expectation(
    path: &str,
    object: &serde_json::Map<String, Value>,
    diagnostics: &mut Vec<SceneRecipeDiagnosticV1>,
) {
    match object.get("image").and_then(Value::as_str) {
        Some(value) if !value.trim().is_empty() => {}
        _ => diagnostics.push(diagnostic(
            "invalid_expect",
            "error",
            format!("{path}.image"),
            "reference image must be a non-empty path",
            "point at a committed PNG golden for this recipe",
            None,
            false,
        )),
    }
    match object.get("metric").and_then(Value::as_str) {
        Some("rgba_abs_diff" | "delta_e2000" | "ssim") => {}
        _ => diagnostics.push(diagnostic(
            "invalid_expect",
            "error",
            format!("{path}.metric"),
            "reference metric must be rgba_abs_diff, delta_e2000, or ssim",
            "choose ssim for structure, delta_e2000 for color, or rgba_abs_diff for strict RGBA",
            None,
            false,
        )),
    }
    for field in ["mean_max", "min_ssim"] {
        if let Some(value) = object.get(field) {
            match value.as_f64() {
                Some(number) if number.is_finite() && number >= 0.0 => {}
                _ => diagnostics.push(diagnostic(
                    "invalid_expect",
                    "error",
                    format!("{path}.{field}"),
                    format!("{field} must be a finite non-negative number"),
                    "use a threshold appropriate for the selected reference metric",
                    None,
                    false,
                )),
            }
        }
    }
}

fn validate_threshold_object(
    value: Option<&Value>,
    path: &str,
    fields: &[&str],
    diagnostics: &mut Vec<SceneRecipeDiagnosticV1>,
) {
    let Some(value) = value else {
        return;
    };
    let Some(object) = value.as_object() else {
        diagnostics.push(diagnostic(
            "invalid_expect",
            "error",
            path,
            "quality threshold block must be an object",
            "emit finite normalized thresholds between 0 and 1",
            None,
            false,
        ));
        return;
    };
    validate_known_fields(path, object.keys().map(String::as_str), fields, diagnostics);
    for field in fields {
        if let Some(value) = object.get(*field) {
            match value.as_f64() {
                Some(number) if number.is_finite() && (0.0..=1.0).contains(&number) => {}
                _ => diagnostics.push(diagnostic(
                    "invalid_expect",
                    "error",
                    format!("{path}.{field}"),
                    format!("{field} must be finite and between 0 and 1"),
                    "use a normalized quality threshold",
                    None,
                    false,
                )),
            }
        }
    }
}

fn validate_reflection_quality(
    value: Option<&Value>,
    diagnostics: &mut Vec<SceneRecipeDiagnosticV1>,
) {
    let Some(value) = value else {
        return;
    };
    let Some(object) = value.as_object() else {
        diagnostics.push(diagnostic(
            "invalid_expect",
            "error",
            "$.expect.expect_quality.reflection",
            "quality threshold block must be an object",
            "emit finite normalized thresholds between 0 and 1",
            None,
            false,
        ));
        return;
    };
    validate_known_fields(
        "$.expect.expect_quality.reflection",
        object.keys().map(String::as_str),
        QUALITY_REFLECTION_FIELDS,
        diagnostics,
    );
    if object.contains_key("target") {
        validate_target(
            "$.expect.expect_quality.reflection.target",
            object.get("target"),
            false,
            diagnostics,
        );
    }
    for field in QUALITY_REFLECTION_THRESHOLD_FIELDS {
        if let Some(value) = object.get(*field) {
            match value.as_f64() {
                Some(number) if number.is_finite() && (0.0..=1.0).contains(&number) => {}
                _ => diagnostics.push(diagnostic(
                    "invalid_expect",
                    "error",
                    format!("$.expect.expect_quality.reflection.{field}"),
                    format!("{field} must be finite and between 0 and 1"),
                    "use a normalized quality threshold",
                    None,
                    false,
                )),
            }
        }
    }
}

fn validate_area_light_quality(
    value: Option<&Value>,
    diagnostics: &mut Vec<SceneRecipeDiagnosticV1>,
) {
    let Some(value) = value else {
        return;
    };
    let Some(object) = value.as_object() else {
        diagnostics.push(diagnostic(
            "invalid_expect",
            "error",
            "$.expect.expect_quality.area_light",
            "area-light quality threshold block must be an object",
            "emit area_light:{target?,min_shadow_contrast?,min_penumbra_width_px?,min_penumbra_luma_levels?}",
            None,
            false,
        ));
        return;
    };
    validate_known_fields(
        "$.expect.expect_quality.area_light",
        object.keys().map(String::as_str),
        QUALITY_AREA_LIGHT_FIELDS,
        diagnostics,
    );
    if object.contains_key("target") {
        validate_target(
            "$.expect.expect_quality.area_light.target",
            object.get("target"),
            false,
            diagnostics,
        );
    }
    validate_unit_threshold(
        "$.expect.expect_quality.area_light.min_shadow_contrast",
        object.get("min_shadow_contrast"),
        diagnostics,
    );
    validate_non_negative_threshold(
        "$.expect.expect_quality.area_light.min_penumbra_width_px",
        object.get("min_penumbra_width_px"),
        diagnostics,
    );
    validate_non_negative_threshold(
        "$.expect.expect_quality.area_light.min_penumbra_luma_levels",
        object.get("min_penumbra_luma_levels"),
        diagnostics,
    );
    validate_non_negative_threshold(
        "$.expect.expect_quality.area_light.min_emitter_extent_meters",
        object.get("min_emitter_extent_meters"),
        diagnostics,
    );
}

fn validate_grounding_quality(
    value: Option<&Value>,
    diagnostics: &mut Vec<SceneRecipeDiagnosticV1>,
) {
    let Some(value) = value else {
        return;
    };
    let Some(object) = value.as_object() else {
        diagnostics.push(diagnostic(
            "invalid_expect",
            "error",
            "$.expect.expect_quality.grounding",
            "grounding quality expectation must be an object",
            "emit grounding:{target?,min_contact_shadow_delta?}",
            None,
            false,
        ));
        return;
    };
    validate_known_fields(
        "$.expect.expect_quality.grounding",
        object.keys().map(String::as_str),
        QUALITY_GROUNDING_FIELDS,
        diagnostics,
    );
    if object.contains_key("target") {
        validate_target(
            "$.expect.expect_quality.grounding.target",
            object.get("target"),
            false,
            diagnostics,
        );
    }
    validate_unit_threshold(
        "$.expect.expect_quality.grounding.min_contact_shadow_delta",
        object.get("min_contact_shadow_delta"),
        diagnostics,
    );
}

fn validate_depth_of_field_quality(
    value: Option<&Value>,
    diagnostics: &mut Vec<SceneRecipeDiagnosticV1>,
) {
    let Some(value) = value else {
        return;
    };
    let Some(object) = value.as_object() else {
        diagnostics.push(diagnostic(
            "invalid_expect",
            "error",
            "$.expect.expect_quality.depth_of_field",
            "depth-of-field quality expectation must be an object",
            "emit depth_of_field:{target?,background_target?,min_background_sobel_drop?,max_focal_mean_delta?}",
            None,
            false,
        ));
        return;
    };
    validate_known_fields(
        "$.expect.expect_quality.depth_of_field",
        object.keys().map(String::as_str),
        QUALITY_DEPTH_OF_FIELD_FIELDS,
        diagnostics,
    );
    if object.contains_key("target") {
        validate_target(
            "$.expect.expect_quality.depth_of_field.target",
            object.get("target"),
            false,
            diagnostics,
        );
    }
    if object.contains_key("background_target") {
        validate_target(
            "$.expect.expect_quality.depth_of_field.background_target",
            object.get("background_target"),
            false,
            diagnostics,
        );
    }
    validate_unit_threshold(
        "$.expect.expect_quality.depth_of_field.min_background_sobel_drop_fraction",
        object.get("min_background_sobel_drop_fraction"),
        diagnostics,
    );
    validate_unit_threshold(
        "$.expect.expect_quality.depth_of_field.max_focal_mean_delta",
        object.get("max_focal_mean_delta"),
        diagnostics,
    );
    validate_non_negative_threshold(
        "$.expect.expect_quality.depth_of_field.min_source_background_sobel",
        object.get("min_source_background_sobel"),
        diagnostics,
    );
    validate_non_negative_threshold(
        "$.expect.expect_quality.depth_of_field.min_background_sobel_drop",
        object.get("min_background_sobel_drop"),
        diagnostics,
    );
}

fn validate_unit_threshold(
    path: &str,
    value: Option<&Value>,
    diagnostics: &mut Vec<SceneRecipeDiagnosticV1>,
) {
    let Some(value) = value else {
        return;
    };
    match value.as_f64() {
        Some(number) if number.is_finite() && (0.0..=1.0).contains(&number) => {}
        _ => diagnostics.push(diagnostic(
            "invalid_expect",
            "error",
            path,
            "threshold must be finite and between 0 and 1",
            "use a normalized threshold",
            None,
            false,
        )),
    }
}

fn validate_non_negative_threshold(
    path: &str,
    value: Option<&Value>,
    diagnostics: &mut Vec<SceneRecipeDiagnosticV1>,
) {
    let Some(value) = value else {
        return;
    };
    match value.as_f64() {
        Some(number) if number.is_finite() && number >= 0.0 => {}
        _ => diagnostics.push(diagnostic(
            "invalid_expect",
            "error",
            path,
            "threshold must be a finite non-negative number",
            "use a non-negative threshold appropriate for the measured quality metric",
            None,
            false,
        )),
    }
}