U
    i2e\                     @  s   d dl mZ d dlZd dlmZmZmZ d dlmZ	 d dl
mZ d dlZd dlmZmZ d dlmZmZmZ d dlmZ erd dlmZ d dlmZ d d	lmZ d dlmZ  G d
d deZ!G dd de!Z"G dd de!Z#dS )    )annotationsN)TYPE_CHECKINGAnycast)FillTypeLineType)filled_to_mpl_pathslines_to_mpl_pathsmpl_codes_to_offsets)Renderer)Axes)Figure)	ArrayLikec                
   @  s@  e Zd ZU ded< ded< ded< dGddddddddddZddddZddddZdddddZdHdd dd!d"dd#d$d%ZdId(d(dd!d"dd"dd)d*d+Z	dJd-d.dd!d"d"dd/d0d1Z
dKd(d(d2dd!dd3d4d5ZdLd!ddd7d8d9Zd:dd;d<Zddd=d>ZdMd!dddd?d@dAZdNd(d(d(dd!d!dddDdEdFZd
S )OMplRendererr   _axesr   _figbool_want_tight   	   r   TNinttuple[float, float]z
str | Nonezdict[str, Any] | NoneNone)nrowsncolsfigsize
show_framebackendgridspec_kwreturnc                 C  s   |d k	rdd l }|| t|dddd}|d k	r<||d< ntdd|d< tj||f|\| _}	|	 | _|s| jD ]}
|
d	 qtd| _	d S )
Nr   FT)r   ZsqueezeZsharexZshareyr   equal)ZaspectZ
subplot_kwoff)

matplotlibZusedictpltZsubplotsr   flattenr   Zaxisr   )selfr   r   r   r   r   r   r#   kwargsZaxesax r*   ?/tmp/pip-unpacked-wheel-_24pu9r8/contourpy/util/mpl_renderer.py__init__'   s    	



zMplRenderer.__init__)r    c                 C  s   t | drt| j d S )Nr   )hasattrr%   closer   r'   r*   r*   r+   __del__B   s    
zMplRenderer.__del__c                 C  sL   | j D ]"}t|ddr|jdd d|_q| jrHt| j dkrH| j  d S )N_need_autoscaleFT)Ztightr   )r   getattrZautoscale_viewr1   r   lenr   Ztight_layoutr'   r)   r*   r*   r+   
_autoscaleF   s    
zMplRenderer._autoscale
Axes | int)r)   r    c                 C  s   t |tr| j| }|S N)
isinstancer   r   r4   r*   r*   r+   _get_axQ   s    

zMplRenderer._get_axr   C0ffffff?cpy.FillReturnr   strfloat)filled	fill_typer)   coloralphar    c                 C  s<   |  |}t||}tj||dd|d}|| d|_dS )a  Plot filled contours on a single Axes.

        Args:
            filled (sequence of arrays): Filled contour data as returned by
                :func:`~contourpy.ContourGenerator.filled`.
            fill_type (FillType): Type of ``filled`` data, as returned by
                :attr:`~contourpy.ContourGenerator.fill_type`.
            ax (int or Maplotlib Axes, optional): Which axes to plot on, default ``0``.
            color (str, optional): Color to plot with. May be a string color or the letter ``"C"``
                followed by an integer in the range ``"C0"`` to ``"C9"`` to use a color from the
                ``tab10`` colormap. Default ``"C0"``.
            alpha (float, optional): Opacity to plot with, default ``0.7``.
        noner   Z
