a
    «ˆc¼C  ã                   @   sØ   d Z ddlmZmZmZmZmZ ddlmZm	Z	m
Z
mZmZmZ ddlmZmZmZmZmZ ddlmZ ddlmZmZ ddlmZmZ ddlmZ G d	d
„ d
eƒZG dd„ deƒZG dd„ deƒZ G dd„ deƒZ!dS )zÇThis module provides a light manager that may be used to change the
lighting of a VTK scene.

This module is largely ported from MayaVi's Lights.py but the
implementation is considerably different.

é    )ÚsinÚcosÚatan2ÚpiÚsqrt)Ú	HasTraitsÚRangeÚfalseÚInstanceÚTraitÚList)ÚViewÚGroupÚHandlerÚ
ListEditorÚItem)Útvtk)Úvtk_color_traitÚRevPrefixMap)Úconfigure_inputÚconfigure_input_data)Ústate_picklerc                   @   sf   e Zd ZdZdZdd„ Zdd„ Zdd„ Zd	d
„ Zdd„ Z	dd„ Z
ddd„Zdd„ Zdd„ Zdd„ ZdS )Ú
LightGlyphz©Manages a glyph that represents a Light source in the scene.
    This gives the user an *idea* of where the light source is placed
    while configuring the lights.
    r   c                 C   sŒ   d| _ d| _t ¡ }t ¡ }t ¡ }||_| d¡ | d¡ t	||j
ƒ t ¡ }t||ƒ t ¡  | _}||_|j}d|_d|_d|_d S )Nç        ç     €V@)éþÿÿÿr   r   )r   é   r   ç      à?)ÚelÚazr   ZArrowSourceZ	TransformZTransformFilterZ	transformÚrotate_yÚ	translater   ÚoutputZPolyDataMapperr   ZFollowerÚactorÚmapperÚpropertyÚcolorZambientZdiffuse)ÚselfZarrowÚtÚtfr$   r#   Úprop© r+   ú\/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/tvtk/pyface/light_manager.pyÚ__init__.   s"    


