catwalk3d 1.0.1

Headless model thumbnails rendered entirely in Rust
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
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
//! STEP Part 21 analytic, faceted and AP242 geometry with shared assembly placements.
use super::{
    step_budget::Budget, step_faceted, step_parameters, step_tessellation, transform::Transform,
};
use crate::Result;
use crate::scene::{Scene, Triangle, Vec3};
use ruststep::ast::{EntityInstance, Name, Parameter, Record};
use ruststep::tables::EntityTable;
use std::collections::{BTreeMap, HashMap, HashSet};
use std::path::Path;
use truck_meshalgo::prelude::*;
use truck_stepio::r#in::{
    FaceAny, ShellHolder, SphericalSurfaceHolder, Table, ToroidalSurfaceHolder, VertexPointHolder,
    ruststep,
};

/// Maximum parsed schema entity count.
const MAX_ENTITIES: usize = 250_000;
/// Maximum CAD face count accepted for a preview body.
const MAX_FACES: usize = 100_000;
/// Maximum source or expanded preview triangle count.
const MAX_TRIANGLES: usize = 5_000_000;
/// Maximum parameter nesting or entity dependency depth.
const MAX_REFERENCE_DEPTH: usize = 64;

/// Read STEP geometry and convert Rust-kernel panics into importer errors.
///
/// # Errors
///
/// File/UTF-8 parsing, unsupported topology, placements or tessellation fail; kernel panics are caught.
pub(super) fn load(path: &Path) -> Result<Scene> {
    let bytes = super::read_file(path)?;
    let text = std::str::from_utf8(&bytes)?.trim_start_matches('\u{feff}');
    // Truck exposes infallible geometry constructors; convert their panics to importer errors.
    std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| parse(text)))
        .map_err(|_| "STEP geometry could not be tessellated by the Rust CAD kernel")?
}

/// Expose an entity identifier and its simple or complex record list.
fn records(entity: &EntityInstance) -> (u64, &[Record]) {
    match entity {
        EntityInstance::Simple { id, record } => (*id, std::slice::from_ref(record)),
        EntityInstance::Complex { id, subsuper } => (*id, &subsuper.0),
    }
}

/// Cap nesting before ruststep recursively parses parameter lists.
fn check_nesting(text: &str) -> Result<()> {
    let bytes = text.as_bytes();
    let (mut index, mut depth) = (0usize, 0usize);
    while index < bytes.len() {
        match bytes[index] {
            b'\'' => {
                index += 1;
                loop {
                    let Some(&byte) = bytes.get(index) else {
                        return Err("unterminated STEP string".into());
                    };
                    index += 1;
                    if byte == b'\'' {
                        if bytes.get(index) == Some(&b'\'') {
                            index += 1;
                        } else {
                            break;
                        }
                    }
                }
            }
            b'/' if bytes.get(index + 1) == Some(&b'*') => {
                index += 2;
                while bytes.get(index..index + 2) != Some(b"*/") {
                    if index >= bytes.len() {
                        return Err("unterminated STEP comment".into());
                    }
                    index += 1;
                }
                index += 2;
            }
            b'(' => {
                depth += 1;
                if depth > MAX_REFERENCE_DEPTH {
                    return Err("STEP parameter nesting exceeds importer limit".into());
                }
                index += 1;
            }
            b')' => {
                depth = depth.checked_sub(1).ok_or("unbalanced STEP parentheses")?;
                index += 1;
            }
            _ => index += 1,
        }
    }
    if depth != 0 {
        return Err("unbalanced STEP parentheses".into());
    }
    Ok(())
}

/// Collect entity references recursively while rejecting non-finite numbers.
///
/// # Errors
///
/// A reference uses an unsupported name kind or a numeric parameter is non-finite.
fn collect_references(parameter: &Parameter, references: &mut Vec<u64>) -> Result<()> {
    match parameter {
        Parameter::Ref(Name::Entity(id)) => references.push(*id),
        Parameter::Ref(_) => return Err("STEP named/value references are not implemented".into()),
        Parameter::Real(value) if !value.is_finite() => {
            return Err("STEP numeric value is not finite".into());
        }
        Parameter::List(values) => {
            for value in values {
                collect_references(value, references)?;
            }
        }
        Parameter::Typed { parameter, .. } => collect_references(parameter, references)?,
        _ => {}
    }
    Ok(())
}

/// Reject dependency cycles before Truck's recursive owned-entity conversion.
fn check_graph(
    id: u64,
    graph: &HashMap<u64, Vec<u64>>,
    visiting: &mut HashSet<u64>,
    done: &mut HashMap<u64, usize>,
    depth: usize,
) -> Result<usize> {
    if let Some(&remaining_depth) = done.get(&id) {
        if depth + remaining_depth > MAX_REFERENCE_DEPTH {
            return Err("STEP geometry reference depth exceeds importer limit".into());
        }
        return Ok(remaining_depth);
    }
    if depth > MAX_REFERENCE_DEPTH {
        return Err("STEP geometry reference depth exceeds importer limit".into());
    }
    if !visiting.insert(id) {
        return Err("STEP geometry contains cyclic entity references".into());
    }
    let references = graph
        .get(&id)
        .ok_or_else(|| format!("STEP references missing entity #{id}"))?;
    let mut remaining_depth = 0usize;
    for &next in references {
        remaining_depth =
            remaining_depth.max(check_graph(next, graph, visiting, done, depth + 1)? + 1);
    }
    visiting.remove(&id);
    done.insert(id, remaining_depth);
    Ok(remaining_depth)
}

/// Require a STEP parameter-list value.
///
/// # Errors
///
/// The parameter is not a list.
fn list(parameter: &Parameter) -> Result<&[Parameter]> {
    match parameter {
        Parameter::List(values) => Ok(values),
        _ => Err("invalid STEP entity parameters".into()),
    }
}

/// Require a STEP entity reference.
///
/// # Errors
///
/// The parameter is not a numeric entity reference.
fn reference(parameter: &Parameter) -> Result<u64> {
    match parameter {
        Parameter::Ref(Name::Entity(id)) => Ok(*id),
        _ => Err("STEP entity must reference a geometry record".into()),
    }
}

/// Resolve an identifier to a supported simple entity record.
///
/// # Errors
///
/// The entity is missing or is a complex record.
fn simple_record<'a>(entities: &HashMap<u64, &'a EntityInstance>, id: u64) -> Result<&'a Record> {
    match entities.get(&id) {
        Some(EntityInstance::Simple { record, .. }) => Ok(record),
        _ => Err(format!("STEP entity #{id} is not a supported simple record").into()),
    }
}

/// Read an analytic radius larger than the kernel's numerical tolerance.
///
/// # Errors
///
/// The parameter is nonnumeric, non-finite or too small for the kernel.
#[allow(
    clippy::cast_precision_loss,
    reason = "STEP integer dimensions intentionally use the geometry kernel's f64 representation, matching real parameters."
)]
fn positive_radius(parameter: &Parameter) -> Result<f64> {
    let value = match parameter {
        Parameter::Real(value) => *value,
        Parameter::Integer(value) => *value as f64,
        _ => return Err("invalid STEP analytic radius".into()),
    };
    if !value.is_finite() || value <= TOLERANCE {
        return Err("STEP analytic radius must be finite and exceed kernel tolerance".into());
    }
    Ok(value)
}

/// Extract a finite included or excluded surface-parameter bound.
///
/// # Errors
///
/// The parameter is unbounded or non-finite.
fn finite_parameter_bound(value: std::ops::Bound<f64>) -> Result<f64> {
    match value {
        std::ops::Bound::Included(value) | std::ops::Bound::Excluded(value)
            if value.is_finite() =>
        {
            Ok(value)
        }
        _ => Err("STEP geometry has unbounded or non-finite parameters".into()),
    }
}

