Möller–Trumbore intersection algorithm explained
The Möller–Trumbore ray-triangle intersection algorithm, named after its inventors Tomas Möller and Ben Trumbore, is a fast method for calculating the intersection of a ray and a triangle in three dimensions without needing precomputation of the plane equation of the plane containing the triangle.[1] Among other uses, it can be used in computer graphics to implement ray tracing computations involving triangle meshes.[2]
Calculation
Definitions
The ray is defined by an origin point
and a direction vector
.Every point on the ray can be expressed by
, where the parameter
ranges from zero to infinity.The triangle is defined by three vertices, named
,
,
.The plane that the triangle is on, which is needed to calculate the ray-triangle intersection, is defined by a point on the plane, such as
, and a vector that is orthogonal to every point on that plane, such as the cross product between the vector from
to
and the vector from
to
:
, where
\vec{n}=(v2-v1) x (v3-v1)
, and
and
are any points on the plane.
Check if the ray is parallel to the triangle
First, find out if the ray intersects with the plane that the triangle is on, and if it does, find the coordinates of that intersection. The only way that the ray will not intersect the plane is if the ray's direction vector is parallel to the plane.[3] When this happens, the dot product between the ray's direction vector and the plane's normal vector will be zero. Otherwise, the ray does intersect the plane somewhere, but not necessarily within the triangle.
Check if the ray-plane intersection lies outside the triangle
Using barycentric coordinates, any point on the triangle can be expressed as a convex combination of the triangle's vertices:
The coefficients must be non-negative and sum to 1, so
can be replaced with
:
\begin{align}
P&=(1-u-v)v1+uv2+vv3\\
P&=v1+u(v2-v1)+v(v3-v1)
\end{align}
, where
is any point on the plane.Observe that
and
are vectors on the edge of the triangle, and together, they span a plane (which goes through the origin). Each point on that plane can be written as
and can be translated by
to "move" that point onto the plane that the triangle is on.
To find
and
for a particular intersection, set the ray expression equal to the plane expression, and put the variables on one side and the constants on the other.
\begin{align}
O+tD&=v1+u(v2-v1)+v(v3-v1)\\
O-v1&=-tD+u(v2-v1)+v(v3-v1)
\end{align}
This is a system of linear equations with three equations (one each for
,
,
) and three unknowns (
,
, and
), and can be represented as a matrix-vector multiplication.
\begin{bmatrix}
\vert&\vert&\vert\\
-D&(v2-v1)&(v3-v1)\\
\vert&\vert&\vert
\end{bmatrix}
\begin{bmatrix}t\ u\ v\end{bmatrix}
=O-v1
This equation will always have a solution when the matrix has three linearly independent column vectors in
and is thus
invertible. This happens if and only if the triangle vertices aren't collinear and the ray isn't parallel to the plane.
The algorithm can use Cramer's Rule to find the
,
, and
values for an intersection, and if it lies within the triangle, the exact coordinates of the intersection can be found by plugging in
to the ray's equation.
C++ implementation
The following is an implementation of the algorithm in C++:std::optional ray_intersects_triangle(const vec3 &ray_origin, const vec3 &ray_vector, const triangle3& triangle)
Rust implementation
The following is an implementation of the algorithm in Rust using the glam crate:fn moller_trumbore_intersection (origin: Vec3, direction: Vec3, triangle: Triangle) -> Option
Java implementation
The following is an implementation of the algorithm in Java using javax.vecmath
from Java 3D API:public class MollerTrumbore
See also
Links
Notes and References
- 10.1080/10867651.1997.10487468 . 2 . Fast, Minimum Storage Ray-Triangle Intersection . 1997 . Journal of Graphics Tools . 21–28 . Möller . Tomas . Trumbore . Ben.
- Web site: Ray-Triangle Intersection. 26 March 2011 . lighthouse3d. 2017-09-10.
- Note: If the ray's origin is itself on the plane, in addition to the ray's direction vector being parallel to the plane, than the entire ray is technically on the plane. However, since theoretical planes are infinitely thin, the ray would still be considered to not intersect the plane in that scenario.
- Ray Intersection of Tessellated Surfaces: Quadrangles versus Triangles, Schlick C., Subrenat G. Graphics Gems 1993