
    \
j                         d Z ddlZddlZddlmZ d Z G d de	          Z
 G d de	          Z G d	 d
e	          Z G d de	          Z G d de	          ZdS )a`  Matrix and Vector math.

This module provides Vector and Matrix objects, include Vec2, Vec3, Vec4,
Mat3 and Mat4. Most common operations are supported, and many helper
methods are included for rotating, scaling, and transforming.
The :py:class:`~pyglet.matrix.Mat4` includes class methods
for creating orthographic and perspective projection matrixes.
    N)mulc                 >    t          t          | |          |          S N)maxmin)nummin_valmax_vals      E/DATA/AppData/hermes/venv/lib/python3.11/site-packages/pyglet/math.pyclampr   2   s    s3  '***    c                   
    e Zd ZdZ fdZed             Zed             Zed             Z	ed             Z
ed             Zd Zd	 Zd
 Zd Zd Zd ZddZd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Z xZ S )Vec2a  A two dimensional vector represented as an X Y coordinate pair.

    :parameters:
        `x` : int or float :
            The X coordinate of the vector.
        `y`   : int or float :
            The Y coordinate of the vector.

    Vectors must be created with either 0 or 2 values. If no arguments are provided a vector with the coordinates 0, 0 is created.

    Vectors are stored as a tuple and therefore immutable and cannot be modified directly
    c                     t          |          dv s
J d            t                                          t          |pd          S )N)r      z*0 or 2 values are required for Vec2 types.)r   r   )lensuper__new__r   clsargs	__class__s     r   r   zVec2.__new__D   s?    4yyF"""$P"""wwtT^V444r   c                 v    t          | t          j        |          z  | t          j        |          z            S )aW  Create a new vector from the given polar coodinates.

        :parameters:
            `mag`   : int or float :
                The magnitude of the vector.
            `angle` : int or float :
                The angle of the vector in radians.

        :returns: A new vector with the given angle and magnitude.
        :rtype: Vec2
        )r   _mathcossin)magangles     r   
from_polarzVec2.from_polarH   s2     C%)E***C%)E2B2B,BCCCr   c                     | d         S z>The X coordinate of the vector.

        :type: float
        r    selfs    r   xzVec2.xW        Awr   c                     | d         S z>The Y coordinate of the vector.

        :type: float
           r"   r#   s    r   yzVec2.y_   r&   r   c                 D    t          j        | d         | d                   S )zBThe angle of the vector in radians.

        :type: float
        r)   r   )r   atan2r#   s    r   headingzVec2.headingg   s     {47DG,,,r   c                 *    |                                  S zThe magnitude, or length of the vector. The distance between the coordinates and the origin.

        Alias of abs(self).

        :type: float
        __abs__r#   s    r   r   zVec2.mago        ||~~r   c                 ^    t          | d         |d         z   | d         |d         z             S Nr   r)   r   r$   others     r   __add__zVec2.__add__y   ,    DGeAh&Q%((:;;;r   c                 ^    t          | d         |d         z
  | d         |d         z
            S r4   r5   r6   s     r   __sub__zVec2.__sub__|   r9   r   c                 ^    t          | d         |d         z  | d         |d         z            S r4   r5   r6   s     r   __mul__zVec2.__mul__   r9   r   c                 ^    t          | d         |d         z  | d         |d         z            S r4   r5   r6   s     r   __truediv__zVec2.__truediv__   r9   r   c                 T    t          j        | d         dz  | d         dz  z             S Nr   r   r)   r   sqrtr#   s    r   r1   zVec2.__abs__   s'    z$q'Q,aA5666r   c                 >    t          | d          | d                    S r4   r5   r#   s    r   __neg__zVec2.__neg__   s    T!WHtAwh'''r   Nc                 .    t          fd| D              S )Nc              3   8   K   | ]}t          |          V  d S r   round.0vndigitss     r   	<genexpr>z!Vec2.__round__.<locals>.<genexpr>   -      66AeAw''666666r   r5   r$   rM   s    `r   	__round__zVec2.__round__   #    666666677r   c                 <    |dk    r| S |                      |          S z;Reverse add. Required for functionality with sum()
        r   r8   r6   s     r   __radd__zVec2.__radd__   $     A::K<<&&&r   c                 P    |                                                      |          S )a4  Create a new Vector of the given magnitude by normalizing, then scaling the vector. The heading remains unchanged.

        :parameters:
            `magnitude` : int or float :
                The magnitude of the new vector.

        :returns: A new vector with the magnitude.
        :rtype: Vec2
        	normalizescaler$   	magnitudes     r   from_magnitudezVec2.from_magnitude   "     ~~%%i000r   c                     |                                  }t          |t          j        |          z  |t          j        |          z            S )a3  Create a new vector of the same magnitude with the given heading. I.e. Rotate the vector to the heading.

        :parameters:
            `heading` : int or float :
                The angle of the new vector in radians.

        :returns: A new vector with the given heading.
        :rtype: Vec2
        )r1   r   r   r   r   )r$   r-   r   s      r   from_headingzVec2.from_heading   s?     llnnC%)G,,,cEIg4F4F.FGGGr   c                 l    | d         dz  | d         dz  z   ||z  k    r|                      |          S | S )a#  Limit the magnitude of the vector to the value used for the max parameter.

        :parameters:
            `max`  : int or float :
                The maximum magnitude for the vector.

        :returns: Either self or a new vector with the maximum magnitude.
        :rtype: Vec2
        r   r   r)   r^   r$   r   s     r   limitz