/// Tessellate a closed analytic surface represented by a vertex loop.
///
/// # Errors
///
/// The vertex is off-surface, parameters are unbounded or geometry/triangle limits fail.
#[allow(
    clippy::cast_precision_loss,
    reason = "Validated dimensions and bounded subdivision indices intentionally retain existing f64 evaluation and rounding."
)]
#[allow(
    clippy::manual_midpoint,
    reason = "Finite analytic surface bounds are limited; preserve the established sampling and rounding order."
)]
fn append_closed_surface<S: ParametricSurface3D + SearchParameter<D2, Point = Point3>>(
    surface: &S,
    invert: bool,
    vertex: Point3,
    scene: &mut Scene,
    budget: &mut Budget,
) -> Result<()> {
    if surface.search_parameter(vertex, None, 100).is_none() {
        return Err("STEP vertex loop point does not lie on its analytic surface".into());
    }
    let ((u0, u1), (v0, v1)) = surface.parameter_range();
    let (u0, u1, v0, v1) = (
        finite_parameter_bound(u0)?,
        finite_parameter_bound(u1)?,
        finite_parameter_bound(v0)?,
        finite_parameter_bound(v1)?,
    );
    for i in 0..64 {
        for j in 0..64 {
            let u = |i: usize| u0 + (u1 - u0) * i as f64 / 64.0;
            let v = |j: usize| v0 + (v1 - v0) * j as f64 / 64.0;
            let points = [
                surface.subs(u(i), v(j)),
                surface.subs(u(i + 1), v(j)),
                surface.subs(u(i + 1), v(j + 1)),
                surface.subs(u(i), v(j + 1)),
            ]
            .map(|point| Vec3::new(point.x, point.y, point.z));
            if points.iter().any(|p| !p.is_finite()) {
                return Err("STEP analytic surface produced non-finite points".into());
            }
            let normal = surface.normal((u(i) + u(i + 1)) * 0.5, (v(j) + v(j + 1)) * 0.5);
            let normal = Vec3::new(normal.x, normal.y, normal.z);
            for mut vertices in [
                [points[0], points[1], points[2]],
                [points[0], points[2], points[3]],
            ] {
                let cross = (vertices[1] - vertices[0]).cross(vertices[2] - vertices[0]);
                if cross.length() == 0.0 {
                    continue;
                }
                if (cross.dot(normal) < 0.0) != invert {
                    vertices.swap(1, 2);
                }
                if scene.triangles.len() == MAX_TRIANGLES {
                    return Err("STEP tessellation exceeds triangle limit".into());
                }
                budget.triangles(1, false)?;
                scene.triangles.push(Triangle {
                    vertices,
                    color: None,
                });
            }
        }
    }
    Ok(())
}

/// A vertex loop on a full sphere/torus marks a closed surface, not a polygonal trim.
/// Truck 0.3 only accepts edge loops; evaluate these analytic surfaces directly.
fn closed_analytic_shell(
    id: u64,
    entities: &HashMap<u64, &EntityInstance>,
    table: &Table,
    budget: &mut Budget,
) -> Result<Option<Scene>> {
    let shell = simple_record(entities, id)?;
    let shell_params = list(&shell.parameter)?;
    let faces = list(shell_params.get(1).ok_or("missing STEP shell faces")?)?;
    if faces.is_empty() {
        return Ok(None);
    }
    let mut analytic_faces = Vec::new();
    for face in faces {
        let face_id = reference(face)?;
        let face_record = simple_record(entities, face_id)?;
        if !matches!(face_record.name.as_str(), "ADVANCED_FACE" | "FACE_SURFACE") {
            return Ok(None);
        }
        let face_params = list(&face_record.parameter)?;
        let bounds = list(face_params.get(1).ok_or("missing STEP face bounds")?)?;
        if bounds.len() != 1 {
            return Ok(None);
        }
        let bound_record = simple_record(entities, reference(&bounds[0])?)?;
        let bound_params = list(&bound_record.parameter)?;
        let loop_record = simple_record(
            entities,
            reference(bound_params.get(1).ok_or("missing STEP face loop")?)?,
        )?;
        if loop_record.name.as_str() != "VERTEX_LOOP" {
            return Ok(None);
        }
        let loop_params = list(&loop_record.parameter)?;
        let vertex_id = reference(loop_params.get(1).ok_or("missing STEP vertex loop point")?)?;
        let vertex = <Table as EntityTable<VertexPointHolder>>::get_owned(table, vertex_id)
            .map_err(|error| format!("cannot resolve STEP vertex loop: {error}"))?;
        let vertex = Point3::from(&vertex.vertex_geometry);
        let surface_id = reference(face_params.get(2).ok_or("missing STEP face surface")?)?;
        let face = table
            .face_surface
            .get(&face_id)
            .ok_or("missing STEP analytic face")?;
        analytic_faces.push((surface_id, !face.same_sense, vertex));
    }
    let mut scene = Scene::default();
    for (surface_id, invert, vertex) in analytic_faces {
        let surface_record = simple_record(entities, surface_id)?;
        let surface_params = list(&surface_record.parameter)?;
        match surface_record.name.as_str() {
            "SPHERICAL_SURFACE" => {
                positive_radius(surface_params.get(2).ok_or("missing STEP sphere radius")?)?;
                let source =
                    <Table as EntityTable<SphericalSurfaceHolder>>::get_owned(table, surface_id)
                        .map_err(|error| format!("cannot resolve STEP sphere: {error}"))?;
                let surface = truck_stepio::r#in::alias::SphericalSurface::from(&source);
                append_closed_surface(&surface, invert, vertex, &mut scene, budget)?;
            }
            "TOROIDAL_SURFACE" => {
                let major = positive_radius(
                    surface_params
                        .get(2)
                        .ok_or("missing STEP torus major radius")?,
                )?;
                let minor = positive_radius(
                    surface_params
                        .get(3)
                        .ok_or("missing STEP torus minor radius")?,
                )?;
                if major <= minor {
                    return Err("STEP horn/spindle torus surfaces are not implemented".into());
                }
                let source =
                    <Table as EntityTable<ToroidalSurfaceHolder>>::get_owned(table, surface_id)
                        .map_err(|error| format!("cannot resolve STEP torus: {error}"))?;
                let surface = truck_stepio::r#in::alias::ToroidalSurface::from(&source);
                append_closed_surface(&surface, invert, vertex, &mut scene, budget)?;
            }
            _ => {
                return Err(
                    "STEP vertex-loop surface requires a supported full sphere or torus".into(),
                );
            }
        }
    }
    scene.validate()?;
    Ok(Some(scene))
}

/// Read a required positional entity field.
///
/// # Errors
///
/// The entity's parameter list lacks the requested field.
fn field(record: &Record, index: usize) -> Result<&Parameter> {
    list(&record.parameter)?
        .get(index)
        .ok_or_else(|| "truncated STEP assembly entity".into())
}

/// Find an entity's record with the requested schema name, if present.
///
/// # Errors
///
/// The referenced entity is missing; an absent matching record returns `None`.
fn named_record<'a>(
    entities: &HashMap<u64, &'a EntityInstance>,
    id: u64,
    name: &str,
) -> Result<Option<&'a Record>> {
    let entity = entities
        .get(&id)
        .ok_or_else(|| format!("STEP references missing entity #{id}"))?;
    Ok(records(entity)
        .1
        .iter()
        .find(|record| record.name.as_str() == name))
}

/// Read a finite integer or real STEP parameter.
///
/// # Errors
///
/// The value is not numeric or is non-finite.
#[allow(
    clippy::cast_precision_loss,
    reason = "STEP integer dimensions intentionally use the geometry kernel's f64 representation, matching real parameters."
)]
fn number(parameter: &Parameter) -> Result<f64> {
    let value = match parameter {
        Parameter::Real(value) => *value,
        Parameter::Integer(value) => *value as f64,
        Parameter::Typed { parameter, .. } => return number(parameter),
        _ => return Err("STEP assembly requires a numeric value".into()),
    };
    if !value.is_finite() {
        return Err("STEP assembly numeric value is non-finite".into());
    }
    Ok(value)
}

/// Resolve supported SI and conversion-based length units to millimetres.
///
/// # Errors
///
/// Units are missing, unsupported, nonpositive or recursively exceed the unit-depth limit.
fn unit_factor(entities: &HashMap<u64, &EntityInstance>, id: u64, depth: usize) -> Result<f64> {
    if depth > 32 {
        return Err("STEP unit definitions are cyclic or too deep".into());
    }
    let factor = if let Some(unit) = named_record(entities, id, "SI_UNIT")? {
        if !matches!(field(unit, 1)?, Parameter::Enumeration(name) if name == "METRE") {
            return Err("STEP assembly length unit must be a supported metre unit".into());
        }
        let exponent = match field(unit, 0)? {
            Parameter::NotProvided => 0,
            Parameter::Enumeration(prefix) => match prefix.as_str() {
                "EXA" => 18,
                "PETA" => 15,
                "TERA" => 12,
                "GIGA" => 9,
                "MEGA" => 6,
                "KILO" => 3,
                "HECTO" => 2,
                "DECA" => 1,
                "DECI" => -1,
                "CENTI" => -2,
                "MILLI" => -3,
                "MICRO" => -6,
                "NANO" => -9,
                "PICO" => -12,
                "FEMTO" => -15,
                "ATTO" => -18,
                _ => return Err("unsupported STEP SI length prefix".into()),
            },
            _ => return Err("invalid STEP SI length prefix".into()),
        };
        1000. * 10f64.powi(exponent)
    } else if let Some(unit) = named_record(entities, id, "CONVERSION_BASED_UNIT")? {
        let value_id = reference(field(unit, 1)?)?;
        // Complex STEP instances put inherited values in MEASURE_WITH_UNIT;
        // LENGTH_MEASURE_WITH_UNIT() is only an empty subtype marker there.
        let value = named_record(entities, value_id, "MEASURE_WITH_UNIT")?
            .or(named_record(
                entities,
                value_id,
                "LENGTH_MEASURE_WITH_UNIT",
            )?)
            .ok_or("unsupported STEP conversion-based length measure")?;
        number(field(value, 0)?)? * unit_factor(entities, reference(field(value, 1)?)?, depth + 1)?
    } else {
        return Err("unsupported STEP assembly length unit".into());
    };
    if !factor.is_finite() || factor <= 0. {
        return Err("invalid STEP assembly unit scale".into());
    }
    Ok(factor)
}

