Source code for simvx.graphics.picking.raycast
"""CPU-side raycaster for entity intersection tests."""
from __future__ import annotations
import logging
import numpy as np
log = logging.getLogger(__name__)
__all__ = ["ray_aabb_intersect"]
[docs]
def ray_aabb_intersect(
origin: np.ndarray,
direction: np.ndarray,
aabb_min: np.ndarray,
aabb_max: np.ndarray,
) -> tuple[bool, float]:
"""Test ray-AABB intersection. Returns (hit, t_near)."""
inv_dir = 1.0 / np.where(direction != 0, direction, 1e-30)
t1 = (aabb_min - origin) * inv_dir
t2 = (aabb_max - origin) * inv_dir
t_near = np.max(np.minimum(t1, t2))
t_far = np.min(np.maximum(t1, t2))
hit = (t_near <= t_far) and (t_far >= 0)
return bool(hit), float(t_near)