Reputation: 237
So I have this problem: I'm writing a program where you can click on a point on the screen with a 3D object projected and the vertex of triangle mesh that is visually (in 2D) closest to that point will be selected. I have calculated the barycenter coordinates of the intersection with a triangle. How can I use those barycentric coordinates to calculate the closest vertex? Thanks for help
Upvotes: 1
Views: 650
Reputation: 80187
For barycentric coordinates (u,v,w)
and triangle side length a,b,c
squared distance to A, B, C vertices are
dA^2 = -(a^2*v*w + b^2*w*(u-1) + c^2*v*(u-1)) =
-(a^2*v*w+b^2*w*u+c^2*v*u - b^2*w - c^2*v) =
b^2*w + c^2*v - Const (TheSameConstantForAllVertices)
dB^2 = -(a^2*(v-1)*w + b^2*w*u + c^2*(v-1)*u) =
a^2*w + c^2*u - Const
dC^2 = -(a^2*v*(w-1) + b^2*(w-1)*u + c^2*v*u)
a^2*v + b^2*u - Const
(more formulas here), so you can compare squared distances and choose the smallest one. I've separated simpler expression for comparison.
Perhaps you need some approximation for rather good (close to equilateral) triangles. In this case choose the largest value from barycentric triplet. It roughly corresponds to the closest vertex.
Upvotes: 1