3
5Ad]p  ã               @   s"  d Z ddlZddlZddlmZ yddlmZ W n ek
rH   dZY nX ddlZyddlm	Z	 dZ
W n ek
r~   dZ
Y nX ddlmZ ejƒ jƒ Zed	kr¨ejZned
kr¶ejZejƒ jƒ Zed	krØejZejZned
krìejZejZejƒ jƒ Zejd dk�reZdd„ Zdd„ Z e
�s&e Z	G dd„ de!ƒZ"dZ#x:d7D ]2Z$e$ej%k�r@ej%e$ Z&e'e&dƒ�rle&j(Z#[&P �q@W e#�r‚e#a(ne"ƒ a([#dd„ Z)dd„ Z*dd„ Z+dd„ Z,dd„ Z-d8dd „Z.d!d"„ Z/d9d#d$„Z0d:d%d&„Z1d;d'd(„Z2d)d*„ Z3d<d+d,„Z4d-d.„ Z5d/d0„ Z6d1d2„ Z7d3d4„ Z8d=d5d6„Z9dS )>aU  
This module contains all the array handling code for TVTK.

The most important functions provided by this module involve the
conversion of numpy arrays/Python lists to different VTK data arrays
and vice-versa.

Warning: Numpy Character arrays will not work properly since there
seems no unique one-to-one VTK data array type to map it to.

é    N)ÚvtkConstants)Únumpy_support)Úset_id_type_arrayTF)Úis_old_pipelineé   é   é   c             C   s   t tdtƒ| ƒS )NÚ	getbuffer)ÚgetattrÚnumpyÚ
memoryview)Úarray© r   úy/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.6/site-packages/mayavi-4.7.1-py3.6-linux-x86_64.egg/tvtk/array_handler.pyr	   9   s    r	   c             C   sÂ   t j| jt jƒst‚|jjdks(tdƒ‚| j}t|ƒdksBtdƒ‚|j	}|d |d d  }||ksttd||f ƒ‚|j}|d |d d f|_|d |dd…df< | |dd…dd…f< ||_dS )aü  Given a 2D Int array (`id_array`), and a contiguous 1D numarray array
    (`out_array`) having the correct size, this function sets the data from
    `id_array` into `out_array` so that it can be used in place of a
    `vtkIdTypeArray` in order to set the cells of a `vtkCellArray`.

    Note that if `shape = id_array.shape` then `size(out_array) ==
    shape[0]*(shape[1] + 1)` should be true. If not you'll get an
    `AssertionError`.

    `id_array` need not be contiguous but `out_array` must be.

    é   zout_array must be contiguous.r   z)id_array must be a two dimensional array.r   z4out_array size is incorrect, expected: %s, given: %sN)
r   Ú
issubdtypeÚdtypeZsignedintegerÚAssertionErrorÚflagsÚ
contiguousÚshapeÚlenÚsize)Zid_arrayÚ	out_arrayÚshpÚszZe_szZout_shpr   r   r   Úset_id_type_array_py=   s    
r   c               @   s@   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Z	dS )Ú
ArrayCachea-  Caches references to numpy arrays that are not copied but views
    of which are converted to VTK arrays.  The caching prevents the user
    from deleting or resizing the numpy array after it has been sent
    down to VTK.  The cached arrays are automatically removed when the
    VTK array destructs.c             C   s
   i | _ d S )N)Ú_cache)Úselfr   r   r   Ú__init__n   s    zArrayCache.__init__c             C   s
   t | jƒS )N)r   r   )r   r   r   r   Ú__len__r   s    zArrayCache.__len__c             C   s   |j }|| jkS )N)Ú__this__r   )r   Úvtk_arrÚkeyr   r   r   Ú__contains__u   s    zArrayCache.__contains__c                s0   |j }ˆ j}|jd|f‡ fdd„	ƒ |||< dS )zDAdd numpy array corresponding to the vtk array to the
        cache.ÚDeleteEventc                s
   ˆ j |ƒS )N)Ú_remove_array)ÚoÚer$   )r   r   r   Ú<lambda>ˆ   s    z ArrayCache.add.<locals>.<lambda>N)r"   r   ÚAddObserver)r   r#   Znp_arrr$   Úcacher   )r   r   Úadd|   s
    zArrayCache.addc             C   s   |j }| j| S )z0Return the cached numpy array given a VTK array.)r"   r   )r   r#   r$   r   r   r   ÚgetŽ   s    zArrayCache.getc             C   s&   y| j |= W n tk
