use sciart_core::rng::{cell_key, hash01};
use sciart_core::{Grid, Size};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum XEdge {
Wrap,
Clamp,
}
#[derive(Debug, Clone, Default)]
pub struct Flow {
pub u: Grid<f32>,
pub v: Grid<f32>,
}
impl Flow {
pub fn resize(&mut self, lattice: Size) {
self.u.resize(lattice, 0.0);
self.v.resize(lattice, 0.0);
}
#[cfg(any(test, feature = "test-support"))]
pub fn fill_with(&mut self, mut f: impl FnMut(f64, f64) -> (f64, f64)) {
let w = usize::from(self.u.width()).max(1);
let (us, vs) = (self.u.as_mut_slice(), self.v.as_mut_slice());
for (k, (u, v)) in us.iter_mut().zip(vs.iter_mut()).enumerate() {
let (a, b) = f((k % w) as f64, (k / w) as f64);
*u = a as f32;
*v = b as f32;
}
}
}
#[cfg(any(test, feature = "test-support"))]
#[inline]
pub fn stream_velocity(psi: impl Fn(f64, f64) -> f64, x: f64, y: f64, h: f64) -> (f64, f64) {
let e = 0.5 * h;
let u = (psi(x, y + e) - psi(x, y - e)) / h;
let v = -(psi(x + e, y) - psi(x - e, y)) / h;
(u, v)
}
#[inline]
pub fn sample(g: &Grid<f32>, x: f32, y: f32, edge: XEdge) -> f32 {
let (w, h) = (usize::from(g.width()), usize::from(g.height()));
if w == 0 || h == 0 || !(x.is_finite() && y.is_finite()) {
return 0.0;
}
let cells = g.as_slice();
let (wf, hf) = (w as f32, h as f32);
let y = y.clamp(0.0, hf - 1.0);
let (y0, fy) = (y.floor(), y - y.floor());
let j0 = y0 as usize;
let j1 = (j0 + 1).min(h - 1);
let (i0, i1, fx) = match edge {
XEdge::Wrap => {
let x = x.rem_euclid(wf);
let x0 = x.floor();
let i0 = (x0 as usize) % w;
(i0, (i0 + 1) % w, x - x0)
}
XEdge::Clamp => {
let x = x.clamp(0.0, wf - 1.0);
let x0 = x.floor();
let i0 = x0 as usize;
(i0, (i0 + 1).min(w - 1), x - x0)
}
};
let (r0, r1) = (&cells[j0 * w..][..w], &cells[j1 * w..][..w]);
let top = r0[i0] + fx * (r0[i1] - r0[i0]);
let bottom = r1[i0] + fx * (r1[i1] - r1[i0]);
top + fy * (bottom - top)
}
pub fn departures(flow: &Flow, dt: f32, edge: XEdge, mut f: impl FnMut(usize, f32, f32)) {
let w = usize::from(flow.u.width());
if w == 0 || flow.u.size() != flow.v.size() {
return;
}
let (us, vs) = (flow.u.as_slice(), flow.v.as_slice());
for (k, (&u, &v)) in us.iter().zip(vs).enumerate() {
let (x, y) = ((k % w) as f32, (k / w) as f32);
let (xm, ym) = (x - 0.5 * dt * u, y - 0.5 * dt * v);
let (um, vm) = (sample(&flow.u, xm, ym, edge), sample(&flow.v, xm, ym, edge));
f(k, x - dt * um, y - dt * vm);
}
}
pub fn advect(src: &Grid<f32>, dst: &mut Grid<f32>, flow: &Flow, dt: f32, edge: XEdge) {
dst.resize(src.size(), 0.0);
if flow.u.size() != src.size() {
dst.fill(0.0);
return;
}
let out = dst.as_mut_slice();
departures(flow, dt, edge, |k, x, y| out[k] = sample(src, x, y, edge));
}
#[inline]
pub fn value_noise_wrap_x(salt: u64, x: f64, y: f64, period: i32) -> f64 {
if !(x.is_finite() && y.is_finite()) || period < 1 {
return 0.0;
}
let p = f64::from(period);
let x = x.rem_euclid(p);
let (x0, y0) = (x.floor(), y.floor());
let (fx, fy) = (x - x0, y - y0);
let (sx, sy) = (fx * fx * (3.0 - 2.0 * fx), fy * fy * (3.0 - 2.0 * fy));
let (i, j) = ((x0 as i32).rem_euclid(period), y0 as i32);
let at =
|di: i32, dj: i32| hash01(salt ^ cell_key((i + di).rem_euclid(period), j.wrapping_add(dj)));
let top = at(0, 0) + sx * (at(1, 0) - at(0, 0));
let bottom = at(0, 1) + sx * (at(1, 1) - at(0, 1));
top + sy * (bottom - top)
}
#[cfg(test)]
mod tests {
use super::*;
use sciart_core::Trig;
fn gaussian_psi(x: f64, y: f64) -> f64 {
let (dx, dy) = (x - 10.0, y - 6.0);
-30.0 * (-(dx * dx / 16.0 + dy * dy / 4.0)).exp() + 0.7 * (0.4 * y).sin_det()
}
#[test]
fn stream_velocity_is_discretely_divergence_free() {
let h = 1.0;
for &(x, y) in &[(3.0, 4.0), (10.2, 6.1), (12.7, 3.3), (0.5, 9.5)] {
let e = 0.5 * h;
let (ue, _) = stream_velocity(gaussian_psi, x + e, y, h);
let (uw, _) = stream_velocity(gaussian_psi, x - e, y, h);
let (_, vs) = stream_velocity(gaussian_psi, x, y + e, h);
let (_, vn) = stream_velocity(gaussian_psi, x, y - e, h);
let div = (ue - uw + vs - vn) / h;
assert!(div.abs() < 1e-9, "div {div} at ({x}, {y})");
}
}
#[test]
fn sample_wraps_and_clamps() {
let mut g = Grid::new(Size::new(4, 2), 0.0f32);
g.set(3, 0, 1.0);
assert!((sample(&g, 3.5, 0.0, XEdge::Wrap) - 0.5).abs() < 1e-6);
assert!((sample(&g, -0.5, 0.0, XEdge::Wrap) - 0.5).abs() < 1e-6);
assert!((sample(&g, 9.0, -3.0, XEdge::Clamp) - 1.0).abs() < 1e-6);
assert_eq!(sample(&g, f32::NAN, 0.0, XEdge::Wrap), 0.0);
assert_eq!(sample(&Grid::default(), 1.0, 1.0, XEdge::Wrap), 0.0);
}
#[test]
fn uniform_flow_translates_a_field() {
let size = Size::new(16, 4);
let mut src = Grid::new(size, 0.0f32);
src.set(5, 2, 1.0);
let mut flow = Flow::default();
flow.resize(size);
flow.fill_with(|_, _| (2.0, 0.0));
let mut dst = Grid::default();
advect(&src, &mut dst, &flow, 0.5, XEdge::Wrap);
assert!((dst.get(6, 2).copied().unwrap_or(0.0) - 1.0).abs() < 1e-6);
assert!(dst.get(5, 2).copied().unwrap_or(1.0).abs() < 1e-6);
}
#[test]
fn value_noise_wrap_x_is_the_core_noise_made_periodic() {
for k in 0..200 {
let (x, y) = (f64::from(k) * 0.37, f64::from(k) * 0.11);
let xr = x.rem_euclid(8.0);
if xr < 7.0 {
let core = sciart_core::rng::value_noise(3, xr, y, 1.0);
assert!((core - value_noise_wrap_x(3, x, y, 8)).abs() < 1e-12);
}
let a = value_noise_wrap_x(3, x, y, 8);
let b = value_noise_wrap_x(3, x + 8.0, y, 8);
assert!((a - b).abs() < 1e-12);
}
assert_eq!(value_noise_wrap_x(1, f64::NAN, 0.0, 8), 0.0);
}
}