Vec2.limit   sA     7a<$q'Q,&s22&&s+++r   c                     t          | d         ||d         | d         z
  z  z   | d         ||d         | d         z
  z  z             S )a  Create a new vector lineraly interpolated between this vector and another vector.

        :parameters:
            `other`  : Vec2 :
                The vector to be linerly interpolated to.
            `alpha` : float or int :
                The amount of interpolation.
                Some value between 0.0 (this vector) and 1.0 (other vector).
                0.5 is halfway inbetween.

        :returns: A new interpolated vector.
        :rtype: Vec2
        r   r)   r5   r$   r7   alphas      r   lerpz	Vec2.lerp   sQ     DGua47(:;<Gua47(:;<> > 	>r   c                 F    t          | d         |z  | d         |z            S )zMultiply the vector by a scalar value.

        :parameters:
            `value`  : int or float :
                The ammount to be scaled by

        :returns: A new vector scaled by the value.
        :rtype: Vec2
        r   r)   r5   r$   values     r   r[   z
Vec2.scale   s$     DGeOT!Wu_555r   c                     | j         }| j        }t          |t          j        ||z             z  |t          j        ||z             z            S )a	  Create a new Vector rotated by the angle. The magnitude remains unchanged.

        :parameters:
            `angle` : int or float :
                The angle to rotate by

        :returns: A new rotated vector of the same magnitude.
        :rtype: Vec2
        )r   r-   r   r   r   r   )r$   r   r   r-   s       r   rotatezVec2.rotate   sH     h,C%)GeO444cEIgem<T<T6TUUUr   c                 x    t          j        |d         | d         z
  dz  |d         | d         z
  dz  z             S )zCalculate the distance between this vector and another 2D vector.

        :parameters:
            `other`  : Vec2 :
                The other vector

        :returns: The distance between the two vectors.
        :rtype: float
        r   r   r)   rB   r6   s     r   distancezVec2.distance   s>     zE!HtAw.14%(T!W:LQR9RSTTTr   c                 v    |                                  }|r"t          | d         |z  | d         |z            S | S )zNormalize the vector to have a magnitude of 1. i.e. make it a unit vector.

        :returns: A unit vector with the same heading.
        :rtype: Vec2
        r   r)   )r1   r   r$   ds     r   rZ   zVec2.normalize   s>     LLNN 	2Q!T!Wq[111r   c                 v    t          t          | d         ||          t          | d         ||                    S )a`  Restrict the value of the X and Y components of the vector to be within the given values.

        :parameters:
            `min_val` : int or float :
                The minimum value
            `max_val` : int or float :
                The maximum value

        :returns: A new vector with clamped X and Y components.
        :rtype: Vec2
        r   r)   )r   r   r$   r	   r
   s      r   r   z
Vec2.clamp   s4     E$q'7G44eDGWg6V6VWWWr   c                 H    | d         |d         z  | d         |d         z  z   S )zCalculate the dot product of this vector and another 2D vector.

        :parameters:
            `other`  : Vec2 :
                The other vector.

        :returns: The dot product of the two vectors.
        :rtype: float
        r   r)   r"   r6   s     r   dotzVec2.dot  s)     Awq!DGeAh$666r   c                      	 t           t          t          d                    t	          |                    } | fd|D              S # t
          $ r! t          d j        j         d| d          w xY w)Nr         c              3   N   K   | ]}d                      |                   V   dS )xyNindexrK   cr$   s     r   rN   z#Vec2.__getattr__.<locals>.<genexpr>  s2      BBqtDJJqMM2BBBBBBr   '' object has no attribute '	r   Vec3Vec4getr   	ExceptionAttributeErrorr   __name__r$   attrs	vec_classs   `  r   __getattr__zVec2.__getattr__  s    	c Td3377E