facecolorsZ
edgecolorslwrB   TN)r9   r   mcollectionsPathCollectionadd_collectionr1   )r'   r?   r@   r)   rA   rB   paths
collectionr*   r*   r+   r?   V   s    

    
zMplRenderer.filledblack皙?r   )xyr)   rA   rB   point_colorquad_as_tri_alphar    c                 C  s  |  |}| ||\}}t||d}|j|||j|jf| |dkrd|ddddf |ddddf  |ddddf  |ddddf   }	d|ddddf |ddddf  |ddddf  |ddddf   }
||d< |jt|ddddf |	|ddddf fdt|ddddf |
|ddddf fdt|ddddf |	|ddddf fdt|ddddf |
|ddddf fdf| |dk	r|j||||d	dd
 d|_dS )a  Plot quad grid lines on a single Axes.

        Args:
            x (array-like of shape (ny, nx) or (nx,)): The x-coordinates of the grid points.
            y (array-like of shape (ny, nx) or (ny,)): The y-coordinates of the grid points.
            ax (int or Matplotlib Axes, optional): Which Axes to plot on, default ``0``.
            color (str, optional): Color to plot grid lines, default ``"black"``.
            alpha (float, optional): Opacity to plot lines with, default ``0.1``.
            point_color (str, optional): Color to plot grid points or ``None`` if grid points
                should not be plotted, default ``None``.
            quad_as_tri_alpha (float, optional): Opacity to plot ``quad_as_tri`` grid, default 0.

        Colors may be a string color or the letter ``"C"`` followed by an integer in the range
        ``"C0"`` to ``"C9"`` to use a color from the ``tab10`` colormap.

        Warning:
            ``quad_as_tri_alpha > 0`` plots all quads as though they are unmasked.
        )rA   rB   r   g      ?Nr   rB   )   rQ   o)rA   rB   markerrE   T)	r9   _grid_as_2dr$   plotTnpstackZreshaper1   )r'   rM   rN   r)   rA   rB   rO   rP   r(   xmidymidr*   r*   r+   gridr   s&    

TT4444
zMplRenderer.grid      ?cpy.LineReturnr   )lines	line_typer)   rA   rB   	linewidthr    c           	      C  s<   |  |}t||}tj|d|||d}|| d|_dS )a6  Plot contour lines on a single Axes.

        Args:
            lines (sequence of arrays): Contour line data as returned by
                :func:`~contourpy.ContourGenerator.lines`.
            line_type (LineType): Type of ``lines`` data, as returned by
                :attr:`~contourpy.ContourGenerator.line_type`.
            ax (int or Matplotlib Axes, optional): Which Axes to plot on, default ``0``.
            color (str, optional): Color to plot lines. May be a string color or the letter ``"C"``
                followed by an integer in the range ``"C0"`` to ``"C9"`` to use a color from the
                ``tab10`` colormap. Default ``"C0"``.
            alpha (float, optional): Opacity to plot lines with, default ``1.0``.
            linewidth (float, optional): Width of lines, default ``1``.
        rC   rD   TN)r9   r	   rF   rG   rH   r1   )	r'   r_   r`   r)   rA   rB   ra   rI   rJ   r*   r*   r+   r_      s    

    
zMplRenderer.linesz'ArrayLike | np.ma.MaskedArray[Any, Any]rM   rN   zr)   rA   r    c                 C  sT   t j|}|t jjkrdS | |}| ||\}}|j|| || d|d dS )a  Plot masked out grid points as circles on a single Axes.

        Args:
            x (array-like of shape (ny, nx) or (nx,)): The x-coordinates of the grid points.
            y (array-like of shape (ny, nx) or (ny,)): The y-coordinates of the grid points.
            z (masked array of shape (ny, nx): z-values.
            ax (int or Matplotlib Axes, optional): Which Axes to plot on, default ``0``.
            color (str, optional): Circle color, default ``"black"``.
        NrS   )c)rX   maZgetmaskZnomaskr9   rU   rV   )r'   rM   rN   rc   r)   rA   maskr*   r*   r+   rf      s    
zMplRenderer.maskF)filenametransparentr    c                 C  s   |    | jj||d dS )zSave plots to SVG or PNG file.

        Args:
            filename (str): Filename to save to.
            transparent (bool, optional): Whether background should be transparent, default
                ``False``.
        )rh   N)r5   r   savefig)r'   rg   rh   r*   r*   r+   save   s    zMplRenderer.savez
io.BytesIOc                 C  s.   |    t }| jj|dd |d |S )zhSave plots to an ``io.BytesIO`` buffer.

        Return:
            BytesIO: PNG image buffer.
        Zpng)formatr   )r5   ioBytesIOr   ri   seek)r'   bufr*   r*   r+   save_to_buffer   s
    
