o
    :ήc                  	   @   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   ?/tmp/pip-target-vg8gfxp4/lib/python/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|st| 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    ^