some advice on muller trumbore code for ray-triangle intersection.

Started by
0 comments, last by daftasbrush 15 years, 9 months ago
hello, this is the ray triangle intersection code based on the muller - trumbore paper i read-

bool intersect_triangle(vector *o, vector *d, vector *v0, vector *v1,
vector *v2, double *t, double *u, double *v)
{
    vector edge1, edge2, pvec, qvec, tvec;
    double det, inv_det;

    vector_sub(v1, v0, &edge1);
    vector_sub(v2, v0, &edge2);
    vector_cross(d, &edge2, &pvec);
    det = vector_dot(&edge1, &pvec);

    if (det < DBL_EPSILON)
    {
        return (false);
    }

    vector_sub(o, v0, &tvec);
    *u = vector_dot(&tvec, &pvec);

    if (*u < 0.0 || *u > det)
    {
        return (false);
    }

    vector_cross(&tvec, &edge1, &qvec);
    *v = vector_dot(d, &qvec);

    if (*v < 0.0 || *u + *v > det)
    {
        return (false);
    }

    *t = vector_dot(&edge2, &qvec);
    inv_det = 1.0 / det;
    *t *= inv_det;

    if (*t < -DBL_EPSILON)
    {
        return (false);
    }

    *u *= inv_det;
    *v *= inv_det;

    return (true);

}
One thing I noticed is that if the ray origin lie on the object itself, then the triangle on which the origin lies is reported as the intersected triangle and the distance to triangle is 0. I think this is undesirable in case of multiple reflections i.e. when ray gets reflected of many surfaces on the object. In that case, *t < - DBL_EPSILON statement is the culprit and I'm not sure why they had this condition in the code. Any particular reasons ? If I change it to *t <= 0.0, the code is alright.
Advertisement
The DBL_EPSILON test is used (here, and plenty of other places)...
to catch problems with / from Floating Point precision.
Often, due to precision loss, complex calculations which should evaluate to 0.000000....
actually result in a "small" postive or negative answer -0.000000000000000001 say.

The DBL_EPSILON value should normally be used to 'account' for this problem and it value should be set something "appropriate".

In your case.. I would recommend...

Leave the DBL_EPSILON test in, but also store a reference to the last face hit / the "source face" for the new ray.

That way, you will detect the "source face" as a hit....
but you can ignore that hit, because you know a ray starting from "Face X" can't hit "Face X" again without hitting something else first...

and then you don't risk missing "real" hits, by not accounting for the FP Precision problem.

HTH

This topic is closed to new replies.

Advertisement