r    Y nX dS )zmPrivate function that removes the cached array.  Do not
        call this unless you know what you are doing.N)r   ÚKeyError)r   r$   r   r   r   r'   –   s    zArrayCache._remove_arrayN)
Ú__name__Ú
__module__Ú__qualname__Ú__doc__r    r!   r%   r-   r.   r'   r   r   r   r   r   c   s   r   Úarray_handlerútvtk.array_handlerÚ_array_cachec             C   sL  t jt jƒtjt jt jƒtjt jt jƒtjt jt jƒtj	t jt j
ƒtjt jt jƒtjt jt jƒtjt jt jƒtjt jt jƒtjt jt jƒtjt jt jƒtjt jt jƒtji}t jtƒtjt jtƒtjt jtƒtji}x |D ]}||krÚ|| ||< qÚW y||  S  tk
�r:   x$|D ]}t j| |ƒ�r|| S �qW Y nX td|  ƒ‚dS )z+Returns a VTK typecode given a numpy array.z)Couldn't translate array's type to VTK %sN) r   r   Ú	characterr   ÚVTK_UNSIGNED_CHARÚuint8Úuint16ÚVTK_UNSIGNED_SHORTÚint8ÚVTK_CHARÚint16Ú	VTK_SHORTÚint32ÚVTK_INTÚuint32ÚVTK_UNSIGNED_INTÚuint64ÚVTK_UNSIGNED_LONGÚfloat32Ú	VTK_FLOATÚfloat64Ú
VTK_DOUBLEÚ	complex64Ú
complex128ÚID_TYPE_CODEÚVTK_ID_TYPEÚULONG_TYPE_CODEÚLONG_TYPE_CODEÚVTK_LONGr/   r   Ú	TypeError)Znumeric_array_typeZ_arr_vtkZ_extraÚtr$   r   r   r   Úget_vtk_array_type·   s4    

rS   c              C   sb   t jtjt jtjt jtjt jtj	t j
tjt jtjt jtjt jtt jtt jtt jtjt jtji} | S )z7Returns the VTK array type to numpy array type mapping.)r   ÚVTK_BITr   Úboolr=   r<   r8   r9   r?   r>   r;   r:   rA   r@   rC   rB   rP   rO   rE   rN   rM   rL   rG   rF   rI   rH   )Z_vtk_arrr   r   r   Úget_vtk_to_numeric_typemapÜ   s    rV   c             C   s
   t ƒ |  S )z6Returns a numpy array typecode given a VTK array type.)rV   )Úvtk_array_typer   r   r   Úget_numeric_array_typeï   s    rX   c             C   sT   t jdt jdt jdt jdt jdt jdt jdt jt	t j
t	t jtt jdt jdi}||  S )z%Returns the size of a VTK array type.r   r   r   r   )r   rT   r=   r8   r?   r;   rA   rC   rP   ÚVTK_LONG_TYPE_SIZErE   rM   ÚVTK_ID_TYPE_SIZErG   rI   )rW   Z
_size_dictr   r   r   Úget_sizeof_vtk_arrayô   s    
r[   c             C   s   t jj| ƒ}|jtƒ |S )zkInternal function used to create a VTK data array from another
    VTK array given the VTK array type.
    )ÚvtkÚvtkDataArrayÚCreateDataArrayÚSetReferenceCountÚBASE_REFERENCE_COUNT)Zvtk_arr_typeÚtmpr   r   r   Úcreate_vtk_array  s    
rb   c       
      C   sh  t j| ƒ}|j}t|ƒdk s$tdƒ‚t j|jt jƒ s>tdƒ‚d}|dkr^t|jƒ}t	|ƒ}n0|j
