# How to check if a line intersects with (triangular) cell?

**URL:** https://discourse.vtk.org/t/how-to-check-if-a-line-intersects-with-triangular-cell/2672
**Category:** Support
**Created:** [February 21, 2020, 6:07pm UTC](https://discourse.vtk.org/t/how-to-check-if-a-line-intersects-with-triangular-cell/2672 "2020-02-21T18:07:36Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![normanius](https://discourse.vtk.org/user_avatar/discourse.vtk.org/normanius/32/611_2.png) [@normanius](https://discourse.vtk.org/u/normanius)
#### Post date: [February 21, 2020, 6:07pm UTC](https://discourse.vtk.org/t/how-to-check-if-a-line-intersects-with-triangular-cell/2672/1 "2020-02-21T18:07:36Z")

</div>

Dear all

I would like to test if a line intersects with a triangle.

If the line is not lying in the same plane as triangle this is done easily with `vtkTriangle.IntersectWithLine()`. But how can I detect a collision if the line is in the same plane? It looks like `vtkTriangle.IntersectWithLine()` does not work in this case.

Here a demonstration:

```python
import vtk

# Construct a triangle for this demo.
points = vtk.vtkPoints()
points.InsertNextPoint([0,0,0])
points.InsertNextPoint([1,0,0])
points.InsertNextPoint([0,1,0])
triangle = vtk.vtkTriangle()
triangle.Initialize(3, [0,1,2], points)
# Alternatively, for a poly-data source:
# cellId = 0
# triangle = source.GetCell(cellId)

# Perform intersection.
tol = 1e-7
x = [0,0,0] # Intersection.
xp = [0,0,0] # Parametric coordinates of intersection.
t = vtk.mutable(0) # Line position, 0 ≤ t ≤ 1.
subId = vtk.mutable(0) # subId? No idea what it is, usually not needed.
p0, p1 = [0,0,1],[1,1,-3] # p0 and p1 define the line.

hasIntersection = triangle.IntersectWithLine(p0, p1, tol, t, x, xp, subId)
# This returns:
# hasIntersection: 1
# t = 0.25
# x = [0.25, 0.25, 0]

# But what if the line is in the same plane as the triangle?
p0, p1 = [0.2,0.2,0],[0.5,0.5,0] # Line entirely contained in triangle
p0, p1 = [1.0,1.0,0],[-1.0,-1.0,0] # Line intersects at [0.5,0.5,0] and [0,0,0]
hasIntersection = triangle.IntersectWithLine(p0, p1, tol, t, x, xp, subId)

```

For the case where `p0` and `p1` lie in the same plane as the triangle, `hasIntersection` is always 0. How can I easily deal with both situations, where the line is either in- or out-of-plane?

I also played with `vtkTriangle.EvaluatePosition()`, but was not able to find a succinct test.

---

<div class="post-metadata">

### Author: ![Paulo\_Carvalho](https://discourse.vtk.org/user_avatar/discourse.vtk.org/paulo_carvalho/32/370_2.png) [@Paulo\_Carvalho](https://discourse.vtk.org/u/Paulo_Carvalho)
#### Post date: [February 22, 2020, 9:59am UTC](https://discourse.vtk.org/t/how-to-check-if-a-line-intersects-with-triangular-cell/2672/2 "2020-02-22T09:59:11Z")

</div>

What two cases?

---

<div class="post-metadata">

### Author: ![normanius](https://discourse.vtk.org/user_avatar/discourse.vtk.org/normanius/32/611_2.png) [@normanius](https://discourse.vtk.org/u/normanius)
#### Post date: [February 22, 2020, 12:12pm UTC](https://discourse.vtk.org/t/how-to-check-if-a-line-intersects-with-triangular-cell/2672/3 "2020-02-22T12:12:10Z")

</div>

I updated the question, it should be clearer now. If `p0` and `p1` lie in the triangle-plane, `IntersectWithLine` doesn’t work. I wondered how to detect an intersection for that case.

---

<div class="post-metadata">

### Author: ![Paulo\_Carvalho](https://discourse.vtk.org/user_avatar/discourse.vtk.org/paulo_carvalho/32/370_2.png) [@Paulo\_Carvalho](https://discourse.vtk.org/u/Paulo_Carvalho)
#### Post date: [February 22, 2020, 9:41pm UTC](https://discourse.vtk.org/t/how-to-check-if-a-line-intersects-with-triangular-cell/2672/4 "2020-02-22T21:41:29Z")

</div>

Have you tried greater tolerances (e.g. 1e-3, 1e-1, etc.)?

---

<div class="post-metadata">

### Author: ![normanius](https://discourse.vtk.org/user_avatar/discourse.vtk.org/normanius/32/611_2.png) [@normanius](https://discourse.vtk.org/u/normanius)
#### Post date: [February 22, 2020, 11:51pm UTC](https://discourse.vtk.org/t/how-to-check-if-a-line-intersects-with-triangular-cell/2672/5 "2020-02-22T23:51:13Z")

</div>

Yes, to no avail.

---

<div class="post-metadata">

### Author: ![normanius](https://discourse.vtk.org/user_avatar/discourse.vtk.org/normanius/32/611_2.png) [@normanius](https://discourse.vtk.org/u/normanius)
#### Post date: [February 26, 2020, 9:17am UTC](https://discourse.vtk.org/t/how-to-check-if-a-line-intersects-with-triangular-cell/2672/6 "2020-02-26T09:17:47Z")

</div>

Hey folks, another try… Does anyone have an idea?

I could of course write my own intersection algorithm, no big deal. But this problem is so basic I’m sure this has been solved already in VTK.

---

<div class="post-metadata">

### Author: ![Paulo\_Carvalho](https://discourse.vtk.org/user_avatar/discourse.vtk.org/paulo_carvalho/32/370_2.png) [@Paulo\_Carvalho](https://discourse.vtk.org/u/Paulo_Carvalho)
#### Post date: [February 26, 2020, 10:39am UTC](https://discourse.vtk.org/t/how-to-check-if-a-line-intersects-with-triangular-cell/2672/7 "2020-02-26T10:39:52Z")

</div>

Well, you can do the following:

1. Determine whether the line lies in the triangle’s plane:  
a) Compute the cross product between two vectors that define the triangle. This yields a vector normal to the triangle;  
b) Compute the dot product of the normal vector against your the vectors that define the lines.  
c) If the dot product is zero, then the line belongs to the triangle’s plane. Proceed to 2);
2. Determine whether the line lies within, overlaps with an edge, touches, etc the triangle:  
a) Reduce the problem to 2D by ignoring either the x,y or z coordinate (you can write a test to determine which is the best to be ignored).  
b) Use this test: [https://gamedev.stackexchange.com/questions/21096/what-is-an-efficient-2d-line-segment-versus-triangle-intersection-test](https://gamedev.stackexchange.com/questions/21096/what-is-an-efficient-2d-line-segment-versus-triangle-intersection-test). It is a test written by game developers, so you can rest assured it works and it is fast.

Either this or you have to link against a specialized computational geometry library like CGAL if you want to avoid writing these tests yourself.
