re_tf 0.31.1

Rerun spatial transform processing
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
use nohash_hasher::{IntMap, IntSet};
use re_byte_size::SizeBytes;
use re_chunk_store::{LatestAtQuery, MissingChunkReporter};
use re_entity_db::EntityDb;
use re_log::debug_assert;

use crate::frame_id_registry::FrameIdRegistry;
use crate::transform_resolution_cache::ParentFromChildTransform;
use crate::{
    CachedTransformsForTimeline, ResolvedPinholeProjection, TransformFrameIdHash,
    TransformResolutionCache, image_view_coordinates,
};

/// Details on how to transform from a source to a target frame.
#[derive(Clone, Debug, PartialEq)]
pub struct TreeTransform {
    /// Root frame this transform belongs to.
    ///
    /// ⚠️ This is the root of the tree this transform belongs to,
    /// not necessarily what the transform transforms into.
    ///
    /// Implementation note:
    /// We could add target and maybe even source to this, but we want to keep this struct small'ish.
    /// On that note, it may be good to split this in the future, as most of the time we're only interested in the
    /// source->target affine transform.
    pub root: TransformFrameIdHash,

    /// The transform from this frame to the target's space.
    ///
    /// Include 3D-from-2D / 2D-from-3D pinhole transform if present.
    pub target_from_source: glam::DAffine3,
}

impl TreeTransform {
    fn new_root(root: TransformFrameIdHash) -> Self {
        Self {
            root,
            target_from_source: glam::DAffine3::IDENTITY,
        }
    }

    /// Multiplies the transform from the left by `target_from_reference`
    ///
    /// Or in other words:
    /// `reference_from_source = self`
    /// `target_from_source = target_from_reference * reference_from_source`
    fn left_multiply(&self, target_from_reference: glam::DAffine3) -> Self {
        let Self {
            root,
            target_from_source: reference_from_source,
        } = self;

        let target_from_source = target_from_reference * reference_from_source;

        Self {
            root: *root,
            target_from_source,
        }
    }
}

impl SizeBytes for TreeTransform {
    fn heap_size_bytes(&self) -> u64 {
        let Self {
            root,
            target_from_source,
        } = self;

        root.heap_size_bytes() + target_from_source.heap_size_bytes()
    }
}

impl re_byte_size::MemUsageTreeCapture for TreeTransform {
    fn capture_mem_usage_tree(&self) -> re_byte_size::MemUsageTree {
        re_tracing::profile_function!();

        let Self {
            root,
            target_from_source,
        } = self;

        re_byte_size::MemUsageNode::new()
            .with_child("root", root.total_size_bytes())
            .with_child("target_from_source", target_from_source.total_size_bytes())
            .into_tree()
    }
}

#[derive(Clone, Debug, thiserror::Error, PartialEq, Eq)]
pub enum TransformFromToError {
    #[error("No transform relationships about the target frame {0:?} are known")]
    UnknownTargetFrame(TransformFrameIdHash),

    #[error("No transform relationships about the source frame {0:?} are known")]
    UnknownSourceFrame(TransformFrameIdHash),

    #[error(
        "There's no path between {target:?} and {src:?}. The target's root is {target_root:?}, the source's root is {source_root:?}"
    )]
    NoPathBetweenFrames {
        target: TransformFrameIdHash,
        src: TransformFrameIdHash, // Can't name this `source` for some strange procmacro reasons
        target_root: TransformFrameIdHash,
        source_root: TransformFrameIdHash,
    },
}

impl TransformFromToError {
    fn no_path_between_target_and_source(target: &TargetInfo, source: &SourceInfo<'_>) -> Self {
        Self::NoPathBetweenFrames {
            target: target.id,
            src: source.id,
            target_root: target.root,
            source_root: source.root,
        }
    }
}

/// Private utility struct for working with a target frame.
struct TargetInfo {
    id: TransformFrameIdHash,
    root: TransformFrameIdHash,
    target_from_root: glam::DAffine3,
}

/// Private utility struct for working with a source frame.
struct SourceInfo<'a> {
    id: TransformFrameIdHash,
    root: TransformFrameIdHash,
    root_from_source: &'a TreeTransform,
}

/// Properties of a pinhole transform tree root.
///
/// Each pinhole forms its own subtree which may be embedded into a 3D space.
/// Everything at and below the pinhole tree root is considered to be 2D,
/// everything above is considered to be 3D.
#[derive(Clone, Debug, PartialEq)]
pub struct PinholeTreeRoot {
    /// The tree root of the parent of this pinhole.
    pub parent_tree_root: TransformFrameIdHash,

    /// Pinhole projection that defines how 2D objects are transformed in this space.
    pub pinhole_projection: ResolvedPinholeProjection,

    /// Transforms the 2D subtree into its parent 3D space.
    ///
    /// Keep in mind that even if you're in a 3D target space, this may not be the final 3D transform
    /// of the pinhole since the target may be a child for the pinhole's parent tree root.
    /// (i.e. your target space may not be the root of the 3D tree!)
    pub parent_root_from_pinhole_root: glam::DAffine3,
}

impl SizeBytes for PinholeTreeRoot {
    fn heap_size_bytes(&self) -> u64 {
        let Self {
            parent_tree_root,
            pinhole_projection,
            parent_root_from_pinhole_root,
        } = self;

        parent_tree_root.heap_size_bytes()
            + pinhole_projection.heap_size_bytes()
            + parent_root_from_pinhole_root.heap_size_bytes()
    }
}

/// Properties of a transform root.
///
/// [`TransformForest`] tries to identify all roots.
#[derive(Clone, Debug, PartialEq)]
pub enum TransformTreeRootInfo {
    /// Regular root without any extra meta information.
    TransformFrameRoot,

    /// The tree root is an entity path with a pinhole transformation,
    /// thus marking a 3D to 2D transition.
    Pinhole(PinholeTreeRoot),
}

impl SizeBytes for TransformTreeRootInfo {
    fn heap_size_bytes(&self) -> u64 {
        match self {
            Self::TransformFrameRoot => 0,
            Self::Pinhole(pinhole_tree_root) => pinhole_tree_root.heap_size_bytes(),
        }
    }
}

/// Analyzes & propagates the transform graph of a recording at a given time & timeline.
///
/// Identifies different transform trees present in the recording and computes transforms relative to their roots,
/// such that arbitrary transforms within the tree can be resolved (relatively) quickly.
#[derive(Default, Clone)]
pub struct TransformForest {
    /// Are there any chunks missing from the chunk store,
    /// leading to an incomplete forest?
    missing_chunk_reporter: MissingChunkReporter,

    /// All known tree roots.
    roots: IntMap<TransformFrameIdHash, TransformTreeRootInfo>,

    /// All frames reachable from one of the tree roots.
    ///
    /// Roots are also contained, targeting themselves with identity.
    /// This simplifies lookups.
    root_from_frame: IntMap<TransformFrameIdHash, TreeTransform>,
    //
    // TODO(RR-2667): Store errors that occur during the graph walk
}

