.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "_gallery/feature_demo/multi_slice1.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_slice1.py: Volume and Mesh Slicing 1 ========================= Slice a volume and a mesh through the three primary planes (XY, XZ, YZ). This example uses a mesh object with custom texture coordinates. This is a generic approach. See multi_slice2.py for a simpler way. .. GENERATED FROM PYTHON SOURCE LINES 9-84 .. image-sg:: /_gallery/feature_demo/images/sphx_glr_multi_slice1_001.webp :alt: multi slice1 :srcset: /_gallery/feature_demo/images/sphx_glr_multi_slice1_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 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") / 2000 tex = gfx.Texture(vol, dim=3) material = gfx.MeshBasicMaterial(map=tex) planes = [] texcoords = { 0: [[0.5, 0, 0], [0.5, 1, 0], [0.5, 0, 1], [0.5, 1, 1]], 1: [[0, 0.5, 0], [1, 0.5, 0], [0, 0.5, 1], [1, 0.5, 1]], 2: [[0, 0, 0.5], [1, 0, 0.5], [0, 1, 0.5], [1, 1, 0.5]], } sizes = { 0: (vol.shape[1], vol.shape[0]), # YZ plane 1: (vol.shape[2], vol.shape[0]), # XZ plane 2: (vol.shape[2], vol.shape[1]), # XY plane (default) } for axis in [0, 1, 2]: geometry = gfx.plane_geometry(*sizes[axis], 1, 1) geometry.texcoords = gfx.Buffer(np.array(texcoords[axis], dtype="f4")) plane = gfx.Mesh(geometry, material) planes.append(plane) scene.add(plane) if axis == 0: # YZ plane plane.local.rotation = la.quat_from_euler( (0.5 * np.pi, 0.5 * np.pi), order="XY" ) elif axis == 1: # XZ plane plane.local.rotation = la.quat_from_euler(0.5 * np.pi, order="X") # else: XY plane # camera = gfx.PerspectiveCamera(70, 16 / 9) camera = gfx.PerspectiveCamera(0) camera.world.reference_up = 0, 0, 1 camera.local.position = (200, 200, 200) camera.show_pos((0, 0, 0)) controller = gfx.OrbitController(camera, register_events=renderer) def animate(): t = np.cos(time() / 2) plane = planes[2] plane.local.z = t * vol.shape[0] * 0.5 plane.geometry.texcoords.data[:, 2] = (t + 1) / 2 plane.geometry.texcoords.update_full() 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.310 seconds) .. _sphx_glr_download__gallery_feature_demo_multi_slice1.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: multi_slice1.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: multi_slice1.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: multi_slice1.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