
    h|"                    P   S SK Jr  S SKJrJrJrJr  S SKrS SKJ	r	J
r
JrJrJrJrJrJr  S SKJr  / SQr\" S\S9r\\\
4   rSS	 jrSS
 jr S       SS jjrSS jr      SS jrSS jrSS jrSS.SS jjrSS.S S jjrS!S jr        S"S jr         S#S jr!g)$    )annotations)IterableUnionSequenceTypeVarN)BSplineBezier4PBezier3PUVecVec3Vec2AnyVecBoundingBox)cubic_equation)bezier_to_bsplinequadratic_to_cubic_bezier have_bezier_curves_g1_continuity	AnyBezierreverse_bezier_curvessplit_bezierquadratic_bezier_from_3pcubic_bezier_from_3pcubic_bezier_bboxquadratic_bezier_bbox intersection_ray_cubic_bezier_2dT)boundc                n    U R                   u  pnUSX!-
  -  S-  -   nUSX#-
  -  S-  -   n[        XXS45      $ )u{   Convert quadratic Bèzier curves (:class:`ezdxf.math.Bezier3P`) into
cubic Bèzier curves (:class:`ezdxf.math.Bezier4P`).

      )control_pointsr	   )curvestartcontrolend	control_1	control_2s         G/var/www/html/env/lib/python3.13/site-packages/ezdxf/math/curvetools.pyr   r   &   sP    
  ..ECW_-11Ia7=)A--IUy677    c                z   S	S jnU  Vs/ s H
  o!" U5      PM     nn[        U5      S:X  a  [        S5      e[        US   5      nUSS  H  nUR                  USS 5        M     / SQn[        U5      n[	        SU5       H  nUR                  XwU45        M     UR                  XfXf45        [        USUS9$ s  snf )
u\  Convert multiple quadratic or cubic Bèzier curves into a single cubic
B-spline.

For good results the curves must be lined up seamlessly, i.e. the starting
point of the following curve must be the same as the end point of the
previous curve. G1 continuity or better at the connection points of the
Bézier curves is required to get best results.

