
    j                         d Z ddlZddlmZ ddlmZ 	 ddlmZ d Zd	 Zd
 Zd Zd Zd Zy# e$ r"Z	ddlm
Z
  e
j                  e	      ZY dZ	[	5dZ	[	ww xY w)z5
curvature.py
---------------

Query mesh curvature.
    N   )util)diagonal_dot)
coo_matrix)
exceptionsc                     t        | j                  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     >/DATA/.local/lib/python3.12/site-packages/trimesh/curvature.pyface_angles_sparser      sX     				!	!	#d&7&7&;&;T=N=N=R=R%STF M    c                     t        j                  | j                  j                  d            j	                         }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   %   sF     0044!4<=EEGI"%%i9$FMr   c                 N   t        j                  |t         j                        }t        j                  |d      st        d      | j                  j                  ||      }|D cg c]  }| j                  |   j                         ! }}t        j                  |      S c c}w )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)!)r   
asanyarrayfloat64r   is_shape
ValueErrorkdtreequery_ball_pointr   r   asarray)r   pointsradiusnearestvertices
gauss_curvs         r   #discrete_gaussian_curvature_measurer1   8   s    . ]]64F==)011kk**66:GFMNg($%%h/335gJN::j!! Os   '$B"c                    t        j                  |t         j                        }t        j                  |d      st        d      t        j                  ||z
  ||z   f      }|D cg c]&  }t        | j                  j                  |            ( }}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  j)                         d	z  ||<    |S c c}w )
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$   Nr   r   )centerr-   r"   r   )r   r%   r&   r   r'   r(   column_stacklistface_adjacency_treeintersectionzeroslen	enumeratezipr/   face_adjacency_edgesline_ball_intersectionface_adjacency_angleswhereface_adjacency_convexr   )r   r,   r-   boundsb
candidates	mean_curvixx_candidates	endpointslengthsanglessignss                 r   discrete_mean_curvature_measurerL   Y   sJ   , ]]64F==)011 __fvov?@F KQQ&Q$t//<<Q?@&JQV%I )#fj*A BA|MM$";";L"IJ	(adOYq!t_Qv
 ++L933LA1bI&(50557!;	! !C  Rs   (+Ec                     || z
  }| |z
  }|}t        ||      }t        ||      }t        ||      }	|dz  ||	|dz  z
  z  z
  }
t        j                  t        |             }|
dkD  }||    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.

    r   r   r   )r   r   r8   r9   sqrtclip)start_points
end_pointsr3   r-   LocrldotlldotococdotocdiscrimsrI   md1d2s                  r   r=   r=      s   ( 	\!A		BAAE!R F2r"Gqy5GadN33H hhs<()G1A!9*rwwx{+
+uQx	7B!9*rwwx{+
+uQx	7B 
Q	B	Q	B r'RWWU1X..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 y)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   )RrT   rF   s      r   sphere_ball_intersectionr_      sk     
QTAqDQU#AQBw255y1}A&&A2v255y1a4 r   )__doc__numpyr    r   r   scipy.sparser   ImportErrorEr   ExceptionWrapperr   r   r1   rL   r=   r_    r   r   <module>rh      sf      0' &"B*Z*Z I  0,,,Q/J0s   - AAA