1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
use num_derive::{FromPrimitive, ToPrimitive};
use num_traits::FromPrimitive;
use crate::{
conventions::{forward_vec, up_vec},
newtypes::{Global, Local},
UnitQuat,
};
#[allow(rustdoc::private_intra_doc_links)]
#[repr(u8)]
#[derive(Debug, Clone, Copy, Eq, Hash, PartialEq, FromPrimitive, ToPrimitive)]
pub enum BoneKind {
Neck = 0,
Chest,
Waist,
Hip,
ThighL,
ThighR,
AnkleL,
AnkleR,
FootL,
FootR,
UpperArmL,
UpperArmR,
ForearmL,
ForearmR,
WristL,
WristR,
}
impl BoneKind {
pub const fn max() -> BoneKind {
BoneKind::WristR
}
pub const MAX: BoneKind = Self::max();
pub const fn min() -> BoneKind {
BoneKind::root()
}
pub const MIN: BoneKind = Self::min();
pub const fn root() -> Self {
Self::Neck
}
pub const ROOT: BoneKind = Self::root();
pub const fn num_types() -> usize {
BoneKind::max() as usize + 1
}
pub const NUM_TYPES: usize = Self::num_types();
pub const fn children(&self) -> &'static [Self] {
use BoneKind::*;
match self {
Neck => &[Chest, UpperArmL, UpperArmR],
Chest => &[Waist],
Waist => &[Hip],
Hip => &[ThighL, ThighR],
ThighL => &[AnkleL],
ThighR => &[AnkleR],
AnkleL => &[FootL],
AnkleR => &[FootR],
FootL => &[],
FootR => &[],
UpperArmL => &[ForearmL],
UpperArmR => &[ForearmR],
ForearmL => &[WristL],
ForearmR => &[WristR],
WristR => &[],
WristL => &[],
}
}
pub const fn parent(&self) -> Option<BoneKind> {
use BoneKind::*;
Some(match self {
Neck => return None,
Chest => Neck,
Waist => Chest,
Hip => Waist,
ThighL => Hip,
ThighR => Hip,
AnkleL => ThighL,
AnkleR => ThighR,
FootL => AnkleL,
FootR => AnkleR,
UpperArmL => Neck,
UpperArmR => Neck,
ForearmL => UpperArmL,
ForearmR => UpperArmR,
WristL => ForearmL,
WristR => ForearmR,
})
}
pub fn iter() -> std::iter::Map<std::ops::RangeInclusive<u8>, fn(u8) -> BoneKind> {
(Self::MIN as u8..=Self::MAX as u8).map(|x| x.try_into().unwrap())
}
pub fn calibration_rotation(self) -> Global<UnitQuat> {
use BoneKind::*;
Global(match self {
FootL | FootR => UnitQuat::look_at_rh(&-up_vec(), &forward_vec()),
_ => UnitQuat::default(),
})
}
pub fn calibration_rotation_local(self) -> Local<UnitQuat> {
let child_rot_g = self.calibration_rotation();
let parent_rot_g = self.parent().unwrap_or(self).calibration_rotation();
Local(parent_rot_g.0.rotation_to(&child_rot_g.0))
}
}
impl TryFrom<u8> for BoneKind {
type Error = ();
fn try_from(value: u8) -> Result<Self, Self::Error> {
FromPrimitive::from_u8(value).ok_or(())
}
}
impl TryFrom<usize> for BoneKind {
type Error = ();
fn try_from(value: usize) -> Result<Self, Self::Error> {
FromPrimitive::from_usize(value).ok_or(())
}
}
impl From<BoneKind> for u8 {
fn from(other: BoneKind) -> Self {
other as _
}
}
impl From<BoneKind> for usize {
fn from(other: BoneKind) -> Self {
other as _
}
}