#![allow(dead_code)]
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum MoleType {
CommonNevus,
DysplasticNevus,
CongenitalNevus,
BluNevus,
}
#[derive(Debug, Clone)]
pub struct MoleEntry {
pub mole_type: MoleType,
pub position: [f32; 3],
pub radius: f32,
pub elevation: f32,
}
#[derive(Debug, Clone)]
pub struct MoleMorph {
pub moles: Vec<MoleEntry>,
pub global_opacity: f32,
pub morph_count: usize,
pub enabled: bool,
}
impl MoleMorph {
pub fn new(morph_count: usize) -> Self {
MoleMorph {
moles: Vec::new(),
global_opacity: 1.0,
morph_count,
enabled: true,
}
}
}
pub fn new_mole_morph(morph_count: usize) -> MoleMorph {
MoleMorph::new(morph_count)
}
pub fn mom_add_mole(morph: &mut MoleMorph, entry: MoleEntry) {
morph.moles.push(entry);
}
pub fn mom_set_opacity(morph: &mut MoleMorph, opacity: f32) {
morph.global_opacity = opacity.clamp(0.0, 1.0);
}
pub fn mom_clear(morph: &mut MoleMorph) {
morph.moles.clear();
}
pub fn mom_evaluate(morph: &MoleMorph) -> Vec<f32> {
if !morph.enabled || morph.morph_count == 0 {
return vec![];
}
vec![morph.global_opacity; morph.morph_count]
}
pub fn mom_set_enabled(morph: &mut MoleMorph, enabled: bool) {
morph.enabled = enabled;
}
pub fn mom_mole_count(morph: &MoleMorph) -> usize {
morph.moles.len()
}
pub fn mom_to_json(morph: &MoleMorph) -> String {
format!(
r#"{{"mole_count":{},"global_opacity":{},"morph_count":{},"enabled":{}}}"#,
morph.moles.len(),
morph.global_opacity,
morph.morph_count,
morph.enabled
)
}
#[cfg(test)]
mod tests {
use super::*;
fn make_mole() -> MoleEntry {
MoleEntry {
mole_type: MoleType::CommonNevus,
position: [0.1, 0.2, 0.3],
radius: 0.02,
elevation: 0.01,
}
}
#[test]
fn test_initial_empty() {
let m = new_mole_morph(4);
assert_eq!(mom_mole_count(&m), 0 );
}
#[test]
fn test_add_mole() {
let mut m = new_mole_morph(4);
mom_add_mole(&mut m, make_mole());
assert_eq!(mom_mole_count(&m), 1 );
}
#[test]
fn test_clear() {
let mut m = new_mole_morph(4);
mom_add_mole(&mut m, make_mole());
mom_clear(&mut m);
assert_eq!(mom_mole_count(&m), 0 );
}
#[test]
fn test_opacity_clamp() {
let mut m = new_mole_morph(4);
mom_set_opacity(&mut m, 2.0);
assert!((m.global_opacity - 1.0).abs() < 1e-6 );
}
#[test]
fn test_evaluate_length() {
let m = new_mole_morph(5);
assert_eq!(
mom_evaluate(&m).len(),
5
);
}
#[test]
fn test_evaluate_disabled() {
let mut m = new_mole_morph(4);
mom_set_enabled(&mut m, false);
assert!(mom_evaluate(&m).is_empty() );
}
#[test]
fn test_to_json_has_mole_count() {
let m = new_mole_morph(4);
let j = mom_to_json(&m);
assert!(j.contains("\"mole_count\"") );
}
#[test]
fn test_enabled_default() {
let m = new_mole_morph(4);
assert!(m.enabled );
}
#[test]
fn test_evaluate_matches_opacity() {
let mut m = new_mole_morph(3);
mom_set_opacity(&mut m, 0.7);
let out = mom_evaluate(&m);
assert!((out[0] - 0.7).abs() < 1e-5 );
}
}