Skip to main content

oxihuman_morph/
cheek_depth_control.rs

1// Copyright (C) 2026 COOLJAPAN OU (Team KitaSan)
2// SPDX-License-Identifier: Apache-2.0
3
4#![allow(dead_code)]
5
6//! Cheek depth morph control: adjusts how deep or hollow cheeks appear.
7
8/// Configuration for cheek depth morphing.
9#[allow(dead_code)]
10#[derive(Debug, Clone)]
11pub struct CheekDepthConfig {
12    pub min_depth: f32,
13    pub max_depth: f32,
14}
15
16/// Runtime state for cheek depth morph.
17#[allow(dead_code)]
18#[derive(Debug, Clone)]
19pub struct CheekDepthState {
20    pub left_depth: f32,
21    pub right_depth: f32,
22    pub hollow_factor: f32,
23}
24
25#[allow(dead_code)]
26pub fn default_cheek_depth_config() -> CheekDepthConfig {
27    CheekDepthConfig {
28        min_depth: -1.0,
29        max_depth: 1.0,
30    }
31}
32
33#[allow(dead_code)]
34pub fn new_cheek_depth_state() -> CheekDepthState {
35    CheekDepthState {
36        left_depth: 0.0,
37        right_depth: 0.0,
38        hollow_factor: 0.0,
39    }
40}
41
42#[allow(dead_code)]
43pub fn cd_set_left(state: &mut CheekDepthState, cfg: &CheekDepthConfig, v: f32) {
44    state.left_depth = v.clamp(cfg.min_depth, cfg.max_depth);
45}
46
47#[allow(dead_code)]
48pub fn cd_set_right(state: &mut CheekDepthState, cfg: &CheekDepthConfig, v: f32) {
49    state.right_depth = v.clamp(cfg.min_depth, cfg.max_depth);
50}
51
52#[allow(dead_code)]
53pub fn cd_set_both(state: &mut CheekDepthState, cfg: &CheekDepthConfig, v: f32) {
54    let clamped = v.clamp(cfg.min_depth, cfg.max_depth);
55    state.left_depth = clamped;
56    state.right_depth = clamped;
57}
58
59#[allow(dead_code)]
60pub fn cd_set_hollow(state: &mut CheekDepthState, v: f32) {
61    state.hollow_factor = v.clamp(0.0, 1.0);
62}
63
64#[allow(dead_code)]
65pub fn cd_reset(state: &mut CheekDepthState) {
66    *state = new_cheek_depth_state();
67}
68
69#[allow(dead_code)]
70pub fn cd_to_weights(state: &CheekDepthState) -> Vec<(String, f32)> {
71    vec![
72        ("cheek_depth_left".to_string(), state.left_depth),
73        ("cheek_depth_right".to_string(), state.right_depth),
74        ("cheek_depth_hollow".to_string(), state.hollow_factor),
75    ]
76}
77
78#[allow(dead_code)]
79pub fn cd_to_json(state: &CheekDepthState) -> String {
80    format!(
81        r#"{{"left_depth":{:.4},"right_depth":{:.4},"hollow_factor":{:.4}}}"#,
82        state.left_depth, state.right_depth, state.hollow_factor
83    )
84}
85
86#[allow(dead_code)]
87pub fn cd_blend(a: &CheekDepthState, b: &CheekDepthState, t: f32) -> CheekDepthState {
88    let t = t.clamp(0.0, 1.0);
89    CheekDepthState {
90        left_depth: a.left_depth + (b.left_depth - a.left_depth) * t,
91        right_depth: a.right_depth + (b.right_depth - a.right_depth) * t,
92        hollow_factor: a.hollow_factor + (b.hollow_factor - a.hollow_factor) * t,
93    }
94}
95
96#[allow(dead_code)]
97pub fn cd_effective_depth(state: &CheekDepthState) -> f32 {
98    (state.left_depth + state.right_depth) * 0.5 - state.hollow_factor * 0.3
99}
100
101#[cfg(test)]
102mod tests {
103    use super::*;
104
105    #[test]
106    fn test_default_config() {
107        let cfg = default_cheek_depth_config();
108        assert!((cfg.min_depth + 1.0).abs() < 1e-6);
109        assert!((cfg.max_depth - 1.0).abs() < 1e-6);
110    }
111
112    #[test]
113    fn test_new_state() {
114        let s = new_cheek_depth_state();
115        assert!(s.left_depth.abs() < 1e-6);
116        assert!(s.hollow_factor.abs() < 1e-6);
117    }
118
119    #[test]
120    fn test_set_left_clamps() {
121        let cfg = default_cheek_depth_config();
122        let mut s = new_cheek_depth_state();
123        cd_set_left(&mut s, &cfg, 5.0);
124        assert!((s.left_depth - 1.0).abs() < 1e-6);
125    }
126
127    #[test]
128    fn test_set_both() {
129        let cfg = default_cheek_depth_config();
130        let mut s = new_cheek_depth_state();
131        cd_set_both(&mut s, &cfg, 0.5);
132        assert!((s.left_depth - 0.5).abs() < 1e-6);
133        assert!((s.right_depth - 0.5).abs() < 1e-6);
134    }
135
136    #[test]
137    fn test_set_hollow() {
138        let mut s = new_cheek_depth_state();
139        cd_set_hollow(&mut s, 0.7);
140        assert!((s.hollow_factor - 0.7).abs() < 1e-6);
141    }
142
143    #[test]
144    fn test_reset() {
145        let cfg = default_cheek_depth_config();
146        let mut s = new_cheek_depth_state();
147        cd_set_left(&mut s, &cfg, 0.5);
148        cd_reset(&mut s);
149        assert!(s.left_depth.abs() < 1e-6);
150    }
151
152    #[test]
153    fn test_to_weights_count() {
154        let s = new_cheek_depth_state();
155        assert_eq!(cd_to_weights(&s).len(), 3);
156    }
157
158    #[test]
159    fn test_to_json() {
160        let s = new_cheek_depth_state();
161        let j = cd_to_json(&s);
162        assert!(j.contains("left_depth"));
163    }
164
165    #[test]
166    fn test_blend() {
167        let a = new_cheek_depth_state();
168        let mut b = new_cheek_depth_state();
169        b.left_depth = 1.0;
170        let mid = cd_blend(&a, &b, 0.5);
171        assert!((mid.left_depth - 0.5).abs() < 1e-6);
172    }
173
174    #[test]
175    fn test_effective_depth() {
176        let s = new_cheek_depth_state();
177        let d = cd_effective_depth(&s);
178        assert!(d.abs() < 1e-6);
179    }
180}