threers 0.0.7

Drop-in three.js replacement for Rust and WebAssembly (wgpu) — THREE.* API, scene graph, PBR, post-fx, mesh BVH, CSG, an OpenSCAD front end with a watertight exact-CSG kernel, headless render, and native media codecs
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
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
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
//! Composition — turning a stack of layers into one scene.
//!
//! A `.usda` file is not a scene; it is one *opinion* about a scene. What a
//! prim actually is comes from combining opinions across sublayers, references,
//! payloads, inherits, variants and specializes. That combining is what USD
//! calls composition, and it is the part that makes USD a scene description
//! rather than a mesh container.
//!
//! # Strength ordering
//!
//! Opinions are ranked, strongest first, by USD's **LIVRPS** order:
//!
//! | | | |
//! |---|---|---|
//! | **L** | local | what this layer stack says directly |
//! | **I** | inherits | a class this prim inherits from |
//! | **V** | variants | the selected variant's body |
//! | **R** | references | another prim, here or in another layer |
//! | **P** | payloads | a reference that may be left unloaded |
//! | **S** | specializes | a base whose opinions are always weakest |
//!
//! The first opinion found for a field wins; children are the union across all
//! of them. Specializes sits last for a reason worth knowing: a specialized
//! prim can be *refined* by anything that references it, which is the opposite
//! of inherits, where the class overrides its instances.
//!
//! # What this does not do
//!
//! There is no lazy stage, no prim index cached between edits and no
//! instancing — this composes eagerly into a flat layer. That is the same
//! thing `usdcat --flatten` produces, which is how the tests check it. A
//! population mask is applied afterwards rather than avoided up front, so it
//! narrows what you get without saving the work of composing it.

use std::collections::{HashMap, HashSet};
use std::rc::Rc;

use super::clips;
use super::expr;
use super::parse::{Specifier, UsdLayer, UsdPrim, UsdProperty};
use super::value::{UsdReference, UsdValue};
use super::UsdError;

/// Where layers come from.
///
/// Composition reaches other files by name, and what a name means is the
/// caller's business: a directory on disk, the entries of a `.usdz`, a bundle
/// of bytes held in memory, or a server. Nothing here opens a file itself.
pub trait AssetResolver {
    /// Read the layer named by `path`, relative to `anchor` — the layer doing
    /// the asking, so a relative path resolves the way USD resolves it.
    fn resolve(&self, anchor: &str, path: &str) -> Option<Vec<u8>>;
}

/// A resolver over the local filesystem, anchored at each layer's own
/// directory.
#[cfg(not(target_arch = "wasm32"))]
pub struct FileResolver;

#[cfg(not(target_arch = "wasm32"))]
impl AssetResolver for FileResolver {
    fn resolve(&self, anchor: &str, path: &str) -> Option<Vec<u8>> {
        std::fs::read(resolve_path(anchor, path)).ok()
    }
}

/// A resolver over layers already in memory, keyed by name.
///
/// This is what a `.usdz` is: an archive whose entries reference each other by
/// the names they have inside it.
#[derive(Default)]
pub struct MemoryResolver {
    layers: HashMap<String, Vec<u8>>,
}

impl MemoryResolver {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn insert(&mut self, name: impl Into<String>, data: impl Into<Vec<u8>>) -> &mut Self {
        self.layers.insert(name.into(), data.into());
        self
    }
}

impl AssetResolver for MemoryResolver {
    fn resolve(&self, anchor: &str, path: &str) -> Option<Vec<u8>> {
        // An exact name first, then the same name resolved against the layer
        // that asked — an archive holds both spellings in practice.
        self.layers
            .get(path)
            .or_else(|| self.layers.get(&resolve_path(anchor, path)))
            .cloned()
    }
}

/// An archive resolves the layers inside itself.
///
/// This is what a `.usdz` is for: not one file but a set of them that reference
/// each other by the names they carry inside the zip.
impl AssetResolver for super::UsdzArchive {
    fn resolve(&self, anchor: &str, path: &str) -> Option<Vec<u8>> {
        let resolved = resolve_path(anchor, path);
        self.entries
            .iter()
            .find(|e| e.name == path || e.name == resolved)
            .map(|e| e.data.clone())
    }
}

/// Resolve `path` against the directory holding `anchor`.
///
/// Absolute paths and paths with no anchor are returned as they are.
pub fn resolve_path(anchor: &str, path: &str) -> String {
    if path.starts_with('/') || anchor.is_empty() {
        return path.to_string();
    }
    let directory = match anchor.rfind('/') {
        Some(at) => &anchor[..at + 1],
        None => return path.to_string(),
    };
    // Fold away `./` and `../` so two spellings of one layer are one layer.
    let joined_raw = format!("{directory}{path}");
    let mut parts: Vec<&str> = Vec::new();
    for part in joined_raw.split('/') {
        match part {
            "." | "" => {}
            ".." => {
                parts.pop();
            }
            other => parts.push(other),
        }
    }
    let joined = parts.join("/");
    if directory.starts_with('/') {
        format!("/{joined}")
    } else {
        joined
    }
}

/// What to compose, and how much of it.
pub struct ComposeOptions {
    /// Whether payloads are loaded. USD's point in distinguishing them from
    /// references is that a payload can be left out, so a scene too big to
    /// open can still be opened.
    pub load_payloads: bool,
    /// Variant choices to apply where the layers themselves select none, as
    /// `(set name, choice)`. An authored selection always wins.
    pub variant_fallbacks: Vec<(String, String)>,
    /// How deep composition may recurse before giving up. Cycles are detected
    /// exactly; this is the backstop for a chain that is merely absurd.
    pub max_depth: usize,
}

impl Default for ComposeOptions {
    fn default() -> Self {
        Self {
            load_payloads: true,
            variant_fallbacks: Vec::new(),
            max_depth: 64,
        }
    }
}

/// Compose a layer and everything it reaches into a single flat layer.
///
/// The result is what USD calls a flattened stage: every arc resolved, every
/// opinion merged, one prim per path.
pub fn compose(
    root: &UsdLayer,
    root_name: &str,
    resolver: &dyn AssetResolver,
    options: &ComposeOptions,
) -> Result<UsdLayer, UsdError> {
    let mut engine = Engine {
        resolver,
        options,
        cache: HashMap::new(),
        stacks: HashMap::new(),
        variables: expr::Variables::new(),
    };

    // The layer stack: the root plus its sublayers, strongest first.
    let stack = engine.stack_for(root, root_name);

    // Expression variables come from the whole stack before anything is
    // resolved, since an asset path in a weak layer may name a variable the
    // strongest one sets.
    for layer in stack.iter().rev() {
        engine
            .variables
            .extend(expr::variables_of(&layer.layer.metadata));
    }

    let mut out = UsdLayer::default();
    // Layer metadata takes the strongest opinion, as everything else does.
    for layer in stack.iter() {
        for (key, value) in &layer.layer.metadata {
            // The sublayers themselves are consumed here, and so is the
            // offset list that goes with them.
            if matches!(bare_key(key), "subLayers" | "subLayerOffsets") {
                continue;
            }
            if !out.metadata.iter().any(|(k, _)| k == key) {
                out.metadata.push((key.clone(), value.clone()));
            }
        }
    }

    // Relocations, which rename prims the arcs brought in. They are stated as
    // whole paths and applied to the composed result, so they are gathered
    // before anything is built.
    let relocations = relocations_of(&stack);

    // Every prim name across the stack, in the order the strongest layer that
    // mentions it puts them.
    let names = ordered_names(stack.iter().map(|l| &l.layer.prims));
    for name in names {
        let sources: Vec<Source> = stack
            .iter()
            .filter_map(|l| {
                l.layer.prims.iter().find(|p| p.name == name).map(|prim| {
                    let mut prim = prim.clone();
                    if !l.time.is_identity() {
                        shift_times(&mut prim, l.time);
                    }
                    Source {
                        prim,
                        anchor: l.name.clone(),
                        stack: stack.clone(),
                        time: l.time,
                    }
                })
            })
            .collect();
        if let Some(prim) = engine.compose_prim(&name, &sources, &mut HashSet::new(), 0) {
            out.prims.push(prim);
        }
    }

    for (from, to) in &relocations {
        relocate(&mut out, from, to);
    }
    Ok(out)
}

/// The relocations a layer stack authors, strongest layer first.
///
/// A relocation renames a prim that an arc brought in — the asset calls it
/// `OldName`, this stage calls it `NewName` — and everything that refers to it
/// by the new name then finds it. USD keeps the statement in the flattened
/// result as well as acting on it, since it still describes what happened.
fn relocations_of(stack: &[StackLayer]) -> Vec<(String, String)> {
    let mut out: Vec<(String, String)> = Vec::new();
    for layer in stack {
        let Some(UsdValue::Dict(entries)) = layer.layer.meta("relocates") else {
            continue;
        };
        for (from, to) in entries {
            let Some(to) = to.as_str() else { continue };
            // A stronger layer's relocation of the same path wins.
            if !out.iter().any(|(have, _)| have == from) {
                out.push((from.clone(), to.to_string()));
            }
        }
    }
    out
}