impl TransformForest {
    /// Computes a forest (several trees) of all transforms for a given point in time.
    pub fn new(
        entity_db: &EntityDb,
        transform_cache: &TransformResolutionCache,
        query: &LatestAtQuery,
    ) -> Self {
        re_tracing::profile_function!();

        // Algorithm overview:
        //
        // We're using a dynamic programming approach that minimizes queries into the transform cache.
        //
        // 1) `walk_towards_parent`:
        // For a given unprocessed frame, we walk towards their parents until there's either no more connection or we hit a frame
        // that we already visited. As we walk, we collect all the encountered transforms into a "transform stack" and mark visited frames as processed.
        // 2) `add_stack_of_transforms`:
        // Then, apply this transform stack to the existing datastructures.
        // First, we figure out which root this stack belongs to, then we walk the stack backwards,
        // computing the respective transforms to the root as we go.
        //
        // Repeat steps 1) & 2) until we've processed all frames.

        let transforms = transform_cache.transforms_for_timeline(query.timeline());
        let frame_id_registry = transform_cache.frame_id_registry();
        let mut unprocessed_frames: IntSet<_> = frame_id_registry.iter_frame_id_hashes().collect();
        let mut transform_stack = Vec::new(); // Keep pushing & draining from the same vector as a simple performance optimization.

        let mut forest = Self::default();

        // Pop an arbitrary source frame from the list of unprocessed frames.
        while let Some(current_frame) = unprocessed_frames.iter().next().copied() {
            // Walk as long as we can until we hit something we already processed or end up in a dead end.
            walk_towards_parent(
                entity_db,
                &forest.missing_chunk_reporter,
                query,
                current_frame,
                &frame_id_registry,
                &transforms,
                &mut unprocessed_frames,
                &mut transform_stack,
            );

            // Process the stack we accumulated.
            debug_assert!(
                !transform_stack.is_empty(),
                "There should be at least one element in the transform stack since we know we had at least one unprocessed element to start with."
            );
            forest.add_stack_of_transforms(transform_cache, &mut transform_stack);
            debug_assert!(
                transform_stack.is_empty(),
                "Expected add_stack_of_transforms to consume an entire transform stack."
            );
        }

        forest
    }

    /// Were there any chunks missing from the chunk store,
    /// leading to an incomplete forest?
    pub fn any_missing_chunks(&self) -> bool {
        self.missing_chunk_reporter.any_missing()
    }

    /// Adds a stack of transforms produced by [`walk_towards_parent`] to the forest.
    ///
    /// Each stack in the transform is a parent item of the item before it.
    fn add_stack_of_transforms(
        &mut self,
        cache: &TransformResolutionCache,
        transform_stack: &mut Vec<ParentChildTransforms>,
    ) {
        re_tracing::profile_function!();

        let Some(top_of_stack) = transform_stack.last() else {
            // Should never happen in regular operation.
            return;
        };

        // Figure out the root frame for this entire stack.
        let (mut root_frame, mut root_from_target) = if let Some(parent_frame) =
            top_of_stack.parent_frame
        {
            // We have a connection further up the stack. That means we must have stopped because we already know that target!
            if let Some(root_from_frame) = self.root_from_frame.get(&parent_frame) {
                // Yes, we can short-circuit to a known root!
                debug_assert!(
                    self.roots.contains_key(&root_from_frame.root),
                    "Known root must be registered as such"
                );
                (root_from_frame.root, root_from_frame.target_from_source)
            } else {
                // We didn't know the target. Must mean that the target is a new root!
                let previous_root = self.roots.insert(
                    parent_frame,
                    // There's apparently no information about this root, so it can't be a pinhole!
                    TransformTreeRootInfo::TransformFrameRoot,
                );
                debug_assert!(previous_root.is_none(), "Root was added already"); // TODO(RR-2667): Build out into cycle detection

                // That parent apparently won't show up in any transform stack (we didn't walk there because there was no information about it!)
                // So if we don't add this root now to our `root_from_frame` map, we'd never fill out the required self-reference!
                self.root_from_frame
                    .insert(parent_frame, TreeTransform::new_root(parent_frame));

                (parent_frame, glam::DAffine3::IDENTITY)
            }
        } else {
            // We're not pointing at a new root. So we ourselves must be a root!
            let previous_root = if let Some(pinhole_projection) = &top_of_stack.pinhole_projection {
                // We're a (lonely) pinhole with no 3D parent.
                // Usually pinholes are embedded into a 3D space, but this one doesn't have any more parents,
                // meaning that everything in this entire tree is 2D.
                let new_root_info = TransformTreeRootInfo::Pinhole(PinholeTreeRoot {
                    parent_tree_root: top_of_stack.child_frame,
                    pinhole_projection: pinhole_projection.clone(),
                    parent_root_from_pinhole_root: glam::DAffine3::IDENTITY,
                });
                self.roots.insert(top_of_stack.child_frame, new_root_info)
            } else {
                self.roots.insert(
                    top_of_stack.child_frame,
                    TransformTreeRootInfo::TransformFrameRoot,
                )
            };
            debug_assert!(previous_root.is_none(), "Root was added already"); // TODO(RR-2667): Build out into cycle detection

            (top_of_stack.child_frame, glam::DAffine3::IDENTITY)
        };

        // Walk the stack backwards, collecting transforms as we go.
        while let Some(transforms) = transform_stack.pop() {
            let mut root_from_current_frame = root_from_target
                * transforms
                    .parent_from_child
                    // Identity here means we're self-referencing. That's fine since we want roots to refer to themselves in our look-up table.
                    .map_or(glam::DAffine3::IDENTITY, |target_from_source| {
                        target_from_source.transform
                    });

            // Did we encounter a pinhole and need to create a new subspace?
            if let Some(pinhole_projection) = transforms.pinhole_projection {
                let new_root_info = TransformTreeRootInfo::Pinhole(PinholeTreeRoot {
                    parent_tree_root: root_frame,
                    pinhole_projection: pinhole_projection.clone(),
                    parent_root_from_pinhole_root: root_from_current_frame,
                });
                root_frame = transforms.child_frame;

                let previous_root = self.roots.insert(root_frame, new_root_info);
                debug_assert!(
                    previous_root.is_none(),
                    "Root was added already at {:?} as {previous_root:?}",
                    cache.frame_id_registry().lookup_frame_id(root_frame)
                ); // TODO(RR-2667): Build out into cycle detection

                root_from_current_frame = glam::DAffine3::IDENTITY;
            }

            let transform_root_from_current = TreeTransform {
                root: root_frame,
                target_from_source: root_from_current_frame,
            };

            let _previous_transform = self
                .root_from_frame
                .insert(transforms.child_frame, transform_root_from_current);

            // TODO(RR-2667): Build out into cycle detection
            #[cfg(debug_assertions)]
            {
                let frame_id_registry = cache.frame_id_registry();
                debug_assert!(
                    _previous_transform.is_none(),
                    "Root from frame relationship was added already for {:?}. Now targeting {:?}, previously {:?}",
                    frame_id_registry.lookup_frame_id(transforms.child_frame),
                    frame_id_registry.lookup_frame_id(root_frame),
                    _previous_transform.and_then(|f| frame_id_registry.lookup_frame_id(f.root))
                );
            }

            root_from_target = root_from_current_frame;
        }
    }
}

