Get intersection of PolyData line and a plane

I have a PolyLine and I’d like to get the data from that line that intersects a plane. How would I do that in Python.

I’ve tried using vtkClipPolyData and vtkCutter to no avail; the output is empty.

I’m also working in Python for this and the PolyData is wrapped in pyvista’s PolyData object type.

vtkCutter should work. Could you provide a full example that reproduces the issue that uses only VTK?

Sorry for the long delay, here’s approximately the code I was using:

vplane = vtk.vtkPlane()
vplane.SetOrigin((-0.4, 0, 0))
vplane.SetNormal((1,0,0))

cutter = vtk.vtkCutter()
cutter.SetCutFunction(vplane)
cutter.SetInputData(walledgePolySample)
cutter.Update()
cutterout = cutter.GetOutputDataObject(0) 

type(cutterout) # vtkCommonDataModelPython.vtkPolyData
cutterout.GetNumberOfVerts() # 0

Note that the walledgePolySample is a line planar in XY and goes from x={-0.54, -0.31}, y={0,0}.

It works well for me:

line = vtk.vtkLineSource()
line.SetPoint1(-0.54, 0, 0)
line.SetPoint2(-0.31, 0, 0)
line.Update()

vplane = vtk.vtkPlane()
vplane.SetOrigin(-0.4, 0, 0)
vplane.SetNormal(1,0,0)

cutter = vtk.vtkCutter()
cutter.SetCutFunction(vplane)
cutter.SetInputData(line.GetOutput())
cutter.Update()
cutterout = cutter.GetOutput()

print(cutterout.GetNumberOfPoints())
print(cutterout.GetPoint(0))

Provides this output:

1
(-0.4000000059604645, 0.0, 0.0)
1 Like

Yep, that worked! Thanks!

If anyone is using pyvista, the only change is to do:

- cutter.SetInputData(line.GetOutput())
+ cutter.SetInputData(line)

where line is a pyvista PolyData object.