a
    «ˆc›N  ã                   @   sä   d Z ddlmZmZmZmZmZmZmZm	Z	m
Z
mZmZ ddl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 ddlmZ ddlmZ dd	lZd
d„ 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 basic picking functionality.  Using this, one
can interactively select a point and/or a cell in the data.  One can
also can use a world point picker (i.e. a generic point in space) and
probe for the data at that point.

é    )Ú	HasTraitsÚTraitÚIntÚArrayÚAnyÚFloatÚInstanceÚRangeÚtrueÚStrÚfalse)ÚViewÚGroupÚItemÚHandler)Útvtk)ÚRevPrefixMapÚfalse_bool_trait)Ú	TVTKScene)Úconfigure_input)Ústate_picklerNc                 C   s<   |  }}|r8z|j }|r|}W q ty4   d}Y q0 q|S )ziAttempts to get the deepest possible data value in the
    pipeline.  Used when probing a selected point.N)ÚinputÚAttributeError)ÚdataÚtmpÚinp© r   úU/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/tvtk/pyface/picker.pyÚget_last_input&   s    r   c                   @   sl   e Zd ZdZeedd�Zeddd�Zeddd�Z	eedd�Z
edd	g d
¢ddd�ZeejƒZeeƒZeZdS )Ú
PickedDataz"This class stores the picked data.z(specifies the validity of the pick event©Údescéÿÿÿÿzthe picked point IDzthe picked cell IDz&specifies if the pick is a world pick.Úd©é   )ÚxÚyÚzr%   ú"the coordinate of the picked point)ÚlabelsÚcolsr!   N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   Úvalidr   Úpoint_idÚcell_idÚ
world_pickr   Ú
coordinater   r   Ú	TextActorÚ
text_actorr   Úrenwinr   r   r   r   r   r   r   7   s   ÿÿÿ
r   c                   @   s   e Zd ZdZdd„ ZdS )ÚPickHandlerzžThis is the handler for the picked data.  Subclass this to do
    what you need.  Each time a pick occurs the handle_pick is called
    by the `Picker` class.c                 C   s   dS )zzCalled when a pick event happens.

        Parameters
        ----------

        - data : `PickedData` instance.
        Nr   )Úselfr   r   r   r   Úhandle_pick[   s    zPickHandler.handle_pickN)r,   r-   r.   r/   r:   r   r   r   r   r8   V   s   r8   c                
       sÚ   e Zd ZdZeddedd�Zeddeddƒdd�Zeddee	dd�Z
eddeddƒd	d�Zeddedd
ƒdd�ZeZeeddd�eddd�eddd�eddd�eddd�eddd�ƒZ‡ fdd„Zdd„ Zdd„ Z‡  ZS )ÚDefaultPickHandlerz(The default handler for the picked data.Nzthe picked IDr    r#   r$   r)   zthe scalar at picked pointzthe vector at picked point)r%   r%   zthe tensor at picked pointÚIDÚreadonly©ÚnameÚstyler4   ÚscalarÚvectorÚtensorÚhistoryÚcustomc                    s,   t t| ƒjf i |¤Ž g g g g g dœ| _d S )N©r<   r4   rA   rB   rC   )Úsuperr;   Ú__init__r   )r9   Útraits©Ú	__class__r   r   rH   „   s    ÿzDefaultPickHandler.__init__c                 C   sÊ   |j r |jdkr|j| _n|jdkr,|j| _t|jƒ| _|jrX|jj|jj|jj	dœ}nddddœ}| 
¡ D ]0}|| r�t| ||| | j ƒ qlt| |dƒ qlndD ]}t| |dƒ q¤|  |j|j¡ dS )z*Called when a pick event happens.
        r"   )rA   rB   rC   NrF   )Úvalid_r1   r<   r2   Úlistr4   r   ZscalarsZvectorsZtensorsÚkeysÚsetattrÚ_update_datar7   r6   )r9   r   Z
