Skip to main content

qualia_vision/biosense/liveness/
challenge_kind.rs

1//! Closed set of liveness challenges (challenge-only PAD).
2
3#[derive(Debug, Clone, Copy, PartialEq, Eq)]
4#[repr(u8)]
5pub enum ChallengeKind {
6    YawLeft = 1,
7    YawRight = 2,
8    Smile = 3,
9    BlinkTwice = 4,
10    /// Open mouth (landmark mouth-gap ratio).
11    OpenMouth = 5,
12    /// Look slightly up (pitch).
13    PitchUp = 6,
14    /// Look slightly down (pitch).
15    PitchDown = 7,
16}
17
18impl ChallengeKind {
19    pub const ALL: [ChallengeKind; 7] = [
20        Self::YawLeft,
21        Self::YawRight,
22        Self::Smile,
23        Self::BlinkTwice,
24        Self::OpenMouth,
25        Self::PitchUp,
26        Self::PitchDown,
27    ];
28
29    /// Challenges that exercise non-rigid Z under rigid rotation (prefer for PAD strength).
30    pub const ROTATION: [ChallengeKind; 4] = [
31        Self::YawLeft,
32        Self::YawRight,
33        Self::PitchUp,
34        Self::PitchDown,
35    ];
36
37    /// `&str` form.
38    pub const fn as_str(self) -> &'static str {
39        match self {
40            Self::YawLeft => "yaw_left",
41            Self::YawRight => "yaw_right",
42            Self::Smile => "smile",
43            Self::BlinkTwice => "blink_2",
44            Self::OpenMouth => "open_mouth",
45            Self::PitchUp => "pitch_up",
46            Self::PitchDown => "pitch_down",
47        }
48    }
49
50    /// `true` when rotation.
51    pub const fn is_rotation(self) -> bool {
52        matches!(
53            self,
54            Self::YawLeft | Self::YawRight | Self::PitchUp | Self::PitchDown
55        )
56    }
57
58    /// Deterministic pick from seed (no heap RNG required).
59    pub fn from_seed(seed: u64) -> Self {
60        Self::ALL[(seed as usize) % Self::ALL.len()]
61    }
62
63    /// Prefer rotation challenges for stronger 3D PAD (still deterministic).
64    pub fn from_seed_prefer_rotation(seed: u64) -> Self {
65        Self::ROTATION[(seed as usize) % Self::ROTATION.len()]
66    }
67}