euv-core 0.48.2

Core cross-platform runtime for euv featuring arena-based fine-grained reactivity, deterministic layout, flat scene graph, multi-mode rendering, and direct OS platform adapters.
Documentation
use super::*;

/// Evaluates the 2D Poly6 smoothing kernel `W_poly6(r, h)` from squared distance `r2`.
///
/// # Arguments
///
/// - `f32` - Squared distance `r²` between two particles in `m²`.
/// - `f32` - Squared smoothing radius `h²` in `m²`.
/// - `f32` - Precomputed normalization factor `4 / (π h⁸)`.
///
/// # Returns
///
/// - `f32` - Non-negative Poly6 kernel weight in `m⁻²`.
#[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
}

/// Evaluates the 2D Spiky smoothing kernel gradient magnitude factor `-(30 / (π h⁵)) * (h - r)² / r`.
///
/// Multiply the returned scalar by `(dx, dy) = (p_i - p_j)` to obtain `∇W_spiky(p_i - p_j, h)`.
///
/// # Arguments
///
/// - `f32` - Euclidean distance `r` in meters (`0 < r < h`).
/// - `f32` - Smoothing radius `h` in meters.
/// - `f32` - Precomputed coefficient `-30 / (π h⁵)`.
///
/// # Returns
///
/// - `f32` - Radial gradient scalar in `m⁻⁴`.
#[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
}

/// Evaluates the Akinci-style smooth 2D cohesion-repulsion radial profile used for liquid surface tension.
///
/// Attractive for mid-range `0.5 h < r < h` and gently repulsive for very close `r < 0.5 h`.
///
/// # Arguments
///
/// - `f32` - Normalized distance `q = r / h` in `(0.0, 1.0)`.
///
/// # Returns
///
/// - `f32` - Dimensionless cohesion weight (negative = attractive, positive = repulsive).
#[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
    }
}

/// Projects a point `(x, y)` inside a rounded rectangular vessel `[pad, w - pad] × [pad, h - pad]` with bevel radius `corner_r`.
///
/// # Arguments
///
/// - `f32` - Candidate x-position in meters.
/// - `f32` - Candidate y-position in meters.
/// - `f32` - Vessel width in meters.
/// - `f32` - Vessel height in meters.
/// - `f32` - Minimum interior clearance from glass walls in meters.
/// - `f32` - Rounded interior corner radius in meters.
///
/// # Returns
///
/// - `(f32, f32, f32, f32, bool)` - Clamped `(x, y)`, inward contact normal `(nx, ny)`, and whether contact occurred.
#[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)
}

/// Generates a deterministic pseudo-random `f32` in `[0.0, 1.0)` using xorshift64*.
///
/// # Arguments
///
/// - `&mut u64` - Mutable PRNG state word.
///
/// # Returns
///
/// - `f32` - Uniform pseudo-random float in `[0.0, 1.0)`.
#[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)
}