hypergraph-rs 0.3.0

Hypergraphs for Rust: a port of XGI built on rustworkx-core, with a value on every membership
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
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
#![cfg(feature = "raster")]

use hypergraph_rs::raster::{Cell, CellGrid, Face, Rgb, SceneGraph3D, Strut, Vertex3};

#[test]
fn test_cell_glyph_floor_always_complete() {
    // Glyph floor: a Cell ALWAYS carries ch+fg+bg (no Option).
    let c = Cell {
        ch: '⠿',
        fg: Rgb(196, 58, 46),
        bg: Rgb(0, 0, 0),
    };
    assert_eq!(c.ch, '⠿');
    assert_eq!(c.fg, Rgb(196, 58, 46));
}

#[test]
fn test_cellgrid_row_major_len_and_text() {
    let g = CellGrid::filled(
        3,
        2,
        Cell {
            ch: ' ',
            fg: Rgb(0, 0, 0),
            bg: Rgb(0, 0, 0),
        },
    );
    assert_eq!(g.cells.len(), 6);
    assert_eq!(g.to_text(), "   \n   "); // 3 cols, 2 rows, glyphs only
}

#[test]
fn test_scenegraph3d_serde_round_trip() {
    let s = SceneGraph3D::empty();
    let j = serde_json::to_string(&s).unwrap();
    let back: SceneGraph3D = serde_json::from_str(&j).unwrap();
    assert_eq!(s, back);
}

#[test]
fn test_fmt6_matches_phase5_formatter() {
    use hypergraph_rs::raster::fmt::fmt6;
    assert_eq!(fmt6(0.4608359187088229), "0.460836");
    assert_eq!(fmt6(-0.0), "0");
    assert_eq!(fmt6(198.3), "198.3");
}

use hypergraph_rs::raster::{depth_sort, project, Camera};

fn cam() -> Camera {
    Camera {
        ry: 0.0,
        rx: 0.0,
        dist: 4.0,
        fov: 70.0,
    }
}

#[test]
fn test_project_center_is_center() {
    let p = project(
        &Vertex3 {
            x: 0.0,
            y: 0.0,
            z: 0.0,
        },
        &cam(),
        80,
        48,
    );
    // Origin projects to the screen midpoint (subcell space: cols*2 wide, rows*4 tall for braille).
    assert!((p.sx - (80.0 * 2.0 - 1.0) / 2.0).abs() < 1e-9);
    assert!((p.sy - (48.0 * 4.0 - 1.0) / 2.0).abs() < 1e-9);
}

#[test]
fn test_project_nearer_z_has_smaller_depth() {
    let near = project(
        &Vertex3 {
            x: 0.0,
            y: 0.0,
            z: 1.0,
        },
        &cam(),
        80,
        48,
    );
    let far = project(
        &Vertex3 {
            x: 0.0,
            y: 0.0,
            z: -1.0,
        },
        &cam(),
        80,
        48,
    );
    assert!(near.depth < far.depth);
}

#[test]
fn test_rotation_determinism_bit_identical() {
    let c = Camera {
        ry: 0.7,
        rx: 0.3,
        dist: 4.0,
        fov: 70.0,
    };
    let v = Vertex3 {
        x: 0.5,
        y: -0.2,
        z: 0.9,
    };
    assert_eq!(project(&v, &c, 80, 48).sx, project(&v, &c, 80, 48).sx);
}

#[test]
fn test_depth_sort_stable_on_ties() {
    // Two faces at identical depth keep insertion order (stable sort).
    let v0 = Vertex3 {
        x: -1.0,
        y: -1.0,
        z: 0.0,
    };
    let v1 = Vertex3 {
        x: 1.0,
        y: -1.0,
        z: 0.0,
    };
    let v2 = Vertex3 {
        x: 0.0,
        y: 1.0,
        z: 0.0,
    };
    let face_a = Face {
        verts: [v0, v1, v2],
        fill: Rgb(1, 0, 0),
        opacity: 1.0,
    };
    let face_b = Face {
        verts: [v0, v1, v2],
        fill: Rgb(2, 0, 0),
        opacity: 1.0,
    };
    let mut faces = vec![face_a.clone(), face_b.clone()];
    let mut struts: Vec<Strut> = Vec::new();
    depth_sort(&mut faces, &mut struts, &cam());
    assert_eq!(faces[0].fill, face_a.fill);
    assert_eq!(faces[1].fill, face_b.fill);
}

