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.

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?

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.

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.