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.
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:
pip install "composeui[web,mesh]" # the web
pip install "composeui[pyside6,vtk]" # Qt
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.
cellsmaps eachCellTypeto 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 areLINE,TRIANGLE,QUAD,TETRA,PYRAMID,WEDGEandHEXAHEDRON. 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
Meshtomesh.mesh.bounds()gives the extent of its points, andmesh.group_cells(names)the numbers of the cells in the groupsnames.
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:
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))
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 ameshlane.Mesh, and also ameshio.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 aMEDFileUMeshor aMEDCouplingUMesh. AMEDFileUMeshgives 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 aMEDCouplingCMeshor aMEDFileCMesh, 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 mefikitUMesh, with its groups. mefikit has no pyramids (pip install mefikit).PyVista.
Mesh.from_pyvista(grid, groups=())takes anUnstructuredGrid, or another dataset, such as aPolyDataor anImageData, which becomes anUnstructuredGridfirst. PyVista has no groups: each name ofgroupsnames a boolean array ofgrid.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.0andMesh.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, withgrid.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_groupsis 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_cellssets 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.SHOWNdraws them in an opaque grey, andOtherCells.HIDDENleaves them out: the groups show alone. The view doesn’t draw the edges of these cells.Edges.
edgesisTrueby default: the view draws the edges of the cells. Set it toFalsefor a large mesh, whose edges hide its shape.Automatic fit.
cameraisNoneby 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
camerato aMeshCamera(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_upmust point across the line frompositiontofocal_point: for a camera above the mesh, give (0, 1, 0). A new mesh then keeps the camera. To fit the mesh again, setcameratoNone.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,
cameraholds the user’s camera andcamera_changedemits, with no arguments. When code setscamera, the signal doesn’t emit. A disabled view doesn’t take the user’s moves.Height.
heightis in pixels, 400 by default.State. The study saves
camera,shown_groupsandother_cells, and a savedNonefits the mesh again. The study doesn’t savemesh,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
vtkextra. It draws on Linux, with X or Wayland, on Windows and on macOS. On a Qt platform without OpenGL windows, such asoffscreenfor tests, or without VTK, the view shows its title and itsdescription, 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_groupsorother_cellschanges. 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.
The Cantilever beam example,
beam.py, shows a beam’s groups, its other cells and its camera,
picked from a sidebar.