CCI9BBBBEBBBCC 	c 	c 	c !aT^%<!a!aY^!a!a!abbb	c   AA	 	+A4c                 ,    d| d          d| d          dS )NzVec2(r   , r)   )r"   r#   s    r   __repr__zVec2.__repr__!  s#    ,tAw,,$q',,,,r   r   )!r   
__module____qualname____doc__r   staticmethodr   propertyr%   r*   r-   r   r8   r;   r=   r?   r1   rE   rQ   rV   r^   ra   re   ri   r[   rn   rp   rZ   r   rw   r   r   __classcell__r   s   @r   r   r   6   s        5 5 5 5 5 D D \D   X   X - - X-   X< < << < << < << < <7 7 7( ( (8 8 8 8' ' '
1 
1 
1H H H  > > >"
6 
6 
6V V V
U 
U 
U	 	 	X X X
7 
7 
7c c c- - - - - - -r   r   c                        e Zd ZdZ fdZed             Zed             Zed             Zed             Z	d Z
d Zd	 Zd
 Zd Zd ZddZd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Z xZS )r   a  A three dimensional vector represented as a X Y Z coordinates.

    :parameters:
        `x` : int or float :
            The X coordinate of the vector.
        `y`   : int or float :
            The Y coordinate of the vector.
        `z`   : int or float :
            The Z coordinate of the vector.

    3D Vectors must be created with either 0 or 3 values. If no arguments are provided a vector with the coordinates 0, 0, 0 is created.

    Vectors are stored as a tuple and therefore immutable and cannot be modified directly
    c                     t          |          dv s
J d            t                                          t          |pd          S )N)r   rz   z*0 or 3 values are required for Vec3 types.)r   r   r   )r   r   r   r   r   s     r   r   zVec3.__new__5  s@    4yyF"""$P"""wwtT%6Y777r   c                     | d         S r!   r"   r#   s    r   r%   zVec3.x9  r&   r   c                     | d         S r(   r"   r#   s    r   r*   zVec3.yA  r&   r   c                     | d         S )z>The Z coordinate of the vector.

        :type: float
        r   r"   r#   s    r   zzVec3.zI  r&   r   c                 *    |                                  S r/   r0   r#   s    r   r   zVec3.magQ  r2   r   c                 ~    t          | d         |d         z   | d         |d         z   | d         |d         z             S Nr   r)   r   r   r6   s     r   r8   zVec3.__add__[  ;    DGeAh&Q%((:DGeAh<NOOOr   c                 ~    t          | d         |d         z
  | d         |d         z
  | d         |d         z
            S r   r   r6   s     r   r;   zVec3.__sub__^  r   r   c                 ~    t          | d         |d         z  | d         |d         z  | d         |d         z            S r   r   r6   s     r   r=   zVec3.__mul__a  r   r   c                 ~    t          | d         |d         z  | d         |d         z  | d         |d         z            S r   r   r6   s     r   r?   zVec3.__truediv__d  r   r   c                 l    t          j        | d         dz  | d         dz  z   | d         dz  z             S rA   rB   r#   s    r   r1   zVec3.__abs__g  s4    z$q'Q,aA5Q1DEEEr   c                 N    t          | d          | d          | d                    S r   r   r#   s    r   rE   zVec3.__neg__j  s&    T!WHtAwha111r   Nc                 .    t          fd| D              S )Nc              3   8   K   | ]}t          |          V  d S r   rH   rJ   s     r   rN   z!Vec3.__round__.<locals>.<genexpr>n  rO   r   r   rP   s    `r   rQ   zVec3.__round__m  rR   r   c                 <    |dk    r| S |                      |          S rT   rU   r6   s     r   rV   zVec3.__radd__p  rW   r   c                 P    |                                                      |          S )a5  Create a new Vector of the given magnitude by normalizing, then scaling the vector. The rotation remains unchanged.

        :parameters:
            `magnitude` : int or float :
                The magnitude of the new vector.

        :returns: A new vector with the magnitude.
        :rtype: Vec3
        rY   r\   s     r   r^   zVec3.from_magnitudex  r_   r   c                     | d         dz  | d         dz  z   | d         dz  z   ||z  |z  k    r|                      |          S | S )a#  Limit the magnitude of the vector to the value used for the max parameter.

        :parameters:
            `max`  : int or float :
                The maximum magnitude for the vector.

        :returns: Either self or a new vector with the maximum magnitude.
        :rtype: Vec3
        r   r   r)   rc   rd   s     r   re   z
Vec3.limit  sR     7a<$q'Q,&a14sSy3FF&&s+++r   c                     t          | d         |d         z  | d         |d         z  z
  | d         |d         z  | d         |d         z  z
  | d         |d         z  | d         |d         z  z
            S )zCalculate the cross product of this vector and another 3D vector.

        :parameters:
            `other`  : Vec3 :
                The other vector.

        :returns: The cross product of the two vectors.
        :rtype: float
        r)   r   r   r   r6   s     r   crossz
Vec3.cross  s~     T!WuQx'DGeAh,>?!WuQx'DGeAh,>?!WuQx'DGeAh,>?A A 	Ar   c                 l    | d         |d         z  | d         |d         z  z   | d         |d         z  z   S )zCalculate the dot product of this vector and another 3D vector.

        :parameters:
            `other`  : Vec3 :
                The other vector.

        :returns: The dot product of the two vectors.
        :rtype: float
        r   r)   r   r"   r6   s     r   rw   zVec3.dot  s;     Awq!DGeAh$66a589KKKr   c           	          t          | d         ||d         | d         z
  z  z   | d         ||d         | d         z
  z  z   | d         ||d         | d         z
  z  z             S )a  Create a new vector lineraly interpolated between this vector and another vector.

        :parameters:
            `other`  : Vec3 :
                The vector to be linerly interpolated to.
            `alpha` : float or int :
                The amount of interpolation.
                Some value between 0.0 (this vector) and 1.0 (other vector).
                0.5 is halfway inbetween.

        :returns: A new interpolated vector.
        :rtype: Vec3
        r   r)   r   r   rg   s      r   ri   z	Vec3.lerp  so     DGua47(:;<Gua47(:;<Gua47(:;<> > 	>r   c                 Z    t          | d         |z  | d         |z  | d         |z            S )zMultiply the vector by a scalar value.

        :parameters:
            `value`  : int or float :
                The ammount to be scaled by

        :returns: A new vector scaled by the value.
        :rtype: Vec3
        r   r)   r   r   rk   s     r   r[   z
Vec3.scale  s.     DGeOT!Wu_d1goFFFr   c                     t          j        |d         | d         z
  dz  |d         | d         z
  dz  z   |d         | d         z
  dz  z             S )zCalculate the distance between this vector and another 3D vector.

        :parameters:
            `other`  : Vec3 :
                The other vector

        :returns: The distance between the two vectors.
        :rtype: float
        r   r   r)   rB   r6   s     r   rp   zVec3.distance  s`     zE!HtAw.14!!HtAw.146!!HtAw.146 7 7 	7r   c                     |                                  }|r,t          | d         |z  | d         |z  | d         |z            S | S )zNormalize the vector to have a magnitude of 1. i.e. make it a unit vector.

        :returns: A unit vector with the same rotation.
        :rtype: Vec3
        r   r)   r   )r1   r   rr   s     r   rZ   zVec3.normalize  sH     LLNN 	?Q!T!Wq[$q'A+>>>r   c           	          t          t          | d         ||          t          | d         ||          t          | d         ||                    S )ag  Restrict the value of the X,  Y and Z components of the vector to be within the given values.

        :parameters:
            `min_val` : int or float :
                The minimum value
            `max_val` : int or float :
                The maximum value

        :returns: A new vector with clamped X, Y and Z components.
        :rtype: Vec3
        r   r)   r   )r   r   ru   s      r   r   z
Vec3.clamp  sN     E$q'7G44$q'7G44$q'7G446 6 	6r   c                      	 t           t          t          d                    t	          |                    } | fd|D              S # t
          $ r! t          d j        j         d| d          w xY w)Nry   c              3   N   K   | ]}d                      |                   V   dS )xyzNr~   r   s     r   rN   z#Vec3.__getattr__.<locals>.<genexpr>  s2      CCtEKKNN3CCCCCCr   r   r   r   r   s   `  r   r   zVec3.__getattr__  s    	c Td3377E

