Ë
    £èhš#  ã                  ó8  — d dl mZ d dlmZmZmZmZ d dlZd dlm	Z	m
Z
mZmZmZmZmZmZ d dlmZ g d¢Z ede¬«      Zeee
f   Zdd	„Zdd
„Z	 d	 	 	 	 	 	 	 dd„Zdd„Z	 	 	 	 	 	 dd„Zdd„Zdd„Zddœdd„Zddœd d„Zd!d„Z	 	 	 	 	 	 	 	 d"d„Z 	 	 	 	 	 	 	 	 d#d„Z!y)$é    )Ú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                óx   — | j                   \  }}}|d||z
  z  dz  z   }|d||z
  z  dz  z   }t        ||||f«      S )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         ú5lib/python3.12/site-packages/ezdxf/math/curvetools.pyr   r   &   sZ   € ð
  ×.Ñ.Ñ€Eˆ7�CØ˜˜W u™_Ñ-°Ñ1Ñ1€IØ�a˜7 S™=Ñ)¨AÑ-Ñ-€IÜ�U˜I y°#Ð6Ó7Ð7ó    c                óv  — d	d„}| D �cg c]
  } ||«      ‘Œ }}t        |«      dk(  rt        d«      ‚t        |d   «      }|dd D ]  }|j                  |dd «       Œ g d¢}t        |«      }t	        d|«      D ]  }|j                  |||f«       Œ |j                  ||||f«       t        |d|¬«      S c c}w )
ut  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                ód   — | j                   }t        |«      dk  rt        | «      j                   S |S )Né   )r!   Úlenr   )ÚbezierÚpointss     r(   Ú
get_pointsz%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!   r3   ÚnÚks           r(   r   r   1   sÍ   € óñ 39Ó9±&¨Q™: a�=°&ÐÐ9Ü
ÐÓ 1Ò$ÜÐ>Ó?Ð?ô Ð-¨aÑ0Ó1€NØ   Ó$ˆØ×Ñ˜a  ˜eÕ$ð %â€EÜÐÓ €AÜ�1�aŽ[ˆØ�‰�a˜˜A�YÕð à	‡L�L�!�Q˜˜1�ÔÜ�>¨°%Ô8Ð8ùò :s   ‰B6c                óx  — t        | j                  «      }t        |j                  «      }|d   j                  |d   «      sy	 |d   |d   z
  j                  «       }	 |d   |d   z
  j                  «       }t        j                  |j                  |«      d|¬«      S # t        $ r Y yw xY w# t        $ r Y yw xY w)uM   Return ``True`` if the given adjacent BÃ©zier curves have G1 continuity.
    éÿÿÿÿr   Féþÿÿÿr1   ç      ð?©Ú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‰;×Ñ˜w q™zÔ*ØðØ�b‰k˜G B™KÑ'×2Ñ2Ó4ˆðØ�a‰j˜7 1™:Ñ%×0Ñ0Ó2ˆô
 �<‰<˜Ÿ™˜r›
 C°Ô8Ð8øô ò Ùðûô
 ò Ùðús$   ÁB ÁB- Â	B*Â)B*Â-	B9Â8B9c                óJ   — t        d„ | D «       «      } | j                  «        | S )Nc              3  ó<   K  — | ]  }|j                  «       –— Œ y ­w©N)Úreverse)Ú.0r9   s     r(   Ú	<genexpr>z(reverse_bezier_curves.<locals>.<genexpr>n   s   è ø€ Ð.¡v !�!—)‘)—+¡vùó   ‚)r5   rS   )r8   s    r(   r   r   m   s!   € ÜÑ.¡vÓ.Ó.€FØ
‡N�NÔØ€Mr)   c                ó˜   ‡‡‡‡— t        | «      dk  rt        d«      ‚‰dk  s‰dkD  rt        d«      ‚g Šg Šdˆˆˆˆfd„Š ‰| «       ‰‰fS )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ç        r@   z%parameter `t` must be in range [0, 1]c                óÈ   •‡ — t        ‰ «      dz
  }‰j                  ‰ d   «       ‰j                  ‰ |   «       |dk(  ry  ‰t        ˆ ˆfd„t        |«      D «       «      «       y )Nr1   r   c              3  óL   •K  — | ]  }‰|   d ‰z
  z  ‰|dz      ‰z  z   –— Œ y­w)r@   r1   N© )rT   Úir/   Úts     €€r(   rU   z.split_bezier.<locals>.split.<locals>.<genexpr>’   s1   øè ø€ ÐNÁXÀ�&˜‘)˜s Q™wÑ'¨&°°Q±©-¸!Ñ*;Õ;ÁXùs   ƒ!$)r-   ÚappendrC   r7   )r/   r;   ÚleftÚrightÚsplitr]   s   ` €€€€r(   ra   zsplit_bezier.<locals>.split‹   sT   ù€ Ü�V“˜q‘ˆØ�‰�F˜1‘IÔØ�‰�V˜A‘YÔØ�Š6ØÙÜÔNÄUÈ1ÄXÓNÓNõ	
