Graphics Reference
In-Depth Information
5.1.4.1 Distance of Point to OBB
To obtain the squared distance between the point P and the closest point on OBB B ,
the previous function could be called in this way:
// Computes the square distance between point p and OBB b
float SqDistPointOBB(Point p, OBB b)
{
Point closest;
ClosestPtPointOBB(p, b, closest);
float sqDist = Dot(closest - p, closest - p);
return sqDist;
}
If only the squared distance and not the closest point is needed, this code can be
further simplified. By projecting the vector v from the center of B to P onto each of the
three OBB axes, the distance d from P to the box center along that axis is obtained.
Because the axes are orthogonal, any excess amount that d is beyond the extent of
the box for a given axis can be computed, squared, and added to the total squared
distance of P independently of the other two axes.
// Computes the square distance between point p and OBB b
float SqDistPointOBB(Point p, OBB b)
{
Vectorv=p-b.c;
float sqDist = 0.0f;
for(inti=0;i<3;i++) {
// Project vector from box center to p on each axis, getting the distance
// of p along that axis, and count any excess distance outside box extents
float d = Dot(v, b.u[i]), excess = 0.0f;
if (d < -b.e[i])
excess=d+b.e[i];
else if (d > b.e[i])
excess=d-b.e[i];
sqDist += excess * excess;
}
return sqDist;
}
 
Search WWH ::




Custom Search