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
use {Error, Result};
use source::Source;
use point::Point;
#[derive(Debug)]
pub struct Interpolator {
index: usize,
source: Box<Source>,
points: Vec<Point>,
}
impl Interpolator {
pub fn new(mut source: Box<Source>) -> Result<Interpolator> {
let mut points = Vec::with_capacity(2);
for _ in 0..2 {
points.push(match try!(source.source()) {
Some(point) => point,
None => {
return Err(Error::Extrapolation("Source must have at least two points for \
interpolation"
.to_string()))
}
});
}
Ok(Interpolator {
points: points,
source: source,
index: 1,
})
}
pub fn interpolate(&mut self, time: f64) -> Result<Point> {
loop {
assert!(self.index != 0 && self.index != self.points.len());
if time < self.points[self.index - 1].time {
if self.index == 1 {
return Err(Error::Extrapolation(format!("Time {} is below the minimum time \
of this source",
time)));
} else {
self.index -= 1;
}
} else if time > self.points[self.index].time {
if self.index < self.points.len() - 1 {
self.index += 1;
} else {
match try!(self.source.source()) {
Some(point) => {
self.points.push(point);
self.index += 1;
}
None => {
return Err(Error::Extrapolation(format!("Time {} is above the \
maximum time of this source",
time)))
}
}
}
} else {
break;
}
}
Ok(self.points[self.index - 1].interpolate(&self.points[self.index], time))
}
}
#[cfg(test)]
mod tests {
use super::*;
use sbet;
#[test]
fn interp_sbet() {
let mut interpolator = Interpolator::new(Box::new(sbet::Reader::from_path("data/2-point\
s.sbet")
.unwrap()))
.unwrap();
let time = 1.516310048360710e5;
let point = interpolator.interpolate(time).unwrap();
assert_eq!(time, point.time);
assert!(interpolator.interpolate(0.0).is_err());
}
}