// --- Task 3: cell rasterizer -------------------------------------------

use hypergraph_rs::raster::rasterize;

#[test]
fn test_single_subdot_maps_to_correct_braille() {
    // cols=1,rows=1 => 2x4 subcell buffer (one terminal cell). Under `cam()`
    // (ry=0,rx=0,dist=4,fov=70) a vertex at (-2.0, 2.0, 0.0) projects to
    // sx≈-0.214 (clamps/rounds to subcell col0) and sy≈0.429 (rounds to
    // subcell row0) — see derivation in the task notes. A degenerate strut
    // (a==b) lights only that one subcell => bit 0x01 => glyph U+2801 '⠁'.
    let v = Vertex3 {
        x: -2.0,
        y: 2.0,
        z: 0.0,
    };
    let mut scene = SceneGraph3D::empty();
    scene.struts.push(Strut {
        a: v,
        b: v,
        color: Rgb(255, 255, 255),
    });
    let grid = rasterize(&scene, &cam(), 1, 1);
    assert_eq!(grid.cells.len(), 1);
    assert_eq!(grid.cells[0].ch, '\u{2801}');
    assert_eq!(grid.cells[0].fg, Rgb(255, 255, 255));
}

#[test]
fn test_bresenham_line_is_integer_deterministic() {
    let mut scene = SceneGraph3D::empty();
    scene.struts.push(Strut {
        a: Vertex3 {
            x: -1.0,
            y: -1.0,
            z: 0.0,
        },
        b: Vertex3 {
            x: 1.0,
            y: 1.0,
            z: 0.0,
        },
        color: Rgb(10, 20, 30),
    });
    let run1 = rasterize(&scene, &cam(), 20, 10).to_text();
    let run2 = rasterize(&scene, &cam(), 20, 10).to_text();
    assert_eq!(run1, run2);
    // The diagonal strut must have lit at least one subcell (non-blank grid).
    assert!(run1.chars().any(|c| c != '\u{2800}'));
}

#[test]
fn test_triangle_fill_top_left_rule_no_double_shade() {
    // Two triangles sharing an edge (the diagonal of a screen-filling quad)
    // together cover every subcell of a 1x1-cell (2x4 subcell) grid, under
    // `cam()`. If the top-left rule leaves a gap along the shared edge, the
    // resulting glyph will be missing bits from the full block U+28FF '⣿'.
    //
    // Corners are solved analytically (not decimal-approximated) from the
    // exact `project()` formula (camera.rs: `f = 1/tan(fov*pi/360)`,
    // `aspect = height/width`, `dist=4`, `z=dist` at world z=0) so the
    // projected subcell coords land within f64-rounding (~1e-15) of the
    // exact integer corners 0/1 (x) and 0/3 (y) — not ~1e-4 off, which
    // would spuriously push a corner sample outside the triangle.
    let f = 1.0 / (70.0_f64 * core::f64::consts::PI / 360.0).tan();
    let aspect = 4.0 / 2.0; // height/width for cols=1,rows=1 (h=4,w=2)
    let z = 4.0; // dist - world_z, world_z=0, dist=4
    let x_mag = 2.0 * z / (f * aspect); // ndc_x = +-1 => sx = 1/0
    let y_mag = 2.0 * z / f; // ndc_y = +-1 => sy = 0/3
    let tl = Vertex3 {
        x: -x_mag,
        y: y_mag,
        z: 0.0,
    };
    let tr = Vertex3 {
        x: x_mag,
        y: y_mag,
        z: 0.0,
    };
    let bl = Vertex3 {
        x: -x_mag,
        y: -y_mag,
        z: 0.0,
    };
    let br = Vertex3 {
        x: x_mag,
        y: -y_mag,
        z: 0.0,
    };
    let mut scene = SceneGraph3D::empty();
    scene.faces.push(Face {
        verts: [tl, tr, bl],
        fill: Rgb(200, 200, 200),
        opacity: 1.0,
    });
    scene.faces.push(Face {
        verts: [tr, br, bl],
        fill: Rgb(200, 200, 200),
        opacity: 1.0,
    });
    let grid = rasterize(&scene, &cam(), 1, 1);
    assert_eq!(
        grid.cells[0].ch, '\u{28FF}',
        "expected full block, got glyph missing subcells along the shared diagonal"
    );
}