/// Move a prim from one path to another within a composed layer.
///
/// Only a rename among siblings is handled here — `</A/Old>` to `</A/New>` —
/// which is what a relocation is for. Moving a prim to a different parent is
/// syntactically expressible and means restructuring the tree; refusing it is
/// better than doing half of it.
fn relocate(layer: &mut UsdLayer, from: &str, to: &str) {
    let (from_parent, from_name) = match from.rsplit_once('/') {
        Some(split) => split,
        None => return,
    };
    let (to_parent, to_name) = match to.rsplit_once('/') {
        Some(split) => split,
        None => return,
    };
    if from_parent != to_parent {
        return;
    }

    let siblings = if from_parent.is_empty() {
        &mut layer.prims
    } else {
        match prim_at_mut(layer, from_parent) {
            Some(parent) => &mut parent.children,
            None => return,
        }
    };
    if let Some(prim) = siblings.iter_mut().find(|p| p.name == from_name) {
        prim.name = to_name.to_string();
    }
}

fn prim_at_mut<'a>(layer: &'a mut UsdLayer, path: &str) -> Option<&'a mut UsdPrim> {
    let mut parts = path.trim_start_matches('/').split('/').filter(|p| !p.is_empty());
    let first = parts.next()?;
    let mut current = layer.prims.iter_mut().find(|p| p.name == first)?;
    for part in parts {
        current = current.children.iter_mut().find(|p| p.name == part)?;
    }
    Some(current)
}

/// One layer of a layer stack, with the name it was reached by.
struct StackLayer {
    layer: UsdLayer,
    name: String,
    /// How this layer's time codes map onto the root's. A sublayer may shift
    /// and stretch everything beneath it — the same map an arc applies, one
    /// level up — and the shifts compose down a chain of sublayers.
    time: LayerOffset,
}

/// One opinion about a prim, and where it came from.
///
/// The origin travels with the opinion rather than being taken from whoever is
/// asking, because an opinion pulled in through a reference resolves its *own*
/// arcs against its *own* layer. An `inherits = </Class>` inside a referenced
/// asset means a class in that asset, not one in the shot that referenced it.
#[derive(Clone)]
struct Source {
    prim: UsdPrim,
    anchor: String,
    stack: Rc<Vec<StackLayer>>,
    /// How this opinion's time codes map onto the root's, as
    /// `(offset, scale)`. An arc may shift and stretch the animation it pulls
    /// in — that is what lets one walk cycle start at frame 100 here and frame
    /// 340 there — and the shifts compose through nested arcs.
    time: LayerOffset,
}

/// `t * scale + offset`, the map an arc applies to the time codes beneath it.
#[derive(Clone, Copy, Debug, PartialEq)]
struct LayerOffset {
    offset: f64,
    scale: f64,
}

impl Default for LayerOffset {
    fn default() -> Self {
        Self {
            offset: 0.0,
            scale: 1.0,
        }
    }
}

impl LayerOffset {
    fn is_identity(&self) -> bool {
        self.offset == 0.0 && self.scale == 1.0
    }

    fn apply(&self, time: f64) -> f64 {
        time * self.scale + self.offset
    }

    /// This offset applied *after* `inner` — the map a nested arc ends up
    /// with, since the inner shift happens in the outer layer's frame.
    fn then(&self, inner: LayerOffset) -> LayerOffset {
        LayerOffset {
            offset: inner.offset * self.scale + self.offset,
            scale: inner.scale * self.scale,
        }
    }
}

/// Move every time sample in a subtree onto a new time line.
fn shift_times(prim: &mut UsdPrim, by: LayerOffset) {
    for property in &mut prim.properties {
        if let UsdValue::TimeSamples(samples) = &mut property.value {
            for (time, _) in samples.iter_mut() {
                *time = by.apply(*time);
            }
        }
    }
    for set in &mut prim.variant_sets {
        for (_, body) in &mut set.variants {
            shift_times(body, by);
        }
    }
    for child in &mut prim.children {
        shift_times(child, by);
    }
}

struct Engine<'a> {
    resolver: &'a dyn AssetResolver,
    options: &'a ComposeOptions,
    /// Layers already opened, so a file referenced fifty times is parsed once.
    cache: HashMap<String, UsdLayer>,
    /// Layer stacks already built, keyed by the layer that roots them.
    stacks: HashMap<String, Rc<Vec<StackLayer>>>,
    /// The variables an expression may refer to. A stronger layer's value for
    /// a name wins, which is how one shot layer drives what a whole stage
    /// pulls in.
    variables: expr::Variables,
}