impl SizeBytes for TransformForest {
    fn heap_size_bytes(&self) -> u64 {
        re_tracing::profile_function!();

        let Self {
            missing_chunk_reporter: _,
            roots,
            root_from_frame,
        } = self;

        roots.heap_size_bytes() + root_from_frame.heap_size_bytes()
    }
}

impl re_byte_size::MemUsageTreeCapture for TransformForest {
    fn capture_mem_usage_tree(&self) -> re_byte_size::MemUsageTree {
        re_tracing::profile_function!();

        let Self {
            missing_chunk_reporter: _,
            roots,
            root_from_frame,
        } = self;

        re_byte_size::MemUsageNode::new()
            .with_child("roots", roots.total_size_bytes())
            .with_child("root_from_frame", root_from_frame.total_size_bytes())
            .into_tree()
    }
}

/// Starting from a `current_frame`, walks towards the parent and accumulates transforms into `transform_stack`.
/// Stops until not more connection is found or an already processed `frame_id` is hit.
#[expect(clippy::too_many_arguments)]
fn walk_towards_parent(
    entity_db: &EntityDb,
    missing_chunk_reporter: &MissingChunkReporter,
    query: &LatestAtQuery,
    current_frame: TransformFrameIdHash,
    id_registry: &FrameIdRegistry,
    transforms: &CachedTransformsForTimeline,
    unprocessed_frames: &mut IntSet<TransformFrameIdHash>,
    transform_stack: &mut Vec<ParentChildTransforms>,
) {
    re_tracing::profile_function!();

    debug_assert!(
        transform_stack.is_empty(),
        "Didn't process the last transform stack fully."
    );

    let mut next_frame = Some(current_frame);
    while let Some(current_frame) = next_frame
        && unprocessed_frames.remove(&current_frame)
    {
        // We either already processed this frame, or we reached the end of our path if this source is not in the list of unprocessed frames.
        let transforms = transforms_at(
            entity_db,
            missing_chunk_reporter,
            current_frame,
            query,
            id_registry,
            transforms,
        );
        next_frame = transforms.parent_frame;

        // No matter whether there's a next frame or not, we push the transform information we got about this frame onto the stack
        // since we expect an entry for every source we process.
        transform_stack.push(transforms);
    }
}

/// If `frame` is an implicit transform frame and has a parent, return said parent.
fn implicit_transform_parent(
    frame: TransformFrameIdHash,
    id_registry: &FrameIdRegistry,
) -> Option<TransformFrameIdHash> {
    debug_assert!(
        &id_registry.lookup_frame_id(frame).is_some(),
        "Frame id hash {frame:?} is not known to the cache at all."
    );

    Some(TransformFrameIdHash::from_entity_path(
        &id_registry
            .lookup_frame_id(frame)?
            .as_entity_path()?
            .parent()?,
    ))
}

impl TransformForest {
    /// An arbitrarily ordered iterator of all transform frame roots.
    pub fn transform_frame_roots(&self) -> impl Iterator<Item = TransformFrameIdHash> {
        self.roots
            .iter()
            .filter(|(_, info)| matches!(info, TransformTreeRootInfo::TransformFrameRoot))
            .map(|(id, _)| *id)
    }

    /// Returns the properties of the transform tree root at the given frame.
    ///
    /// If frame is not known as a transform tree root, returns [`None`].
    #[inline]
    pub fn root_info(&self, root_frame: TransformFrameIdHash) -> Option<&TransformTreeRootInfo> {
        self.roots.get(&root_frame)
    }

    /// Returns the properties of the pinhole tree root at the given frame if the frame's root is a pinhole tree root.
    #[inline]
    pub fn pinhole_tree_root_info(
        &self,
        root_frame: TransformFrameIdHash,
    ) -> Option<&PinholeTreeRoot> {
        if let TransformTreeRootInfo::Pinhole(pinhole_tree_root) = self.roots.get(&root_frame)? {
            Some(pinhole_tree_root)
        } else {
            None
        }
    }

    /// Returns the transform information of how to get from a given frame to its tree root.
    #[inline]
    pub fn root_from_frame(&self, frame: TransformFrameIdHash) -> Option<&TreeTransform> {
        self.root_from_frame.get(&frame)
    }

    /// Computes the transform from one frame to another if there is a path between them.
    ///
    /// This function computes all T, such that for each source `p_target = T * p_source`
    ///
    /// `target`: The frame into which to transform.
    /// `sources`: The frames from which to transform.
    ///
    /// If the target's root & sources are connected with a pinhole camera,
    /// we'll transform it according to the image plane distance.
    ///
    /// Returns an iterator of results, one for each source.
    /// If the target frame is not known at all, returns [`TransformFromToError::UnknownTargetFrame`] for every source.
    pub fn transform_from_to(
        &self,
        target: TransformFrameIdHash,
        sources: impl Iterator<Item = TransformFrameIdHash>,
        lookup_image_plane_distance: &dyn Fn(TransformFrameIdHash) -> f64,
    ) -> impl Iterator<
        Item = (
            TransformFrameIdHash,
            Result<TreeTransform, TransformFromToError>,
        ),
    > {
        // We're looking for a common root between source and target.
        // We start by looking up the target's tree root.

        let Some(root_from_target) = self.root_from_frame.get(&target) else {
            return itertools::Either::Left(sources.map(move |source| {
                (
                    source,
                    Err(TransformFromToError::UnknownTargetFrame(target)),
                )
            }));
        };

        // Invert `root_from_target` to get `target.from_root`.
        let target = {
            let TreeTransform {
                root: target_root,
                target_from_source: root_from_entity,
            } = &root_from_target;

            TargetInfo {
                id: target,
                root: *target_root,
                target_from_root: root_from_entity.inverse(),
            }
        };

        // Query type of target's root for later.
        let target_root_info = self.roots.get(&target.root);

        // Local cache for connecting pinhole spaces with their parent 3D space.
        let mut pinhole_tree_connector_cache = IntMap::default();

        itertools::Either::Right(sources.map(move |source| {
            let Some(root_from_source) = self.root_from_frame.get(&source) else {
                return (
                    source,
                    Err(TransformFromToError::UnknownSourceFrame(source)),
                );
            };

            let source = SourceInfo {
                id: source,
                root: root_from_source.root,
                root_from_source,
            };

            // Common case: both source & target share the same root.
            let result = if source.root == target.root {
                if source.root == target.id {
                    // Fast track for source's root being the target.
                    Ok(source.root_from_source.clone())
                } else {
                    // target_from_source = target_from_reference * root_from_source
                    Ok(root_from_source.left_multiply(target.target_from_root))
                }
            }
            // There might be a connection via a pinhole making this 3D in 2D.
            else if let Some(TransformTreeRootInfo::Pinhole(pinhole_tree_root)) = target_root_info
            {
                from_3d_source_to_2d_target(
                    &target,
                    &source,
                    pinhole_tree_root,
                    &mut pinhole_tree_connector_cache,
                )
            }
            // There might be a connection via a pinhole making this 2D in 3D.
            else if let Some(TransformTreeRootInfo::Pinhole(pinhole_tree_root)) =
                self.roots.get(&source.root)
            {
                from_2d_source_to_3d_target(
                    &target,
                    &source,
                    pinhole_tree_root,
                    lookup_image_plane_distance,
                    &mut pinhole_tree_connector_cache,
                )
            }
            // Disconnected, we can't transform into the target space.
            else {
                Err(TransformFromToError::no_path_between_target_and_source(
                    &target, &source,
                ))
            };

            (source.id, result)
        }))
    }
}