zMplRenderer.save_to_bufferc                 C  s   |    t  dS )zMShow plots in an interactive window, in the usual Matplotlib manner.
        N)r5   r%   showr/   r*   r*   r+   rq      s    zMplRenderer.show)titler)   rA   r    c                 C  s.   |r|  |j||d n|  || dS )a  Set the title of a single Axes.

        Args:
            title (str): Title text.
            ax (int or Matplotlib Axes, optional): Which Axes to set the title of, default ``0``.
            color (str, optional): Color to set title. May be a string color or the letter ``"C"``
                followed by an integer in the range ``"C0"`` to ``"C9"`` to use a color from the
                ``tab10`` colormap. Default is ``None`` which uses Matplotlib's default title color
                that depends on the stylesheet in use.
        )rA   N)r9   	set_title)r'   rr   r)   rA   r*   r*   r+   rr      s    zMplRenderer.titlegreen.1f)rM   rN   rc   r)   rA   fmtquad_as_trir    c                 C  s2  |  |}| ||\}}t|}|j\}}	t|D ]H}
t|	D ]:}|j||
|f ||
|f ||
|f | dd|dd qBq6|r.t|d D ]}
t|	d D ]}t||
|
d ||d f }t||
|
d ||d f }t||
|
d ||d f }|j|||| dd|dd qqdS )a  Show ``z`` values on a single Axes.

        Args:
            x (array-like of shape (ny, nx) or (nx,)): The x-coordinates of the grid points.
            y (array-like of shape (ny, nx) or (ny,)): The y-coordinates of the grid points.
            z (array-like of shape (ny, nx): z-values.
            ax (int or Matplotlib Axes, optional): Which Axes to plot on, default ``0``.
            color (str, optional): Color of added text. May be a string color or the letter ``"C"``
                followed by an integer in the range ``"C0"`` to ``"C9"`` to use a color from the
                ``tab10`` colormap. Default ``"green"``.
            fmt (str, optional): Format to display z-values, default ``".1f"``.
            quad_as_tri (bool, optional): Whether to show z-values at the ``quad_as_tri`` centers
                of quads.

        Warning:
            ``quad_as_tri=True`` shows z-values for all quads, even if masked.
        centerTZhavarA   Zclip_onr      N)r9   rU   rX   asarrayshaperangetextmean)r'   rM   rN   rc   r)   rA   rv   rw   nynxjiZxxyyzzr*   r*   r+   z_values  s&    


