3
îÈ‘[‚B  ã               @   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 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
„ 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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ÚLongÚArrayÚAnyÚFloatÚInstanceÚRangeÚtrueÚStr)ÚViewÚGroupÚItemÚHandler)Útvtk)ÚTraitRevPrefixMapÚfalse_bool_trait)Úconfigure_input)Ústate_pickler)Úvtk_major_versionc             C   sB   |  }}x4|r<y|j }|r|}W q
 tk
r8   d}Y q
X q
W |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/venv/lib/python3.6/site-packages/tvtk/pyface/picker.pyÚget_last_input%   s    r   c               @   s\   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d
ddgd	dd�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Úzz"the coordinate of the picked point)ÚlabelsÚcolsr   Néÿÿÿÿr(   )r"   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   Úvalidr   Úpoint_idÚcell_idÚ
world_pickr   Ú
coordinater   r   r   r   r   r   r   5   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_pickT   s    zPickHandler.handle_pickN)r)   r*   r+   r,   r4   r   r   r   r   r2   O   s   r2   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 ID)r   r!   r"   z"the coordinate of the picked pointzthe scalar at picked pointzthe vector at picked pointzthe tensor at picked pointÚIDÚreadonly)ÚnameÚstyler1   ÚscalarÚvectorÚtensorÚhistoryÚcustomc                s(   t t| ƒjf |Ž g g g g g dœ| _d S )N)r6   r1   r:   r;   r<   )Úsuperr5   Ú__init__r   )r3   Útraits)Ú	__class__r   r   r@   ~   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œ}xX|j
ƒ D ]0}|| r’t| ||| | j ƒ qnt| |dƒ qnW nxdD ]}t| |dƒ qªW | jƒ  dS )z*Called when a pick event happens.
        r    )r:   r;   r<   Nr6   r1   r:   r;   r<   r(   r(   )r6   r1   r:   r;   r<   )Zvalid_r.   r6   r/   Úlistr1   r   ÚscalarsÚvectorsÚtensorsÚkeysÚsetattrÚ_update_data)r3   r   Z
array_datar8   r   r   r   r4   ‡   s(    



zDefaultPickHandler.handle_pickc             C   sJ   xDdD ]<}t | |ƒ}| jj|ƒjt | |ƒƒ |  jd||f 7  _qW d S )Nr6   r1   r:   r;   r<   z%s: %r
)r6   r1   r:   r;   r<   )Úgetattrr   ÚgetÚappendr=   )r3   r8   Úvaluer   r   r   rI   ¦   s    

zDefaultPickHandler._update_data)r"   )r"   )r"   r"   )r)   r*   r+   r,   r   r   r6   r   r1   r   r:   r;   r<   r   r=   r   r   Údefault_viewr@   r4   rI   Ú__classcell__r   r   )rB   r   r5   b   s&   



	r5   c               @   s   e Zd ZdZdd„ ZdS )ÚCloseHandlerz;This class cleans up after the UI for the Picker is closed.c             C   s   |j }|jƒ  dS )z3This method is invoked when the user closes the UI.T)ÚobjectÚon_ui_close)r3   ÚinfoÚis_okZpickerr   r   r   Úclose³   s    zCloseHandler.closeN)r)   r*   r+   r,   rU   r   r   r   r   rP   ±   s   rP   c            	       s   e Zd ZdZdZededdddœƒdd	�Zeeƒ e	e
ƒƒZed
ddƒZe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�eedd�edd�dd�ƒddgeƒ 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/d0„ Zd1d2„ Z d3d4„ Z!‡  Z"S )5Ú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   Úpointr    é   r"   )Zpoint_pickerZcell_pickerZworld_pickerz specifies the picker type to use)r   g        g      Ð?gš™™™™™™?z&whether to show the picker GUI on pickz'whether to raise the picker GUI on pickÚ	pick_type)r8   Ú	toleranceT)Úshow_borderÚpick_handlerr>   )r8   r9   F)r[   Úshow_labelsÚshow_guiÚ
auto_raiseÚOK)Ú	resizableÚbuttonsÚhandlerc                sÎ   t t| ƒjf |Ž || _tjƒ | _tjƒ | _tj	ƒ | _
tjƒ | _| j| jƒ tjƒ | _tjƒ | _tjƒ | _d| j_d| j_d| j_| jj}d|_d|_d|_t| j| jƒ | j| j_dddgg| j_d | _d S )Nr    r   rX   g      ð?g        ) r?   rV   r@   Úrenwinr   ZPointPickerÚpointpickerZ
CellPickerÚ
cellpickerZWorldPointPickerÚworldpickerÚPolyDataÚ
probe_dataÚ_tolerance_changedrZ   ZAxesÚp_sourceZPolyDataMapperZp_mapperZActorÚp_actorÚ	symmetricÚpickableÚ
visibilityÚpropertyÚ
line_widthZambientZdiffuser   ÚmapperÚpointsÚui)r3   rd   rA   Úprop)rB   r   r   r@   ð   s*    







zPicker.__init__c             C   s(   | j jƒ }xdD ]}|j|d ƒ qW |S )Nrd   rt   r\   Ú__sync_trait__Ú__traits_listener__)rd   rt   r\   rv   rw   )Ú__dict__ÚcopyÚpop)r3   r!   r#   r   r   r   Ú__get_pure_state__  s    