fn from_2d_source_to_3d_target(
    target: &TargetInfo,
    source: &SourceInfo<'_>,
    source_pinhole_tree_root: &PinholeTreeRoot,
    lookup_image_plane_distance: &dyn Fn(TransformFrameIdHash) -> f64,
    target_from_image_plane_cache: &mut IntMap<TransformFrameIdHash, glam::DAffine3>,
) -> Result<TreeTransform, TransformFromToError> {
    let PinholeTreeRoot {
        parent_tree_root,
        pinhole_projection,
        parent_root_from_pinhole_root: root_from_pinhole3d,
    } = source_pinhole_tree_root;

    // `root` here is the target's root!
    // We call the source's root `pinhole3d` to distinguish it.
    if *parent_tree_root != target.root {
        return Err(TransformFromToError::no_path_between_target_and_source(
            target, source,
        ));
    }

    // Rename for clarification:
    let image_plane_from_source = source.root_from_source;

    let target_from_image_plane = target_from_image_plane_cache
        .entry(source.root)
        .or_insert_with(|| {
            let pinhole_image_plane_distance = lookup_image_plane_distance(source.root);
            let pinhole3d_from_image_plane =
                pinhole3d_from_image_plane(pinhole_projection, pinhole_image_plane_distance);
            target.target_from_root * root_from_pinhole3d * pinhole3d_from_image_plane
        });

    // target_from_source = target_from_image_plane * image_plane_from_source
    Ok(image_plane_from_source.left_multiply(*target_from_image_plane))
}

fn from_3d_source_to_2d_target(
    target: &TargetInfo,
    source: &SourceInfo<'_>,
    target_pinhole_tree_root: &PinholeTreeRoot,
    target_from_source_root_cache: &mut IntMap<TransformFrameIdHash, glam::DAffine3>,
) -> Result<TreeTransform, TransformFromToError> {
    let PinholeTreeRoot {
        parent_tree_root,
        pinhole_projection,
        parent_root_from_pinhole_root: root_from_pinhole3d,
    } = target_pinhole_tree_root;

    // `root` here is the source's root!
    // We call the target's root `pinhole3d` to distinguish it.
    if *parent_tree_root != source.root {
        return Err(TransformFromToError::no_path_between_target_and_source(
            target, source,
        ));
    }

    // Rename for clarification:
    let target_from_image_plane = target.target_from_root;

    let target_from_root = target_from_source_root_cache
        .entry(source.root)
        .or_insert_with(|| {
            // TODO(#1025):
            // There's no meaningful image plane distance for 3D->2D views.
            let pinhole_image_plane_distance = 500.0;
            // Currently our 2D views require us to invert the `pinhole2d_image_plane_from_pinhole3d` matrix.
            // This builds a relationship between the 2D plane and the 3D world, when actually the 2D plane
            // should have infinite depth!
            // The inverse of this matrix *is* working for this, but quickly runs into precision issues.
            // See also `ui_2d.rs#setup_target_config`

            let pinhole3d_from_image_plane =
                pinhole3d_from_image_plane(pinhole_projection, pinhole_image_plane_distance);
            let image_plane_from_pinhole3d = pinhole3d_from_image_plane.inverse();
            let pinhole3d_from_root = root_from_pinhole3d.inverse();
            target_from_image_plane * image_plane_from_pinhole3d * pinhole3d_from_root
        });

    // target_from_source = target_from_root * root_from_source
    Ok(source.root_from_source.left_multiply(*target_from_root))
}

fn pinhole3d_from_image_plane(
    resolved_pinhole_projection: &ResolvedPinholeProjection,
    pinhole_image_plane_distance: f64,
) -> glam::DAffine3 {
    let ResolvedPinholeProjection {
        parent: _, // TODO(andreas): Make use of this.
        image_from_camera,
        resolution: _,
        view_coordinates,
    } = resolved_pinhole_projection;

    // Everything under a pinhole camera is a 2D projection, thus doesn't actually have a proper 3D representation.
    // Our visualization interprets this as looking at a 2D image plane from a single point (the pinhole).

    // Center the image plane and move it along z, scaling the further the image plane is.
    let focal_length = image_from_camera.focal_length_in_pixels();
    let focal_length = glam::dvec2(focal_length.x() as f64, focal_length.y() as f64);
    let scale = pinhole_image_plane_distance / focal_length;
    let translation = (glam::DVec2::from(-image_from_camera.principal_point()) * scale)
        .extend(pinhole_image_plane_distance);

    let image_plane3d_from_2d_content = glam::DAffine3::from_translation(translation)
            // We want to preserve any depth that might be on the pinhole image.
            // Use harmonic mean of x/y scale for those.
            * glam::DAffine3::from_scale(
                scale.extend(2.0 / (1.0 / scale.x + 1.0 / scale.y)),
            );

    // Our interpretation of the pinhole camera implies that the axis semantics, i.e. ViewCoordinates,
    // determine how the image plane is oriented.
    // (see also `CamerasPart` where the frustum lines are set up)
    let obj_from_image_plane3d = glam::DMat3::from_cols_array(
        &view_coordinates
            .from_other(&image_view_coordinates())
            .to_cols_array()
            .map(|x| x as f64),
    );

    glam::DAffine3::from_mat3(obj_from_image_plane3d) * image_plane3d_from_2d_content

    // Above calculation is nice for a certain kind of visualizing a projected image plane,
    // but the image plane distance is arbitrary and there might be other, better visualizations!
}

struct ParentChildTransforms {
    parent_frame: Option<TransformFrameIdHash>,
    child_frame: TransformFrameIdHash,
    parent_from_child: Option<ParentFromChildTransform>,
    pinhole_projection: Option<ResolvedPinholeProjection>,
}

