use std::collections::{BTreeMap, HashMap};
#[derive(Debug, Clone)]
pub struct MotionConfig {
pub joint_count: u32,
pub sample_rate: u32,
pub enable_prediction: bool,
}
impl Default for MotionConfig {
fn default() -> Self {
Self {
joint_count: 25,
sample_rate: 60,
enable_prediction: true,
}
}
}
#[derive(Debug, Clone, Copy)]
pub struct JointPosition {
pub position: [f32; 3],
pub rotation: [f32; 4],
pub confidence: f32,
}
impl Default for JointPosition {
fn default() -> Self {
Self {
position: [0.0, 0.0, 0.0],
rotation: [0.0, 0.0, 0.0, 1.0],
confidence: 1.0,
}
}
}
#[derive(Debug, Clone)]
pub struct BodyPose {
pub joints: Vec<JointPosition>,
pub confidence: f32,
pub timestamp: u64,
}
impl Default for BodyPose {
fn default() -> Self {
Self {
joints: vec![JointPosition::default(); 25],
confidence: 1.0,
timestamp: 0,
}
}
}
pub struct MotionCapture {
config: MotionConfig,
current_pose: Option<BodyPose>,
frame_count: u64,
}
impl MotionCapture {
pub fn new(config: MotionConfig) -> Result<Self, MotionCaptureError> {
Ok(Self {
config,
current_pose: None,
frame_count: 0,
})
}
pub fn joint_count(&self) -> u32 {
self.config.joint_count
}
pub fn get_body_pose(&mut self) -> Result<BodyPose, MotionCaptureError> {
self.frame_count += 1;
let pose: _ = BodyPose {
joints: vec![JointPosition::default(); self.config.joint_count as usize],
confidence: 0.95,
timestamp: self.frame_count,
};
self.current_pose = Some(pose.clone());
Ok(pose)
}
pub fn current_pose(&self) -> Option<&BodyPose> {
self.current_pose.as_ref()
}
pub fn prediction_enabled(&self) -> bool {
self.config.enable_prediction
}
}
#[derive(Debug, Clone)]
pub enum MotionCaptureError {
InitializationFailed(String),
CaptureFailed(String),
DeviceNotFound,
}
impl std::fmt::Display for MotionCaptureError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::InitializationFailed(msg) => write!(f, "初始化失败: {}", msg),
Self::CaptureFailed(msg) => write!(f, "捕捉失败: {}", msg),
Self::DeviceNotFound => write!(f, "设备未找到"),
}
}
}
impl std::error::Error for MotionCaptureError {}