a
    «ˆcÄÅ  ã                   @   s˜  d Z ddlZddlmZ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mZ d	d
lmZ d	dlmZmZ g 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G dd„ deƒZG dd„ deƒZG dd„ deƒ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+d5d6„ Z,d=d7d8„Z-d9d:„ Z.d;d<„ Z/e/e0ƒ ƒ dS )>z?
Data sources classes and their associated functions for mlab.
é    N)ÚBoolÚ	HasTraitsÚInstanceÚon_trait_change)Útvtk)Úcamel2enthought)ÚArraySource)Úregistry)ÚArrayNumberOrNoneÚArrayOrNoneé   )Útools)Úget_null_engineÚengine_manager)
Úvector_scatterÚvector_fieldÚscalar_scatterÚscalar_fieldÚline_sourceÚarray2d_sourceÚgrid_sourceÚopenÚtriangular_mesh_sourceÚvertical_vectors_sourcec                       sT   e Zd ZdZeejƒZeeƒZ	e
dƒZdd„ Zdd„ Zd‡ fdd	„	Zd
d„ Z‡  ZS )Ú
MlabSourcez±
    This class represents the base class for all mlab sources.  These
    classes allow a user to easily update the data without having to
    recreate the whole pipeline.
    Fc                 K   s
   t ƒ ‚dS )zûFunction to create the data from input arrays etc.

        This is to be used when the size of the arrays change or the
        first time when the data is created.  This regenerates the data
        structures and will be slower in general.
        N)ÚNotImplementedError)ÚselfÚtraits© r   úW/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/mayavi/tools/sources.pyÚreset6   s    zMlabSource.resetc                 C   sl   | j sh| j ¡  | j}|durht|ddƒ}t| ddƒ}t| ddƒ}|durb|dusZ|durb| ¡  d|_dS )zyUpdate the visualization.

        This is to be called after the data of the visualization has
        changed.
        NZ_assign_attributeÚvectorsÚscalarsT)Ú_disable_updateÚdatasetÚmodifiedÚm_dataÚgetattrÚupdateZdata_changed)r   ZmdZaar!   r"   r   r   r   r(   ?   s    
ÿzMlabSource.updateTc                    sB   z(d| _ tt| ƒj|fi |¤Ž W d| _ nd| _ 0 |r>|  ¡  | S )a,  Shortcut for setting object trait attributes.

        This is an overridden method that will make changing multiple
        traits easier.  This method is to be called when the arrays have
        changed content but not in shape/size.  In that case one must
        call the `reset` method.

        Parameters
        ----------
        trait_change_notify : Boolean
            If **True** (the default), then each value assigned may generate a
            trait change notification. If **False**, then no trait change
            notifications will be generated. (see also: trait_setq)
        traits : list of key/value pairs
            Trait attributes and their values to be set

        Returns
        -------
        self
            The method returns this object, after setting attributes.
        TF)r#   Úsuperr   Ú	trait_setr(   )r   Útrait_change_notifyr   ©Ú	__class__r   r   ÚsetQ   s    zMlabSource.setc                 C   s$   t |dƒs| dttƒ¡ | |_d S )NÚmlab_source)ÚhasattrZ	add_traitr   r   r/   )r   Údsr   r   r   Ú_m_data_changeds   s    
zMlabSource._m_data_changed)T)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   ZDataSetr$   r   r&   r   r#   r    r(   r.   r2   Ú__classcell__r   r   r,   r   r       s   
	"r   c                   @   s„   e Zd ZdZeZeZeZeZ	eZ
eZeZeZe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d„ ZdS )ÚMGlyphSourcezŠ
    This class represents a glyph data source for Mlab objects and
    allows the user to set the x, y, z, scalar/vector attributes.
    c                 K   sl  dD ]*}||v r|| durt  || ¡||< q| jf ddi|¤Ž | j}| j}| j}| j| j| j  }}}t  |¡}t  |¡}t  |¡}d|v râ|dd…df  	¡ }|dd…df  	¡ }|dd…df  	¡ }| j|||dd	� n4t j
| 	¡ | 	¡ | 	¡ f  	¡ }d
|_| j|dd� | j| j| j  }	}
}|	du�r–t  |	¡}	t  |
¡}
t  |¡}t|	ƒdk�r–t j
|	 	¡ |
 	¡ | 	¡ f  	¡ }d
|_| j|dd� d|v �rð|dd…df  	¡ }	|dd…df  	¡ }
|dd…df  	¡ }| j|	|
|dd� nL|	du�r<t|	ƒdk�r<t j
|	 	¡ |
 	¡ | 	¡ f  	¡ }d
|_| j|dd� |du�rjt|ƒdk�rjt|ƒt|ƒk�sjJ ‚|du�r¦t  |¡}t|ƒdk�r¦t|ƒt| 	¡ ƒk�s¦J ‚t  dt|ƒdd¡}t  |t|ƒdf¡}d}| jdu �rìt ¡ }d}n| j}d|_|j|d� |j|d� | jdu�r0| j|j_d|jj_| jdu�rT| j 	¡ |j_d|jj_|| _|�sh|  ¡  dS )ú:Creates the dataset afresh or resets existing data source.©ÚxÚyÚzÚuÚvÚwr"   Nr+   FÚpointsr   r   é   ©r;   r<   r=   r+   ©éÿÿÿÿé   ©rA   r+   ©r!   r+   r!   ©r>   r?   r@   r+   ÚlT©rA   )Zvertsr"   )ÚnpÚ
atleast_1dr*   r!   r"   rA   r;   r<   r=   ÚravelÚc_Úshaper>   r?   r@   ÚlenÚarangeZreshaper$   r   ÚPolyDataÚpolysÚ
point_dataÚnamer(   )r   r   rV   r!   r"   rA   r;   r<   r=   r>   r?   r@   rT   Únew_datasetÚpdr   r   r   r    ”   s„    


 