impl Engine<'_> {
    /// Open a layer by name, through the resolver, once.
    fn layer(&mut self, anchor: &str, path: &str) -> Option<UsdLayer> {
        // An asset path may be an expression. One that does not evaluate is
        // not opened at all: naming the wrong file would be worse than naming
        // none.
        let path = &expr::evaluate(path, &self.variables)?;
        let key = resolve_path(anchor, path);
        if let Some(found) = self.cache.get(&key) {
            return Some(found.clone());
        }
        let bytes = self.resolver.resolve(anchor, path)?;
        let layer = super::UsdLoader::parse_layer(&bytes).ok()?;
        self.cache.insert(key, layer.clone());
        Some(layer)
    }

    /// A layer and its sublayers, strongest first.
    ///
    /// `subLayers` is authored strongest-first, which is the opposite of how
    /// most people read a list, and getting it backwards silently inverts
    /// every override in a shot.
    fn layer_stack(
        &mut self,
        layer: &UsdLayer,
        name: &str,
        seen: &mut HashSet<String>,
        depth: usize,
    ) -> Vec<StackLayer> {
        if depth > self.options.max_depth || !seen.insert(resolve_path("", name)) {
            return Vec::new();
        }
        let mut out = vec![StackLayer {
            layer: layer.clone(),
            name: name.to_string(),
            time: LayerOffset::default(),
        }];
        for (key, value) in &layer.metadata {
            if bare_key(key) != "subLayers" {
                continue;
            }
            for (sub, offset) in sublayers_of(value) {
                let Some(opened) = self.layer(name, &sub) else {
                    continue;
                };
                let resolved = resolve_path(name, &sub);
                // A sublayer of a shifted sublayer is shifted by both.
                for mut inner in self.layer_stack(&opened, &resolved, seen, depth + 1) {
                    inner.time = offset.then(inner.time);
                    out.push(inner);
                }
            }
        }
        out
    }

    /// The layer stack rooted at a named layer, built once.
    fn stack_for(&mut self, layer: &UsdLayer, name: &str) -> Rc<Vec<StackLayer>> {
        let key = resolve_path("", name);
        if let Some(found) = self.stacks.get(&key) {
            return found.clone();
        }
        let built = Rc::new(self.layer_stack(layer, name, &mut HashSet::new(), 0));
        self.stacks.insert(key, built.clone());
        built
    }

    /// Merge every opinion about one prim, in LIVRPS order.
    fn compose_prim(
        &mut self,
        name: &str,
        sources: &[Source],
        visiting: &mut HashSet<String>,
        depth: usize,
    ) -> Option<UsdPrim> {
        if sources.is_empty() || depth > self.options.max_depth {
            return None;
        }

        // Variant selections resolve across the prim's *local* opinions before
        // any arc is followed, because the layer that selects a variant is
        // very often not the layer that authored the variant set.
        let mut selections: Vec<(String, String)> = Vec::new();
        for source in sources {
                for (set, choice) in selections_of(&source.prim) {
                // A selection may be an expression too, which is how a shot
                // picks a look for everything at once.
                let Some(choice) = expr::evaluate(&choice, &self.variables) else {
                    continue;
                };
                if !selections.iter().any(|(s, _)| *s == set) {
                    selections.push((set, choice));
                }
            }
        }
        for (set, choice) in &self.options.variant_fallbacks {
            if !selections.iter().any(|(s, _)| s == set) {
                selections.push((set.clone(), choice.clone()));
            }
        }

        // The full opinion list: each source, then whatever its arcs bring in,
        // weaker than the source that named them.
        //
        // The arcs are combined across the whole stack before any is followed,
        // because a list operation composes across layers: the shot that
        // deletes a reference and the asset that prepended it are different
        // opinions, and resolving each on its own means the delete never meets
        // what it was deleting.
        let mut opinions: Vec<Source> = sources.to_vec();
        self.expand_arcs(sources, &selections, &mut opinions, visiting, depth);

        // A prim exists only if something defines it: a stack of `over`s with
        // no `def` anywhere is an override of nothing.
        if !opinions.iter().any(|o| o.prim.specifier == Specifier::Def) {
            return None;
        }
        // `active = false` takes the prim off the stage, and everything under
        // it with it. It is how a shot switches a prop off without editing the
        // asset, so the strongest opinion is the one that decides.
        if let Some(UsdValue::Bool(false)) = opinions.iter().find_map(|o| o.prim.meta("active")) {
            return None;
        }
        let specifier = opinions
            .iter()
            .map(|o| o.prim.specifier)
            .find(|s| *s != Specifier::Over)
            .unwrap_or(Specifier::Def);

        let mut prim = UsdPrim {
            specifier,
            type_name: opinions
                .iter()
                .map(|o| o.prim.type_name.as_str())
                .find(|t| !t.is_empty())
                .unwrap_or_default()
                .to_string(),
            name: name.to_string(),
            metadata: Vec::new(),
            properties: Vec::new(),
            children: Vec::new(),
            variant_sets: Vec::new(),
        };

        // Metadata and properties: strongest opinion wins, field by field.
        for opinion in &opinions {
            for (key, value) in &opinion.prim.metadata {
                // The arcs are what composition *is* and do not survive into
                // the result. A `reorder` does survive — USD keeps it in a
                // flattened layer as well as acting on it, since it still
                // describes the order.
                if is_arc_field(key) {
                    continue;
                }
                if !prim.metadata.iter().any(|(k, _)| k == key) {
                    prim.metadata.push((key.clone(), value.clone()));
                }
            }
            for property in &opinion.prim.properties {
                match prim.properties.iter_mut().find(|p| p.name == property.name) {
                    // A weaker opinion still fills in what the stronger one
                    // left unsaid — a type, an interpolation, a value.
                    Some(existing) => fill_in(existing, property),
                    None => prim.properties.push(property.clone()),
                }
            }
        }

        if let Some(order) = opinions
            .iter()
            .find_map(|o| o.prim.meta_exact("reorder properties"))
            .map(UsdValue::flat_tokens)
        {
            prim.properties.sort_by_key(|property| {
                order
                    .iter()
                    .position(|o| *o == property.name.as_str())
                    .unwrap_or(usize::MAX)
            });
        }

        // Value clips, which supply the animation for attributes the prim
        // already has. They are resolved after the opinions are merged,
        // because what they apply to is the merged result.
        self.apply_clips(&mut prim, sources);

        // Children: the union, each composed from every opinion that has one,
        // and each carrying the origin of the opinion it was found in.
        let mut names = ordered_names(opinions.iter().map(|o| &o.prim.children));
        // `reorder nameChildren` says what order they compose in — the
        // strongest opinion that states one wins, and anything it does not
        // name keeps its place behind those it does.
        if let Some(order) = opinions
            .iter()
            .find_map(|o| o.prim.meta_exact("reorder nameChildren"))
            .map(UsdValue::flat_tokens)
        {
            names.sort_by_key(|name| {
                order
                    .iter()
                    .position(|o| *o == name.as_str())
                    .unwrap_or(usize::MAX)
            });
        }
        for child_name in names {
            let child_sources: Vec<Source> = opinions
                .iter()
                .filter_map(|o| {
                    o.prim
                        .children
                        .iter()
                        .find(|c| c.name == child_name)
                        .map(|prim| Source {
                            prim: prim.clone(),
                            anchor: o.anchor.clone(),
                            stack: o.stack.clone(),
                            time: o.time,
                        })
                })
                .collect();
            if let Some(child) = self.compose_prim(&child_name, &child_sources, visiting, depth + 1)
            {
                prim.children.push(child);
            }
        }
        Some(prim)
    }

    /// Replace attribute values with what the clips hold, where a prim has
    /// them.
    ///
    /// Clips do not *create* attributes: a prim with nothing declared stays
    /// that way however many clips name it, which is why a topology layer
    /// usually sits underneath one.
    fn apply_clips(&mut self, prim: &mut UsdPrim, sources: &[Source]) {
        let sets = clips::sets_of(prim);
        if sets.is_empty() {
            return;
        }
        // Relative asset paths resolve against the layer that declared them.
        let anchor = sources
            .first()
            .map(|s| s.anchor.clone())
            .unwrap_or_default();

        for set in sets {
            if set.assets.is_empty() || set.prim_path.is_empty() {
                continue;
            }
            let manifest = (!set.manifest.is_empty())
                .then(|| self.layer(&anchor, &set.manifest))
                .flatten();
            let names = clips::animated_names(manifest.as_ref(), &set.prim_path, prim);

            // Opening a clip is not free, so each is opened once for the whole
            // set rather than once per attribute per frame.
            let layers: Vec<Option<UsdLayer>> = set
                .assets
                .iter()
                .map(|asset| self.layer(&anchor, asset))
                .collect();

            for name in names {
                // The times this attribute is sampled at, which without a
                // `times` table come from the clips themselves.
                let schedule = clips::sample_times(&set, |index| {
                    layers
                        .get(index)
                        .and_then(|l| l.as_ref())
                        .and_then(|l| l.prim_at(&set.prim_path))
                        .and_then(|p| p.value(&name))
                        .and_then(|v| v.samples().map(|s| s.iter().map(|(t, _)| *t).collect()))
                        .unwrap_or_default()
                });
                if schedule.is_empty() {
                    continue;
                }

                let mut samples = Vec::with_capacity(schedule.len());
                for (stage_time, clip_time) in schedule {
                    let Some(index) = set.clip_at(stage_time) else {
                        continue;
                    };
                    let Some(value) = layers
                        .get(index)
                        .and_then(|l| l.as_ref())
                        .and_then(|l| l.prim_at(&set.prim_path))
                        .and_then(|p| p.value(&name))
                    else {
                        continue;
                    };
                    samples.push((stage_time, value.at_time(clip_time)));
                }
                if samples.is_empty() {
                    continue;
                }
                // Only onto an attribute that exists.
                if let Some(property) = prim.properties.iter_mut().find(|p| p.name == name) {
                    property.value = UsdValue::TimeSamples(samples);
                }
            }
        }
    }

    /// Everything a stack of opinions' arcs contribute, appended weakest-last.
    fn expand_arcs(
        &mut self,
        sources: &[Source],
        selections: &[(String, String)],
        out: &mut Vec<Source>,
        visiting: &mut HashSet<String>,
        depth: usize,
    ) {
        if depth > self.options.max_depth {
            return;
        }

        // I — inherits.
        for (arc, origin) in combine_arcs(sources, "inherits") {
            self.follow(&sources[origin], &arc, selections, out, visiting, depth);
        }

        // V — variants, which are per-prim bodies rather than a shared list.
        for source in sources {
        for set in source.prim.variant_sets.clone() {
            let chosen = selections
                .iter()
                .find(|(s, _)| *s == set.name)
                .map(|(_, c)| c.clone())
                // With nothing selected anywhere, USD falls back to the first
                // variant authored.
                .or_else(|| set.variants.first().map(|(n, _)| n.clone()));
            let Some(body) = chosen.as_deref().and_then(|c| set.get(c)) else {
                continue;
            };
            let inner = Source {
                prim: body.clone(),
                anchor: source.anchor.clone(),
                stack: source.stack.clone(),
                time: source.time,
            };
            out.push(inner.clone());
            // A variant may itself carry arcs, and its own variant sets.
            self.expand_arcs(
                std::slice::from_ref(&inner),
                selections,
                out,
                visiting,
                depth + 1,
            );
        }
        }

        // R — references, then P — payloads, then S — specializes.
        for field in ["references", "payload", "specializes"] {
            if field == "payload" && !self.options.load_payloads {
                continue;
            }
            for (arc, origin) in combine_arcs(sources, field) {
                self.follow(&sources[origin], &arc, selections, out, visiting, depth);
            }
        }
    }

    /// Pull in what one arc points at.
    fn follow(
        &mut self,
        source: &Source,
        arc: &UsdReference,
        selections: &[(String, String)],
        out: &mut Vec<Source>,
        visiting: &mut HashSet<String>,
        depth: usize,
    ) {
        // A layer that references itself, directly or around a loop, would
        // otherwise recurse until the stack gives out.
        let key = format!(
            "{}|{}",
            resolve_path(&source.anchor, &arc.asset),
            arc.prim_path
        );
        if !visiting.insert(key.clone()) {
            return;
        }

        let (stack, anchor) = if arc.is_internal() {
            // An internal arc points inside the layer stack this opinion came
            // from — which is not necessarily the root's.
            (source.stack.clone(), source.anchor.clone())
        } else {
            let Some(opened) = self.layer(&source.anchor, &arc.asset) else {
                visiting.remove(&key);
                return;
            };
            let anchor = resolve_path(&source.anchor, &arc.asset);
            // The referenced layer brings its own sublayers with it.
            (self.stack_for(&opened, &anchor), anchor)
        };

        // Where to look inside it: the named prim, or the layer's default.
        let target_path = if !arc.prim_path.is_empty() {
            arc.prim_path.clone()
        } else {
            match stack.first().map(|l| &l.layer).and_then(default_prim_of) {
                Some(name) => format!("/{name}"),
                None => {
                    visiting.remove(&key);
                    return;
                }
            }
        };

        // The arc's own shift, on top of whatever is already in effect.
        let time = source.time.then(LayerOffset {
            offset: arc.offset,
            scale: arc.scale,
        });

        // Every layer of that stack that has an opinion about the target.
        let found: Vec<UsdPrim> = stack
            .iter()
            .filter_map(|l| l.layer.prim_at(&target_path).cloned())
            .collect();
        for mut target in found {
            if !time.is_identity() {
                shift_times(&mut target, time);
            }
            let inner = Source {
                prim: target,
                anchor: anchor.clone(),
                stack: stack.clone(),
                time,
            };
            // The target's own variant selections count too, weaker than
            // every one already made: an asset that selects `oak` for itself
            // gets oak unless whoever references it says otherwise. (A
            // `variant_fallbacks` entry is weaker still — USD falls back only
            // where nothing is authored.)
            let mut inner_selections = selections.to_vec();
            for (set, choice) in selections_of(&inner.prim) {
                let Some(choice) = expr::evaluate(&choice, &self.variables) else {
                    continue;
                };
                match inner_selections.iter().position(|(s, _)| *s == set) {
                    None => inner_selections.push((set, choice)),
                    Some(i) if self.is_fallback(&inner_selections[i]) => {
                        inner_selections[i] = (set, choice);
                    }
                    Some(_) => {}
                }
            }
            out.push(inner.clone());
            self.expand_arcs(
                std::slice::from_ref(&inner),
                &inner_selections,
                out,
                visiting,
                depth + 1,
            );
        }
        visiting.remove(&key);
    }

    /// Whether a selection is a `variant_fallbacks` entry rather than one a
    /// layer authored. (An authored selection that happens to name the
    /// fallback reads as one; it selects the same variant either way, so a
    /// weaker authored opinion replacing it is the only difference, and that
    /// is the one USD makes too.)
    fn is_fallback(&self, selection: &(String, String)) -> bool {
        self.options.variant_fallbacks.iter().any(|f| f == selection)
    }
}

