
    tj#                     n   d Z ddlmZ ddlmZmZmZ ddlZ e       j                         Z	e	dk(  rej                  Zne	dk(  rej                  Z e       j                         Zedk(  rej                  Zej                  Znedk(  rej                  Zej"                  Zd Zd	 Zd
 Zd ZddZddZd Zy)a  This module adds support to easily import and export NumPy
(http://numpy.scipy.org) arrays into/out of VTK arrays.  The code is
loosely based on TVTK (https://svn.enthought.com/enthought/wiki/TVTK).

This code depends on an addition to the VTK data arrays made by Berk
Geveci to make it support Python's buffer protocol (on Feb. 15, 2008).

The main functionality of this module is provided by the two functions:
    numpy_to_vtk,
    vtk_to_numpy.


Caveats:
--------

 - Bit arrays in general do not have a numpy equivalent and are not
   supported.  Char arrays are also not easy to handle and might not
   work as you expect.  Patches welcome.

 - You need to make sure you hold a reference to a Numpy array you want
   to import into VTK.  If not you'll get a segfault (in the best case).
   The same holds in reverse when you convert a VTK array to a numpy
   array -- don't delete the VTK array.


Created by Prabhu Ramachandran in Feb. 2008.
   )vtkConstants    )vtkDataArrayvtkIdTypeArrayvtkLongArrayN      c                    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}|j1                         D ]<  \  }}| |k(  s/t        j2                  | |      s| t        j4                  |      k(  s:|c S  t7        dt9        |       z        )z+Returns a VTK typecode given a numpy array.z)Could not find a suitable VTK type for %s)numpyuint8r   VTK_UNSIGNED_CHARuint16VTK_UNSIGNED_SHORTuint32VTK_UNSIGNED_INTuint64VTK_UNSIGNED_LONG_LONGint8VTK_SIGNED_CHARint16	VTK_SHORTint32VTK_INTint64VTK_LONG_LONGfloat32	VTK_FLOATfloat64
VTK_DOUBLE	complex64
complex128items
issubdtypedtype	TypeErrorstr)numpy_array_type_np_vtkkeyvtk_types       J/DATA/.local/lib/python3.12/site-packages/vtkmodules/util/numpy_support.pyget_vtk_array_typer,   1   s%    {{<99\<<\::\@@

<77L22L00L66l44l55 6 6  !8!8:G !Xs",c2u{{3//O	 )
 3s;K7LMO O    c                  R   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"        t         j$                  t        j&                  t         j(                  t*        t         j,                  t        j.                  t         j0                  t2        t         j4                  t        j6                  t         j8                  t        j:                  i} | S )z7Returns the VTK array type to numpy array type mapping.)r   VTK_BITr   r   VTK_CHARr   r   r   r   r   r   r   r   r   r   r   VTK_LONGLONG_TYPE_CODEr   r   VTK_UNSIGNED_LONGULONG_TYPE_CODEr   r   VTK_ID_TYPEID_TYPE_CODEr   r   r   r   )_vtk_nps    r+   get_vtk_to_numpy_typemapr8   H   s    ##EKK%%ejj,,UZZ..u{{&&u{{//$$U[[--ell%%n**5;;..33ELL((&&u}}''7G Nr-   c                     t               |    S )z6Returns a numpy array typecode given a VTK array type.)r8   )vtk_array_types    r+   get_numpy_array_typer;   \   s    #%n55r-   c                 ,    t        j                  |       S )zkInternal function used to create a VTK data array from another
    VTK array given the VTK array type.
    )r   CreateDataArray)vtk_arr_types    r+   create_vtk_arrayr?   a   s     ''55r-   c                 2   t        j                  |       }|j                  j                  st        j                  |      }|j
                  }|j                  j                  sJ d       t        |      dk  sJ d       t        j                  |j                  t        j                  t              j                        rJ d       |r|}nt        |j                        }t        |      }	 |d   }t        |      dk(  r|j                  d       n|j                  |d          |j                  |d          t        |      }t        j                  |j                  |      s"|j                  t        j                  |      k(  rt        j                   |      }	n&t        j                   |      j#                  |      }	d}|j%                  |	t        |	      d       |r%|j'                         }
|
j)                  |       |
}|S ||_        |S #  d}Y $xY w)a=  Converts a real numpy Array 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.

    If the second argument is set to 1, the array is deep-copied from
    from numpy. This is not as efficient as the default behavior
    (shallow copy) and uses more memory but detaches the two arrays
    such that the numpy array can be released.

    WARNING: You must maintain a reference to the passed numpy array, if
    the numpy data is gc'd and VTK will point to garbage which will in
    the best case give you a segfault.

    Parameters:

    num_array
      a 1D or 2D, real numpy array.

    z%Only contiguous arrays are supported.   z5Only arrays of dimensionality 2 or lower are allowed!zComplex numpy arrays cannot be converted to vtk arrays.Use real() or imag() to get a component of the array before passing it to vtk.r   r   r   )r   asarrayflags
contiguousascontiguousarrayshapelenr#   r$   complextyper,   r?   SetNumberOfComponentsSetNumberOfTuplesr;   ravelastypeSetVoidArrayNewInstanceDeepCopy_numpy_reference)	num_arraydeep
array_typezrG   vtk_typecoderesult_arraytestVar	arr_dtypez_flatcopys              r+   numpy_to_vtkr]   h   s   2 	i A77##A&GGE77FFFu:> CBC>W)=)B)BC !!!C !)!''2#L1L(
 5zQ**1-**584""58, %\2I+ww%++i((QQ&&y1  fc&k15'')l#  )*%As   -H Hc                 2   t               j                         }| j                  }|dk(  r*|t        j                  k7  rAt        dt        |      z        |t        j                  k7  rt        dt        |      z        t        | |t        j                        S )Nr   z.Expecting a numpy.int32 array, got %s instead.z.Expecting a numpy.int64 array, got %s instead.)r   GetDataTypeSizer$   r   r   
ValueErrorr&   r   r]   r   r5   )rS   rT   isizer$   s       r+   numpy_to_vtkIdTypeArrayrb      s    ,,.EOOEzEKK=ULN N EKK=ULN N 	4)A)ABBr-   c                    | j                         }|t               j                         v s
J d|z         |t        j                  k7  sJ d       | j                         | j                         f}t        |      }	 t        j                  | |      }|d   dk(  r|d   f}	 ||_        |S # t        $ r$ |d   dk(  rt        j                  ||      }n Y Dw xY w# t        $ r$ |d   dk(  rt        j                  ||      }Y |S  w xY w)aL  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 -- it actually
    points to the same data.

    WARNING: This does not work for bit arrays.

    Parameters

    vtk_array
      The VTK data array to be converted.

    zUnsupported array type %szBit arrays are not supported.)r$   r   r   )GetDataTyper8   keysr   r/   GetNumberOfTuplesGetNumberOfComponentsr;   r   
frombufferr`   emptyrG   )	vtk_arraytyprG   r$   results        r+   vtk_to_numpyrm      s0    


!C*,1133 +&s*+3,&&&G(GG&'')++-.E !%E!!)59 Qx1}q M+  	
 8q=[[e4F 	  8q= KKU36M s$   ;B* !C **CC(DD)r   NrB   )__doc__ r   vtkmodules.vtkCommonCorer   r   r   r   r_   VTK_ID_TYPE_SIZEr   r6   r   VTK_LONG_TYPE_SIZEr2   r   r4   r   r,   r8   r;   r?   r]   rb   rm    r-   r+   <module>rt      s   8  O O  "#335 q;;L;;L!^335 [[NllO1[[NllOO.(6
6PdC0r-   