use std::collections::{BTreeMap, HashMap};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Gesture {
Unknown,
Fist,
Open,
Pinch,
Point,
ThumbsUp,
Ok,
Peace,
Grab,
}
impl Default for Gesture {
fn default() -> Self {
Self::Unknown
}
}
#[derive(Debug, Clone)]
pub struct HandTrackingConfig {
pub sample_rate: u32,
pub enable_gesture_recognition: bool,
pub prediction_latency_ms: u32,
}
impl Default for HandTrackingConfig {
fn default() -> Self {
Self {
sample_rate: 60,
enable_gesture_recognition: true,
prediction_latency_ms: 10,
}
}
}
#[derive(Debug, Clone)]
pub struct MockHandData {
pub joints: Vec<[f32; 3]>,
pub confidence: f32,
}
impl Default for MockHandData {
fn default() -> Self {
Self {
joints: vec![[0.0, 0.0, 0.0]; 21],
confidence: 1.0,
}
}
}
#[derive(Debug, Clone)]
pub struct HandPose {
pub joints: Vec<[f32; 3]>,
pub confidence: f32,
pub is_left: bool,
pub gesture: Gesture,
}
impl HandPose {
pub fn mock_pinch() -> Self {
Self {
joints: vec![[0.0, 0.0, 0.0]; 21],
confidence: 0.95,
is_left: false,
gesture: Gesture::Pinch,
}
}
}
pub struct HandTracking {
config: HandTrackingConfig,
current_pose: Option<HandPose>,
}
impl HandTracking {
pub fn new(config: HandTrackingConfig) -> Result<Self, HandTrackingError> {
Ok(Self {
config,
current_pose: None,
})
}
pub fn gesture_recognition_enabled(&self) -> bool {
self.config.enable_gesture_recognition
}
pub fn process_frame(&mut self, data: &MockHandData) -> Result<HandPose, HandTrackingError> {
let pose: _ = HandPose {
joints: data.joints.clone(),
confidence: data.confidence,
is_left: false,
gesture: Gesture::Unknown,
};
self.current_pose = Some(pose.clone());
Ok(pose)
}
pub fn process_frame_sync(&self, _data: &MockHandData) -> Result<HandPose, HandTrackingError> {
Ok(HandPose {
joints: vec![[0.0, 0.0, 0.0]; 21],
confidence: 1.0,
is_left: false,
gesture: Gesture::Unknown,
})
}
pub fn recognize_gesture(&self, pose: &HandPose) -> Result<Gesture, HandTrackingError> {
Ok(pose.gesture)
}
pub fn current_pose(&self) -> Option<&HandPose> {
self.current_pose.as_ref()
}
}
#[derive(Debug, Clone)]
pub enum HandTrackingError {
InitializationFailed(String),
TrackingFailed(String),
DeviceNotFound,
}
impl std::fmt::Display for HandTrackingError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::InitializationFailed(msg) => write!(f, "初始化失败: {}", msg),
Self::TrackingFailed(msg) => write!(f, "追踪失败: {}", msg),
Self::DeviceNotFound => write!(f, "设备未找到"),
}
}
}
impl std::error::Error for HandTrackingError {}