#[test]
fn test_depth_buffer_occludes_farther_primitive() {
    // Two full-grid-covering triangles at different depths, same footprint;
    // the nearer (z=+1, smaller camera-space depth) must win every subcell
    // regardless of scene insertion order.
    let tl = Vertex3 {
        x: -14.004,
        y: 28.009,
        z: -10.0,
    };
    let tr = Vertex3 {
        x: 14.004,
        y: 28.009,
        z: -10.0,
    };
    let apex = Vertex3 {
        x: 0.0,
        y: -28.009,
        z: -10.0,
    };
    let far = Face {
        verts: [tl, tr, apex],
        fill: Rgb(0, 255, 0),
        opacity: 1.0,
    };
    let near = Face {
        verts: [
            Vertex3 { z: 1.0, ..tl },
            Vertex3 { z: 1.0, ..tr },
            Vertex3 { z: 1.0, ..apex },
        ],
        fill: Rgb(255, 0, 0),
        opacity: 1.0,
    };
    let mut scene = SceneGraph3D::empty();
    scene.faces.push(far.clone());
    scene.faces.push(near.clone());
    let grid = rasterize(&scene, &cam(), 1, 1);
    assert_eq!(grid.cells[0].fg, Rgb(255, 0, 0));

    // Insertion order reversed must give the same occlusion result.
    let mut scene2 = SceneGraph3D::empty();
    scene2.faces.push(near);
    scene2.faces.push(far);
    let grid2 = rasterize(&scene2, &cam(), 1, 1);
    assert_eq!(grid2.cells[0].fg, Rgb(255, 0, 0));
}

#[test]
fn test_frame_is_pure_function_byte_exact() {
    let mut scene = SceneGraph3D::empty();
    scene.struts.push(Strut {
        a: Vertex3 {
            x: -1.0,
            y: 0.5,
            z: 0.0,
        },
        b: Vertex3 {
            x: 1.0,
            y: -0.5,
            z: 0.0,
        },
        color: Rgb(5, 6, 7),
    });
    let c = cam();
    assert_eq!(
        rasterize(&scene, &c, 40, 20).to_text(),
        rasterize(&scene, &c, 40, 20).to_text()
    );
}

// ---------- Task 4: color under constraint ----------

use hypergraph_rs::raster::{heat, to_ansi256};

#[test]
fn test_heat_endpoints_match_deck_canon() {
    assert_eq!(heat(0.0), Rgb(217, 164, 65)); // gold
    assert_eq!(heat(0.5), Rgb(233, 223, 201)); // bone
    assert_eq!(heat(1.0), Rgb(196, 58, 46)); // crimson
}

#[test]
fn test_heat_clamps_out_of_range() {
    assert_eq!(heat(-1.0), heat(0.0));
    assert_eq!(heat(2.0), heat(1.0));
}

#[test]
fn test_ansi256_quantization_deterministic_and_pinned() {
    assert_eq!(to_ansi256(Rgb(196, 58, 46)), to_ansi256(Rgb(196, 58, 46)));
    // Pin the exact cube index for crimson (computed once, frozen as golden).
    assert_eq!(to_ansi256(Rgb(196, 58, 46)), 167);
    // Grayscale ramp: mid-gray maps into 232..=255.
    assert!((232..=255).contains(&to_ansi256(Rgb(128, 128, 128))));
}