ƒ tjkr‚tj}t	tjƒ}d}n|j
ƒ }|}t|ƒdkr¦|jdƒ n|j|d ƒ |j|d ƒ t|ƒ}t j|j|ƒrät j|ƒ}nt j|ƒj|ƒ}|jt|ƒt|ƒdƒ |�rX|j|jƒ ƒ |j|jƒ ƒ x$t|jƒ ƒD ]}	|j|	||	ƒ �q:W |}ntj||ƒ |S )	a  Converts a real numpy Array (or a Python list) to a VTK array
    object.

    This function only works for real arrays.  Complex arrays are NOT
    handled.  It also works for multi-component arrays.  However, only
    1, and 2 dimensional arrays are supported.  This function is very
    efficient, so large arrays should not be a problem.

    Even in cases when no copy of the numpy array data is performed,
    a reference to the array is cached.  The passed array can
    therefore be deleted safely in all circumstances.

    Parameters
    ----------

    - num_array : numpy array or Python list/tuple

      The input array must be 1 or 2D.  A copy of the numeric array
      data passed is made in the following circumstances:

       1. A Python list/tuple was passed.
       2. A non-contiguous numpy array was passed.
       3. A `vtkBitArray` instance was passed as the second argument.
       4. The types of the `vtk_array` and the `num_array` are not
          equivalent to each other.  For example if one is an integer
          array and the other a float.

    - vtk_array : `vtkDataArray` (default: `None`)

      If an optional `vtkDataArray` instance, is passed as an argument
      then a new array is not created and returned.  The passed array
      is itself returned.

    é   z5Only arrays of dimensionality 2 or lower are allowed!z…Complex numpy arrays cannot be converted to vtk arrays.Use real() or imag() to get a component of the array before passing it to vtk.FNTr   r   )r   Úasarrayr   r   r   r   r   ÚcomplexfloatingrS   rb   ÚGetDataTyper   rT   r=   ÚSetNumberOfComponentsÚSetNumberOfTuplesrX   ÚravelÚastypeÚSetVoidArrayr	   ÚGetNumberOfTuplesÚGetNumberOfComponentsÚrangeÚCopyComponentr6   r-   )
Ú	num_arrayÚ	vtk_arrayÚzr   Z	bit_arrayZvtk_typecodeZresult_arrayZ	arr_dtypeZz_flatÚir   r   r   Ú	array2vtk  sB    $



rt   c             C   sÊ  | j ƒ }|tƒ jƒ ks"td| ƒ‚| jƒ | jƒ f}|d dkrRt|ƒ}tjg |ƒS | t	kršt	j
| ƒ}|d dkrz|d f}|jtj|ƒkrštj||ƒ}|S tdk	râ|tjkrât|ƒ}tj| |d�}|d dkrØ|d f}||_|S tjƒ }|j|d ddƒ |tjk�r*tjƒ }|j| ƒ |jƒ j|ƒ nt|tjk�r�tjƒ }|j| jƒ ƒ | jƒ }|j|ƒ x t|ƒD ]}	|j|	| |	ƒ �qhW |jƒ j|ƒ n|jƒ j| ƒ t ƒ �r|j!|d ƒ |tjk�rØ|j"tj#ƒ ttj#ƒ}
n6|tjk�rü|j"tj$ƒ ttj$ƒ}
n|j"|ƒ t|ƒ}
|j%ƒ  n@|tjk�r0ttj#ƒ}
n |tjk�rHttj$ƒ}
nt|ƒ}
|j&ƒ  tj'ƒ }t ƒ �rt|j(|ƒ n
|j)|ƒ tj*|d |d  f|
ƒ}|j+|ƒ |d dk�rº|d f}tj||ƒ}|S )a"  Converts a VTK data array to a numpy array.

    Given a subclass of vtkDataArray, this function returns an
    appropriate numpy array containing the same data.  The function
    is very efficient since it uses the VTK imaging pipeline to
    convert the data.  If a sufficiently new version of VTK (5.2) is
    installed then it actually uses the buffer interface to return a
    view of the VTK array in the returned numpy array.

    Parameters
    ----------

    - vtk_array : `vtkDataArray`

      The VTK data array to be converted.

    zUnsupported array type %sr   r   N)r   ),rf   rV   Úkeysr   rl   rm   rX   r   r   r6   r.   r   ÚprodÚreshaper   r   rT   Ú
