a
    >PÎj#  ã                   @  s¼  d dl mZ d dlmZ d dlmZ d dlZd dlm	Z	m
Z
mZ d dlmZmZmZmZmZmZ erpd dlmZ ddd	œd
d„Zddddœdd„Zdddœdd„Zdddœdd„Zdddœdd„Zdddœdd„Zd d!d"œd#d$„Zd%dd&œd'd(„Zd)d*d+œd,d-„Zd)d*d+œd.d/„Zd%dd&œd0d1„Z dddd2œd3d4„Z!ddd5œd6d7„Z"d%dd&œd8d9„Z#dddœd:d;„Z$dddœd<d=„Z%dd>dœd?d@„Z&ddddAœdBdC„Z'ddd%d2œdDdE„Z(dd%dœdFdG„Z)dS )Hé    )Úannotations)Úchain)ÚTYPE_CHECKINGN)Úcheck_code_arrayÚcheck_offset_arrayÚcheck_point_array)Ú	CLOSEPOLYÚLINETOÚMOVETOÚ
code_dtypeÚoffset_dtypeÚpoint_dtypezcpy.OffsetArrayzcpy.CodeArray)ÚoffsetsÚreturnc                 C  sH   t | ƒ | d }tj|ttd�}t|| dd… < t|| dd… d < |S )zSDetermine codes from offsets, assuming they all correspond to closed polygons.
    éÿÿÿÿ©ZdtypeNé   )r   ÚnpÚfullr	   r   r
   r   )r   ÚnÚcodes© r   ú_/mounts/lovelace/software/anaconda3/envs/metadmg/lib/python3.9/site-packages/contourpy/array.pyÚcodes_from_offsets   s    r   zcpy.PointArray)r   Úpointsr   c                 C  sv   t | ƒ t|ƒ tjt|ƒttd�}t|| dd… < | dd… d }tj|| dd…  || kdd�}t	||| < |S )z–Determine codes from offsets and points, using the equality of the start and end points of
    each line to determine if lines are closed or not.
    r   Nr   r   ©Zaxis)
r   r   r   r   Úlenr	   r   r
   Úallr   )r   r   r   Zend_offsetsÚclosedr   r   r   Úcodes_from_offsets_and_points   s    "r   )r   r   c                 C  sJ   t | ƒ t| ƒ}tj|ttd�}t|d< t | d | d k¡rFt|d< |S )z…Determine codes for a single line, using the equality of the start and end points to
    determine if the line is closed or not.
    r   r   r   )	r   r   r   r   r	   r   r
   r   r   )r   r   r   r   r   r   Úcodes_from_points/   s    r    zlist[cpy.CodeArray])Úlist_of_codesr   c                 C  s   | st dƒ‚tj| td�S )zAConcatenate a list of codes arrays into a single code array.
    z!Empty list passed to concat_codesr   )Ú
ValueErrorr   Úconcatenater   ©r!   r   r   r   Úconcat_codes=   s    r%   zlist[cpy.CodeArray | None]zcpy.CodeArray | None)Úlist_of_codes_or_noner   c                 C  s"   dd„ | D ƒ}|rt |ƒS dS dS )zQConcatenate a list of codes arrays or None into a single code array or None.
    c                 S  s   g | ]}|d ur|‘qS ©Nr   ©Ú.0r   r   r   r   Ú
<listcomp>I   ó    z(concat_codes_or_none.<locals>.<listcomp>N)r%   )r&   r!   r   r   r   Úconcat_codes_or_noneF   s    r,   zlist[cpy.OffsetArray])Úlist_of_offsetsr   c                   sb   ˆst dƒ‚tˆƒ}tjdd„ ˆD ƒtd�‰ tjˆd g‡ ‡fdd„t|d ƒD ƒ¢R td�}|S )	zEConcatenate a list of offsets arrays into a single offset array.
    z#Empty list passed to concat_offsetsc                 S  s   g | ]}|d  ‘qS )r   r   ©r)   r   r   r   r   r*   W   r+   z"concat_offsets.<locals>.<listcomp>r   r   c                 3  s*   | ]"}ˆ|d   d d… ˆ |  V  qdS )r   Nr   )r)   Úi©Z
