U
    ?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dddgd ddgfd	d
dgdd
d	gfddddgd dddgfddddgddddgfdddddgd ddddgfdœZddd„ZdS )é    N)Úsignalc                 C   sh  |dkr| S t  | jd d¡}t  | jd ¡}d|d< d|d< d|jd d fg}d}|sX| ¡ \}}| |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¡rF|t  |¡ d }| ||f¡ | ||f¡ d||< t|ƒdkrVd}qV| |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ÚpopZarctan2ÚsinÚcosÚlogical_andZlogical_notÚabsÚminimumÚsqrtÚanyZargmaxÚappendÚlen)ÚcoordsZ	toleranceÚchainÚdistsZ	pos_stackZend_of_chainÚstartÚendZr0Zc0Úr1Úc1ZdrZdcZsegment_angleZsegment_distZsegment_coordsZsegment_distsZdr0Zdc0Zdr1Zdc1Zprojected_lengths0Zprojected_lengths1ÚperpZeuclÚnew_end© r   úJ/var/www/html/venv/lib/python3.8/site-packages/skimage/measure/_polygon.pyÚapproximate_polygon   s^    
ÿ
ÿþÿür   r   é   é   é   é   é
   é   é   é   é#   é   é   éF   é   é8   )r   r   r    r"   r#   r!   r'   r   Fc           
      C   sT  |t krtdƒ‚t | ddd…f | ddd…f k¡}d}|rV| 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…< |rt |	|	ddd…f g¡}	|rP|sPt | 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   ZvalidZsamer   Úwrap)ÚmodeÚboundaryr   )Ú_SUBDIVISION_MASKSÚ
ValueErrorr   ÚallÚarrayÚfloatr   Z
convolve2dÚTZ
atleast_2dr   r	   Zvstack)
r   ZdegreeZpreserve_endsZcircularÚmethodZ	mask_evenZmask_oddZevenZoddÚoutr   r   r   Úsubdivide_polygonm   s0    &ÿÿ(r9   )r   F)Únumpyr   Zscipyr   r   r1   r9   r   r   r   r   Ú<module>   s   ^÷