frombufferr   r\   ÚvtkImageDataÚSetDimensionsÚvtkCharArrayÚDeepCopyÚGetPointDataÚ
SetScalarsrM   ÚvtkLongArrayrh   rg   rn   ro   r   ÚSetNumberOfScalarComponentsÚSetScalarTyperP   r=   ÚUpdateÚModifiedÚvtkImageExportÚSetInputÚSetInputDataÚemptyÚExport)rq   Útypr   r   ÚarrÚresultZimg_dataZiarrZncrs   Zr_dtypeÚexpZim_arrr   r   r   Ú	vtk2arrayu  s€    











r�   c             C   s  |r
|}nt jƒ }|jƒ dks&tdƒ‚t| ƒdkr6|S dd„ }dd„ }dd	„ }d
}tt| ƒttfƒ�r‚t| d ƒdks~tdƒ‚t| d ƒ}t|tƒr¢|| |ƒ |S t|t	j
ƒ�rxd}d}	xL| D ]D}
t|
jƒdksØtdƒ‚|
j}||d |d d  7 }|	|d 7 }	q¾W t	j|ftƒ}d}xN| D ]F}
||
ƒ}|
j}|d |d d  }t||||| … ƒ ||7 }�qW |||	|ƒ |S t|ƒ‚n|tt| ƒt	j
ƒ�röt| jƒdk�s¬tdƒ‚|| ƒ}|j}t	j|d |d d  ftƒ}t||ƒ |||d |ƒ |S t|ƒ‚dS )a`  Given a nested Python list or a numpy array, this method
    creates a vtkCellArray instance and returns it.

    A variety of input arguments are supported as described in the
    Parameter documentation.  If numpy arrays are given, this method
    is highly efficient.  This function is most efficient if the
    passed numpy arrays have a typecode `ID_TYPE_CODE`.  Otherwise a
    typecast is necessary and this involves an extra copy.  This
    method *always copies* the input data.

    An alternative and more efficient way to build the connectivity
    list is to create a vtkIdTypeArray having data of the form
    (npts,p0,p1,...p(npts-1), repeated for each cell) and then call
    <vtkCellArray_instance>.SetCells(n_cell, id_list).

    Parameters
    ----------

    - num_array : numpy array or Python list/tuple

      Valid values are:

        1. A Python list of 1D lists.  Each 1D list can contain one
           cell connectivity list.  This is very slow and is to be
           used only when efficiency is of no consequence.

        2. A 2D numpy array with the cell connectivity list.

        3. A Python list of 2D numpy arrays.  Each numeric array can
           have a different shape.  This makes it easy to generate a
           cell array having cells of different kinds.

    - vtk_array : `vtkCellArray` (default: `None`)

      If an optional `vtkCellArray` instance, is passed as an argument
      then a new array is not created and returned.  The passed array
      is itself modified and returned.

    Example
    -------

       >>> a = [[0], [1, 2], [3, 4, 5], [6, 7, 8, 9]]
       >>> cells = array_handler.array2vtkCellArray(a)
       >>> a = numpy.array([[0,1,2], [3,4,5], [6,7,8]], 'l')
       >>> cells = array_handler.array2vtkCellArray(a)
       >>> l_a = [a[:,:1], a[:2,:2], a]
       >>> cells = array_handler.array2vtkCellArray(l_a)

    ÚvtkCellArrayz2Second argument must be a `vtkCellArray` instance.r   c             S   sL   |j ƒ  tjƒ }x6| D ].}|j ƒ  x|D ]}|j|ƒ q(W |j|ƒ qW d S )N)ÚResetr\   Ú	vtkIdListÚInsertNextIdÚInsertNextCell)rr   ÚcellsZvtk_idsrs   Újr   r   r   Ú_slow_array2cells  s    

z-array2vtkCellArray.<locals>._slow_array2cellsc             S   s4   yt j| tƒ}W n tk
r.   | jtƒ}Y nX |S )N)r   rd   rL   rQ   rj   )rŠ   Útmp_arrr   r   r   Ú_get_tmp_array&  s
    z*array2vtkCellArray.<locals>._get_tmp_arrayc             S   s"   t jƒ }t||ƒ | j||ƒ d S )N)r\   ÚvtkIdTypeArrayrt   ÚSetCells)r“   Ún_cellsÚ
id_typ_arrr#   r   r   r   Ú
_set_cells-  s    
z&array2vtkCellArray.<locals>._set_cellszkInvalid argument.  Valid types are a Python list of lists, a Python list of numpy arrays, or a numpy array.zInput array must be 2D.r   zEach array must be 2Dr   N)r\   rŽ   ÚGetClassNamer   r   Ú
issubclassÚtypeÚlistÚtupler   Úndarrayr   r‡   rL   r   rQ   )rp   rq   r“   r•   r—   rœ   ÚmsgÚtpZtot_sizerš   rŠ   r   r›   Úcountr–   r   r   r   r   Úarray2vtkCellArrayß  sZ    2	





r¦   c             C   s^   |r
|}nt jƒ }tj| ƒ}t|jƒdks2tdƒ‚|jd dksHtdƒ‚t|ƒ}|j|ƒ |S )a9  Converts a numpy array/Python list to a vtkPoints object.

    Unless a Python list/tuple or a non-contiguous array is given, no
    copy of the data is made.  Thus the function is very efficient.

    Parameters
    ----------

    - num_array : numpy array or Python list/tuple

      The input array must be 2D with `shape[1] == 3`.

    - vtk_points : `vtkPoints` (default: `None`)

      If an optional `vtkPoints` instance, is passed as an argument
      then a new array is not created and returned.  The passed array
      is itself modified and returned.

    r   z#Points array must be 2 dimensional.r   rc   z$Incorrect shape: shape[1] must be 3.)	r\   Ú	vtkPointsr   rd   r   r   r   rt   ÚSetData)rp   Z
vtk_pointsÚpointsrŠ   rq   r   r   r   Úarray2vtkPoints`  s    

