.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "_gallery/feature_demo/multi_slice2.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr__gallery_feature_demo_multi_slice2.py: Mesh and Volume Slicing 2 ========================= Slice a volume and a mesh through the three primary planes (XY, XZ, YZ). This example uses Volume object with a VolumeSliceMaterial, which produces an implicit geometry defined by the volume data. See multi_slice1.py for a more generic approach. .. GENERATED FROM PYTHON SOURCE LINES 11-79 .. image-sg:: /_gallery/feature_demo/images/sphx_glr_multi_slice2_001.webp :alt: multi slice2 :srcset: /_gallery/feature_demo/images/sphx_glr_multi_slice2_001.webp :class: sphx-glr-single-img .. code-block:: Python from time import time import imageio.v3 as iio import numpy as np from rendercanvas.auto import RenderCanvas, loop import pygfx as gfx from skimage.measure import marching_cubes import pylinalg as la canvas = RenderCanvas() renderer = gfx.renderers.WgpuRenderer(canvas) scene = gfx.Scene() dark_gray = np.array((169, 167, 168, 255)) / 255 light_gray = np.array((100, 100, 100, 255)) / 255 background = gfx.Background.from_color(light_gray, dark_gray) scene.add(background) scene.add(gfx.AxesHelper(size=50)) vol = iio.imread("imageio:stent.npz").astype("float32") tex = gfx.Texture(vol, dim=3) surface = marching_cubes(vol[0:], 200) geo = gfx.Geometry( positions=np.fliplr(surface[0]), indices=surface[1], normals=surface[2] ) mesh = gfx.Mesh( geo, gfx.MeshSliceMaterial(plane=(0, 0, -1, vol.shape[0] / 2), color=(1, 1, 0, 1)) ) scene.add(mesh) planes = [] for dim in [0, 1, 2]: # xyz abcd = [0, 0, 0, 0] abcd[dim] = -1 abcd[-1] = vol.shape[2 - dim] / 2 material = gfx.VolumeSliceMaterial(clim=(0, 2000), plane=abcd) plane = gfx.Volume(gfx.Geometry(grid=tex), material) planes.append(plane) scene.add(plane) camera = gfx.PerspectiveCamera(50) camera.show_object(scene, (-1, -1, -1), up=(0, 0, 1)) controller = gfx.OrbitController(camera, register_events=renderer) # Add a slight tilt. This is to show that the slices are still orthogonal # to the world coordinates. for ob in [*planes, mesh]: ob.local.rotation = la.quat_from_axis_angle((1, 0, 0), 0.1) def animate(): t = np.cos(time() / 2) * 0.5 + 0.5 # 0..1 planes[2].material.plane = 0, 0, -1, t * vol.shape[0] mesh.material.plane = 0, 0, -1, (1 - t) * vol.shape[0] renderer.render(scene, camera) canvas.request_draw() if __name__ == "__main__": canvas.request_draw(animate) loop.run() .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.905 seconds) .. _sphx_glr_download__gallery_feature_demo_multi_slice2.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: multi_slice2.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: multi_slice2.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: multi_slice2.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_ .. only:: html Interactive example ------------------- Try this example in your browser using Pyodide. Might not work with all examples and all devices. Check the output and your browser's console for details. .. raw:: html