ÿ

ÿ





zMGlyphSource.resetc                 C   s,   t  |¡}| ¡ | jd d …df< |  ¡  d S ©Nr   ©rL   rM   rN   rA   r(   ©r   r;   r   r   r   Ú
_x_changedï   s    
zMGlyphSource._x_changedc                 C   s,   t  |¡}| ¡ | jd d …df< |  ¡  d S ©Nr   rZ   ©r   r<   r   r   r   Ú
_y_changedô   s    
zMGlyphSource._y_changedc                 C   s,   t  |¡}| ¡ | jd d …df< |  ¡  d S ©NrB   rZ   ©r   r=   r   r   r   Ú
_z_changedù   s    
zMGlyphSource._z_changedc                 C   s,   t  |¡}| ¡ | jd d …df< |  ¡  d S rY   ©rL   rM   rN   r!   r(   ©r   r>   r   r   r   Ú
_u_changedþ   s    
zMGlyphSource._u_changedc                 C   s,   t  |¡}| ¡ | jd d …df< |  ¡  d S r]   rc   ©r   r?   r   r   r   Ú
_v_changed  s    
zMGlyphSource._v_changedc                 C   s,   t  |¡}| ¡ | jd d …df< |  ¡  d S r`   rc   ©r   r@   r   r   r   Ú
_w_changed  s    
zMGlyphSource._w_changedc                 C   s   t  |¡}|| j_|  ¡  d S ©N)rL   Ú
atleast_2dr$   rA   r(   ©r   Úpr   r   r   Ú_points_changed  s    
zMGlyphSource._points_changedc                 C   sR   |d u r"d | j j_| j j d¡ n$t |¡}| ¡ | j j_d| j jj_|  ¡  d S ©Nr"   )	r$   rU   r"   Zremove_arrayrL   rM   rN   rV   r(   ©r   Úsr   r   r   Ú_scalars_changed  s    

zMGlyphSource._scalars_changedc                 C   s"   || j j_d| j jj_|  ¡  d S )Nr!   )r$   rU   r!   rV   r(   rf   r   r   r   Ú_vectors_changed  s    
zMGlyphSource._vectors_changedN)r3   r4   r5   r6   r
   r;   r<   r=   r   rA   r"   r>   r?   r@   r!   r    r\   r_   rb   re   rg   ri   rn   rr   rs   r   r   r   r   r8   |   s(   [
r8   c                       s(   e Zd ZdZ‡ fdd„Zdd„ Z‡  ZS )ÚMVerticalGlyphSourcezê
    This class represents a vertical glyph data source for Mlab objects
    and allows the user to set the x, y, z, scalar attributes. The
    vectors are created from the scalars to represent them in the
    vertical direction.
    c                    sR   d|v r8|d }|dur8t  |¡f |d< |d< ||d< tt| ƒjf i |¤Ž dS )r9   r"   Nr>   r?   r@   )rL   Ú	ones_liker)   rt   r    )r   r   rq   r,   r   r   r    -  s    zMVerticalGlyphSource.resetc                 C   sF   || j j_d| j jj_| jtjt |¡t |¡|f d� |  ¡  d S )Nr"   )r!   )	r$   rU   r"   rV   r*   rL   rO   ru   r(   rp   r   r   r   rr   6  s    
þ
z%MVerticalGlyphSource._scalars_changed)r3   r4   r5   r6   r    rr   r7   r   r   r,   r   rt   %  s   	rt   c                   @   sp   e Zd ZdZeZeZeZeZeZ	eZ
eZeZdd„ Zedƒdd„ ƒZdd„ Zd	d
„ Zdd„ Zdd„ Zdd„ ZdS )ÚMArraySourcez‹
    This class represents an array data source for Mlab objects and
    allows the user to set the x, y, z, scalar/vector attributes.
    c                 K   s2  | j f ddi|¤Ž | j}| j}dd„ | j| j| jfD ƒ\}}}| j| j| j  }}}	d|v r®|dd…df  	¡ }|dd…df  	¡ }|dd…d	f  	¡ }	| j |||	dd
� nf|du�rt
|ƒdk�rtj| 	¡ | 	¡ |	 	¡ f  	¡ }|jd |jd |	jd	 df|_| j |dd� |du�rLt
|ƒdk�rL|du�rLt
|ƒt
|ƒk�sLJ ‚|jd dk�rbd}
n|d |d  }
|jd dk�rˆd}n|d |d  }|jd	 dk�r®d}n|d |d  }| jdu �rÖtdd�}n| j}|j}|j || ¡ | ¡ | ¡ g|
||g|d� ||u �r | |¡ |j| _|| _dS )r9   r+   Fc                 S   s   g | ]}t  |¡‘qS r   ©rL   Ú
atleast_3d©Ú.0Úar   r   r   Ú
<listcomp>b  ó    z&MArraySource.reset.<locals>.<listcomp>r!   Nr   r   rB   rI   rF   rH   ©r   r   r   ©r   r   r   ©r   r   r   ©r   r   r   T©Ztranspose_input_array)Úvector_dataÚoriginÚspacingÚscalar_data)r*   r!   r"   r;   r<   r=   r>   r?   r@   rN   rQ   rL   rO   rP   r&   r   r†   ÚminÚ_scalar_data_changedZ
image_datar$   )r   r   r!   r"   r;   r<   r=   r>   r?   r@   ÚdxÚdyÚdzr1   Ú
old_scalarr   r   r   r    Y  sP     ÿ
 "ý

zMArraySource.resetz	[x, y, z]c                 C   sš   | j | j| j  }}}|d |d  }|d |d  }|d |d  }| j}| ¡ | ¡ | ¡ g|_|||g|_| jd urŽ| jj|j|jd� |  	¡  d S )Nr~   r   r€   r�   ©r„   r…   )
r;   r<   r=   r$   r‡   r„   r…   r&   r*   r(   )r   r;   r<   r=   r‰   rŠ   r‹   r1   r   r   r   Ú_xyz_changed˜  s    
zMArraySource._xyz_changedc                 C   s   || j d< | j | j ¡ d S )N).r   ©r!   r&   Z_vector_data_changedrd   r   r   r   re   ¥  s    
zMArraySource._u_changedc                 C   s   || j d< | j | j ¡ d S )N).r   r�   rf   r   r   r   rg   ©  s    
zMArraySource._v_changedc                 C   s   || j d< | j | j ¡ d S )N).rB   r�   rh   r   r   r   ri   ­  s    
zMArraySource._w_changedc                 C   s(   | j j}|| j _||u r$| j  |¡ d S rj   )r&   r†   rˆ   )r   rq   Úoldr   r   r   rr   ±  s    zMArraySource._scalars_changedc                 C   s   || j _d S rj   )r&   rƒ   rf   r   r   r   rs   ·  s    zMArraySource._vectors_changedN)r3   r4   r5   r6   r   r;   r<   r=   r"   r>   r?   r@   r!   r    r   rŽ   re   rg   ri   rr   rs   r   r   r   r   rv   B  s"   ?
rv   c                   @   sT   e Zd ZdZeZeZeZeZeZ	dd„ Z
dd„ Zdd„ Zdd	„ Zd
d„ Zdd„ ZdS )ÚMLineSourcez‚
    This class represents a line data source for Mlab objects and
    allows the user to set the x, y, z, scalar attributes.
    c                 K   sÌ  | j f ddi|¤Ž | j}| j}| j| j| j  }}}d|v rŽ|dd…df  ¡ }|dd…df  ¡ }|dd…df  ¡ }| j |||dd� n<tj| ¡ | ¡ | ¡ f  ¡ }t	|ƒd	f|_
| j |dd
� t	|ƒd }t |dfd¡}t d|d dd¡|dd…df< t d|d dd¡|dd…df< d}	| jdu �rDt ¡ }
d}	n| j}
|
j dd� |
j |d� |
j |d� |du�r´t	|ƒdk�r´t	|ƒt	|ƒk�sœJ ‚t |¡|
j_d|
jj_|
| _|	�sÈ|  ¡  dS )r9   r+   FrA   Nr   r   rB   rC   rF   rG   rJ   g      à?T)ÚlinesrK   r"   )r*   rA   r"   r;   r<   r=   rN   rL   rO   rQ   rP   ÚzerosrR   r$   r   rS   rU   rV   r(   )r   r   rA   r"   r;   r<   r=   Zn_ptsr’   rW   rX   r   r   r   r    Ð  s>       
zMLineSource.resetc                 C   s   || j d d …df< |  ¡  d S rY   ©rA   r(   r[   r   r   r   r\     s    zMLineSource._x_changedc                 C   s   || j d d …df< |  ¡  d S r]   r”   r^   r   r   r   r_     s    zMLineSource._y_changedc                 C   s   || j d d …df< |  ¡  d S r`   r”   ra   r   r   r   rb     s    zMLineSource._z_changedc                 C   s   || j _|  ¡  d S rj   ©r$   rA   r(   rl   r   r   r   rn     s    zMLineSource._points_changedc                 C   s&   |  ¡ | jj_d| jjj_|  ¡  d S ro   ©rN   r$   rU   r"   rV   r(   rp   r   r   r   rr     s    zMLineSource._scalars_changedN)r3   r4   r5   r6   r   r;   r<   r=   rA   r"   r    r\   r_   rb   rn   rr   r   r   r   r   r‘   ¾  s   4r‘   c                   @   s@   e Zd ZdZeZeZeZeZdd„ Z	e
dƒdd„ ƒZdd„ Zd	S )
ÚMArray2DSourcez‡
    This class represents a 2D array data source for Mlab objects and
    allows the user to set the x, y  and scalar attributes.
    c                 K   sÄ  | j f ddi|¤Ž | j| j| j  }}}| j}|j\}}|du rx|du rxtj| d |d …| d |d …f \}}|dur´t|ƒdkr´tj	|| 
