简体   繁体   English

在python中将vtk UnstrucutredGrid写入文件

[英]writing vtk UnstrucutredGrid to file in python

I would like to write 3D scalar data out to *.vtu file (using python) to be later picked up in Paraview and viewed volumetrically.我想将 3D 标量数据写入 *.vtu 文件(使用 python),以便稍后在 Paraview 中提取并按体积查看。 I can write the *.vtu file, but Paraview crashes upon loading it.我可以编写 *.vtu 文件,但是 Paraview 在加载它时崩溃。 Have I used the vtk API incorrectly?我是否错误地使用了 vtk API?

My approach: (Python 2.7.12, VTK_MAJOR_VERSION 6, Paraview 5.0.1)我的方法:(Python 2.7.12,VTK_MAJOR_VERSION 6,Paraview 5.0.1)

I followed the only example I could find using PolyVertex objects here .我遵循了在此处使用 PolyVertex 对象可以找到的唯一示例。

and came up with the following class:并提出了以下课程:

import vtk
import numpy as np

class VtkPolyVertCloud(object):

    def __init__(self):

        self.points= vtk.vtkPoints()
        self.grid = vtk.vtkUnstructuredGrid()
        self.values = vtk.vtkDoubleArray()
        self.values.SetName('point_values_array')
        self.grid.SetPoints(self.points)
        self.grid.GetPointData().SetScalars(self.values)

    def add_polyVertex_cell(self, points, data):
        """
        adds points according to user-supplied numpy arrays

        @param points: numpy array of 3d point coords -- points.shape = (npoints, 3)
        @param data: scalar-valued data belonging to each point -- data.shape = (npoints,)
        """
        npts = points.shape[0]

        pv = vtk.vtkPolyVertex()
        pv.GetPointIds().SetNumberOfIds(npts)
        for idx, point in enumerate(points):
            pointID = self.points.InsertNextPoint(point)
            pv.GetPointIds().SetId(idx, pointID)
            self.values.InsertNextValue(data[idx])

        self.grid.InsertNextCell(pv.GetCellType(), pv.GetPointIds())

I instantiate the class and attempt to write a simple random PolyVertex cell to XML file:我实例化该类并尝试将一个简单的随机 PolyVertex 单元格写入 XML 文件:

def test_vtkPolyVertexCloud_writeToFile():
    """ adds a set of polyvertices meant to represent a finite element """
    pc = vtku.VtkPolyVertCloud()
    points, data = np.random.rand(10, 3), np.random.rand(10)
    pc.add_polyVertex_cell(points, data)

    # write
    fn = 'test_PolyVertexCloud.vtu'
    writer = vtk.vtkUnstructuredGridWriter()
    writer.SetFileName(fn)
    writer.SetInputData(pc.grid)
    writer.Write()

Upon opening the file in Paraview, the Paraview Gui is unresponsive, then crashes.在 Paraview 中打开文件时,Paraview Gui 没有响应,然后崩溃。

An arguably easier approach would be to use meshio (a project of mine).一个可以说更简单的方法是使用meshio (我的一个项目)。 You can easily write unstructured meshes in a variety of formats, eg, VTK/VTU.您可以轻松编写各种格式的非结构化网格,例如 VTK/VTU。 Install with安装

pip3 install meshio [--user]

(no dependency on vtk even) and use it like (甚至不依赖于 vtk)并像使用它一样使用它

import meshio
import numpy

points = numpy.array([
    [0.0, 0.0, 0.0],
    [0.0, 1.0, 0.0],
    [0.0, 0.0, 1.0],
    ])
cells = {
    "triangle": numpy.array([
        [0, 1, 2]
        ])
    }
meshio.write_points_cells(
    "foo.vtu",
    points,
    cells,
    # Optionally provide extra data on points, cells, etc.
    # point_data=point_data,
    # cell_data=cell_data,
    # field_data=field_data
    )

I am using the writing function of the meshio module but the 'points' are printed in a single row instead of three columns. 我正在使用meshio模块的写入功能,但是“点”打印在单行而不是三列中。 Any suggestion? 有什么建议吗? Thanks 谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM