
    Mi
$             
          S r SSKrSSKJrJr  SSKJrJrJ	r	  SSK
Jr  \R                  " S5      rS\R                  S	'   S
 rS rS rS rS rSuS jrS rSvS jrS rSwS jrSxS jrSxS jrS rS rS rSyS jrS rSzS jr S{S jr!S|S jr"S|S jr#S|S  jr$S|S! jr%S" r&S# r'SxS$ jr(S% r)S& r*S' r+S( r,S) r-S}S* jr.S~S+ jr/ SS,\	\   S-\S.\	\   4S/ jjr0 " S0 S15      r1S2 r2S3 r3S4 r4\Rj                  " \65      Rn                  S5-  r8/ S6Qr90 SS7_S8S9_S:S;_S<S=_S>S?_S@SA_SBSC_SDSE_SFSG_SHSI_SJSK_SLSM_SNSO_SPSQ_SRSS_STSU_SVSW_SXSYSZS[S\S]S^S_.Er:\:Rw                  5        V Vs0 s H  u  pX_M	     snn r<SvS` jr=SvSa jr>Sb r?SSc jr@SSd jrASe rBSf rCSg rDSh rESi rFSSj jrGSk rHS|Sl jrI SSm\Sn\S.\JSo\\R                     4Sp jjrLSSq jrMSSr jrNSvSs jrOSt rPgs  snn f )a$  Homogeneous Transformation Matrices and Quaternions.

A library for calculating 4x4 matrices for translating, rotating, reflecting,
scaling, shearing, projecting, orthogonalizing, and superimposing arrays of
3D homogeneous coordinates as well as for converting between rotation matrices,
Euler angles, and quaternions. Also includes an Arcball control object and
functions to decompose transformation matrices.

:Author:
  `Christoph Gohlke <http://www.lfd.uci.edu/~gohlke/>`_

:Organization:
  Laboratory for Fluorescence Dynamics, University of California, Irvine

:Version: 2017.02.17

Requirements
------------
* `CPython 2.7 or 3.4 <http://www.python.org>`_
* `numpy 1.9 <http://www.np.org>`_
* `Transformations.c 2015.03.19 <http://www.lfd.uci.edu/~gohlke/>`_
  (recommended for speedup of some functions)

Notes
-----
The API is not stable yet and is expected to change between revisions.

This Python code is not optimized for speed. Refer to the transformations.c
module for a faster implementation of some functions.

Documentation in HTML format can be generated with epydoc.

Matrices (M) can be inverted using np.linalg.inv(M), be concatenated using
np.dot(M0, M1), or transform homogeneous coordinate arrays (v) using
np.dot(M, v) for shape (4, *) column vectors, respectively
np.dot(v, M.T) for shape (*, 4) row vectors ("array of points").

This module follows the "column vectors on the right" and "row major storage"
(C contiguous) conventions. The translation components are in the right column
of the transformation matrix, i.e. M[:3, 3].
The transpose of the transformation matrices may have to be used to interface
with other graphics systems, e.g. with OpenGL's glMultMatrixd(). See also [16].

Calculations are carried out with np.float64 precision.

Vector, point, quaternion, and matrix function arguments are expected to be
"array like", i.e. tuple, list, or numpy arrays.

Return types are numpy arrays unless specified otherwise.

Angles are in radians unless specified otherwise.

Quaternions w+ix+jy+kz are represented as [w, x, y, z].

A triple of Euler angles can be applied/interpreted in 24 ways, which can
be specified using a 4 character string or encoded 4-tuple:

  *Axes 4-string*: e.g. 'sxyz' or 'ryxy'

  - first character : rotations are applied to 's'tatic or 'r'otating frame
  - remaining characters : successive rotation axis 'x', 'y', or 'z'

  *Axes 4-tuple*: e.g. (0, 0, 0, 0) or (1, 1, 1, 1)

  - inner axis: code of axis ('x':0, 'y':1, 'z':2) of rightmost matrix.
  - parity : even (0) if inner axis 'x' is followed by 'y', 'y' is followed
    by 'z', or 'z' is followed by 'x'. Otherwise odd (1).
  - repetition : first and last axis are same (1) or different (0).
  - frame : rotations are applied to static (0) or rotating (1) frame.

Other Python packages and modules for 3D transformations and quaternions:

* `Transforms3d <https://pypi.python.org/pypi/transforms3d>`_
   includes most code of this module.
* `Blender.mathutils <http://www.blender.org/api/blender_python_api>`_
* `numpy-dtypes <https://github.com/numpy/numpy-dtypes>`_

References
----------
(1)  Matrices and transformations. Ronald Goldman.
     In "Graphics Gems I", pp 472-475. Morgan Kaufmann, 1990.
(2)  More matrices and transformations: shear and pseudo-perspective.
     Ronald Goldman. In "Graphics Gems II", pp 320-323. Morgan Kaufmann, 1991.
(3)  Decomposing a matrix into simple transformations. Spencer Thomas.
     In "Graphics Gems II", pp 320-323. Morgan Kaufmann, 1991.
(4)  Recovering the data from the transformation matrix. Ronald Goldman.
     In "Graphics Gems II", pp 324-331. Morgan Kaufmann, 1991.
(5)  Euler angle conversion. Ken Shoemake.
     In "Graphics Gems IV", pp 222-229. Morgan Kaufmann, 1994.
(6)  Arcball rotation control. Ken Shoemake.
     In "Graphics Gems IV", pp 175-192. Morgan Kaufmann, 1994.
(7)  Representing attitude: Euler angles, unit quaternions, and rotation
     vectors. James Diebel. 2006.
(8)  A discussion of the solution for the best rotation to relate two sets
     of vectors. W Kabsch. Acta Cryst. 1978. A34, 827-828.
(9)  Closed-form solution of absolute orientation using unit quaternions.
     BKP Horn. J Opt Soc Am A. 1987. 4(4):629-642.
(10) Quaternions. Ken Shoemake.
     http://www.sfu.ca/~jwa3/cmpt461/files/quatut.pdf
(11) From quaternion to matrix and back. JMP van Waveren. 2005.
     http://www.intel.com/cd/ids/developer/asmo-na/eng/293748.htm
(12) Uniform random rotations. Ken Shoemake.
     In "Graphics Gems III", pp 124-132. Morgan Kaufmann, 1992.
(13) Quaternion in molecular modeling. CFF Karney.
     J Mol Graph Mod, 25(5):595-604
(14) New method for extracting the quaternion from a rotation matrix.
     Itzhack Y Bar-Itzhack, J Guid Contr Dynam. 2000. 23(6): 1085-1087.
(15) Multiple View Geometry in Computer Vision. Hartley and Zissermann.
     Cambridge University Press; 2nd Ed. 2004. Chapter 4, Algorithm 4.7, p 130.
(16) Column Vectors vs. Row Vectors.
     http://steve.hollasch.net/cgindex/math/matrix/column-vec.html

Examples
--------
>>> alpha, beta, gamma = 0.123, -1.234, 2.345
>>> origin, xaxis, yaxis, zaxis = [0, 0, 0], [1, 0, 0], [0, 1, 0], [0, 0, 1]
>>> I = identity_matrix()
>>> Rx = rotation_matrix(alpha, xaxis)
>>> Ry = rotation_matrix(beta, yaxis)
>>> Rz = rotation_matrix(gamma, zaxis)
>>> R = concatenate_matrices(Rx, Ry, Rz)
>>> euler = euler_from_matrix(R, 'rxyz')
>>> np.allclose([alpha, beta, gamma], euler)
True
>>> Re = euler_matrix(alpha, beta, gamma, 'rxyz')
>>> is_same_transform(R, Re)
True
>>> al, be, ga = euler_from_matrix(Re, 'rxyz')
>>> is_same_transform(Re, euler_matrix(al, be, ga, 'rxyz'))
True
>>> qx = quaternion_about_axis(alpha, xaxis)
>>> qy = quaternion_about_axis(beta, yaxis)
>>> qz = quaternion_about_axis(gamma, zaxis)
>>> q = quaternion_multiply(qx, qy)
>>> q = quaternion_multiply(q, qz)
>>> Rq = quaternion_matrix(q)
>>> is_same_transform(R, Rq)
True
>>> S = scale_matrix(1.23, origin)
>>> T = translation_matrix([1, 2, 3])
>>> Z = shear_matrix(beta, xaxis, origin, zaxis)
>>> R = random_rotation_matrix(np.random.rand(3))
>>> M = concatenate_matrices(T, R, Z, S)
>>> scale, shear, angles, trans, persp = decompose_matrix(M)
>>> np.allclose(scale, 1.23)
True
>>> np.allclose(trans, [1, 2, 3])
True
>>> np.allclose(shear, [0, np.tan(beta), 0])
True
>>> is_same_transform(R, euler_matrix(axes='sxyz', *angles))
True
>>> M1 = compose_matrix(scale, shear, angles, trans, persp)
>>> is_same_transform(M, M1)
True
>>> v0, v1 = random_vector(3), random_vector(3)
>>> M = rotation_matrix(angle_between_vectors(v0, v1), vector_product(v0, v1))
>>> v2 = np.dot(v0, M[:3,:3].T)
>>> np.allclose(unit_vector(v1), unit_vector(v2))
True

    N)	ArrayLikeNDArray   )IntegerNumberOptional)diagonal_dot   F	WRITEABLEc                  .    [         R                  " S5      $ )zReturn 4x4 identity/unit matrix.

>>> I = identity_matrix()
>>> np.allclose(I, np.dot(I, I))
True
>>> float(np.sum(I)), float(np.trace(I))
(4.0, 4.0)
>>> np.allclose(I, np.identity(4))
True

r
   )npidentity     q/var/www/eduai.edurigo.com/storigo/production/storigo_env/lib/python3.13/site-packages/trimesh/transformations.pyidentity_matrixr      s     ;;q>r   c                     [        U 5      n[        S U  5       5      (       a  SSKJn  U" US-   5      nO[        R                  " US-   5      nU SU USU2U4'   U$ )z
Return matrix to translate by direction vector.

>>> v = np.random.random(3) - 0.5
>>> np.allclose(v, translation_matrix(v)[:3, 3])
True

c              3   P   #    U  H  nS [        [        U5      5      ;   v   M     g7f)sympyN)strtype).0vs     r   	<genexpr>%translation_matrix.<locals>.<genexpr>   s     
6Iq7c$q'l"Is   $&r   )eyer   N)lenanyr   r   r   )	directiondimr   Ms       r   translation_matrixr"      s_     i.C 
6I
666aLFF37O Tc?AdsdCiLHr   c                 X    [         R                  " U 5      SS2S4   R                  5       $ )zReturn translation vector from translation matrix.

>>> v0 = np.random.random(3) - 0.5
>>> v1 = translation_from_matrix(translation_matrix(v0))
>>> np.allclose(v0, v1)
True

N   )r   asarraycopymatrixs    r   translation_from_matrixr)      s)     ::fbqb!e$))++r   c                     [        USS 5      n[        R                  " S5      nUSS2SS24==   S[        R                  " X5      -  -  ss'   S[        R                  " U SS U5      -  U-  USS2S4'   U$ )a{  Return matrix to mirror at plane defined by point and normal vector.

>>> v0 = np.random.random(4) - 0.5
>>> v0[3] = 1.
>>> v1 = np.random.random(3) - 0.5
>>> R = reflection_matrix(v0, v1)
>>> np.allclose(2, np.trace(R))
True
>>> np.allclose(v0, np.dot(R, v0))
True
>>> v2 = v0.copy()
>>> v2[:3] += v1
>>> v3 = v0.copy()
>>> v2[:3] -= v1
>>> np.allclose(v2, np.dot(R, v3))
True

Nr$   r
          @)unit_vectorr   r   outerdot)pointnormalr!   s      r   reflection_matrixr1   	  sz    & $F