cumulativer-   r   r   Ú	<genexpr>Y   r+   z!concat_offsets.<locals>.<genexpr>r   )r"   r   r   Úcumsumr   r#   Úrange)r-   r   Úretr   r0   r   Úconcat_offsetsP   s    &þr5   zlist[cpy.OffsetArray | None]zcpy.OffsetArray | None)Úlist_of_offsets_or_noner   c                 C  s"   dd„ | D ƒ}|rt |ƒS dS dS )zUConcatenate a list of offsets arrays or None into a single offset array or None.
    c                 S  s   g | ]}|d ur|‘qS r'   r   r.   r   r   r   r*   d   r+   z*concat_offsets_or_none.<locals>.<listcomp>N)r5   )r6   r-   r   r   r   Úconcat_offsets_or_none_   s    r7   zlist[cpy.PointArray])Úlist_of_pointsr   c                 C  s   | st dƒ‚tj| td�S )zBConcatenate a list of point arrays into a single point array.
    z"Empty list passed to concat_pointsr   )r"   r   r#   r   ©r8   r   r   r   Úconcat_pointsk   s    r:   zlist[cpy.PointArray | None]zcpy.PointArray | None)Úlist_of_points_or_noner   c                 C  s"   dd„ | D ƒ}|rt |ƒS dS dS )zRConcatenate a list of point arrays or None into a single point array or None.
    c                 S  s   g | ]}|d ur|‘qS r'   r   ©r)   r   r   r   r   r*   y   r+   z)concat_points_or_none.<locals>.<listcomp>N)r:   ©r;   r8   r   r   r   Úconcat_points_or_nonet   s    r>   c                 C  s"   dd„ | D ƒ}|rt |ƒS dS dS )zvConcatenate a list of points or None into a single point array or None, with NaNs used to
    separate each line.
    c                 S  s   g | ]}|d ur|‘qS r'   r   r<   r   r   r   r*   †   r+   z2concat_points_or_none_with_nan.<locals>.<listcomp>N)Úconcat_points_with_nanr=   r   r   r   Úconcat_points_or_none_with_nan€   s    r@   c                   sj   | st dƒ‚t| ƒdkr | d S tjdtjtd�‰ | d gtt‡ fdd„| dd… D ƒŽ ƒ¢} t| ƒS dS )	zaConcatenate a list of points into a single point array with NaNs used to separate each line.
    z+Empty list passed to concat_points_with_nanr   r   )r   é   r   c                 3  s   | ]}ˆ |fV  qd S r'   r   )r)   Úx©Ú
nan_spacerr   r   r1   ˜   r+   z)concat_points_with_nan.<locals>.<genexpr>N)	r"   r   r   r   Únanr   Úlistr   r:   r9   r   rC   r   r?   �   s     ÿr?   )r   r   r   c                 C  s\   t | ƒ t|ƒ t|ƒdkr | S tjtjtjgtd�}tj| |dd…  tj	¡|dd�S dS )zNInsert NaNs into a point array at locations specified by an offset array.
    rA   r   r   r   r   r   N)