rª   c             C   sf   |r
|}nt jƒ }tj| ƒ}t|jƒdks2tdƒ‚|jt|ƒƒ x t|ƒD ]\}}|j	||ƒ qJW |S )a®  Converts a numpy array/Python list to a vtkIdList object.

    Parameters
    ----------

    - num_array : numpy array or Python list/tuple

      The input array must be 2D with `shape[1] == 3`.

    - vtk_idlist : `vtkIdList` (default: `None`)

      If an optional `vtkIdList` instance, is passed as an argument
      then a new array is not created and returned.  The passed array
      is itself modified and returned.

    r   zArray for vtkIdList must be 1D)
r\   r�   r   rd   r   r   r   ÚSetNumberOfIdsÚ	enumerateÚSetId)rp   Z
vtk_idlistÚidsrŠ   rs   r”   r   r   r   Úarray2vtkIdList�  s    
r¯   c             C   s   t t| ƒtjtfƒrdS dS )z<Returns True if the passed `arr` is a numpy array or a List.TF)rž   rŸ   r   r¢   r    )rŠ   r   r   r   Úis_array£  s    r°   c             C   sŒ   |r€t ttdœ}||jƒ kr6tt|ƒƒ }|| | |ƒS |jdƒdkrzytt|ƒƒ }W n tk
rl   t| ƒS X t| |ƒS qˆ| S nt| ƒS dS )zêConvert the given array to the optional type specified by
    `vtk_typ`.

    Parameters
    ----------

    - arr : numpy array/list.
    - vtk_typ : `string` or `None`
      represents the type the array is to be converted to.

    )rŽ   r§   r�   ÚArrayr   Néÿÿÿÿ)	r¦   rª   r¯   ru   r
   r\   ÚfindrQ   rt   )rŠ   Zvtk_typÚconvr#   r   r   r   Úconvert_arrayª  s    
