{
    "nbformat": 4,
    "nbformat_minor": 5,
    "metadata": {
        "kernelspec": {
            "display_name": "Python",
            "language": "python",
            "name": "python"
        },
        "language_info": {
            "name": "python"
        }
    },
    "cells": [
        {
            "id": "50f71ae0",
            "cell_type": "markdown",
            "source": "<div class='alert alert-warning'>\n\nSciPy's interactive examples with Jupyterlite are experimental and may not always work as expected. Execution of cells containing imports may result in large downloads (up to 60MB of content for the first import from SciPy). Load times when importing from SciPy may take roughly 10-20 seconds. If you notice any problems, feel free to open an [issue](https://github.com/scipy/scipy/issues/new/choose).\n\n</div>",
            "metadata": {}
        },
        {
            "id": "bd3c26b3",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "from scipy.spatial.transform import Rotation, RotationSpline\nimport numpy as np",
            "outputs": []
        },
        {
            "id": "a8d4b7e8",
            "cell_type": "markdown",
            "source": "Define the sequence of times and rotations from the Euler angles:\n",
            "metadata": {}
        },
        {
            "id": "058c9d4c",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "times = [0, 10, 20, 40]\nangles = [[-10, 20, 30], [0, 15, 40], [-30, 45, 30], [20, 45, 90]]\nrotations = Rotation.from_euler('XYZ', angles, degrees=True)",
            "outputs": []
        },
        {
            "id": "c2e0cc19",
            "cell_type": "markdown",
            "source": "Create the interpolator object:\n",
            "metadata": {}
        },
        {
            "id": "54c63c32",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "spline = RotationSpline(times, rotations)",
            "outputs": []
        },
        {
            "id": "4bbf7ee6",
            "cell_type": "markdown",
            "source": "Interpolate the Euler angles, angular rate and acceleration:\n",
            "metadata": {}
        },
        {
            "id": "7974d483",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "angular_rate = np.rad2deg(spline(times, 1))\nangular_acceleration = np.rad2deg(spline(times, 2))\ntimes_plot = np.linspace(times[0], times[-1], 100)\nangles_plot = spline(times_plot).as_euler('XYZ', degrees=True)\nangular_rate_plot = np.rad2deg(spline(times_plot, 1))\nangular_acceleration_plot = np.rad2deg(spline(times_plot, 2))",
            "outputs": []
        },
        {
            "id": "c983de3d",
            "cell_type": "markdown",
            "source": "On this plot you see that Euler angles are continuous and smooth:\n",
            "metadata": {}
        },
        {
            "id": "437a204e",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "import matplotlib.pyplot as plt\nplt.plot(times_plot, angles_plot)\nplt.plot(times, angles, 'x')\nplt.title(\"Euler angles\")\nplt.show()",
            "outputs": []
        },
        {
            "id": "38ebac16",
            "cell_type": "markdown",
            "source": "The angular rate is also smooth:\n",
            "metadata": {}
        },
        {
            "id": "71e211e8",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "plt.plot(times_plot, angular_rate_plot)\nplt.plot(times, angular_rate, 'x')\nplt.title(\"Angular rate\")\nplt.show()",
            "outputs": []
        },
        {
            "id": "2c9c5e20",
            "cell_type": "markdown",
            "source": "The angular acceleration is continuous, but not smooth. Also note that\nthe angular acceleration is not a piecewise-linear function, because\nit is different from the second derivative of the rotation vector (which\nis a piecewise-linear function as in the cubic spline).\n",
            "metadata": {}
        },
        {
            "id": "154c34db",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "plt.plot(times_plot, angular_acceleration_plot)\nplt.plot(times, angular_acceleration, 'x')\nplt.title(\"Angular acceleration\")\nplt.show()",
            "outputs": []
        }
    ]
}