Graphics Reference
In-Depth Information
// For all three slabs
for(inti=0;i<3;i++) {
if (Abs(d[i]) < EPSILON) {
// Ray is parallel to slab. No hit if origin not within slab
if (p[i] < a.min[i] || p[i] > a.max[i]) return 0;
} else {
// Compute intersection t value of ray with near and far plane of slab
float ood = 1.0f / d[i];
float t1 = (a.min[i] - p[i]) * ood;
float t2 = (a.max[i] - p[i]) * ood;
// Make t1 be intersection with near plane, t2 with far plane
if (t1 > t2) Swap(t1, t2);
// Compute the intersection of slab intersection intervals
if (t1 > tmin) tmin = t1;
if (t2 > tmax) tmax = t2;
// Exit with no collision as soon as slab intersection becomes empty
if (tmin > tmax) return 0;
}
}
// Ray intersects all 3 slabs. Return point (q) and intersection t value (tmin)
q=p+d*tmin;
return 1;
}
If the same ray is being intersected against a large number of boxes, the three
divisions involved can be precomputed once for the ray and then be reused for all tests.
As a further optimization, the outcome of the statement if (t1 > t2) Swap(t1, t2)
is also completely predetermined by the signs of the components of the ray direction
vector. It could therefore be removed by determining in advance which one of eight (or
four for a 2D test) alternative routines to call, in which the effects of the if statements
have been folded into the surrounding code.
Note that the presented ray-box intersection test is a special case of the intersection
test of a ray against the Kay-Kajiya slab volume, described in Section 4.6.1. Increasing
the number of slabs allows an arbitrarily good fit of the convex hulls of objects to be
achieved. The Kay-Kajiya test is, in turn, really just a specialization of the Cyrus-Beck
clipping algorithm [Cyrus78]. A Cyrus-Beck-like clipping approach is also used in
Section 5.3.8 for the intersection of a ray or segment against a convex polyhedron.
If the problem is just to test if the segment intersects with the box, without deter-
mining the intersection point an alternative solution is to use the separating-axis
test. Without loss of generality, a coordinate system can be chosen in which the box
is centered at the origin and oriented with the coordinate axes. For an AABB, the
centering is accomplished by translating the segment along with the AABB to the
origin. For an OBB, the segment endpoints can be transformed from world space into
 
Search WWH ::




Custom Search