VTK segfault - intersectWithLine() on Ubuntu

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?

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.

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.

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.

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:

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

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…

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.

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')

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.

No worries glad it worked out…