euv-core 0.57.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 radical inverse Halton low-discrepancy sequence at `index` in prime `base`.
///
/// # Arguments
///
/// - `u32` - 1-based or 0-based sample index.
/// - `u32` - Prime base (`>= 2`, e.g. `2`, `3`, `5`).
///
/// # Returns
///
/// - `f32` - Low-discrepancy scalar in `[0.0, 1.0)`.
#[must_use]
pub fn halton_sequence(index: u32, base: u32) -> f32 {
    let b: u32 = base.max(2);
    let mut f: f32 = 1.0;
    let mut r: f32 = 0.0;
    let mut i: u32 = index;
    let inv_b: f32 = 1.0 / (b as f32);
    while i > 0 {
        f *= inv_b;
        r += f * ((i % b) as f32);
        i /= b;
    }
    r.clamp(0.0, 0.999_999_9)
}

/// Generates a 2D quasi-random sub-pixel offset in `[0.0, 1.0)^2` using the Martin Roberts R2 golden-ratio sequence.
///
/// # Arguments
///
/// - `u32` - Sample index.
///
/// # Returns
///
/// - `[f32; 2]` - Low-discrepancy sub-pixel jitter `(jx, jy)` in `[0.0, 1.0)^2`.
#[must_use]
pub fn r2_subpixel_jitter(sample_index: u32) -> [f32; 2] {
    let a1: f64 = 1.0 / RT_R2_PLASTIC_G;
    let a2: f64 = 1.0 / (RT_R2_PLASTIC_G * RT_R2_PLASTIC_G);
    let n: f64 = (sample_index as f64) + 1.0;
    let x: f64 = (0.5 + a1 * n).fract();
    let y: f64 = (0.5 + a2 * n).fract();
    [
        (x as f32).clamp(0.0, 0.999_999_9),
        (y as f32).clamp(0.0, 0.999_999_9),
    ]
}

/// Maps a uniform `[0.0, 1.0)^2` sample onto the unit disk `x^2 + y^2 <= 1` with low distortion (Shirley-Chiu concentric map).
///
/// # Arguments
///
/// - `[f32; 2]` - Uniform unit square sample `(u, v)`.
///
/// # Returns
///
/// - `[f32; 2]` - Uniform unit disk coordinate `(dx, dy)`.
#[must_use]
pub fn concentric_sample_disk(xi: [f32; 2]) -> [f32; 2] {
    let sx: f32 = 2.0 * xi[0].clamp(0.0, 1.0) - 1.0;
    let sy: f32 = 2.0 * xi[1].clamp(0.0, 1.0) - 1.0;
    if sx.abs() < RT_SCENE_EPSILON && sy.abs() < RT_SCENE_EPSILON {
        return [0.0, 0.0];
    }
    let (r, theta): (f32, f32) = if sx.abs() > sy.abs() {
        (sx, (PI * 0.25) * (sy / sx))
    } else {
        (sy, FRAC_PI_2 - (PI * 0.25) * (sx / sy))
    };
    [r * theta.cos(), r * theta.sin()]
}

/// Mixes a 32-bit float bit pattern into a running FNV-1a 64-bit hash accumulator.
///
/// # Arguments
///
/// - `u64` - Current FNV-1a hash state.
/// - `f32` - Float value to fold into the hash.
///
/// # Returns
///
/// - `u64` - Updated FNV-1a hash state.
#[must_use]
pub(crate) fn fnv1a_mix_f32(hash: u64, value: f32) -> u64 {
    let normalized: f32 = if value == 0.0 { 0.0 } else { value };
    let bits: u32 = normalized.to_bits();
    let mut h: u64 = hash;
    for byte in bits.to_le_bytes() {
        h ^= byte as u64;
        h = h.wrapping_mul(RT_FNV_PRIME);
    }
    h
}

/// Mixes a `Vec3` into a running FNV-1a 64-bit hash accumulator.
///
/// # Arguments
///
/// - `u64` - Current FNV-1a hash state.
/// - `Vec3` - Vector to fold into the hash.
///
/// # Returns
///
/// - `u64` - Updated FNV-1a hash state.
#[must_use]
pub(crate) fn fnv1a_mix_vec3(hash: u64, value: Vec3) -> u64 {
    let h1: u64 = fnv1a_mix_f32(hash, value.get_x());
    let h2: u64 = fnv1a_mix_f32(h1, value.get_y());
    fnv1a_mix_f32(h2, value.get_z())
}