/// The variant choices a prim's own metadata selects.
fn selections_of(prim: &UsdPrim) -> Vec<(String, String)> {
    let mut out = Vec::new();
    for (key, value) in &prim.metadata {
        if bare_key(key) != "variants" {
            continue;
        }
        if let UsdValue::Dict(entries) = value {
            for (set, choice) in entries {
                if let Some(choice) = choice.as_str() {
                    out.push((set.clone(), choice.to_string()));
                }
            }
        }
    }
    out
}

/// A metadata key without its list-operation qualifier.
fn bare_key(key: &str) -> &str {
    key.trim_start_matches("prepend ")
        .trim_start_matches("append ")
        .trim_start_matches("delete ")
        .trim_start_matches("add ")
}

fn default_prim_of(layer: &UsdLayer) -> Option<String> {
    layer
        .meta("defaultPrim")
        .and_then(|v| v.as_str())
        .map(str::to_string)
        .or_else(|| layer.prims.first().map(|p| p.name.clone()))
}

/// Whether a metadata key is a composition arc, which is consumed here rather
/// than carried through to the flattened result.
fn is_arc_field(key: &str) -> bool {
    let bare = key
        .trim_start_matches("prepend ")
        .trim_start_matches("append ")
        .trim_start_matches("delete ")
        .trim_start_matches("add ");
    matches!(
        bare,
        "references" | "payload" | "inherits" | "specializes" | "variants" | "variantSets"
    )
}

/// The arcs authored in one of a prim's composition fields, in the order they
/// take effect.
///
/// An arc has three spellings and all three mean something slightly different:
/// `@layer.usda@</Prim>` names a prim in another layer, `@layer.usda@` alone
/// means that layer's `defaultPrim`, and `</Prim>` alone is an internal arc to
/// this layer's own stack.
/// One field's arcs, composed across a stack of opinions.
///
/// A list operation is applied *over* the result of the weaker ones: an
/// explicit list replaces everything below it, `delete` removes, `prepend` goes
/// in front and `append` behind. Each arc is returned with the index of the
/// opinion that contributed it, because a relative asset path resolves against
/// the layer that named it and not against whichever layer happened to be
/// strongest.
fn combine_arcs(sources: &[Source], field: &str) -> Vec<(UsdReference, usize)> {
    let mut out: Vec<(UsdReference, usize)> = Vec::new();
    // Weakest first, so each stronger opinion is applied on top.
    for (index, source) in sources.iter().enumerate().rev() {
        let prim = &source.prim;
        let of = |key: &str| -> Vec<UsdReference> {
            let mut found = Vec::new();
            if let Some(value) = prim.meta_exact(key) {
                collect_arcs(value, &mut found);
            }
            found
        };

        // An explicit list says what the answer *is*, whatever was below.
        let explicit = of(field);
        let unqualified_is_explicit = prim.meta_exact(field).is_some();
        if unqualified_is_explicit {
            out = explicit.into_iter().map(|arc| (arc, index)).collect();
        }

        for arc in of(&format!("delete {field}")) {
            out.retain(|(have, _)| have.asset != arc.asset || have.prim_path != arc.prim_path);
        }
        // Prepended items go in front, keeping their own order.
        let prepended = of(&format!("prepend {field}"));
        for arc in prepended.into_iter().rev() {
            out.retain(|(have, _)| have.asset != arc.asset || have.prim_path != arc.prim_path);
            out.insert(0, (arc, index));
        }
        for arc in of(&format!("append {field}"))
            .into_iter()
            .chain(of(&format!("add {field}")))
        {
            out.retain(|(have, _)| have.asset != arc.asset || have.prim_path != arc.prim_path);
            out.push((arc, index));
        }
        // `reorder` moves what is there without adding or removing.
        let order = of(&format!("reorder {field}"));
        if !order.is_empty() {
            out.sort_by_key(|(arc, _)| {
                order
                    .iter()
                    .position(|o| o.asset == arc.asset && o.prim_path == arc.prim_path)
                    .unwrap_or(usize::MAX)
            });
        }
    }
    out
}

fn collect_arcs(value: &UsdValue, out: &mut Vec<UsdReference>) {
    match value {
        UsdValue::Reference(r) => out.push(r.clone()),
        // A layer with no prim named: its `defaultPrim`.
        UsdValue::Asset(a) => out.push(UsdReference::new(a.clone(), "")),
        // A path with no layer: inside this one.
        UsdValue::Path(p) => out.push(UsdReference::new("", p.clone())),
        UsdValue::Array(items) => {
            for item in items {
                collect_arcs(item, out);
            }
        }
        _ => {}
    }
}

/// The sublayers named by a value, each with the time offset it carries.
fn sublayers_of(value: &UsdValue) -> Vec<(String, LayerOffset)> {
    match value {
        UsdValue::Reference(r) => vec![(
            r.asset.clone(),
            LayerOffset {
                offset: r.offset,
                scale: r.scale,
            },
        )],
        UsdValue::Array(items) => items.iter().flat_map(sublayers_of).collect(),
        other => asset_paths(other)
            .into_iter()
            .map(|a| (a, LayerOffset::default()))
            .collect(),
    }
}

/// The asset paths in a `subLayers`-shaped value.
fn asset_paths(value: &UsdValue) -> Vec<String> {
    match value {
        UsdValue::Asset(a) => vec![a.clone()],
        UsdValue::String(s) => vec![s.clone()],
        UsdValue::Reference(r) => vec![r.asset.clone()],
        UsdValue::Array(items) => items.iter().flat_map(asset_paths).collect(),
        _ => Vec::new(),
    }
}

/// Names across several prim lists, strongest list first, each name once.
fn ordered_names<'a>(lists: impl Iterator<Item = &'a Vec<UsdPrim>>) -> Vec<String> {
    let mut out: Vec<String> = Vec::new();
    for list in lists {
        for prim in list {
            if !out.contains(&prim.name) {
                out.push(prim.name.clone());
            }
        }
    }
    out
}

/// Let a weaker opinion supply what a stronger one left unsaid.
fn fill_in(strong: &mut UsdProperty, weak: &UsdProperty) {
    if strong.type_name.is_empty() {
        strong.type_name = weak.type_name.clone();
    }
    // A declaration with nothing said takes what a weaker layer says. A
    // *block* does not: `foo = None` exists to stop the weaker opinion, and
    // filling it in from below would do the opposite of what it asks.
    if matches!(strong.value, UsdValue::None) && !matches!(weak.value, UsdValue::None) {
        strong.value = weak.value.clone();
    }
    strong.uniform |= weak.uniform;
    strong.relationship |= weak.relationship;
    for (key, value) in &weak.metadata {
        if !strong.metadata.iter().any(|(k, _)| k == key) {
            strong.metadata.push((key.clone(), value.clone()));
        }
    }
}

