Search code examples
mathgeometrynearest-neighborcad

Finding nearest element to a point STL format


A short introduction: I have a body that is expressed by the outer-surface, given in STL form (set of triangular elements and their outside-pointing normal vector). I'm trying to detect if a point given by coordinates is inside or outside the body.

The problem: How do I find the nearest element to a given point? More specifically, say you have found the nearest vertex to the point and this vertex is shared by two (or more) elements. Which is is the "nearest" one? Note that the end result is determining if the point is inside or outside the body. A simple normal distance (dot product with the normal) does not solve the problem and can lead to ambiguous result based on which of the elements, sharing the node is selected. Using the centroid of the element is also problematic.

Any suggestions, ideas (especially from people who have been involved in this issue before) are most welcomed!

EDIT: I'll make the issue slightly harder. Say there's an open surface (but it covers the whole domain so that every point is on one of two sides of the surface, either in or out, based on the direction relative to the normal. This also needs to be answered using the same approach.

EDIT2: Answer was found!

Hope this helps!


Solution

  • The problem was answered with a variation of the ray-intersection method. 1. Find the nearest vertex, using the L2 norm (squared). 2. Consider the elements which share the vertex. It is recommended to have a connectivity list and not map them every time. 3. Cast a ray is cast from the interest point to the centroid of the first element. 4. Check among the elements in <2> which intersect the ray and select the one with the shortest intersection distance 5. Use the dot product of the intersection vector with the element normal (negative sign = outside, else inside)

    (This was added to the question post itself)