pyvista_js.UnstructuredGrid#

class pyvista_js.UnstructuredGrid(cells: ArrayLike, celltypes: ArrayLike, points: ArrayLike)#

Bases: object

Unstructured grid dataset.

An unstructured grid can hold cells of arbitrary type (tetrahedra, hexahedra, wedges, pyramids, triangles, quads, etc.). This class mirrors the pyvista.UnstructuredGrid constructor API.

For rendering in vtk.js the 3-D cells are decomposed into their surface faces so that they can be displayed as polygonal geometry.

Parameters:
  • cells (array-like) – Cell connectivity in VTK format: [n_pts0, p0, p1, ..., n_pts1, p0, p1, ...].

  • celltypes (array-like) – Array of VTK cell type constants (see CellType) - one value per cell.

  • points (array-like) – Vertex coordinates as an (n, 3) array.

Examples

Create a single tetrahedron:

>>> import pyvista_js as pv
>>> import numpy as np
>>> points = np.array([[0, 0, 0], [1, 0, 0], [0, 1, 0], [0, 0, 1]], dtype=float)
>>> cells = [4, 0, 1, 2, 3]
>>> celltypes = [pv.CellType.TETRA]
>>> grid = pv.UnstructuredGrid(cells, celltypes, points)
>>> grid.n_points
4
>>> grid.n_cells
1

Create a hexahedron (cube):

>>> import pyvista_js as pv
>>> import numpy as np
>>> points = np.array([
...     [0, 0, 0], [1, 0, 0], [1, 1, 0], [0, 1, 0],  # bottom face
...     [0, 0, 1], [1, 0, 1], [1, 1, 1], [0, 1, 1],  # top face
... ], dtype=float)
>>> cells = [8, 0, 1, 2, 3, 4, 5, 6, 7]
>>> celltypes = [pv.CellType.HEXAHEDRON]
>>> grid = pv.UnstructuredGrid(cells, celltypes, points)
>>> grid.n_points
8
>>> grid.n_cells
1

Create a mixed mesh with multiple cell types:

>>> import pyvista_js as pv
>>> import numpy as np
>>> # Triangle and quad cells
>>> points = np.array([
...     [0, 0, 0], [1, 0, 0], [0, 1, 0], [1, 1, 0],  # vertices
... ], dtype=float)
>>> cells = [3, 0, 1, 2, 4, 1, 2, 3, 0]  # triangle, quad
>>> celltypes = [pv.CellType.TRIANGLE, pv.CellType.QUAD]
>>> grid = pv.UnstructuredGrid(cells, celltypes, points)
>>> grid.n_cells
2

Access point data:

>>> import pyvista_js as pv
>>> import numpy as np
>>> points = np.array([[0, 0, 0], [1, 0, 0], [0, 1, 0], [0, 0, 1]], dtype=float)
>>> grid = pv.UnstructuredGrid([4, 0, 1, 2, 3], [pv.CellType.TETRA], points)
>>> grid['temperature'] = np.array([20.0, 25.0, 22.0, 24.0])
>>> grid['temperature']
array([20., 25., 22., 24.])

Plot the grid:

>>> import pyvista_js as pv
>>> import numpy as np
>>> points = np.array([[0, 0, 0], [1, 0, 0], [0, 1, 0], [0, 0, 1]], dtype=float)
>>> grid = pv.UnstructuredGrid([4, 0, 1, 2, 3], [pv.CellType.TETRA], points)
>>> grid.plot()
__init__(cells: ArrayLike, celltypes: ArrayLike, points: ArrayLike) None#

Initialize an UnstructuredGrid.

Methods

__init__(cells, celltypes, points)

Initialize an UnstructuredGrid.

plot([color, opacity, pbr, metallic, roughness])

Plot this grid.

to_scene_data()

Return a JSON-serializable dict describing this grid.

Attributes

bounding_sphere

Compute the radius and center of a bounding sphere.

n_cells

Return the number of cells.

n_points

Return the number of points.

point_data

Access point data arrays.

property bounding_sphere: tuple[float, tuple[float, float, float]]#

Compute the radius and center of a bounding sphere.

Uses Ritter’s algorithm to approximate the minimum bounding sphere. Returns NaN values if there are no points.

Returns:

Sphere radius as a float and center as a tuple of floats (x, y, z).

Return type:

float, tuple

property n_cells: int#

Return the number of cells.

Returns:

Number of cells in the grid.

Return type:

int

Examples

>>> import numpy as np
>>> import pyvista_js as pv
>>> points = np.array([[0, 0, 0], [1, 0, 0], [0, 1, 0], [0, 0, 1]], dtype=float)
>>> grid = pv.UnstructuredGrid([4, 0, 1, 2, 3], [pv.CellType.TETRA], points)
>>> grid.n_cells
1
>>> # Multiple cells
>>> import numpy as np
>>> import pyvista_js as pv
>>> points = np.array([[0, 0, 0], [1, 0, 0], [0, 1, 0], [0, 0, 1]], dtype=float)
>>> cells = [3, 0, 1, 2, 4, 1, 2, 3, 0]  # triangle + quad
>>> celltypes = [pv.CellType.TRIANGLE, pv.CellType.QUAD]
>>> grid = pv.UnstructuredGrid(cells, celltypes, points[:4])
>>> grid.n_cells
2
property n_points: int#

Return the number of points.

Returns:

Number of points in the grid.

Return type:

int

Examples

>>> import numpy as np
>>> import pyvista_js as pv
>>> points = np.array([[0, 0, 0], [1, 0, 0], [0, 1, 0], [0, 0, 1]], dtype=float)
>>> grid = pv.UnstructuredGrid([4, 0, 1, 2, 3], [pv.CellType.TETRA], points)
>>> grid.n_points
4
plot(color: str | tuple[float, float, float] | None = None, opacity: float = 1.0, pbr: bool = False, metallic: float = 0.0, roughness: float = 0.5) None#

Plot this grid.

This is a convenience method that creates a Plotter, adds this grid, and calls show().

Parameters:
  • color (str or tuple, optional) – Color of the mesh.

  • opacity (float, optional) – Opacity, between 0 and 1. Default is 1.

  • pbr (bool, optional) – Enable physically based rendering. Default is False.

  • metallic (float, optional) – Metallic factor for PBR. Default is 0.0.

  • roughness (float, optional) – Roughness factor for PBR. Default is 0.5.

Examples

>>> import pyvista_js as pv
>>> import numpy as np
>>> points = np.array([[0, 0, 0], [1, 0, 0], [0, 1, 0], [0, 0, 1]], dtype=float)
>>> grid = pv.UnstructuredGrid([4, 0, 1, 2, 3], [pv.CellType.TETRA], points)
>>> grid.plot()
property point_data: PointData#

Access point data arrays.

Returns:

Dict-like container for point data arrays.

Return type:

PointData

to_scene_data() dict[str, object]#

Return a JSON-serializable dict describing this grid.

The 3-D cells are decomposed into their surface polygons so that they can be rendered as a "mesh" source in vtk.js.

Returns:

Source configuration with "type": "mesh".

Return type:

dict