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::*;

/// Constructs an orthonormal tangent-bitangent basis `(tangent, bitangent)` around unit `normal` using the singularity-free Duff et al. formulation.
///
/// # Arguments
///
/// - `Vec3` - Unit surface normal vector.
///
/// # Returns
///
/// - `(Vec3, Vec3)` - Right-handed orthonormal `(tangent, bitangent)` axes perpendicular to `normal`.
#[must_use]
pub fn build_orthonormal_basis(normal: Vec3) -> (Vec3, Vec3) {
    let n: Vec3 = normal.normalize();
    let nz: f32 = n.get_z();
    let sign: f32 = if nz >= 0.0 { 1.0 } else { -1.0 };
    let a: f32 = -1.0 / (sign + nz);
    let b: f32 = n.get_x() * n.get_y() * a;
    let tangent: Vec3 = Vec3::new(
        1.0 + sign * n.get_x() * n.get_x() * a,
        sign * b,
        -sign * n.get_x(),
    )
    .normalize();
    let bitangent: Vec3 = Vec3::new(b, sign + n.get_y() * n.get_y() * a, -n.get_y()).normalize();
    (tangent, bitangent)
}

/// Orients geometric and shading normals against an incoming ray direction and reports whether the hit struck the front face.
///
/// # Arguments
///
/// - `Vec3` - Incoming ray direction vector.
/// - `Vec3` - Outward geometric face normal.
/// - `Vec3` - Outward interpolated shading normal.
///
/// # Returns
///
/// - `(Vec3, Vec3, bool)` - Oriented `(geometric_normal, shading_normal, front_face)` tuple.
#[must_use]
pub fn orient_hit_normals(
    ray_dir: Vec3,
    geometric_normal: Vec3,
    shading_normal: Vec3,
) -> (Vec3, Vec3, bool) {
    let gn_unit: Vec3 = geometric_normal.normalize();
    let front_face: bool = gn_unit.dot(ray_dir) < 0.0;
    let oriented_gn: Vec3 = if front_face {
        gn_unit
    } else {
        gn_unit.scale(-1.0)
    };
    let sn_unit: Vec3 = shading_normal.normalize();
    let oriented_sn: Vec3 = if sn_unit.dot(oriented_gn) >= 0.0 {
        sn_unit
    } else {
        sn_unit.scale(-1.0)
    };
    (oriented_gn, oriented_sn, front_face)
}

/// Intersects a ray with an `RtTriangle` using the watertight Möller–Trumbore algorithm returning `(distance, u, v)`.
///
/// # Arguments
///
/// - `Vec3` - Ray origin in triangle local space.
/// - `Vec3` - Ray direction in triangle local space (may be unnormalized for instanced ray transforms).
/// - `&RtTriangle` - Target triangle storing `v0`, `edge1`, and `edge2`.
/// - `f32` - Minimum accepted parametric distance.
/// - `f32` - Maximum accepted parametric distance.
///
/// # Returns
///
/// - `Option<(f32, f32, f32)>` - Parametric hit distance and clamped barycentric coordinates `(t, u, v)` if intersected.
#[must_use]
pub fn intersect_triangle_moller_trumbore(
    origin: Vec3,
    direction: Vec3,
    triangle: &RtTriangle,
    t_min: f32,
    t_max: f32,
) -> Option<(f32, f32, f32)> {
    let edge1: Vec3 = triangle.get_edge1();
    let edge2: Vec3 = triangle.get_edge2();
    let pvec: Vec3 = direction.cross(edge2);
    let det: f32 = edge1.dot(pvec);
    if det.abs() < RT_EPSILON {
        return None;
    }
    let inv_det: f32 = 1.0 / det;
    let tvec: Vec3 = origin.sub(triangle.get_v0());
    let raw_u: f32 = tvec.dot(pvec) * inv_det;
    if raw_u < -RT_TRIANGLE_EDGE_EPSILON || raw_u > 1.0 + RT_TRIANGLE_EDGE_EPSILON {
        return None;
    }
    let qvec: Vec3 = tvec.cross(edge1);
    let raw_v: f32 = direction.dot(qvec) * inv_det;
    if raw_v < -RT_TRIANGLE_EDGE_EPSILON || raw_u + raw_v > 1.0 + RT_TRIANGLE_EDGE_EPSILON {
        return None;
    }
    let dist: f32 = edge2.dot(qvec) * inv_det;
    if !dist.is_finite() {
        return None;
    }
    if dist < t_min || dist > t_max {
        return None;
    }
    let u: f32 = raw_u.clamp(0.0, 1.0);
    let v: f32 = raw_v.clamp(0.0, 1.0 - u);
    Some((dist, u, v))
}

/// Computes the scalar axis component `(0 = X, 1 = Y, 2 = Z)` of a `Vec3`.
///
/// # Arguments
///
/// - `Vec3` - Source 3D vector.
/// - `usize` - Axis index (`0`, `1`, or `2`).
///
/// # Returns
///
/// - `f32` - Selected axis scalar value.
#[must_use]
pub(crate) fn vec3_axis(value: Vec3, axis: usize) -> f32 {
    match axis {
        0 => value.get_x(),
        1 => value.get_y(),
        _ => value.get_z(),
    }
}

/// Evaluates the upper-left 3x3 determinant of a column-major `Mat4`.
///
/// # Arguments
///
/// - `&Mat4` - Affine transformation matrix.
///
/// # Returns
///
/// - `f32` - Linear 3x3 determinant.
#[must_use]
pub(crate) fn mat4_linear_determinant(matrix: &Mat4) -> f32 {
    let elems: [f32; 16] = matrix.get_elements();
    let col0: Vec3 = Vec3::new(elems[0], elems[1], elems[2]);
    let col1: Vec3 = Vec3::new(elems[4], elems[5], elems[6]);
    let col2: Vec3 = Vec3::new(elems[8], elems[9], elems[10]);
    col0.dot(col1.cross(col2))
}

/// Evaluates a smooth polynomial minimum between two SDF distances with blend radius `smoothness`.
///
/// # Arguments
///
/// - `f32` - First signed distance value.
/// - `f32` - Second signed distance value.
/// - `f32` - Positive smoothing radius.
///
/// # Returns
///
/// - `f32` - Smoothly blended minimum signed distance.
#[must_use]
pub(crate) fn sdf_smooth_min(dist_a: f32, dist_b: f32, smoothness: f32) -> f32 {
    let k: f32 = smoothness.max(RT_EPSILON);
    let h: f32 = (0.5 + 0.5 * (dist_b - dist_a) / k).clamp(0.0, 1.0);
    dist_b * (1.0 - h) + dist_a * h - k * h * (1.0 - h)
}

/// Evaluates a smooth polynomial subtraction carving `cutout` from `base` with blend radius `smoothness`.
///
/// # Arguments
///
/// - `f32` - Base solid signed distance value.
/// - `f32` - Cutout volume signed distance value.
/// - `f32` - Positive smoothing radius.
///
/// # Returns
///
/// - `f32` - Smoothly carved signed distance.
#[must_use]
pub(crate) fn sdf_smooth_subtract(base_dist: f32, cutout_dist: f32, smoothness: f32) -> f32 {
    let k: f32 = smoothness.max(RT_EPSILON);
    let h: f32 = (0.5 - 0.5 * (base_dist + cutout_dist) / k).clamp(0.0, 1.0);
    base_dist * (1.0 - h) + (-cutout_dist) * h + k * h * (1.0 - h)
}