use ndarray::{Array3, ArrayView3, ArrayViewMut2, Axis};
use rayon::prelude::*;
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct MagnificationParams {
pub magnification: f64,
pub zp_diameter_m: f64,
pub zp_outermost_width_m: f64,
pub num_zones: u32,
}
impl Default for MagnificationParams {
fn default() -> Self {
Self {
magnification: 23.08,
zp_diameter_m: 300e-6,
zp_outermost_width_m: 30e-9,
num_zones: 2500,
}
}
}
fn focal_length(energy: f64, p: &MagnificationParams) -> f64 {
let wl_nm = 1.239842 / energy * 1000.0; p.zp_outermost_width_m * p.zp_diameter_m / wl_nm / 1000.0
}
pub fn magnification_corr_factors(energies: &[f64], p: &MagnificationParams) -> Vec<f64> {
if energies.is_empty() {
return Vec::new();
}
let a: Vec<f64> = energies
.iter()
.map(|&e| focal_length(e, p) * (1.0 + 1.0 / p.magnification))
.collect();
let mut b_real = vec![0.0f64; a.len()];
b_real[0] = a[0] * p.magnification;
for i in 1..a.len() {
b_real[i] = b_real[i - 1] - (a[i] - a[i - 1]);
}
let mag: Vec<f64> = a.iter().zip(&b_real).map(|(&ai, &bi)| ai / bi).collect();
let mag0 = mag[0];
mag.iter().map(|&m| mag0 / m).collect()
}
pub fn apply_magnification(vol: ArrayView3<f32>, cf: f64) -> Array3<f32> {
let (nz, ny, nx) = vol.dim();
if (cf - 1.0).abs() < 1e-8 || nz == 0 || nx == 0 {
return vol.to_owned();
}
let off_z = (1.0 - cf) * nz as f64 / 2.0;
let off_x = (1.0 - cf) * nx as f64 / 2.0;
let at = |z: isize, y: usize, x: isize| -> f32 {
if z < 0 || x < 0 || z as usize >= nz || x as usize >= nx {
0.0
} else {
vol[[z as usize, y, x as usize]]
}
};
let mut out = Array3::<f32>::zeros((nz, ny, nx));
let planes: Vec<ArrayViewMut2<f32>> = out.axis_iter_mut(Axis(0)).collect();
planes
.into_par_iter()
.enumerate()
.for_each(|(oz, mut plane)| {
let src_z = cf * oz as f64 + off_z;
let z0f = src_z.floor();
let fz = (src_z - z0f) as f32;
let z0 = z0f as isize;
for ox in 0..nx {
let src_x = cf * ox as f64 + off_x;
let x0f = src_x.floor();
let fx = (src_x - x0f) as f32;
let x0 = x0f as isize;
for oy in 0..ny {
let v00 = at(z0, oy, x0);
let v01 = at(z0, oy, x0 + 1);
let v10 = at(z0 + 1, oy, x0);
let v11 = at(z0 + 1, oy, x0 + 1);
let top = v00 * (1.0 - fx) + v01 * fx;
let bot = v10 * (1.0 - fx) + v11 * fx;
plane[[oy, ox]] = top * (1.0 - fz) + bot * fz;
}
}
});
out
}
#[cfg(test)]
mod tests {
use super::*;
use ndarray::Array3;
#[test]
fn first_energy_factor_is_unity() {
let energies = [8300.0, 8325.0, 8350.0, 8400.0];
let cf = magnification_corr_factors(&energies, &MagnificationParams::default());
assert_eq!(cf.len(), 4);
assert!(
(cf[0] - 1.0).abs() < 1e-12,
"cf[0] must be 1, got {}",
cf[0]
);
assert!(cf.windows(2).all(|w| w[1] < w[0]), "factors: {cf:?}");
}
#[test]
fn empty_energies_gives_no_factors() {
assert!(magnification_corr_factors(&[], &MagnificationParams::default()).is_empty());
}
#[test]
fn unit_factor_is_identity() {
let mut vol = Array3::<f32>::zeros((3, 2, 4));
vol[[1, 0, 2]] = 5.0;
let out = apply_magnification(vol.view(), 1.0);
assert_eq!(out, vol);
}
#[test]
fn scaling_preserves_the_centre_voxel() {
let (nz, ny, nx) = (4, 1, 4);
let mut vol = Array3::<f32>::zeros((nz, ny, nx));
vol[[2, 0, 2]] = 1.0; let out = apply_magnification(vol.view(), 0.5);
assert!(
(out[[2, 0, 2]] - 1.0).abs() < 1e-6,
"centre lost: {}",
out[[2, 0, 2]]
);
}
#[test]
fn y_axis_is_not_scaled() {
let (nz, ny, nx) = (4, 2, 4);
let mut vol = Array3::<f32>::zeros((nz, ny, nx));
for z in 0..nz {
for x in 0..nx {
vol[[z, 0, x]] = 1.0;
vol[[z, 1, x]] = 7.0;
}
}
let out = apply_magnification(vol.view(), 0.8);
assert!((out[[2, 0, 2]] - 1.0).abs() < 1e-4, "y=0 plane bled");
assert!((out[[2, 1, 2]] - 7.0).abs() < 1e-4, "y=1 plane bled");
}
}