#[inline]
pub fn apply(raw: f64, scale: Option<f64>, offset: Option<f64>) -> f64 {
let scale = scale.unwrap_or(1.0);
let offset = offset.unwrap_or(0.0);
if scale == 1.0 && offset == 0.0 {
raw
} else {
raw / scale - offset
}
}
#[inline]
pub fn unapply(physical: f64, scale: Option<f64>, offset: Option<f64>) -> f64 {
let scale = scale.unwrap_or(1.0);
let offset = offset.unwrap_or(0.0);
(physical + offset) * scale
}
#[inline]
pub fn is_identity(scale: Option<f64>, offset: Option<f64>) -> bool {
scale.unwrap_or(1.0) == 1.0 && offset.unwrap_or(0.0) == 0.0
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn no_scale_no_offset_is_identity() {
assert_eq!(apply(126.0, None, None), 126.0);
assert_eq!(apply(126.0, Some(1.0), Some(0.0)), 126.0);
}
#[test]
fn speed_scale_1000() {
assert_eq!(apply(3000.0, Some(1000.0), None), 3.0);
}
#[test]
fn altitude_scale_5_offset_500() {
assert_eq!(apply(10435.0, Some(5.0), Some(500.0)), 1587.0);
}
#[test]
fn unapply_round_trips_apply() {
let raw = 10435.0;
let scale = Some(5.0);
let offset = Some(500.0);
let physical = apply(raw, scale, offset);
assert_eq!(unapply(physical, scale, offset), raw);
}
#[test]
fn is_identity_classifies_correctly() {
assert!(is_identity(None, None));
assert!(is_identity(Some(1.0), Some(0.0)));
assert!(!is_identity(Some(1000.0), None));
assert!(!is_identity(None, Some(500.0)));
}
}