【问题标题】:python vtk writer蟒蛇vtk作家
【发布时间】:2013-02-06 15:50:26
【问题描述】:

我需要用python写一个xml vtk文件。 特别是在结构化网格中。 我有 x、y、z 轴的坐标以及空间中每个点的 x、y、z 分量的值。 我怎样才能写这个文件?

【问题讨论】:

    标签: python vtk


    【解决方案1】:

    (编辑)你可以通过这个函数来做到这一点(在python中)

    def writeVtkStructuredGrid(StructuredGrid, file_name):
        """
        StructuredGrid  : StructuredGrid input object
        file_name   : output xml path file (example: 'out.vtp')
        """
    
        sg = vtkXMLStructuredGridWriter()
        sg.SetFileName(file_name)
        sg.SetInput(StructuredGrid)
        sg.Write()
    

    关于 vtkXMLPolyDataWriter 对象的文档在: vtkXMLPolyDataWriter class reference

    【讨论】:

    • 他指的是 vtkStructuredGrid 文件,而不是 vtkPolyData 文件。我认为他还没有构建 vtkStructuredGrid,需要根据他当前的数据来构建。
    【解决方案2】:

    请检查此 Stackoverflow 链接,调整注释代码,您可以完成工作。 StackOverflow Link。具体例子from tvtk

    示例代码是

    from numpy import mgrid, empty, sin, pi
    from tvtk.api import tvtk, write_data
    
    # Generate some points.
    x, y, z = mgrid[1:6:11j, 0:4:13j, 0:3:6j]
    base = x[..., 0] + y[..., 0]
    # Some interesting z values.
    for i in range(z.shape[2]):
            z[..., i] = base * 0.25 * i
    
    # The actual points.
    pts = empty(z.shape + (3,), dtype=float)
    pts[..., 0] = x
    pts[..., 1] = y
    pts[..., 2] = z
    
    # Simple scalars.
    scalars = x * x + y * y + z * z
    # Some vectors
    vectors = empty(z.shape + (3,), dtype=float)
    vectors[..., 0] = (4 - y * 2)
    vectors[..., 1] = (x * 3 - 12)
    vectors[..., 2] = sin(z * pi)
    
    # We reorder the points, scalars and vectors so this is as per VTK's
    # requirement of x first, y next and z last.
    pts = pts.transpose(2, 1, 0, 3).copy()
    pts.shape = pts.size / 3, 3
    scalars = scalars.T.copy()
    vectors = vectors.transpose(2, 1, 0, 3).copy()
    vectors.shape = vectors.size / 3, 3
    
    # Create the dataset.
    sg = tvtk.StructuredGrid(dimensions=x.shape, points=pts)
    sg.point_data.scalars = scalars.ravel()
    sg.point_data.scalars.name = 'temperature'
    sg.point_data.vectors = vectors
    sg.point_data.vectors.name = 'velocity'
    write_data(sg, 'test') # creates test.vtu xml file
    #write_data(sg, 'test.vtk') # creates test.vtk
    

    【讨论】:

      猜你喜欢
      • 2010-10-27
      • 2021-08-04
      • 2010-10-21
      • 2016-05-28
      • 2014-08-01
      • 2021-06-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多