# Ray-Triangle Intersection: Geometric Solution

Conventional Approach To The Ray-Triangle Intersection Test

In the previous paragraphs we have learned how to compute the plane's normal (which is the same as the triangle's normal). Next what we need to find out is the position of point P (for some illustrations we also used Phit), the point of intersection between the plane and the ray.

## Step 1: Finding P

We know that P is somewhere on the ray defined by its origin O and its direction D. Consequently we can write the following equation (1):

$$\scriptsize P=O + tD$$

Where t is distance from the ray origin O to P. To find P we need to find t (figure xx). What else do we know? We know the plane's normal which we have already computed and the plane equation (2) which is (check the chapter on the ray-plane intersection in lesson 7 for more information on this topic):

$$\scriptsize Ax + By + Cz + D = 0$$

Where A, B, C can be seen as the components (or coordinates) of the normal to the plane (\(\small N_{plane}=(A, B, C)\normalsize\)) and D is the distance from the origin (0, 0, 0) to the plane (if we trace a line from the origin to the plane, parallel to the plane's normal). The variables x, y and z are the coordinates of any point that lies on this plane.

We know the plane normal and we know than any of the three triangle's vertices (v0, v1, v2) lie in the plane. It is therefore possible to compute D. Any of the three vertices can be chosen but we use the first one v0 by convention:

We also know that the point P which is the intersection point of the ray and the plane lies in the plane. Consequently we can substitute P (from equation 1) to (x, y, z) in equation 2 and solve for t (equation 3):

$$\scriptsize\begin{array}{l} P = O + tD\\ Ax + By + Cz + D = 0\\ A * P_x + B * P_y + C * P_z + D = 0\\ A * (O_x + tD_x) + B * (O_y + tD_y) + C * (O_z + tD_z) + D = 0\\ A * O_x + B * O_y + C * O_z + A * tD_x + B * tD_y + C * tD_z + D = 0\\ t * (A * D_x + B * D_y + C * D_z) + A * O_x + B * O_y + C * O_z + D = 0\\ t = -{{A * O_x + B * O_y + C * O_z + D} \over {A * D_x + B * D_y + C * D_z}}\\ t = -{{ N(A,B,C) \cdot O + D} \over {N(A,B,C) \cdot D}} \end{array}\normalsize$$

We have computed t which we can use to compute the position of P:

There are two very important cases though we need to look at before we check if the point is inside the triangle.

## The Ray And The Triangle Are Parallel

If the ray and the plane are parallel they wont intersect (figure 2). For robustness we need to handle that case if it happens. This is very simple. if the triangle is parallel to the ray direction, it means that the triangle's normal and the ray's direction should be perpendicular.

We have learned that the dot product of two perpendicular vectors is 0. If you look at the denominator of equation 3 (the term below the line) we do already compute the dot product between the triangle's normal N and the ray direction D. In fact our code is not very robust because this term can potentially be 0 and we should always catch a possible division by 0. In turns out that when this term is equal to 0 the ray is parallel to the triangle. Since they don't intersect in that case there's therefore no need to compute t anyway. Conclusion: before we compute t in our code, we will compute the result of the term \(\small N \cdot D \normalsize \) first and if the result is 0, the function will return the value false (no intersection).

## The Triangle Is "Behind" The Ray

So far we have assumed that the triangle was always in front of the ray. But what happens if the triangle is behind the ray with the ray still looking in the same direction? Normally the triangle shouldn't be visible. Even when the triangle is "behind" the ray, we do get a valid value with equation 3. In that case, t is negative which causes the intersection point to be in the opposition direction than the ray's direction. If we don't catch this "error" though, the triangle will show up in the final frame which is wrong (geometry behind the camera shouldn't be visible). The conclusion of all this is that we need to check for the sign of t before we can decide if the intersection point P is valid or not. If t is lower than 0, the triangle is behind the ray's origin (in regards to the ray's direction) and is not visible. There's no intersection and we can return from the function. If t is greater than 0, the triangle is visible for that ray and we can proceed to the next step.

## Step 2: Is P Inside Or Outside The Triangle?

Now that we have found the point P which is the point where the ray and the plane intersection we still have to find out if P is inside the triangle (in which case the ray intersects the triangle) or if P is outside (in which case the rays misses the triangle). Figure 2 illustrates these two cases.

