2

I need to know when two triangles intersect in a 3D environment, given the 3 points. Any help appreciate have been stuck on this for a long time, ive been told "Step one. Get the equations of the planes containing the triangles. Step two. Compute the line of intersection of these planes. Step three. Find the points of intersection of this line with each of these triangles. If either miss you are done, since the triangles can't overlap. Step four. These intersection points define a pair of intervals (one for each triangle). If the intervals overlap, the triangles overlap along this overlap interval. Otherwise they do not overlap."

I can do step one, and half of step two and three, I took the cross product of the normals to get the direction of line of intersection but don't know how to find another point on it.

  • Welcome to Math.SE! Do the answers here or here help? – Andrew D. Hwang Apr 12 '15 at 13:44
  • 1
    i'm not very knowledgeable in any areas of high level math aside from calculus. I can do basic matrix math but not anything fancy. If his algorithm is so famous and his is such a common problem why are there no direct tutorials on it?? I basically need something I can translate directly into an algorithm – BinkyNichols Apr 12 '15 at 14:04
  • also no it doesn't help me. Can know one give a full example give that so many programmers want to know this very question every day and no one answers it? It would help alot of people if someone gave a full answer or example. – BinkyNichols Apr 12 '15 at 14:15
  • This paper of Tomas Akenine-Moller (which I assume is the one mentioned by John Hughes in the second post I linked) looks pretty readable; some great diagrams, and nothing but vector algebra. The strategy it implements is that outlined by Michael Burr's answer (in the first post I linked). I'd be a bit surprised if you couldn't find the algorithm already implemented with a Free Software license. – Andrew D. Hwang Apr 12 '15 at 14:30
  • Incidentally, it seems your question is more thorny than it may first appear. Getting the intersection of two triangles potentially entails handling coplanar triangles, and pairs of triangles whose edges touch but whose interiors do not. If the triangles are small, handling round-off is crucial. Out of curiosity, can you safely assume these issues are avoidable, i.e., that your triangles are large, and either "widely-separated" or "definitely crossing"? – Andrew D. Hwang Apr 12 '15 at 14:38
  • Yes, those issues arn't relevant. – BinkyNichols Apr 13 '15 at 02:07
  • Try here: https://www.geometrictools.com/Source/Mathematics.html – bubba Mar 11 '24 at 03:29

2 Answers2

1

$\DeclareMathOperator{\ht}{ht}$Here's an algorithm and associated formulas, under the assumptions that the two triangles have non-empty interior, i.e., non-collinear vertices; no vertex of either triangle lies in the plane of the other, particularly, the triangles are not coplanar; and the triangles either are disjoint or their interiors intersect. The parenthetical notes below indicate how these assumptions affect the logic of the algorithm.

If $p = (p_{x}, p_{y}, p_{z})$ and $q = (q_{x}, q_{y}, q_{z})$ are vectors, their dot and cross products are given by the formulas $$ p \cdot q = p_{x} q_{x} + p_{y} q_{y} + p_{z} q_{z},\qquad p \times q = (p_{y} q_{z} - p_{z} q_{y}, p_{z} q_{x} - p_{x} q_{z}, p_{x} q_{y} - p_{y} q_{x}). $$

Denote the triangles by $P$ and $Q$, their respective vertices by $p_{1}$, $p_{2}$, $p_{3}$ and $q_{1}$, $q_{2}$, $q_{3}$, and let $$ n_{P} = (p_{2} - p_{1}) \times (p_{3} - p_{1}),\qquad n_{Q} = (q_{2} - q_{1}) \times (q_{3} - q_{1}) $$ be the respective normal vectors to the planes containing the triangles.

