# How to calculate the cross point between line and mask

**URL:** https://discourse.vtk.org/t/how-to-calculate-the-cross-point-between-line-and-mask/5159
**Category:** Support
**Created:** [February 9, 2021, 7:34am UTC](https://discourse.vtk.org/t/how-to-calculate-the-cross-point-between-line-and-mask/5159 "2021-02-09T07:34:36Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![zhang-qiang-github](https://discourse.vtk.org/user_avatar/discourse.vtk.org/zhang-qiang-github/32/2519_2.png) [@zhang-qiang-github](https://discourse.vtk.org/u/zhang-qiang-github)
#### Post date: [February 9, 2021, 7:34am UTC](https://discourse.vtk.org/t/how-to-calculate-the-cross-point-between-line-and-mask/5159/1 "2021-02-09T07:34:37Z")

</div>

For a line, it will intersect with mask for several points, then I want to calculate the intersection point.

For example:

mask = np.zeros([300, 300, 50])  
mask[:, :, 10:30] = 1

Then, the line is:

p1 = [100, 100, -1]  
p2 = [100, 100, 1]

The intersection point should be:  
[100, 100, 0] and [100, 100, 49].

How to implement this function with vtk?

---

<div class="post-metadata">

### Author: ![will.schroeder](https://discourse.vtk.org/user_avatar/discourse.vtk.org/will.schroeder/32/233_2.png) [@will.schroeder](https://discourse.vtk.org/u/will.schroeder)
#### Post date: [February 9, 2021, 11:53am UTC](https://discourse.vtk.org/t/how-to-calculate-the-cross-point-between-line-and-mask/5159/2 "2021-02-09T11:53:05Z")

</div>

Look at the methods in vtkLine. For example, vtkLine::DistanceToLine()

---

<div class="post-metadata">

### Author: ![dgobbi](https://discourse.vtk.org/user_avatar/discourse.vtk.org/dgobbi/32/18_2.png) [@dgobbi](https://discourse.vtk.org/u/dgobbi)
#### Post date: [February 9, 2021, 5:13pm UTC](https://discourse.vtk.org/t/how-to-calculate-the-cross-point-between-line-and-mask/5159/3 "2021-02-09T17:13:59Z")

</div>

Hi Zhang, did you have a typo in your question?

It seems like

```auto
mask[:, :, 10:30] = 1

```

should have intersection points [100, 100, 10] and [100, 100, 29], unless I am mistaken?

Also, note that all the `IntersectWithLine()` methods in VTK assume that the line is a finite line segment, and intersections never occur beyond the line’s endpoints.

---

<div class="post-metadata">

### Author: ![zhang-qiang-github](https://discourse.vtk.org/user_avatar/discourse.vtk.org/zhang-qiang-github/32/2519_2.png) [@zhang-qiang-github](https://discourse.vtk.org/u/zhang-qiang-github)
#### Post date: [February 10, 2021, 7:40am UTC](https://discourse.vtk.org/t/how-to-calculate-the-cross-point-between-line-and-mask/5159/4 "2021-02-10T07:40:48Z")

</div>

Sorry for the mistake in my question, and the intersection points are [100, 100, 10] and [100, 100, 29].

`IntersectWithLIne()` was used to intersect a line with a mesh, but I want to intersect a line with a mask (`vtkImageData`). How can I implement vtk code for this purpose?

---

<div class="post-metadata">

### Author: ![zhang-qiang-github](https://discourse.vtk.org/user_avatar/discourse.vtk.org/zhang-qiang-github/32/2519_2.png) [@zhang-qiang-github](https://discourse.vtk.org/u/zhang-qiang-github)
#### Post date: [February 10, 2021, 7:42am UTC](https://discourse.vtk.org/t/how-to-calculate-the-cross-point-between-line-and-mask/5159/5 "2021-02-10T07:42:40Z")

</div>

`vtkLine::DistanceToLine()` is used to calculate the distance from a point to a line. But what I need is to intersect a line with a mask (`vtkImageData`) and calculate the intersection point.

---

<div class="post-metadata">

### Author: ![dgobbi](https://discourse.vtk.org/user_avatar/discourse.vtk.org/dgobbi/32/18_2.png) [@dgobbi](https://discourse.vtk.org/u/dgobbi)
#### Post date: [February 10, 2021, 12:58pm UTC](https://discourse.vtk.org/t/how-to-calculate-the-cross-point-between-line-and-mask/5159/6 "2021-02-10T12:58:21Z")

</div>

You are correct that `vtkImageData` does not have any methods for line intersection. But I think you can do it efficiently with VTK’s clipping methods.

Here is the idea. First, use `vtkLineSource` to create the line segment as a polydata, it has methods `SetPoint1(p1)` and `SetPoint2(p2)`.

Then, use `vtkClipPolyData` to clip the line segment by removing all parts of the line where the image is zero. Since `vtkClipPolyData` has a `SetClipFunction()` method that requires a `vtkImplicitFunction`, you will have to convert the `vtkImageData` to a `vtkImplicitFunction` with the `vtkImplicitVolume` class.

Essentially, `vtkImplicitVolume` is the class that allows you to use an image with VTK’s clipping and cutting filters.

The output of `vtkClipPolyData` will be a `vtkPolyData`. You can call the `vtkPolyData::GetLines()` method to get the clipped line segments.

One thing you have to be careful about is that `vtkImplicitVolume` generates a continuous implicit function from the image via linear interpolation, so (for example) [100,100,9] will be zero, [100,100,9.5] will be 0.5, and [100,100,10] will be 1. Make sure you understand this! So because `vtkClipPolyData` will clip at zero, it will compute the intersection point to be [100,100,9] instead of [100,100,10]. The way to fix this is to use `vtkClipPolyData::SetValue(0.99999)` to clip at a value just below 1.