fn transforms_at(
    entity_db: &EntityDb,
    missing_chunk_reporter: &MissingChunkReporter,
    child_frame: TransformFrameIdHash,
    query: &LatestAtQuery,
    id_registry: &FrameIdRegistry,
    transforms_for_timeline: &CachedTransformsForTimeline,
) -> ParentChildTransforms {
    #![expect(clippy::useless_let_if_seq)]

    let mut parent_from_child;
    let pinhole_projection;

    if let Some(source_transforms) = transforms_for_timeline.frame_transforms(child_frame) {
        parent_from_child =
            source_transforms.latest_at_transform(entity_db, missing_chunk_reporter, query);
        pinhole_projection =
            source_transforms.latest_at_pinhole(entity_db, missing_chunk_reporter, query);
    } else {
        parent_from_child = None;
        pinhole_projection = None;
    }

    // Parent frame may be defined on either the pinhole projection or `parent_from_child`, or implicitly via entity derived transform frames.
    let parent_frame = if let Some(transform) = parent_from_child.as_ref() {
        // If there's a pinhole AND a regular transform, they need to have the same target.
        if let Some(pinhole_projection) = pinhole_projection.as_ref()
            && pinhole_projection.parent != transform.parent
        {
            let transform_frame = id_registry.lookup_frame_id(child_frame);
            let pinhole_parent_frame = id_registry.lookup_frame_id(pinhole_projection.parent);
            let transform_parent_frame = id_registry.lookup_frame_id(transform.parent);

            // If any of the frames ids can't be resolved to a string, we're in bigger trouble and can't show a useful error
            // as this implies that the registry is in an invalid state.
            if let Some(transform_frame) = transform_frame
                && let Some(pinhole_parent_frame) = pinhole_parent_frame
                && let Some(transform_parent_frame) = transform_parent_frame
            {
                re_log::warn_once!(
                    "The transform frame {transform_frame:?} is connected to {pinhole_parent_frame:?} via a pinhole but also connected to {transform_parent_frame:?} via a transform. Any frame is only ever allowed to have a single parent at any given time.",
                );
            } else {
                for frame in [
                    transform_frame,
                    pinhole_parent_frame,
                    transform_parent_frame,
                ] {
                    if frame.is_none() {
                        re_log::debug_panic!(
                            "Couldn't resolve frame id for {frame:?} in the registry, even though it was present in the transforms for timeline.",
                        );
                    }
                }
            }
        }

        Some(transform.parent)
    } else if let Some(pinhole_projection) = pinhole_projection.as_ref() {
        // If there's no regular transform, maybe the Pinhole has a connection to offer.
        Some(pinhole_projection.parent)
    } else if let Some(parent) = implicit_transform_parent(child_frame, id_registry) {
        // Maybe there's an implicit connection that we have to fill in?
        // Implicit connections are identity connections!
        parent_from_child = Some(ParentFromChildTransform {
            parent,
            transform: glam::DAffine3::IDENTITY,
        });
        Some(parent)
    } else {
        None
    };

    ParentChildTransforms {
        parent_frame,
        child_frame,
        parent_from_child,
        pinhole_projection,
    }
}

#[cfg(test)]
mod tests {
    use std::sync::Arc;

    use itertools::Itertools as _;
    use re_chunk_store::Chunk;
    use re_entity_db::EntityDb;
    use re_log_types::{EntityPath, StoreInfo, TimeCell, TimePoint, Timeline, TimelineName};
    use re_sdk_types::components::TransformFrameId;
    use re_sdk_types::{RowId, archetypes, components};

    use super::*;

    fn test_pinhole() -> archetypes::Pinhole {
        archetypes::Pinhole::from_focal_length_and_resolution([1.0, 2.0], [100.0, 200.0])
    }

    fn test_resolved_pinhole(parent: TransformFrameIdHash) -> ResolvedPinholeProjection {
        ResolvedPinholeProjection {
            parent,
            image_from_camera: components::PinholeProjection::from_focal_length_and_principal_point(
                [1.0, 2.0],
                [50.0, 100.0],
            ),
            resolution: Some([100.0, 200.0].into()),
            view_coordinates: archetypes::Pinhole::DEFAULT_CAMERA_XYZ,
        }
    }

    /// A test scene that relies exclusively on the entity hierarchy.
    ///
    /// We're using relatively basic transforms here as we assume that resolving transforms have been tested on [`TransformResolutionCache`] already.
    /// Similarly, since [`TransformForest`] does not yet maintain anything over time, we're using static timing instead.
    ///
    /// Tree structure:
    /// ```text
    /// tf#/top
    /// ├─── tf#/top/pinhole
    /// │          └─── tf#/top/pinhole/child2d
    /// ├─── tf#/top/pure_leaf_pinhole
    /// └─── tf#/top/child3d
    /// ```
    fn entity_hierarchy_test_scene() -> Result<EntityDb, Box<dyn std::error::Error>> {
        let mut entity_db = EntityDb::new(StoreInfo::testing().store_id);
        entity_db.add_chunk(&Arc::new(
            Chunk::builder(EntityPath::from("top"))
                .with_archetype_auto_row(
                    TimePoint::STATIC,
                    &archetypes::Transform3D::from_translation([1.0, 0.0, 0.0]),
                )
                .with_archetype_auto_row(
                    TimePoint::STATIC,
                    // Add some instance transforms - we need to make sure they don't propagate.
                    &archetypes::InstancePoses3D::new()
                        .with_translations([[10.0, 0.0, 0.0], [20.0, 0.0, 0.0]]),
                )
                .build()?,
        ))?;
        entity_db.add_chunk(&Arc::new(
            Chunk::builder(EntityPath::from("top/pinhole"))
                .with_archetype_auto_row(
                    TimePoint::STATIC,
                    &archetypes::Transform3D::from_translation([0.0, 1.0, 0.0]),
                )
                .with_archetype(RowId::new(), TimePoint::STATIC, &test_pinhole())
                .build()?,
        ))?;
        entity_db.add_chunk(&Arc::new(
            Chunk::builder(EntityPath::from("top/pure_leaf_pinhole"))
                // A pinhole without any extrinsics which is also a leaf.
                .with_archetype(RowId::new(), TimePoint::STATIC, &test_pinhole())
                .build()?,
        ))?;

        entity_db.add_chunk(&Arc::new(
            Chunk::builder(EntityPath::from("top/pinhole/child2d"))
                .with_archetype_auto_row(
                    TimePoint::STATIC,
                    &archetypes::Transform3D::from_translation([2.0, 0.0, 0.0]),
                )
                .build()?,
        ))?;
        entity_db.add_chunk(&Arc::new(
            Chunk::builder(EntityPath::from("top/child3d"))
                .with_archetype_auto_row(
                    TimePoint::STATIC,
                    &archetypes::Transform3D::from_translation([0.0, 0.0, 1.0]),
                )
                .build()?,
        ))?;

        Ok(entity_db)
    }

    fn pretty_print_transform_frame_ids_in<T: std::fmt::Debug>(
        obj: T,
        transform_cache: &TransformResolutionCache,
    ) -> String {
        let mut result = format!("{obj:#?}");
        for (hash, frame) in transform_cache.frame_id_registry().iter_frame_ids() {
            result = result.replace(&format!("{hash:#?}"), &format!("{frame}"));
        }
        result
    }

