Skip to main content

smlm_imp/circles/
mod.rs

1mod bresenham;
2
3use crate::{Image};
4use crate::utils;
5
6use std::collections::{HashSet};
7
8fn within_radius(radius: f64, centre_position: (f64, f64), pixel: (f64, f64)) -> bool
9{
10    let (centre_row, centre_col) = centre_position;
11    let (row, col) = pixel;
12
13    let x = col - centre_col;
14    let y = row - centre_row;
15    let distance = (x.powi(2) + y.powi(2)).sqrt();
16    distance < radius
17}
18
19fn locale(pixel: (usize, usize)) -> [(usize, usize); 4]
20{
21    let (row, col) = pixel;
22    let top_right = (row, col + 1);
23    let lower_left = (row + 1, col);
24    let lower_right = (row + 1, col + 1);
25    [pixel, top_right, lower_left, lower_right]
26}
27
28fn locale_indicies<I: Image>(image: I, pixel: (usize, usize)) -> [usize; 4]
29{
30    locale(pixel).map(|(r, c)| image.get_index(r, c))
31}
32
33fn within_image<I: Image>(image: I, radius: f64) -> Vec<bool>
34{
35    let centre = image.centre_position();
36    let centre = (centre.0 as f64, centre.1 as f64);
37    // println!("centre: {:?}", centre);
38    let mut within = vec![false; image.size()];
39    for row in 0..image.n_rows()
40    {
41        for col in 0..image.n_cols()
42        {
43            let pixel = (row as f64, col as f64);
44            let index = image.get_index(row, col);
45            within[index] = within_radius(radius, centre, pixel);
46            // println!("{index} {row},{col}={}", within[index])
47        }
48    }
49    within
50}
51
52fn local_crossing<I: Image>(image: I, radius: f64) -> Vec<usize>
53{
54    let within = within_image(&image, radius);
55    // println!("{:?}", w_data);
56    let mut perimeter = HashSet::new();
57    for row in 0..(image.n_rows() - 1)
58    {
59        for col in 0..(image.n_cols() - 1)
60        {
61            let pixel = (row, col);
62            let locale = locale_indicies(&image, pixel);
63            let locale_data = locale.map(|i| within[i]);
64            // println!("{:?} = {:?}", pixel, locale_data);
65            let seed = locale_data[0];
66            let index = image.get_index(row, col);
67            for value in &locale_data[1..]
68            {
69                // check if perimeter goes through pixel
70                if *value != seed 
71                {
72                    perimeter.insert(index);
73                }
74            }
75        }
76    }
77    perimeter.into_iter().collect()
78}
79
80pub fn angle_carve<I: Image>(image: I, radius: f64) -> Vec<usize>
81{
82    let mut indicies = HashSet::new();
83    let (centre_row, centre_col) = image.centre_position();
84    for radian in 0..360
85    {
86        let row = ((radian as f64).sin() * radius + centre_row as f64).round() as usize;
87        let col = ((radian as f64).cos() * radius + centre_col as f64).round() as usize;
88        let index = utils::get_index(row, col, image.n_cols());
89        indicies.insert(index);
90    }
91    indicies.into_iter().collect()
92}
93
94pub fn bresenham<I: Image>(image: I, radius: f64) -> Vec<usize>
95{
96    bresenham::bresenham(image, radius.round() as usize)
97}
98
99pub trait Perimeter
100{
101    fn get<I: Image>(image: I, radius: f64) -> Vec<usize>;
102}
103
104pub struct AngleCarve;
105impl Perimeter for AngleCarve
106{
107    fn get<I>(image: I, radius: f64) -> Vec<usize> where I: Image
108    {
109        angle_carve(image, radius)
110    }
111}
112
113pub struct LocalCrossing;
114impl Perimeter for LocalCrossing
115{
116    fn get<I>(image: I, radius: f64) -> Vec<usize> where I: Image
117    {
118        local_crossing(image, radius)
119    }
120}
121
122pub struct Bresenham;
123impl Perimeter for Bresenham
124{
125    fn get<I>(image: I, radius: f64) -> Vec<usize> where I: Image
126    {
127        bresenham(image, radius)
128    }
129}
130
131#[derive(Debug)]
132pub enum Method 
133{
134    LocalCrossing,
135    AngleCarve,
136    Bresenham
137}
138
139impl Method
140{
141    pub fn get_perimeter<I: Image>(&self, image: I, radius: f64) -> Vec<usize>
142    {
143        match self
144        {
145            Self::LocalCrossing => local_crossing(image, radius),
146            Self::AngleCarve => angle_carve(image, radius),
147            Self::Bresenham => bresenham(image, radius)
148        }
149    }
150}
151
152#[cfg(test)]
153mod tests 
154{
155    use crate::images::BorrowedImage;
156    
157    fn generate(n_rows: usize, n_cols: usize) -> ((usize, usize),Vec<usize>)
158    {
159        ((n_rows, n_cols), (0..(n_rows * n_cols)).collect())
160    }
161
162
163    // These tests have slightly different results they are both correct in what they do but result in different circles
164
165    #[test]
166    fn local_crossing()
167    {
168        let (shape, data) = generate(14, 14);
169        let image = BorrowedImage::new(shape, &data);
170        let expected = [60, 61, 62, 63, 64, 65, 74, 79, 88, 93, 102, 107, 116, 121, 130, 131, 132, 133, 134, 135];
171        let mut result = super::local_crossing(image, 3.0);
172        result.sort();
173        assert_eq!(result.as_slice(), expected);
174    }
175
176    #[test]
177    fn angle_carve()
178    {
179        let (shape, data) = generate(14, 14);
180        let image = BorrowedImage::new(shape, &data);
181        let expected = [61, 62, 63, 64, 65, 74, 75, 79, 80, 88, 94, 102, 108, 116, 122, 130, 131, 135, 136, 145, 146, 147, 148, 149];
182        let mut result = super::angle_carve(image, 3.0);
183        result.sort();
184        assert_eq!(result.as_slice(), expected);
185    }
186}