a
    «ˆc‰.  ã                   @   sÚ   d Z ddlZddlZddlZddl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 dd	lmZ dd
lmZmZmZ edƒZd dd„Zd!dd„Zd"dd„Zd#dd„Zd$dd„Zd%dd„Zdd„ Zd&dd„ZdS )'z;
Functions related to creating the engine or the figures.

é    N)Údo_later)Útvtk)Úvtk_major_version)ÚScene)Úregistryé   )Úview)Ú
get_engineÚoptionsÚ
set_engine)r   ©i�  i^  c                 C   s„  t | tƒr>| jdu r t | ¡}nt | j¡}t|ƒ | |_�n|du rLtƒ }| du rŒtt	ƒd }t	 
|f¡ d| }|j||d� ||j_n‚t| ƒttjtjtjtjtjfv rÌt| ƒ}t	 
|f¡ d| }nt| ƒ}|jD ]}|j|krÚ||_|  S qÚ|j||d� ||j_|j} | j}|du�rDt|dƒ�r:| ¡  n
tddƒ | j}|du�r€|du �rdtj}||_|du �rztj}||_| S )aÝ   Creates a new scene or retrieves an existing scene. If the mayavi
    engine is not running this also starts it.

    **Keyword arguments**

        :figure: The name of the figure, or handle to it.

        :bgcolor: The color of the background (None is default).

        :fgcolor: The color of the foreground, that is the color of all text
                  annotation labels (axes, orientation axes, scalar bar
                  labels). It should be sufficiently far from `bgcolor`
                  to see the annotation texts. (None is default).

        :engine: The mayavi engine that controls the figure.

        :size: The size of the scene created, in pixels. May not apply
               for certain scene viewer.
    Nr   úMayavi Scene %d)ÚnameÚsizeÚisometric_viewé(   é2   )Ú
isinstancer   Úscener   Úfind_scene_enginer   Úcurrent_scener	   ÚmaxÚ__scene_number_listÚupdateZ	new_scener   ÚtypeÚintÚnpÚint0Úint8Úint16Úint32Úint64ÚstrÚscenesÚhasattrr   r   r
   Zbackground_colorZ
backgroundZforeground_colorZ
foreground)ÚfigureZbgcolorZfgcolorÚenginer   r   r   © r'   úV/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/mayavi/tools/figure.pyr%   "   sT    



ÿ









r%   c                 C   s*   | du rt ƒ } | j}|du r&t| d�S |S )z¦Return a handle to the current figure.

    You can supply the engine from which you want to retrieve the
    current figure, if you have several mayavi engines.
    N)r&   )r	   r   r%   )r&   r   r'   r'   r(   Úgcfk   s    
r)   c                 C   s|   z\| du rt ƒ }n| }|jdur0|jj}d|j_g |jdd…< |j ¡  |jdurZ||j_W n tyn   Y n0 t ¡  dS )zZClear the current figure.

    You can also supply the figure that you want to clear.
    NT)	r)   r   Údisable_renderÚchildrenZ_mouse_pick_dispatcherZclear_callbacksÚAttributeErrorÚgcZcollect)r%   r   r*   r'   r'   r(   Úclfy   s    


r.   Fc                 C   sè   |du r.t ƒ }t |j¡D ]} | | ¡ qdS t| tƒs¸t ƒ }| du rN|j} qÚt| ƒtt	j
t	jt	jt	jt	jfv r‚t| ƒ} d|  }nt| ƒ}|jD ]} | j|kr� qÚq�t d| ¡ dS n"| jdu rÎt | ¡}nt | j¡}| | ¡ dS )aC   Close a figure window

        close() by itself closes the current figure.

        close(num) closes figure number num.

        close(name) closes figure named name.

        close(figure), where figure is a scene instance, closes that
        figure.

        close(all=True) closes all figures controlled by mlab
    TNr   zScene %s not managed by mlab)r	   Úcopyr#   Zclose_scener   r   r   r   r   r   r   r   r   r    r!   r"   r   ÚwarningsÚwarnr   r   r   )r   Úallr&   r   r'   r'   r(   Úclose�   s0    
ÿ



