Graphics Reference
In-Depth Information
A
P
Q
D
B
C
Figure 5.7
The point
Q
on the tetrahedron
ABCD
closest to
P
.
by calling the
ClosestPtPointTriangle()
function (defined in the previous section)
once for each face plane of the tetrahedron
P
. Of all computed points, the one closest
to
P
is returned as
Q
. Separately from the distance tests, a different test is made to
see if
P
lies inside all face planes. When it does,
P
itself is the closest point.
Assuming the tetrahedron
ABCD
has been defined so that its faces
ABC
,
ACD
,
ADB
, and
BDC
all are arranged counterclockwise when viewed from outside the
tetrahedron, this solution can be implemented as follows.
Point ClosestPtPointTetrahedron(Point p, Point a, Point b, Point c, Point d)
{
// Start out assuming point inside all halfspaces, so closest to itself
Point closestPt = p;
float bestSqDist = FLT_MAX;
// If point outside face abc then compute closest point on abc
if (PointOutsideOfPlane(p, a, b, c)) {
Point q = ClosestPtPointTriangle(p, a, b, c);
float sqDist = Dot(q - p,q-p);
// Update best closest point if (squared) distance is less than current best
if (sqDist < bestSqDist) bestSqDist = sqDist, closestPt = q;
}
// Repeat test for face acd
if (PointOutsideOfPlane(p, a, c, d)) {
Point q = ClosestPtPointTriangle(p, a, c, d);
float sqDist = Dot(q - p,q-p);
if (sqDist < bestSqDist) bestSqDist = sqDist, closestPt = q;
}