oxihuman_morph/
eye_depth_control.rs1#![allow(dead_code)]
7
8#[allow(dead_code)]
10#[derive(Debug, Clone)]
11pub struct EyeDepthConfig {
12 pub max_depth: f32,
13}
14
15#[allow(dead_code)]
18#[derive(Debug, Clone)]
19pub struct EyeDepthState {
20 pub depth_l: f32,
21 pub depth_r: f32,
22}
23
24#[allow(dead_code)]
25pub fn default_eye_depth_config() -> EyeDepthConfig {
26 EyeDepthConfig { max_depth: 1.0 }
27}
28
29#[allow(dead_code)]
30pub fn new_eye_depth_state() -> EyeDepthState {
31 EyeDepthState {
32 depth_l: 0.0,
33 depth_r: 0.0,
34 }
35}
36
37#[allow(dead_code)]
38pub fn ed_set_depth_left(state: &mut EyeDepthState, cfg: &EyeDepthConfig, v: f32) {
39 state.depth_l = v.clamp(-cfg.max_depth, cfg.max_depth);
40}
41
42#[allow(dead_code)]
43pub fn ed_set_depth_right(state: &mut EyeDepthState, cfg: &EyeDepthConfig, v: f32) {
44 state.depth_r = v.clamp(-cfg.max_depth, cfg.max_depth);
45}
46
47#[allow(dead_code)]
48pub fn ed_set_depth_both(state: &mut EyeDepthState, cfg: &EyeDepthConfig, v: f32) {
49 let clamped = v.clamp(-cfg.max_depth, cfg.max_depth);
50 state.depth_l = clamped;
51 state.depth_r = clamped;
52}
53
54#[allow(dead_code)]
55pub fn ed_mirror(state: &mut EyeDepthState) {
56 let avg = (state.depth_l + state.depth_r) * 0.5;
57 state.depth_l = avg;
58 state.depth_r = avg;
59}
60
61#[allow(dead_code)]
62pub fn ed_reset(state: &mut EyeDepthState) {
63 *state = new_eye_depth_state();
64}
65
66#[allow(dead_code)]
67pub fn ed_to_weights(state: &EyeDepthState) -> Vec<(String, f32)> {
68 vec![
69 ("eye_depth_l".to_string(), state.depth_l),
70 ("eye_depth_r".to_string(), state.depth_r),
71 ]
72}
73
74#[allow(dead_code)]
75pub fn ed_to_json(state: &EyeDepthState) -> String {
76 format!(
77 r#"{{"depth_l":{:.4},"depth_r":{:.4}}}"#,
78 state.depth_l, state.depth_r
79 )
80}
81
82#[allow(dead_code)]
83pub fn ed_clamp(state: &mut EyeDepthState, cfg: &EyeDepthConfig) {
84 state.depth_l = state.depth_l.clamp(-cfg.max_depth, cfg.max_depth);
85 state.depth_r = state.depth_r.clamp(-cfg.max_depth, cfg.max_depth);
86}
87
88#[cfg(test)]
89mod tests {
90 use super::*;
91
92 #[test]
93 fn test_default_config() {
94 let cfg = default_eye_depth_config();
95 assert!((cfg.max_depth - 1.0).abs() < 1e-6);
96 }
97
98 #[test]
99 fn test_new_state_zeros() {
100 let s = new_eye_depth_state();
101 assert_eq!(s.depth_l, 0.0);
102 assert_eq!(s.depth_r, 0.0);
103 }
104
105 #[test]
106 fn test_set_depth_left_clamps() {
107 let cfg = default_eye_depth_config();
108 let mut s = new_eye_depth_state();
109 ed_set_depth_left(&mut s, &cfg, 3.0);
110 assert!((s.depth_l - 1.0).abs() < 1e-6);
111 ed_set_depth_left(&mut s, &cfg, -3.0);
112 assert!((s.depth_l + 1.0).abs() < 1e-6);
113 }
114
115 #[test]
116 fn test_set_depth_right() {
117 let cfg = default_eye_depth_config();
118 let mut s = new_eye_depth_state();
119 ed_set_depth_right(&mut s, &cfg, 0.5);
120 assert!((s.depth_r - 0.5).abs() < 1e-6);
121 }
122
123 #[test]
124 fn test_set_depth_both() {
125 let cfg = default_eye_depth_config();
126 let mut s = new_eye_depth_state();
127 ed_set_depth_both(&mut s, &cfg, -0.7);
128 assert!((s.depth_l + 0.7).abs() < 1e-6);
129 assert!((s.depth_r + 0.7).abs() < 1e-6);
130 }
131
132 #[test]
133 fn test_mirror() {
134 let cfg = default_eye_depth_config();
135 let mut s = new_eye_depth_state();
136 ed_set_depth_left(&mut s, &cfg, 0.4);
137 ed_set_depth_right(&mut s, &cfg, 0.8);
138 ed_mirror(&mut s);
139 assert!((s.depth_l - 0.6).abs() < 1e-6);
140 assert!((s.depth_r - 0.6).abs() < 1e-6);
141 }
142
143 #[test]
144 fn test_reset() {
145 let cfg = default_eye_depth_config();
146 let mut s = new_eye_depth_state();
147 ed_set_depth_both(&mut s, &cfg, 0.5);
148 ed_reset(&mut s);
149 assert_eq!(s.depth_l, 0.0);
150 }
151
152 #[test]
153 fn test_to_weights_count() {
154 let s = new_eye_depth_state();
155 assert_eq!(ed_to_weights(&s).len(), 2);
156 }
157
158 #[test]
159 fn test_to_json_contains_fields() {
160 let s = new_eye_depth_state();
161 let j = ed_to_json(&s);
162 assert!(j.contains("depth_l"));
163 assert!(j.contains("depth_r"));
164 }
165}