CCI9CCCCUCCCDD 	c 	c 	c !aT^%<!a!aY^!a!a!abbb	cr   c                 >    d| d          d| d          d| d          dS )NzVec3(r   r   r)   r   r   r"   r#   s    r   r   zVec3.__repr__  s/    7tAw77$q'77T!W7777r   r   )r   r   r   r   r   r   r%   r*   r   r   r8   r;   r=   r?   r1   rE   rQ   rV   r^   re   r   rw   ri   r[   rp   rZ   r   r   r   r   r   s   @r   r   r   %  s        8 8 8 8 8   X   X   X   XP P PP P PP P PP P PF F F2 2 28 8 8 8' ' '
1 
1 
1  A A A
L 
L 
L> > >$
G 
G 
G7 7 7	 	 	6 6 6 c c c8 8 8 8 8 8 8r   r   c                        e Zd Z fdZed             Zed             Zed             Zed             Zd Z	d Z
d Zd	 Zd
 Zd ZddZd Zd Zd Zd Zd Zd Zd Zd Zd Z xZS )r   c                     t          |          dv s
J d            t                                          t          |pd          S )N)r   r{   z*0 or 4 values are required for Vec4 types.)r   r   r   r   )r   r   r   r   r   s     r   r   zVec4.__new__  s@    4yyF"""$P"""wwtT%9\:::r   c                     | d         S Nr   r"   r#   s    r   r%   zVec4.x      Awr   c                     | d         S )Nr)   r"   r#   s    r   r*   zVec4.y	  r   r   c                     | d         S )Nr   r"   r#   s    r   r   zVec4.z  r   r   c                     | d         S )Nrz   r"   r#   s    r   wzVec4.w  r   r   c                     t          | d         |d         z   | d         |d         z   | d         |d         z   | d         |d         z             S Nr   r)   r   rz   r   r6   s     r   r8   zVec4.__add__  P    DGeAh&Q%((:DGeAh<NPTUVPWZ_`aZbPbcccr   c                     t          | d         |d         z
  | d         |d         z
  | d         |d         z
  | d         |d         z
            S r   r   r6   s     r   r;   zVec4.__sub__  r   r   c                     t          | d         |d         z  | d         |d         z  | d         |d         z  | d         |d         z            S r   r   r6   s     r   r=   zVec4.__mul__  r   r   c                     t          | d         |d         z  | d         |d         z  | d         |d         z  | d         |d         z            S r   r   r6   s     r   r?   zVec4.__truediv__  r   r   c                     t          j        | d         dz  | d         dz  z   | d         dz  z   | d         dz  z             S Nr   r   r)   rz   rB   r#   s    r   r1   zVec4.__abs__!  sB    z$q'Q,aA5Q1DtAwRS|STTTr   c                 ^    t          | d          | d          | d          | d                    S r   r   r#   s    r   rE   zVec4.__neg__$  s.    T!WHtAwha47(;;;r   Nc                 .    t          fd| D              S )Nc              3   8   K   | ]}t          |          V  d S r   rH   rJ   s     r   rN   z!Vec4.__round__.<locals>.<genexpr>(  rO   r   r   rP   s    `r   rQ   zVec4.__round__'  rR   r   c                 <    |dk    r| S |                      |          S r   rU   r6   s     r   rV   zVec4.__radd__*  s"    A::K<<&&&r   c           
          t          | d         ||d         | d         z
  z  z   | d         ||d         | d         z
  z  z   | d         ||d         | d         z
  z  z   | d         ||d         | d         z
  z  z             S r   r   rg   s      r   ri   z	Vec4.lerp0  s    DGua47(:;<Gua47(:;<Gua47(:;<Gua47(:;<> > 	>r   c                 n    t          | d         |z  | d         |z  | d         |z  | d         |z            S r   r   rk   s     r   r[   z
Vec4.scale6  s7    DGeOT!Wu_d1gotAwQVWWWr   c                     t          j        |d         | d         z
  dz  |d         | d         z
  dz  z   |d         | d         z
  dz  z   |d         | d         z
  dz  z             S r   rB   r6   s     r   rp   zVec4.distance9  sz    zE!HtAw.14!!HtAw.146!!HtAw.146 "!HtAw.146 7 7 	7r   c                     |                                  }|r6t          | d         |z  | d         |z  | d         |z  | d         |z            S | S r   )r1   r   rr   s     r   rZ   zVec4.normalize?  sQ    LLNN 	LQ!T!Wq[$q'A+tAw{KKKr   c           
          t          t          | d         ||          t          | d         ||          t          | d         ||          t          | d         ||                    S r   )r   r   ru   s      r   r   z
Vec4.clampE  s_    E$q'7G44$q'7G44$q'7G44$q'7G446 6 	6r   c                     | d         |d         z  | d         |d         z  z   | d         |d         z  z   | d         |d         z  z   S r   r"   r6   s     r   rw   zVec4.dotK  sO    Awq!DGeAh$66a589KKdSTgX]^_X`N```r   c                      	 t           t          t          d                    t	          |                    } | fd|D              S # t
          $ r! t          d j        j         d| d          w xY w)Nry   c              3   N   K   | ]}d                      |                   V   dS )xyzwNr~   r   s     r   rN   z#Vec4.__getattr__.<locals>.<genexpr>R  s2      DDtFLLOO4DDDDDDr   r   r   r   r   s   `  r   r   zVec4.__getattr__N  s    	c Td3377E

