DataSetFilters.voxelize_rectilinear#
- DataSetFilters.voxelize_rectilinear(
- *,
- background_value: int | float = 0,
- foreground_value: int | float = 1,
- reference_volume: ImageData | None = None,
- dimensions: VectorLikeInt | None = None,
- spacing: float | VectorLikeFloat | None = None,
- target_n_points: int | None = None,
- max_n_points: int | None = None,
- rounding_func: Callable[[VectorLikeFloat], VectorLikeInt] | None = None,
- cell_length_percentile: float | None = None,
- cell_length_sample_size: int | None = None,
- progress_bar: bool = False,
Voxelize mesh to create a RectilinearGrid voxel volume.
The voxelization can be controlled in several ways:
Specify the output geometry using a
reference_volume.Specify the
spacingexplicitly.Specify the
dimensionsexplicitly.Specify the
target_n_points. The spacing is isotropic and estimated so the output has approximately this many points.Specify the
cell_length_percentile. The spacing is estimated from the surface’s cells using the specified percentile.
Set
max_n_pointsto cap the result of any of these. It differs fromtarget_n_points, which is a resolution to aim for: a geometry specified explicitly raises if it exceeds the cap, while an estimated one is coarsened to fit.Use
reference_volumefor full control of the output grid’s geometry. For all other options, the geometry is implicitly defined such that the generated grid fits the bounds of the input mesh.If no inputs are provided,
cell_length_percentile=0.1(tenth percentile) is used by default to estimate the spacing.A point data array
maskis included where points inside and outside of the input surface are labelled withforeground_valueandbackground_value, respectively.Added in version 0.46.
Note
The input must be a surface with faces or strips. Generate a surface from a point cloud with
reconstruct_surface()before voxelizing it.Note
This method is a wrapper around
voxelize_binary_mask(). See that method for additional information.- Parameters:
- background_value
int, default: 0 Background value of the generated grid.
- foreground_value
int, default: 1 Foreground value of the generated grid.
- reference_volume
ImageData, optional Volume to use as a reference. The output will have the same
dimensions,origin,spacing,offset, anddirection_matrixas the reference.- dimensions
VectorLikeInt, optional Dimensions of the generated rectilinear grid. Set this value to control the dimensions explicitly. If unset, the dimensions are defined implicitly through other parameter. See summary and examples for details.
Note
Dimensions is the number of points along each axis, not cells. Set dimensions to
N+1instead forNcells along each axis.- spacing
float|VectorLikeFloat, optional Approximate spacing to use for the generated grid. Set this value to control the spacing explicitly. If unset, the spacing is defined implicitly through other parameters. See summary and examples for details.
- target_n_points
int, optional Approximate number of points to generate. The spacing is isotropic and chosen so the output holds about this many points, distributed between the axes in proportion to the input’s bounds. An axis with no extent holds a single point and takes no part in the count. Rounding to whole voxels means the count is approached, not matched exactly. Cannot be set with
reference_volume,dimensions,spacing, or the cell length options.Note
Like
dimensions, this counts the points of the output, which has one more point than cells along each axis. The same holds formax_n_points.Added in version 0.50.
- max_n_points
int, optional Strict upper bound on the number of points generated. Unlike
target_n_points, which is only approached, this limit is never exceeded. How it is enforced depends on how the geometry is defined:Geometry set explicitly, with
reference_volume,dimensions,spacingor a cell length option, raises if it exceeds the limit.target_n_pointsmust not exceed the limit, and the grid estimated from it is coarsened if rounding would take it above.Geometry left to the defaults is coarsened to fit, without raising.
Added in version 0.50.
- rounding_func
Callable[VectorLikeFloat,VectorLikeInt], optional Control how the dimensions are rounded to integers based on the provided or calculated
spacing. Should accept a length-3 vector containing the dimension values along the three directions and return a length-3 vector.numpy.round()is used by default.Rounding the dimensions implies rounding the actual spacing.
Has no effect if
reference_volumeordimensionsare specified.- cell_length_percentile
float, optional Cell length percentage
pto use for computing the defaultspacing. Default is0.1(tenth percentile) and must be between0and1. The input’s surface is extracted first, and thep-th percentile is computed from the lengths of the edges of its cells. Up tocell_length_sample_sizeof those cells are used, drawn at random with a fixed seed, and degenerate edges with zero length are ignored.Changed in version 0.50.0: The percentile is computed from every edge of the sampled cells instead of the distance between two random points of each triangulated cell, and the sample is drawn with a fixed seed. The estimate is now deterministic.
Has no effect if
dimensionsorreference_volumeare specified.- cell_length_sample_size
int, optional Maximum number of cells to use when computing the
cell_length_percentile.100 000cells are used by default.- progress_barbool, default:
False Display a progress bar to indicate progress.
- background_value
- Returns:
RectilinearGridRectilinearGrid as voxelized volume with discretized cells.
Examples#
Download Python source code | Download Jupyter notebook
Create a voxel volume of a nut. By default, the spacing is automatically estimated.
>>> import pyvista as pv
>>> from pyvista import examples
>>> mesh = pv.examples.load_nut()
>>> vox = mesh.voxelize_rectilinear()
Plot the mesh together with its volume.
>>> pl = pv.Plotter()
>>> _ = pl.add_mesh(mesh=vox, show_edges=True)
>>> _ = pl.add_mesh(mesh=mesh, show_edges=True, opacity=1)
>>> pl.show()
Load a mesh of a cow.
>>> mesh = examples.download_cow()
Create an equal density voxel volume and plot the result.
>>> vox = mesh.voxelize_rectilinear(spacing=0.15)
>>> cpos = pv.CameraPosition(
... position=(15, 3, 15), focal_point=(0, 0, 0), viewup=(0, 1, 0)
... )
>>> vox.plot(scalars='mask', show_edges=True, cpos=cpos)
Slice the voxel volume to view the mask scalars.
>>> slices = vox.slice_orthogonal()
>>> slices.plot(scalars='mask', show_edges=True)
Create a voxel volume from unequal density dimensions and plot result.
>>> vox = mesh.voxelize_rectilinear(spacing=(0.15, 0.15, 0.5))
>>> vox.plot(scalars='mask', show_edges=True, cpos=cpos)
Slice the unequal density voxel volume to view the mask scalars.
>>> slices = vox.slice_orthogonal()
>>> slices.plot(scalars='mask', show_edges=True, cpos=cpos)
See Also#
voxelizeSimilar function that returns a
UnstructuredGridofVOXELcells.voxelize_binary_maskSimilar function that returns a
ImageDatawith point data.pyvista.DataObjectFilters.resample_to_imageSimilar function which generates a
ImageDataof the same geometry. It resamples the input’s data arrays instead of generating a mask, and fills the voxels its cells or points reach rather than a closed surface’s interior.