d¡< | 
d¡}| j |dd	� t dg¡}| j |||dd
� t | ¡ j¡ddd…f  ¡ }t | ¡ ¡ddd…f  ¡ }|jdk�r"d}	n|d |d  }	|jdk�rDd}
n|d |d  }
| jdu �rltdd�}n| j}|j}|j | ¡ | ¡ dg|	|
dg|d� ||u �r°| |¡ | ¡ | _|| _dS )r9   r+   FNç       @rB   r   ÚboolÚfloat©r"   r+   rC   r   Tr‚   )r„   r…   r†   )r*   r;   r<   Úmaskr"   rP   rL   ÚmgridrQ   ÚnanÚastypeÚarrayrk   ÚsqueezeÚTÚndimr&   r   r†   r‡   rˆ   Zget_output_datasetr$   )r   r   r;   r<   rœ   r"   ÚnxÚnyr=   r‰   rŠ   r1   rŒ   r   r   r   r    2  s@    
.
 þ


zMArray2DSource.resetz[x, y]c           	      C   s  | j | j| j  }}}|j\}}|d u s0|d u r^tj| d |d …| d |d …f \}}| j||d� t | ¡ j	¡dd d …f  ¡ }t | ¡ ¡dd d …f  ¡ }|d |d  }|d |d  }| j
}| ¡ | ¡ dg|_||dg|_| jd u�r| jj|j|jd� |  ¡  d S )Nr˜   rB   )r;   r<   r   r   r�   )r;   r<   r"   rP   rL   r�   Ú
trait_setqrk   r¡   r¢   r$   r‡   r„   r…   r&   r*   r(   )	r   r;   r<   r"   r¤   r¥   r‰   rŠ   r1   r   r   r   Ú_xy_changedk  s    
. zMArray2DSource._xy_changedc                 C   sj   | j }|d urBt|ƒdkrBtj|| d¡< | d¡}| j|dd� | jj}|| j_||u rf| j |¡ d S )Nr   r™   rš   Fr›   )	rœ   rQ   rL   rž   rŸ   r*   r&   r†   rˆ   )r   rq   rœ   r�   r   r   r   rr   €  s    
zMArray2DSource._scalars_changedN)r3   r4   r5   r6   r   r;   r<   r"   rœ   r    r   r§   rr   r   r   r   r   r—     s   9
r—   c                   @   sX   e Zd ZdZeZeZeZeZeZ	eZ
dd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ ZdS )ÚMGridSourcezz
    This class represents a grid source for Mlab objects and
    allows the user to set the x, y, scalar attributes.
    c                 K   sø  | j f ddi|¤Ž | j}| j}| j| j| j| jf\}}}}|durxt|ƒdkrxtj	|| 
