3D mesh plot#
New in version 24.09.2.
This notebook illustrates how to render 3D meshes by supplying a list of vertex positions and vertex indices to construct the mesh faces.
[1]:
import scipp as sc
import plopp as pp
from plopp.data import examples
Loading mesh data#
We load a file containing the data to construct the Utah teapot (see below for a description of the data format).
[2]:
dg = sc.io.load_hdf5(examples.teapot())
dg
Downloading file 'teapot.h5' from 'https://public.esss.dk/groups/scipp/plopp/1/teapot.h5' to '/home/runner/.cache/plopp/1'.
[2]:
- verticesscippVariable(pix: 137)vector3m[ 7. 0. 12.], [ 4.97 -4.97 12. ], ..., [ 2.9288 2.9288 12.75 ], [ 4.615 4.615 12. ]
- facesscippVariable(pix: 720)int64𝟙0, 1, ..., 135, 124
Creating a mesh plot#
We can now send the data to the `mesh3d
<../../generated/plopp.mesh3d.html>`__ function for rendering (we color the mesh according to z position):
[3]:
pp.mesh3d(
vertices=dg["vertices"],
faces=dg["faces"],
vertexcolors=dg["vertices"].fields.z,
)
[3]:
Adding mesh edges#
It is also possible to show the edges of the mesh using the edgecolor
argument:
[4]:
pp.mesh3d(
vertices=dg["vertices"],
faces=dg["faces"],
vertexcolors=dg["vertices"].fields.z,
edgecolor="black",
)
[4]:
The data format#
The data used above contains a list of vertices
(position vectors in 3d space), and a list of faces
which define how the vertices are connected to each other.
The faces is a flat list of sequences of 3 indices that code for vertices which make up mesh triangles.
As an example, we will construct a simple tetrahedric mesh.
[5]:
vertices = sc.vectors(
dims=["vertices"],
values=[[-1, 0, 0], [0.7, 0, 1], [0.7, 0, -1], [0, 1.3, 0]],
unit="m",
)
faces = sc.array(
dims=["faces"],
values=[
# First triangle
0, 1, 3,
# Second triangle
1, 2, 3,
# Third triangle
2, 0, 3,
# Fourth triangle
0, 2, 1,
],
)
pp.mesh3d(
vertices=vertices,
faces=faces,
edgecolor="black",
)
[5]:
You can then also add colors on the vertices:
[6]:
pp.mesh3d(
vertices=vertices,
faces=faces,
vertexcolors=vertices.fields.x,
edgecolor="black",
)
[6]: