使用 python 构建 vtkUnstructuredGrid 时如何避免循环?

How to avoid loops when building a vtkUnstructuredGrid with python?

我想使用 vtk python 模块构建一个 vtkUnstructuredGrid 对象。

我发现的每个代码片段都使用像 InsertNextPoint()InsertNextCell() 这样的方法,这些方法需要像 .

这样的点和单元格循环

有一个名为 tvtk 的 vtk python 模块覆盖层,它实现了一些有趣的东西:

ug = tvtk.UnstructuredGrid(nodes)
ug.set_cells(cell_type, cells)

其中 nodes 是一个包含节点坐标的二维 numpy 数组,cells 是一个包含元素连接性的二维 numpy 数组(根据 cell_type)。 tvtkvtk 模块 pythonic 多得多,但不幸的是没有那么流行,而且在集群上很少可用。

我的问题是:使用 vtk python 模块,是否有任何方法可以像 tvtk 一样使用 numpy 数组设置 vtkUnstructuredGrid,从而避免昂贵的循环?

您可以查看vtk_numpy。 例如:

import vtk
from vtk.util.numpy_support import numpy_to_vtk, vtk_to_numpy

varr = numpy_to_vtk(myarray.ravel(order='F'), deep=True, array_type=vtk.VTK_FLOAT)
varr.SetName('myarray')
ugrid.GetPointData().SetScalars(varr)