# Intersection Test Utility Functions

## Ray AABB Intersection (Slab)

```cpp
bool RayAABB(const glm::vec3 &orig, const glm::vec3 &dir, const glm::vec3 &minB, const glm::vec3 &maxB, float &tmin)
{
    float t1 = (minB.x - orig.x) / dir.x;
    float t2 = (maxB.x - orig.x) / dir.x;
    float t3 = (minB.y - orig.y) / dir.y;
    float t4 = (maxB.y - orig.y) / dir.y;
    float t5 = (minB.z - orig.z) / dir.z;
    float t6 = (maxB.z - orig.z) / dir.z;

    float tminCandidate = std::max({std::min(t1, t2), std::min(t3, t4), std::min(t5, t6)});
    float tmaxCandidate = std::min({std::max(t1, t2), std::max(t3, t4), std::max(t5, t6)});

    if (tmaxCandidate < 0 || tminCandidate > tmaxCandidate)
        return false;
    tmin = tminCandidate;
    return true;
}
```

## Ray Triangle Intersection

```cpp
bool RayTriangle(const Ray &ray, const Triangle &tri, float &t)
{
    const float EPS = 1e-6f;
    glm::vec3 edge1 = tri.v1 - tri.v0;
    glm::vec3 edge2 = tri.v2 - tri.v0;
    glm::vec3 pvec = glm::cross(ray.dir, edge2);
    float det = glm::dot(edge1, pvec);
    if (fabs(det) < EPS)
        return false;
    float invDet = 1.0f / det;

    glm::vec3 tvec = ray.origin - tri.v0;
    float u = glm::dot(tvec, pvec) * invDet;
    if (u < 0 || u > 1)
        return false;

    glm::vec3 qvec = glm::cross(tvec, edge1);
    float v = glm::dot(ray.dir, qvec) * invDet;
    if (v < 0 || u + v > 1)
        return false;

    t = glm::dot(edge2, qvec) * invDet;
    return t > EPS;
}
```