r   r   r   r   ÚarrayrE   r   ÚinsertÚastypeZint64)r   r   rD   r   r   r   Úinsert_nan_at_offsetsœ   s    rJ   )r   r   c                 C  s,   t | ƒ t t | tk¡d t| ƒ¡ t¡S )zBDetermine offsets from codes using locations of MOVETO codes.
    r   )r   r   ÚappendÚnonzeror
   r   rI   r   )r   r   r   r   Úoffsets_from_codesª   s    rM   c                 C  s*   | st dƒ‚tjdgdd„ | D ƒ td�S )z4Determine offsets from lengths of point arrays.
    z)Empty list passed to offsets_from_lengthsr   c                 S  s   g | ]}t |ƒ‘qS r   ©r   )r)   Úliner   r   r   r*   ¸   r+   z(offsets_from_lengths.<locals>.<listcomp>r   ©r"   r   r2   r   r9   r   r   r   Úoffsets_from_lengths²   s    rQ   c                 C  s*   | st dƒ‚tjdgdd„ | D ƒ td�S )zHDetermine outer offsets from codes using locations of MOVETO codes.
    z5Empty list passed to outer_offsets_from_list_of_codesr   c                 S  s   g | ]}t  |tk¡‘qS r   )r   Zcount_nonzeror
   r(   r   r   r   r*   Á   r+   z4outer_offsets_from_list_of_codes.<locals>.<listcomp>r   rP   r$   r   r   r   Ú outer_offsets_from_list_of_codes»   s
    ÿrR   c                 C  s*   | st dƒ‚tjdgdd„ | D ƒ td�S )z4Determine outer offsets from a list of offsets.
    z7Empty list passed to outer_offsets_from_list_of_offsetsr   c                 S  s   g | ]}t |ƒd  ‘qS ©r   rN   r.   r   r   r   r*   Ë   r+   z6outer_offsets_from_list_of_offsets.<locals>.<listcomp>r   rP   )r-   r   r   r   Ú"outer_offsets_from_list_of_offsetsÅ   s    rT   z&tuple[cpy.PointArray, cpy.OffsetArray]c                 C  s²   t | ƒ t t | dd…df ¡¡d }t|ƒdkrN| tjdt| ƒgtd�fS tj| |dd�} |t t|ƒ¡8 }tj	t|ƒd td�}d|d< ||dd…< t| ƒ|d< | |fS dS )z^Remove NaN from a points array, also return the offsets corresponding to the NaN removed.
    Nr   r   r   rA   r   r   )
r   r   rL   Úisnanr   rG   r   ÚdeleteZarangeÚempty)r   Únan_offsetsr   r   r   r   Ú
remove_nanÎ   s     rY   )r   r   r   c                 C  s:   t | ƒ t|ƒ t|ƒdkr0t | |dd… ¡S | gS dS )z]Split a code array at locations specified by an offset array into a list of code arrays.
    rA   r   r   N)r   r   r   r   Úsplit)r   r   r   r   r   Úsplit_codes_by_offsetsà   s
    r[   c                 C  s:   t | ƒ t|ƒ t|ƒdkr0t | |dd… ¡S | gS dS )z_Split a point array at locations specified by an offset array into a list of point arrays.
    rA   r   r   N)r   r   r   r   rZ   )r   r   r   r   r   Úsplit_points_by_offsetsì   s
    r\   c                   s~   t ˆ ƒ t t ˆ dd…df ¡¡d }t|ƒdkr:ˆ gS t dg|tˆ ƒgf¡}‡ fdd„t|dd… |dd… ƒD ƒS dS )z>Split a points array at NaNs into a list of point arrays.
    Nr   r   c                   s    g | ]\}}ˆ |d  |… ‘qS rS   r   )r)   ÚsÚe©r   r   r   r*     r+   z'split_points_at_nan.<locals>.<listcomp>r   )r   r   rL   rU   r   r#   Úzip)r   rX   r   r_   r   Úsplit_points_at_nanû   s     ra   )*Ú
__future__r   Ú	itertoolsr   Útypingr   Únumpyr   Zcontourpy.typecheckr   r   r   Zcontourpy.typesr   r	   r
   r   r   r   Zcontourpy._contourpyZ
_contourpyZcpyr   r   r    r%   r,   r5   r7   r:   r>   r@   r?   rJ   rM   rQ   rR   rT   rY   r[   r\   ra   r   r   r   r   Ú<module>   s6    	
		
	