
    h<                    p   S SK Jr  S SKJrJrJr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  S SKJrJr  \	(       a  S SKJr  / SQr " S S	\
R0                  5      r " S
 S\5      r " S S5      r " S S\5      r " S S\5      r " S S\5      r " S S\5      r " S S\5      r SS jr!      SS jr"g)    )annotations)OptionalIterableIteratorSequence
NamedTupleCallableTYPE_CHECKINGN)Matrix44Vec2BoundingBox2dUVecis_convex_polygon_2dis_axes_aligned_rectangle_2d)NumpyPath2dNumpyPoints2d)Clipping)ClippingShapeClippingPortalClippingRectConvexClippingPolygonInvertedClippingPolygon	MultiClipfind_best_clipping_shapemake_inverted_clipping_shapec                     \ rS rSrSr\R                  SS j5       r\R                  SS j5       r\R                  SS j5       r	\R                  SS j5       r
\R                  SS j5       r\R                  SS j5       r\R                  SS	 j5       r\R                        SS
 j5       r\R                        SS j5       rSrg)r   (   a  The ClippingShape defines a single clipping path and executes the clipping on
basic geometries:

- point: a single point
- line: a line between two vertices
- polyline: open polyline with one or more straight line segments
- polygon: closed shape with straight line as edges
- path: open shape with straight lines and Bezier-curves as segments
- filled-path: closed shape with straight lines and Bezier-curves as edges

Difference between open and closed shapes:

    - an open shape is treated as a linear shape without a filling
    - clipping an open shape returns one or more open shapes
    - a closed shape is treated as a filled shape, where the first vertex is
      coincident to the last vertex.
    - clipping a closed shape returns one or more closed shapes

Notes:

    An arbitrary clipping polygon can split any basic geometry (except point) into
    multiple parts.

    All current implemented clipping algorithms flatten Bezier-curves into polylines.

c                    g N selfs    M/var/www/html/env/lib/python3.13/site-packages/ezdxf/tools/clipping_portal.pybboxClippingShape.bboxD   s    %(    c                    g r   r    r"   others     r#   is_completely_inside"ClippingShape.is_completely_insideG   s    BEr&   c                    g r   r    r(   s     r#   is_completely_outside#ClippingShape.is_completely_outsideL   s    CFr&   c                    g r   r    )r"   points     r#   
clip_pointClippingShape.clip_pointO   s    9<r&   c                    g r   r    r"   startends      r#   	clip_lineClippingShape.clip_lineR   s    PSr&   c                    g r   r    r"   pointss     r#   clip_polylineClippingShape.clip_polylineU   s    ORr&   c                    g r   r    r:   s     r#   clip_polygonClippingShape.clip_polygonX   s    NQr&   c                    g r   r    r"   pathsmax_sagittas      r#   
clip_pathsClippingShape.clip_paths[        !$r&   c                    g r   r    rB   s      r#   clip_filled_pathsClippingShape.clip_filled_paths`   rG   r&   r    Nreturnr   r)   r   rL   boolr0   r   rL   zOptional[Vec2]r5   r   r6   r   rL   zSequence[tuple[Vec2, Vec2]]r;   r   rL   zSequence[NumpyPoints2d]rC   Iterable[NumpyPath2d]rD   floatrL   zIterator[NumpyPath2d])__name__
__module____qualname____firstlineno____doc__abcabstractmethodr$   r*   r-   r1   r7   r<   r?   rE   rI   __static_attributes__r    r&   r#   r   r   (   s    6 	( (E E 	F F< <S SR RQ Q$*$9>$	$ $ 	$*$9>$	$ $r&   r   c                  *    \ rS rSr% S\S'   S\S'   Srg)ClippingStagef   r   portalMatrix44 | None	transformr    N)rU   rV   rW   rX   __annotations__r\   r    r&   r#   r^   r^   f   s    r&   r^   c                      \ rS rSrSrSS jr\SS j5       rSS jrSS jr	SS jr
SS jrSS	 jrSS
 jrSS jr      SS jr      SS jrSS jrSrg)r   k   z1The ClippingPortal manages a clipping path stack.c                    / U l         g r   _stagesr!   s    r#   __init__ClippingPortal.__init__n   s	    ,.r&   c                ,    [        U R                  5      $ r   )rN   rh   r!   s    r#   	is_activeClippingPortal.is_activeq   s    DLL!!r&   c                L    U R                   R                  [        X5      5        g r   )rh   appendr^   )r"   r`   rb   s      r#   pushClippingPortal.pushu   s    M&<=r&   c                \    U R                   (       a  U R                   R                  5         g g r   )rh   popr!   s    r#   rs   ClippingPortal.popx   s    <<LL r&   c                T    U R                   S S S2    H  nU" U5      (       a  M    g    g Nrg   )r"   commandstages      r#   foreach_stageClippingPortal.foreach_stage|   s%    \\$B$'E5>> (r&   c                >   ^ UmSU4S jjnU R                  U5        T$ )Nc                   > Tc   eU R                   (       a$  [        U R                   R                  T5      5      mU R                  R                  T5      mTS L$ r   )rb   r   r`   r1   )ry   results    r#   do%ClippingPortal.clip_point.<locals>.do   sN    %%%eoo77?@\\,,V4F%%r&   ry   r^   rL   rN   rz   )r"   r0   r   r~   s      @r#   r1   ClippingPortal.clip_point   s"    #	& 	2r&   c                B   ^ SU4S jjnX4/mU R                  U5        T$ )Nc                  > [        T5      nTR                  5         U H^  u  p#U R                  (       a  U R                  R                  X#45      u  p#TR	                  U R
                  R                  X#5      5        M`     [        T5      $ r   )listclearrb   fast_2d_transformextendr`   r7   rN   )ry   linesser~   s       r#   r   $ClippingPortal.clip_line.<locals>.do   sg    LELLN?? ??<<aVDDAell44Q:;  <r&   r   r   )r"   r5   r6   r   r~   s       @r#   r7   ClippingPortal.clip_line   s&    	  ,2r&   c                @   ^ SU4S jjnU/mU R                  U5        T$ )Nc                  > [        T5      nTR                  5         U HY  nU R                  (       a  UR                  U R                  5        TR	                  U R
                  R                  U5      5        M[     [        T5      $ r   )r   r   rb   transform_inplacer   r`   r<   rN   )ry   	polylinespolyliner~   s      r#   r   (ClippingPortal.clip_polyline.<locals>.do   s_    VILLN%??..u?ell88BC & <r&   r   r   r"   r;   r   r~   s      @r#   r<   ClippingPortal.clip_polyline   $    	  2r&   c                @   ^ SU4S jjnU/mU R                  U5        T$ )Nc                  > [        T5      nTR                  5         U HY  nU R                  (       a  UR                  U R                  5        TR	                  U R
                  R                  U5      5        M[     [        T5      $ r   )r   r   rb   r   r   r`   r?   rN   )ry   polygonspolygonr~   s      r#   r   'ClippingPortal.clip_polygon.<locals>.do   s_    F|HLLN#??--eoo>ell77@A $ <r&   r   r   r   s      @r#   r?   ClippingPortal.clip_polygon   r   r&   c                T   ^^ SUU4S jjn[        U5      mU R                  U5        T$ )Nc                  > [        T5      nTR                  5         U H1  nU R                  (       d  M  UR                  U R                  5        M3     TR	                  U R
                  R                  UT5      5        [        T5      $ r   )r   r   rb   r   r   r`   rE   rN   ry   rC   pathrD   r~   s      r#   r   %ClippingPortal.clip_paths.<locals>.do   sc    LELLN???**5??;  MM%,,11%EF<r&   r   r   rz   r"   rC   rD   r   r~   s     ` @r#   rE   ClippingPortal.clip_paths   +    	  	  e2r&   c                T   ^^ SUU4S jjn[        U5      mU R                  U5        T$ )Nc                  > [        T5      nTR                  5         U H1  nU R                  (       d  M  UR                  U R                  5        M3     TR	                  U R
                  R                  UT5      5        [        T5      $ r   )r   r   rb   r   r   r`   rI   rN   r   s      r#   r   ,ClippingPortal.clip_filled_paths.<locals>.do   sc    LELLN???**5??;  MM%,,88LM<r&   r   r   r   s     ` @r#   rI    ClippingPortal.clip_filled_paths   r   r&   c                N    U R                   S S S2    H  u  p#Uc  M
  X-  nM     U$ rv   rg   )r"   m_rb   s       r#   transform_matrixClippingPortal.transform_matrix   s/     LL2.LA$ / r&   rg   N)rL   None)rL   rN   )r`   r   rb   ra   rL   r   )rx   zCallable[[ClippingStage], bool]rL   r   rO   )r5   r   r6   r   rL   zlist[tuple[Vec2, Vec2]])r;   r   rL   zlist[NumpyPoints2d])rC   rS   rD   rT   rL   zlist[NumpyPath2d])r   r   rL   r   )rU   rV   rW   rX   rY   ri   propertyrl   rp   rs   rz   r1   r7   r<   r?   rE   rI   r   r\   r    r&   r#   r   r   k   s    ;/ " ">
*9>	 *9>	 r&   r   c                      \ rS rSrSrSS jrSS jrSS jrSS jrSS jr	SS jr
      SS	 jr      SS
 jrSrg)ClippingPolygon   zhRepresents an arbitrary polygon as clipping shape.  Removes the geometry
outside the clipping polygon.

c                T    UR                   (       d  [        S5      eXl        X l        g )Nclipping box not detectable)has_data
ValueError_bboxclipper)r"   r$   r   s      r#   ri   ClippingPolygon.__init__   s    }}:;;
r&   c                    U R                   $ r   )r   r!   s    r#   r$   ClippingPolygon.bbox   s    zzr&   c                ^    U R                   R                  [        U5      5      nU(       d  g U$ r   )r   	is_insider   )r"   r0   r   s      r#   r1   ClippingPolygon.clip_point   s%    LL**4;7	r&   c                8    U R                   R                  X5      $ r   )r   r7   r4   s      r#   r7   ClippingPolygon.clip_line   s    ||%%e11r&   c                   U R                   n[        U5      S:X  a
  [        5       $ [        UR	                  5       5      nU R                  U5      (       a
  [        5       $ U R                  U5      (       a  U4$ UR                  UR                  5       5       Vs/ s H  n[        U5      S:  d  M  [        U5      PM!     sn$ s  snf )Nr   )
r   lentupler   extentsr-   r*   r<   verticesr   )r"   r;   r   polyline_bboxparts        r#   r<   ClippingPolygon.clip_polyline   s    ,,v;!7N%fnn&67%%m447N$$]339  --foo.?@
@4y1}  M$@
 	
 
   C0Cc                   U R                   n[        U5      S:  a
  [        5       $ [        UR	                  5       5      nU R                  U5      (       a
  [        5       $ U R                  U5      (       a  U4$ UR                  UR                  5       5       Vs/ s H  n[        U5      S:  d  M  [        U5      PM!     sn$ s  snf )N   r   )
r   r   r   r   r   r-   r*   r?   r   r   )r"   r;   r   polygon_bboxr   s        r#   r?   ClippingPolygon.clip_polygon  s    ,,v;?7N$V^^%56%%l337N$$\229  ,,V__->?
?4y1}  M$?
 	
 
r   c           	   #    #    U R                   nU H  nUR                  5        H  n[        UR                  5       5      nUR                  (       d  M/  U R                  U5      (       a  Uv   MK  U R                  U5      (       a  Mc  [        R                  " UR                  USS95      nUR                  U5       H+  n[        U5      S:  d  M  [        R                  " USS9v   M-     M     M     g 7f)N   segmentsr   Fclose)r   	sub_pathsr   control_verticesr   r*   r-   r   r   
flatteningr<   r   r   from_vertices)	r"   rC   rD   r   r   sub_path	path_bboxr   r   s	            r#   rE   ClippingPolygon.clip_paths  s      ,,D NN,)(*C*C*EF	 )),,Y77"N--i8899X%8%8q%8%QR#11(;D4y1})77EJJ < - s   C
C4$C4c              #    #    U R                   nU H  nUR                  5        H  n[        U5      S:  a  M  [        UR	                  5       5      nU R                  U5      (       a  Uv   MI  U R                  U5      (       a  Ma  UR                  [        R                  " UR                  USS95      5       H+  n[        U5      S:  d  M  [        R                  " USS9v   M-     M     M     g 7f)Nr   r   r   r   Tr   )r   r   r   r   r   r*   r-   r?   r   r   r   r   r   )r"   rC   rD   r   r   r   r   r   s           r#   rI   !ClippingPolygon.clip_filled_paths)  s      ,,D NN,x=1$)(*C*C*EF	,,Y77"N--i88#00IIh11+1JKD 4y1})77DII	 - s   CC0$C0)r   r   N)r$   r   r   r   rL   r   rK   rO   rP   rQ   rR   )rU   rV   rW   rX   rY   ri   r$   r1   r7   r<   r?   rE   rI   r\   r    r&   r#   r   r      sg    
2

K*K9>K	K&J*J9>J	Jr&   r   c                     ^  \ rS rSrSrSU 4S jjrSS jrSS jrSU 4S jjrSS jr	SU 4S jjr
SU 4S	 jjr      SU 4S
 jjr      SU 4S jjrSrU =r$ )r   i>  zRepresents a rectangle as clipping shape where the edges are parallel to
the x- and  y-axis of the coordinate system.  Removes the geometry outside the
clipping rectangle.

c                :  > SSK Jn  [        R                  " U5      n[	        U5      nUR
                  (       d  [        S5      eUR                  nUR                  UR                  -  S:  U l
        [        TU ]1  XB" UR                  UR                  5      5        g )Nr   )ClippingRect2dr   g&.>)ezdxf.math.clippingr   r   r   r   r   r   sizexy
remove_allsuperri   extminextmax)r"   r   r   r   r$   r   	__class__s         r#   ri   ClippingRect.__init__E  sp    6))H%W%}}:;;YY&&466/D0~dkk4;;GHr&   c                8    U R                   R                  U5      $ r   )r   containsr(   s     r#   r*   !ClippingRect.is_completely_insideQ  s    zz""5))r&   c                B    U R                   R                  U5      (       + $ r   r   has_intersectionr(   s     r#   r-   "ClippingRect.is_completely_outsideT      ::..u555r&   c                F   > U R                   (       a  g [        TU ]	  U5      $ r   )r   r   r1   )r"   r0   r   s     r#   r1   ClippingRect.clip_pointW  s    ??w!%((r&   c                n    U R                   (       a
  [        5       $ U R                  R                  X5      $ r   )r   r   r   r7   r4   s      r#   r7   ClippingRect.clip_line\  s%    ??7N||%%e11r&   c                l   > U R                   (       a  [        [        5       5      4$ [        TU ]  U5      $ r   )r   r   r   r   r<   r"   r;   r   s     r#   r<   ClippingRect.clip_polylinea  s*    ??!%'*,,w$V,,r&   c                l   > U R                   (       a  [        [        5       5      4$ [        TU ]  U5      $ r   )r   r   r   r   r?   r   s     r#   r?   ClippingRect.clip_polygonf  s*    ??!%'*,,w#F++r&   c                j   > U R                   (       a  [        [        5       5      $ [        TU ]  X5      $ r   )r   iterr   r   rE   r"   rC   rD   r   s      r#   rE   ClippingRect.clip_pathsk  s(     ??= w!%55r&   c                j   > U R                   (       a  [        [        5       5      $ [        TU ]  X5      $ r   )r   r   r   r   rI   r   s      r#   rI   ClippingRect.clip_filled_pathsr  s(     ??= w(<<r&   )r   r   Iterable[UVec]rL   r   rM   rO   rP   rQ   rR   )rU   rV   rW   rX   rY   ri   r*   r-   r1   r7   r<   r?   rE   rI   r\   __classcell__r   s   @r#   r   r   >  sk    
I*6)
2
-
,
6*69>6	6=*=9>=	= =r&   r   c                  D   ^  \ rS rSrSrSU 4S jjrSS jrSS jrSrU =r	$ )	r   iz  zoRepresents an arbitrary convex polygon as clipping shape.  Removes the geometry
outside the clipping polygon.

c                |   > SSK Jn  [        R                  " U5      n[        TU ]  [        U5      U" U5      5        g )Nr   )ConvexClippingPolygon2d)r   r	  r   r   r   ri   r   )r"   r   r	  r   r   s       r#   ri   ConvexClippingPolygon.__init__  s.    ?))H%w/1H1QRr&   c                    gNFr    r(   s     r#   r*   *ConvexClippingPolygon.is_completely_inside      r&   c                B    U R                   R                  U5      (       + $ r   r   r(   s     r#   r-   +ConvexClippingPolygon.is_completely_outside  r   r&   r    r  rM   
rU   rV   rW   rX   rY   ri   r*   r-   r\   r  r  s   @r#   r   r   z  s    
S6 6r&   r   c                  D   ^  \ rS rSrSrSU 4S jjrSS jrSS jrSrU =r	$ )	ConcaveClippingPolygoni  zpRepresents an arbitrary concave polygon as clipping shape.  Removes the geometry
outside the clipping polygon.

