Skip to main content

smlm_renderer/render_styles/
single_point_patch_renderer.rs

1use crate::{gaussian, utils, DataType, Properties, RenderPolicy};
2use crate::patch::{PatchFactory, SigmaFactory};
3
4use imp::{ImageMut, OwnedImage};
5
6use locs::{UncertainLocalisation};
7
8fn render_patch<I: ImageMut<Data=DataType>>(localisation: (f64, f64), pixel: (usize, usize), patch: (usize, usize), sigma_nm: f64, mut image: I, properties: &Properties) -> Result<(), String>
9{
10    let (n_rows, n_cols) = image.shape();
11    let (x, y) = localisation;
12    let (row, col) = pixel;
13    // println!("row: {row}, col: {col}");
14    let (patch_height, patch_width) = patch;
15    let patch_rows = (patch_height / 2) + 1;
16    let patch_cols = (patch_width / 2) + 1;
17    // println!("patch_rows: {patch_rows}, patch_cols: {patch_cols}");
18    let image_row_pos = row + patch_rows;
19    // println!("image_row_pos: {:?}", image_row_pos);
20    let image_row_neg = row.checked_sub(patch_rows);
21    // println!("image_row_neg: {:?}", image_row_neg);
22    let image_col_pos = col + patch_cols;
23    // println!("image_col_pos: {:?}", image_col_pos);
24    let image_col_neg = col.checked_sub(patch_cols);
25    // println!("image_col_neg: {:?}", image_col_neg);
26
27    let row_start = if image_row_neg.is_some() {image_row_neg.unwrap()} else{0};
28    let row_end = std::cmp::min(image_row_pos, n_rows);
29    let col_start = if image_col_neg.is_some() {image_col_neg.unwrap()} else{0};
30    let col_end = std::cmp::min(image_col_pos, n_cols);
31    for row in row_start..row_end
32    {
33        // println!("global row: {row}");
34        let row_nm = properties.get_row_centre(row);
35        // println!("row_nm: {row_nm}");
36        let y_dist = utils::distance(y, row_nm);
37        // println!("y_dist: {y_dist} between {y} and {row_nm} for row {row}");
38        for col in col_start..col_end
39        {
40            // println!("row: {row}, p_col: {col}");
41            // println!("global_row: {row}, global_col: {col}");
42            let col_nm = properties.get_col_centre(col);
43            let x_dist = utils::distance(x, col_nm);
44            // println!("x_dist: {x_dist} for col {col}");
45            let value = gaussian::blur_2d(x_dist, y_dist, sigma_nm);
46            *image.get_at_mut(row, col) += value;
47        }
48    }
49    Ok(())
50}
51
52#[derive(Debug, Clone)]
53pub struct SinglePointPatchRenderer<F>
54{
55	factory: F
56}
57
58impl SinglePointPatchRenderer<SigmaFactory>
59{
60    pub fn with_sigma(sigma: f64) -> Self
61    {
62        Self::new(SigmaFactory::new(sigma))
63    }
64}
65
66impl<F> SinglePointPatchRenderer<F>
67{
68	pub fn new(factory: F) -> Self
69	{
70		Self{factory}
71	}
72}
73
74impl<F: PatchFactory> SinglePointPatchRenderer<F>
75{
76	fn render_as_patch<L: UncertainLocalisation, I: ImageMut<Data=DataType>>(&self, localisation: L, image: I, properties: &Properties) -> Result<(), String>
77	{
78	    let col = properties.get_column(localisation.x());
79	    // println!("col: {col}");
80	    let row = properties.get_row(localisation.y());
81	    // println!("row: {row}");
82	    let sigma_nm = localisation.uncertainty();
83	    // println!("sigma (nm): {sigma_nm}");
84	    let patch_size = self.factory.get_patch(sigma_nm, &properties)?;
85	    // println!("sigma_patch: {:?}", patch_size);
86	    let l = (localisation.x(), localisation.y());
87	    let pixel = (row, col);
88	    let patch = (patch_size.height(), patch_size.width());
89	    // let _ = render_symmetrical_patch(l, pixel, patch, sigma_nm, image, properties);
90	    let _ = render_patch(l, pixel, patch, sigma_nm, image, properties);
91	    Ok(())
92	}
93}
94
95impl<F: PatchFactory, L: UncertainLocalisation> RenderPolicy<L> for SinglePointPatchRenderer<F>
96{
97    type Output = ();
98    type Error = String;
99    fn render(&self, localisation: L, image: &mut OwnedImage<DataType>, properties: &Properties) -> Result<Self::Output, Self::Error>
100    {
101    	self.render_as_patch(localisation, image, properties)
102    }
103}
104
105#[cfg(test)]
106mod tests 
107{
108	use super::*;
109
110	use crate::{DataProperties};
111	use imp::{Image, OwnedImage};
112
113	#[test]
114    fn render_patch_test() 
115    {
116        let sigma = 1.0;
117        let pixel_size = 1.0;
118        let mut image = OwnedImage::<DataType>::zeros((5, 5));
119        let localisation = (3.8, 2.9);
120        let pixel_location = (2, 3);
121        assert_eq!(utils::pixel_location(3.8, pixel_size), 3);
122        assert_eq!(utils::pixel_location(2.9, pixel_size), 2);
123        let patch_size = (3, 3);
124        let data_properties = DataProperties::from(0.0, 5.0, 0.0, 5.0);
125        let properties = Properties::new(data_properties, pixel_size);
126        let _ = render_patch(localisation, pixel_location, patch_size, sigma, &mut image, &properties);
127        let expected = [0.0, 0.000634370704867446, 0.003837719126199197, 0.00854100098741466, 0.006992780170465791, 0.0, 0.0042413355700180394, 0.02565858497688406, 0.05710423103325742, 0.0467529900777979, 0.0, 0.010432002163477002, 0.06310993543702902, 0.1404537443096252, 0.11499379985124172, 0.0, 0.009439265902546075, 0.05710423103325742, 0.1270878033546041, 0.10405069294754148, 0.0, 0.0, 0.0, 0.0, 0.0];
128        assert_eq!(image.data(), expected);
129    }
130}