
    sg7                     h    d dl Zd dlmZ ddlmZ d Zd Zd Zd Z	d	 Z
d
 Zd Zd Z G d d      Zy)    N)solve_banded   )Rotationc                    t        j                  t        |       ddf      }| dddf    |ddddf<   | dddf   |ddddf<   | dddf   |ddddf<   | dddf    |ddddf<   | dddf    |ddddf<   | dddf   |ddddf<   |S )zCreate skew-symmetric matrices corresponding to vectors.

    Parameters
    ----------
    x : ndarray, shape (n, 3)
        Set of vectors.

    Returns
    -------
    ndarray, shape (n, 3, 3)
       N   r   r   )npzeroslen)xresults     [/var/www/html/venv/lib/python3.12/site-packages/scipy/spatial/transform/_rotation_spline.py_create_skew_matrixr      s     XXs1vq!n%FAwhF1a7O1gF1a7O1gF1a7OAwhF1a7OAwhF1a7O1gF1a7OM    c                 0    t        j                  d| |      S )z5Compute the product of stack of matrices and vectors.z
ijk,ik->ij)r	   einsum)Abs     r    _matrix_vector_product_of_stacksr      s    99\1a((r   c           	      
   t         j                  j                  | d      }t        j                  |      }|dkD  }||   }dd|z  t        j                  d|z        z  z
  |dz  z  ||<   | }||   }dd|dz  z  z   ||<   t        |       }t        j                  t        |       ddf      }t        j                  d      |d	d	 |d	d	xxx d|z  z  ccc |d	d	xxx |d	d	d	d	f   t        j                  ||      z  z  ccc |S )
a=  Compute matrices to transform angular rates to rot. vector derivatives.

    The matrices depend on the current attitude represented as a rotation
    vector.

    Parameters
    ----------
    rotvecs : ndarray, shape (n, 3)
        Set of rotation vectors.

    Returns
    -------
    ndarray, shape (n, 3, 3)
    r   axis-C6?      ?r   UUUUUU?gllV?r   N)
r	   linalgnorm
empty_liketanr   emptyr   identitymatmul)rotvecsr   kmasknmskewr   s          r   "_angular_rate_to_rotvec_dot_matrixr(   !   s    99>>'>*D
dA$;D	dB38bffS2X..."a%7AdG5D	dBURU]"AdGw'DXXs7|Q*+FAF1I
1ItI
1I1dD=!BIIdD$999IMr   c           	         t         j                  j                  | d      }t        j                  |      }t        j                  |      }|dkD  }||   }dt        j                  |      z
  |dz  z  ||<   |t        j
                  |      z
  |dz  z  ||<   | }||   }d|dz  dz  z
  ||<   d|dz  d	z  z
  ||<   t        |       }t        j                  t        |       ddf      }t        j                  d      |d
d
 |d
d
xxx |d
d
d
d
f   |z  z  ccc |d
d
xxx |d
d
d
d
f   t        j                  ||      z  z  ccc |S )a=  Compute matrices to transform rot. vector derivatives to angular rates.

    The matrices depend on the current attitude represented as a rotation
    vector.

    Parameters
    ----------
    rotvecs : ndarray, shape (n, 3)
        Set of rotation vectors.

    Returns
    -------
    ndarray, shape (n, 3, 3)
    r   r   r   r   r   r      UUUUUU?x   N)r	   r   r   r   cossinr   r    r   r!   r"   )r#   r   k1k2r%   r&   r'   r   s           r   "_rotvec_dot_to_angular_rate_matrixr1   D   s@    99>>'>*D	t	B	t	B$;D	dBBFF2J"')BtHRVVBZ27*BtH5D	dBR1Wr\!BtHrQw}$BtHw'DXXs7|Q*+FAF1I
1IAtTM"T))I
1IAtTM"RYYtT%:::IMr   c                    t         j                  j                  | d      }t        j                  | |z  d      }t        j                  | |      }t        j                  | |      }t        j                  ||      }t        j
                  |      }t        j
                  |      }t        j
                  |      }	|dkD  }
||
   }| t        j                  |      z  dt        j                  |      dz
  z  z
  |dz  z  ||