/// Narrow a composed layer to a set of prims, as USD's population mask does.
///
/// Each path keeps the prim it names, everything beneath it, and its ancestors
/// — and an ancestor keeps its own properties while losing the children that
/// lead nowhere. Masking on `/World/Keep` leaves `World` with its transform
/// intact, because the kept prim is still positioned by it, and drops
/// `World`'s other children entirely.
///
/// A path naming nothing still keeps the ancestors it does name: masking on
/// `/World/Keep/Missing` leaves `World` and `Keep` standing and drops `Keep`'s
/// real children. Which prims are ancestors is decided by the path asked for,
/// not by what is found along it.
///
/// An empty mask means the whole stage, matching `UsdStagePopulationMask::All`.
///
/// This prunes after composing rather than before, which is the same answer
/// USD gives but not the same saving: `usdcat --mask` never builds the parts
/// it discards, and this builds them and then throws them away.
pub fn mask(layer: &UsdLayer, paths: &[&str]) -> UsdLayer {
    if paths.is_empty() {
        return layer.clone();
    }
    let wanted: Vec<String> = paths
        .iter()
        .map(|p| {
            let p = p.trim().trim_end_matches('/');
            if p.starts_with('/') {
                p.to_string()
            } else {
                format!("/{p}")
            }
        })
        .filter(|p| p.len() > 1)
        .collect();
    let mut out = layer.clone();
    out.prims = layer
        .prims
        .iter()
        .filter_map(|prim| keep(prim, "", &wanted))
        .collect();
    out
}

/// One prim under the mask, or `None` if nothing at or below it is wanted.
fn keep(prim: &UsdPrim, parent: &str, wanted: &[String]) -> Option<UsdPrim> {
    let path = format!("{parent}/{}", prim.name);
    // At or below something asked for: the whole subtree comes through.
    if wanted
        .iter()
        .any(|w| path == *w || path.starts_with(&format!("{w}/")))
    {
        return Some(prim.clone());
    }
    // On the way to something asked for: this prim stays, with its own
    // properties, and keeps only the children that lead onwards.
    //
    // This is decided by the path asked for, not by what is found along it. A
    // mask naming a prim that does not exist still keeps the part of the chain
    // that does — `--mask /World/Keep/Missing` leaves `World` and `Keep`
    // standing with their properties and drops `Keep`'s actual children.
    // Pruning on "did any child survive" instead would empty the stage.
    let prefix = format!("{path}/");
    if !wanted.iter().any(|w| w.starts_with(&prefix)) {
        return None;
    }
    let mut kept = prim.clone();
    kept.children = prim
        .children
        .iter()
        .filter_map(|child| keep(child, &path, wanted))
        .collect();
    Some(kept)
}

#[cfg(test)]
mod tests {
    /// A population mask keeps what it names, what is under it, and the
    /// ancestors that position it — and nothing else.
    ///
    /// The expectations here are `usdcat --mask /World/Keep --flatten`:
    /// `World` survives *with its transform*, `Keep` and `Inner` survive
    /// whole, and `Drop` and its child are gone. Getting the ancestor rule
    /// wrong in either direction is easy — dropping `World` moves the geometry,
    /// keeping all of `World` defeats the mask.
    #[test]
    fn a_population_mask_keeps_ancestors_and_descendants() {
        let layer = super::super::parse::parse(include_str!("testdata/mask/src.usda")).unwrap();
        let masked = mask(&layer, &["/World/Keep"]);
        let paths: Vec<String> = masked
            .prims_by_path()
            .into_iter()
            .map(|(p, _)| p)
            .collect();
        assert_eq!(paths, ["/World", "/World/Keep", "/World/Keep/Inner"]);
        assert!(
            masked.prim_at("/World").unwrap().value("xformOp:translate").is_some(),
            "the ancestor keeps the transform that places what was kept"
        );
    }

    /// Several paths union, and an empty mask is the whole stage.
    #[test]
    fn a_population_mask_unions_and_defaults_to_everything() {
        let layer = super::super::parse::parse(include_str!("testdata/mask/src.usda")).unwrap();
        let both: Vec<String> = mask(&layer, &["/World/Keep", "/Other"])
            .prims_by_path()
            .into_iter()
            .map(|(p, _)| p)
            .collect();
        assert_eq!(
            both,
            [
                "/World",
                "/World/Keep",
                "/World/Keep/Inner",
                "/Other",
                "/Other/Elsewhere"
            ]
        );
        assert_eq!(
            mask(&layer, &[]).prims_by_path().len(),
            layer.prims_by_path().len()
        );
    }

    /// A path naming nothing leaves an empty stage rather than the whole one.
    #[test]
    fn a_mask_that_matches_nothing_keeps_nothing() {
        let layer = super::super::parse::parse(include_str!("testdata/mask/src.usda")).unwrap();
        assert!(mask(&layer, &["/Nowhere"]).prims.is_empty());
    }

    /// A mask through a prim that does not exist keeps the chain that does.
    ///
    /// `--mask /World/Keep/Missing` leaves `World` and `Keep` standing, with
    /// their properties, and drops `Keep`'s real child — the ancestors are
    /// decided by the path asked for, not by what was found along it. Deciding
    /// instead on "did any child survive" empties the stage, which is what
    /// this crate did until `usdcat --mask` was asked.
    #[test]
    fn a_mask_through_a_missing_prim_keeps_the_chain_that_exists() {
        let layer = super::super::parse::parse(include_str!("testdata/mask/src.usda")).unwrap();
        let masked = mask(&layer, &["/World/Keep/Missing"]);
        let paths: Vec<String> = masked.prims_by_path().into_iter().map(|(p, _)| p).collect();
        assert_eq!(paths, ["/World", "/World/Keep"]);
        assert!(masked.prim_at("/World/Keep").unwrap().value("size").is_some());

        // And the same one level deeper, where the surviving chain is longer.
        let deep = mask(&layer, &["/World/Drop/Gone/Deeper"]);
        let paths: Vec<String> = deep.prims_by_path().into_iter().map(|(p, _)| p).collect();
        assert_eq!(paths, ["/World", "/World/Drop", "/World/Drop/Gone"]);
        assert!(deep.prim_at("/World/Drop/Gone").unwrap().value("radius").is_some());
    }

    use super::*;
    use super::super::parse::parse;
    use super::super::write::layer_to_usda;

    /// The three layers of `testdata/comp`, as an in-memory resolver.
    fn pipeline() -> MemoryResolver {
        let mut r = MemoryResolver::new();
        r.insert("asset.usda", include_str!("testdata/comp/asset.usda"))
            .insert("base.usda", include_str!("testdata/comp/base.usda"))
            .insert("shot.usda", include_str!("testdata/comp/shot.usda"));
        r
    }

    fn compose_shot(options: &ComposeOptions) -> UsdLayer {
        let root = parse(include_str!("testdata/comp/shot.usda")).expect("the shot parses");
        compose(&root, "shot.usda", &pipeline(), options).expect("composes")
    }

    /// The whole of it, against what `usdcat --flatten` produced from the same
    /// three files: sublayers, a reference, an inherit, a variant selected in
    /// one layer and overridden in another, and `over` opinions merging.
    #[test]
    fn a_composed_stage_matches_what_openusd_flattens() {
        let ours = compose_shot(&ComposeOptions::default());
        let theirs = parse(include_str!("testdata/comp/flat.usda")).expect("the truth parses");

        let chair = ours
            .prim_at("/Room/Chair1")
            .expect("the referenced chair is there");
        let expected = theirs.prim_at("/Room/Chair1").unwrap();

        // The type came through the reference, from the asset.
        assert_eq!(chair.type_name, expected.type_name, "type name");
        assert_eq!(chair.type_name, "Xform");

        for property in &expected.properties {
            let mine = chair
                .value(&property.name)
                .unwrap_or_else(|| panic!("{} is missing", property.name));
            if let Some(text) = property.value.as_str() {
                assert_eq!(mine.as_str(), Some(text), "{}", property.name);
            } else {
                assert_eq!(
                    mine.flat_f32(),
                    property.value.flat_f32(),
                    "{}",
                    property.name
                );
            }
        }

        // And the child that came in with the reference.
        let seat = ours.prim_at("/Room/Chair1/Seat").expect("the seat");
        assert_eq!(seat.type_name, "Mesh");
        assert_eq!(seat.value("points").unwrap().flat_f32().len(), 9);
    }

