3
6Ad]ä;  ã               @   s"  d Z ddlZddljZddlmZ G dd„ deƒZdd„ Z	dd	„ Z
d
d„ Zdd„ Zejdddƒd1dd„ƒZG dd„ deƒZejdddƒd2dd„ƒZejdƒejdddƒd3dd„ƒƒZejdddƒd4dd„ƒZdd „ Zd!d"„ Zejdddƒd5d$d%„ƒZd&d'„ Zd(d)„ Zd6d+d,„Zd-d.„ Zd/d0„ ZdS )7zO
A module providing some utility functions regarding bezier path manipulation.
é    N)ÚPathc               @   s   e Zd ZdS )ÚNonIntersectingPathExceptionN)Ú__name__Ú
__module__Ú__qualname__© r   r   ú|/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.6/site-packages/matplotlib-3.1.1-py3.6-linux-x86_64.egg/matplotlib/bezier.pyr      s   r   c                sÀ   ||  ||  }|| ||  }	||  }
}||  }}|
| ||  ‰ t jˆ ƒdk r^tdƒ‚||  }}| |
 }}‡ fdd„||||gD ƒ\}}}}|| ||	  }|| ||	  }||fS )z‹
    Return the intersection between the line through (*cx1*, *cy1*) at angle
    *t1* and the line through (*cx2, cy2) at angle *t2*.
    gê-�™—q=zcGiven lines do not intersect. Please verify that the angles are not equal or differ by 180 degrees.c                s   g | ]}|ˆ  ‘qS r   r   )Ú.0Úk)Úad_bcr   r   ú
