# Trouble with vtkCellLocator example online?

**URL:** https://discourse.vtk.org/t/trouble-with-vtkcelllocator-example-online/13489
**Category:** Support
**Tags:** python
**Created:** [March 8, 2024, 5:44pm UTC](https://discourse.vtk.org/t/trouble-with-vtkcelllocator-example-online/13489 "2024-03-08T17:44:57Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![rexthor](https://discourse.vtk.org/user_avatar/discourse.vtk.org/rexthor/32/6481_2.png) [@rexthor](https://discourse.vtk.org/u/rexthor)
#### Post date: [March 8, 2024, 5:44pm UTC](https://discourse.vtk.org/t/trouble-with-vtkcelllocator-example-online/13489/1 "2024-03-08T17:44:57Z")

</div>

I’m trying to use vtkCellLocator in a fashion similar to that shown [here](https://examples.vtk.org/site/Python/DataManipulation/LineOnMesh/) 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:

```plaintext
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:

```python
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}")

```

---

<div class="post-metadata">

### Author: ![rexthor](https://discourse.vtk.org/user_avatar/discourse.vtk.org/rexthor/32/6481_2.png) [@rexthor](https://discourse.vtk.org/u/rexthor)
#### Post date: [March 8, 2024, 7:06pm UTC](https://discourse.vtk.org/t/trouble-with-vtkcelllocator-example-online/13489/2 "2024-03-08T19:06:57Z")

</div>

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`.

```python
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}")

```
