Skip to main content

oxihuman_morph/
shoulder_height_morph.rs

1// Copyright (C) 2026 COOLJAPAN OU (Team KitaSan)
2// SPDX-License-Identifier: Apache-2.0
3#![allow(dead_code)]
4
5//! Shoulder height asymmetry morph.
6
7/// Shoulder height asymmetry configuration.
8#[derive(Debug, Clone)]
9pub struct ShoulderHeightMorphConfig {
10    pub left_raise: f32,
11    pub right_raise: f32,
12    pub clavicle_tilt: f32,
13}
14
15impl Default for ShoulderHeightMorphConfig {
16    fn default() -> Self {
17        Self {
18            left_raise: 0.0,
19            right_raise: 0.0,
20            clavicle_tilt: 0.0,
21        }
22    }
23}
24
25/// Shoulder height morph state.
26#[derive(Debug, Clone)]
27pub struct ShoulderHeightMorph {
28    pub config: ShoulderHeightMorphConfig,
29    pub intensity: f32,
30    pub enabled: bool,
31}
32
33impl ShoulderHeightMorph {
34    pub fn new() -> Self {
35        Self {
36            config: ShoulderHeightMorphConfig::default(),
37            intensity: 0.0,
38            enabled: true,
39        }
40    }
41}
42
43impl Default for ShoulderHeightMorph {
44    fn default() -> Self {
45        Self::new()
46    }
47}
48
49/// Create a new ShoulderHeightMorph.
50pub fn new_shoulder_height_morph() -> ShoulderHeightMorph {
51    ShoulderHeightMorph::new()
52}
53
54/// Set left shoulder raise amount (-1.0 to 1.0).
55pub fn shoulder_height_set_left(morph: &mut ShoulderHeightMorph, v: f32) {
56    morph.config.left_raise = v.clamp(-1.0, 1.0);
57}
58
59/// Set right shoulder raise amount.
60pub fn shoulder_height_set_right(morph: &mut ShoulderHeightMorph, v: f32) {
61    morph.config.right_raise = v.clamp(-1.0, 1.0);
62}
63
64/// Set overall clavicle tilt.
65pub fn shoulder_height_set_tilt(morph: &mut ShoulderHeightMorph, v: f32) {
66    morph.config.clavicle_tilt = v.clamp(-1.0, 1.0);
67}
68
69/// Compute shoulder asymmetry magnitude.
70pub fn shoulder_height_asymmetry(morph: &ShoulderHeightMorph) -> f32 {
71    (morph.config.left_raise - morph.config.right_raise).abs() * morph.intensity
72}
73
74/// Serialize to JSON.
75pub fn shoulder_height_to_json(morph: &ShoulderHeightMorph) -> String {
76    format!(
77        r#"{{"intensity":{},"left_raise":{},"right_raise":{},"clavicle_tilt":{}}}"#,
78        morph.intensity,
79        morph.config.left_raise,
80        morph.config.right_raise,
81        morph.config.clavicle_tilt,
82    )
83}
84
85#[cfg(test)]
86mod tests {
87    use super::*;
88
89    #[test]
90    fn test_new() {
91        let m = new_shoulder_height_morph();
92        assert!((m.config.left_raise - 0.0).abs() < 1e-6 /* default zero */);
93    }
94
95    #[test]
96    fn test_left_clamp() {
97        let mut m = new_shoulder_height_morph();
98        shoulder_height_set_left(&mut m, 5.0);
99        assert!((m.config.left_raise - 1.0).abs() < 1e-6 /* clamped */);
100    }
101
102    #[test]
103    fn test_right_negative() {
104        let mut m = new_shoulder_height_morph();
105        shoulder_height_set_right(&mut m, -0.5);
106        assert!((m.config.right_raise - (-0.5)).abs() < 1e-6 /* negative ok */);
107    }
108
109    #[test]
110    fn test_tilt() {
111        let mut m = new_shoulder_height_morph();
112        shoulder_height_set_tilt(&mut m, 0.3);
113        assert!((m.config.clavicle_tilt - 0.3).abs() < 1e-6 /* stored */);
114    }
115
116    #[test]
117    fn test_asymmetry_zero() {
118        let m = new_shoulder_height_morph();
119        assert!((shoulder_height_asymmetry(&m) - 0.0).abs() < 1e-6 /* zero */);
120    }
121
122    #[test]
123    fn test_asymmetry_nonzero() {
124        let mut m = new_shoulder_height_morph();
125        shoulder_height_set_left(&mut m, 0.5);
126        shoulder_height_set_right(&mut m, -0.5);
127        m.intensity = 1.0;
128        assert!((shoulder_height_asymmetry(&m) - 1.0).abs() < 1e-6 /* 1.0 diff */);
129    }
130
131    #[test]
132    fn test_json_key() {
133        let m = new_shoulder_height_morph();
134        let j = shoulder_height_to_json(&m);
135        assert!(j.contains("left_raise") /* key */);
136    }
137
138    #[test]
139    fn test_default_enabled() {
140        let m = ShoulderHeightMorph::default();
141        assert!(m.enabled /* enabled */);
142    }
143
144    #[test]
145    fn test_clone() {
146        let m = new_shoulder_height_morph();
147        let c = m.clone();
148        assert!((c.intensity - m.intensity).abs() < 1e-6 /* equal */);
149    }
150}