<listcomp>*   s    z$get_intersection.<locals>.<listcomp>)ÚnpÚabsÚ
ValueError)Úcx1Úcy1Úcos_t1Úsin_t1Úcx2Úcy2Úcos_t2Úsin_t2Z	line1_rhsZ	line2_rhsÚaÚbÚcÚdZa_Zb_Úc_Zd_ÚxÚyr   )r   r   Úget_intersection   s    
"r   c             C   sl   |dkr| || |fS ||  }}| | }}|| |  || |  }	}
|| |  || |  }}|	|
||fS )z¶
    For a line passing through (*cx*, *cy*) and having a angle *t*, return
    locations of the two points located along its perpendicular line at the
    distance of *length*.
    g        r   )ÚcxÚcyÚcos_tÚsin_tÚlengthr   r   r   r   Úx1Úy1Úx2Úy2r   r   r   Úget_normal_points2   s    r)   c             C   s(   | d d… d|  | dd … |  }|S )Né   éÿÿÿÿr   )ÚbetaÚtZ	next_betar   r   r   Ú_de_casteljau1K   s    $r.   c             C   s`   t j| ƒ} | g}x&t| |ƒ} |j| ƒ t| ƒdkrP qW dd„ |D ƒ}dd„ t|ƒD ƒ}||fS )z”
    Split a bezier segment defined by its control points *beta* into two
    separate segments divided at *t* and return their control points.
    r*   c             S   s   g | ]}|d  ‘qS )r   r   )r	   r,   r   r   r   r   \   s    z&split_de_casteljau.<locals>.<listcomp>c             S   s   g | ]}|d ‘qS )r*   r+   r   )r	   r,   r   r   r   r   ]   s    )r   Úasarrayr.   ÚappendÚlenÚreversed)r,   r-   Z	beta_listZ	left_betaZ
right_betar   r   r   Úsplit_de_casteljauP   s    


r3   z3.1Z	tolerenceÚ	toleranceç        ç      ð?ç{®Gáz„?c             C   s°   | |ƒ}| |ƒ}||ƒ}||ƒ}||kr8||kr8t dƒ‚xrtj|d |d  |d |d  ƒ|k rj||fS d||  }	| |	ƒ}
||
ƒ}||A rœ|	}|
}|}q:|	}|
}|}q:W dS )a$   Find a parameter t0 and t1 of the given bezier path which
    bounds the intersecting points with a provided closed
    path(*inside_closedpath*). Search starts from *t0* and *t1* and it
    uses a simple bisecting algorithm therefore one of the end point
    must be inside the path while the other doesn't. The search stop
    when |t0-t1| gets smaller than the given tolerance.
    value for

    - bezier_point_at_t : a function which returns x, y coordinates at *t*

    - inside_closedpath : return True if the point is inside the path

    z3Both points are on the same side of the closed pathr   r*   g      à?N)r   r   Úhypot)Úbezier_point_at_tÚinside_closedpathÚt0Út1r4   ÚstartÚendZstart_insideZ
end_insideZmiddle_tZmiddleZmiddle_insider   r   r   Ú*find_bezier_t_intersecting_with_closedpathb   s(    (r?   c               @   sP   e Zd ZdZejddgƒejdddgƒejddddgƒdœZdd„ Zdd	„ Zd
S )ÚBezierSegmentz:
    A simple class of a 2-dimensional bezier segment
    g      ð?g       @g      @)r*   é   é   c             C   sJ   t |ƒ}tj|ƒ| _tj|d  }tj|ƒj\}}|| | _|| | _	dS )z³
        *control_points* : location of contol points. It needs have a
         shape of n * 2, where n is the order of the bezier line. 1<=
         n <= 3 is supported.
        r*   N)
r1   r   ÚarangeÚ_ordersr@   Ú_binom_coeffr/   ÚTÚ_pxÚ_py)ÚselfZcontrol_pointsÚ_oZ_coeffÚxxÚyyr   r   r   Ú__init__ž   s    
zBezierSegment.__init__c             C   sF   d| | j  ddd… || j   }tj|| jƒ}tj|| jƒ}||fS )zevaluate a point at tr*   Nr+   )rD   r   ÚdotrG   rH   )rI   r-   ÚttÚ_xÚ_yr   r   r   Ú
point_at_t¬   s    "zBezierSegment.point_at_tN)	r   r   r   Ú__doc__r   ÚarrayrE   rM   rR   r   r   r   r   r@   “   s   r@   c       	      C   s>   t | ƒ}|j}t|||d�\}}t| || d ƒ\}}||fS )z¥
    bezier : control points of the bezier segment
    inside_closedpath : a function which returns true if the point is inside
                        the path
    )r4   g       @)r@   rR   r?   r3   )	Úbezierr:   r4   Zbzr9   r;   r<   Ú_leftÚ_rightr   r   r   Ú)split_bezier_intersecting_with_closedpath´   s    
rX   c                s0   |\‰‰‡ ‡‡‡fdd„}t || |||d� dS )zç
    Find a radius r (centered at *xy*) between *rmin* and *rmax* at
    which it intersect with the path.

    inside_closedpath : function
    cx, cy : center
    cos_t, sin_t : cosine and sine for the angle
    rmin, rmax :
    c                s   ˆ |  ˆ ˆ|  ˆ fS )Nr   )Úr)r"   r    r!   r#   r   r   Ú_fØ   s    z,find_r_to_boundary_of_closedpath.<locals>._f)r;   r<   r4   N)r?   )r:   Úxyr"   r#   ÚrminÚrmaxr4   rZ   r   )r"   r    r!   r#   r   Ú find_r_to_boundary_of_closedpathÈ   s    r^   Fc             C   s  | j ƒ }t|ƒ\}}||d	d… ƒ}|}tj}	d}
d}xZ|D ]J\}}|}
|t|ƒd 7 }||d
d… ƒ|kr‚|	|dd… |gƒ}P |}q<W tdƒ‚|jdƒ}t|||ƒ\}}t|ƒdkrÎtj	g}tj
tj	g}nht|ƒdk� rútjtjg}tj
tjtjg}n<t|ƒdk�r.tjtjtjg}tj
tjtjtjg}ntdƒ‚|dd… }|dd… }| jdk�r�t|	| jd|… |gƒƒ}t|	|| j|d… gƒƒ}n\t|	| jd|
… |gƒ|	| jd|
… |gƒƒ}t|	|| j|d… gƒ|	|| j|d… gƒƒ}|�r| �r|| }}||fS )zW divide a path into two segment at the point where inside(x, y)
    becomes False.
    rA   Nr   r*   z*The path does not intersect with the patchrB   é   zThis should never be reachedéþÿÿÿr`   r`   r+   )r+   rA   )Úiter_segmentsÚnextr   Úconcatenater1   r   ÚreshaperX   r   ÚLINETOÚMOVETOÚCURVE3ÚCURVE4ÚAssertionErrorÚcodesÚvertices)ÚpathÚinsider4   Zreorder_inoutZ	path_iterZ
ctl_pointsÚcommandZbegin_insideZctl_points_oldÚconcatZioldÚiZbezier_pathÚbpÚleftÚrightZ
codes_leftZcodes_rightZ
verts_leftZverts_rightÚpath_inÚpath_outr   r   r   Úsplit_path_inoutá   sP    

rv   c                s   |d ‰‡ ‡‡fdd„}|S )NrA   c                s$   | \}}|ˆ  d |ˆ d  ˆk S )NrA   r   )r[   r   r   )r    r!   Úr2r   r   rZ   #  s    zinside_circle.<locals>._fr   )r    r!   rY   rZ   r   )r    r!   rw   r   Úinside_circle   s    rx   c             C   sB   ||  ||  }}|| ||  d }|dkr2dS || || fS )Ng      à?r   ç        )ry   ry   r   )Úx0Úy0r%   r&   ÚdxÚdyr   r   r   r   Úget_cos_sin+  s
    r~   çñhãˆµøä>c             C   sR   t j| |ƒ}t j||ƒ}t j|| ƒ}||k r2dS t j|t j ƒ|k rJdS dS dS )z• returns
       * 1 if two lines are parallel in same direction
       * -1 if two lines are parallel in opposite direction
       * 0 otherwise
    r*   FNr+   )r   Úarctan2r   Úpi)Údx1Údy1Údx2Údy2r4   Útheta1Útheta2Údthetar   r   r   Úcheck_if_parallel4  s    r‰   c          	   C   sn  | d \}}| d \}}| d \}}t || || || || ƒ}|dkrrtjdƒ t||||ƒ\}	}
|	|
 }}n$t||||ƒ\}	}
t||||ƒ\}}t|||	|
|ƒ\}}}}t|||||ƒ\}}}}|dk�rd||  d||   }}d||  d||   }}n4t|||	|
||||ƒ\}}t|||	|
||||ƒ\}}||f||f||fg}||f||f||fg}||fS )z«
    Given the quadratic bezier control points *bezier2*, returns
    control points of quadratic bezier lines roughly parallel to given
    one separated by *width*.
    r   r*   rA   z8Lines do not intersect. A straight line is used instead.g      à?r+   )r‰   ÚcbookÚ_warn_externalr~   r)   r   )Úbezier2ÚwidthÚc1xÚc1yÚcmxÚcmyÚc2xÚc2yZparallel_testr   r   r   r   Úc1x_leftÚc1y_leftÚ	c1x_rightÚ	c1y_rightZc2x_leftZc2y_leftZ	c2x_rightZ	c2y_rightZcmx_leftZcmy_leftZ	cmx_rightZ	cmy_rightÚ	path_leftÚ
path_rightr   r   r   Úget_parallelsF  s<    


rš   c             C   s>   dd| | |   }dd| ||   }| |f||f||fgS )z�
    Find control points of the Bezier curve passing through (*c1x*, *c1y*),
    (*mmx*, *mmy*), and (*c2x*, *c2y*), at parametric values 0, 0.5, and 1.
    g      à?r_   r   )rŽ   r�   ZmmxZmmyr’   r“   r�   r‘   r   r   r   Úfind_control_points�  s    r›   ç      à?c       %      C   s(  | d \}}| d \}}| d \}	}
t ||||ƒ\}}t |||	|
ƒ\}}t|||||| ƒ\}}}}t|	|
|||| ƒ\}}}}|| d || d  }}||	 d ||
 d  }}|| d || d  }}t ||||ƒ\}}t|||||| ƒ\}} }!}"t|||| ||ƒ}#t|||!|"||ƒ}$|#|$fS )z©
    Being similar to get_parallels, returns control points of two quadratic
    bezier lines having a width roughly parallel to given one separated by
    *width*.
    r   r*   rA   g      à?)r~   r)   r›   )%rŒ   r�   Úw1ÚwmÚw2rŽ   r�   r�   r‘   Zc3xZc3yr   r   r   r   r”   r•   r–   r—   Zc3x_leftZc3y_leftZ	c3x_rightZ	c3y_rightZc12xZc12yZc23xZc23yZc123xZc123yZcos_t123Zsin_t123Z
c123x_leftZ
c123y_leftZc123x_rightZc123y_rightr˜   r™   r   r   r   Úmake_wedged_bezier2š  s&    r    c             C   sF   | j }|dkr>tjt| jƒtjtjd�}tj|d< t| j|ƒS | S dS )z¾
    If the :attr:`codes` attribute of `Path` *p* is None, return a copy of *p*
    with the :attr:`codes` set to (MOVETO, LINETO, LINETO, ..., LINETO);
    otherwise return *p* itself.
    N)Údtyper   )	rj   r   Úfullr1   rk   r   re   Ú	code_typerf   )Úpr   r   r   r   Úmake_path_regularÍ  s    
r¥   c             C   s2   t jdd„ | D ƒƒ}t jdd„ | D ƒƒ}t||ƒS )z/Concatenate a list of paths into a single path.c             S   s   g | ]
}|j ‘qS r   )rk   )r	   r¤   r   r   r   r   Þ  s    z%concatenate_paths.<locals>.<listcomp>c             S   s   g | ]}t |ƒj‘qS r   )r¥   rj   )r	   r¤   r   r   r   r   ß  s    )r   rc   r   )Úpathsrk   rj   r   r   r   Úconcatenate_pathsÜ  s    r§   )r5   r6   r7   )r7   )r5   r6   r7   )r7   F)r   )r6   rœ   r5   )rS   Únumpyr   Úmatplotlib.cbookrŠ   Úmatplotlib.pathr   r   r   r   r)   r.   r3   Ú_rename_parameterr?   Úobjectr@   rX   Ú
deprecatedr^   rv   rx   r~   r‰   rš   r›   r    r¥   r§   r   r   r   r   Ú<module>   s6   
!/!>	J

3