So I’m trying to use the Append() function in vtkSignedDistance / vtkUnsignedDistance which allows for polydata to be incrementally added which is supposedly supported from the documentation, when initially calling StartAppend() the code immediately segfaults and displays:
Segmentation fault: 11
I initially thought this was due to the point locator used being static and unable to handle the insertion of new data so I tried a variety of locators which should work but the issue persists.
I’ve produced a minimum working example which describes the issue, the code runs through the singular polydata example fine but breaks on the example with multiple polydata.
Are there some additional steps I’m missing here?
import vtk
import numpy as np
def generate_random_polydata(total_points=1000):
points = np.random.rand(total_points,3)
vtk_points = vtk.vtkPoints()
for point in points:
vtk_points.InsertNextPoint(point)
vtk_polydata = vtk.vtkPolyData()
vtk_polydata.SetPoints(vtk_points)
vtk_polydata.Modified()
return vtk_polydata
def generate_distance_field(bounds=[-1.5,1.5,-1.5,1.5,-1.5,1.5], voxel_spacing=[0.1,0.1,0.1], total_polydata=4):
signed_distance = vtk.vtkSignedDistance()
dimensions = [
int((bounds[1] - bounds[0]) / voxel_spacing[0]),
int((bounds[3] - bounds[2]) / voxel_spacing[1]),
int((bounds[5] - bounds[4]) / voxel_spacing[2])
]
array_size = [
(bounds[1] - bounds[0]),
(bounds[3] - bounds[2]),
(bounds[5] - bounds[4])
]
signed_distance = vtk.vtkUnsignedDistance() # signed distance functions require normals
# signed_distance = vtk.vtkSignedDistance()
signed_distance.SetBounds(bounds)
signed_distance.SetDimensions(dimensions)
signed_distance.SetRadius(np.linalg.norm(array_size))
# These parameters do not change the error
# signed_distance.AdjustBoundsOff()
# signed_distance.CappingOff()
# Changing the locator to an incremental one does not change the error
# locator = signed_distance.GetLocator()
# new_locator = vtk.vtkNonMergingPointLocator()
# new_locator = vtk.vtkPointLocator()
# new_locator = vtk.vtkIncrementalOctreePointLocator()
# new_locator = vtk.vtkNonMergingPointLocator()
# new_locator = vtk.vtkMergePoints()
# signed_distance.SetLocator(new_locator)
signed_distance.Modified()
locator = signed_distance.GetLocator()
# print(locator)
if total_polydata > 1:
print('Case with multiple polydata')
signed_distance.StartAppend() #required to call before Append(), but just segfaults (11)
print('Code breaks in the line above here')
for _ in range(total_polydata):
test_polydata = generate_random_polydata()
signed_distance.Append(test_polydata)
signed_distance.EndAppend()
else:
print('Case with single polydata')
polydata = generate_random_polydata()
signed_distance.SetInputData(polydata)
signed_distance.Update()
distance_field = signed_distance.GetOutput()
print('Distances successfully generated\n')
# locator = signed_distance.GetLocator()
# print(locator)
return distance_field
Many thanks.