1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
use super::{CalibrationSource, Result, Scope};
use crate::{error, R64};
use indexmap::IndexMap;
use snafu::ensure;

/// A direction of a slope.
#[derive(PartialEq, Debug)]
pub enum Direction {
    /// Slope inclines.
    Incline,
    /// Slope declines.
    Decline,
}

/// A trait for entities that can be reversed.
pub trait HasReverse {
    /// Reverse the entity.
    fn reversed(&self) -> Self;
}

impl HasReverse for IndexMap<R64, R64> {
    fn reversed(&self) -> Self {
        self.iter()
            .map(|(k, v)| (*v, *k))
            .collect::<IndexMap<R64, R64>>()
    }
}

/// A trait for entities that can have a direction.
pub trait HasDirection {
    /// Determine the direction.
    ///
    /// Will return `None` if no single direction can be determined.
    /// The reason is one of the following:
    /// - Too few points (e.g. zero or one point only in a curve).
    /// - The slope changes (e.g. incline first, then decline) in the
    ///   course of the curve.
    fn determine_direction(&self) -> Option<Direction>;

    /// Check whether a calibration has a direction.
    fn check_direction(
        &self,
        scope: &Scope,
        calibration: &CalibrationSource,
    ) -> Result<()> {
        ensure!(
            self.determine_direction().is_some(),
            error::InvalidCalibrationCurveSlope {
                scope: scope.clone(),
                calibration: calibration.clone(),
            }
        );
        Ok(())
    }
}

impl HasDirection for IndexMap<R64, R64> {
    fn determine_direction(&self) -> Option<Direction> {
        let mut res = None;
        let mut last_item: Option<R64> = None;
        let mut cloned = self.clone();
        cloned.sort_keys();
        for v in cloned.into_iter().map(|(_k, v)| v) {
            if let Some(last_value) = last_item {
                if v == last_value {
                    return None;
                } else if v > last_value {
                    if res == None {
                        res = Some(Direction::Incline);
                    } else if res == Some(Direction::Decline) {
                        return None;
                    }
                } else if v < last_value {
                    if res == None {
                        res = Some(Direction::Decline);
                    } else if res == Some(Direction::Incline) {
                        return None;
                    }
                }
            }
            last_item = Some(v);
        }
        res
    }
}

#[cfg(test)]
mod tests {
    use super::{Direction, HasDirection};
    use crate::R64;
    use indexmap::IndexMap;

    #[test]
    fn determine_direction() {
        let map = [(0f64, 0f64), (1f64, 1f64), (2f64, 2f64)]
            .into_iter()
            .map(|&(x, y)| (R64::from(x), R64::from(y)))
            .collect::<IndexMap<R64, R64>>();
        assert_eq!(map.determine_direction(), Some(Direction::Incline));

        let map = [(0f64, 0f64), (1f64, 1f64), (2f64, 0f64)]
            .into_iter()
            .map(|&(x, y)| (R64::from(x), R64::from(y)))
            .collect::<IndexMap<R64, R64>>();
        assert_eq!(map.determine_direction(), None);

        let map = [(0f64, 1f64), (1f64, 0f64), (2f64, 1f64)]
            .into_iter()
            .map(|&(x, y)| (R64::from(x), R64::from(y)))
            .collect::<IndexMap<R64, R64>>();
        assert_eq!(map.determine_direction(), None);

        let map = [(0f64, 2f64), (1f64, 1f64), (2f64, 0f64)]
            .into_iter()
            .map(|&(x, y)| (R64::from(x), R64::from(y)))
            .collect::<IndexMap<R64, R64>>();
        assert_eq!(map.determine_direction(), Some(Direction::Decline));
    }
}