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
use std::collections::{BTreeMap, BTreeSet};

use serde_json::json;

use super::checks::{
    CompositionCheckExt, checked_check, error_check, observed_pairs, round3, skip_check,
};
use super::helpers::{
    draws_for_handle, expected_color_handles, projected_node_rect, visible_coverage_for_rect,
    visible_coverage_for_region,
};
use super::object_framing::object_framing_check;
use super::object_pixels::object_pixel_quality_check;
use super::object_textures::object_texture_result_check;
use crate::scene::recipe::SceneRecipeAutoExposureV1;
use crate::{
    CaptureRgba8, CaptureScreenRegion, Color, RenderQualityProfile, SceneCompositionCheckV1,
    SceneCompositionStatusV1, SceneInspectionReportV1, SceneRecipeBuildV1, SceneRecipeExpectV1,
    SceneRecipeV1,
};

pub(super) struct ObjectCompositionInput<'a> {
    pub(super) recipe: &'a SceneRecipeV1,
    pub(super) manifest: &'a SceneRecipeBuildV1,
    pub(super) capture: &'a CaptureRgba8,
    pub(super) inspection: &'a SceneInspectionReportV1,
    pub(super) expect: Option<&'a SceneRecipeExpectV1>,
    pub(super) label_regions: &'a BTreeMap<u64, CaptureScreenRegion>,
    pub(super) line_regions: &'a BTreeMap<u64, CaptureScreenRegion>,
    pub(super) background: Color,
}