r3   c                 C   s   | du rt ƒ } |  ¡  dS )z, Forces a redraw of the current figure.
    N)r)   Úrender)r%   r'   r'   r(   Údraw¾   s    r5   Úautoc           
      K   sÌ   |du rt ƒ }|jj}z¢|durzt|j ¡ ƒ\}}|\}}	|dkr†t|| |	| ƒd }t|| ƒ}t|	| ƒ}	||	f}n|dkr†d}t|ƒ|j_|jj| fd|i|¤Ž W t|ƒ|j_nt|ƒ|j_0 dS )aò   Save the current scene.
        The output format are deduced by the extension to filename.
        Possibilities are png, jpg, bmp, tiff, ps, eps, pdf, rib (renderman),
        oogl (geomview), iv (OpenInventor), wrl, vrml, obj (wavefront),
        x3d, pov (povray).

        **Parameters**

        :size: the size of the image created (unless magnification is
               set, in which case it is the size of the window used
               for rendering).

        :figure: the figure instance to save to a file.

        :magnification: the magnification is the scaling between the
                        pixels on the screen, and the pixels in the
                        file saved. If you do not specify it, it will be
                        calculated so that the file is saved with the
                        specified size. If you specify a magnification,
                        Mayavi will use the given size as a screen size,
                        and the file size will be 'magnification * size'.

        **Notes**

        If the size specified is larger than the window size, and no
        magnification parameter is passed, the magnification of the scene
        is changed so that the image created has the requested size.
        Please note that if you are trying to save images with sizes
        larger than the window size, there will be additional computation
        cost.

        Any extra keyword arguments are passed along to the respective
        image format's save method.
    Nr6   r   r   )r)   r   ÚmagnificationÚtupleÚget_sizer   r   Úsave)
Úfilenamer   r%   r7   ÚkwargsZcurrent_magZ	current_xZ	current_yZtarget_xZtarget_yr'   r'   r(   ÚsavefigÆ   s2    $ÿÿ
ÿþr=   c                    s0   | j j dˆ j j¡ ˆ j jj ‡ fdd„¡ dS )z` Synchronise the camera of the target_figure on the camera of the
        reference_figure.
    Úactive_camerac                      s   t ˆ jjƒS )N)r   r   r4   r'   ©Útarget_figurer'   r(   Ú<lambda>	  ó    zsync_camera.<locals>.<lambda>N)r   Z	_rendererZ
sync_traitr>   Zon_trait_change)Zreference_figurer@   r'   r?   r(   Úsync_camera  s    ÿ

ÿrC   Úrgbc                 C   sŽ  | du rt ƒ } t| jjjƒ\}}| j ¡  |dkrŒt ¡ }||df}| jjj}t	dkrrdd|d |d d|df}qödd|d |d d|f}nj|dkrît 
¡ }||df}| jjj}t	dkrÔdd|d |d d|df}qödd|d |d d|f}ntd	ƒ‚|�rj| jj}	t|	d
ƒ�r"|	j}
| jj|	_n|	j}
| jj|	_| j ¡  ||Ž  t|	d
ƒ�rX|
|	_n|
|	_| j ¡  n||Ž  | ¡ }||_t |¡}|S )aW   Return the current figure pixmap as an array.

        **Parameters**

        :figure: a figure instance or None, optional
            If specified, the figure instance to capture the view of.
        :mode: {'rgb', 'rgba'}
            The color mode of the array captured.
        :antialiased: {True, False}
            Use anti-aliasing for rendering the screenshot.
            Uses the number of aa frames set by
            figure.scene.anti_aliasing_frames

        **Notes**

        On most systems, this works similarly to taking a screenshot of
        the rendering window. Thus if it is hidden by another window, you
        will capture the other window. This limitation is due to the
        heavy use of the hardware graphics system.

        **Examples**

        This function can be useful for integrating 3D plotting with
        Mayavi in a 2D plot created by matplotlib.

        >>> from mayavi import mlab
        >>> mlab.test_plot3d()
        >>> arr = mlab.screenshot()
        >>> import pylab as pl
        >>> pl.imshow(arr)
        >>> pl.axis('off')
        >>> pl.show()

    NrD   é   é   r   r   Zrgbaé   zmode type not understoodÚ	aa_frames)r)   r8   r   Úrender_windowr   Z_liftr   ZUnsignedCharArrayZget_pixel_datar   Z
FloatArrayZget_rgba_pixel_dataÚ
ValueErrorr$   rH   Zanti_aliasing_framesZmulti_samplesr4   Zto_arrayÚshaper   Zflipud)r%   ÚmodeZantialiasedÚxÚyÚoutrK   Zpixel_getterZpg_argsrI   Zold_aar'   r'   r(   Ú
screenshot  sJ    #







rP   )NNNNr   )N)N)NF)N)NNr6   )NrD   F) Ú__doc__r-   r0   r/   Únumpyr   Zpyface.timer.apir   Ztvtk.apir   Ztvtk.commonr   Zmayavi.core.scener   Zmayavi.core.registryr   Zcamerar   Zengine_managerr	   r
   r   Úsetr   r%   r)   r.   r3   r5   r=   rC   rP   r'   r'   r'   r(   Ú<module>   s,   
  ÿ
I


/

;