d¡< | 
d¡}| j |dd� t|jƒdksŽJ d	ƒ‚t|jƒdks¤J d
ƒ‚t|jƒdksºJ dƒ‚|j|jksÎJ dƒ‚|j|jksâJ dƒ‚|j\}}	tj| ¡ | ¡ | ¡ f  ¡ }||	 df|_| j |dd� tjd|d …d|	d …f \}
}t |
¡t |¡ }
}|
|	 | |
d |	 | |
d |	 |d  f}|
d |	 |d  |
|	 |d  |
|	 | f}t|d ƒ}t |d dfd¡}|\|d|…df< |d|…df< |d|…df< |\||d…df< ||d…df< ||d…df< d}| jdu �rVt ¡ }d}n| j}|j dd� |j |d� |j |d� |du�ràt|ƒdk�rà|jj�s¸| ¡ }| j |dd� |j|jk�sÊJ ‚| ¡ |j_d|jj_|| _|�sô|  ¡  dS )r9   r+   FNr   r™   rš   r›   rB   zArray x must be 2 dimensional.zArray y must be 2 dimensional.zArray z must be 2 dimensional.z$Arrays x and y must have same shape.z$Arrays y and z must have same shape.rF   rG   r   rJ   T©rT   rK   r"   )r*   rA   r"   r;   r<   r=   rœ   rQ   rL   rž   rŸ   rP   rO   rN   r�   r“   r$   r   rS   ÚflagsÚ
contiguousÚcopyrU   rV   r(   )r   r   rA   r"   r;   r<   r=   rœ   r¤   r¥   ÚiÚjÚt1Út2ÚntÚ	trianglesrW   rX   r   r   r   r    ¥  sV    

 "....