pub(super) fn composition_object_checks(
    input: ObjectCompositionInput<'_>,
) -> Vec<SceneCompositionCheckV1> {
    let explicit_color_handles = expected_color_handles(input.expect, input.manifest);
    let required_profile = required_object_profile(input.recipe, input.expect);
    let require_object_masks = required_profile.is_some();
    let mut checks = Vec::new();

    for target in &input.manifest.nodes {
        if !matches!(
            target.kind.as_str(),
            "node" | "instance_set" | "particle_set" | "label"
        ) {
            continue;
        }
        let target_path = format!("node.{}", target.id);
        let Some(node) = input.inspection.node_by_handle(target.handle) else {
            checks.push(error_check(
                format!("{target_path}.presence"),
                "completeness",
                "declared_node_missing",
                Some(target.id.clone()),
                vec![target.handle],
                BTreeMap::new(),
                (
                    "declared recipe node is absent from the inspection report",
                    "rebuild the recipe or remove the stale manifest target",
                ),
            ));
            continue;
        };

        let draws = draws_for_handle(input.inspection, target.handle);
        let label_region = (target.kind == "label")
            .then(|| input.label_regions.get(&target.handle).copied())
            .flatten();
        let line_region = input.line_regions.get(&target.handle).copied();
        let has_draw = !draws.is_empty()
            || (target.kind == "label" && input.label_regions.contains_key(&target.handle));
        let projected_rect = projected_node_rect(input.capture, draws.as_slice());
        if node.visible && (has_draw || line_region.is_some()) {
            checks.push(checked_check(
                format!("{target_path}.presence"),
                "completeness",
                "node_visible",
                Some(target.id.clone()),
                vec![target.handle],
                observed_pairs([
                    ("visible", json!(true)),
                    ("draw_count", json!(draws.len())),
                    ("kind", json!(target.kind)),
                ]),
                (
                    "declared node is visible and contributes owned output",
                    "no action needed",
                ),
            ));
        } else {
            checks.push(error_check(
                format!("{target_path}.presence"),
                "completeness",
                "declared_node_not_drawn",
                Some(target.id.clone()),
                vec![target.handle],
                observed_pairs([
                    ("visible", json!(node.visible)),
                    ("draw_count", json!(draws.len())),
                    ("kind", json!(target.kind)),
                ]),
                (
                    "declared recipe node did not produce visible draw output",
                    "make the node visible, move it into frame, or remove it from the declared recipe",
                ),
            ));
        }

        if let Some(rect) = projected_rect.filter(|rect| rect.width > 0.0 && rect.height > 0.0) {
            checks.push(
                checked_check(
                    format!("{target_path}.projected_bbox"),
                    "placement",
                    "projected_bbox_available",
                    Some(target.id.clone()),
                    vec![target.handle],
                    observed_pairs([
                        ("width_px", json!(round3(rect.width))),
                        ("height_px", json!(round3(rect.height))),
                        ("center_x_px", json!(round3(rect.center_x))),
                        ("center_y_px", json!(round3(rect.center_y))),
                    ]),
                    (
                        "declared node has a projected screen region",
                        "no action needed",
                    ),
                )
                .with_region("node", Some(target.handle), Some(rect)),
            );
            checks.push(checked_check(
                format!("{target_path}.size"),
                "framing",
                "projected_size_nonzero",
                Some(target.id.clone()),
                vec![target.handle],
                observed_pairs([
                    ("width_px", json!(round3(rect.width))),
                    ("height_px", json!(round3(rect.height))),
                ]),
                (
                    "declared node projects to a non-empty screen size",
                    "no action needed",
                ),
            ));
            if let Some(profile) = required_profile
                && matches!(
                    target.kind.as_str(),
                    "node" | "instance_set" | "particle_set"
                )
            {
                checks.push(object_framing_check(
                    &target_path,
                    &target.id,
                    target.handle,
                    input.capture,
                    rect,
                    profile,
                ));
            }
        } else if let Some(region) = label_region.or(line_region) {
            checks.push(
                checked_check(
                    format!("{target_path}.projected_bbox"),
                    "placement",
                    "projected_bbox_available",
                    Some(target.id.clone()),
                    vec![target.handle],
                    observed_pairs([
                        ("width_px", json!(region.width)),
                        ("height_px", json!(region.height)),
                        (
                            "center_x_px",
                            json!(round3(region.x as f32 + region.width as f32 * 0.5)),
                        ),
                        (
                            "center_y_px",
                            json!(round3(region.y as f32 + region.height as f32 * 0.5)),
                        ),
                    ]),
                    (
                        "declared overlay element has an exact projected screen region",
                        "no action needed",
                    ),
                )
                .with_region_from_screen("overlay", Some(target.handle), region),
            );
            checks.push(checked_check(
                format!("{target_path}.size"),
                "framing",
                "projected_size_nonzero",
                Some(target.id.clone()),
                vec![target.handle],
                observed_pairs([
                    ("width_px", json!(region.width)),
                    ("height_px", json!(region.height)),
                ]),
                (
                    "declared overlay element projects to a non-empty screen size",
                    "no action needed",
                ),
            ));
        } else if has_draw {
            checks.push(error_check(
                format!("{target_path}.projected_bbox"),
                "placement",
                "projected_bbox_missing",
                Some(target.id.clone()),
                vec![target.handle],
                BTreeMap::new(),
                (
                    "declared node draws but its projected screen bounds could not be computed",
                    "check camera metadata and node bounds",
                ),
            ));
        } else {
            checks.push(skip_check(
                format!("{target_path}.projected_bbox"),
                "placement",
                "projected_bbox_not_applicable",
                SceneCompositionStatusV1::NotApplicable,
                Some(target.id.clone()),
                vec![target.handle],
                (
                    "projected bounds are not applicable because the node did not draw",
                    "fix the presence failure first",
                ),
            ));
        }

        if explicit_color_handles.contains(&target.handle) {
            checks.push(checked_check(
                format!("{target_path}.expected_color"),
                "color_exposure",
                "expected_color_declared",
                Some(target.id.clone()),
                vec![target.handle],
                BTreeMap::new(),
                (
                    "recipe declares an expected color for this target; appearance verification owns the pixel assertion",
                    "inspect the appearance report if this color check fails",
                ),
            ));
        } else {
            let material_base_colors = draws
                .iter()
                .filter_map(|draw| draw.material.as_ref().map(|material| material.base_color))
                .collect::<Vec<_>>();
            if !material_base_colors.is_empty() {
                checks.push(checked_check(
                    format!("{target_path}.expected_color"),
                    "color_exposure",
                    "material_base_color_available",
                    Some(target.id.clone()),
                    vec![target.handle],
                    observed_pairs([("base_colors", json!(material_base_colors))]),
                    (
                        "declared node has material base-color intent in the inspected draw output",
                        "use expect_color when the rendered pixel result must match a swatch",
                    ),
                ));
            } else {
                checks.push(skip_check(
                    format!("{target_path}.expected_color"),
                    "color_exposure",
                    "expected_color_not_declared",
                    SceneCompositionStatusV1::SkippedNoDeclaredIntent,
                    Some(target.id.clone()),
                    vec![target.handle],
                    (
                        "recipe did not declare a color intent for this node",
                        "add expect_color for nodes whose color matters",
                    ),
                ));
            }
        }

        let visible_coverage = projected_rect
            .and_then(|rect| visible_coverage_for_rect(input.capture, rect, input.background))
            .or_else(|| {
                label_region.map(|region| {
                    visible_coverage_for_region(input.capture, region, input.background)
                })
            })
            .or_else(|| {
                line_region.map(|region| {
                    visible_coverage_for_region(input.capture, region, input.background)
                })
            });
        if let Some(coverage) = visible_coverage {
            if coverage.foreground_pixels > 0 {
                checks.push(
                    checked_check(
                        format!("{target_path}.visible_coverage"),
                        "occlusion_depth",
                        "visible_pixel_coverage_available",
                        Some(target.id.clone()),
                        vec![target.handle],
                        observed_pairs([
                            ("region_pixels", json!(coverage.region_pixels)),
                            ("foreground_pixels", json!(coverage.foreground_pixels)),
                            (
                                "foreground_fraction",
                                json!(round3(coverage.foreground_fraction)),
                            ),
                        ]),
                        (
                            "declared node has measured non-background pixels in its projected screen region",
                            "no action needed",
                        ),
                    )
                    .with_region_from_screen("node", Some(target.handle), coverage.region),
                );
                if let Some(profile) = required_profile {
                    checks.push(object_pixel_quality_check(
                        &target_path,
                        &target.id,
                        target.handle,
                        input.capture,
                        coverage.region,
                        input.background,
                        profile,
                    ));
                    if let Some(check) = object_texture_result_check(
                        &target_path,
                        &target.id,
                        target.handle,
                        input.capture,
                        coverage.region,
                        input.background,
                        draws.as_slice(),
                    ) {
                        checks.push(check);
                    }
                }
            } else {
                checks.push(
                    error_check(
                        format!("{target_path}.visible_coverage"),
                        "occlusion_depth",
                        "visible_pixel_coverage_missing",
                        Some(target.id.clone()),
                        vec![target.handle],
                        observed_pairs([
                            ("region_pixels", json!(coverage.region_pixels)),
                            ("foreground_pixels", json!(coverage.foreground_pixels)),
                            (
                                "foreground_fraction",
                                json!(round3(coverage.foreground_fraction)),
                            ),
                            ("profile", json!(required_profile.unwrap_or_default())),
                        ]),
                        (
                            "declared node projects into the captured frame but contains no measured non-background pixels",
                            "move it into frame, make it visible, change its material/background contrast, or remove the stale declaration",
                        ),
                    )
                    .with_region_from_screen("node", Some(target.handle), coverage.region),
                );
            }
        } else if has_draw || require_object_masks {
            checks.push(error_check(
                format!("{target_path}.visible_coverage"),
                "occlusion_depth",
                "visible_pixel_coverage_missing",
                Some(target.id.clone()),
                vec![target.handle],
                observed_pairs([("profile", json!(required_profile.unwrap_or_default()))]),
                (
                    "declared node has no viewport-clipped projected region for visible-pixel coverage",
                    "move it into the captured frame or remove the stale declaration",
                ),
            ));
        } else {
            checks.push(skip_check(
                format!("{target_path}.visible_coverage"),
                "occlusion_depth",
                "visible_coverage_not_applicable",
                SceneCompositionStatusV1::NotApplicable,
                Some(target.id.clone()),
                vec![target.handle],
                (
                    "visible coverage is not applicable because the node did not draw",
                    "fix the presence failure first",
                ),
            ));
        }
        checks.push(skip_check(
            format!("{target_path}.grounding"),
            "occlusion_depth",
            "grounding_intent_not_declared",
            SceneCompositionStatusV1::SkippedNoDeclaredIntent,
            Some(target.id.clone()),
            vec![target.handle],
            (
                "recipe did not declare that this node should be grounded",
                "add a grounding intent before expecting contact or ground-plane checks",
            ),
        ));
    }

    checks.extend(super::import_roots::composition_import_checks(
        &input,
        required_profile,
        require_object_masks,
    ));

    checks
}

