o
    iY	                     @  sV   d Z ddlmZ ddlmZ ddlZddlmZm	Z	m
Z
 dd
dZdefdddZdS )u   Archetype layer: RBF interpolation over authored preset blendshapes.

Per docs/research/vad-to-arkit-blendshape-mapping.md §6.1.
    )annotations)OptionalN   )	RBF_SIGMARBF_EMOTION_FAMILY_BOOSTRBF_NEUTRAL_DEEMPHASISpreset_namestrreturnc                 C  s   d| v r|  dd S | S )uB   Extract emotion family from preset name (e.g. 'joy_L3' → 'joy')._r   )split)r    r   R/dataset/kemix-engine/package/face/animasync-face-v3/scripts/compiler/archetype.py_extract_family   s   r   vad
np.ndarraypresetsdictemotion_hintOptional[str]sigmafloatc                   s`  |s
t jdt jdS t j t jd g }g }| D ]W\}}t j|d t jd}tt j | }	tt |	d  d| |  }
|dur_t	|}||krS|
t
9 }
n|dkr_|dkr_|
t9 }
||
 |t j|d t jd qt j|t jd}| d	k r fd
d| D }tt |}||  S ||  }t j|dd}t d||t jS )a  Weight-blend preset blendshapes by RBF distance in VAD space.

    Args:
        vad: (3,) float, query VAD in [-1, 1]
        presets: dict {name: {"vad": [V,A,D], "bs": np.ndarray(52,)}}
        emotion_hint: optional family name (e.g. 'joy'); boosts same-family presets
        sigma: RBF Gaussian bandwidth

    Returns:
        (52,) float32 weighted blend of preset blendshapes
    4   dtyper      g       @Nneutralbsgư>c              
     s0   g | ]}t tj tj|d  tjd qS )r   r   )r   nplinalgnormasarrayfloat32).0dr   r   r   
<listcomp>E   s    "z#archetype_blend.<locals>.<listcomp>r   )axiszi,ij->j)r   zerosr"   r!   itemsr   r   r    expr   r   r   appendsumvaluesintargmincopystackeinsumastype)r   r   r   r   weightsbs_listnamedata
anchor_vaddistwfamily	distancesidxbs_stackr   r%   r   archetype_blend   s6    


r?   )r   r	   r
   r	   )
r   r   r   r   r   r   r   r   r
   r   )__doc__
__future__r   typingr   numpyr   	constantsr   r   r   r   r?   r   r   r   r   <module>   s    

