o
    e_                     @  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rVd 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< 	 						
	
dVdWddZdXddZdXddZdYdd Z	!	"	#dZd[d,d-Z	!	.	/	
	!d\d]d5d6Z		!	"	7	d^d_d=d>Z
	!	.d`dadAdBZdbdcdFdGZdddIdJZdXdKdLZdedfdNdOZ	!	P	Q	CdgdhdTdUZd
S )iMplRendererr   _axesr   _figbool_want_tight   	   r   TNnrowsintncolsfigsizetuple[float, float]
show_framebackend
str | Nonegridspec_kwdict[str, Any] | NonereturnNonec                 C  s   |d urdd l }|| t|dddd}|d ur||d< ntdd|d< tj||fi |\| _}	|	 | _|sD| jD ]}
|
d	 q<d| _	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,   KD:\Projects\ConvertPro\env\Lib\site-packages\contourpy/util/mpl_renderer.py__init__'   s   	




zMplRenderer.__init__c                 C  s   t | drt| j d S d S )Nr   )hasattrr'   closer   r)   r,   r,   r-   __del__B   s   
zMplRenderer.__del__c                 C  sT   | j D ]}t|ddr|jdd d|_q| jr&t| j dkr(| j  d S d S d S )N_need_autoscaleFT)Ztightr   )r   getattrZautoscale_viewr3   r   lenr   Ztight_layoutr)   r+   r,   r,   r-   
_autoscaleF   s   
zMplRenderer._autoscaler+   
Axes | intc                 C  s   t |tr
| j| }|S N)
isinstancer   r   r6   r,   r,   r-   _get_axQ   s   

zMplRenderer._get_axr   C0ffffff?filledcpy.FillReturn	fill_typer   colorstralphafloatc                 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lwrC   TN)r;   r   mcollectionsPathCollectionadd_collectionr3   )r)   r>   r@   r+   rA   rC   paths
collectionr,   r,   r-   r>   V   s   




zMplRenderer.filledblack皙?xr   ypoint_colorquad_as_tri_alphac                 C  s  |  |}| ||\}}t||d}|j|||j|jfi | |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i | |du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   rC   r   g      ?Nr   rC   )   rS   o)rA   rC   markerrG   T)	r;   _grid_as_2dr&   plotTnpstackZreshaper3   )r)   rO   rP   r+   rA   rC   rQ   rR   r*   xmidymidr,   r,   r-   gridr   s&   
TT4444
zMplRenderer.grid      ?linescpy.LineReturn	line_typer   	linewidthc           	      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``.
        rE   rF   TN)r;   r	   rH   rI   rJ   r3   )	r)   r`   rb   r+   rA   rC   rc   rK   rL   r,   r,   r-   r`      s   




zMplRenderer.linesz'ArrayLike | np.ma.MaskedArray[Any, Any]c                 C  sT   t j|}|t jju 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"``.
        NrU   )c)rZ   maZgetmaskZnomaskr;   rW   rX   )r)   rO   rP   rd   r+   rA   maskr,   r,   r-   rh      s   
zMplRenderer.maskFfilenametransparentc                 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``.
        )rj   N)r7   r   savefig)r)   ri   rj   r,   r,   r-   save   s   zMplRenderer.save
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   )r7   ioBytesIOr   rk   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)r7   r'   showr1   r,   r,   r-   rt      s   zMplRenderer.showtitlec                 C  s0   |r|  |j||d dS |  || 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)r;   	set_title)r)   ru   r+   rA   r,   r,   r-   ru      s   zMplRenderer.titlegreen.1ffmtquad_as_tric                 C  s4  |  |}| ||\}}t|}|j\}}	t|D ]$}
t|	D ]}|j||
|f ||
|f ||
|f | dd|dd q!q|rt|d D ]O}
t|	d D ]D}t||
|
d ||d f }t||
|
d ||d f }t||
|
d ||d f }|j|||| dd|dd qPqHdS d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)r;   rW   rZ   asarrayshaperangetextmean)r)   rO   rP   rd   r+   rA   ry   rz   nynxjixxyyzzr,   r,   r-   z_values  s,   


,"""zMplRenderer.z_values)r   r   r   TNN)r   r   r   r   r   r   r   r   r   r   r   r    r!   r"   )r!   r"   )r+   r8   r!   r   )r   r<   r=   )r>   r?   r@   r   r+   r8   rA   rB   rC   rD   r!   r"   )r   rM   rN   Nr   )rO   r   rP   r   r+   r8   rA   rB   rC   rD   rQ   r   rR   rD   r!   r"   )r   r<   r_   r   )r`   ra   rb   r   r+   r8   rA   rB   rC   rD   rc   rD   r!   r"   )r   rM   )rO   r   rP   r   rd   re   r+   r8   rA   rB   r!   r"   )F)ri   rB   rj   r   r!   r"   )r!   rm   )r   N)ru   rB   r+   r8   rA   r   r!   r"   )r   rw   rx   F)rO   r   rP   r   rd   r   r+   r8   rA   rB   ry   rB   rz   r   r!   r"   )__name__
__module____qualname____annotations__r.   r2   r7   r;   r>   r^   r`   rh   rl   rs   rt   ru   r   r,   r,   r,   r-   r      sT   
 


	 3#

r   c                      s*   e Zd ZdZ			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  qd| _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   r   r   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				dKdL fddZdMddZdNd!d"ZdOd(d)Z	*	+	,	-	,	-	.	/dPdQ fd5d6Z	*	-	7		-	.	/dRdS fd9d:Z		*	.dTdUd?d@Z
	*	AdVdUdBdCZ	D	*	EdWdXdIdJZ  ZS )Y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 r9   )r   r.   )r)   r   r   r   r   r   r,   r-   r.   X  s   zMplDebugRenderer.__init__r+   r   
line_startcpy.CoordinateArrayline_endrA   rB   rC   rD   
arrow_sizec                 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   -rf   rC   )rZ   sqrtdotr   r[   rX   )r)   r+   r   r   rA   rC   r   midZalongr   arrowr,   r,   r-   _arrowa  s   	0zMplDebugRenderer._arrowr>   r?   r@   r   2tuple[list[cpy.PointArray], list[cpy.OffsetArray]]c                 C  sj  |t jkrtrttj|}|d }dd |d D }||fS |t jkrAtr+ttj|}dd |d D }dd |d D }||fS |t jkrZtrNttj	|}|d }|d }||fS |t j
kr}trgttj|}dd |d D }dd |d D }||fS |t jkrtrttj|}g }g }t| D ]5\}}}|d u rqtr|d ur|d usJ |t||dd	 7 }t||dd	 }|d
d |D 7 }q||fS |t jkr-trttj|}g }g }t| D ]F\}}	}|d u rqtr|	d ur|d usJ tt|d D ]'}
|	||
 ||
d  d  }|||d |d	   |||d   q q||fS td| d)Nr   c                 S     g | ]}t |qS r,   r
   .0codesr,   r,   r-   
<listcomp>~      zKMplDebugRenderer._filled_to_lists_of_points_and_offsets.<locals>.<listcomp>r   c                 S     g | ]}|d ur|qS r9   r,   r   pointsr,   r,   r-   r         c                 S  s   g | ]
}|d urt |qS r9   r   r   r,   r,   r-   r     s    c                 S  r   r9   r,   r   r,   r,   r-   r     r   c                 S  r   r9   r,   )r   offsetsr,   r,   r-   r     r   rS   c                 S  r   r,   r   r   r,   r,   r-   r     r   zRendering FillType  not implemented)r   Z	OuterCoder   r   cpyZFillReturn_OuterCodeChunkCombinedCodeZFillReturn_ChunkCombinedCodeZOuterOffsetZFillReturn_OuterOffsetChunkCombinedOffsetZFillReturn_ChunkCombinedOffsetZChunkCombinedCodeOffsetZ"FillReturn_ChunkCombinedCodeOffsetziprZ   splitZChunkCombinedOffsetOffsetZ$FillReturn_ChunkCombinedOffsetOffsetr   r5   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  sn   
.
)
$

z7MplDebugRenderer._filled_to_lists_of_points_and_offsetsr`   ra   rb   r   list[cpy.PointArray]c           	   	   C  s`  |t jkrtrttj|}|}|S |t jkr$trttj|}|d }|S |t jkrhtr1ttj	|}g }t
| D ].\}}|d uretrG|d usGJ t|}tt|d D ]}|||| ||d    qSq7|S |t jkrtruttj|}g }t
| D ]*\}}|d urtr|d usJ tt|d D ]}|||| ||d    qq{|S td| d)Nr   r   zRendering LineType r   )r   ZSeparater   r   r   ZLineReturn_SeparateZSeparateCodeZLineReturn_SeparateCoder   ZLineReturn_ChunkCombinedCoder   r
   r   r5   r   r   ZLineReturn_ChunkCombinedOffsetr   )	r)   r`   rb   	all_linesr   r   r   r   r   r,   r,   r-   _lines_to_list_of_points  sH   


 
 z)MplDebugRenderer._lines_to_list_of_pointsr   C1r=   r<   redrN   r8   
