| 1 |
""" |
| 2 |
.. _glyph_table_example: |
| 3 |
|
| 4 |
Table of Glyphs |
| 5 |
~~~~~~~~~~~~~~~ |
| 6 |
|
| 7 |
``vtk`` supports tables of glyphs from which glyphs are looked |
| 8 |
up. This example demonstrates this functionality. |
| 9 |
""" |
| 10 |
|
| 11 |
import numpy as np |
| 12 |
import pyvista as pv |
| 13 |
|
| 14 |
# %% |
| 15 |
# |
| 16 |
# We can allow tables of glyphs in a backward-compatible way by |
| 17 |
# allowing a sequence of geometries as well as single (scalar) |
| 18 |
# geometries to be passed as the ``geom`` kwarg of :func:`pyvista.DataSetFilters.glyph`. |
| 19 |
# An ``indices`` optional keyword specifies the index of each glyph geometry in |
| 20 |
# the table, and it has to be the same length as ``geom`` if specified. If it is |
| 21 |
# absent a default value of ``range(len(geom))`` is assumed. |
| 22 |
|
| 23 |
# sphinx_gallery_start_ignore |
| 24 |
# interactive plot has wrong lighting |
| 25 |
PYVISTA_GALLERY_FORCE_STATIC_IN_DOCUMENT = True |
| 26 |
# sphinx_gallery_end_ignore |
| 27 |
|
| 28 |
# get dataset for the glyphs: supertoroids in xy plane |
| 29 |
# use N random kinds of toroids over a mesh with 27 points |
| 30 |
N = 5 |
| 31 |
values = np.arange(N) # values for scalars to look up glyphs by |
| 32 |
|
| 33 |
|
| 34 |
# taken from: |
| 35 |
# rng = np.random.default_rng() |
| 36 |
# params = rng.uniform(0.5, 2, size=(N, 2)) # (n1, n2) parameters for the toroids |
| 37 |
params = np.array( |
| 38 |
[ |
| 39 |
[1.56821334, 0.99649769], |
| 40 |
[1.08247844, 1.83758874], |
| 41 |
[1.49598881, 0.83495047], |
| 42 |
[1.52442129, 0.89600688], |
| 43 |
[1.92212387, 0.78096621], |
| 44 |
], |
| 45 |
) |
| 46 |
|
| 47 |
geoms = [pv.ParametricSuperToroid(n1=n1, n2=n2) for n1, n2 in params] |
| 48 |
|
| 49 |
# get dataset where to put glyphs |
| 50 |
x, y, z = np.mgrid[:3.0, :3.0, :3.0] |
| 51 |
mesh = pv.StructuredGrid(x, y, z) |
| 52 |
|
| 53 |
# add random scalars |
| 54 |
# rng_int = rng.integers(0, N, size=x.size) |
| 55 |
rng_int = np.array( |
| 56 |
[4, 1, 2, 0, 4, 0, 1, 4, 3, 1, 1, 3, 3, 4, 3, 4, 4, 3, 3, 2, 2, 1, 1, 1, 2, 0, 3], |
| 57 |
) |
| 58 |
mesh.point_data['scalars'] = rng_int |
| 59 |
|
| 60 |
# construct the glyphs on top of the mesh; don't scale by scalars now |
| 61 |
glyphs = mesh.glyph( |
| 62 |
geom=geoms, indices=values, scale=False, factor=0.3, rng=(0, N - 1), orient=False |
| 63 |
) |
| 64 |
|
| 65 |
# create plotter and add our glyphs with some nontrivial lighting |
| 66 |
pl = pv.Plotter() |
| 67 |
pl.add_mesh( |
| 68 |
glyphs, specular=1, specular_power=15, smooth_shading=True, show_scalar_bar=False |
| 69 |
) |
| 70 |
pl.show() |
| 71 |
# %% |
| 72 |
# .. tags:: filter |