#![allow(dead_code)]
#[derive(Debug, Clone)]
pub struct OccipitalMorph {
pub protuberance: f32,
pub nuchal_width: f32,
pub squama_curve: f32,
}
pub fn new_occipital_morph() -> OccipitalMorph {
OccipitalMorph {
protuberance: 0.0,
nuchal_width: 0.0,
squama_curve: 0.0,
}
}
pub fn occ_set_protuberance(m: &mut OccipitalMorph, v: f32) {
m.protuberance = v.clamp(0.0, 1.0);
}
pub fn occ_set_nuchal_width(m: &mut OccipitalMorph, v: f32) {
m.nuchal_width = v.clamp(0.0, 1.0);
}
pub fn occ_set_squama_curve(m: &mut OccipitalMorph, v: f32) {
m.squama_curve = v.clamp(0.0, 1.0);
}
pub fn occ_overall_weight(m: &OccipitalMorph) -> f32 {
(m.protuberance + m.nuchal_width + m.squama_curve) / 3.0
}
pub fn occ_blend(a: &OccipitalMorph, b: &OccipitalMorph, t: f32) -> OccipitalMorph {
let t = t.clamp(0.0, 1.0);
OccipitalMorph {
protuberance: a.protuberance + (b.protuberance - a.protuberance) * t,
nuchal_width: a.nuchal_width + (b.nuchal_width - a.nuchal_width) * t,
squama_curve: a.squama_curve + (b.squama_curve - a.squama_curve) * t,
}
}
pub fn occ_is_neutral(m: &OccipitalMorph) -> bool {
m.protuberance < 1e-5 && m.nuchal_width < 1e-5 && m.squama_curve < 1e-5
}
pub fn occ_to_json(m: &OccipitalMorph) -> String {
format!(
r#"{{"protuberance":{:.4},"nuchal_width":{:.4},"squama_curve":{:.4}}}"#,
m.protuberance, m.nuchal_width, m.squama_curve
)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_defaults() {
let m = new_occipital_morph();
assert_eq!(m.protuberance, 0.0);
}
#[test]
fn test_set_protuberance() {
let mut m = new_occipital_morph();
occ_set_protuberance(&mut m, 0.4);
assert!((m.protuberance - 0.4).abs() < 1e-6);
}
#[test]
fn test_clamp_high() {
let mut m = new_occipital_morph();
occ_set_protuberance(&mut m, 5.0);
assert_eq!(m.protuberance, 1.0);
}
#[test]
fn test_clamp_low() {
let mut m = new_occipital_morph();
occ_set_squama_curve(&mut m, -2.0);
assert_eq!(m.squama_curve, 0.0);
}
#[test]
fn test_neutral_true() {
assert!(occ_is_neutral(&new_occipital_morph()));
}
#[test]
fn test_neutral_false() {
let mut m = new_occipital_morph();
occ_set_nuchal_width(&mut m, 0.2);
assert!(!occ_is_neutral(&m));
}
#[test]
fn test_weight() {
let m = OccipitalMorph {
protuberance: 0.6,
nuchal_width: 0.6,
squama_curve: 0.6,
};
assert!((occ_overall_weight(&m) - 0.6).abs() < 1e-5);
}
#[test]
fn test_blend() {
let a = new_occipital_morph();
let b = OccipitalMorph {
protuberance: 1.0,
nuchal_width: 0.0,
squama_curve: 0.0,
};
let c = occ_blend(&a, &b, 1.0);
assert!((c.protuberance - 1.0).abs() < 1e-6);
}
#[test]
fn test_to_json() {
assert!(occ_to_json(&new_occipital_morph()).contains("protuberance"));
}
#[test]
fn test_clone() {
let m = OccipitalMorph {
protuberance: 0.2,
nuchal_width: 0.5,
squama_curve: 0.8,
};
let m2 = m.clone();
assert_eq!(m.nuchal_width, m2.nuchal_width);
}
}