/// Read the length-unit scale attached to a representation context.
///
/// # Errors
///
/// The context/unit structure is missing, ambiguous or unsupported.
#[allow(
    clippy::float_cmp,
    reason = "Representation dimensionality must equal the exact schema value three."
)]
fn context_factor(entities: &HashMap<u64, &EntityInstance>, id: u64) -> Result<f64> {
    if let Some(context) = named_record(entities, id, "GEOMETRIC_REPRESENTATION_CONTEXT")?
        && number(field(context, 0)?)? != 3.
    {
        return Err("STEP assembly requires 3D representation contexts".into());
    }
    let Some(units) = named_record(entities, id, "GLOBAL_UNIT_ASSIGNED_CONTEXT")? else {
        return Ok(1.);
    };
    let mut scale = None;
    for unit in list(field(units, 0)?)? {
        let unit = reference(unit)?;
        if named_record(entities, unit, "LENGTH_UNIT")?.is_some() {
            if scale.is_some() {
                return Err("STEP context has multiple length units".into());
            }
            scale = Some(unit_factor(entities, unit, 0)?);
        }
    }
    scale.ok_or_else(|| "STEP unit context lacks a supported length unit".into())
}

/// Read three finite Cartesian or direction components from an entity.
///
/// # Errors
///
/// The entity kind, dimensionality or numeric components are invalid.
fn coordinate(entities: &HashMap<u64, &EntityInstance>, id: u64, kind: &str) -> Result<Vec3> {
    let record = named_record(entities, id, kind)?
        .ok_or_else(|| format!("STEP placement requires {kind}"))?;
    let values = list(field(record, 1)?)?;
    if values.len() != 3 {
        return Err("STEP placement coordinates must be three-dimensional".into());
    }
    Ok(Vec3::new(
        number(&values[0])?,
        number(&values[1])?,
        number(&values[2])?,
    ))
}

/// Resolve an optional normalized direction, using the supplied default when omitted.
///
/// # Errors
///
/// The referenced direction is missing, invalid or has zero length.
fn direction(
    entities: &HashMap<u64, &EntityInstance>,
    parameter: &Parameter,
    default: Vec3,
) -> Result<Vec3> {
    let value = if matches!(parameter, Parameter::NotProvided) {
        default
    } else {
        coordinate(entities, reference(parameter)?, "DIRECTION")?
    };
    let length = value.length();
    if !length.is_finite() || length <= 0. {
        return Err("STEP placement has an invalid direction".into());
    }
    Ok(value / length)
}

/// Build a rigid frame from an axis placement in the representation's units.
///
/// # Errors
///
/// The placement, coordinates or axis directions cannot define a finite orthogonal frame.
fn axis_frame(entities: &HashMap<u64, &EntityInstance>, id: u64, scale: f64) -> Result<Transform> {
    let record = named_record(entities, id, "AXIS2_PLACEMENT_3D")?
        .ok_or("STEP assembly supports 3D axis placements only")?;
    let origin = coordinate(entities, reference(field(record, 1)?)?, "CARTESIAN_POINT")? * scale;
    let z = direction(entities, field(record, 2)?, Vec3::new(0., 0., 1.))?;
    let default_x = if z.cross(Vec3::new(1., 0., 0.)).length() > 1e-12 {
        Vec3::new(1., 0., 0.)
    } else {
        Vec3::new(0., 1., 0.)
    };
    let reference = direction(entities, field(record, 3)?, default_x)?;
    let x = reference - z * reference.dot(z);
    if x.length() < 1e-12 {
        return Err("STEP placement axis and reference direction are parallel".into());
    }
    let x = x / x.length();
    let y = z.cross(x);
    let transform = Transform([
        [x.x, x.y, x.z, 0.],
        [y.x, y.y, y.z, 0.],
        [z.x, z.y, z.z, 0.],
        [origin.x, origin.y, origin.z, 1.],
    ]);
    if transform.0.iter().flatten().any(|v| !v.is_finite()) {
        return Err("STEP placement is non-finite".into());
    }
    Ok(transform)
}

/// Frames are orthonormal, so inverse rotation is its transpose.
fn inverse_frame(transform: Transform) -> Transform {
    let mut inverse = Transform::IDENTITY;
    for column in 0..3 {
        for row in 0..3 {
            inverse.0[column][row] = transform.0[row][column];
        }
    }
    let origin = Vec3::new(transform.0[3][0], transform.0[3][1], transform.0[3][2]);
    let origin = inverse.apply(origin) * -1.;
    inverse.0[3] = [origin.x, origin.y, origin.z, 1.];
    inverse
}

/// Identify supported representation schema names.
fn is_shape_representation(name: &str) -> bool {
    (name.ends_with("SHAPE_REPRESENTATION") && name != "CONTEXT_DEPENDENT_SHAPE_REPRESENTATION")
        || name == "TESSELLATED_SHAPE_REPRESENTATION_WITH_ACCURACY_PARAMETERS"
}

/// Shape items and authored unit scale belonging to one representation.
struct Representation {
    /// Entity identifiers for geometry or mapped items owned by this representation.
    items: Vec<u64>,
    /// Authored representation length scale in millimetres.
    scale: f64,
}
/// A placed child representation owned by an assembly occurrence.
#[derive(Clone, Copy)]
struct Placement {
    /// Product-occurrence relationship identifier owning this placement.
    occurrence: u64,
    /// Entity identifier of the placed child representation.
    child: u64,
    /// Local-to-parent affine placement in normalized scene units.
    transform: Transform,
}
/// Indexed representation ownership, equivalent-shape groups and assembly roots.
struct Assembly<'a> {
    /// Indexed source entities used to resolve typed geometry and placements.
    entities: &'a HashMap<u64, &'a EntityInstance>,
    /// Shape representations indexed by their source entity identifiers.
    representations: BTreeMap<u64, Representation>,
    /// Placed child occurrences grouped by parent representation identifier.
    children: HashMap<u64, Vec<Placement>>,
    /// Equivalent representation groups for shared product-shape ownership.
    groups: BTreeMap<u64, Vec<u64>>,
    /// Representation identifiers selected as assembly roots.
    roots: Vec<u64>,
}
/// Mutable assembly expansion state and shared tessellated item cache.
struct Expansion<'a> {
    /// Cached tessellated item scenes indexed by source geometry identifier.
    meshes: &'a HashMap<u64, Scene>,
    /// Owned AP242 decoder loading only geometry selected by shape ownership.
    decoder: step_tessellation::Decoder<'a>,
    /// Owned planar polygon decoder loading only selected topology.
    faceted: step_faceted::Decoder<'a>,
    /// Decoded owned item scenes reused across repeated part occurrences.
    owned: HashMap<u64, Scene>,
    /// Source dependency graph used to validate selected polygonal geometry.
    graph: &'a HashMap<u64, Vec<u64>>,
    /// Dependency depths already checked for selected polygonal items.
    dependencies: HashMap<u64, usize>,
    /// Cumulative geometry, allocation and processing budgets across every loader.
    budget: Budget,
    /// Scene receiving all expanded assembly occurrences.
    scene: Scene,
    /// Active representation identifiers used to reject expansion cycles.
    visiting: HashSet<u64>,
    /// Geometry item identifiers consumed by successful expansion.
    used: HashSet<u64>,
    /// Accumulated processing work charged against the preview budget.
    work: usize,
}