/// A single-cell (1x1, 2x4 subcells), depth-decoupled-from-screen-position
/// camera rig: with `ry=rx=0.0, dist=1.0, fov=90.0` (`f=1`, `aspect=2`),
/// `project()` reduces to `ndc_x = v.x/D*2`, `ndc_y = v.y/D`, `depth=D` for
/// `v = (ndc_x*D/2, ndc_y*D, 1.0-D)` — letting a test pick screen subcell
/// AND depth independently.
fn subcell_cam() -> Camera {
    Camera {
        ry: 0.0,
        rx: 0.0,
        dist: 1.0,
        fov: 90.0,
    }
}

/// `ndc_x` for braille subcell column 0/1 in a 1-wide grid (`w=2`).
fn col_ndc(sub_col: usize) -> f64 {
    if sub_col == 0 {
        -1.0
    } else {
        1.0
    }
}

/// `ndc_y` for braille subcell row 0..3 in a 1-tall grid (`h=4`).
fn row_ndc(sub_row: usize) -> f64 {
    match sub_row {
        0 => 1.0,
        1 => 1.0 / 3.0,
        2 => -1.0 / 3.0,
        _ => -1.0,
    }
}

/// A degenerate (`a==b`) strut at exactly subcell `(sub_col,sub_row)` at
/// depth `d`, colored `color` — writes exactly one subcell.
fn subcell_strut(sub_col: usize, sub_row: usize, d: f64, color: Rgb) -> Strut {
    let ndc_x = col_ndc(sub_col);
    let ndc_y = row_ndc(sub_row);
    let v = Vertex3 {
        x: ndc_x * d / 2.0,
        y: ndc_y * d,
        z: 1.0 - d,
    };
    Strut { a: v, b: v, color }
}

#[test]
fn test_cell_fg_bg_pick_deterministic_tiebreak() {
    // Case A: clean majority (5 vs 3, same depth) -> fg = majority color.
    let a = Rgb(255, 0, 0);
    let b = Rgb(0, 0, 255);
    let mut scene = SceneGraph3D::empty();
    let majority_subcells = [(0, 0), (0, 1), (0, 2), (1, 0), (1, 1)]; // 5
    let minority_subcells = [(0, 3), (1, 2), (1, 3)]; // 3
    for &(c, r) in &majority_subcells {
        scene.struts.push(subcell_strut(c, r, 1.0, a));
    }
    for &(c, r) in &minority_subcells {
        scene.struts.push(subcell_strut(c, r, 1.0, b));
    }
    let grid = rasterize(&scene, &subcell_cam(), 1, 1);
    assert_eq!(grid.cells[0].fg, a);
    assert_eq!(grid.cells[0].bg, b);

    // Case B: 4-vs-4 count tie, broken by nearest depth (smaller depth wins).
    let mut scene = SceneGraph3D::empty();
    for r in 0..4usize {
        scene.struts.push(subcell_strut(0, r, 2.0, a)); // farther
        scene.struts.push(subcell_strut(1, r, 1.0, b)); // nearer
    }
    let grid = rasterize(&scene, &subcell_cam(), 1, 1);
    assert_eq!(grid.cells[0].fg, b);
    assert_eq!(grid.cells[0].bg, a);

    // Case C: 4-vs-4 count AND depth tie, broken by fixed scan order
    // ((sub_col,sub_row) in the order (0,0),(1,0),(0,1),(1,1),(0,2),(1,2),
    // (0,3),(1,3)) -> the color occupying the lowest scan index wins.
    let mut scene = SceneGraph3D::empty();
    for r in 0..4usize {
        scene.struts.push(subcell_strut(0, r, 1.0, a)); // scan indices 0,2,4,6
        scene.struts.push(subcell_strut(1, r, 1.0, b)); // scan indices 1,3,5,7
    }
    let grid = rasterize(&scene, &subcell_cam(), 1, 1);
    assert_eq!(grid.cells[0].fg, a);
    assert_eq!(grid.cells[0].bg, b);
}

// ---------- Task 12: Stage 2 — CellGrid text golden frames ----------
//
// Matrix: 3 instruments x 2 cameras (front, 3/4) x 1 size (80x40) x
// TrueColor glyph-floor text (`to_text()` is palette-independent — glyphs
// only — so palette is asserted separately via a small `to_ansi()` golden).
// `cells3d` is off by default, so every combo built from a `cells3d`-gated
// instrument builder (`raster::deck`/`raster::instruments`) is wrapped in a
// per-test `#[cfg(feature = "cells3d")]`; this file stays ungated at the
// crate level so Tasks 1-4's camera/grid/color tests keep compiling and
// running at default features (raster on, cells3d off).