r)   )r/   úSequence[T])r-   r4   )r!   r]   r_   r`   ra   s    `@@@r(   r   r   s   s_   û€ ô" ˆ>Ó˜QÒÜÐ<Ó=Ð=Øˆ3‚w�!�c’'ÜÐ@ÓAÐAØ€DØ€E÷
ð 
ñ 
ˆ.ÔØ�ˆ;Ðr)   c                ó
  — dd„}dd„}t        | «      }t        |«      }t        |«      }||z
  j                  }||z
  j                  }	|||	z   z  }
 ||
«      }||z  |d|z
  z  z   }|||z
   ||
«      z  z   }t        |||g«      S )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                ó,   — d| z
  }||z  }|| | z  |z   z  S ©Nr@   r[   )r]   ÚmtÚmt2s      r(   Úu_funcz(quadratic_bezier_from_3p.<locals>.u_func¢   s'   € Ø�1‰WˆØ�2‰gˆØ�a˜!‘e˜c‘kÑ"Ð"r)   c                óN   — | | z  }d| z
  }||z  }t        ||z   dz
  ||z   z  «      S re   )Úabs)r]   Út2rf   rg   s       r(   Úratioz'quadratic_bezier_from_3p.<locals>.ratio§   s9   € Ø�‰UˆØ�1‰WˆØ�2‰gˆÜ�B˜‘H˜s‘N r¨C¡xÑ0Ó1Ð1r)   r@   )r]   ÚfloatÚreturnrm   )r   Ú	magnituder
   )Úp1Úp2Úp3rh   rl   ÚsÚbÚeÚd1Úd2r]   Úur9   Úas                 r(   r   r   ™   s›   € ó#ó
2ô 	ˆR‹€AÜˆR‹€AÜˆR‹€AØ
ˆa‰%×	Ñ	€BØ
ˆa‰%×	Ñ	€BØ
ˆb�2‰g‰€AÙˆq‹	€AØ	ˆA‰��S˜1‘W‘Ñ€AØ	ˆQ�‰U‘e˜A“hÑÑ€AÜ�Q˜˜1�IÓÐr)   c                ó2   — t        | ||«      }t        |«      S )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   )rp   rq   rr   Úqbezs       r(   r   r   ¹   s   € ô
 $ B¨¨BÓ/€DÜ$ TÓ*Ð*r)   gê-�™—q=rA   c               ó¸  — | j                   }|d   |d   g}t        |Ž D �]  \  }}}}d| d|z  z   d|z  z
  |z   z  }d|d|z  z
  |z   z  }	d||z
  z  }
t        |«      |k  rGt        |	«      |k  r|
 }n|
 |	z  }d|cxk  rdk  r#n n |j                  | j	                  |«      «       Œ‰	 t        j                  |	|	z  d|z  |
z  z
  «      }d|z  }|	 |z   |z  }d|cxk  rdk  r#n n |j                  | j	                  |«      «       |	 |z
  |z  }d|cxk  rdk  sŒün Œÿ|j                  | j	                  |«      «       �Œ! t        |«      S # t        $ r Y �Œ9w xY w)	uu   Returns the :class:`~ezdxf.math.BoundingBox` of a cubic BÃ©zier curve
    of type :class:`~ezdxf.math.Bezier4P`.
    r   r    ç      @ç      @g       @rX   r@   g      @)	r!   Úziprj   r^   ÚpointrG   Úsqrtr4   r   )r"   rB   Úcpr/   rp   rq   rr   Úp4ry   rt   r9   r]   Ú
