euv-core 0.50.0

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(red_val: f32, height_val: f32, spiky_grad_coeff: f32) -> f32 {
    if red_val <= 1.0e-7 || red_val >= height_val {
        return 0.0;
    }
    let diff: f32 = height_val - red_val;
    spiky_grad_coeff * diff * diff / red_val
}

/// Applies a symmetric smooth deadzone `dz` to a hardware sensor scalar so stationary MEMS noise maps cleanly to `0.0`.
///
/// # Arguments
///
/// - `f32` - Finite input sensor scalar.
/// - `f32` - Non-negative deadzone half-width.
///
/// # Returns
///
/// - `f32` - Deadzone-filtered value with continuous ramp above `dz`.
#[inline(always)]
#[must_use]
pub(crate) fn fluid_sensor_deadzone(value: f32, dz: f32) -> f32 {
    let abs_v: f32 = value.abs();
    if abs_v <= dz {
        0.0
    } else {
        value.signum() * (abs_v - dz)
    }
}

/// 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(
    pos_x: f32,
    pos_y: f32,
    width_val: f32,
    height_val: f32,
    pad: f32,
    corner_r: f32,
) -> (f32, f32, f32, f32, bool) {
    let min_x: f32 = pad;
    let max_x: f32 = (width_val - pad).max(pad + 1.0e-4);
    let min_y: f32 = pad;
    let max_y: f32 = (height_val - pad).max(pad + 1.0e-4);
    let mut cx: f32 = pos_x;
    let mut cy: f32 = pos_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 pos_x: u64 = *state;
    if pos_x == 0 {
        pos_x = FLUID_RNG_SEED;
    }
    pos_x ^= pos_x >> 12;
    pos_x ^= pos_x << 25;
    pos_x ^= pos_x >> 27;
    *state = pos_x;
    let bits: u32 = ((pos_x.wrapping_mul(0x2545_F491_4F6C_DD1D)) >> 40) as u32;
    (bits as f32) * (1.0 / 16_777_216.0)
}