#[cfg(feature = "cells3d")]
fn frame_front_camera() -> Camera {
    Camera {
        ry: 0.0,
        rx: 0.0,
        dist: 4.0,
        fov: 70.0,
    }
}

#[cfg(feature = "cells3d")]
fn frame_three_quarter_camera() -> Camera {
    Camera {
        ry: 0.6,
        rx: 0.3,
        dist: 4.0,
        fov: 70.0,
    }
}

#[cfg(feature = "cells3d")]
fn frame_scene(
    inst: hypergraph_rs::raster::instruments::Instrument,
) -> hypergraph_rs::raster::SceneGraph3D {
    use hypergraph_rs::raster::deck::build_world;
    use hypergraph_rs::raster::instruments::{build, InstrumentOpts};
    let world = build_world(1917);
    build(inst, &world, &InstrumentOpts::default())
}

#[cfg(feature = "cells3d")]
#[test]
fn test_frame_cylinder_front() {
    use hypergraph_rs::raster::instruments::Instrument;
    let scene = frame_scene(Instrument::Cylinder);
    assert_eq!(
        rasterize(&scene, &frame_front_camera(), 80, 40).to_text(),
        include_str!("fixtures/frames/cylinder_front.txt")
    );
}

#[cfg(feature = "cells3d")]
#[test]
fn test_frame_cylinder_three_quarter() {
    use hypergraph_rs::raster::instruments::Instrument;
    let scene = frame_scene(Instrument::Cylinder);
    assert_eq!(
        rasterize(&scene, &frame_three_quarter_camera(), 80, 40).to_text(),
        include_str!("fixtures/frames/cylinder_3q.txt")
    );
}

#[cfg(feature = "cells3d")]
#[test]
fn test_frame_terrain_front() {
    use hypergraph_rs::raster::instruments::Instrument;
    let scene = frame_scene(Instrument::Terrain);
    assert_eq!(
        rasterize(&scene, &frame_front_camera(), 80, 40).to_text(),
        include_str!("fixtures/frames/terrain_front.txt")
    );
}

#[cfg(feature = "cells3d")]
#[test]
fn test_frame_terrain_three_quarter() {
    use hypergraph_rs::raster::instruments::Instrument;
    let scene = frame_scene(Instrument::Terrain);
    assert_eq!(
        rasterize(&scene, &frame_three_quarter_camera(), 80, 40).to_text(),
        include_str!("fixtures/frames/terrain_3q.txt")
    );
}

#[cfg(feature = "cells3d")]
#[test]
fn test_frame_sankey_front() {
    use hypergraph_rs::raster::instruments::Instrument;
    let scene = frame_scene(Instrument::Sankey);
    assert_eq!(
        rasterize(&scene, &frame_front_camera(), 80, 40).to_text(),
        include_str!("fixtures/frames/sankey_front.txt")
    );
}

#[cfg(feature = "cells3d")]
#[test]
fn test_frame_sankey_three_quarter() {
    use hypergraph_rs::raster::instruments::Instrument;
    let scene = frame_scene(Instrument::Sankey);
    assert_eq!(
        rasterize(&scene, &frame_three_quarter_camera(), 80, 40).to_text(),
        include_str!("fixtures/frames/sankey_3q.txt")
    );
}

#[cfg(feature = "cells3d")]
#[test]
fn test_frame_cylinder_front_ansi_truecolor() {
    use hypergraph_rs::raster::instruments::Instrument;
    use hypergraph_rs::raster::Palette;
    let scene = frame_scene(Instrument::Cylinder);
    assert_eq!(
        rasterize(&scene, &frame_front_camera(), 80, 40).to_ansi(Palette::TrueColor),
        include_str!("fixtures/frames/cylinder_front.ansi.txt")
    );
}

// ---------- Task 13: Stage 3 — property tests ----------

