Syntax
[b,t]=intersectstriangle46(p,d,v0,v1,v2)
Input Parameter
p : | | Point of line |
d : | | Direction of line |
v0 : | | Vertex 0 |
v1 : | | Vertex 1 |
v2 : | | Vertex 2 |
Output Parameter
b : | | if t>0 and intersecting point exists |
t : | | intersecting point is p+t*d |
Examples
[a,b]=intersectstriangle([5 5 -10],[0 0 1],[0 0 0],[10 0 0],[10 10 0])
Copyright 2012-2025 Tim C. Lueth. All rights reserved. The code is the property of Tim C. Lueth and may not be redistributed or modified without explicit written permission. This software may be used free of charge for academic research and teaching purposes only. Commercial use, redistribution, modification, or reverse engineering is strictly prohibited. Access to source code is restricted and granted only under specific agreements. For licensing inquiries or commercial use, please contact: Tim C. Lueth
Algorithm (Workflow)
This function, intersectstriangle46
, determines if a ray intersects with a triangle in 3D space. It is a fundamental function for intersection calculations between a ray and a triangle.
Input Parameters
- p0: A point on the line (ray origin).
- d: The direction vector of the line (ray direction).
- p1: Vertex 0 of the triangle.
- p2: Vertex 1 of the triangle.
- p3: Vertex 2 of the triangle.
Output Results
- b: A boolean indicating if the intersection point exists and is in the direction of the ray.
- t: The parameter for the intersection point along the ray, where the intersection point is
p0 + t * d
.
Algorithm Steps
- Initialize a threshold
thr
to 1e-7
for numerical precision.
- Set
t
to infinity, indicating no intersection initially.
- Calculate edge vectors of the triangle:
e1 = p2 - p1
and e2 = p3 - p1
.
- Compute the cross product
h = cross(d, e2)
.
- Calculate
a = e1 * h'
(dot product).
- Check if
a
is near zero (within thr
), indicating the ray is parallel to the triangle's plane. If so, set b = false
and return.
- Compute
f = 1/a
.
- Calculate vector
s = p0 - p1
.
- Determine
u = f * (s * h')
. If u
is outside the range [0, 1], set b = false
and return.
- Compute
q = cross(s, e1)
.
- Calculate
v = f * (d * q')
. If v
is less than 0 or u + v > 1
, set b = false
and return.
- Determine
t = f * (e2 * q')
.
- If
t > thr
, set b = true
and return, indicating a valid intersection point exists.
- If none of the above conditions are met, set
b = false
.
Algorithm explaination created using ChatGPT on 2025-08-19 07:13. (Please note: No guarantee for the correctness of this explanation)
Last html export of this page out of FM database by TL: 2025-09-21