Skip to main content

qualia_vision/biosense/face_mesh/
mediapipe_index.rs

1//! MediaPipe Face Mesh index → [`PadLandmarkId`] mapping for pure-landmark PAD.
2//!
3//! Full Face Mesh exposes **468** landmarks. PAD keeps only the eight
4//! geometry-critical slots. Indices below match MediaPipe Face Mesh topology
5//! (same numbers as `PadLandmarkId::mediapipe_index`).
6//!
7//! | Role | MediaPipe index | [`PadLandmarkId`] |
8//! |------|-----------------|-------------------|
9//! | Nose tip | **1** | `NoseTip` |
10//! | Chin | **152** | `Chin` |
11//! | Left eye outer | **33** | `LeftEyeOuter` |
12//! | Right eye outer | **263** | `RightEyeOuter` |
13//! | Left cheek / face edge | **234** | `LeftCheek` |
14//! | Right cheek / face edge | **454** | `RightCheek` |
15//! | Upper lip | **13** | `UpperLip` |
16//! | Lower lip | **14** | `LowerLip` |
17//!
18//! PAR flat-mask lock uses **1 / 234 / 454** (\(N, L, R\)) on raw image \(x\) only.
19//! Never use model \(Z\).
20
21use crate::biosense::liveness::landmark_types::PadLandmarkId;
22
23/// MediaPipe Face Mesh landmark count (standard tesselation).
24pub const MEDIAPIPE_FACE_MESH_COUNT: usize = 468;
25
26/// Canonical MediaPipe indices for the PAD packing order
27/// (`PadLandmarkId` discriminant order 0..7).
28pub const PAD_MEDIAPIPE_INDICES: [u16; PadLandmarkId::COUNT] = [
29    1,   // NoseTip
30    152, // Chin
31    33,  // LeftEyeOuter
32    263, // RightEyeOuter
33    234, // LeftCheek
34    454, // RightCheek
35    13,  // UpperLip
36    14,  // LowerLip
37];
38
39/// MediaPipe index for a PAD slot (delegates to [`PadLandmarkId::mediapipe_index`]).
40#[inline]
41pub const fn mediapipe_index_for_pad(id: PadLandmarkId) -> u16 {
42    id.mediapipe_index()
43}
44
45/// Inverse map: MediaPipe index → PAD slot, if that index is one of the eight.
46pub const fn pad_id_for_mediapipe_index(mp: u16) -> Option<PadLandmarkId> {
47    match mp {
48        1 => Some(PadLandmarkId::NoseTip),
49        152 => Some(PadLandmarkId::Chin),
50        33 => Some(PadLandmarkId::LeftEyeOuter),
51        263 => Some(PadLandmarkId::RightEyeOuter),
52        234 => Some(PadLandmarkId::LeftCheek),
53        454 => Some(PadLandmarkId::RightCheek),
54        13 => Some(PadLandmarkId::UpperLip),
55        14 => Some(PadLandmarkId::LowerLip),
56        _ => None,
57    }
58}
59
60/// All PAD slots as a fixed table (stack-friendly iteration).
61pub const PAD_LANDMARK_IDS: [PadLandmarkId; PadLandmarkId::COUNT] = [
62    PadLandmarkId::NoseTip,
63    PadLandmarkId::Chin,
64    PadLandmarkId::LeftEyeOuter,
65    PadLandmarkId::RightEyeOuter,
66    PadLandmarkId::LeftCheek,
67    PadLandmarkId::RightCheek,
68    PadLandmarkId::UpperLip,
69    PadLandmarkId::LowerLip,
70];
71
72#[cfg(test)]
73mod tests {
74    use super::*;
75
76    #[test]
77    fn table_matches_pad_landmark_id() {
78        for id in PAD_LANDMARK_IDS {
79            assert_eq!(
80                PAD_MEDIAPIPE_INDICES[id as usize],
81                id.mediapipe_index(),
82                "slot {:?}",
83                id
84            );
85            assert_eq!(mediapipe_index_for_pad(id), id.mediapipe_index());
86            assert_eq!(pad_id_for_mediapipe_index(id.mediapipe_index()), Some(id));
87        }
88    }
89
90    #[test]
91    fn par_triple_documented() {
92        assert_eq!(PadLandmarkId::NoseTip.mediapipe_index(), 1);
93        assert_eq!(PadLandmarkId::LeftCheek.mediapipe_index(), 234);
94        assert_eq!(PadLandmarkId::RightCheek.mediapipe_index(), 454);
95    }
96
97    #[test]
98    fn unknown_index_none() {
99        assert!(pad_id_for_mediapipe_index(0).is_none());
100        assert!(pad_id_for_mediapipe_index(100).is_none());
101        assert!(pad_id_for_mediapipe_index(467).is_none());
102    }
103}