CCI9DDDDeDDDEE 	c 	c 	c !aT^%<!a!aY^!a!a!abbb	cr   c           	      P    d| d          d| d          d| d          d| d          d	S )NzVec4(r   r   r)   r   rz   r   r"   r#   s    r   r   zVec4.__repr__V  s;    BtAwBB$q'BBT!WBBQBBBBr   r   )r   r   r   r   r   r%   r*   r   r   r8   r;   r=   r?   r1   rE   rQ   rV   ri   r[   rp   rZ   r   rw   r   r   r   r   s   @r   r   r     s       ; ; ; ; ;   X   X   X   Xd d dd d dd d dd d dU U U< < <8 8 8 8' ' '> > >X X X7 7 7  6 6 6a a ac c cC C C C C C Cr   r   c                        e Zd ZdZdd fdZdedefdZded	efd
ZdefdZdedefdZ	ddZ
ddZd ZddZdddZd ZddZdefdZ xZS )Mat3zA 3x3 Matrix class

    `Mat3` is an immutable 3x3 Matrix, including most common
    operators. Matrix multiplication must be performed using
    the "@" operator.
    Nreturnc                     |t          |          dk    s
J d            t                                          t          |pd          S )a  Create a 3x3 Matrix

        A Mat3 can be created with a list or tuple of 9 values.
        If no values are provided, an "identity matrix" will be created
        (1.0 on the main diagonal). Matrix objects are immutable, so
        all operations return a new Mat3 object.

        :Parameters:
            `values` : tuple of float or int
                A tuple or list containing 9 floats or ints.
        N	   zA 3x3 Matrix requires 9 values)	      ?        r   r   r   r   r   r   r   )r   r   r   r   r   valuesr   s     r   r   zMat3.__new__b  sT     ~V!1!1!13S!1!11wwtV &? 0? @ @ 	@r   sxsyc           
      *    | d|z  dddd|z  ddddf	z  S Nr   r   r"   r$   r   r   s      r   r[   z
Mat3.scales  s(    sRxc3b#sCMMMr   txtyc           
           | dddddd| |df	z  S r   r"   )r$   r   r   s      r   	translatezMat3.translatev  s"    sCc3bS"cBBBr   phic           
          t          j        t          j        |                    }t          j        t          j        |                    }| ||d| |ddddf	z  S Nr   r   )r   r   radiansr   )r$   r   sr   s       r   rn   zMat3.rotatey  sT    IemC(())IemC(())q!S1"ac3<<<r   c           
          | d|d|dddddf	z  S r   r"   r   s      r   shearz
Mat3.shear~  s     sBRc3SAAAr   c           	          t          |          dk    s
J d            t          t          d t          | |          D                                 S )Nr   z Can only add to other Mat3 typesc              3   &   K   | ]\  }}||z   V  d S r   r"   rK   r   os      r   rN   zMat3.__add__.<locals>.<genexpr>  *      ==DAq!a%======r   r   r   tuplezipr6   s     r   r8   zMat3.__add__  sK    5zzQ BE==Ce,<,<=====>>>r   c           	          t          |          dk    s
J d            t          t          d t          | |          D                                 S )Nr   z'Can only subtract from other Mat3 typesc              3   &   K   | ]\  }}||z
  V  d S r   r"   r   s      r   rN   zMat3.__sub__.<locals>.<genexpr>  r   r   r   r6   s     r   r;   zMat3.__sub__  sK    5zzQ IE==Ce,<,<=====>>>r   c                     | S r   r"   r#   s    r   __pos__zMat3.__pos__      r   c                 N    t          t          d | D                                 S )Nc              3      K   | ]}| V  d S r   r"   rK   rL   s     r   rN   zMat3.__neg__.<locals>.<genexpr>  $      ++1"++++++r   r   r   r#   s    r   rE   zMat3.__neg__  '    E++d+++++,,,r   c                 T    t          t          fd| D                                 S )Nc              3   8   K   | ]}t          |          V  d S r   rH   rJ   s     r   rN   z!Mat3.__round__.<locals>.<genexpr>  -      ::%7++::::::r   r  rP   s    `r   rQ   zMat3.__round__  .    E::::T:::::;;;r   c                      t          d          Nz4Please use the @ operator for Matrix multiplication.NotImplementedErrorr6   s     r   r=   zMat3.__mul__      !"XYYYr   c                    t          |          dv s