<   d|z  dt        j                  |      z  z   |t        j                  |      z  z
  |dz  z  ||
<   |t        j                  |      z
  |dz  z  |	|
<   |
 }
||
   }d	|dz  d
z  z
  ||
<   d|dz  dz  z   ||
<   d|dz  dz  z
  |	|
<   |dddf   }|dddf   }|dddf   }|	dddf   }	|||z  ||z  z   z  |	|z  z   S )a  Compute the non-linear term in angular acceleration.

    The angular acceleration contains a quadratic term with respect to
    the derivative of the rotation vector. This function computes that.

    Parameters
    ----------
    rotvecs : ndarray, shape (n, 3)
        Set of rotation vectors.
    rotvecs_dot : ndarray, shape (n, 3)
        Set of rotation vector derivatives.

    Returns
    -------
    ndarray, shape (n, 3)
    r   r   r   r      r      r      gi<1  r+   r,   N)r	   r   r   sumcrossr   r.   r-   )r#   rotvecs_dotr   dpcpccpdccpr/   r0   k3r%   r&   s               r   $_angular_acceleration_nonlinear_termr?   k   s   " 99>>'>*D	+%A	.B	';	'B
((7B
C88K$D	t	B	t	B	t	B$;D	dBbffRj 1r
Q#7727BBtHR!bffRj.(2r
?:bAgEBtHRVVBZ27*BtH5D	dBbAgm#BtHrQw&BtHR1Ws]"BtH	AtGB	AtGB	AtGB	AtGBb28#$rDy00r   c                 ,    t        t        |       |      S )a1  Compute angular rates given rotation vectors and its derivatives.

    Parameters
    ----------
    rotvecs : ndarray, shape (n, 3)
        Set of rotation vectors.
    rotvecs_dot : ndarray, shape (n, 3)
        Set of rotation vector derivatives.

    Returns
    -------
    ndarray, shape (n, 3)
    )r   r1   )r#   r9   s     r   _compute_angular_raterA      s     ,*73[B Br   c                 4    t        | |      t        | |      z   S )a  Compute angular acceleration given rotation vector and its derivatives.

    Parameters
    ----------
    rotvecs : ndarray, shape (n, 3)
        Set of rotation vectors.
    rotvecs_dot : ndarray, shape (n, 3)
        Set of rotation vector derivatives.
    rotvecs_dot_dot : ndarray, shape (n, 3)
        Set of rotation vector second derivatives.

    Returns
    -------
    ndarray, shape (n, 3)
    )rA   r?   )r#   r9   rotvecs_dot_dots      r   _compute_angular_accelerationrD      s$      "'?;0+FG Hr   c                 .   t        j                  d      }t        j                  t        |             }t        j                  | t              }|dddf   |dd |dd|ddddf   z   z  z  }t        j                  | t              }||dd |d|ddddf   z  z  }t        j                  |t              }|dddf   |dd |d|ddddf   z  z  }t        j                  |t              }||dd |dd|ddddf   z   z  z  }t        j                  dt        |      z        x}	}
t        j
                  |j                         |j                         |	f      }t        j
                  |j                         |j                         |
f      }t        j
                  | j                         |j                         t        j                  |d      f      }d}d}t        j                  ||z   dz   dt        |      z  f      }||||z   |z
  |f<   |S )a^  Create a 3-diagonal block matrix as banded.

    The matrix has the following structure:

        DB...
        ADB..
        .ADB.
        ..ADB
        ...AD

    The blocks A, B and D are 3-by-3 matrices. The D matrices has the form
    d * I.

    Parameters
    ----------
    A : ndarray, shape (n, 3, 3)
        Stack of A blocks.
    B : ndarray, shape (n, 3, 3)
        Stack of B blocks.
    d : ndarray, shape (n + 1,)
        Values for diagonal blocks.

    Returns
    -------
    ndarray, shape (11, 3 * (n + 1))
        Matrix in the banded form as used by `scipy.linalg.solve_banded`.
    r   dtypeNr   r5   )	r	   aranger   r   inthstackravelrepeatr
   )r   Bdind
