use ndarray::{Array3, ArrayViewMut2};
use crate::backend::Fft;
use crate::data::{Layout, Tomo};
use crate::dtype::Complex32;
use crate::error::Result;
use crate::geometry::Geometry;
const EPS: f64 = 1e-3;
#[inline]
fn shift_sign(i: usize) -> f32 {
if i.is_multiple_of(2) {
-1.0
} else {
1.0
}
}
pub fn fourierrec(
sino: &Tomo<f32>,
geom: &Geometry,
n: usize,
fft: &dyn Fft,
) -> Result<Array3<f32>> {
let b = sino.as_layout(Layout::Sinogram);
let nz = b.n_rows();
let nang = b.n_angles();
let nd = b.n_cols();
let ndf = nd as f64;
let neg_log_eps = -EPS.ln(); let mu = neg_log_eps / (2.0 * ndf * ndf);
let inside = mu * neg_log_eps + (mu * ndf) * (mu * ndf) / 4.0;
let m = (2.0 * ndf / std::f64::consts::PI * inside.sqrt()).ceil() as usize;
let ng = 2 * nd + 2 * m; let nf = 2 * nd; let mu = mu as f32;
let coeff0 = (std::f32::consts::PI) / (mu * 4.0 * (nd as f32) * (nd as f32)); let coeff1 = -std::f32::consts::PI * std::f32::consts::PI / mu; let gscale = 4.0 / nd as f32; let phi_sign = 1.0 - (nd % 4) as f32;
let phi_amp = std::f32::consts::PI * (nd as f32) * (nd as f32) / nang as f32;
let trig: Vec<(f32, f32)> = geom
.angles
.0
.iter()
.map(|&a| a.sin_cos())
.map(|(s, c)| (c, s))
.collect();
let bdata = b
.array
.as_slice()
.expect("contiguous sinogram (to_layout yields a standard-layout copy)");
let mut out = Array3::<f32>::zeros((nz, n, n));
let crop = (nd - n.min(nd)) / 2;
let process_row = |row: usize, mut slab: ArrayViewMut2<f32>| -> Result<()> {
let mut kern0 = vec![0.0f32; 2 * m + 1];
let mut kern1 = vec![0.0f32; 2 * m + 1];
let mut radial = vec![Complex32::new(0.0, 0.0); nang * nd];
for ia in 0..nang {
let src = row * nang * nd + ia * nd;
for j in 0..nd {
radial[ia * nd + j] = Complex32::new(bdata[src + j] * shift_sign(j), 0.0);
}
}
fft.fft_1d(&mut radial, nd, nang, false)?;
for ia in 0..nang {
for k in 0..nd {
radial[ia * nd + k] *= shift_sign(k);
}
}
let mut grid = vec![Complex32::new(0.0, 0.0); ng * ng];
for ia in 0..nang {
let (c, s) = trig[ia];
for td in 0..nd {
let mut x0 = (td as f32 - nd as f32 / 2.0) / nd as f32 * c;
let mut y0 = (td as f32 - nd as f32 / 2.0) / nd as f32 * s;
if x0 >= 0.5 {
x0 = 0.5 - 1e-5;
}
if y0 >= 0.5 {
y0 = 0.5 - 1e-5;
}
let g0 = radial[ia * nd + td] * gscale;
let base0 = (2.0 * nd as f32 * x0).floor() as isize - m as isize;
let base1 = (2.0 * nd as f32 * y0).floor() as isize - m as isize;
for (i0, kv) in kern0.iter_mut().enumerate() {
let w0 = (base0 + i0 as isize) as f32 / (2.0 * nd as f32) - x0;
*kv = (coeff1 * w0 * w0).exp();
}
for (i1, kv) in kern1.iter_mut().enumerate() {
let w1 = (base1 + i1 as isize) as f32 / (2.0 * nd as f32) - y0;
*kv = (coeff1 * w1 * w1).exp();
}
let col0 = nd as isize + m as isize + base0;
let row0 = nd as isize + m as isize + base1;
for (i1, &wy) in kern1.iter().enumerate() {
let rbase = (row0 + i1 as isize) as usize * ng;
for (i0, &wx) in kern0.iter().enumerate() {
let w = coeff0 * wx * wy;
let gc = (col0 + i0 as isize) as usize;
grid[rbase + gc] += g0 * w;
}
}
}
}
for ty in 0..ng {
for tx in 0..ng {
if tx < m || tx >= 2 * nd + m || ty < m || ty >= 2 * nd + m {
let tx0 = (tx + 2 * nd - m) % (2 * nd);
let ty0 = (ty + 2 * nd - m) % (2 * nd);
let v = grid[ty * ng + tx];
grid[(ty0 + m) * ng + (tx0 + m)] += v;
}
}
}
let mut inner = vec![Complex32::new(0.0, 0.0); nf * nf];
for ry in 0..nf {
for rx in 0..nf {
inner[ry * nf + rx] = grid[(ry + m) * ng + (rx + m)];
}
}
for ry in 0..nf {
for rx in 0..nf {
inner[ry * nf + rx] *= shift_sign(rx) * shift_sign(ry);
}
}
fft.fft_2d(&mut inner, nf, nf, 1, true)?;
for ry in 0..nf {
for rx in 0..nf {
inner[ry * nf + rx] *= shift_sign(rx) * shift_sign(ry);
}
}
for oy in 0..n {
let ty = oy + crop; let dy = ty as f32 / nd as f32 - 0.5;
let inner_row = ty + nd / 2;
let my = (ty as f32 - nd as f32 / 2.0) / nd as f32;
for ox in 0..n {
let tx = ox + crop;
let dx = tx as f32 / nd as f32 - 0.5;
let phi = (mu * (nd as f32) * (nd as f32) * (dx * dx + dy * dy)).exp()
* phi_amp
* phi_sign;
let inner_col = tx + nd / 2;
let v = inner[inner_row * nf + inner_col].re * phi;
let mx = (tx as f32 - nd as f32 / 2.0) / nd as f32;
let masked = if 4.0 * mx * mx + 4.0 * my * my < 1.0 {
v
} else {
0.0
};
slab[[oy, ox]] = masked;
}
}
Ok(())
};
fft.for_each_slice(&mut out, &process_row)?;
Ok(out)
}