c                |   > SSK Jn  [        R                  " U5      n[        TU ]  [        U5      U" U5      5        g )Nr   )ConcaveClippingPolygon2d)r   r  r   r   r   ri   r   )r"   r   r  r   r   s       r#   ri   ConcaveClippingPolygon.__init__  s.    @))H%w/1I'1RSr&   c                    gr  r    r(   s     r#   r*   +ConcaveClippingPolygon.is_completely_inside  r  r&   c                B    U R                   R                  U5      (       + $ r   r   r(   s     r#   r-   ,ConcaveClippingPolygon.is_completely_outside  r   r&   r    r  rM   r  r  s   @r#   r  r    s    
T6 6r&   r  c                  D   ^  \ rS rSrSrSU 4S jjrSS jrSS jrSrU =r	$ )	r   i  zRepresents an arbitrary inverted clipping polygon.  Removes the geometry
inside the clipping polygon.

.. Important:: 

    The `outer_bounds` must be larger than the content to clip to work correctly.

c                h   > SSK Jn  [        R                  " U5      n[        TU ]  X#" XB5      5        g )Nr   )InvertedClippingPolygon2d)r   r  r   r   r   ri   )r"   r   outer_boundsr  r   r   s        r#   ri    InvertedClippingPolygon.__init__  s)    A))H%'@'WXr&   c                    gr  r    r(   s     r#   r*   ,InvertedClippingPolygon.is_completely_inside  s    r&   c                B    U R                   R                  U5      (       + $ r   r   r(   s     r#   r-   -InvertedClippingPolygon.is_completely_outside  r   r&   r    )r   r  r  r   rL   r   rM   r  r  s   @r#   r   r     s    Y6 6r&   r   c                    [         R                  " U 5      n[        U5      (       a  [        U5      $ [	        USS9(       a  [        U5      $ [        U5      $ )a  Returns the best clipping shape for the given clipping polygon.

The function analyses the given polygon (rectangular, convex or concave polygon, ...)
and returns the optimized (fastest) clipping shape.

Args:
    polygon: clipping polygon as iterable vertices

F)strict)r   r   r   r   r   r   r  )r   r;   s     r#   r   r     sH     YYwF#F++F##	fU	3$V,,!&))r&   c                    [        X5      $ )zReturns an inverted clipping shape that removes the geometry inside the clipping
polygon and beyond the outer bounds.

Args:
    polygon: clipping polygon as iterable vertices
    outer_bounds: outer bounds of the clipping shape

)r   )r   r  s     r#   r   r     s     #799r&   )r   r  rL   r   )r   r  r  r   rL   r   )#
__future__r   typingr   r   r   r   r   r	   r
   rZ   
ezdxf.mathr   r   r   r   r   r   ezdxf.npshapesr   r   r   r   __all__ABCr   r^   r   r   r   r   r  r   r   r   r    r&   r#   <module>r-     s    #     6,	;$CGG ;$|J 
r rj[Jm [J|9=? 9=x6O 6&6_ 6&6o 60*$::+8::r&   