    /// Every prim and every value, against OpenUSD's own flattening — not a
    /// sample of them. A composition engine that gets the common case right
    /// and quietly drops a prim is worse than one that fails loudly.
    #[test]
    fn nothing_is_missing_and_nothing_is_invented() {
        let ours = compose_shot(&ComposeOptions::default());
        let theirs = parse(include_str!("testdata/comp/flat.usda")).unwrap();

        let ours_paths: Vec<String> = ours.prims_by_path().into_iter().map(|(p, _)| p).collect();
        let theirs_paths: Vec<String> =
            theirs.prims_by_path().into_iter().map(|(p, _)| p).collect();
        assert_eq!(ours_paths, theirs_paths, "the set of prims differs");

        for (path, expected) in theirs.prims_by_path() {
            let mine = ours.prim_at(&path).unwrap();
            assert_eq!(mine.type_name, expected.type_name, "type of {path}");

            let mut mine_names: Vec<&str> =
                mine.properties.iter().map(|p| p.name.as_str()).collect();
            let mut expected_names: Vec<&str> =
                expected.properties.iter().map(|p| p.name.as_str()).collect();
            mine_names.sort();
            expected_names.sort();
            assert_eq!(mine_names, expected_names, "properties of {path}");

            for property in &expected.properties {
                let mine = mine.value(&property.name).unwrap();
                match property.value.as_str() {
                    Some(text) => assert_eq!(mine.as_str(), Some(text), "{path}.{}", property.name),
                    None => assert_eq!(
                        mine.flat_f32(),
                        property.value.flat_f32(),
                        "{path}.{}",
                        property.name
                    ),
                }
            }
        }
    }

    /// The same three-layer pipeline, composed from `.usdc` instead of
    /// `.usda`, must give the same answer.
    ///
    /// Composition arcs are stored in a crate under names a document never
    /// uses — `inheritPaths`, `variantSelection`, `variantSetNames` — and
    /// variant bodies live at their own paths rather than inside the prim, so
    /// a reader that handles the text form tells you nothing about this one.
    #[test]
    fn a_binary_pipeline_composes_to_the_same_thing_as_a_text_one() {
        let mut binary = MemoryResolver::new();
        binary
            .insert("asset.usda", include_bytes!("testdata/comp/asset.usdc").as_slice())
            .insert("base.usda", include_bytes!("testdata/comp/base.usdc").as_slice());
        let root = super::super::UsdLoader::parse_layer(include_bytes!(
            "testdata/comp/shot.usdc"
        ))
        .expect("the binary shot parses");
        let from_binary = compose(&root, "shot.usda", &binary, &ComposeOptions::default())
            .expect("the binary pipeline composes");

        let from_text = compose_shot(&ComposeOptions::default());
        // By meaning rather than by spelling: a document remembers that `10`
        // was written without a decimal point and a crate does not.
        let paths = |l: &UsdLayer| -> Vec<String> {
            l.prims_by_path().into_iter().map(|(p, _)| p).collect()
        };
        assert_eq!(paths(&from_binary), paths(&from_text), "different prims");
        for (path, expected) in from_text.prims_by_path() {
            let mine = from_binary.prim_at(&path).unwrap();
            assert_eq!(mine.type_name, expected.type_name, "type of {path}");
            for property in &expected.properties {
                let mine = mine
                    .value(&property.name)
                    .unwrap_or_else(|| panic!("{path}.{} missing", property.name));
                match property.value.as_str() {
                    Some(text) => assert_eq!(mine.as_str(), Some(text), "{path}.{}", property.name),
                    None => assert_eq!(
                        mine.flat_f32(),
                        property.value.flat_f32(),
                        "{path}.{}",
                        property.name
                    ),
                }
            }
        }
    }

    /// Every arc spelling survives a trip through the binary form: an asset
    /// with a prim path, an asset without one, an internal path, a payload,
    /// a specializes, and a layer offset.
    #[test]
    fn every_arc_spelling_survives_the_binary_form() {
        let from_text = parse(include_str!("testdata/comp/arcs.usda")).unwrap();
        let from_binary =
            super::super::UsdLoader::parse_layer(include_bytes!("testdata/comp/arcs.usdc"))
                .expect("parses");

        let a = from_binary.prim_at("/A").expect("A");
        let refs = a.meta("references").expect("references survived");
        let refs = refs.references();
        assert_eq!(refs.len(), 3, "{refs:?}");
        assert_eq!(refs[0].asset, "asset.usda");
        assert_eq!(refs[0].prim_path, "/Chair");
        assert_eq!(refs[1].asset, "other.usda");
        assert_eq!(refs[1].prim_path, "", "no prim path means the default prim");
        assert_eq!(refs[2].asset, "", "an internal arc has no asset");
        assert_eq!(refs[2].prim_path, "/A/Local");

        let payload = a.meta("payload").expect("payload survived").references();
        assert_eq!(payload.len(), 1);
        assert_eq!(payload[0].asset, "heavy.usda");
        assert_eq!(payload[0].prim_path, "/Deep");

        assert_eq!(
            a.meta("specializes").unwrap().references()[0].prim_path,
            "/A/Base"
        );

        // The layer offset, which is what lets a clip start somewhere else.
        let b = from_binary.prim_at("/B").unwrap().meta("references").unwrap();
        let b = b.references();
        assert_eq!((b[0].offset, b[0].scale), (10.0, 2.0));

        // And the text form agrees about all of it.
        let text_refs = from_text.prim_at("/A").unwrap().meta("references").unwrap();
        assert_eq!(text_refs.references().len(), 3);
    }

    /// Variant sets and the selection of one, read out of a crate file.
    #[test]
    fn variants_survive_the_binary_form() {
        let layer = super::super::UsdLoader::parse_layer(include_bytes!(
            "testdata/comp/asset.usdc"
        ))
        .unwrap();
        let chair = layer.prim_at("/Chair").expect("the chair");

        let set = chair
            .variant_sets
            .iter()
            .find(|s| s.name == "look")
            .expect("the look variant set");
        let mut names: Vec<&str> = set.variants.iter().map(|(n, _)| n.as_str()).collect();
        names.sort();
        assert_eq!(names, vec!["oak", "steel"]);

        // Each variant carries its own body.
        let oak = set.get("oak").expect("oak");
        assert_eq!(
            oak.value("primvars:displayColor").unwrap().flat_f32(),
            vec![0.6, 0.4, 0.2]
        );

        // The asset's own selection, under the name a document uses for it.
        let UsdValue::Dict(entries) = chair.meta("variants").expect("variants") else {
            panic!("expected a dictionary");
        };
        assert_eq!(entries[0].0, "look");
        assert_eq!(entries[0].1.as_str(), Some("oak"));

        // And the inherit, which a crate calls `inheritPaths`.
        assert_eq!(
            chair.meta("inherits").unwrap().references()[0].prim_path,
            "/Furniture"
        );
    }

    /// An arc may shift and stretch the animation it pulls in, and this crate
    /// read and wrote those numbers faithfully while ignoring them — so a clip
    /// referenced with `(offset = 100)` composed at the wrong time.
    ///
    /// Checked against `usdcat --flatten`, which maps a sample at `t` to
    /// `t * scale + offset`.
    #[test]
    fn an_arc_shifts_the_animation_it_pulls_in() {
        let mut r = MemoryResolver::new();
        r.insert("clip.usda", include_str!("testdata/comp/offset_clip.usda"));
        let root = parse(include_str!("testdata/comp/offset_use.usda")).unwrap();
        let out = compose(&root, "use.usda", &r, &ComposeOptions::default()).unwrap();

        let samples = out
            .prim_at("/Shifted")
            .expect("the referenced prim")
            .value("xformOp:translate")
            .expect("still animated")
            .samples()
            .expect("time samples")
            .to_vec();

        let theirs = parse(include_str!("testdata/comp/offset_flat.usda")).unwrap();
        let expected = theirs
            .prim_at("/Shifted")
            .unwrap()
            .value("xformOp:translate")
            .unwrap()
            .samples()
            .unwrap();

        assert_eq!(samples.len(), expected.len());
        for (mine, theirs) in samples.iter().zip(expected) {
            assert_eq!(mine.0, theirs.0, "sample time");
            assert_eq!(mine.1.flat_f32(), theirs.1.flat_f32(), "sample value");
        }
        // Explicitly: 0 and 10 became 100 and 120, not 100 and 110.
        assert_eq!(samples[0].0, 100.0);
        assert_eq!(samples[1].0, 120.0);
    }

    /// Shifts compose: an arc inside a shifted arc is shifted by both.
    #[test]
    fn nested_arcs_compose_their_shifts() {
        let mut r = MemoryResolver::new();
        r.insert("clip.usda", include_str!("testdata/comp/offset_clip.usda"))
            // Shifts the clip by 10 at double speed.
            .insert(
                "mid.usda",
                "#usda 1.0\n(\n    defaultPrim = \"Mid\"\n)\ndef Xform \"Mid\" (\n                     references = @clip.usda@ (offset = 10; scale = 2)\n)\n{\n}\n",
            );
        // ...and then shifts that by 100 at triple speed.
        let root = parse(
            "#usda 1.0\ndef Xform \"Top\" (\n    references = @mid.usda@ (offset = 100; scale = 3)\n)\n{\n}\n",
        )
        .unwrap();
        let out = compose(&root, "root.usda", &r, &ComposeOptions::default()).unwrap();
        let samples = out
            .prim_at("/Top")
            .unwrap()
            .value("xformOp:translate")
            .unwrap()
            .samples()
            .unwrap()
            .to_vec();

        // A sample at t lands at (t * 2 + 10) * 3 + 100.
        assert_eq!(samples[0].0, 130.0, "0 -> (0*2+10)*3+100");
        assert_eq!(samples[1].0, 190.0, "10 -> (10*2+10)*3+100");
    }

