mikedh / trimesh

Python library for loading and using triangular meshes.
https://trimesh.org
MIT License
3.02k stars 583 forks source link

Ray intersection behavior on edges and vertex #2317

Open brandonrwin opened 2 weeks ago

brandonrwin commented 2 weeks ago

RayMeshIntersector.intersects_location() has the following comment:

If you are counting the number of hits a ray had, this method should be used as if only the triangle index is used on- edge hits will be counted twice.

I'm having trouble understanding this, but I think what it's trying to say to me doesn't go far enough.

There's three possibilities for a ray hitting a "sane" mesh:

  1. Hits the interior of a triangle: one hit, one triangle
  2. Hits an edge: two hits, one for each triangle the edge belongs to
  3. Hits a vertex: n hits, one for each triangle the vertex belongs to

So, my questions are:

  1. How does trimesh.ray.raytriangle handle 3? From the comment, it's not clear what I should do in the case of 2 or 3. Maybe we can come up with something that provides some more direct guidance for edges, and a clear indication of vertex behavior.
  2. As is, the comment appears to be incorrect when embree is used (see below).

For embree's handling of edge and vertex hits, a develop said:

For context, I'm making a, probably futile, attempt to quickly use this in a real world scenario, where a ray passing through without detection means real world things get smashed. 😅

brandonrwin commented 2 weeks ago

What do you think of this, for a modified comment?

Edge and vertex hits will not be detected reliably. Edges can result in 0, 1, or 2 hits and vertices can result in 0 to n hits, where n is the number of triangles that share that vertex. Multiple rays, with slight offsets, can be used to help prevent the 0 hit case.