From 1f3a2a35ef7c3f7c2cddafa59e010313a4b247ed Mon Sep 17 00:00:00 2001 From: Behrang Shafei <50267830+bertiqwerty@users.noreply.github.com> Date: Fri, 10 May 2024 22:45:24 +0200 Subject: [PATCH] code simplification --- rvimage/src/rvlib/tools/bbox/core.rs | 10 +-- rvimage/src/rvlib/tools/bbox/on_events.rs | 16 +---- rvimage/src/rvlib/tools/brush.rs | 19 ++---- .../src/rvlib/tools/instance_anno_shared.rs | 65 +++++++++---------- 4 files changed, 43 insertions(+), 67 deletions(-) diff --git a/rvimage/src/rvlib/tools/bbox/core.rs b/rvimage/src/rvlib/tools/bbox/core.rs index edadc60..5f25f9e 100644 --- a/rvimage/src/rvlib/tools/bbox/core.rs +++ b/rvimage/src/rvlib/tools/bbox/core.rs @@ -27,10 +27,10 @@ use rvimage_domain::{shape_unscaled, BbF, Circle, PtF, TPtF}; use std::{iter, mem, time::Instant}; use super::on_events::{ - change_annos_bbox, closest_corner, export_if_triggered, find_close_vertex, - import_coco_if_triggered, move_corner_tol, on_key_released, on_mouse_held_left, - on_mouse_held_right, on_mouse_released_left, on_mouse_released_right, KeyReleasedParams, - MouseHeldLeftParams, MouseReleaseParams, PrevPos, + change_annos_bbox, closest_corner, export_if_triggered, find_close_vertex, import_coco, + move_corner_tol, on_key_released, on_mouse_held_left, on_mouse_held_right, + on_mouse_released_left, on_mouse_released_right, KeyReleasedParams, MouseHeldLeftParams, + MouseReleaseParams, PrevPos, }; pub const ACTOR_NAME: &str = "Bbox"; const MISSING_ANNO_MSG: &str = "bbox annotations have not yet been initialized"; @@ -421,7 +421,7 @@ impl Manipulate for Bbox { get_rot90_data, get_specific, get_specific_mut, - import_coco_if_triggered, + import_coco, ); if imported { set_visible(&mut world); diff --git a/rvimage/src/rvlib/tools/bbox/on_events.rs b/rvimage/src/rvlib/tools/bbox/on_events.rs index b5fcb5c..50530bf 100644 --- a/rvimage/src/rvlib/tools/bbox/on_events.rs +++ b/rvimage/src/rvlib/tools/bbox/on_events.rs @@ -91,22 +91,12 @@ pub(super) fn find_close_vertex<'a>( .map(|(bb_idx, c_idx, _)| (bb_idx, c_idx)) } -pub(super) fn import_coco_if_triggered( +pub(super) fn import_coco( meta_data: &MetaData, - coco_file: Option<&ExportPath>, + coco_file: &ExportPath, rot90_data: Option<&Rot90ToolData>, ) -> Option { - if let Some(coco_file) = coco_file { - match tools_data::coco_io::read_coco(meta_data, coco_file, rot90_data) { - Ok((bbox_data, _)) => Some(bbox_data), - Err(e) => { - tracing::error!("could not import coco due to {e:?}"); - None - } - } - } else { - None - } + trace_ok_err(tools_data::coco_io::read_coco(meta_data, coco_file, rot90_data).map(|(d, _)| d)) } pub(super) fn export_if_triggered( diff --git a/rvimage/src/rvlib/tools/brush.rs b/rvimage/src/rvlib/tools/brush.rs index 84336d2..0bc82e4 100644 --- a/rvimage/src/rvlib/tools/brush.rs +++ b/rvimage/src/rvlib/tools/brush.rs @@ -58,23 +58,14 @@ tools_data_accessors_objects!( brush_mut ); -fn import_coco_if_triggered( +fn import_coco( meta_data: &MetaData, - coco_file: Option<&ExportPath>, + coco_file: &ExportPath, rot90_data: Option<&Rot90ToolData>, ) -> Option { - if let Some(coco_file) = coco_file { - match tools_data::coco_io::read_coco(meta_data, coco_file, rot90_data) { - Ok((_, brush_data)) => Some(brush_data), - Err(e) => { - tracing::error!("could not import coco due to {e:?}"); - None - } - } - } else { - None - } + trace_ok_err(tools_data::coco_io::read_coco(meta_data, coco_file, rot90_data).map(|(_, d)| d)) } + fn max_select_dist(shape: ShapeI) -> TPtF { (TPtF::from(shape.w.pow(2) + shape.h.pow(2)).sqrt() / 100.0).max(50.0) } @@ -565,7 +556,7 @@ impl Manipulate for Brush { get_rot90_data, get_specific, get_specific_mut, - import_coco_if_triggered, + import_coco, ); if imported { set_visible(&mut world); diff --git a/rvimage/src/rvlib/tools/instance_anno_shared.rs b/rvimage/src/rvlib/tools/instance_anno_shared.rs index e935a50..39fd6d3 100644 --- a/rvimage/src/rvlib/tools/instance_anno_shared.rs +++ b/rvimage/src/rvlib/tools/instance_anno_shared.rs @@ -21,52 +21,47 @@ pub fn check_cocoimport( get_rot90_data: impl Fn(&World) -> Option<&Rot90ToolData>, get_specific: impl Fn(&World) -> Option<&T>, get_specific_mut: impl Fn(&mut World) -> Option<&mut T>, - import_coco_if_triggered: impl Fn( - &MetaData, - Option<&ExportPath>, - Option<&Rot90ToolData>, - ) -> Option, + import_coco: impl Fn(&MetaData, &ExportPath, Option<&Rot90ToolData>) -> Option, ) -> (World, bool) where T: ExportAsCoco + Default, A: InstanceAnnotate + 'static, { - // import coco if demanded - let mut imported = false; let options = get_options(&world); - if let Some(options) = options { - let rot90_data = get_rot90_data(&world); - let d = if options.is_import_triggered { - get_specific(&world).map(|o| o.cocofile_conn()) - } else { - None - }; + let rot90_data = get_rot90_data(&world); + let import_info = if let Some(options) = options { + get_specific(&world).map(|d| (d.cocofile_conn(), options.import_mode)) + } else { + None + }; + let imported = if let Some((coco_connection, import_mode)) = import_info { if let Some(imported_data) = - import_coco_if_triggered(&world.data.meta_data, d.as_ref(), rot90_data) + import_coco(&world.data.meta_data, &coco_connection, rot90_data) { - if let Some(data_mut) = get_specific_mut(&mut world) { - if options.is_import_triggered { - let (_, import_label_info, import_anno_map, _) = imported_data.separate_data(); - match options.import_mode { - ImportMode::Replace => { - trace_ok_err(data_mut.set_annotations_map(import_anno_map)); - data_mut.set_labelinfo(import_label_info); - } - ImportMode::Merge => { - let (options, label_info, anno_map, export_path) = - mem::take(data_mut).separate_data(); - let (annotations_map, label_info) = - merge(anno_map, label_info, import_anno_map, import_label_info); - *data_mut = T::new(options, label_info, annotations_map, export_path); - } - } - data_mut.core_options_mut().is_import_triggered = false; + let (_, import_label_info, import_anno_map, _) = imported_data.separate_data(); + match (import_mode, get_specific_mut(&mut world)) { + (ImportMode::Replace, Some(data_mut)) => { + trace_ok_err(data_mut.set_annotations_map(import_anno_map)); + data_mut.set_labelinfo(import_label_info); + } + (ImportMode::Merge, Some(data_mut)) => { + let (options, label_info, anno_map, export_path) = + mem::take(data_mut).separate_data(); + let (annotations_map, label_info) = + merge(anno_map, label_info, import_anno_map, import_label_info); + *data_mut = T::new(options, label_info, annotations_map, export_path); } - imported = true; + _ => (), } - } else if let Some(data_mut) = get_specific_mut(&mut world) { - data_mut.core_options_mut().is_import_triggered = false; + true + } else { + false } + } else { + false + }; + if let Some(data_mut) = get_specific_mut(&mut world) { + data_mut.core_options_mut().is_import_triggered = false; } (world, imported) }