array_datar?   r   r   r   r:   �   s,    


þþzDefaultPickHandler.handle_pickc              	   C   s”  dD ]<}t | |ƒ}| j |¡ t | |ƒ¡ |  jd||f 7  _qtj| jd dd�}tj| jd dd�}tj| jd dd�}| jd urÄ| j	d urÄ| j
d urÄ|jd| j|||| j	| j| j
f d	� nÌ| jd u�r<| j	d u�r<tj| j	dd�}t d¡}	tdƒD ]}
tj| j|
 dd�|	|
< qþ|jd
| j|||||	f d	� nT| j	d u�rvtj| j	dd�}|jd| j||||f d	� n|jd| j|||f d	� d S )NrF   z%s: %r
r   r%   )Z	precisioné   é   zBID : %s
x : %s
y : %s
z : %s 
scalar : %s
vector : %s
tensor : %s )r   z5ID : %s
x : %s
y : %s
z : %s
scalar : %s
vector : %s z(ID : %s
x : %s
y : %s
z : %s
scalar : %szID : %s
x : %s
y : %s
z : %s )Úgetattrr   ÚgetÚappendrD   ÚnpZformat_float_scientificr4   rB   rA   rC   Ú	trait_setr<   ÚzerosÚrange)r9   r7   r6   r?   ÚvalueZx_coordZy_coordZz_coordrA   rB   Úir   r   r   rP   ®   sV    

ÿþ
ÿþÿ
ÿþÿÿÿÿÿzDefaultPickHandler._update_data)r,   r-   r.   r/   r   r   r<   r   r4   r   rA   rB   rC   r   rD   r   r   Údefault_viewrH   r:   rP   Ú__classcell__r   r   rJ   r   r;   i   s.   ÿÿÿ




û	!r;   c                       s  e Zd ZdZdZeddddœddd	�Zeeƒ e	e
ƒƒZed
ddƒZedd�Zeeeedd�edd�dd�eedd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d/d0„ Zd1d2„ Zd3d4„ Z d8d6d7„Z!‡  Z"S )9ÚPickeraØ  This module creates a 'Picker' that can interactively select a
    point and/or a cell in the data.  It also can use a world point
    picker (i.e. a generic point in space) and will probe for the data
    at that point.

    The Picker is usually called via a callback from the GUI
    interactor window.  After performing a pick on the VTK scene, a
    Picker object creates a `PickedData` object and passes it on to
    the pick_handler trait for further handling.
    r   rQ   rR   r%   )Úpoint_pickerZcell_pickerZworld_pickerr_   z specifies the picker type to use)Údefault_valuer!   ç        gš™™™™™¹?gš™™™™™™?z'whether to raise the picker GUI on pickr    Ú	pick_type)r?   Ú	toleranceT)Úshow_borderÚpick_handlerrE   r>   F)rd   Zshow_labelsÚOK)Z	resizableZbuttonsc                    sò   t t| ƒjf i |¤Ž t ¡ | _t ¡ | _t ¡ | _	t 
¡ | _t ¡ | _t ¡ | _t ¡ | _d| j_d| j_d| j_| jj}d|_d|_d|_t| j| jƒ | j| j_g d¢g| j_t ¡ | _t ¡ | _t |d�| _!t "¡ | j!_#|  $¡  d| _%d S )	NrQ   r   rR   g      ð?ra   )ra   ra   ra   )r7   F)&rG   r^   rH   r   ZPointPickerÚpointpickerZ
CellPickerÚ
cellpickerZWorldPointPickerÚworldpickerZPolyDataÚ
probe_dataZAxesÚp_sourceZPolyDataMapperZp_mapperZActorÚp_actorZ	symmetricZpickableÚ
visibilityÚpropertyÚ
line_widthZambientZdiffuser   ÚmapperÚpointsZTextRepresentationÚtext_repZ
TextWidgetÚtext_widgetr   r   r5   r6   Ú
text_setupÚwidgets)r9   r7   rI   ÚproprJ   r   r   rH     s0    









