VTK Learning Thirty-one - pyvista
程序员文章站
2022-07-14 16:29:05
...
pyvista is a high-level API to the VTK
创建规则网格曲面
# sphinx_gallery_thumbnail_number = 2
import pyvista as pv
import numpy as np
生成一个二维的正弦曲面
# Make data
x = np.arange(-10, 10, 0.25)
y = np.arange(-10, 10, 0.25)
x, y = np.meshgrid(x, y)
r = np.sqrt(x ** 2 + y ** 2)
z = np.sin(r)
# Create and plot structured grid
grid = pv.StructuredGrid(x, y, z)
grid.plot()
# Plot mean curvature as well 显示曲面的平均曲率
grid.plot_curvature(clim=[-1, 1])
访问网格曲面的点集
grid.points
pyvista_ndarray([[-10. , -10. , 0.99998766],
[-10. , -9.75 , 0.98546793],
[-10. , -9.5 , 0.9413954 ],
...,
[ 9.75 , 9.25 , 0.76645876],
[ 9.75 , 9.5 , 0.86571785],
[ 9.75 , 9.75 , 0.93985707]])
生成点集(n*3)pv.PolyData.points
def make_point_set():
"""Ignore the contents of this function. Just know that it returns an
n by 3 numpy array of structured coordinates."""
n, m = 29, 32
x = np.linspace(-200, 200, num=n) + np.random.uniform(-5, 5, size=n)
y = np.linspace(-200, 200, num=m) + np.random.uniform(-5, 5, size=m)
xx, yy = np.meshgrid(x, y)
A, b = 100, 100
zz = A * np.exp(-0.5 * ((xx / b) ** 2.0 + (yy / b) ** 2.0))
points = np.c_[xx.reshape(-1), yy.reshape(-1), zz.reshape(-1)]
foo = pv.PolyData(points)
foo.rotate_z(36.6)
return foo.points
# Get the points as a 2D NumPy array (N by 3)
points = make_point_set()
points[0:5, :]
pyvista_ndarray([[ -34.71943671, -281.07091791, 1.8127684 ],
[ -23.93431169, -273.06117719, 2.33592346],
[ -16.54269672, -267.57167946, 2.75042917],
[ -3.27044266, -257.71483371, 3.61029187],
[ 6.2556755 , -250.64011401, 4.31540067]])
import matplotlib.pyplot as plt
plt.figure(figsize=(10, 10))
plt.scatter(points[:, 0], points[:, 1], c=points[:, 2])
plt.axis("image")
plt.xlabel("X Coordinate")
plt.ylabel("Y Coordinate")
plt.show()
生成一个规则网格曲面 pv.StructuredGrid
# Once you've figured out your grid's dimensions, simple create the
# :class:`pyvista.StructuredGrid` as follows:
mesh = pv.StructuredGrid()
# Set the coordinates from the numpy array
mesh.points = points
# set the dimensions
mesh.dimensions = [29, 32, 1]
# and then inspect it!
mesh.plot(show_edges=True, show_grid=True, cpos="xy")
grid.plot(show_edges=True)
top = grid.points.copy()
bottom = grid.points.copy()
print(bottom[0:5,:])
#生成底面
bottom[:,-1] = -10.0 # Wherever you want the plane
print(bottom[0:5,:])
vol = pv.StructuredGrid()
#堆叠top , bottom
vol.points = np.vstack((top, bottom))
# [grid.x,grid.y,2]
vol.dimensions = [*grid.dimensions[0:2], 2]
vol.plot(show_edges=True)
[[-10. -10. 0.99998766]
[-10. -9.75 0.98546793]
[-10. -9.5 0.9413954 ]
[-10. -9.25 0.87027587]
[-10. -9. 0.7753401 ]]
[[-10. -10. -10. ]
[-10. -9.75 -10. ]
[-10. -9.5 -10. ]
[-10. -9.25 -10. ]
[-10. -9. -10. ]]
上一篇: 模仿kfifo实现的环形缓冲区