a
    «ˆcGA  ã                   @   s   d Z ddlZddlZddlZddlZddlmZmZm	Z	m
Z
mZ ddlmZmZmZ ddlmZmZmZ ddlmZ ddlmZ ddlmZ dd	lmZ dd
lmZ ddl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„ Z(G dd„ deƒZ)dS )z™This source manages a VTK dataset given to it.  When this source is
pickled or persisted, it saves the data given to it in the form of a
gzipped string.
é    N)ÚInstanceÚListÚStrÚBoolÚInt)ÚViewÚGroupÚItem)Úgzip_stringÚgunzip_stringÚ	set_state)Útvtk)Ú	messenger)Ú	array2vtk)Úconfigure_input_data)ÚSource)Úhandle_children_state)ÚDEnum)ÚPipelineInfoÚget_tvtk_dataset_nameé   )Úhas_attributes)Úget_all_attributesc                 C   s’   t jdd�}|j}t|| ƒ d|_| ¡  |jdkr‚d|_t d¡\}}t	 
|¡ t	 |¡ ||_| ¡  t|ƒ ¡ }t	 |¡ n|j}||_|S )zdGiven a dataset, convert the dataset to an ASCII string that can
    be stored for persistence.
    r   )Úwrite_to_output_stringr   z.vtk)r   ZDataSetWriterÚglobal_warning_displayr   ÚupdateZoutput_string_lengthr   ÚtempfileÚmkstempÚosÚcloseÚremoveÚ	file_nameÚwriteÚopenÚreadZoutput_string)ÚdataÚwÚwarnÚfhÚfnameÚsdata© r+   úa/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/mayavi/sources/vtk_data_source.pyÚwrite_dataset_to_string&   s     

r-   c                       s¼  e Zd ZdZdZeejdd�Ze	dgdgdgd�Z
eddd	�Zed
dd	�Zeddd	�Zeddd	�Zeddd	�Zeddd	�Zeeedd�edd�edd�edd�edd�edd�edd�ƒƒZeeƒZeeƒZeeƒZeeƒZeeƒZeeƒZeejddd�Ze dƒZ!e"dƒZ#‡ fd d!„Z$d"d#„ Z%‡ fd$d%„Z&d&d'„ Z'dGd)d*„Z(dHd+d,„Z)dId-d.„Z*d/d0„ Z+d1d2„ Z,d3d4„ Z-d5d6„ Z.d7d8„ Z/d9d:„ Z0d;d<„ Z1d=d>„ Z2d?d@„ Z3dAdB„ Z4dCdD„ Z5dJdEdF„Z6‡  Z7S )KÚVTKDataSourcea�  This source manages a VTK dataset given to it.  When this
    source is pickled or persisted, it saves the data given to it in
    the form of a gzipped string.

    Note that if the VTK dataset has changed internally and you need
    to notify the mayavi pipeline to flush the data just call the
    `modified` method of the VTK dataset and the mayavi pipeline will
    update automatically.
    r   F)Ú
allow_noneÚany)ÚdatasetsZattribute_typesÚ
attributesÚ_point_scalars_listz"scalar point data attribute to use)Zvalues_nameÚdescÚ_point_vectors_listz#vectors point data attribute to useÚ_point_tensors_listz"tensor point data attribute to useÚ_cell_scalars_listz!scalar cell data attribute to useÚ_cell_vectors_listz"vectors cell data attribute to useÚ_cell_tensors_listz!tensor cell data attribute to useÚpoint_scalars_name)ÚnameÚpoint_vectors_nameÚpoint_tensors_nameÚcell_scalars_nameÚcell_vectors_nameÚcell_tensors_namer%   r+   )Úargsr/   Téÿÿÿÿc                    s†   t t| ƒ ¡ }dD ]}| |d ¡ qdD ],}| d| d d ¡ | d| d d ¡ q(| j}|d ur‚t|ƒ}t| d¡ƒ}||d< |S )N)Ú_assign_attributeÚ_firstZ	_observer)Zpoint_scalarsZpoint_vectorsZpoint_tensorsZcell_scalarsZcell_vectorsZcell_tensorsÚ_Z_listÚ_nameÚasciir%   )Úsuperr.   Ú__get_pure_state__Úpopr%   r-   r
   Úencode)ÚselfÚdr;   r%   r*   Úz©Ú	__class__r+   r,   rI   ›   s    z VTKDataSource.__get_pure_state__c                 C   s„   |j }|d urLt|ƒ d¡}tjd|d�}|j}d|_| ¡  ||_|j| _ t| |ddgd� t	| j
