Skip to content

Commit

Permalink
code simplification
Browse files Browse the repository at this point in the history
  • Loading branch information
bertiqwerty committed May 10, 2024
1 parent a19f494 commit 1f3a2a3
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 67 deletions.
10 changes: 5 additions & 5 deletions rvimage/src/rvlib/tools/bbox/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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);
Expand Down
16 changes: 3 additions & 13 deletions rvimage/src/rvlib/tools/bbox/on_events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<BboxSpecificData> {
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(
Expand Down
19 changes: 5 additions & 14 deletions rvimage/src/rvlib/tools/brush.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<BrushToolData> {
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)
}
Expand Down Expand Up @@ -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);
Expand Down
65 changes: 30 additions & 35 deletions rvimage/src/rvlib/tools/instance_anno_shared.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,52 +21,47 @@ pub fn check_cocoimport<T, A>(
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<T>,
import_coco: impl Fn(&MetaData, &ExportPath, Option<&Rot90ToolData>) -> Option<T>,
) -> (World, bool)
where
T: ExportAsCoco<A> + 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)
}

0 comments on commit 1f3a2a3

Please sign in to comment.