impl<'a> Assembly<'a> {
    /// Resolve an assembly representation by its entity identifier.
    ///
    /// # Errors
    ///
    /// The identifier does not name a known representation.
    fn representation(&self, id: u64) -> Result<&Representation> {
        self.representations.get(&id).ok_or_else(|| {
            format!("STEP assembly references unsupported shape representation #{id}").into()
        })
    }
    /// Build representation ownership, placement relationships and assembly roots.
    ///
    /// # Errors
    ///
    /// Representation/unit/product relationships are missing, ambiguous, unsupported or inconsistent.
    #[allow(
        clippy::too_many_lines,
        reason = "Keep the format record dispatch, validation and its resource checks together without changing geometry evaluation."
    )]
    fn new(entities: &'a HashMap<u64, &'a EntityInstance>) -> Result<Self> {
        let mut assembly = Self {
            entities,
            representations: BTreeMap::new(),
            children: HashMap::new(),
            groups: BTreeMap::new(),
            roots: Vec::new(),
        };
        let mut items_count = 0usize;
        for (&id, entity) in entities {
            let entity_records = records(entity).1;
            if !entity_records
                .iter()
                .any(|r| is_shape_representation(r.name.as_str()))
            {
                continue;
            }
            let record = match entity {
                EntityInstance::Simple { record, .. } => record,
                EntityInstance::Complex { .. } => entity_records
                    .iter()
                    .find(|r| r.name.as_str() == "REPRESENTATION")
                    .ok_or("unsupported complex STEP shape representation")?,
            };
            let items = list(field(record, 1)?)?
                .iter()
                .map(reference)
                .collect::<Result<Vec<_>>>()?;
            items_count = items_count
                .checked_add(items.len())
                .ok_or("STEP assembly item count overflow")?;
            if items_count > MAX_ENTITIES {
                return Err("STEP assembly exceeds representation-item limit".into());
            }
            assembly.representations.insert(
                id,
                Representation {
                    items,
                    scale: context_factor(entities, reference(field(record, 2)?)?)?,
                },
            );
        }
        if assembly.representations.is_empty() {
            return Err("STEP assembly has no supported shape representations".into());
        }
        let mut owners = HashMap::new();
        let mut relationships = BTreeMap::new();
        for (&id, entity) in entities {
            for record in records(entity).1 {
                if record.name.as_str() == "SHAPE_DEFINITION_REPRESENTATION" {
                    let rep = reference(field(record, 1)?)?;
                    let definition = reference(field(record, 0)?)?;
                    if let Some(shape) =
                        named_record(entities, definition, "PRODUCT_DEFINITION_SHAPE")?
                    {
                        let product = reference(field(shape, 2)?)?;
                        if named_record(entities, product, "PRODUCT_DEFINITION")?.is_some() {
                            assembly.representation(rep)?;
                            if owners.insert(rep, product).is_some_and(|p| p != product) {
                                return Err(
                                    "STEP representation has ambiguous product ownership".into()
                                );
                            }
                        }
                    }
                }
                if record.name.as_str() == "REPRESENTATION_RELATIONSHIP_WITH_TRANSFORMATION" {
                    let relationship = named_record(entities, id, "REPRESENTATION_RELATIONSHIP")?
                        .unwrap_or(record);
                    let parameters = list(&record.parameter)?;
                    let operator = if parameters.len() == 1 {
                        reference(&parameters[0])?
                    } else {
                        reference(field(record, 4)?)?
                    };
                    let first = reference(field(relationship, 2)?)?;
                    let second = reference(field(relationship, 3)?)?;
                    assembly.representation(first)?;
                    assembly.representation(second)?;
                    let transform = assembly.relationship_transform(operator, first, second)?;
                    relationships.insert(id, (first, second, transform));
                }
            }
        }
        let mut equivalent = Vec::new();
        for &id in entities.keys() {
            if !relationships.contains_key(&id)
                && let Some(record) =
                    named_record(entities, id, "SHAPE_REPRESENTATION_RELATIONSHIP")?
            {
                let record =
                    named_record(entities, id, "REPRESENTATION_RELATIONSHIP")?.unwrap_or(record);
                let first = reference(field(record, 2)?)?;
                let second = reference(field(record, 3)?)?;
                assembly.representation(first)?;
                assembly.representation(second)?;
                equivalent.push((first, second));
            }
        }
        // A producer may attach an occurrence to the B-rep representation,
        // while the product owner is attached to its equivalent axis-only
        // representation. Propagate ownership before resolving occurrences.
        assembly.group_equivalent_representations(&equivalent, &mut owners)?;
        for (&anchor, members) in &assembly.groups {
            if let Some(&owner) = owners.get(&anchor) {
                for &member in members {
                    owners.insert(member, owner);
                }
            }
        }
        let mut usages = HashSet::new();
        let mut consumed_relationships = HashSet::new();
        // Product ownership determines direction even when a producer reverses
        // the relationship. See STEPConstruct_Assembly::CheckSRRReversesNAUO.
        for entity in entities.values() {
            for record in records(entity).1 {
                if record.name.as_str() != "CONTEXT_DEPENDENT_SHAPE_REPRESENTATION" {
                    continue;
                }
                let relationship = reference(field(record, 0)?)?;
                let &(first, second, transform) = relationships.get(&relationship).ok_or(
                    "STEP assembly occurrence lacks a supported relationship transformation",
                )?;
                let shape_id = reference(field(record, 1)?)?;
                let shape = named_record(entities, shape_id, "PRODUCT_DEFINITION_SHAPE")?
                    .ok_or("STEP assembly occurrence lacks a product-definition shape")?;
                let usage_id = reference(field(shape, 2)?)?;
                let usage = named_record(entities, usage_id, "NEXT_ASSEMBLY_USAGE_OCCURRENCE")?
                    .ok_or("unsupported STEP assembly usage relationship")?;
                let parent = reference(field(usage, 3)?)?;
                let child = reference(field(usage, 4)?)?;
                let first_owner = owners
                    .get(&first)
                    .ok_or("STEP assembly representation lacks product ownership")?;
                let second_owner = owners
                    .get(&second)
                    .ok_or("STEP assembly representation lacks product ownership")?;
                let (parent_rep, placement) = if *first_owner == child && *second_owner == parent {
                    (
                        second,
                        Placement {
                            occurrence: records(entity).0,
                            child: first,
                            transform,
                        },
                    )
                } else if *first_owner == parent && *second_owner == child {
                    (
                        first,
                        Placement {
                            occurrence: records(entity).0,
                            child: second,
                            transform: inverse_frame(transform),
                        },
                    )
                } else {
                    return Err("STEP representation relationship disagrees with assembly product ownership".into());
                };
                assembly
                    .children
                    .entry(parent_rep)
                    .or_default()
                    .push(placement);
                usages.insert(usage_id);
                consumed_relationships.insert(relationship);
            }
        }
        for (&id, (first, second, transform)) in &relationships {
            if !consumed_relationships.contains(&id) {
                assembly
                    .children
                    .entry(*second)
                    .or_default()
                    .push(Placement {
                        occurrence: id,
                        child: *first,
                        transform: *transform,
                    });
            }
        }
        for (&id, entity) in entities {
            for record in records(entity).1 {
                match record.name.as_str() {
                    "NEXT_ASSEMBLY_USAGE_OCCURRENCE" if !usages.contains(&id) => {
                        return Err(
                            "STEP assembly usage has no supported component placement".into()
                        );
                    }
                    _ => {}
                }
            }
        }
        // Mapping-origin subtraction matters when the referenced part is not
        // authored at the origin. Frames and geometry use each context's units.
        let mut mapped_owners = HashMap::<u64, Vec<u64>>::new();
        let mut presentation_items = HashSet::new();
        for (&id, entity) in entities {
            if records(entity)
                .1
                .iter()
                .any(|record| record.name.as_str() == "DRAUGHTING_MODEL")
            {
                let record = named_record(entities, id, "REPRESENTATION")?
                    .or(named_record(entities, id, "DRAUGHTING_MODEL")?)
                    .ok_or("STEP draughting model lacks representation attributes")?;
                for item in list(field(record, 1)?)? {
                    presentation_items.insert(reference(item)?);
                }
            }
        }
        for (&parent, representation) in &assembly.representations {
            for &item in &representation.items {
                if named_record(entities, item, "MAPPED_ITEM")?.is_some() {
                    mapped_owners.entry(item).or_default().push(parent);
                }
            }
        }
        for (&id, entity) in entities {
            if records(entity)
                .1
                .iter()
                .any(|r| r.name.as_str() == "MAPPED_ITEM")
                && !mapped_owners.contains_key(&id)
                && !presentation_items.contains(&id)
            {
                return Err("STEP mapped item has no owning shape representation".into());
            }
        }
        for (item, parents) in mapped_owners {
            let record = named_record(entities, item, "MAPPED_ITEM")?.unwrap();
            let mapping = reference(field(record, 1)?)?;
            let mapping = named_record(entities, mapping, "REPRESENTATION_MAP")?
                .ok_or("STEP mapped item lacks a representation map")?;
            let origin = reference(field(mapping, 0)?)?;
            let child = reference(field(mapping, 1)?)?;
            let source = axis_frame(entities, origin, assembly.representation(child)?.scale)?;
            for parent in parents {
                let target = axis_frame(
                    entities,
                    reference(field(record, 2)?)?,
                    assembly.representation(parent)?.scale,
                )?;
                assembly
                    .children
                    .entry(parent)
                    .or_default()
                    .push(Placement {
                        occurrence: item,
                        child,
                        transform: target.then(inverse_frame(source)),
                    });
            }
        }
        assembly.group_equivalent_representations(&equivalent, &mut owners)?;
        // Check every representation, including disconnected cycles, before
        // instance expansion. Shared parts are legal; cyclic maps are not.
        let representation_graph: HashMap<_, _> = assembly
            .groups
            .keys()
            .map(|&id| {
                (
                    id,
                    assembly
                        .children
                        .get(&id)
                        .map(|children| children.iter().map(|p| p.child).collect())
                        .unwrap_or_default(),
                )
            })
            .collect();
        let mut checked = HashMap::new();
        for &id in assembly.groups.keys() {
            check_graph(
                id,
                &representation_graph,
                &mut HashSet::new(),
                &mut checked,
                0,
            )?;
        }
        let incoming: HashSet<_> = assembly
            .children
            .values()
            .flatten()
            .map(|p| p.child)
            .collect();
        assembly.roots = assembly
            .groups
            .keys()
            .filter(|id| !incoming.contains(id) && (owners.is_empty() || owners.contains_key(id)))
            .copied()
            .collect();
        if assembly.roots.is_empty() {
            return Err("STEP assembly has no roots or contains a cycle".into());
        }
        // Deterministic hierarchy order makes repeated instances stable across
        // platforms while keeping separate coincident authored occurrences.
        for children in assembly.children.values_mut() {
            children.sort_by(|a, b| {
                a.child.cmp(&b.child).then_with(|| {
                    a.transform
                        .0
                        .iter()
                        .flatten()
                        .zip(b.transform.0.iter().flatten())
                        .find_map(|(a, b)| {
                            let order = a.total_cmp(b);
                            (!order.is_eq()).then_some(order)
                        })
                        .unwrap_or(std::cmp::Ordering::Equal)
                })
            });
        }
        Ok(assembly)
    }
    /// Plain shape relationships link equivalent representations, with no
    /// directional occurrence transform. Both endpoint orders occur in AP203
    /// exporters. Preserve every member's context while grouping the geometry.
    fn group_equivalent_representations(
        &mut self,
        equivalent: &[(u64, u64)],
        owners: &mut HashMap<u64, u64>,
    ) -> Result<()> {
        self.groups.clear();
        let mut adjacency = HashMap::<u64, Vec<u64>>::new();
        for &(first, second) in equivalent {
            adjacency.entry(first).or_default().push(second);
            adjacency.entry(second).or_default().push(first);
        }
        let mut aliases = HashMap::new();
        let mut grouped_owners = HashMap::new();
        for &id in self.representations.keys() {
            if aliases.contains_key(&id) {
                continue;
            }
            let mut pending = vec![id];
            let mut members = Vec::new();
            // Mark on insertion, so redundant undirected links remain bounded.
            aliases.insert(id, id);
            while let Some(next) = pending.pop() {
                members.push(next);
                for &neighbor in adjacency.get(&next).into_iter().flatten() {
                    if let std::collections::hash_map::Entry::Vacant(entry) =
                        aliases.entry(neighbor)
                    {
                        entry.insert(id);
                        pending.push(neighbor);
                    }
                }
            }
            members.sort_unstable();
            let owned: Vec<_> = members
                .iter()
                .filter_map(|id| owners.get(id).map(|&owner| (*id, owner)))
                .collect();
            if owned.iter().any(|&(_, owner)| owner != owned[0].1) {
                return Err(
                    "STEP plain shape relationship ambiguously links different products".into(),
                );
            }
            let anchor = owned.first().map_or(members[0], |&(id, _)| id);
            if let Some(&(_, owner)) = owned.first() {
                grouped_owners.insert(anchor, owner);
            }
            for &member in &members {
                aliases.insert(member, anchor);
            }
            self.groups.insert(anchor, members);
        }
        let mut grouped_children = HashMap::<u64, Vec<Placement>>::new();
        // A mapped item shared by equivalent representations denotes the same
        // occurrence. Distinct authored occurrence IDs remain distinct even
        // when their placements coincide.
        let mut occurrences = HashMap::<(u64, u64), Placement>::new();
        for (parent, children) in std::mem::take(&mut self.children) {
            let parent = aliases[&parent];
            for mut child in children {
                child.child = aliases[&child.child];
                if let Some(previous) = occurrences.insert((parent, child.occurrence), child) {
                    if previous.child != child.child || previous.transform.0 != child.transform.0 {
                        return Err(
                            "STEP equivalent representations disagree on occurrence placement"
                                .into(),
                        );
                    }
                    continue;
                }
                grouped_children.entry(parent).or_default().push(child);
            }
        }
        self.children = grouped_children;
        *owners = grouped_owners;
        Ok(())
    }
    /// Resolve the source-to-target placement operator between two representations.
    ///
    /// # Errors
    ///
    /// The operator or axis frames are missing, unsupported or invalid.
    fn relationship_transform(&self, operator: u64, first: u64, second: u64) -> Result<Transform> {
        let record = named_record(self.entities, operator, "ITEM_DEFINED_TRANSFORMATION")?
            .ok_or("STEP supports axis-based item-defined assembly transformations only")?;
        let mut origin = reference(field(record, 2)?)?;
        let mut target = reference(field(record, 3)?)?;
        let source_rep = self.representation(first)?;
        let target_rep = self.representation(second)?;
        if !source_rep.items.contains(&origin)
            && source_rep.items.contains(&target)
            && target_rep.items.contains(&origin)
            && !target_rep.items.contains(&target)
        {
            std::mem::swap(&mut origin, &mut target);
        }
        let origin = axis_frame(self.entities, origin, source_rep.scale)?;
        let target = axis_frame(self.entities, target, target_rep.scale)?;
        // OCCT STEPControl_ActorRead::ComputeTransformation converts both
        // frames to common units before mapping the source frame to target.
        Ok(target.then(inverse_frame(origin)))
    }
    /// Expand assembly roots using cached, tessellated item geometry.
    ///
    /// # Errors
    ///
    /// Expansion encounters invalid references, missing geometry, cycles or occurrence/triangle limits.
    fn render(
        &self,
        meshes: &HashMap<u64, Scene>,
        graph: &HashMap<u64, Vec<u64>>,
        budget: Budget,
    ) -> Result<Scene> {
        let mut expansion = Expansion {
            meshes,
            decoder: step_tessellation::Decoder::new(self.entities),
            faceted: step_faceted::Decoder::new(self.entities),
            owned: HashMap::new(),
            graph,
            dependencies: HashMap::new(),
            budget,
            scene: Scene::default(),
            visiting: HashSet::new(),
            used: HashSet::new(),
            work: 0,
        };
        for &root in &self.roots {
            self.expand(root, Transform::IDENTITY, 0, &mut expansion)?;
        }
        if meshes.keys().any(|id| !expansion.used.contains(id)) {
            return Err("STEP assembly contains unowned or unsupported required shells".into());
        }
        expansion.scene.validate()?;
        Ok(expansion.scene)
    }
    /// Expand a representation's items and placed child occurrences recursively.
    ///
    /// # Errors
    ///
    /// Representations are missing, cyclic or exceed the expansion budget.
    fn expand(
        &self,
        id: u64,
        transform: Transform,
        depth: usize,
        expansion: &mut Expansion<'_>,
    ) -> Result<()> {
        expansion.work += 1;
        if depth > MAX_REFERENCE_DEPTH || expansion.work > MAX_ENTITIES {
            return Err("STEP assembly exceeds hierarchy depth or expansion limit".into());
        }
        if !expansion.visiting.insert(id) {
            return Err("STEP assembly contains a representation cycle".into());
        }
        let mut exact_items = HashSet::new();
        for &member in self
            .groups
            .get(&id)
            .ok_or("missing STEP representation group")?
        {
            let representation = self.representation(member)?;
            for &item in &representation.items {
                if let Some(shell) = step_faceted::solid_outer(self.entities[&item])? {
                    exact_items.insert((item, representation.scale.to_bits()));
                    exact_items.insert((shell, representation.scale.to_bits()));
                } else if let Some(record) =
                    named_record(self.entities, item, "MANIFOLD_SOLID_BREP")?
                {
                    exact_items.insert((item, representation.scale.to_bits()));
                    exact_items.insert((
                        reference(field(record, 1)?)?,
                        representation.scale.to_bits(),
                    ));
                } else if let Some(record) =
                    named_record(self.entities, item, "SHELL_BASED_SURFACE_MODEL")?
                {
                    for shell in list(field(record, 1)?)? {
                        exact_items.insert((reference(shell)?, representation.scale.to_bits()));
                    }
                } else if records(self.entities[&item])
                    .1
                    .iter()
                    .any(|record| matches!(record.name.as_str(), "OPEN_SHELL" | "CLOSED_SHELL"))
                {
                    exact_items.insert((item, representation.scale.to_bits()));
                }
            }
        }
        let mut included = HashSet::new();
        for &member in self
            .groups
            .get(&id)
            .ok_or("missing STEP representation group")?
        {
            let representation = self.representation(member)?;
            let mut scale = Transform::IDENTITY;
            for i in 0..3 {
                scale.0[i][i] = representation.scale;
            }
            let geometry_transform = transform.then(scale);
            if geometry_transform
                .0
                .iter()
                .flatten()
                .any(|v| !v.is_finite())
            {
                return Err("STEP assembled placement is non-finite".into());
            }
            for &item in &representation.items {
                // An explicit top-level geometric link names the same exact body
                // in this equivalent representation group. Preserve separate
                // bodies and occurrences; matching coordinates are insufficient.
                if let Some(link) =
                    step_tessellation::exact_body_link(self.entities[&item], self.entities)?
                    && exact_items.contains(&(link, representation.scale.to_bits()))
                {
                    continue;
                }
                if included.insert((item, representation.scale.to_bits())) {
                    self.append_item(item, geometry_transform, 0, expansion)?;
                }
            }
        }
        if let Some(children) = self.children.get(&id) {
            for child in children {
                self.expand(
                    child.child,
                    transform.then(child.transform),
                    depth + 1,
                    expansion,
                )?;
            }
        }
        expansion.visiting.remove(&id);
        Ok(())
    }
    /// Append one representation item, mapped item or cached shell under its world placement.
    ///
    /// # Errors
    ///
    /// The item is missing, unsupported, cyclic or exceeds expansion/geometry limits.
    fn append_item(
        &self,
        id: u64,
        transform: Transform,
        depth: usize,
        expansion: &mut Expansion<'_>,
    ) -> Result<()> {
        expansion.work += 1;
        if depth > MAX_REFERENCE_DEPTH || expansion.work > MAX_ENTITIES {
            return Err("STEP assembly exceeds geometry-item expansion limit".into());
        }
        if !expansion.owned.contains_key(&id) {
            if step_faceted::handles(self.entities[&id])
                || step_faceted::owns_shell(self.entities, id, &mut expansion.budget)?
            {
                check_graph(
                    id,
                    expansion.graph,
                    &mut HashSet::new(),
                    &mut expansion.dependencies,
                    0,
                )?;
            }
            let mesh = if let Some(mesh) = expansion.faceted.decode(id, &mut expansion.budget)? {
                Some(mesh)
            } else {
                expansion.decoder.decode(id, &mut expansion.budget)?
            };
            if let Some(mesh) = mesh {
                expansion.owned.insert(id, mesh);
            }
        }
        if let Some(mesh) = expansion.owned.get(&id) {
            append_mesh(mesh, transform, &mut expansion.scene, &mut expansion.budget)?;
            expansion.used.insert(id);
            return Ok(());
        }
        let record = simple_record(self.entities, id)?;
        match record.name.as_str() {
            "AXIS2_PLACEMENT_3D" | "MAPPED_ITEM" => {}
            "MANIFOLD_SOLID_BREP" => self.append_item(
                reference(field(record, 1)?)?,
                transform,
                depth + 1,
                expansion,
            )?,
            "SHELL_BASED_SURFACE_MODEL" => {
                for child in list(field(record, 1)?)? {
                    self.append_item(reference(child)?, transform, depth + 1, expansion)?;
                }
            }
            "OPEN_SHELL" | "CLOSED_SHELL" => {
                let mesh = expansion
                    .meshes
                    .get(&id)
                    .ok_or("STEP component references an unsupported shell")?;
                append_mesh(mesh, transform, &mut expansion.scene, &mut expansion.budget)?;
                expansion.used.insert(id);
            }
            _ => {
                return Err(format!(
                    "unsupported required STEP assembly geometry {} (#{id})",
                    record.name.as_str()
                )
                .into());
            }
        }
        Ok(())
    }
}