sqrt_bb4acÚaas                 r(   r   r   Â   sv  € ð 
×	Ñ	€BØ˜Q™%  A¡˜€FÜ˜rœ(‰ˆˆB��BØ�B�3˜˜r™‘> C¨"¡HÑ,¨rÑ1Ñ2ˆØ�2˜˜b™‘= 2Ñ%Ñ&ˆØ�2˜‘7‰OˆÜˆq‹6�GÒÜ�1‹v˜ÒØ�B‘à�B˜‘F�Ø�QŒ}˜�}Ø—‘˜uŸ{™{¨1›~Ô/Øð	ÜŸ™ 1 q¡5¨3°©7°Q©;Ñ#6Ó7ˆJð �1‰WˆØˆR�*‰_ Ñ"ˆØ�Œ=�S�=Ø�M‰M˜%Ÿ+™+ a›.Ô)ØˆR�*‰_ Ñ"ˆØ�Œ=�SŽ=Ø�M‰M˜%Ÿ+™+ a›.Ö)ð/ #ô0 �vÓÐøô ò 	Úð	ús   Â)!EÅ	EÅEc               ó.   — t        t        | «      |¬«      S )uy   Returns the :class:`~ezdxf.math.BoundingBox` of a quadratic BÃ©zier curve
    of type :class:`~ezdxf.math.Bezier3P`.
    rA   )r   r   )r"   rB   s     r(   r   r   ã   s   € ô Ô6°uÓ=ÀwÔOÐOr)   c                óZ   — | dz  }|dz  }|dz  }|  |z   |z
  |z   ||dz  z
  |z   | |z   | fS )Nr}   r~   r[   )ry   rt   r9   ÚdÚa3Úb3Úc3s          r(   Ú_bezier4polyrŒ   ê   sN   € Ø	
ˆS‰€BØ	
ˆS‰€BØ	
ˆS‰€BØˆ2�‰7�R‰<˜!Ñ˜R ! c¡'™\¨BÑ.°°°b±¸!Ð;Ð;r)   c                ó   — |j                   | j                   z
  }| j                  |j                  z
  }| j                  | j                   |j                   z
  z  | j                   |j                  | j                  z
  z  z   }|\  }}}}	t        |j                  |j                  |j                  |	j                  «      }
t        |j                   |j                   |j                   |	j                   «      }t        d„ t	        ||
d   z  ||d   z  z   ||
d   z  ||d   z  z   ||
d   z  ||d   z  z   ||
d   z  ||d   z  z   |z   «      D «       «      S )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  ó<   K  — | ]  }d |cxk  rdk  rn n|–— Œ y­w)rX   r@   Nr[   )rT   Úvs     r(   rU   z7intersection_params_ray_cubic_bezier.<locals>.<genexpr>   s)   è ø€ ð 	ñ
ˆAð �!Œ?�s�?ô 	
ñ
ùrV   r   r1   r   r    )ÚyÚxrŒ   Úsortedr   )Úp0rp   r‚   ÚAÚBÚCÚc0Úc1Úc2r‹   ÚbxÚbys               r(   Ú$intersection_params_ray_cubic_bezierrœ   ò   s+  € ð 	�‰ˆr�t‰t‰€AØ
�‰ˆr�t‰t‰€AØ
�‰�—‘�r—t‘t‘Ñ˜rŸt™t r§t¡t¨b¯d©d¡{Ñ3Ñ3€Aà�N€BˆˆB�Ü	�b—d‘d˜BŸD™D "§$¡$¨¯©Ó	-€BÜ	�b—d‘d˜BŸD™D "§$¡$¨¯©Ó	-€BÜñ 	äØ��1‘‰I˜˜B˜q™E™	Ñ!Ø��1‘‰I˜˜B˜q™E™	Ñ!Ø��1‘‰I˜˜B˜q™E™	Ñ!Ø��1‘‰I˜˜B˜q™E™	Ñ! AÑ%ô	
ó	ó 	ð 	r)   c           	     óŽ   ‡— t        j                  ˆfd„t        t        | «      t        |«      ‰j                  «      D «       «      S )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  ó@   •K  — | ]  }‰j                  |«      –— Œ y ­wrR   )r€   )rT   r]   r"   s     €r(   rU   z3intersection_ray_cubic_bezier_2d.<locals>.<genexpr>  s%   øè ø€ ð ñ
ˆAð 	�‰�A�ñ
ùs   ƒ)r   rC   rœ   r!   )r“   rp   r"   s     `r(   r   r     s=   ø€ ô �:‰:ó ä5Ü�‹H”d˜2“h × 4Ñ 4ô
óó ð r)   )r"   r
   rn   r	   )r8   zIterable[AnyBezier]rn   r   )g-Cëâ6?)rI   r   rJ   r   rK   rm   rn   Úbool)r8   úlist[AnyBezier]rn   r    )r!   rb   r]   rm   rn   ztuple[list[T], list[T]])rp   r   rq   r   rr   r   rn   r
   )rp   r   rq   r   rr   r   rn   r	   )r"   r	   rn   r   )r"   r
   rn   r   )ry   rm   rt   rm   r9   rm   rˆ   rm   )r“   r   rp   r   r‚   zSequence[AnyVec]rn   zlist[float])r“   r   rp   r   r"   r	   rn   zSequence[Vec2])"Ú
__future__r   Útypingr   r   r   r   rG   Ú
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Û ÷	÷ 	ó 	õ -ò€ñ ˆC�vÔ€à�(˜HÐ$Ñ%€	ó8ó 9ðH 37ð9Øð9Ø ð9Ø*/ð9à	ó9ó2ð#Øð#Ø$)ð#àó#óLó@+ð 38õ ðB 7<õ Pó<ðØðØðØ 0ðàóð4Øðàðð ðð ô	r)   