.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/01_introductory/viz_selection.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_selection.py: ========================== Selecting multiple objects ========================== Here we show how to select objects in the 3D world. In this example 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. Nonetheless the method works for multiple actors too. The difference with the picking tutorial is that here we will be able to select more than one object. In addition we can select interactively many vertices or faces. In summary, we will create an actor with thousands of cubes and then interactively we will be moving a rectangular box by hovering the mouse and making transparent everything that is behind that box. .. GENERATED FROM PYTHON SOURCE LINES 22-27 .. code-block:: Python import numpy as np from fury import actor, pick, utils, window .. GENERATED FROM PYTHON SOURCE LINES 28-29 Adding many cubes of different sizes and colors .. GENERATED FROM PYTHON SOURCE LINES 29-37 .. code-block:: Python num_cubes = 50000 centers = 10000 * (np.random.rand(num_cubes, 3) - 0.5) colors = np.random.rand(num_cubes, 4) colors[:, 3] = 1.0 radii = 100 * np.random.rand(num_cubes) + 0.1 .. GENERATED FROM PYTHON SOURCE LINES 38-41 Keep track of total number of triangle faces Note that every quad of each cube has 2 triangles and each cube has 6 quads in total. .. GENERATED FROM PYTHON SOURCE LINES 41-44 .. code-block:: Python num_faces = num_cubes * 6 * 2 .. GENERATED FROM PYTHON SOURCE LINES 45-46 Build scene and add an actor with many objects. .. GENERATED FROM PYTHON SOURCE LINES 46-49 .. code-block:: Python scene = window.Scene() .. GENERATED FROM PYTHON SOURCE LINES 50-51 Build the actor containing all the cubes .. GENERATED FROM PYTHON SOURCE LINES 51-54 .. code-block:: Python cube_actor = actor.cube(centers, directions=(1, 0, 0), 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 = utils.vertices_from_actor(cube_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 = utils.colors_from_actor(cube_actor, 'colors') .. GENERATED FROM PYTHON SOURCE LINES 67-68 Create a rectangular 2d box as a texture .. GENERATED FROM PYTHON SOURCE LINES 68-78 .. code-block:: Python rgba = 255 * np.ones((100, 200, 4)) rgba[1:-1, 1:-1] = np.zeros((98, 198, 4)) + 100 texa = actor.texture_2d(rgba.astype(np.uint8)) scene.add(cube_actor) scene.add(texa) scene.reset_camera() scene.zoom(3.0) .. GENERATED FROM PYTHON SOURCE LINES 79-80 Create the Selection Manager .. GENERATED FROM PYTHON SOURCE LINES 80-83 .. code-block:: Python selm = pick.SelectionManager(select='faces') .. GENERATED FROM PYTHON SOURCE LINES 84-85 Tell Selection Manager to avoid selecting specific actors .. GENERATED FROM PYTHON SOURCE LINES 85-88 .. code-block:: Python selm.selectable_off(texa) .. GENERATED FROM PYTHON SOURCE LINES 89-91 Let's make the callback which will be called when we hover the mouse .. GENERATED FROM PYTHON SOURCE LINES 91-116 .. code-block:: Python def hover_callback(_obj, _event): event_pos = selm.event_position(showm.iren) # updates rectangular box around mouse texa.SetPosition(event_pos[0] - 200 // 2, event_pos[1] - 100 // 2) # defines selection region and returns information from selected objects info = selm.select(event_pos, showm.scene, (200 // 2, 100 // 2)) for node in info.keys(): if info[node]['face'] is not None: if info[node]['actor'] is cube_actor: for face_index in info[node]['face']: # generates an object_index to help with coloring # by dividing by the number of faces of each cube (6 * 2) object_index = face_index // 12 sec = int(num_vertices / num_objects) color_change = np.array([150, 0, 0, 255], dtype='uint8') vcolors[ object_index * sec : object_index * sec + sec ] = color_change utils.update_actor(cube_actor) showm.render() .. GENERATED FROM PYTHON SOURCE LINES 117-118 Make the window appear .. GENERATED FROM PYTHON SOURCE LINES 118-124 .. code-block:: Python showm = window.ShowManager( scene, size=(1024, 768), order_transparent=True, reset_camera=False ) .. GENERATED FROM PYTHON SOURCE LINES 125-126 Bind the callback to the actor .. GENERATED FROM PYTHON SOURCE LINES 126-129 .. code-block:: Python showm.add_iren_callback(hover_callback) .. GENERATED FROM PYTHON SOURCE LINES 130-131 Change interactive to True to start interacting with the scene .. GENERATED FROM PYTHON SOURCE LINES 131-139 .. code-block:: Python interactive = False if interactive: showm.start() .. GENERATED FROM PYTHON SOURCE LINES 140-141 Save the current framebuffer in a PNG file .. GENERATED FROM PYTHON SOURCE LINES 141-143 .. code-block:: Python window.record(showm.scene, size=(1024, 768), out_path='viz_selection.png') .. image-sg:: /auto_examples/01_introductory/images/sphx_glr_viz_selection_001.png :alt: viz selection :srcset: /auto_examples/01_introductory/images/sphx_glr_viz_selection_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 1.308 seconds) .. _sphx_glr_download_auto_examples_01_introductory_viz_selection.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: viz_selection.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: viz_selection.py ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_