ind_blocksA_iA_jB_iB_jdiag_idiag_jijvaluesulr   s                    r   _create_block_3_diagonal_matrixr\      s   8 ))A,C3q6"J
--
%CD\CF1Jq$}--..C
--
%CCF1z!T4-(((C
--
%CD\CF1z!T4-(((C
--
%CCF1Jq$}--..CiiCF
++FV
		399;		V45A
		399;		V45AYY	1779bii1o>?F	A	AXXq1uqy!c!f*-.F!F1q519a<Mr   c                   ,    e Zd ZdZdZdZd Zd ZddZy)	RotationSplinea7
  Interpolate rotations with continuous angular rate and acceleration.

    The rotation vectors between each consecutive orientation are cubic
    functions of time and it is guaranteed that angular rate and acceleration
    are continuous. Such interpolation are analogous to cubic spline
    interpolation.

    Refer to [1]_ for math and implementation details.

    Parameters
    ----------
    times : array_like, shape (N,)
        Times of the known rotations. At least 2 times must be specified.
    rotations : `Rotation` instance
        Rotations to perform the interpolation between. Must contain N
        rotations.

    Methods
    -------
    __call__

    References
    ----------
    .. [1] `Smooth Attitude Interpolation
            <https://github.com/scipy/scipy/files/2932755/attitude_interpolation.pdf>`_

    Examples
    --------
    >>> from scipy.spatial.transform import Rotation, RotationSpline
    >>> import numpy as np

    Define the sequence of times and rotations from the Euler angles:

    >>> times = [0, 10, 20, 40]
    >>> angles = [[-10, 20, 30], [0, 15, 40], [-30, 45, 30], [20, 45, 90]]
    >>> rotations = Rotation.from_euler('XYZ', angles, degrees=True)

    Create the interpolator object:

    >>> spline = RotationSpline(times, rotations)

    Interpolate the Euler angles, angular rate and acceleration:

    >>> angular_rate = np.rad2deg(spline(times, 1))
    >>> angular_acceleration = np.rad2deg(spline(times, 2))
    >>> times_plot = np.linspace(times[0], times[-1], 100)
    >>> angles_plot = spline(times_plot).as_euler('XYZ', degrees=True)
    >>> angular_rate_plot = np.rad2deg(spline(times_plot, 1))
    >>> angular_acceleration_plot = np.rad2deg(spline(times_plot, 2))

    On this plot you see that Euler angles are continuous and smooth:

    >>> import matplotlib.pyplot as plt
    >>> plt.plot(times_plot, angles_plot)
    >>> plt.plot(times, angles, 'x')
    >>> plt.title("Euler angles")
    >>> plt.show()

    The angular rate is also smooth:

    >>> plt.plot(times_plot, angular_rate_plot)
    >>> plt.plot(times, angular_rate, 'x')
    >>> plt.title("Angular rate")
    >>> plt.show()

    The angular acceleration is continuous, but not smooth. Also note that
    the angular acceleration is not a piecewise-linear function, because
    it is different from the second derivative of the rotation vector (which
    is a piecewise-linear function as in the cubic spline).

    >>> plt.plot(times_plot, angular_acceleration_plot)
    >>> plt.plot(times, angular_acceleration, 'x')
    >>> plt.title("Angular acceleration")
    >>> plt.show()
    
   g&.>c           
         |d   j                         }t        |      }t        |      }t        d|dd z  |ddd d f   z  d|dd z  |ddd d f   z  dd|d d z  d|dd  z  z   z        }d|d d |d dd f   dz  z  |dd  |dd d f   dz  z  z   z  }|dxx   d|d   z  |d   j	                  |      z  z  cc<   |dxx   d|d   z  |d   j	                  |d         z  z  cc<   t        | j                        D ]  }	t        ||      }
t        |d d |
d d       }||z
  }t        d||j                               }|j                  d	      }t        j                  ||d d z
        }||d d t        j                  || j                  dt        j                  |      z   z  k        s n t        ||      }
t        j                   ||d d f      }||
fS )
Nr   r   r   r3      r4   )r5   r5   )ra   r   )copyr(   r1   r\   dotrangeMAX_ITERr   r?   r   rK   reshaper	   absallTOLvstack)selfdtangular_ratesr#   angular_rate_firstr   A_invMb0	iterationr9   
delta_betar   angular_rates_newdeltas                  r   _solve_for_angular_ratesz'RotationSpline._solve_for_angular_ratesK  s1   *1-224.w727;+aOb2tT!122!BK"QrT4-..RWq2ab6z)*,
 '#2,CRCI"!44!"+12t8 223 4