    /// `delete` takes an arc away, and `reorder` says what order what is
    /// left composes in. Both were parsed and then ignored: a deleted
    /// reference still applied, which is how a shot that drops a prop gets the
    /// prop anyway.
    #[test]
    fn delete_and_reorder_compose_as_openusd_does() {
        let mut r = MemoryResolver::new();
        r.insert("a.usda", include_str!("testdata/listops/a.usda"))
            .insert("b.usda", include_str!("testdata/listops/b.usda"))
            .insert("base.usda", include_str!("testdata/listops/base.usda"));
        let root = parse(include_str!("testdata/listops/shot.usda")).unwrap();
        let ours = compose(&root, "shot.usda", &r, &ComposeOptions::default()).unwrap();
        let theirs = parse(include_str!("testdata/listops/flat.usda")).unwrap();

        // The deleted reference is gone and the other one is not.
        let holder = ours.prim_at("/Root/Holder").expect("Holder");
        assert!(
            holder.value("fromA").is_none(),
            "the deleted reference still applied: {:?}",
            holder.properties
        );
        assert_eq!(holder.value("fromB").unwrap().flat_f32(), vec![2.0]);

        // Children compose in the order the reorder names.
        let order: Vec<&str> = ours
            .prim_at("/Root/Ordered")
            .unwrap()
            .children
            .iter()
            .map(|c| c.name.as_str())
            .collect();
        assert_eq!(order, vec!["Third", "First", "Second"]);

        // Which is what OpenUSD produced from the same files.
        let expected: Vec<&str> = theirs
            .prim_at("/Root/Ordered")
            .unwrap()
            .children
            .iter()
            .map(|c| c.name.as_str())
            .collect();
        assert_eq!(order, expected);
    }

    /// A reorder survives into the flattened layer as well as acting on it —
    /// it still describes the order, so USD keeps it and so does this.
    #[test]
    fn a_reorder_is_kept_as_well_as_applied() {
        let mut r = MemoryResolver::new();
        r.insert("base.usda", include_str!("testdata/listops/base.usda"));
        let root = parse(include_str!("testdata/listops/shot.usda")).unwrap();
        let ours = compose(&root, "shot.usda", &r, &ComposeOptions::default()).unwrap();
        assert!(
            ours.prim_at("/Root/Ordered")
                .unwrap()
                .meta_exact("reorder nameChildren")
                .is_some(),
            "the reorder statement was consumed rather than kept"
        );
    }

    /// The shot's `over` beats the base layer's opinion, which beats the
    /// asset's. Getting this backwards is the classic composition bug.
    #[test]
    fn the_strongest_layer_wins() {
        let ours = compose_shot(&ComposeOptions::default());
        assert_eq!(
            ours.prim_at("/Room/Chair1")
                .unwrap()
                .value("xformOp:translate")
                .unwrap()
                .flat_f32(),
            vec![9.0, 1.0, 0.0],
            "the shot's override should win over the base layer and the asset"
        );
    }

    /// The shot selects `steel`; the asset selects `oak`. The shot is stronger.
    #[test]
    fn a_variant_selection_can_be_overridden_from_a_stronger_layer() {
        let ours = compose_shot(&ComposeOptions::default());
        let colour = ours
            .prim_at("/Room/Chair1")
            .unwrap()
            .value("primvars:displayColor")
            .unwrap()
            .flat_f32();
        assert_eq!(colour, vec![0.7, 0.7, 0.75], "steel, not oak");
    }

    /// A referenced prim's own variant selection holds unless the prim that
    /// references it selects otherwise.
    #[test]
    fn a_referenced_prim_keeps_its_own_variant_selection() {
        let stage = |placement: &str| {
            let text = format!(
                "#usda 1.0\n\
                 class Xform \"Piece\" (\n\
                 \x20   prepend variantSets = \"finish\"\n\
                 \x20   variants = {{\n        string finish = \"enamel\"\n    }}\n\
                 )\n{{\n\
                 \x20   variantSet \"finish\" = {{\n\
                 \x20       \"brass\" {{\n            float weight = 1\n        }}\n\
                 \x20       \"enamel\" {{\n            float weight = 2\n        }}\n\
                 \x20   }}\n}}\n\
                 def Xform \"A\" (\n    prepend references = </Piece>{placement}\n)\n{{\n}}\n"
            );
            let root = parse(&text).unwrap();
            let composed =
                compose(&root, "root.usda", &MemoryResolver::new(), &ComposeOptions::default())
                    .unwrap();
            composed.prim_at("/A").unwrap().value("weight").unwrap().flat_f32()
        };
        assert_eq!(stage(""), vec![2.0], "the class's own selection, not the first variant");
        assert_eq!(
            stage("\n    variants = {\n        string finish = \"brass\"\n    }"),
            vec![1.0],
            "the placement's selection is stronger"
        );
    }

    /// A class contributes its opinions and is not itself part of the scene.
    #[test]
    fn an_inherited_class_contributes_but_does_not_appear() {
        let ours = compose_shot(&ComposeOptions::default());
        let chair = ours.prim_at("/Room/Chair1").unwrap();
        assert_eq!(chair.value("purpose").unwrap().as_str(), Some("render"));
        assert_eq!(chair.value("weight").unwrap().flat_f32(), vec![10.0]);
    }

    #[test]
    fn a_payload_can_be_left_unloaded() {
        let mut r = MemoryResolver::new();
        r.insert(
            "heavy.usda",
            "#usda 1.0\n(\n    defaultPrim = \"Heavy\"\n)\ndef Mesh \"Heavy\"\n{\n    int[] faceVertexCounts = [3]\n}\n",
        );
        let root = parse(
            "#usda 1.0\ndef Xform \"Holder\" (\n    payload = @heavy.usda@\n)\n{\n}\n",
        )
        .unwrap();

        let loaded = compose(&root, "root.usda", &r, &ComposeOptions::default()).unwrap();
        assert!(loaded.prim_at("/Holder").unwrap().value("faceVertexCounts").is_some());

        let unloaded = compose(
            &root,
            "root.usda",
            &r,
            &ComposeOptions {
                load_payloads: false,
                ..Default::default()
            },
        )
        .unwrap();
        let holder = unloaded.prim_at("/Holder").expect("the prim is still there");
        assert!(
            holder.value("faceVertexCounts").is_none(),
            "an unloaded payload should bring nothing in"
        );
    }

    /// A layer that references itself must stop rather than recurse forever.
    #[test]
    fn a_cycle_terminates() {
        let mut r = MemoryResolver::new();
        r.insert(
            "a.usda",
            "#usda 1.0\n(\n    defaultPrim = \"A\"\n)\ndef Xform \"A\" (\n    references = @b.usda@\n)\n{\n}\n",
        )
        .insert(
            "b.usda",
            "#usda 1.0\n(\n    defaultPrim = \"B\"\n)\ndef Xform \"B\" (\n    references = @a.usda@\n)\n{\n}\n",
        );
        let root = parse(include_str!("testdata/comp/asset.usda")).unwrap();
        let _ = compose(&root, "asset.usda", &r, &ComposeOptions::default());

        let a = parse("#usda 1.0\ndef Xform \"Top\" (\n    references = @a.usda@\n)\n{\n}\n").unwrap();
        let out = compose(&a, "root.usda", &r, &ComposeOptions::default()).unwrap();
        assert!(out.prim_at("/Top").is_some(), "it should still compose");
    }

    /// A sublayer that includes itself is the same hazard one level up.
    #[test]
    fn a_sublayer_cycle_terminates() {
        let mut r = MemoryResolver::new();
        r.insert(
            "loop.usda",
            "#usda 1.0\n(\n    subLayers = [\n        @loop.usda@\n    ]\n)\ndef Xform \"X\"\n{\n}\n",
        );
        let root = parse(
            "#usda 1.0\n(\n    subLayers = [\n        @loop.usda@\n    ]\n)\n",
        )
        .unwrap();
        let out = compose(&root, "root.usda", &r, &ComposeOptions::default()).unwrap();
        assert!(out.prim_at("/X").is_some());
    }

    #[test]
    fn paths_resolve_against_the_layer_that_named_them() {
        assert_eq!(resolve_path("shots/010/shot.usda", "asset.usda"), "shots/010/asset.usda");
        assert_eq!(resolve_path("shots/010/shot.usda", "../lib/chair.usda"), "shots/lib/chair.usda");
        assert_eq!(resolve_path("shots/010/shot.usda", "./here.usda"), "shots/010/here.usda");
        assert_eq!(resolve_path("shot.usda", "asset.usda"), "asset.usda");
        // Absolute stays absolute, and so does anything with no anchor.
        assert_eq!(resolve_path("shots/shot.usda", "/abs/a.usda"), "/abs/a.usda");
        assert_eq!(resolve_path("", "a.usda"), "a.usda");
    }

