用于测试的无头 VTK

headless VTK for tests

我有一个绘制四面体的简单 VTK Python 代码:

# plot a tetrahedron
import numpy
import vtk

node_coords = numpy.array(
    [
        [1.0, 0.0, -1.0 / numpy.sqrt(8)],
        [-0.5, +numpy.sqrt(3.0) / 2.0, -1.0 / numpy.sqrt(8)],
        [-0.5, -numpy.sqrt(3.0) / 2.0, -1.0 / numpy.sqrt(8)],
        [0.0, 0.0, numpy.sqrt(2.0) - 1.0 / numpy.sqrt(8)],
    ]
) / numpy.sqrt(3.0)
cells = numpy.array([[0, 1, 2, 3]])
cell_id = 0
line_width = 3.0


def get_line_actor(x0, x1, line_width=1.0):
    source = vtk.vtkLineSource()
    source.SetPoint1(x0)
    source.SetPoint2(x1)
    # mapper
    mapper = vtk.vtkPolyDataMapper()
    mapper.SetInputConnection(source.GetOutputPort())
    # actor
    actor = vtk.vtkActor()
    actor.SetMapper(mapper)
    # color actor
    actor.GetProperty().SetColor(0, 0, 0)
    actor.GetProperty().SetLineWidth(line_width)
    return actor


# Visualize
renderer = vtk.vtkRenderer()
renderWindow = vtk.vtkRenderWindow()
renderWindow.AddRenderer(renderer)
renderWindowInteractor = vtk.vtkRenderWindowInteractor()
renderWindowInteractor.SetRenderWindow(renderWindow)

for ij in [[0, 1], [0, 2], [0, 3], [1, 2], [1, 3], [2, 3]]:
    x0, x1 = node_coords[cells[cell_id][ij]]
    renderer.AddActor(get_line_actor(x0, x1, line_width))
renderer.SetBackground(1.0, 1.0, 1.0)

renderWindow.Render()
renderWindowInteractor.Start()

我现在想为此代码(在无头环境中运行)添加持续集成测试,但我得到的只是

Received 'aborted' signal

添加用于关闭 window

的代码
# renderWindowInteractor.Start()

render_window.Finalize()
del render_window, render_window_interactor

没有帮助。

如何在无头环境下使用VTK?

原来是 Render 调用导致 VTK 抛出。对于单元测试,我可以注释掉那些行

if render:
    renderWindow.Render()
    renderWindowInteractor.Start()

其余代码即使在无头环境中也能正常执行。