10.9 Linear Component to Triangle, Rectangle, Tetrahedron, Oriented Box 437
gradient ∇Q = 0; this can be expressed as finding the solution to the system of
equations
a
00
a
01
a
02
a
10
a
11
a
12
a
20
a
21
a
22
b
0
b
1
b
2
=
u
v
t
which can be done by inverting the 3 ×3 matrix. As noted in Section 2.5.4, a matrix
must have a nonzero determinant in order for it to be invertible; in this case, the
determinant is zero if and only if the linear component and the plane are parallel,
and so we handle this as a special case.
For each type of linear component, the first step is to find the determinant of
the system, and if it is nonzero, to then compute the solution in order to find the
region in which the solution lies. Once the region has been determined, calculations
specific to that region are made in order to find the point ( ¯u, ¯v,
¯
t) on the boundary
that represents the minimum.
Line to Triangle
For the line to triangle distance, the solution domain breaks up the domain into six
regions. The pseudocode to determine the region is as follows:
float LineTriangleDistanceSquared(Line line, Triangle triangle)
{
e0 = triangle.v[1] - triangle.v[0];
e1 = triangle.v[2] - triangle.v[0];
a00 = Dot(e0, e0);
a01 = Dot(e0, e1);
a02 = -Dot(e0, line.direction);
a11 = Dot(e1, e1);
a12 = -Dot(e1, line.direction);
a22 = Dot(line.direction, line.direction);
diff = triangle.v[0] - line.base;
b0 = Dot(e0, diff);
b1 = Dot(e1, diff);
b2 = -Dot(line.direction, diff);
c = Dot(diff, diff);
// Cofactors to be used for determinant
// and inversion of matrix A
cof00 = a11 * a22 - a12 * a12;
cof01 = a02 * a12 - a01 * a22;
cof02 = a01 * a12 - a02 * a11;