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 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 within_bounds(&self, value: T) -> bool
88 where T: PartialOrd
89 {
90 self.min() <= value && value <= self.max()
91 }
92}
93
94pub type NmRange = Range<f64>;
96
97
98#[derive(Debug, Clone, PartialEq)]
99pub struct Frame<T>
100{
101 col_range: Range<T>, row_range: Range<T>, }
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 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 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 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]
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 }