Skip to main content

smlm_renderer/
data_properties.rs

1use crate::utils;
2
3use std::cmp::{PartialOrd};
4use std::fmt::{Display, Formatter};
5use std::ops::{Add, Sub, Mul};
6
7#[derive(Debug, PartialEq)]
8pub struct Dimensions<T>
9{
10    height: T,
11    width: T
12}
13
14impl<T>  Dimensions<T>
15{
16    pub fn new(height: T, width: T) -> Self
17    {
18        Self{height, width}
19    }
20}
21
22impl<T: Copy>  Dimensions<T>
23{
24    pub fn height(&self) -> T
25    {
26        self.height
27    }
28
29    pub fn width(&self) -> T
30    {
31        self.width
32    }
33}
34
35impl<T: Display> Display for Dimensions<T>
36{
37    fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), std::fmt::Error>
38    {
39        write!(f, "height: {}, width: {}", self.height, self.width)
40    }
41}
42
43pub type ImageDimensions = Dimensions<usize>;
44
45#[derive(Debug, Clone, PartialEq)]
46pub struct Range<T>
47{
48    min: T,
49    max: T,
50}
51
52impl<T: Copy> Range<T>
53{
54    pub fn new(min: T, max: T) -> Self
55    {
56        Self{min, max}
57    }    
58
59    pub fn min(&self) -> T
60    {
61        self.min
62    }
63
64    pub fn set_min(&mut self, value: T) -> ()
65    {
66        // let adjusted = if value > 0.0 {value} else{0.0};
67        // self.min = adjusted;
68        self.min = value;
69    }
70
71    pub fn max(&self) -> T
72    {
73        self.max
74    }
75
76    pub fn length(&self) -> T
77    where T: Sub<Output = T>
78    {
79        self.max() - self.min()
80    }
81
82    // pub fn centre(&self) -> f64
83    // {
84    //     self.min() + (self.length() / 2.0)
85    // }
86
87    pub fn within_bounds(&self, value: T) -> bool
88    where T: PartialOrd
89    {
90        self.min() <= value && value <= self.max()
91    }
92}
93
94// pub type PixelRange = Range<usize>;
95pub type NmRange = Range<f64>;
96
97
98#[derive(Debug, Clone, PartialEq)]
99pub struct Frame<T>
100{
101    col_range: Range<T>, // width
102    row_range: Range<T>, //height
103}
104
105impl<T: Copy + Sub<Output=T> + Add<Output=T> + Mul<Output=T> + PartialOrd> Frame<T>
106{
107    pub fn new(col_range: Range<T>, row_range: Range<T>) -> Self
108    {
109        Self{row_range, col_range}
110    }
111
112    pub fn from(x_start: T, y_start: T, width: T, height: T) -> Self
113    {
114        let col_range = Range::new(x_start, x_start + width);
115        let row_range = Range::new(y_start, y_start + height);
116        Self::new(col_range, row_range)
117    }
118
119    pub fn start_col(&self) -> T
120    {
121        self.col_range.min()
122    }
123
124    pub fn width(&self) -> T
125    {
126        self.col_range.length()
127    }    
128
129    pub fn start_row(&self) -> T
130    {
131        self.row_range.min()
132    }
133
134    pub fn height(&self) -> T
135    {
136        self.row_range.length()
137    }
138
139    pub fn scale_by(&self, sf: T) -> Self
140    {
141        let height = self.height() * sf;
142        let width = self.width() * sf;
143        Self::from(self.start_row(), self.start_col(), height, width)
144    }
145
146    pub fn within_bounds(&self, row: T, col: T) -> bool
147    {
148        self.row_range.within_bounds(row) && self.col_range.within_bounds(col)
149    }
150
151    pub fn within_xy_bounds(&self, x: T, y: T) -> bool
152    {
153        self.within_bounds(y, x)
154    }
155}
156
157impl<T: Copy + Sub<Output=T> + Add<Output=T> + Mul<Output=T> + PartialOrd + Display> Display for Frame<T>
158{
159    fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), std::fmt::Error>
160    {
161        write!(f, "start row: {}, start col: {}, height: {}, width: {}", self.start_row(), self.start_col(), self.height(), self.width())
162    }
163}
164
165pub type ImageFrame = Frame<usize>;
166pub type NmFrame = Frame<f64>;
167
168#[derive(Debug, Clone)]
169pub struct DataProperties 
170{
171    nm_frame: NmFrame
172}
173
174impl DataProperties
175{
176    pub fn new(nm_frame: NmFrame) -> Self
177    {
178        Self{nm_frame}
179    }
180
181    fn with(x_range: NmRange, y_range: NmRange) -> Self
182    {
183        Self::new(NmFrame::new(x_range, y_range))
184    }
185
186    pub fn from(min_x: f64, max_x: f64, min_y: f64, max_y: f64) -> Self
187    {
188        Self::with(Range::new(min_x, max_x), Range::new(min_y, max_y))
189    }
190
191    pub fn data_frame(&self) -> &NmFrame
192    {
193        &self.nm_frame
194    }
195
196    pub fn width(&self) -> f64
197    {
198        self.nm_frame.width()
199    }
200
201    pub fn width_offset(&self) -> f64
202    {
203        self.nm_frame.start_col()
204    }
205
206    pub fn set_width_offset(&mut self, _value: f64) -> ()
207    {
208        
209    }
210
211    // fn width_centre(&self) -> f64
212    // {
213    //     self.x_range.centre()
214    // }
215
216    pub fn height(&self) -> f64
217    {
218        self.nm_frame.height()
219    }
220
221    pub fn height_offset(&self) -> f64
222    {
223        self.nm_frame.start_row()
224    }
225
226    pub fn set_height_offset(&mut self, _value: f64) -> ()
227    {
228        
229    }
230
231    // fn height_centre(&self) -> f64
232    // {
233    //     self.y_range.centre()
234    // }
235
236    // pub fn max_length(&self) -> f64
237    // {
238    //     if self.height() < self.width()
239    //     {
240    //         self.width()
241    //     }
242    //     else 
243    //     {
244    //         self.height()
245    //     }
246    // }
247
248    // fn length_difference(&self) -> f64
249    // {
250    //     utils::distance(self.height(), self.width())
251    // }
252
253    // pub fn adjust_shortest_dimension_offset(&mut self) -> ()
254    // {
255    //     // we minus here as we are in effect treating the image as a frame over global space
256    //     // and pulling it backwards so the image is centred
257    //     let difference = self.length_difference();
258    //     let amount = difference / 2.0;
259    //     if self.height() < self.width()
260    //     {
261    //         self.set_height_offset(self.height_offset() - amount)
262    //     }
263    //     else 
264    //     {
265    //         self.set_width_offset(self.width_offset() - amount)
266    //     }
267    // }
268
269    pub fn calculate_image_height(&self, pixel_size: f64) -> usize
270    {
271        utils::n_pixels_required(self.height(), pixel_size)
272    }
273
274    pub fn calculate_image_width(&self, pixel_size: f64) -> usize
275    {
276        utils::n_pixels_required(self.width(), pixel_size)
277    }
278
279    // fn image_bounds(&self, image_size: usize, pixel_size_nm: f64) -> DataProperties
280    // {
281    //     let image_size_nm = utils::image_size_from_pixels(image_size, pixel_size_nm);
282    //     let half_image = image_size_nm / 2.0;
283    //     let min_x = self.width_centre() - half_image;
284    //     let max_x = self.width_centre() + half_image;
285    //     let min_y = self.height_centre() - half_image;
286    //     let max_y = self.height_centre() + half_image;
287    //     DataProperties::from(min_x, max_x, min_y, max_y)
288    // }
289
290    // // this is for cropping / padding images to fit image sizes with specific pixel sizes
291    // // if data is too big then reduce the dimensions to where the image would be 
292    // // given centre point
293    // pub fn adjust_bounds_to_image_size(&mut self, image_size: usize, pixel_size_nm: f64) -> ()
294    // {
295    //     println!("{:?}", self);
296    //     let new_bounds = self.image_bounds(image_size, pixel_size_nm);
297    //     self.x_range = new_bounds.x_range;
298    //     self.y_range = new_bounds.y_range;
299    //     println!("{:?}", self);
300    // }
301
302    pub fn within_bounds(&self, x: f64, y: f64) -> bool
303    {
304        self.nm_frame.within_xy_bounds(x, y)
305    }
306}
307
308#[derive(Debug, Clone)]
309pub struct Properties 
310{
311    data_properties: DataProperties,
312    pixel_size : f64
313}
314
315impl Properties
316{
317    pub fn new(data_properties: DataProperties, pixel_size: f64) -> Self
318    {
319        Self{data_properties, pixel_size}
320    }
321
322    pub fn pixel_size(&self) -> f64
323    {
324        self.pixel_size
325    }
326
327    pub fn data_frame(&self) -> &NmFrame
328    {
329        self.data_properties.data_frame()
330    }
331
332    pub fn height_offset(&self) -> f64
333    {
334        self.data_properties.height_offset()
335    }
336
337    pub fn width_offset(&self) -> f64
338    {
339        self.data_properties.width_offset()
340    }
341
342    pub fn get_column(&self, x: f64) -> usize
343    {
344        utils::shifted_pixel_location(x, self.width_offset(), self.pixel_size())
345    }
346
347    pub fn get_row(&self, y: f64) -> usize
348    {
349        utils::shifted_pixel_location(y, self.height_offset(), self.pixel_size())
350    }
351
352    pub fn get_row_centre(&self, row: usize) -> f64
353    {
354        utils::get_global_pixel_centre_nm(row, self.height_offset(), self.pixel_size())
355    }
356
357    pub fn get_col_centre(&self, col: usize) -> f64
358    {
359        utils::get_global_pixel_centre_nm(col, self.width_offset(), self.pixel_size())
360    }
361
362    pub fn within_bounds(&self, x: f64, y: f64) -> bool
363    {
364        self.data_properties.within_bounds(x, y)
365    }
366}
367
368#[cfg(test)]
369mod tests 
370{
371    use super::*;
372
373    // #[test]
374    // fn range_centre()
375    // {
376    //     assert_eq!(Range::new(10.0, 20.0).centre(), 15.0);
377    //     assert_eq!(Range::new(-10.0, 20.0).centre(), 5.0);
378    // }
379
380    #[test]
381    fn range_within() 
382    {
383        let range = Range::new(1.0, 2.0);
384        assert_eq!(range.within_bounds(1.0), true);
385        assert_eq!(range.within_bounds(1.0 - std::f64::EPSILON), false);
386        assert_eq!(range.within_bounds(2.0), true);
387        assert_eq!(range.within_bounds(2.0000001), false);
388    }
389
390    #[test]
391    fn frame_height() 
392    {
393        assert_eq!(Frame::from(10, 20, 100, 50).height(), 50);
394    }
395
396    #[test]
397    fn frame_width() 
398    {
399        assert_eq!(Frame::from(10, 20, 100, 50).width(), 100);
400    }
401
402    #[test]
403    fn data_properties_width_offset()
404    {
405        assert_eq!(DataProperties::from(1.0, 2.0, 1.0, 2.0).width_offset(), 1.0);
406    }
407
408    #[test]
409    fn data_properties_height_offset()
410    {
411        assert_eq!(DataProperties::from(1.0, 2.0, 1.0, 2.0).height_offset(), 1.0);
412    }
413
414    #[test]
415    fn data_properties_within_bounds()
416    {
417        let data_properties = DataProperties::from(1.0, 2.0, 1.0, 2.0);
418        assert_eq!(data_properties.within_bounds(1.0, 1.0), true);
419        assert_eq!(data_properties.within_bounds(0.99999, 1.0), false);
420        assert_eq!(data_properties.within_bounds(1.0, 0.99999), false);
421    }
422
423    #[test]
424    fn correct_offsets()
425    {
426        let data_properties = DataProperties::from(1.0, 2.0, 3.0, 4.0);
427        println!("{:?}",data_properties);
428        assert_eq!(data_properties.width_offset(), 1.0);
429        assert_eq!(data_properties.height_offset(), 3.0);
430    }
431
432    // #[test]
433    // fn adjust_offsets_for_shortest_dimension()
434    // {
435    //     let mut data_properties = DataProperties::from(10.0, 20.0, 30.0, 50.0);
436    //     assert_eq!(data_properties.width_offset(), 10.0);
437    //     data_properties.adjust_shortest_dimension_offset();
438    //     assert_eq!(data_properties.width_offset(), 5.0);
439    // }
440
441    // #[test]
442    // fn adjust_bounds_to_crop_to_image_size()
443    // {
444    //     let mut data_properties = DataProperties::from(10.0, 20.0, 30.0, 50.0);
445    //     data_properties.adjust_bounds_to_image_size(5, 1.0);
446    //     assert_eq!(data_properties.height(), 5.0);
447    //     assert_eq!(data_properties.height_offset(), 37.5);
448    //     assert_eq!(data_properties.width(), 5.0);
449    //     assert_eq!(data_properties.width_offset(), 12.5);
450    // }
451
452    // #[test]
453    // fn adjust_bounds_to_pad_to_image_size()
454    // {
455    //     let mut data_properties = DataProperties::from(10.0, 15.0, 30.0, 35.0);
456    //     data_properties.adjust_bounds_to_image_size(100, 1.0);
457    //     assert_eq!(data_properties.height(), 100.0);
458    //     assert_eq!(data_properties.height_offset(), -17.5);
459    //     assert_eq!(data_properties.width(), 100.0);
460    //     assert_eq!(data_properties.width_offset(), -37.5);
461    // }
462
463    // #[test]
464    // fn height_can_be_adjusted_to()
465    // {
466    //     let mut data_properties = DataProperties::from(30.0, 50.0, 10.0, 20.0);
467    //     assert_eq!(data_properties.height_offset(), 10.0);
468    //     data_properties.adjust_shortest_dimension_offset();
469    //     assert_eq!(data_properties.height_offset(), 5.0);
470    // }
471}