I am trying to write C++ code to find the intersection points of a segment intersecting a tetrahedron. I reduced the problem like this:
For each face of the tetrahedron (a triangle), find the intersection point of the line segment. Then, I have three cases: a) The segment doesn't intersect any face - thus either the segment is entirely in the tetrahedron or completely outside. b) The segment only intersects one face. Then I just need to determine the side of the segment that is in the tetrahedron and I get the two points that are in the tetrahedron. c) The segment intersects two faces. I am having trouble implementing this algorithm. Here are the issues:
If the segment and triangle are in the same plane, how do I find the intersection points? How can I determine if the segment lies on one of the edges of the tetrahedron? Thanks.
So your segment has endpoints $\mathbf p_1, \mathbf p_2$ and one of the planes is $\mathbf n \cdot \mathbf r = d$, where $\mathbf n$ points into the tetrahedron, then examine $\mathbf n \cdot \mathbf r - d$ for $\mathbf r = \mathbf p_1$ and $\mathbf r = \mathbf p_2$.
Otherwise, your segment intersects the tetrahedron in at least one point. Go through your planes and if for any of them you have a negative point:
When you are done, both points will have positive or 0 values for each plane, and at least one of them will have 0 values for some plane.
The segment is in a plane if both endpoints lie within that plane. The segment is along an edge, if both endpoints lie within the edge.