pyvista / pyvista-support

[moved] Please head over to the Discussions tab of the PyVista repository
https://github.com/pyvista/pyvista/discussions
59 stars 4 forks source link

Subsampling a StructuredGrid #62

Open banesullivan opened 5 years ago

banesullivan commented 5 years ago

@jsfraser1

I'm trying to select a subsample of a StructuredGrid, and get back 3 numpy 2-D arrays (like meshgrid). I am already entering those arrays when constructing the structured grid, and I am looking for the right workflow to get numpy arrays from structuredgrids, not just a long list of points. Is there a shortcut to do that?

banesullivan commented 5 years ago

There isn't really a shortcut for this, but maybe we could make one for both StructuredGrids and UniformGrids. Here is an example of subsampling a sturctured grid in a few different ways. The trick is simply reshaping the points to the dimesnions and properly iterating over them:

import pyvista as pv
import numpy as np

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)

grid = pv.StructuredGrid(x, y, z)
grid.plot(show_edges=True)

download

# Structure the points into their grid
structured_points = grid.points.reshape((*grid.dimensions, 3))
# Get every step point in the grid
step = 3
subset_points = structured_points[::step, ::step, ::step, :]
xx = subset_points[:, :, :, 0]
yy = subset_points[:, :, :, 1]
zz = subset_points[:, :, :, 2]
subset = pv.StructuredGrid(xx, yy, zz)
subset.plot(show_edges=True)

download

# Or extract a VOI
subset_points = structured_points[:, 0:50, :, :]
xx = subset_points[:, :, :, 0]
yy = subset_points[:, :, :, 1]
zz = subset_points[:, :, :, 2]
subset = pv.StructuredGrid(xx, yy, zz)
subset.plot(show_edges=True)

download