Pyvista---(3)pyvista中mesh的构造和画图

1. mesh的构造

  1. cell的数量:mesh.n_cells
  2. points的数量:mesh.n_points
  3. mesh的中心:mesh.center
  4. mesh的点:mesh.points
  5. mesh中包含的矩阵:mesh.point_arrays
  6. 查看mesh的信息:print mesh
  7. 访问mesh中矩阵的值:通过字典的方式访问
    1. 访问point的值:mesh.point_arrays[‘new_points’]
    2. 访问cell的值:mesh.cell_arrays[‘new_points’]
  8. 给mesh赋值:
    1. 给point赋值:mesh.point_arrays[‘new_points’] = np.zeros(mesh.n_points)
    2. 给cell赋值:mesh.cell_arrays[‘new_points’] = np.zeros(mesh.n_cells)
  9. 如果是结构化的矩阵,获得mesh的值的维度:mesh.dimensions

2. 画mesh

有两种方法画mesh

2.1 第一种

直接画。

import pyvista as pv
from pyvista import examples

mesh = examples.load_airplane()
mesh.plot(screenshot='airplane.png')

2.2 第二种

指定更多参数的画:
先通过pv.Plotter()新建一个渲染窗口,然后通过add_mesh增加mesh。

plotter = pv.Plotter()    # instantiate the plotter
plotter.add_mesh(mesh)    # add a mesh to the scene
cpos = plotter.show()     # show the rendering window

请注意,该show方法将返回渲染窗口上次使用的相机位置,以备您选择相机位置并在以后再次使用时使用。

然后,您可以使用此缓存的摄像机进行其他打印,而不必与打印窗口手动交互:

plotter = pv.Plotter(off_screen=True)
plotter.add_mesh(mesh, color='tan')
plotter.camera_position = cpos
plotter.show(screenshot='airplane.png') ## 保存成图片

2.3 可交互的窗口

  • pyvista.Plotter:标准绘图仪,它将代码暂停直到关闭
  • pyvista.BackgroundPlotter:创建一个交互式的渲染窗口,并且不暂停代码执行

2.4 导出mesh,保存成vtk

可以使用.save() 直接绑定到这些对象的方法将任何PyVista网格对象保存为VTK文件格式。例如,上面使用的网格可以保存为:

mesh.save("mesh.vtk")

3. 例子

>>> mesh = examples.load_airplane()
>>> # cell的数量
>>> mesh.n_cells
2452
>>> # point的数量
>>> mesh.n_points
1335
>>> # What about scalar arrays? Are there any?
>>> mesh.n_arrays
0
>>> # What are the mesh bounds?
>>> mesh.bounds
[139.06100463867188, 1654.9300537109375, 32.09429931640625, 1319.949951171875, -17.741199493408203, 282.1300048828125]
>>> # mesh的中心
>>> mesh.center
[896.9955291748047, 676.0221252441406, 132.19440269470215]
# 点
the_pts = mesh.points
array([[896.994 ,  48.7601,  82.2656],
       [906.593 ,  48.7601,  80.7452],
       [907.539 ,  55.4902,  83.6581],
       [896.994 ,  55.4902,  85.3283],
       [896.994 ,  42.8477,  77.825 ]], dtype=float32)

3.1 point_arrays

mesh = examples.load_uniform()
## 获得值
arr = mesh.point_arrays['Spatial Point Data']
print mesh #查看mesh信息

output:
UniformGrid (0x12c9a9328)
  N Cells:	729
  N Points:	1000
  X Bounds:	0.000e+00, 9.000e+00
  Y Bounds:	0.000e+00, 9.000e+00
  Z Bounds:	0.000e+00, 9.000e+00
  Dimensions:	10, 10, 10
  Spacing:	1.000e+00, 1.000e+00, 1.000e+00
  N Arrays:	2

# mesh.dimensions
mesh = examples.load_uniform()
mesh.dimensions
output:
[10, 10, 10]

mesh的值是存储成一维的,所以如果想转换成原来的三维矩阵的话,就需要按照mesh.dimensions 将arr矩阵转换成矩阵。这样其实就是一个矩阵了,因为numpy矩阵本身就是这样。

3.2 如果是’UnstructuredGrid’则没有mesh.dimensions

访问的话就会报错:

AttributeError: 'UnstructuredGrid' object has no attribute 'dimensions'
  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值