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

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

.. _sphx_glr_auto_examples_plot_curvature_combs.py:


Curvature combs
---------------

In this example, we plot the curvature comb of a 2D spline.
Calculating curvature requires computing the second derivative, which can be sensitive to noise when using numerical methods.
Splines are particularly useful in this context because their derivatives can be computed analytically using the chain rule, resulting in smoother and more accurate curvature calculations.

.. GENERATED FROM PYTHON SOURCE LINES 9-14

.. code-block:: Python


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








.. GENERATED FROM PYTHON SOURCE LINES 15-17

We begin by generating an arbitrary B3 spline with 7 knots.
Any twice-differentiable basis function can be used here.

.. GENERATED FROM PYTHON SOURCE LINES 17-22

.. code-block:: Python


    M = 7
    spline = splinebox.Spline(M=M, basis_function=splinebox.B3(), closed=False)
    spline.knots = np.array([[0, 0], [1, 1], [2.5, 0.5], [3.5, 1.5], [3, 3], [2, 3.5], [1, 2]])








.. GENERATED FROM PYTHON SOURCE LINES 23-24

Next, we select finely spaced parameter values along the spline for evaluation and plotting.

.. GENERATED FROM PYTHON SOURCE LINES 24-27

.. code-block:: Python


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








.. GENERATED FROM PYTHON SOURCE LINES 28-30

To plot the curvature comb, we need to compute the spline's values, curvature, and normal vectors at each t.
Normal vectors are unit vectors perpendicular to the spline at each point.

.. GENERATED FROM PYTHON SOURCE LINES 30-36

.. code-block:: Python


    vals = spline.eval(t)

    curvature = spline.curvature(t)
    normals = spline.normal(t)








.. GENERATED FROM PYTHON SOURCE LINES 37-40

To construct the curvature comb, we scale the normal vectors by the curvature and a factor `d`.
The factor `d` controls the height of the comb, allowing us to adjust its visual appearance.
The scaled normal vectors are then added to the spline values.

.. GENERATED FROM PYTHON SOURCE LINES 40-46

.. code-block:: Python


    max_comb_height = 1
    d = max_comb_height / np.max(np.abs(curvature))

    comb = vals + d * curvature[:, np.newaxis] * normals








.. GENERATED FROM PYTHON SOURCE LINES 47-49

Finally, we plot the curvature comb. The backbone of the comb and the spline itself are plotted as lines,
while the "teeth" of the comb are plotted individually using a loop.

.. GENERATED FROM PYTHON SOURCE LINES 49-57

.. code-block:: Python


    plt.plot(comb[:, 1], comb[:, 0], label="Curvature comb", alpha=0.5)
    for p in range(0, len(comb), 7):
        plt.plot([vals[p, 1], comb[p, 1]], [vals[p, 0], comb[p, 0]], alpha=0.5)
    plt.plot(vals[:, 1], vals[:, 0], label="spline")
    plt.gca().set_aspect("equal", "box")
    plt.legend()
    plt.show()



.. image-sg:: /auto_examples/images/sphx_glr_plot_curvature_combs_001.png
   :alt: plot curvature combs
   :srcset: /auto_examples/images/sphx_glr_plot_curvature_combs_001.png
   :class: sphx-glr-single-img






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

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


.. _sphx_glr_download_auto_examples_plot_curvature_combs.py:

.. only:: html

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

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

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

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

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

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

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


.. only:: html

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

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