    #[test]
    fn test_simple_entity_hierarchy() -> Result<(), Box<dyn std::error::Error>> {
        let test_scene = entity_hierarchy_test_scene()?;
        let mut transform_cache = TransformResolutionCache::new(&test_scene);
        transform_cache.ensure_timeline_is_initialized(
            test_scene.storage_engine().store(),
            TimelineName::log_tick(),
        );

        let query = LatestAtQuery::latest(TimelineName::log_tick());
        let transform_forest = TransformForest::new(&test_scene, &transform_cache, &query);
        assert!(!transform_forest.any_missing_chunks());

        // Check that we get the expected roots.
        {
            assert_eq!(
                transform_forest.root_info(TransformFrameIdHash::entity_path_hierarchy_root()),
                Some(&TransformTreeRootInfo::TransformFrameRoot)
            );
            assert_eq!(
                transform_forest.root_info(TransformFrameIdHash::from_entity_path(
                    &EntityPath::from("top/pinhole")
                )),
                Some(&TransformTreeRootInfo::Pinhole(PinholeTreeRoot {
                    parent_tree_root: TransformFrameIdHash::entity_path_hierarchy_root(),
                    pinhole_projection: test_resolved_pinhole(
                        TransformFrameIdHash::from_entity_path(&EntityPath::from("top"))
                    ),
                    parent_root_from_pinhole_root: glam::DAffine3::from_translation(glam::dvec3(
                        1.0, 1.0, 0.0
                    )),
                }))
            );
            assert_eq!(
                transform_forest.root_info(TransformFrameIdHash::from_entity_path(
                    &EntityPath::from("top/pure_leaf_pinhole")
                )),
                Some(&TransformTreeRootInfo::Pinhole(PinholeTreeRoot {
                    parent_tree_root: TransformFrameIdHash::entity_path_hierarchy_root(),
                    pinhole_projection: test_resolved_pinhole(
                        TransformFrameIdHash::from_entity_path(&EntityPath::from("top"))
                    ),
                    parent_root_from_pinhole_root: glam::DAffine3::from_translation(glam::dvec3(
                        1.0, 0.0, 0.0
                    )),
                }))
            );
            assert_eq!(transform_forest.roots.len(), 3);
        }

        // Perform some tree queries.
        let target_paths = [
            EntityPath::root(),
            EntityPath::from("top"),
            EntityPath::from("top/pinhole"),
            EntityPath::from("top/nonexistent"),
            EntityPath::from("top/pinhole/child2d"),
            EntityPath::from("top/pure_leaf_pinhole"),
        ];
        let source_paths = [
            EntityPath::root(),
            EntityPath::from("top"),
            EntityPath::from("top/pinhole"),
            EntityPath::from("top/child3d"),
            EntityPath::from("top/nonexistent"),
            EntityPath::from("top/pinhole/child2d"),
            EntityPath::from("top/pure_leaf_pinhole"),
        ];

        for target in &target_paths {
            let name = if target == &EntityPath::root() {
                "_root".to_owned()
            } else {
                target.to_string().replace('/', "_")
            };

            let target_frame = TransformFrameIdHash::from_entity_path(target);
            let result = transform_forest
                .transform_from_to(
                    target_frame,
                    source_paths
                        .iter()
                        .map(TransformFrameIdHash::from_entity_path),
                    &|_| 1.0,
                )
                .collect::<Vec<_>>();

            // If the target exists, it should have an identity transform.
            // (this is covered by the snapshot below as well, but its a basic sanity check I wanted to call out)
            let target_result = result.iter().find(|(key, _)| *key == target_frame).unwrap();
            if let Ok(target_result) = &target_result.1 {
                assert_eq!(target_result.target_from_source, glam::DAffine3::IDENTITY);
            } else {
                assert_eq!(
                    target_result.1,
                    Err(TransformFromToError::UnknownTargetFrame(target_frame))
                );
            }

            insta::assert_snapshot!(
                format!("simple_entity_hierarchy__transform_from_to_{}", name),
                pretty_print_transform_frame_ids_in(&result, &transform_cache)
            );
        }

        Ok(())
    }

    /// A test scene that exclusively uses the parent/child frame ids.
    ///
    /// We're using relatively basic transforms here as we assume that resolving transforms have been tested on [`TransformResolutionCache`] already.
    /// Similarly, since [`TransformForest`] does not yet maintain anything over time, we're using static timing instead.
    ///
    /// Tree structure:
    /// ```text
    /// root
    /// ├── top
    /// │    ├── child0
    /// │    └── child1
    /// └── pinhole
    ///      └── child2d
    /// ```
    fn simple_frame_hierarchy_test_scene(
        multiple_entities: bool,
    ) -> Result<EntityDb, Box<dyn std::error::Error>> {
        let mut entity_db = EntityDb::new(StoreInfo::testing().store_id);
        entity_db.add_chunk(&Arc::new(
            Chunk::builder(EntityPath::from(if multiple_entities {
                "transforms0"
            } else {
                "tf"
            }))
            .with_archetype_auto_row(
                [(Timeline::log_tick(), 0)],
                &archetypes::Transform3D::from_translation([1.0, 0.0, 0.0])
                    .with_child_frame("top")
                    .with_parent_frame("root"),
            )
            .build()?,
        ))?;

        entity_db.add_chunk(&Arc::new(
            Chunk::builder(EntityPath::from(if multiple_entities {
                "transforms1"
            } else {
                "tf"
            }))
            .with_archetype_auto_row(
                [(Timeline::log_tick(), 0)],
                &archetypes::Transform3D::from_translation([2.0, 0.0, 0.0])
                    .with_child_frame("child0")
                    .with_parent_frame("top"),
            )
            .build()?,
        ))?;
        entity_db.add_chunk(&Arc::new(
            Chunk::builder(EntityPath::from(if multiple_entities {
                "transforms2"
            } else {
                "tf"
            }))
            .with_archetype_auto_row(
                [(Timeline::log_tick(), 0)],
                &archetypes::Transform3D::from_translation([3.0, 0.0, 0.0])
                    .with_child_frame("child1")
                    .with_parent_frame("top"),
            )
            .build()?,
        ))?;
        entity_db.add_chunk(&Arc::new(
            Chunk::builder(EntityPath::from(if multiple_entities {
                "transforms3"
            } else {
                "tf"
            }))
            .with_archetype_auto_row(
                [(Timeline::log_tick(), 0)],
                &archetypes::Transform3D::from_translation([0.0, 1.0, 0.0])
                    .with_child_frame("pinhole")
                    .with_parent_frame("root"),
            )
            .with_archetype(
                RowId::new(),
                [(Timeline::log_tick(), 0)],
                &test_pinhole()
                    .with_child_frame("pinhole")
                    .with_parent_frame("root"),
            )
            .build()?,
        ))?;
        entity_db.add_chunk(&Arc::new(
            Chunk::builder(EntityPath::from(if multiple_entities {
                "transforms4"
            } else {
                "tf"
            }))
            .with_archetype_auto_row(
                [(Timeline::log_tick(), 0)],
                &archetypes::Transform3D::from_translation([0.0, 2.0, 0.0])
                    .with_child_frame("child2d")
                    .with_parent_frame("pinhole"),
            )
            .build()?,
        ))?;

        Ok(entity_db)
    }

