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
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
#[cfg(test)]
mod registry_tests {
use crate::{
Registry, Transformable,
errors::RegistryError,
geometry::{Point, Quaternion, Transform, UNIT_NORM_TOLERANCE, Vector3},
time::{Stamp, Timestamp},
};
use approx::assert_abs_diff_eq;
use core::time::Duration;
#[test]
fn basic_chain_linear() {
let mut registry = Registry::new();
let t = Timestamp::from_nanos(1_000_000_000);
// Child frame B at x=1m without rotation
let t_a_b = Transform::new(
"a",
"b",
Vector3::new(1.0, 0.0, 0.0),
Quaternion::identity(),
Stamp::At(t),
)
.unwrap();
// Child frame C at y=1m
let t_b_c = Transform::new(
"b",
"c",
Vector3::new(0.0, 1.0, 0.0),
Quaternion::identity(),
Stamp::At(t),
)
.unwrap();
registry.add_transform(t_a_b.clone()).unwrap();
registry.add_transform(t_b_c.clone()).unwrap();
let t_a_c = Transform::new(
"a",
"c",
Vector3::new(1.0, 1.0, 0.0),
Quaternion::identity(),
Stamp::At(t),
)
.unwrap();
let r = registry.get_transform("a", "c", t_a_b.timestamp().at().unwrap());
assert!(r.is_ok(), "Registry returned Error, expected Ok");
assert_abs_diff_eq!(r.unwrap(), t_a_c);
}
#[test]
fn basic_chain_linear_reverse() {
let mut registry = Registry::new();
let t = Timestamp::from_nanos(1_000_000_000);
// Child frame B at x=1m without rotation
let t_a_b = Transform::new(
"a",
"b",
Vector3::new(1.0, 0.0, 0.0),
Quaternion::identity(),
Stamp::At(t),
)
.unwrap();
// Child frame C at y=1m
let t_b_c = Transform::new(
"b",
"c",
Vector3::new(0.0, 1.0, 0.0),
Quaternion::identity(),
Stamp::At(t),
)
.unwrap();
registry.add_transform(t_a_b.clone()).unwrap();
registry.add_transform(t_b_c.clone()).unwrap();
let t_c_a = Transform::new(
"c",
"a",
Vector3::new(-1.0, -1.0, 0.0),
Quaternion::identity(),
Stamp::At(t),
)
.unwrap();
let r = registry.get_transform("c", "a", t_a_b.timestamp().at().unwrap());
assert!(r.is_ok(), "Registry returned Error, expected Ok");
assert_abs_diff_eq!(r.unwrap(), t_c_a);
}
#[test]
fn basic_chain_rotation() {
let mut registry = Registry::new();
let t = Timestamp::from_nanos(1_000_000_000);
// Child frame B at x=1m without rotation
let t_a_b = Transform::new(
"a",
"b",
Vector3::new(1.0, 0.0, 0.0),
Quaternion::identity(),
Stamp::At(t),
)
.unwrap();
// Child frame C at +90 degrees
let theta = core::f64::consts::PI / 2.0;
let t_b_c = Transform::new(
"b",
"c",
Vector3::new(0.0, 0.0, 0.0),
Quaternion::from_wxyz((theta / 2.0).cos(), 0.0, 0.0, (theta / 2.0).sin()),
Stamp::At(t),
)
.unwrap();
// Child frame D at x=1m
let t_c_d = Transform::new(
"c",
"d",
Vector3::new(1.0, 0.0, 0.0),
Quaternion::identity(),
Stamp::At(t),
)
.unwrap();
registry.add_transform(t_a_b.clone()).unwrap();
registry.add_transform(t_b_c.clone()).unwrap();
registry.add_transform(t_c_d.clone()).unwrap();
let t_a_d = Transform::new(
"a",
"d",
Vector3::new(1.0, 1.0, 0.0),
Quaternion::from_wxyz((theta / 2.0).cos(), 0.0, 0.0, (theta / 2.0).sin()),
Stamp::At(t),
)
.unwrap();
let r = registry.get_transform("a", "d", t_a_b.timestamp().at().unwrap());
assert!(r.is_ok(), "Registry returned Error, expected Ok");
assert_abs_diff_eq!(r.unwrap(), t_a_d);
}
#[test]
fn basic_exact_match() {
let mut registry = Registry::new();
let t = Timestamp::from_nanos(1_000_000_000);
// Child frame B at x=1m without rotation
let t_a_b = Transform::new(
"a",
"b",
Vector3::new(1.0, 0.0, 0.0),
Quaternion::identity(),
Stamp::At(t),
)
.unwrap();
// Child frame C at y=1m with 90 degrees rotation around +Z
let theta = core::f64::consts::PI / 2.0;
let t_a_c = Transform::new(
"a",
"c",
Vector3::new(0.0, 1.0, 0.0),
Quaternion::from_wxyz((theta / 2.0).cos(), 0.0, 0.0, (theta / 2.0).sin()),
Stamp::At(t),
)
.unwrap();
registry.add_transform(t_a_b.clone()).unwrap();
registry.add_transform(t_a_c.clone()).unwrap();
let r = registry.get_transform("a", "b", t_a_b.timestamp().at().unwrap());
assert!(r.is_ok(), "Registry returned Error, expected Ok");
assert_abs_diff_eq!(r.unwrap(), t_a_b);
let r = registry.get_transform("a", "c", t_a_c.timestamp().at().unwrap());
assert!(r.is_ok(), "Registry returned Error, expected Ok");
assert_abs_diff_eq!(r.unwrap(), t_a_c);
}
#[test]
fn basic_interpolation() {
let mut registry = Registry::new();
let t = Timestamp::from_nanos(1_000_000_000);
// Child frame B at x=1m without rotation
let t_a_b_0 = Transform::new(
"a",
"b",
Vector3::new(1.0, 0.0, 0.0),
Quaternion::identity(),
Stamp::At(t),
)
.unwrap();
// Child frame B at y=1m with 90 degrees rotation around +Z
let theta = core::f64::consts::PI / 2.0;
let t_a_b_1 = Transform::new(
"a",
"b",
Vector3::new(0.0, 1.0, 0.0),
Quaternion::from_wxyz((theta / 2.0).cos(), 0.0, 0.0, (theta / 2.0).sin()),
Stamp::At((t + Duration::from_secs(1)).unwrap()),
)
.unwrap();
registry.add_transform(t_a_b_0.clone()).unwrap();
registry.add_transform(t_a_b_1.clone()).unwrap();
let middle_timestamp = Timestamp::from_nanos(u64::midpoint(
t_a_b_0.timestamp().at().unwrap().as_nanos(),
t_a_b_1.timestamp().at().unwrap().as_nanos(),
));
let t_a_b_2 = Transform::new(
"a",
"b",
(t_a_b_0.translation() + t_a_b_1.translation()) / 2.0,
t_a_b_0.rotation().slerp(t_a_b_1.rotation(), 0.5),
Stamp::At(middle_timestamp),
)
.unwrap();
let r = registry.get_transform("a", "b", middle_timestamp);
assert!(r.is_ok(), "Registry returned Error, expected Ok");
assert_abs_diff_eq!(r.unwrap(), t_a_b_2);
}
#[test]
fn basic_chained_interpolation() {
let mut registry = Registry::new();
let t = Timestamp::from_nanos(1_000_000_000);
// Child frame B at t=0, x=1m without rotation
let t_a_b_0 = Transform::new(
"a",
"b",
Vector3::new(1.0, 0.0, 0.0),
Quaternion::identity(),
Stamp::At(t),
)
.unwrap();
// Child frame B at t=1, x=2m without rotation
let t_a_b_1 = Transform::new(
"a",
"b",
Vector3::new(2.0, 0.0, 0.0),
Quaternion::identity(),
Stamp::At((t + Duration::from_secs(1)).unwrap()),
)
.unwrap();
// Child frame C at t=0, y=1m without rotation
let t_b_c_0 = Transform::new(
"b",
"c",
Vector3::new(0.0, 1.0, 0.0),
Quaternion::identity(),
Stamp::At(t),
)
.unwrap();
// Child frame C at t=1, y=2m without rotation
let t_b_c_1 = Transform::new(
"b",
"c",
Vector3::new(0.0, 2.0, 0.0),
Quaternion::identity(),
Stamp::At((t + Duration::from_secs(1)).unwrap()),
)
.unwrap();
registry.add_transform(t_a_b_0.clone()).unwrap();
registry.add_transform(t_a_b_1.clone()).unwrap();
registry.add_transform(t_b_c_0.clone()).unwrap();
registry.add_transform(t_b_c_1.clone()).unwrap();
let middle_timestamp = Timestamp::from_nanos(u64::midpoint(
t_a_b_0.timestamp().at().unwrap().as_nanos(),
t_a_b_1.timestamp().at().unwrap().as_nanos(),
));
let t_a_c = Transform::new(
"a",
"c",
Vector3::new(1.5, 1.5, 0.0),
Quaternion::identity(),
Stamp::At(middle_timestamp),
)
.unwrap();
let r = registry.get_transform("a", "c", middle_timestamp);
assert!(r.is_ok(), "Registry returned Error, expected Ok");
assert_abs_diff_eq!(r.unwrap(), t_a_c);
}
#[test]
fn basic_branch_navigation() {
let mut registry = Registry::new();
let t = Timestamp::from_nanos(1_000_000_000);
// Child frame B at t=0, y=1m without rotation
let t_a_b = Transform::new(
"a",
"b",
Vector3::new(0.0, 1.0, 0.0),
Quaternion::identity(),
Stamp::At(t),
)
.unwrap();
// Child frame C at t=0, x=1m without rotation
let t_b_c = Transform::new(
"b",
"c",
Vector3::new(1.0, 0.0, 0.0),
Quaternion::identity(),
Stamp::At(t),
)
.unwrap();
// Child frame D at t=0, x=2m without rotation
let t_b_d = Transform::new(
"b",
"d",
Vector3::new(2.0, 0.0, 0.0),
Quaternion::identity(),
Stamp::At(t),
)
.unwrap();
registry.add_transform(t_a_b).unwrap();
registry.add_transform(t_b_c).unwrap();
registry.add_transform(t_b_d).unwrap();
let result = registry.get_transform("c", "d", t);
assert!(result.is_ok());
let t_c_d = result.unwrap();
let t_c_d_expected = Transform::new(
"c",
"d",
Vector3::new(1.0, 0.0, 0.0),
Quaternion::identity(),
Stamp::At(t),
)
.unwrap();
assert_abs_diff_eq!(t_c_d, t_c_d_expected);
}
#[test]
fn basic_common_parent_elimination() {
let mut registry = Registry::new();
let t = Timestamp::from_nanos(1_000_000_000);
// Child frame B at t=0, y=1m without rotation
let t_a_b = Transform::new(
"a",
"b",
Vector3::new(0.0, 1.0, 0.0),
Quaternion::identity(),
Stamp::At(t),
)
.unwrap();
// Child frame C at t=0, x=1m without rotation
let t_b_c = Transform::new(
"b",
"c",
Vector3::new(1.0, 0.0, 0.0),
Quaternion::identity(),
Stamp::At(t),
)
.unwrap();
// Child frame D at t=0, x=2m without rotation
let t_b_d = Transform::new(
"b",
"d",
Vector3::new(2.0, 0.0, 0.0),
Quaternion::identity(),
Stamp::At(t),
)
.unwrap();
registry.add_transform(t_a_b).unwrap();
registry.add_transform(t_b_c).unwrap();
registry.add_transform(t_b_d).unwrap();
let mut walk_failure = None;
let target_chain =
Registry::get_transform_chain("d", "a", t, ®istry.data, &mut walk_failure);
let source_chain =
Registry::get_transform_chain("c", "a", t, ®istry.data, &mut walk_failure);
assert!(target_chain.is_some());
assert!(source_chain.is_some());
let mut target = target_chain.unwrap();
let mut source = source_chain.unwrap();
// Both walks climb through "b" to "a"; the shared "a -> b" hop is
// dropped, leaving one hop on each side.
Registry::truncate_at_common_parent(&mut target, &mut source);
assert_eq!(target.len(), 1);
assert_eq!(source.len(), 1);
let result = Registry::combine_transforms(target, source)
.expect("chains are non-empty")
.expect("combining the truncated chains must succeed");
assert_eq!(result.parent(), "d");
assert_eq!(result.child(), "c");
assert_eq!(result.translation(), Vector3::new(-1.0, 0.0, 0.0));
}
#[test]
fn a_deep_common_trunk_is_truncated_to_the_divergent_hops() {
// The same elimination as above, with a trunk deep enough for its
// removal to matter: both leaves sit four hops under the root and
// share three of them. Skipping the truncation still yields the
// right answer — it just composes the shared trunk up and back down
// again — so the chain lengths are the only place the work becomes
// visible, and on a deep tree that work is the whole lookup cost.
let mut registry = Registry::new();
let t = Timestamp::from_nanos(1_000_000_000);
let edges = [
("a", "b", Vector3::new(1.0, 0.0, 0.0)),
("b", "c", Vector3::new(0.0, 1.0, 0.0)),
("c", "d", Vector3::new(0.0, 0.0, 1.0)),
("d", "e", Vector3::new(1.0, 0.0, 0.0)),
("d", "f", Vector3::new(0.0, 2.0, 0.0)),
];
for (parent, child, translation) in edges {
registry
.add_transform(
Transform::new(
parent,
child,
translation,
Quaternion::identity(),
Stamp::At(t),
)
.unwrap(),
)
.unwrap();
}
let mut walk_failure = None;
let mut target =
Registry::get_transform_chain("e", "a", t, ®istry.data, &mut walk_failure).unwrap();
let mut source =
Registry::get_transform_chain("f", "a", t, ®istry.data, &mut walk_failure).unwrap();
assert_eq!(target.len(), 4);
assert_eq!(source.len(), 4);
Registry::truncate_at_common_parent(&mut target, &mut source);
assert_eq!(target.len(), 1);
assert_eq!(source.len(), 1);
let result = Registry::combine_transforms(target, source)
.expect("chains are non-empty")
.expect("combining the truncated chains must succeed");
assert_eq!(result.parent(), "e");
assert_eq!(result.child(), "f");
// "e" sits at x=1 under "d", "f" at y=2: "f" expressed in "e".
assert_eq!(result.translation(), Vector3::new(-1.0, 2.0, 0.0));
}
#[test]
fn time_travel_different_frames() {
// All three frames (fixed, target, source) are different, so both
// process_get_transform lookups are non-trivial (no identity shortcut).
//
// Tree: fixed -> a -> b
// At t1: a at x=1 in fixed, b at y=1 in a → b in fixed = (1,1,0)
// At t2: a at x=2 in fixed, b at y=2 in a → a in fixed = (2,0,0)
//
// get_transform_at("a", t2, "b", t1, "fixed")
// = "b-at-t1 expressed in a-at-t2"
// = inverse(a-in-fixed@t2) * (b-in-fixed@t1)
// = (-2,0,0) + (1,1,0) = (-1, 1, 0)
let mut registry = Registry::new();
let t1 = Timestamp::from_nanos(1_000_000_000);
let t2 = Timestamp::from_nanos(2_000_000_000);
// fixed -> a at t1: a is at x=1
registry
.add_transform(
Transform::new(
"fixed",
"a",
Vector3::new(1.0, 0.0, 0.0),
Quaternion::identity(),
Stamp::At(t1),
)
.unwrap(),
)
.unwrap();
// fixed -> a at t2: a has moved to x=2
registry
.add_transform(
Transform::new(
"fixed",
"a",
Vector3::new(2.0, 0.0, 0.0),
Quaternion::identity(),
Stamp::At(t2),
)
.unwrap(),
)
.unwrap();
// a -> b at t1: b is at y=1 relative to a
registry
.add_transform(
Transform::new(
"a",
"b",
Vector3::new(0.0, 1.0, 0.0),
Quaternion::identity(),
Stamp::At(t1),
)
.unwrap(),
)
.unwrap();
// a -> b at t2: b is at y=2 relative to a
registry
.add_transform(
Transform::new(
"a",
"b",
Vector3::new(0.0, 2.0, 0.0),
Quaternion::identity(),
Stamp::At(t2),
)
.unwrap(),
)
.unwrap();
let result = registry.get_transform_at(
"a", // target_frame
t2, // target_time
"b", // source_frame
t1, // source_time
"fixed", // fixed_frame
);
assert!(result.is_ok(), "get_transform_at failed: {result:?}");
let tf = result.unwrap();
assert!(
(tf.translation().x - (-1.0)).abs() < f64::EPSILON,
"Expected x=-1.0, got {}",
tf.translation().x
);
assert!(
(tf.translation().y - 1.0).abs() < f64::EPSILON,
"Expected y=1.0, got {}",
tf.translation().y
);
assert!(
tf.translation().z.abs() < f64::EPSILON,
"Expected z=0.0, got {}",
tf.translation().z
);
}
#[test]
fn time_travel_same_time() {
// When source_time == target_time, time travel should match get_transform.
// Uses target != fixed so both lookups are non-trivial.
//
// Tree: fixed -> a -> b, all at time t
let mut registry = Registry::new();
let t = Timestamp::from_nanos(1_000_000_000);
registry
.add_transform(
Transform::new(
"fixed",
"a",
Vector3::new(1.0, 0.0, 0.0),
Quaternion::identity(),
Stamp::At(t),
)
.unwrap(),
)
.unwrap();
registry
.add_transform(
Transform::new(
"a",
"b",
Vector3::new(0.0, 1.0, 0.0),
Quaternion::identity(),
Stamp::At(t),
)
.unwrap(),
)
.unwrap();
let regular = registry.get_transform("a", "b", t);
let time_travel = registry.get_transform_at("a", t, "b", t, "fixed");
assert!(regular.is_ok());
assert!(time_travel.is_ok());
let regular_tf = regular.unwrap();
let time_travel_tf = time_travel.unwrap();
assert_abs_diff_eq!(regular_tf.translation(), time_travel_tf.translation());
assert_abs_diff_eq!(regular_tf.rotation(), time_travel_tf.rotation());
}
#[test]
fn time_travel_with_rotation() {
// All three frames different, with rotation on the target frame.
//
// Tree: fixed -> a -> b
// At t1: a at (1,0,0) no rotation, b at (0.5,0,0) in a
// → b in fixed at t1 = (1.5, 0, 0)
// At t2: a at origin rotated 90° CCW around z, b at (0.5,0,0) in a
//
// get_transform_at("a", t2, "b", t1, "fixed")
// = "b-at-t1 expressed in a-at-t2"
// = inverse(a-in-fixed@t2) * (b-in-fixed@t1)
// a-in-fixed@t2 = {t: (0,0,0), R: 90°} → inverse = {t: (0,0,0), R: -90°}
// R(-90°) * (1.5, 0, 0) = (0, -1.5, 0)
let mut registry = Registry::new();
let t1 = Timestamp::from_nanos(1_000_000_000);
let t2 = Timestamp::from_nanos(2_000_000_000);
let theta = core::f64::consts::PI / 2.0;
// fixed -> a at t1: at (1,0,0), no rotation
registry
.add_transform(
Transform::new(
"fixed",
"a",
Vector3::new(1.0, 0.0, 0.0),
Quaternion::identity(),
Stamp::At(t1),
)
.unwrap(),
)
.unwrap();
// fixed -> a at t2: at origin, rotated 90° CCW around z
registry
.add_transform(
Transform::new(
"fixed",
"a",
Vector3::new(0.0, 0.0, 0.0),
Quaternion::from_wxyz((theta / 2.0).cos(), 0.0, 0.0, (theta / 2.0).sin()),
Stamp::At(t2),
)
.unwrap(),
)
.unwrap();
// a -> b at t1: b is at (0.5, 0, 0) relative to a
registry
.add_transform(
Transform::new(
"a",
"b",
Vector3::new(0.5, 0.0, 0.0),
Quaternion::identity(),
Stamp::At(t1),
)
.unwrap(),
)
.unwrap();
// a -> b at t2: b still at (0.5, 0, 0) relative to a
registry
.add_transform(
Transform::new(
"a",
"b",
Vector3::new(0.5, 0.0, 0.0),
Quaternion::identity(),
Stamp::At(t2),
)
.unwrap(),
)
.unwrap();
let result = registry.get_transform_at(
"a", // target_frame
t2, // target_time
"b", // source_frame
t1, // source_time
"fixed", // fixed_frame
);
assert!(
result.is_ok(),
"Time travel with rotation failed: {result:?}"
);
let tf = result.unwrap();
// b was at (1.5, 0, 0) in fixed at t1.
// a is at origin rotated 90° CCW at t2.
// In a's frame at t2: R(-90°) * (1.5, 0, 0) = (0, -1.5, 0)
assert!(
tf.translation().x.abs() < 1e-10,
"Expected x=0.0, got {}",
tf.translation().x
);
assert!(
(tf.translation().y - (-1.5)).abs() < 1e-10,
"Expected y=-1.5, got {}",
tf.translation().y
);
assert!(
tf.translation().z.abs() < 1e-10,
"Expected z=0.0, got {}",
tf.translation().z
);
}
#[test]
fn time_travel_branching_tree() {
// Tree is a <- fixed -> b (source and target on separate branches).
//
// At t1: fixed->a is (1,0,0), fixed->b is (0,1,0)
// → b in fixed at t1 = (0,1,0)
// At t2: fixed->a is (2,0,0), fixed->b is (0,2,0)
// → a in fixed at t2 = (2,0,0)
//
// get_transform_at("a", t2, "b", t1, "fixed")
// = "b-at-t1 expressed in a-at-t2"
// = inverse(a-in-fixed@t2) * (b-in-fixed@t1)
// = (-2,0,0) + (0,1,0) = (-2, 1, 0)
let mut registry = Registry::new();
let t1 = Timestamp::from_nanos(1_000_000_000);
let t2 = Timestamp::from_nanos(2_000_000_000);
// fixed -> a at t1
registry
.add_transform(
Transform::new(
"fixed",
"a",
Vector3::new(1.0, 0.0, 0.0),
Quaternion::identity(),
Stamp::At(t1),
)
.unwrap(),
)
.unwrap();
// fixed -> a at t2
registry
.add_transform(
Transform::new(
"fixed",
"a",
Vector3::new(2.0, 0.0, 0.0),
Quaternion::identity(),
Stamp::At(t2),
)
.unwrap(),
)
.unwrap();
// fixed -> b at t1
registry
.add_transform(
Transform::new(
"fixed",
"b",
Vector3::new(0.0, 1.0, 0.0),
Quaternion::identity(),
Stamp::At(t1),
)
.unwrap(),
)
.unwrap();
// fixed -> b at t2
registry
.add_transform(
Transform::new(
"fixed",
"b",
Vector3::new(0.0, 2.0, 0.0),
Quaternion::identity(),
Stamp::At(t2),
)
.unwrap(),
)
.unwrap();
let result = registry.get_transform_at(
"a", // target_frame
t2, // target_time
"b", // source_frame
t1, // source_time
"fixed", // fixed_frame
);
assert!(
result.is_ok(),
"Time travel with branching tree failed: {result:?}"
);
let tf = result.unwrap();
assert!(
(tf.translation().x - (-2.0)).abs() < f64::EPSILON,
"Expected x=-2.0, got {}",
tf.translation().x
);
assert!(
(tf.translation().y - 1.0).abs() < f64::EPSILON,
"Expected y=1.0, got {}",
tf.translation().y
);
assert!(
tf.translation().z.abs() < f64::EPSILON,
"Expected z=0.0, got {}",
tf.translation().z
);
}
#[test]
fn time_travel_source_equals_fixed_returns_inverted_target_leg() {
// "Where is the fixed/world origin relative to my platform now" —
// source_frame == fixed_frame, a routine time-travel query that must
// resolve to the inverse of the target leg, not error with
// SameFrameMultiplication.
let mut registry = Registry::new();
let t1 = Timestamp::from_nanos(1_000_000_000);
let t2 = Timestamp::from_nanos(2_000_000_000);
// fixed -> a at t1: a is at x=1; at t2: a has moved to x=2.
for (t, x) in [(t1, 1.0), (t2, 2.0)] {
registry
.add_transform(
Transform::new(
"fixed",
"a",
Vector3::new(x, 0.0, 0.0),
Quaternion::identity(),
Stamp::At(t),
)
.unwrap(),
)
.unwrap();
}
let result = registry.get_transform_at("a", t2, "fixed", t1, "fixed");
assert!(result.is_ok(), "get_transform_at failed: {result:?}");
let tf = result.unwrap();
// Inverse of fixed -> a at t2 (x=2): the origin sits at x=-2 in "a".
assert_eq!(tf.parent(), "a");
assert_eq!(tf.child(), "fixed");
assert_eq!(tf.timestamp(), Stamp::At(t2));
assert!(
(tf.translation().x - (-2.0)).abs() < f64::EPSILON,
"Expected x=-2.0, got {}",
tf.translation().x
);
}
#[test]
fn time_travel_target_equals_fixed_returns_source_leg() {
let mut registry = Registry::new();
let t1 = Timestamp::from_nanos(1_000_000_000);
let t2 = Timestamp::from_nanos(2_000_000_000);
// fixed -> a at t1: a is at x=1; at t2: a has moved to x=2.
for (t, x) in [(t1, 1.0), (t2, 2.0)] {
registry
.add_transform(
Transform::new(
"fixed",
"a",
Vector3::new(x, 0.0, 0.0),
Quaternion::identity(),
Stamp::At(t),
)
.unwrap(),
)
.unwrap();
}
let result = registry.get_transform_at("fixed", t2, "a", t1, "fixed");
assert!(result.is_ok(), "get_transform_at failed: {result:?}");
let tf = result.unwrap();
// The source leg alone: a at t1 (x=1), stamped with target_time.
assert_eq!(tf.parent(), "fixed");
assert_eq!(tf.child(), "a");
assert_eq!(tf.timestamp(), Stamp::At(t2));
assert!(
(tf.translation().x - 1.0).abs() < f64::EPSILON,
"Expected x=1.0, got {}",
tf.translation().x
);
}
#[test]
fn time_travel_all_frames_equal_returns_identity() {
let mut registry = Registry::new();
let t1 = Timestamp::from_nanos(1_000_000_000);
let t2 = Timestamp::from_nanos(2_000_000_000);
// The registry content is irrelevant for the degenerate query, but
// keep it non-empty to mirror real use.
registry
.add_transform(
Transform::new(
"fixed",
"a",
Vector3::new(1.0, 0.0, 0.0),
Quaternion::identity(),
Stamp::At(t1),
)
.unwrap(),
)
.unwrap();
let result = registry.get_transform_at("fixed", t2, "fixed", t1, "fixed");
assert!(result.is_ok(), "get_transform_at failed: {result:?}");
let tf = result.unwrap();
assert_eq!(tf.parent(), "fixed");
assert_eq!(tf.child(), "fixed");
assert_eq!(tf.timestamp(), Stamp::At(t2));
assert_eq!(tf.translation(), Vector3::zero());
assert_eq!(tf.rotation(), Quaternion::identity());
}
#[test]
fn get_transform_at_unknown_fixed_frame_returns_not_found() {
let mut registry = Registry::new();
let t1 = Timestamp::from_nanos(1_000_000_000);
let t2 = Timestamp::from_nanos(2_000_000_000);
// Tree: fixed -> a -> b, known at both times.
for &t in &[t1, t2] {
registry
.add_transform(
Transform::new(
"fixed",
"a",
Vector3::new(1.0, 0.0, 0.0),
Quaternion::identity(),
Stamp::At(t),
)
.unwrap(),
)
.unwrap();
registry
.add_transform(
Transform::new(
"a",
"b",
Vector3::new(0.0, 1.0, 0.0),
Quaternion::identity(),
Stamp::At(t),
)
.unwrap(),
)
.unwrap();
}
// The fixed frame is not part of the tree: neither leg of the time
// travel can resolve, so the whole query must fail loudly instead of
// silently picking another reference — naming the unknown frame.
let result = registry.get_transform_at("a", t2, "b", t1, "nowhere");
assert!(
matches!(&result, Err(RegistryError::UnknownFrame(frame)) if frame == "nowhere"),
"expected UnknownFrame for unknown fixed frame, got {result:?}"
);
}
#[test]
fn get_transform_at_missing_data_at_requested_times_returns_error() {
let mut registry = Registry::new();
let t1 = Timestamp::from_nanos(1_000_000_000);
let t2 = Timestamp::from_nanos(2_000_000_000);
let t3 = Timestamp::from_nanos(3_000_000_000);
// fixed -> a is known at t1 and t2; a -> b only at t1.
for &t in &[t1, t2] {
registry
.add_transform(
Transform::new(
"fixed",
"a",
Vector3::new(1.0, 0.0, 0.0),
Quaternion::identity(),
Stamp::At(t),
)
.unwrap(),
)
.unwrap();
}
registry
.add_transform(
Transform::new(
"a",
"b",
Vector3::new(0.0, 1.0, 0.0),
Quaternion::identity(),
Stamp::At(t1),
)
.unwrap(),
)
.unwrap();
// The source frame has no data at the requested source time: the
// b -> fixed leg cannot resolve at t2, and the error names "b" as
// the frame that could not serve the time.
let result = registry.get_transform_at("a", t1, "b", t2, "fixed");
assert!(
matches!(&result, Err(RegistryError::NotFoundAt { frame, .. }) if frame == "b"),
"expected NotFoundAt naming frame b for missing source data, got {result:?}"
);
// The target frame has no data at the requested target time: the
// a -> fixed leg cannot resolve at t3 (no extrapolation).
let result = registry.get_transform_at("a", t3, "b", t1, "fixed");
assert!(
matches!(&result, Err(RegistryError::NotFoundAt { frame, .. }) if frame == "a"),
"expected NotFoundAt naming frame a for missing target data, got {result:?}"
);
}
#[test]
fn get_transform_for_success_with_point() {
let mut registry = Registry::new();
let t = Timestamp::from_nanos(1_000_000_000);
registry
.add_transform(
Transform::new(
"map",
"camera",
Vector3::new(2.0, 0.0, 0.0),
Quaternion::identity(),
Stamp::At(t),
)
.unwrap(),
)
.unwrap();
let mut point = Point::new(
Vector3::new(1.0, 0.0, 0.0),
Quaternion::identity(),
t,
"camera",
);
let transform = registry.get_transform_for(&point, "map");
assert!(transform.is_ok(), "get_transform_for failed: {transform:?}");
let transform = transform.unwrap();
assert_eq!(transform.parent(), "map");
assert_eq!(transform.child(), "camera");
assert_eq!(transform.timestamp(), Stamp::At(t));
let result = point.transform(&transform);
assert!(result.is_ok(), "transform apply failed: {result:?}");
assert_eq!(point.frame, "map");
assert_eq!(point.timestamp, t);
assert_eq!(point.position, Vector3::new(3.0, 0.0, 0.0));
}
#[test]
fn get_transform_for_same_frame_returns_identity_on_empty_registry() {
let registry = Registry::new();
let t = Timestamp::from_nanos(1_000_000_000);
let mut point = Point::new(
Vector3::new(1.0, 2.0, 3.0),
Quaternion::identity(),
t,
"camera",
);
let transform = registry.get_transform_for(&point, "camera");
assert!(
transform.is_ok(),
"same-frame get_transform_for should be Ok: {transform:?}"
);
let transform = transform.unwrap();
assert_eq!(transform.parent(), "camera");
assert_eq!(transform.child(), "camera");
assert_eq!(transform.timestamp(), Stamp::At(t));
assert_eq!(transform.translation(), Vector3::new(0.0, 0.0, 0.0));
assert_eq!(transform.rotation(), Quaternion::identity());
let result = point.transform(&transform);
assert!(result.is_ok(), "identity apply failed: {result:?}");
assert_eq!(point.frame, "camera");
assert_eq!(point.position, Vector3::new(1.0, 2.0, 3.0));
}
#[test]
fn get_transform_for_propagates_lookup_error() {
let registry = Registry::new();
let t = Timestamp::from_nanos(1_000_000_000);
let point = Point::new(
Vector3::new(0.0, 0.0, 0.0),
Quaternion::identity(),
t,
"camera",
);
let result = registry.get_transform_for(&point, "map");
assert!(
matches!(&result, Err(RegistryError::UnknownFrame(frame)) if frame == "map"),
"expected UnknownFrame on an empty registry, got {result:?}"
);
}
#[test]
fn add_transform_rejects_cycles() {
let mut registry = Registry::new();
let t = Timestamp::from_nanos(1_000_000_000);
registry
.add_transform(
Transform::new(
"a",
"b",
Vector3::new(1.0, 0.0, 0.0),
Quaternion::identity(),
Stamp::At(t),
)
.unwrap(),
)
.unwrap();
// Two-frame cycle: a -> b already exists, so b -> a must be rejected.
let result = registry.add_transform(
Transform::new(
"b",
"a",
Vector3::new(0.0, 1.0, 0.0),
Quaternion::identity(),
Stamp::At(t),
)
.unwrap(),
);
assert!(matches!(result, Err(RegistryError::CycleDetected)));
// The direct lookup keeps working; the poisoning path is gone.
assert!(registry.get_transform("a", "b", t).is_ok());
// Three-frame cycle: extend the chain, then try to close it.
registry
.add_transform(
Transform::new(
"b",
"c",
Vector3::new(0.0, 1.0, 0.0),
Quaternion::identity(),
Stamp::At(t),
)
.unwrap(),
)
.unwrap();
let result = registry.add_transform(
Transform::new(
"c",
"a",
Vector3::new(0.0, 0.0, 1.0),
Quaternion::identity(),
Stamp::At(t),
)
.unwrap(),
);
assert!(matches!(result, Err(RegistryError::CycleDetected)));
}
#[test]
fn add_transform_rejects_self_referential_frames() {
let mut registry = Registry::new();
let result = registry.add_transform(
Transform::new(
"a",
"a",
Vector3::new(1.0, 0.0, 0.0),
Quaternion::identity(),
Stamp::At(Timestamp::from_nanos(1_000_000_000)),
)
.unwrap(),
);
assert!(matches!(result, Err(RegistryError::SelfReferentialFrame)));
}
#[test]
fn add_transform_rejects_reparenting() {
let mut registry = Registry::new();
let t1 = Timestamp::from_nanos(1_000_000_000);
let t2 = Timestamp::from_nanos(2_000_000_000);
registry
.add_transform(
Transform::new(
"world",
"object",
Vector3::new(1.0, 0.0, 0.0),
Quaternion::identity(),
Stamp::At(t1),
)
.unwrap(),
)
.unwrap();
// The object is "picked up": its parent changes. Not supported;
// the frame must be removed first.
let reparented = Transform::new(
"gripper",
"object",
Vector3::new(0.0, 0.5, 0.0),
Quaternion::identity(),
Stamp::At(t2),
)
.unwrap();
let result = registry.add_transform(reparented.clone());
assert!(matches!(
result,
Err(RegistryError::ReparentingNotSupported { current_parent }) if current_parent == "world"
));
// remove_frame is the escape hatch: after removal the new parent is
// accepted.
assert!(registry.remove_frame("object"));
assert!(!registry.remove_frame("object"));
registry.add_transform(reparented).unwrap();
assert!(registry.get_transform("gripper", "object", t2).is_ok());
assert!(registry.get_transform("world", "object", t1).is_err());
}
#[test]
fn remove_transforms_before_keeps_the_parent_pin() {
let mut registry = Registry::new();
let t1 = Timestamp::from_nanos(1_000_000_000);
let t2 = Timestamp::from_nanos(3_000_000_000);
registry
.add_transform(
Transform::new(
"world",
"object",
Vector3::new(1.0, 0.0, 0.0),
Quaternion::identity(),
Stamp::At(t1),
)
.unwrap(),
)
.unwrap();
// Regression test: draining a frame used to release it, so routine
// cleanup silently turned a rejected re-parenting into an accepted
// one and changed the topology behind the caller's back.
registry.remove_transforms_before(t2);
let reparented = Transform::new(
"gripper",
"object",
Vector3::new(0.0, 0.5, 0.0),
Quaternion::identity(),
Stamp::At(t2),
)
.unwrap();
let result = registry.add_transform(reparented.clone());
assert!(
matches!(
result,
Err(RegistryError::ReparentingNotSupported { ref current_parent }) if current_parent == "world"
),
"cleanup must not release the parent pin, got {result:?}"
);
// remove_frame remains the sole escape hatch, drained or not.
assert!(registry.remove_frame("object"));
registry.add_transform(reparented).unwrap();
assert!(registry.get_transform("gripper", "object", t2).is_ok());
}
#[test]
fn remove_transforms_before_keeps_the_buffer_kind() {
let mut registry = Registry::new();
let t1 = Timestamp::from_nanos(1_000_000_000);
let t2 = Timestamp::from_nanos(3_000_000_000);
registry
.add_transform(
Transform::new(
"world",
"object",
Vector3::new(1.0, 0.0, 0.0),
Quaternion::identity(),
Stamp::At(t1),
)
.unwrap(),
)
.unwrap();
// Regression test: draining a frame used to release its kind too, so
// a moving frame could become an eternal static one that answered
// confidently at times its data never covered.
registry.remove_transforms_before(t2);
let result = registry.add_transform(
Transform::new(
"world",
"object",
Vector3::new(0.0, 0.5, 0.0),
Quaternion::identity(),
Stamp::Static,
)
.unwrap(),
);
assert!(
matches!(result, Err(RegistryError::StaticDynamicConflict)),
"cleanup must not release the static/dynamic kind, got {result:?}"
);
}
#[test]
fn remove_transforms_before_leaves_drained_frames_diagnosable() {
let mut registry = Registry::new();
let t1 = Timestamp::from_nanos(1_000_000_000);
let t2 = Timestamp::from_nanos(3_000_000_000);
registry
.add_transform(
Transform::new(
"world",
"object",
Vector3::new(1.0, 0.0, 0.0),
Quaternion::identity(),
Stamp::At(t1),
)
.unwrap(),
)
.unwrap();
// A drained frame is known but empty. The lookup must say so —
// naming the frame that holds no data — instead of claiming the
// frame was never heard of, which reads as a publisher typo.
registry.remove_transforms_before(t2);
let result = registry.get_transform("world", "object", t2);
assert!(
matches!(
&result,
Err(RegistryError::NotFoundAt { frame, requested, covered, .. })
if frame == "object" && *requested == t2 && covered.is_none()
),
"expected NotFoundAt naming the drained frame, got {result:?}"
);
}
#[test]
fn get_transform_unknown_frame_returns_not_found() {
let mut registry = Registry::new();
let t = Timestamp::from_nanos(1_000_000_000);
registry
.add_transform(
Transform::new(
"a",
"b",
Vector3::new(1.0, 0.0, 0.0),
Quaternion::identity(),
Stamp::At(t),
)
.unwrap(),
)
.unwrap();
// The requested frame does not exist. The walk from "b" still resolves
// up to the root "a", but that partial answer must not be returned as
// if it were the requested transform; the error names the unknown
// frame.
let result = registry.get_transform("b", "does_not_exist", t);
assert!(
matches!(&result, Err(RegistryError::UnknownFrame(frame)) if frame == "does_not_exist"),
"expected UnknownFrame for unknown target frame, got {result:?}"
);
let result = registry.get_transform("does_not_exist", "b", t);
assert!(
matches!(&result, Err(RegistryError::UnknownFrame(frame)) if frame == "does_not_exist"),
"expected UnknownFrame for unknown source frame, got {result:?}"
);
}
#[test]
// The compared seconds are exactly representable; the assertion is on
// the reported values, not on float arithmetic.
#[allow(clippy::float_cmp)]
fn get_transform_partial_chain_reports_failing_frame() {
let mut registry = Registry::new();
let t0 = Timestamp::from_nanos(1_000_000_000);
let t1 = (t0 + Duration::from_secs(1)).unwrap();
// a -> b is only known at t0; b -> c is known at t0 and t1.
registry
.add_transform(
Transform::new(
"a",
"b",
Vector3::new(1.0, 0.0, 0.0),
Quaternion::identity(),
Stamp::At(t0),
)
.unwrap(),
)
.unwrap();
for &t in &[t0, t1] {
registry
.add_transform(
Transform::new(
"b",
"c",
Vector3::new(0.0, 1.0, 0.0),
Quaternion::identity(),
Stamp::At(t),
)
.unwrap(),
)
.unwrap();
}
// At t1 only the c -> b hop can be resolved; the chain to "a" is
// incomplete and must not be returned as a c -> a transform. The
// error pinpoints "b" as the frame that could not serve t1 and
// carries the covered range as the cause: b's data ends at t0
// (1.0s), one second before the requested t1 (2.0s).
let result = registry.get_transform("c", "a", t1);
assert!(
matches!(
&result,
Err(RegistryError::NotFoundAt { frame, requested, covered, .. })
if frame == "b" && *requested == t1 && *covered == Some((t0, t0))
),
"expected NotFoundAt naming frame b with the covered range, got {result:?}"
);
}
#[test]
fn get_transform_mid_chain_gap_reports_gap_frame() {
// Tree: r -> a -> b -> c. The a -> b hop is only known at t1; the
// others are known at t1 and t3. A query at t2 hits a timestamp gap
// in the MIDDLE of the chain, so both partial walks stop in
// different subtrees. That is a transient data gap and must be
// reported as NotFoundAt naming the gap frame — not
// IncompatibleFrames, whose "frames do not have a parent-child
// relationship" message is false here.
let mut registry = Registry::new();
let t1 = Timestamp::from_nanos(1_000_000_000);
let t2 = Timestamp::from_nanos(2_000_000_000);
let t3 = Timestamp::from_nanos(3_000_000_000);
for &t in &[t1, t3] {
registry
.add_transform(
Transform::new(
"r",
"a",
Vector3::new(1.0, 0.0, 0.0),
Quaternion::identity(),
Stamp::At(t),
)
.unwrap(),
)
.unwrap();
registry
.add_transform(
Transform::new(
"b",
"c",
Vector3::new(0.0, 0.0, 1.0),
Quaternion::identity(),
Stamp::At(t),
)
.unwrap(),
)
.unwrap();
}
registry
.add_transform(
Transform::new(
"a",
"b",
Vector3::new(0.0, 1.0, 0.0),
Quaternion::identity(),
Stamp::At(t1),
)
.unwrap(),
)
.unwrap();
// With all hops resolvable (t1) the chain works: the topology is
// intact and only the data gap at t2 must trip the lookup.
let result = registry.get_transform("a", "c", t1);
assert!(
result.is_ok(),
"expected chain at t1 to resolve: {result:?}"
);
let result = registry.get_transform("a", "c", t2);
assert!(
matches!(&result, Err(RegistryError::NotFoundAt { frame, .. }) if frame == "b"),
"expected NotFoundAt naming the gap frame b, got {result:?}"
);
}
#[test]
fn get_transform_disconnected_trees_returns_disconnected() {
// Two disjoint trees: r1 -> a and r2 -> b. There is no path between
// "a" and "b", which must be reported as Disconnected — not as a
// failed composition of the two unrelated root transforms, and not
// as a data gap: both frames exist and both walks complete cleanly,
// so the disconnection is a statement about the current topology.
let mut registry = Registry::new();
let t = Timestamp::from_nanos(1_000_000_000);
registry
.add_transform(
Transform::new(
"r1",
"a",
Vector3::new(1.0, 0.0, 0.0),
Quaternion::identity(),
Stamp::At(t),
)
.unwrap(),
)
.unwrap();
registry
.add_transform(
Transform::new(
"r2",
"b",
Vector3::new(0.0, 1.0, 0.0),
Quaternion::identity(),
Stamp::At(t),
)
.unwrap(),
)
.unwrap();
let result = registry.get_transform("a", "b", t);
assert!(
matches!(
&result,
Err(RegistryError::Disconnected { target_frame, source_frame })
if target_frame == "a" && source_frame == "b"
),
"expected Disconnected for frames in disconnected trees, got {result:?}"
);
}
#[test]
fn get_transform_unknown_frame_takes_precedence_over_data_gap() {
// a -> b holds data at t1 only. Querying b -> "nope" at t2 records
// a data gap during the walk AND asks for a frame that does not
// exist. The unknown frame is the more fundamental error — no
// amount of waiting for data can make the lookup succeed — so it
// must win the diagnosis.
let mut registry = Registry::new();
let t1 = Timestamp::from_nanos(1_000_000_000);
let t2 = Timestamp::from_nanos(2_000_000_000);
registry
.add_transform(
Transform::new(
"a",
"b",
Vector3::new(1.0, 0.0, 0.0),
Quaternion::identity(),
Stamp::At(t1),
)
.unwrap(),
)
.unwrap();
let result = registry.get_transform("b", "nope", t2);
assert!(
matches!(&result, Err(RegistryError::UnknownFrame(frame)) if frame == "nope"),
"expected UnknownFrame to take precedence over the data gap, got {result:?}"
);
}
#[test]
fn add_transform_rejects_static_dynamic_mixing() {
let t_dynamic = Timestamp::from_nanos(1_000_000_000);
let static_tf = Transform::new(
"a",
"b",
Vector3::new(1.0, 0.0, 0.0),
Quaternion::identity(),
Stamp::Static,
)
.unwrap();
let dynamic_tf = Transform::new(
"a",
"b",
Vector3::new(2.0, 0.0, 0.0),
Quaternion::identity(),
Stamp::At(t_dynamic),
)
.unwrap();
// Static first, then dynamic.
let mut registry = Registry::new();
registry.add_transform(static_tf.clone()).unwrap();
assert!(
matches!(
registry.add_transform(dynamic_tf.clone()),
Err(RegistryError::StaticDynamicConflict)
),
"dynamic insert into a static child frame must be rejected"
);
// Dynamic first, then static.
let mut registry = Registry::new();
registry.add_transform(dynamic_tf.clone()).unwrap();
assert!(
matches!(
registry.add_transform(static_tf),
Err(RegistryError::StaticDynamicConflict)
),
"static insert into a dynamic child frame must be rejected"
);
// The registry state is untouched by the rejected insert.
let result = registry.get_transform("a", "b", t_dynamic);
assert_eq!(result.unwrap(), dynamic_tf);
}
#[test]
fn remove_transforms_before_removes_old_dynamic_transforms() {
let mut registry = Registry::new();
let t1 = Timestamp::from_nanos(1_000_000_000);
let t2 = Timestamp::from_nanos(3_000_000_000);
for &t in &[t1, t2] {
registry
.add_transform(
Transform::new(
"a",
"b",
Vector3::new(1.0, 0.0, 0.0),
Quaternion::identity(),
Stamp::At(t),
)
.unwrap(),
)
.unwrap();
}
registry.remove_transforms_before(Timestamp::from_nanos(2_000_000_000));
assert!(
registry.get_transform("a", "b", t1).is_err(),
"transforms before the cutoff must be removed"
);
assert!(registry.get_transform("a", "b", t2).is_ok());
}
#[test]
fn remove_transforms_before_preserves_static_transforms() {
let mut registry = Registry::new();
let static_tf = Transform::new(
"base",
"lidar",
Vector3::new(0.5, 0.0, 0.0),
Quaternion::identity(),
Stamp::Static,
)
.unwrap();
registry.add_transform(static_tf.clone()).unwrap();
// The documented manual-cleanup workflow must not destroy static
// transforms: they are valid for all time.
registry.remove_transforms_before(Timestamp::from_nanos(5_000_000_000));
let query = Timestamp::from_nanos(9_000_000_000);
let result = registry.get_transform("base", "lidar", query).unwrap();
assert_eq!(
result.translation(),
static_tf.translation(),
"static transforms must survive manual cleanup"
);
// Lookup results carry the requested timestamp, not the static
// sentinel, so they compose with timestamped data.
assert_eq!(result.timestamp(), Stamp::At(query));
}
#[test]
fn mixed_static_dynamic_chain_resolves_and_interpolates() {
let mut registry = Registry::new();
// Static sensor mount: lidar sits 0.5 m ahead of base.
registry
.add_transform(
Transform::new(
"base",
"lidar",
Vector3::new(0.5, 0.0, 0.0),
Quaternion::identity(),
Stamp::Static,
)
.unwrap(),
)
.unwrap();
// Dynamic robot pose: base moves from x=1 to x=3 between t1 and t2.
let t1 = Timestamp::from_nanos(1_000_000_000);
let t2 = Timestamp::from_nanos(3_000_000_000);
for (t, x) in [(t1, 1.0), (t2, 3.0)] {
registry
.add_transform(
Transform::new(
"map",
"base",
Vector3::new(x, 0.0, 0.0),
Quaternion::identity(),
Stamp::At(t),
)
.unwrap(),
)
.unwrap();
}
// Query mid-way: the dynamic hop interpolates to x=2, the static hop
// contributes its fixed 0.5 offset, and the result carries the query
// timestamp.
let mid = Timestamp::from_nanos(2_000_000_000);
let result = registry.get_transform("map", "lidar", mid).unwrap();
assert_eq!(result.parent(), "map");
assert_eq!(result.child(), "lidar");
assert_eq!(result.timestamp(), Stamp::At(mid));
assert_abs_diff_eq!(result.translation(), Vector3::new(2.5, 0.0, 0.0));
}
#[test]
// The overflow assertion compares against f64::INFINITY, which is exact;
// stable clippy flags it where nightly no longer does.
#[allow(clippy::float_cmp)]
fn add_transform_rejects_a_republished_chain_that_left_validity() {
let mut registry = Registry::new();
let t = Timestamp::from_nanos(1_000_000_000);
// Both operands are valid: norm 1.000001 is inside
// UNIT_NORM_TOLERANCE — an f32-widened rotation, exactly what the
// tolerance exists to accept. Composition multiplies the norms and
// `*` deliberately does not re-check, so flattening an ordinary
// two-hop chain yields a rotation that is out of tolerance while
// looking entirely unremarkable.
let q = Quaternion::from_wxyz(1.0 + 1e-6, 0.0, 0.0, 0.0);
let t_a_b = Transform::new("a", "b", Vector3::new(1.0, 0.0, 0.0), q, Stamp::At(t)).unwrap();
let t_b_c = Transform::new("b", "c", Vector3::new(1.0, 0.0, 0.0), q, Stamp::At(t)).unwrap();
let flattened = (t_a_b * t_b_c).unwrap();
assert!(flattened.rotation().norm() > 1.0 + UNIT_NORM_TOLERANCE);
// Re-publishing it must fail. Stored, it would scale every vector
// every later lookup through the frame rotates, and report success.
assert!(matches!(
registry.add_transform(flattened),
Err(RegistryError::NonUnitRotation(_))
));
// The same for a translation that overflowed during composition.
let far = Transform::new(
"a",
"b",
Vector3::new(1.0e308, 0.0, 0.0),
Quaternion::identity(),
Stamp::At(t),
)
.unwrap();
let farther = Transform::new(
"b",
"c",
Vector3::new(1.0e308, 0.0, 0.0),
Quaternion::identity(),
Stamp::At(t),
)
.unwrap();
let overflowed = (far * farther).unwrap();
assert_eq!(overflowed.translation().x, f64::INFINITY);
assert!(matches!(
registry.add_transform(overflowed),
Err(RegistryError::NonFiniteValues)
));
// Nothing was stored, so no lookup can serve either value.
assert!(registry.get_transform("a", "c", t).is_err());
}
#[test]
fn with_max_age_expires_old_transforms_on_insert() {
let mut registry = Registry::with_max_age(Duration::from_secs(1));
let t1 = Timestamp::from_nanos(1_000_000_000);
let t2 = Timestamp::from_nanos(6_000_000_000);
for &t in &[t1, t2] {
registry
.add_transform(
Transform::new(
"a",
"b",
Vector3::new(1.0, 0.0, 0.0),
Quaternion::identity(),
Stamp::At(t),
)
.unwrap(),
)
.unwrap();
}
assert!(
registry.get_transform("a", "b", t1).is_err(),
"with_max_age registries must expire entries older than max_age"
);
assert!(registry.get_transform("a", "b", t2).is_ok());
// A registry without max_age keeps everything.
let mut registry = Registry::new();
for &t in &[t1, t2] {
registry
.add_transform(
Transform::new(
"a",
"b",
Vector3::new(1.0, 0.0, 0.0),
Quaternion::identity(),
Stamp::At(t),
)
.unwrap(),
)
.unwrap();
}
assert!(registry.get_transform("a", "b", t1).is_ok());
assert!(registry.get_transform("a", "b", t2).is_ok());
}
#[test]
fn failed_insert_does_not_bypass_cycle_detection() {
let mut registry = Registry::new();
let t = Timestamp::from_nanos(1_000_000_000);
// A rejected insert must not leave an empty frame behind in the
// registry map — here one rejected for naming "a" as its own parent,
// which still asks the registry to create the frame "a".
let invalid = Transform::new(
"a",
"a",
Vector3::new(1.0, 0.0, 0.0),
Quaternion::identity(),
Stamp::At(t),
)
.unwrap();
assert!(matches!(
registry.add_transform(invalid),
Err(RegistryError::SelfReferentialFrame)
));
registry
.add_transform(
Transform::new(
"a",
"b",
Vector3::new(1.0, 0.0, 0.0),
Quaternion::identity(),
Stamp::At(t),
)
.unwrap(),
)
.unwrap();
// ...otherwise this valid insert would close the cycle a <-> b
// without ever hitting the cycle check.
let result = registry.add_transform(
Transform::new(
"b",
"a",
Vector3::new(-1.0, 0.0, 0.0),
Quaternion::identity(),
Stamp::At(t),
)
.unwrap(),
);
assert!(matches!(result, Err(RegistryError::CycleDetected)));
// The stored transform still resolves, unpoisoned.
let result = registry.get_transform("a", "b", t).unwrap();
assert_eq!(result.translation(), Vector3::new(1.0, 0.0, 0.0));
}
#[test]
fn single_hop_lookup_returns_the_stored_transform_bit_for_bit() {
// In the documented direction — target is the source's parent — only
// the source-side chain is walked, and a one-element chain composes
// into itself: no inversion, no renormalization, no arithmetic at
// all. The answer is the stored sample, down to the last bit. The
// pre-rework path reached the same transform through two inversions
// and returned a translation up to ten ulps away from the stored one.
let mut registry = Registry::new();
let t = Timestamp::from_nanos(1_734_000_000_123_456_789);
let stored = Transform::new(
"map",
"lidar",
Vector3::new(0.1, -2.5, 3.75),
Quaternion::from_wxyz(0.3, -0.5, 0.7, 0.2)
.normalize()
.unwrap(),
Stamp::At(t),
)
.unwrap();
registry.add_transform(stored.clone()).unwrap();
let result = registry.get_transform("map", "lidar", t).unwrap();
assert_eq!(result.parent(), stored.parent());
assert_eq!(result.child(), stored.child());
assert_eq!(result.timestamp(), stored.timestamp());
for (got, expected) in [
(result.translation().x, stored.translation().x),
(result.translation().y, stored.translation().y),
(result.translation().z, stored.translation().z),
(result.rotation().w, stored.rotation().w),
(result.rotation().x, stored.rotation().x),
(result.rotation().y, stored.rotation().y),
(result.rotation().z, stored.rotation().z),
] {
assert_eq!(
got.to_bits(),
expected.to_bits(),
"component changed: {got} vs {expected}"
);
}
}
#[test]
fn same_frame_lookup_returns_identity() {
let mut registry = Registry::new();
let t = Timestamp::from_nanos(1_000_000_000);
registry
.add_transform(
Transform::new(
"a",
"b",
Vector3::new(1.0, 0.0, 0.0),
Quaternion::identity(),
Stamp::At(t),
)
.unwrap(),
)
.unwrap();
// Identity for a known child frame, a root frame, and an unknown
// frame alike: a frame relative to itself is always the identity.
for frame in ["b", "a", "unknown"] {
let result = registry.get_transform(frame, frame, t).unwrap();
assert_eq!(result.parent(), frame);
assert_eq!(result.child(), frame);
assert_eq!(result.timestamp(), Stamp::At(t));
assert_eq!(result.translation(), Vector3::zero());
assert_eq!(result.rotation(), Quaternion::identity());
}
}
#[test]
fn static_chain_composes_with_timestamped_data() {
let mut registry = Registry::new();
// Purely static chain: base -> camera mount.
registry
.add_transform(
Transform::new(
"base",
"camera",
Vector3::new(0.0, 1.0, 0.0),
Quaternion::identity(),
Stamp::Static,
)
.unwrap(),
)
.unwrap();
// A detection stamped at observation time, in the camera frame.
let t = Timestamp::from_nanos(5_000_000_000);
let mut point = Point::new(
Vector3::new(1.0, 0.0, 0.0),
Quaternion::identity(),
t,
"camera",
);
// The flagship static-mount workflow: resolve and apply. The lookup
// result carries the query time, so the application succeeds.
let tf = registry.get_transform_for(&point, "base").unwrap();
assert_eq!(tf.timestamp(), Stamp::At(t));
point.transform(&tf).unwrap();
assert_eq!(point.frame, "base");
assert_eq!(point.position, Vector3::new(1.0, 1.0, 0.0));
}
#[test]
fn static_transform_applies_directly_to_any_timestamp() {
// A hand-built static transform (Stamp::Static) is valid
// for all time when applied through Transformable.
let static_tf = Transform::new(
"base",
"camera",
Vector3::new(0.0, 1.0, 0.0),
Quaternion::identity(),
Stamp::Static,
)
.unwrap();
let mut point = Point::new(
Vector3::new(1.0, 0.0, 0.0),
Quaternion::identity(),
Timestamp::from_nanos(5_000_000_000),
"camera",
);
point.transform(&static_tf).unwrap();
assert_eq!(point.frame, "base");
assert_eq!(point.position, Vector3::new(1.0, 1.0, 0.0));
}
#[test]
fn public_types_are_send_and_sync() {
fn assert_send_sync<T: Send + Sync>() {}
assert_send_sync::<Registry>();
assert_send_sync::<RegistryError>();
assert_send_sync::<Transform>();
assert_send_sync::<Point>();
assert_send_sync::<Vector3>();
assert_send_sync::<Quaternion>();
assert_send_sync::<Timestamp>();
}
/// A transform translated by `x` along the x-axis.
fn translated(
parent: &str,
child: &str,
timestamp: Stamp,
x: f64,
) -> Transform {
Transform::new(
parent,
child,
Vector3::new(x, 0.0, 0.0),
Quaternion::identity(),
timestamp,
)
.unwrap()
}
#[test]
fn an_overflowing_lookup_reports_the_flat_variant_only_where_it_inverts() {
// Two individually finite hops compose to an infinite translation.
// Where the lookup inverts the chain it notices, and the condition
// must arrive as the same flat `NonFiniteValues` an insert reports:
// one spelling per condition, so a caller matching it cannot miss a
// wrapped copy arriving from the other code path.
//
// The opposite direction pins the scope of that claim. A lookup
// toward an ancestor resolves entirely from the source half and
// inverts nothing, and a lookup result is deliberately never
// re-validated, so the overflow is returned as `Ok`. That is what
// the docs on `get_transform` and `RegistryError::NonFiniteValues`
// say; if this assertion ever starts failing, they must change with
// it rather than the other way around.
let t = Timestamp::from_nanos(1_000_000_000);
let mut registry = Registry::new();
registry
.add_transform(translated("a", "b", Stamp::At(t), 1.0e308))
.unwrap();
registry
.add_transform(translated("b", "c", Stamp::At(t), 1.0e308))
.unwrap();
let inverting = registry.get_transform("c", "a", t);
assert!(
matches!(inverting, Err(RegistryError::NonFiniteValues)),
"expected a flat NonFiniteValues, got {inverting:?}"
);
let ancestor_ward = registry.get_transform("a", "c", t).unwrap();
assert_eq!(
ancestor_ward.translation(),
Vector3::new(f64::INFINITY, 0.0, 0.0)
);
}
#[test]
fn not_found_at_renders_both_coverage_cases_in_seconds() {
// Error formatting goes through `TimePoint::as_seconds_lossy`, which
// is infallible by contract: neither shape of `covered` can fail to
// render and mask the error being reported. The two shapes must also
// read differently — a drained frame is not a timing problem.
let t1 = Timestamp::from_nanos(1_000_000_000);
let t2 = Timestamp::from_nanos(2_000_000_000);
let t3 = Timestamp::from_nanos(3_000_000_000);
let mut registry = Registry::new();
registry
.add_transform(translated("a", "b", Stamp::At(t1), 1.0))
.unwrap();
registry
.add_transform(translated("a", "b", Stamp::At(t2), 2.0))
.unwrap();
let gap = registry.get_transform("a", "b", t3).unwrap_err();
assert_eq!(
alloc::format!("{gap}"),
"transform from b into a at 3 not found (b covers [1, 2])"
);
registry.remove_transforms_before(t3);
let drained = registry.get_transform("a", "b", t3).unwrap_err();
assert_eq!(
alloc::format!("{drained}"),
"transform from b into a at 3 not found (b holds no transforms)"
);
}
#[test]
// The compared values are exactly representable; the assertion is on
// reported payloads, not on float arithmetic.
#[allow(clippy::float_cmp)]
fn duplicate_timestamp_add_is_a_last_write_wins_upsert() {
let t = Timestamp::from_nanos(5_000_000_000);
let mut registry = Registry::new();
registry
.add_transform(translated("a", "b", Stamp::At(t), 1.0))
.unwrap();
registry
.add_transform(translated("a", "b", Stamp::At(t), 2.0))
.unwrap();
assert_eq!(
registry.get_transform("a", "b", t).unwrap().translation().x,
2.0
);
}
#[test]
// The compared values are exactly representable; the assertion is on
// reported payloads, not on float arithmetic.
#[allow(clippy::float_cmp)]
fn duplicate_static_add_is_a_last_write_wins_upsert() {
// Re-publishing a static transform replaces it: last write wins.
let mut registry = Registry::new();
registry
.add_transform(translated("a", "b", Stamp::Static, 1.0))
.unwrap();
registry
.add_transform(translated("a", "b", Stamp::Static, 7.0))
.unwrap();
let got = registry
.get_transform("a", "b", Timestamp::from_nanos(3_000_000_000))
.unwrap();
assert_eq!(got.translation().x, 7.0);
}
#[test]
// The compared values are exactly representable; the assertion is on
// reported payloads, not on float arithmetic.
#[allow(clippy::float_cmp)]
fn zero_max_age_keeps_only_the_newest_sample() {
let t1 = Timestamp::from_nanos(1_000_000_000);
let t2 = Timestamp::from_nanos(2_000_000_000);
let t3 = Timestamp::from_nanos(3_000_000_000);
let mut registry = Registry::with_max_age(Duration::ZERO);
registry
.add_transform(translated("a", "b", Stamp::At(t1), 1.0))
.unwrap();
registry
.add_transform(translated("a", "b", Stamp::At(t2), 2.0))
.unwrap();
registry
.add_transform(translated("a", "b", Stamp::At(t3), 3.0))
.unwrap();
// Exact hit on the newest sample still works.
assert_eq!(
registry
.get_transform("a", "b", t3)
.unwrap()
.translation()
.x,
3.0
);
// Older samples are gone: the covered range collapsed to [t3, t3].
match registry.get_transform("a", "b", t2) {
Err(RegistryError::NotFoundAt {
frame,
requested,
covered,
..
}) => {
assert_eq!(frame, "b");
assert_eq!(requested, t2);
assert_eq!(covered, Some((t3, t3)));
}
other => panic!("expected NotFoundAt, got {other:?}"),
}
}
#[test]
fn remove_frame_mid_tree_strands_descendants() {
// map -> odom -> base_link; removing odom strands base_link, whose
// buffer keeps its pin to the removed parent. The subsequent lookup
// is diagnosed relative to the remaining tree: "map" now exists
// nowhere (it was only ever odom's parent), so the error names it —
// the documented, deliberately-pinned behavior.
let t = Timestamp::from_nanos(1_000_000_000);
let mut registry = Registry::new();
registry
.add_transform(translated("map", "odom", Stamp::At(t), 1.0))
.unwrap();
registry
.add_transform(translated("odom", "base_link", Stamp::At(t), 1.0))
.unwrap();
assert!(registry.remove_frame("odom"));
match registry.get_transform("map", "base_link", t) {
Err(RegistryError::UnknownFrame(frame)) => assert_eq!(frame, "map"),
other => panic!("expected UnknownFrame, got {other:?}"),
}
}
}