/// Append cached triangles and rotate their authored normals under a rigid frame and positive scale.
///
/// # Errors
///
/// The expanded geometry exceeds the triangle limit or a transformed normal is invalid.
fn append_mesh(
    mesh: &Scene,
    transform: Transform,
    scene: &mut Scene,
    budget: &mut Budget,
) -> Result<()> {
    if scene.triangles.len() + mesh.triangles.len() > MAX_TRIANGLES {
        return Err("STEP assembled preview exceeds triangle limit".into());
    }
    budget.storage(mesh.triangles.len(), std::mem::size_of::<Triangle>())?;
    budget.storage(
        mesh.appearances.len(),
        std::mem::size_of::<crate::scene::TriangleAppearance>() + 4 * std::mem::size_of::<usize>(),
    )?;
    budget.work(
        mesh.triangles
            .len()
            .checked_mul(3)
            .ok_or("STEP expanded corner work overflows")?,
    )?;
    scene.triangles.try_reserve_exact(mesh.triangles.len())?;
    let offset = scene.triangles.len();
    for triangle in &mesh.triangles {
        scene.triangles.push(Triangle {
            vertices: triangle.vertices.map(|point| transform.apply(point)),
            color: triangle.color,
        });
    }
    for (&index, appearance) in &mesh.appearances {
        let mut appearance = appearance.clone();
        if let Some(normals) = appearance.normals {
            let mut transformed = normals;
            for normal in &mut transformed {
                let components = [normal.x, normal.y, normal.z];
                let result: [f64; 3] = std::array::from_fn(|row| {
                    (0..3)
                        .map(|column| transform.0[column][row] * components[column])
                        .sum()
                });
                let direction = Vec3::new(result[0], result[1], result[2]);
                let length = direction.length();
                if !length.is_finite() || length <= 0. {
                    return Err("STEP placement produces an invalid tessellated normal".into());
                }
                *normal = direction / length;
            }
            appearance.normals = Some(transformed);
        }
        scene.appearances.insert(offset + index, appearance);
    }
    Ok(())
}

