#![allow(dead_code)]
#[derive(Debug, Clone)]
pub struct CervicalMorph {
pub lordosis: f32,
pub forward_head: f32,
pub lateral_list: f32,
}
pub fn new_cervical_morph() -> CervicalMorph {
CervicalMorph {
lordosis: 0.0,
forward_head: 0.0,
lateral_list: 0.0,
}
}
pub fn cerv_set_lordosis(m: &mut CervicalMorph, v: f32) {
m.lordosis = v.clamp(0.0, 1.0);
}
pub fn cerv_set_forward_head(m: &mut CervicalMorph, v: f32) {
m.forward_head = v.clamp(0.0, 1.0);
}
pub fn cerv_set_lateral_list(m: &mut CervicalMorph, v: f32) {
m.lateral_list = v.clamp(-1.0, 1.0);
}
pub fn cerv_overall_weight(m: &CervicalMorph) -> f32 {
(m.lordosis + m.forward_head + m.lateral_list.abs()) / 3.0
}
pub fn cerv_blend(a: &CervicalMorph, b: &CervicalMorph, t: f32) -> CervicalMorph {
let t = t.clamp(0.0, 1.0);
CervicalMorph {
lordosis: a.lordosis + (b.lordosis - a.lordosis) * t,
forward_head: a.forward_head + (b.forward_head - a.forward_head) * t,
lateral_list: a.lateral_list + (b.lateral_list - a.lateral_list) * t,
}
}
pub fn cerv_is_neutral(m: &CervicalMorph) -> bool {
m.lordosis < 1e-5 && m.forward_head < 1e-5 && m.lateral_list.abs() < 1e-5
}
pub fn cerv_to_json(m: &CervicalMorph) -> String {
format!(
r#"{{"lordosis":{:.4},"forward_head":{:.4},"lateral_list":{:.4}}}"#,
m.lordosis, m.forward_head, m.lateral_list
)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_defaults() {
let m = new_cervical_morph();
assert_eq!(m.lordosis, 0.0);
}
#[test]
fn test_set_lordosis() {
let mut m = new_cervical_morph();
cerv_set_lordosis(&mut m, 0.6);
assert!((m.lordosis - 0.6).abs() < 1e-6);
}
#[test]
fn test_clamp_high() {
let mut m = new_cervical_morph();
cerv_set_lordosis(&mut m, 2.0);
assert_eq!(m.lordosis, 1.0);
}
#[test]
fn test_lateral_clamp() {
let mut m = new_cervical_morph();
cerv_set_lateral_list(&mut m, 5.0);
assert_eq!(m.lateral_list, 1.0);
}
#[test]
fn test_neutral_true() {
assert!(cerv_is_neutral(&new_cervical_morph()));
}
#[test]
fn test_neutral_false() {
let mut m = new_cervical_morph();
cerv_set_lordosis(&mut m, 0.5);
assert!(!cerv_is_neutral(&m));
}
#[test]
fn test_weight_formula() {
let m = CervicalMorph {
lordosis: 0.3,
forward_head: 0.3,
lateral_list: 0.3,
};
assert!((cerv_overall_weight(&m) - 0.3).abs() < 1e-5);
}
#[test]
fn test_blend() {
let a = new_cervical_morph();
let b = CervicalMorph {
lordosis: 1.0,
forward_head: 0.0,
lateral_list: 0.0,
};
let c = cerv_blend(&a, &b, 0.5);
assert!((c.lordosis - 0.5).abs() < 1e-5);
}
#[test]
fn test_to_json() {
assert!(cerv_to_json(&new_cervical_morph()).contains("lordosis"));
}
#[test]
fn test_clone() {
let m = CervicalMorph {
lordosis: 0.4,
forward_head: 0.5,
lateral_list: -0.2,
};
let m2 = m.clone();
assert_eq!(m.forward_head, m2.forward_head);
}
}