rµ   c             C   s@   t | ttfƒsdS dddg}x|D ]}| j|ƒdkr"dS q"W dS )z8Given a signature, return if the signature has an array.Fr±   r§   r�   r   Tr²   )Ú
isinstanceÚunicodeÚstrr³   )ÚsZ	arr_typesrs   r   r   r   Úis_array_sigÊ  s    

rº   c             C   s,   t | ƒrdS t| dƒr(t| jjjƒr(dS dS )zQReturns True if the argument is an array/Python list or if it
    is a vtk array.TÚ_vtk_objF)r°   Úhasattrrº   r»   Ú	__class__r0   )Úargr   r   r   Úis_array_or_vtkarrayÕ  s    
r¿   c                sð   |dkrdS t |ƒdkr |d S t | ƒ‰ ‡ fdd„|D ƒ}t |ƒ}|dkr\d| }t|ƒ‚n�|dkrl|d S dd„ t| ƒD ƒ}t |ƒ}|dkr’dS xT|D ]L}dd„ |D ƒ}|jd	ƒt |ƒkr¾q˜d
}	x|D ]}
||
 sÈd	}	qÈW |	s˜|S q˜W dS dS )zïGiven a list of args and a collection of possible signatures,
    this function returns the most appropriate signature.  This
    function is only called by deref_array.  This implies that one of
    the signatures has an array type.

    Nr   r   c                s   g | ]}t |ƒˆ kr|‘qS r   )r   )Ú.0r¹   )Úlar   r   ú
<listcomp>ñ  s    z#get_correct_sig.<locals>.<listcomp>zBInsufficient number of arguments to method.Valid arguments are:
%sc             S   s   g | ]\}}t |ƒr|‘qS r   )r¿   )rÀ   rs   Úar   r   r   rÂ   ÿ  s    c             S   s   g | ]}t |ƒ‘qS r   )rº   )rÀ   r¹   r   r   r   rÂ     s    TF)r   rQ   r¬   r¥   )ÚargsÚsigsZcandidate_sigsr¥   r£   Z	array_idxZn_arrÚsigZarray_in_sigÚbadrs   r   )rÁ   r   Úget_correct_sigâ  s8    


rÈ   c             C   s   t | dƒr| jS | S dS )z±Dereferences the VTK object from the object if possible.  This
    is duplicated from `tvtk_base.py` because I'd like to keep this
    module independent of `tvtk_base.py`.
    r»   N)r¼   r»   )Úobjr   r   r   Ú	deref_vtk  s    
rÊ   c             C   s”   g }t | |ƒ}|r\x|t| |ƒD ]8\}}t|ƒrHt|ƒrH|jt||ƒƒ q|jt|ƒƒ qW n4x2| D ]*}t|ƒr~|jt|ƒƒ qb|jt|ƒƒ qbW |S )a#  Given a bunch of arguments and optional signature information,
    this converts the arguments suitably.  If the argument is either a
    Python list or a numpy array it is converted to a suitable type
    based on the signature information.  If it is not an array, but a
    TVTK object the VTK object is dereferenced.  Otherwise nothing is
    done.  If no signature information is provided the arrays are
    automatically converted (this can sometimes go wrong).  The
    signature information is provided in the form of a list of lists.

    )rÈ   Úzipr°   rº   Úappendrµ   rÊ   )rÄ   rÅ   ÚretrÆ   rÃ   r¹   r   r   r   Úderef_array#  s    

rÎ   )r4   r5   )N)N)N)N)N)N):r3   Úsysr\   Zvtk.utilr   r   ÚImportErrorr   Ztvtk.array_extr   ZHAS_ARRAY_EXTZtvtk.commonr   r˜   ÚGetDataTypeSizerZ   r@   rL   Úint64r   rY   rO   rB   rN   rD   Ú	vtkObjectÚGetReferenceCountr`   Úversion_infor¸   r·   r	   r   Úobjectr   Z_dummyÚnameÚmodulesÚmodr¼   r6   rS   rV   rX   r[   rb   rt   r�   r¦   rª   r¯   r°   rµ   rº   r¿   rÈ   rÊ   rÎ   r   r   r   r   Ú<module>   s€   

C

%
cj
 
!
"
 6