line_color
line_alpharQ   start_point_colorc                   s  t  ||||| |d u r|d u rd S | |}| ||\}}|d ur}t||D ]R\}}t|d d |dd  D ]@\}}||| }|j|d d df |d d df ||d |
dkr{t|}t|d D ]}| ||| ||d  |||
 qhq;q*|d urt||D ]_\}}t	j
|d td}d||dd  d < |	d ur|d d }d||< |j|d d df | |d d df | d||d |	d ur|j|d d df | |d d df | d|	|d qd S d S )	NrS   r   r   r   r   )ZdtypeFrU   )r   r>   r;   r   r   rX   r5   r   r   rZ   Zonesr   )r)   r>   r@   r+   rA   rC   r   r   rQ   r   r   r   r   r   r   startendZxysnr   rh   Zstart_indicesr   r,   r-   r>     sB   
"*"
**zMplDebugRenderer.filledr_   rc   c
              
     s:  t  |||||| |	dkr|d u rd S | |}| ||}
|	dkrD|
D ]}tt|d D ]}| ||| ||d  |||	 q0q&|d ur|
D ]P}d}t|}|d u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 qJd S d S )	Nr   r   r   )r   r   )r   r   rU   r   rS   )r   r`   r;   r   r   r5   r   rX   )r)   r`   rb   r+   rA   rC   rc   rQ   r   r   r   liner   Zstart_indexZ	end_indexr   r,   r-   r`     s0   
"0"zMplDebugRenderer.linesrO   r   rP   rd   c                 C  s   |  |}| ||\}}t|}|j\}}t|D ]%}t|D ]}	|	||  }
|j|||	f |||	f t|
dd|dd q!qd S )Nr   r   Tr|   )r;   rW   rZ   r   r   r   r   rB   )r)   rO   rP   rd   r+   rA   r   r   r   r   quadr,   r,   r-   point_numbers(  s   


$zMplDebugRenderer.point_numbersbluec                 C  s   |  |}| ||\}}t|}|j\}}td|D ]F}td|D ]>}	|	||  }
||d |d |	d |	d f  }||d |d |	d |	d f  }|j||t|
dd|dd q#qd S )Nr   r{   Tr|   )	r;   rW   rZ   r   r   r   r   r   rB   )r)   rO   rP   rd   r+   rA   r   r   r   r   r   r\   r]   r,   r,   r-   quad_numbers:  s   


((zMplDebugRenderer.quad_numbersNrw   lower_levelupper_levelfloat | Nonec                 C  s   |  |}| ||\}}t|}|j\}}	t|D ]7}
t|	D ]0}||
|f }|d ur4||kr4d}n	||kr;d}nd}|j||
|f ||
|f |dd|dd q!qd S )Nr~   r   r   r   r   Tr|   )r;   rW   rZ   r   r   r   r   )r)   rO   rP   rd   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   r   r!   r"   )r+   r   r   r   r   r   rA   rB   rC   rD   r   rD   r!   r"   )r>   r?   r@   r   r!   r   )r`   ra   rb   r   r!   r   )r   r   r=   r<   r=   r<   r   rN   )r>   r?   r@   r   r+   r8   rA   rB   rC   rD   r   rB   r   rD   rQ   rB   r   rB   r   rD   r!   r"   )r   r<   r_   r   r<   r   rN   )r`   ra   rb   r   r+   r8   rA   rB   rC   rD   rc   rD   rQ   rB   r   rB   r   rD   r!   r"   )r   r   )rO   r   rP   r   rd   r   r+   r8   rA   rB   r!   r"   )r   r   )Nr   rw   )rO   r   rP   r   rd   r   r   rD   r   r   r+   r8   rA   rB   r!   r"   )r   r   r   r   r.   r   r   r   r>   r`   r   r   r   r   r,   r,   r   r-   r   R  sJ    
	

9)4*r   )$
__future__r   ro   typingr   r   r   Zmatplotlib.collectionscollectionsrH   Zmatplotlib.pyplotZpyplotr'   numpyrZ   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&      !