zMGridSource.resetc                 C   s.   | j |d� | ¡ | jd d …df< |  ¡  d S ©N)r;   r   ©r¦   rN   rA   r(   r[   r   r   r   r\   ç  s    zMGridSource._x_changedc                 C   s.   | j |d� | ¡ | jd d …df< |  ¡  d S ©N)r<   r   r´   r^   r   r   r   r_   ì  s    zMGridSource._y_changedc                 C   s.   | j |d� | ¡ | jd d …df< |  ¡  d S ©N)r=   rB   r´   ra   r   r   r   rb   ñ  s    zMGridSource._z_changedc                 C   s   || j _|  ¡  d S rj   r•   rl   r   r   r   rn   ö  s    zMGridSource._points_changedc                 C   sh   | j }|d urBt|ƒdkrBtj|| d¡< | d¡}| j|dd� | ¡ | jj_	d| jjj	_
|  ¡  d S )Nr   r™   rš   Fr›   r"   )rœ   rQ   rL   rž   rŸ   r*   rN   r$   rU   r"   rV   r(   )r   rq   rœ   r   r   r   rr   ú  s    
zMGridSource._scalars_changedN)r3   r4   r5   r6   r   r;   r<   r=   rA   r"   rœ   r    r\   r_   rb   rn   rr   r   r   r   r   r¨   �  s   Br¨   c                   @   s`   e Zd ZdZeZeZeZeZeZ	eZ
dd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Zdd„ ZdS )ÚMTriangularMeshSourcez…
    This class represents a triangular mesh source for Mlab objects and
    allows the user to set the x, y, scalar attributes.
    c           
      K   s�  | j f ddi|¤Ž | j}| j}| j| j| j  }}}tj| ¡ | ¡ | ¡ f  ¡ }d|_	| j |dd� | j
}|j	d dks†J dƒ‚| ¡ t|ƒk sžJ dƒ‚| ¡ d	ks²J d
ƒ‚d}| jdu rÎt ¡ }	d}n| j}	d|	_|	j |d� |	j |d� d|v�r|du�r|j	|j	k�r|}|du�rxt|ƒd	k�rx|jj�sP| ¡ }| j |dd� |j	|j	k�sbJ ‚| ¡ |	j_d|	jj_|	| _|�sŒ|  ¡  dS )r9   r+   FrD   rG   r   rF   z/The shape of the triangles array must be (X, 3)z?The triangles indices must be smaller that the number of pointsr   z.The triangles indices must be positive or nullNTrK   r©   r"   r›   )r*   rA   r"   r;   r<   r=   rL   rO   rN   rP   r²   ÚmaxrQ   r‡   r$   r   rS   rT   rª   r«   r¬   rU   rV   r(   )
r   r   rA   r"   r;   r<   r=   r²   rW   rX   r   r   r   r      sN     ÿÿÿ

ÿ

zMTriangularMeshSource.resetc                 C   s.   | j |d� | ¡ | jd d …df< |  ¡  d S r³   r´   r[   r   r   r   r\   X  s    z MTriangularMeshSource._x_changedc                 C   s.   | j |d� | ¡ | jd d …df< |  ¡  d S rµ   r´   r^   r   r   r   r_   ]  s    z MTriangularMeshSource._y_changedc                 C   s.   | j |d� | ¡ | jd d …df< |  ¡  d S r¶   r´   ra   r   r   r   rb   b  s    z MTriangularMeshSource._z_changedc                 C   s   || j _|  ¡  d S rj   r•   rl   r   r   r   rn   g  s    z%MTriangularMeshSource._points_changedc                 C   s&   |  ¡ | jj_d| jjj_|  ¡  d S ro   r–   rp   r   r   r   rr   k  s    z&MTriangularMeshSource._scalars_changedc                 C   s@   |  ¡ dk rtdƒ‚| ¡ | jjkr,tdƒ‚|| j_|  ¡  d S )Nr   ú'The triangles array has negative valuesú>The triangles array has values larger thanthe number of points)r‡   Ú
ValueErrorr¸   r;   Úsizer$   rT   r(   )r   r²   r   r   r   Ú_triangles_changedp  s    z(MTriangularMeshSource._triangles_changedN)r3   r4   r5   r6   r   r;   r<   r=   rA   r²   r"   r    r\   r_   rb   rn   rr   r½   r   r   r   r   r·   
  s   ;r·   c                 C   sd   t | ƒ} t| ƒD ]N\}}t|ƒst|dƒs4t |¡}t |¡}t t |¡¡rVt	dƒ‚|| |< q| S )zW Converts a list of iterables to a list of arrays or callables,
        if needed.
    rP   zxInput array contains infinite values
                You can remove them using: a[np.isinf(a)] = np.nan
                )
ÚlistÚ	enumerateÚcallabler0   rL   r    rM   ÚanyÚisinfr»   )ÚargsÚindexÚargr   r   r   Úconvert_to_arrays~  s    



rÆ   c                  G   s  t | ƒ} t| ƒdkrRdd„ | D ƒ\}}}t|jƒdks>J dƒ‚t |j¡\}}}nbt| ƒdkrp| \}}}}}}nDt| ƒdkr¬| \}}}}t|ƒs˜tdƒ‚||||ƒ\}}}ntdƒ‚|j|jkrð|j|jkrð|j|jkrð|j|jkrð|j|jksøJ d	ƒ‚||||||fS )
z6 Converts different signatures to (x, y, z, u, v, w). rF   c                 S   s   g | ]}t  |¡‘qS r   rw   ry   r   r   r   r|   ”  r}   z+process_regular_vectors.<locals>.<listcomp>ú3D array requiredé   é   z<When 4 arguments are provided, the fourth must be a callableúwrong number of argumentsúargument shape are not equal)rÆ   rQ   rP   rL   ÚindicesrÀ   r»   )rÃ   r>   r?   r@   r;   r<   r=   Úfr   r   r   Úprocess_regular_vectors�  s2    
ÿ
þ
ý
üürÎ   c                  G   sÞ   t | ƒ} t| ƒdkrLt | d ¡}t|jƒdks8J dƒ‚t |j¡\}}}nRt| ƒdkrh| \}}}d}n6t| ƒdkr–| \}}}}t|ƒrž||||ƒ}ntdƒ‚|j|jkrÊ|j|jkrÊ|du sÒ|j|jksÒJ dƒ‚||||fS )	z0 Converts different signatures to (x, y, z, s). r   r   rF   rÇ   NrÉ   rÊ   rË   )rÆ   rQ   rL   rx   rP   rÌ   rÀ   r»   )rÃ   rq   r;   r<   r=   r   r   r   Úprocess_regular_scalars«  s.    

