Skip to main content

oxihuman_morph/
forehead_tension_control.rs

1// Copyright (C) 2026 COOLJAPAN OU (Team KitaSan)
2// SPDX-License-Identifier: Apache-2.0
3#![allow(dead_code)]
4
5//! Forehead tension control — frontalis muscle tension and skin compression.
6
7/// Configuration for forehead tension.
8#[allow(dead_code)]
9#[derive(Debug, Clone)]
10pub struct ForeheadTensionConfig {
11    pub max_tension: f32,
12}
13
14/// Runtime state.
15#[allow(dead_code)]
16#[derive(Debug, Clone)]
17pub struct ForeheadTensionState {
18    pub central_tension: f32,
19    pub left_tension: f32,
20    pub right_tension: f32,
21    pub skin_compression: f32,
22}
23
24#[allow(dead_code)]
25pub fn default_forehead_tension_config() -> ForeheadTensionConfig {
26    ForeheadTensionConfig { max_tension: 1.0 }
27}
28
29#[allow(dead_code)]
30pub fn new_forehead_tension_state() -> ForeheadTensionState {
31    ForeheadTensionState {
32        central_tension: 0.0,
33        left_tension: 0.0,
34        right_tension: 0.0,
35        skin_compression: 0.0,
36    }
37}
38
39#[allow(dead_code)]
40pub fn ften_set_central(state: &mut ForeheadTensionState, cfg: &ForeheadTensionConfig, v: f32) {
41    state.central_tension = v.clamp(0.0, cfg.max_tension);
42}
43
44#[allow(dead_code)]
45pub fn ften_set_lateral(
46    state: &mut ForeheadTensionState,
47    cfg: &ForeheadTensionConfig,
48    left: f32,
49    right: f32,
50) {
51    state.left_tension = left.clamp(0.0, cfg.max_tension);
52    state.right_tension = right.clamp(0.0, cfg.max_tension);
53}
54
55#[allow(dead_code)]
56pub fn ften_set_compression(state: &mut ForeheadTensionState, v: f32) {
57    state.skin_compression = v.clamp(0.0, 1.0);
58}
59
60#[allow(dead_code)]
61pub fn ften_set_all(state: &mut ForeheadTensionState, cfg: &ForeheadTensionConfig, v: f32) {
62    let clamped = v.clamp(0.0, cfg.max_tension);
63    state.central_tension = clamped;
64    state.left_tension = clamped;
65    state.right_tension = clamped;
66}
67
68#[allow(dead_code)]
69pub fn ften_reset(state: &mut ForeheadTensionState) {
70    *state = new_forehead_tension_state();
71}
72
73#[allow(dead_code)]
74pub fn ften_is_neutral(state: &ForeheadTensionState) -> bool {
75    let vals = [
76        state.central_tension,
77        state.left_tension,
78        state.right_tension,
79        state.skin_compression,
80    ];
81    vals.iter().all(|v| v.abs() < 1e-6)
82}
83
84#[allow(dead_code)]
85pub fn ften_average(state: &ForeheadTensionState) -> f32 {
86    (state.central_tension + state.left_tension + state.right_tension) / 3.0
87}
88
89#[allow(dead_code)]
90pub fn ften_blend(
91    a: &ForeheadTensionState,
92    b: &ForeheadTensionState,
93    t: f32,
94) -> ForeheadTensionState {
95    let t = t.clamp(0.0, 1.0);
96    ForeheadTensionState {
97        central_tension: a.central_tension + (b.central_tension - a.central_tension) * t,
98        left_tension: a.left_tension + (b.left_tension - a.left_tension) * t,
99        right_tension: a.right_tension + (b.right_tension - a.right_tension) * t,
100        skin_compression: a.skin_compression + (b.skin_compression - a.skin_compression) * t,
101    }
102}
103
104#[allow(dead_code)]
105pub fn ften_to_weights(state: &ForeheadTensionState) -> Vec<(String, f32)> {
106    vec![
107        (
108            "forehead_tension_central".to_string(),
109            state.central_tension,
110        ),
111        ("forehead_tension_left".to_string(), state.left_tension),
112        ("forehead_tension_right".to_string(), state.right_tension),
113        (
114            "forehead_skin_compression".to_string(),
115            state.skin_compression,
116        ),
117    ]
118}
119
120#[allow(dead_code)]
121pub fn ften_to_json(state: &ForeheadTensionState) -> String {
122    format!(
123        r#"{{"central":{:.4},"left":{:.4},"right":{:.4},"compression":{:.4}}}"#,
124        state.central_tension, state.left_tension, state.right_tension, state.skin_compression
125    )
126}
127
128#[cfg(test)]
129mod tests {
130    use super::*;
131
132    #[test]
133    fn default_config() {
134        let cfg = default_forehead_tension_config();
135        assert!((cfg.max_tension - 1.0).abs() < 1e-6);
136    }
137
138    #[test]
139    fn new_state_neutral() {
140        let s = new_forehead_tension_state();
141        assert!(ften_is_neutral(&s));
142    }
143
144    #[test]
145    fn set_central_clamps() {
146        let cfg = default_forehead_tension_config();
147        let mut s = new_forehead_tension_state();
148        ften_set_central(&mut s, &cfg, 3.0);
149        assert!((s.central_tension - 1.0).abs() < 1e-6);
150    }
151
152    #[test]
153    fn set_lateral() {
154        let cfg = default_forehead_tension_config();
155        let mut s = new_forehead_tension_state();
156        ften_set_lateral(&mut s, &cfg, 0.3, 0.7);
157        assert!((s.left_tension - 0.3).abs() < 1e-6);
158        assert!((s.right_tension - 0.7).abs() < 1e-6);
159    }
160
161    #[test]
162    fn set_all() {
163        let cfg = default_forehead_tension_config();
164        let mut s = new_forehead_tension_state();
165        ften_set_all(&mut s, &cfg, 0.5);
166        assert!((ften_average(&s) - 0.5).abs() < 1e-6);
167    }
168
169    #[test]
170    fn set_compression() {
171        let mut s = new_forehead_tension_state();
172        ften_set_compression(&mut s, 0.6);
173        assert!((s.skin_compression - 0.6).abs() < 1e-6);
174    }
175
176    #[test]
177    fn reset_clears() {
178        let cfg = default_forehead_tension_config();
179        let mut s = new_forehead_tension_state();
180        ften_set_all(&mut s, &cfg, 0.8);
181        ften_reset(&mut s);
182        assert!(ften_is_neutral(&s));
183    }
184
185    #[test]
186    fn blend_midpoint() {
187        let a = new_forehead_tension_state();
188        let cfg = default_forehead_tension_config();
189        let mut b = new_forehead_tension_state();
190        ften_set_all(&mut b, &cfg, 1.0);
191        let m = ften_blend(&a, &b, 0.5);
192        assert!((m.central_tension - 0.5).abs() < 1e-6);
193    }
194
195    #[test]
196    fn to_weights_count() {
197        let s = new_forehead_tension_state();
198        assert_eq!(ften_to_weights(&s).len(), 4);
199    }
200
201    #[test]
202    fn to_json_fields() {
203        let s = new_forehead_tension_state();
204        let j = ften_to_json(&s);
205        assert!(j.contains("central"));
206    }
207}