From 5608be3b80fc82da1c64298cd51ab2422f82f7cf Mon Sep 17 00:00:00 2001 From: maharshigor Date: Fri, 20 Dec 2024 10:35:24 +0530 Subject: [PATCH] RF: Updated viz examples. - Updated the examples to work properly after actor addition. - Updated the imports for the show manager, scene and io. - Removed v2 refernce from import. - Removed the old trial examples. --- docs/examples/example.ipynb | 150 --------------------------- docs/examples/example.py | 33 ------ docs/examples/example_offscreen.py | 23 ---- docs/examples/example_streamlines.py | 79 -------------- docs/examples/example_ui.py | 37 ------- docs/examples/viz_display.py | 25 +++-- docs/examples/viz_multi_screen.py | 41 ++++++-- docs/examples/viz_skybox.py | 21 ++-- fury/lib.py | 1 + 9 files changed, 63 insertions(+), 347 deletions(-) delete mode 100644 docs/examples/example.ipynb delete mode 100644 docs/examples/example.py delete mode 100644 docs/examples/example_offscreen.py delete mode 100644 docs/examples/example_streamlines.py delete mode 100644 docs/examples/example_ui.py diff --git a/docs/examples/example.ipynb b/docs/examples/example.ipynb deleted file mode 100644 index f58670335..000000000 --- a/docs/examples/example.ipynb +++ /dev/null @@ -1,150 +0,0 @@ -{ - "cells": [ - { - "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [ - { - "data": { - "application/vnd.jupyter.widget-view+json": { - "model_id": "58f700bf851b496c9cb0a622f1f276c3", - "version_major": 2, - "version_minor": 0 - }, - "text/plain": [ - "RFBOutputContext()" - ] - }, - "metadata": {}, - "output_type": "display_data" - }, - { - "data": { - "application/vnd.jupyter.widget-view+json": { - "model_id": "a3ae7c589bac4f1b9f2819381edb769d", - "version_major": 2, - "version_minor": 0 - }, - "text/html": [ - "
snapshot
" - ], - "text/plain": [ - "JupyterWgpuCanvas()" - ] - }, - "execution_count": 1, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "from fury.furyV2.window import ShowManager\n", - "import pygfx as gfx\n", - "\n", - "show_m = ShowManager(is_jupyter=True)\n", - "\n", - "cube = gfx.Mesh(\n", - " gfx.box_geometry(100, 100, 100),\n", - " gfx.MeshPhongMaterial(color=\"red\", pick_write=True),\n", - ")\n", - "\n", - "show_m.scene.add(cube)\n", - "\n", - "\n", - "def screenshot(event):\n", - " show_m.snapshot(\"snapshot.png\")\n", - "\n", - "\n", - "cube.add_event_handler(screenshot, \"click\")\n", - "\n", - "if __name__ == \"__main__\":\n", - " show_m.render()\n", - " # show_m.start()\n", - "\n", - "show_m.canvas" - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "" - ] - }, - "execution_count": 4, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "cube2 = gfx.Mesh(\n", - " gfx.box_geometry(150, 100, 50),\n", - " gfx.MeshPhongMaterial(color=\"blue\", pick_write=True),\n", - ")\n", - "\n", - "show_m.scene.add(cube2)" - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "metadata": {}, - "outputs": [ - { - "data": { - "application/vnd.jupyter.widget-view+json": { - "model_id": "42d5ebb06d644dd59db048ee7851ec4a", - "version_major": 2, - "version_minor": 0 - }, - "text/html": [ - "
snapshot
" - ], - "text/plain": [ - "JupyterWgpuCanvas(frame_feedback={'index': 227, 'timestamp': 1724433677.834229, 'localtime': 1724433677.874}, …" - ] - }, - "execution_count": 5, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "show_m.canvas" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 3 (ipykernel)", - "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.11.8" - } - }, - "nbformat": 4, - "nbformat_minor": 4 -} diff --git a/docs/examples/example.py b/docs/examples/example.py deleted file mode 100644 index d0de33ae0..000000000 --- a/docs/examples/example.py +++ /dev/null @@ -1,33 +0,0 @@ -from fury.v2.window import ShowManager -from fury.v2.actor import sphere, points -# import pygfx as gfx - -show_m = ShowManager() - -# cube = gfx.Mesh( -# gfx.box_geometry(100, 100, 100), -# gfx.MeshPhongMaterial(color="red", pick_write=True), -# ) - -s = sphere(15, color=(1, 0, 1, 1), position=(25, 25, 25)) - -point_cloud = points( - 3, - point_positions=[(5, -5, 5), (-5, 5, 5), (5, 5, -5)], - colors=[(1, 1, 1, 1), (1, 1, 0, 1), (1, 0, 0, 1)], -) - -# show_m.scene.add(cube) -show_m.scene.add(s) -show_m.scene.add(point_cloud) - - -# def screenshot(event): -# show_m.snapshot('snapshot.png') - - -# cube.add_event_handler(screenshot, "click") - -if __name__ == "__main__": - show_m.render() - show_m.start() diff --git a/docs/examples/example_offscreen.py b/docs/examples/example_offscreen.py deleted file mode 100644 index 8f29db367..000000000 --- a/docs/examples/example_offscreen.py +++ /dev/null @@ -1,23 +0,0 @@ -from fury.v2.windowed import ShowManager -import pygfx as gfx - -show_m = ShowManager(is_offscreen=True) - -cube = gfx.Mesh( - gfx.box_geometry(100, 100, 100), - gfx.MeshPhongMaterial(color="red", pick_write=True), -) - -show_m.scene.add(cube) - - -def screenshot(event): - show_m.snapshot("snapshot.png") - - -# cube.add_event_handler(screenshot, "click") - -if __name__ == "__main__": - show_m.render() - show_m.canvas.draw() - show_m.snapshot("offscreen.png") diff --git a/docs/examples/example_streamlines.py b/docs/examples/example_streamlines.py deleted file mode 100644 index 182fbb1e5..000000000 --- a/docs/examples/example_streamlines.py +++ /dev/null @@ -1,79 +0,0 @@ -import os -import numpy as np -from fury.v2.window import ShowManager -from fury.v2.actor import lines - -from dipy.io.streamline import load_tractogram -from fury.colormap import distinguishable_colormap - -fname = os.path.expanduser( - "~/.dipy/bundle_atlas_hcp842/Atlas_80_Bundles/bundles/AC.trk" -) - -sft = load_tractogram(fname, "same", bbox_valid_check=False) -streamlines = sft.streamlines - -# Pygfx window setup -show_m = ShowManager() -# renderer.blend_mode = 'weighted' - -# Pygfx line porting -nan_buffer = np.array([[np.nan, np.nan, np.nan]], dtype=np.float32) -pygfx_streamlines = streamlines._data.copy().astype(np.float32) -pygfx_offsets = streamlines._offsets.copy().astype(np.float32) -pygfx_lengths = streamlines._lengths.copy().astype(np.float32) - -color_gen = distinguishable_colormap() - -no_streamlines = len(pygfx_offsets) -no_vertices = len(pygfx_streamlines) + no_streamlines - -pygfx_colors = np.zeros((no_vertices, 4), dtype=np.float32) - -for i in range(no_streamlines): - color = next(color_gen) - start_idx = int(pygfx_offsets[i] + i) - end_idx = int(start_idx + pygfx_lengths[i]) - pygfx_streamlines = np.insert(pygfx_streamlines, end_idx, nan_buffer, axis=0) - pygfx_colors[start_idx:end_idx] = (*color, 1) - -# Pygfx line definition -lines = lines(positions=pygfx_streamlines, colors=pygfx_colors, color_mode="vertex") - -show_m.scene.add(lines) - -# # Pygfx camera setup -# camera = gfx.PerspectiveCamera(100, 16 / 9) -# camera.local.position = (100, 100, 50) -# camera.show_pos((0, 0, 0)) -# controller = gfx.OrbitController(camera, register_events=renderer) - - -@lines.add_event_handler("pointer_down") -def on_pick(event): - vertex = event.pick_info["vertex_index"] - print("Vertex : ", vertex) - print("Vertex Coord: ", pygfx_streamlines[vertex]) - selected = find_line(vertex) - color = pygfx_colors[selected[0]][:3] - pygfx_colors[selected[0] : selected[1] + 1] = (*color, 0.5) - lines.geometry.colors.update_range() - show_m.update() - - -def find_line(vertex): - left = right = vertex - - while not np.isnan(pygfx_streamlines[left][0]): - left -= 1 - - while not np.isnan(pygfx_streamlines[right][0]): - right += 1 - - print(left, right) - return [left + 1, right - 1] - - -if __name__ == "__main__": - show_m.render() - show_m.start() diff --git a/docs/examples/example_ui.py b/docs/examples/example_ui.py deleted file mode 100644 index 3f83afb31..000000000 --- a/docs/examples/example_ui.py +++ /dev/null @@ -1,37 +0,0 @@ -from fury.v2.window import ShowManager -from fury.v2.ui import Panel2D -from fury.v2.actor import sphere -import pygfx as gfx - - -show_m = ShowManager() - -s = sphere(15, color=(1, 0, 1, 1), position=(25, 25, 25)) -show_m.scene.add(s) -# show_m.scene.add(cube1) - -panel = Panel2D((200, 50)) -panel.add_to_scene(show_m.scene) -panel.register_events(panel.obj) - - -def clicked(event): - print(show_m.scene.get_bounding_box()) - print("I am clicked on external") - - -# panel.obj.add_event_handler(clicked, 'pointer_down') -show_m.scene.add(panel.obj) -panel.obj.handle_event(gfx.PointerEvent(x=10, y=10, type="pointer_down")) - - -# geo = gfx.plane_geometry(200, 50) -# mat = gfx.MeshPhongMaterial(color=(255, 255, 0, 1), pick_write=True) -# obj = gfx.Mesh(geo, mat) -# obj.add_event_handler(clicked, 'pointer_down') -# show_m.scene.add(obj) - - -if __name__ == "__main__": - show_m.render() - show_m.start() diff --git a/docs/examples/viz_display.py b/docs/examples/viz_display.py index cd58350ad..0a9682b9e 100644 --- a/docs/examples/viz_display.py +++ b/docs/examples/viz_display.py @@ -1,16 +1,23 @@ -from fury.v2.window import display, record -from fury.v2.actor import sphere +import numpy as np -sphere_actor0 = sphere(15, color=(1, 0, 0, 1), position=(15, 0, 0)) -sphere_actor1 = sphere(15, color=(0, 1, 0, 1), position=(0, 15, 0)) -sphere_actor2 = sphere(15, color=(0, 0, 1, 1), position=(0, 0, 15)) +from fury.window import display, snapshot +from fury.actor import sphere + +############################################################################### +# Let's create sphere actor to add three spheres to display. + +sphere_actor = sphere( + np.asarray([(15, 0, 0), (0, 15, 0), (0, 0, 15)]).reshape((3, 3)), + radii=15, + colors=np.asarray([(1, 0, 0), (0, 1, 0), (0, 0, 1)]).reshape((3, 3)), + phi=48, + theta=48, +) interactive = False if __name__ == "__main__": if interactive: - display(actors=(sphere_actor0, sphere_actor1, sphere_actor2)) + display(actors=[sphere_actor]) else: - record( - actors=(sphere_actor0, sphere_actor1, sphere_actor2), fname="display.png" - ) + snapshot(actors=[sphere_actor], fname="display.png") diff --git a/docs/examples/viz_multi_screen.py b/docs/examples/viz_multi_screen.py index cdc119ec0..b708e9e8b 100644 --- a/docs/examples/viz_multi_screen.py +++ b/docs/examples/viz_multi_screen.py @@ -1,7 +1,9 @@ -from fury.v2.window import ShowManager, Scene, record -from fury.v2.actor import sphere +import numpy as np + +from fury.window import ShowManager, Scene, snapshot +from fury.actor import sphere from fury.data import read_viz_cubemap, fetch_viz_cubemaps -from fury.v2.io import load_cube_map_texture +from fury.io import load_cube_map_texture ############################################################################### @@ -29,11 +31,34 @@ scene0 = Scene(skybox=cube_map) scene1 = Scene(background=(1, 1, 1, 1)) -scene2 = Scene(background=(1, 0, 1, 1)) +scene2 = Scene(background=(1, 0, 0, 1)) + +############################################################################### +# Let's create three different sphere actors to add to respective scenes. +# Note: Adding same actor to multiple scenes will not work and only add to the +# last scene that got the actor added to it. -sphere_actor0 = sphere(15, color=(1, 0, 0, 1), position=(0, 0, 0)) -sphere_actor1 = sphere(15, color=(1, 0, 0, 1), position=(0, 0, 0)) -sphere_actor2 = sphere(15, color=(1, 0, 0, 1), position=(0, 0, 0)) +sphere_actor0 = sphere( + np.zeros((1, 3)), + colors=(1, 0, 1, 1), + radii=15.0, + phi=48, + theta=48, +) +sphere_actor1 = sphere( + np.zeros((1, 3)), + colors=(1, 0, 1, 1), + radii=15.0, + phi=48, + theta=48, +) +sphere_actor2 = sphere( + np.zeros((1, 3)), + colors=(1, 0, 1, 1), + radii=15.0, + phi=48, + theta=48, +) scene0.add(sphere_actor0) scene1.add(sphere_actor1) @@ -51,7 +76,7 @@ ) show_m.start() else: - record( + snapshot( scene=[scene0, scene1, scene2], fname="multi_screen.png", screen_config=[2, 1], diff --git a/docs/examples/viz_skybox.py b/docs/examples/viz_skybox.py index 0eedb4c6b..b39ecf3e8 100644 --- a/docs/examples/viz_skybox.py +++ b/docs/examples/viz_skybox.py @@ -1,7 +1,9 @@ -from fury.v2.window import ShowManager, Scene, record -from fury.v2.actor import sphere +import numpy as np + +from fury.window import ShowManager, Scene, snapshot +from fury.actor import sphere from fury.data import read_viz_cubemap, fetch_viz_cubemaps -from fury.v2.io import load_cube_map_texture +from fury.io import load_cube_map_texture ############################################################################### @@ -29,11 +31,14 @@ scene = Scene(skybox=cube_map) -sphere_actor = sphere(15, color=(1, 0, 1, 1), position=(0, 0, 0)) -sphere_actor1 = sphere(15, color=(1, 0, 1, 1), position=(10, 10, 10)) +sphere_actor = sphere( + np.zeros((1, 3)), + colors=(1, 0, 1, 1), + radii=15.0, + phi=48, + theta=48, +) scene.add(sphere_actor) -scene.add(sphere_actor1) - interactive = False @@ -42,4 +47,4 @@ show_m = ShowManager(scene=scene, title="FURY 2.0: Skybox Example") show_m.start() else: - record(scene=scene, fname="skybox.png") + snapshot(scene=scene, fname="skybox.png") diff --git a/fury/lib.py b/fury/lib.py index 98872643c..77ef37b89 100644 --- a/fury/lib.py +++ b/fury/lib.py @@ -34,5 +34,6 @@ run = run Canvas = WgpuCanvas OffscreenCanvas = OffscreenWgpuCanvas +JupyterCanvas = None if have_jupyter_rfb: JupyterCanvas = JupyterWgpuCanvas