ÿþ
ýýrÏ   c                  O   sè   t | ƒ} t| ƒD ]\}}t|ƒst |¡| |< qt| ƒdkrl| d }t|jƒdksZJ dƒ‚t |j¡\}}n2t| ƒdkr–| \}}}t|ƒrž|||ƒ}ntdƒ‚t|jƒdks´J dƒ‚d|v rÞ|d }tj	|| 
d¡< | 
d	¡}|||fS )
z- Converts different signatures to (x, y, s). r   r   rB   z2D array requiredrF   rÊ   rœ   r™   rš   )rÆ   r¿   rÀ   rL   rk   rQ   rP   rÌ   r»   rž   rŸ   )rÃ   ÚkwargsrÄ   rÅ   rq   r;   r<   rœ   r   r   r   Úprocess_regular_2d_scalarsÄ  s&    

rÑ   c               	   O   s|   t | Ž \}}}}}}| dd¡}|dur2t |¡}| dd¡}	tƒ }
|
j|||||||d� tj|
j|	fi |¤Ž}||
_	|S )aÓ   Creates scattered vector data.

    **Function signatures**::

        vector_scatter(u, v, w, ...)
        vector_scatter(x, y, z, u, v, w, ...)
        vector_scatter(x, y, z, f, ...)

    If only 3 arrays u, v, w are passed the x, y and z arrays are assumed to be
    made from the indices of vectors.

    If 4 positional arguments are passed the last one must be a callable, f,
    that returns vectors.

    **Keyword arguments**:

        :name: the name of the vtk object created.

        :scalars: optional scalar data.

        :figure: optionally, the figure on which to add the data source.
                 If None, the source is not added to any figure, and will
                 be added automatically by the modules or
                 filters. If False, no figure will be created by modules
                 or filters applied to the source: the source can only
                 be used for testing, or numerical algorithms, not
                 visualization.r"   NrV   ZVectorScatterr:   )
rÎ   ÚpoprL   rN   r8   r    r   Úadd_datasetr$   r&   )rÃ   rÐ   r;   r<   r=   r>   r?   r@   r"   rV   Údata_sourcer1   r   r   r   r   ã  s    
r   c               	   O   s°   t | ƒdkr4t d¡ } }}dd„ | D ƒ\}}}ndd„ t| Ž D ƒ\}}}}}}| dd¡}|durpt |¡}tƒ }	|	j|||||||d� | d	d
¡}
tj|	j	|
fi |¤ŽS )a¢   Creates vector field data.

    **Function signatures**::

        vector_field(u, v, w, ...)
        vector_field(x, y, z, u, v, w, ...)
        vector_field(x, y, z, f, ...)

    If only 3 arrays u, v, w are passed the x, y and z arrays are assumed to be
    made from the indices of vectors.

    If the x, y and z arrays are passed, they should have been generated
    by `numpy.mgrid` or `numpy.ogrid`. The function builds a scalar field
    assuming the points are regularily spaced on an orthogonal grid.

    If 4 positional arguments are passed the last one must be a callable, f,
    that returns vectors.

    **Keyword arguments**:

        :name: the name of the vtk object created.

        :scalars: optional scalar data.

        :figure: optionally, the figure on which to add the data source.
                 If None, the source is not added to any figure, and will
                 be added automatically by the modules or
                 filters. If False, no figure will be created by modules
                 or filters applied to the source: the source can only
                 be used for testing, or numerical algorithms, not
                 visualization.rF   r   c                 S   s   g | ]}t  |¡‘qS r   rw   ry   r   r   r   r|   0  r}   z vector_field.<locals>.<listcomp>c                 S   s   g | ]}t  |¡‘qS r   rw   ry   r   r   r   r|   2  s   ÿr"   Nr:   rV   ZVectorField)
rQ   rL   rx   rÎ   rÒ   rv   r    r   rÓ   r&   )rÃ   rÐ   r;   r<   r=   r>   r?   r@   r"   rÔ   rV   r   r   r   r     s     ÿ
r   c            	      O   sf   t | Ž \}}}}|dur"t |¡}tƒ }|j||||d� | dd¡}tj|j|fi |¤Ž}||_	|S )aÒ  
    Creates scattered scalar data.

    **Function signatures**::

        scalar_scatter(s, ...)
        scalar_scatter(x, y, z, s, ...)
        scalar_scatter(x, y, z, s, ...)
        scalar_scatter(x, y, z, f, ...)

    If only 1 array s is passed the x, y and z arrays are assumed to be
    made from the indices of vectors.

    If 4 positional arguments are passed the last one must be an array s, or
    a callable, f, that returns an array.

    **Keyword arguments**:

        :name: the name of the vtk object created.

        :figure: optionally, the figure on which to add the data source.
                 If None, the source is not added to any figure, and will
                 be added automatically by the modules or
                 filters. If False, no figure will be created by modules
                 or filters applied to the source: the source can only
                 be used for testing, or numerical algorithms, not
                 visualization.N©r;   r<   r=   r"   rV   ZScalarScatter)
