i
i
i
i
i
i
i
i
744 16. Intersection Test Methods
returns, then the box is behind the ray origin. line 15 is executed if the
ray is parallel to the slab (and so cannot intersect it); it tests if the ray is
outside the slab. If so, then the ray misses the box and the test terminates.
For even faster code, Haines discusses a way of unwrapping the loop and
thereby avoiding some code [480].
There is an additional test not shown in the pseudocode that is worth
adding in actual code. As mentioned when we defined the ray, we usually
want to find the closest object. So after line 15, we could also test whether
t
min
≥ l,wherel is the current ray length. If the new intersection is not
closer, the intersection is rejected. This test could be deferred until after
the entire ray/OBB test has been completed, but it is usually more efficient
to try for an early rejection inside the loop.
There are other optimizations for the special case of an OBB that is
an AABB. Lines 5 and 6 change to e = p
i
and f = d
i
, which makes the
test faster. Normally the a
min
and a
max
corners of the AABB are used
on lines 8 and 9, so the addition and subtraction is avoided. Kay and
Kajiya [639] and Smits [1200] note that line 7 can be avoided by allowing
division by 0 and interpreting the processor’s results correctly. Williams et
al. [1352] provide implementation details to handle division by 0 correctly,
along with other optimizations. Woo [1373] introduced an optimization
where only three candidate planes are identified and tested against.
A generalization of the slabs method can be used to compute the inter-
section of a ray with a k-DOP, frustum, or any convex polyhedron; code is
available on the web [481].
16.7.2 Line Segment/Box Overlap Test
In this section, it will be shown that the separating axis test on page 731
can be used to determine whether a line segment overlaps an AABB [454].
The line segment has finite length in contrast to a ray. Assume the line
segment is defined by a center (mean of the two endpoints) c and a half
vector w. Furthermore, both the AABB, denoted B, and the line segment
have been translated so the AABB’s origin is (0, 0, 0). Also, the size of the
box is (2h
x
, 2h
y
, 2h
z
), i.e., h is the half vector of the box. This is shown in
Figure 16.8.
It is normally most effective to first test against an axis that is orthog-
onal to a face of both objects to be tested. Since the line segment does not
have a face, it does not generate any test. The box generates three axes to
test: (1, 0, 0), (0, 1, 0), and (0, 0, 1). Finally, the axes formed from the cross
product between line segment direction and the box axes should be tested.
Thetestfortheaxis(1, 0, 0) is shown below, and Figure 16.8 shows this
as well. The other two axes are similar:
|c
x
| > |w
x
| + h
x
. (16.16)