    fn test_simple_frame_hierarchy(
        multiple_entities: bool,
    ) -> Result<(), Box<dyn std::error::Error>> {
        let test_scene = simple_frame_hierarchy_test_scene(multiple_entities)?;
        let mut transform_cache = TransformResolutionCache::new(&test_scene);
        transform_cache.ensure_timeline_is_initialized(
            test_scene.storage_engine().store(),
            TimelineName::log_tick(),
        );

        let query = LatestAtQuery::latest(TimelineName::log_tick());
        let transform_forest = TransformForest::new(&test_scene, &transform_cache, &query);
        assert!(!transform_forest.any_missing_chunks());

        // Check that we get the expected roots.
        {
            assert_eq!(
                transform_forest.root_info(TransformFrameIdHash::entity_path_hierarchy_root()),
                Some(&TransformTreeRootInfo::TransformFrameRoot)
            );
            assert_eq!(
                transform_forest.root_info(TransformFrameIdHash::from_str("root")),
                Some(&TransformTreeRootInfo::TransformFrameRoot)
            );
            assert_eq!(
                transform_forest.root_info(TransformFrameIdHash::from_str("pinhole")),
                Some(&TransformTreeRootInfo::Pinhole(PinholeTreeRoot {
                    parent_tree_root: TransformFrameIdHash::from_str("root"),
                    pinhole_projection: test_resolved_pinhole(TransformFrameIdHash::from_str(
                        "root"
                    )),
                    parent_root_from_pinhole_root: glam::DAffine3::from_translation(glam::dvec3(
                        0.0, 1.0, 0.0
                    )),
                }))
            );
            assert_eq!(transform_forest.roots.len(), 3);
        }

        // Check that there is no connection between the implicit & explicit frames.
        let implicit_frame = if multiple_entities {
            TransformFrameIdHash::from_entity_path(&"transforms2".into())
        } else {
            TransformFrameIdHash::from_entity_path(&"tf".into())
        };
        assert_eq!(
            transform_forest
                .transform_from_to(
                    TransformFrameIdHash::from_str("child0"),
                    std::iter::once(implicit_frame),
                    &|_| 0.0
                )
                .collect_vec(),
            vec![(
                implicit_frame,
                Err(TransformFromToError::NoPathBetweenFrames {
                    target: TransformFrameIdHash::from_str("child0"),
                    src: implicit_frame,
                    target_root: TransformFrameIdHash::from_str("root"),
                    source_root: TransformFrameIdHash::entity_path_hierarchy_root(),
                })
            )]
        );

        // Check that for our two trees everything is connected with everything
        let implicit_frames = if multiple_entities {
            vec![
                TransformFrameId::from_entity_path(&"transforms0".into()),
                TransformFrameId::from_entity_path(&"transforms1".into()),
                TransformFrameId::from_entity_path(&"transforms2".into()),
                TransformFrameId::from_entity_path(&EntityPath::root()),
            ]
        } else {
            vec![TransformFrameId::from_entity_path(&"tf".into())]
        };
        for tree_elements in [
            [
                TransformFrameId::new("top"),
                TransformFrameId::new("root"),
                TransformFrameId::new("child0"),
                TransformFrameId::new("child1"),
                TransformFrameId::new("pinhole"),
                TransformFrameId::new("child2d"),
            ]
            .iter(),
            implicit_frames.iter(),
        ] {
            for pair in tree_elements.permutations(2) {
                let from = pair[0];
                let to = pair[1];
                assert!(
                    matches!(
                        transform_forest
                            .transform_from_to(
                                TransformFrameIdHash::new(from),
                                std::iter::once(TransformFrameIdHash::new(to)),
                                &|_| 1.0
                            )
                            .next(),
                        Some((_, Ok(_)))
                    ),
                    "Connection from {from:?} to {to:?}"
                );
            }
        }

        // Blanket check that we have all the right connections. A bit redundant to above checks, but not as stable due to encompassing snapshotting.
        // We don't test for tree rearrangement here, this has been already tested quite a bit in `test_simple_entity_hierarchy`
        insta::assert_snapshot!(
            if multiple_entities {
                "simple_frame_hierarchy__multiple_entities"
            } else {
                "simple_frame_hierarchy__all_on_single_entity"
            },
            pretty_print_transform_frame_ids_in(
                &transform_forest.root_from_frame,
                &transform_cache
            )
        );

        Ok(())
    }

    #[test]
    fn test_simple_frame_hierarchy_multiple_entities() -> Result<(), Box<dyn std::error::Error>> {
        test_simple_frame_hierarchy(true)
    }

    #[test]
    fn test_simple_frame_hierarchy_all_on_single_entity() -> Result<(), Box<dyn std::error::Error>>
    {
        test_simple_frame_hierarchy(false)
    }

