
    l0j                         d Z ddlZddlmZ 	 ddlmZ n(# e$ r Zddlm	Z	  e	j
        e          ZY dZ[ndZ[ww xY wd Zd Zd	 Zd
 Zd Zd ZdS )z5
curvature.py
---------------

Query mesh curvature.
    N   )util)
coo_matrix)
exceptionsc                     t          | j                                        | j        j        | j        j        ff| j        j                  }|S )z
    A sparse matrix representation of the face angles.

    Returns
    ----------
    sparse : scipy.sparse.coo_matrix
      matrix is float shaped (len(vertices), len(faces))
    )r   face_anglesflattenfaces_sparserowcolshape)meshmatrixs     T/home/wildlama/miniconda3/envs/lam/lib/python3.11/site-packages/trimesh/curvature.pyface_angles_sparser      sK     			!	!	#	#d&7&;T=N=R%ST F M    c                     t          j        | j                            d                                                    }dt           j        z  |z
  }|S )a  
    Return the vertex defects, or (2*pi) minus the sum of the
    angles of every face that includes that vertex.

    If a vertex is only included by coplanar triangles, this
    will be zero. For convex regions this is positive, and
    concave negative.

    Returns
    --------
    vertex_defect : (len(self.vertices), ) float
                     Vertex defect at the every vertex
    r   )axis   )nparrayr   sumr	   pi)r   	angle_sumdefects      r   vertex_defectsr   $   sH     044!4<<==EEGGI"%i9$FMr   c                     t          j        |t           j                  }t          j        |d          st          d           j                            ||          } fd|D             }t          j        |          S )a>  
    Return the discrete gaussian curvature measure of a sphere
    centered at a point as detailed in 'Restricted Delaunay
    triangulations and normal cycle'- Cohen-Steiner and Morvan.

    This is the sum of the vertex defects at all vertices
    within the radius for each point.

    Parameters
    ----------
    points : (n, 3) float
      Points in space
    radius : float ,
      The sphere radius, which can be zero if vertices
      passed are points.

    Returns
    --------
    gaussian_curvature:  (n,) float
      Discrete gaussian curvature measure.
    dtype   points must be (n,3)!c                 N    g | ]!}j         |                                         "S  )r   r   ).0verticesr   s     r   
<listcomp>z7discrete_gaussian_curvature_measure.<locals>.<listcomp>S   s-    NNN($%h/3355NNNr   )	r   
asanyarrayfloat64r   is_shape
ValueErrorkdtreequery_ball_pointasarray)r   pointsradiusnearest
gauss_curvs   `    r   #discrete_gaussian_curvature_measurer4   7   s~    . ]6444F=)) 20111k**66::GNNNNgNNNJ:j!!!r   c                     t          j        |t           j                  }t          j        |d          st          d          t          j        ||z
  ||z   f          } fd|D             }t          j        t          |                    }t          t          ||                    D ]\  }\  }} j         j        |                  }	t          |	dddf         |	dddf         ||          }
 j        |         }t          j         j        |         dd	          }|
|z  |z                                  d
z  ||<   |S )a  
    Return the discrete mean curvature measure of a sphere
    centered at a point as detailed in 'Restricted Delaunay
    triangulations and normal cycle'- Cohen-Steiner and Morvan.

    This is the sum of the angle at all edges contained in the
    sphere for each point.

    Parameters
    ----------
    points : (n, 3) float
      Points in space
    radius : float
      Sphere radius which should typically be greater than zero

    Returns
    --------
    mean_curvature : (n,) float
      Discrete mean curvature measure.
    r   r    r#   c                 ^    g | ])}t          j                            |                    *S r%   )listface_adjacency_treeintersection)r&   br   s     r   r(   z3discrete_mean_curvature_measure.<locals>.<listcomp>v   s2    QQQQ$t/<<Q??@@QQQr   Nr   r   )centerr1   r!   r   )r   r)   r*   r   r+   r,   column_stackzeroslen	enumeratezipr'   face_adjacency_edgesline_ball_intersectionface_adjacency_angleswhereface_adjacency_convexr   )r   r0   r1   bounds
candidates	mean_curvixx_candidates	endpointslengthsanglessignss   `            r   discrete_mean_curvature_measurerP   X   sW   , ]6444F=)) 20111 _fvov?@@F RQQQ&QQQJV%%I )#fj*A*A B B < <A|M$";L"IJ	(aaadOYqqq!t_Qv
 
 
 +L93LA1bII&(505577!;	!r   c                 d   || z
  }| |z
  }|}t          j        d||          }t          j        d||          }t          j        d||          }	|dz  ||	|dz  z
  z  z
  }
t          j        t          |                     }|
dk    }||          t          j        |
|                   z
  ||         z  }||          t          j        |
|                   z   ||         z  }t          j        |dd          }t          j        |dd          }||z
  t          j        ||                   z  ||<   |S )a|  
    Compute the length of the intersection of a line segment with a ball.

    Parameters
    ----------
    start_points : (n,3) float, list of points in space
    end_points   : (n,3) float, list of points in space
    center       : (3,) float, the sphere center
    radius       : float, the sphere radius

    Returns
    --------
    lengths: (n,) float, the lengths.

    z	ij, ij->ir   r   r   )r   einsumr=   r>   sqrtclip)start_points
end_pointsr;   r1   LocrldotlldotococdotocdiscrimsrM   md1d2s                  r   rB   rB      s0   ( 	\!A		BAIk1a((EY{Ar**FiR,,Gqy5GadN33H hs<(())G1A!9*rwx{++
+uQx	7B!9*rwx{++
+uQx	7B 
Q		B	Q		B r'RWU1X...GAJNr   c                     d| dz  z  |dz  z
  d| z  z  }||  k    rdt           j        z  | z  | |z
  z  S ||  k     rdt           j        z  | dz  z  S dS )a(  
    Compute the surface area of the intersection of sphere of radius R centered
    at (0, 0, 0) with a ball of radius r centered at (R, 0, 0).

    Parameters
    ----------
    R : float, sphere radius
    r : float, ball radius

    Returns
    --------
    area: float, the surface are.
    r      N)r   r   )RrY   rJ   s      r   sphere_ball_intersectionrd      sm     
QTAqDQU#AQBww25y1}A&&A2vv25y1a4 vr   )__doc__numpyr    r   scipy.sparser   ImportErrorEr   ExceptionWrapperr   r   r4   rP   rB   rd   r%   r   r   <module>rl      s              0''''''' 0 0 0,,Q//JJJJJJ0     &" " "B* * *Z* * *Z         s    :5: