3
îÈ‘['1  ã               @   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 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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)ÚViewÚGroupÚItem)Útvtk)Ú	array2vtkÚget_vtk_array_type)Úis_old_pipeline)ÚVTK_MAJOR_VERSION)ÚSource)ÚPipelineInfoc             C   sf   |dkrdS t j|ƒ}t|jƒdks,tdƒ‚| j}|dk	rb|jdd… |jksbt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)r   r   éÿÿÿÿ)ÚnpÚasarrayÚlenÚshapeÚAssertionErrorÚvector_data)ÚobjÚnameÚvalueÚarrÚvd© r$   ú^/home/yuhw/wc/wire-cell-python/venv/lib/python3.6/site-packages/mayavi/sources/array_source.pyÚ_check_scalar_array   s    
r&   za 2D or 3D numpy arrayc             C   s|   |dkrdS t j|ƒ}t|jƒdks,tdƒ‚|jd	 dksBtdƒ‚| j}|dk	rx|jdd
… |jksxtd|j|jf ƒ‚|S )z.Validates a vector array passed to the object.Nr   é   z'Vector array must be 3 or 4 dimensionalr   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   )r   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dd�ZedƒZede	dd�Z
edƒZeddd	d
�Zedddd
�ZedƒZeejf dd�Zeejf d<d=dœd�Zeddd
�Zedgd�Zeedddgƒ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(d0d1„ Z)d2d3„ Z*d4d5„ Z+d6d7„ Z,d8d9„ Z-d:d;„ Z.‡  Z/S )AÚArraySourcezˆA simple source that allows one to view a suitably shaped numpy
    array as ImageData.  This supports both scalar and vector data.
    NF)Úrich_compareÚscalarÚ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 origin)Ú
allow_noneç      ð?ç        )r0   r2   )ÚargsÚkwTzJif input array should be transposed (if True VTK will copy the input data)Ú
image_data)Údatasetsr   r   r   Útranspose_input_array)r    Úscalar_nameÚvector_nameÚspacingÚorigin)Úshow_labelsc                sr   |j dd ƒ}|j dd ƒ}tt| ƒjf |Ž | j| j| jƒ |d k	rH|| _|d k	rV|| _| jg| _	| j
| jdƒ d S )Nr(   r   zspacing,origin)ÚpopÚsuperr+   Ú__init__Úconfigure_input_datar/   r8   r(   r   ÚoutputsÚon_trait_changeÚ_information_changed)ÚselfÚtraitsr)   r#   )Ú	__class__r$   r%   rB   Š   s    
zArraySource.__init__c                s   t t| ƒjƒ }|jdd ƒ |S )Nr8   )rA   r+   Ú__get_pure_state__r@   )rG   Úd)rI   r$   r%   rJ   œ   s    zArraySource.__get_pure_state__c             C   sP   | j }|jƒ  |j}| jdk	r(|jjƒ  | jdk	r<|jjƒ  | jjƒ  d| _	dS )zCCall this function when you change the array data
        in-place.NT)
r8   ÚmodifiedÚ
point_datar(   Úscalarsr   Úvectorsr/   ÚupdateÚdata_changed)rG   rK   Úpdr$   r$   r%   rP   ¤   s    




zArraySource.updateÚpointc             C   sv   t j|ƒ}t|jƒdks tdƒ‚t| jd| ƒ}t|jƒdkrT|jd d
ksTtdƒ‚tjt	|ƒƒ}||_
|j|ƒ 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.z%s_datar   r   r'   é	   z6Only N x m arrays where (m in [1,3,4,9]) are supportedN)r   r   r'   rT   )r   r   r   r   r   Úgetattrr8   r   Zto_tvtkr   r    Z	add_array)rG   Úarrayr    ÚcategoryÚdataÚvar$   r$   r%   Úadd_attribute±   s    
zArraySource.add_attributec             C   s   t | jd| ƒ}|j|ƒ dS )ztRemove an attribute by its name and optional category (point and
        cell).  Returns the removed array.
        z%s_dataN)rU   r8   Zremove_array)rG   r    rW   rX   r$   r$   r%   Úremove_attributeÏ   s    zArraySource.remove_attributec             C   s$   t | jd| ƒ}|j|ƒ}||_dS )z?Rename a particular attribute from `name1` to `name2`.
        z%s_dataN)rU   r8   Z	get_arrayr    )rG   Zname1Zname2rW   rX   r"   r$   r$   r%   Úrename_attributeÖ   s    
