re_tf 0.31.3

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
use std::sync::Arc;

use ahash::HashMap;
use parking_lot::{ArcRwLockReadGuard, RawRwLock, RwLock};
use re_byte_size::SizeBytes;
use re_chunk_store::ChunkStore;
use re_entity_db::EntityDb;
use re_log::{debug_assert, debug_assert_eq};
use re_log_encoding::RrdManifest;
use re_log_types::{TimeInt, TimelineName};
use re_sdk_types::archetypes;

use crate::frame_id_registry::FrameIdRegistry;
use crate::transform_aspect::TransformAspect;

use super::cached_transforms_for_timeline::CachedTransformsForTimeline;
use super::iter_child_frames_in_chunk;

type ArcRwLock<T> = Arc<RwLock<T>>;

/// Resolves all transform relationship defining components to affine transforms for fast lookup.
///
/// It only handles resulting transforms individually to each frame connection, not how these transforms propagate in the tree.
/// For transform tree propagation see [`crate::TransformForest`].
///
/// There are different kinds of transforms handled here:
/// * [`archetypes::Transform3D`]
///   Tree transforms that should propagate in the tree (via [`crate::TransformForest`]).
/// * [`re_sdk_types::components::PinholeProjection`] and [`re_sdk_types::components::ViewCoordinates`]
///   Pinhole projections & associated view coordinates used for visualizing cameras in 3D and embedding 2D in 3D
/// * [`archetypes::InstancePoses3D`]
///   Instance poses that should be applied to the tree transforms (via [`crate::TransformForest`]) but not propagate.
///   Also unlike tree transforms, these are not associated with transform frames but rather with entity paths.
pub struct TransformResolutionCache {
    /// The frame id registry is co-located in the resolution cache for convenience:
    /// the resolution cache is often the lowest level of transform access and
    /// thus allowing us to access debug information across the stack.
    frame_id_registry: ArcRwLock<FrameIdRegistry>,

    /// The timelines for which we have cached transforms for.
    ///
    /// Some timelines may be missing from this map.
    /// They will be lazily initialized from scratch on-demand.
    per_timeline: HashMap<TimelineName, ArcRwLock<CachedTransformsForTimeline>>,

    static_timeline: ArcRwLock<CachedTransformsForTimeline>,
}

impl Default for TransformResolutionCache {
    #[inline]
    fn default() -> Self {
        Self {
            frame_id_registry: Default::default(),
            per_timeline: Default::default(),
            static_timeline: Arc::new(RwLock::new(CachedTransformsForTimeline::new_static())),
        }
    }
}

impl TransformResolutionCache {
    /// Creates a new cache, initialized with the static timeline and frame registry from the given entity database.
    ///
    /// Per-timeline data will be lazily initialized on demand.
    pub fn new(entity_db: &EntityDb) -> Self {
        re_tracing::profile_function!();

        let mut cache = Self::default();

        if let Some(manifest) = entity_db.rrd_manifest_index().manifest() {
            cache.register_manifest(manifest);
        }

        for chunk in entity_db.storage_engine().store().iter_physical_chunks() {
            // Register all frames even if this chunk doesn't have transform data.
            cache
                .frame_id_registry
                .write()
                .register_all_frames_in_chunk(chunk);

            let aspects = TransformAspect::transform_aspects_of(chunk);
            if aspects.is_empty() {
                continue;
            }

            // Only process static chunks - temporal data will be lazily initialized per-timeline.
            if chunk.is_static() {
                cache.add_static_chunk(chunk, aspects);
            }
        }

        cache
    }
}

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

        let Self {
            frame_id_registry,
            per_timeline,
            static_timeline,
        } = self;

        frame_id_registry.heap_size_bytes()
            + per_timeline.heap_size_bytes()
            + static_timeline.heap_size_bytes()
    }
}

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

        let Self {
            frame_id_registry,
            per_timeline,
            static_timeline,
        } = self;

        let mut per_timeline_node = re_byte_size::MemUsageNode::new();
        for (timeline, cached_transforms) in per_timeline {
            per_timeline_node = per_timeline_node.with_child(
                timeline.to_string(),
                cached_transforms.read().capture_mem_usage_tree(),
            );
        }

        re_byte_size::MemUsageNode::new()
            .with_child("frame_id_registry", frame_id_registry.total_size_bytes())
            .with_child("per_timeline", per_timeline_node.into_tree())
            .with_child("static_timeline", static_timeline.total_size_bytes())
            .into_tree()
    }
}

impl TransformResolutionCache {
    /// Returns the registry of all known frame ids.
    #[inline]
    pub fn frame_id_registry(&self) -> ArcRwLockReadGuard<RawRwLock, FrameIdRegistry> {
        self.frame_id_registry.read_arc()
    }

