3
ÌœŸWD  ã               @   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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ÚTraitRevPrefixMap)Úis_old_pipelineÚ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jƒ }tjƒ }tjƒ }||_|jdƒ |jdƒ t	||j
ƒ tjƒ }t||ƒ tjƒ  | _}||_|j}d	|_d|_d|_d S )
Ng        g     €V@é   r   é   g      à?éþÿÿÿ)r   r   r   )r   r   r   )ÚelÚazr   ZArrowSourceÚ	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/venv/lib/python3.6/site-packages/tvtk/pyface/light_manager.pyÚ__init__.   s"    


zLightGlyph.__init__c             C   s(   | j jƒ }xdD ]}|j|d ƒ qW |S )NÚ__sync_trait__Ú__traits_listener__)r.   r/   )Ú__dict__ÚcopyÚpop)r'   ÚdÚnamer+   r+   r,   Ú__get_pure_state__G   s    

zLightGlyph.__get_pure_state__c             C   s
   t j| ƒS )N)r   Údumps)r'   r+   r+   r,   Ú__getstate__M   s    zLightGlyph.__getstate__c             C   s   | j ƒ  tj| tj|ƒƒ d S )N)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}|| j_dS )	zfAdds the actors to the given renderer (`ren`).  The actors
        are scaled as per the given bounds.r   r   é   r   é   é   g      è?N)Úmaxr#   ÚscaleZ	add_actorZactive_cameraÚcamera)r'   ÚrenÚboundsr@   Úcamr+   r+   r,   ÚaddW   s    zLightGlyph.addc             C   s   |j | jƒ dS )zHRemoves the actors of the glyph from the given renderer
        (`ren`).N)Zremove_actorr#   )r'   rB   r+   r+   r,   Úremovea   s    zLightGlyph.removeNc             C   s\   | j j| j ƒ | j j| j ƒ |dkr.|| _|dkr<|| _| j j| 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#   Ú
visibility)r'   r+   r+   r,   Úshowq   s    zLightGlyph.showc             C   s   d| j _dS )zHide the glyphs on screen.r   N)r#   rJ   )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-   r5   r7   r;   rE   rF   rI   rK   rL   rM   r+   r+   r+   r,   r   &   s   

r   c                   s  e Zd ZdZdZed2dddd�Zed3dddd�Zed	e	d
d�Z
eejf ƒZeddddd�Zed4ƒ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!d,d-„ Z"d.d/„ Z#d0d1„ Z$‡  Z%S )5ÚCameraLightz?This class manages a tvtk.Light object and a LightGlyph object.r   g     €V@g        zthe elevation of the light)Údescg     €f@z the aziumthal angle of the lightFz(specifies if the light is enabled or notç      ð?zthe intensity of the lightzthe color of the lightÚactivate)r4   rG   rH   Ú	intensityr&   c                sd   t ƒ | _tt| ƒjf |Ž d| j_| j| jƒ | j	| j
ƒ | j| jƒ |jj| jƒ | j|jƒ d S )NZcamera_light)r   ÚglyphÚsuperrS   r-   ÚsourceZ
light_typeÚ_intensity_changedrW   Ú_activate_changedrV   Ú_color_changedr&   ÚrendererZ	add_lightÚon_trait_changeÚrender)r'   ÚrenwinÚtraits)Ú	__class__r+   r,   r-   ¦   s    zCameraLight.__init__c             C   s(   | j jƒ }xdD ]}|j|d ƒ qW |S )Nr.   r/   )r.   r/   )r0   r1   r2   )r'   r3   r4   r+   r+   r,   r5   ±   s    

