
    Ti	1                         d dl Z d dlZd dlZddlmZ ddlmZ ddlmZ	 ddl
mZ ddlmZmZ dd
ZddZd ZddZddZddZddZdS )    N   )util)log)tol_path)fit_nsphere   )arcentitiesFc                    t          |           dk     rdS t          j        | t          j                  } t	          | |          \  }}}t
          j        ||z  cxk     rt
          j        k     sn |rt          j	        d||z             dS ||z  }|t
          j
        k    r&|r"t          j	        dt          |                     dS t          j        | d          }	t          j        |	          }
|
|z  }|t
          j        k                                    r&|r"t          j	        d	t          |                     dS |rJ|t
          j        k                                    dk     r$t          j	        d
t          |                     dS |
|z  }|t
          j        k                                    r&|r"t          j	        dt          |                     dS | ddg         |	ddg         dz  z   }t          j        ||z
            }t          j        |	ddg                   }t          j        t          j        t          j        ||                              }t          j        |t          j        dz  z
                                            }|t
          j        k    r+|r't          j	        dt          j        |                     dS ||d}|S )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/DATA/AppData/hermes/venv/lib/python3.11/site-packages/trimesh/path/simplify.pyfit_circle_checkr@      s   : 6{{Qt]6444F $F%888Aq+ >QY8888#.8888 	;I.E	:::t AoG   	@I0#g,,???tgf1%%%GmG$$G aKE""$$  	@I2CJJ???t %#++0022Q66	#SZZ000t u_F""$$  	CI4c&kkBBBt QG_B 03 67F\&1*%%F<B())DfRYt0>>??@@GfWruqy())--//G 	KI4bj6I6IJJJtQ''FM    c                    t          j        |           } t          |          }t           j                            | d         | d         z
            t
          j        k    rdS t          j        | d          }t          j        | }t          j	        |dz
            t
          j
        k    rdS t          | |          }|dS t          j        di |}|S )a  
    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%LLE 
y~~fQi&*,--	99t
&a
 
 
 C Y_F	vfslco--t 
&	.	.	.B	zt %%"%%GNrA   c                 \   t          j        | t           j                  } t          |          }t	          | j                  dk    s| j        d         dk    rt          d          t	          |           dk     r|                                 S | dd         | dd         z
  }t          j	        |          }|t          j        k    }t          j        | d         | dd         |         f          } ||         }||         }| dd         | dd	         z
  j        ddd         j        }|dddfxx         dz  cc<   t          j	        |          }|t          j        k    }||xx         ||                             d
          z  cc<   t          j        t          j        ||dd                             }t          j        ||dd         z  ||dd         z  fd          }	t          j        t	          |           t&                    }
d|
dd         |	d|z  k     <   | |
         }|S )ar  
    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444F%LLE
6<AaA!5!5./// 6{{Q{{}} qrr
VCRC[(I]9--N!CI-L Yq	6!"":l#;<==F,'I#L1N 122J$'"-/DAJJJ"JJJd##Isy(L)L199'BBB )$	#2#??@@Jv	nQRR(	(*~crc7J*JKRS   73v;;d+++D27D2J$,./D\FMrA   MbP?r   c                    ddl m}m} |t          |           }t	          j        |           } t          j                            | d         | d         z
            t          j	        k     } || j
        ||          d         }t	          j        dd|          }t	          j         |||                    }	|r(|	ddg                             d          }
|
|	d<   |
|	d<   |	S )	a  
    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splprepNr   )skg        rC   r   )scipy.interpolaterj   rk   r   r   r   rG   rH   r   rI   rY   linspacecolumn_stackmean)r/   smoothcountdegreerj   rk   closedtpli	resampledshareds              r?   resample_splinerz      s    ( 10000000}F]6""FY^^F1Ir
233ci?F
'&(f
/
/
/
2C
Ce$$Aa..I Ar7#((a(00	!	"rA   c                 8   ddl m} |t          |           }|d}t          j        | t          j                  } t          j                            | d         | d         z
            t          j	        k     } || j
        |          d         \  }}}t          j        |          }t          j        t          |                    }|r6|ddg                             d          |d<   |dd         }|d         |d<   t          j        |||	          }	|	|fS )
a  
    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   )rk   NgMb`?r   r   )rl   r   )r/   knotsru   )rn   rk   r   r   r   r   rG   rH   r   rI   rY   	transposearangerq   r
   BSpline)
r/   rr   rs   rk   ru   r|   rQ   _degreeindexentitys
             r?   points_to_spline_entityr      s   * *)))))}F~]6444FY^^F1Ir
233ci?F%gfh&999!<E7Gl7##GIc'll##E aW%***22
#2#,!Hb	U%GGGF7?rA   c           	      
   t          d | j        D                       rt          j        d           | S t	          j        | j                  }t          j                    }t          j                    }| j	        }| j
        D ]}t          ||          }|a|                    t          j        t          j        d          t!          |          z   d                     |                    |           wt%          ||          }	t          j        t!          |	                    t!          |          z   }
|
d         |
d	<   |                    t          j        |

                     |                    |	            t)          |           ||t	          j        | j                  |          }|j                            t          j        t!          |                                        d          t          j        t!          |          t4                    t          j        g           d           d|j        v r|j                            d           ||_        |j                                         |S )a  
    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   6   K   | ]}|j         j        d k    V  dS )LineN)	__class____name__).0r   s     r?   	<genexpr>z!simplify_basic.<locals>.<genexpr>;  s-      
N
N66$.
N
N
N
N
N
NrA   z3Skipping path containing entities other than `Line`rD   Nr   Tr/   ru   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   ,  sb    
N
NW=M
N
N
NNN 	GHHH M'.))E $&&L$&&L ME $ ( (85111 BIaLL3|3D3D$DTRRR   '''' $HE:::FiF,,s</@/@@G!!*GBKW = = =>>>'''' gw/00	  J 
KYs<001199'BB'#l"3"34@@@	
 	
   5;!!!JrA   c           	         g }g }| j         }| j        D ]}t          |||          }|a|                    t	          j        t          j        d          t          |          z   d                     |	                    |           wt          ||          \  }}	|xj        t          |          z  c_        |	                    |	           |                    |            t          |           ||          }
|
S )aJ  
    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   Nr   Tr   )rr   )r
   r   )r0   r   rR   r   r
   r   r   r~   r   r   r   r/   r   )pathrr   r2   new_verticesnew_entitiesr0   r   r   r   r   r   s              r?   simplify_spliner   ~  s   $ LLJEM $ $85'BBB BIaLL3|3D3D$DTRRR   ''' 38FKKK\***H%%%F#### d\LIIIJrA   )NFF)F)rh   Nr   )NN)NF)r   rW   numpyr    r   	constantsr   r   r   nspherer   r	   r
   r@   rR   rg   rz   r   r   r   rE   rA   r?   <module>r      s%                        ' ' ' ' ' ' ! ! ! ! ! !        X X X Xv+ + + +\? ? ?D$ $ $ $N* * * *ZO O O Od, , , , , ,rA   