Minimum distance between point and face

12.5k Views Asked by At

Given a point in 3D space of the form (x, y, z) and a triangle consisting of 3 vectors (also in the (x, y, z) format), how would I calculate the minimum distance between the point and the face of the triangle?

I understand it involves the normal of the plane that the triangle lies on however I'm unsure how to calculate the plane and then the magnitude of the plane's normal, to the point.

Thanks for any help!

2

There are 2 best solutions below

1
On

without going into all the gory detail, maybe i can outline one approach to the problem which may assist you in developing your own understanding of the situation. the method can also easily be applied to a hyperplane in a higher-dimensional space.

if your vectors are $v_1, v_2, v_3$ then a point in the plane determined by their endpoints must have the form $$ \alpha v_1 + \beta v_2 + \gamma v_3$$

with the additional condition that

$$ \alpha + \beta +\gamma = 1$$

the square of the distance of a vector's endpoint from the origin is given by the scalar product of the vector with itself. we can attempt therefore to minimize this squared distance. using Lagrange's technique, we attempt to find a value of $\lambda$ so that the three derivatives of the expression:

$$ L(\lambda) = (\alpha v_1 + \beta v_2 + \gamma v_3)^2 - \lambda(\alpha + \beta +\gamma) $$

with respect to $\alpha, \beta, \gamma $ all vanish. the squaring here signifies a scalar product, so it is convenient to introduce the constants $k_{ij}$ where $$ k_{ij} = v_i . v_j$$

now $$ \frac{\partial L}{\partial \alpha} = 2v_1 . (\alpha v_1 + \beta v_2 + \gamma v_3) - \lambda \\ = 2(k_{11} \alpha + k_{12} \beta + k_{13} \gamma) - \lambda $$ setting this equal to zero, and likewise for the other two partial derivatives, then together with the condition $ \alpha + \beta +\gamma = 1$ this will give you a system of four linear equations in the four unknowns $\alpha, \beta, \gamma$ and $\lambda$, and solving this system will give you the coordinates of the point where the normal drawn through the origin meets the plane determined by the vertices of your triangle.

3
On

A good way to solve it is using vectors:

First we have to find the normal to the triangle. Let's say the triangle is defined by vectors $v_1, v_2, v_3$ and our point is $p$

Then to calculate normal vector, just do:

$$ n = (v_2 - v_1) * ( v_3 - v_1 ) $$ (where '*' denotes cross product)

and normalize it:

$$ n_n = \frac{n}{||n||} $$

Now we have to find the intersection between the line with direction $ n_n$ passing through $ p $ and the plane of the triangle.

The plane of the triangle is defined by:

$$ n_n · x = n_n · v_1 $$ for each point $ x $

and the line passing through $ p $ and direction $ n_n $ is:

$$ x = p + t·n_n $$

so the intersection point must satisfy:

$$ n_n · (p + t·n_n ) = n_n · v_1 $$

simplifying:

$$ n_n·p + t = n_n·v_1 $$

and:

$$ t = n_n·v_1 - n_n·p $$

So, the intersection point with the plane is:

$$ p_0 = p + t·n_n $$

and the distance:

$$ \text{distance} = || p-p_0 || $$

Now we need to know if the intersection point lies inside our triangle. To do this we can use barycentric coordinates. In the point $ p_0 $ lies inside the triangle its barycentric coordinates must lie in the interval $ [0,1] $.

We can determine this point just doing:

$$ α·v_1 + \beta·v_2 + \gamma·v_3 = p_0 $$

and solving the system for $ α, \beta $ and $ \gamma $. They all must lie in the $[0,1]$ interval.

EDIT ---------------------

As the comment from Samuel Danielson states, if some of the barycentric coordinates is not in the $ [0,1] $ interval, it means that the point $p0$ found is the point of the plane containing the triangle with the minimum distance to the point $p$. This means that the point in the triangle with minimum distance to $p$ lies in one vertex or one side of the triangle.

So, in case that some of the barycentric are outside the $ [0,1] $ interval, we can find which is the point (in one edge or in one vertex) of the triangle nearest to $p$:

Look at the sign of $ α, \beta $ and $ \gamma $. If only one of them is positive, then the nearest point is the corresponding vertex:

$v_1$ if the only positive is $ α $

$v_2$ if the only positive is $ \beta $

$v_3$ if the only positive is $ \gamma $

and in case there are two positive coordinates and only one negative, this means that the nearest point lies in the corresponding edge:

$v_1, v_2$ if the only negative is $ \gamma $

$v_1, v_3$ if the only negative is $ \beta $

$v_2, v_3$ if the only negative is $ α $

In that case you have to find the point in the segment (edge) nearest to $p$