zCameraLight.__get_pure_state__c             C   s
   t j| ƒS )N)r   r6   )r'   r+   r+   r,   r7   ·   s    zCameraLight.__getstate__c             C   s   t j| t j|ƒƒ d S )N)r   r8   r9   )r'   r:   r+   r+   r,   r;   º   s    zCameraLight.__setstate__c             C   s0   |j }| j|jdd� |j| jƒ | 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)rF   N)r^   r_   r`   Zremove_lightrZ   Úremove_glyph)r'   ra   rB   r+   r+   r,   ÚcloseÁ   s    zCameraLight.closec             C   s   | j j||ƒ dS )zsAdd the light glyph to the passed renderer ('ren').  Scale
        the actors using the bounds (`bounds`) provided.N)rX   rE   )r'   rB   rC   r+   r+   r,   Ú	add_glyphÊ   s    zCameraLight.add_glyphc             C   s   | j j|ƒ dS )z0Remove the light glyph from the passed renderer.N)rX   rF   )r'   rB   r+   r+   r,   rd   Ï   s    zCameraLight.remove_glyphc             C   s   || _ || _dS )zTMove the light to the specified elevation and azimuthal
        angles (in degrees).N)rG   rH   )r'   rG   rH   r+   r+   r,   rI   Ó   s    zCameraLight.move_toc             C   s.   |rd| j _| jjƒ  nd| j _| jjƒ  d S )Nr   r   )rZ   ÚswitchrX   rK   rL   )r'   Úvalr+   r+   r,   r\   Ü   s
    zCameraLight._activate_changedc             C   s   || j _d S )N)rZ   rW   )r'   rh   r+   r+   r,   r[   ä   s    zCameraLight._intensity_changedc             C   s,   t ƒ r|| j_n| jj|ƒ | jj|ƒ d S )N)r   rZ   r&   rM   rX   )r'   rh   r+   r+   r,   r]   ç   s    
zCameraLight._color_changedc             C   s&   | j j|| jƒ | j|| jƒ| j_d S )N)rX   rI   rH   Ú_to_posrZ   Úposition)r'   rh   r+   r+   r,   Ú_elevation_changedî   s    zCameraLight._elevation_changedc             C   s&   | j j| j|ƒ | j| j|ƒ| j_d S )N)rX   rI   rG   ri   rZ   rj   )r'   rh   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.g     €f@g     €V@)r   r   r   )r'   rG   rH   ÚthetaÚphiÚxÚyÚzr+   r+   r,   ri   ù   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).r   g     €f@g     €V@)r   r   r   )r'   ro   rp   rq   rm   rn   r   r   r+   r+   r,   Ú	_from_pos  s
    
zCameraLight._from_posg     €VÀg     €fÀ)rU   rU   rU   )&rN   rO   rP   rQ   rR   r   rG   rH   r   r	   rV   r
   r   ÚLightrZ   rW   r   r&   rT   r   r   Zdefault_viewr-   r5   r7   r;   re   rf   rd   rI   r\   r[   r]   rk   rl   ri   rr   Ú__classcell__r+   r+   )rc   r,   rS   ‚   s>   		
rS   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 }|jƒ  dS )z3This method is invoked when the user closes the UI.T)ÚobjectÚon_ui_close)r'   ÚinfoÚis_okZlight_managerr+   r+   r,   re     s    zCloseHandler.closeN)rN   rO   rP   rQ   re   r+   r+   r+   r,   ru     s   ru   c                   sÌ   e Zd ZdZdZede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   Úraymondr   r   )r{   Úvtkz!specifies a default lighting mode)rT   r<   é   r>   zspecifies the number of lightsTrs   )Úuse_notebookÚ	page_name)ÚeditorÚrecordÚ
light_modeÚnumber_of_lightsÚlightsÚcustomF)ÚstyleÚ
show_labelÚOK)Ú	resizableÚbuttonsc                sr   t t| ƒjf |Ž d | _|| _| jj}d|jd _g | _x(t| j	ƒD ]}t
| jƒ}| jj|ƒ qDW | j| jƒ d S )Ng        r   )rY   rz   r-   Úuira   r^   r„   rW   Úrangerƒ   rS   ÚappendÚ_light_mode_changedr‚   )r'   ra   rb   rB   ÚiÚlight)rc   r+   r,   r-   P  s    
zLightManager.__init__c             C   s(   | j jƒ }xdD ]}|j|d ƒ qW |S )Nr.   ra   r‹   r/   )r.   ra   r‹   r/   )r0   r1   r2   )r'   r3   r4   r+   r+   r,   r5   h  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   r8   )r'   Ústater‘   r+   r+   r,   Ú__set_pure_state__o  s    zLightManager.__set_pure_state__c             C   s
   t j| ƒS )N)r   r6   )r'   r+   r+   r,   r7   s  s    zLightManager.__getstate__c             C   s"   t j|ƒ}t j|ƒ | j|ƒ d S )N)r   r9   Úupdate_stater”   )r'   r:   r“   r+   r+   r,   r;   v  s    

zLightManager.__setstate__c             C   sŠ   | j dkr`| jƒ  tttdd�tdd�dd�ttddd	�d
ddd�ddgtƒ d�}|j | ƒ| _ n&y| j jjƒ  W n tk
r„   Y nX dS )zPops up the GUI control widget.Nr‚   )r4   rƒ   rz   )Úlabelr„   r…   )r4   r†   ZLightsTF)r–   ÚselectedÚshow_labelsrˆ   )r‰   rŠ   Úhandler)	r‹   Ú_show_glyphsr   r   r   ru   ÚcontrolÚRaiseÚAttributeError)r'   Úviewr+   r+   r,   Ú	configureƒ  s     