1RUU1X\\*<===
2!bf*quyyr):;;;t}}- 	I:1mLK=k#2.0JZA ,VQ	 B 1 9 9' BFF,}Sb/AABE!2M#2vvedhh!bff5F.G*GHHI	 7q-H		#5}Sb7I"JKk))r   c                    ddl m} |j                  rt        d      t	        |      dk(  rt        d      t        j                  |t              }|j                  dk7  rt        d      t	        |      t	        |      k7  r-t        dj                  t	        |      t	        |                  t        j                  |      }t        j                  |dk        rt        d	      |d d
 j                         |dd  z  j                         }||d d d f   z  }t	        |      dk(  r|}n| j                  |||      \  }}|d d d f   }t        j                  dt	        |      dz
  df      }d|z  ||z  z   ||z  z   |dz  z  |d<   d|z  d|z  |z  z
  ||z  z
  |dz  z  |d<   ||d<   d|d<   || _        || _         |||      | _        y )Nr   )PPolyz,`rotations` must be a sequence of rotations.r   z.`rotations` must contain at least 2 rotations.rF   z`times` must be 1-dimensional.zkExpected number of rotations to be equal to number of timestamps given, got {} rotations and {} timestamps.z9Values in `times` must be in a strictly increasing order.ra   r   r3   r   r4   )scipy.interpolatery   single
ValueErrorr   r	   asarrayfloatndimformatdiffanyinv	as_rotvecrw   r    times	rotationsinterpolator)	rl   r   r   ry   rm   r#   rn   r9   coeffs	            r   __init__zRotationSpline.__init__l  s   +KLLy>QMNN

5.::?=>>u:Y' 2 %fS^SZ@B B
 WWU^66"'? 1 2 2 Sb>%%')AB-7BBD"QW+-y>Q'K)-)F)FM7*,&M; 4[!SZ!^Q/0L2#55;&'*,'2aK!b&="88;&'*,'2a aa
"!%/r   c                    |dvrt        d      t        j                  |t              }|j                  dkD  rt        d      |j                  dk(  }t        j
                  |      }| j                  |      }|dk(  r{t        j                  | j                  |d      }|dz  }d||dk  <   t        | j                        dz
  }|dz
  |||dz
  kD  <   | j                  |   t        j                  |      z  }n]|dk(  r| j                  |d      }t        ||      }n9|d	k(  r2| j                  |d      }| j                  |d	      }	t        |||	      }nJ |r|d   }|S )
a  Compute interpolated values.

        Parameters
        ----------
        times : float or array_like
            Times of interest.
        order : {0, 1, 2}, optional
            Order of differentiation:

                * 0 (default) : return Rotation
                * 1 : return the angular rate in rad/sec
                * 2 : return the angular acceleration in rad/sec/sec

        Returns
        -------
        Interpolated Rotation, angular rate or acceleration.
        )r   r   r   z`order` must be 0, 1 or 2.rF   r   z&`times` must be at most 1-dimensional.r   right)sider   )r|   r	   r}   r~   r   
atleast_1dr   searchsortedr   r   r   r   from_rotvecrA   rD   )
rl   r   order
singe_timer#   index
n_segmentsr   r9   rC   s
             r   __call__zRotationSpline.__call__  s_   $ 	!9::

5.::>EFFZZ1_
e$##E*A:OODJJGDEQJE E%!)TZZ1,J,6NE%*q.()^^E*X-A-A'-JJFaZ++E15K*7K@FaZ++E15K"//q9O27K3BDF 5AYFr   N)r   )	__name__
__module____qualname____doc__rf   rj   rw   r   r    r   r   r^   r^      s&    JX H
C*B,0\2r   r^   )numpyr	   scipy.linalgr   	_rotationr   r   r   r(   r1   r?   rA   rD   r\   r^   r   r   r   <module>r      sH     % ,)
 F$N,1^B$H(8vQ Qr   