Skip to main content

oxihuman_morph/
shin_control.rs

1// Copyright (C) 2026 COOLJAPAN OU (Team KitaSan)
2// SPDX-License-Identifier: Apache-2.0
3#![allow(dead_code)]
4
5//! Shin morph — controls shin shape, curvature and calf definition.
6
7/// Configuration for shin control.
8#[allow(dead_code)]
9#[derive(Debug, Clone)]
10pub struct ShinConfig {
11    pub max_girth: f32,
12}
13
14/// Side selector.
15#[allow(dead_code)]
16#[derive(Debug, Clone, Copy, PartialEq, Eq)]
17pub enum ShinSide {
18    Left,
19    Right,
20}
21
22/// Runtime state.
23#[allow(dead_code)]
24#[derive(Debug, Clone)]
25pub struct ShinState {
26    pub left_girth: f32,
27    pub right_girth: f32,
28    pub left_curvature: f32,
29    pub right_curvature: f32,
30}
31
32#[allow(dead_code)]
33pub fn default_shin_config() -> ShinConfig {
34    ShinConfig { max_girth: 1.0 }
35}
36
37#[allow(dead_code)]
38pub fn new_shin_state() -> ShinState {
39    ShinState {
40        left_girth: 0.0,
41        right_girth: 0.0,
42        left_curvature: 0.0,
43        right_curvature: 0.0,
44    }
45}
46
47#[allow(dead_code)]
48pub fn shn_set_girth(state: &mut ShinState, cfg: &ShinConfig, side: ShinSide, v: f32) {
49    let clamped = v.clamp(0.0, cfg.max_girth);
50    match side {
51        ShinSide::Left => state.left_girth = clamped,
52        ShinSide::Right => state.right_girth = clamped,
53    }
54}
55
56#[allow(dead_code)]
57pub fn shn_set_both_girth(state: &mut ShinState, cfg: &ShinConfig, v: f32) {
58    let clamped = v.clamp(0.0, cfg.max_girth);
59    state.left_girth = clamped;
60    state.right_girth = clamped;
61}
62
63#[allow(dead_code)]
64pub fn shn_set_curvature(state: &mut ShinState, side: ShinSide, v: f32) {
65    let clamped = v.clamp(-1.0, 1.0);
66    match side {
67        ShinSide::Left => state.left_curvature = clamped,
68        ShinSide::Right => state.right_curvature = clamped,
69    }
70}
71
72#[allow(dead_code)]
73pub fn shn_reset(state: &mut ShinState) {
74    *state = new_shin_state();
75}
76
77#[allow(dead_code)]
78pub fn shn_is_neutral(state: &ShinState) -> bool {
79    let vals = [
80        state.left_girth,
81        state.right_girth,
82        state.left_curvature,
83        state.right_curvature,
84    ];
85    !vals.is_empty() && vals.iter().all(|v| v.abs() < 1e-6)
86}
87
88#[allow(dead_code)]
89pub fn shn_average_girth(state: &ShinState) -> f32 {
90    (state.left_girth + state.right_girth) * 0.5
91}
92
93#[allow(dead_code)]
94pub fn shn_symmetry(state: &ShinState) -> f32 {
95    (state.left_girth - state.right_girth).abs()
96}
97
98#[allow(dead_code)]
99pub fn shn_blend(a: &ShinState, b: &ShinState, t: f32) -> ShinState {
100    let t = t.clamp(0.0, 1.0);
101    ShinState {
102        left_girth: a.left_girth + (b.left_girth - a.left_girth) * t,
103        right_girth: a.right_girth + (b.right_girth - a.right_girth) * t,
104        left_curvature: a.left_curvature + (b.left_curvature - a.left_curvature) * t,
105        right_curvature: a.right_curvature + (b.right_curvature - a.right_curvature) * t,
106    }
107}
108
109#[allow(dead_code)]
110pub fn shn_to_weights(state: &ShinState) -> Vec<(String, f32)> {
111    vec![
112        ("shin_girth_l".to_string(), state.left_girth),
113        ("shin_girth_r".to_string(), state.right_girth),
114        ("shin_curvature_l".to_string(), state.left_curvature),
115        ("shin_curvature_r".to_string(), state.right_curvature),
116    ]
117}
118
119#[allow(dead_code)]
120pub fn shn_to_json(state: &ShinState) -> String {
121    format!(
122        r#"{{"left_girth":{:.4},"right_girth":{:.4},"left_curvature":{:.4},"right_curvature":{:.4}}}"#,
123        state.left_girth, state.right_girth, state.left_curvature, state.right_curvature
124    )
125}
126
127#[cfg(test)]
128mod tests {
129    use super::*;
130
131    #[test]
132    fn default_config() {
133        let cfg = default_shin_config();
134        assert!((cfg.max_girth - 1.0).abs() < 1e-6);
135    }
136
137    #[test]
138    fn new_state_neutral() {
139        let s = new_shin_state();
140        assert!(shn_is_neutral(&s));
141    }
142
143    #[test]
144    fn set_girth_left() {
145        let cfg = default_shin_config();
146        let mut s = new_shin_state();
147        shn_set_girth(&mut s, &cfg, ShinSide::Left, 0.5);
148        assert!((s.left_girth - 0.5).abs() < 1e-6);
149        assert_eq!(s.right_girth, 0.0);
150    }
151
152    #[test]
153    fn set_girth_clamps() {
154        let cfg = default_shin_config();
155        let mut s = new_shin_state();
156        shn_set_girth(&mut s, &cfg, ShinSide::Right, 10.0);
157        assert!((s.right_girth - 1.0).abs() < 1e-6);
158    }
159
160    #[test]
161    fn set_both_girth() {
162        let cfg = default_shin_config();
163        let mut s = new_shin_state();
164        shn_set_both_girth(&mut s, &cfg, 0.6);
165        assert!(shn_symmetry(&s) < 1e-6);
166    }
167
168    #[test]
169    fn set_curvature_signed() {
170        let mut s = new_shin_state();
171        shn_set_curvature(&mut s, ShinSide::Left, -0.3);
172        assert!((s.left_curvature + 0.3).abs() < 1e-6);
173    }
174
175    #[test]
176    fn average_girth() {
177        let cfg = default_shin_config();
178        let mut s = new_shin_state();
179        shn_set_girth(&mut s, &cfg, ShinSide::Left, 0.4);
180        shn_set_girth(&mut s, &cfg, ShinSide::Right, 0.6);
181        assert!((shn_average_girth(&s) - 0.5).abs() < 1e-6);
182    }
183
184    #[test]
185    fn reset_clears() {
186        let cfg = default_shin_config();
187        let mut s = new_shin_state();
188        shn_set_both_girth(&mut s, &cfg, 0.8);
189        shn_reset(&mut s);
190        assert!(shn_is_neutral(&s));
191    }
192
193    #[test]
194    fn blend_midpoint() {
195        let a = new_shin_state();
196        let cfg = default_shin_config();
197        let mut b = new_shin_state();
198        shn_set_both_girth(&mut b, &cfg, 1.0);
199        let mid = shn_blend(&a, &b, 0.5);
200        assert!((mid.left_girth - 0.5).abs() < 1e-6);
201    }
202
203    #[test]
204    fn to_weights_count() {
205        let s = new_shin_state();
206        assert_eq!(shn_to_weights(&s).len(), 4);
207    }
208
209    #[test]
210    fn to_json_fields() {
211        let s = new_shin_state();
212        let j = shn_to_json(&s);
213        assert!(j.contains("left_girth"));
214        assert!(j.contains("right_curvature"));
215    }
216}