/// Property 1 (depth-sort stability): three non-overlapping equal-depth
/// triangles, each in its own screen-space corner so no two ever composite
/// onto the same subcell. Feeding `rasterize` the scene faces in insertion
/// order vs. two other permutations of that same `Vec` must yield a
/// byte-identical frame — the stable sort at equal depth never introduces a
/// draw-order dependence when primitives don't actually overlap.
#[test]
fn test_property_depth_sort_stability_under_shuffle() {
    let tri = |cx: f64, color: Rgb| Face {
        verts: [
            Vertex3 {
                x: cx - 0.2,
                y: -0.2,
                z: 0.0,
            },
            Vertex3 {
                x: cx + 0.2,
                y: -0.2,
                z: 0.0,
            },
            Vertex3 {
                x: cx,
                y: 0.2,
                z: 0.0,
            },
        ],
        fill: color,
        opacity: 1.0,
    };
    let left = tri(-1.5, Rgb(255, 0, 0));
    let mid = tri(0.0, Rgb(0, 255, 0));
    let right = tri(1.5, Rgb(0, 0, 255));

    let orderings: [[Face; 3]; 3] = [
        [left.clone(), mid.clone(), right.clone()],
        [right.clone(), left.clone(), mid.clone()],
        [mid.clone(), right.clone(), left.clone()],
    ];

    let c = cam();
    let mut frames = orderings.into_iter().map(|order| {
        let mut scene = SceneGraph3D::empty();
        scene.faces = order.to_vec();
        rasterize(&scene, &c, 80, 40).to_text()
    });
    let first = frames.next().unwrap();
    for other in frames {
        assert_eq!(
            first, other,
            "non-overlapping equal-depth shuffle changed the frame"
        );
    }
}

/// Property 4 (determinism sweep): a fixed sweep of camera/size
/// combinations — including cameras behind or through the scene
/// (`dist <= 0`) and degenerate 1x1 grids — must be (a) deterministic
/// (`rasterize` called twice gives a bit-identical `CellGrid`) and (b)
/// panic-free. No RNG: the sweep is a literal, bounded table (Power-of-10
/// rule 2), never `HashMap` order.
#[test]
fn test_property_determinism_sweep_panic_free() {
    let mut scene = SceneGraph3D::empty();
    scene.faces.push(Face {
        verts: [
            Vertex3 {
                x: -0.5,
                y: -0.5,
                z: 0.3,
            },
            Vertex3 {
                x: 0.5,
                y: -0.5,
                z: -0.2,
            },
            Vertex3 {
                x: 0.0,
                y: 0.6,
                z: 0.1,
            },
        ],
        fill: Rgb(200, 20, 20),
        opacity: 1.0,
    });
    scene.struts.push(Strut {
        a: Vertex3 {
            x: -1.0,
            y: 0.0,
            z: 0.0,
        },
        b: Vertex3 {
            x: 1.0,
            y: 0.0,
            z: 0.0,
        },
        color: Rgb(10, 200, 10),
    });

    let cameras = [
        Camera {
            ry: 0.0,
            rx: 0.0,
            dist: 4.0,
            fov: 70.0,
        },
        Camera {
            ry: 1.9,
            rx: -1.2,
            dist: 0.0,
            fov: 90.0,
        }, // camera at the origin
        Camera {
            ry: -0.4,
            rx: 3.1,
            dist: -5.0,
            fov: 45.0,
        }, // camera behind/through the scene
        Camera {
            ry: core::f64::consts::PI,
            rx: core::f64::consts::PI,
            dist: 100.0,
            fov: 1.0,
        }, // near-zero fov, huge dist
        Camera {
            ry: 0.0,
            rx: 0.0,
            dist: 0.5,
            fov: 179.0,
        }, // fov near the tan() blow-up edge
    ];
    let sizes: [(u16, u16); 3] = [(80, 40), (1, 1), (200, 1)];

    for c in &cameras {
        for &(w, h) in &sizes {
            let a = rasterize(&scene, c, w, h);
            let b = rasterize(&scene, c, w, h);
            assert_eq!(a, b, "non-deterministic frame at {c:?} {w}x{h}");
        }
    }
}