
    l0j                         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dlZd	 Zn# e$ r d
 ZY n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                  8    t          j                    j        dz  S )Ng       @)psutilvirtual_memoryfree     R/home/wildlama/miniconda3/envs/lam/lib/python3.11/site-packages/trimesh/nsphere.py_MAX_MEMORYr      s    $&&+c11r   c                      dS )Ng    eAr   r   r   r   r   r   "   s    sr   c                    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  }|t                      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	   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     r   
<listcomp>z#minimum_nsphere.<locals>.<listcomp>o   s?    MMMqvza$$!$,,0022MMMr   )r   hull_pointsminnpptpfit_nspherer   r   r   Voronoilenverticesr   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_nspherer>   '   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[]]**"((f] ) 
 

#1#++ 	  
 
 
@AAA(MMMMG<LMMM
 
	
   I wwy)**\9H +l:mKH%e|Xs   A;E 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float64rE   shapemeanr	   
ValueErrorr   row_normr&   )
r!   priorrF   guesscenter_resultreturn_coderadiiradiuserrorrE   s
   `        @r   r'   r'      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_radiusrV   checks        r   
is_nspherer\      s)     *&11GWeCIELr   )N)__doc__numpyr%    r   r   	constantsr   r   scipyr   scipy.optimizer	   BaseExceptionEr
   ExceptionWrapperr   r   r>   r'   r\   r   r   r   <module>rf      sq                       	-&&&&&&& - - -)j)!,,G)j)!,,GGGGGG-MMM2 2 2 2        U U Up.( .( .( .(b    s&   % A&AAA& &A10A1