{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%gui qt" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n# RGB Volume Rendering\n\nDemonstrates rendering a volume with RGB data, where the color is encoded\ndirectly in the data rather than mapped through a colormap.\n\nThree overlapping Gaussian blobs (red, green, blue) are rendered using\ndifferent volume rendering methods.\n\nControls:\n\n* 1 - toggle between volume rendering methods\n* 2 - toggle between turntable and arcball 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\n\n\ndef make_rgb_volume(shape=(80, 80, 80)):\n \"\"\"Create a volume with three colored Gaussian blobs.\"\"\"\n vol = np.zeros(shape + (3,), dtype=np.float32)\n zz, yy, xx = np.mgrid[:shape[0], :shape[1], :shape[2]]\n cx, cy, cz = [s // 2 for s in shape]\n sigma = min(shape) / 4\n\n # Red blob offset in +x\n d2 = (xx - cx - 10) ** 2 + (yy - cy) ** 2 + (zz - cz) ** 2\n vol[..., 0] = np.exp(-d2 / (2 * sigma ** 2))\n\n # Green blob offset in +y\n d2 = (xx - cx) ** 2 + (yy - cy - 10) ** 2 + (zz - cz) ** 2\n vol[..., 1] = np.exp(-d2 / (2 * sigma ** 2))\n\n # Blue blob offset in +z\n d2 = (xx - cx) ** 2 + (yy - cy) ** 2 + (zz - cz - 10) ** 2\n vol[..., 2] = np.exp(-d2 / (2 * sigma ** 2))\n\n return vol\n\n\nvol = make_rgb_volume()\n\ncanvas = scene.SceneCanvas(keys='interactive', size=(800, 600), show=True)\ncanvas.measure_fps()\n\nview = canvas.central_widget.add_view()\nvolume = scene.visuals.Volume(vol, parent=view.scene, method='mip')\n\ncam1 = scene.cameras.TurntableCamera(parent=view.scene, fov=60,\n name='Turntable')\ncam2 = scene.cameras.ArcballCamera(parent=view.scene, fov=60,\n name='Arcball')\nview.camera = cam1\n\nmethods = cycle(['mip', 'attenuated_mip', 'translucent', 'additive',\n 'average', 'iso'])\n\n\n@canvas.events.key_press.connect\ndef on_key_press(event):\n if event.text == '1':\n method = next(methods)\n volume.method = method\n print(\"Render method: %s\" % method)\n elif event.text == '2':\n cam_toggle = {cam1: cam2, cam2: cam1}\n view.camera = cam_toggle.get(view.camera, cam1)\n print(view.camera.name + ' camera')\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.12.13" } }, "nbformat": 4, "nbformat_minor": 0 }