aboutsummaryrefslogtreecommitdiff
path: root/src/analysis.rs
blob: 9cf0b6aca3535d2f954565f91225d830d30b9f9c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
use crate::{LabelFormat,LabelU16};

use avl_tree::*;

#[derive(Debug)]
struct Data {
    label: u16,
    area: usize,
    perimeter: usize,
    cell_cell: usize,
    cell_tissue: usize,
}

impl Data {
    const fn new(label: u16, area: usize, perimeter: usize, cell_cell: usize, cell_tissue: usize) -> Self {
        Self {
            label,
            area,
            perimeter,
            cell_cell,
            cell_tissue,
        }
    }
}


impl LabelFormat<u16> {
    fn get_tree(&self) -> AVLTree<u16, Data> {
        let mut avt = avl_tree::AVLTree::<u16, Data>::new();
        for y in 0..self.height {
            for x in 0..self.width {
                let index = x + y * self.width;
                let key = self.buffer[index];
                if key.is_zero() {
                    continue;
                }
                if let Some(data) = avt.get(key) {
                    let mut new_data = Data::new(
                        data.label,
                        data.area,
                        data.perimeter,
                        data.cell_cell,
                        data.cell_tissue,
                    );
                    new_data.area += 1;
                    avt.insert(key, new_data);
                } else {
                    avt.insert(key, Data::new(key, 1, 0, 0, 0));
                }
            }
        }
        avt
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn get_areas_test() {
        const DIM: usize = 6;
        let mut test_data = LabelFormat::<u16> {
            buffer: vec![0u16; DIM*DIM],
            width: DIM,
            height: DIM,
        };
        test_data.buffer[2+3*DIM] = 1;
        test_data.buffer[3+3*DIM] = 1;
        test_data.buffer[4+3*DIM] = 2;

        for (key,val) in test_data.get_tree().iter() {
            println!("{} -> {:?}",key,val)
        }
    }
}