J d            t          |          t          u r| dd d         }| dd d         }| dd d         }t          t          t	          t
          ||                    t          t	          t
          ||                    t          t	          t
          ||                              S | dd         }| dd         }| dd         }|dd d         }|dd d         }|dd d         }t          t          t	          t
          ||                    t          t	          t
          ||                    t          t	          t
          ||                    t          t	          t
          ||                    t          t	          t
          ||                    t          t	          t
          ||                    t          t	          t
          ||                    t          t	          t
          ||                    t          t	          t
          ||                    f	          S )	N)rz   r   z)Can only multiply with Mat3 or Vec3 typesr   rz   r)   r      r   )r   typer   summap_mulr   )r$   r7   c0c1c2r0r1r2s           r   
__matmul__zMat3.__matmul__  s   5zzV###%P###;;$addBaddBaddBCb%0011Cb%0011Cb%00113 3 3
 !A#Y!A#Y!A#Y14a4[14a4[14a4[ ST2r**++T2r**++T2r**++T2r**++T2r**++T2r**++T2r**++T2r**++T2r**++
- 
. 
. 
	.r   c                 ^    | j         j         | dd          d| dd          d| dd          S )Nr   rz   
    r  r   r   r   r#   s    r   r   zMat3.__repr__  sC    .)X4!9XXD1IXXTRSTURUYXXXr   r   )r   r   )r   r   r   r   r   floatr[   r   rn   r   r8   r;   r   rE   rQ   r=   r  strr   r   r   s   @r   r   r   Z  s        @ @ @ @ @ @ @"N N5 N N N NCE Cu C C C C=% = = = =
B B5 B B B B? ? ? ?? ? ? ?  - - - -< < < < <Z Z Z .  .  .  .DY# Y Y Y Y Y Y Y Yr   r   c            	           e Zd ZdZd"d# fdZed#d            Zed$d#d            Zededd fd	            Z	ed
e
dedd fd            Zedededd fd            Zededededd fd            ZdefdZdefdZdedd fdZdedd fdZd
e
dedd fdZd#dZd#dZd#dZd Zd#dZd#dZd"d#dZd Zd#d Zdefd!Z xZS )%Mat4a
  A 4x4 Matrix class

    `Mat4` is an immutable 4x4 Matrix, including most common
    operators. Matrix multiplication must be performed using
    the "@" operator.
    Class methods are available for creating orthogonal
    and perspective projections matrixes.
    Nr   c                     |t          |          dk    s
J d            t                                          t          |pd          S )a  Create a 4x4 Matrix

        A Matrix can be created with a list or tuple of 16 values.
        If no values are provided, an "identity matrix" will be created
        (1.0 on the main diagonal). Matrix objects are immutable, so
        all operations return a new Mat4 object.

        :Parameters:
            `values` : tuple of float or int
                A tuple or list containing 16 floats or ints.
        N   zA 4x4 Matrix requires 16 values)r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   )r   r   r   r$  r   s     r   r   zMat4.__new__  sV     ~V!2!2!24U!2!22wwtV &D 0D E E 	Er   c                     ||z
  }||z
  }||z
  }	d|z  }
d|z  }d|	 z  }||z    |z  }||z    |z  }||z    |	z  } | |
dddd|dddd|d|||df          S )z-Create a Mat4 orthographic projection matrix.g       @r   r   r"   )r   leftrightbottomtopz_nearz_farwidthheightdepthr   r   szr   r   tzs                   r   orthogonal_projectionzMat4.orthogonal_projection  s     v5[6\E6\t|_u$V|_v%v~&sBS#S#b#B% & & 	&r   <   c                     ||z
  }||z
  }	||	z  }
|t          j        |t           j        z  dz            z  }| }| }||z
  }||z
  }	||z
  }||z    |z  }d|z  |z  |z  }d|z  |z  }||
z  }d|z  |	z  } | |dddd|dddd|ddd|df          S )z,Create a Mat4 perspective projection matrix.ih  r   r   )r   tanpi)r   r(  r)  r*  r+  r,  r-  fovr.  r/  aspectxy_maxy_minx_minr0  qqnr   hs                      r   perspective_projectionzMat4.perspective_projection  s     v%)C%(NS$8999%fn%%Z& 5(JJJsAq!QaAaBaQ  ! ! 	!r   vectorc                 \     | dddddddddddd|d         |d         |d         df          S )zCreate a translaton matrix from a Vec3.

        :Parameters:
            `vector` : A `Vec3`, or 3 component tuple of float or int
                Vec3 or tuple with x, y and z translaton values
        r   r   r   r)   r   r"   )r   rC  s     r   from_translationzMat4.from_translation  sL     sCc3c3c31Ivay&)S: ; ; 	;r   r   c                 >     |                                  ||          S )zCreate a rotation matrix from an angle and Vec3.

        :Parameters:
            `angle` : A `float`
            `vector` : A `Vec3`, or 3 component tuple of float or int
                Vec3 or tuple with x, y and z translaton values
        )rn   )r   r   rC  s      r   from_rotationzMat4.from_rotation  s     suu||E6***r   	directionupc                 V   |                                 }|                    |                                           }|                    |                                           } | |j        |j        |j        d|j        |j        |j        d|j        |j        |j        dddddf          S r   )rZ   cross_productr%   r*   r   )r   rH  rI  vec_zvec_xvec_ys         r   look_at_directionzMat4.look_at_direction  s    ##%%''++5577''..88::sEGUWegsGUWegsGUWegsc3( ) ) 	)r   positiontargetc                     ||z
  }|                      ||          }|                     |                                          }||z  S r   )rO  rE  negate)r   rP  rQ  rI  rH  direction_mat4position_mat4s          r   look_atzMat4.look_at(  sI    X%	..y"==,,X__->->??--r   r   c                 (    | |dz  |dz  dz            S )zGet a specific row as a tuple.r{   r"   r$   r   s     r   rowzMat4.row/  s    E!GE!GAI%&&r   c                     | |dd         S )z!Get a specific column as a tuple.Nr{   r"   rX  s     r   columnzMat4.column3  s    EH1H~r   c                     t          |           }|dxx         |d         z  cc<   |dxx         |d         z  cc<   |dxx         |d         z  cc<   t          |          S )z&Get a scale Matrix on x, y, or z axis.r      r)   
   r   )listr$  )r$   rC  temps      r   r[   z
Mat4.scale7  sg    DzzQ6!9Q6!9RF1IDzzr   c                 b    t          |           t          ddddddddddddg|dR           z  S )z.Get a translate Matrix along x, y, and z axis.r)   r   r$  )r$   rC  s     r   r   zMat4.translate?  sA    DzzD!Q1aAq!Q1!Qv!Qq!Q!QRRRRr   c                    t          d |D                       s
J d            |\  }}}t          j        |          }t          j        |          }d|z
  }||z  ||z  ||z  }}
}	||	|z  z   }d|	|z  z   ||z  z   }d|	|z  z   ||z  z
  }d|