/// Identify outer-shell hints without requiring unused faceted geometry to be valid.
fn faceted_outer_hint(entity: &EntityInstance) -> Option<u64> {
    let (_, parts) = records(entity);
    if !parts.iter().any(|record| record.name == "FACETED_BREP") {
        return None;
    }
    let (record, index) = match entity {
        EntityInstance::Simple { record, .. } => (record, 1),
        EntityInstance::Complex { .. } => (
            parts
                .iter()
                .find(|record| record.name == "MANIFOLD_SOLID_BREP")?,
            0,
        ),
    };
    list(&record.parameter)
        .ok()?
        .get(index)
        .and_then(|value| reference(value).ok())
}

/// Partition polygon shells and explicitly faceted outer shells before kernel loading.
///
/// # Errors
///
/// Bounded shell classification exceeds its reference ceiling.
fn polygon_shells(
    entities: &HashMap<u64, &EntityInstance>,
    budget: &mut Budget,
) -> Result<HashSet<u64>> {
    let mut owned: HashSet<_> = entities
        .values()
        .filter_map(|entity| faceted_outer_hint(entity))
        .collect();
    for (&id, entity) in entities {
        if records(entity)
            .1
            .iter()
            .any(|record| matches!(record.name.as_str(), "OPEN_SHELL" | "CLOSED_SHELL"))
            && step_faceted::owns_shell(entities, id, budget)?
        {
            owned.insert(id);
        }
    }
    Ok(owned)
}

/// Collect existing geometry dependencies once per loader family with bounded storage and work.
///
/// # Errors
///
/// Metadata traversal or retained lookup storage exceeds the shared preview budget.
fn dependency_closure(
    roots: &HashSet<u64>,
    graph: &HashMap<u64, Vec<u64>>,
    budget: &mut Budget,
) -> Result<HashSet<u64>> {
    let mut seen = HashSet::new();
    let mut pending = Vec::new();
    for &id in roots {
        if graph.contains_key(&id) && seen.insert(id) {
            budget.storage(1, 4 * std::mem::size_of::<u64>())?;
            pending.push(id);
        }
    }
    while let Some(id) = pending.pop() {
        let references = &graph[&id];
        budget.work(references.len())?;
        for &next in references {
            if graph.contains_key(&next) && seen.insert(next) {
                budget.storage(1, 4 * std::mem::size_of::<u64>())?;
                pending.push(next);
            }
        }
    }
    Ok(seen)
}

