
    Mi	1                         S SK r S SKrS SKrSSKJr  SSKJr  SSKJr	  SSK
Jr  SSKJrJr  SS	 jrSS
 jrS rSS jrSS jrSS jrSS jrg)    N   )util)log)tol_path)fit_nsphere   )arcentitiesc                    [        U 5      S:  a  g[        R                  " U [        R                  S9n [	        XS9u  pVn[
        R                  Xa-  s=:  a  [
        R                  :  d#  O  U(       a  [        R                  " SXa-  5        gXv-  nU[
        R                  :  a(  U(       a   [        R                  " S[        U5      5        g[        R                  " U SS9n	[        R                  " U	5      n
X-  nU[
        R                  :  R!                  5       (       a(  U(       a   [        R                  " S	[        U5      5        gU(       aF  U[
        R"                  :  R%                  5       S:  a!  [        R                  " S
[        U5      5        gX-  nU[
        R&                  :  R!                  5       (       a(  U(       a   [        R                  " S[        U5      5        gU SS/   U	SS/   S-  -   n[        R(                  " X-
  5      n[        R(                  " U	SS/   5      n[        R*                  " [        R,                  " [        R.                  " X5      5      5      n[        R*                  " U[        R0                  S-  -
  5      R3                  5       nU[
        R4                  :  a3  U(       a+  [        R                  " S[        R6                  " U5      5        gXVS.nU$ )a  
Fit a circle, and reject the fit if:
* the radius is larger than tol.radius_min*scale or tol.radius_max*scale
* any segment spans more than tol.seg_angle
* any segment is longer than tol.seg_frac*scale
* the fit deviates by more than tol.radius_frac*radius
* the segments on the ends deviate from tangent by more than tol.tangent

Parameters
---------
points :  (n, d)
  List of points which represent a path
prior :  (center, radius) tuple
  Best guess or None if unknown
scale : float
  What is the overall scale of the set of points
verbose : bool
 Output log.debug messages for the reasons
 for fit rejection only suggested for manual debugging

Returns
-----------
if fit is acceptable:
    (center, radius) tuple
else:
    None
   Ndtype)priorzcircle fit error: R %fzcircle fit error: fit %sr   axiszcircle fit error: angle %szfinal: angle %szcircle fit error: segment %sg      ?r   zcircle fit error: tangent %f)centerradius)lennp
asanyarrayfloat64r   tol
radius_min
radius_maxr   debugradius_fracstrdiffr   row_norm	seg_angleanyseg_angle_minsumseg_fracunitizeabsarccosdiagonal_dotpimaxtangentdegrees)pointsscaler   finalverboseCRr_deviationr_errorvectorssegmentanglescaledmid_ptradialendsr-   results                     o/var/www/eduai.edurigo.com/storigo/production/storigo_env/lib/python3.13/site-packages/trimesh/path/simplify.pyfit_circle_checkr@      s!   : 6{Q]]64F $F8A+ >>QY8#..8II.	: oG II0#g,?ggf1%GmmG$G KE""$$II2CJ?%#+++002Q6		#SZ0 _F""$$II4c&kB QG_B 03 67F\\&*%F<<B()DffRYYt00>?@GffWruuqy()--/GII4bjj6IJ'FM    c                    [         R                  " U 5      n [        U5      n[         R                  R	                  U S   U S   -
  5      [
        R                  :  a  g[         R                  " U SS9n[         R                  " U6 n[         R                  " US-
  5      [
        R                  :  a  g[        XS9nUc  g[        R                  " S0 UD6nU$ )aH  
Given a set of points, quickly determine if they represent
a circle or not.

Parameters
-------------
points : (n,2 ) float
  Points in space
scale : float
  Scale of overall drawing
verbose : bool
  Print all fit messages or not

Returns
-------------
control: (3,2) float, points in space, OR
          None, if not a circle
r   r   Nr         ?r0    )r   r   floatlinalgnormr   mergeptpdivider(   aspect_fracr@   r	   to_threepoint)r/   r0   r2   boxaspectCRcontrols          r?   	is_circlerR   h   s    * ]]6"F%LE 
yy~~fQi&*,-		9
&&a
 C YY_F	vvfslcoo- 
&	.B	z %"%GNrA   c                    [         R                  " U [         R                  S9n [        U5      n[	        U R
                  5      S:w  d  U R
                  S   S:w  a  [        S5      e[	        U 5      S:  a  U R                  5       $ U SS U SS -
  n[        R                  " U5      nU[        R                  :  n[         R                  " U S   U SS U   45      n X$   nX4   nU SS U SS	 -
  R                  SSS2   R                  nUSS2S4==   S-  ss'   [        R                  " U5      nU[        R                  :  nXW==   Xg   R                  S
5      -  ss'   [         R                  " [        R                   " XRSS 5      5      n[         R"                  " XSS -  XSS -  4SS9n	[         R$                  " [	        U 5      [&        S9n
SU
SS U	SU-  :  '   X
   nU$ )a:  
Given a set of points representing a path in space,
merge points which are colinear.

Parameters
----------
points : (n, dimension) float
  Points in space
scale : float
  Scale of drawing for precision

Returns
----------
merged : (j, d) float
  Points with colinear and duplicate
  points merged, where (j < n)
r   r   r   zonly for 2D points!r   Nr   r   r   r   r   r   Fg-C6?)r   r   r   rF   r   shape
ValueErrorcopyr   r!   r   rI   vstackTreshaper(   r*   r,   onesbool)r/   r0   	directiondirection_normdirection_okperp	perp_normperp_nonzero
projectionprojection_ratiomaskmergeds               r?   merge_colinearrg      s   $ ]]64F%LE
6<<AaA!5.// 6{Q{{} qr
VCR[(I]]9-N!CII-L YYq	6!":l#;<=F'I#1N 12J$''"-//DAJ"Jd#Isyy(L)199'BB ))$#2?@Jvv	QR(	(*cr7J*JKRS 773v;d+D27D2J$,./\FMrA   c                    SSK JnJn  Uc  [        U 5      n[        R
                  " U 5      n [        R                  R                  U S   U S   -
  5      [        R                  :  nU" U R                  XS9S   n[        R                  " SSU5      n[        R                  " U" X5      5      n	U(       a  U	SS/   R                  SS9n
XS'   XS'   U	$ )aS  
Resample a path in space, smoothing along a b-spline.

Parameters
-----------
points : (n, dimension) float
  Points in space
smooth : float
  Smoothing distance
count :  int or None
  Number of samples desired in output
degree : int
  Degree of spline polynomial

Returns
---------
resampled : (count, dimension) float
  Points in space
r   )splevsplprepr   )skg        rC   r   )scipy.interpolateri   rj   r   r   r   rG   rH   r   rI   rY   linspacecolumn_stackmean)r/   smoothcountdegreeri   rj   closedtpli	resampledshareds              r?   resample_splinery      s    ( 1}F]]6"FYY^^F1Ir
23cii?F
&((f
/
2C
Ce$Aa.IAr7#((a(0!"rA   c                 
   SSK Jn  Uc  [        U 5      nUc  Sn[        R                  " U [        R
                  S9n [        R                  R                  U S   U S   -
  5      [        R                  :  nU" U R                  US9S   u  pVn[        R                  " U5      n[        R                  " [        U5      5      nU(       a$  USS/   R                  SS9US'   USS nUS   US'   [        R                  " XUS	9n	X4$ )
ag  
Create a spline entity from a curve in space

Parameters
-----------
points : (n, dimension) float
  Points in space
smooth : float
  Smoothing distance
count :  int or None
  Number of samples desired in result

Returns
---------
entity : entities.BSpline
  Entity object with points indexed at zero
control : (m, dimension) float
  New vertices for entity
r   )rj   NgMb`?r   r   )rk   r   )r/   knotsrt   )rm   rj   r   r   r   r   rG   rH   r   rI   rY   	transposearangerp   r
   BSpline)
r/   rq   rr   rj   rt   r{   rQ   _degreeindexentitys
             r?   points_to_spline_entityr      s    * *}F~]]64FYY^^F1Ir
23cii?F%fhh&9!<EGll7#GIIc'l#EaW%***2
#2,!Hb	UGF?rA   c           	         [        S U R                   5       5      (       a  [        R                  " S5        U $ [        R
                  " U R                  5      n[        R                  " 5       n[        R                  " 5       nU R                  nU R                   H  n[        XvS9nUbW  UR                  [        R                  " [        R                  " S5      [!        U5      -   SS95        UR#                  U5        Mf  [%        XvS9n	[        R                  " [!        U	5      5      [!        U5      -   n
U
S   U
S'   UR                  [        R&                  " U
S	95        UR#                  U	5        M     [)        U 5      " UU[        R
                  " U R*                  5      US
9nUR,                  R/                  [        R                  " [!        U5      5      R1                  S5      [        R2                  " [!        U5      [4        S9[        R6                  " / 5      S.5        SUR,                  ;   a  UR,                  R9                  S5        X;l        UR                  R;                  5         U$ )z
Merge colinear segments and fit circles.

Parameters
-----------
drawing : Path2D
  Source geometry, will not be modified

Returns
-----------
simplified : Path2D
  Original path but with some closed line-loops converted to circles
c              3   R   #    U  H  oR                   R                  S :g  v   M     g7f)LineN)	__class____name__).0r   s     r?   	<genexpr>!simplify_basic.<locals>.<genexpr>;  s!     
N=M6$$.=Ms   %'z3Skipping path containing entities other than `Line`rD   r   Tr/   rt   r   r   )r/   )r
   verticesmetadataprocessrT   r   )paths
path_validdanglingbounds)r#   r
   r   r   rW   deepcopy_cachecollectionsdequer0   discreterR   appendArcr   r}   r   extendrg   r   typer   cacheupdaterZ   r[   r\   arraypopid_set)drawingr   kwargsr   vertices_newentities_newr0   r   circler/   indexes
simplifieds               r?   simplify_basicr   ,  s    
NW=M=M
NNN		GH MM'..)E $$&L$$&L MME $$81 BIIaL3|3D$DTR ' $H:FiiF,s</@@G!!*GBKW =>') %. gw//0	J 
KKYYs<0199'B''#l"34@	
 5;;!rA   c           	         / n/ nU R                   nU R                   H  n[        XeUS9nUbW  UR                  [        R
                  " [        R                  " S5      [        U5      -   SS95        UR                  U5        Mg  [        XaS9u  pU=R                  [        U5      -  sl        UR                  U	5        UR                  U5        M     [        U 5      " XCS9n
U
$ )a  
Replace discrete curves with b-spline or Arc and
return the result as a new Path2D object.

Parameters
------------
path : trimesh.path.Path2D
  Input geometry
smooth : float
  Distance to smooth

Returns
------------
simplified : Path2D
  Consists of Arc and BSpline entities
)r0   r2   r   Tr   )rq   )r
   r   )r0   r   rR   r   r
   r   r   r}   r   r   r   r/   r   )pathrq   r2   new_verticesnew_entitiesr0   r   r   r   r   r   s              r?   simplify_spliner   ~  s    $ LLJJEMM8'B BIIaL3|3D$DTR ' 38K\**H%F## "( d\IJrA   )NFF)F)gMbP?Nr   )NN)NF)r   rW   numpyr    r   	constantsr   r   r   nspherer   r	   r
   r@   rR   rg   ry   r   r   r   rE   rA   r?   <module>r      sH         ' ! Xv+\?D$N*ZOd,rA   