How To Fill A Distance Between Two Meshed As Filled Cells/mesh In Pyvista?
So I look at this sample and I want to create a mesh/surface grid that fills that distance. How to do such a thing in PyVista? What I try and seem not to be able to bridge with And
Solution 1:
I don't know if there's a built-in way to interpolate between two surfaces, but it's not very hard to do so using just numpy.
Here's an example that uses Perlin noise to generate two sheets of data on the same grid in two different heights. The actual code for the answer comes after.
import numpy as np
import pyvista as pv
# generate two sheets of input data
noise = pv.perlin_noise(2, (0.2, 0.2, 0.2), (0, 0, 0))
bounds_2d = (-10, 10, -10, 10)
dim = (40, 50, 1)
bottom, top = [
pv.sample_function(noise, dim=dim, bounds=bounds_2d + (z, z)).warp_by_scalar()
for z in [-5, 5]
]
# actual answer starts here# the top and bottom sheets are named `top` and `bottom`# and they share the same 2d grid# rebuild grid points
grid_2d = top.points.reshape(dim[:-1] + (3,), order='F')[..., :-1]
values_x = grid_2d[:, 0, 0]
values_y = grid_2d[0, :, 1]
# generate full grid with equidistant interpolation in each (x, y)
nz = 10
scale = np.linspace(0, 1, nz)
scale_z = scale[:, None] * [0, 0, 1] # shape (nz, 3)
scale_z_inv = (1 - scale[:, None]) * [0, 0, 1] # shape (nz, 3)
z_bottom = bottom.points.reshape(dim[:-1] + (3,), order='F')[..., -1] # shape (nx, ny)
z_top = top.points.reshape(dim[:-1] + (3,), order='F')[..., -1] # shape (nx, ny)
interpolated_z = scale * z_bottom[..., None] + (1 - scale) * z_top[..., None] # shape (nx, ny, nz)
grid_2d_in_3d = np.pad(grid_2d, [(0, 0), (0, 0), (0, 1)]) # shape (nx, ny, 3)
final_grid = grid_2d_in_3d[..., None, :] + interpolated_z[..., None] * [0, 0, 1] # shape (nx, ny, nz, 3)
mesh = pv.StructuredGrid(*final_grid.transpose())
# plot the two sheets and the interpolated grid
pv.set_plot_theme('document')
plotter = pv.Plotter()
plotter.add_mesh(bottom, show_scalar_bar=False)
plotter.add_mesh(top, show_scalar_bar=False)
plotter.add_mesh(mesh, style='wireframe')
plotter.show()
Post a Comment for "How To Fill A Distance Between Two Meshed As Filled Cells/mesh In Pyvista?"