webxr_api/
hand.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
5use euclid::RigidTransform3D;
6use malloc_size_of_derive::MallocSizeOf;
7use serde::{Deserialize, Serialize};
8
9use crate::Native;
10
11#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq, Serialize, Deserialize)]
12pub struct HandSpace;
13
14#[derive(Clone, Debug, Default, Serialize, Deserialize, MallocSizeOf)]
15pub struct Hand<J> {
16    pub wrist: Option<J>,
17    pub thumb_metacarpal: Option<J>,
18    pub thumb_phalanx_proximal: Option<J>,
19    pub thumb_phalanx_distal: Option<J>,
20    pub thumb_phalanx_tip: Option<J>,
21    pub index: Finger<J>,
22    pub middle: Finger<J>,
23    pub ring: Finger<J>,
24    pub little: Finger<J>,
25}
26
27#[derive(Clone, Debug, Default, Serialize, Deserialize, MallocSizeOf)]
28pub struct Finger<J> {
29    pub metacarpal: Option<J>,
30    pub phalanx_proximal: Option<J>,
31    pub phalanx_intermediate: Option<J>,
32    pub phalanx_distal: Option<J>,
33    pub phalanx_tip: Option<J>,
34}
35
36#[derive(Clone, Copy, Debug, Serialize, Deserialize, MallocSizeOf)]
37pub struct JointFrame {
38    pub pose: RigidTransform3D<f32, HandSpace, Native>,
39    pub radius: f32,
40}
41
42impl Default for JointFrame {
43    fn default() -> Self {
44        Self {
45            pose: RigidTransform3D::identity(),
46            radius: 0.,
47        }
48    }
49}
50
51impl<J> Hand<J> {
52    pub fn map<R>(&self, map: impl (Fn(&Option<J>, Joint) -> Option<R>) + Copy) -> Hand<R> {
53        Hand {
54            wrist: map(&self.wrist, Joint::Wrist),
55            thumb_metacarpal: map(&self.thumb_metacarpal, Joint::ThumbMetacarpal),
56            thumb_phalanx_proximal: map(&self.thumb_phalanx_proximal, Joint::ThumbPhalanxProximal),
57            thumb_phalanx_distal: map(&self.thumb_phalanx_distal, Joint::ThumbPhalanxDistal),
58            thumb_phalanx_tip: map(&self.thumb_phalanx_tip, Joint::ThumbPhalanxTip),
59            index: self.index.map(|f, j| map(f, Joint::Index(j))),
60            middle: self.middle.map(|f, j| map(f, Joint::Middle(j))),
61            ring: self.ring.map(|f, j| map(f, Joint::Ring(j))),
62            little: self.little.map(|f, j| map(f, Joint::Little(j))),
63        }
64    }
65
66    pub fn get(&self, joint: Joint) -> Option<&J> {
67        match joint {
68            Joint::Wrist => self.wrist.as_ref(),
69            Joint::ThumbMetacarpal => self.thumb_metacarpal.as_ref(),
70            Joint::ThumbPhalanxProximal => self.thumb_phalanx_proximal.as_ref(),
71            Joint::ThumbPhalanxDistal => self.thumb_phalanx_distal.as_ref(),
72            Joint::ThumbPhalanxTip => self.thumb_phalanx_tip.as_ref(),
73            Joint::Index(f) => self.index.get(f),
74            Joint::Middle(f) => self.middle.get(f),
75            Joint::Ring(f) => self.ring.get(f),
76            Joint::Little(f) => self.little.get(f),
77        }
78    }
79}
80
81impl<J> Finger<J> {
82    pub fn map<R>(&self, map: impl (Fn(&Option<J>, FingerJoint) -> Option<R>) + Copy) -> Finger<R> {
83        Finger {
84            metacarpal: map(&self.metacarpal, FingerJoint::Metacarpal),
85            phalanx_proximal: map(&self.phalanx_proximal, FingerJoint::PhalanxProximal),
86            phalanx_intermediate: map(&self.phalanx_intermediate, FingerJoint::PhalanxIntermediate),
87            phalanx_distal: map(&self.phalanx_distal, FingerJoint::PhalanxDistal),
88            phalanx_tip: map(&self.phalanx_tip, FingerJoint::PhalanxTip),
89        }
90    }
91
92    pub fn get(&self, joint: FingerJoint) -> Option<&J> {
93        match joint {
94            FingerJoint::Metacarpal => self.metacarpal.as_ref(),
95            FingerJoint::PhalanxProximal => self.phalanx_proximal.as_ref(),
96            FingerJoint::PhalanxIntermediate => self.phalanx_intermediate.as_ref(),
97            FingerJoint::PhalanxDistal => self.phalanx_distal.as_ref(),
98            FingerJoint::PhalanxTip => self.phalanx_tip.as_ref(),
99        }
100    }
101}
102
103#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize, Deserialize)]
104pub enum FingerJoint {
105    Metacarpal,
106    PhalanxProximal,
107    PhalanxIntermediate,
108    PhalanxDistal,
109    PhalanxTip,
110}
111
112#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize, Deserialize)]
113pub enum Joint {
114    Wrist,
115    ThumbMetacarpal,
116    ThumbPhalanxProximal,
117    ThumbPhalanxDistal,
118    ThumbPhalanxTip,
119    Index(FingerJoint),
120    Middle(FingerJoint),
121    Ring(FingerJoint),
122    Little(FingerJoint),
123}