use crate::backend::{make_fbp_filter, Fft, RampShape};
use crate::dtype::Complex32;
use crate::error::Result;
use crate::geometry::lamino_phi;
use crate::params::FilterName;
use rayon::prelude::*;
use std::f32::consts::PI;
const EPS: f64 = 1e-3;
#[inline]
fn sign(i: usize) -> f32 {
if i.is_multiple_of(2) {
-1.0
} else {
1.0
}
}
fn usfft_params(n: usize) -> (usize, f32) {
let nf = n as f64;
let neg_log_eps = -EPS.ln();
let mu = neg_log_eps / (2.0 * nf * nf);
let inside = mu * neg_log_eps + (mu * nf) * (mu * nf) / 4.0;
let m = (2.0 * nf / std::f64::consts::PI * inside.sqrt()).ceil() as usize;
(m, mu as f32)
}
fn centered_fft_axis0(
fdee: &mut [Complex32],
n: usize,
m: usize,
batch: usize,
inverse: bool,
fft: &dyn Fft,
) -> Result<()> {
let len = 2 * n;
let mut lines = vec![Complex32::new(0.0, 0.0); batch * len];
lines.par_chunks_mut(len).enumerate().for_each(|(b, row)| {
for (i, slot) in row.iter_mut().enumerate() {
*slot = fdee[(m + i) * batch + b] * sign(m + i);
}
});
fft.fft_1d(&mut lines, len, batch, inverse)?;
let renorm = if inverse { len as f32 } else { 1.0 };
fdee.par_chunks_mut(batch)
.enumerate()
.for_each(|(grid, row)| {
if grid >= m && grid < m + len {
let i = grid - m;
let s = sign(m + i) * renorm;
for (b, slot) in row.iter_mut().enumerate() {
*slot = lines[b * len + i] * s;
}
}
});
Ok(())
}
#[allow(clippy::too_many_arguments)]
fn centered_fft2d(
fdee: &mut [Complex32],
n0: usize,
n1: usize,
m0: usize,
m1: usize,
batch: usize,
inverse: bool,
fft: &dyn Fft,
) -> Result<()> {
let gx = 2 * n0 + 2 * m0;
let gy = 2 * n1 + 2 * m1;
let (wx, wy) = (2 * n0, 2 * n1);
let mut win = vec![Complex32::new(0.0, 0.0); batch * wy * wx];
win.par_chunks_mut(wy * wx)
.zip(fdee.par_chunks(gx * gy))
.for_each(|(wslice, fslice)| {
for iy in 0..wy {
let sy = sign(m1 + iy);
for ix in 0..wx {
let s = sign(m0 + ix) * sy;
wslice[iy * wx + ix] = fslice[(m0 + ix) + (m1 + iy) * gx] * s;
}
}
});
fft.fft_2d(&mut win, wy, wx, batch, inverse)?;
let renorm = if inverse { (wx * wy) as f32 } else { 1.0 };
fdee.par_chunks_mut(gx * gy)
.zip(win.par_chunks(wy * wx))
.for_each(|(fslice, wslice)| {
for iy in 0..wy {
let sy = sign(m1 + iy);
for ix in 0..wx {
let s = sign(m0 + ix) * sy * renorm;
fslice[(m0 + ix) + (m1 + iy) * gx] = wslice[iy * wx + ix] * s;
}
}
});
Ok(())
}
pub fn fft2d_fwd(
proj: &[f32],
ntheta: usize,
deth: usize,
detw: usize,
fft: &dyn Fft,
) -> Result<Vec<Complex32>> {
let mut buf = vec![Complex32::new(0.0, 0.0); ntheta * deth * detw];
buf.par_chunks_mut(deth * detw)
.zip(proj.par_chunks(deth * detw))
.for_each(|(bchunk, pchunk)| {
for ty in 0..deth {
let sy = sign(ty);
for tx in 0..detw {
let s = sign(tx) * sy;
bchunk[ty * detw + tx] = Complex32::new(pchunk[ty * detw + tx] * s, 0.0);
}
}
});
fft.fft_2d(&mut buf, deth, detw, ntheta, false)?;
let scale = 1.0 / (deth * detw) as f32;
buf.par_chunks_mut(deth * detw).for_each(|bchunk| {
for ty in 0..deth {
let sy = sign(ty);
for tx in 0..detw {
let s = sign(tx) * sy * scale;
bchunk[ty * detw + tx] *= s;
}
}
});
Ok(buf)
}
pub fn fft2d_inv(
g: &[Complex32],
ntheta: usize,
deth: usize,
detw: usize,
fft: &dyn Fft,
) -> Result<Vec<f32>> {
let mut buf = vec![Complex32::new(0.0, 0.0); ntheta * deth * detw];
for tz in 0..ntheta {
for ty in 0..deth {
let sy = sign(ty);
for tx in 0..detw {
let s = sign(tx) * sy;
buf[(tz * deth + ty) * detw + tx] = g[(tz * deth + ty) * detw + tx] * s;
}
}
}
fft.fft_2d(&mut buf, deth, detw, ntheta, true)?;
let renorm = (deth * detw) as f32;
let mut out = vec![0.0f32; ntheta * deth * detw];
for tz in 0..ntheta {
for ty in 0..deth {
let sy = sign(ty);
for tx in 0..detw {
let s = sign(tx) * sy * renorm;
out[(tz * deth + ty) * detw + tx] = buf[(tz * deth + ty) * detw + tx].re * s;
}
}
}
Ok(out)
}
fn usfft1d_z(deth: usize, phi: f32) -> Vec<f32> {
let sp = phi.sin();
(0..deth)
.map(|t| (t as f32 - deth as f32 / 2.0) / deth as f32 * sp)
.collect()
}
pub fn usfft1d_adj(
g: &[Complex32],
n0: usize,
n1: usize,
n2: usize,
deth: usize,
phi: f32,
fft: &dyn Fft,
) -> Result<Vec<f32>> {
let (m2, mu2) = usfft_params(n2);
let z = usfft1d_z(deth, phi);
let ng = 2 * n2 + 2 * m2;
let mut fdee = vec![Complex32::new(0.0, 0.0); ng * n1 * n0];
let wscale = (PI / (mu2 * n0 as f32)).sqrt();
let bases: Vec<isize> = z
.iter()
.map(|&z0| (2.0 * n2 as f32 * z0).floor() as isize - m2 as isize)
.collect();
let cols: Vec<Vec<Complex32>> = (0..n1)
.into_par_iter()
.map(|ty| {
let mut col = vec![Complex32::new(0.0, 0.0); ng * n0]; for tz in 0..deth {
let z0 = z[tz];
let base = bases[tz];
for tx in 0..n0 {
let g0 = g[tx + tz * n0 + ty * n0 * deth];
for i2 in 0..2 * m2 + 1 {
let ell2 = base + i2 as isize;
let w2 = ell2 as f32 / (2.0 * n2 as f32) - z0;
let w = wscale * (-PI * PI / mu2 * w2 * w2).exp();
let a = (n2 as isize + m2 as isize + ell2) as usize;
col[a * n0 + tx] += g0 * w;
}
}
}
col
})
.collect();
for (ty, col) in cols.iter().enumerate() {
for a in 0..ng {
let dst = ty * n0 + a * n0 * n1;
fdee[dst..dst + n0].copy_from_slice(&col[a * n0..a * n0 + n0]);
}
}
for a in 0..ng {
if a < m2 || a >= 2 * n2 + m2 {
let a0 = (a + 2 * n2 - m2) % (2 * n2);
for ty in 0..n1 {
for tx in 0..n0 {
let v = fdee[tx + ty * n0 + a * n0 * n1];
fdee[tx + ty * n0 + (a0 + m2) * n0 * n1] += v;
}
}
}
}
centered_fft_axis0(&mut fdee, n2, m2, n0 * n1, true, fft)?;
let mut f = vec![0.0f32; n1 * n2 * n0];
f.par_chunks_mut(n2 * n0)
.enumerate()
.for_each(|(ty, fchunk)| {
for tz in 0..n2 {
let ker = (-mu2 * (tz as f32 - n2 as f32 / 2.0).powi(2)).exp();
let a = tz + n2 / 2 + m2;
for tx in 0..n0 {
let g_ind = tx + ty * n0 + a * n0 * n1;
fchunk[tz * n0 + tx] = fdee[g_ind].re / ker / (2.0 * n2 as f32);
}
}
});
Ok(f)
}
pub fn usfft1d_fwd(
f: &[f32],
n0: usize,
n1: usize,
n2: usize,
deth: usize,
phi: f32,
fft: &dyn Fft,
) -> Result<Vec<Complex32>> {
let (m2, mu2) = usfft_params(n2);
let z = usfft1d_z(deth, phi);
let ng = 2 * n2 + 2 * m2;
let mut fdee = vec![Complex32::new(0.0, 0.0); ng * n1 * n0];
for tz in 0..n2 {
let ker = (-mu2 * (tz as f32 - n2 as f32 / 2.0).powi(2)).exp();
let a = tz + n2 / 2 + m2;
for ty in 0..n1 {
for tx in 0..n0 {
let v = f[tx + tz * n0 + ty * n0 * n2] / ker / (2.0 * n2 as f32);
fdee[tx + ty * n0 + a * n0 * n1] = Complex32::new(v, 0.0);
}
}
}
centered_fft_axis0(&mut fdee, n2, m2, n0 * n1, false, fft)?;
for a in 0..ng {
if a < m2 || a >= 2 * n2 + m2 {
let a0 = (a + 2 * n2 - m2) % (2 * n2);
for ty in 0..n1 {
for tx in 0..n0 {
let v = fdee[tx + ty * n0 + (a0 + m2) * n0 * n1];
fdee[tx + ty * n0 + a * n0 * n1] = v;
}
}
}
}
let wscale = (PI / (mu2 * n0 as f32)).sqrt();
let mut g = vec![Complex32::new(0.0, 0.0); n1 * deth * n0];
for tz in 0..deth {
let z0 = z[tz];
let base = (2.0 * n2 as f32 * z0).floor() as isize - m2 as isize;
for ty in 0..n1 {
for tx in 0..n0 {
let mut acc = Complex32::new(0.0, 0.0);
for i2 in 0..2 * m2 + 1 {
let ell2 = base + i2 as isize;
let w2 = ell2 as f32 / (2.0 * n2 as f32) - z0;
let w = wscale * (-PI * PI / mu2 * w2 * w2).exp();
let a = (n2 as isize + m2 as isize + ell2) as usize;
acc += fdee[tx + ty * n0 + a * n0 * n1] * w;
}
g[tx + tz * n0 + ty * n0 * deth] = acc;
}
}
}
Ok(g)
}
fn usfft2d_xy(
theta: &[f32],
phi: f32,
ntheta: usize,
nky: usize,
detw: usize,
) -> (Vec<f32>, Vec<f32>) {
let cph = phi.cos();
let lim = 0.5 - 1e-5;
let mut xs = vec![0.0f32; ntheta * nky * detw];
let mut ys = vec![0.0f32; ntheta * nky * detw];
for (tz, &th) in theta.iter().enumerate().take(ntheta) {
let (st, ct) = (th.sin(), th.cos());
for ky in 0..nky {
let kv = (ky as f32 - nky as f32 / 2.0) / nky as f32;
for kx in 0..detw {
let ku = (kx as f32 - detw as f32 / 2.0) / detw as f32;
let x = (ku * ct + kv * st * cph).clamp(-lim, lim);
let y = (ku * st - kv * ct * cph).clamp(-lim, lim);
let ind = kx + ky * detw + tz * detw * nky;
xs[ind] = x;
ys[ind] = y;
}
}
}
(xs, ys)
}
#[allow(clippy::too_many_arguments)]
pub fn usfft2d_adj(
g: &[Complex32],
n0: usize,
n1: usize,
nky: usize,
ntheta: usize,
detw: usize,
theta: &[f32],
phi: f32,
fft: &dyn Fft,
) -> Result<Vec<Complex32>> {
let (m0, mu0) = usfft_params(n0);
let (m1, mu1) = usfft_params(n1);
let gx = 2 * n0 + 2 * m0;
let gy = 2 * n1 + 2 * m1;
let (xs, ys) = usfft2d_xy(theta, phi, ntheta, nky, detw);
let mut fdee = vec![Complex32::new(0.0, 0.0); nky * gy * gx];
let wpre = PI / (mu0 * mu1 * ntheta as f32).sqrt();
fdee.par_chunks_mut(gx * gy)
.enumerate()
.for_each(|(ky, sub)| {
for tz in 0..ntheta {
for kx in 0..detw {
let ind = kx + ky * detw + tz * detw * nky;
let (x0, y0) = (xs[ind], ys[ind]);
let g0 = g[ind];
let base0 = (2.0 * n0 as f32 * x0).floor() as isize - m0 as isize;
let base1 = (2.0 * n1 as f32 * y0).floor() as isize - m1 as isize;
for i1 in 0..2 * m1 + 1 {
let ell1 = base1 + i1 as isize;
let w1 = ell1 as f32 / (2.0 * n1 as f32) - y0;
let ew1 = (-PI * PI / mu1 * w1 * w1).exp();
let yg = (n1 as isize + m1 as isize + ell1) as usize;
for i0 in 0..2 * m0 + 1 {
let ell0 = base0 + i0 as isize;
let w0 = ell0 as f32 / (2.0 * n0 as f32) - x0;
let w = wpre * (-PI * PI / mu0 * w0 * w0).exp() * ew1;
let xg = (n0 as isize + m0 as isize + ell0) as usize;
sub[xg + yg * gx] += g0 * w;
}
}
}
}
});
wrap2d(&mut fdee, n0, n1, m0, m1, true);
centered_fft2d(&mut fdee, n0, n1, m0, m1, nky, true, fft)?;
let mut f = vec![Complex32::new(0.0, 0.0); n1 * nky * n0];
f.par_chunks_mut(nky * n0)
.enumerate()
.for_each(|(ty, fchunk)| {
let yg = ty + n1 / 2 + m1;
for ky in 0..nky {
for tx in 0..n0 {
let ker = (-mu0 * (tx as f32 - n0 as f32 / 2.0).powi(2)
- mu1 * (ty as f32 - n1 as f32 / 2.0).powi(2))
.exp();
let xg = tx + n0 / 2 + m0;
let g_ind = xg + yg * gx + ky * gx * gy;
fchunk[ky * n0 + tx] = fdee[g_ind] / ker / (n0 * n1) as f32;
}
}
});
Ok(f)
}
#[allow(clippy::too_many_arguments)]
pub fn usfft2d_fwd(
f: &[Complex32],
n0: usize,
n1: usize,
nky: usize,
ntheta: usize,
detw: usize,
theta: &[f32],
phi: f32,
fft: &dyn Fft,
) -> Result<Vec<Complex32>> {
let (m0, mu0) = usfft_params(n0);
let (m1, mu1) = usfft_params(n1);
let gx = 2 * n0 + 2 * m0;
let gy = 2 * n1 + 2 * m1;
let (xs, ys) = usfft2d_xy(theta, phi, ntheta, nky, detw);
let mut fdee = vec![Complex32::new(0.0, 0.0); nky * gy * gx];
for ky in 0..nky {
for ty in 0..n1 {
let yg = ty + n1 / 2 + m1;
for tx in 0..n0 {
let ker = (-mu0 * (tx as f32 - n0 as f32 / 2.0).powi(2)
- mu1 * (ty as f32 - n1 as f32 / 2.0).powi(2))
.exp();
let xg = tx + n0 / 2 + m0;
let v = f[tx + ky * n0 + ty * n0 * nky] / ker / (n0 * n1) as f32;
fdee[xg + yg * gx + ky * gx * gy] = v;
}
}
}
centered_fft2d(&mut fdee, n0, n1, m0, m1, nky, false, fft)?;
wrap2d(&mut fdee, n0, n1, m0, m1, false);
let wpre = PI / (mu0 * mu1 * ntheta as f32).sqrt();
let mut g = vec![Complex32::new(0.0, 0.0); ntheta * nky * detw];
for tz in 0..ntheta {
for ky in 0..nky {
for kx in 0..detw {
let ind = kx + ky * detw + tz * detw * nky;
let (x0, y0) = (xs[ind], ys[ind]);
let base0 = (2.0 * n0 as f32 * x0).floor() as isize - m0 as isize;
let base1 = (2.0 * n1 as f32 * y0).floor() as isize - m1 as isize;
let mut acc = Complex32::new(0.0, 0.0);
for i1 in 0..2 * m1 + 1 {
let ell1 = base1 + i1 as isize;
let w1 = ell1 as f32 / (2.0 * n1 as f32) - y0;
let ew1 = (-PI * PI / mu1 * w1 * w1).exp();
let yg = (n1 as isize + m1 as isize + ell1) as usize;
for i0 in 0..2 * m0 + 1 {
let ell0 = base0 + i0 as isize;
let w0 = ell0 as f32 / (2.0 * n0 as f32) - x0;
let w = wpre * (-PI * PI / mu0 * w0 * w0).exp() * ew1;
let xg = (n0 as isize + m0 as isize + ell0) as usize;
acc += fdee[xg + yg * gx + ky * gx * gy] * w;
}
}
g[ind] = acc;
}
}
}
Ok(g)
}
fn wrap2d(fdee: &mut [Complex32], n0: usize, n1: usize, m0: usize, m1: usize, adj: bool) {
let gx = 2 * n0 + 2 * m0;
let gy = 2 * n1 + 2 * m1;
fdee.par_chunks_mut(gx * gy).for_each(|slice| {
for ty in 0..gy {
for tx in 0..gx {
if tx < m0 || tx >= 2 * n0 + m0 || ty < m1 || ty >= 2 * n1 + m1 {
let tx0 = (tx + 2 * n0 - m0) % (2 * n0);
let ty0 = (ty + 2 * n1 - m1) % (2 * n1);
let id1 = tx + ty * gx;
let id2 = (tx0 + m0) + (ty0 + m1) * gx;
if adj {
let v = slice[id1];
slice[id2] += v;
} else {
slice[id1] = slice[id2];
}
}
}
}
});
}
pub(crate) fn projection_filter(filter: FilterName, detw: usize) -> Result<(Vec<f32>, usize)> {
let w = make_fbp_filter(filter, detw, RampShape::Wint)?;
let pad = w.len() / 2 - detw / 2;
Ok((w, pad))
}
fn ramp_filter_detw(
proj: &mut [f32],
ntheta: usize,
deth: usize,
detw: usize,
filter: FilterName,
fft: &dyn Fft,
) -> Result<()> {
let (ramp, pad) = projection_filter(filter, detw)?;
let ne = ramp.len();
let nlines = ntheta * deth;
let mut buf = vec![Complex32::new(0.0, 0.0); nlines * ne];
for l in 0..nlines {
let row = &proj[l * detw..(l + 1) * detw];
for (k, slot) in buf[l * ne..(l + 1) * ne].iter_mut().enumerate() {
let v = if k < pad {
row[0]
} else if k < pad + detw {
row[k - pad]
} else {
row[detw - 1]
};
*slot = Complex32::new(v, 0.0);
}
}
fft.fft_1d(&mut buf, ne, nlines, false)?;
for l in 0..nlines {
for k in 0..ne {
buf[l * ne + k] *= ramp[k];
}
}
fft.fft_1d(&mut buf, ne, nlines, true)?;
for l in 0..nlines {
for k in 0..detw {
proj[l * detw + k] = buf[l * ne + pad + k].re;
}
}
Ok(())
}
pub fn lamino(
proj: &[f32],
theta: &[f32],
lamino_angle_deg: f32,
n: usize,
rh: usize,
filter: FilterName,
fft: &dyn Fft,
) -> Result<Vec<f32>> {
let nproj = theta.len();
let (detw, deth) = (n, proj.len() / (nproj * n));
assert_eq!(
proj.len(),
nproj * deth * detw,
"proj shape != [nproj, nz, n]"
);
let phi = lamino_phi(lamino_angle_deg);
let mut filtered = proj.to_vec();
ramp_filter_detw(&mut filtered, nproj, deth, detw, filter, fft)?;
let p22 = fft2d_fwd(&filtered, nproj, deth, detw, fft)?;
let p11 = usfft2d_adj(&p22, detw, n, deth, nproj, detw, theta, phi, fft)?;
let p00 = usfft1d_adj(&p11, detw, n, rh, deth, phi, fft)?;
let (n1, n2) = (n, n);
let mut vol = vec![0.0f32; rh * n1 * n2];
for ty in 0..n1 {
for tz in 0..rh {
for tx in 0..n2 {
vol[(tz * n1 + ty) * n2 + tx] = p00[(ty * rh + tz) * n2 + tx];
}
}
}
Ok(vol)
}
pub fn lamino_project(
vol: &[f32],
theta: &[f32],
lamino_angle_deg: f32,
n: usize,
nz: usize,
fft: &dyn Fft,
) -> Result<Vec<f32>> {
let nproj = theta.len();
let rh = vol.len() / (n * n);
assert_eq!(vol.len(), rh * n * n, "vol shape != [rh, n, n]");
let (detw, deth) = (n, nz);
let phi = lamino_phi(lamino_angle_deg);
let (n1, n2) = (n, n);
let mut p00 = vec![0.0f32; n1 * rh * n2];
for tz in 0..rh {
for ty in 0..n1 {
for tx in 0..n2 {
p00[(ty * rh + tz) * n2 + tx] = vol[(tz * n1 + ty) * n2 + tx];
}
}
}
let p11 = usfft1d_fwd(&p00, detw, n, rh, deth, phi, fft)?;
let p22 = usfft2d_fwd(&p11, detw, n, deth, nproj, detw, theta, phi, fft)?;
fft2d_inv(&p22, nproj, deth, detw, fft)
}
#[cfg(test)]
mod tests {
use super::*;
use crate::cpu::CpuBackend;
fn fill(seed: u64, n: usize) -> Vec<f32> {
let mut s = seed.wrapping_add(0x9E3779B97F4A7C15);
(0..n)
.map(|_| {
s ^= s << 13;
s ^= s >> 7;
s ^= s << 17;
((s >> 11) as f32 / (1u64 << 53) as f32) * 2.0 - 1.0
})
.collect()
}
fn cfill(seed: u64, n: usize) -> Vec<Complex32> {
let re = fill(seed, n);
let im = fill(seed ^ 0xABCDEF, n);
re.into_iter()
.zip(im)
.map(|(r, i)| Complex32::new(r, i))
.collect()
}
fn cdot(a: &[Complex32], b: &[Complex32]) -> f64 {
a.iter()
.zip(b)
.map(|(x, y)| (x.re * y.re + x.im * y.im) as f64)
.sum()
}
fn rdot(a: &[f32], b: &[f32]) -> f64 {
a.iter().zip(b).map(|(x, y)| (*x * *y) as f64).sum()
}
#[test]
fn fft2d_inv_inverts_fwd() {
let cpu = CpuBackend::new();
let (ntheta, deth, detw) = (3, 8, 8);
let p = fill(1, ntheta * deth * detw);
let g = fft2d_fwd(&p, ntheta, deth, detw, &cpu).unwrap();
let r = fft2d_inv(&g, ntheta, deth, detw, &cpu).unwrap();
let err: f32 = p
.iter()
.zip(&r)
.map(|(a, b)| (a - b).abs())
.fold(0.0, f32::max);
eprintln!("fft2d inv round-trip max abs err = {err:.2e}");
assert!(err < 1e-4, "fft2d_inv does not invert fft2d_fwd: {err:.2e}");
}
#[test]
fn usfft1d_fwd_adj_are_transposes() {
let cpu = CpuBackend::new();
let (n0, n1, n2, deth) = (6, 5, 8, 8);
let f = fill(3, n1 * n2 * n0);
let g = cfill(4, n1 * deth * n0);
let phi = std::f32::consts::FRAC_PI_2 + 0.3;
let af = usfft1d_fwd(&f, n0, n1, n2, deth, phi, &cpu).unwrap();
let atg = usfft1d_adj(&g, n0, n1, n2, deth, phi, &cpu).unwrap();
let lhs = cdot(&af, &g);
let rhs = rdot(&f, &atg);
let rel = (lhs - rhs).abs() / lhs.abs().max(rhs.abs()).max(1e-9);
eprintln!("usfft1d adjoint: lhs={lhs:.6} rhs={rhs:.6} rel={rel:.2e}");
assert!(rel < 1e-4, "usfft1d fwd/adj not transposes: rel={rel:.2e}");
}
#[test]
fn usfft2d_fwd_adj_are_transposes() {
let cpu = CpuBackend::new();
let (n0, n1, nky, ntheta, detw) = (6, 5, 4, 7, 6);
let theta: Vec<f32> = (0..ntheta).map(|i| i as f32 / ntheta as f32 * PI).collect();
let phi = std::f32::consts::FRAC_PI_2 + 0.35;
let f = cfill(5, n1 * nky * n0);
let g = cfill(6, ntheta * nky * detw);
let af = usfft2d_fwd(&f, n0, n1, nky, ntheta, detw, &theta, phi, &cpu).unwrap();
let atg = usfft2d_adj(&g, n0, n1, nky, ntheta, detw, &theta, phi, &cpu).unwrap();
let lhs = cdot(&af, &g);
let rhs = cdot(&f, &atg);
let rel = (lhs - rhs).abs() / lhs.abs().max(rhs.abs()).max(1e-9);
eprintln!("usfft2d adjoint: lhs={lhs:.6} rhs={rhs:.6} rel={rel:.2e}");
assert!(rel < 1e-4, "usfft2d fwd/adj not transposes: rel={rel:.2e}");
}
fn pearson(a: &[f32], b: &[f32]) -> f64 {
let n = a.len() as f64;
let ma = a.iter().map(|&v| v as f64).sum::<f64>() / n;
let mb = b.iter().map(|&v| v as f64).sum::<f64>() / n;
let (mut cov, mut va, mut vb) = (0.0, 0.0, 0.0);
for (&x, &y) in a.iter().zip(b) {
let (dx, dy) = (x as f64 - ma, y as f64 - mb);
cov += dx * dy;
va += dx * dx;
vb += dy * dy;
}
cov / (va.sqrt() * vb.sqrt()).max(1e-12)
}
#[test]
fn lamino_roundtrip_recovers_phantom() {
let cpu = CpuBackend::new();
let (n, rh, nz) = (16usize, 8usize, 16usize);
let nproj = 64usize;
let lamino_angle = 20.0f32;
let theta: Vec<f32> = (0..nproj)
.map(|i| i as f32 / nproj as f32 * 2.0 * PI)
.collect();
let mut vol = vec![0.0f32; rh * n * n];
let sphere = |vol: &mut [f32], cz: f32, cy: f32, cx: f32, r: f32, val: f32| {
for z in 0..rh {
for y in 0..n {
for x in 0..n {
let d2 = (z as f32 - cz).powi(2)
+ (y as f32 - cy).powi(2)
+ (x as f32 - cx).powi(2);
if d2 <= r * r {
vol[(z * n + y) * n + x] = val;
}
}
}
}
};
sphere(&mut vol, 3.0, 6.0, 6.0, 2.5, 1.0);
sphere(&mut vol, 5.0, 10.0, 9.0, 1.8, 0.6);
let proj = lamino_project(&vol, &theta, lamino_angle, n, nz, &cpu).unwrap();
let rec = lamino(&proj, &theta, lamino_angle, n, rh, FilterName::Ramp, &cpu).unwrap();
let corr = pearson(&rec, &vol);
eprintln!("lamino round-trip corr = {corr:.4}");
assert!(corr > 0.7, "lamino round-trip corr too low: {corr:.4}");
}
}