# Meshes A `MeshView` shows a 3D mesh that the user rotates, pans and zooms, for example the mesh of a part before a simulation, with the groups where it is fixed and where a load applies. For a diagram in the plane, use a [`DrawingView`](widgets.md#drawing). These sections cover the mesh and its groups, the view's camera, and what each backend draws. The view needs NumPy, which the `mesh` extra installs. On Qt, install the `vtk` extra instead, which installs NumPy and VTK. Add it to the extra of your backend: ```bash pip install "composeui[web,mesh]" # the web pip install "composeui[pyside6,vtk]" # Qt ``` ```python import numpy as np from composeui.core.view import ( CellType, MainView, Mesh, MeshView, OtherCells, child, define_view, ) def beam(length: int) -> Mesh: """Get a beam of `length` unit cubes along x, its ends in the groups "fixed" and "load".""" x, y, z = np.meshgrid(np.arange(length + 1), [0, 1], [0, 1], indexing="ij") points = np.column_stack([x.ravel(), y.ravel(), z.ravel()]) first = 4 * np.arange(length)[:, None] # the point (x, 0, 0) of each cube corners = np.array([0, 4, 6, 2, 1, 5, 7, 3]) # a hexahedron's corners, in VTK's order return Mesh( points, {CellType.HEXAHEDRON: first + corners}, {"fixed": [0], "load": [length - 1]} ) @define_view class BeamMainView(MainView): app_title = "Beam" beam: MeshView = child( MeshView, title="Beam", description="A beam of 10 cubes, fixed at its left end", mesh=beam(10), shown_groups=["fixed", "load"], other_cells=OtherCells.TRANSPARENT, ) ``` ## The mesh and its groups - **Points.** `Mesh(points, cells, groups)` takes the points as an array of shape (n, 3), or (n, 2) for a mesh in the plane z = 0. A point that is not finite raises an error. - **Cells.** `cells` maps each `CellType` to an array of shape (m, k): a row per cell, with the indices of its k points in the order of VTK and meshio. The kinds are `LINE`, `TRIANGLE`, `QUAD`, `TETRA`, `PYRAMID`, `WEDGE` and `HEXAHEDRON`. An index that isn't an integer, or that names no point, raises an error. - **Wedges.** The first triangle of a wedge turns toward its other triangle, by the right-hand rule, as in meshio and VTK 9.7. VTK 9.6, the last version for Python 3.9, turns a wedge the other way. - **Groups.** A group maps its name to the numbers of its cells, for example the faces where a load applies. The cells are numbered across the kinds, in the order of `cells`: with 10 hexahedra then 4 quads, the quads are the cells 10 to 13. A number that names no cell raises an error. - **A new mesh.** The mesh copies the arrays and makes them read-only. To change the mesh, assign a new `Mesh` to `mesh`. `mesh.bounds()` gives the extent of its points, and `mesh.group_cells(names)` the numbers of the cells in the groups `names`. ## Meshes from other libraries A class method of `Mesh` makes a mesh of the mesh of another library, with its groups of cells. ComposeUI doesn't install these libraries: install the one that your app uses. For example, read a MED file, the format of Code_Aster, with meshlane or with MEDCoupling: ```python import meshlane from composeui.core.view import Mesh def read_part(path: str) -> Mesh: """Get the mesh of a file, its cell sets as groups.""" return Mesh.from_meshlane(meshlane.read(path)) ``` ```python from medcoupling import MEDFileUMesh from composeui.core.view import Mesh def read_part(path: str) -> Mesh: """Get the mesh of a MED file, the faces and the groups of each level too.""" return Mesh.from_medcoupling(MEDFileUMesh.New(path)) ``` - **meshlane and meshio.** `Mesh.from_meshlane(mesh)` takes a `meshlane.Mesh`, and also a `meshio.Mesh`: meshlane continues meshio. Its cell sets become the groups. `meshlane.read(path)` reads the formats of meshio, and MED files with h5py (`pip install "meshlane[all]"`). - **MEDCoupling.** `Mesh.from_medcoupling(mesh)` takes a `MEDFileUMesh` or a `MEDCouplingUMesh`. A `MEDFileUMesh` gives the cells of each of its levels, for example the faces of a volume mesh, and its groups of cells. A structured mesh, such as a `MEDCouplingCMesh` or a `MEDFileCMesh`, becomes unstructured first. MEDCoupling has wheels for Linux on x86-64 and for Windows, up to Python 3.13 (`pip install medcoupling`). - **mefikit.** `Mesh.from_mefikit(umesh)` takes a mefikit `UMesh`, with its groups. mefikit has no pyramids (`pip install mefikit`). - **PyVista.** `Mesh.from_pyvista(grid, groups=())` takes an `UnstructuredGrid`, or another dataset, such as a `PolyData` or an `ImageData`, which becomes an `UnstructuredGrid` first. PyVista has no groups: each name of `groups` names a boolean array of `grid.cell_data`, and the group holds the cells where the array is true. For example, `grid.cell_data["fixed"] = grid.cell_centers().points[:, 0] < 1.0` and `Mesh.from_pyvista(grid, groups=["fixed"])` (`pip install pyvista`). A grid's wedges can follow VTK 9.6 or VTK 9.7: a wedge turned the way of VTK 9.6 is turned over. - **Cells of higher order.** A cell of higher order, such as a quadratic tetrahedron or a Lagrange hexahedron, keeps its corners: the mesh view draws its straight edges. - **Cells left out.** A vertex cell is left out, as it has nothing to draw. A polygon, a polyhedron or another kind of cell raises a `ValueError`. With PyVista, split them into triangles and tetrahedra first, with `grid.triangulate()`. - **Groups of points.** A mesh has groups of cells only, so the groups of points are left out. ## Camera, groups, and edges - **Shown groups.** `shown_groups` is empty by default: the view shows the whole mesh in one color. With names, it shows the cells of these groups, each group in its color, in the order of the list. For example, `shown_groups=["fixed", "load"]` shows the two ends of the beam in color. A name that the mesh doesn't have shows nothing. - **Other cells.** `other_cells` sets how the view draws the cells in none of the shown groups, while groups are shown. `OtherCells.TRANSPARENT`, the default, draws them in a grey that the groups show through, also a group inside the part. `OtherCells.SHOWN` draws them in an opaque grey, and `OtherCells.HIDDEN` leaves them out: the groups show alone. The view doesn't draw the edges of these cells. - **Edges.** `edges` is `True` by default: the view draws the edges of the cells. Set it to `False` for a large mesh, whose edges hide its shape. - **Automatic fit.** `camera` is `None` by default. The view then fits the mesh, seen from the side of +x, -y and +z, or from +z for a flat mesh. It fits again when the mesh changes. - **Your own camera.** Set `camera` to a `MeshCamera(position=..., focal_point=..., view_up=...)`: where the camera stands, the point it looks at, and the direction that is up on screen, (0, 0, 1) by default. `view_up` must point across the line from `position` to `focal_point`: for a camera above the mesh, give (0, 1, 0). A new mesh then keeps the camera. To fit the mesh again, set `camera` to `None`. `MeshView.fitted_camera(mesh)` gives the camera of the automatic fit. - **The user's camera.** The user rotates the mesh with the left button, pans with the middle button, and zooms with the wheel. On the web, the user can also rotate the mesh with the arrow keys and zoom with + and -, once the view has the focus. When the move ends, `camera` holds the user's camera and `camera_changed` emits, with no arguments. When code sets `camera`, the signal doesn't emit. A disabled view doesn't take the user's moves. - **Height.** `height` is in pixels, 400 by default. - **State.** The study saves `camera`, `shown_groups` and `other_cells`, and a saved `None` fits the mesh again. The study doesn't save `mesh`, `edges`, `height`, the title or the description. After a study opens, build the mesh again from the model. ## What each backend draws - **Qt** shows the outer surface of the mesh, which VTK draws in Qt's own OpenGL widget, with the `vtk` extra. It draws on Linux, with X or Wayland, on Windows and on macOS. On a Qt platform without OpenGL windows, such as `offscreen` for tests, or without VTK, the view shows its title and its `description`, and logs a warning. - **The web** shows the same surface, which vtk.js draws in the browser with WebGL. The browser loads vtk.js once the page shows its first mesh. In a browser without WebGL, the view shows its title and its `description`. - **Large meshes on the web.** The server sends the outer surface, not the cells: a block of 1,000,000 hexahedra sends 3.6 MB, 1.1 MB compressed, which the server makes in about 0.6 s. A surface of 1,000,000 triangles, such as a skin mesh, sends about 30 MB. The browser keeps the surface: the server sends it again only when `mesh`, `shown_groups` or `other_cells` changes. Without a GPU, as in a virtual machine, the browser turns such a mesh slowly. - **Textual** doesn't show a `MeshView`. The other views of the app still show. To show a table of the groups instead on a backend without meshes, see [Adapt the views to the backend](running-the-app.md#adapt-the-views-to-the-backend). The [Cantilever beam](../examples-guide.md#cantilever-beam) example, [`beam.py`](../../src/examples/beam.py), shows a beam's groups, its other cells and its camera, picked from a sidebar.