# VTK segfault - intersectWithLine() on Ubuntu

**URL:** <https://discourse.vtk.org/t/vtk-segfault-intersectwithline-on-ubuntu/5463>\
**Category:** Development\
**Created:** [March 30, 2021, 12:22pm UTC](https://discourse.vtk.org/t/vtk-segfault-intersectwithline-on-ubuntu/5463 "2021-03-30T12:22:20Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![nantille](https://discourse.vtk.org/user_avatar/discourse.vtk.org/nantille/32/2840_2.png) [@nantille](https://discourse.vtk.org/u/nantille)\
**Post date:** [March 30, 2021, 12:22pm UTC](https://discourse.vtk.org/t/vtk-segfault-intersectwithline-on-ubuntu/5463/1 "2021-03-30T12:22:20Z")

</div>

When testing a visualiser I developed for neuroscientific data, I narrowed down the reason for abrupt segfault when using VTK.

I’ve got a surface mesh and I use the method intersectWithLine to find intersections with a given ray/vector. It works on MacOS and Windows but segfaults on Ubuntu 20 LTS.

Has anybody seen this before?

---

<div class="post-metadata">

**Author:** ![will.schroeder](https://discourse.vtk.org/user_avatar/discourse.vtk.org/will.schroeder/32/233_2.png) [@will.schroeder](https://discourse.vtk.org/u/will.schroeder)\
**Post date:** [March 30, 2021, 12:33pm UTC](https://discourse.vtk.org/t/vtk-segfault-intersectwithline-on-ubuntu/5463/2 "2021-03-30T12:33:06Z")

</div>

I have not seen this. My hunch is that it’s not this function that’s at fault, I suspect memory is being corrupted / not initialized elsewhere.

---

<div class="post-metadata">

**Author:** ![nantille](https://discourse.vtk.org/user_avatar/discourse.vtk.org/nantille/32/2840_2.png) [@nantille](https://discourse.vtk.org/u/nantille)\
**Post date:** [March 30, 2021, 12:50pm UTC](https://discourse.vtk.org/t/vtk-segfault-intersectwithline-on-ubuntu/5463/3 "2021-03-30T12:50:05Z")

</div>

Thanks for your reply. In my case I used Python so I don’t have control over memory management.  
In the meantime, my only solution is to use another module like trimesh to run the intersection algorithm. Alternatively, I could maybe use OSPRay’s methods but I don’t see any plan for releasing VTK 9.1 with OSPRay.

---

<div class="post-metadata">

**Author:** ![will.schroeder](https://discourse.vtk.org/user_avatar/discourse.vtk.org/will.schroeder/32/233_2.png) [@will.schroeder](https://discourse.vtk.org/u/will.schroeder)\
**Post date:** [March 30, 2021, 1:02pm UTC](https://discourse.vtk.org/t/vtk-segfault-intersectwithline-on-ubuntu/5463/4 "2021-03-30T13:02:45Z")

</div>

Yes that’s true, you don’t have direct control over memory management. But indirectly you can corrupt data by e.g., saying adding more points to a vtkPoints than it is allocated for; e.g., using negative ids (or ids outside of range) in connectivity arrays etc. you get the idea.

---

<div class="post-metadata">

**Author:** ![nantille](https://discourse.vtk.org/user_avatar/discourse.vtk.org/nantille/32/2840_2.png) [@nantille](https://discourse.vtk.org/u/nantille)\
**Post date:** [March 30, 2021, 1:36pm UTC](https://discourse.vtk.org/t/vtk-segfault-intersectwithline-on-ubuntu/5463/5 "2021-03-30T13:36:25Z")

</div>

I get your point. However in my case I’m merely loading a .ply file into an actor which I keep in memory to run:

```auto
line_locator = vtk.vtkOBBTree()
# polydata is the loaded surface mesh
line_locator.SetDataSet(polydata)
line_locator.BuildLocator()

intersections = vtk.vtkPoints()
line_locator.IntersectWithLine(source, destination, intersections, None) 
# Segfault here on Ubuntu

```

---

<div class="post-metadata">

**Author:** ![will.schroeder](https://discourse.vtk.org/user_avatar/discourse.vtk.org/will.schroeder/32/233_2.png) [@will.schroeder](https://discourse.vtk.org/u/will.schroeder)\
**Post date:** [March 30, 2021, 8:47pm UTC](https://discourse.vtk.org/t/vtk-segfault-intersectwithline-on-ubuntu/5463/6 "2021-03-30T20:47:54Z")

</div>

Can you share data and python script with me? I’m running Ubuntu and could try it out without a lot of effort… It might be easiest to create an issue in gitlab and post the data / script there…

---

<div class="post-metadata">

**Author:** ![nantille](https://discourse.vtk.org/user_avatar/discourse.vtk.org/nantille/32/2840_2.png) [@nantille](https://discourse.vtk.org/u/nantille)\
**Post date:** [April 1, 2021, 8:26am UTC](https://discourse.vtk.org/t/vtk-segfault-intersectwithline-on-ubuntu/5463/7 "2021-04-01T08:26:48Z")

</div>

Actually the code is distributed in my app and I use vedo, a wrapper python lib that simplifies interactions with VTK. For more advanced stuff I directly use VTK python. I’ll turn that example into a test and integrate that in my lib.

On similar hardware (8-core, 32GB RAM), this yields a segfault only on Ubuntu. You might want to `pip install vedo`.

```auto
import vtk
import vedo
import os

# Download the mous brain boundary mesh prior to testing this
# https://github.com/int-brain-lab/iblviewer/blob/main/data/surfaces/997.ply
# This mesh has around 150K polygons

test_rays = [[[-13496.7, 8629.8, -14009.0], [27944.8, -1090.3, 17709.4]],
            [[-2435.4, -9487.5, -15286.4], [18739.5, 15562.2, 26610.1]],
            [[-16653.4, -3609.6, -5646.1], [30438.2, 11050.2, 13714.2]]]

print('Loading mesh')

# Change this path to wherever you put the mesh file
file_path = './data/surfaces/997.ply'
plot = vedo.Plotter()
if not os.path.exists(file_path):
    print('Mesh file path is invalid:', file_path)
    exit()
actor = vedo.load(file_path)

print('Now intersecting', len(test_rays), 'rays with it...')
line_locator = vtk.vtkOBBTree()
# polydata is the loaded surface mesh
line_locator.SetDataSet(actor.polydata())
line_locator.BuildLocator()

for ray_id in range(len(test_rays)):
    source = test_rays[ray_id][0]
    destination = test_rays[ray_id][1]
    intersections = vtk.vtkPoints()
    line_locator.IntersectWithLine(source, destination, intersections, None)
    # Segfault here on Ubuntu
    print('Ray', ray_id, 'yields', intersections.GetNumberOfPoints(), 'hits')
    # Should print for each ray: "...yields 2 hits"
    
print('Done')

```

---

<div class="post-metadata">

**Author:** ![nantille](https://discourse.vtk.org/user_avatar/discourse.vtk.org/nantille/32/2840_2.png) [@nantille](https://discourse.vtk.org/u/nantille)\
**Post date:** [April 1, 2021, 1:17pm UTC](https://discourse.vtk.org/t/vtk-segfault-intersectwithline-on-ubuntu/5463/8 "2021-04-01T13:17:03Z")

</div>

Well…it seems that it’s not a bug on VTK side but something much nastier. For some reason this very that I put on github and that I downloaded for testing on Ubuntu got invalid or corrupt at download time. It was the same size as the original file but for instance I could not load it into Blender…

My above code sample works on Ubuntu.  
Thank you for your time @will.schroeder and sorry for this.

---

<div class="post-metadata">

**Author:** ![will.schroeder](https://discourse.vtk.org/user_avatar/discourse.vtk.org/will.schroeder/32/233_2.png) [@will.schroeder](https://discourse.vtk.org/u/will.schroeder)\
**Post date:** [April 1, 2021, 1:17pm UTC](https://discourse.vtk.org/t/vtk-segfault-intersectwithline-on-ubuntu/5463/9 "2021-04-01T13:17:34Z")

</div>

No worries glad it worked out…