rÏ   rL   rN   r8   r    rÒ   r   rÓ   r$   r&   ©	rÃ   rÐ   r;   r<   r=   rq   rÔ   rV   r1   r   r   r   r   >  s    
r   c                  O   sr   t | ƒdkr(t d¡ } }}| d }nt| Ž \}}}}tƒ }|j||||d� | dd¡}tj|j	|fi |¤ŽS )a[  
    Creates a scalar field data.

    **Function signatures**::

        scalar_field(s, ...)
        scalar_field(x, y, z, s, ...)
        scalar_field(x, y, z, f, ...)

    If only 1 array s is passed the x, y and z arrays are assumed to be
    made from the indices of arrays.

    If the x, y and z arrays are passed they are supposed to have been
    generated by `numpy.mgrid`. The function builds a scalar field assuming
    the points are regularily spaced.

    If 4 positional arguments are passed the last one must be an array s, or
    a callable, f, that returns an array.

    **Keyword arguments**:

        :name: the name of the vtk object created.

        :figure: optionally, the figure on which to add the data source.
                 If None, the source is not added to any figure, and will
                 be added automatically by the modules or
                 filters. If False, no figure will be created by modules
                 or filters applied to the source: the source can only
                 be used for testing, or numerical algorithms, not
                 visualization.r   r   rÕ   rV   ZScalarField)
rQ   rL   rM   rÏ   rv   r    rÒ   r   rÓ   r&   )rÃ   rÐ   r;   r<   r=   rq   rÔ   rV   r   r   r   r   h  s    
r   c            	      O   sh   t | ƒdkrtdƒ‚t| Ž \}}}}tƒ }|j||||d� | dd¡}tj|j|fi |¤Ž}||_	|S )a4  
    Creates line data.

    **Function signatures**::

        line_source(x, y, z, ...)
        line_source(x, y, z, s, ...)
        line_source(x, y, z, f, ...)

        If 4 positional arguments are passed the last one must be an array s,
        or a callable, f, that returns an array.

    **Keyword arguments**:

        :name: the name of the vtk object created.

        :figure: optionally, the figure on which to add the data source.
                 If None, the source is not added to any figure, and will
                 be added automatically by the modules or
                 filters. If False, no figure will be created by modules
                 or filters applied to the source: the source can only
                 be used for testing, or numerical algorithms, not
                 visualization.r   rÊ   rÕ   rV   Z
LineSource)
rQ   r»   rÏ   r‘   r    rÒ   r   rÓ   r$   r&   rÖ   r   r   r   r   –  s    r   c                  O   sŒ   t ƒ }| dd¡}t| ƒdkrDt| ƒ} t | d ¡}|j||d� n&t| i |¤Ž\}}}|j||||d� | dd¡}tj	|j
|fi |¤ŽS )	aŠ  
    Creates structured 2D data from a 2D array.

    **Function signatures**::

        array2d_source(s, ...)
        array2d_source(x, y, s, ...)
        array2d_source(x, y, f, ...)

    If 3 positional arguments are passed the last one must be an array s,
    or a callable, f, that returns an array. x and y give the
    coordinates of positions corresponding to the s values.

    x and y can be 1D or 2D arrays (such as returned by numpy.ogrid or
    numpy.mgrid), but the points should be located on an orthogonal grid
    (possibly non-uniform). In other words, all the points sharing a same
    index in the s array need to have the same x or y value.

    If only 1 array s is passed the x and y arrays are assumed to be
    made from the indices of arrays, and an uniformly-spaced data set is
    created.

    **Keyword arguments**:

        :name: the name of the vtk object created.

        :figure: optionally, the figure on which to add the data source.
                 If None, the source is not added to any figure, and will
                 be added automatically by the modules or
                 filters. If False, no figure will be created by modules
                 or filters applied to the source: the source can only
                 be used for testing, or numerical algorithms, not
                 visualization.

        :mask: Mask points specified in a boolean masking array.
    rœ   Nr   r   )r"   rœ   )r;   r<   r"   rœ   rV   ZArray2DSource)r—   rÒ   rQ   rÆ   rL   rk   r    rÑ   r   rÓ   r&   )rÃ   rÐ   rÔ   rœ   rq   r;   r<   rV   r   r   r   r   »  s    %r   c           	      K   s‚   |  dd¡}|du r|}|  dd¡}t| |||fƒ\} }}}tƒ }|j| ||||d� |  dd¡}tj|j|fi |¤Ž}||_|S )aÛ  
    Creates 2D grid data.

    x, y, z are 2D arrays giving the positions of the vertices of the surface.
    The connectivity between these points is implied by the connectivity on
    the arrays.

    For simple structures (such as orthogonal grids) prefer the array2dsource
    function, as it will create more efficient data structures.

    **Keyword arguments**:

        :name: the name of the vtk object created.

        :scalars: optional scalar data.

        :figure: optionally, the figure on which to add the data source.
                 If None, the source is not added to any figure, and will
                 be added automatically by the modules or
                 filters. If False, no figure will be created by modules
                 or filters applied to the source: the source can only
                 be used for testing, or numerical algorithms, not
                 visualization.

        :mask: Mask points specified in a boolean masking array.

        r"   Nrœ   )r;   r<   r=   r"   rœ   rV   Z