    /// Composition is idempotent: flattening a flat layer changes nothing.
    #[test]
    fn composing_a_flat_layer_is_a_no_op() {
        let layer = parse(include_str!("testdata/rich.usda")).unwrap();
        let composed = compose(
            &layer,
            "rich.usda",
            &MemoryResolver::new(),
            &ComposeOptions::default(),
        )
        .unwrap();
        assert_eq!(layer_to_usda(&composed), layer_to_usda(&layer));
    }
}

#[cfg(test)]
mod gaps {
    use super::*;
    use super::super::parse::parse;

    /// `foo = None` blocks a weaker layer's opinion. It is not the same as
    /// saying nothing: a declaration with no value takes what is below it, and
    /// treating a block that way does the opposite of what it asks.
    #[test]
    fn a_blocked_value_stays_blocked() {
        let mut r = MemoryResolver::new();
        r.insert("weak.usda", include_str!("testdata/block/weak.usda"));
        let root = parse(include_str!("testdata/block/strong.usda")).unwrap();
        let ours = compose(&root, "strong.usda", &r, &ComposeOptions::default()).unwrap();
        let theirs = parse(include_str!("testdata/block/flat.usda")).unwrap();

        let root_prim = ours.prim_at("/Root").expect("Root");
        assert_eq!(
            root_prim.value("blocked"),
            Some(&UsdValue::Block),
            "the block should have stopped the weaker `= 2`"
        );
        // Everything else still comes through from below.
        assert_eq!(root_prim.value("kept").unwrap().flat_f32(), vec![1.0]);
        assert_eq!(root_prim.value("stamp").unwrap().flat_f32(), vec![1.0, 2.0, 3.0]);
        // And a time code is a value like any other.
        assert_eq!(root_prim.value("when").unwrap().flat_f32(), vec![24.0]);

        // Which is the stage OpenUSD composes from the same files.
        assert_eq!(
            theirs.prim_at("/Root").unwrap().value("blocked"),
            Some(&UsdValue::Block)
        );
    }

    /// A variable in an asset path is how one shot layer drives which of
    /// several assets a whole stage pulls in.
    #[test]
    fn an_expression_in_an_asset_path_resolves() {
        let mut r = MemoryResolver::new();
        r.insert("red_asset.usda", include_str!("testdata/expr/red_asset.usda"))
            .insert("blue_asset.usda", include_str!("testdata/expr/blue_asset.usda"));
        let root = parse(include_str!("testdata/expr/expr.usda")).unwrap();
        let ours = compose(&root, "expr.usda", &r, &ComposeOptions::default()).unwrap();

        let thing = ours.prim_at("/Root/Thing").expect("the referenced prim");
        assert_eq!(
            thing.value("which").unwrap().flat_f32(),
            vec![1.0],
            "COLOR was red, so the red asset should have been pulled in"
        );

        // Which is what OpenUSD composes from the same files.
        let theirs = parse(include_str!("testdata/expr/flat.usda")).unwrap();
        assert_eq!(
            thing.value("which").unwrap().flat_f32(),
            theirs.prim_at("/Root/Thing").unwrap().value("which").unwrap().flat_f32()
        );
    }

    /// Change the variable and a different asset arrives, with nothing else
    /// edited — the whole point of the feature.
    #[test]
    fn changing_the_variable_changes_the_asset() {
        let mut r = MemoryResolver::new();
        r.insert("red_asset.usda", include_str!("testdata/expr/red_asset.usda"))
            .insert("blue_asset.usda", include_str!("testdata/expr/blue_asset.usda"));
        let root = parse(
            &include_str!("testdata/expr/expr.usda").replace("\"red\"", "\"blue\""),
        )
        .unwrap();
        let ours = compose(&root, "expr.usda", &r, &ComposeOptions::default()).unwrap();
        assert_eq!(
            ours.prim_at("/Root/Thing").unwrap().value("which").unwrap().flat_f32(),
            vec![2.0]
        );
    }

    /// An expression that cannot be evaluated opens nothing, rather than
    /// opening whatever the unevaluated text happens to name.
    #[test]
    fn an_unresolvable_expression_opens_nothing() {
        let mut r = MemoryResolver::new();
        r.insert("red_asset.usda", include_str!("testdata/expr/red_asset.usda"));
        let root = parse(
            r#"#usda 1.0
def Xform "Root"
{
    def Scope "Thing" (
        references = @`"${NOT_SET}_asset.usda"`@
    )
    {
    }
}
"#,
        )
        .unwrap();
        let ours = compose(&root, "expr.usda", &r, &ComposeOptions::default()).unwrap();
        assert!(
            ours.prim_at("/Root/Thing").unwrap().value("which").is_none(),
            "an undefined variable should not have resolved to anything"
        );
    }

    /// `active = false` takes a prim off the stage, and its children with it.
    /// It is how a shot switches a prop off without editing the asset.
    #[test]
    fn an_inactive_prim_is_not_on_the_stage() {
        let root = parse(include_str!("testdata/gaps/active.usda")).unwrap();
        let ours = compose(
            &root,
            "active.usda",
            &MemoryResolver::new(),
            &ComposeOptions::default(),
        )
        .unwrap();
        let theirs = parse(include_str!("testdata/gaps/flat_active.usda")).unwrap();

        assert!(ours.prim_at("/Root/Kept").is_some());
        assert!(
            ours.prim_at("/Root/Dropped").is_none(),
            "an inactive prim should be gone: {:?}",
            ours.prim_at("/Root").map(|p| p.children.iter().map(|c| &c.name).collect::<Vec<_>>())
        );
        assert!(ours.prim_at("/Root/Dropped/ChildOfDropped").is_none());

        // Which is the stage OpenUSD composes from the same file.
        let names = |l: &UsdLayer| -> Vec<String> {
            l.prims_by_path().into_iter().map(|(p, _)| p).collect()
        };
        assert_eq!(names(&ours), names(&theirs));
    }

    /// A sublayer may shift and stretch the time of everything in it — the
    /// same map an arc applies, one level up. It was read and written
    /// faithfully and then ignored, so a shot composed unshifted.
    #[test]
    fn a_sublayers_time_offset_applies() {
        let mut r = MemoryResolver::new();
        r.insert("anim_sub.usda", include_str!("testdata/gaps/anim_sub.usda"));
        let root = parse(include_str!("testdata/gaps/shifted.usda")).unwrap();
        let ours = compose(&root, "shifted.usda", &r, &ComposeOptions::default()).unwrap();

        let samples = ours
            .prim_at("/Moving")
            .expect("the prim came through the sublayer")
            .value("xformOp:translate")
            .expect("still animated")
            .samples()
            .expect("time samples")
            .to_vec();

        let theirs = parse(include_str!("testdata/gaps/flat_shifted.usda")).unwrap();
        let expected = theirs
            .prim_at("/Moving")
            .unwrap()
            .value("xformOp:translate")
            .unwrap()
            .samples()
            .unwrap();

        assert_eq!(samples.len(), expected.len());
        for (mine, theirs) in samples.iter().zip(expected) {
            assert_eq!(mine.0, theirs.0, "sample time");
        }
        // Spelled out: 0 and 10 became 100 and 120, not 100 and 110.
        assert_eq!(samples[0].0, 100.0);
        assert_eq!(samples[1].0, 120.0);
    }

    /// A relocation renames a prim an arc brought in: the asset calls it
    /// `OldName` and this stage calls it `NewName`.
    #[test]
    fn a_relocation_renames_what_an_arc_brought_in() {
        let mut r = MemoryResolver::new();
        r.insert("asset_r.usda", include_str!("testdata/gaps/asset_r.usda"));
        let root = parse(include_str!("testdata/gaps/reloc2.usda")).unwrap();
        let ours = compose(&root, "reloc2.usda", &r, &ComposeOptions::default()).unwrap();
        let theirs = parse(include_str!("testdata/gaps/flat_reloc2.usda")).unwrap();

        assert!(
            ours.prim_at("/Holder/OldName").is_none(),
            "the old name should be gone"
        );
        let renamed = ours
            .prim_at("/Holder/NewName")
            .expect("the prim should be under its new name");
        assert_eq!(renamed.value("marker").unwrap().flat_f32(), vec![7.0]);

        let names = |l: &UsdLayer| -> Vec<String> {
            l.prims_by_path().into_iter().map(|(p, _)| p).collect()
        };
        assert_eq!(names(&ours), names(&theirs));
    }

    /// A relocation that would move a prim to a different parent is not a
    /// rename, and doing half of it is worse than doing none.
    #[test]
    fn a_relocation_across_parents_is_refused() {
        let mut layer = parse(
            r#"#usda 1.0
def Scope "A"
{
    def Scope "Thing"
    {
    }
}

def Scope "B"
{
}
"#,
        )
        .unwrap();
        relocate(&mut layer, "/A/Thing", "/B/Thing");
        assert!(layer.prim_at("/A/Thing").is_some(), "left where it was");
        assert!(layer.prim_at("/B/Thing").is_none());
    }
}