/// Parse Part 21 entities, tessellate required shells and instantiate assembly geometry.
///
/// # Errors
///
/// Syntax, entity dependencies, CAD surfaces, placements or preview limits fail validation.
#[allow(
    clippy::too_many_lines,
    reason = "Keep the format record dispatch, validation and its resource checks together without changing geometry evaluation."
)]
fn parse(text: &str) -> Result<Scene> {
    check_nesting(text)?;
    let exchange = step_parameters::parse(text)?;
    if exchange.data.is_empty() {
        return Err("STEP file has no DATA section".into());
    }
    let mut table = Table::default();
    let mut graph = HashMap::new();
    let mut entities = HashMap::new();
    for entity in exchange.data.iter().flat_map(|section| &section.entities) {
        if graph.len() >= MAX_ENTITIES {
            return Err("STEP file exceeds entity limit".into());
        }
        let (id, records) = records(entity);
        let mut references = Vec::new();
        for record in records {
            collect_references(&record.parameter, &mut references)?;
        }
        if graph.insert(id, references).is_some() {
            return Err(format!("duplicate STEP entity #{id}").into());
        }
        entities.insert(id, entity);
    }
    let mut budget = Budget::default();
    let owned_shells = polygon_shells(&entities, &mut budget)?;
    let kernel_shells: HashSet<_> = entities
        .iter()
        .filter(|(id, entity)| {
            !owned_shells.contains(id)
                && records(entity)
                    .1
                    .iter()
                    .any(|record| matches!(record.name.as_str(), "OPEN_SHELL" | "CLOSED_SHELL"))
        })
        .map(|(&id, _)| id)
        .collect();
    let owned_geometry = dependency_closure(&owned_shells, &graph, &mut budget)?;
    let kernel_geometry = dependency_closure(&kernel_shells, &graph, &mut budget)?;
    for entity in exchange.data.iter().flat_map(|section| &section.entities) {
        let (id, _) = records(entity);
        // Keep shared ordinary BREP dependencies in Truck's public table, while
        // facet-only records are validated lazily by the selected owned loader.
        if !(step_tessellation::handles(entity)
            || step_faceted::handles(entity)
            || (owned_geometry.contains(&id) && !kernel_geometry.contains(&id)))
        {
            table
                .push_instance(entity)
                .map_err(|error| format!("cannot decode STEP entity #{id}: {error}"))?;
        }
    }
    if table.shell.is_empty()
        && !entities
            .values()
            .any(|entity| step_tessellation::handles(entity) || step_faceted::handles(entity))
        && owned_shells.is_empty()
    {
        return Err("STEP file has no supported open/closed B-rep shells".into());
    }
    if table.face_surface.len() > MAX_FACES {
        return Err("STEP file exceeds face limit".into());
    }
    budget.faces(table.face_surface.len())?;
    let mut shells: Vec<_> = table
        .shell
        .iter()
        .filter(|(id, _)| !owned_shells.contains(id))
        .collect();
    shells.sort_unstable_by_key(|&(id, _)| *id);
    let mut meshes = HashMap::new();
    let mut total_triangles = 0usize;
    let mut done = HashMap::new();
    for &(id, shell_holder) in &shells {
        let id = *id;
        let mut scene = Scene::default();
        check_graph(id, &graph, &mut HashSet::new(), &mut done, 0)?;
        if let Some(analytic) = closed_analytic_shell(id, &entities, &table, &mut budget)? {
            if total_triangles + analytic.triangles.len() > MAX_TRIANGLES {
                return Err("STEP tessellation exceeds triangle limit".into());
            }
            total_triangles += analytic.triangles.len();
            meshes.insert(id, analytic);
            continue;
        }
        let owned_shell = <Table as EntityTable<ShellHolder>>::get_owned(&table, id)
            .map_err(|error| format!("cannot resolve STEP shell #{id}: {error}"))?;
        if owned_shell.cfs_faces.is_empty() {
            return Err(format!("STEP shell #{id} has no faces").into());
        }
        let shell = table
            .to_compressed_shell(shell_holder)
            .map_err(|error| format!("cannot convert STEP shell #{id}: {error}"))?;
        if shell.faces.len() != owned_shell.cfs_faces.len() {
            return Err(format!("STEP shell #{id} contains unsupported faces").into());
        }
        for (converted, original) in shell.faces.iter().zip(&owned_shell.cfs_faces) {
            let face = match original {
                FaceAny::FaceSurface(face) => face,
                FaceAny::OrientedFace(face) => &face.face_element,
            };
            if face.bounds.is_empty() || face.bounds.len() != converted.boundaries.len() {
                return Err(
                    format!("STEP shell #{id} contains unsupported face boundaries").into(),
                );
            }
            for (boundary, original) in converted.boundaries.iter().zip(&face.bounds) {
                if boundary.is_empty() || boundary.len() != original.bound.edge_list.len() {
                    return Err(
                        format!("STEP shell #{id} contains unsupported boundary edges").into(),
                    );
                }
            }
        }
        let mut minimum = Vec3::new(f64::INFINITY, f64::INFINITY, f64::INFINITY);
        let mut maximum = Vec3::new(f64::NEG_INFINITY, f64::NEG_INFINITY, f64::NEG_INFINITY);
        {
            let mut include = |point: Point3| -> Result<()> {
                let point = Vec3::new(point.x, point.y, point.z);
                if !point.is_finite() {
                    return Err("STEP geometry contains non-finite points".into());
                }
                minimum = Vec3::new(
                    minimum.x.min(point.x),
                    minimum.y.min(point.y),
                    minimum.z.min(point.z),
                );
                maximum = Vec3::new(
                    maximum.x.max(point.x),
                    maximum.y.max(point.y),
                    maximum.z.max(point.z),
                );
                Ok(())
            };
            for point in &shell.vertices {
                include(*point)?;
            }
            // Seam vertices do not describe the extent of a full circle. Sample
            // boundary geometry so wide, thin CAD solids use a useful tolerance.
            for edge in &shell.edges {
                let (start, end) = edge.curve.parameter_range();
                let (start, end) = (finite_parameter_bound(start)?, finite_parameter_bound(end)?);
                for sample in 0..=16 {
                    include(
                        edge.curve
                            .subs(start + (end - start) * f64::from(sample) / 16.),
                    )?;
                }
            }
        }
        let diameter = (maximum - minimum).length();
        if !diameter.is_finite() || diameter <= 0.0 {
            return Err("STEP shell has no finite geometric extent".into());
        }
        let tolerance = (diameter * 0.003).max(TOLERANCE * 10.0);
        let tessellated = shell.robust_triangulation(tolerance);
        if tessellated.faces.iter().any(|face| {
            face.surface
                .as_ref()
                .is_none_or(|mesh| mesh.face_iter().next().is_none())
        }) {
            return Err(
                format!("Rust CAD tessellation failed for a face of STEP shell #{id}").into(),
            );
        }
        let mesh = tessellated.to_polygon();
        for face in mesh.face_iter() {
            let polygon = face
                .iter()
                .map(|vertex| {
                    mesh.positions()
                        .get(vertex.pos)
                        .map(|point| Vec3::new(point.x, point.y, point.z))
                        .ok_or_else(|| "STEP tessellation returned an invalid vertex index".into())
                })
                .collect::<Result<Vec<_>>>()?;
            for vertices in super::mesh::triangulate_polygon(&polygon)? {
                if scene.triangles.len() >= MAX_TRIANGLES {
                    return Err("STEP tessellation exceeds triangle limit".into());
                }
                budget.triangles(1, false)?;
                scene.triangles.push(Triangle {
                    vertices,
                    color: None,
                });
            }
        }
        total_triangles += scene.triangles.len();
        if total_triangles > MAX_TRIANGLES {
            return Err("STEP tessellation exceeds triangle limit".into());
        }
        meshes.insert(id, scene);
    }
    if entities.values().any(|entity| {
        records(entity).1.iter().any(|record| {
            is_shape_representation(record.name.as_str())
                || matches!(
                    record.name.as_str(),
                    "MAPPED_ITEM"
                        | "CONTEXT_DEPENDENT_SHAPE_REPRESENTATION"
                        | "REPRESENTATION_RELATIONSHIP_WITH_TRANSFORMATION"
                        | "NEXT_ASSEMBLY_USAGE_OCCURRENCE"
                )
        })
    }) {
        return Assembly::new(&entities)?.render(&meshes, &graph, budget);
    }
    let mut scene = Scene::default();
    for (&id, _) in shells {
        scene
            .triangles
            .extend(meshes.remove(&id).unwrap().triangles);
    }
    let mut decoder = step_faceted::Decoder::new(&entities);
    let mut polygon_shells: Vec<_> = owned_shells.into_iter().collect();
    polygon_shells.sort_unstable();
    for id in polygon_shells {
        check_graph(id, &graph, &mut HashSet::new(), &mut done, 0)?;
        let mesh = decoder
            .decode(id, &mut budget)?
            .ok_or("STEP polygon shell is unsupported")?;
        append_mesh(&mesh, Transform::IDENTITY, &mut scene, &mut budget)?;
    }
    scene.validate()?;
    Ok(scene)
}

#[cfg(test)]
mod tests {
    use super::*;
    use truck_modeling::{Point3, Vector3, builder};
    use truck_stepio::out::{CompleteStepDisplay, StepHeaderDescriptor, StepModel};

    fn cube_step() -> String {
        let origin = builder::vertex(Point3::new(2.0, 3.0, 4.0));
        let edge = builder::tsweep(&origin, Vector3::new(2.0, 0.0, 0.0));
        let face = builder::tsweep(&edge, Vector3::new(0.0, 3.0, 0.0));
        let cube = builder::tsweep(&face, Vector3::new(0.0, 0.0, 4.0)).compress();
        CompleteStepDisplay::new(StepModel::from(&cube), StepHeaderDescriptor::default())
            .to_string()
    }

