{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%gui qt" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n# Aspect Ratios\n\nExample demonstrating the use of aspect ratio, and also the flipping\nof axis using negative aspect ratios.\n\nKeys:\n* 1: flip x dimension\n* 2: flip y dimension\n* 3: flip z dimension\n* 4: cycle through up-vectors\n* 5: cycle through cameras\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "from itertools import cycle\n\nimport numpy as np\n\nfrom vispy import app, scene, io\n\n# Read volume\nvol1 = np.load(io.load_data_file('volume/stent.npz'))['arr_0']\n\n# Prepare canvas\ncanvas = scene.SceneCanvas(keys='interactive', size=(800, 600), show=True)\ncanvas.measure_fps()\n\n# Set up a viewbox to display the image with interactive pan/zoom\nview = canvas.central_widget.add_view()\n\n# Create the volume visuals, only one is visible\nvolume1 = scene.visuals.Volume(vol1, parent=view.scene, threshold=0.5)\n# volume1.method = 'iso'\nvolume1.threshold = 0.1\n\n# Plot a line that shows where positive x is, with at the end a small\n# line pointing at positive y\narr = np.array([(100, -1, -1), (-1, -1, -1), (-1, 10, -1)])\nline1 = scene.visuals.Line(arr, color='red', parent=view.scene)\n\n# Create cameras\ncam1 = scene.cameras.PanZoomCamera(parent=view.scene, aspect=1, name='PanZoom')\ncam2 = scene.cameras.FlyCamera(parent=view.scene, name='Fly')\ncam3 = scene.cameras.TurntableCamera(fov=60, parent=view.scene,\n name='Turntable')\ncam4 = scene.cameras.ArcballCamera(fov=60, parent=view.scene, name='Arcball')\ncams = (cam1, cam2, cam3, cam4)\nview.camera = cam3 # Select turntable at first\n\nups = cycle(('+z', '-z', '+y', '-y', '+x', '-x'))\n\n\n# Implement key presses\n@canvas.events.key_press.connect\ndef on_key_press(event):\n if event.text == '1':\n for cam in cams:\n flip = cam.flip\n cam.flip = not flip[0], flip[1], flip[2]\n elif event.text == '2':\n for cam in cams:\n flip = cam.flip\n cam.flip = flip[0], not flip[1], flip[2]\n elif event.text == '3':\n for cam in cams:\n flip = cam.flip\n cam.flip = flip[0], flip[1], not flip[2]\n elif event.text == '4':\n up = next(ups)\n print('up: ' + up)\n for cam in cams:\n cam.up = up\n if event.text == '5':\n cam_toggle = {cam1: cam2, cam2: cam3, cam3: cam4, cam4: cam1}\n view.camera = cam_toggle.get(view.camera, cam2)\n print(view.camera.name + ' camera')\n elif event.text == '0':\n for cam in cams:\n cam.set_range()\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 }