簡體   English   中英

在python中將vtk UnstrucutredGrid寫入文件

[英]writing vtk UnstrucutredGrid to file in python

我想將 3D 標量數據寫入 *.vtu 文件(使用 python),以便稍后在 Paraview 中提取並按體積查看。 我可以編寫 *.vtu 文件,但是 Paraview 在加載它時崩潰。 我是否錯誤地使用了 vtk API?

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

我遵循了在此處使用 PolyVertex 對象可以找到的唯一示例。

並提出了以下課程:

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

我實例化該類並嘗試將一個簡單的隨機 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()

在 Paraview 中打開文件時,Paraview Gui 沒有響應,然后崩潰。

一個可以說更簡單的方法是使用meshio (我的一個項目)。 您可以輕松編寫各種格式的非結構化網格,例如 VTK/VTU。 安裝

pip3 install meshio [--user]

(甚至不依賴於 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
    )

我正在使用meshio模塊的寫入功能,但是“點”打印在單行而不是三列中。 有什么建議嗎? 謝謝

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM