fit/transforms/
scale_offset.rs1#[inline]
16pub fn apply(raw: f64, scale: Option<f64>, offset: Option<f64>) -> f64 {
17 let scale = scale.unwrap_or(1.0);
18 let offset = offset.unwrap_or(0.0);
19 if scale == 1.0 && offset == 0.0 {
20 raw
23 } else {
24 raw / scale - offset
25 }
26}
27
28#[inline]
31pub fn unapply(physical: f64, scale: Option<f64>, offset: Option<f64>) -> f64 {
32 let scale = scale.unwrap_or(1.0);
33 let offset = offset.unwrap_or(0.0);
34 (physical + offset) * scale
35}
36
37#[inline]
40pub fn is_identity(scale: Option<f64>, offset: Option<f64>) -> bool {
41 scale.unwrap_or(1.0) == 1.0 && offset.unwrap_or(0.0) == 0.0
42}
43
44#[cfg(test)]
45mod tests {
46 use super::*;
47
48 #[test]
49 fn no_scale_no_offset_is_identity() {
50 assert_eq!(apply(126.0, None, None), 126.0);
51 assert_eq!(apply(126.0, Some(1.0), Some(0.0)), 126.0);
52 }
53
54 #[test]
55 fn speed_scale_1000() {
56 assert_eq!(apply(3000.0, Some(1000.0), None), 3.0);
58 }
59
60 #[test]
61 fn altitude_scale_5_offset_500() {
62 assert_eq!(apply(10435.0, Some(5.0), Some(500.0)), 1587.0);
64 }
65
66 #[test]
67 fn unapply_round_trips_apply() {
68 let raw = 10435.0;
69 let scale = Some(5.0);
70 let offset = Some(500.0);
71 let physical = apply(raw, scale, offset);
72 assert_eq!(unapply(physical, scale, offset), raw);
73 }
74
75 #[test]
76 fn is_identity_classifies_correctly() {
77 assert!(is_identity(None, None));
78 assert!(is_identity(Some(1.0), Some(0.0)));
79 assert!(!is_identity(Some(1000.0), None));
80 assert!(!is_identity(None, Some(500.0)));
81 }
82}