AAbqb"1"fIrxx///IbffU2AY//69Abqb!eHHr   c                    [         R                  " U [         R                  S9n[         R                  R	                  USS2SS24   5      u  p#[         R
                  " [        [         R                  " U5      S-   5      S:  5      S   n[        U5      (       d  [        S5      e[         R                  " USS2US   4   5      R                  5       n[         R                  R	                  U5      u  p#[         R
                  " [        [         R                  " U5      S-
  5      S:  5      S   n[        U5      (       d  [        S5      e[         R                  " USS2US	   4   5      R                  5       nXfS   -  nXe4$ )
a+  Return mirror plane point and normal vector from reflection matrix.

>>> v0 = np.random.random(3) - 0.5
>>> v1 = np.random.random(3) - 0.5
>>> M0 = reflection_matrix(v0, v1)
>>> point, normal = reflection_from_matrix(M0)
>>> M1 = reflection_matrix(point, normal)
>>> is_same_transform(M0, M1)
True

dtypeNr$         ?:0yE>r   z2no unit eigenvector corresponding to eigenvalue -11no unit eigenvector corresponding to eigenvalue 1)r   r%   float64linalgeigwhereabsrealr   
ValueErrorsqueeze)r(   r!   wVir0   r/   s          r   reflection_from_matrixrD   #  s(    	

6,A99==2A2rr6#DA
RWWQZ#%&-.q1Aq66MNNWWQq!A$wZ ((*F99==DA
RWWQZ#%&-.q1Aq66LMMGGAa2hK ((*E	1XE=r   c           
         S[        [        U 5      5      ;   a)  SSKnSnUR                  U 5      nUR	                  U 5      nO.Sn[
        R                  " U 5      n[
        R                  " U 5      n[        USS 5      n[
        R                  " XfUS/5      nUSS2SS24==   [
        R                  " X5      SU-
  -  -  ss'   X-  nUSS2SS24==   [
        R                  " SUS	   * US
   /US	   SUS   * /US
   * US   S//5      -  ss'   UbP  [
        R                  " USS [
        R                  S9nU[
        R                  " USS2SS24   U5      -
  USS2S4'   U(       a  WR                  U5      $ U$ )a>  
Return matrix to rotate about axis defined by point and
direction.

Parameters
-------------
angle     : float, or sympy.Symbol
  Angle, in radians or symbolic angle
direction : (3,) float
  Any vector along rotation axis
point     : (3, ) float, or None
  Origin point of rotation axis

Returns
-------------
matrix : (4, 4) float, or (4, 4) sympy.Matrix
  Homogeneous transformation matrix

Examples
-------------
>>> R = rotation_matrix(np.pi/2, [0, 0, 1], [1, 0, 0])
>>> np.allclose(np.dot(R, [0, 0, 0, 1]), [1, -1, 0, 1])
True
>>> angle = (random.random() - 0.5) * (2*np.pi)
>>> direc = np.random.random(3) - 0.5
>>> point = np.random.random(3) - 0.5
>>> R0 = rotation_matrix(angle, direc, point)
>>> R1 = rotation_matrix(angle-2*np.pi, direc, point)
>>> is_same_transform(R0, R1)
True
>>> R0 = rotation_matrix(angle, direc, point)
>>> R1 = rotation_matrix(-angle, -direc, point)
>>> is_same_transform(R0, R1)
True
>>> I = np.identity(4, np.float64)
>>> np.allclose(I, rotation_matrix(np.pi*2, direc))
True
>>> np.allclose(2, np.trace(rotation_matrix(np.pi/2,direc,point)))
True

r   r   NTFr$   r5              r   r3   )r   r   r   sincosr   r,   diagr-   arrayr%   r9   r.   Matrix)angler   r/   spsymbolicsinacosar!   s           r   rotation_matrixrR   @  s   T #d5k""vve}vve}vve}vve}IbqM*I 	T3'(Abqb"1"fI)/3:>>I Ibqb"1"fI9Q<-1.q\31.l]IaL#.	
 I 

5!9BJJ7266!BQBF)U33"1"a% yy|Hr   c                 ~   [         R                  " U [         R                  S9nUSS2SS24   n[         R                  R	                  UR
                  5      u  p4[         R                  " [        [         R                  " U5      S-
  5      S:  5      S   n[        U5      (       d  [        S5      e[         R                  " USS2US   4   5      R                  5       n[         R                  R	                  U5      u  p7[         R                  " [        [         R                  " U5      S-
  5      S:  5      S   n[        U5      (       d  [        S5      e[         R                  " USS2US   4   5      R                  5       nXS   -  n[         R                  " U5      S-
  S	-  n	[        US
   5      S:  a  US   U	S-
  US   -  US   -  -   US
   -  n
OM[        US   5      S:  a  US   U	S-
  US   -  US
   -  -   US   -  n
OUS   U	S-
  US   -  US
   -  -   US   -  n
[         R                  " X5      nXU4$ )ae  Return rotation angle and axis from rotation matrix.

>>> angle = (random.random() - 0.5) * (2*np.pi)
>>> direc = np.random.random(3) - 0.5
>>> point = np.random.random(3) - 0.5
>>> R0 = rotation_matrix(angle, direc, point)
>>> angle, direc, point = rotation_from_matrix(R0)
>>> R1 = rotation_matrix(angle, direc, point)
>>> is_same_transform(R0, R1)
True

r3   Nr$   r5   r6   r   r7   r8   r+   rG   r   r   r   r   rG   rG   r   )r   r%   r9   r:   r;   Tr<   r=   r>   r   r?   r@   tracearctan2)r(   RR33rA   WrC   r   Qr/   rQ   rP   rM   s               r   rotation_from_matrixr^     s    	

6,A
BQBF)C99==DA
RWWQZ#%&-.q1Aq66LMM!QrU($,,.I99==DA
RWWQZ#%&-.q1Aq66LMMGGAa2hK ((*E	1XEHHSMC3&D
9Q<4$4#:15	!DD	RST	Yq\	T	!$4#:15	!DD	RST$4#:15	!DD	RSTJJt"EU""r   c                    Uc>  [         R                  " X U S/5      nUb   USS USS2S4'   USS2S4==   SU -
  -  ss'   U$ [        USS 5      nSU -
  n [         R                  " S5      nUSS2SS24==   U [         R                  " X"5      -  -  ss'   Ub'  U [         R
                  " USS U5      -  U-  USS2S4'   U$ )a  Return matrix to scale by factor around origin in direction.

Use factor -1 for point symmetry.

>>> v = (np.random.rand(4, 5) - 0.5) * 20
>>> v[3] = 1
>>> S = scale_matrix(-1.234)
>>> np.allclose(np.dot(S, v)[:3], -1.234*v[:3])
True
>>> factor = random.random() * 10 - 5
>>> origin = np.random.random(3) - 0.5
>>> direct = np.random.random(3) - 0.5
>>> S = scale_matrix(factor, origin)
>>> S = scale_matrix(factor, origin, direct)

Nr5   r$   r
   )r   rJ   r,   r   r-   r.   )factororiginr   r!   s       r   scale_matrixrb     s    " GGVVS12bqzAbqb!eHbqb!eHf$H H  	"1.	vKKN	"1"bqb&	Vbhhy<<<	r
I!>>)KAbqb!eHHr   c                 Z   [         R                  " U [         R                  S9nUSS2SS24   n[         R                  " U5      S-
  n [         R                  R                  U5      u  pE[         R                  " [        [         R                  " U5      U-
  5      S:  5      S   S   n[         R                  " USS2U4   5      R                  5       nU[        U5      -  n[         R                  R                  U5      u  pE[         R                  " [        [         R                  " U5      S-
  5      S:  5      S   n[        U5      (       d  [        S	5      e[         R                  " USS2US
   4   5      R                  5       nXS   -  nX8U4$ ! [         a    US-   S-  nSn Nf = f)a  Return scaling factor, origin and direction from scaling matrix.

>>> factor = random.random() * 10 - 5
>>> origin = np.random.random(3) - 0.5
>>> direct = np.random.random(3) - 0.5
>>> S0 = scale_matrix(factor, origin)
>>> factor, origin, direction = scale_from_matrix(S0)
>>> S1 = scale_matrix(factor, origin, direction)
>>> is_same_transform(S0, S1)
True
>>> S0 = scale_matrix(factor, origin, direct)
>>> factor, origin, direction = scale_from_matrix(S0)
>>> S1 = scale_matrix(factor, origin, direction)
>>> is_same_transform(S0, S1)
True

r3   Nr$   r+   r6   r         @r5   ,no eigenvector corresponding to eigenvalue 1r8   )r   r%   r9   rX   r:   r;   r<   r=   r>   r@   vector_norm
IndexErrorr   r?   )	r(   r!   M33r`   rA   rB   rC   r   ra   s	            r   scale_from_matrixri     sh   $ 	

6,A
BQBF)CXXc]S F	yy}}S!HHSf,-45a8;GGAadG$,,.	[++	 99==DA
RWWQZ#%&-.q1Aq66GHHWWQq!B%x[!))+F
QiF9$$  3,#%	s   	BF F*)F*c                 ^   [         R                  " S5      n[         R                  " U SS [         R                  S9n [	        USS 5      nUGb	  [         R                  " USS [         R                  S9n[         R
                  " X0-
  U5      =US'   =US'   US'   USS2SS24==   [         R                  " X15      -  ss'   U(       aJ  USS2SS24==   [         R                  " X5      -  ss'   [         R
                  " X5      X1-   -  USS2S4'   O [         R
                  " X5      U-  USS2S4'   U* USSS24'   [         R
                  " X15      US'   U$ Ub  [         R                  " USS [         R                  S9n[         R
                  " X!5      nUSS2SS24==   [         R                  " X!5      U-  -  ss'   U[         R
                  " X5      U-  -  USS2S4'   U$ USS2SS24==   [         R                  " X5      -  ss'   [         R
                  " X5      U-  USS2S4'   U$ )	a  Return matrix to project onto plane defined by point and normal.

Using either perspective point, projection direction, or none of both.

If pseudo is True, perspective projections will preserve relative depth
such that Perspective = dot(Orthogonal, PseudoPerspective).

>>> P = projection_matrix([0, 0, 0], [1, 0, 0])
>>> np.allclose(P[1:, 1:], np.identity(4)[1:, 1:])
True
>>> point = np.random.random(3) - 0.5
>>> normal = np.random.random(3) - 0.5
>>> direct = np.random.random(3) - 0.5
>>> persp = np.random.random(3) - 0.5
>>> P0 = projection_matrix(point, normal)
>>> P1 = projection_matrix(point, normal, direction=direct)
>>> P2 = projection_matrix(point, normal, perspective=persp)
>>> P3 = projection_matrix(point, normal, perspective=persp, pseudo=True)
>>> is_same_transform(P2, np.dot(P0, P3))
True
>>> P = projection_matrix([3, 0, 0], [1, 1, 0], [1, 0, 0])
>>> v0 = (np.random.rand(4, 5) - 0.5) * 20
>>> v0[3] = 1
>>> v1 = np.dot(P, v0)
>>> np.allclose(v1[1], v0[1])
True
>>> np.allclose(v1[0], 3-v1[1])
True

r
   Nr$   r3   r   r   r   r   rG   rG   r$   r$   )r   r   r%   r9   r,   r.   r-   )r/   r0   r   perspectivepseudor!   scales          r   projection_matrixrr     s   > 	AAJJuRay

3E$FjjRa

C&(ff[-@&&II$I!D'AdG	"1"bqb&	RXXk22	bqb"1"fI&11Ivve,0DEAbqb!eHvve,{:Abqb!eH7!RaR%&&-$ H 
	JJy!}BJJ?	y)	"1"bqb&	RXXi0588	u 5 =>"1"a%
 H 	
"1"bqb&	RXXf--	66%(61"1"a%Hr   c                    [         R                  " U [         R                  S9nUSS2SS24   n[         R                  R	                  U5      u  pE[         R
                  " [        [         R                  " U5      S-
  5      S:  5      S   nU(       Gd  [        U5      (       Ga  [         R                  " USS2US   4   5      R                  5       nXwS   -  n[         R                  R	                  U5      u  pE[         R
                  " [        [         R                  " U5      5      S:  5      S   n[        U5      (       d  [        S5      e[         R                  " USS2US   4   5      R                  5       nU[        U5      -  n[         R                  R	                  UR                  5      u  pE[         R
                  " [        [         R                  " U5      5      S:  5      S   n[        U5      (       aB  [         R                  " USS2US   4   5      R                  5       n	U	[        U	5      -  n	XyUSS	4$ XxSSS	4$ [         R
                  " [        [         R                  " U5      5      S:  5      S   n[        U5      (       d  [        S
5      e[         R                  " USS2US   4   5      R                  5       nXwS   -  nUSSS24   * n	USS2S4   [         R                  " USS U	5      -  n
U(       a  X-  n
XySX4$ )a"  Return projection plane and perspective point from projection matrix.

Return values are same as arguments for projection_matrix function:
point, normal, direction, perspective, and pseudo.

>>> point = np.random.random(3) - 0.5
>>> normal = np.random.random(3) - 0.5
>>> direct = np.random.random(3) - 0.5
>>> persp = np.random.random(3) - 0.5
>>> P0 = projection_matrix(point, normal)
>>> result = projection_from_matrix(P0)
>>> P1 = projection_matrix(*result)
>>> is_same_transform(P0, P1)
True
>>> P0 = projection_matrix(point, normal, direct)
>>> result = projection_from_matrix(P0)
>>> P1 = projection_matrix(*result)
>>> is_same_transform(P0, P1)
True
>>> P0 = projection_matrix(point, normal, perspective=persp, pseudo=False)
>>> result = projection_from_matrix(P0, pseudo=False)
>>> P1 = projection_matrix(*result)
>>> is_same_transform(P0, P1)
True
>>> P0 = projection_matrix(point, normal, perspective=persp, pseudo=True)
>>> result = projection_from_matrix(P0, pseudo=True)
>>> P1 = projection_matrix(*result)
>>> is_same_transform(P0, P1)
True

r3   Nr$   r5   r6   r   r8   z,no eigenvector corresponding to eigenvalue 0Fz0no eigenvector not corresponding to eigenvalue 0)r   r%   r9   r:   r;   r<   r=   r>   r   r@   r?   rf   rW   r.   )r(   rp   r!   rh   rA   rB   rC   r/   r   r0   ro   s              r   projection_from_matrixrt   @  sx   @ 	

6,A
BQBF)C99==DA
RWWQZ#%&-.q1Ac!ff!QrU($,,.qyy}}S!HHS_t+,Q/1vvKLLGGAa1gJ'//1	[++	yy}}SUU#HHS_t+,Q/q66WWQq!A$wZ(002Fk&))F)T588 T466 HHS_t+,Q/1vvOPP!QrU($,,.qArrE(Ahbq	6!::!KdK77r   c                    X:  d
  X#:  d  XE:  a  [        S5      eU(       aU  U[        ::  a  [        S5      eSU-  nXpU-
  -  SX-   X-
  -  S/SXrU-
  -  X2-   X2-
  -  S/SSXT-   XE-
  -  Xu-  XT-
  -  // SQ/nO8SX-
  -  SSX-   X-
  -  /SSX2-
  -  SX2-   X#-
  -  /SSSXT-
  -  XT-   XE-
  -  // SQ/n[        R                  " U5      $ )a  Return matrix to obtain normalized device coordinates from frustum.

The frustum bounds are axis-aligned along x (left, right),
y (bottom, top) and z (near, far).

Normalized device coordinates are in range [-1, 1] if coordinates are
inside the frustum.

If perspective is True the frustum is a truncated pyramid with the
perspective point at origin and direction along z axis, otherwise an
orthographic canonical view volume (a box).

Homogeneous coordinates transformed by the perspective clip matrix
need to be dehomogenized (divided by w coordinate).

>>> frustum = np.random.rand(6)
>>> frustum[1] += frustum[0]
>>> frustum[3] += frustum[2]
>>> frustum[5] += frustum[4]
>>> M = clip_matrix(perspective=False, *frustum)
>>> a = np.dot(M, [frustum[0], frustum[2], frustum[4], 1])
>>> np.allclose(a, [-1., -1., -1.,  1.])
True
>>> b = np.dot(M, [frustum[1], frustum[3], frustum[5], 1])
>>> np.allclose(b, [ 1.,  1.,  1.,  1.])
True
>>> M = clip_matrix(perspective=True, *frustum)
>>> v = np.dot(M, [frustum[0], frustum[2], frustum[4], 1])
>>> c = v / v[3]
>>> np.allclose(c, [-1., -1., -1.,  1.])
True
>>> v = np.dot(M, [frustum[1], frustum[3], frustum[4], 1])
>>> d = v / v[3]
>>> np.allclose(d, [ 1.,  1., -1.,  1.])
True

zinvalid frustumzinvalid frustum: near <= 0r+   rF   )rF   rF         rF   rF   rF   rF   r5   )r?   _EPSr   rK   )	leftrightbottomtopnearfarro   tr!   s	            r   clip_matrixr     s   L }*++4<9::$J,u|&EsK!|$s|&EsK#
tz2AGsz4JK!	
 EL!3elt|-LM#&clv|-LM#scj)CJ4:+FG 	
 88A;r   c                    [        USS 5      n[        USS 5      n[        [        R                  " X15      5      S:  a  [	        S5      e[        R
                  " U 5      n [        R                  " S5      nUSS2SS24==   U [        R                  " X5      -  -  ss'   U * [        R                  " USS U5      -  U-  USS2S4'   U$ )a  Return matrix to shear by angle along direction vector on shear plane.

The shear plane is defined by a point and normal vector. The direction
vector must be orthogonal to the plane's normal vector.

A point P is transformed by the shear matrix into P" such that
the vector P-P" is parallel to the direction vector and its extent is
given by the angle of P-P'-P", where P' is the orthogonal projection
of P onto the shear plane.

>>> angle = (random.random() - 0.5) * 4*np.pi
>>> direct = np.random.random(3) - 0.5
>>> point = np.random.random(3) - 0.5
>>> normal = np.cross(direct, np.random.random(3))
>>> S = shear_matrix(angle, direct, point, normal)
>>> np.allclose(1, np.linalg.det(S))
True

Nr$   gư>z/direction and normal vectors are not orthogonalr
   )r,   r=   r   r.   r?   tanr   r-   )rM   r   r/   r0   r!   s        r   shear_matrixr     s    ( $FIbqM*I
266&$%,JKKFF5ME
AAbqb"1"fI)444IvuRay&11I=Abqb!eHHr   c                 J   [         R                  " U [         R                  S9nUSS2SS24   n[         R                  R	                  U5      u  p4[         R
                  " [        [         R                  " U5      S-
  5      S:  5      S   n[        U5      S:  a  [        SU 35      e[         R                  " USS2U4   5      R                  5       R                  nS	nS
 H6  u  px[         R                  " XG   XH   5      n	[        U	5      nX6:  d  M2  UnU	n
M8     W
U-  n
[         R                  " U[         R                  " S5      -
  U
5      n[        U5      nX-  n[         R                   " U5      n[         R                  R	                  U5      u  p4[         R
                  " [        [         R                  " U5      S-
  5      S:  5      S   n[        U5      (       d  [        S5      e[         R                  " USS2US   4   5      R                  5       nXS   -  nXX4$ )a  Return shear angle, direction and plane from shear matrix.

>>> angle  = np.pi / 2.0
>>> direct = [0.0, 1.0, 0.0]
>>> point  = [0.0, 0.0, 0.0]
>>> normal = np.cross(direct, np.roll(direct,1))
>>> S0 = shear_matrix(angle, direct, point, normal)
>>> angle, direct, point, normal = shear_from_matrix(S0)
>>> S1 = shear_matrix(angle, direct, point, normal)
>>> is_same_transform(S0, S1)
True

r3   Nr$   r5   g-C6?r   rG   z-no two linear independent eigenvectors found rv   )r   r   rU   r   rG   r6   re   r8   )r   r%   r9   r:   r;   r<   r=   r>   r   r?   r@   rW   crossrf   r.   r   arctan)r(   r!   rh   rA   rB   rC   lenormi0i1nr0   r   rM   r/   s                 r   shear_from_matrixr     s    	

6,A
BQBF)C99==DA
RWWQZ#%&-.q1A
1vzHLMM
!Q$  "$$AF*HHQUAE"N:FF + fFsR[[^+V4I	"EIIIeE99==DA
RWWQZ#%&-.q1Aq66GHHGGAa2hK ((*E	1XEU**r   c                 j   [         R                  " U [         R                  S9R                  n[	        US   5      [
        :  a  [        S5      eXS   -  nUR                  5       nSUSS2S4'   [         R                  R                  U5      (       d  [        S5      e[         R                  " S5      n/ S	Qn/ S	Qn[        [	        USS2S4   5      [
        :  5      (       aO  [         R                  " USS2S4   [         R                  R                  UR                  5      5      nSUSS2S4'   O[         R                  " / SQ5      nUSSS24   R                  5       nS
USSS24'   USS2SS24   R                  5       n[        US   5      US'   US==   US   -  ss'   [         R                  " US   US   5      US'   US==   US   US   -  -  ss'   [        US   5      US'   US==   US   -  ss'   US==   US   -  ss'   [         R                  " US   US   5      US'   US==   US   US   -  -  ss'   [         R                  " US   US   5      US'   US==   US   US   -  -  ss'   [        US   5      US'   US==   US   -  ss'   USS=== US   -  sss& [         R                  " US   [         R                  " US   US   5      5      S:  a,  [         R                   " X35        [         R                   " X5        [         R"                  " US   * 5      US'   [         R$                  " US   5      (       aA  [         R&                  " US   US   5      US'   [         R&                  " US   US   5      US'   O&[         R&                  " US   * US   5      US'   S
US'   X4XWU4$ )a  Return sequence of transformations from transformation matrix.

matrix : array_like
    Non-degenerative homogeneous transformation matrix

Return tuple of:
    scale : vector of 3 scaling factors
    shear : list of shear factors for x-y, x-z, y-z axes
    angles : list of Euler angles about static x, y, z axes
    translate : translation vector along x, y, z axes
    perspective : perspective partition of matrix

Raise ValueError if matrix is of wrong type or degenerative.

>>> T0 = translation_matrix([1, 2, 3])
>>> scale, shear, angles, trans, persp = decompose_matrix(T0)
>>> T1 = translation_matrix(trans)
>>> np.allclose(T0, T1)
True
>>> S = scale_matrix(0.123)
>>> scale, shear, angles, trans, persp = decompose_matrix(S)
>>> bool(np.isclose(scale[0], 0.123))
True
>>> R0 = euler_matrix(1, 2, 3)
>>> scale, shear, angles, trans, persp = decompose_matrix(R0)
>>> R1 = euler_matrix(*angles)
>>> np.allclose(R0, R1)
True

r3   rn   zM[3, 3] is zerorw   Nr$   zmatrix is singular)r$   )rF   rF   rF   rF   r   r   rG   rU   r   rm   r   rk   rV   rl   )r   rK   r9   rW   r=   rx   r?   r&   r:   detzerosr   r.   invrf   r   negativearcsinrI   rY   )	r(   r!   Prq   shearanglesro   	translaterows	            r   decompose_matrixr     sL   > 	rzz*,,A
1T7|d*++4LA	A AadG99==-..HHTNEEF
3q!Qx=4  ffQq!tWbiimmACC&89$!Q$hh34!RaR%IAa!eH
BQBF)..
C3q6"E!HFeAhFvvc!fc!f%E!HFc!fuQxF3q6"E!HFeAhF	!HaHvvc!fc!f%E!HFc!fuQxFvvc!fc!f%E!HFc!fuQxF3q6"E!HFeAhF	!"IqI	vvc!fbhhs1vs1v./!3
E!
C		3t9*%F1I	vvfQiJJs4y#d)4q	JJs4y#d)4q	JJD	z3t95q	q	K77r   c                    [         R                  " S5      nUb8  [         R                  " S5      nUSS USSS24'   [         R                  " XV5      nUb8  [         R                  " S5      nUSS USS2S4'   [         R                  " XW5      nUb-  [        US   US   US   S5      n[         R                  " XX5      nUbD  [         R                  " S5      n	US   U	S'   US   U	S	'   US   U	S
'   [         R                  " XY5      nU bD  [         R                  " S5      n
U S   U
S'   U S   U
S'   U S   U
S'   [         R                  " XZ5      nXUS   -  nU$ )a
  Return transformation matrix from sequence of transformations.

This is the inverse of the decompose_matrix function.

Sequence of transformations:
    scale : vector of 3 scaling factors
    shear : list of shear factors for x-y, x-z, y-z axes
    angles : list of Euler angles about static x, y, z axes
    translate : translation vector along x, y, z axes
    perspective : perspective partition of matrix

>>> scale = np.random.random(3) - 0.5
>>> shear = np.random.random(3) - 0.5
>>> angles = (np.random.random(3) - 0.5) * (2*np.pi)
>>> trans = np.random.random(3) - 0.5
>>> persp = np.random.random(4) - 0.5
>>> M0 = compose_matrix(scale, shear, angles, trans, persp)
>>> result = decompose_matrix(M0)
>>> M1 = compose_matrix(*result)
>>> is_same_transform(M0, M1)
True

r
   Nr$   r   r   rG   sxyzr   rU   r   rk   rl   rm   rn   )r   r   r.   euler_matrix)rq   r   r   r   ro   r!   r   rW   rZ   ZSs              r   compose_matrixr   f  sH   0 	AAKKNbq/!Q$FF1LKKNRa="1"a%FF1LF1Ivay&AFF1LKKN($($($FF1LKKN($($($FF1L4LAHr   c                 Z   U u  p#n[         R                  " U5      n[         R                  " U5      u  pVn[         R                  " U5      u  pn
X-  U
-
  XV-  -  n[         R                  " X&-  [         R
                  " SX-  -
  5      -  SSS/U* U-  U-  X5-  SS/X)-  X8-  US// SQ/5      $ )a  Return orthogonalization matrix for crystallographic cell coordinates.

Angles are expected in degrees.

The de-orthogonalization matrix is the inverse.

>>> O = orthogonalization_matrix([10, 10, 10], [90, 90, 90])
>>> np.allclose(O[:3, :3], np.identity(3, float) * 10)
True
>>> O = orthogonalization_matrix([9.8, 12.0, 15.5], [87.2, 80.7, 69.7])
>>> np.allclose(np.sum(O), 43.063229)
True

r5   rF   rw   )r   radiansrH   rI   rK   sqrt)lengthsr   abcrP   sinb_rQ   cosbcosgcos               r   orthogonalization_matrixr     s     GA!ZZFFF6NMDvvf~D
+
	-B88Xbg..S#>R$Y^QXsC0XqxC( 		
 r   c           	      	   [         R                  " U [         R                  S9n [         R                  " U[         R                  S9nU R                  S   nUS:  d-  U R                  S   U:  d  U R                  UR                  :w  a  [	        S5      e[         R
                  " U SS9* n[         R                  " US-   5      nXgSU2U4'   XR                  US5      -  n [         R
                  " USS9* n[         R                  " US-   5      n	XSU2U4'   XR                  US5      -  nU(       a  [         R                  " X4SS9n
[         R                  R                  U
R                  5      u  pnUSU R                  nUSU nXSU-   n[         R                  " U[         R                  R                  U5      5      n[         R                  " U[         R                  " US45      4SS9n[         R                  " USU-  S	-   45      nGOU(       d  US
:w  a  [         R                  R                  [         R                  " XR                  5      5      u  pn[         R                  " X5      n[         R                  R!                  U5      S:  a=  U[         R"                  " USS2US-
  4   XS-
  SS24   S-  5      -  nUS==   S-  ss'   [         R                  " US-   5      nUUSU2SU24'   GO[         R$                  " X-  SS9u  nnn[         R$                  " U [         R&                  " USSS9-  SS9u  nnn[         R$                  " U [         R&                  " USSS9-  SS9u  nnnUU-   U-   SSS/UU-
  UU-
  U-
  SS/UU-
  UU-   UU-
  U-
  S/UU-
  UU-   UU-   UU-
  U-
  //n[         R                  R)                  U5      u  nnUSS2[         R*                  " U5      4   nU[-        U5      -  n[/        U5      nU(       aa  U(       dZ  X -  n X-  nUSU2SU24==   [         R0                  " [         R$                  " U5      [         R$                  " U 5      -  5      -  ss'   [         R                  " [         R                  R3                  U	5      [         R                  " UU5      5      nUUXU4   -  nU$ )a  Return affine transform matrix to register two point sets.

v0 and v1 are shape (ndims, *) arrays of at least ndims non-homogeneous
coordinates, where ndims is the dimensionality of the coordinate space.

If shear is False, a similarity transformation matrix is returned.
If also scale is False, a rigid/Euclidean transformation matrix
is returned.

By default the algorithm by Hartley and Zissermann [15] is used.
If usesvd is True, similarity and Euclidean transformation matrices
are calculated by minimizing the weighted sum of squared deviations
(RMSD) according to the algorithm by Kabsch [8].
Otherwise, and if ndims is 3, the quaternion based algorithm by Horn [9]
is used, which is slower when using this Python implementation.

The returned matrix performs rotation, translation and uniform scaling
(if specified).

>>> v0 = [[0, 1031, 1031, 0], [0, 0, 1600, 1600]]
>>> v1 = [[675, 826, 826, 677], [55, 52, 281, 277]]
>>> mat = affine_matrix_from_points(v0, v1)
>>> T = translation_matrix(np.random.random(3)-0.5)
>>> R = random_rotation_matrix(np.random.random(3))
>>> S = scale_matrix(random.random())
>>> M = concatenate_matrices(T, R, S)
>>> v0 = (np.random.rand(4, 100) - 0.5) * 20
>>> v0[3] = 1
>>> v1 = np.dot(M, v0)
>>> v0[:3] += np.random.normal(0, 1e-8, 300).reshape(3, -1)
>>> M = affine_matrix_from_points(v0[:3], v1[:3])
>>> check = np.allclose(v1, np.dot(M, v0))

More examples in superimposition_matrix()

r3   r   rG   r   z'input arrays are of wrong shape or typeaxisNrF   )r5   r$   rF   r+   r8   rv   )r   rK   r9   shaper?   meanr   reshapeconcatenater:   svdrW   r.   pinvr   vstackr   r-   sumrolleighargmaxrf   quaternion_matrixr   r   ) v0v1r   rq   usesvdndimst0M0t1M1AusvhBCr   r!   rZ   xxyyzzxyyzzxxzyxzyNrA   rB   qs                                    r   affine_matrix_from_pointsr     s   J 
"BJJ	'B	"BJJ	'BHHQKEqyBHHQK%'288rxx+?BCC ''"1
	B	UQY	Bvvu}**UA
B
''"1
	B	UQY	Bvvu}**UA
BNNB8!,99==%bZ\\vJq5y!FF1biinnQ'(NNArxx
341=IIq6E>V345	5A:99==DD!12bFF1M99==c!!AuqyL/2ail+;c+ABBAbETMEKK	"&5&&5&. VVBG!,
BVVBRa!88qA
BVVBRa!88qA
B"Wr\3S)"Wb2glC-"Wb2grBw|S1"Wb2grBwR"5	
 yy~~a 1a1o	[^a U

	&5&&5&.RWWRVVBZ"&&*%<== 	ryy}}R "&&B-0A5<AHr   c                     [         R                  " U [         R                  S9SS n [         R                  " U[         R                  S9SS n[        XSX#S9$ )a  Return matrix to transform given 3D point set into second point set.

v0 and v1 are shape (3, *) or (4, *) arrays of at least 3 points.

The parameters scale and usesvd are explained in the more general
affine_matrix_from_points function.

The returned matrix is a similarity or Euclidean transformation matrix.
This function has a fast C implementation in transformations.c.

>>> v0 = np.random.rand(3, 10)
>>> M = superimposition_matrix(v0, v0)
>>> np.allclose(M, np.identity(4))
True
>>> R = random_rotation_matrix(np.random.random(3))
>>> v0 = [[1,0,0], [0,1,0], [0,0,1], [1,1,1]]
>>> v1 = np.dot(R, v0)
>>> M = superimposition_matrix(v0, v1)
>>> np.allclose(v1, np.dot(M, v0))
True
>>> v0 = (np.random.rand(4, 100) - 0.5) * 20
>>> v0[3] = 1
>>> v1 = np.dot(R, v0)
>>> M = superimposition_matrix(v0, v1)
>>> np.allclose(v1, np.dot(M, v0))
True
>>> S = scale_matrix(random.random())
>>> T = translation_matrix(np.random.random(3)-0.5)
>>> M = concatenate_matrices(T, R, S)
>>> v1 = np.dot(M, v0)
>>> v0[:3] += np.random.normal(0, 1e-9, 300).reshape(3, -1)
>>> M = superimposition_matrix(v0, v1, scale=True)
>>> np.allclose(v1, np.dot(M, v0))
True
>>> M = superimposition_matrix(v0, v1, scale=True, usesvd=False)
>>> np.allclose(v1, np.dot(M, v0))
True
>>> v = np.zeros((4, 100, 3))
>>> v[:, :, 0] = v0
>>> M = superimposition_matrix(v0, v1, scale=True, usesvd=False)
>>> np.allclose(v1, np.dot(M, v[:, :, 0]))
True

r3   Nr$   F)r   rq   r   )r   r%   r9   r   )r   r   rq   r   s       r   superimposition_matrixr   #  sJ    Z 
Bbjj	)"1	-B	Bbjj	)"1	-B$R5UUr   r   c                     [         U   u  pEpgUn[        X-      n	[        X-
  S-      n
U(       a  X p U(       a  U * U* U* p!n S[        [        U 5      5      ;   a  SSKJnJ	nJ
n  U" S5      nO5[        R                  [        R                  p[        R                  " S5      nU" U 5      U" U5      U" U5      nnnU" U 5      U" U5      U" U5      nnnUU-  UU-  nnUU-  UU-  nnU(       a_  UXU4'   UU-  XU	4'   UU-  XU
4'   UU-  XU4'   U* U-  U-   XU	4'   U* U-  U-
  XU
4'   U* U-  XU4'   UU-  U-   XU	4'   UU-  U-
  XU
4'   U$ UU-  XU4'   UU-  U-
  XU	4'   UU-  U-   XU
4'   UU-  XU4'   UU-  U-   XU	4'   UU-  U-
  XU
4'   U* XU4'   UU-  XU	4'   UU-  XU
4'   U$ ! [        [        4 a    [        U     Uu  pEpg GNf = f)a>  Return homogeneous rotation matrix from Euler angles and axis sequence.

ai, aj, ak : Euler's roll, pitch and yaw angles
axes : One of 24 axis sequences as string or encoded tuple

>>> R = euler_matrix(1, 2, 3, 'syxz')
>>> np.allclose(np.sum(R[0]), -1.34786452)
True
>>> R = euler_matrix(1, 2, 3, (0, 1, 0, 1))
>>> np.allclose(np.sum(R[0]), -0.383436184)
True
>>> ai, aj, ak = (4*np.pi) * (np.random.random(3) - 0.5)
>>> for axes in _AXES2TUPLE.keys():
...    R = euler_matrix(ai, aj, ak, axes)
>>> for axes in _TUPLE2AXES.keys():
...    R = euler_matrix(ai, aj, ak, axes)

r   r   r   )rI   r   rH   r
   )_AXES2TUPLEAttributeErrorKeyError_TUPLE2AXES
_NEXT_AXISr   r   r   rI   r   rH   r   )aiajakaxes	firstaxisparity
repetitionframerC   jkrI   r   rH   r!   sisjskcicjckcccsscsss                            r   r   r   U  sP   &4/:4/@,	:
 	A1:A1:>"ABS2#s#d2h- 	('F66266SFF1IR#b'3r7BBR#b'3r7BB"Wb2gB"Wb2gBQ$r'Q$r'Q$r'Q$#(R-Q$#(R-Q$#(Q$r'B,Q$r'B,Q$ H r'Q$r'B,Q$r'B,Q$r'Q$r'B,Q$r'B,Q$#Q$r'Q$r'Q$Ha H% 4D/3,	:u4s   F( (G
	G
c                     [         UR                  5          u  p#pEUn[
        Xc-      n[
        Xc-
  S-      n[        R                  " U [        R                  S9SS2SS24   n	U(       a  [        R                  " XU4   XU4   -  XU4   XU4   -  -   5      n
U
[        :  a[  [        R                  " XU4   XU4   5      n[        R                  " XXf4   5      n[        R                  " XU4   XU4   * 5      nGO[        R                  " XU4   * XU4   5      n[        R                  " XXf4   5      nSnO[        R                  " XU4   XU4   -  XU4   XU4   -  -   5      nU[        :  a[  [        R                  " XU4   XU4   5      n[        R                  " XU4   * U5      n[        R                  " XU4   XU4   5      nO>[        R                  " XU4   * XU4   5      n[        R                  " XU4   * U5      nSnU(       a  U* U* U* pnU(       a  XpXU4$ ! [        [        4 a    [        U     Uu  p#pE GN.f = f)a`  Return Euler angles from rotation matrix for specified axis sequence.

axes : One of 24 axis sequences as string or encoded tuple

Note that many Euler angle triplets can describe one matrix.

>>> R0 = euler_matrix(1, 2, 3, 'syxz')
>>> al, be, ga = euler_from_matrix(R0, 'syxz')
>>> R1 = euler_matrix(al, be, ga, 'syxz')
>>> np.allclose(R0, R1)
True
>>> angles = (4*np.pi) * (np.random.random(3) - 0.5)
>>> for axes in _AXES2TUPLE.keys():
...    R0 = euler_matrix(axes=axes, *angles)
...    R1 = euler_matrix(axes=axes, *euler_from_matrix(R0, axes))
...    if not np.allclose(R0, R1): print(axes, "failed")

r   r3   Nr$   rF   )r   lowerr   r   r   r   r   r%   r9   r   rx   rY   )r(   r   r   r   r   r   rC   r   r   r!   syaxayazcys                  r   euler_from_matrixr     s@   &4/:4::</H,	:
 	A1:A1:>"A


6,RaR!V4AWWQ!tWqAw&a41T7)::;9AdGQ!tW-BB!$(BAdGa1gX.BQ!tWHa1g.BB!$(BBWWQ!tWqAw&a41T7)::;9AdGQ!tW-BQ!tWHb)BAdGQ!tW-BQ!tWHa1g.BQ!tWHb)BBS2#sB2:E H% 4D/3,	:u4s   H) )I
Ic                 ,    [        [        U 5      U5      $ )zReturn Euler angles from quaternion for specified axis sequence.

>>> angles = euler_from_quaternion([0.99810947, 0.06146124, 0, 0])
>>> np.allclose(angles, [0.123, 0, 0])
True

)r   r   )
quaternionr   s     r   euler_from_quaternionr     s     .z:DAAr   c                 j    [         UR                  5          u  pEpgUS-   n[
        X-   S-
     S-   n	[
        X-
     S-   n
U(       a  X p U(       a  U* nU S-  n US-  nUS-  n[        R                  " U 5      n[        R                  " U 5      n[        R                  " U5      n[        R                  " U5      n[        R                  " U5      n[        R                  " U5      nX-  nUU-  nX-  nUU-  n[        R                  " S5      nU(       a-  UUU-
  -  US'   UUU-   -  UU'   UUU-   -  UU	'   UUU-
  -  UU
'   O8UU-  UU-  -   US'   UU-  UU-  -
  UU'   UU-  UU-  -   UU	'   UU-  UU-  -
  UU
'   U(       a  UU	==   S-  ss'   U$ ! [        [        4 a    [        U     Uu  pEpg GNf = f)a  Return quaternion from Euler angles and axis sequence.

ai, aj, ak : Euler's roll, pitch and yaw angles
axes : One of 24 axis sequences as string or encoded tuple

>>> q = quaternion_from_euler(1, 2, 3, 'ryxz')
>>> np.allclose(q, [0.435953, 0.310622, -0.718287, 0.444435])
True

r   r+   r
   r   rv   )
r   r   r   r   r   r   r   rI   rH   r   )r   r   r   r   r   r   r   r   rC   r   r   r   r   r   r   r   r   r   r   r   r   r   s                         r   quaternion_from_eulerr    s   4/:4::</H,	:
 	AA1:>"Q&A1:"ABS#IB#IB#IB	B	B	B	B	B	B	B	bB	B	bB
AR"W~!R"W~!R"W~!R"W~!Bwb !Bwb !Bwb !Bwb !	!HS H% 4D/3,	:u4s   F F21F2c                     [         R                  " SUS   US   US   /5      n[        U5      nU[        :  a  U[         R                  " U S-  5      U-  -  n[         R
                  " U S-  5      US'   U$ )zReturn quaternion for rotation about axis.

>>> q = quaternion_about_axis(0.123, [1, 0, 0])
>>> np.allclose(q, [0.99810947, 0.06146124, 0, 0])
True

rF   r   r   rG   r+   )r   rK   rf   rx   rH   rI   )rM   r   r   qlens       r   quaternion_about_axisr    sn     	#tAwQa12Aq>Dd{	RVVECK 4''66%#+AaDHr   c           	         [         R                  " U [         R                  S9R                  S5      n[	        X5      n[        U5      nU[        :  nX) SS24==   [         R                  " SX$) S4   -  5      -  ss'   USS2SSS24   USS2SS2S4   -  n[         R                  " USS45      nSUSS2SS4   -
  USS2SS4   -
  USS2S	S	4'   USS2S
S4   USS2SS	4   -
  USS2S	S
4'   USS2S
S4   USS2SS	4   -   USS2S	S4'   USS2S
S4   USS2SS	4   -   USS2S
S	4'   SUSS2S
S
4   -
  USS2SS4   -
  USS2S
S
4'   USS2SS4   USS2S
S	4   -
  USS2S
S4'   USS2S
S4   USS2SS	4   -
  USS2SS	4'   USS2SS4   USS2S
S	4   -   USS2SS
4'   SUSS2S
S
4   -
  USS2SS4   -
  USS2SS4'   SUSS2SS4'   [         R                  " S5      S   XT'   UR                  5       $ )a  
Return a homogeneous rotation matrix from quaternion.

>>> M = quaternion_matrix([0.99810947, 0.06146124, 0, 0])
>>> np.allclose(M, rotation_matrix(0.123, [1, 0, 0]))
True
>>> M = quaternion_matrix([1, 0, 0, 0])
>>> np.allclose(M, np.identity(4))
True
>>> M = quaternion_matrix([0, 1, 0, 0])
>>> np.allclose(M, np.diag([1, -1, -1, 1]))
True
>>> M = quaternion_matrix([[1, 0, 0, 0],[0, 1, 0, 0]])
>>> np.allclose(M, np.array([np.identity(4), np.diag([1, -1, -1, 1])]))
True


r3   )r8   r
   Nr+   r
   r5   rG   r$   r   r   )N.)r   rK   r9   r   r	   r   rx   r   r   r   r@   )r   r   r   num_qs
identitiesrets         r   r   r   +  sG   & 	2::.66w?AQAVFTJk1nqd):';!;<<	!T1*!Q*%A ((FAq>
"C 1a7#a1aj0C1aLQ1W:!Q'
*C1aLQ1W:!Q'
*C1aLQ1W:!Q'
*C1aL1a7#a1aj0C1aLQ1W:!Q'
*C1aLQ1W:!Q'
*C1aLQ1W:!Q'
*C1aL1a7#a1aj0C1aLC1aLffQi	*CO;;=r   c           
      $   [         R                  " U [         R                  S9SS2SS24   nU(       Ga   [         R                  " S5      n[         R                  " U5      nXBS   :  a/  XCS'   US   US   -
  US	'   US
   US   -
  US'   US   US   -
  US'   O{Su  pVnUS   US   :  a  Su  pVnUS   X%U4   :  a  Su  pVnX%U4   X&U4   X'U4   -   -
  US   -   nXCU'   X%U4   X&U4   -   X6'   X'U4   X%U4   -   X7'   X'U4   X&U4   -
  US	'   U/ SQ   nUS[         R
                  " XBS   -  5      -  -  nOUS   nUS   n	US
   n
US   nUS   nUS   nUS   nUS   nUS   n[         R                  " X-
  U-
  SSS/X-   X-
  U-
  SS/X-   X-   UU-
  U-
  S/X-
  X-
  X-
  X-   U-   //5      nUS-  n[         R                  R                  U5      u  nnU/ SQ[         R                  " U5      4   nUS   S:  a  [         R                  " X35        U$ )ax  Return quaternion from rotation matrix.

If isprecise is True, the input matrix is assumed to be a precise rotation
matrix and a faster algorithm is used.

>>> q = quaternion_from_matrix(np.identity(4), True)
>>> np.allclose(q, [1, 0, 0, 0])
True
>>> q = quaternion_from_matrix(np.diag([1, -1, -1, 1]))
>>> np.allclose(q, [0, 1, 0, 0]) or np.allclose(q, [0, -1, 0, 0])
True
>>> R = rotation_matrix(0.123, (1, 2, 3))
>>> q = quaternion_from_matrix(R, True)
>>> np.allclose(q, [0.9981095, 0.0164262, 0.0328524, 0.0492786])
True
>>> R = [[-0.545, 0.797, 0.260, 0], [0.733, 0.603, -0.313, 0],
...      [-0.407, 0.021, -0.913, 0], [0, 0, 0, 1]]
>>> q = quaternion_from_matrix(R)
>>> np.allclose(q, [0.19069, 0.43736, 0.87485, -0.083611])
True
>>> R = [[0.395, 0.362, 0.843, 0], [-0.626, 0.796, -0.056, 0],
...      [-0.677, -0.498, 0.529, 0], [0, 0, 0, 1]]
>>> q = quaternion_from_matrix(R)
>>> np.allclose(q, [0.82336615, -0.13610694, 0.46344705, -0.29792603])
True
>>> R = random_rotation_matrix()
>>> q = quaternion_from_matrix(R)
>>> is_same_transform(R, quaternion_matrix(q))
True
>>> is_same_quaternion(quaternion_from_matrix(R, isprecise=False),
...                    quaternion_from_matrix(R, isprecise=True))
True
>>> R = euler_matrix(0.0, 0.0, np.pi/2.0)
>>> is_same_quaternion(quaternion_from_matrix(R, isprecise=False),
...                    quaternion_from_matrix(R, isprecise=True))
True

r3   Nr
   r   rn   r   rT   r   r$   rU   )rG   r   rG   rV   r   r   )r   r   rG   rl   rk   )r   rG   r   rm   )rG   r   r   )r$   r   r   rG         ?rF   rd   )r   r%   r9   r   rX   r   rK   r:   r   r   r   )r(   	ispreciser!   r   r   rC   r   r   m00m01m02m10m11m12m20m21m22KrA   rB   s                       r   quaternion_from_matrixr  Z  s   N 	

6,RaR!V4AHHTNHHQKw;aDT7QtW$AaDT7QtW$AaDT7QtW$AaDGA!w4 !awa4 !aQ$1T7Q!tW,-$7AaDT7Q!tW$ADT7Q!tW$ADT7Q!tW$AaD,A	S2771w;'''gggggggggHHS#sC0CIOS#6CIsSy3<CIsy#)c/B	
 	
Syy~~a 1lBIIaL()tcz
AHr   c                     Uu  p#pEU u  pgp[         R                  " U* U-  X-  -
  X-  -
  Xb-  -   Xr-  X-  -   X-  -
  Xc-  -   U* U-  X-  -   X-  -   Xd-  -   Xt-  X-  -
  X-  -   Xe-  -   /[         R                  S9$ )zReturn multiplication of two quaternions.

>>> q = quaternion_multiply([4, 1, -2, 3], [8, -5, 6, 7])
>>> np.allclose(q, [28, -44, -14, 48])
True

r3   r   rK   r9   )
quaternion1quaternion0w0x0y0z0w1x1y1z1s
             r   quaternion_multiplyr#    s     !NBB NBB88C"Hrw(272Gbg'"'1C"Hrw(272Gbg'"'1		
 jj r   c                     [         R                  " U [         R                  S9n[         R                  " USS USS 5        U$ )zReturn conjugate of quaternion.

>>> q0 = random_quaternion()
>>> q1 = quaternion_conjugate(q0)
>>> q1[0] == q0[0] and all(q1[1:] == -q0[1:])
True

r3   r   N)r   rK   r9   r   r   r   s     r   quaternion_conjugater&    s7     	2::.AKK!"quHr   c                     [         R                  " U [         R                  S9n[         R                  " USS USS 5        U[         R                  " X5      -  $ )zReturn inverse of quaternion.

>>> q0 = random_quaternion()
>>> q1 = quaternion_inverse(q0)
>>> np.allclose(quaternion_multiply(q0, q1), [1, 0, 0, 0])
True

r3   r   N)r   rK   r9   r   r.   r%  s     r   quaternion_inverser(    sE     	2::.AKK!"qurvva|r   c                     [        U S   5      $ )zHReturn real part of quaternion.

>>> quaternion_real([3, 0, 1, 2])
3.0

r   )floatr   s    r   quaternion_realr,    s     Ar   c                 N    [         R                  " U SS [         R                  S9$ )z]Return imaginary part of quaternion.

>>> quaternion_imag([3, 0, 1, 2])
array([0., 1., 2.])

r   r
   r3   r  r+  s    r   quaternion_imagr.    s     88JqO2::66r   c                 P   [        U SS 5      n[        USS 5      nUS:X  a  U$ US:X  a  U$ [        R                  " XV5      n[        [        U5      S-
  5      [        :  a  U$ U(       a  US:  a  U* n[        R
                  " Xf5        [        R                  " U5      U[        R                  -  -   n[        U5      [        :  a  U$ S[        R                  " U5      -  n	U[        R                  " SU-
  U-  5      U	-  -  nU[        R                  " X(-  5      U	-  -  nXV-  nU$ )a  Return spherical linear interpolation between two quaternions.

>>> q0 = random_quaternion()
>>> q1 = random_quaternion()
>>> q = quaternion_slerp(q0, q1, 0)
>>> np.allclose(q, q0)
True
>>> q = quaternion_slerp(q0, q1, 1, 1)
>>> np.allclose(q, q1)
True
>>> q = quaternion_slerp(q0, q1, 0.5)
>>> angle = np.arccos(np.dot(q0, q))
>>> np.allclose(2, np.arccos(np.dot(q0, q1)) / angle) or         np.allclose(2, np.arccos(-np.dot(q0, q1)) / angle)
True

Nr
   rF   r5   )	r,   r   r.   r=   rx   r   arccospirH   )
quat0quat1fractionspinshortestpathq0q1drM   isins
             r   quaternion_slerpr;    s   $ 
U2AY	B	U2AY	B3		S	
rA
3q6C<4	CB
BIIaL4"%%<'E
5zD	D"&&#.E)
*T
11B"&&!
"T
))BHBIr   c                 ^   U c2  [         R                  R                  SU-  5      R                  S5      n OU R                  S   S:X  d   e[         R
                  " SU S   -
  5      n[         R
                  " U S   5      n[         R                  S-  nX@S   -  nX@S   -  n[         R                  " [         R                  " U5      U-  [         R                  " U5      U-  [         R                  " U5      U-  [         R                  " U5      U-  /5      R                  R                  5       $ )a  Return uniform random unit quaternion.

rand: array like or None
    Three independent random variables that are uniformly distributed
    between 0 and 1.

>>> q = random_quaternion()
>>> np.allclose(1, vector_norm(q))
True
>>> q = random_quaternion(num=10)
>>> np.allclose(1, vector_norm(q, axis=1))
True
>>> q = random_quaternion(np.random.random(3))
>>> len(q.shape), q.shape[0]==4
(1, True)

r$   )r$   r8   r   r5   r+   r   rG   )r   randomrandr   r   r   r1  rK   rI   rH   rW   r@   )r>  numr1r2pi2r   t2s          r   random_quaternionrD  !  s    $ |yy~~a#g&..w7zz!}!!!	tAw	B	a	B
%%#+C	AwB	AwB88	b"&&*r/266":?BFF2JOLa	r   r>  r?  r   c                     [        [        XS95      nU(       a5  [        R                  R                  S5      S-
  [	        U5      -  USS2S4'   U$ )a7  
Return uniform random rotation matrix.

Parameters
------------
rand : (3,)
  Three independent random variables that are uniformly distributed
  between 0 and 1 for each returned quaternion.
num
  Number of matrices to return.
translate
  If passed the rotation matrix will include translation
  that is random and between positive and negative half this value.

>>> R = random_rotation_matrix()
>>> np.allclose(np.dot(R.T, R), np.identity(4))
True
>>> R = random_rotation_matrix(num=10)
>>> np.allclose(np.einsum('...ji,...jk->...ik', R, R), np.identity(4))
True

)r>  r?  r$   r
  N)r   rD  r   r=  r*  )r>  r?  r   r(   s       r   random_rotation_matrixrF  A  sK    2 0dDEF))!,s2eI6FFrr1uMr   c                   ~    \ rS rSrSrSS jrS rS r\S 5       r	\	R                  S 5       r	S	 rS
 rSS jrS rSrg)Arcballib  a  Virtual Trackball Control.

>>> ball = Arcball()
>>> ball = Arcball(initial=np.identity(4))
>>> ball.place([320, 320], 320)
>>> ball.down([500, 250])
>>> ball.drag([475, 275])
>>> R = ball.matrix()
>>> np.allclose(np.sum(R), 3.90583455)
True
>>> ball = Arcball(initial=[1, 0, 0, 0])
>>> ball.place([320, 320], 320)
>>> ball.setaxes([1, 1, 0], [-1, 1, 0])
>>> ball.constrain = True
>>> ball.down([400, 200])
>>> ball.drag([200, 400])
>>> R = ball.matrix()
>>> np.allclose(np.sum(R), 0.2055924)
True
>>> ball.next()

Nc                    SU l         SU l        SU l        SS/U l        [        R
                  " / SQ5      U l        SU l        Uc  [        R
                  " / SQ5      U l        Ot[        R
                  " U[        R                  S9nUR                  S:X  a  [        U5      U l        O0UR                  S	:X  a  U[        U5      -  nXl        O[        S
5      eU R                  =U l        U l        g)zPInitialize virtual trackball control.

initial : quaternion or rotation matrix

Nr5   rF   )rF   rF   r5   F)r5   rF   rF   rF   r3   r
   r
   r   z"initial not a quaternion or matrix)_axis_axes_radius_centerr   rK   _vdown
_constrain_qdownr9   r   r  rf   r?   _qnow_qpre)selfinitials     r   __init__Arcball.__init__z  s     

Szhh/?((#78DKhhwbjj9G}}&4W=$&;w//% !EFF"&++-
TZr   c                 l    [        U5      U l        US   U R                  S'   US   U R                  S'   g)zPlace Arcball, e.g. when window size changes.

center : sequence[2]
    Window coordinates of trackball center.
radius : float
    Radius of trackball in window coordinates.

r   r   N)r*  rM  rN  )rT  centerradiuss      r   placeArcball.place  s1     V} )Q )Qr   c                 f    Uc  SU l         gU Vs/ s H  n[        U5      PM     snU l         gs  snf )z Set axes to constrain rotations.N)rL  r,   )rT  r   r   s      r   setaxesArcball.setaxes  s,    <DJ8<=+d+=DJ=s   .c                     U R                   $ )z'Return state of constrain to axis mode.)rP  rT  s    r   	constrainArcball.constrain  s     r   c                 $    [        U5      U l        g)z$Set state of constrain to axis mode.N)boolrP  )rT  values     r   rb  rc    s     u+r   c                 ^   [        XR                  U R                  5      U l        U R                  =U l        U l        U R                  (       aX  U R                  bK  [        U R                  U R                  5      U l
        [        U R                  U R                  5      U l        gSU l
        g)z>Set initial cursor window coordinates and pick constrain-axis.N)arcball_map_to_sphererN  rM  rO  rR  rQ  rS  rP  rL  arcball_nearest_axisrK  arcball_constrain_to_axis)rT  r/   s     r   downArcball.down  sm    +E<<N#'::-dj??tzz5-dkk4::FDJ3DKKLDKDJr   c                    [        XR                  U R                  5      nU R                  b  [	        X R                  5      nU R
                  U l        [        R                  " U R                  U5      n[        R                  " X35      [        :  a  U R                  U l        g[        R                  " U R                  U5      US   US   US   /n[        X@R                  5      U l        g)z)Update current cursor window coordinates.Nr   r   rG   )rh  rN  rM  rK  rj  rR  rS  r   r   rO  r.   rx   rQ  r#  )rT  r/   vnowr   r   s        r   dragArcball.drag  s    $ULL$,,G::!,T::>DZZ
HHT[[$'66!<$DJT*AaD!A$!=A,Q<DJr   c                     [        U R                  U R                  SU-   S5      nU R                  UsU l        U l        g)z,Continue rotation in direction of last drag.r+   FN)r;  rS  rR  )rT  accelerationr   s      r   nextArcball.next  s3    TZZS<5GO!%Q
DJr   c                 ,    [        U R                  5      $ )z#Return homogeneous rotation matrix.)r   rR  ra  s    r   r(   Arcball.matrix  s     ,,r   )	rL  rK  rN  rP  rQ  rR  rS  rM  rO  Nr   )__name__
__module____qualname____firstlineno____doc__rV  r[  r^  propertyrb  setterrk  ro  rs  r(   __static_attributes__r   r   r   rH  rH  b  sX    ..2$>   & &=/
-r   rH  c                    U S   US   -
  U-  nUS   U S   -
  U-  nX3-  XD-  -   nUS:  a3  [         R                  " U5      n[         R                  " X5-  XE-  S/5      $ [         R                  " X4[         R                  " SU-
  5      /5      $ )z7Return unit sphere coordinates from window coordinates.r   r   r5   rF   )r   r   rK   )r/   rY  rZ  r   r   r   s         r   rh  rh    s    
(VAY
&	(B
)eAh
&	(B
"'A3wGGAJxx-..xxq!1233r   c                    [         R                  " U [         R                  S9n[         R                  " U[         R                  S9nX#[         R                  " X25      -  -  n[	        U5      nU[
        :  a%  US   S:  a  [         R                  " X"5        X$-  nU$ US   S:X  a  [         R                  " / SQ5      $ [        US   * US   S/5      $ )z*Return sphere point perpendicular to axis.r3   rG   rF   r5   )r5   rF   rF   r   r   )r   rK   r9   r.   rf   rx   r   r,   )r/   r   r   r   r   s        r   rj  rj    s    
bjj)A
RZZ(ARVVA\	AAA4xQ4#:KK	ts{xx((1qtS)**r   c                     [         R                  " U [         R                  S9n SnSnU H.  n[         R                  " [	        X5      U 5      nXS:  d  M*  UnUnM0     U$ )z+Return axis, which arc is nearest to point.r3   Nrv   )r   r%   r9   r.   rj  )r/   r   nearestmxr   r   s         r   ri  ri    sV    JJuBJJ/EG	BFF,U95A6GB	 
 Nr   g      @)r   rG   r   r   )r   r   r   r   sxyx)r   r   r   r   sxzy)r   r   r   r   sxzx)r   r   r   r   syzx)r   r   r   r   syzy)r   r   r   r   syxz)r   r   r   r   syxy)r   r   r   r   szxy)rG   r   r   r   szxz)rG   r   r   r   szyx)rG   r   r   r   szyz)rG   r   r   r   rzyxr   r   r   r   rxyx)r   r   r   r   ryzx)r   r   r   r   rxzx)r   r   r   r   rxzy)r   r   r   r   )r   r   r   r   )r   r   r   r   )r   r   r   r   )rG   r   r   r   )rG   r   r   r   )rG   r   r   r   )rG   r   r   r   )ryzyrzxyryxyryxzrzxzrxyzrzyzc                    [         R                  " U [         R                  S9n Uc~  U R                  S:X  a*  [         R                  " [         R
                  " X 5      5      $ X -  n [         R                  " [         R                  " XS95      n[         R                  " X"5        U$ X -  n [         R                  " XUS9  [         R                  " X"5        g)a6  Return length, i.e. Euclidean norm, of ndarray along axis.

>>> v = np.random.random(3)
>>> n = vector_norm(v)
>>> np.allclose(n, np.linalg.norm(v))
True
>>> v = np.random.rand(6, 5, 3)
>>> n = vector_norm(v, axis=-1)
>>> np.allclose(n, np.sqrt(np.sum(v*v, axis=2)))
True
>>> n = vector_norm(v, axis=1)
>>> np.allclose(n, np.sqrt(np.sum(v*v, axis=1)))
True
>>> v = np.random.rand(5, 4, 3)
>>> n = np.zeros((5, 3))
>>> vector_norm(v, axis=1, out=n)
>>> np.allclose(n, np.sqrt(np.sum(v*v, axis=1)))
True
>>> float(vector_norm([]))
0.0
>>> float(vector_norm([1]))
1.0

r3   Nr   r   )r   out)r   rK   r9   ndimr   r.   
atleast_1dr   )datar   r  s      r   rf   rf   !  s    2 88D

+D
{99>77266$-..mmBFF434


tC(
r   c                    Ucc  [         R                  " U [         R                  S9n U R                  S:X  a/  U [         R                  " [         R
                  " X 5      5      -  n U $ OX La  [         R                  " U 5      USS& Un [         R                  " [         R                  " X -  U5      5      n[         R                  " X35        Ub  [         R                  " X15      nX-  n Uc  U $ g)ax  Return ndarray normalized by length, i.e. Euclidean norm, along axis.

>>> v0 = np.random.random(3)
>>> v1 = unit_vector(v0)
>>> np.allclose(v1, v0 / np.linalg.norm(v0))
True
>>> v0 = np.random.rand(5, 4, 3)
>>> v1 = unit_vector(v0, axis=-1)
>>> v2 = v0 / np.expand_dims(np.sqrt(np.sum(v0*v0, axis=2)), 2)
>>> np.allclose(v1, v2)
True
>>> v1 = unit_vector(v0, axis=1)
>>> v2 = v0 / np.expand_dims(np.sqrt(np.sum(v0*v0, axis=1)), 1)
>>> np.allclose(v1, v2)
True
>>> v1 = np.zeros((5, 4, 3))
>>> unit_vector(v0, axis=1, out=v1)
>>> np.allclose(v1, v2)
True
>>> list(unit_vector([]))
[]
>>> [float(i) for i in unit_vector([1])]
[1.0]

Nr3   r   )
r   rK   r9   r  r   r.   r%   r  r   expand_dims)r  r   r  lengths       r   r,   r,   H  s    4 {xxBJJ/99>BGGBFF4.//DK  ?ZZ%CF]]266$+t45FGGF-ND
{ r   c                 @    [         R                  R                  U 5      $ )zReturn array of random doubles in the half-open interval [0.0, 1.0).

>>> v = random_vector(10000)
>>> bool(np.all(v >= 0) and np.all(v < 1))
True
>>> v0 = random_vector(10)
>>> v1 = random_vector(10)
>>> bool(np.any(v0 == v1))
False

)r   r=  )sizes    r   random_vectorr  t  s     99D!!r   c                 ,    [         R                  " XUS9$ )a  Return vector perpendicular to vectors.

>>> v = vector_product([2, 0, 0], [0, 3, 0])
>>> np.allclose(v, [0, 0, 6])
True
>>> v0 = [[2, 0, 0, 2], [0, 2, 0, 2], [0, 0, 2, 2]]
>>> v1 = [[3], [0], [0]]
>>> v = vector_product(v0, v1)
>>> np.allclose(v, [[0, 0, 0, 0], [0, 0, 6, 6], [0, -6, 0, -6]])
True
>>> v0 = [[2, 0, 0], [2, 0, 0], [0, 2, 0], [2, 0, 0]]
>>> v1 = [[0, 3, 0], [0, 0, 3], [0, 0, 3], [3, 3, 3]]
>>> v = vector_product(v0, v1, axis=1)
>>> np.allclose(v, [[0, 0, 6], [0, -6, 0], [6, 0, 0], [0, -6, 6]])
True

r   )r   r   )r   r   r   s      r   vector_productr    s    $ 88B&&r   c                    [         R                  " U [         R                  S9n [         R                  " U[         R                  S9n[         R                  " X-  US9nU[	        XS9[	        XS9-  -  n[         R
                  " USS5      n[         R                  " U(       a  U5      $ [         R                  " U5      5      $ )a  Return angle between vectors.

If directed is False, the input vectors are interpreted as undirected axes,
i.e. the maximum angle is pi/2.

>>> a = angle_between_vectors([1, -2, 3], [-1, 2, -3])
>>> np.allclose(a, np.pi)
True
>>> a = angle_between_vectors([1, -2, 3], [-1, 2, -3], directed=False)
>>> np.allclose(a, 0)
True
>>> v0 = [[2, 0, 0, 2], [0, 2, 0, 2], [0, 0, 2, 2]]
>>> v1 = [[3], [0], [0]]
>>> a = angle_between_vectors(v0, v1)
>>> np.allclose(a, [0, 1.5708, 1.5708, 0.95532])
True
>>> v0 = [[2, 0, 0], [2, 0, 0], [0, 2, 0], [2, 0, 0]]
>>> v1 = [[0, 3, 0], [0, 0, 3], [0, 0, 3], [3, 3, 3]]
>>> a = angle_between_vectors(v0, v1, axis=1)
>>> np.allclose(a, [1.5708, 1.5708, 1.5708, 0.95532])
True

r3   r   rv   r5   )r   r%   r9   r   rf   clipr0  fabs)r   r   directedr   r.   s        r   angle_between_vectorsr    s    0 
Bbjj	)B	Bbjj	)B
&&t
$C;r%B(BBBC ''#tS
!C99HS77"''#,77r   c                 @    [         R                  R                  U 5      $ )aD  Return inverse of square transformation matrix.

>>> M0 = random_rotation_matrix()
>>> M1 = inverse_matrix(M0.T)
>>> np.allclose(M1, np.linalg.inv(M0.T))
True
>>> for size in range(1, 7):
...     M0 = np.random.rand(size, size)
...     M1 = inverse_matrix(M0)
...     if not np.allclose(M1, np.linalg.inv(M0)): print(size)

)r   r:   r   r'   s    r   inverse_matrixr    s     99==  r   c                  p    [         R                  " S5      nU  H  n[         R                  " X5      nM     U$ )zReturn concatenation of series of transformation matrices.

>>> M = np.random.rand(16).reshape((4, 4)) - 0.5
>>> np.allclose(M, concatenate_matrices(M))
True
>>> np.allclose(np.dot(M, M.T), concatenate_matrices(M, M.T))
True

r
   )r   r   r.   )matricesr!   rC   s      r   concatenate_matricesr    s-     	AAFF1L Hr   c                     [         R                  " U [         R                  S9n X S   -  n [         R                  " U[         R                  S9nXS   -  n[         R                  " X5      $ )zReturn True if two matrices perform same transformation.

>>> is_same_transform(np.identity(4), np.identity(4))
True
>>> is_same_transform(np.identity(4), random_rotation_matrix())
False

r3   rn   )r   rK   r9   allclose)matrix0matrix1s     r   is_same_transformr    sS     hhwbjj1Gt}Ghhwbjj1Gt}G;;w((r   c                     [         R                  " U 5      n [         R                  " U5      n[         R                  " X5      =(       d    [         R                  " X* 5      $ )z)Return True if two quaternions are equal.)r   rK   r  )r7  r8  s     r   is_same_quaternionr    s;    	"B	"B;;r6"++b#"66r   c                 h   [         R                  " U5      n[         R                  " U 5      n [        U5      nU R                  US-   US-   4:w  a  [	        S5      e[         R
                  " US-   5      nU* USU2U4'   [         R                  " X5      nXSU2U4'   [         R                  " X45      nU$ )z
Given a transformation matrix, apply its rotation
around a point in space.

Parameters
----------
matrix: (4,4) or (3, 3) float, transformation matrix
point:  (3,) or (2,)  float, point in space

Returns
---------
result: (4,4) transformation matrix
r   zmatrix must be (d+1, d+1)N)r   
asanyarrayr   r   r?   r   r.   )r(   r/   r    r   results        r   transform_aroundr    s     MM% E]]6"F
e*C||aq))455sQwI!6IdsdCiVVF&F dsdCiVVI&FMr   c                 r   U c  SS/n Uc  Sn[         R                  " U [         R                  S9n [        U5      n[         R                  " U5      (       d  [        S5      eU R                  S:w  a  [        S5      e[         R                  " S[         R                  S9n[         R                  " U5      n[         R                  " U5      nXe/USSS	24'   U* U/US
SS	24'   XSS	2S	4'   Ub	  [        XBS9nUb?  [         R                  " S5      nUSS	2SS	24==   U-  ss'   [         R                  " Xt5      nU$ )a5  
2D homogeonous transformation matrix.

Parameters
----------
offset : (2,) float
  XY offset
theta : float
  Rotation around Z in radians
point :  (2, ) float
  Point to rotate around
scale : (2,) float or None
  Scale to apply

Returns
----------
matrix : (3, 3) flat
  Homogeneous 2D transformation matrix
NrF   r3   ztheta must be finite angle!)rG   zoffset must be length 2!r$   r   rG   r   )r(   r/   )r   r  r9   r*  isfiniter?   r   r   rH   rI   r  r.   )offsetthetar/   rq   rW   r   r   r   s           r   planar_matrixr    s"   ( ~s}]]64F%LE;;u677||t344
q

#A
uA
uAvAa!eHAwAa!eHbqb!eHA3FF1I	"1"bqb&	U	FF1LHr   c                     [         R                  " U [         R                  S9n U R                  S:w  a  [	        S5      e[         R
                  " S5      nU SS2S4   USS2S4'   U SS2SS24   USS2SS24'   U$ )a  
Given a 2D homogeneous rotation matrix convert it to a 3D rotation
matrix that is rotating around the Z axis

Parameters
----------
matrix_2D: (3,3) float, homogeneous 2D rotation matrix

Returns
----------
matrix_3D: (4,4) float, homogeneous 3D rotation matrix
r3   rn   z.Homogeneous 2D transformation matrix required!r
   NrG   r$   )r   r  r9   r   r?   r   )	matrix_2D	matrix_3Ds     r   planar_matrix_to_3Dr  @  s     irzz:I& IJJq	I !Q'Ibqb!e!"1"bqb&)Ibqb"1"fr   c                     [        SXUS9nU$ )a  
Give a spherical coordinate vector, find the rotation that will
transform a [0,0,1] vector to those coordinates

Parameters
-----------
theta: float, rotation angle in radians
phi:   float, rotation angle in radians

Returns
----------
matrix: (4,4) rotation matrix where the following will
         be a cartesian vector in the direction of the
         input spherical coordinates:
            np.dot(matrix, [0,0,1,0])

rF   )r   )r   )r  phir   r  s       r   spherical_matrixr  [  s    $ #s5FMr   pointsr(   returnc                    [         R                  " U [         R                  S9n [        U 5      S:X  d  Uc  U R	                  5       $ [         R                  " U[         R                  S9nU R
                  u  p4[         R                  " U[        SUS-   2SUS-   24   -
  5      R                  5       S:  a$  [         R                  " U R	                  5       5      $ U(       a_  [         R                  " U [         R                  " U5      45      n[         R                  " XR                  5      R                  SS2SU24   $ [         R                  " USU2SU24   U R                  5      R                  $ )a  
Returns points rotated by a homogeneous
transformation matrix.

If points are (n, 2) matrix must be (3, 3)
If points are (n, 3) matrix must be (4, 4)

Parameters
----------
points : (n, dim) float
  Points where `dim` is 2 or 3.
matrix : (3, 3) or (4, 4) float
  Homogeneous rotation matrix.
translate : bool
  Apply translation from matrix or not.

Returns
----------
transformed : (n, dim) float
  Transformed points.
r3   r   Nr   r6   )r   r  r9   r   r&   r   r=   	_IDENTITYmaxascontiguousarraycolumn_stackonesr.   rW   )r  r(   r   countr    stacks         r   transform_pointsr  q  s   0 ]]64F
6{a6>{{} ]]64FJE 
vvfy37IcAgI!5667;;=D##FKKM22 89vvfgg&((DSD11 66&#tt$fhh/111r   c           	      ,   U R                   S   S-
  n[        R                  " [        R                  " U SU2SU24   U SU2SU24   R                  5      [
        SU2SU24   -
  5      R                  5       nUS:  a  X1:  a  [        R                  R                  U SU2SU24   5      u  pEn[        R                  " US-   5      n[        R                  " XF5      USU2SU24'   U SU2U4   USU2U4'   [        R                  " XpUS9(       d   eU$ U $ )a  
If a homogeneous transformation matrix is *almost* a rigid
transform but many matrix-multiplies have accumulated some
floating point error try to restore the matrix using SVD.

Parameters
-----------
matrix : (4, 4) or (3, 3) float
  Homogeneous transformation matrix.
max_deviance : float
  Do not alter the matrix if it is not rigid by more
  than this amount.

Returns
----------
repaired : (4, 4) or (3, 3) float
  Repaired homogeneous transformation matrix
r   r   NgvIh%<=)atol)r   r   r=   r.   rW   r  r  r:   r   r   r  )r(   max_deviancer    checkUr   rB   repaireds           r   	fix_rigidr    s   & ,,q/A
CFF
vdsdDSDj!6$3$*#5#7#789TcT4C4Z;PP	ce 

 u}-))--ttTcTz 23a66#'?!vva|#tt$TcT3Y/#s{{8,???Mr   c                 h   [         R                  " U [         R                  S9n U R                  S:w  a  g[         R                  " U S   / SQ-
  5      U:  a  g[         R
                  " U SS2SS24   U SS2SS24   R                  5      [        SS2SS24   -
  n[         R                  " U5      U:  $ )a  
Check to make sure a homogeonous transformation
matrix is a rigid transform.

Parameters
-----------
matrix : (4, 4) float
  A transformation matrix

Returns
-----------
check : bool
  True if matrix is a a transform with
  only translation, scale, and rotation
r3   rJ  Fr8   r  Nr$   )r   r  r9   r   ptpr.   rW   r  )r(   epsilonr  s      r   is_rigidr    s    " ]]64F||v 
vvfRj<'(72 FF6"1"bqb&>6"1"bqb&>#3#34y!RaR7HHE66%=7""r   c                     [         R                  " S5      n[         R                  " U S:g  5      (       a  USS2SS24==   U -  ss'   Ub  XSS2S4'   U$ )z
Optimized version of `compose_matrix` for just
scaling then translating.

Scalar args are broadcast to arrays of shape (3,)

Parameters
--------------
scale : float or (3,) float
  Scale factor
translate : float or (3,) float
  Translation
r
   r   Nr$   )r   r   r   )rq   r   r!   s      r   scale_and_translater    sX     	q	A	vveqj	"1"bqb&	U	"1"a%Hr   c                 $   [         R                  " U [         R                  S9n Sn[         R                  R                  US-  S45      n[         R                  " U SS2SS24   UR
                  5      R
                  n[         R                  " X#45      R                  S5      n[         R                  " USS9n[         R                  " USS2S4   USS2S4   5      n[         R                  " U SS2SS24   USU R
                  5      R
                  USU& [         R                  " [         R                  " Xf-  / SQ5      5      R                  S	5      nXg-  n[         R                  " USU XaS -  S
/S-  5      nUR                  5       S:  n	U	$ )z
Check to see if a matrix will invert triangles.

Parameters
-------------
matrix : (4, 4) float
  Homogeneous transformation matrix

Returns
--------------
flip : bool
  True if matrix will flip winding of triangles.
r3   r$   N)r8   r$   r$   r   r   r   )r   r   r   )r8   r   r5   rF   )r   r  r9   r=  r.   rW   r   r   diffr   r   r   )
r(   r  trirot	trianglesvectorsr   norm
projectionflips
             r   flips_windingr    sP    ]]64FE
))

EAIq>
*C
&&BQB
'
)
)C 		3*%--j9Iggia(GHHWQT]GAqDM2EFF6"1"bqb&>5%=??;==E&5M77266%-34<<WEDLEfuf5uqyAJ ??s"DKr   rw  )NN)NNF)F)NNNNN)TTT)FT)r   )r   T)Nr   )Nr   N)r   )Tr   )NNNN)T)gh㈵>)r6   )Qr|  numpyr   numpy.typingr   r   typedr   r   r   utilr	   r   r  flagsr   r"   r)   r1   rD   rR   r^   rb   ri   rr   rt   r   r   r   r   r   r   r   r   r   r   r   r  r  r   r  r#  r&  r(  r,  r.  r;  rD  rF  rH  rh  rj  ri  finfor*  epsrx   r   r   itemsr   rf   r,   r  r  r  r  r  r  r  r  r  r  r  re  r9   r  r  r  r  r  )r   r   s   00r   <module>r     s  HaF  + , ,  FF1I	$	 8	,4:Nb%#PD&%R9xE8P9x>,+^Q8h1h<hV/VdEP7tB6r ,^Vr* 7&RB W[
9
+2CKFCSBm- m-`
4+ 
 
xxS  

L
L L L	
 L L L L L L L L L L L  L!" L#$ 16 !, 1 1 34 3qt 34$N)X"'*8D!  ) 7:/d6. =A+2+2(+259+2RZZ+2\$N#@,'} 5s   G