#![allow(dead_code)]
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum HeatMapRamp {
BlueRed,
Inferno,
Viridis,
Grayscale,
}
#[derive(Debug, Clone)]
pub struct WeightHeatMapView {
pub ramp: HeatMapRamp,
pub bone_index: usize,
pub threshold_low: f32,
pub threshold_high: f32,
pub enabled: bool,
}
impl WeightHeatMapView {
pub fn new() -> Self {
WeightHeatMapView {
ramp: HeatMapRamp::BlueRed,
bone_index: 0,
threshold_low: 0.0,
threshold_high: 1.0,
enabled: true,
}
}
}
impl Default for WeightHeatMapView {
fn default() -> Self {
Self::new()
}
}
pub fn new_weight_heat_map_view() -> WeightHeatMapView {
WeightHeatMapView::new()
}
pub fn whm_set_ramp(view: &mut WeightHeatMapView, ramp: HeatMapRamp) {
view.ramp = ramp;
}
pub fn whm_set_bone_index(view: &mut WeightHeatMapView, bone_index: usize) {
view.bone_index = bone_index;
}
pub fn whm_set_thresholds(view: &mut WeightHeatMapView, low: f32, high: f32) {
view.threshold_low = low.clamp(0.0, 1.0);
view.threshold_high = high.clamp(0.0, 1.0);
}
pub fn whm_set_enabled(view: &mut WeightHeatMapView, enabled: bool) {
view.enabled = enabled;
}
pub fn whm_to_json(view: &WeightHeatMapView) -> String {
let ramp = match view.ramp {
HeatMapRamp::BlueRed => "blue_red",
HeatMapRamp::Inferno => "inferno",
HeatMapRamp::Viridis => "viridis",
HeatMapRamp::Grayscale => "grayscale",
};
format!(
r#"{{"ramp":"{}","bone_index":{},"threshold_low":{},"threshold_high":{},"enabled":{}}}"#,
ramp, view.bone_index, view.threshold_low, view.threshold_high, view.enabled
)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_default_ramp() {
let v = new_weight_heat_map_view();
assert_eq!(
v.ramp,
HeatMapRamp::BlueRed
);
}
#[test]
fn test_set_ramp() {
let mut v = new_weight_heat_map_view();
whm_set_ramp(&mut v, HeatMapRamp::Viridis);
assert_eq!(v.ramp, HeatMapRamp::Viridis );
}
#[test]
fn test_set_bone_index() {
let mut v = new_weight_heat_map_view();
whm_set_bone_index(&mut v, 5);
assert_eq!(v.bone_index, 5 );
}
#[test]
fn test_threshold_clamp() {
let mut v = new_weight_heat_map_view();
whm_set_thresholds(&mut v, -0.5, 1.5);
assert!(v.threshold_low.abs() < 1e-6 );
assert!((v.threshold_high - 1.0).abs() < 1e-6 );
}
#[test]
fn test_set_enabled() {
let mut v = new_weight_heat_map_view();
whm_set_enabled(&mut v, false);
assert!(!v.enabled );
}
#[test]
fn test_to_json_has_ramp() {
let v = new_weight_heat_map_view();
let j = whm_to_json(&v);
assert!(j.contains("\"ramp\"") );
}
#[test]
fn test_enabled_default() {
let v = new_weight_heat_map_view();
assert!(v.enabled );
}
#[test]
fn test_default_bone_index() {
let v = new_weight_heat_map_view();
assert_eq!(v.bone_index, 0 );
}
#[test]
fn test_default_thresholds() {
let v = new_weight_heat_map_view();
assert!(v.threshold_low.abs() < 1e-6 );
assert!((v.threshold_high - 1.0).abs() < 1e-6 );
}
}