Skip to main content

oxihuman_morph/
cheek_puff_depth.rs

1// Copyright (C) 2026 COOLJAPAN OU (Team KitaSan)
2// SPDX-License-Identifier: Apache-2.0
3#![allow(dead_code)]
4
5//! Cheek puff depth morph — controls how deeply cheeks puff outward.
6
7/// Configuration for cheek puff depth.
8#[allow(dead_code)]
9#[derive(Debug, Clone)]
10pub struct CheekPuffDepthConfig {
11    pub max_depth: f32,
12}
13
14/// Runtime state.
15#[allow(dead_code)]
16#[derive(Debug, Clone)]
17pub struct CheekPuffDepthState {
18    pub left_depth: f32,
19    pub right_depth: f32,
20    pub upper_bias: f32,
21    pub lower_bias: f32,
22}
23
24#[allow(dead_code)]
25pub fn default_cheek_puff_depth_config() -> CheekPuffDepthConfig {
26    CheekPuffDepthConfig { max_depth: 1.0 }
27}
28
29#[allow(dead_code)]
30pub fn new_cheek_puff_depth_state() -> CheekPuffDepthState {
31    CheekPuffDepthState {
32        left_depth: 0.0,
33        right_depth: 0.0,
34        upper_bias: 0.0,
35        lower_bias: 0.0,
36    }
37}
38
39#[allow(dead_code)]
40pub fn cpd_set_left(state: &mut CheekPuffDepthState, cfg: &CheekPuffDepthConfig, v: f32) {
41    state.left_depth = v.clamp(0.0, cfg.max_depth);
42}
43
44#[allow(dead_code)]
45pub fn cpd_set_right(state: &mut CheekPuffDepthState, cfg: &CheekPuffDepthConfig, v: f32) {
46    state.right_depth = v.clamp(0.0, cfg.max_depth);
47}
48
49#[allow(dead_code)]
50pub fn cpd_set_both(state: &mut CheekPuffDepthState, cfg: &CheekPuffDepthConfig, v: f32) {
51    let clamped = v.clamp(0.0, cfg.max_depth);
52    state.left_depth = clamped;
53    state.right_depth = clamped;
54}
55
56#[allow(dead_code)]
57pub fn cpd_set_bias(state: &mut CheekPuffDepthState, upper: f32, lower: f32) {
58    state.upper_bias = upper.clamp(0.0, 1.0);
59    state.lower_bias = lower.clamp(0.0, 1.0);
60}
61
62#[allow(dead_code)]
63pub fn cpd_reset(state: &mut CheekPuffDepthState) {
64    *state = new_cheek_puff_depth_state();
65}
66
67#[allow(dead_code)]
68pub fn cpd_is_neutral(state: &CheekPuffDepthState) -> bool {
69    let vals = [
70        state.left_depth,
71        state.right_depth,
72        state.upper_bias,
73        state.lower_bias,
74    ];
75    !vals.is_empty() && vals.iter().all(|v| v.abs() < 1e-6)
76}
77
78#[allow(dead_code)]
79pub fn cpd_average_depth(state: &CheekPuffDepthState) -> f32 {
80    (state.left_depth + state.right_depth) * 0.5
81}
82
83#[allow(dead_code)]
84pub fn cpd_symmetry(state: &CheekPuffDepthState) -> f32 {
85    (state.left_depth - state.right_depth).abs()
86}
87
88#[allow(dead_code)]
89pub fn cpd_blend(a: &CheekPuffDepthState, b: &CheekPuffDepthState, t: f32) -> CheekPuffDepthState {
90    let t = t.clamp(0.0, 1.0);
91    CheekPuffDepthState {
92        left_depth: a.left_depth + (b.left_depth - a.left_depth) * t,
93        right_depth: a.right_depth + (b.right_depth - a.right_depth) * t,
94        upper_bias: a.upper_bias + (b.upper_bias - a.upper_bias) * t,
95        lower_bias: a.lower_bias + (b.lower_bias - a.lower_bias) * t,
96    }
97}
98
99#[allow(dead_code)]
100pub fn cpd_to_weights(state: &CheekPuffDepthState) -> Vec<(String, f32)> {
101    vec![
102        ("cheek_puff_depth_l".to_string(), state.left_depth),
103        ("cheek_puff_depth_r".to_string(), state.right_depth),
104        ("cheek_puff_upper".to_string(), state.upper_bias),
105        ("cheek_puff_lower".to_string(), state.lower_bias),
106    ]
107}
108
109#[allow(dead_code)]
110pub fn cpd_to_json(state: &CheekPuffDepthState) -> String {
111    format!(
112        r#"{{"left_depth":{:.4},"right_depth":{:.4},"upper_bias":{:.4},"lower_bias":{:.4}}}"#,
113        state.left_depth, state.right_depth, state.upper_bias, state.lower_bias
114    )
115}
116
117#[cfg(test)]
118mod tests {
119    use super::*;
120
121    #[test]
122    fn default_config() {
123        let cfg = default_cheek_puff_depth_config();
124        assert!((cfg.max_depth - 1.0).abs() < 1e-6);
125    }
126
127    #[test]
128    fn new_state_neutral() {
129        let s = new_cheek_puff_depth_state();
130        assert!(cpd_is_neutral(&s));
131    }
132
133    #[test]
134    fn set_left_clamps() {
135        let cfg = default_cheek_puff_depth_config();
136        let mut s = new_cheek_puff_depth_state();
137        cpd_set_left(&mut s, &cfg, 3.0);
138        assert!((s.left_depth - 1.0).abs() < 1e-6);
139    }
140
141    #[test]
142    fn set_both_equal() {
143        let cfg = default_cheek_puff_depth_config();
144        let mut s = new_cheek_puff_depth_state();
145        cpd_set_both(&mut s, &cfg, 0.6);
146        assert!((s.left_depth - 0.6).abs() < 1e-6);
147        assert!((s.right_depth - 0.6).abs() < 1e-6);
148    }
149
150    #[test]
151    fn set_bias_clamped() {
152        let mut s = new_cheek_puff_depth_state();
153        cpd_set_bias(&mut s, 2.0, -1.0);
154        assert!((s.upper_bias - 1.0).abs() < 1e-6);
155        assert_eq!(s.lower_bias, 0.0);
156    }
157
158    #[test]
159    fn reset_clears() {
160        let cfg = default_cheek_puff_depth_config();
161        let mut s = new_cheek_puff_depth_state();
162        cpd_set_left(&mut s, &cfg, 0.5);
163        cpd_reset(&mut s);
164        assert!(cpd_is_neutral(&s));
165    }
166
167    #[test]
168    fn average_depth() {
169        let cfg = default_cheek_puff_depth_config();
170        let mut s = new_cheek_puff_depth_state();
171        cpd_set_left(&mut s, &cfg, 0.4);
172        cpd_set_right(&mut s, &cfg, 0.6);
173        assert!((cpd_average_depth(&s) - 0.5).abs() < 1e-6);
174    }
175
176    #[test]
177    fn symmetry_difference() {
178        let cfg = default_cheek_puff_depth_config();
179        let mut s = new_cheek_puff_depth_state();
180        cpd_set_left(&mut s, &cfg, 0.3);
181        cpd_set_right(&mut s, &cfg, 0.7);
182        assert!((cpd_symmetry(&s) - 0.4).abs() < 1e-6);
183    }
184
185    #[test]
186    fn blend_midpoint() {
187        let a = new_cheek_puff_depth_state();
188        let cfg = default_cheek_puff_depth_config();
189        let mut b = new_cheek_puff_depth_state();
190        cpd_set_both(&mut b, &cfg, 1.0);
191        let mid = cpd_blend(&a, &b, 0.5);
192        assert!((mid.left_depth - 0.5).abs() < 1e-6);
193    }
194
195    #[test]
196    fn to_weights_count() {
197        let s = new_cheek_puff_depth_state();
198        assert_eq!(cpd_to_weights(&s).len(), 4);
199    }
200
201    #[test]
202    fn to_json_fields() {
203        let s = new_cheek_puff_depth_state();
204        let j = cpd_to_json(&s);
205        assert!(j.contains("left_depth"));
206        assert!(j.contains("lower_bias"));
207    }
208}