<p>The way of detecting intersection is two steps. (I referred to  <a href="http://www-bcf.usc.edu/~jbarbic/cs420-s18/runuscedu/cs420-s18/16-geometric-queries/16-geometric-queries-6up.pdf" rel="nofollow noreferrer">http://www-bcf.usc.edu/~jbarbic/cs420-s18/runuscedu/cs420-s18/16-geometric-queries/16-geometric-queries-6up.pdf</a>)</p>

<ol>
<li>Check intersection between the plane that includes the given triangle and given ray.</li>
</ol>

<p>Let's suppose the start point of the ray $R$ is $(x_0, y_0, z_0)$ and direction is $(x_d, y_d, z_d)$,  then $R = (x_0, y_0, z_0) + (x_d, y_d, z_d)t$ , and the plane is $ax + by + cz + d = 0$. </p>

<p>To check intersection between the ray and plane, calculate the below equation.</p>

<p>$t = \frac{-(ax_0 + by_0 + cz_0 + d)}{ax_d + by_d + cz+d}$</p>

<p>If $t \ge 0$, then there is a possibility of intersection, so go second step.</p>

<p>else there is not intersection.</p>

<ol start="2">
<li>Test the point inside triangle. </li>
</ol>

<p>Since $t \ge 0 $, we know the point on the plane.</p>

<p><a href="https://i.sstatic.net/xikMu.jpg" rel="nofollow noreferrer"><img src="https://i.sstatic.net/xikMu.jpg" alt="enter image description here"></a></p>

<p>Hnece, we can get the $\alpha, \beta, \gamma$.</p>

<p>If $0 \le\alpha, \beta, \gamma \le 1$ and $\alpha+ \beta + \gamma = 1$, then there is intersecton.</p>

<p>else No intersection!.</p>

<p>(The page 19, 20 is explaining how to calculate the 'Area'. (Actually, we can project the 3D space points to 2D space  for performance, but avoid the perpendicular projection!)  )</p>

CC BY-SA 3.0
2018-04-24T03:52:28.990Z
2018-04-24T03:52:28.990Z
shashack
<p>I've been thinking about this question for a quite long time.And my implementation seems to be correct for some cases but wrong with few others.</p>

<p>How can I comprehensive test the algorithm? Is there any provided test cases?</p>

CC BY-SA 3.0
2018-04-22T23:06:36.783Z
Do you want to know triangle intersection with a line?
shashack
CC BY-SA 3.0
2018-04-24T02:44:42.180Z
@shashack Yes, specifically with a ray.
jinglei
<raytracing><algorithm>
How to test my triangle intersection implementation?
jinglei
# How to test my triangle intersection implementation?

(Asked by: username_0 on 2018-04-22)

I've been thinking about this question for a quite long time.And my implementation seems to be correct for some cases but wrong with few others.

How can I comprehensive test the algorithm? Is there any provided test cases?

**Tags:** raytracing, algorithm

> **Comments:**
> **username_1**: Do you want to know triangle intersection with a line?  
> **username_0**: @username_1 Yes, specifically with a ray.  

---

## 1 answers

### Answer by username_1 on 2018-04-24. Score: 0

The way of detecting intersection is two steps. (I referred to <http://www-bcf.usc.edu/~jbarbic/cs420-s18/runuscedu/cs420-s18/16-geometric-queries/16-geometric-queries-6up.pdf>)

1. Check intersection between the plane that includes the given triangle and given ray.

Let's suppose the start point of the ray $R$ is $(x\_0, y\_0, z\_0)$ and direction is $(x\_d, y\_d, z\_d)$, then $R = (x\_0, y\_0, z\_0) + (x\_d, y\_d, z\_d)t$ , and the plane is $ax + by + cz + d = 0$.

To check intersection between the ray and plane, calculate the below equation.

$t = \frac{-(ax\_0 + by\_0 + cz\_0 + d)}{ax\_d + by\_d + cz+d}$

If $t \ge 0$, then there is a possibility of intersection, so go second step.

else there is not intersection.

2. Test the point inside triangle.

Since $t \ge 0 $, we know the point on the plane.

[![enter image description here](https://i.sstatic.net/xikMu.jpg)](https://i.sstatic.net/xikMu.jpg)

Hnece, we can get the $\alpha, \beta, \gamma$.

If $0 \le\alpha, \beta, \gamma \le 1$ and $\alpha+ \beta + \gamma = 1$, then there is intersecton.

else No intersection!.

(The page 19, 20 is explaining how to calculate the 'Area'. (Actually, we can project the 3D space points to 2D space for performance, but avoid the perpendicular projection!) )


---

