Skip to main content

oxihuman_morph/
intercanthal_control.rs

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