Graphics Reference
In-Depth Information
and, in fact, be the orthogonal projection R , which can now be easily computed per
the preceding. This information is now enough to produce a code solution.
Point ClosestPtPointTriangle(Point p, Point a, Point b, Point c)
{
Vector ab=b-a;
Vector ac=c-a;
Vector bc=c-b;
// Compute parametric position s for projection P' of P on AB,
//P'=A+s*AB, s = snom/(snom+sdenom)
float snom = Dot(p - a, ab), sdenom = Dot(p - b,a-b);
// Compute parametric position t for projection P' of P on AC,
//P'=A+t*AC, s = tnom/(tnom+tdenom)
float tnom = Dot(p - a, ac), tdenom = Dot(p - c,a-c);
if (snom <= 0.0f && tnom <= 0.0f) return a;
// Vertex region early out
// Compute parametric position u for projection P' of P on BC,
//P'=B+u*BC, u = unom/(unom+udenom)
float unom = Dot(p - b, bc), udenom = Dot(p - c,b-c);
if (sdenom <= 0.0f && unom <= 0.0f) return b;
// Vertex region early out
if (tdenom <= 0.0f && udenom <= 0.0f) return c;
// Vertex region early out
// P is outside (or on) AB if the triple scalar product [N PA PB] <= 0
Vector n = Cross(b - a,c-a);
float vc = Dot(n, Cross(a - p, b - p));
// If P outside AB and within feature region of AB,
// return projection of P onto AB
if (vc <= 0.0f && snom >= 0.0f && sdenom >= 0.0f)
return a + snom / (snom + sdenom) * ab;
// P is outside (or on) BC if the triple scalar product [N PB PC] <= 0
float va = Dot(n, Cross(b - p, c - p));
// If P outside BC and within feature region of BC,
// return projection of P onto BC
if (va <= 0.0f && unom >= 0.0f && udenom >= 0.0f)
return b + unom / (unom + udenom) * bc;
// P is outside (or on) CA if the triple scalar product [N PC PA] <= 0
float vb = Dot(n, Cross(c - p, a - p));
 
Search WWH ::




Custom Search