Skip to main content

mermin_core/
field.rs

1// mermin-core/src/field.rs
2
3use crate::Real;
4
5/// A 2D scalar field on a regular grid, stored row-major.
6/// `data[row * width + col]` gives the value at pixel (row, col).
7#[derive(Debug, Clone)]
8pub struct ImageField {
9    pub data: Vec<Real>,
10    pub width: usize,
11    pub height: usize,
12}
13
14impl ImageField {
15    pub fn new(data: Vec<Real>, width: usize, height: usize) -> Self {
16        assert_eq!(
17            data.len(),
18            width * height,
19            "data length must equal width * height"
20        );
21        Self {
22            data,
23            width,
24            height,
25        }
26    }
27
28    pub fn zeros(width: usize, height: usize) -> Self {
29        Self {
30            data: vec![0.0; width * height],
31            width,
32            height,
33        }
34    }
35
36    /// Access pixel at (row, col).
37    #[inline]
38    pub fn get(&self, row: usize, col: usize) -> Real {
39        self.data[row * self.width + col]
40    }
41
42    /// Mutable access to pixel at (row, col).
43    #[inline]
44    pub fn get_mut(&mut self, row: usize, col: usize) -> &mut Real {
45        &mut self.data[row * self.width + col]
46    }
47}