use crate::error::{Error, Result};
#[derive(Clone, Debug)]
pub struct Angles(pub Vec<f32>);
impl Angles {
pub fn uniform(nang: usize, ang1: f32, ang2: f32) -> Self {
if nang == 0 {
return Angles(Vec::new());
}
let step = (ang2 - ang1) / nang as f32;
Angles((0..nang).map(|i| ang1 + step * i as f32).collect())
}
pub fn len(&self) -> usize {
self.0.len()
}
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
}
#[derive(Clone, Debug)]
pub enum Center {
Scalar(f32),
PerRow(Vec<f32>),
}
impl Center {
pub fn at(&self, row: usize) -> f32 {
match self {
Center::Scalar(c) => *c,
Center::PerRow(v) => v[row],
}
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum Beam {
Parallel,
Cone {
source_dist: f32,
},
Laminography {
phi: f32,
},
}
impl Beam {
pub fn laminography(tilt_deg: f32) -> Self {
Beam::Laminography {
phi: lamino_phi(tilt_deg),
}
}
}
pub fn lamino_phi(tilt_deg: f32) -> f32 {
std::f32::consts::FRAC_PI_2 + tilt_deg * std::f32::consts::PI / 180.0
}
pub fn lamino_tilt_deg(phi: f32) -> f32 {
(phi - std::f32::consts::FRAC_PI_2) * 180.0 / std::f32::consts::PI
}
#[derive(Clone, Copy, Debug)]
pub struct Detector {
pub width: usize,
pub height: usize,
pub pixel_size: f32,
}
#[derive(Clone, Debug)]
pub struct Geometry {
pub angles: Angles,
pub center: Center,
pub beam: Beam,
pub detector: Detector,
}
impl Geometry {
pub fn parallel(angles: Angles, width: usize, height: usize, pixel_size: f32) -> Self {
Geometry {
center: Center::Scalar(width as f32 / 2.0),
angles,
beam: Beam::Parallel,
detector: Detector {
width,
height,
pixel_size,
},
}
}
pub fn valid_fov(&self, depth: usize) -> Result<ValidFov> {
let tilt = match self.beam {
Beam::Parallel => 0.0,
Beam::Laminography { phi } => f64::from(lamino_tilt_deg(phi)).to_radians(),
Beam::Cone { .. } => {
return Err(Error::InvalidParam(
"valid_fov: cone beam has no parallel-projection field of view".into(),
))
}
};
let w = self.detector.width as f64;
let lateral = |c: f32| f64::from(c).min(w - f64::from(c));
let r_lateral = match &self.center {
Center::Scalar(c) => lateral(*c),
Center::PerRow(v) => v.iter().map(|&c| lateral(c)).fold(f64::INFINITY, f64::min),
}
.max(0.0);
let (sin, cos) = (tilt.sin().abs(), tilt.cos());
let half_h = self.detector.height as f64 / 2.0;
let radius = (0..depth)
.map(|z| {
let reach = half_h - (z as f64 - depth as f64 / 2.0).abs() * cos;
let r = if sin > 0.0 {
reach / sin
} else if reach >= 0.0 {
f64::INFINITY
} else {
0.0
};
r.clamp(0.0, r_lateral)
})
.collect();
Ok(ValidFov { r_lateral, radius })
}
}
#[derive(Clone, Debug, PartialEq)]
pub struct ValidFov {
pub r_lateral: f64,
pub radius: Vec<f64>,
}
impl ValidFov {
pub fn full_z(&self) -> Option<(usize, usize)> {
let full = |r: &f64| *r >= self.r_lateral;
Some((
self.radius.iter().position(full)?,
self.radius.iter().rposition(full)?,
))
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn lamino_phi_is_tomocupy_convention_and_inverts() {
assert_eq!(lamino_phi(0.0), std::f32::consts::FRAC_PI_2);
assert!((lamino_phi(20.0) - (90.0f32 + 20.0).to_radians()).abs() < 1e-6);
assert_eq!(
Beam::laminography(20.0),
Beam::Laminography {
phi: lamino_phi(20.0)
}
);
for d in [-30.0f32, 0.0, 17.5, 20.0, 35.0] {
assert!((lamino_tilt_deg(lamino_phi(d)) - d).abs() < 1e-4, "{d}");
}
}
fn lamino(center: f32, n: usize, tilt_deg: f32) -> Geometry {
Geometry {
beam: Beam::laminography(tilt_deg),
center: Center::Scalar(center),
..Geometry::parallel(Angles(vec![0.0]), n, n, 1.0)
}
}
#[test]
fn valid_fov_bounds_the_laminography_disk_by_height() {
let fov = lamino(504.0, 1024, 20.0).valid_fov(1090).unwrap();
assert_eq!(fov.r_lateral, 504.0);
assert_eq!(fov.full_z(), Some((545 - 361, 545 + 361)));
assert_eq!(fov.radius[545], 504.0);
assert!(fov.radius[545 - 362] < 504.0);
assert_eq!(fov.radius[0], 0.0);
let full = lamino(1008.0, 2048, 20.0).valid_fov(2180).unwrap();
assert_eq!(full.full_z(), Some((1090 - 722, 1090 + 722)));
}
#[test]
fn valid_fov_parallel_is_the_lateral_disk_on_every_detector_row() {
let mut g = Geometry::parallel(Angles(vec![0.0]), 100, 50, 1.0);
g.center = Center::PerRow(vec![40.0, 70.0]);
let fov = g.valid_fov(60).unwrap();
assert_eq!(fov.r_lateral, 30.0);
assert_eq!(fov.full_z(), Some((5, 55)));
assert_eq!(fov.radius[4], 0.0);
g.beam = Beam::Cone { source_dist: 1.0 };
assert!(matches!(g.valid_fov(60), Err(Error::InvalidParam(_))));
}
}