o
    oh(4                     @   s   d dl mZ zd dlmZ W n ey   edw 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 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ZeddG dd dZ G dd dZ!dS )    )RLockNz;pyglet is required for plotting.
 visit https://pyglet.org/)Integer)
SYMPY_INTS)GeometryEntity)PlotAxes)PlotMode)
PlotObject)
PlotWindow)parse_option_string)doctest_depends_on)is_sequence)sleepgetcwdlistdirpygletmodulesc                   @   s   e Zd ZdZedddd Zdd Zdd	 Zd'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!d" Zd#d$ Zd%d& Zd
S )(
PygletPlotag  
    Plot Examples
    =============

    See examples/advanced/pyglet_plotting.py for many more examples.

    >>> from sympy.plotting.pygletplot import PygletPlot as Plot
    >>> from sympy.abc import x, y, z

    >>> Plot(x*y**3-y*x**3)
    [0]: -x**3*y + x*y**3, 'mode=cartesian'

    >>> p = Plot()
    >>> p[1] = x*y
    >>> p[1].color = z, (0.4,0.4,0.9), (0.9,0.4,0.4)

    >>> p = Plot()
    >>> p[1] =  x**2+y**2
    >>> p[2] = -x**2-y**2


    Variable Intervals
    ==================

    The basic format is [var, min, max, steps], but the
    syntax is flexible and arguments left out are taken
    from the defaults for the current coordinate mode:

    >>> Plot(x**2) # implies [x,-5,5,100]
    [0]: x**2, 'mode=cartesian'
    >>> Plot(x**2, [], []) # [x,-1,1,40], [y,-1,1,40]
    [0]: x**2, 'mode=cartesian'
    >>> Plot(x**2-y**2, [100], [100]) # [x,-1,1,100], [y,-1,1,100]
    [0]: x**2 - y**2, 'mode=cartesian'
    >>> Plot(x**2, [x,-13,13,100])
    [0]: x**2, 'mode=cartesian'
    >>> Plot(x**2, [-13,13]) # [x,-13,13,100]
    [0]: x**2, 'mode=cartesian'
    >>> Plot(x**2, [x,-13,13]) # [x,-13,13,10]
    [0]: x**2, 'mode=cartesian'
    >>> Plot(1*x, [], [x], mode='cylindrical')
    ... # [unbound_theta,0,2*Pi,40], [x,-1,1,20]
    [0]: x, 'mode=cartesian'


    Coordinate Modes
    ================

    Plot supports several curvilinear coordinate modes, and
    they independent for each plotted function. You can specify
    a coordinate mode explicitly with the 'mode' named argument,
    but it can be automatically determined for Cartesian or
    parametric plots, and therefore must only be specified for
    polar, cylindrical, and spherical modes.

    Specifically, Plot(function arguments) and Plot[n] =
    (function arguments) will interpret your arguments as a
    Cartesian plot if you provide one function and a parametric
    plot if you provide two or three functions. Similarly, the
    arguments will be interpreted as a curve if one variable is
    used, and a surface if two are used.

    Supported mode names by number of variables:

    1: parametric, cartesian, polar
    2: parametric, cartesian, cylindrical = polar, spherical

    >>> Plot(1, mode='spherical')


    Calculator-like Interface
    =========================

    >>> p = Plot(visible=False)
    >>> f = x**2
    >>> p[1] = f
    >>> p[2] = f.diff(x)
    >>> p[3] = f.diff(x).diff(x)
    >>> p
    [1]: x**2, 'mode=cartesian'
    [2]: 2*x, 'mode=cartesian'
    [3]: 2, 'mode=cartesian'
    >>> p.show()
    >>> p.clear()
    >>> p
    <blank plot>
    >>> p[1] =  x**2+y**2
    >>> p[1].style = 'solid'
    >>> p[2] = -x**2-y**2
    >>> p[2].style = 'wireframe'
    >>> p[1].color = z, (0.4,0.4,0.9), (0.9,0.4,0.4)
    >>> p[1].style = 'both'
    >>> p[2].style = 'both'
    >>> p.close()


    Plot Window Keyboard Controls
    =============================

    Screen Rotation:
        X,Y axis      Arrow Keys, A,S,D,W, Numpad 4,6,8,2
        Z axis        Q,E, Numpad 7,9

    Model Rotation:
        Z axis        Z,C, Numpad 1,3

    Zoom:             R,F, PgUp,PgDn, Numpad +,-

    Reset Camera:     X, Numpad 5

    Camera Presets:
        XY            F1
        XZ            F2
        YZ            F3
        Perspective   F4

    Sensitivity Modifier: SHIFT

    Axes Toggle:
        Visible       F5
        Colors        F6

    Close Window:     ESCAPE

    =============================

    r   r   c                 O   s   ddl m} || _d| _t | _i | _g | _t| | _	t
