0

I have an unstructured grid that I need to intersect with a line to find both the points and cells. The VTK documentation for vtkCellLocator has an overloaded version of IntersectWithLine that will do this, but it is not implemented in python? Any suggestions/workarounds would be appreciated.

import vtk
import numpy as np

# Create points
points = vtk.vtkPoints()
points.SetNumberOfPoints(8)
points.SetPoint(0,[-.5,-.5,-.5])
points.SetPoint(1,[ .5,-.5,-.5])
points.SetPoint(2,[ .5, .5,-.5])
points.SetPoint(3,[-.5, .5,-.5])
points.SetPoint(4,[-.5,-.5, .5])
points.SetPoint(5,[ .5,-.5, .5])
points.SetPoint(6,[ .5, .5, .5])
points.SetPoint(7,[-.5, .5, .5])

# Create Unstructured Grid and add Cells 
ugrid = vtk.vtkUnstructuredGrid()
if vtk.VTK_MAJOR_VERSION <= 9:
    ugrid.Allocate(2)
else:
    ugrid.AllocateExact(2,8)
ugrid.InsertNextCell(vtk.VTK_QUAD,4,[4,5,6,7])
ugrid.InsertNextCell(vtk.VTK_HEXAHEDRON,8,[0,1,2,3,4,5,6,7])
ugrid.SetPoints(points)

# Create a locator
locator = vtk.vtkCellLocator()
locator.SetDataSet(ugrid)
locator.BuildLocator()

# Line End Points
p1 = np.array([0., 0., -1.])
p2 = np.array([0., 0., 1.])

# Works
tol = 1.e-6
t = vtk.mutable(0)
x = np.array([0., 0., 0.])
pcoords = np.array([0., 0., 0.])
sub_id = vtk.mutable(0)
cell_id = vtk.mutable(0)
cell = vtk.vtkGenericCell()
locator.IntersectWithLine(p1, p2, tol, t, x, pcoords, sub_id, cell_id, cell)

# Doesn't Work
intersect_points = vtk.vtkPoints()
intersect_cells = vtk.vtkIdList()
locator.IntersectWithLine(p1, p2, tol, intersect_points, intersect_cells, cell)

Adam
  • 37
  • 5
  • Please share the error message. Also note the existence of [vtkExtractCellsAlongPolyLine](https://vtk.org/doc/nightly/html/classvtkExtractCellsAlongPolyLine.html#details) that may do the work for you – Nico Vuaille Sep 14 '22 at 07:19

0 Answers0