.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "auto_examples/01_introductory/viz_spiky.py"
.. LINE NUMBERS ARE GIVEN BELOW.

.. only:: html

    .. note::
        :class: sphx-glr-download-link-note

        :ref:`Go to the end <sphx_glr_download_auto_examples_01_introductory_viz_spiky.py>`
        to download the full example code

.. rst-class:: sphx-glr-example-title

.. _sphx_glr_auto_examples_01_introductory_viz_spiky.py:


===============
Spiky Sphere
===============
In this tutorial, we show how to create a sphere with spikes.

.. GENERATED FROM PYTHON SOURCE LINES 7-14

.. code-block:: Python


    import itertools

    import numpy as np

    import fury








.. GENERATED FROM PYTHON SOURCE LINES 15-19

Create a sphere actor. Define the center, radius and color of a sphere.
The sphere actor is made of points (vertices) evenly distributed on a
sphere.
Let's create a scene.

.. GENERATED FROM PYTHON SOURCE LINES 19-22

.. code-block:: Python


    scene = fury.window.Scene()








.. GENERATED FROM PYTHON SOURCE LINES 23-26

The vertices are connected with triangles in order to specify the direction
of the surface normal.
``prim_sphere`` provides a sphere with evenly distributed points

.. GENERATED FROM PYTHON SOURCE LINES 26-29

.. code-block:: Python


    vertices, triangles = fury.primitive.prim_sphere(name="symmetric362", gen_faces=False)








.. GENERATED FROM PYTHON SOURCE LINES 30-32

To be able to visualize the vertices, let's define a point actor with
green color.

.. GENERATED FROM PYTHON SOURCE LINES 32-35

.. code-block:: Python


    point_actor = fury.actor.point(vertices, point_radius=0.01, colors=(0, 1, 0))








.. GENERATED FROM PYTHON SOURCE LINES 36-39

Normals are the vectors that are perpendicular to the surface at each
vertex. We specify the normals at the vertices to tell the system
whether triangles represent curved surfaces.

.. GENERATED FROM PYTHON SOURCE LINES 39-42

.. code-block:: Python


    normals = fury.utils.normals_from_v_f(vertices, triangles)








.. GENERATED FROM PYTHON SOURCE LINES 43-47

The normals are usually used to calculate how the light will bounce on
the surface of an object. However, here we will use them to direct the
spikes (represented with arrows).
So, let's create an arrow actor at the center of each vertex.

.. GENERATED FROM PYTHON SOURCE LINES 47-58

.. code-block:: Python


    arrow_actor = fury.actor.arrow(
        centers=vertices,
        directions=normals,
        colors=(1, 0, 0),
        heights=0.2,
        resolution=10,
        vertices=None,
        faces=None,
    )








.. GENERATED FROM PYTHON SOURCE LINES 59-61

To be able to visualize the surface of the primitive sphere, we use
``get_actor_from_primitive``.

.. GENERATED FROM PYTHON SOURCE LINES 61-72

.. code-block:: Python


    primitive_colors = np.zeros(vertices.shape)
    primitive_colors[:, 2] = 180
    primitive_actor = fury.utils.get_actor_from_primitive(
        vertices=vertices,
        triangles=triangles,
        colors=primitive_colors,
        normals=normals,
        backface_culling=True,
    )








.. GENERATED FROM PYTHON SOURCE LINES 73-74

We add all actors (visual objects) defined above to the scene.

.. GENERATED FROM PYTHON SOURCE LINES 74-80

.. code-block:: Python


    scene.add(point_actor)
    scene.add(arrow_actor)
    scene.add(primitive_actor)
    scene.add(fury.actor.axes())








.. GENERATED FROM PYTHON SOURCE LINES 81-83

The ShowManager class is the interface between the scene, the window and the
interactor.

.. GENERATED FROM PYTHON SOURCE LINES 83-88

.. code-block:: Python


    showm = fury.window.ShowManager(
        scene=scene, size=(900, 768), reset_camera=False, order_transparent=True
    )








.. GENERATED FROM PYTHON SOURCE LINES 89-92

We want to make a small animation for fun!
We can determine the duration of animation with using the ``counter``.
Use itertools to avoid global variables.

.. GENERATED FROM PYTHON SOURCE LINES 92-95

.. code-block:: Python


    counter = itertools.count()








.. GENERATED FROM PYTHON SOURCE LINES 96-98

The timer will call this user defined callback every 200 milliseconds. The
application will exit after the callback has been called 20 times.

.. GENERATED FROM PYTHON SOURCE LINES 98-113

.. code-block:: Python



    def timer_callback(_obj, _event):
        cnt = next(counter)
        showm.scene.azimuth(0.05 * cnt)
        primitive_actor.GetProperty().SetOpacity(cnt / 10.0)
        showm.render()
        if cnt == 20:
            showm.exit()


    showm.add_timer_callback(True, 200, timer_callback)
    showm.start()
    fury.window.record(scene=showm.scene, size=(900, 768), out_path="viz_spiky.png")




.. image-sg:: /auto_examples/01_introductory/images/sphx_glr_viz_spiky_001.png
   :alt: viz spiky
   :srcset: /auto_examples/01_introductory/images/sphx_glr_viz_spiky_001.png
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 114-116

Instead of arrows, you can choose other geometrical objects
such as cones, cubes or spheres.


.. rst-class:: sphx-glr-timing

   **Total running time of the script:** (0 minutes 4.353 seconds)


.. _sphx_glr_download_auto_examples_01_introductory_viz_spiky.py:

.. only:: html

  .. container:: sphx-glr-footer sphx-glr-footer-example

    .. container:: sphx-glr-download sphx-glr-download-jupyter

      :download:`Download Jupyter notebook: viz_spiky.ipynb <viz_spiky.ipynb>`

    .. container:: sphx-glr-download sphx-glr-download-python

      :download:`Download Python source code: viz_spiky.py <viz_spiky.py>`


.. only:: html

 .. rst-class:: sphx-glr-signature

    `Gallery generated by Sphinx-Gallery <https://sphinx-gallery.github.io>`_