
    Ti                         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 n8# e$ r0ZddlmZ  ej        e          Z ej        e          Z
Y dZ[ndZ[ww xY wd	 Zdd
Zd ZdS )zp
nsphere.py
--------------

Functions for fitting and minimizing nspheres:
circles, spheres, hyperspheres, etc.
    N   )convexutil)logtol)spatial)leastsq)
exceptionsc                    t          j        |                               d          }t          j        d                                          }|z
  |z  t                    \  }}}|z
  dz                      d                                          dz  |z  }||z  |z   }|dk     r||fS t          j	        d          }	 t          |j                  t                    z  d	z  }|d
k    rt          t          j                            |j        d                              d          }nI# t          $ r< t          j        d           t          j        fd|j        D                       }Y nw xY w|                                }	t          j        ||	                   |z  }
|j        |	         |z  |z   }|
|k    r||fS ||
fS )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!c                 l    g | ]0}|z
  d z                       d                                          1S )r   r   r   )summax).0vpointss     I/DATA/AppData/hermes/venv/lib/python3.11/site-packages/trimesh/nsphere.py
<listcomp>z#minimum_nsphere.<locals>.<listcomp>d   s?    MMMqvza$$!$,,0022MMM    )r   hull_pointsminnpptpfit_nspherer   r   r   VoronoilenverticesMemoryErrordistancecdistr   warningarrayargminsqrt)objpoints_originpoints_scalefit_Cfit_Rfit_Evoronoimemory_estimateradii_2	radii_idxradius_vcenter_vr   s               @r   minimum_nspherer7      s   2 $$F
 JJAJ&&M6&q)))--//L}$4F
 &f--E5%~!#((a(004466#=ME\!]2Et||e| ofD999G

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

#1#++ 	  
 
 
@AAA(MMMMG<LMMM
 
	
   I wwy)**\9H +l:mKH%e|Xs   A/D< <AFFc                    	 t          j         t           j                   t          j         j        d                   		 fd}|                     d          }nt          j        |          }t          ||d          \  }}|d	vrt          d
          t          j	         |z
            }|                                }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                     t          j        | z
  dz            }||                                t          |          z  z
  S )Nr   )r   dotr   r"   )centerradii_sqonesr   s     r   	residualszfit_nsphere.<locals>.residuals   s?     66F?q0$77 8<<>>CMM9::r   Nr   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
   `        @r   r    r    t   s    * ]6444F76<?##D; ; ; ; ; ; }##e$$!(E!E!E!EM;,&&3444M&=011EZZ\\FF5MME&%''r   c                 L    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_radiusrO   checks        r   
is_nsphererU      s)     *&11GWeCIELr   )N)__doc__numpyr    r   r   	constantsr   r   scipyr   scipy.optimizer	   BaseExceptionEr
   ExceptionWrapperr7   r    rU    r   r   <module>r`      s                       	-&&&&&&& - - -)j)!,,G)j)!,,GGGGGG-W W Wt.( .( .( .(b    s   % A&AA