The solution to this problem is simple and is called the "inside-outside" test. A simple demonstration will help to understand how this test works. Imagine that you have a vector A which is aligned with the x-axis (figure 4). Lets imagine that this vector is actually aligned with one edge of our triangle (the edge defined by the two vertices v0v1). Now, the second edge B, is defined by the vertices v0 and v2 of the triangles as showed in figure 4. Lets compute the cross product of these two vectors. As expected the result is a vector which is pointing in the same direction as the z-axis and the normal of the triangle.

$$\scriptsize\begin{array}{l} A=(1, 0, 0)\\ B=(1, 1, 0)\\ C_x = A_y B_z - A_z B_y = 0\\ C_y = A_z B_x - A_x B_z = 0\\ C_z = A_x B_y - A_y B_x = 1 * 1 - 0 * 1 = 1\\ C = (0, 0, 1) \end{array}$$

Now lets imagine that rather than having the coordinates (1, 1, 0) the vertex v2 has the coordinates (1, -1, 0). In other words we have mirrored its position about the x-axis. If we now compute the cross product AxB' we get the result C'=(0, 0, -1).

$$\scriptsize\begin{array}{l} A=(1, 0, 0)\\ B=(1, -1, 0)\\ C_x = A_y B_z - A_z B_y = 0\\ C_y = A_z B_x - A_x B_z = 0\\ C_z = A_x B_y - A_y B_x = 1 * -1 - 0 * 1 = -1\\ C = (0, 0, -1) \end{array}$$

Because the C and N are pointing in the same direction their dot product returns a value greater than 0 (positive). However because C' and N are pointing in opposite directions, their dot product returns a value lower than 0 (negative). What does that test tell us? We know that the point where the ray intersects the triangle and the triangle are in the same plane. We also know from the test we have just made that if a point P which is in the triangle's plane (such as the vertex v2 or the intersection point) is on the left side of vector A, then the dot product between the triangle's normal and vector C is positive (C is the result of the cross product between A and B. In this scenario, A = (v1 - v0) and B = (P - v0)). However if P is on the right side of A (as with v2') then this dot product is negative. And as you can see in figure 5, the point P is inside the triangle when it is located on the left side of A. To apply the technique we have just described to the ray-triangle intersection problem, we need to repeat the left/right test for each edge of the triangle. If for each one of triangle's edges we find that point P is on the left side of vector C (where C is defined as v1-v0, v2-v1 and v0-v2 respectively for each edge of the triangle), then we know for sure that P is inside the triangle. If the test fails for any of the triangle edges, then P lies outside the triangle's boundaries. This process is illustrated in figure 6.

In pseudocode we can write:

Lets write a complete ray-triangle intersection test in pseudo code. First we will compute the triangle's normal, then test if the ray and the triangle are parallel. If they are the intersection test fails. If they are not, we compute \(t\) from which we can compute the intersection point P. If the inside-out test succeeds (we test if P is on the left side of each one of the triangle's edges) then the ray intersects the triangle and P is inside the triangle's boundaries. The test is successful.

The "inside-outside" technique we have just described works for any convex polygon. Repeat the technique we have used for triangles for each edge of the polygon. Compute the cross product of the vector defined by the two edge's vertices and the vector defined by the first edge's vertex and the point. Compute the dot product of the resulting vector and the polygon's normal. The sign of the resulting dot product determines if the point is on the right or left side of that edge. Iterate through each edge of the polygon. There's no need testing the other edges if one fails passing the test.

Note that this technique can be made faster if the triangle's normal as well as the value D from the plane equation are pre-computed and stored in memory for each triangle of the scene.

## Source Code

In the following code we implemented the method described in this chapter to compute the intersection of a ray with a triangle. The constructor of the Triangle class set the position of a default triangle which we can use for quick testings. Change the values of these vertices if you wich to experiment with different triangle shapes. You could also play with the object-to-world matrix to move, rotate or scale this default triangle (transform the triangle's input vertices). The intersect method (line 29) now takes an IsectData as one of the method's parameters (instead a t). This structure (lines 1-5) can be used to store useful information about the intersection (only t for now but we will extend it as we progress in this lesson).

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

The download section of this lesson contain the complete souce code which you need to compile with the files from lesson 8 (use instructions for lesson 8 to compile the code).

## What's Next?

In this chapter we have presented a technique to compute the ray-triangle intersection test using simple geometry. However there is much more to the ray-triangle intersection test which we haven't considered yet such as whether the ray hits the triangle from the front or from the back. We can also compute what we call the barycentric coordinates of the hit point, which are the two-dimensional coordinates of P in regards to the first vertex of the triangle. These coordinates are necessary for doing things such as applying a texture to the triangle. The next chapter will look into these two topics.

« Previous Chapter Chapter 3 of 6 Next Chapter »