# Wavefront mesh: determine which face a point belongs to?

I have a 3D mesh Wavefront .obj file.

Is there any algorithm that takes an arbitrary point coordinates as input and determines which face of the mesh that point belongs to ??

The mesh is rendered on the screen, then the user clicks on it, I want to determine which part of the mesh the user has clicked on ?

Here's the code using LibGDX:

``````Vector3 intersection=new Vector3();
Ray ray=camera.getPickRay(x, y);
//vertices is an array that hold the coordinates of the mesh
boolean ok=Intersector.intersectRayTriangles(ray, vertices, intersection);
``````

Thanks

-

## 2 Answers

For each face in the mesh: test for a ray-triangle intersection using a ray that goes through the pixel the user clicked on.

You can compute the ray using gluUnproject (or the equivalent math). Unproject two points with winX and winY set to where the user clicked: one with winZ=0.0 at the near plane, and one with winZ=1.0 at the far plane. These two points form a line you can use to test for triangle intersections.

-
Thanks for the answer, I'm using LibGDX, I was able to get the intersection vector, but still cann't tell which face this vector is within ? – Mina Samy Nov 28 '12 at 14:00
Test all the faces for intersection with the vector, and you will find out. lighthouse3d.com/tutorials/maths/ray-triangle-intersection – ccxvii Nov 28 '12 at 14:12

OBJ format is not meant to reflect how you store things in memory and operate on data. The fact that you're using an OBJ format is hence not relevant. You need to be loading your data into an appropriate in-memory format that makes the operations you need efficient.

In the most general case you store a list of `Triangle` objects and iterate over each to do a ray-triangle test. You can optimize this heavily by using a smarter data structure, such as a model-local BSP tree (or some other spatial partitioning as appropriate for the kinds of shapes you're using).

-