pub(super) fn required_object_profile<'a>(
    recipe: &'a SceneRecipeV1,
    expect: Option<&'a SceneRecipeExpectV1>,
) -> Option<&'a str> {
    if let Some(profile) = expect.and_then(|expect| {
        expect
            .expect_quality
            .as_ref()
            .map(|quality| quality.profile.as_str())
    }) {
        return Some(profile);
    }
    if let Some(profile) = recipe
        .render
        .as_ref()
        .and_then(|render| render.profile.as_deref())
        && RenderQualityProfile::parse(profile).is_some()
    {
        return Some(profile);
    }
    let auto_exposure_preset = recipe
        .render
        .as_ref()
        .and_then(|render| render.auto_exposure.as_ref())
        .map(|auto_exposure| match auto_exposure {
            SceneRecipeAutoExposureV1::Preset(preset) => preset.as_str(),
            SceneRecipeAutoExposureV1::Config { preset, .. } => preset.as_str(),
        });
    if matches!(auto_exposure_preset, Some("product_studio")) {
        return Some("product");
    }
    None
}

pub(super) fn owned_draw_handles(
    manifest: &SceneRecipeBuildV1,
    label_regions: &BTreeMap<u64, CaptureScreenRegion>,
    overlay_handles: impl Iterator<Item = Option<u64>>,
) -> BTreeSet<u64> {
    let mut owned_handles = super::helpers::declared_draw_handles(manifest);
    owned_handles.extend(label_regions.keys().copied());
    owned_handles.extend(overlay_handles.flatten());
    owned_handles
}