oxihuman_morph/
chin_width_control.rs1#![allow(dead_code)]
5
6#[allow(dead_code)]
10#[derive(Debug, Clone)]
11pub struct ChinWidthConfig {
12 pub min_width: f32,
13 pub max_width: f32,
14}
15
16#[allow(dead_code)]
18#[derive(Debug, Clone)]
19pub struct ChinWidthState {
20 pub width: f32,
21 pub taper: f32,
22 pub cleft_depth: f32,
23}
24
25#[allow(dead_code)]
26pub fn default_chin_width_config() -> ChinWidthConfig {
27 ChinWidthConfig {
28 min_width: 0.0,
29 max_width: 1.0,
30 }
31}
32
33#[allow(dead_code)]
34pub fn new_chin_width_state() -> ChinWidthState {
35 ChinWidthState {
36 width: 0.5,
37 taper: 0.5,
38 cleft_depth: 0.0,
39 }
40}
41
42#[allow(dead_code)]
43pub fn cw_set_width(state: &mut ChinWidthState, cfg: &ChinWidthConfig, v: f32) {
44 state.width = v.clamp(cfg.min_width, cfg.max_width);
45}
46
47#[allow(dead_code)]
48pub fn cw_set_taper(state: &mut ChinWidthState, v: f32) {
49 state.taper = v.clamp(0.0, 1.0);
50}
51
52#[allow(dead_code)]
53pub fn cw_set_cleft(state: &mut ChinWidthState, v: f32) {
54 state.cleft_depth = v.clamp(0.0, 1.0);
55}
56
57#[allow(dead_code)]
58pub fn cw_reset(state: &mut ChinWidthState) {
59 *state = new_chin_width_state();
60}
61
62#[allow(dead_code)]
63pub fn cw_to_weights(state: &ChinWidthState) -> Vec<(String, f32)> {
64 vec![
65 ("chin_width".to_string(), state.width),
66 ("chin_taper".to_string(), state.taper),
67 ("chin_cleft".to_string(), state.cleft_depth),
68 ]
69}
70
71#[allow(dead_code)]
72pub fn cw_to_json(state: &ChinWidthState) -> String {
73 format!(
74 r#"{{"width":{:.4},"taper":{:.4},"cleft_depth":{:.4}}}"#,
75 state.width, state.taper, state.cleft_depth
76 )
77}
78
79#[allow(dead_code)]
80pub fn cw_blend(a: &ChinWidthState, b: &ChinWidthState, t: f32) -> ChinWidthState {
81 let t = t.clamp(0.0, 1.0);
82 ChinWidthState {
83 width: a.width + (b.width - a.width) * t,
84 taper: a.taper + (b.taper - a.taper) * t,
85 cleft_depth: a.cleft_depth + (b.cleft_depth - a.cleft_depth) * t,
86 }
87}
88
89#[allow(dead_code)]
90pub fn cw_effective_width(state: &ChinWidthState) -> f32 {
91 state.width * (1.0 - state.taper * 0.3)
92}
93
94#[cfg(test)]
95mod tests {
96 use super::*;
97
98 #[test]
99 fn test_default_config() {
100 let cfg = default_chin_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_chin_width_state();
108 assert!((s.width - 0.5).abs() < 1e-6);
109 assert!(s.cleft_depth.abs() < 1e-6);
110 }
111
112 #[test]
113 fn test_set_width_clamps() {
114 let cfg = default_chin_width_config();
115 let mut s = new_chin_width_state();
116 cw_set_width(&mut s, &cfg, 5.0);
117 assert!((s.width - 1.0).abs() < 1e-6);
118 }
119
120 #[test]
121 fn test_set_taper() {
122 let mut s = new_chin_width_state();
123 cw_set_taper(&mut s, 0.8);
124 assert!((s.taper - 0.8).abs() < 1e-6);
125 }
126
127 #[test]
128 fn test_set_cleft() {
129 let mut s = new_chin_width_state();
130 cw_set_cleft(&mut s, 0.4);
131 assert!((s.cleft_depth - 0.4).abs() < 1e-6);
132 }
133
134 #[test]
135 fn test_reset() {
136 let cfg = default_chin_width_config();
137 let mut s = new_chin_width_state();
138 cw_set_width(&mut s, &cfg, 0.9);
139 cw_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_chin_width_state();
146 assert_eq!(cw_to_weights(&s).len(), 3);
147 }
148
149 #[test]
150 fn test_to_json() {
151 let s = new_chin_width_state();
152 let j = cw_to_json(&s);
153 assert!(j.contains("width"));
154 assert!(j.contains("taper"));
155 }
156
157 #[test]
158 fn test_blend() {
159 let a = new_chin_width_state();
160 let mut b = new_chin_width_state();
161 b.width = 1.0;
162 let mid = cw_blend(&a, &b, 0.5);
163 assert!((mid.width - 0.75).abs() < 1e-6);
164 }
165
166 #[test]
167 fn test_effective_width() {
168 let s = new_chin_width_state();
169 let w = cw_effective_width(&s);
170 assert!(w > 0.0);
171 }
172}