
.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "_auto_examples/plot_3D_curvature.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_plot_3D_curvature.py>`
        to download the full example code.

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

.. _sphx_glr__auto_examples_plot_3D_curvature.py:


Plot 3D curvature
-----------------

This example shows how to plot curvature combs in 3D
or how to color the spline based on the local curvature.
Both options are illustrated in pyvista and matplotlib.

.. GENERATED FROM PYTHON SOURCE LINES 9-16

.. code-block:: Python


    import matplotlib.pyplot as plt
    import mpl_toolkits
    import numpy as np
    import pyvista
    import splinebox








.. GENERATED FROM PYTHON SOURCE LINES 17-19

1. Construct as random spline
-----------------------------

.. GENERATED FROM PYTHON SOURCE LINES 19-29

.. code-block:: Python


    M = 4
    spline = splinebox.Spline(M=M, basis_function=splinebox.B3(), closed=False)
    np.random.seed(0)
    spline.control_points = np.random.rand(M + 2, 3)

    t = np.linspace(0, M - 1, M * 15)

    vals = spline(t)








.. GENERATED FROM PYTHON SOURCE LINES 30-32

2. Curvature comb with pyvista
------------------------------

.. GENERATED FROM PYTHON SOURCE LINES 32-53

.. code-block:: Python


    mesh = pyvista.MultipleLines(points=vals)

    curvature = spline.curvature(t)

    normals = spline.normal(t, frame="frenet")
    normal = -normals[:, 0]

    comb = vals + curvature[:, np.newaxis] / 100 * normal

    comb_mesh_points = np.zeros((comb.shape[0] * 3 - 1, 3))
    comb_mesh_points[0::3] = vals
    comb_mesh_points[1::3] = comb
    comb_mesh_points[2::3] = comb[1:]
    comb_mesh = pyvista.MultipleLines(points=comb_mesh_points)

    plotter = pyvista.Plotter()
    plotter.add_mesh(mesh, color="blue")
    plotter.add_mesh(comb_mesh, color="lightblue")
    plotter.show()








.. tab-set::



   .. tab-item:: Static Scene



            
     .. image-sg:: /_auto_examples/images/sphx_glr_plot_3D_curvature_001.png
        :alt: plot 3D curvature
        :srcset: /_auto_examples/images/sphx_glr_plot_3D_curvature_001.png
        :class: sphx-glr-single-img
     


   .. tab-item:: Interactive Scene



       .. offlineviewer:: /home/docs/checkouts/readthedocs.org/user_builds/splinebox/checkouts/latest/docs/_auto_examples/images/sphx_glr_plot_3D_curvature_001.vtksz






.. GENERATED FROM PYTHON SOURCE LINES 54-56

3. Curvature comb with matplotlib
---------------------------------

.. GENERATED FROM PYTHON SOURCE LINES 56-62

.. code-block:: Python


    ax = plt.figure().add_subplot(projection="3d")
    ax.plot(vals[:, 2], vals[:, 1], vals[:, 0], color="blue")
    ax.plot(comb_mesh_points[:, 2], comb_mesh_points[:, 1], comb_mesh_points[:, 0], color="lightblue")
    plt.show()




.. image-sg:: /_auto_examples/images/sphx_glr_plot_3D_curvature_002.png
   :alt: plot 3D curvature
   :srcset: /_auto_examples/images/sphx_glr_plot_3D_curvature_002.png
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 63-67

4. Colour with pyvista
----------------------

We start with a simple line.

.. GENERATED FROM PYTHON SOURCE LINES 67-74

.. code-block:: Python


    points, connectivity = spline.mesh(step_t=t[1] - t[0], radius=None)
    connectivity = np.hstack((np.full((connectivity.shape[0], 1), 2), connectivity))
    mesh = pyvista.PolyData(points, lines=connectivity)
    mesh.cell_data["curvature"] = spline.curvature(t[:-1] + np.diff(t))
    mesh.plot()








.. tab-set::



   .. tab-item:: Static Scene



            
     .. image-sg:: /_auto_examples/images/sphx_glr_plot_3D_curvature_003.png
        :alt: plot 3D curvature
        :srcset: /_auto_examples/images/sphx_glr_plot_3D_curvature_003.png
        :class: sphx-glr-single-img
     


   .. tab-item:: Interactive Scene



       .. offlineviewer:: /home/docs/checkouts/readthedocs.org/user_builds/splinebox/checkouts/latest/docs/_auto_examples/images/sphx_glr_plot_3D_curvature_003.vtksz






.. GENERATED FROM PYTHON SOURCE LINES 75-76

To control the thickness of the the line we can use a mesh

.. GENERATED FROM PYTHON SOURCE LINES 76-84

.. code-block:: Python


    points, connectivity = spline.mesh(step_t=t[1] - t[0], radius=0.01)
    connectivity = np.hstack((np.full((connectivity.shape[0], 1), 3), connectivity))
    mesh = pyvista.PolyData(points, faces=connectivity)
    segment_curvature = spline.curvature(t[:-1] + np.diff(t))
    mesh.cell_data["curvature"] = np.repeat(segment_curvature, len(connectivity) / len(segment_curvature))
    mesh.plot()








.. tab-set::



   .. tab-item:: Static Scene



            
     .. image-sg:: /_auto_examples/images/sphx_glr_plot_3D_curvature_004.png
        :alt: plot 3D curvature
        :srcset: /_auto_examples/images/sphx_glr_plot_3D_curvature_004.png
        :class: sphx-glr-single-img
     


   .. tab-item:: Interactive Scene



       .. offlineviewer:: /home/docs/checkouts/readthedocs.org/user_builds/splinebox/checkouts/latest/docs/_auto_examples/images/sphx_glr_plot_3D_curvature_004.vtksz






.. GENERATED FROM PYTHON SOURCE LINES 85-87

5. Colour with matplotlib
-------------------------

.. GENERATED FROM PYTHON SOURCE LINES 87-94

.. code-block:: Python


    ax = plt.figure().add_subplot(projection="3d")
    segments = np.stack([vals[:-1], vals[1:]], axis=1)
    lc = mpl_toolkits.mplot3d.art3d.Line3DCollection(segments)
    lc.set_array(segment_curvature)
    ax.add_collection(lc)
    plt.show()



.. image-sg:: /_auto_examples/images/sphx_glr_plot_3D_curvature_005.png
   :alt: plot 3D curvature
   :srcset: /_auto_examples/images/sphx_glr_plot_3D_curvature_005.png
   :class: sphx-glr-single-img






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

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


.. _sphx_glr_download__auto_examples_plot_3D_curvature.py:

.. only:: html

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

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

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

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

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

    .. container:: sphx-glr-download sphx-glr-download-zip

      :download:`Download zipped: plot_3D_curvature.zip <plot_3D_curvature.zip>`


.. only:: html

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

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