zPicker.__init__c                 C   s$   | j  ¡ }dD ]}| |d ¡ q|S )N)r7   Zuire   Z__sync_trait__Z__traits_listener__)Ú__dict__ÚcopyÚpop)r9   r#   r&   r   r   r   Ú__get_pure_state__,  s    
zPicker.__get_pure_state__c                 C   s
   t  | ¡S ©N)r   Údumps©r9   r   r   r   Ú__getstate__3  s    zPicker.__getstate__c                 C   s   t  | t  |¡¡ d S r{   )r   Z	set_stateZloads_state)r9   Z	str_stater   r   r   Ú__setstate__6  s    zPicker.__setstate__c                 C   sÄ   | j dkr|  ||¡| _n2| j dkr4|  ||¡| _n| j dkrL|  ||¡| _| jdu r^|  ¡  | jjr’d| j_	| j
 | j¡ | jj ¡ jdd� n|  ¡  | jj ¡ j}| jjj|ks¸n|  ¡  dS )a   Calls one of the current pickers and then passes the
        obtained data to the `self.pick_handler` object's
        `handle_pick` method.

        Parameters
        ----------

        - x : X position of the mouse in the window.

        - y : Y position of the mouse in the window.

        Note that the origin of x, y must be at the left bottom
        corner of the window.  Thus, for most GUI toolkits, y must
        be flipped appropriately such that y=0 is the bottom of the
        window.
        rQ   rR   r%   FÚleft)ZjustificationN)Z
pick_type_Ú
pick_pointr   Ú	pick_cellÚ
pick_worldru   Úsetup_widgetsrL   rs   Úenabledre   r:   r6   Ú_get_text_propertyrW   Úclose_pickerÚcolorr7   Ú
backgroundÚset_text_color)r9   r&   r'   Ú
text_colorr   r   r   Úpick@  s&    



ÿzPicker.pickc           	      C   s¢   | j  t|ƒt|ƒdf| jjj¡ | j }|j}t| jj| jjd�}|j	}||_
|dkrŠ|jjj}|jjj}d|_||_||_|  ||¡ nd| j_| jj ¡  |S )z: Picks the nearest point. Returns a `PickedData` instance.ra   ©r7   r6   r"   rQ   r   )rg   rŒ   Úfloatr   r7   Úrendererr1   r   r6   Úpick_positionr4   rp   r   Ú
point_dataÚboundsr0   Ú_update_actorrl   rm   Úrender)	r9   r&   r'   ÚppÚidÚpicked_dataÚcoordr   r’   r   r   r   r�   j  s(    ÿÿ

zPicker.pick_pointc           	      C   sÚ   z$| j  t|ƒt|ƒd| jjj¡ W n4 tyX   | j  t|ƒt|ƒdf| jjj¡ Y n0 | j }|j}t| jj| jj	d�}|j
}||_|dkrÂ|jjj}|jjj}d|_||_||_|  ||¡ nd| j_| jj ¡  |S )z9 Picks the nearest cell. Returns a `PickedData` instance.ra   r�   r"   rQ   r   )rh   rŒ   rŽ   r   r7   r�   Ú	TypeErrorr2   r   r6   r�   r4   rp   r   Z	cell_datar’   r0   r“   rl   rm   r”   )	r9   r&   r'   Úcpr–   r—   r˜   r   r’   r   r   r   r‚   …  s2    ÿÿ
ÿ

zPicker.pick_cellc           
   	   C   s@  | j  t|ƒt|ƒdf| jjj¡ z&| j t|ƒt|ƒdf| jjj¡ W n2 tyz   | j t|ƒt|ƒd| jjj¡ Y n0 | j }| j}|j}t	|ƒg| j
