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
use re_log_types::EntityPath;
use re_renderer::{renderer::TexturedRect, QueueableDrawData};
use re_types::components::DepthMeter;
use re_viewer_context::{ImageInfo, SpaceViewSystemExecutionError};

#[derive(Clone)]
pub enum PickableRectSourceData {
    /// The rectangle is an image with pixel data, potentially some depth meta information.
    Image {
        image: ImageInfo,
        depth_meter: Option<DepthMeter>,
    },

    /// The rectangle is a frame in a video.
    Video,

    /// The rectangle represents an error icon.
    ErrorPlaceholder,
}

/// Image rectangle that can be picked in the view.
pub struct PickableTexturedRect {
    /// Path to the image (note image instance ids would refer to pixels!)
    pub ent_path: EntityPath,

    /// Textured rectangle used by the renderer.
    pub textured_rect: TexturedRect,

    /// Associated data.
    pub source_data: PickableRectSourceData,
}

impl PickableTexturedRect {
    /// Resolution of the underlying texture.
    pub fn resolution(&self) -> [u32; 2] {
        self.textured_rect.colormapped_texture.width_height()
    }

    pub fn to_draw_data(
        render_ctx: &re_renderer::RenderContext,
        rects: &[Self],
    ) -> Result<QueueableDrawData, SpaceViewSystemExecutionError> {
        // TODO(wumpf): Can we avoid this copy, maybe let DrawData take an iterator?
        let rectangles = rects
            .iter()
            .map(|image| image.textured_rect.clone())
            .collect::<Vec<_>>();
        match re_renderer::renderer::RectangleDrawData::new(render_ctx, &rectangles) {
            Ok(draw_data) => Ok(draw_data.into()),
            Err(err) => Err(SpaceViewSystemExecutionError::DrawDataCreationError(
                Box::new(err),
            )),
        }
    }
}