oxihuman_morph/
foot_ball_control.rs1#![allow(dead_code)]
4
5#[allow(dead_code)]
9#[derive(Debug, Clone)]
10pub struct FootBallConfig {
11 pub max_width: f32,
12}
13
14#[allow(dead_code)]
16#[derive(Debug, Clone, Copy, PartialEq, Eq)]
17pub enum FootBallSide {
18 Left,
19 Right,
20}
21
22#[allow(dead_code)]
24#[derive(Debug, Clone)]
25pub struct FootBallState {
26 pub left_width: f32,
27 pub right_width: f32,
28 pub padding: f32,
29}
30
31#[allow(dead_code)]
32pub fn default_foot_ball_config() -> FootBallConfig {
33 FootBallConfig { max_width: 1.0 }
34}
35
36#[allow(dead_code)]
37pub fn new_foot_ball_state() -> FootBallState {
38 FootBallState {
39 left_width: 0.0,
40 right_width: 0.0,
41 padding: 0.0,
42 }
43}
44
45#[allow(dead_code)]
46pub fn fb_set_width(state: &mut FootBallState, cfg: &FootBallConfig, side: FootBallSide, v: f32) {
47 let clamped = v.clamp(0.0, cfg.max_width);
48 match side {
49 FootBallSide::Left => state.left_width = clamped,
50 FootBallSide::Right => state.right_width = clamped,
51 }
52}
53
54#[allow(dead_code)]
55pub fn fb_set_both(state: &mut FootBallState, cfg: &FootBallConfig, v: f32) {
56 let clamped = v.clamp(0.0, cfg.max_width);
57 state.left_width = clamped;
58 state.right_width = clamped;
59}
60
61#[allow(dead_code)]
62pub fn fb_set_padding(state: &mut FootBallState, v: f32) {
63 state.padding = v.clamp(0.0, 1.0);
64}
65
66#[allow(dead_code)]
67pub fn fb_reset(state: &mut FootBallState) {
68 *state = new_foot_ball_state();
69}
70
71#[allow(dead_code)]
72pub fn fb_is_neutral(state: &FootBallState) -> bool {
73 state.left_width.abs() < 1e-6 && state.right_width.abs() < 1e-6 && state.padding.abs() < 1e-6
74}
75
76#[allow(dead_code)]
77pub fn fb_average_width(state: &FootBallState) -> f32 {
78 (state.left_width + state.right_width) * 0.5
79}
80
81#[allow(dead_code)]
82pub fn fb_symmetry(state: &FootBallState) -> f32 {
83 (state.left_width - state.right_width).abs()
84}
85
86#[allow(dead_code)]
87pub fn fb_blend(a: &FootBallState, b: &FootBallState, t: f32) -> FootBallState {
88 let t = t.clamp(0.0, 1.0);
89 FootBallState {
90 left_width: a.left_width + (b.left_width - a.left_width) * t,
91 right_width: a.right_width + (b.right_width - a.right_width) * t,
92 padding: a.padding + (b.padding - a.padding) * t,
93 }
94}
95
96#[allow(dead_code)]
97pub fn fb_to_weights(state: &FootBallState) -> Vec<(String, f32)> {
98 vec![
99 ("foot_ball_width_l".to_string(), state.left_width),
100 ("foot_ball_width_r".to_string(), state.right_width),
101 ("foot_ball_padding".to_string(), state.padding),
102 ]
103}
104
105#[allow(dead_code)]
106pub fn fb_to_json(state: &FootBallState) -> String {
107 format!(
108 r#"{{"left_width":{:.4},"right_width":{:.4},"padding":{:.4}}}"#,
109 state.left_width, state.right_width, state.padding
110 )
111}
112
113#[cfg(test)]
114mod tests {
115 use super::*;
116
117 #[test]
118 fn default_config() {
119 let cfg = default_foot_ball_config();
120 assert!((cfg.max_width - 1.0).abs() < 1e-6);
121 }
122
123 #[test]
124 fn new_state_neutral() {
125 let s = new_foot_ball_state();
126 assert!(fb_is_neutral(&s));
127 }
128
129 #[test]
130 fn set_width_left() {
131 let cfg = default_foot_ball_config();
132 let mut s = new_foot_ball_state();
133 fb_set_width(&mut s, &cfg, FootBallSide::Left, 0.5);
134 assert!((s.left_width - 0.5).abs() < 1e-6);
135 }
136
137 #[test]
138 fn set_width_clamps() {
139 let cfg = default_foot_ball_config();
140 let mut s = new_foot_ball_state();
141 fb_set_width(&mut s, &cfg, FootBallSide::Right, 2.0);
142 assert!((s.right_width - 1.0).abs() < 1e-6);
143 }
144
145 #[test]
146 fn set_both_symmetric() {
147 let cfg = default_foot_ball_config();
148 let mut s = new_foot_ball_state();
149 fb_set_both(&mut s, &cfg, 0.6);
150 assert!(fb_symmetry(&s) < 1e-6);
151 }
152
153 #[test]
154 fn set_padding() {
155 let mut s = new_foot_ball_state();
156 fb_set_padding(&mut s, 0.4);
157 assert!((s.padding - 0.4).abs() < 1e-6);
158 }
159
160 #[test]
161 fn reset_clears() {
162 let cfg = default_foot_ball_config();
163 let mut s = new_foot_ball_state();
164 fb_set_both(&mut s, &cfg, 0.8);
165 fb_reset(&mut s);
166 assert!(fb_is_neutral(&s));
167 }
168
169 #[test]
170 fn blend_midpoint() {
171 let a = new_foot_ball_state();
172 let cfg = default_foot_ball_config();
173 let mut b = new_foot_ball_state();
174 fb_set_both(&mut b, &cfg, 1.0);
175 let m = fb_blend(&a, &b, 0.5);
176 assert!((m.left_width - 0.5).abs() < 1e-6);
177 }
178
179 #[test]
180 fn to_weights_count() {
181 let s = new_foot_ball_state();
182 assert_eq!(fb_to_weights(&s).len(), 3);
183 }
184
185 #[test]
186 fn to_json_fields() {
187 let s = new_foot_ball_state();
188 let j = fb_to_json(&s);
189 assert!(j.contains("left_width"));
190 }
191}