zLightGlyph.__init__c                 C   s$   | j  ¡ }dD ]}| |d ¡ q|S ©N)Ú__sync_trait__Ú__traits_listener__©Ú__dict__ÚcopyÚpop©r'   ÚdÚnamer+   r+   r,   Ú__get_pure_state__G   s    
zLightGlyph.__get_pure_state__c                 C   s
   t  | ¡S ©N©r   Údumps©r'   r+   r+   r,   Ú__getstate__M   s    zLightGlyph.__getstate__c                 C   s   |   ¡  t | t |¡¡ d S r9   )r-   r   Ú	set_stateÚloads_state©r'   Ú	str_stater+   r+   r,   Ú__setstate__P   s    zLightGlyph.__setstate__c                 C   s`   t |d |d  |d |d  |d |d  ƒd }|||f| j_| | j¡ |j}|| j_dS )	zfAdds the actors to the given renderer (`ren`).  The actors
        are scaled as per the given bounds.r   r   é   é   é   é   g      è?N)Úmaxr#   ÚscaleZ	add_actorZactive_cameraZcamera)r'   ÚrenÚboundsrH   Zcamr+   r+   r,   ÚaddW   s    ÿÿzLightGlyph.addc                 C   s   |  | j¡ dS )zHRemoves the actors of the glyph from the given renderer
        (`ren`).N)Zremove_actorr#   ©r'   rI   r+   r+   r,   Úremovea   s    zLightGlyph.removeNc                 C   s\   | j  | j ¡ | j  | j ¡ |dkr.|| _|dkr<|| _| j  | j¡ | j  | j¡ dS )z7Move the glyphs to the specified elevation and azimuth.N)r#   Zrotate_xr   r    r   ©r'   Ú	elevationÚazimuthr+   r+   r,   Úmove_tof   s    zLightGlyph.move_toc                 C   s   d| j _dS )zShow the glyphs on screen.r   N©r#   Z
visibilityr<   r+   r+   r,   Úshowq   s    zLightGlyph.showc                 C   s   d| j _dS )zHide the glyphs on screen.r   NrR   r<   r+   r+   r,   Úhideu   s    zLightGlyph.hidec                 C   s   || j j_dS )zChange the glyphs color.N)r#   r%   r&   )r'   Zclrr+   r+   r,   Ú	set_colory   s    zLightGlyph.set_color)NN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ú__version__r-   r8   r=   rB   rK   rM   rQ   rS   rT   rU   r+   r+   r+   r,   r   &   s   

r   c                       s  e Zd ZdZdZeddddd�Zedd	dd
d�Zede	dd�Z
eejdƒZeddddd�ZedƒZde_eedd�edd�edd�edd�edd�ƒZ‡ f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!d0d1„ Z"d2d3„ Z#d4d5„ Z$‡  Z%S )6ÚCameraLightz?This class manages a tvtk.Light object and a LightGlyph object.r   g     €VÀr   r   zthe elevation of the light©Údescg     €fÀç     €f@z the aziumthal angle of the lightFz(specifies if the light is enabled or notr+   ç      ð?zthe intensity of the light©r_   r_   r_   zthe color of the lightÚactivate©r7   rO   rP   Ú	intensityr&   c                    sh   t ƒ | _tt| ƒjf i |¤Ž d| j_|  | j¡ |  	| j
¡ |  | j¡ |j | j¡ |  |j¡ d S )NZcamera_light)r   ÚglyphÚsuperr[   r-   ÚsourceZ
light_typeÚ_intensity_changedrc   Ú_activate_changedra   Ú_color_changedr&   ÚrendererZ	add_lightÚon_trait_changeÚrender)r'   ÚrenwinÚtraits©Ú	__class__r+   r,   r-   ¦   s    zCameraLight.__init__c                 C   s$   | j  ¡ }dD ]}| |d ¡ q|S r.   r1   r5   r+   r+   r,   r8   ±   s    
zCameraLight.__get_pure_state__c                 C   s
   t  | ¡S r9   r:   r<   r+   r+   r,   r=   ·   s    zCameraLight.__getstate__c                 C   s   t  | t  |¡¡ d S r9   )r   r>   r?   r@   r+   r+   r,   rB   º   s    zCameraLight.__setstate__c                 C   s0   |j }| j|jdd� | | j¡ |  |¡ dS )z”Remove the light source and the glyph from the
        renderwindow.  This is usually to be called when the Light is
        removed from the scene.T)rM   N)rj   rk   rl   Zremove_lightrf   Úremove_glyph)r'   rm   rI   r+   r+   r,   ÚcloseÁ   s    zCameraLight.closec                 C   s   | j  ||¡ dS )zsAdd the light glyph to the passed renderer ('ren').  Scale
        the actors using the bounds (`bounds`) provided.N)rd   rK   )r'   rI   rJ   r+   r+   r,   Ú	add_glyphÊ   s    zCameraLight.add_glyphc                 C   s   | j  |¡ dS )z0Remove the light glyph from the passed renderer.N)rd   rM   rL   r+   r+   r,   rq   Ï   s    zCameraLight.remove_glyphc                 C   s   || _ || _dS )zTMove the light to the specified elevation and azimuthal
        angles (in degrees).N)rO   rP   rN   r+   r+   r,   rQ   Ó   s    zCameraLight.move_toc                 C   s.   |rd| j _| j ¡  nd| j _| j ¡  d S )Nr   r   )rf   Úswitchrd   rS   rT   ©r'   Úvalr+   r+   r,   rh   Ü   s
    zCameraLight._activate_changedc                 C   s   || j _d S r9   )rf   rc   ru   r+   r+   r,   rg   ä   s    zCameraLight._intensity_changedc                 C   s   | j  |¡ | j |¡ d S r9   )rf   rU   rd   ru   r+   r+   r,   ri   ç   s    zCameraLight._color_changedc                 C   s&   | j  || j¡ |  || j¡| j_d S r9   )rd   rQ   rP   Ú_to_posrf   Úpositionru   r+   r+   r,   Ú_elevation_changedë   s    zCameraLight._elevation_changedc                 C   s&   | j  | j|¡ |  | j|¡| j_d S r9   )rd   rQ   rO   rw   rf   rx   ru   r+   r+   r,   Ú_azimuth_changedï   s    zCameraLight._azimuth_changedc                 C   sN   |t  d }d| t  d }t|ƒt|ƒ }t|ƒ}t|ƒt|ƒ }|||fS )zVConvert the given elevation and azimuth angles (degrees) to
        a position vector.r^   r   )r   r   r   )r'   rO   rP   ÚthetaÚphiÚxÚyÚzr+   r+   r,   rw   ö   s    zCameraLight._to_posc                 C   sH   t ||ƒ}t t|d |d  ƒ|ƒ}|d t }d|d t  }||fS )zVGiven the position vector, return an elevation and azimuth
        angle (in degrees).rD   r^   r   )r   r   r   )r'   r}   r~   r   r{   r|   r   r   r+   r+   r,   Ú	_from_pos   s
    
zCameraLight._from_pos)&rV   rW   rX   rY   rZ   r   rO   rP   r   r	   ra   r
   r   ÚLightrf   rc   r   r&   r]   r   r   Zdefault_viewr-   r8   r=   rB   rr   rs   rq   rQ   rh   rg   ri   ry   rz   rw   r€   Ú__classcell__r+   r+   ro   r,   r[   ‚   sH   ÿÿÿÿþ		
r[   c                   @   s   e Zd ZdZdd„ ZdS )ÚCloseHandlerzFThis class cleans up after the UI for the Light Manager is
    closed.c                 C   s   |j }| ¡  dS )z3This method is invoked when the user closes the UI.T)ÚobjectÚon_ui_close)r'   ÚinfoZis_okZlight_managerr+   r+   r,   rr     s    zCloseHandler.closeN)rV   rW   rX   rY   rr   r+   r+   r+   r,   rƒ     s   rƒ   c                       sÈ   e Zd ZdZdZedddœddd�Zed	d
ddd�Ze	e
eddd�dd�Zeeddƒedddd�ddgd�Z‡ f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‡  ZS ).ÚLightManagerav  This class manages all the lights and presents a GUI for the
    lights.

    There are two default lighting modes possible (specified via the
    `light_mode` trait).  The 'vtk' mode uses the default lighting
    mode which is basically a single headlight.  The 'raymond' mode
    creates three lights to give better overall illumination (thanks
    to Raymond Maple).
    r   r   rD   )ÚraymondZvtkrˆ   z!specifies a default lighting mode)Údefault_valuer]   rC   é   rF   zspecifies the number of lightsr\   Tr�   )Zuse_notebookZ	page_name)ZeditorÚrecordÚ
light_modeÚnumber_of_lightsÚlightsÚcustomF)ÚstyleZ
show_labelÚOK)Ú	resizableÚbuttonsc                    sr   t t| ƒjf i |¤Ž d | _|| _| jj}d|jd _g | _t| j	ƒD ]}t
| jƒ}| j |¡ qF|  | j¡ d S )Nr   r   )re   r‡   r-   Úuirm   rj   rŽ   rc   Úranger�   r[   ÚappendÚ_light_mode_changedrŒ   )r'   rm   rn   rI   ÚiÚlightro   r+   r,   r-   N  s    
zLightManager.__init__c                 C   s$   | j  ¡ }dD ]}| |d ¡ q|S )N)r/   rm   r”   r0   r1   r5   r+   r+   r,   r8   f  s    
zLightManager.__get_pure_state__c                 C   s    ddg}t j| ||dgd� d S )NrŒ   r�   rŽ   )ÚfirstÚlast)r   r>   )r'   Ústaterš   r+   r+   r,   Ú__set_pure_state__m  s    zLightManager.__set_pure_state__c                 C   s
   t  | ¡S r9   r:   r<   r+   r+   r,   r=   q  s    zLightManager.__getstate__c                 C   s"   t  |¡}t  |¡ |  |¡ d S r9   )r   r?   Zupdate_stater�   )r'   rA   rœ   r+   r+   r,   rB   t  s    

zLightManager.__setstate__c                 C   sˆ   | j du r`|  ¡  tttdd�tdd�dd�ttddd	�d
ddd�ddgtƒ d�}|  | ¡| _ n$z| j j ¡  W n ty‚   Y n0 dS )zPops up the GUI control widget.NrŒ   rb   r�   r‡   )ÚlabelrŽ   r�   )r7   r�   ZLightsTF)rž   ÚselectedZshow_labelsr‘   )r’   r“   Úhandler)	r”   Ú_show_glyphsr   r   r   rƒ   ÚcontrolÚRaiseÚAttributeError)r'   Úviewr+   r+   r,   Ú	configure�  s&    
þþø	zLightManager.configurec                 C   s(   | j j}| jD ]}| |¡ qd| _dS )z¥This method removes the glyphs used to show the lights on
        screen when the GUI dialog is closed.  This is typically only
        called from the CloseHandler.N)rm   rj   rŽ   rq   r”   )r'   rI   Úlr+   r+   r,   r…   •  s    
