rvlib/tools/bbox/
core.rs

1use crate::{
2    annotations_accessor_mut,
3    drawme::{Annotation, BboxAnnotation, Stroke},
4    events::{Events, KeyCode},
5    history::{History, Record},
6    instance_annotations_accessor, make_tool_transform,
7    result::trace_ok_err,
8    tools::{
9        core::{
10            check_autopaste, check_erase_mode, check_recolorboxes, check_trigger_history_update,
11            check_trigger_redraw, deselect_all, instance_label_display_sort, map_released_key,
12            Mover,
13        },
14        instance_anno_shared::{check_cocoimport, get_rot90_data},
15        Manipulate, BBOX_NAME,
16    },
17    tools_data::{
18        annotations::BboxAnnotations, bbox_data, vis_from_lfoption, LabelInfo,
19        OUTLINE_THICKNESS_CONVERSION,
20    },
21    tools_data_accessors, tools_data_accessors_objects,
22    util::Visibility,
23    world::World,
24    world_annotations_accessor, GeoFig, Polygon,
25};
26use rvimage_domain::{shape_unscaled, BbF, Circle, PtF, TPtF};
27use std::{iter, mem, time::Instant};
28
29use super::on_events::{
30    change_annos_bbox, closest_corner, export_if_triggered, find_close_vertex, import_coco,
31    move_corner_tol, on_key_released, on_mouse_held_left, on_mouse_held_right,
32    on_mouse_released_left, on_mouse_released_right, KeyReleasedParams, MouseHeldLeftParams,
33    MouseReleaseParams, PrevPos,
34};
35pub const ACTOR_NAME: &str = "Bbox";
36const MISSING_ANNO_MSG: &str = "bbox annotations have not yet been initialized";
37const MISSING_DATA_MSG: &str = "bbox tools data not available";
38annotations_accessor_mut!(ACTOR_NAME, bbox_mut, MISSING_ANNO_MSG, BboxAnnotations);
39world_annotations_accessor!(ACTOR_NAME, bbox, MISSING_ANNO_MSG, BboxAnnotations);
40instance_annotations_accessor!(GeoFig);
41tools_data_accessors!(
42    ACTOR_NAME,
43    MISSING_DATA_MSG,
44    bbox_data,
45    BboxToolData,
46    bbox,
47    bbox_mut
48);
49tools_data_accessors_objects!(
50    ACTOR_NAME,
51    MISSING_DATA_MSG,
52    bbox_data,
53    BboxSpecificData,
54    bbox,
55    bbox_mut
56);
57
58pub(super) fn current_cat_idx(world: &World) -> Option<usize> {
59    get_specific(world).map(|d| d.label_info.cat_idx_current)
60}
61
62fn check_cocoexport(mut world: World) -> World {
63    // export label file if demanded
64    let bbox_data = get_specific(&world);
65    if let Some(bbox_data) = bbox_data {
66        let rot90_data = get_rot90_data(&world);
67        export_if_triggered(&world.data.meta_data, bbox_data, rot90_data);
68        if let Some(o) = get_options_mut(&mut world) {
69            o.core.import_export_trigger.untrigger_export();
70        }
71    }
72    world
73}
74
75fn show_grab_ball(
76    mp: Option<PtF>,
77    prev_pos: &PrevPos,
78    world: &mut World,
79    last_proximal_circle_check: Option<Instant>,
80    options: Option<&bbox_data::Options>,
81) -> Instant {
82    if last_proximal_circle_check.map(|lc| lc.elapsed().as_millis()) > Some(2) {
83        if let Some(mp) = mp {
84            if prev_pos.prev_pos.is_empty() {
85                let label_info = get_label_info(world);
86                let geos = get_annos_if_some(world).map(|a| {
87                    (0..a.elts().len())
88                        .filter(|elt_idx| {
89                            let cur = label_info.map(|li| li.cat_idx_current);
90                            let show_only_current = label_info.map(|li| li.show_only_current);
91                            a.is_of_current_label(*elt_idx, cur, show_only_current)
92                        })
93                        .map(|elt_idx| (elt_idx, &a.elts()[elt_idx]))
94                });
95                if let Some((bb_idx, c_idx)) = geos.and_then(|geos| {
96                    let unscaled = shape_unscaled(world.zoom_box(), world.shape_orig());
97                    let tolerance = move_corner_tol(unscaled);
98                    find_close_vertex(mp, geos, tolerance)
99                }) {
100                    let annos = get_annos(world);
101                    let corner_point = annos.map(|a| &a.elts()[bb_idx]).map(|a| a.point(c_idx));
102                    let data = get_specific_mut(world);
103                    if let (Some(data), Some(corner_point), Some(options)) =
104                        (data, corner_point, options)
105                    {
106                        data.highlight_circles = vec![Circle {
107                            center: corner_point,
108                            radius: TPtF::from(options.outline_thickness)
109                                / OUTLINE_THICKNESS_CONVERSION
110                                * 2.5,
111                        }];
112                        let vis = get_visible(world);
113                        world.request_redraw_annotations(BBOX_NAME, vis);
114                    }
115                } else {
116                    let data = get_specific_mut(world);
117                    let n_circles = data.as_ref().map_or(0, |d| d.highlight_circles.len());
118                    if let Some(data) = data {
119                        data.highlight_circles = vec![];
120                    }
121                    if n_circles > 0 {
122                        let vis = get_visible(world);
123                        world.request_redraw_annotations(BBOX_NAME, vis);
124                    }
125                }
126            } else {
127                let (c_idx, c_dist) = closest_corner(mp, prev_pos.prev_pos.iter().copied());
128                let unscaled = shape_unscaled(world.zoom_box(), world.shape_orig());
129                let tolerance = move_corner_tol(unscaled);
130                if c_dist < tolerance {
131                    let center = prev_pos.prev_pos[c_idx];
132                    let data = get_specific_mut(world);
133                    if let (Some(data), Some(options)) = (data, options) {
134                        data.highlight_circles = vec![Circle {
135                            center,
136                            radius: TPtF::from(options.outline_thickness)
137                                / OUTLINE_THICKNESS_CONVERSION
138                                * 3.5,
139                        }];
140                        let vis = get_visible(world);
141                        world.request_redraw_annotations(BBOX_NAME, vis);
142                    }
143                } else {
144                    let data = get_specific_mut(world);
145                    if let Some(data) = data {
146                        data.highlight_circles = vec![];
147                    }
148                    let vis = get_visible(world);
149                    world.request_redraw_annotations(BBOX_NAME, vis);
150                }
151            }
152        }
153    }
154    Instant::now()
155}
156
157#[derive(Clone, Debug)]
158pub struct Bbox {
159    prev_pos: PrevPos,
160    mover: Mover,
161    start_press_time: Option<Instant>,
162    points_at_press: Option<usize>,
163    points_after_held: Option<usize>,
164    last_proximal_circle_check: Option<Instant>,
165}
166
167impl Bbox {
168    fn mouse_pressed(
169        &mut self,
170        event: &Events,
171        mut world: World,
172        history: History,
173    ) -> (World, History) {
174        if get_options(&world).map(|o| o.core.erase) != Some(true) {
175            if event.pressed(KeyCode::MouseRight) {
176                self.mover.move_mouse_pressed(event.mouse_pos_on_orig);
177            } else {
178                self.start_press_time = Some(Instant::now());
179                self.points_at_press = Some(self.prev_pos.prev_pos.len());
180                if !(event.held_alt() || event.held_ctrl() || event.held_shift()) {
181                    world =
182                        deselect_all::<_, DataAccessors, InstanceAnnoAccessors>(world, BBOX_NAME);
183                }
184            }
185        }
186        (world, history)
187    }
188
189    fn mouse_held(
190        &mut self,
191        event: &Events,
192        mut world: World,
193        mut history: History,
194    ) -> (World, History) {
195        if event.held(KeyCode::MouseRight) {
196            on_mouse_held_right(event.mouse_pos_on_orig, &mut self.mover, world, history)
197        } else {
198            let options = get_options(&world);
199            let params = MouseHeldLeftParams {
200                prev_pos: self.prev_pos.clone(),
201                is_alt_held: event.held_alt(),
202                is_shift_held: event.held_shift(),
203                is_ctrl_held: event.held_ctrl(),
204                distance: f64::from(options.map_or(2, |o| o.drawing_distance)),
205                elapsed_millis_since_press: self
206                    .start_press_time
207                    .map_or(0, |t| t.elapsed().as_millis()),
208            };
209            (world, history, self.prev_pos) =
210                on_mouse_held_left(event.mouse_pos_on_orig, params, world, history);
211            self.points_after_held = Some(self.prev_pos.prev_pos.len());
212            (world, history)
213        }
214    }
215
216    fn mouse_released(
217        &mut self,
218        event: &Events,
219        mut world: World,
220        mut history: History,
221    ) -> (World, History) {
222        // evaluate if a box or a polygon should be closed based on the number of points
223        // at the time of the press and the number of points after the held
224        let close_box_or_poly = self.points_at_press.map(|x| x + 4) < self.points_after_held;
225        self.points_at_press = None;
226        self.points_after_held = None;
227
228        let are_boxes_visible = get_visible(&world);
229        if event.released(KeyCode::MouseLeft) {
230            let params = MouseReleaseParams {
231                prev_pos: self.prev_pos.clone(),
232                visible: are_boxes_visible,
233                is_alt_held: event.held_alt(),
234                is_shift_held: event.held_shift(),
235                is_ctrl_held: event.held_ctrl(),
236                close_box_or_poly,
237            };
238            (world, history, self.prev_pos) =
239                on_mouse_released_left(event.mouse_pos_on_orig, params, world, history);
240        } else if event.released(KeyCode::MouseRight) {
241            (world, history, self.prev_pos) = on_mouse_released_right(
242                event.mouse_pos_on_orig,
243                self.prev_pos.clone(),
244                are_boxes_visible,
245                world,
246                history,
247            );
248        } else {
249            history.push(Record::new(world.clone(), ACTOR_NAME));
250        }
251        (world, history)
252    }
253
254    fn key_held(
255        &mut self,
256        events: &Events,
257        mut world: World,
258        history: History,
259    ) -> (World, History) {
260        // up, down, left, right
261        let shape_orig = world.data.shape();
262        let split_mode = get_options(&world).map(|o| o.split_mode);
263        let shift_annos = |annos: &mut BboxAnnotations| {
264            if let Some(split_mode) = split_mode {
265                if events.held(KeyCode::Up) && events.held_ctrl() {
266                    *annos = mem::take(annos).shift_min_bbs(0.0, -1.0, shape_orig, split_mode);
267                } else if events.held(KeyCode::Down) && events.held_ctrl() {
268                    *annos = mem::take(annos).shift_min_bbs(0.0, 1.0, shape_orig, split_mode);
269                } else if events.held(KeyCode::Right) && events.held_ctrl() {
270                    *annos = mem::take(annos).shift_min_bbs(1.0, 0.0, shape_orig, split_mode);
271                } else if events.held(KeyCode::Left) && events.held_ctrl() {
272                    *annos = mem::take(annos).shift_min_bbs(-1.0, 0.0, shape_orig, split_mode);
273                } else if events.held(KeyCode::Up) && events.held_alt() {
274                    *annos = mem::take(annos).shift(0.0, -1.0, shape_orig, split_mode);
275                } else if events.held(KeyCode::Down) && events.held_alt() {
276                    *annos = mem::take(annos).shift(0.0, 1.0, shape_orig, split_mode);
277                } else if events.held(KeyCode::Right) && events.held_alt() {
278                    *annos = mem::take(annos).shift(1.0, 0.0, shape_orig, split_mode);
279                } else if events.held(KeyCode::Left) && events.held_alt() {
280                    *annos = mem::take(annos).shift(-1.0, 0.0, shape_orig, split_mode);
281                } else if events.held(KeyCode::Up) {
282                    *annos = mem::take(annos).shift_max_bbs(0.0, -1.0, shape_orig, split_mode);
283                } else if events.held(KeyCode::Down) {
284                    *annos = mem::take(annos).shift_max_bbs(0.0, 1.0, shape_orig, split_mode);
285                } else if events.held(KeyCode::Right) {
286                    *annos = mem::take(annos).shift_max_bbs(1.0, 0.0, shape_orig, split_mode);
287                } else if events.held(KeyCode::Left) {
288                    *annos = mem::take(annos).shift_max_bbs(-1.0, 0.0, shape_orig, split_mode);
289                }
290            }
291        };
292        change_annos_bbox(&mut world, shift_annos);
293        let vis = get_visible(&world);
294        world.request_redraw_annotations(BBOX_NAME, vis);
295        (world, history)
296    }
297
298    fn key_released(
299        &mut self,
300        events: &Events,
301        mut world: World,
302        mut history: History,
303    ) -> (World, History) {
304        let params = KeyReleasedParams {
305            is_ctrl_held: events.held_ctrl(),
306            released_key: map_released_key(events),
307        };
308        world = check_erase_mode::<DataAccessors>(params.released_key, set_visible, world);
309        (world, history) = on_key_released(world, history, events.mouse_pos_on_orig, &params);
310        (world, history)
311    }
312}
313
314impl Manipulate for Bbox {
315    fn new() -> Self {
316        Self {
317            prev_pos: PrevPos::default(),
318            mover: Mover::new(),
319            start_press_time: None,
320            points_after_held: None,
321            points_at_press: None,
322            last_proximal_circle_check: None,
323        }
324    }
325
326    fn on_activate(&mut self, mut world: World) -> World {
327        self.prev_pos = PrevPos::default();
328        if let Some(data) = trace_ok_err(get_data_mut(&mut world)) {
329            data.menu_active = true;
330        }
331        set_visible(&mut world);
332        world
333    }
334
335    fn on_deactivate(&mut self, mut world: World) -> World {
336        self.prev_pos = PrevPos::default();
337        if let Some(td) = world.data.tools_data_map.get_mut(BBOX_NAME) {
338            td.menu_active = false;
339        }
340        world.request_redraw_annotations(BBOX_NAME, Visibility::None);
341        world
342    }
343    fn on_always_active_zoom(&mut self, mut world: World, history: History) -> (World, History) {
344        let visible = get_options(&world).map(|o| o.core.visible) == Some(true);
345        let vis = vis_from_lfoption(get_label_info(&world), visible);
346        world.request_redraw_annotations(BBOX_NAME, vis);
347        (world, history)
348    }
349    fn on_filechange(&mut self, mut world: World, mut history: History) -> (World, History) {
350        let bbox_data = get_specific_mut(&mut world);
351        if let Some(bbox_data) = bbox_data {
352            for (_, (anno, _)) in bbox_data.anno_iter_mut() {
353                anno.deselect_all();
354            }
355            let ild = get_instance_label_display(&world);
356            world = instance_label_display_sort::<_, DataAccessors, InstanceAnnoAccessors>(
357                world, ild, ACTOR_NAME,
358            );
359        }
360
361        let visible = get_options(&world).map(|o| o.core.visible) == Some(true);
362        let vis = vis_from_lfoption(get_label_info(&world), visible);
363        world.request_redraw_annotations(BBOX_NAME, vis);
364
365        (world, history) =
366            check_autopaste::<_, DataAccessors, InstanceAnnoAccessors>(world, history, ACTOR_NAME);
367
368        (world, history)
369    }
370
371    fn events_tf(
372        &mut self,
373        mut world: World,
374        mut history: History,
375        events: &Events,
376    ) -> (World, History) {
377        world = check_recolorboxes::<DataAccessors>(world, BBOX_NAME);
378
379        (world, history) = check_trigger_history_update::<DataAccessors>(world, history, BBOX_NAME);
380
381        world = check_cocoexport(world);
382        let imported;
383        (world, imported) = check_cocoimport::<_, _, DataAccessors>(
384            world,
385            get_specific,
386            get_specific_mut,
387            import_coco,
388        );
389        if imported {
390            set_visible(&mut world);
391        }
392
393        let options = get_options(&world).copied();
394
395        self.last_proximal_circle_check = Some(show_grab_ball(
396            events.mouse_pos_on_orig,
397            &self.prev_pos,
398            &mut world,
399            self.last_proximal_circle_check,
400            options.as_ref(),
401        ));
402        if let Some(options) = options {
403            world = check_trigger_redraw::<DataAccessors>(world, BBOX_NAME);
404
405            let in_menu_selected_label = current_cat_idx(&world);
406            if let (Some(in_menu_selected_label), Some(mp)) =
407                (in_menu_selected_label, events.mouse_pos_on_orig)
408            {
409                if !self.prev_pos.prev_pos.is_empty() {
410                    let geo = if self.prev_pos.prev_pos.len() == 1 {
411                        GeoFig::BB(BbF::from_points(mp, self.prev_pos.prev_pos[0]))
412                    } else {
413                        GeoFig::Poly(
414                            Polygon::from_vec(
415                                self.prev_pos
416                                    .prev_pos
417                                    .iter()
418                                    .chain(iter::once(&mp))
419                                    .copied()
420                                    .collect::<Vec<_>>(),
421                            )
422                            .unwrap(),
423                        )
424                    };
425                    // animation
426                    let circles = get_specific(&world).map(|d| d.highlight_circles.clone());
427                    let label_info = get_specific(&world).map(|d| &d.label_info);
428
429                    if let (Some(circles), Some(label_info)) = (circles, label_info) {
430                        let label = Some(label_info.labels()[in_menu_selected_label].clone());
431                        let color = label_info.colors()[in_menu_selected_label];
432                        let anno = BboxAnnotation {
433                            geofig: geo,
434                            label,
435                            fill_color: Some(color),
436                            fill_alpha: 0,
437                            outline: Stroke {
438                                color,
439                                thickness: TPtF::from(options.outline_thickness) / 4.0,
440                            },
441                            outline_alpha: options.outline_alpha,
442                            is_selected: None,
443                            highlight_circles: circles,
444                            instance_label_display: options.core.instance_label_display,
445                        };
446                        let vis = get_visible(&world);
447                        world.request_redraw_annotations(BBOX_NAME, vis);
448                        world.request_redraw_tmp_anno(Annotation::Bbox(anno));
449                    }
450                }
451            }
452        }
453        (world, history) = make_tool_transform!(
454            self,
455            world,
456            history,
457            events,
458            [
459                (pressed, KeyCode::MouseRight, mouse_pressed),
460                (pressed, KeyCode::MouseLeft, mouse_pressed),
461                (held, KeyCode::MouseRight, mouse_held),
462                (held, KeyCode::MouseLeft, mouse_held),
463                (released, KeyCode::MouseLeft, mouse_released),
464                (released, KeyCode::MouseRight, mouse_released),
465                (released, KeyCode::Delete, key_released),
466                (released, KeyCode::Back, key_released),
467                (released, KeyCode::H, key_released),
468                (released, KeyCode::A, key_released),
469                (released, KeyCode::D, key_released),
470                (released, KeyCode::E, key_released),
471                (released, KeyCode::C, key_released),
472                (released, KeyCode::V, key_released),
473                (released, KeyCode::L, key_released),
474                (released, KeyCode::Down, key_released),
475                (released, KeyCode::Up, key_released),
476                (released, KeyCode::Left, key_released),
477                (released, KeyCode::Right, key_released),
478                (released, KeyCode::Key1, key_released),
479                (released, KeyCode::Key2, key_released),
480                (released, KeyCode::Key3, key_released),
481                (released, KeyCode::Key4, key_released),
482                (released, KeyCode::Key5, key_released),
483                (released, KeyCode::Key6, key_released),
484                (released, KeyCode::Key7, key_released),
485                (released, KeyCode::Key8, key_released),
486                (released, KeyCode::Key9, key_released),
487                (held, KeyCode::Down, key_held),
488                (held, KeyCode::Up, key_held),
489                (held, KeyCode::Left, key_held),
490                (held, KeyCode::Right, key_held)
491            ]
492        );
493        (world, history)
494    }
495}
496
497#[cfg(test)]
498use {
499    super::on_events::test_data,
500    crate::cfg::{ExportPath, ExportPathConnection},
501    crate::Event,
502    std::{path::PathBuf, thread, time::Duration},
503};
504#[test]
505fn test_bbox_ctrl_h() {
506    let (_, mut world, mut history) = test_data();
507    let mut bbox = Bbox::new();
508    bbox.last_proximal_circle_check = Some(Instant::now());
509    thread::sleep(Duration::from_millis(3));
510    assert_eq!(get_visible(&world), Visibility::All);
511    let events = Events::default()
512        .events(vec![
513            Event::Held(KeyCode::Ctrl),
514            Event::Released(KeyCode::H),
515        ])
516        .mousepos_orig(Some((1.0, 1.0).into()));
517    (world, history) = bbox.events_tf(world, history, &events);
518    thread::sleep(Duration::from_millis(3));
519    (world, _) = bbox.events_tf(
520        world,
521        history,
522        &Events::default().mousepos_orig(Some((1.0, 1.0).into())),
523    );
524    assert_eq!(get_visible(&world), Visibility::None);
525}
526
527#[test]
528fn test_coco_import_label_info() {
529    const TEST_DATA_FOLDER: &str = "resources/test_data/";
530    let (_, mut world, history) = test_data();
531    let data = get_specific_mut(&mut world).unwrap();
532    data.coco_file = ExportPath {
533        path: PathBuf::from(format!("{}catids_12_coco.json", TEST_DATA_FOLDER)),
534        conn: ExportPathConnection::Local,
535    };
536    let label_info_before = data.label_info.clone();
537    data.options.core.import_export_trigger.trigger_import();
538    let mut bbox = Bbox::new();
539    let events = Events::default();
540    let (mut world, history) = bbox.events_tf(world, history, &events);
541    let data = get_specific(&world).unwrap();
542    assert_eq!(label_info_before.labels(), &["rvimage_fg", "label"]);
543    assert_eq!(label_info_before.cat_ids(), &[1, 2]);
544    assert_eq!(data.label_info.labels(), &["first label", "second label"]);
545    assert_eq!(data.label_info.cat_ids(), &[1, 2]);
546    assert!(!data.options.core.import_export_trigger.import_triggered());
547
548    // now we import another coco file with different labels
549    let data = get_specific_mut(&mut world).unwrap();
550    data.coco_file = ExportPath {
551        path: PathBuf::from(format!("{}catids_01_coco_3labels.json", TEST_DATA_FOLDER)),
552        conn: ExportPathConnection::Local,
553    };
554    data.options.core.import_export_trigger.trigger_import();
555    let (world, _) = bbox.events_tf(world, history, &events);
556    let data = get_specific(&world).unwrap();
557    assert_eq!(
558        data.label_info.labels(),
559        &["first label", "second label", "third label"]
560    );
561    assert_eq!(data.label_info.cat_ids(), &[0, 1, 2]);
562    let all_occurring_cats = data
563        .annotations_map
564        .iter()
565        .flat_map(|(_, (v, _))| v.cat_idxs().iter().copied())
566        .collect::<Vec<usize>>();
567    assert!(all_occurring_cats.contains(&0));
568    assert!(all_occurring_cats.contains(&1));
569    assert!(all_occurring_cats.contains(&2));
570}