zArraySource.rename_attributec             C   s   | j | j|ƒ d S )N)rC   r/   )rG   r!   r$   r$   r%   Ú_image_data_changedá   s    zArraySource._image_data_changedc       
      C   s–  | j }|d kr d |j_d| _d S t|jƒ}t|ƒdkr@|jdƒ | j\}}}t	| j
ƒ|_
t	|ƒ|_d|| d d|| d d|| d f|_tdkrútƒ rÆd|| d d|| d d|| d f|_n4d|| d d|| d d|| d g}| jj|ƒ | j�rtjtj|ƒƒ|j_ntj|ƒ|j_| j|jj_|j}tƒ �rTt|ƒ|_|jƒ  n&| jjdƒ}	|j|	t|ƒdƒ |jƒ  |jƒ  | jjƒ  d| _d S )NTr   r   r   é   r   ) r8   rM   rN   rQ   Úlistr   r   ÚappendÚdimensions_orderÚtupler>   Ú
dimensionsÚextentr   r   Úupdate_extentr/   Úset_update_extentr:   r   ÚravelÚ	transposer;   r    Údtyper   Zscalar_typerP   Úget_output_informationZ!set_point_data_active_scalar_inforL   Úupdate_traits)
rG   rX   Úimg_dataÚdimsZdim0Zdim1Zdim2re   ÚtypecodeZfilter_out_infor$   r$   r%   Ú_scalar_data_changedä   s@    


*,(


z ArraySource._scalar_data_changedc             C   sŽ  | j }|d kr d |j_d| _d S t|jƒ}t|ƒdkrN|jddƒ tj	||ƒ}t
| jƒ|_t
|d d… ƒ|_d|d d d|d d d|d d f|_tdk�rtƒ rÒd|d d d|d d d|d d f|_n>| jjƒ  d|d d d|d d d|d d g}| jj|ƒ tj|ƒ}| j�r0tj|dƒ}n|}tj	||d dfƒ|j_| j|jj_tƒ �rj|jƒ  n|jƒ  |jƒ  | jjƒ  d| _d S )	NTr   r   r   r   r^   r   )r   r   r   r   )r8   rM   rO   rQ   r_   r   r   Úinsertr   Úreshaperb   r>   rc   rd   r   r   re   r/   Zupdate_informationrf   Úsizer:   rh   r<   r    rP   rL   rk   )rG   rX   rl   rm   re   ÚszZdata_tr$   r$   r%   Ú_vector_data_changed  s<    
*
,
(


z ArraySource._vector_data_changedc             C   s    | j d k	r|| jjj_d| _d S )NT)r(   r8   rM   rN   r    rQ   )rG   r!   r$   r$   r%   Ú_scalar_name_changed5  s    
z ArraySource._scalar_name_changedc             C   s    | j d k	r|| jjj_d| _d S )NT)r   r8   rM   rO   r    rQ   )rG   r!   r$   r$   r%   Ú_vector_name_changed:  s    
z ArraySource._vector_name_changedc             C   s0   | j d k	r| j| j ƒ | jd k	r,| j| jƒ d S )N)r(   ro   r   rt   )rG   r!   r$   r$   r%   Ú_transpose_input_array_changed?  s    

z*ArraySource._transpose_input_array_changedc             C   s   | j jƒ  d| _d S )NT)r/   rP   rQ   )rG   r$   r$   r%   rF   E  s    
z ArraySource._information_changed)r4   r4   r4   )r5   r5   r5   )rS   )rS   )rS   )0Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r&   r(   r   r;   r*   r   r<   r   r=   r>   r   Zupdate_image_datar   r   Z	ImageDatar8   ZImageChangeInformationr/   r   r:   r   Úoutput_infor	   r
   ra   r   r   r   ÚviewrB   rJ   rP   rZ   r[   r\   r]   ro   rt   ru   rv   rw   rF   Ú__classcell__r$   r$   )rI   r%   r+   C   sL   	



+&r+   )#r{   Únumpyr   Úvtk.utilr   Ú
traits.apir   r   r   r   r   r   r	   r
   Útraitsui.apir   r   r   Útvtk.apir   Ztvtk.array_handlerr   r   Ztvtk.commonr   Ztvtk.vtk_moduler   Zmayavi.core.sourcer   Úmayavi.core.pipeline_infor   r&   Úinfor*   r+   r$   r$   r$   r%   Ú<module>   s   (