rvlib/tools_data/
rot90_data.rs

1use serde::{Deserialize, Serialize};
2
3use crate::{ShapeI, implement_annotate, implement_annotations_getters};
4
5use super::label_map::LabelMap;
6
7#[derive(Deserialize, Serialize, Clone, Debug, PartialEq, Eq, Default, Copy)]
8pub enum NRotations {
9    #[default]
10    Zero,
11    One,
12    Two,
13    Three,
14}
15
16impl NRotations {
17    pub fn increase(self) -> Self {
18        match self {
19            Self::Zero => Self::One,
20            Self::One => Self::Two,
21            Self::Two => Self::Three,
22            Self::Three => Self::Zero,
23        }
24    }
25    pub fn to_num(self) -> u8 {
26        match self {
27            Self::Zero => 0,
28            Self::One => 1,
29            Self::Two => 2,
30            Self::Three => 3,
31        }
32    }
33    pub fn max(self, other: Self) -> Self {
34        if self.to_num() >= other.to_num() {
35            self
36        } else {
37            other
38        }
39    }
40    #[must_use]
41    pub fn is_empty(self) -> bool {
42        matches!(self, Self::Zero)
43    }
44}
45
46pub type Rot90AnnotationsMap = LabelMap<NRotations>;
47
48#[derive(Deserialize, Serialize, Default, Clone, Debug, PartialEq, Eq)]
49pub struct Rot90ToolData {
50    // maps the filename to the number of rotations
51    annotations_map: Rot90AnnotationsMap,
52}
53impl Rot90ToolData {
54    implement_annotations_getters!(NRotations);
55    #[must_use]
56    pub fn merge(mut self, other: Self) -> Self {
57        for (filename, (nrot_other, shape)) in other.annotations_map {
58            let nrot = if let Some((nrot_self, _)) = self.annotations_map.get(&filename) {
59                nrot_self.max(nrot_other)
60            } else {
61                nrot_other
62            };
63            self.annotations_map.insert(filename, (nrot, shape));
64        }
65        self
66    }
67    pub fn set_annotations_map(&mut self, map: Rot90AnnotationsMap) {
68        self.annotations_map = map;
69    }
70    pub fn annotations_map(&self) -> &Rot90AnnotationsMap {
71        &self.annotations_map
72    }
73}
74
75implement_annotate!(Rot90ToolData);