, 
"""zMplRenderer.z_values)r   r   r   TNN)r   r:   r;   )r   rK   rL   Nr   )r   r:   r]   r   )r   rK   )F)r   N)r   rt   ru   F)__name__
__module____qualname____annotations__r,   r0   r5   r9   r?   r\   r_   rf   rj   rp   rq   rr   r   r*   r*   r*   r+   r      sP   
      	         3    #      r   c                      s.   e Zd ZdZd
ddddd fdd	Z  ZS )MplTestRendererzTest renderer implemented using Matplotlib.

    No whitespace around plots and no spines/ticks displayed.
    Uses Agg backend, so can only save to file/buffer, cannot call ``show()``.
    r   r   r   r   r   )r   r   r   r    c                   sh   ddddddd}t  j|||dd|d | jD ],}|d |d |g  |g  q0d| _d S )	Ng{Gz?gGz?)leftrighttopbottomZwspaceZhspaceTZAgg)r   r   r           F)superr,   r   Zset_xmarginZset_ymarginZ
set_xticksZ
set_yticksr   )r'   r   r   r   Zgridspecr)   	__class__r*   r+   r,   7  s*         



zMplTestRenderer.__init__)r   r   r   )r   r   r   __doc__r,   __classcell__r*   r*   r   r+   r   1  s
      r   c                      s  e Zd ZdZd:dddddd	 fd
dZddddddddddZddddddZddddddZd;ddd%ddddddddd& fd'd(Zd<ddd%dddddddd*
 fd+d,Z	d=d-d-d-d%ddd.d/d0Z
d>d-d-d-d%ddd.d2d3Zd?d-d-d-dd6d%ddd7d8d9Z  ZS )@MplDebugRendererzDebug renderer implemented using Matplotlib.

    Extends ``MplRenderer`` to add extra information to help in debugging such as markers, arrows,
    text, etc.
    r   r   Tr   r   r   r   )r   r   r   r   r    c                   s   t  |||| d S r7   )r   r,   )r'   r   r   r   r   r   r*   r+   r,   X  s    zMplDebugRenderer.__init__r   zcpy.CoordinateArrayr=   r>   )r)   
line_startline_endrA   rB   
arrow_sizer    c                 C  s   d||  }|| }|t t || }t |d |d  f}	t ||d |	 |  ||d |  ||d |	 |  f}
|j|
d d df |
d d df d||d d S )Ng      ?r   r   -rd   rB   )rX   sqrtdotr|   rY   rV   )r'   r)   r   r   rA   rB   r   ZmidZalongr   Zarrowr*   r*   r+   _arrowa  s    	zMplDebugRenderer._arrowr<   r   z2tuple[list[cpy.PointArray], list[cpy.OffsetArray]])r?   r@   r    c                 C  st  |t jkr8trttj|}|d }dd |d D }n4|t jkrztrRttj|}dd |d D }dd |d D }n|t jkrtrttj	|}|d }|d }n|t j
krtrttj|}dd |d D }dd |d D }n|t jkrtrttj|}g }g }t| D ]v\}}}|d kr0qtrN|d k	rJ|d k	sNt|t||dd	 7 }t||dd	 }|d
d |D 7 }qn|t jkr\trttj|}g }g }t| D ]\}}	}|d krؐqtr|	d k	r|d k	sttt|d D ]N}
|	||
 ||
d  d  }|||d |d	   |||d   qqntd| d||fS )Nr   c                 S  s   g | ]}t |qS r*   r
   .0codesr*   r*   r+   
<listcomp>~  s     zKMplDebugRenderer._filled_to_lists_of_points_and_offsets.<locals>.<listcomp>r   c                 S  s   g | ]}|d k	r|qS r7   r*   r   pointsr*   r*   r+   r     s      c                 S  s   g | ]}|d k	rt |qS r7   r   r   r*   r*   r+   r     s      c                 S  s   g | ]}|d k	r|qS r7   r*   r   r*   r*   r+   r     s      c                 S  s   g | ]}|d k	r|qS r7   r*   )r   offsetsr*   r*   r+   r     s      rQ   c                 S  s   g | ]}t |qS r*   r   r   r*   r*   r+   r     s     zRendering FillType  not implemented)r   Z	OuterCoder   r   cpyZFillReturn_OuterCodeChunkCombinedCodeZFillReturn_ChunkCombinedCodeZOuterOffsetZFillReturn_OuterOffsetChunkCombinedOffsetZFillReturn_ChunkCombinedOffsetZChunkCombinedCodeOffsetZ"FillReturn_ChunkCombinedCodeOffsetzipAssertionErrorrX   splitZChunkCombinedOffsetOffsetZ$FillReturn_ChunkCombinedOffsetOffsetr~   r3   appendRuntimeError)r'   r?   r@   
all_pointsall_offsetsr   r   Zouter_offsetsZ	all_codesr   r   Zoffsr*   r*   r+   &_filled_to_lists_of_points_and_offsetsu  sb    





z7MplDebugRenderer._filled_to_lists_of_points_and_offsetsr^   r   zlist[cpy.PointArray])r_   r`   r    c           	   	   C  sh  |t jkr"trttj|}|}nB|t jkrHtr<ttj|}|d }n|t jkrtrbttj	|}g }t
| D ]\\}}|d k	rntr|d k	stt|}tt|d D ]"}|||| ||d    qqnn|t jkrTtrttj|}g }t
| D ]Z\}}|d k	rtr|d k	sttt|d D ]$}|||| ||d    q*qntd| d|S )Nr   r   zRendering LineType r   )r   ZSeparater   r   r   ZLineReturn_SeparateZSeparateCodeZLineReturn_SeparateCoder   ZLineReturn_ChunkCombinedCoder   r   r
   r~   r3   r   r   ZLineReturn_ChunkCombinedOffsetr   )	r'   r_   r`   	all_linesr   r   r   r   r   r*   r*   r+   _lines_to_list_of_points  s>    


$&z)MplDebugRenderer._lines_to_list_of_pointsr   C1r;   r:   redrL   r6   )r?   r@   r)   rA   rB   
line_color
line_alpharO   start_point_colorr   r    c                   s  t  ||||| |d kr(|d kr(d S | |}| ||\}}|d k	rt||D ]\}}t|d d |dd  D ]\}}||| }|j|d d df |d d df ||d |
dkrvt|}t|d D ]$}| ||| ||d  |||
 qqvqT|d k	rt||D ]\}}t	j
|d td}d||dd  d < |	d k	rZ|d d }d||< |j|d d df | |d d df | d||d |	d k	r|j|d d df | |d d df | d|	|d qd S )	NrQ   r   r   r   r   )ZdtypeFrS   )r   r?   r9   r   r   rV   r3   r~   r   rX   Zonesr   )r'   r?   r@   r)   rA   rB   r   r   rO   r   r   r   r   r   r   startendZxysnr   rf   Zstart_indicesr   r*   r+   r?     sB    
"*&

    
* zMplDebugRenderer.filledr]   )
r_   r`   r)   rA   rB   ra   rO   r   r   r    c
              
     s>  t  |||||| |	dkr*|d kr*d S | |}| ||}
|	dkr|
D ]:}tt|d D ]$}| ||| ||d  |||	 q`qL|d k	r:|
D ]}d}t|}|d k	r|j|d |d d||d d}|d d |d d kr|d d |d d kr|d8 }|j|||df |||df d||d qd S )	Nr   r   r   )r   r   )r   r   rS   r   rQ   )r   r_   r9   r   r~   r3   r   rV   )r'   r_   r`   r)   rA   rB   ra   rO   r   r   r   liner   start_indexZ	end_indexr   r*   r+   r_     s,    
$

4" zMplDebugRenderer.linesr   rb   c                 C  s   |  |}| ||\}}t|}|j\}}t|D ]J}t|D ]<}	|	||  }
|j|||	f |||	f t|
dd|dd qBq6d S )Nr   r   Try   )r9   rU   rX   r|   r}   r~   r   r=   )r'   rM   rN   rc   r)   rA   r   r   r   r   quadr*   r*   r+   point_numbers(  s    


$zMplDebugRenderer.point_numbersbluec                 C  s   |  |}| ||\}}t|}|j\}}td|D ]}td|D ]|}	|	||  }
||d |d |	d |	d f  }||d |d |	d |	d f  }|j||t|
dd|dd qFq8d S )Nr   rx   Try   )	r9   rU   rX   r|   r}   r~   r   r   r=   )r'   rM   rN   rc   r)   rA   r   r   r   r   r   rZ   r[   r*   r*   r+   quad_numbers:  s    


((zMplDebugRenderer.quad_numbersNrt   zfloat | None)rM   rN   rc   lower_levelupper_levelr)   rA   r    c                 C  s   |  |}| ||\}}t|}|j\}}	t|D ]n}
t|	D ]`}||
|f }|d k	rh||krhd}n||krvd}nd}|j||
|f ||
|f |dd|dd qBq6d S )Nr{   r   r   r   r   Try   )r9   rU   rX   r|   r}   r~   r   )r'   rM   rN   rc   r   r   r)   rA   r   r   r   r   r   Zz_levelr*   r*   r+   z_levelsM  s    



 zMplDebugRenderer.z_levels)r   r   r   T)r   r   r;   r:   r;   r:   r   rL   )r   r:   r]   r   r:   r   rL   )r   r   )r   r   )Nr   rt   )r   r   r   r   r,   r   r   r   r?   r_   r   r   r   r   r*   r*   r   r+   r   R  sH       	9)        (4       &*       r   )$
__future__r   rl   typingr   r   r   Zmatplotlib.collectionscollectionsrF   Zmatplotlib.pyplotZpyplotr%   ZnumpyrX   Z	contourpyr   r   Zcontourpy.util.mpl_utilr   r	   r
   Zcontourpy.util.rendererr   Zmatplotlib.axesr   Zmatplotlib.figurer   Znumpy.typingr   Zcontourpy._contourpyZ
_contourpyr   r   r   r   r*   r*   r*   r+   <module>   s$     !