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

博客讨论了如何使用Python的meshio库读取VTK文件,并探讨了尝试用matplotlib绘制VTK数据结构的困难。作者建议使用专门的VTK库,如vtk、mayavi或pyvista,因为matplotlib不适合此类3D复杂数据的可视化。文章提供了一个简化版的VTK数据示例,并展示了如何使用vtk和mayavi进行可视化。
摘要由CSDN通过智能技术生成

1586010002-jmsa.png

I have a VTK file (unstructured grid) with points and cells.

I can import the file and read it into using the meshio python package.

If I type the command mesh.cells I see a dictionary called 'hexahedron' with an array made up of lists inside like this:

{'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]])}

I would like to plot this in matplotlib (I know ParaView is an alternative, which I've been using, but I would also like to use matplotlib for this at the moment). Anyways, I'm having trouble wrapping my head around the structure.

There are 8 data points in each list.

If I run the command mesh.points I get an array of lists of x, y, z coordinates, which makes sense. However, with the hexahedron, are there also x, y, z coordinates in the list? It would make more sense if there were lists of x, y, z coordinates, as that would make up polygons.

I've seen this thread, but I'm still stuck on understanding this.

Attached is the VTK file, as well as what it looks like in ParaView. Thanks!

MSMu8.png

解决方案

tl;dr: I don't think you should try to use matplotlib for this, and it would be difficult and not work very well. I suggest using a dedicated vtk library, either bare vtk, the higher-level mayavi.mlab or my recently acquired favourite, pyvista. I'll elaborate on all this in detail.

The data

First, here is a small, self-contained version of your input data (since the data you linked in the question is both too large and likely to become a broken link sooner or later). I've reduced your data to three rectangular cuboids of varying sizes to approximate your figure.

# vtk DataFile Version 3.1

MCVE VTK file

ASCII

DATASET UNSTRUCTURED_GRID

POINTS 16 float

0. 0. 0.

0. 0. 3.

0. 2. 0.

0. 2. 3.

4. 0. 0.

4. 0. 3.

4. 2. 0.

4. 2. 3.

5. 0. 0.

5. 0. 3.

要画三维函数图,可以使用Python中的Matplotlib模块。以下是一个简单的例子: ```python import numpy as np import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D # 定义函数 def f(x, y): return np.sin(np.sqrt(x**2 + y**2)) # 生成数据 x = np.linspace(-5, 5, 50) y = np.linspace(-5, 5, 50) X, Y = np.meshgrid(x, y) Z = f(X, Y) # 绘制图形 fig = plt.figure() ax = fig.add_subplot(111, projection='3d') ax.plot_surface(X, Y, Z) plt.show() ``` 如果要使用VTK来实现三维模型的显示和切割,可以参考下面的代码: ```python import vtk # 读取STL文件 reader = vtk.vtkSTLReader() reader.SetFileName("model.stl") reader.Update() # 显示模型 mapper = vtk.vtkPolyDataMapper() mapper.SetInputConnection(reader.GetOutputPort()) actor = vtk.vtkActor() actor.SetMapper(mapper) renderer = vtk.vtkRenderer() renderer.AddActor(actor) renderWindow = vtk.vtkRenderWindow() renderWindow.AddRenderer(renderer) # 切割模型 plane = vtk.vtkPlane() plane.SetOrigin(0, 0, 0) plane.SetNormal(1, 0, 0) cutter = vtk.vtkCutter() cutter.SetInputConnection(reader.GetOutputPort()) cutter.SetCutFunction(plane) cutter.Update() cutterMapper = vtk.vtkPolyDataMapper() cutterMapper.SetInputConnection(cutter.GetOutputPort()) cutterActor = vtk.vtkActor() cutterActor.SetMapper(cutterMapper) # 显示切割后的模型 renderer2 = vtk.vtkRenderer() renderer2.AddActor(cutterActor) renderWindow2 = vtk.vtkRenderWindow() renderWindow2.AddRenderer(renderer2) # 显示窗口 iren = vtk.vtkRenderWindowInteractor() iren.SetRenderWindow(renderWindow) iren.Initialize() iren.Start() ``` 这个例子中,我们首先读取一个STL文件,然后显示整个模型。接着,我们定义了一个切割平面,使用vtkCutter对模型进行切割,并显示切割后的模型。最后,我们使用vtkRenderWindowInteractor来显示窗口,并启动事件循环。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值