o
    ðËhé  ã                	   @   s’   d dl Zd dlmZ dd„ Zddgddgfddgddgfg d¢g d¢fg d	¢g d
¢fg d¢g d¢fg d¢g d¢fg d¢g d¢fdœZddd„ZdS )é    N)Úsignalc                 C   sl  |dkr| S t  | jd d¡}t  | jd ¡}d|d< d|d< d|jd d fg}d}|s.| ¡ \}}| |dd…f \}}	| |dd…f \}
}|
| }||	 }t  ||¡ }|	t  |¡ |t  |¡  }| |d |…dd…f }||d |… }|dd…df | }|dd…df |	 }|dd…df |
 }|dd…df | }|| ||  }| | ||  }t  |dk|dk¡}t  |¡}t  	||df t  |¡ ||df t  |¡  | ¡||< t  
t  || d || d  ¡t  || d || d  ¡¡||< t  ||k¡r#|t  |¡ d }| ||f¡ | ||f¡ d||< t|ƒdkr,d}|r.| |dd…f S )	a¸  Approximate a polygonal chain with the specified tolerance.

    It is based on the Douglas-Peucker algorithm.

    Note that the approximated polygon is always within the convex hull of the
    original polygon.

    Parameters
    ----------
    coords : (N, 2) array
        Coordinate array.
    tolerance : float
        Maximum distance from original points of polygon to approximated
        polygonal chain. If tolerance is 0, the original coordinate array
        is returned.

    Returns
    -------
    coords : (M, 2) array
        Approximated polygonal chain where M <= N.

    References
    ----------
    .. [1] https://en.wikipedia.org/wiki/Ramer-Douglas-Peucker_algorithm
    r   ÚboolTéÿÿÿÿé   FNé   )ÚnpÚzerosÚshapeÚpopÚarctan2ÚsinÚcosÚlogical_andÚlogical_notÚabsÚminimumÚsqrtÚanyÚargmaxÚappendÚlen)ÚcoordsÚ	toleranceÚchainÚdistsÚ	pos_stackÚend_of_chainÚstartÚendÚr0Úc0Úr1Úc1ÚdrÚdcÚsegment_angleÚsegment_distÚsegment_coordsÚsegment_distsÚdr0Údc0Údr1Údc1Úprojected_lengths0Úprojected_lengths1ÚperpÚeuclÚnew_end© r2   úS/var/www/html/scripts/venv/lib/python3.10/site-packages/skimage/measure/_polygon.pyÚapproximate_polygon   s`   
ÿ
ÿþÿüÐ2r4   r   é   )r   é   r   )r   é   r7   )é   é
   r   )r   r9   r8   )r   é   r:   r   )r   r6   é   r6   )é   é#   é   r   )r   r>   r=   r<   )r   é   éF   r?   r   )r   é   é8   rB   rA   )r   r   r5   r7   r8   r6   r<   r   Fc           
      C   sN  |t vrtdƒ‚t | ddd…f | ddd…f k¡}d}|r+| dd…dd…f } d}t | \}}t |t¡d|  }t |t¡d|  }tj| jt 	|¡|dd	}tj| jt 	|¡|dd	}t 
|jd
 |jd
  df¡}	|j|	d
dd…< |j|	ddd…< |rt |	|	ddd…f g¡}	|r¥|s¥t | ddd…f |	| ddd…f g¡}	|	S )a¸  Subdivision of polygonal curves using B-Splines.

    Note that the resulting curve is always within the convex hull of the
    original polygon. Circular polygons stay closed after subdivision.

    Parameters
    ----------
    coords : (N, 2) array
        Coordinate array.
    degree : {1, 2, 3, 4, 5, 6, 7}, optional
        Degree of B-Spline. Default is 2.
    preserve_ends : bool, optional
        Preserve first and last coordinate of non-circular polygon. Default is
        False.

    Returns
    -------
    coords : (M, 2) array
        Subdivided coordinate array.

    References
    ----------
    .. [1] http://mrl.nyu.edu/publications/subdiv-course2000/coursenotes00.pdf
    z8Invalid B-Spline degree. Only degree 1 - 7 is supported.r   Nr   ÚvalidÚsamer   Úwrap)ÚmodeÚboundaryr   )Ú_SUBDIVISION_MASKSÚ
ValueErrorr   ÚallÚarrayÚfloatr   Ú
convolve2dÚTÚ
atleast_2dr   r	   Úvstack)
r   ÚdegreeÚpreserve_endsÚcircularÚmethodÚ	mask_evenÚmask_oddÚevenÚoddÚoutr2   r2   r3   Úsubdivide_polygonm   s0   &ÿÿ(rZ   )r   F)Únumpyr   Úscipyr   r4   rH   rZ   r2   r2   r2   r3   Ú<module>   s    ^÷