1#[cfg(test)]
2use super::annotations::InstanceAnnotations;
3use super::{
4 annotations::{BrushAnnotations, ClipboardData},
5 core::{
6 self, AccessInstanceData, AnnotationsMap, CocoRle, CocoSegmentation, ExportAsCoco,
7 LabelInfo,
8 },
9 InstanceAnnotate, InstanceExportData,
10};
11use crate::{
12 cfg::ExportPath, result::trace_ok_warn,
13 tools_data::predictive_labeling::PredictiveLabelingData, BrushLine,
14};
15use crate::{implement_annotate, implement_annotations_getters};
16use rvimage_domain::{
17 access_mask_abs, access_mask_rel, mask_to_rle, rle_bb_to_image, rverr, BbF, Canvas, PtF, PtI,
18 PtS, RvResult, ShapeI, TPtF, TPtI, TPtS, BB,
19};
20
21use serde::{Deserialize, Serialize};
22
23pub type BrushAnnoMap = AnnotationsMap<Canvas>;
24
25pub const MAX_THICKNESS: f64 = 100.0;
26pub const MIN_THICKNESS: f64 = 1.0;
27pub const MAX_INTENSITY: f64 = 1.0;
28pub const MIN_INTENSITY: f64 = 0.01;
29const fn default_alpha() -> u8 {
30 255
31}
32const fn default_perfilecrowd() -> bool {
33 false
34}
35#[derive(Serialize, Deserialize, Clone, Copy, Debug, PartialEq)]
36pub struct Options {
37 pub thickness: TPtF,
38 pub intensity: TPtF,
39 #[serde(skip)]
40 pub is_selection_change_needed: bool,
41 #[serde(skip)]
42 pub core: core::Options,
43 #[serde(default = "default_alpha")]
44 pub fill_alpha: u8,
45 #[serde(default = "default_perfilecrowd")]
46 pub per_file_crowd: bool,
47}
48impl Default for Options {
49 fn default() -> Self {
50 Self {
51 thickness: 15.0,
52 intensity: 0.5,
53 is_selection_change_needed: false,
54 core: core::Options::default(),
55 fill_alpha: default_alpha(),
56 per_file_crowd: default_perfilecrowd(),
57 }
58 }
59}
60
61#[derive(Serialize, Deserialize, Clone, Debug, Default, PartialEq)]
62pub struct BrushToolData {
63 pub annotations_map: BrushAnnoMap,
64 #[serde(skip)]
67 pub tmp_line: Option<(BrushLine, usize)>,
68 pub options: Options,
69 pub label_info: LabelInfo,
70 #[serde(skip)]
71 pub clipboard: Option<ClipboardData<Canvas>>,
72 pub coco_file: ExportPath,
73 #[serde(default)]
74 pub predictive_labeling_data: PredictiveLabelingData,
75}
76impl BrushToolData {
77 implement_annotations_getters!(BrushAnnotations);
78 pub fn from_coco_export_data(input_data: InstanceExportData<Canvas>) -> RvResult<Self> {
79 let label_info = input_data.label_info()?;
80 let mut out_data = Self {
81 tmp_line: None,
82
83 label_info,
84 annotations_map: AnnotationsMap::new(),
85 clipboard: None,
86 options: Options {
87 core: core::Options {
88 visible: true,
89 ..Default::default()
90 },
91 ..Default::default()
92 },
93 coco_file: input_data.coco_file,
94 predictive_labeling_data: PredictiveLabelingData::default(),
95 };
96 out_data.set_annotations_map(
97 input_data
98 .annotations
99 .into_iter()
100 .map(|(s, (canvases, cat_ids, dims))| {
101 (
102 s,
103 (BrushAnnotations::from_elts_cats(canvases, cat_ids), dims),
104 )
105 })
106 .collect(),
107 )?;
108 Ok(out_data)
109 }
110}
111
112impl AccessInstanceData<Canvas> for BrushToolData {
113 fn annotations_map(&self) -> &AnnotationsMap<Canvas> {
114 &self.annotations_map
115 }
116 fn label_info(&self) -> &LabelInfo {
117 &self.label_info
118 }
119}
120impl ExportAsCoco<Canvas> for BrushToolData {
121 fn cocofile_conn(&self) -> ExportPath {
122 self.coco_file.clone()
123 }
124 fn separate_data(self) -> (core::Options, LabelInfo, AnnotationsMap<Canvas>, ExportPath) {
125 (
126 self.options.core,
127 self.label_info,
128 self.annotations_map,
129 self.coco_file,
130 )
131 }
132 fn core_options_mut(&mut self) -> &mut core::Options {
133 &mut self.options.core
134 }
135 fn new(
136 options: core::Options,
137 label_info: LabelInfo,
138 anno_map: AnnotationsMap<Canvas>,
139 export_path: ExportPath,
140 ) -> Self {
141 Self {
142 annotations_map: anno_map,
143 tmp_line: None,
144 options: Options {
145 core: options,
146 ..Default::default()
147 },
148 label_info,
149 clipboard: None,
150 coco_file: export_path,
151 predictive_labeling_data: PredictiveLabelingData::default(),
152 }
153 }
154 fn set_annotations_map(&mut self, map: AnnotationsMap<Canvas>) -> RvResult<()> {
155 for (_, (annos, _)) in map.iter() {
156 for cat_idx in annos.cat_idxs() {
157 let len = self.label_info.len();
158 if *cat_idx >= len {
159 return Err(rverr!(
160 "cat idx {cat_idx} does not have a label, out of bounds, {len}"
161 ));
162 }
163 }
164 }
165 self.annotations_map = map;
166 Ok(())
167 }
168 fn set_labelinfo(&mut self, info: LabelInfo) {
169 self.label_info = info;
170 }
171 #[cfg(test)]
172 fn anno_iter(&self) -> impl Iterator<Item = (&String, &(InstanceAnnotations<Canvas>, ShapeI))> {
173 self.anno_iter()
174 }
175}
176
177impl InstanceAnnotate for Canvas {
178 fn is_contained_in_image(&self, shape: crate::ShapeI) -> bool {
179 self.bb.is_contained_in_image(shape)
180 }
181 fn contains<P>(&self, point: P) -> bool
182 where
183 P: Into<PtF>,
184 {
185 let p_tmp: PtF = point.into();
186 let p_idx: PtI = p_tmp.into();
187 access_mask_abs(&self.mask, self.bb, p_idx) > 0
188 }
189 fn enclosing_bb(&self) -> BbF {
190 self.bb.into()
191 }
192 fn rot90_with_image_ntimes(self, shape: ShapeI, n: u8) -> RvResult<Self> {
193 let bb = self.bb;
194 let bb_s: BB<TPtS> = BB::from(self.bb);
195 let bb_rot = bb_s.rot90_with_image_ntimes(shape, n);
196 if bb_rot.x < 0 || bb_rot.y < 0 {
197 return Err(rverr!("rotated bb {bb_rot:?} has negative coordinates",));
198 }
199 let mut new_mask = self.mask.clone();
200 for y in 0..bb.h {
201 for x in 0..bb.w {
202 let p_mask = PtI { x, y };
203 let p_im = p_mask + bb.min();
204 let p_im_rot = PtS::from(p_im).rot90_with_image_ntimes(shape, n);
205 let p_newmask = p_im_rot - bb_rot.min();
206 let p_newmask: PtI = p_newmask.into();
207 new_mask[p_newmask.y as usize * bb_rot.w as usize + p_newmask.x as usize] =
208 self.mask[p_mask.y as usize * bb.w as usize + p_mask.x as usize];
209 }
210 }
211 Ok(Self {
212 mask: new_mask,
213 bb: bb_rot.into(),
214 intensity: self.intensity,
215 })
216 }
217 fn to_cocoseg(
218 &self,
219 shape_im: ShapeI,
220 _is_export_absolute: bool,
221 ) -> RvResult<Option<core::CocoSegmentation>> {
222 if self.bb.is_contained_in_image(shape_im) {
223 let rle_bb = mask_to_rle(&self.mask, self.bb.w, self.bb.h);
224
225 let rle_im = trace_ok_warn(rle_bb_to_image(&rle_bb, self.bb, shape_im));
226 Ok(rle_im.map(|rle_im| {
227 CocoSegmentation::Rle(CocoRle {
228 counts: rle_im,
229 size: (shape_im.w, shape_im.h),
230 intensity: Some(self.intensity),
231 })
232 }))
233 } else {
234 Err(rverr!(
235 "bb {:?} not contained in image {shape_im:?}",
236 self.bb
237 ))
238 }
239 }
240 fn dist_to_boundary(&self, p: PtF) -> TPtF {
245 let mut min_dist = TPtF::MAX;
246 let to_coord = |x| {
247 if x > 0.0 {
248 x as TPtI
249 } else {
250 TPtI::MAX
251 }
252 };
253 let point_pixel_inside = PtI {
256 x: to_coord(p.x - TPtF::from(self.bb.x)),
257 y: to_coord(p.y - TPtF::from(self.bb.y)),
258 };
259 let point_pixel_value = access_mask_rel(
260 &self.mask,
261 point_pixel_inside.x,
262 point_pixel_inside.y,
263 self.bb.w,
264 self.bb.h,
265 );
266 for y in 1..self.bb.h {
267 for x in 1..self.bb.w {
268 let neighbors_fg_mask = [
269 access_mask_rel(&self.mask, x + 1, y, self.bb.w, self.bb.h),
270 access_mask_rel(&self.mask, x - 1, y, self.bb.w, self.bb.h),
271 access_mask_rel(&self.mask, x, y + 1, self.bb.w, self.bb.h),
272 access_mask_rel(&self.mask, x, y - 1, self.bb.w, self.bb.h),
273 ];
274 if neighbors_fg_mask.iter().any(|&b| b != point_pixel_value) {
275 let x = TPtF::from(x + self.bb.x);
276 let y = TPtF::from(y + self.bb.y);
277 let dist = p.dist_square(&PtF { x, y }).sqrt();
278 if dist < min_dist {
279 min_dist = dist;
280 }
281 }
282 }
283 }
284 min_dist
285 }
286}
287
288implement_annotate!(BrushToolData);
289
290#[cfg(test)]
291use rvimage_domain::{BbI, Line};
292#[test]
293fn test_canvas() {
294 let orig_shape = ShapeI::new(30, 30);
295 let bl = BrushLine {
296 line: Line {
297 points: vec![PtF { x: 5.0, y: 5.0 }, PtF { x: 15.0, y: 15.0 }],
298 },
299 intensity: 0.5,
300 thickness: 3.0,
301 };
302 let canv = Canvas::new(&bl, orig_shape, None).unwrap();
303 assert!(canv.contains(PtF { x: 5.0, y: 5.0 }));
304 assert!(!canv.contains(PtF { x: 0.0, y: 0.0 }));
305 assert!(canv.contains(PtF { x: 14.9, y: 14.9 }));
306 assert!(!canv.contains(PtF { x: 0.0, y: 9.9 }));
307 assert!(!canv.contains(PtF { x: 15.0, y: 15.0 }));
308 let d = canv.dist_to_boundary(PtF { x: 5.0, y: 5.0 });
309 assert!((d - 1.0).abs() < 1e-8);
310 let dist = canv.dist_to_boundary(PtF { x: 5.0, y: 15.0 });
311 assert!(5.0 < dist && dist < 7.0);
312 for y in canv.bb.y_range() {
313 for x in canv.bb.x_range() {
314 _ = access_mask_abs(&canv.mask, canv.bb, PtI { x, y });
315 }
316 }
317 let canv = Canvas::new(&bl, orig_shape, None).unwrap();
318 let canv_rot = canv.clone().rot90_with_image_ntimes(orig_shape, 1).unwrap();
319 let bl_rot = BrushLine {
320 line: Line {
321 points: vec![
322 PtF { x: 5.0, y: 5.0 }.rot90_with_image_ntimes(orig_shape, 1),
323 PtF { x: 15.0, y: 15.0 }.rot90_with_image_ntimes(orig_shape, 1),
324 ],
325 },
326 intensity: 0.5,
327 thickness: 3.0,
328 };
329 let canv_rot_ref = Canvas::new(&bl_rot, orig_shape, None).unwrap();
330 let inter = canv_rot
331 .enclosing_bb()
332 .intersect(canv_rot_ref.enclosing_bb());
333 assert!(
334 (inter.w - canv_rot.enclosing_bb().w).abs() <= 1.0
335 && (inter.h - canv_rot.enclosing_bb().h).abs() <= 1.0
336 );
337 let canv = Canvas::new(&bl, orig_shape, None).unwrap();
338 assert_eq!(
339 canv,
340 canv.clone().rot90_with_image_ntimes(orig_shape, 0).unwrap()
341 );
342}
343
344#[test]
345fn test_canvas_rot() {
346 let canv = Canvas {
347 mask: vec![0, 0, 0, 1],
348 bb: BbI::from_arr(&[0, 0, 4, 1]),
349 intensity: 0.5,
350 };
351 let canv_rot = canv
352 .clone()
353 .rot90_with_image_ntimes(ShapeI::new(4, 1), 1)
354 .unwrap();
355 let canv_ref = Canvas {
356 mask: vec![1, 0, 0, 0],
357 bb: BbI::from_arr(&[0, 0, 1, 4]),
358 intensity: 0.5,
359 };
360 assert_eq!(canv_rot, canv_ref);
361}