oxihuman_morph/
forehead_protrusion_control.rs1#![allow(dead_code)]
5
6use std::f32::consts::FRAC_PI_3;
9
10#[allow(dead_code)]
11#[derive(Debug, Clone)]
12pub struct ForeheadProtrusionConfig {
13 pub min_protrusion: f32,
14 pub max_protrusion: f32,
15}
16
17#[allow(dead_code)]
18#[derive(Debug, Clone)]
19pub struct ForeheadProtrusionState {
20 pub protrusion: f32,
21 pub slope: f32,
22 pub bossing: f32,
23}
24
25#[allow(dead_code)]
26pub fn default_forehead_protrusion_config() -> ForeheadProtrusionConfig {
27 ForeheadProtrusionConfig {
28 min_protrusion: 0.0,
29 max_protrusion: 1.0,
30 }
31}
32
33#[allow(dead_code)]
34pub fn new_forehead_protrusion_state() -> ForeheadProtrusionState {
35 ForeheadProtrusionState {
36 protrusion: 0.5,
37 slope: 0.5,
38 bossing: 0.3,
39 }
40}
41
42#[allow(dead_code)]
43pub fn fp_set_protrusion(
44 state: &mut ForeheadProtrusionState,
45 cfg: &ForeheadProtrusionConfig,
46 v: f32,
47) {
48 state.protrusion = v.clamp(cfg.min_protrusion, cfg.max_protrusion);
49}
50
51#[allow(dead_code)]
52pub fn fp_set_slope(state: &mut ForeheadProtrusionState, v: f32) {
53 state.slope = v.clamp(0.0, 1.0);
54}
55
56#[allow(dead_code)]
57pub fn fp_set_bossing(state: &mut ForeheadProtrusionState, v: f32) {
58 state.bossing = v.clamp(0.0, 1.0);
59}
60
61#[allow(dead_code)]
62pub fn fp_reset(state: &mut ForeheadProtrusionState) {
63 *state = new_forehead_protrusion_state();
64}
65
66#[allow(dead_code)]
67pub fn fp_slope_angle(state: &ForeheadProtrusionState) -> f32 {
68 state.slope * FRAC_PI_3
69}
70
71#[allow(dead_code)]
72pub fn fp_to_weights(state: &ForeheadProtrusionState) -> Vec<(String, f32)> {
73 vec![
74 ("forehead_protrusion".to_string(), state.protrusion),
75 ("forehead_slope".to_string(), state.slope),
76 ("forehead_bossing".to_string(), state.bossing),
77 ]
78}
79
80#[allow(dead_code)]
81pub fn fp_to_json(state: &ForeheadProtrusionState) -> String {
82 format!(
83 r#"{{"protrusion":{:.4},"slope":{:.4},"bossing":{:.4}}}"#,
84 state.protrusion, state.slope, state.bossing
85 )
86}
87
88#[allow(dead_code)]
89pub fn fp_blend(
90 a: &ForeheadProtrusionState,
91 b: &ForeheadProtrusionState,
92 t: f32,
93) -> ForeheadProtrusionState {
94 let t = t.clamp(0.0, 1.0);
95 ForeheadProtrusionState {
96 protrusion: a.protrusion + (b.protrusion - a.protrusion) * t,
97 slope: a.slope + (b.slope - a.slope) * t,
98 bossing: a.bossing + (b.bossing - a.bossing) * t,
99 }
100}
101
102#[cfg(test)]
103mod tests {
104 use super::*;
105
106 #[test]
107 fn test_default_config() {
108 let cfg = default_forehead_protrusion_config();
109 assert!(cfg.min_protrusion.abs() < 1e-6);
110 }
111
112 #[test]
113 fn test_new_state() {
114 let s = new_forehead_protrusion_state();
115 assert!((s.protrusion - 0.5).abs() < 1e-6);
116 }
117
118 #[test]
119 fn test_set_protrusion_clamps() {
120 let cfg = default_forehead_protrusion_config();
121 let mut s = new_forehead_protrusion_state();
122 fp_set_protrusion(&mut s, &cfg, 5.0);
123 assert!((s.protrusion - 1.0).abs() < 1e-6);
124 }
125
126 #[test]
127 fn test_set_slope() {
128 let mut s = new_forehead_protrusion_state();
129 fp_set_slope(&mut s, 0.8);
130 assert!((s.slope - 0.8).abs() < 1e-6);
131 }
132
133 #[test]
134 fn test_set_bossing() {
135 let mut s = new_forehead_protrusion_state();
136 fp_set_bossing(&mut s, 0.7);
137 assert!((s.bossing - 0.7).abs() < 1e-6);
138 }
139
140 #[test]
141 fn test_reset() {
142 let cfg = default_forehead_protrusion_config();
143 let mut s = new_forehead_protrusion_state();
144 fp_set_protrusion(&mut s, &cfg, 0.9);
145 fp_reset(&mut s);
146 assert!((s.protrusion - 0.5).abs() < 1e-6);
147 }
148
149 #[test]
150 fn test_slope_angle() {
151 let mut s = new_forehead_protrusion_state();
152 s.slope = 1.0;
153 assert!((fp_slope_angle(&s) - FRAC_PI_3).abs() < 1e-6);
154 }
155
156 #[test]
157 fn test_to_weights() {
158 let s = new_forehead_protrusion_state();
159 assert_eq!(fp_to_weights(&s).len(), 3);
160 }
161
162 #[test]
163 fn test_blend() {
164 let a = new_forehead_protrusion_state();
165 let mut b = new_forehead_protrusion_state();
166 b.protrusion = 1.0;
167 let mid = fp_blend(&a, &b, 0.5);
168 assert!((mid.protrusion - 0.75).abs() < 1e-6);
169 }
170
171 #[test]
172 fn test_to_json() {
173 let s = new_forehead_protrusion_state();
174 let j = fp_to_json(&s);
175 assert!(j.contains("protrusion"));
176 }
177}