|j
ƒ t| |dgdgd	� d S )
NrG   r   )Zread_from_input_stringZinput_stringr   Úchildrenr%   )ÚignoreÚ*)ÚfirstrR   )r%   r   Údecoder   ZDataSetReaderr   r   Úoutputr   r   rQ   )rL   ÚstaterN   rM   Úrr'   r+   r+   r,   Ú__set_pure_state__«   s    ÿz VTKDataSource.__set_pure_state__c                    s$   | j r
dS |  ¡  tt| ƒ ¡  dS )zRThis is invoked when this object is added to the mayavi
        pipeline.
        N)ZrunningÚ_update_datarH   r.   Ústart©rL   rO   r+   r,   r[   À   s    zVTKDataSource.startc                 C   s"   | j  ¡  t| j ƒr| j ¡  dS )z§Invoke this to flush data changes downstream.  This is
        typically used when you change the data object and want the
        mayavi pipeline to refresh.
        N)r%   Úmodifiedr   rC   r   r\   r+   r+   r,   r   Ï   s    

zVTKDataSource.updateÚpointc           	      C   sæ   t  |¡}t|jƒdks J dƒ‚t| jd| ƒ}t|jƒdkr˜|jd dv sTJ dƒ‚t t|ƒ¡}||_	| 
|¡ dddd	dœ}d
|||jd  f }n&t t|ƒ¡}||_	| 
|¡ d| }tt| |ƒ|g ƒ}t| |t|ƒƒ dS )aÜ  Add an attribute to the dataset to specified category ('point' or
        'cell').

        One may add a scalar, vector (3/4 components) or a tensor (9 components).

        Note that it is the user's responsibility to set the correct size of
        the arrays.

        Parameters
        ----------

        array: numpy array/list : array data to add.

        name: str: name of the array.

        category: 'point'/'cell': the category of the attribute data.

        é   zOnly 2D arrays can be added.ú%s_datar   )r   é   é   é	   z4Only Nxm arrays where (m in [1,3,4,9]) are supportedÚscalarsÚvectorsÚtensorsú_%s_%s_listz_%s_scalars_listN)ÚnpZasarrayÚlenÚshapeÚgetattrr%   r   Zto_tvtkr   r;   Z	add_arrayÚsetÚsetattrÚsorted)	rL   Úarrayr;   Úcategoryr%   ÚvaÚmappingÚ	attributeÚnamesr+   r+   r,   Úadd_attributeÚ   s(    
ÿ
ÿ
zVTKDataSource.add_attributec                 C   sB   |   ||¡}t| jd| ƒ}| |¡ t| d||f ƒ}| |¡S )ztRemove an attribute by its name and optional category (point and
        cell).  Returns the removed array.
        r`   rg   )Ú_find_array_listrk   r%   Zremove_arrayr    )rL   r;   rp   Útyper%   Ú	attr_listr+   r+   r,   Úremove_attribute  s
    
zVTKDataSource.remove_attributec           	      C   sj   |   ||¡}t| jd| ƒ}| |¡}||_d||f }t| |ƒ}| |¡ | |¡ t| |t|ƒƒ dS )z?Rename a particular attribute from `name1` to `name2`.
        r`   rg   N)	rv   rk   r%   Z	get_arrayr;   r    Úappendrm   rn   )	rL   Zname1Zname2rp   rw   r%   Zarrrs   rx   r+   r+   r,   Úrename_attribute  s    



zVTKDataSource.rename_attributec                 C   s¸   t | jƒr6| j}|  ||¡ |  ¡  | ¡  |g| _nt ¡ }| 	| j¡ |g| _d| _
t| jd ƒg| j_|d ur€| | j¡ | dtj¡| _t |¡}t |d| j¡ |  ¡ | _d S )NTr   ZModifiedEvent)r   r%   rC   r   rZ   r   Úoutputsr   ZTrivialProducerZ
set_outputÚdata_changedr   Úoutput_infor1   Zremove_observerÚ_observer_idZadd_observerr   ÚsendZto_vtkÚconnectÚ_fire_data_changedÚ	_get_namer;   )rL   ÚoldÚnewÚaaÚtpZnew_vtkr+   r+   r,   Ú_data_changed  s*    

