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, RecommendedView, ViewClass, ViewSpawnHeuristics,
    ViewerContext, VisualizerSystem,
};

/// Spawns a view for each single entity which is visualizable & indicator-matching for a given visualizer.
///
/// This is used as utility by *some* view types that want
/// to spawn a view for every single entity that is visualizable with a given visualizer.
pub fn suggest_view_for_each_entity<TVisualizer>(
    ctx: &ViewerContext<'_>,
    view: &impl ViewClass,
) -> ViewSpawnHeuristics
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_views = applicable_entities
        .intersection(indicator_matching_entities)
        .filter_map(|entity| {
            let context = 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(RecommendedView::new_single_entity(entity.clone()))
            }
        });

    re_viewer_context::ViewSpawnHeuristics::new(recommended_views)
}