use std::collections::HashMap;
use vrm_spec::vrmc_vrm_1_0::{HumanBoneName, Humanoid as SpecHumanoid};
use crate::NodeId;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum HumanBone {
Hips,
Spine,
Chest,
UpperChest,
Neck,
Head,
LeftEye,
RightEye,
Jaw,
LeftUpperLeg,
LeftLowerLeg,
LeftFoot,
LeftToes,
RightUpperLeg,
RightLowerLeg,
RightFoot,
RightToes,
LeftShoulder,
LeftUpperArm,
LeftLowerArm,
LeftHand,
RightShoulder,
RightUpperArm,
RightLowerArm,
RightHand,
LeftThumbMetacarpal,
LeftThumbProximal,
LeftThumbDistal,
LeftIndexProximal,
LeftIndexIntermediate,
LeftIndexDistal,
LeftMiddleProximal,
LeftMiddleIntermediate,
LeftMiddleDistal,
LeftRingProximal,
LeftRingIntermediate,
LeftRingDistal,
LeftLittleProximal,
LeftLittleIntermediate,
LeftLittleDistal,
RightThumbMetacarpal,
RightThumbProximal,
RightThumbDistal,
RightIndexProximal,
RightIndexIntermediate,
RightIndexDistal,
RightMiddleProximal,
RightMiddleIntermediate,
RightMiddleDistal,
RightRingProximal,
RightRingIntermediate,
RightRingDistal,
RightLittleProximal,
RightLittleIntermediate,
RightLittleDistal,
}
impl HumanBone {
pub(crate) const fn parent(self) -> Option<Self> {
use HumanBone::*;
Some(match self {
Hips => return None,
Spine => Hips,
Chest => Spine,
UpperChest => Chest,
Neck => UpperChest,
Head => Neck,
LeftEye | RightEye | Jaw => Head,
LeftUpperLeg => Hips,
LeftLowerLeg => LeftUpperLeg,
LeftFoot => LeftLowerLeg,
LeftToes => LeftFoot,
RightUpperLeg => Hips,
RightLowerLeg => RightUpperLeg,
RightFoot => RightLowerLeg,
RightToes => RightFoot,
LeftShoulder => UpperChest,
LeftUpperArm => LeftShoulder,
LeftLowerArm => LeftUpperArm,
LeftHand => LeftLowerArm,
RightShoulder => UpperChest,
RightUpperArm => RightShoulder,
RightLowerArm => RightUpperArm,
RightHand => RightLowerArm,
LeftThumbMetacarpal | LeftIndexProximal | LeftMiddleProximal | LeftRingProximal
| LeftLittleProximal => LeftHand,
LeftThumbProximal => LeftThumbMetacarpal,
LeftThumbDistal => LeftThumbProximal,
LeftIndexIntermediate => LeftIndexProximal,
LeftIndexDistal => LeftIndexIntermediate,
LeftMiddleIntermediate => LeftMiddleProximal,
LeftMiddleDistal => LeftMiddleIntermediate,
LeftRingIntermediate => LeftRingProximal,
LeftRingDistal => LeftRingIntermediate,
LeftLittleIntermediate => LeftLittleProximal,
LeftLittleDistal => LeftLittleIntermediate,
RightThumbMetacarpal | RightIndexProximal | RightMiddleProximal | RightRingProximal
| RightLittleProximal => RightHand,
RightThumbProximal => RightThumbMetacarpal,
RightThumbDistal => RightThumbProximal,
RightIndexIntermediate => RightIndexProximal,
RightIndexDistal => RightIndexIntermediate,
RightMiddleIntermediate => RightMiddleProximal,
RightMiddleDistal => RightMiddleIntermediate,
RightRingIntermediate => RightRingProximal,
RightRingDistal => RightRingIntermediate,
RightLittleIntermediate => RightLittleProximal,
RightLittleDistal => RightLittleIntermediate,
})
}
pub(crate) const fn requires_direct_parent(self) -> bool {
use HumanBone::*;
matches!(
self,
UpperChest
| LeftThumbProximal
| LeftThumbDistal
| LeftIndexIntermediate
| LeftIndexDistal
| LeftMiddleIntermediate
| LeftMiddleDistal
| LeftRingIntermediate
| LeftRingDistal
| LeftLittleIntermediate
| LeftLittleDistal
| RightThumbProximal
| RightThumbDistal
| RightIndexIntermediate
| RightIndexDistal
| RightMiddleIntermediate
| RightMiddleDistal
| RightRingIntermediate
| RightRingDistal
| RightLittleIntermediate
| RightLittleDistal
)
}
pub(crate) const fn spec_name(self) -> &'static str {
use HumanBone::*;
match self {
Hips => "hips",
Spine => "spine",
Chest => "chest",
UpperChest => "upperChest",
Neck => "neck",
Head => "head",
LeftEye => "leftEye",
RightEye => "rightEye",
Jaw => "jaw",
LeftUpperLeg => "leftUpperLeg",
LeftLowerLeg => "leftLowerLeg",
LeftFoot => "leftFoot",
LeftToes => "leftToes",
RightUpperLeg => "rightUpperLeg",
RightLowerLeg => "rightLowerLeg",
RightFoot => "rightFoot",
RightToes => "rightToes",
LeftShoulder => "leftShoulder",
LeftUpperArm => "leftUpperArm",
LeftLowerArm => "leftLowerArm",
LeftHand => "leftHand",
RightShoulder => "rightShoulder",
RightUpperArm => "rightUpperArm",
RightLowerArm => "rightLowerArm",
RightHand => "rightHand",
LeftThumbMetacarpal => "leftThumbMetacarpal",
LeftThumbProximal => "leftThumbProximal",
LeftThumbDistal => "leftThumbDistal",
LeftIndexProximal => "leftIndexProximal",
LeftIndexIntermediate => "leftIndexIntermediate",
LeftIndexDistal => "leftIndexDistal",
LeftMiddleProximal => "leftMiddleProximal",
LeftMiddleIntermediate => "leftMiddleIntermediate",
LeftMiddleDistal => "leftMiddleDistal",
LeftRingProximal => "leftRingProximal",
LeftRingIntermediate => "leftRingIntermediate",
LeftRingDistal => "leftRingDistal",
LeftLittleProximal => "leftLittleProximal",
LeftLittleIntermediate => "leftLittleIntermediate",
LeftLittleDistal => "leftLittleDistal",
RightThumbMetacarpal => "rightThumbMetacarpal",
RightThumbProximal => "rightThumbProximal",
RightThumbDistal => "rightThumbDistal",
RightIndexProximal => "rightIndexProximal",
RightIndexIntermediate => "rightIndexIntermediate",
RightIndexDistal => "rightIndexDistal",
RightMiddleProximal => "rightMiddleProximal",
RightMiddleIntermediate => "rightMiddleIntermediate",
RightMiddleDistal => "rightMiddleDistal",
RightRingProximal => "rightRingProximal",
RightRingIntermediate => "rightRingIntermediate",
RightRingDistal => "rightRingDistal",
RightLittleProximal => "rightLittleProximal",
RightLittleIntermediate => "rightLittleIntermediate",
RightLittleDistal => "rightLittleDistal",
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum HumanoidHierarchyViolation {
MissingRequiredParent {
bone: HumanBone,
required_parent: HumanBone,
},
InvalidParent {
bone: HumanBone,
expected_parent: Option<HumanBone>,
actual_parent: Option<HumanBone>,
},
}
impl HumanoidHierarchyViolation {
pub(crate) const fn bone(self) -> HumanBone {
match self {
Self::MissingRequiredParent { bone, .. } | Self::InvalidParent { bone, .. } => bone,
}
}
pub(crate) fn message(self) -> String {
match self {
Self::MissingRequiredParent {
bone,
required_parent,
} => format!(
"humanoid bone `{}` requires parent bone `{}`",
bone.spec_name(),
required_parent.spec_name()
),
Self::InvalidParent {
bone,
expected_parent: Some(expected_parent),
actual_parent,
} => {
let found = actual_parent
.map(|parent| format!("`{}`", parent.spec_name()))
.unwrap_or_else(|| "no mapped humanoid ancestor".to_string());
format!(
"humanoid bone `{}` must be a descendant of `{}` with that bone as its nearest mapped humanoid ancestor; found {found}",
bone.spec_name(),
expected_parent.spec_name()
)
}
Self::InvalidParent {
bone,
expected_parent: None,
actual_parent,
} => {
let found = actual_parent
.map(|parent| format!("`{}`", parent.spec_name()))
.unwrap_or_else(|| "an unknown bone".to_string());
format!(
"humanoid root bone `{}` must not have mapped humanoid ancestor {found}",
bone.spec_name()
)
}
}
}
}
pub(crate) fn hierarchy_violations(
bones: impl IntoIterator<Item = (HumanBone, usize)>,
node_parents: &[Option<usize>],
) -> Vec<HumanoidHierarchyViolation> {
let bone_nodes = bones.into_iter().collect::<HashMap<_, _>>();
let node_bones = bone_nodes
.iter()
.map(|(&bone, &node)| (node, bone))
.collect::<HashMap<_, _>>();
let mut violations = Vec::new();
for &bone in Humanoid::ALL_BONES {
let Some(&node) = bone_nodes.get(&bone) else {
continue;
};
let direct_parent = bone.parent();
if bone.requires_direct_parent()
&& let Some(required_parent) = direct_parent
&& !bone_nodes.contains_key(&required_parent)
{
violations.push(HumanoidHierarchyViolation::MissingRequiredParent {
bone,
required_parent,
});
}
let mut expected_parent = direct_parent;
while let Some(parent) = expected_parent {
if bone_nodes.contains_key(&parent) {
break;
}
expected_parent = parent.parent();
}
let mut cursor = node_parents.get(node).copied().flatten();
let mut actual_parent = None;
for _ in 0..node_parents.len() {
let Some(node) = cursor else {
break;
};
if let Some(&parent_bone) = node_bones.get(&node) {
actual_parent = Some(parent_bone);
break;
}
cursor = node_parents.get(node).copied().flatten();
}
if actual_parent != expected_parent {
violations.push(HumanoidHierarchyViolation::InvalidParent {
bone,
expected_parent,
actual_parent,
});
}
}
violations
}
impl From<HumanBoneName> for HumanBone {
fn from(name: HumanBoneName) -> Self {
match name {
HumanBoneName::Hips => Self::Hips,
HumanBoneName::Spine => Self::Spine,
HumanBoneName::Chest => Self::Chest,
HumanBoneName::UpperChest => Self::UpperChest,
HumanBoneName::Neck => Self::Neck,
HumanBoneName::Head => Self::Head,
HumanBoneName::LeftEye => Self::LeftEye,
HumanBoneName::RightEye => Self::RightEye,
HumanBoneName::Jaw => Self::Jaw,
HumanBoneName::LeftUpperLeg => Self::LeftUpperLeg,
HumanBoneName::LeftLowerLeg => Self::LeftLowerLeg,
HumanBoneName::LeftFoot => Self::LeftFoot,
HumanBoneName::LeftToes => Self::LeftToes,
HumanBoneName::RightUpperLeg => Self::RightUpperLeg,
HumanBoneName::RightLowerLeg => Self::RightLowerLeg,
HumanBoneName::RightFoot => Self::RightFoot,
HumanBoneName::RightToes => Self::RightToes,
HumanBoneName::LeftShoulder => Self::LeftShoulder,
HumanBoneName::LeftUpperArm => Self::LeftUpperArm,
HumanBoneName::LeftLowerArm => Self::LeftLowerArm,
HumanBoneName::LeftHand => Self::LeftHand,
HumanBoneName::RightShoulder => Self::RightShoulder,
HumanBoneName::RightUpperArm => Self::RightUpperArm,
HumanBoneName::RightLowerArm => Self::RightLowerArm,
HumanBoneName::RightHand => Self::RightHand,
HumanBoneName::LeftThumbMetacarpal => Self::LeftThumbMetacarpal,
HumanBoneName::LeftThumbProximal => Self::LeftThumbProximal,
HumanBoneName::LeftThumbDistal => Self::LeftThumbDistal,
HumanBoneName::LeftIndexProximal => Self::LeftIndexProximal,
HumanBoneName::LeftIndexIntermediate => Self::LeftIndexIntermediate,
HumanBoneName::LeftIndexDistal => Self::LeftIndexDistal,
HumanBoneName::LeftMiddleProximal => Self::LeftMiddleProximal,
HumanBoneName::LeftMiddleIntermediate => Self::LeftMiddleIntermediate,
HumanBoneName::LeftMiddleDistal => Self::LeftMiddleDistal,
HumanBoneName::LeftRingProximal => Self::LeftRingProximal,
HumanBoneName::LeftRingIntermediate => Self::LeftRingIntermediate,
HumanBoneName::LeftRingDistal => Self::LeftRingDistal,
HumanBoneName::LeftLittleProximal => Self::LeftLittleProximal,
HumanBoneName::LeftLittleIntermediate => Self::LeftLittleIntermediate,
HumanBoneName::LeftLittleDistal => Self::LeftLittleDistal,
HumanBoneName::RightThumbMetacarpal => Self::RightThumbMetacarpal,
HumanBoneName::RightThumbProximal => Self::RightThumbProximal,
HumanBoneName::RightThumbDistal => Self::RightThumbDistal,
HumanBoneName::RightIndexProximal => Self::RightIndexProximal,
HumanBoneName::RightIndexIntermediate => Self::RightIndexIntermediate,
HumanBoneName::RightIndexDistal => Self::RightIndexDistal,
HumanBoneName::RightMiddleProximal => Self::RightMiddleProximal,
HumanBoneName::RightMiddleIntermediate => Self::RightMiddleIntermediate,
HumanBoneName::RightMiddleDistal => Self::RightMiddleDistal,
HumanBoneName::RightRingProximal => Self::RightRingProximal,
HumanBoneName::RightRingIntermediate => Self::RightRingIntermediate,
HumanBoneName::RightRingDistal => Self::RightRingDistal,
HumanBoneName::RightLittleProximal => Self::RightLittleProximal,
HumanBoneName::RightLittleIntermediate => Self::RightLittleIntermediate,
HumanBoneName::RightLittleDistal => Self::RightLittleDistal,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct HumanBoneNode(pub u32);
#[derive(Debug, Clone, Default)]
pub struct Humanoid {
bones: HashMap<HumanBone, HumanBoneNode>,
}
impl Humanoid {
pub const ALL_BONES: &[HumanBone] = &[
HumanBone::Hips,
HumanBone::Spine,
HumanBone::Chest,
HumanBone::UpperChest,
HumanBone::Neck,
HumanBone::Head,
HumanBone::LeftEye,
HumanBone::RightEye,
HumanBone::Jaw,
HumanBone::LeftUpperLeg,
HumanBone::LeftLowerLeg,
HumanBone::LeftFoot,
HumanBone::LeftToes,
HumanBone::RightUpperLeg,
HumanBone::RightLowerLeg,
HumanBone::RightFoot,
HumanBone::RightToes,
HumanBone::LeftShoulder,
HumanBone::LeftUpperArm,
HumanBone::LeftLowerArm,
HumanBone::LeftHand,
HumanBone::RightShoulder,
HumanBone::RightUpperArm,
HumanBone::RightLowerArm,
HumanBone::RightHand,
HumanBone::LeftThumbMetacarpal,
HumanBone::LeftThumbProximal,
HumanBone::LeftThumbDistal,
HumanBone::LeftIndexProximal,
HumanBone::LeftIndexIntermediate,
HumanBone::LeftIndexDistal,
HumanBone::LeftMiddleProximal,
HumanBone::LeftMiddleIntermediate,
HumanBone::LeftMiddleDistal,
HumanBone::LeftRingProximal,
HumanBone::LeftRingIntermediate,
HumanBone::LeftRingDistal,
HumanBone::LeftLittleProximal,
HumanBone::LeftLittleIntermediate,
HumanBone::LeftLittleDistal,
HumanBone::RightThumbMetacarpal,
HumanBone::RightThumbProximal,
HumanBone::RightThumbDistal,
HumanBone::RightIndexProximal,
HumanBone::RightIndexIntermediate,
HumanBone::RightIndexDistal,
HumanBone::RightMiddleProximal,
HumanBone::RightMiddleIntermediate,
HumanBone::RightMiddleDistal,
HumanBone::RightRingProximal,
HumanBone::RightRingIntermediate,
HumanBone::RightRingDistal,
HumanBone::RightLittleProximal,
HumanBone::RightLittleIntermediate,
HumanBone::RightLittleDistal,
];
pub fn bone(&self, bone: HumanBone) -> Option<&HumanBoneNode> {
self.bones.get(&bone)
}
pub fn bone_id(&self, bone: HumanBone) -> Option<NodeId> {
self.bone(bone)
.map(|node| NodeId::from_index(node.0 as usize))
}
pub fn bones(&self) -> impl Iterator<Item = (&HumanBone, &HumanBoneNode)> {
self.bones.iter()
}
pub fn bone_ids(&self) -> impl Iterator<Item = (HumanBone, NodeId)> + '_ {
self.bones
.iter()
.map(|(&bone, node)| (bone, NodeId::from_index(node.0 as usize)))
}
pub fn with_bone(mut self, bone: HumanBone, node_index: u32) -> Self {
self.bones.insert(bone, HumanBoneNode(node_index));
self
}
pub(crate) fn load_humanoid(schema: &SpecHumanoid) -> Self {
let mut bones = HashMap::new();
for (bone_name, bone_spec) in &schema.human_bones.0 {
let Some(node) = bone_spec.as_ref().and_then(|spec| spec.node.as_ref()) else {
continue;
};
let human_bone = HumanBone::from(*bone_name);
let node_index = node.value() as u32;
bones.insert(human_bone, HumanBoneNode(node_index));
}
Self { bones }
}
}