Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用python读取和绘制VTK文件数据结构_Python_Python 3.x_Mesh_Vtk_Paraview - Fatal编程技术网

用python读取和绘制VTK文件数据结构

用python读取和绘制VTK文件数据结构,python,python-3.x,mesh,vtk,paraview,Python,Python 3.x,Mesh,Vtk,Paraview,我有一个VTK文件(非结构化网格),其中包含点和单元格 我可以导入文件并使用python包将其读入 如果我键入命令mesh.cells我会看到一个名为“六面体”的字典,里面有一个由列表组成的数组,如下所示: {'hexahedron': array([[ 0, 162, 185, ..., 163, 186, 23], [162, 329, 351, ..., 330, 352, 186], [329, 491, 514, ..., 492, 515, 352

我有一个VTK文件(非结构化网格),其中包含
单元格

我可以导入文件并使用python包将其读入

如果我键入命令
mesh.cells
我会看到一个名为
“六面体”
的字典,里面有一个由列表组成的数组,如下所示:

{'hexahedron': array([[  0, 162, 185, ..., 163, 186,  23],
        [162, 329, 351, ..., 330, 352, 186],
        [329, 491, 514, ..., 492, 515, 352],
        ...,
        [483, 583, 600, ..., 584, 601, 490],
        [583, 650, 656, ..., 651, 657, 601],
        [650, 746, 762, ..., 747, 763, 657]])}
我想在matplotlib中绘制这个图(我知道ParaView是一个替代方案,我一直在使用它,但我现在也想使用matplotlib)。不管怎么说,我的头很难绕住这个结构

每个列表中有8个数据点

如果我运行命令
mesh.points
,我会得到一组
x,y,z
坐标的列表,这很有意义。但是,对于六面体,列表中是否也有
x,y,z
坐标?如果有
x,y,z
坐标的列表就更有意义了,因为这将构成多边形

,但我仍然无法理解这一点

,以及它在ParaView中的外观。谢谢
tl;dr:我认为您不应该尝试使用matplotlib来实现这一点,这将很困难,而且效果也不太好。我建议使用专用的vtk库,无论是裸库、高级库还是我最近获得的最爱库。我将详细阐述这一切

数据 首先,这里是输入数据的一个小的、自包含的版本(因为您在问题中链接的数据太大,迟早可能成为断开的链接)。我已经把你的数据缩小到三个不同的大小来近似你的数字

#vtk数据文件版本3.1
MCVE VTK文件
ASCII码
数据集非结构化网格
第16点浮动
000
003.
02.0
02.3.
4.00
4.03.
4.2.0
4.2.3.
5.00
5.03.
5.2.0
5.2.3.
1300
1303.
132.0
132.3.
第3单元27
8    0   1   3   2   4   5   7   6
8    4   5   7   6   8   9  11  10
8    8   9  11  10  12  13  15  14
细胞类型3
12          12          12
单元数据3
标量元素浮点数
查找表默认值
1.
2.
3.
让我们讨论一下这个文件代表什么。标头指定它是非结构化网格。这意味着它可以包含以任意方式排列的点。基本上是一包分数。您可以找到有关文件格式的一些说明

第一个块,
POINTS
,包含
16个float
行,每行对应于3d中一个点的坐标,总共16个点

第二个块,
CELLS
,定义了3行,每行对应一个单元(在本例中为体积较小的单元),该单元根据点的基于0的索引定义。第一个数字(
8
)表示给定单元格中的顶点数,以下数字是对应顶点的点索引。上述示例数据文件中的所有三个单元格都由8个顶点组成,因为我们要绘制的每个长方体都有8个顶点。
单元格
行上的第二个数字是此块中的数字总数,即
3*(8+1)
,即27

第三个块,
CELL\u TYPES
,为每个
3
单元定义单元的类型。在这种情况下,它们都是类型
12
,对应于“六面体”。资料性数字: 这些列表列出了主要的单元格类型及其各自的索引

最后一个块,
SCALARS
,包含每个单元格的标量(数字),稍后将根据该标量进行着色。标量
1
3
将被映射到一个颜色映射图上,为您提供图中所示的从红色到蓝色的过渡

为什么不是matplotlib? 我不熟悉
meshio
,但我怀疑它可以让您访问VTK文件中的上述块。您显示的
mesh.cells
属性表明它可以识别每个单元都是“六面体”,并列出每个单元及其各自的8个顶点索引。
mesh.points
属性可能是一个形状数组
(n,3)
,在这种情况下,
mesh.points[cell_inds,:]
为您提供由8长度数组
cell_inds
定义的给定单元格的
(8,3)
形状坐标

您将如何使用matplotlib将其可视化?首先,您的实际数据是巨大的,它包含84480个单元格,尽管从远处看,它们看起来很像上面的示例数据。所以你必须

  • 想出一种方法将所有这些单元坐标转换为曲面,用matplotlib绘制,这并不容易
  • 然后意识到80k曲面将导致matplotlib中的巨大内存和CPU开销
  • 请注意,matplotlib有一个二维渲染器,因此复杂(读取:不相交、互锁)曲面的三维可视化
  • 考虑到所有这些因素,我绝对不会尝试使用matplotlib进行此操作

    然后呢? 使用ParaView在引擎盖下使用的东西:VTK!您仍然可以通过低级模块或高级(er)模块以编程方式使用机器。还有与
    mayavi
    相关的
    tvtk
    模块,它是一个中间地带(出于这些目的,它仍然是低级的VTK,但是有一个更友好的python API),但我将把它作为一个练习留给读者

    1. <代码>vtk 使用vtk读取和绘制非结构化网格有点复杂(与使用裸vtk时一样,因为您必须自己组装管道),但可以使用plus进行管理:

    请注意,我只对原始wiki版本进行了最小程度的更改。以下是视口旋转后的输出:

    实际颜色取决于默认颜色贴图和标量的缩放。
    vtk
    模块的上述默认设置请参见
    from vtk import (vtkUnstructuredGridReader, vtkDataSetMapper, vtkActor,
                     vtkRenderer, vtkRenderWindow, vtkRenderWindowInteractor)
    
    file_name = "mesh_mcve.vtk"  # minimal example vtk file
    
    # Read the source file.
    reader = vtkUnstructuredGridReader()
    reader.SetFileName(file_name)
    reader.Update()  # Needed because of GetScalarRange
    output = reader.GetOutput()
    output_port = reader.GetOutputPort()
    scalar_range = output.GetScalarRange()
    
    # Create the mapper that corresponds the objects of the vtk file
    # into graphics elements
    mapper = vtkDataSetMapper()
    mapper.SetInputConnection(output_port)
    mapper.SetScalarRange(scalar_range)
    
    # Create the Actor
    actor = vtkActor()
    actor.SetMapper(mapper)
    
    # Create the Renderer
    renderer = vtkRenderer()
    renderer.AddActor(actor)
    renderer.SetBackground(1, 1, 1) # Set background to white
    
    # Create the RendererWindow
    renderer_window = vtkRenderWindow()
    renderer_window.AddRenderer(renderer)
    
    # Create the RendererWindowInteractor and display the vtk_file
    interactor = vtkRenderWindowInteractor()
    interactor.SetRenderWindow(renderer_window)
    interactor.Initialize()
    interactor.Start()
    
    from mayavi import mlab
    from mayavi.modules.surface import Surface
    
    file_name = "mesh_mcve.vtk"  # minimal example vtk file
    
    # create a new figure, grab the engine that's created with it
    fig = mlab.figure()
    engine = mlab.get_engine()
    
    # open the vtk file, let mayavi figure it all out
    vtk_file_reader = engine.open(file_name)
    
    # plot surface corresponding to the data
    surface = Surface()
    engine.add_filter(surface, vtk_file_reader)
    
    # block until figure is closed
    mlab.show()
    
    import pyvista as pv
    
    # read the data
    grid = pv.read('mesh_mcve.vtk')
    
    # plot the data with an automatically created Plotter
    grid.plot(show_scalar_bar=False, show_axes=False)