o
    li                     @   s   d Z ddlZddlmZ dd Z	d%ddZdd Zd	d
 Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd Zdd ZeZdd Zdd  Zd!d" Zd#d$ ZdS )&z,
Various transforms used for by the 3D code
    Nc                 C   sl   t |}|| }| | }|| }t ||d}|t jt || | dd| }| | d jddd S )a7  
    Return the distance(s) from point(s) *p* to segment(s) (*s0*, *s1*).

    Parameters
    ----------
    p : (ndim,) or (N, ndim) array-like
        The points from which the distances are computed.
    s0, s1 : (ndim,) or (N, ndim) array-like
        The xy(z...) coordinates of the segment endpoints.
       r      )axisg      ?)npasarraywheremultiplyouterclipsum)ps0s1s01s0pl2p1 r   Y/home/ubuntu/SoloSpeech/.venv/lib/python3.10/site-packages/mpl_toolkits/mplot3d/proj3d.py_line2d_seg_dist	   s   
$r   c              	   C   s   ||  }|| }|| }	|dur!|\}
}}||
 }|| }|	| }	t d| dd|  | gdd| d| | gddd|	 | |	 gg dgS )z
    Produce a matrix that scales homogeneous coords in the specified ranges
    to [0, 1], or [0, pb_aspect[i]] if the plotbox aspect ratio is specified.
    Nr   r   r   r   r   r   r   array)xminxmaxyminymaxzminzmax	pb_aspectdxdydzaxayazr   r   r   world_transformation    s   
r'   c           	      C   s   | t j|  \}}}t |}t |}dt |d d  }t || | | || | ||  || | ||  g|| | ||  || | | || | ||  g|| | ||  || | ||  || | | gg}|S )zK
    Produce a rotation matrix for an angle in radians about a vector.
    r   )r   linalgnormsincosr   )	vanglevxvyvzsctRr   r   r   rotation_about_vector6   s   

444r5   c                 C   sv   | | }|t j| }t ||}|t j| }t ||}|dkr6t|| }t ||}t ||}|||fS )a  
    Get the unit viewing axes in data coordinates.

    Parameters
    ----------
    E : 3-element numpy array
        The coordinates of the eye/camera.
    R : 3-element numpy array
        The coordinates of the center of the view box.
    V : 3-element numpy array
        Unit vector in the direction of the vertical axis.
    roll : float
        The roll angle in radians.

    Returns
    -------
    u : 3-element numpy array
        Unit vector pointing towards the right of the screen.
    v : 3-element numpy array
        Unit vector pointing towards the top of the screen.
    w : 3-element numpy array
        Unit vector pointing out of the screen.
    r   )r   r(   r)   crossr5   dot)Er4   Vrollwur,   Rrollr   r   r   
_view_axesG   s   
r>   c                 C   sP   t d}t d}| ||g|ddddf< | |dddf< t ||}|S )a  
    Return the view transformation matrix.

    Parameters
    ----------
    u : 3-element numpy array
        Unit vector pointing towards the right of the screen.
    v : 3-element numpy array
        Unit vector pointing towards the top of the screen.
    w : 3-element numpy array
        Unit vector pointing out of the screen.
    E : 3-element numpy array
        The coordinates of the eye/camera.
       N   r   )r   eyer7   )r<   r,   r;   r8   MrMtMr   r   r   _view_transformation_uvwn   s   

rE   c                 C   s&   t | |||\}}}t|||| }|S )az  
    Return the view transformation matrix.

    Parameters
    ----------
    E : 3-element numpy array
        The coordinates of the eye/camera.
    R : 3-element numpy array
        The coordinates of the center of the view box.
    V : 3-element numpy array
        Unit vector in the direction of the vertical axis.
    roll : float
        The roll angle in radians.
    )r>   rE   )r8   r4   r9   r:   r<   r,   r;   rD   r   r   r   view_transformation   s   rF   c                 C   sb   |}d}| | | |  }d| |  | |  }t |dddgd|| ddgdd||gg dg}|S )Nr   r   )r   r   r   r   r   )zfrontzbackfocal_lengtheabr2   proj_matrixr   r   r   persp_transformation   s   
rO   c              	   C   s>   | |  }| |  }t g dg dg ddd||gg}|S )N)r   r   r   r   )r   r   r   r   )r   r   rG   r   r   r   )rH   rI   rL   rM   rN   r   r   r   ortho_transformation   s   



rP   c                 C   sF   t || }|d }|d | |d | |d | }}}|||fS Nr@   r   r   r   )r   r7   )vecrD   vecwr;   txstystzsr   r   r   _proj_transform_vec   s   (
rW   c                 C   s   t || }|d }|d | |d | |d | }}}d|d k|d dk@ d|d k@ |d dk@ }t |rA|d dk }||||fS rQ   )r   r7   any)rR   rD   rS   r;   rT   rU   rV   tisr   r   r   _proj_transform_vec_clip   s   (0
rZ   c                 C   s\   t |}t| ||}t||}z||d  }W n	 ty"   Y nw |d |d |d fS )zK
    Transform the points by the inverse of the projection matrix *M*.
    r@   r   r   r   )r(   inv_vec_pad_onesr   r7   OverflowError)xsyszsrD   iMrR   vecrr   r   r   inv_transform   s   
rc   c                 C   s   t | ||t | gS N)r   r   	ones_like)r^   r_   r`   r   r   r   r\      s   r\   c                 C      t | ||}t||S )z<
    Transform the points by the projection matrix *M*.
    )r\   rW   r^   r_   r`   rD   rR   r   r   r   proj_transform   s   
rh   c                 C   rf   )zy
    Transform the points by the projection matrix
    and return the clipping result
    returns txs, tys, tzs, tis
    )r\   rZ   rg   r   r   r   proj_transform_clip   s   
ri   c                 C   s   t t| |S rd   )r   column_stackproj_trans_points)pointsrD   r   r   r   proj_points   s   rm   c                 C   s   t |  \}}}t||||S rd   )ziprh   )rl   rD   r^   r_   r`   r   r   r   rk      s   rk   c                 C   sN   t |t |}}t g dd|| dgd||dgg dg}t || S )N)r   r   r   r   r   r   )r   r+   r*   r   r7   )r9   alphacosasinaM1r   r   r   rot_x   s   

rs   rd   )__doc__numpyr   numpy.linalgr(   r   r'   r5   r>   rE   rF   rO   rP   rW   rZ   rc   r\   rh   	transformri   rm   rk   rs   r   r   r   r   <module>   s,    
'
