mymesh.rays.RayTriangleIntersection#

mymesh.rays.RayTriangleIntersection(pt, ray, TriCoords, bidirectional=False, eps=1e-06)[source]#

Möller-Trumbore intersection algorithm to detect whether a ray intersects with a triangle. Möller, T., & Trumbore, B. (2005). Fast, minimum storage ray/triangle intersection. In ACM SIGGRAPH 2005 Courses, SIGGRAPH 2005. https://doi.org/10.1145/1198555.1198746 [MollerT05]

For multiple triangles, see RayTrianglesIntersection() and for multiple rays, see RaysTrianglesIntersection().

When choosing between RayTriangleIntersection(), RayTrianglesIntersection(), and RaysTrianglesIntersection(), one should generally only choose the one that has as much vectorization as is needed, and not more. For example, RayTriangleIntersection will generally be slightly more efficient than RayTrianglesIntersection() if only one triangle is being considered, but RayTrianglesIntersection() will be significantly faster than using RayTriangleIntersection() many times within a loop.

Parameters:
  • pt (array_like) – 3D coordinates for the starting point of the ray.

  • ray (array_like) – 3D vector of ray direction. This should, in general, be a unit vector.

  • TriCoords (array_like) – Coordinates of the three vertices of the triangle in the format np.array([[a, b, c], [d, e, f], [g, h, i]])

  • bidirectional (bool, optional) – Determines whether to check for intersections only the direction the ray is pointing, or in both directions (±ray), by default False.

  • eps (float, optional) – Small parameter used to determine if a value is sufficiently close to 0, by default 1e-6

Returns:

intersectionPt – If there is an intersection, the 3D coordinates of the intersection point are returned, otherwise [] is returned.

Return type:

np.ndarray or []

Examples

from mymesh import rays
import numpy as np

pt = [0,0,0]
ray = np.array([1.,1.,0.])
ray /= np.linalg.norm(ray)
TriCoords = np.array([
    [1,0,-1],
    [0,1,-1],
    [1,1,1]
])
intersection = rays.RayTriangleIntersection(pt, ray, TriCoords)
print(intersection)
[0.75 0.75 0.  ]

Flipping the direction causes the ray to no longer intersect with the triangle

ray = -ray

intersection = rays.RayTriangleIntersection(pt, ray, TriCoords)
print(intersection)
[]

Using the bidirectional=True option detects an intersection with the ray even though it’s pointing in the opposite direction.

intersection = rays.RayTriangleIntersection(pt, ray, TriCoords, bidirectional=True)
print(intersection)
[0.75 0.75 0.  ]