
    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    eA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                 </DATA/.local/lib/python3.12/site-packages/trimesh/nsphere.pyminimum_nspherer3      s   2 $F
 JJAJ&M66&q)--/L}$4F
 &f-E5%~!#((a(0446#=ME\!]2Et|e| oofD9G

 g../#f+=A S ""((f] ) 

#1#+ 	  I wwwy)*\9H  +l:mKH%e|X%  
@A((<C<L<LM<Lqvza$$!$,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:     666F?q0$7 8<<>CM9::    r   r   g:0yE>)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4F776<<?#D; }#e$!(E!EM;,&344MM&=01EZZ\FFF5ME&%''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GWeCII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(