    /// Accesses the transform component tracking data for a given timeline.
    #[inline]
    pub fn transforms_for_timeline(
        &self,
        timeline: TimelineName,
    ) -> ArcRwLockReadGuard<RawRwLock, CachedTransformsForTimeline> {
        if let Some(per_timeline) = self.per_timeline.get(&timeline) {
            per_timeline.read_arc()
        } else {
            self.static_timeline.read_arc()
        }
    }

    /// Returns an iterator over all initialized timelines.
    #[inline]
    pub fn cached_timelines(&self) -> impl Iterator<Item = TimelineName> + '_ {
        self.per_timeline.keys().copied()
    }

    /// Ensure we have a cache for this timeline.
    pub fn ensure_timeline_is_initialized(
        &mut self,
        chunk_store: &ChunkStore,
        timeline: TimelineName,
    ) {
        re_tracing::profile_function!(timeline);

        let static_timeline = self.static_timeline.read();
        let frame_id_registry = self.frame_id_registry.read();

        self.per_timeline.entry(timeline).or_insert_with(|| {
            Arc::new(RwLock::new(CachedTransformsForTimeline::new_temporal(
                timeline,
                &static_timeline,
                &frame_id_registry,
                chunk_store,
            )))
        });
    }

    /// Evicts a timeline from the cache.
    pub fn evict_timeline_cache(&mut self, timeline: TimelineName) {
        re_tracing::profile_function!(); // There can be A LOT of tiny allocations to drop.
        self.per_timeline.remove(&timeline);
    }

    /// Makes sure the internal transform index is up to date and outdated cache entries are discarded.
    ///
    /// This needs to be called once per frame prior to any transform propagation.
    /// (which is done by [`crate::TransformForest`])
    ///
    /// This will internally…
    /// * keep track of which child frames are influenced by which entity
    /// * create empty entries for where transforms may change over time (may happen conservatively - creating more entries than needed)
    ///     * this may invalidate previous entries at the same position
    /// * remove cached entries if chunks were GC'ed
    pub fn process_store_events<'a>(
        &mut self,
        events: impl Iterator<Item = &'a re_chunk_store::ChunkStoreEvent>,
    ) {
        re_tracing::profile_function!();

        for event in events {
            match &**event {
                re_chunk_store::ChunkStoreDiff::Addition(addition) => {
                    let delta_chunk = addition.delta_chunk();

                    // Since entity paths lead to implicit frames, we have to prime our lookup table
                    // with them even if this chunk doesn't have transform data.
                    self.frame_id_registry
                        .write()
                        .register_all_frames_in_chunk(delta_chunk);

                    let aspects = TransformAspect::transform_aspects_of(delta_chunk);
                    if !aspects.is_empty() {
                        if delta_chunk.is_static() {
                            self.add_static_chunk(delta_chunk, aspects);
                        } else {
                            self.add_temporal_chunk(delta_chunk, aspects);
                        }
                    }
                }

                re_chunk_store::ChunkStoreDiff::VirtualAddition(addition) => {
                    self.register_manifest(&addition.rrd_manifest);
                }

                re_chunk_store::ChunkStoreDiff::Deletion(deletion) => {
                    let aspects = TransformAspect::transform_aspects_of(&deletion.chunk);
                    if !aspects.is_empty() {
                        self.remove_chunk(&deletion.chunk, aspects);
                    }
                }

                re_chunk_store::ChunkStoreDiff::SchemaAddition(_) => {}
            }
        }
    }

    fn register_manifest(&self, manifest: &RrdManifest) {
        re_tracing::profile_function!();

        // Make all the entity paths known as potential transform paths.
        let mut frame_id_registry = self.frame_id_registry.write();
        for entity_path in manifest.recording_schema().all_entities() {
            frame_id_registry.register_frame_id_from_entity_path(entity_path);
        }
    }

    fn add_temporal_chunk(&self, chunk: &re_chunk_store::Chunk, aspects: TransformAspect) {
        re_tracing::profile_function!(format!(
            "{} rows, {}",
            chunk.num_rows(),
            chunk.entity_path()
        ));

        debug_assert!(!chunk.is_static());

        let static_timeline = self.static_timeline.read();
        let frame_id_registry = self.frame_id_registry.read();

        for timeline in chunk.timelines().keys() {
            // Skip timelines that haven't been requested yet (lazy initialization).
            let Some(per_timeline) = self.per_timeline.get(timeline) else {
                continue;
            };
            let mut per_timeline = per_timeline.write();

            per_timeline.add_temporal_chunk(
                chunk,
                aspects,
                *timeline,
                &static_timeline,
                &frame_id_registry,
            );
        }
    }

    fn add_static_chunk(&mut self, chunk: &re_chunk_store::Chunk, aspects: TransformAspect) {
        re_tracing::profile_function!();

        debug_assert!(chunk.is_static());

        let entity_path = chunk.entity_path();
        let place_holder_timeline = TimelineName::new("ignored for static chunk");

        let transform_child_frame_component =
            archetypes::Transform3D::descriptor_child_frame().component;
        let pinhole_child_frame_component = archetypes::Pinhole::descriptor_child_frame().component;

        let mut static_timeline = self.static_timeline.write();
        let frame_id_registry = self.frame_id_registry.read();

        // Add a static transform invalidation to affected child frames on ALL timelines.

        if aspects.contains(TransformAspect::Frame) {
            for (time, frame) in iter_child_frames_in_chunk(
                chunk,
                place_holder_timeline,
                transform_child_frame_component,
            ) {
                debug_assert_eq!(time, TimeInt::STATIC);

                let frame_transforms = static_timeline.get_or_create_tree_transforms_static(
                    entity_path,
                    frame,
                    &frame_id_registry,
                );
                frame_transforms.invalidate_transform_at(TimeInt::STATIC);

                #[cfg_attr(not(debug_assertions), expect(clippy::for_kv_map))]
                for (_timeline, per_timeline) in &mut self.per_timeline {
                    // Don't call `get_or_create_tree_transforms_temporal` here since we may not yet know a temporal entity that this is associated with.
                    // Also, this may be the first time we associate with a static entity instead which `get_or_create_tree_transforms_static` takes care of.
                    let mut per_timeline_guard = per_timeline.write();
                    let transforms = per_timeline_guard.get_or_create_tree_transforms_static(
                        entity_path,
                        frame,
                        &frame_id_registry,
                    );
                    transforms.invalidate_transform_at(TimeInt::STATIC);

                    // Entry might have been newly created. Have to ensure that its associated with the right timeline.
                    #[cfg(debug_assertions)]
                    {
                        transforms.timeline = Some(*_timeline);
                    }
                }
            }
        }
        if aspects.contains(TransformAspect::Pose) {
            let frame_transforms =
                static_timeline.get_or_create_pose_transforms_static(entity_path);
            frame_transforms.invalidate_at(TimeInt::STATIC);

            for per_timeline in self.per_timeline.values_mut() {
                per_timeline
                    .write()
                    .get_or_create_pose_transforms_temporal(entity_path, &static_timeline)
                    .invalidate_at(TimeInt::STATIC);
            }
        }
        if aspects.contains(TransformAspect::PinholeOrViewCoordinates) {
            for (time, frame) in iter_child_frames_in_chunk(
                chunk,
                place_holder_timeline,
                pinhole_child_frame_component,
            ) {
                debug_assert_eq!(time, TimeInt::STATIC);

                let frame_transforms = static_timeline.get_or_create_tree_transforms_static(
                    entity_path,
                    frame,
                    &frame_id_registry,
                );
                frame_transforms.invalidate_pinhole_projection_at(TimeInt::STATIC);

                #[cfg_attr(not(debug_assertions), expect(clippy::for_kv_map))]
                for (_timeline, per_timeline) in &mut self.per_timeline {
                    // Don't call `get_or_create_tree_transforms_temporal` here since we may not yet know a temporal entity that this is associated with.
                    // Also, this may be the first time we associate with a static entity instead which `get_or_create_tree_transforms_static` takes care of.
                    let mut per_timeline_guard = per_timeline.write();
                    let transforms = per_timeline_guard.get_or_create_tree_transforms_static(
                        entity_path,
                        frame,
                        &frame_id_registry,
                    );
                    transforms.invalidate_pinhole_projection_at(TimeInt::STATIC);

                    // Entry might have been newly created. Have to ensure that its associated with the right timeline.
                    #[cfg(debug_assertions)]
                    {
                        transforms.timeline = Some(*_timeline);
                    }
                }
            }
        }

        // Don't care about clears here, they don't have any effect for keeping track of changes when logged static.
    }

    fn remove_chunk(&mut self, chunk: &re_chunk_store::Chunk, aspects: TransformAspect) {
        re_tracing::profile_function!();

        // TODO(andreas): handle removal of static chunks?
        for timeline in chunk.timelines().keys() {
            let Some(per_timeline_rw) = self.per_timeline.get_mut(timeline) else {
                continue;
            };

            let mut per_timeline = per_timeline_rw.write();
            per_timeline.remove_chunk(chunk, aspects, *timeline);

            // Remove the entire timeline if it's empty.
            let is_empty = per_timeline.per_child_frame_transforms.is_empty();
            drop(per_timeline);
            if is_empty {
                self.per_timeline.remove(timeline);
            }
        }
    }
}