#![forbid(unsafe_code)]
mod noise;
pub mod phantom;
use crate::backend::Backend;
use crate::data::{Layout, Tomo, Volume};
use crate::error::{Error, Result};
use crate::geometry::{Angles, Geometry};
pub use noise::{add_drift, add_gaussian, add_poisson, add_rings, add_salt_pepper, add_zingers};
pub use phantom::{shepp2d, shepp3d};
pub fn angles(nang: usize, ang1: f32, ang2: f32) -> Vec<f32> {
Angles::uniform(nang, ang1, ang2).0
}
pub fn project(vol: &Volume<f32>, geom: &Geometry, backend: &dyn Backend) -> Result<Tomo<f32>> {
let proj = backend.projector().ok_or(Error::MissingCapability {
backend: backend.name(),
capability: "ForwardProject",
})?;
let mut out = Tomo::new(ndarray::Array3::zeros((0, 0, 0)), Layout::Sinogram);
proj.project(vol, geom, &mut out)?;
Ok(out)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn shepp2d_has_expected_skull_intensity() {
let p = shepp2d(64).unwrap();
assert_eq!(p.dim(), (64, 64));
let center = p[[32, 32]];
assert!(center > 0.0, "center intensity was {center}");
assert_eq!(p[[0, 0]], 0.0);
}
#[test]
fn angles_span_half_turn() {
let a = angles(180, 0.0, std::f32::consts::PI);
assert_eq!(a.len(), 180);
assert_eq!(a[0], 0.0);
}
#[test]
fn project_reports_missing_capability() {
use crate::backend::DeviceKind;
use crate::dtype::Dtype;
struct NullBackend;
impl Backend for NullBackend {
fn name(&self) -> &'static str {
"null"
}
fn device(&self) -> DeviceKind {
DeviceKind::Cpu
}
fn supports(&self, _dt: Dtype) -> bool {
false
}
}
let v = Volume::new(ndarray::Array3::<f32>::zeros((1, 4, 4)));
let g = Geometry::parallel(Angles::uniform(4, 0.0, std::f32::consts::PI), 4, 1, 1.0);
assert!(matches!(
project(&v, &g, &NullBackend),
Err(Error::MissingCapability {
capability: "ForwardProject",
..
})
));
}
}