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
use crate::{BlenderArmature, Bone};

/// A coordinate system is used to make sense of coordinates.
///
/// Without knowing the coordinate system you wouldn't know if a value is meant for the
/// Y axis, or Z axis.
#[derive(Debug, Eq, PartialEq, Copy, Clone, Serialize, Deserialize)]
pub struct CoordinateSystem {
    up: Axis,
    hand: Hand,
}

impl CoordinateSystem {
    #[allow(missing_docs)]
    pub fn new(up: Axis, hand: Hand) -> Self {
        CoordinateSystem { up, hand }
    }
}

#[allow(missing_docs)]
#[derive(Debug, Eq, PartialEq, Copy, Clone, Serialize, Deserialize)]
pub enum Axis {
    X,
    Y,
    Z,
}

/// Represents the orientation of the coordinate system using the [right hand rule].
///
/// [right hand rule]: https://en.wikipedia.org/wiki/Right-hand_rule
#[derive(Debug, Eq, PartialEq, Copy, Clone, Serialize, Deserialize)]
pub enum Hand {
    /// A right handed coordinate system
    Right,
    /// A left handed coordinate system
    Left,
}

/// Blender's coordinate system is Z up right handed
impl Default for CoordinateSystem {
    fn default() -> Self {
        CoordinateSystem {
            up: Axis::Z,
            hand: Hand::Right,
        }
    }
}

impl BlenderArmature {
    /// Shift around the data in the armature to a new coordinate system.
    ///
    /// For example, if the armature was previously Z up and we're switching to Y up
    ///  - the new +Y axis would be the old +Z axis
    ///  - the new +Z axis would be the old -Y axis
    pub fn change_coordinate_system(&mut self, system: CoordinateSystem) {
        if self.coordinate_system == system {
            return;
        }

        match (
            (self.coordinate_system.hand, self.coordinate_system.up),
            (system.hand, system.up),
        ) {
            ((Hand::Right, Axis::Z), (Hand::Right, Axis::Y)) => {
                for bone in self.inverse_bind_poses.iter_mut() {
                    *bone = dual_quat_z_up_right_to_y_up_right(*bone);
                }

                for (_action_name, action) in self.bone_space_actions.iter_mut() {
                    for (bone_idx, keyframes) in action.keyframes_mut() {
                        for bone_keyframe in keyframes.iter_mut() {
                            let bone = bone_keyframe.bone();
                            bone_keyframe.set_bone(dual_quat_z_up_right_to_y_up_right(bone));
                        }
                    }
                }
            }
            _ => unimplemented!(),
        }

        self.coordinate_system = system;
    }
}

fn dual_quat_z_up_right_to_y_up_right(bone: Bone) -> Bone {
    match bone {
        Bone::Matrix(_) => unimplemented!(),
        Bone::DualQuat(mut dq) => {
            let rot_y = dq.real.j;
            let rot_z = dq.real.k;

            dq.real.j = rot_z;
            dq.real.k = -rot_y;

            let trans_y = dq.dual.j;
            let trans_z = dq.dual.k;

            dq.dual.j = trans_z;
            dq.dual.k = -trans_y;

            Bone::DualQuat(dq)
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::interpolate::tests::dq_to_bone;
    use crate::test_util::{action_name, action_with_keyframes, BONE_IDX};
    use crate::{Action, BlenderArmature, BoneKeyframe, Keyframe};
    use std::collections::HashMap;

    /// Convert from the default Z-up right handed coordinate system to a Y-up right handed
    /// coordinate system.
    #[test]
    fn convert_dual_quaternions_z_up_right_to_y_up_right() {
        let mut arm = BlenderArmature::default();

        let expected_bone = dq_to_bone([0., 1., 3., -2., 4., 5., 7., -6.]);

        let bone = dq_to_bone([0., 1., 2., 3., 4., 5., 6., 7.]);
        arm.inverse_bind_poses = vec![bone.clone()];

        let keyframes = vec![BoneKeyframe::new(0, bone)];

        arm.bone_space_actions = action_with_keyframes(keyframes);

        arm.change_coordinate_system(CoordinateSystem::new(Axis::Y, Hand::Right));

        assert_eq!(&arm.inverse_bind_poses[0], &expected_bone);
        assert_eq!(
            &arm.bone_space_actions[&action_name()].bone_keyframes()[&BONE_IDX][0].bone(),
            &expected_bone
        );
    }

    /// If the armature is already using the coordinate system that we want to change to
    /// then nothing should change
    #[test]
    fn does_not_change_if_coordinate_system_same() {
        let mut arm = BlenderArmature::default();
        arm.change_coordinate_system(CoordinateSystem::new(Axis::Y, Hand::Right));

        let expected_bone = dq_to_bone([0., 1., 2., 3., 4., 5., 6., 7.]);

        let bone = dq_to_bone([0., 1., 2., 3., 4., 5., 6., 7.]);
        arm.inverse_bind_poses = vec![bone];

        let keyframes = vec![BoneKeyframe::new(0, bone)];

        arm.bone_space_actions = action_with_keyframes(keyframes);

        arm.change_coordinate_system(CoordinateSystem::new(Axis::Y, Hand::Right));

        assert_eq!(&arm.inverse_bind_poses[0], &expected_bone);
        assert_eq!(
            &arm.bone_space_actions[&action_name()].bone_keyframes()[&BONE_IDX][0].bone(),
            &expected_bone
        );
    }
}