U
    Âmœd  ã                   @   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*    þ
'