|dd}td	i || _| j| j || d< |ddrB|   dS dS )
a  
        Positional Arguments
        ====================

        Any given positional arguments are used to
        initialize a plot function at index 1. In
        other words...

        >>> from sympy.plotting.pygletplot import PygletPlot as Plot
        >>> from sympy.abc import x
        >>> p = Plot(x**2, visible=False)

        ...is equivalent to...

        >>> p = Plot(visible=False)
        >>> p[1] = x**2

        Note that in earlier versions of the plotting
        module, you were able to specify multiple
        functions in the initializer. This functionality
        has been dropped in favor of better automatic
        plot plot_mode detection.


        Named Arguments
        ===============

        axes
            An option string of the form
            "key1=value1; key2 = value2" which
            can use the following options:

            style = ordinate
                none OR frame OR box OR ordinate

            stride = 0.25
                val OR (val_x, val_y, val_z)

            overlay = True (draw on top of plot)
                True OR False

            colored = False (False uses Black,
                             True uses colors
                             R,G,B = X,Y,Z)
                True OR False

            label_axes = False (display axis names
                                at endpoints)
                True OR False

        visible = True (show immediately
            True OR False


        The following named arguments are passed as
        arguments to window initialization:

        antialiasing = True
            True OR False

        ortho = False
            True OR False

        invert_mouse_zoom = False
            True OR False

           )
plot_modesNaxes r   visibleT )r   r   	_win_args_windowr   _render_lock
_functions	_pobjects
ScreenShot_screenshotr
   popr   r   appendgetshow)selffargswin_argsr   axe_optionsr   r   r/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/sympy/plotting/pygletplot/plot.py__init__   s   F
zPygletPlot.__init__c                 C   sH   | j r| j js| j   dS d| jd< | j  t| fi | j| _ dS )z~
        Creates and displays a plot window, or activates it
        (gives it focus) if it has already been created.
        Tr   N)r   has_exitactivater   r   reset_resourcesr	   r'   r   r   r+   r&      s
   

zPygletPlot.showc                 C   s   | j r
| j   dS dS )z)
        Closes the plot window.
        N)r   closer0   r   r   r+   r1     s   zPygletPlot.closeNr   iX  i  c                 C   s   | j ||| dS )aK  
        Saves a screen capture of the plot window to an
        image file.

        If outfile is given, it can either be a path
        or a file object. Otherwise a png image will
        be saved to the current working directory.
        If the format is omitted, it is determined from
        the filename extension.
        N)r"   saver'   outfileformatsizer   r   r+   	saveimage  s   zPygletPlot.saveimagec                 C   s&   | j   i | _|   | j   dS )z8
        Clears the function list of this plot.
        Nr   acquirer   adjust_all_boundsreleaser0   r   r   r+   clear  s   
zPygletPlot.clearc                 C   s
   | j | S )zR
        Returns the function at position i in the
        function list.
        )r   r'   ir   r   r+   __getitem__&  s   
zPygletPlot.__getitem__c                 C   s   t |ttfr|dkstdt |tr|}n t|r t |tr#|g}t|dkr+dS d| ji}t	|i |}|rJ| j
  || j|< | j
  dS tdddd |D  )	zJ
        Parses and adds a PlotMode to the function
        list.
        r   z'Function index must be an integer >= 0.Nbounds_callbackzFailed to parse '%s'.z, c                 s   s    | ]}t |V  qd S Nstr).0ar   r   r+   	<genexpr>F  s    z)PygletPlot.__setitem__.<locals>.<genexpr>)
isinstancer   r   
ValueErrorr   r   r   lenr;   r   r   r:   r   r<   join)r'   r?   argsfkwargsr   r   r+   __setitem__-  s"   



zPygletPlot.__setitem__c                 C   s(   | j   | j|= |   | j   dS )zR
        Removes the function in the function list at
        position i.
        Nr9   r>   r   r   r+   __delitem__H  s   
zPygletPlot.__delitem__c                 C   s8   d}| j   || jv r|d7 }|| jv s| j   |S )zF
        Returns the first unused index in the function list.
        r   r   )r   r:   r   r<   r>   r   r   r+   firstavailableindexR  s   



