Gallery · 3D widgets

A surface you can orbit

A gyroid isosurface and the point cloud tracing it, together in one k3d scene you can orbit, zoom, and take apart object by object. Python built the mesh; the page only draws what the run saved.

gyroid-orbit.verso Python · 5 cells Read-only preview Download

A surface you can orbit

Some Python libraries draw nothing on their own. They hand the notebook a reference to a model and leave the drawing to the front end, which is why a cell ending in plot.display() can print Plot(antialias=3, ...) and stop there.

Verso draws them. The figure below is live: drag to orbit, scroll to zoom, and open the panel in its corner to light it differently or hide part of it. No code runs on this page. Everything you are turning was captured when the notebook was executed.

[1]
codePython
1import k3d
2import numpy as np
3
4# A gyroid is the surface where cos(x)sin(y) + cos(y)sin(z) + cos(z)sin(x) = 0.
5# It splits space into two interlocking halves that never touch each other.
6N = 44
7
8
9def gyroid(n):
10 axis = np.linspace(0, 2 * np.pi, n, dtype=np.float32)
11 x, y, z = np.meshgrid(axis, axis, axis, indexing="ij")
12 return (
13 np.cos(x) * np.sin(y) + np.cos(y) * np.sin(z) + np.cos(z) * np.sin(x)
14 ).astype(np.float32)
15
16
17field = gyroid(N)
18
19# Two readings of one array in a single scene: the surface itself, and the
20# grid samples that land closest to it.
21ix, iy, iz = np.nonzero(np.abs(field) < 0.08)
22points = np.stack([ix, iy, iz], axis=1).astype(np.float32) / (N - 1)
23
24plot = k3d.plot(grid_visible=False, camera_auto_fit=True)
25plot += k3d.marching_cubes(
26 field,
27 level=0.0,
28 color=0x2F6FD0,
29 compression_level=0,
30 xmin=0, xmax=1, ymin=0, ymax=1, zmin=0, zmax=1,
31 name="isosurface",
32)
33plot += k3d.points(
34 points,
35 point_size=0.016,
36 shader="flat",
37 attribute=points[:, 2],
38 color_map=k3d.matplotlib_color_maps.Viridis,
39 compression_level=0,
40 name="sample points",
41)
42
43print(f"{len(points):,} sample points on a {N} by {N} by {N} grid")
44plot.display()
✓completed
4,340 sample points on a 44 by 44 by 44 grid

Both objects are in there

Open Objects in the panel and switch the isosurface off. Underneath it is the point cloud, the same array read a second way: every grid sample whose value sits within 0.08 of zero, colored by height. Turn the mesh back on and the points disappear inside it, because that is exactly where they live.

Flatten the whole thing and you get contours. Below is the identical array cut at three heights, drawn by matplotlib instead.

[2]
codePython
1import matplotlib.pyplot as plt
2
3cube = gyroid(N)
4
5fig, axes = plt.subplots(1, 3, figsize=(10.5, 3.6), constrained_layout=True)
6for ax, k in zip(axes, (0, N // 4, N // 2)):
7 ax.contourf(cube[:, :, k], levels=18, cmap="RdBu_r")
8 ax.contour(cube[:, :, k], levels=[0.0], colors="black", linewidths=1.2)
9 ax.set_title(f"slice at z index {k}", fontsize=10)
10 ax.set_xticks([])
11 ax.set_yticks([])
12plt.show()
✓completed

Make it yours

Download the notebook and change level to cut the field somewhere other than zero. Positive values shrink one half and grow the other until the surface pinches apart completely. N sets the resolution, and it is worth knowing that the mesh travels inside the saved notebook, so a finer grid buys detail with file size.

Two objects share one plot here for the same reason. A widget carries the state of everything live in the session, so a second plot would have shipped this mesh all over again.

The same rendering applies to any library built on ipywidgets, among them ipyleaflet for maps, pythreejs for scenes, and bqplot for charts.

Run it yourself

Nothing on this page is executable; the outputs were baked in by a real run. Download the notebook and it runs anywhere Verso does: VS Code, the browser, or the terminal.

dotnet tool install --global Verso.Cli
verso serve gyroid-orbit.verso

What this shows

A gyroid isosurface and the point cloud tracing it, together in one k3d scene you can orbit, zoom, and take apart object by object. Python built the mesh; the page only draws what the run saved.

Every language in this notebook reads and writes the same variable store, so a value produced in one cell is simply there in the next, whatever the language.