U
    ê‹Ñh  ã                   @   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   úI/tmp/user/2202/pip-unpacked-wheel-9k0_ldo3/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*    þ
'