ÿ
ÿzVTKDataSource._data_changedc                 G   s
   d| _ dS )z%Simply fire the `data_changed` event.TN)r}   )rL   rA   r+   r+   r,   r‚   9  s    z VTKDataSource._fire_data_changedc                 C   sÞ   |d u rd S | j }t|ƒdkrLt||d ƒ}t|d| ƒ}|d ƒ d| _d S | j}d }|dkrf|j}n|dkrt|j}t|d| ƒ}||ƒ | || ¡ | ¡ d ¡ |dkrÌ| 	d	¡rÌt||d ƒj
}	|	j}
| ¡  d| _d S )
Nr   Ú_dataúset_active_%sTr^   ÚcellÚ_DATArd   ÚvtkImageData)r%   ri   rk   r}   rC   Ú
point_dataZ	cell_dataÚassignÚupperÚis_ard   Úranger   )rL   Z	data_typeZ	attr_typeÚvalueZdatasetrM   Úmethodr†   r%   ÚsrX   r+   r+   r,   Ú_set_data_name=  s.    zVTKDataSource._set_data_namec                 C   s   |   dd|¡ d S )Nrd   r^   ©r–   ©rL   r“   r+   r+   r,   Ú_point_scalars_name_changed\  s    z)VTKDataSource._point_scalars_name_changedc                 C   s   |   dd|¡ d S )Nre   r^   r—   r˜   r+   r+   r,   Ú_point_vectors_name_changed_  s    z)VTKDataSource._point_vectors_name_changedc                 C   s   |   dd|¡ d S )Nrf   r^   r—   r˜   r+   r+   r,   Ú_point_tensors_name_changedb  s    z)VTKDataSource._point_tensors_name_changedc                 C   s   |   dd|¡ d S )Nrd   r‹   r—   r˜   r+   r+   r,   Ú_cell_scalars_name_changede  s    z(VTKDataSource._cell_scalars_name_changedc                 C   s   |   dd|¡ d S )Nre   r‹   r—   r˜   r+   r+   r,   Ú_cell_vectors_name_changedh  s    z(VTKDataSource._cell_vectors_name_changedc                 C   s   |   dd|¡ d S )Nrf   r‹   r—   r˜   r+   r+   r,   Ú_cell_tensors_name_changedk  s    z(VTKDataSource._cell_tensors_name_changedc                 C   sz   | j d u rd S t| j ƒ\}}| j j}|j}| j  d¡rD|d urD|j}dd„ }|| |dƒ || |dƒ | jrpd| _d| _d S )Nr�   c           
      S   sæ   g d¢}| j }t| jd| ƒ}|D ]¾}t|| ƒ}| d¡ t| d||f |ƒ t|ƒdkr"t| d||f ƒ}| jrˆt|ƒdkrˆ|d }t|d| ƒ|ƒ | || 	¡ | 	¡ d	 ¡ | 
¡  d||f |d
di}	| jf i |	¤Ž q"dS )zÅGiven the object, the dict of the attributes from the
            `get_all_attributes` function and the data type
            (point/cell) data this will setup the object and the data.
            ©rd   re   rf   r`   Ú rg   r   z
%s_%s_namer   rŠ   rŒ   Ztrait_change_notifyFN)rC   rk   r%   rn   rz   rm   ri   rD   r�   r�   r   Z	trait_set)
Úobjr2   Zd_typeÚattrsr†   r%   ÚattrÚvaluesÚdefaultÚkwr+   r+   r,   Ú_setup_data_traits{  s(    

ÿÿz6VTKDataSource._update_data.<locals>._setup_data_traitsr^   r‹   FT)r%   r   rŽ   rd   r‘   r’   rD   r}   )rL   Zpnt_attrZ	cell_attrÚpdrd   rX   r§   r+   r+   r,   rZ   n  s    
zVTKDataSource._update_datac                 C   s6   d}| j dur | j jj}d| }d| jv r2|d7 }|S )z/ Gets the name to display on the tree.
        zVTK Data (uninitialized)NzVTK Data (%s)z[Hidden]z	 [Hidden])r%   rP   Ú__name__r;   )rL   ÚretÚtypr+   r+   r,   rƒ   š  s    


zVTKDataSource._get_namec                 C   sL   g d¢}|D ]*}d||f }t | |ƒ}||v r|  S qtd||f ƒ‚dS )zrReturn information on which kind of attribute contains the
        specified named array in a particular category.rŸ   rg   z)No %s array named %s available in datasetN)rk   ÚKeyError)rL   r;   rp   Útypesrw   r£   rt   r+   r+   r,   rv   ¥  s    

ÿzVTKDataSource._find_array_list)r^   )r^   )r^   )r^   )8r©   Ú
__module__Ú__qualname__Ú__doc__Ú__version__r   r   ZDataSetr%   r   r~   r   r:   r<   r=   r>   r?   r@   r   r   r	   Úviewr   r   r3   r5   r6   r7   r8   r9   ZAssignAttributerC   r   rD   r   r   rI   rY   r[   r   ru   ry   r{   rˆ   r‚   r–   r™   rš   r›   rœ   r�   rž   rZ   rƒ   rv   Ú__classcell__r+   r+   rO   r,   r.   D   s~   þ	ÿÿÿÿÿÿúÿ
'


,r.   )*r°   Úsysr   r   Únumpyrh   Z
traits.apir   r   r   r   r   Ztraitsui.apir   r   r	   Z"apptools.persistence.state_picklerr
   r   r   Ztvtk.apir   r   Ztvtk.array_handlerr   Ztvtk.commonr   Zmayavi.core.sourcer   Zmayavi.core.commonr   Zmayavi.core.trait_defsr   Zmayavi.core.pipeline_infor   r   Úutilsr   Zvtk_xml_file_readerr   r-   r.   r+   r+   r+   r,   Ú<module>   s&   