_t| jj| jjd�}||_|j�r(t|jjƒ}t ¡ }| |¡ | | j
¡ | ¡  |jj}|jjj}	d|_d|_d|_||_|  ||	¡ nd| j_| jj  ¡  |S )zX Picks a world point and probes for data there. Returns a
        `PickedData` instance.ra   r�   rQ   r   )!ri   rŒ   rŽ   r   r7   r�   rh   r™   r�   rM   rj   rq   r   r6   r4   rp   r   r   r   ZProbeFilterZset_source_dataZset_input_dataÚupdateÚoutputr‘   r’   r0   r3   r1   r“   rl   rm   r”   )
r9   r&   r'   Zwprš   r˜   r—   r   Zprober’   r   r   r   rƒ   ¥  sF    ÿÿÿ
ÿ

zPicker.pick_worldc                 C   s\   | j rXd| j_| jjj | j¡ d| jj_| jjj | jj¡ d| j_	d| _ | jj 
¡  dS )zVThis method makes the picker actor invisible when a non
        data point is selectedr   FN)ru   rl   rm   r   r7   r�   Zremove_actorr6   rs   r…   r”   r}   r   r   r   r‡   Ò  s    
zPicker.close_pickerc                 C   s`   d| j j ¡ _| j ¡ jdd� | j ¡ jdd� | jj| jd� | jj| j jd� d| j_	dS )	z&Sets the properties of the text widgetéd   )ç333333Ã?rž   r   )rZ   )ç333333Ó?gš™™™™™É?r   )Zrepresentation)r6   r   N)
r   r6   r†   Z	font_sizerr   Z_get_position_coordinaterW   Z_get_position2_coordinaters   Z
selectabler}   r   r   r   rt   á  s    zPicker.text_setupc                 C   sn   d|d |d   }d|d |d   }d|d |d   }t |||ƒ}|| j_|| j_d| j_d| jj_dS )	z4Updates the actor by setting its position and scale.rŸ   rQ   r   r%   rR   é   é   N)Úmaxrk   ÚoriginZscale_factorrl   rm   r   r6   )r9   r4   r’   ZdxZdyZdzZscaler   r   r   r“   ê  s    zPicker._update_actorc                 C   sV   | j j| jjjd� | jjj | j¡ | jjj | jj¡ d| j_	d| jj_	d| _
dS )z'Sets up the picker actor and text actor)Ú
interactorrQ   TN)rs   rW   r   r7   r¤   r�   Z	add_actorrl   r6   rm   ru   r}   r   r   r   r„   õ  s    
zPicker.setup_widgetsc                 C   s   || _ || _|  |¡ dS )z"Lets you set the picker propertiesN)rb   rc   rŠ   )r9   rb   rc   r‹   r   r   r   Úset_picker_propsÿ  s    zPicker.set_picker_propsNc                 C   sb   |d u rP| j jj}g d¢}tdƒD ]}t|| d ƒ||< q"t|ƒ| j j ¡ _n|| j j ¡ _d S )N)r   r   r   r%   rQ   )	r   r7   r‰   rY   ÚabsÚtupler6   r†   rˆ   )r9   rˆ   Zbgcolorr‹   r[   r   r   r   rŠ     s    
zPicker.set_text_color)N)#r,   r-   r.   r/   Ú__version__r   rb   r   r;   r   r8   re   r	   rc   r   Z
auto_raiser   r   r   r\   rH   rz   r~   r   rŒ   r�   r‚   rƒ   r‡   rt   r“   r„   r¥   rŠ   r]   r   r   rJ   r   r^   à   sH   þü

ÿÿþú
* -	
r^   )$r/   Z
traits.apir   r   r   r   r   r   r   r	   r
   r   r   Ztraitsui.apir   r   r   r   Ztvtk.apir   Ztvtk.tvtk_baser   r   Ztvtk.pyface.tvtk_scener   Ztvtk.commonr   Zapptools.persistencer   ÚnumpyrV   r   r   r8   r;   r^   r   r   r   r   Ú<module>   s   
4w