    #[test]
    fn solid_step_tessellates_real_faces_and_preserves_bounds() {
        let scene = parse(&cube_step()).unwrap();
        assert!(scene.triangles.len() >= 12);
        assert!(scene.points.is_empty());
        for (component, expected) in [(0, (2.0, 4.0)), (1, (3.0, 6.0)), (2, (4.0, 8.0))] {
            let values: Vec<_> = scene
                .positions()
                .map(|p| [p.x, p.y, p.z][component])
                .collect();
            let min = values.iter().copied().fold(f64::INFINITY, f64::min);
            let max = values.iter().copied().fold(f64::NEG_INFINITY, f64::max);
            assert!((min - expected.0).abs() < 1e-8);
            assert!((max - expected.1).abs() < 1e-8);
        }
        let area: f64 = scene
            .triangles
            .iter()
            .map(|t| {
                (t.vertices[1] - t.vertices[0])
                    .cross(t.vertices[2] - t.vertices[0])
                    .length()
                    * 0.5
            })
            .sum();
        assert!((area - 52.0).abs() < 1e-7);
        let thumbnail = crate::render::render(
            &scene,
            &crate::render::RenderOptions {
                width: 64,
                height: 64,
                ..Default::default()
            },
        )
        .unwrap();
        assert!(thumbnail.pixels().filter(|pixel| pixel[3] > 0).count() > 100);
    }

    #[test]
    fn malformed_reference_and_assembly_inputs_fail() {
        let valid = cube_step();
        assert!(parse("not a STEP file").is_err());
        let point_only = "ISO-10303-21; HEADER; FILE_DESCRIPTION(('test'),'2;1'); FILE_NAME('','',(''),(''),'','',''); FILE_SCHEMA(('AUTOMOTIVE_DESIGN')); ENDSEC; DATA; #1=CARTESIAN_POINT('',(0.,0.,0.)); ENDSEC; END-ISO-10303-21;";
        assert!(parse(point_only).is_err());
        let assembly = valid.replace(
            "ENDSEC;\nEND-ISO",
            "#999999=MAPPED_ITEM('',#1,#1);\nENDSEC;\nEND-ISO",
        );
        assert!(parse(&assembly).is_err());
        let broken = valid.replacen("CARTESIAN_POINT", "UNKNOWN_GEOMETRY", 1);
        assert!(parse(&broken).is_err());
    }

    #[test]
    fn curved_step_cylinder_is_tessellated_as_a_surface() {
        let vertex = builder::vertex(Point3::new(1.0, 0.0, 0.0));
        let circle = builder::rsweep(
            &vertex,
            Point3::new(0.0, 0.0, 0.0),
            Vector3::unit_z(),
            truck_modeling::Rad(std::f64::consts::TAU),
        );
        let disk = builder::try_attach_plane(&[circle]).unwrap();
        let cylinder = builder::tsweep(&disk, Vector3::new(0.0, 0.0, 2.0)).compress();
        let text =
            CompleteStepDisplay::new(StepModel::from(&cylinder), StepHeaderDescriptor::default())
                .to_string();
        let scene = parse(&text).unwrap();
        assert!(scene.triangles.len() > 40);
        let area: f64 = scene
            .triangles
            .iter()
            .map(|t| {
                (t.vertices[1] - t.vertices[0])
                    .cross(t.vertices[2] - t.vertices[0])
                    .length()
                    * 0.5
            })
            .sum();
        assert!((area - 6.0 * std::f64::consts::PI).abs() < 0.3);
        assert!(scene.positions().all(|p| p.x.abs() <= 1.00001
            && p.y.abs() <= 1.00001
            && p.z >= -1e-6
            && p.z <= 2.000_001));
    }

    #[test]
    fn wide_thin_cylinder_uses_boundary_geometry_to_scale_tessellation() {
        let radius = 10_000.;
        let vertex = builder::vertex(Point3::new(radius, 0., 0.));
        let circle = builder::rsweep(
            &vertex,
            Point3::origin(),
            Vector3::unit_z(),
            truck_modeling::Rad(std::f64::consts::TAU),
        );
        let disk = builder::try_attach_plane(&[circle]).unwrap();
        let cylinder = builder::tsweep(&disk, Vector3::unit_z()).compress();
        let text =
            CompleteStepDisplay::new(StepModel::from(&cylinder), StepHeaderDescriptor::default())
                .to_string();
        let scene = parse(&text).unwrap();
        assert!(scene.triangles.len() < 2000);
        let area: f64 = scene
            .triangles
            .iter()
            .map(|t| {
                (t.vertices[1] - t.vertices[0])
                    .cross(t.vertices[2] - t.vertices[0])
                    .length()
                    * 0.5
            })
            .sum();
        let expected = std::f64::consts::TAU * radius * (radius + 1.);
        assert!((area - expected).abs() / expected < 0.03);
    }

    fn vertex_loop_step(surface: &str, vertex: [f64; 3]) -> String {
        format!(
            "ISO-10303-21; HEADER; FILE_DESCRIPTION(('analytic surface'),'2;1'); FILE_SCHEMA(('AUTOMOTIVE_DESIGN')); ENDSEC; DATA;\n#1=CLOSED_SHELL('',(#2));\n#2=ADVANCED_FACE('',(#3),#7,.T.);\n#3=FACE_BOUND('',#4,.T.);\n#4=VERTEX_LOOP('',#5);\n#5=VERTEX_POINT('',#6);\n#6=CARTESIAN_POINT('',({}, {}, {}));\n#7={surface};\n#8=AXIS2_PLACEMENT_3D('',#9,#10,#11);\n#9=CARTESIAN_POINT('',(1.,2.,3.));\n#10=DIRECTION('',(0.,0.,-1.));\n#11=DIRECTION('',(0.,1.,0.));\nENDSEC; END-ISO-10303-21;",
            vertex[0], vertex[1], vertex[2]
        )
    }

    #[test]
    fn sphere_and_torus_vertex_loops_preserve_surface_and_placement() {
        for (surface, vertex, area, bounds) in [
            (
                "SPHERICAL_SURFACE('',#8,5.)",
                [1.0, 2.0, 8.0],
                100.0 * std::f64::consts::PI,
                [(-4.0, 6.0), (-3.0, 7.0), (-2.0, 8.0)],
            ),
            (
                "TOROIDAL_SURFACE('',#8,3.,1.)",
                [1.0, 6.0, 3.0],
                12.0 * std::f64::consts::PI.powi(2),
                [(-3.0, 5.0), (-2.0, 6.0), (2.0, 4.0)],
            ),
        ] {
            let scene = parse(&vertex_loop_step(surface, vertex)).unwrap();
            assert!(scene.triangles.len() >= 4000);
            assert!(scene.points.is_empty());
            let observed_area: f64 = scene
                .triangles
                .iter()
                .map(|t| {
                    (t.vertices[1] - t.vertices[0])
                        .cross(t.vertices[2] - t.vertices[0])
                        .length()
                        * 0.5
                })
                .sum();
            assert!((observed_area - area).abs() / area < 0.005);
            for (axis, (expected_min, expected_max)) in bounds.into_iter().enumerate() {
                let values: Vec<_> = scene.positions().map(|p| [p.x, p.y, p.z][axis]).collect();
                let observed_min = values.iter().copied().fold(f64::INFINITY, f64::min);
                let observed_max = values.iter().copied().fold(f64::NEG_INFINITY, f64::max);
                assert!((observed_min - expected_min).abs() < 1e-8);
                assert!((observed_max - expected_max).abs() < 1e-8);
            }
        }
        assert!(
            parse(&vertex_loop_step(
                "TOROIDAL_SURFACE('',#8,1.,2.)",
                [1.0, 5.0, 3.0]
            ))
            .is_err()
        );
        assert!(
            parse(&vertex_loop_step(
                "SPHERICAL_SURFACE('',#8,5.)",
                [1.0, 2.0, 3.0]
            ))
            .is_err()
        );
    }

    #[test]
    fn cyclic_and_deep_references_are_rejected_without_recursing_forever() {
        let mut graph = HashMap::from([(1, vec![2]), (2, vec![1])]);
        assert!(check_graph(1, &graph, &mut HashSet::new(), &mut HashMap::new(), 0).is_err());
        graph = (0..100)
            .map(|id| (id, if id == 99 { vec![] } else { vec![id + 1] }))
            .collect();
        assert!(check_graph(0, &graph, &mut HashSet::new(), &mut HashMap::new(), 0).is_err());
        assert!(check_nesting(&format!("{}{}", "(".repeat(100), ")".repeat(100))).is_err());
    }
}