zLightManager.on_ui_closec                 C   s@   | j }|j}| ¡  | ¡ }| jD ]}| ||¡ q"| ¡  dS )z3Shows the glyphs when the light config UI is shown.N)rm   rj   Z
reset_zoomZcompute_visible_prop_boundsrŽ   rs   rl   )r'   ÚrwrI   rJ   r™   r+   r+   r,   r¡   ¡  s    
zLightManager._show_glyphsc                 C   s  | j }|dkr¼tt|ƒƒD ]Z}|dk rFd|| _d|| _d|| _qd|| _||  dd¡ d|| _d|| _q|d  d	d	¡ |d
  dd¡ d|d
 _|d  dd¡ d|d _nVtt|ƒƒD ]H}||  dd¡ d|| _d|| _|dk�rd|| _qÈd|| _qÈd S )Nrˆ   rC   Tr_   r`   Fr   r   g     €F@r   g      >Àg      NÀg333333ã?rD   g      N@r   )rŽ   r•   Úlenra   rc   r&   rQ   )r'   ÚmoderŽ   r˜   r+   r+   r,   r—   «  s.    







z LightManager._light_mode_changedc                 C   s†   | j j}d}||krd S ||krNt|| ƒD ]}t| j ƒ}| j |¡ q,d}n4||k r‚t|| ƒD ]}| j ¡ }| | j ¡ qbd}d S )NFT)rm   rj   r•   r[   rŽ   r–   r4   rr   )r'   ÚoldÚnewrI   Úchangedr˜   r™   r+   r+   r,   Ú_number_of_lights_changedÈ  s    

z&LightManager._number_of_lights_changed)rV   rW   rX   rY   rZ   r   rŒ   r   r�   r   r[   r   rŽ   r   r   r   r¥   r-   r8   r�   r=   rB   r¦   r…   r¡   r—   r®   r‚   r+   r+   ro   r,   r‡     s>   ÿýÿþÿü

r‡   N)"rY   Úmathr   r   r   r   r   Z
traits.apir   r   r	   r
   r   r   Ztraitsui.apir   r   r   r   r   Ztvtk.apir   Ztvtk.tvtk_baser   r   Ztvtk.commonr   r   Zapptools.persistencer   r   r[   rƒ   r‡   r+   r+   r+   r,   Ú<module>   s    \ 