|z  z   ||z  z
  }||
|z  z   }d|
|z  z   ||z  z   }d||z  z   ||z  z   }d||z  z   ||z  z
  }|||z  z   }t	          |           t	          |||d|||d|||dddddf          z  S )z)Get a rotation Matrix on x, y, or z axis.c              3   <   K   | ]}t          |          d k    V  dS )r)   N)abs)rK   ns     r   rN   zMat4.rotate.<locals>.<genexpr>E  s,      //13q66Q;//////r   zvector must be normalized (<=1)r)   r   )allr   r   r   r$  )r$   r   rC  r%   r*   r   r   r   ttemp_xtemp_ytemp_zrarbrcrerfrgrirjrks                        r   rn   zMat4.rotateC  sc   ///////RR1RRR/1aIeIeE!"QAq1u!^!^a!e#!^a!e#!^a!e#!^!^a!e#!^a!e#!^a!e#!^ DzzD"b"aRQBAqRSUVXY!Z[[[[r   c                 z    t          | ddd         | ddd         z   | ddd         z   | ddd         z             S )zGet a tranpose of this Matrix.r   Nr{   r)   r   rz   rb  r#   s    r   	transposezMat4.transpose]  sG    DAJadd+d14a4j841:EFFFr   c           	          t          |          dk    s
J d            t          t          d t          | |          D                                 S )Nr&  z Can only add to other Mat4 typesc              3   &   K   | ]\  }}||z   V  d S r   r"   r   s      r   rN   zMat4.__add__.<locals>.<genexpr>c  r   r   r   r$  r   r   r6   s     r   r8   zMat4.__add__a  sQ    5zzR!CE==Ce,<,<=====>>>r   c           	          t          |          dk    s
J d            t          t          d t          | |          D                                 S )Nr&  z'Can only subtract from other Mat4 typesc              3   &   K   | ]\  }}||z
  V  d S r   r"   r   s      r   rN   zMat4.__sub__.<locals>.<genexpr>g  r   r   ry  r6   s     r   r;   zMat4.__sub__e  sQ    5zzR!JE==Ce,<,<=====>>>r   c                     | S r   r"   r#   s    r   r   zMat4.__pos__i  r  r   c                 N    t          t          d | D                                 S )Nc              3      K   | ]}| V  d S r   r"   r  s     r   rN   zMat4.__neg__.<locals>.<genexpr>m  r  r   r$  r   r#   s    r   rE   zMat4.__neg__l  r  r   c                 Z   | d         | d         z  | d         | d         z  z
  }| d         | d         z  | d         | d         z  z
  }| d         | d         z  | d         | d         z  z
  }| d         | d         z  | d         | d         z  z
  }| d         | d         z  | d         | d         z  z
  }| d         | d         z  | d         | d         z  z
  }| d	         | d         z  | d
         | d         z  z
  }| d         | d         z  | d
         | d         z  z
  }| d         | d         z  | d	         | d         z  z
  }	| d	         | d         z  | d
         | d         z  z
  }
| d         | d         z  | d
         | d         z  z
  }| d         | d         z  | d	         | d         z  z
  }| d         | d         z  | d
         | d         z  z
  }| d         | d         z  | d	         | d         z  z
  }| d         | d         z  | d
         | d         z  z
  }| d         | d         z  | d	         | d         z  z
  }| d         | d         z  | d         | d         z  z
  }| d         | d         z  | d         | d         z  z
  }| d         | d         |z  | d	         |z  z
  | d
         |z  z   z  | d         | d         |z  | d	         |z  z
  | d
         |z  z   z  z
  | d         | d         |z  | d         |z  z
  | d
         |z  z   z  z   | d         | d         |z  | d         |z  z
  | d	         |z  z   z  z
  }|dk    rt          j        d           | S d|z  }| }t          || d         |z  | d	         |z  z
  | d
         |z  z   z  || d         |z  | d         |z  z
  | d         |z  z   z  || d         |z  | d         |z  z
  | d         |	z  z   z  || d         |
z  | d         |z  z
  | d         |z  z   z  || d         |z  | d	         |z  z
  | d
         |z  z   z  || d         |z  | d         |z  z
  | d         |z  z   z  || d         |z  | d         |z  z
  | d         |z  z   z  || d         |
