# Examples from pyvista.DataObjectFilters.resample_to_image
# =========================================================

# Resample a solid sphere's point data onto a uniform grid. The spacing is
# estimated from the mesh's own cells.
import pyvista as pv
solid_sphere = pv.SolidSphere()
solid_sphere['height'] = solid_sphere.points[:, 2]
volume = solid_sphere.resample_to_image()

# Volume render the result. Every voxel is visible, so the volume is a solid
# block.
volume.plot(volume=True)

# Set `mark_blank=True` to hide the voxels no value could be resampled for.
blanked = solid_sphere.resample_to_image(mark_blank=True)
blanked.plot(volume=True)

# Set the `dimensions` or the `spacing` to control the resolution explicitly.
solid_sphere.resample_to_image(spacing=0.05).dimensions

# Compare the three kinds of input the sphere can be given as: a solid, the
# surface enclosing it, and its points alone.
def resample_as_voxels(mesh):
    image = mesh.resample_to_image(spacing=0.05, mark_blank=True)
    return image.points_to_cells()
surface = solid_sphere.extract_surface(algorithm=None)
pointset = solid_sphere.cast_to_pointset()
datasets = {
    'solid': solid_sphere,
    'solid voxels': resample_as_voxels(solid_sphere),
    'surface': surface,
    'surface voxels': resample_as_voxels(surface),
    'pointset': pointset,
    'pointset voxels': resample_as_voxels(pointset),
}
pv.plot_compare(datasets, shape=(3, 2), scalars='height')

# The solid is filled throughout, and the surface fills only the shell its cells
# occupy. The point cloud keeps the solid's interior points, so it reaches the
# interior too, but only within `radius` of each point, which leaves gaps where
# the points are sparse.
# Resample every block of a `MultiBlock` onto one grid.
blocks = pv.MultiBlock([pv.Sphere(), pv.Sphere(center=(1.5, 0, 0))])
for block in blocks:
    block['height'] = block.points[:, 2]
blocks.resample_to_image(target_n_points=20_000).dimensions

# ----------------------------------------------------------------------
# Generated by sphinx-examples-as-code https://github.com/pyvista/sphinx-examples-as-code
