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
use std::fmt::Debug;
use std::fs::File;
use std::io::{BufReader, Seek, Read};
use std::iter::IntoIterator;
use std::path::Path;
use Result;
use pof;
use poq;
use point::{Accuracy, Point};
pub trait Source: Debug {
fn source(&mut self) -> Result<Option<Point>>;
}
impl IntoIterator for Box<Source> {
type Item = Point;
type IntoIter = SourceIterator;
fn into_iter(self) -> Self::IntoIter {
SourceIterator { source: self }
}
}
#[derive(Debug)]
pub struct SourceIterator {
source: Box<Source>,
}
impl Iterator for SourceIterator {
type Item = Point;
fn next(&mut self) -> Option<Point> {
self.source.source().unwrap()
}
}
pub trait AccuracySource: Debug {
fn source(&mut self) -> Result<Option<Accuracy>>;
}
impl<R: Debug + Seek + Read> AccuracySource for poq::Reader<R> {
fn source(&mut self) -> Result<Option<Accuracy>> {
self.read_accuracy()
}
}
pub trait FileSource {
fn open_file_source<P: AsRef<Path>>(path: P) -> Result<Box<Source>>;
}
impl FileSource for pof::Reader<BufReader<File>> {
fn open_file_source<P: AsRef<Path>>(path: P) -> Result<Box<Source>> {
Ok(Box::new(try!(pof::Reader::from_path(path))))
}
}
pub trait FileAccuracySource {
fn open_file_accuracy_source<P: AsRef<Path>>(path: P) -> Result<Box<AccuracySource>>;
}
impl FileAccuracySource for poq::Reader<BufReader<File>> {
fn open_file_accuracy_source<P: AsRef<Path>>(path: P) -> Result<Box<AccuracySource>> {
Ok(Box::new(try!(poq::Reader::from_path(path))))
}
}
#[derive(Debug)]
pub struct CombinedSource {
source: Box<Source>,
accuracy_source: Box<AccuracySource>,
accuracies: (Option<Accuracy>, Option<Accuracy>),
}
impl CombinedSource {
pub fn new(source: Box<Source>,
mut accuracy_source: Box<AccuracySource>)
-> Result<CombinedSource> {
let accuracies = (try!(accuracy_source.source()),
try!(accuracy_source.source()));
Ok(CombinedSource {
source: source,
accuracy_source: accuracy_source,
accuracies: accuracies,
})
}
}
impl Source for CombinedSource {
fn source(&mut self) -> Result<Option<Point>> {
let mut point = match try!(self.source.source()) {
Some(point) => point,
None => return Ok(None),
};
if self.accuracies.0.is_none() || self.accuracies.1.is_none() ||
point.time < self.accuracies.0.unwrap().time {
return Ok(Some(point));
}
loop {
if point.time > self.accuracies.1.unwrap().time {
self.accuracies.0 = self.accuracies.1;
self.accuracies.1 = try!(self.accuracy_source.source());
} else {
break;
}
if self.accuracies.1.is_none() {
return Ok(Some(point));
}
}
point.accuracy = Some(self.accuracies
.0
.unwrap()
.interpolate(&self.accuracies.1.unwrap(), point.time));
Ok(Some(point))
}
}
impl IntoIterator for CombinedSource {
type Item = Point;
type IntoIter = CombinedSourceIterator;
fn into_iter(self) -> Self::IntoIter {
CombinedSourceIterator { source: self }
}
}
#[derive(Debug)]
pub struct CombinedSourceIterator {
source: CombinedSource,
}
impl Iterator for CombinedSourceIterator {
type Item = Point;
fn next(&mut self) -> Option<Point> {
self.source.source().unwrap()
}
}
#[cfg(test)]
mod tests {
use super::*;
use pof;
use poq;
#[test]
fn read_pof() {
let source = pof::Reader::open_file_source("data/sbet_mission_1.pof").unwrap();
let points: Vec<_> = source.into_iter().collect();
assert_eq!(1114521, points.len());
}
#[test]
fn read_pof_with_poq() {
let source = pof::Reader::open_file_source("data/sbet_mission_1.pof").unwrap();
let accuracy_source = poq::Reader::open_file_accuracy_source("data/sbet_mission_1.poq")
.unwrap();
let accuracies: Vec<_> = CombinedSource::new(source, accuracy_source)
.unwrap()
.into_iter()
.take(20)
.collect();
assert_eq!(20, accuracies.len());
}
}