    #[test]
    fn test_handling_unknown_frames_gracefully() -> Result<(), Box<dyn std::error::Error>> {
        let query = LatestAtQuery::latest(TimelineName::log_tick());

        // Handle empty store & cache.
        {
            let test_scene = EntityDb::new(StoreInfo::testing().store_id);
            let transform_cache = TransformResolutionCache::default();
            let transform_forest = TransformForest::new(&test_scene, &transform_cache, &query);
            assert!(!transform_forest.any_missing_chunks());

            assert_eq!(
                transform_forest
                    .transform_from_to(
                        TransformFrameIdHash::from_str("top"),
                        std::iter::once(TransformFrameIdHash::from_str("child0")),
                        &|_| 1.0
                    )
                    .collect::<Vec<_>>(),
                vec![(
                    TransformFrameIdHash::from_str("child0"),
                    Err(TransformFromToError::UnknownTargetFrame(
                        TransformFrameIdHash::from_str("top")
                    ))
                )]
            );
        }

        // Handle creation from empty cache but full store gracefully.
        {
            let transform_cache = TransformResolutionCache::default();
            let test_scene = simple_frame_hierarchy_test_scene(true)?;
            let transform_forest = TransformForest::new(&test_scene, &transform_cache, &query);
            assert!(!transform_forest.any_missing_chunks());

            // The forest doesn't know about any of the frames despite having seen the populated store.
            assert_eq!(
                transform_forest
                    .transform_from_to(
                        TransformFrameIdHash::from_str("top"),
                        std::iter::once(TransformFrameIdHash::from_str("child0")),
                        &|_| 1.0
                    )
                    .collect::<Vec<_>>(),
                vec![(
                    TransformFrameIdHash::from_str("child0"),
                    Err(TransformFromToError::UnknownTargetFrame(
                        TransformFrameIdHash::from_str("top")
                    ))
                )]
            );
        }

        // Handle creation from partially filled cache gracefully.
        {
            let mut test_scene = simple_frame_hierarchy_test_scene(true)?;
            let mut transform_cache = TransformResolutionCache::new(&test_scene);
            transform_cache.ensure_timeline_is_initialized(
                test_scene.storage_engine().store(),
                query.timeline(),
            );

            // Add a connection the cache doesn't know about.
            test_scene.add_chunk(&Arc::new(
                Chunk::builder(EntityPath::from("transforms"))
                    .with_archetype_auto_row(
                        [(query.timeline(), TimeCell::from_sequence(0))],
                        &archetypes::Transform3D::from_translation([4.0, 0.0, 0.0])
                            .with_child_frame("child2")
                            .with_parent_frame("top"),
                    )
                    .build()?,
            ))?;
            // But the forest seen the scene with it.
            let transform_forest = TransformForest::new(&test_scene, &transform_cache, &query);
            assert!(!transform_forest.any_missing_chunks());

            // Forest doesn't know about the newly added `child2` frame.
            assert_eq!(
                transform_forest
                    .transform_from_to(
                        TransformFrameIdHash::from_str("top"),
                        std::iter::once(TransformFrameIdHash::from_str("child2")),
                        &|_| 1.0
                    )
                    .collect::<Vec<_>>(),
                vec![(
                    TransformFrameIdHash::from_str("child2"),
                    Err(TransformFromToError::UnknownSourceFrame(
                        TransformFrameIdHash::from_str("child2")
                    ))
                )]
            );
        }

        // Extra nasty case: given a cold cache, the cache knows about everything except for a row _on the same time_ which talks about a new frame.
        // (this also makes sure that we get the right transform back for the known frames even when a latest-at query would yield something the cache doesn't know about)
        {
            let mut test_scene = simple_frame_hierarchy_test_scene(true)?;

            test_scene.add_chunk(&Arc::new(
                Chunk::builder(EntityPath::from("transforms"))
                    .with_archetype_auto_row(
                        [(query.timeline(), TimeCell::from_sequence(0))],
                        &archetypes::Transform3D::from_translation([4.0, 0.0, 0.0])
                            .with_child_frame("child2")
                            .with_parent_frame("top"),
                    )
                    .build()?,
            ))?;
            let mut transform_cache = TransformResolutionCache::new(&test_scene);
            transform_cache.ensure_timeline_is_initialized(
                test_scene.storage_engine().store(),
                query.timeline(),
            );

            test_scene.add_chunk(&Arc::new(
                // Add a connection the cache doesn't know about.
                Chunk::builder(EntityPath::from("transforms"))
                    .with_archetype_auto_row(
                        [(query.timeline(), TimeCell::from_sequence(0))], // Same time before, different parent frame!
                        &archetypes::Transform3D::from_translation([5.0, 0.0, 0.0])
                            .with_child_frame("child2")
                            .with_parent_frame("new_top"),
                    )
                    .build()?,
            ))?;
            let transform_forest = TransformForest::new(&test_scene, &transform_cache, &query);
            assert!(!transform_forest.any_missing_chunks());

            // Forest sees the new relationship despite not having it reported since the cold cache will pick it up.
            assert_eq!(
                transform_forest
                    .transform_from_to(
                        TransformFrameIdHash::from_str("child2"),
                        std::iter::once(TransformFrameIdHash::from_str("new_top")),
                        &|_| 1.0
                    )
                    .collect::<Vec<_>>(),
                vec![(
                    TransformFrameIdHash::from_str("new_top"),
                    Ok(TreeTransform {
                        root: TransformFrameIdHash::from_str("new_top"),
                        target_from_source: glam::DAffine3::from_translation(glam::dvec3(
                            -5.0, 0.0, 0.0
                        )),
                    })
                )]
            );
            assert_eq!(
                transform_forest
                    .transform_from_to(
                        TransformFrameIdHash::from_str("child2"),
                        std::iter::once(TransformFrameIdHash::from_str("top")),
                        &|_| 1.0
                    )
                    .collect::<Vec<_>>(),
                vec![(
                    TransformFrameIdHash::from_str("top"),
                    Err(TransformFromToError::NoPathBetweenFrames {
                        target: TransformFrameIdHash::from_str("child2"),
                        src: TransformFrameIdHash::from_str("top"),
                        target_root: TransformFrameIdHash::from_str("new_top"),
                        source_root: TransformFrameIdHash::from_str("root"),
                    })
                )]
            );
        }

        Ok(())
    }

    #[test]
    fn test_implicit_transform_at_root_being_ignored_with_warning()
    -> Result<(), Box<dyn std::error::Error>> {
        re_log::setup_logging();
        let (logger, log_rx) = re_log::ChannelLogger::new(re_log::LevelFilter::Warn);
        re_log::add_boxed_logger(Box::new(logger)).expect("Failed to add logger");

        let mut entity_db = EntityDb::new(StoreInfo::testing().store_id);

        // Add a transform that tries to make a root frame a child of something else
        entity_db.add_chunk(&Arc::new(
            Chunk::builder(EntityPath::root())
                .with_archetype_auto_row(
                    TimePoint::STATIC,
                    &archetypes::Transform3D::from_translation([1.0, 0.0, 0.0]),
                )
                .build()?,
        ))?;
        entity_db.add_chunk(&Arc::new(
            Chunk::builder("/child")
                .with_archetype_auto_row(
                    TimePoint::STATIC,
                    &archetypes::Transform3D::from_translation([0.0, 1.0, 0.0]),
                )
                .build()?,
        ))?;

        let query = LatestAtQuery::latest(TimelineName::log_tick());
        let mut transform_cache = TransformResolutionCache::new(&entity_db);
        transform_cache
            .ensure_timeline_is_initialized(entity_db.storage_engine().store(), query.timeline());
        let transform_forest = TransformForest::new(&entity_db, &transform_cache, &query);
        assert!(!transform_forest.any_missing_chunks());

        // Child still connects up to the root.
        assert_eq!(
            transform_forest
                .transform_from_to(
                    TransformFrameIdHash::from_entity_path(&"child".into()),
                    std::iter::once(TransformFrameIdHash::from_entity_path(&EntityPath::root())),
                    &|_| 1.0
                )
                .collect::<Vec<_>>(),
            vec![(
                TransformFrameIdHash::from_entity_path(&EntityPath::root()),
                Ok(TreeTransform {
                    root: TransformFrameIdHash::from_entity_path(&EntityPath::root()),
                    target_from_source: glam::DAffine3::from_translation(glam::dvec3(
                        0.0, -1.0, 0.0
                    )),
                })
            )]
        );

        let received_log = log_rx.try_recv()?;
        assert_eq!(received_log.level, re_log::Level::Warn);
        assert!(
            received_log
                .msg
                .contains("Ignoring transform at root entity"),
            "Expected warning about ignoring implicit root parent frame, got: {}",
            received_log.msg
        );

        Ok(())
    }
}