zPygletPlot.firstavailableindexc                 G   s   |  |  | dS )zg
        Parses and adds a PlotMode to the function
        list at the first available index.
        N)rO   rQ   )r'   rL   r   r   r+   r$   ]  s   zPygletPlot.appendc                 C   s
   t | jS )zG
        Returns the number of functions in the function list.
        )rJ   r   r0   r   r   r+   __len__d     
zPygletPlot.__len__c                 C   s
   | j  S )z8
        Allows iteration of the function list.
        )r   
itervaluesr0   r   r   r+   __iter__j  rS   zPygletPlot.__iter__c                 C   s   t | S rB   rC   r0   r   r   r+   __repr__p  s   zPygletPlot.__repr__c                    sT   d}t  jdkr|d7 }|S  j  |d fdd jD 7 } j  |S )zv
        Returns a string containing a new-line separated
        list of the functions in the function list.
        r   r   z<blank plot>
c                    s$   g | ]}d d|t  j| f qS )z
%s[%i]: %sr   )rD   r   )rE   r?   r0   r   r+   
<listcomp>}  s    z&PygletPlot.__str__.<locals>.<listcomp>)rJ   r   r   r:   rK   r<   )r'   sr   r0   r+   __str__s  s   


zPygletPlot.__str__c                 C   sB   | j   | j  | jD ]}| j| j| j q| j   d S rB   )r   r:   r   reset_bounding_boxr   adjust_boundsboundsr<   )r'   rM   r   r   r+   r;     s
   


zPygletPlot.adjust_all_boundsc                 C   sd   t d | j  | jD ]}| j| j}| j| j}| s | r*t d | s | s q| j  d S )Nr   )r   r   r:   r   _get_calculating_verts_get_calculating_cvertsr<   )r'   rM   rF   br   r   r+   wait_for_calculations  s   

z PygletPlot.wait_for_calculationsNr   r2   )__name__
__module____qualname____doc__r   r,   r&   r1   r8   r=   r@   rO   rP   rQ   r$   rR   rU   rV   rZ   r;   ra   r   r   r   r+   r      s(     
Z
	
r   c                   @   s6   e Zd Zdd Zdd Zdd Zdd
dZdd ZdS )r!   c                 C   s(   || _ d| _d | _d| _d| _d| _d S )NFr   r   )_plotscreenshot_requestedr5   r6   invisibleModeflag)r'   plotr   r   r+   r,     s   
zScreenShot.__init__c                 C   s   | j S rB   )rh   r0   r   r   r+   __bool__  s   zScreenShot.__bool__c              	   C   s   | j dk r|  j d7  _ d S | jj \}}|| d ttj }t|}t	dd||tj
tj| ddlm} |d||f|jdddd}||j| j| j d| _ d| _| jre| jj  d S d S )	N   r      r   )ImageRGBArawF)rj   rg   r   get_sizectypessizeofc_ubytecreate_string_bufferpglglReadPixelsGL_RGBAGL_UNSIGNED_BYTEPILro   
frombufferrq   	transposeFLIP_TOP_BOTTOMr3   r5   r6   rh   ri   r1   )r'   size_xsize_yr7   imagero   imr   r   r+   _execute_saving  s"   

zScreenShot._execute_savingNr   r2   c                 C   s   || _ || _|| _d| _| jjr| jjjrAd| jjd< |d | jjd< |d | jjd< | jj	  t
| jfi | jj| j_d| _| j d u rR|  | _ t| j  d S d S )NTFr   r   widthr   height)r5   r6   r7   rh   rg   r   r-   r   r   r/   r	   ri   _create_unique_pathprintr4   r   r   r+   r3     s   

zScreenShot.savec                 C   s@   t  }t|}d}d}	 d| |vr|d|  }	 |S |d7 }q)Nr   r   Tzplot_%s.pngz/plot_%s.pngr   r   )r'   cwdlpathr?   r   r   r+   r     s   zScreenShot._create_unique_pathrb   )rc   rd   re   r,   rl   r   r3   r   r   r   r   r+   r!     s    
r!   )"	threadingr   	pyglet.glglrw   ImportErrorsympy.core.numbersr   sympy.external.gmpyr   sympy.geometry.entityr   #sympy.plotting.pygletplot.plot_axesr   #sympy.plotting.pygletplot.plot_moder   %sympy.plotting.pygletplot.plot_objectr   %sympy.plotting.pygletplot.plot_windowr	   sympy.plotting.pygletplot.utilr
   sympy.utilities.decoratorr   sympy.utilities.iterablesr   timer   osr   r   rs   r   r!   r   r   r   r+   <module>   s0      z