Blue Noise

Triangle Intersection Test

The following is an implementation of a triangle intersection algorithm for testing if a ray will intersect a triangle in C++; If it does intersect it returns true and the point at which it will intersect is returned in ‘outIntersectionPoint’, otherwise it returns false:

bool RayIntersectsTriangle(Vector3D rayOrigin, Vector3D rayVector,
    Triangle *inTriangle, Vector3D &outIntersectionPoint)
{
    const float EPSILON = 0.0000001;
    Vector3D vertex0 = inTriangle->vertex0;
    Vector3D vertex1 = inTriangle->vertex1;
    Vector3D vertex2 = inTriangle->vertex2;
    Vector3D edge1, edge2, h, s, q;
    float a, f, u, v;

    edge1 = vertex1 - vertex0;
    edge2 = vertex2 - vertex1;
    h = rayVector.crossProduct(edge2);
    a = edge1.dotProduct(h);
    if(a > -EPSILON && a < EPSILON) {
        return false;
    }

    f = 1.0/a;
    s = rayOrigin - vertex0;
    u = f * s.dotProduct(h);
    if(u < 0.0 || u > 1.0) {
        return false;
    }

    q = s.crossProduct(edge1);
    v = f * rayVector.dotProduct(q);
    if(v < 0.0 || u + v > 1.0) { return false; }

    // At this stage we can compute 't' to find out where the intersection
    // point is on the line.
    if(t > EPSILON) { // ray intersection
        outIntersectionPoint = rayOrigin + rayVector * t;
        return true;
    } else { // This means that there is a line intersection but not a ray intersection.
        return false;
    }
}