GridSource)rÒ   rÆ   r¨   r    r   rÓ   r$   r&   )	r;   r<   r=   rÐ   r"   rœ   rÔ   rV   r1   r   r   r   r   î  s    r   c            
      O   s¢   t | ƒdkr<| \}}}t |¡r&d}n
t |¡}||||f} t| Ž \}}}}|dur^t |¡}tƒ }|j||||d� | dd¡}t	j
|j|fi |¤Ž}	|	|_|	S )a@  
    Creates a set of vectors pointing upward, useful eg for bar graphs.

    **Function signatures**::

        vertical_vectors_source(s, ...)
        vertical_vectors_source(x, y, s, ...)
        vertical_vectors_source(x, y, f, ...)
        vertical_vectors_source(x, y, z, s, ...)
        vertical_vectors_source(x, y, z, f, ...)

    If only one positional argument is passed, it can be a 1D, 2D, or 3D
    array giving the length of the vectors. The positions of the data
    points are deducted from the indices of array, and an
    uniformly-spaced data set is created.

    If 3 positional arguments (x, y, s) are passed the last one must be
    an array s, or a callable, f, that returns an array. x and y give the
    2D coordinates of positions corresponding to the s values. The
    vertical position is assumed to be 0.

    If 4 positional arguments (x, y, z, s) are passed, the 3 first are
    arrays giving the 3D coordinates of the data points, and the last one
    is an array s, or a callable, f, that returns an array giving the
    data value.

    **Keyword arguments**:

        :name: the name of the vtk object created.

        :figure: optionally, the figure on which to add the data source.
                 If None, the source is not added to any figure, and will
                 be added automatically by the modules or
                 filters. If False, no figure will be created by modules
                 or filters applied to the source: the source can only
                 be used for testing, or numerical algorithms, not
                 visualization.
    rF   r   NrÕ   rV   ZVerticalVectorsSource)rQ   rL   ZisscalarZ
zeros_likerÏ   rN   rt   r    rÒ   r   rÓ   r$   r&   )
rÃ   rÐ   r;   r<   Údatar=   rq   rÔ   rV   r1   r   r   r   r     s    '



r   c           	      K   s    t | |||fƒ\} }}}| ¡ dk r,tdƒ‚| ¡ | jkrBtdƒ‚| dd¡}|du rZ|}tƒ }|j| ||||d� | dd¡}tj	|j
|fi |¤Ž}||_|S )	ax  
    Creates 2D mesh by specifying points and triangle connectivity.

    x, y, z are 2D arrays giving the positions of the vertices of the surface.
    The connectivity between these points is given by listing triplets of
    vertices inter-connected. These vertices are designed by there
    position index.

    **Keyword arguments**:

        :name: the name of the vtk object created.

        :scalars: optional scalar data.

        :figure: optionally, the figure on which to add the data source.
                 If None, the source is not added to any figure, and will
                 be added automatically by the modules or
                 filters. If False, no figure will be created by modules
                 or filters applied to the source: the source can only
                 be used for testing, or numerical algorithms, not
                 visualization.
        r   r¹   rº   r"   N)r;   r<   r=   r²   r"   rV   ZTriangularMeshSource)rÆ   r‡   r»   r¸   r¼   rÒ   r·   r    r   rÓ   r$   r&   )	r;   r<   r=   r²   rÐ   r"   rÔ   rV   r1   r   r   r   r   V  s    r   c                 C   s@   |du rt  ¡ }n |du r"tƒ }nt |¡}||_| | ¡}|S )záOpen a supported data file given a filename.  Returns the source
    object if a suitable reader was found for the file.

    If 'figure' is False, no view is opened, and the code does not need
    GUI or openGL context.
    NF)r   Ú
get_enginer   r   Zfind_figure_engineZcurrent_scener   )ÚfilenameÚfigureÚengineÚsrcr   r   r   r   �  s    


r   c                 C   s$   |   ¡ }|ƒ }t ¡ }| |¡ |S )zzCreates a data source and adds it to the mayavi engine given
    metadata of the source.  Returns the created source.
    )Zget_callabler   rØ   Z
add_source)ÚmetadataÚfactoryrÜ   rÛ   r   r   r   Ú_create_data_source—  s
    
rß   c                 C   sj   t jD ]^}t|jƒdkrt|jƒ}| d¡r8|dd… }|fdd„}|j|_||_	|| |< t
 |¡ qdS )z;Make the automatic functions and add them to the namespace.r   Ú_sourceNiùÿÿÿc                 S   s   t | ƒS rj   )rß   )rÝ   r   r   r   Ú<lambda>©  r}   z!_make_functions.<locals>.<lambda>)r	   ÚsourcesrQ   Ú
extensionsr   ÚidÚendswithÚhelpr6   r3   Ú__all__Úappend)Ú	namespacerÜ   Ú	func_nameÚfuncr   r   r   Ú_make_functions¢  s    


rì   )N)1r6   ÚnumpyrL   Z
traits.apir   r   r   r   Ztvtk.apir   Ztvtk.commonr   Zmayavi.sources.array_sourcer   Zmayavi.core.registryr	   Zmayavi.core.trait_defsr
   r   Ú r   r   r   rç   r   r8   rt   rv   r‘   r—   r¨   r·   rÆ   rÎ   rÏ   rÑ   r   r   r   r   r   r   r   r   r   r   rß   rì   Úlocalsr   r   r   r   Ú<module>   sH   		\ *|_szt+0*.%3+=+