c                f    U R                   n[        U5      S:  a  [        U 5      R                   $ U$ )N   )r!   lenr   )bezierpointss     r(   
get_points%bezier_to_bspline.<locals>.get_points=   s.    &&v;?,V4CCCMr)   r   u#   one or more Bézier curves required   N)r   r   r   r   r,   )orderknots)r.   r   )r-   
ValueErrorlistextendranger   )curvesr0   cbezier_curve_pointsr!   r4   nks           r(   r   r   1   s     399&Q:a=&9
1$>?? -a01N $ae$ %E A1a[aAY 	LL!>%88 :s   B8c                   [        U R                  5      n[        UR                  5      nUS   R                  US   5      (       d  g US   US   -
  R                  5       n US   US   -
  R                  5       n[
        R                  " UR                  U5      SUS9$ ! [         a     gf = f! [         a     gf = f)uM   Return ``True`` if the given adjacent Bézier curves have G1 continuity.
    r   Fr2         ?abs_tol)tupler!   isclose	normalizeZeroDivisionErrormathdot)b1b2g1_tolb1_pntsb2_pntstetss          r(   r   r   T   s    
 B%%&GB%%&G2;wqz**bkGBK'224aj71:%002
 <<r
C88  
  s$   	B! #B1 !
B.-B.1
B>=B>c                J    [        S U  5       5      n U R                  5         U $ )Nc              3  @   #    U  H  oR                  5       v   M     g 7fN)reverse).0r:   s     r(   	<genexpr>(reverse_bezier_curves.<locals>.<genexpr>n   s     .v!))++vs   )r6   rT   )r9   s    r(   r   r   m   s!    .v..F
NNMr)   c                   ^^^^ [        U 5      S:  a  [        S5      eTS:  d  TS:  a  [        S5      e/ m/ mSUUUU4S jjmT" U 5        TT4$ )u  Split a Bèzier curve at parameter `t`.

Returns the control points for two new Bèzier curves of the same degree
and type as the input curve. (source: `pomax-1`_)

Args:
     control_points: of the Bèzier curve as :class:`Vec2` or :class:`Vec3`
        objects. Requires 3 points for a quadratic curve, 4 points for a
        cubic curve , ...
     t: parameter where to split the curve in the range [0, 1]

.. _pomax-1: https://pomax.github.io/bezierinfo/#splitting

r   z!2 or more control points required        rA   z%parameter `t` must be in range [0, 1]c                   >^  [        T 5      S-
  nTR                  T S   5        TR                  T U   5        US:X  a  g T" [        U U4S j[        U5       5       5      5        g )Nr2   r   c              3  R   >#    U  H  nTU   S T-
  -  TUS-      T-  -   v   M     g7f)rA   r2   N )rU   ir/   ts     r(   rV   .split_bezier.<locals>.split.<locals>.<genexpr>   s0     NX&)sQw'&Q-!*;;Xs   $')r-   appendrD   r8   )r/   r<   leftrightsplitr^   s   ` r(   rc   split_bezier.<locals>.split   sT    VqF1IVAY6NU1XNN	
r)   )r/   Sequence[T])r-   r5   )r!   r^   ra   rb   rc   s    `@@@r(   r   r   s   s_    " >Q<==3w!c'@AADE
 
 
.;r)   c                   SS jnSS jn[        U 5      n[        U5      n[        U5      nXV-
  R                  nXv-
  R                  n	XU	-   -  n
U" U
5      nX[-  USU-
  -  -   nXfU-
  U" U
5      -  -   n[        X]U/5      $ )u   Returns a quadratic Bèzier curve :class:`Bezier3P` from three points.
The curve starts at `p1`, goes through `p2` and ends at `p3`.
(source: `pomax-2`_)

.. _pomax-2: https://pomax.github.io/bezierinfo/#pointcurves

c                (    SU -
  nX-  nX U -  U-   -  $ NrA   r\   )r^   mtmt2s      r(   u_func(quadratic_bezier_from_3p.<locals>.u_func   s#    1Wg!eck""r)   c                F    X -  nSU -
  nX"-  n[        X-   S-
  X-   -  5      $ rh   )abs)r^   t2ri   rj   s       r(   ratio'quadratic_bezier_from_3p.<locals>.ratio   s1    U1WgBHsNrx011r)   rA   )r^   floatreturnrr   )r   	magnituder
   )p1p2p3rk   rp   sbed1d2r^   ur:   as                 r(   r   r      s    #
2 	RARARA
%		B
%		B
2gAq	A	S1WA	UeAhAQ1Ir)   c                0    [        XU5      n[        U5      $ )u   Returns a cubic Bèzier curve :class:`Bezier4P` from three points.
The curve starts at `p1`, goes through `p2` and ends at `p3`.
(source: `pomax-2`_)
)r   r   )ru   rv   rw   qbezs       r(   r   r      s    
 $BB/D$T**r)   g-q=rB   c                  U R                   nUS   US   /n[        U6  GH)  u  pEpgSU* SU-  -   SU-  -
  U-   -  nSUSU-  -
  U-   -  n	SXT-
  -  n
[        U5      U:  aK  [        U	5      U:  a  U
* nOU
* U	-  nSUs=:  a  S:  a#  O  O UR                  U R	                  U5      5        M   [
        R                  " X-  SU-  U
-  -
  5      nSU-  nU	* U-   U-  nSUs=:  a  S:  a#  O  O UR                  U R	                  U5      5        U	* U-
  U-  nSUs=:  a	  S:  d  GM  O  GM	  UR                  U R	                  U5      5        GM,     [        U5      $ ! [         a     GMG  f = f)	um   Returns the :class:`~ezdxf.math.BoundingBox` of a cubic Bézier curve
of type :class:`~ezdxf.math.Bezier4P`.
r   r          @      @g       @rY   rA   g      @)	r!   ziprn   r`   pointrH   sqrtr5   r   )r"   rC   cpr/   ru   rv   rw   p4r~   ry   r:   r^   
sqrt_bb4acaas                 r(   r   r      sp    
		BQ%AFr(B3r>C"H,r122b=2%&27Oq6G1vBBFQ}}u{{1~/	1537Q;#67J 1WR*_"=S=MM%++a.)R*_"=S==MM%++a.)/ #0 v  		s   +!E
E'&E'c               (    [        [        U 5      US9$ )uq   Returns the :class:`~ezdxf.math.BoundingBox` of a quadratic Bézier curve
of type :class:`~ezdxf.math.Bezier3P`.
rB   )r   r   )r"   rC   s     r(   r   r      s     6u=wOOr)   c                X    U S-  nUS-  nUS-  nU * U-   U-
  U-   XAS-  -
  U-   U* U-   U 4$ )Nr   r   r\   )r~   ry   r:   da3b3c3s          r(   _bezier4polyr      sL    	
SB	
SB	
SB27R<!Rc'\B.b!;;r)   c                   UR                   U R                   -
  nU R                  UR                  -
  nU R                  U R                   UR                   -
  -  U R                   UR                  U R                  -
  -  -   nUu  pgp[        UR                  UR                  UR                  U	R                  5      n
[        UR                   UR                   UR                   U	R                   5      n[        S [	        X:S   -  XKS   -  -   X:S   -  XKS   -  -   X:S   -  XKS   -  -   X:S   -  XKS   -  -   U-   5       5       5      $ )u   Returns the parameters of the intersection points between the ray defined
by two points `p0` and `p1` and the cubic Bézier curve defined by four
control points `cp`.
c              3  L   #    U  H  nS Us=::  a  S::  d  M  O  M  Uv   M     g7f)rY   rA   Nr\   )rU   vs     r(   rV   7intersection_params_ray_cubic_bezier.<locals>.<genexpr>   s2      	
A !?s? 	
  	

s   $$	$r   r2   r   r    )yxr   sortedr   )p0ru   r   ABCc0c1c2r   bxbys               r(   $intersection_params_ray_cubic_bezierr      s    	rttA
rttA
rttrttrttbdd{33ANBB	bddBDD"$$	-B	bddBDD"$$	-B 	1IqE	!1IqE	!1IqE	!1IqE	!A%	
	 	 	r)   c           	        ^ [         R                  " U4S j[        [        U 5      [        U5      TR                  5       5       5      $ )u  Returns the intersection points between the `ray` defined by two points
`p0` and `p1` and the given cubic Bézier `curve`. Ignores the z-axis of 3D
curves.

Returns 0-3 intersection points as :class:`Vec2` objects in the
order start- to end point of the curve.

c              3  F   >#    U  H  nTR                  U5      v   M     g 7frS   )r   )rU   r^   r"   s     r(   rV   3intersection_ray_cubic_bezier_2d.<locals>.<genexpr>  s&      
A 	A
s   !)r   rD   r   r!   )r   ru   r"   s     `r(   r   r     s=     :: 5Hd2h 4 4
  r)   )r"   r
   rs   r	   )r9   zIterable[AnyBezier]rs   r   )g-C6?)rJ   r   rK   r   rL   rr   rs   bool)r9   list[AnyBezier]rs   r   )r!   re   r^   rr   rs   ztuple[list[T], list[T]])ru   r   rv   r   rw   r   rs   r
   )ru   r   rv   r   rw   r   rs   r	   )r"   r	   rs   r   )r"   r
   rs   r   )r~   rr   ry   rr   r:   rr   r   rr   )r   r   ru   r   r   zSequence[AnyVec]rs   zlist[float])r   r   ru   r   r"   r	   rs   zSequence[Vec2])"
__future__r   typingr   r   r   r   rH   
ezdxf.mathr   r	   r
   r   r   r   r   r   ezdxf.math.linalgr   __all__r   r   r   r   r   r   r   r   r   r   r   r   r   r   r\   r)   r(   <module>r      s   # 5 5 	 	 	 - Cv(H$%	8 9H 3799 9*/9	92##$)##L@+ 38 B 7< P< 04  	r)   