U
    Evf                     @   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   ?/tmp/pip-unpacked-wheel-7vhvci0g/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   Z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   Z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    

r3   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-   r.   r0   r1   r2   r   r   r   ortho_transformation   s    




r4   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 r5   )r   r    any)r6   r)   r7   r$   r8   r9   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iMr6   Zvecrr   r   r   inv_transform   s    
rD   c                 C   s   t | ||t | gS N)r   r   Z	ones_like)rA   rB   rC   r   r   r   r?      s    r?   c                 C   s   t | ||}t||S )z<
    Transform the points by the projection matrix *M*.
    )r?   r;   rA   rB   rC   r)   r6   r   r   r   proj_transform   s    rG   c                 C   s   t | ||}t||S )zy
    Transform the points by the projection matrix
    and return the clipping result
    returns txs, tys, tzs, tis
    )r?   r=   rF   r   r   r   proj_transform_clip   s    rH   c                 C   s   t t| |S rE   )r   Zcolumn_stackproj_trans_points)pointsr)   r   r   r   proj_points   s    rK   c                 C   s   t |  \}}}t||||S rE   )ziprG   )rJ   r)   rA   rB   rC   r   r   r   rI      s    rI   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    

rN   )N)__doc__Znumpyr   Znumpy.linalgr   r   r   r   r&   r*   r+   r3   r4   r;   r=   rD   r?   rG   Z	transformrH   rK   rI   rN   r   r   r   r   <module>   s*    
'
