<jupyter_start><jupyter_text>Marching Cubes {marching_cubes_example}==============Generate a surface from a scalar field using the flying edges andmarching cubes filters as provided by the `contour`{.interpreted-text role="func"} filter.Special thanks to GitHub user [stla](https://gist.github.com/stla) forproviding examples.<jupyter_code>import numpy as np

import pyvista as pv<jupyter_output><empty_output><jupyter_text>Spider Cage===========Use the marching cubes algorithm to extract the isosurface generatedfrom the spider cage function.<jupyter_code>a = 0.9


def spider_cage(x, y, z):
    x2 = x * x
    y2 = y * y
    x2_y2 = x2 + y2
    return (np.sqrt((x2 - y2) ** 2 / x2_y2 + 3 * (z * np.sin(a)) ** 2) - 3) ** 2 + 6 * (
        np.sqrt((x * y) ** 2 / x2_y2 + (z * np.cos(a)) ** 2) - 1.5
    ) ** 2


# create a uniform grid to sample the function with
n = 100
x_min, y_min, z_min = -5, -5, -3
grid = pv.UniformGrid(
    dims=(n, n, n),
    spacing=(abs(x_min) / n * 2, abs(y_min) / n * 2, abs(z_min) / n * 2),
    origin=(x_min, y_min, z_min),
)
x, y, z = grid.points.T

# sample and plot
values = spider_cage(x, y, z)
mesh = grid.contour(1, values, method='marching_cubes', rng=[1, 0])
dist = np.linalg.norm(mesh.points, axis=1)
mesh.plot(scalars=dist, smooth_shading=True, specular=5, cmap="plasma", show_scalar_bar=False)<jupyter_output><empty_output><jupyter_text>Barth Sextic============Use the flying edges algorithm to extract the isosurface generated fromthe Barth sextic function.<jupyter_code>phi = (1 + np.sqrt(5)) / 2
phi2 = phi * phi


def barth_sextic(x, y, z):
    x2 = x * x
    y2 = y * y
    z2 = z * z
    arr = (
        3 * (phi2 * x2 - y2) * (phi2 * y2 - z2) * (phi2 * z2 - x2)
        - (1 + 2 * phi) * (x2 + y2 + z2 - 1) ** 2
    )
    nan_mask = x2 + y2 + z2 > 3.1
    arr[nan_mask] = np.nan
    return arr


# create a uniform grid to sample the function with
n = 100
k = 2.0
x_min, y_min, z_min = -k, -k, -k
grid = pv.UniformGrid(
    dims=(n, n, n),
    spacing=(abs(x_min) / n * 2, abs(y_min) / n * 2, abs(z_min) / n * 2),
    origin=(x_min, y_min, z_min),
)
x, y, z = grid.points.T

# sample and plot
values = barth_sextic(x, y, z)
mesh = grid.contour(1, values, method='flying_edges', rng=[-0.0, 0])
dist = np.linalg.norm(mesh.points, axis=1)
mesh.plot(scalars=dist, smooth_shading=True, specular=5, cmap="plasma", show_scalar_bar=False)<jupyter_output><empty_output><jupyter_text>Animate Barth Sextic====================Show 15 frames of various isocurves extracted from the Barth sexticfunction.<jupyter_code>def angle_to_range(angle):
    return -2 * np.sin(angle)


mesh = grid.contour(1, values, method='flying_edges', rng=[angle_to_range(0), 0])
dist = np.linalg.norm(mesh.points, axis=1)

pl = pv.Plotter()
pl.add_mesh(
    mesh,
    scalars=dist,
    smooth_shading=True,
    specular=5,
    rng=[0.5, 1.5],
    cmap="plasma",
    show_scalar_bar=False,
)
pl.open_gif('barth_sextic.gif')

for angle in np.linspace(0, np.pi, 15)[:-1]:
    new_mesh = grid.contour(1, values, method='flying_edges', rng=[angle_to_range(angle), 0])
    mesh.overwrite(new_mesh)
    pl.update_scalars(np.linalg.norm(new_mesh.points, axis=1), render=False)
    pl.write_frame()

pl.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.16630736064059132, 'fraction_numerical': 0.031105635971666153, 'mean_word_length': 3.811851851851852, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 18, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '516045', 'license': 'MIT', 'n_tokens_mistral': 1383, 'n_tokens_neox': 1315, 'n_words': 322, 'path': 'examples/01-filter/flying_edges.ipynb', 'repo_name': 'vtkiorg/vtki-examples', 'size': '3247'}