.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/01_introductory/viz_picking.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_auto_examples_01_introductory_viz_picking.py: ===================== Simple picking ===================== Here we present a tutorial showing how to interact with objects in the 3D world. All objects to be picked are part of a single actor. FURY likes to bundle objects in a few actors to reduce code and increase speed. When the objects are picked they will change size and color. .. GENERATED FROM PYTHON SOURCE LINES 13-24 .. code-block:: Python import numpy as np import fury centers = 0.5 * np.array([[0, 0, 0], [100, 0, 0], [200, 0, 0.0]]) colors = np.array([[0.8, 0, 0], [0, 0.8, 0], [0, 0, 0.8]]) radii = 0.1 * np.array([50, 100, 150.0]) selected = np.zeros(3, dtype=bool) .. GENERATED FROM PYTHON SOURCE LINES 25-26 Let's create a panel to show what is picked .. GENERATED FROM PYTHON SOURCE LINES 26-33 .. code-block:: Python panel = fury.ui.Panel2D(size=(400, 200), color=(1, 0.5, 0.0), align="right") panel.center = (150, 200) text_block = fury.ui.TextBlock2D(text="Left click on object \n") panel.add_element(text_block, (0.3, 0.3)) .. GENERATED FROM PYTHON SOURCE LINES 34-35 Build scene and add an actor with many objects. .. GENERATED FROM PYTHON SOURCE LINES 35-40 .. code-block:: Python scene = fury.window.Scene() label_actor = fury.actor.vector_text(text="Test") .. GENERATED FROM PYTHON SOURCE LINES 41-42 This actor is made with 3 cubes of different orientation .. GENERATED FROM PYTHON SOURCE LINES 42-54 .. code-block:: Python directions = np.array( [ [np.sqrt(2) / 2, 0, np.sqrt(2) / 2], [np.sqrt(2) / 2, np.sqrt(2) / 2, 0], [0, np.sqrt(2) / 2, np.sqrt(2) / 2], ] ) fury_actor = fury.actor.cube( centers, directions=directions, colors=colors, scales=radii ) .. GENERATED FROM PYTHON SOURCE LINES 55-56 Access the memory of the vertices of all the cubes .. GENERATED FROM PYTHON SOURCE LINES 56-61 .. code-block:: Python vertices = fury.utils.vertices_from_actor(fury_actor) num_vertices = vertices.shape[0] num_objects = centers.shape[0] .. GENERATED FROM PYTHON SOURCE LINES 62-63 Access the memory of the colors of all the cubes .. GENERATED FROM PYTHON SOURCE LINES 63-66 .. code-block:: Python vcolors = fury.utils.colors_from_actor(fury_actor, array_name="colors") .. GENERATED FROM PYTHON SOURCE LINES 67-68 Adding an actor showing the axes of the world coordinates .. GENERATED FROM PYTHON SOURCE LINES 68-75 .. code-block:: Python ax = fury.actor.axes(scale=(10, 10, 10)) scene.add(fury_actor) scene.add(label_actor) scene.add(ax) scene.reset_camera() .. GENERATED FROM PYTHON SOURCE LINES 76-77 Create the Picking manager .. GENERATED FROM PYTHON SOURCE LINES 77-80 .. code-block:: Python pickm = fury.pick.PickingManager() .. GENERATED FROM PYTHON SOURCE LINES 81-82 Time to make the callback which will be called when we pick an object .. GENERATED FROM PYTHON SOURCE LINES 82-136 .. code-block:: Python def left_click_callback(obj, event): # Get the event position on display and pick event_pos = pickm.event_position(showm.iren) picked_info = pickm.pick(event_pos, showm.scene) vertex_index = picked_info["vertex"] # Calculate the objects index object_index = int(np.floor((vertex_index / num_vertices) * num_objects)) # Find how many vertices correspond to each object sec = int(num_vertices / num_objects) if not selected[object_index]: scale = 6 / 5 color_add = np.array([30, 30, 30], dtype="uint8") selected[object_index] = True else: scale = 5 / 6 color_add = np.array([-30, -30, -30], dtype="uint8") selected[object_index] = False # Update vertices positions vertices[object_index * sec : object_index * sec + sec] = ( scale * ( vertices[object_index * sec : object_index * sec + sec] - centers[object_index] ) + centers[object_index] ) # Update colors vcolors[object_index * sec : object_index * sec + sec] += color_add # Tell actor that memory is modified fury.utils.update_actor(fury_actor) face_index = picked_info["face"] # Show some info text = "Object " + str(object_index) + "\n" text += "Vertex ID " + str(vertex_index) + "\n" text += "Face ID " + str(face_index) + "\n" text += "World pos " + str(np.round(picked_info["xyz"], 2)) + "\n" text += "Actor ID " + str(id(picked_info["actor"])) text_block.message = text showm.render() .. GENERATED FROM PYTHON SOURCE LINES 137-138 Bind the callback to the actor .. GENERATED FROM PYTHON SOURCE LINES 138-141 .. code-block:: Python fury_actor.AddObserver("LeftButtonPressEvent", left_click_callback, 1) .. GENERATED FROM PYTHON SOURCE LINES 142-143 Make the window appear .. GENERATED FROM PYTHON SOURCE LINES 143-148 .. code-block:: Python showm = fury.window.ShowManager(scene=scene, size=(1024, 768), order_transparent=True) scene.add(panel) .. GENERATED FROM PYTHON SOURCE LINES 149-150 Change interactive to True to start interacting with the scene .. GENERATED FROM PYTHON SOURCE LINES 150-157 .. code-block:: Python interactive = False if interactive: showm.start() .. GENERATED FROM PYTHON SOURCE LINES 158-159 Save the current framebuffer in a PNG file .. GENERATED FROM PYTHON SOURCE LINES 159-161 .. code-block:: Python fury.window.record(scene=showm.scene, size=(1024, 768), out_path="viz_picking.png") .. _sphx_glr_download_auto_examples_01_introductory_viz_picking.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: viz_picking.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: viz_picking.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: viz_picking.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_