
    i.                     J    d dl mZ d dlZd dlmZ 	 d
dededee   dedef
d	Zy)    )ListN)Tensorvertices_predvertices_gt	mouth_mapvalidate_argsreturnc                    |r| j                   dk7  s|j                   dk7  r&t        d| j                    d|j                    d      | j                  dd |j                  dd k7  r&t        d| j                   d|j                   d      |st        d	      t        |      | j                  d   k\  r(t        d
t        |       d| j                  d    d      t	        | j                  d   |j                  d         }| d| } |d| }|dd|ddf   | dd|ddf   z
  }t        j                  |dz  d      }t        j                  |d      j                  }t        j                  |      S )a  Compute Lip Vertex Error (LVE) for 3D talking head evaluation.

    The Lip Vertex Error (LVE) metric evaluates the quality of lip synchronization in 3D facial animations by measuring
    the maximum Euclidean distance (L2 error) between corresponding lip vertices of the generated and ground truth
    meshes for each frame. The metric is defined as:

    .. math::
        \text{LVE} = \frac{1}{N} \sum_{i=1}^{N} \max_{v \in \text{lip}} \|x_{i,v} - \hat{x}_{i,v}\|_2^2

    where :math:`N` is the number of frames, :math:`x_{i,v}` represents the 3D coordinates of vertex :math:`v` in the
    lip region of the ground truth frame :math:`i`, and :math:`\hat{x}_{i,v}` represents the corresponding vertex in
    the predicted frame. The metric computes the maximum squared L2 distance between corresponding lip vertices for each
    frame and averages across all frames. A lower LVE value indicates better lip synchronization quality.

    Args:
        vertices_pred: Predicted vertices tensor of shape (T, V, 3) where T is number of frames,
            V is number of vertices, and 3 represents XYZ coordinates
        vertices_gt: Ground truth vertices tensor of shape (T', V, 3) where T' can be different from T
        mouth_map: List of vertex indices corresponding to the mouth region
        validate_args: bool indicating if input arguments and tensors should be validated for correctness.
            Set to ``False`` for faster computations.

    Returns:
        torch.Tensor: Scalar tensor containing the mean LVE value across all frames

    Raises:
        ValueError:
            If the number of dimensions of `vertices_pred` or `vertices_gt` is not 3.
            If vertex dimensions (V) or coordinate dimensions (3) don't match
            If ``mouth_map`` is empty or contains invalid indices

    Example:
        >>> import torch
        >>> from torchmetrics.functional.multimodal import lip_vertex_error
        >>> vertices_pred = torch.randn(10, 100, 3, generator=torch.manual_seed(42))
        >>> vertices_gt = torch.randn(10, 100, 3, generator=torch.manual_seed(43))
        >>> mouth_map = [0, 1, 2, 3, 4]
        >>> lip_vertex_error(vertices_pred, vertices_gt, mouth_map)
        tensor(12.7688)

       zIExpected both vertices_pred and vertices_gt to have 3 dimensions but got z and z dimensions respectively.   NzdExpected vertices_pred and vertices_gt to have same vertex and coordinate dimensions but got shapes .zmouth_map cannot be empty.z5mouth_map contains invalid vertex indices. Max index z# is larger than number of vertices r      )dim)	ndim
ValueErrorshapemaxmintorchsumvaluesmean)r   r   r   r   
min_framesdiffsq_distmax_per_frames           {/Volumes/fast/ai/experiments/voice-extract-mac/.venv/lib/python3.12/site-packages/torchmetrics/functional/multimodal/lve.pylip_vertex_errorr      s   ^ "k&6&6!&;[ %%&eK,<,<+==VX  qr"k&7&7&;;'--.eK4E4E3FaI  9::y>]0033GIGW X&&3&9&9!&<%=Q@ 
 ]((+[->->q-ABJ!+:.Mkz*Kq)Q'-9a*HHDiiaR(GIIg1-44M::m$$    )T)typingr   r   r   intboolr    r    r   <module>r%      sR       	I%I%I% CyI% 	I%
 I%r    