Graphics Reference
In-Depth Information
Vector ap=p-a;
t = Dot(ap, n);
if (t < 0.0f) return 0;
if (t > d) return 0;
// For segment; exclude this code line for a ray test
// Compute barycentric coordinate components and test if within bounds
Vector e = Cross(qp, ap);
v = Dot(ac, e);
if (v < 0.0f ||v>d)return 0;
w = -Dot(ab, e);
if (w < 0.0f ||v+w>d)return 0;
// Segment/ray intersects triangle. Perform delayed division and
// compute the last barycentric coordinate component
float ood = 1.0f / d;
t *= ood;
v *= ood;
w *= ood;
u=1.0f-v-w;
return 1;
}
This formulation differs slightly from the one given in [Möller97a] because as a
byproduct it computes the normal n of the triangle ABC , which is often useful to
have.
Another way of looking at this test is as consisting of first computing the intersec-
tion point S between the segment and the plane of the triangle. This point is then
tested for containment in the triangle through the computation of its barycentric
coordinates with respect to the triangle (as described in Section 3.4). Some of the
calculations in computing S and its barycentric coordinates can be shared, including
those for the normal of the triangle plane. These calculations can also be precomputed
and stored with the triangle.
When precomputation is allowed, the method can be further optimized by com-
puting and storing plane equations for the plane of the triangle as well as for what
could be considered the“edge planes”of the triangle (the three planes perpendicular
to the triangle plane, through each of the edges, as shown in Figure 5.25). By scaling
the edge plane equations so that they report a distance of one for the opposing tri-
angle vertex (not on the edge plane), the evaluation of the edge plane equations for
S directly give the barycentric coordinates of S (with respect to the opposing triangle
vertex). Note that it is only necessary to store two of the three edge planes, as the third
barycentric coordinate is directly obtained from the other two. The segment-triangle
test can now be implemented in four plane equation evaluations (plus a few stray
operations), exemplified by the following code.
 
Search WWH ::




Custom Search