zLightManager.configurec             C   s,   | j j}x| jD ]}|j|ƒ qW 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)ra   r^   r„   rd   r‹   )r'   rB   Úlr+   r+   r,   rw   —  s    zLightManager.on_ui_closec             C   sD   | j }|j}|jƒ  |jƒ }x| jD ]}|j||ƒ q$W |jƒ  dS )z3Shows the glyphs when the light config UI is shown.N)ra   r^   Ú
reset_zoomÚcompute_visible_prop_boundsr„   rf   r`   )r'   ÚrwrB   rC   r�   r+   r+   r,   rš   £  s    zLightManager._show_glyphsc             C   s  | j }|dkrÀxjtt|ƒƒD ]Z}|dk rHd|| _d|| _d|| _qd|| _|| jddƒ d|| _d|| _qW |d jddƒ |d	 jddƒ d|d	 _|d jddƒ d|d _nZxXtt|ƒƒD ]H}|| jddƒ d|| _d|| _|dk�rd|| _qÎd|| _qÎW d S )Nr{   r<   Tç      ð?Fg        r   g     €F@r   g      >@g      N@g333333ã?r   g      à?)r¤   r¤   r¤   )r¤   r¤   r¤   g      >Àg      NÀg      >À)r¤   r¤   r¤   )r„   rŒ   ÚlenrV   rW   r&   rI   )r'   Úmoder„   r�   r+   r+   r,   rŽ   ­  s.    







z LightManager._light_mode_changedc             C   sŽ   | j j}d}||krd S ||krRx*t|| ƒD ]}t| j ƒ}| jj|ƒ q.W d}n8||k rŠx*t|| ƒD ]}| jjƒ }|j| j ƒ qhW d}d S )NFT)ra   r^   rŒ   rS   r„   r�   r2   re   )r'   ÚoldÚnewrB   Úchangedr�   r�   r+   r+   r,   Ú_number_of_lights_changedÊ  s    

z&LightManager._number_of_lights_changed)rN   rO   rP   rQ   rR   r   r   r‚   r   rƒ   r   rS   r   r„   r   r   r   rž   r-   r5   r”   r7   r;   rŸ   rw   rš   rŽ   rª   rt   r+   r+   )rc   r,   rz     s0   


rz   N)#rQ   Úmathr   r   r   r   r   Ú
traits.apir   r   r	   r
   r   r   Útraitsui.apir   r   r   r   r   Útvtk.apir   Útvtk.tvtk_baser   r   Ztvtk.commonr   r   r   Zapptools.persistencer   r   rS   ru   rz   r+   r+   r+   r,   Ú<module>   s    \ 