Skip to main content

oxihuman_morph/
slouch_morph.rs

1// Copyright (C) 2026 COOLJAPAN OU (Team KitaSan)
2// SPDX-License-Identifier: Apache-2.0
3#![allow(dead_code)]
4
5//! Slouch and forward head posture morph.
6
7/// Configuration for slouch morph.
8#[derive(Debug, Clone)]
9pub struct SlouchMorphConfig {
10    pub slouch_degree: f32,
11    pub head_forward: f32,
12    pub shoulder_round: f32,
13}
14
15impl Default for SlouchMorphConfig {
16    fn default() -> Self {
17        Self {
18            slouch_degree: 0.0,
19            head_forward: 0.0,
20            shoulder_round: 0.0,
21        }
22    }
23}
24
25/// Slouch morph state.
26#[derive(Debug, Clone)]
27pub struct SlouchMorph {
28    pub config: SlouchMorphConfig,
29    pub intensity: f32,
30    pub enabled: bool,
31}
32
33impl SlouchMorph {
34    pub fn new() -> Self {
35        Self {
36            config: SlouchMorphConfig::default(),
37            intensity: 0.0,
38            enabled: true,
39        }
40    }
41}
42
43impl Default for SlouchMorph {
44    fn default() -> Self {
45        Self::new()
46    }
47}
48
49/// Create a new SlouchMorph.
50pub fn new_slouch_morph() -> SlouchMorph {
51    SlouchMorph::new()
52}
53
54/// Set slouch degree (0.0–1.0).
55pub fn slouch_set_degree(morph: &mut SlouchMorph, degree: f32) {
56    morph.config.slouch_degree = degree.clamp(0.0, 1.0);
57}
58
59/// Set forward head translation factor.
60pub fn slouch_set_head_forward(morph: &mut SlouchMorph, v: f32) {
61    morph.config.head_forward = v.clamp(0.0, 1.0);
62}
63
64/// Set shoulder rounding factor.
65pub fn slouch_set_shoulder_round(morph: &mut SlouchMorph, v: f32) {
66    morph.config.shoulder_round = v.clamp(0.0, 1.0);
67}
68
69/// Apply morph to a weight buffer.
70#[allow(clippy::needless_range_loop)]
71pub fn slouch_apply(morph: &SlouchMorph, weights: &mut [f32]) {
72    let scale = morph.intensity * morph.config.slouch_degree;
73    for i in 0..weights.len() {
74        weights[i] = (weights[i] + scale).min(1.0);
75    }
76}
77
78/// Serialize to JSON.
79pub fn slouch_to_json(morph: &SlouchMorph) -> String {
80    format!(
81        r#"{{"intensity":{},"slouch_degree":{},"head_forward":{},"shoulder_round":{}}}"#,
82        morph.intensity,
83        morph.config.slouch_degree,
84        morph.config.head_forward,
85        morph.config.shoulder_round,
86    )
87}
88
89#[cfg(test)]
90mod tests {
91    use super::*;
92
93    #[test]
94    fn test_new() {
95        let m = new_slouch_morph();
96        assert!((m.config.slouch_degree - 0.0).abs() < 1e-6 /* defaults zero */);
97    }
98
99    #[test]
100    fn test_degree_clamp_high() {
101        let mut m = new_slouch_morph();
102        slouch_set_degree(&mut m, 3.0);
103        assert!((m.config.slouch_degree - 1.0).abs() < 1e-6 /* clamped to 1 */);
104    }
105
106    #[test]
107    fn test_degree_clamp_low() {
108        let mut m = new_slouch_morph();
109        slouch_set_degree(&mut m, -1.0);
110        assert!((m.config.slouch_degree - 0.0).abs() < 1e-6 /* clamped to 0 */);
111    }
112
113    #[test]
114    fn test_head_forward() {
115        let mut m = new_slouch_morph();
116        slouch_set_head_forward(&mut m, 0.7);
117        assert!((m.config.head_forward - 0.7).abs() < 1e-6 /* stored */);
118    }
119
120    #[test]
121    fn test_shoulder_round() {
122        let mut m = new_slouch_morph();
123        slouch_set_shoulder_round(&mut m, 0.5);
124        assert!((m.config.shoulder_round - 0.5).abs() < 1e-6 /* stored */);
125    }
126
127    #[test]
128    fn test_apply_increases_weights() {
129        let mut m = new_slouch_morph();
130        slouch_set_degree(&mut m, 0.5);
131        m.intensity = 1.0;
132        let mut w = vec![0.0f32, 0.0f32];
133        slouch_apply(&m, &mut w);
134        assert!(w[0] > 0.0 /* increased */);
135    }
136
137    #[test]
138    fn test_apply_clamps_to_one() {
139        let mut m = new_slouch_morph();
140        slouch_set_degree(&mut m, 1.0);
141        m.intensity = 1.0;
142        let mut w = vec![1.0f32];
143        slouch_apply(&m, &mut w);
144        assert!((w[0] - 1.0).abs() < 1e-6 /* capped at 1 */);
145    }
146
147    #[test]
148    fn test_json_contains_key() {
149        let m = new_slouch_morph();
150        let j = slouch_to_json(&m);
151        assert!(j.contains("slouch_degree") /* key present */);
152    }
153
154    #[test]
155    fn test_default() {
156        let m = SlouchMorph::default();
157        assert!(m.enabled /* default enabled */);
158    }
159
160    #[test]
161    fn test_clone() {
162        let m = new_slouch_morph();
163        let c = m.clone();
164        assert!((c.intensity - m.intensity).abs() < 1e-6 /* equal */);
165    }
166}