Möller–Trumbore Intersection Algorithm
   HOME

TheInfoList



OR:

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 In mathematics, the intersection of two or more objects is another object consisting of everything that is contained in all of the objects simultaneously. For example, in Euclidean geometry, when two lines in a plane are not parallel, thei ...
of a
ray Ray may refer to: Fish * Ray (fish), any cartilaginous fish of the superorder Batoidea * Ray (fish fin anatomy), a bony or horny spine on a fin Science and mathematics * Ray (geometry), half of a line proceeding from an initial point * Ray (gra ...
and a
triangle A triangle is a polygon with three edges and three vertices. It is one of the basic shapes in geometry. A triangle with vertices ''A'', ''B'', and ''C'' is denoted \triangle ABC. In Euclidean geometry, any three points, when non- colli ...
in
three dimensions Three-dimensional space (also: 3D space, 3-space or, rarely, tri-dimensional space) is a geometric setting in which three values (called ''parameters'') are required to determine the position of an element (i.e., point). This is the informal ...
without needing precomputation of the plane equation of the
plane Plane(s) most often refers to: * Aero- or airplane, a powered, fixed-wing aircraft * Plane (geometry), a flat, 2-dimensional surface Plane or planes may also refer to: Biology * Plane (tree) or ''Platanus'', wetland native plant * ''Planes'' ...
containing the triangle. Among other uses, it can be used in
computer graphics Computer graphics deals with generating images with the aid of computers. Today, computer graphics is a core technology in digital photography, film, video games, cell phone and computer displays, and many specialized applications. A great deal ...
to implement ray tracing computations involving
triangle mesh In computer graphics, a triangle mesh is a type of polygon mesh. It comprises a set of triangles (typically in three dimensions) that are connected by their common edges or vertices. Many graphics software packages and hardware devices can ...
es.


Calculation


Definitions

The ray is defined by an origin point O and a direction vector \vec. Every point on the ray can be expressed by \vec(t) = O + t\vec, where the parameter t ranges from zero to infinity. The triangle is defined by three vertices, named v_1, v_2, v_3. 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 v_1, and a vector that is orthogonal to every point on that plane, such as the cross product between the vector from v_1 to v_2 and the vector from v_1 to v_3: \vec \cdot (P_1 - P_2) = 0, where \vec = (v_2 - v_1) \times (v_3 - v_1), and P_1 and P_2 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. When this happens, the
dot product In mathematics, the dot product or scalar productThe term ''scalar product'' means literally "product with a scalar as a result". It is also used sometimes for other symmetric bilinear forms, for example in a pseudo-Euclidean space. is an alg ...
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 In mathematics, an affine space is a geometric structure that generalizes some of the properties of Euclidean spaces in such a way that these are independent of the concepts of distance and measure of angles, keeping only the properties relat ...
, any point on the triangle can be expressed as a
convex combination In convex geometry and vector algebra, a convex combination is a linear combination of points (which can be vectors, scalars, or more generally points in an affine space) where all coefficients are non-negative and sum to 1. In other ...
of the triangle's vertices: P = w v_1 + u v_2 + v v_3 The coefficients must be non-negative and sum to 1, so w can be replaced with 1 - u - v: \begin P &= (1 - u - v) v_1 + u v_2 + v v_3 \\ P &= v_1 + u (v_2 - v_1) + v (v_3 - v_1) \end, where P is any point on the plane. Observe that \vec = v_2 - v_1 and \vec = v_3 - v_1 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 u e_1 + v e_2 and can be translated by v_1 to "move" that point onto the plane that the triangle is on. To find u and v 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 O + tD &= v_1 + u(v_2 - v_1) + v(v_3 - v_1) \\ O - v_1 &= -tD + u(v_2 - v_1) + v(v_3 - v_1) \end This is a system of linear equations with three equations (one each for x, y, z) and three unknowns (t, u, and v), and can be represented as a matrix-vector multiplication. \begin \vert & \vert & \vert \\ -D & (v2 - v1) & (v3 - v1) \\ \vert & \vert & \vert \end \begin t \\ u \\ v \end = O - v_1 This equation will always have a solution when the matrix has three linearly independent column vectors in \mathbb^3 and is thus
invertible In mathematics, the concept of an inverse element generalises the concepts of opposite () and reciprocal () of numbers. Given an operation denoted here , and an identity element denoted , if , one says that is a left inverse of , and that ...
. 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 In linear algebra, Cramer's rule is an explicit formula for the solution of a system of linear equations with as many equations as unknowns, valid whenever the system has a unique solution. It expresses the solution in terms of the determinants ...
to find the t, u, and v values for an intersection, and if it lies within the triangle, the exact coordinates of the intersection can be found by plugging in t to the ray's equation.


C++ implementation

The following is an implementation of the algorithm in
C++ C++ (pronounced "C plus plus") is a high-level general-purpose programming language created by Danish computer scientist Bjarne Stroustrup as an extension of the C programming language, or "C with Classes". The language has expanded significa ...
: 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 Rust is an iron oxide, a usually reddish-brown oxide formed by the reaction of iron and oxygen in the catalytic presence of water or air moisture. Rust consists of hydrous iron(III) oxides (Fe2O3·nH2O) and iron(III) oxide-hydroxide (FeO(OH), ...
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 Java (; id, Jawa, ; jv, ꦗꦮ; su, ) is one of the Greater Sunda Islands in Indonesia. It is bordered by the Indian Ocean to the south and the Java Sea to the north. With a population of 151.6 million people, Java is the world's mo ...
using javax.vecmath from
Java 3D Java 3D is a scene graph-based 3D application programming interface (API) for the Java platform. It runs on top of either OpenGL or Direct3D until version 1.6.0, which runs on top of Java OpenGL (JOGL). Since version 1.2, Java 3D has bee ...
API: public class MollerTrumbore


See also

* Badouel intersection algorithm
MATLAB version
of this algorithm (highly vectorized)
Baldwin-Weber ray-triangle intersection algorithm
* Schlick–Subrenat algorithm
Ray Intersection of Tessellated Surfaces: Quadrangles versus Triangles
', Schlick C., Subrenat G. Graphics Gems 1993
for ray-quadrilateral intersection


Links


Fast Minimum Storage Ray-Triangle Intersection

Optimizations on the basic algorithm by Möller & Trumbore
code from ''journal of graphics tools''


References

{{DEFAULTSORT:Moller-Trumbore intersection algorithm Geometric algorithms Geometric intersection