use super::*;
#[inline(always)]
#[must_use]
pub(crate) fn poly6_2d(r2: f32, h2: f32, poly6_coeff: f32) -> f32 {
if r2 >= h2 {
return 0.0;
}
let diff: f32 = h2 - r2;
poly6_coeff * diff * diff * diff
}
#[inline(always)]
#[must_use]
pub(crate) fn spiky_grad_factor_2d(r: f32, h: f32, spiky_grad_coeff: f32) -> f32 {
if r <= 1.0e-7 || r >= h {
return 0.0;
}
let diff: f32 = h - r;
spiky_grad_coeff * diff * diff / r
}
#[inline(always)]
#[must_use]
pub(crate) fn surface_cohesion_weight(q: f32) -> f32 {
if q <= 0.0 || q >= 1.0 {
return 0.0;
}
let omt: f32 = 1.0 - q;
let profile: f32 = omt * omt * q;
if q < 0.5 {
profile * (0.5 - q) * 1.6
} else {
-profile * (q - 0.5) * 2.4
}
}
#[inline(always)]
#[must_use]
pub(crate) fn clamp_to_rounded_vessel(
x: f32,
y: f32,
w: f32,
h: f32,
pad: f32,
corner_r: f32,
) -> (f32, f32, f32, f32, bool) {
let min_x: f32 = pad;
let max_x: f32 = (w - pad).max(pad + 1.0e-4);
let min_y: f32 = pad;
let max_y: f32 = (h - pad).max(pad + 1.0e-4);
let mut cx: f32 = x;
let mut cy: f32 = y;
let mut nx: f32 = 0.0;
let mut ny: f32 = 0.0;
let mut hit: bool = false;
if cx < min_x {
cx = min_x;
nx = 1.0;
hit = true;
} else if cx > max_x {
cx = max_x;
nx = -1.0;
hit = true;
}
if cy < min_y {
cy = min_y;
ny = 1.0;
hit = true;
} else if cy > max_y {
cy = max_y;
ny = -1.0;
hit = true;
}
let cr: f32 = corner_r
.min((max_x - min_x) * 0.45)
.min((max_y - min_y) * 0.45)
.max(0.0);
if cr > 1.0e-5 {
let inner_min_x: f32 = min_x + cr;
let inner_max_x: f32 = max_x - cr;
let inner_min_y: f32 = min_y + cr;
let inner_max_y: f32 = max_y - cr;
let corner_cx: f32 = if cx < inner_min_x {
inner_min_x
} else if cx > inner_max_x {
inner_max_x
} else {
cx
};
let corner_cy: f32 = if cy < inner_min_y {
inner_min_y
} else if cy > inner_max_y {
inner_max_y
} else {
cy
};
if corner_cx != cx && corner_cy != cy {
let dx: f32 = cx - corner_cx;
let dy: f32 = cy - corner_cy;
let dist2: f32 = dx * dx + dy * dy;
if dist2 > cr * cr {
let dist: f32 = dist2.sqrt();
let inv: f32 = 1.0 / dist;
cx = corner_cx + dx * inv * cr;
cy = corner_cy + dy * inv * cr;
nx = -dx * inv;
ny = -dy * inv;
hit = true;
}
}
}
(cx, cy, nx, ny, hit)
}
#[inline(always)]
#[must_use]
pub(crate) fn fluid_next_rand_f32(state: &mut u64) -> f32 {
let mut x: u64 = *state;
if x == 0 {
x = FLUID_RNG_SEED;
}
x ^= x >> 12;
x ^= x << 25;
x ^= x >> 27;
*state = x;
let bits: u32 = ((x.wrapping_mul(0x2545_F491_4F6C_DD1D)) >> 40) as u32;
(bits as f32) * (1.0 / 16_777_216.0)
}