zPicker.__get_pure_state__c             C   s
   t j| ƒS )N)r   Údumps)r3   r   r   r   Ú__getstate__  s    zPicker.__getstate__c             C   s   t j| t j|ƒƒ d S )N)r   Ú	set_stateÚloads_state)r3   Z	str_stater   r   r   Ú__setstate__  s    zPicker.__setstate__c             C   sh   d}| j dkr| j||ƒ}n.| j dkr4| j||ƒ}n| j dkrJ| j||ƒ}| jj|ƒ | jrd| jƒ  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.
        Nr    rX   r"   )Z
pick_type_Ú
pick_pointÚ	pick_cellÚ
pick_worldr\   r4   r^   Ú
_setup_gui)r3   r#   r$   r   r   r   r   Úpick  s    


zPicker.pickc       	      C   s�   | j jt|ƒt|ƒdf| jjƒ | j }|j}tƒ }|j}||_|dkrz|j	j
j}|j	j
j}d|_||_||_| j||ƒ nd| j_| jjƒ  |S )z: Picks the nearest point. Returns a `PickedData` instance.g        r    r   r(   )re   r…   Úfloatrd   Úrendererr.   r   Úpick_positionr1   rr   r   Ú
point_dataÚboundsr-   r   Ú_update_actorrl   ro   Úrender)	r3   r#   r$   ÚppÚidÚpicked_dataÚcoordr   rŠ   r   r   r   r�   =  s      


zPicker.pick_pointc       	      C   sÈ   y"| j jt|ƒt|ƒd| jjƒ W n4 tk
rV   | j jt|ƒt|ƒdf| jjƒ Y nX | j }|j}tƒ }|j}||_	|dkr²|j
jj}|j
jj}d|_||_||_| j||ƒ nd| j_| jjƒ  |S )z9 Picks the nearest cell. Returns a `PickedData` instance.g        r    r   r(   )rf   r…   r†   rd   r‡   Ú	TypeErrorr/   r   rˆ   r1   rr   r   Z	cell_datarŠ   r-   r   r‹   rl   ro   rŒ   )	r3   r#   r$   ÚcprŽ   r�   r�   r   rŠ   r   r   r   r‚   V  s*    


zPicker.pick_cellc       
      C   sD  | j jt|ƒt|ƒdf| jjƒ y$| jjt|ƒt|ƒdf| jjƒ W n2 tk
rv   | jjt|ƒt|ƒd| jjƒ Y nX | j }| j}|j}t|ƒg| j	_
tƒ }||_|j�r.t|jjƒ}tjƒ }tdkrà|j|ƒ |j| j	ƒ n||_| j	|_|jƒ  |jj}|jjj}	d|_d|_d|_||_| j||	ƒ nd| j_ | jj!ƒ  |S )zX Picks a world point and probes for data there. Returns a
        `PickedData` instance.g        é   r    r   )"rg   r…   r†   rd   r‡   rf   r‘   rˆ   rC   ri   rs   r   r1   rr   r   r   r   ZProbeFilterr   Úset_source_dataÚset_input_dataÚsourceÚupdateÚoutputr‰   rŠ   r-   r0   r.   r   r‹   rl   ro   rŒ   )
r3   r#   r$   Zwpr’   r�   r�   r   ZproberŠ   r   r   r   rƒ   u  s<     $$


zPicker.pick_worldc             C   s"   d| j _| jjj| j ƒ d| _dS )zSThis method makes the picker actor invisible when the GUI
        dialog is closed.r   N)rl   ro   rd   r‡   Zremove_actorrt   )r3   r   r   r   rR   ¢  s    zPicker.on_ui_closec             C   s   || j _|| j_dS )z' Trait handler for the tolerance trait.N)re   rZ   rf   )r3   Úvalr   r   r   rj   ¬  s    zPicker._tolerance_changedc             C   sd   d|d |d   }d|d |d   }d|d |d   }t |||ƒ}|| j_|| j_d| j_dS )	z4Updates the actor by setting its position and scale.g333333Ó?r    r   r"   rX   é   é   N)Úmaxrk   ÚoriginÚscale_factorrl   ro   )r3   r1   rŠ   ÚdxÚdyZdzÚscaler   r   r   r‹   ±  s    zPicker._update_actorc             C   sV   | j dkr&| jƒ | _ | jjj| jƒ n,| jrRy| j jjƒ  W n t	k
rP   Y nX dS )zPops up the GUI control widget.N)
rt   Úedit_traitsrd   r‡   Z	add_actorrl   r_   ÚcontrolÚRaiser   )r3   r   r   r   r„   »  s    

zPicker._setup_gui)#r)   r*   r+   r,   Ú__version__r   r   rY   r5   r   r2   r\   r	   rZ   r
   r^   r_   r   r   r   rP   rN   r@   r{   r}   r€   r…   r�   r‚   rƒ   rR   rj   r‹   r„   rO   r   r   )rB   r   rV   ½   s>   





-

rV   N)!r,   Ú
traits.apir   r   r   r   r   r   r   r	   r
   r   Útraitsui.apir   r   r   r   Útvtk.apir   Útvtk.tvtk_baser   r   Ztvtk.commonr   Zapptools.persistencer   r   r   r   r2   r5   rP   rV   r   r   r   r   Ú<module>   s   0O