z  | d         |z  z
  | d         |z  z   z  || d         |z  | d         |z  z
  | d
         |z  z   z  || d         |z  | d         |z  z
  | d         |z  z   z  || d         |z  | d         |z  z
  | d         |z  z   z  || d         |z  | d         |z  z
  | d         |z  z   z  || d         |z  | d         |z  z
  | d	         |z  z   z  || d         |z  | d         |z  z
  | d         |z  z   z  || d         |	z  | d         |z  z
  | d         |z  z   z  || d         |z  | d         |z  z
  | d         |z  z   z  f          S )Nr^           r            r     r]  r{   r   r)   r   rz   z.Unable to calculate inverse of singular Matrix)	_warningswarnr$  )r$   abr   rs   efgrA  ijklmrf  r   pr?  rdetpdetndets                         r   
__invert__zMat4.__invert__o  sC   HtBx$r(T"X"55Gd2hbDH!44Gd2hbDH!44Gd2hbDH!44Gd2hbDH!44Gd2ha48!33Gd2ha48!33Gd2ha48!33Gd2ha48!33Gd2ha48!33Gd2ha47!22Gd2ha47!22Gd2ha48!33Gd2ha48!33Gd2ha47!22Gd2ha47!22Gd2ha48!33Gd1gQ$q' 11Aw$q'A+Q!3d1gkABaDGaK$q'A+5Q!CDEaDGaK$q'A+5Q!CDE aDGaK$q'A+5Q!CDE
 !88NKLLLK3wuTT!Wq[47Q;6a1DET!Wq[47Q;6a1DET!Wq[47Q;6a1DET!Wq[47Q;6a1DET!Wq[47Q;6a1DET!Wq[47Q;6a1DET!Wq[47Q;6a1DET!Wq[47Q;6a1DET!Wq[47Q;6a1DET!Wq[47Q;6a1DET!Wq[47Q;6a1DET!Wq[47Q;6a1DET!Wq[47Q;6a1DET!Wq[47Q;6a1DET!Wq[47Q;6a1DET!Wq[47Q;6a1DEG H H 	Hr   c                 T    t          t          fd| D                                 S )Nc              3   8   K   | ]}t          |          V  d S r   rH   rJ   s     r   rN   z!Mat4.__round__.<locals>.<genexpr>  r
  r   r  rP   s    `r   rQ   zMat4.__round__  r  r   c                      t          d          r  r  r6   s     r   r=   zMat4.__mul__  r  r   c                    t          |          dv s
J d            t          |          t          u r| dd d         }| dd d         }| dd d         }| dd d         }t          t          t	          t
          ||                    t          t	          t
          ||                    t          t	          t
          ||                    t          t	          t
          ||                              S | dd         }| dd         }| dd	         }| d	d
         }	|dd d         }|dd d         }|dd d         }|dd d         }t          t          t	          t
          ||                    t          t	          t
          ||                    t          t	          t
          ||                    t          t	          t
          ||                    t          t	          t
          ||                    t          t	          t
          ||                    t          t	          t
          ||                    t          t	          t
          ||                    t          t	          t
          ||                    t          t	          t
          ||                    t          t	          t
          ||                    t          t	          t
          ||                    t          t	          t
          |	|                    t          t	          t
          |	|                    t          t	          t
          |	|                    t          t	          t
          |	|                    f          S )N)r{   r&  z)Can only multiply with Mat4 or Vec4 typesr   r{   r)   r   rz   r  r  r&  )r   r  r   r  r  r  r$  )
r$   r7   r  r  r  c3r  r  r  r3s
             r   r  zMat4.__matmul__  s   5zzW$$$&Q$$$;;$addBaddBaddBaddBCb%0011Cb%0011Cb%0011Cb%00113 3 3 !A#Y!A#Y!B$Z"R%[14a4[14a4[14a4[14a4[ ST2r**++T2r**++T2r**++T2r**++T2r**++T2r**++T2r**++T2r**++T2r**++T2r**++T2r**++T2r**++T2r**++T2r**++T2r**++T2r**++%- . . 	.r   c           
      t    | j         j         | dd          d| dd          d| dd          d| dd          S )Nr   r{   r  r  r  r&  r   r#   s    r   r   zMat4.__repr__  sX    .)l4!9llD1IllTRSTVRVZll_cdfgidi_jlllr   r   )r   r$  )r4  ) r   r   r   r   r   classmethodr3  rB  r   rE  r!  rG  rO  rV  intrY  r[  r[   r   rn   rv  r8   r;   r   rE   r  rQ   r=   r  r"  r   r   r   s   @r   r$  r$    s        E E E E E E E$ & & & [&& ! ! ! ! [!2 
;d 
;v 
; 
; 
; [
; +% + +& + + + [+ )$ )D )V ) ) ) [) .t .T .t . . . . [.' ' ' ' 'C    D V    S S S S S S\E \4 \F \ \ \ \4G G G G? ? ? ?? ? ? ?  - - - -/H /H /H /Hb< < < < <Z Z Z,. ,. ,. ,.dm# m m m m m m m mr   r$  )r   mathr   warningsr  operatorr   r  r   r   r   r   r   r   r$  r"   r   r   <module>r     so  H                     + + +l- l- l- l- l-5 l- l- l-^W8 W8 W8 W8 W85 W8 W8 W8tXC XC XC XC XC5 XC XC XCv^Y ^Y ^Y ^Y ^Y5 ^Y ^Y ^YB^m ^m ^m ^m ^m5 ^m ^m ^m ^m ^mr   