a
    «ˆc"/  ã                   @   sÈ   d Z ddlZddlmZ ddl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 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de_dd„ Z de _G dd„ deƒZ!dS )z€A simple source that allows one to view a suitably shaped numpy
array as ImageData.  This supports both scalar and vector data.
é    N)ÚvtkConstants)	ÚInstanceÚTraitÚStrÚBoolÚButtonÚDelegatesToÚListÚIntÚOBJECT_IDENTITY_COMPARE)ÚViewÚGroupÚItem)Útvtk)Ú	array2vtkÚget_vtk_array_type)ÚVTK_MAJOR_VERSION)ÚSource)ÚPipelineInfoc                 C   sf   |du rdS t  |¡}t|jƒdv s,J dƒ‚| j}|durb|jdd… |jksbJ d|j|jf ƒ‚|S )z.Validates a scalar array passed to the object.N)é   é   z'Scalar array must be 2 or 3 dimensionaléÿÿÿÿz_Scalar array must match already set vector data.
vector_data.shape = %s, given array shape = %s)ÚnpÚasarrayÚlenÚshapeÚvector_data)ÚobjÚnameÚvalueÚarrÚvd© r"   ú^/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/mayavi/sources/array_source.pyÚ_check_scalar_array   s    
ÿÿÿr$   za 2D or 3D numpy arrayc                 C   s|   |du rdS t  |¡}t|jƒdv s,J dƒ‚|jd dksBJ dƒ‚| j}|durx|jdd… |jksxJ d|j|jf ƒ‚|S )z.Validates a vector array passed to the object.N)r   é   z'Vector array must be 3 or 4 dimensionalr   r   zAThe vectors must be three dimensional with `array.shape[-1] == 3`z_Vector array must match already set scalar data.
scalar_data.shape = %s, given array shape = %s)r   r   r   r   Úscalar_data)r   r   r   r    Úsdr"   r"   r#   Ú_check_vector_array+   s"    
ÿÿÿÿr(   z)a 3D or 4D numpy array with shape[-1] = 3c                	       s\  e Zd ZdZedeed�ZedƒZ	ede
ed�ZedƒZedddd	�Zedd
dd	�ZedƒZeejddd�Zeejddddœd�Zeddd	�Zedgd�Zeeg d¢ƒZee e!dd�e!dd�e!dd�e!dd�e!dd�dd�ƒZ"‡ fd d!„Z#‡ fd"d#„Z$d$d%„ Z%d;d'd(„Z&d<d)d*„Z'd=d+d,„Z(d-d.„ Z)d/d0„ Z*d1d2„ Z+d3d4„ Z,d5d6„ Z-d7d8„ Z.d9d:„ Z/‡  Z0S )>ÚArraySourcezˆA simple source that allows one to view a suitably shaped numpy
    array as ImageData.  This supports both scalar and vector data.
    N)Zcomparison_modeZscalarZvectorÚchange_information_filterÚoutput_spacingz#the spacing between points in array)ÚdescÚoutput_originz!the origin of the points in arrayzUpdate spacing and originr"   F)Ú
allow_none)ç      ð?r/   r/   )ç        r0   r0   )r+   r-   )ÚargsÚkwTzJif input array should be transposed (if True VTK will copy the input data)Ú
image_data)Zdatasets)r   é   r   Útranspose_input_array)r   Úscalar_nameÚvector_nameÚspacingÚorigin)Zshow_labelsc                    sv   |  dd ¡}|  dd ¡}tt| ƒjf i |¤Ž |  | j| j¡ |d urL|| _|d urZ|| _| jg| _	|  
| jd¡ d S )Nr&   r   zspacing,origin)ÚpopÚsuperr)   Ú__init__Úconfigure_input_datar*   r3   r&   r   ÚoutputsZon_trait_changeÚ_information_changed)ÚselfZtraitsr'   r!   ©Ú	__class__r"   r#   r<   ’   s    ÿ
zArraySource.__init__c                    s   t t| ƒ ¡ }| dd ¡ |S )Nr3   )r;   r)   Ú__get_pure_state__r:   )r@   ÚdrA   r"   r#   rC   ¤   s    zArraySource.__get_pure_state__c                 C   sP   | j }| ¡  |j}| jdur(|j ¡  | jdur<|j ¡  | j ¡  d| _	dS )zCCall this function when you change the array data
        in-place.NT)
r3   ÚmodifiedÚ
point_datar&   Úscalarsr   Úvectorsr*   ÚupdateÚdata_changed)r@   rD   Úpdr"   r"   r#   rI   ¬   s    




zArraySource.updateÚpointc                 C   sv   t  |¡}t|jƒdks J dƒ‚t| jd| ƒ}t|jƒdkrT|jd dv sTJ dƒ‚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. Also no automatic transposing of the data is done.

        Parameters
        ----------

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

        name: str: name of the array.

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

        r   zOnly 2D arrays can be added.ú%s_datar4   )r4   r   r%   é	   z6Only N x m arrays where (m in [1,3,4,9]) are supportedN)r   r   r   r   Úgetattrr3   r   Zto_tvtkr   r   Z	add_array)r@   Úarrayr   ÚcategoryÚdataÚvar"   r"   r#   Úadd_attribute¹   s    
