re_space_view/
heuristics.rs

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
use re_viewer_context::{
    ApplicableEntities, IdentifiedViewSystem, RecommendedSpaceView, SpaceViewClass,
    SpaceViewSpawnHeuristics, ViewerContext, VisualizerSystem,
};

/// Spawns a space view for each single entity which is visualizable & indicator-matching for a given visualizer.
///
/// This is used as utility by *some* space view types that want
/// to spawn a space view for every single entity that is visualizable with a given visualizer.
pub fn suggest_space_view_for_each_entity<TVisualizer>(
    ctx: &ViewerContext<'_>,
    space_view: &impl SpaceViewClass,
) -> SpaceViewSpawnHeuristics
where
    TVisualizer: VisualizerSystem + IdentifiedViewSystem + Default,
{
    re_tracing::profile_function!();

    let Some(indicator_matching_entities) = ctx
        .indicated_entities_per_visualizer
        .get(&TVisualizer::identifier())
    else {
        return Default::default();
    };
    let Some(applicable_entities) = ctx
        .applicable_entities_per_visualizer
        .get(&TVisualizer::identifier())
    else {
        return Default::default();
    };

    let visualizer = TVisualizer::default();
    let recommended_space_views = applicable_entities
        .intersection(indicator_matching_entities)
        .filter_map(|entity| {
            let context = space_view.visualizable_filter_context(entity, ctx.recording());
            if visualizer
                .filter_visualizable_entities(
                    ApplicableEntities(std::iter::once(entity.clone()).collect()),
                    context.as_ref(),
                )
                .is_empty()
            {
                None
            } else {
                Some(RecommendedSpaceView::new_single_entity(entity.clone()))
            }
        });

    re_viewer_context::SpaceViewSpawnHeuristics::new(recommended_space_views)
}