PyVista 笔记
Concept
Mesh
任意的空间参考信息、2d、3d集合数据集都能成为Mesh
Point
Point是Mesh的顶点。可以使用 pv.PolyData()创建1D或2D Cell Type(点云)。
points = np.random.rand(100, 3)
mesh = pv.PolyData(points)
mesh.plot(point_size=10, style='points')
Grid Mesh
使用 mesh.points
获取mesh的顶点。
from pyvista import examples
mesh = examples.load_hexbeam()
cpos = [(6.20, 3.00, 7.50),
(0.16, 0.13, 2.65),
(-0.28, 0.94, -0.21)]
pl = pv.Plotter()
pl.add_mesh(mesh, show_edges=True, color='white')
pl.add_points(mesh.points, color='red',
point_size=20)
pl.camera_position = cpos
pl.show()
Cell
Cell是顶点间连接起来的几何结构。使用 mesh.export_cells()
与 mesh.n_cells
导出mesh的cell。
mesh = examples.load_hexbeam()
pl = pv.Plotter()
pl.add_mesh(mesh, show_edges=True, color='white')
pl.add_points(mesh.points, color='red', point_size=20)
single_cell = mesh.extract_cells(mesh.n_cells - 1)
pl.add_mesh(single_cell, color='pink', edge_color='blue',
line_width=5, show_edges=True)
pl.camera_position = [(6.20, 3.00, 7.50),
(0.16, 0.13, 2.65),
(-0.28, 0.94, -0.21)]
pl.show()
Attributes
Attributes(属性)为mesh上的点或cell的数据值,属性可以通过以下接口添加到mesh中:
point_data
cell_data
field_data
Point Data
点数据是一个对应于mesh上的点的数组数据,当绘图时,点间的Cell将会被差值。
mesh.point_data['my point values'] = np.arange(mesh.n_points)
mesh.plot(scalars='my point values', cpos=cpos, show_edges=True)
Cell Data
对应整个Cell的值
mesh.cell_data['my cell values'] = np.arange(mesh.n_cells)
mesh.plot(scalars='my cell values', cpos=cpos, show_edges=True)
Basic API Usage
mesh.n_cells
返回mesh中cells的数量,下同mesh.n_points
mesh.n_arrays
返回mesh中标量数组mesh.bounds
返回边界范围数组,shape=(6)
mesh.center
返回中心位置3d坐标数组mesh.points
返回mesh的所有顶点,是一个np.ndarray
,shape=(n, 3)
n个三维坐标
mesh的属性数据是一个字典,即可以存不同名字的数据:mesh.point_data['Spatial Point Data']
Plotting
对mesh使用 mesh.plot()
可打开绘图窗口绘制mesh。
亦可创建一个 plotter
对象,来调整绘制场景。首先使用 pyvista.Plotter
或 pyvista.BackgroundPlotter
。 pyvista.Plotter
会创建一个渲染窗口并在调用其 show()
之后暂停执行代码。
mesh = examples.load_airplane()
plotter = pv.Plotter() # instantiate the plotter
plotter.add_mesh(mesh) # add a mesh to the scene
plotter.camera.zoom(2) # Note how we can now access underlying attributes
plotter.show() # show the rendering window
- 使用
add_mesh()
添加mesh到plotting中 - 使用
camera.zoom(int zoom)
控制镜头缩放 - 使用
camera_position
设置镜头位置
Exporting
任意mesh物体都可以使用 save()
导出为VTK格式的文件。
mesh.save("mesh.vtk")