oxihuman_morph/
temple_control.rs1#![allow(dead_code)]
7
8#[allow(dead_code)]
9#[derive(Debug, Clone)]
10pub struct TempleConfig {
11 pub max_prominence: f32,
12}
13
14#[allow(dead_code)]
15#[derive(Debug, Clone)]
16pub struct TempleState {
17 pub prominence_l: f32,
18 pub prominence_r: f32,
19 pub hollow_l: f32,
20 pub hollow_r: f32,
21}
22
23#[allow(dead_code)]
24pub fn default_temple_config() -> TempleConfig {
25 TempleConfig {
26 max_prominence: 1.0,
27 }
28}
29
30#[allow(dead_code)]
31pub fn new_temple_state() -> TempleState {
32 TempleState {
33 prominence_l: 0.0,
34 prominence_r: 0.0,
35 hollow_l: 0.0,
36 hollow_r: 0.0,
37 }
38}
39
40#[allow(dead_code)]
41pub fn temple_set_prominence(state: &mut TempleState, cfg: &TempleConfig, left: f32, right: f32) {
42 state.prominence_l = left.clamp(0.0, cfg.max_prominence);
43 state.prominence_r = right.clamp(0.0, cfg.max_prominence);
44}
45
46#[allow(dead_code)]
47pub fn temple_set_hollow(state: &mut TempleState, left: f32, right: f32) {
48 state.hollow_l = left.clamp(0.0, 1.0);
49 state.hollow_r = right.clamp(0.0, 1.0);
50}
51
52#[allow(dead_code)]
53pub fn temple_mirror(state: &mut TempleState) {
54 let avg_p = (state.prominence_l + state.prominence_r) * 0.5;
55 let avg_h = (state.hollow_l + state.hollow_r) * 0.5;
56 state.prominence_l = avg_p;
57 state.prominence_r = avg_p;
58 state.hollow_l = avg_h;
59 state.hollow_r = avg_h;
60}
61
62#[allow(dead_code)]
63pub fn temple_reset(state: &mut TempleState) {
64 *state = new_temple_state();
65}
66
67#[allow(dead_code)]
68pub fn temple_to_weights(state: &TempleState) -> Vec<(String, f32)> {
69 vec![
70 ("temple_prominence_l".to_string(), state.prominence_l),
71 ("temple_prominence_r".to_string(), state.prominence_r),
72 ("temple_hollow_l".to_string(), state.hollow_l),
73 ("temple_hollow_r".to_string(), state.hollow_r),
74 ]
75}
76
77#[allow(dead_code)]
78pub fn temple_to_json(state: &TempleState) -> String {
79 format!(
80 r#"{{"prominence_l":{:.4},"prominence_r":{:.4},"hollow_l":{:.4},"hollow_r":{:.4}}}"#,
81 state.prominence_l, state.prominence_r, state.hollow_l, state.hollow_r
82 )
83}
84
85#[cfg(test)]
86mod tests {
87 use super::*;
88
89 #[test]
90 fn test_default_config() {
91 let cfg = default_temple_config();
92 assert_eq!(cfg.max_prominence, 1.0);
93 }
94
95 #[test]
96 fn test_new_state_zeros() {
97 let s = new_temple_state();
98 assert_eq!(s.prominence_l, 0.0);
99 assert_eq!(s.hollow_r, 0.0);
100 }
101
102 #[test]
103 fn test_set_prominence_clamps() {
104 let cfg = default_temple_config();
105 let mut s = new_temple_state();
106 temple_set_prominence(&mut s, &cfg, 2.0, -1.0);
107 assert_eq!(s.prominence_l, 1.0);
108 assert_eq!(s.prominence_r, 0.0);
109 }
110
111 #[test]
112 fn test_set_hollow_clamps() {
113 let mut s = new_temple_state();
114 temple_set_hollow(&mut s, 0.4, 1.5);
115 assert!((s.hollow_l - 0.4).abs() < 1e-6);
116 assert_eq!(s.hollow_r, 1.0);
117 }
118
119 #[test]
120 fn test_mirror_averages() {
121 let mut s = new_temple_state();
122 s.prominence_l = 0.2;
123 s.prominence_r = 0.6;
124 temple_mirror(&mut s);
125 assert!((s.prominence_l - 0.4).abs() < 1e-6);
126 assert!((s.prominence_r - 0.4).abs() < 1e-6);
127 }
128
129 #[test]
130 fn test_reset() {
131 let cfg = default_temple_config();
132 let mut s = new_temple_state();
133 temple_set_prominence(&mut s, &cfg, 0.8, 0.8);
134 temple_reset(&mut s);
135 assert_eq!(s.prominence_l, 0.0);
136 }
137
138 #[test]
139 fn test_to_weights_count() {
140 let s = new_temple_state();
141 assert_eq!(temple_to_weights(&s).len(), 4);
142 }
143
144 #[test]
145 fn test_to_json_contains_fields() {
146 let s = new_temple_state();
147 let j = temple_to_json(&s);
148 assert!(j.contains("prominence_l"));
149 assert!(j.contains("hollow_r"));
150 }
151
152 #[test]
153 fn test_set_prominence_valid() {
154 let cfg = default_temple_config();
155 let mut s = new_temple_state();
156 temple_set_prominence(&mut s, &cfg, 0.5, 0.3);
157 assert!((s.prominence_l - 0.5).abs() < 1e-6);
158 assert!((s.prominence_r - 0.3).abs() < 1e-6);
159 }
160}
161
162#[allow(dead_code)]
166#[derive(Debug, Clone, PartialEq)]
167pub struct TempleControl {
168 pub width: f32,
170 pub flatness: f32,
172 pub prominence: f32,
174}
175
176#[allow(dead_code)]
178pub fn default_temple_control() -> TempleControl {
179 TempleControl {
180 width: 0.5,
181 flatness: 0.0,
182 prominence: 0.5,
183 }
184}
185
186#[allow(dead_code)]
188pub fn apply_temple_control(weights: &mut [f32], tc: &TempleControl) {
189 if !weights.is_empty() {
190 weights[0] = tc.width;
191 }
192 if weights.len() > 1 {
193 weights[1] = tc.flatness;
194 }
195 if weights.len() > 2 {
196 weights[2] = tc.prominence;
197 }
198}
199
200#[allow(dead_code)]
202pub fn temple_blend(a: &TempleControl, b: &TempleControl, t: f32) -> TempleControl {
203 let t = t.clamp(0.0, 1.0);
204 TempleControl {
205 width: a.width + (b.width - a.width) * t,
206 flatness: a.flatness + (b.flatness - a.flatness) * t,
207 prominence: a.prominence + (b.prominence - a.prominence) * t,
208 }
209}