ó
 ‰\c           @   s#  d  d l  Z d  d l m Z d „  Z i d d g d d g f d 6d d g d d g f d 6d d d g d d	 d	 g f d 6d
 d d g d d d
 g f d	 6d d d d g d d d d g f d
 6d d d d g d d d d g f d 6d d d d d g d d d d d g f d 6Z d e d „ Z d S(   iÿÿÿÿN(   t   signalc         C   s'  | d k r |  St  j |  j d d ƒ } t  j |  j d ƒ } t | d <t | d <d | j d d f g } t } x| s| j ƒ  \ } } |  | d d … f \ } }	 |  | d d … f \ }
 } |
 | } | |	 } t  j | | ƒ } |	 t  j | ƒ | t  j | ƒ } |  | d | … d d … f } | | d | !} | d d … d f | } | d d … d f |	 } | d d … d f |
 } | d d … d f | } | | | | } | | | | } t  j	 | d k | d k ƒ } t  j
 | ƒ } t  j | | d f t  j | ƒ | | d f t  j | ƒ | ƒ | | <t  j t  j | | d | | d ƒ t  j | | d | | d ƒ ƒ | | <t  j | | k ƒ rô| t  j | ƒ d } | j | | f ƒ | j | | f ƒ t | | <n  t | ƒ d k rv t } qv qv W|  | d d … f S(   s·  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] http://en.wikipedia.org/wiki/Ramer-Douglas-Peucker_algorithm
    i    t   booliÿÿÿÿi   Ni   (   t   npt   zerost   shapet   Truet   Falset   popt   arctan2t   sint   cost   logical_andt   logical_nott   abst   minimumt   sqrtt   anyt   argmaxt   appendt   len(   t   coordst	   tolerancet   chaint   distst	   pos_stackt   end_of_chaint   startt   endt   r0t   c0t   r1t   c1t   drt   dct   segment_anglet   segment_distt   segment_coordst   segment_distst   dr0t   dc0t   dr1t   dc1t   projected_lengths0t   projected_lengths1t   perpt   euclt   new_end(    (    s7   lib/python2.7/site-packages/skimage/measure/_polygon.pyt   approximate_polygon   sP    

	

$ C *i   i   i   i   i    i   i   i
   i   i   i   i#   i   i   iF   i   i8   c   
      C   sì  | t  k r t d ƒ ‚ n  t j |  d d d … f |  d d d … f k ƒ } d } | r |  d d … d d … f }  d } n  t  | \ } } t j | t j ƒ d | } t j | t j ƒ d | } t j |  j t j	 | ƒ d | d	 d
 ƒ} t j |  j t j	 | ƒ d | d	 d
 ƒ} t j
 | j d | j d d f ƒ }	 | j |	 d d d … <| j |	 d d d … <| r t j |	 |	 d d d … f g ƒ }	 n  | rè| rèt j |  d d d … f |	 |  d d d … f g ƒ }	 n  |	 S(   s¸  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
    s8   Invalid B-Spline degree. Only degree 1 - 7 is supported.i    Niÿÿÿÿt   validt   samei   t   modet   boundaryt   wrapi   (   t   _SUBDIVISION_MASKSt
   ValueErrorR   t   allt   arrayt   floatR    t
   convolve2dt   Tt
   atleast_2dR   R   t   vstack(
   R   t   degreet   preserve_endst   circulart   methodt	   mask_event   mask_oddt   event   oddt   out(    (    s7   lib/python2.7/site-packages/skimage/measure/_polygon.pyt   subdivide_polygonm   s,    5	!	!	'(;(   t   numpyR   t   scipyR    R/   R5   R   RG   (    (    (    s7   lib/python2.7/site-packages/skimage/measure/_polygon.pyt   <module>   s   	[%%.