ÿzArraySource.add_attributec                 C   s   t | jd| ƒ}| |¡ dS )ztRemove an attribute by its name and optional category (point and
        cell).  Returns the removed array.
        rM   N)rO   r3   Zremove_array)r@   r   rQ   rR   r"   r"   r#   Úremove_attribute×   s    zArraySource.remove_attributec                 C   s$   t | jd| ƒ}| |¡}||_dS )z?Rename a particular attribute from `name1` to `name2`.
        rM   N)rO   r3   Z	get_arrayr   )r@   Zname1Zname2rQ   rR   r    r"   r"   r#   Úrename_attributeÞ   s    
zArraySource.rename_attributec                 C   s   |   | j|¡ d S ©N)r=   r*   ©r@   r   r"   r"   r#   Ú_image_data_changedé   s    zArraySource._image_data_changedc           
      C   sF  | j }|d u r d |j_d| _d S t|jƒ}t|ƒdkr@| d¡ | j\}}}t	| j
ƒ|_
t	|ƒ|_d|| d d|| d d|| d f|_tdkrÈd|| d d|| d d|| d g}| j |¡ | jrät t |¡¡|j_nt |¡|j_| j|jj_|j}| j d¡}	| |	t|ƒd¡ | ¡  | ¡  | j ¡  d| _d S )NTr   r4   r   é   r   )r3   rF   rG   rJ   Úlistr   r   ÚappendÚdimensions_orderÚtupler9   Ú
dimensionsÚextentr   r*   Úset_update_extentr5   r   ZravelÚ	transposer6   r   ZdtypeZget_output_informationZ!set_point_data_active_scalar_infor   rE   Úupdate_traitsrI   )
r@   rR   Úimg_dataÚdimsZdim0Zdim1Zdim2Úupdate_extentÚtypecodeZfilter_out_infor"   r"   r#   Ú_scalar_data_changedì   s8    


*(ÿ
z ArraySource._scalar_data_changedc                 C   sF  | j }|d u r d |j_d| _d S t|jƒ}t|ƒdkrN| dd¡ t 	||¡}t
| jƒ|_t
|d d… ƒ|_d|d d d|d d d|d d f|_tdkrÜ| j ¡  d|d d d|d d d|d d g}| j |¡ t |¡}| jrút |d¡}n|}t 	||d df¡|j_| j|jj_| ¡  | ¡  | j ¡  d| _d S )	NTr   r   r4   r   r   rZ   )r   r4   r   r   )r3   rF   rH   rJ   r[   r   r   Úinsertr   Zreshaper^   r9   r_   r`   r   r*   Zupdate_informationra   Úsizer5   rb   r7   r   rE   rc   rI   )r@   rR   rd   re   rf   ÚszZdata_tr"   r"   r#   Ú_vector_data_changed  s4    
*
(

z ArraySource._vector_data_changedc                 C   s    | j d ur|| jjj_d| _d S ©NT)r&   r3   rF   rG   r   rJ   rX   r"   r"   r#   Ú_scalar_name_changed0  s    
z ArraySource._scalar_name_changedc                 C   s    | j d ur|| jjj_d| _d S rm   )r   r3   rF   rH   r   rJ   rX   r"   r"   r#   Ú_vector_name_changed5  s    
z ArraySource._vector_name_changedc                 C   s0   | j d ur|  | j ¡ | jd ur,|  | j¡ d S rW   )r&   rh   r   rl   rX   r"   r"   r#   Ú_transpose_input_array_changed:  s    

z*ArraySource._transpose_input_array_changedc                 C   s   | j  ¡  d| _d S rm   )r*   rI   rJ   )r@   r"   r"   r#   r?   @  s    
z ArraySource._information_changed)rL   )rL   )rL   )1Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r$   r   r&   r   r6   r(   r   r7   r   r8   r9   r   Zupdate_image_datar   r   Z	ImageDatar3   ZImageChangeInformationr*   r   r5   r   Zoutput_infor	   r
   r]   r   r   r   Úviewr<   rC   rI   rT   rU   rV   rY   rh   rl   rn   ro   rp   r?   Ú__classcell__r"   r"   rA   r#   r)   B   sh   ýýÿÿÿÿþû


$ r)   )"rt   Únumpyr   Zvtk.utilr   Z
traits.apir   r   r   r   r   r   r	   r
   r   Ztraitsui.apir   r   r   Ztvtk.apir   Ztvtk.array_handlerr   r   Ztvtk.vtk_moduler   Zmayavi.core.sourcer   Zmayavi.core.pipeline_infor   r$   Úinfor(   r)   r"   r"   r"   r#   Ú<module>   s   ,