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

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

.. _sphx_glr__auto_examples_plot_distance.py:


Distance between splines
------------------------

In this example, we compute the euclidean distance between two splines.

.. GENERATED FROM PYTHON SOURCE LINES 7-13

.. code-block:: Python


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








.. GENERATED FROM PYTHON SOURCE LINES 14-16

For simplicity we create a function that can plot the two splines and the
distance between the splines at parameter values t_min and s_min.

.. GENERATED FROM PYTHON SOURCE LINES 16-39

.. code-block:: Python



    def plot_splines(spline1, spline2, t_min=None, s_min=None):
        vals1 = spline1(np.linspace(0, spline1.M, 1000))
        vals2 = spline2(np.linspace(0, spline2.M, 1000))
        knots1 = spline1.knots
        knots2 = spline2.knots

        plt.plot(vals1[:, 1], vals1[:, 0])
        plt.plot(vals2[:, 1], vals2[:, 0])
        plt.scatter(knots1[:, 1], knots1[:, 0])
        plt.scatter(knots2[:, 1], knots2[:, 0])

        if t_min is not None and s_min is not None:
            point1 = spline1(t_min)
            point2 = spline2(s_min)
            plt.plot([point1[1], point2[1]], [point1[0], point2[0]], color="k", linestyle="--")

        plt.gca().set_aspect("equal", "box")

        plt.show()









.. GENERATED FROM PYTHON SOURCE LINES 40-41

We start by constructing two arbitrary closed splines.

.. GENERATED FROM PYTHON SOURCE LINES 41-67

.. code-block:: Python


    basis_function = splinebox.basis_functions.B3()
    M = 5
    spline1 = splinebox.spline_curves.Spline(M=M, basis_function=basis_function, closed=True)
    spline2 = splinebox.spline_curves.Spline(M=M, basis_function=basis_function, closed=True)

    spline1.control_points = np.array(
        [
            [1, 2],
            [2, 2],
            [3, 2.5],
            [2.2, 3],
            [1.3, 2.2],
        ]
    )

    spline2.control_points = np.array(
        [
            [2, -2],
            [2.5, -1],
            [2, -1.5],
            [1.5, -1],
            [1, -2],
        ]
    )








.. GENERATED FROM PYTHON SOURCE LINES 68-69

Plot the splines

.. GENERATED FROM PYTHON SOURCE LINES 69-71

.. code-block:: Python

    plot_splines(spline1, spline2)




.. image-sg:: /_auto_examples/images/sphx_glr_plot_distance_001.png
   :alt: plot distance
   :srcset: /_auto_examples/images/sphx_glr_plot_distance_001.png
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 72-76

To get an initial guess of the spline parameter pair with the
smallest distance, we perform a brute force search.
Note: This can be made more accurate by increasing the number
of parameters we interogate.

.. GENERATED FROM PYTHON SOURCE LINES 76-89

.. code-block:: Python


    t = np.linspace(0, spline1.M, 5)
    s = np.linspace(0, spline2.M, 5)
    vals1 = spline1(t)
    vals2 = spline2(s)
    distance_vectors = vals1[:, np.newaxis] - vals2[np.newaxis, :]
    distances = np.linalg.norm(distance_vectors, axis=-1)
    indices = np.unravel_index(np.argmin(distances), distances.shape)
    t_min = t[indices[0]]
    s_min = s[indices[1]]

    plot_splines(spline1, spline2, t_min, s_min)




.. image-sg:: /_auto_examples/images/sphx_glr_plot_distance_002.png
   :alt: plot distance
   :srcset: /_auto_examples/images/sphx_glr_plot_distance_002.png
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 90-92

To further refine the estimate we can
run an optimization.

.. GENERATED FROM PYTHON SOURCE LINES 92-104

.. code-block:: Python



    def distance(parameters):
        val1 = spline1(parameters[0])
        val2 = spline2(parameters[1])
        return np.linalg.norm(val1 - val2)


    result = scipy.optimize.minimize(distance, np.array([t_min, s_min]), bounds=((0, spline1.M), (0, spline2.M)))
    t_min, s_min = result.x

    plot_splines(spline1, spline2, t_min, s_min)



.. image-sg:: /_auto_examples/images/sphx_glr_plot_distance_003.png
   :alt: plot distance
   :srcset: /_auto_examples/images/sphx_glr_plot_distance_003.png
   :class: sphx-glr-single-img






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

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


.. _sphx_glr_download__auto_examples_plot_distance.py:

.. only:: html

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

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

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

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

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

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

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


.. only:: html

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

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