.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/04_demos/viz_vector_field.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_04_demos_viz_vector_field.py: ============================================ Vector Field Visualization with Slicer Actor ============================================ This example demonstrates how to visualize a vector field using vector field actor present in FURY. The vector field can be visualized using arrows, line or thin lines. Later, we will use the cross-section of the vector field using vector field slicer actor. To demonstrate we will create a diverging radial field and visualize it with arrows. .. GENERATED FROM PYTHON SOURCE LINES 14-19 .. code-block:: Python import numpy as np from fury import actor, window from fury.lib import EventType .. GENERATED FROM PYTHON SOURCE LINES 20-24 Create a diverging radial vector field. we will create a 4D array to represent the vector field, where the last dimension contains the x, y, z components of the vector at each point in the grid. The grid will be 20x20x20, and we will fill it with vectors that point outward from the center of the grid. .. GENERATED FROM PYTHON SOURCE LINES 24-32 .. code-block:: Python X, Y, Z = 20, 20, 20 vector_field = np.zeros((X, Y, Z, 3)) sparse_step = 1 center = np.array([X / 2, Y / 2, Z / 2]) .. GENERATED FROM PYTHON SOURCE LINES 33-34 Define a function to calculate the vector at a given point in the grid. .. GENERATED FROM PYTHON SOURCE LINES 34-45 .. code-block:: Python def vector_function(x, y, z): r = np.array([x - center[0], y - center[1], z - center[2]]) norm = np.linalg.norm(r) if norm > 0: return r / norm else: return np.zeros(3) .. GENERATED FROM PYTHON SOURCE LINES 46-47 Fill the vector field with vectors calculated from the vector_function. .. GENERATED FROM PYTHON SOURCE LINES 47-54 .. code-block:: Python for i in range(0, X, sparse_step): for j in range(0, Y, sparse_step): for k in range(0, Z, sparse_step): vector_field[i, j, k] = vector_function(i, j, k) .. GENERATED FROM PYTHON SOURCE LINES 55-56 Create a vector field actor to visualize the vector field. .. GENERATED FROM PYTHON SOURCE LINES 56-64 .. code-block:: Python vector_field_actor = actor.vector_field( vector_field, actor_type="arrow", # Use 'arrow' for arrow visualization scales=0.2, # Scale the vectors for better visibility thickness=10, # Thickness of the vectors ) .. GENERATED FROM PYTHON SOURCE LINES 65-66 Create a scene and add the vector field actor to it. .. GENERATED FROM PYTHON SOURCE LINES 66-70 .. code-block:: Python scene = window.Scene() scene.add(vector_field_actor) .. GENERATED FROM PYTHON SOURCE LINES 71-72 Create a show manager to render the scene. .. GENERATED FROM PYTHON SOURCE LINES 72-77 .. code-block:: Python show_manager = window.ShowManager( scene=scene, size=(800, 600), title="Vector Field Visualization" ) show_manager.start() .. GENERATED FROM PYTHON SOURCE LINES 78-81 Next, we will create a vector field slicer actor to visualize the cross-section of the vector field. The slicer will allow us to slice through the vector field and visualize the vectors in the sliced plane. .. GENERATED FROM PYTHON SOURCE LINES 81-94 .. code-block:: Python # We will use the Z slice of the vector field for this example. To, use that # option we will set the visibility of the X and Y slices to False and Z slice # to True. vector_field_slicer_actor = actor.vector_field_slicer( vector_field, actor_type="arrow", # Use 'arrow' for arrow visualization scales=0.7, # Scale the vectors for better visibility thickness=10, # Thickness of the vectors visibility=(False, False, True), ) .. GENERATED FROM PYTHON SOURCE LINES 95-96 Add the vector field slicer actor to the scene. .. GENERATED FROM PYTHON SOURCE LINES 96-103 .. code-block:: Python scene.remove(vector_field_actor) # Remove the original vector field actor scene.add(vector_field_slicer_actor) show_manager = window.ShowManager( scene=scene, size=(800, 600), title="Vector Field Slicer Visualization" ) .. GENERATED FROM PYTHON SOURCE LINES 104-105 handle key events to move the cross-section of the vector field slicer. .. GENERATED FROM PYTHON SOURCE LINES 105-119 .. code-block:: Python def handle_key_event(event): print("Key pressed:", event.key) position = actor.get_slices(vector_field_slicer_actor) if event.key == "ArrowUp": position = (position[0], position[1], position[2] + sparse_step) elif event.key == "ArrowDown": position = (position[0], position[1], position[2] - sparse_step) actor.show_slices(vector_field_slicer_actor, position) show_manager.render() .. GENERATED FROM PYTHON SOURCE LINES 120-121 Add event handler for key events to the show manager. .. GENERATED FROM PYTHON SOURCE LINES 121-124 .. code-block:: Python show_manager.renderer.add_event_handler(handle_key_event, EventType.KEY_DOWN) show_manager.start() .. _sphx_glr_download_auto_examples_04_demos_viz_vector_field.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: viz_vector_field.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: viz_vector_field.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: viz_vector_field.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_