Ë
    :vžjú  ã                   óÖ   — d Z ddlZddlmZmZ ddlmZmZ 	 ddl	m
Z
 ddlmZ d	„ Zdd
„Zd„ Zy# e$ r4ZddlmZ  ej                   e«      Z ej                   e«      Z
Y dZ[Œ?dZ[ww xY w)zp
nsphere.py
--------------

Functions for fitting and minimizing nspheres:
circles, spheres, hyperspheres, etc.
é    Né   )ÚconvexÚutil)ÚlogÚtol)Úspatial)Úleastsq)Ú
exceptionsc           	      óØ  — t        j                  | «      }|j                  d¬«      }t        j                  |d¬«      j                  «       }||z
  |z  }t        |«      \  }}}||z
  dz  j                  d¬«      j                  «       dz  |z  }||z  |z   }|dk  r||fS t        j                  |d¬«      }	 t        |j                  «      t        |«      z  d	z  }|d
kD  rt        ‚t        j                  j                  |j                  |d¬«      j                  d¬«      }	|	j%                  «       }t        j&                  |	|   «      |z  }|j                  |   |z  |z   }||kD  r||fS ||fS # t        $ ro t        j                   d«       t        j"                  |j                  D �
cg c](  }
||
z
  dz  j                  d¬«      j                  «       ‘Œ* nc c}
w c}
«      }	Y ŒÄw xY w)aT  
    Compute the minimum n- sphere for a mesh or a set of points.

    Uses the fact that the minimum n- sphere will be centered at one of
    the vertices of the furthest site voronoi diagram, which is n*log(n)
    but should be pretty fast due to using the scipy/qhull implementations
    of convex hulls and voronoi diagrams.

    Parameters
    ----------
    obj : (n, d) float or trimesh.Trimesh
      Points or mesh to find minimum bounding nsphere

    Returns
    ----------
    center : (d,) float
      Center of fitted n- sphere
    radius : float
      Radius of fitted n-sphere
    r   ©Úaxisé   r   g      à?g�íµ ÷Æ°>T)Úfurthest_siteé	   g    eÍíAÚsqeuclidean)Úmetricz*MemoryError: falling back to slower check!)r   Úhull_pointsÚminÚnpÚptpÚfit_nsphereÚsumÚmaxr   ÚVoronoiÚlenÚverticesÚMemoryErrorÚdistanceÚcdistr   ÚwarningÚarrayÚargminÚsqrt)ÚobjÚpointsÚpoints_originÚpoints_scaleÚfit_CÚfit_RÚfit_EÚvoronoiÚmemory_estimateÚradii_2ÚvÚ	radii_idxÚradius_vÚcenter_vs                 ú`/workspace/moltex-experiments/.pixi/envs/default/lib/python3.12/site-packages/trimesh/nsphere.pyÚminimum_nspherer3      sò  € ô2 ×Ñ Ó$€Fð
 —J‘J A�JÓ&€MÜ—6‘6˜& qÔ)×-Ñ-Ó/€LØ�}Ñ$¨Ñ4€Fô
 & fÓ-Ñ€Eˆ5�%à˜‰~ !Ñ#×(Ñ(¨aÐ(Ó0×4Ñ4Ó6¸#Ñ=ÀÑM€EØ�\Ñ! ]Ñ2€Eàˆt‚|à�eˆ|Ðô �o‰o˜f°DÔ9€Gð
ô
 ˜g×.Ñ.Ó/´#°f³+Ñ=ÀÑAˆð ˜SÒ ÜÐÜ×"Ñ"×(Ñ(Ø×Ñ˜f¨]ð )ó 
ç
‰#�1ˆ#‹+ð 	ð —‘Ó €Iô �w‰w�w˜yÑ)Ó*¨\Ñ9€HØ× Ñ  Ñ+¨lÑ:¸mÑK€Hà�%ÒØ�eˆ|Ðà�XÐÐøô% ò 
ä�‰Ð@ÔAä—(‘(Ø<C×<LÒ<LÓMÑ<L°qˆv˜‰z˜aÑ×$Ñ$¨!Ð$Ó,×0Ñ0Õ2Ñ<LùÔMó
Šð	
ús   Â9A+E1 Å1;G)Æ,-G
ÇG)Ç(G)c                 ó¸  ‡ ‡	— t        j                  ‰ t         j                  ¬«      Š t        j                  ‰ j                  d   «      Š	ˆ	ˆ fd„}|€‰ j                  d¬«      }nt        j                  |«      }t        ||d¬«      \  }}|dvrt        d	«      ‚t        j                  ‰ |z
  «      }|j                  «       }t        j                  |«      }|||fS )
a˜  
    Fit an n-sphere to a set of points using least squares.

    Parameters
    ------------
    points : (n, d) float
      Points in space
    prior : (d,) float
      Best guess for center of nsphere

    Returns
    ---------
    center : (d,) float
      Location of center
    radius : float
      Mean radius across circle
    error : float
      Peak to peak value of deviation from mean radius
    )Údtyper   c                 óz   •— t        j                  ‰| z
  dz  ‰«      }||j                  «       t        |«      z  z
  S )Nr   )r   Údotr   r   )ÚcenterÚradii_sqÚonesr%   s     €€r2   Ú	residualszfit_nsphere.<locals>.residuals�   s:   ø€ ô —6‘6˜6 F™?¨qÑ0°$Ó7ˆð ˜8Ÿ<™<›>¬C°«MÑ9Ñ:Ð:ó    r   r   g:Œ0âŽyE>)Úxtol)r   r   é   é   zLeast square fit failed!)r   Ú
asanyarrayÚfloat64r:   ÚshapeÚmeanr	   Ú
ValueErrorr   Úrow_normr   )
r%   Úpriorr;   ÚguessÚcenter_resultÚreturn_codeÚradiiÚradiusÚerrorr:   s
   `        @r2   r   r   t   s¸   ù€ ô* �]‰]˜6¬¯©Ô4€Fä�7‰7�6—<‘< ‘?Ó#€Dõ;ð €}Ø—‘ �Ó#‰ä—‘˜eÓ$ˆä!(¨°EÀÔ!EÑ€M�;à˜,Ñ&ÜÐ3Ó4Ð4ä�M‰M˜& =Ñ0Ó1€EØ�Z‰Z‹\€FÜ�F‰F�5‹M€EØ˜& %Ð'Ð'r<   c                 óJ   — t        | «      \  }}}|t        j                  k  }|S )zå
    Check if a list of points is an nsphere.

    Parameters
    -----------
    points : (n, dimension) float
      Points in space

    Returns
    -----------
    check : bool
      True if input points are on an nsphere
    )r   r   Úmerge)r%   Ú_centerÚ_radiusrL   Úchecks        r2   Ú
is_nsphererR   ¥   s(   € ô *¨&Ó1Ñ€GˆW�eØ”C—I‘IÑ€EØ€Lr<   )N)Ú__doc__Únumpyr   Ú r   r   Ú	constantsr   r   Úscipyr   Úscipy.optimizer	   ÚBaseExceptionÚEr
   ÚExceptionWrapperr3   r   rR   © r<   r2   Ú<module>r]      sl   ðñó ç ß ð	-åÝ&òWót.(óbøðg ò -åà)ˆj×)Ñ)¨!Ó,€GØ)ˆj×)Ñ)¨!Ó,…Gûð-ús   ˜/ ¯A(´*A#Á#A(