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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
//! A bin that performs a multi-point calibration on an input value.
//!
//! ```text
//!   ┌────[calibration]────┐
//!  ⇒│input          output│⇒
//!   └─────────────────────┘
//! ```

use super::{
    sink_names_input, source_names_output, BinBuildEnvironment,
    BinDescription, Calculator, CalibrationSource, FetchItem,
    GetCalibration, Item, Iteration, Result, Scope, SinkBin, SinkNames,
    SourceBin, SourceId, SourceNames, SourceSinkBinDescription,
    WriteDotSimple, SINK_INPUT, SOURCE_OUTPUT,
};
use crate::sortable_float::{HasDirection, HasReverse};
use crate::{error, R64};
use indexmap::{IndexMap, IndexSet};

static BIN_TYPE: &str = "calibration";

/// A calibration bin.
#[derive(Debug)]
pub struct Bin {
    scope: Scope,
    source_input: Box<dyn FetchItem>,
    curve: IndexMap<R64, R64>,
    result_output: Item,
}

impl SinkBin for Bin {}

impl SourceBin for Bin {
    fn get_source_data(&self, source: &SourceId) -> Result<Item> {
        if source.id == SOURCE_OUTPUT {
            Ok(self.result_output.clone())
        } else {
            error::MissingSourceName {
                scope: self.scope.clone(),
                name: source.id.to_string(),
                bin_type: BIN_TYPE.to_string(),
            }
            .fail()
        }
    }
}

impl Calculator for Bin {
    fn calculate(&mut self, _iteration: &Iteration) -> Result<()> {
        let input = self.source_input.fetch_item(&self.scope)?;

        self.result_output = if let Ok(sv) = input.to_float() {
            use std::iter::FromIterator;
            let curve =
                std::collections::BTreeMap::from_iter(self.curve.iter());
            let mut before = curve.range(..sv);
            let mut after = curve.range(sv..);
            let (a, b) = match (before.next_back(), after.next()) {
                (None, None) => error::InvalidCalibration {
                    scope: self.scope.clone(),
                }
                .fail()?,
                (Some(b), None) => {
                    let a = before.next_back();
                    if let Some(a) = a {
                        (a, b)
                    } else {
                        error::InvalidCalibration {
                            scope: self.scope.clone(),
                        }
                        .fail()?
                    }
                }
                (None, Some(a)) => {
                    let b = after.next();
                    if let Some(b) = b {
                        (a, b)
                    } else {
                        error::InvalidCalibration {
                            scope: self.scope.clone(),
                        }
                        .fail()?
                    }
                }
                (Some(a), Some(b)) => (a, b),
            };

            let a: (f64, f64) =
                ((*a.0).clone().into(), (*a.1).clone().into());
            let b: (f64, f64) =
                ((*b.0).clone().into(), (*b.1).clone().into());

            calculate_intersection(sv, a, b)
        } else {
            Item::Nothing
        };
        Ok(())
    }
}

fn calculate_intersection(
    input: R64,
    a: (f64, f64),
    b: (f64, f64),
) -> Item {
    let (ax, ay) = a;
    let (bx, by) = b;
    let (dx, dy) = ((bx - ax), (by - ay));

    let offset = input - ax;
    let value = R64::from(ay) + offset / dx * dy;

    Item::from(value)
}

/// Description of the calibration bin.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Description {
    /// The source of the calibration values.
    pub details: CalibrationSource,

    /// Flag to indicate whether the calibration should be calculated
    /// in reverse direction.
    ///
    /// For a reverse calibration, the slope of the calibration curve
    /// must be strictly into one direction, either ascending or
    /// descending.
    pub reversed: bool,
}

impl BinDescription for Description {
    type Bin = Bin;

    fn check_validity(
        &self,
        scope: &Scope,
        get_calibration: &mut dyn GetCalibration,
    ) -> Result<()> {
        let calibration = get_calibration.calibration(&self.details)?;
        if self.reversed {
            calibration.check_direction(scope, &self.details)
        } else {
            Ok(())
        }
    }

    fn bin_type(&self) -> &'static str {
        BIN_TYPE
    }
}

impl SinkNames for Description {
    fn sink_names(&self) -> IndexSet<String> {
        sink_names_input()
    }
}

impl SourceNames for Description {
    fn source_names(&self) -> Result<IndexSet<String>> {
        Ok(source_names_output())
    }
}

impl SourceSinkBinDescription for Description {
    fn build_bin(
        &self,
        scope: &Scope,
        env: &mut dyn BinBuildEnvironment,
    ) -> Result<Self::Bin> {
        let curve = if self.reversed {
            env.calibration(&self.details)?.reversed()
        } else {
            env.calibration(&self.details)?
        };

        Ok(Bin {
            scope: scope.clone(),
            source_input: env.resolve(SINK_INPUT)?,
            curve,
            result_output: Item::Nothing,
        })
    }
}

impl WriteDotSimple for Description {}