{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%gui qt" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n# Clipping planes with volume and markers\nControls:\n- x/y/z/o - add new clipping plane with normal along x/y/z or [1,1,1] oblique axis\n- r - remove a clipping plane\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import numpy as np\n\nfrom vispy import app, scene, io\nfrom vispy.visuals.filters.clipping_planes import PlanesClipper\n\n# Prepare canvas\ncanvas = scene.SceneCanvas(keys='interactive', size=(800, 600), show=True)\n\n# Set up a viewbox to display the image with interactive pan/zoom\nview = canvas.central_widget.add_view()\n\n# Create the visuals\nvol = np.load(io.load_data_file('volume/stent.npz'))['arr_0']\nvolume = scene.visuals.Volume(vol, parent=view.scene, threshold=0.225)\n\nnp.random.seed(1)\npoints = np.random.rand(100, 3) * (128, 128, 128)\nmarkers = scene.visuals.Markers(pos=points, parent=view.scene)\n# add a transform to markers, to show clipping is in scene coordinates\nmarkers.transform = scene.STTransform(translate=(0, 0, 128))\n\n# Create the clipping planes filter for the markers (Volume has its own clipping logic)\nclipper = PlanesClipper()\n# and attach it to the markers\nmarkers.attach(clipper)\n\n# Create and set the camera\nfov = 60.\ncam = scene.cameras.TurntableCamera(\n parent=view.scene,\n fov=fov,\n name='Turntable'\n)\nview.camera = cam\n\n\n# since volume data is in 'zyx' coordinates, we have to reverse the coordinates\n# we use as a center\nvolume_center = (np.array(vol.shape) / 2)[::-1]\n\n# clipping planes around the origin\nclip_modes = {\n 'x': np.array([[volume_center, [1, 0, 0]]]),\n 'y': np.array([[volume_center, [0, 1, 0]]]),\n 'z': np.array([[volume_center, [0, 0, 1]]]),\n 'o': np.array([[volume_center, [1, 1, 1]]]),\n}\n\n\ndef add_clip(mode):\n if mode not in clip_modes:\n return\n clipping_planes = np.concatenate([volume.clipping_planes, clip_modes[mode]])\n volume.clipping_planes = clipping_planes\n clipper.clipping_planes = clipping_planes\n\n\ndef remove_clip():\n if volume.clipping_planes.shape[0] > 0:\n volume.clipping_planes = volume.clipping_planes[:-1]\n clipper.clipping_planes = clipper.clipping_planes[:-1]\n\n\n# Implement key presses\n@canvas.events.key_press.connect\ndef on_key_press(event):\n if event.text in 'xyzo':\n add_clip(event.text)\n elif event.text == 'r':\n remove_clip()\n\n\nif __name__ == '__main__':\n print(__doc__)\n app.run()" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.19" } }, "nbformat": 4, "nbformat_minor": 0 }