(Note: We're assuming these vectors are non-zero, i.e., that neither triangle has collinear vertices.)

Calculate the "heights" $$ \ht(p_{1}) = n_{Q} \cdot (p_{1} - q_{1}),\qquad \ht(p_{2}) = n_{Q} \cdot (p_{2} - q_{1}),\qquad \ht(p_{3}) = n_{Q} \cdot (p_{3} - q_{1}) $$ of the vertices $p_{i}$ with respect to the plane containing the $q_{i}$. If these three numbers are all positive or all negative, the triangles do not intersect (because $P$ does not cross the plane containing $Q$). If at least one is positive and one is negative, calculate the "heights" $$ \ht(q_{1}) = n_{P} \cdot (q_{1} - p_{1}),\qquad \ht(q_{2}) = n_{P} \cdot (q_{2} - p_{1}),\qquad \ht(q_{3}) = n_{P} \cdot (q_{3} - p_{1}) $$ of the vertices $q_{i}$ with respect to the plane containing the $p_{i}$. If these three numbers are all positive or all negative, the triangles do not intersect (because $Q$ does not cross the plane containing $P$).

(Notes: If the "height" of a vertex is zero, that vertex lies in the plane of the other triangle. If all three vertices of one triangle have height zero, the triangles are coplanar; if exactly two vertices have "height" zero, that triangle has an edge lying in the plane of the other triangle; if exactly one vertex has height zero, that triangle either touches the plane of the other triangle at one point, or else crosses the plane of the other triangle. In each case, a separate intersection test is required.)

Assume from now on that each triangle crosses the plane containing the other, i.e., that two of the numbers $\ht(p_{1})$, $\ht(p_{2})$, $\ht(p_{3})$ have the same sign and the third has opposite sign, and similarly for $\ht(q_{1})$, $\ht(q_{2})$, $\ht(q_{3})$. Suppose for definiteness that $\ht(p_{1})$ has opposite sign to $\ht(p_{2})$ and $\ht(p_{3})$, and that $\ht(q_{1})$ has opposite sign to $\ht(q_{2})$ and $\ht(q_{3})$.

The planes of the triangles intersect in a line $\ell$ (because we're assuming non-coplanar triangles), and the four points $$ p_{1j} = p_{1} + \frac{\ht(p_{1})}{\ht(p_{1}) - \ht(p_{j})}(p_{j} - p_{1}),\qquad q_{1j} = q_{1} + \frac{\ht(q_{1})}{\ht(q_{1}) - \ht(q_{j})}(q_{j} - q_{1})\quad j = 2, 3, $$ lie on $\ell$. Since $\ell$ has unit direction vector $$ v = \frac{n_{P} \times n_{Q}}{\|n_{P} \times n_{Q}\|}, $$ each point $x$ of $\ell$ may be written uniquely as $x = p_{12} + (tx)v$, with the scalar $tx$ defined by $$ tx = (x - p_{12}) \cdot v. $$ Clearly $tp_{12} = 0$; calculate the scalars $$ tp_{13} = (p_{13} - p_{12}) \cdot v,\qquad tq_{12} = (q_{12} - p_{12}) \cdot v,\qquad tq_{13} = (q_{13} - p_{12}) \cdot v, $$ and form the real intervals $$ I_{P} = [pa, pb] = \bigl[\min(0, tp_{13}), \max(0, tp_{13})\bigr],\qquad I_{Q} = [qa, qb] = \bigl[\min(tq_{12}, tq_{13}), \max(tq_{12}, tq_{13})\bigr]. $$

The triangles $P$ and $Q$ intersect if and only if the intervals $I_{P}$ and $I_{Q}$ intersect, if and only if $\max(pa, qa) < \min(pb, qb)$, in which case the intersection is the interval $$ [a, b] = \bigl[\max(pa, qa), \min(pb, qb)\bigr], $$ and the triangles themselves intersect in the interval with endpoints $$ p_{12} + av,\qquad p_{12} + bv. $$

0

If you have the equations of the planes in Cartesian form, to find the line of intersection you have to solve them simultaneously. First eliminate any one of the letters $x$, $y$ or $z$. Then, from the single equation that remains, separate the letters on opposite sides of the equation and make both sides equal to a parameter, $\lambda$, say. Now all you need to do is get each of $x$, $y$ and $z$ in terms of $\lambda$, whereupon you have the equation of the line of intersection of the planes.

David Quinn
  • 35,087