Trouble with vtkCellLocator example online?

I’m trying to use vtkCellLocator in a fashion similar to that shown here in the online examples. The specific call signature that I’d like to use is this:

locator.IntersectWithLine(p1, p2, tolerance, t, pos, pcoords, subId)

EDIT: I’m using Ubuntu’s VTK 9 package (vtk.__version__ = '9.1.0')

From what I can tell:

  • p1 and p2 are lists.
  • tolerance and t are floats
  • pos and pcoords are lists
  • subid is an integer

I’m making the same call in my example code, but I get this error:

Traceback (most recent call last):
  File "/home/rexthor/Programming/Python/Ray_Mesh_Intersection/ray_mesh.py", line 51, in <module>
    retval = loc.IntersectWithLine(start, end, tolerance, t, x, pcoords, subid)
TypeError: IntersectWithLine argument 5: expected a sequence of 3 values, got float

I expect that argument 5 is really argument 4 in my listing, since the first argument is likely self?

Here is my full example:

from random import seed, uniform
from vtkmodules.vtkCommonCore import vtkPoints
from vtkmodules.vtkFiltersGeneral import vtkOBBTree, vtkCellTreeLocator
from vtkmodules.vtkFiltersFlowPaths import vtkModifiedBSPTree
from vtkmodules.vtkCommonDataModel import vtkCellLocator, vtkStaticCellLocator
from vtkmodules.vtkIOGeometry import vtkOBJReader

if __name__ == "__main__":
    seed(0)

    r = vtkOBJReader()
    r.SetFileName("monkey.obj")
    r.Update()
    data = r.GetOutput()

    # loc = vtkOBBTree()
    loc = vtkCellLocator()
    loc.SetDataSet(data)
    loc.AutomaticOn()
    loc.BuildLocator()

    tolerance = 1e-6
    t = 0
    x = [0, 0, 0]
    pcoords = [0, 0, 0]
    subid = 0

    x = uniform(-1, 1)
    z = uniform(-1, 1)
    start = [x, 1, z]
    end = [x, -1, z]

    # WORKS ...
    # pts = vtkPoints()
    # retval = loc.IntersectWithLine(start, end, pts, None)
    # print(f"retval:{retval}, pts:{pts}")

    # DOESN'T WORK ...
    retval = loc.IntersectWithLine(start, end, tolerance, t, x, pcoords, subid)
    print(f"retval:{retval}, t:{t}, pcoords:{pcoords}, subid:{subid}")

I’m my own worst enemy. I re-used x both for the output position and for the start/end tuple generation. One was a float, one was a tuple - the second usage of the name overwrote the first - thus the error.

The answer involved vtk.reference which I couldn’t figure out how to import from vtkmodules
EDIT: it would be from vtkmodules.vtkCommonCore import reference.

from random import seed, uniform
from vtkmodules.vtkCommonCore import vtkPoints, reference
from vtkmodules.vtkFiltersGeneral import vtkOBBTree, vtkCellTreeLocator
from vtkmodules.vtkFiltersFlowPaths import vtkModifiedBSPTree
from vtkmodules.vtkCommonDataModel import vtkCellLocator, vtkStaticCellLocator
from vtkmodules.vtkIOGeometry import vtkOBJReader

if __name__ == "__main__":
    seed(0)

    r = vtkOBJReader()
    r.SetFileName("monkey.obj")
    r.Update()
    data = r.GetOutput()

    # loc = vtkOBBTree()
    loc = vtkCellLocator()
    loc.SetDataSet(data)
    loc.AutomaticOn()
    loc.BuildLocator()

    tolerance = reference(1e-6)
    t = reference(0)
    x = [0, 0, 0]
    pcoords = [0, 0, 0]
    subid = reference(0)

    x_coord = uniform(-1, 1)
    z_coord = uniform(-1, 1)
    start = [x_coord, 1, z_coord]
    end = [x_coord, -1, z_coord]

    retval = loc.IntersectWithLine(start, end, tolerance, t, x, pcoords, subid)
    print(f"retval:{retval}, t:{t}, pcoords:{pcoords}, subid:{subid}")