Skip to main content

oxihuman_morph/
forehead_width_control.rs

1// Copyright (C) 2026 COOLJAPAN OU (Team KitaSan)
2// SPDX-License-Identifier: Apache-2.0
3
4#![allow(dead_code)]
5
6//! Forehead width morph control: adjusts lateral span of the forehead.
7
8/// Configuration for forehead width morphing.
9#[allow(dead_code)]
10#[derive(Debug, Clone)]
11pub struct ForeheadWidthConfig {
12    pub min_width: f32,
13    pub max_width: f32,
14}
15
16/// Runtime state for forehead width morph.
17#[allow(dead_code)]
18#[derive(Debug, Clone)]
19pub struct ForeheadWidthState {
20    pub width: f32,
21    pub temple_depth: f32,
22    pub bossing: f32,
23}
24
25#[allow(dead_code)]
26pub fn default_forehead_width_config() -> ForeheadWidthConfig {
27    ForeheadWidthConfig {
28        min_width: 0.0,
29        max_width: 1.0,
30    }
31}
32
33#[allow(dead_code)]
34pub fn new_forehead_width_state() -> ForeheadWidthState {
35    ForeheadWidthState {
36        width: 0.5,
37        temple_depth: 0.3,
38        bossing: 0.0,
39    }
40}
41
42#[allow(dead_code)]
43pub fn fw_set_width(state: &mut ForeheadWidthState, cfg: &ForeheadWidthConfig, v: f32) {
44    state.width = v.clamp(cfg.min_width, cfg.max_width);
45}
46
47#[allow(dead_code)]
48pub fn fw_set_temple(state: &mut ForeheadWidthState, v: f32) {
49    state.temple_depth = v.clamp(0.0, 1.0);
50}
51
52#[allow(dead_code)]
53pub fn fw_set_bossing(state: &mut ForeheadWidthState, v: f32) {
54    state.bossing = v.clamp(0.0, 1.0);
55}
56
57#[allow(dead_code)]
58pub fn fw_reset(state: &mut ForeheadWidthState) {
59    *state = new_forehead_width_state();
60}
61
62#[allow(dead_code)]
63pub fn fw_to_weights(state: &ForeheadWidthState) -> Vec<(String, f32)> {
64    vec![
65        ("forehead_width".to_string(), state.width),
66        ("forehead_temple_depth".to_string(), state.temple_depth),
67        ("forehead_bossing".to_string(), state.bossing),
68    ]
69}
70
71#[allow(dead_code)]
72pub fn fw_to_json(state: &ForeheadWidthState) -> String {
73    format!(
74        r#"{{"width":{:.4},"temple_depth":{:.4},"bossing":{:.4}}}"#,
75        state.width, state.temple_depth, state.bossing
76    )
77}
78
79#[allow(dead_code)]
80pub fn fw_blend(a: &ForeheadWidthState, b: &ForeheadWidthState, t: f32) -> ForeheadWidthState {
81    let t = t.clamp(0.0, 1.0);
82    ForeheadWidthState {
83        width: a.width + (b.width - a.width) * t,
84        temple_depth: a.temple_depth + (b.temple_depth - a.temple_depth) * t,
85        bossing: a.bossing + (b.bossing - a.bossing) * t,
86    }
87}
88
89#[allow(dead_code)]
90pub fn fw_effective_width(state: &ForeheadWidthState) -> f32 {
91    state.width * (1.0 + state.bossing * 0.1)
92}
93
94#[cfg(test)]
95mod tests {
96    use super::*;
97
98    #[test]
99    fn test_default_config() {
100        let cfg = default_forehead_width_config();
101        assert!(cfg.min_width.abs() < 1e-6);
102        assert!((cfg.max_width - 1.0).abs() < 1e-6);
103    }
104
105    #[test]
106    fn test_new_state() {
107        let s = new_forehead_width_state();
108        assert!((s.width - 0.5).abs() < 1e-6);
109        assert!(s.bossing.abs() < 1e-6);
110    }
111
112    #[test]
113    fn test_set_width_clamps() {
114        let cfg = default_forehead_width_config();
115        let mut s = new_forehead_width_state();
116        fw_set_width(&mut s, &cfg, 5.0);
117        assert!((s.width - 1.0).abs() < 1e-6);
118    }
119
120    #[test]
121    fn test_set_temple() {
122        let mut s = new_forehead_width_state();
123        fw_set_temple(&mut s, 0.8);
124        assert!((s.temple_depth - 0.8).abs() < 1e-6);
125    }
126
127    #[test]
128    fn test_set_bossing() {
129        let mut s = new_forehead_width_state();
130        fw_set_bossing(&mut s, 0.5);
131        assert!((s.bossing - 0.5).abs() < 1e-6);
132    }
133
134    #[test]
135    fn test_reset() {
136        let cfg = default_forehead_width_config();
137        let mut s = new_forehead_width_state();
138        fw_set_width(&mut s, &cfg, 0.9);
139        fw_reset(&mut s);
140        assert!((s.width - 0.5).abs() < 1e-6);
141    }
142
143    #[test]
144    fn test_to_weights() {
145        let s = new_forehead_width_state();
146        assert_eq!(fw_to_weights(&s).len(), 3);
147    }
148
149    #[test]
150    fn test_to_json() {
151        let s = new_forehead_width_state();
152        let j = fw_to_json(&s);
153        assert!(j.contains("width"));
154    }
155
156    #[test]
157    fn test_blend() {
158        let a = new_forehead_width_state();
159        let mut b = new_forehead_width_state();
160        b.width = 1.0;
161        let mid = fw_blend(&a, &b, 0.5);
162        assert!((mid.width - 0.75).abs() < 1e-6);
163    }
164
165    #[test]
166    fn test_effective_width() {
167        let s = new_forehead_width_state();
168        let w = fw_effective_width(&s);
169        assert!(w > 0.0);
170    }
171}