oxihuman_morph/
jaw_depth_control.rs1#![allow(dead_code)]
5
6#[allow(dead_code)]
10#[derive(Debug, Clone)]
11pub struct JawDepthConfig {
12 pub min_depth: f32,
13 pub max_depth: f32,
14}
15
16#[allow(dead_code)]
18#[derive(Debug, Clone)]
19pub struct JawDepthState {
20 pub depth: f32,
21 pub angle: f32,
22 pub ramus_height: f32,
23}
24
25#[allow(dead_code)]
26pub fn default_jaw_depth_config() -> JawDepthConfig {
27 JawDepthConfig {
28 min_depth: 0.0,
29 max_depth: 1.0,
30 }
31}
32
33#[allow(dead_code)]
34pub fn new_jaw_depth_state() -> JawDepthState {
35 JawDepthState {
36 depth: 0.5,
37 angle: 0.5,
38 ramus_height: 0.5,
39 }
40}
41
42#[allow(dead_code)]
43pub fn jd_set_depth(state: &mut JawDepthState, cfg: &JawDepthConfig, v: f32) {
44 state.depth = v.clamp(cfg.min_depth, cfg.max_depth);
45}
46
47#[allow(dead_code)]
48pub fn jd_set_angle(state: &mut JawDepthState, v: f32) {
49 state.angle = v.clamp(0.0, 1.0);
50}
51
52#[allow(dead_code)]
53pub fn jd_set_ramus(state: &mut JawDepthState, v: f32) {
54 state.ramus_height = v.clamp(0.0, 1.0);
55}
56
57#[allow(dead_code)]
58pub fn jd_reset(state: &mut JawDepthState) {
59 *state = new_jaw_depth_state();
60}
61
62#[allow(dead_code)]
63pub fn jd_to_weights(state: &JawDepthState) -> Vec<(String, f32)> {
64 vec![
65 ("jaw_depth".to_string(), state.depth),
66 ("jaw_angle".to_string(), state.angle),
67 ("jaw_ramus_height".to_string(), state.ramus_height),
68 ]
69}
70
71#[allow(dead_code)]
72pub fn jd_to_json(state: &JawDepthState) -> String {
73 format!(
74 r#"{{"depth":{:.4},"angle":{:.4},"ramus_height":{:.4}}}"#,
75 state.depth, state.angle, state.ramus_height
76 )
77}
78
79#[allow(dead_code)]
80pub fn jd_blend(a: &JawDepthState, b: &JawDepthState, t: f32) -> JawDepthState {
81 let t = t.clamp(0.0, 1.0);
82 JawDepthState {
83 depth: a.depth + (b.depth - a.depth) * t,
84 angle: a.angle + (b.angle - a.angle) * t,
85 ramus_height: a.ramus_height + (b.ramus_height - a.ramus_height) * t,
86 }
87}
88
89#[allow(dead_code)]
90pub fn jd_effective_depth(state: &JawDepthState) -> f32 {
91 state.depth * (1.0 + state.angle * 0.15)
92}
93
94#[cfg(test)]
95mod tests {
96 use super::*;
97
98 #[test]
99 fn test_default_config() {
100 let cfg = default_jaw_depth_config();
101 assert!(cfg.min_depth.abs() < 1e-6);
102 assert!((cfg.max_depth - 1.0).abs() < 1e-6);
103 }
104
105 #[test]
106 fn test_new_state() {
107 let s = new_jaw_depth_state();
108 assert!((s.depth - 0.5).abs() < 1e-6);
109 assert!((s.angle - 0.5).abs() < 1e-6);
110 }
111
112 #[test]
113 fn test_set_depth_clamps() {
114 let cfg = default_jaw_depth_config();
115 let mut s = new_jaw_depth_state();
116 jd_set_depth(&mut s, &cfg, 5.0);
117 assert!((s.depth - 1.0).abs() < 1e-6);
118 }
119
120 #[test]
121 fn test_set_angle() {
122 let mut s = new_jaw_depth_state();
123 jd_set_angle(&mut s, 0.8);
124 assert!((s.angle - 0.8).abs() < 1e-6);
125 }
126
127 #[test]
128 fn test_set_ramus() {
129 let mut s = new_jaw_depth_state();
130 jd_set_ramus(&mut s, 0.2);
131 assert!((s.ramus_height - 0.2).abs() < 1e-6);
132 }
133
134 #[test]
135 fn test_reset() {
136 let cfg = default_jaw_depth_config();
137 let mut s = new_jaw_depth_state();
138 jd_set_depth(&mut s, &cfg, 0.9);
139 jd_reset(&mut s);
140 assert!((s.depth - 0.5).abs() < 1e-6);
141 }
142
143 #[test]
144 fn test_to_weights() {
145 let s = new_jaw_depth_state();
146 assert_eq!(jd_to_weights(&s).len(), 3);
147 }
148
149 #[test]
150 fn test_to_json() {
151 let s = new_jaw_depth_state();
152 let j = jd_to_json(&s);
153 assert!(j.contains("depth"));
154 assert!(j.contains("angle"));
155 }
156
157 #[test]
158 fn test_blend() {
159 let a = new_jaw_depth_state();
160 let mut b = new_jaw_depth_state();
161 b.depth = 1.0;
162 let mid = jd_blend(&a, &b, 0.5);
163 assert!((mid.depth - 0.75).abs() < 1e-6);
164 }
165
166 #[test]
167 fn test_effective_depth() {
168 let s = new_jaw_depth_state();
169 let d = jd_effective_depth(&s);
170 assert!(d > 0.0);
171 }
172}