U
    md                     @   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      )Zaxisg      ?)npZasarraywheremultiplyouterZclipsum)ps0s1Zs01Zs0pl2p1 r   T/home/sam/Atlas/atlas_env/lib/python3.8/site-packages/mpl_toolkits/mplot3d/proj3d.py_line2d_seg_dist	   s    
$r   c              	   C   s   ||  }|| }|| }	|dk	rB|\}
}}||
 }|| }|	| }	t d| dd|  | gdd| d| | gddd|	 | |	 gddddg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   array)ZxminZxmaxZyminZymaxZzminZzmaxZ	pb_aspectZdxZdyZdzZaxZ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   )	vZangleZvxZvyZvzsctRr   r   r   rotation_about_vector6   s    

444r   c                 C   sv   | | }|t j| }t ||}|t j| }t ||}|dkrlt|| }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   crossr   dot)Er   Vrollwur   Z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   eyer!   )r&   r   r%   r"   ZMrZMtMr   r   r   _view_transformation_uvwn   s    

r,   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'   r,   )r"   r   r#   r$   r&   r   r%   r+   r   r   r   view_transformation   s    r-   c              	   C   sf   |}d}| | | |  }d| |  | |  }t |dddgd|| ddgdd||gddddgg}|S )Nr   r   r   r   )zfrontzbackZfocal_lengtheabr   proj_matrixr   r   r   persp_transformation   s    

r5   c              	   C   sJ   | |  }| |  }t ddddgddddgddddgdd||gg}|S )Nr   r   r.   r   )r/   r0   r2   r3   r4   r   r   r   ortho_transformation   s    




r6   c                 C   sF   t || }|d }|d | |d | |d |   }}}|||fS Nr)   r   r   r   )r   r!   )vecr+   vecwr%   txstystzsr   r   r   _proj_transform_vec   s    (r=   c                 C   s   t || }|d }|d | |d | |d |   }}}d|d k|d dk@ d|d k@ |d dk@ }t |r|d dk }||||fS r7   )r   r!   any)r8   r+   r9   r%   r:   r;   r<   Ztisr   r   r   _proj_transform_vec_clip   s    (0
r?   c                 C   s^   t |}t| ||}t||}z||d  }W n tk
rF   Y nX |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   r!   OverflowError)xsyszsr+   ZiMr8   Zvecrr   r   r   inv_transform   s    
rF   c                 C   s   t | ||t | gS N)r   r   Z	ones_like)rC   rD   rE   r   r   r   rA      s    rA   c                 C   s   t | ||}t||S )z<
    Transform the points by the projection matrix *M*.
    )rA   r=   rC   rD   rE   r+   r8   r   r   r   proj_transform   s    rI   c                 C   s   t | ||}t||S )zy
    Transform the points by the projection matrix
    and return the clipping result
    returns txs, tys, tzs, tis
    )rA   r?   rH   r   r   r   proj_transform_clip   s    rJ   c                 C   s   t t| |S rG   )r   Zcolumn_stackproj_trans_points)pointsr+   r   r   r   proj_points   s    rM   c                 C   s   t |  \}}}t||||S rG   )ziprI   )rL   r+   rC   rD   rE   r   r   r   rK      s    rK   c              	   C   sV   t |t | }}t ddddgd|| dgd||dgddddgg}t || S )Nr   r   )r   r   r   r   r!   )r#   alphaZcosaZsinaZM1r   r   r   rot_x   s    

rP   )N)__doc__numpyr   Znumpy.linalgr   r   r   r   r'   r,   r-   r5   r6   r=   r?   rF   rA   rI   Z	transformrJ   rM   rK   rP   r   r   r   r   <module>   s*    
'
