-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathroute.rs
208 lines (185 loc) · 7.12 KB
/
route.rs
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
use std::collections::HashMap;
use derivative::Derivative;
use serde::Deserialize;
use serde::Serialize;
use super::OSRDIdentified;
use super::ObjectType;
use crate::infra_cache::Cache;
use crate::infra_cache::Graph;
use crate::infra_cache::InfraCache;
use crate::infra_cache::ObjectCache;
use editoast_common::Identifier;
use editoast_schemas::infra::Direction;
use editoast_schemas::infra::DirectionalTrackRange;
use editoast_schemas::infra::Endpoint;
use editoast_schemas::infra::TrackEndpoint;
use editoast_schemas::infra::Waypoint;
use editoast_schemas::primitives::OSRDTyped;
#[derive(Debug, Derivative, Clone, Deserialize, Serialize, PartialEq, Eq)]
#[serde(deny_unknown_fields)]
#[derivative(Default)]
pub struct Route {
pub id: Identifier,
pub entry_point: Waypoint,
#[derivative(Default(value = "Direction::StartToStop"))]
pub entry_point_direction: Direction,
pub exit_point: Waypoint,
pub release_detectors: Vec<Identifier>,
pub switches_directions: HashMap<Identifier, Identifier>,
}
impl OSRDTyped for Route {
fn get_type() -> ObjectType {
ObjectType::Route
}
}
impl OSRDIdentified for Route {
fn get_id(&self) -> &String {
&self.id
}
}
impl Cache for Route {
fn get_track_referenced_id(&self) -> Vec<&String> {
// We don't have a layer linked to this object yet.
// So we don't need to keep track of the referenced tracks.
vec![]
}
fn get_object_cache(&self) -> ObjectCache {
ObjectCache::Route(self.clone())
}
}
#[derive(Debug, Clone)]
pub struct RoutePath {
pub track_ranges: Vec<DirectionalTrackRange>,
pub switches_directions: HashMap<Identifier, Identifier>,
}
impl Route {
/// Return the track and position of a waypoint
fn get_waypoint_location<'a>(
waypoint: &Waypoint,
infra_cache: &'a InfraCache,
) -> Option<(&'a String, f64)> {
if waypoint.is_detector() {
let detector = infra_cache.detectors().get(waypoint.get_id())?;
let detector = detector.unwrap_detector();
Some((&detector.track, detector.position))
} else {
let bs = infra_cache.buffer_stops().get(waypoint.get_id())?;
let bs = bs.unwrap_buffer_stop();
Some((&bs.track, bs.position))
}
}
/// Compute the track ranges through which the route passes.
/// If the path cannot be computed (e.g. invalid topology), returns None.
pub fn compute_track_ranges(
&self,
infra_cache: &InfraCache,
graph: &Graph,
) -> Option<RoutePath> {
// Check if entry and exit points are the same
if self.entry_point == self.exit_point {
return None;
}
let mut cur_dir = self.entry_point_direction;
let (cur_track, mut cur_offset) =
Self::get_waypoint_location(&self.entry_point, infra_cache)?;
let (exit_track, exit_offset) = Self::get_waypoint_location(&self.exit_point, infra_cache)?;
// Check that the track exists
let mut cur_track = infra_cache
.track_sections()
.get(cur_track)?
.unwrap_track_section();
// Save track ranges and used switches
let mut track_ranges = vec![];
let mut used_switches = HashMap::new();
// Check path validity
loop {
let cur_track_id = cur_track.get_id();
// Add track range
let end_offset = if cur_track_id == exit_track {
exit_offset
} else if cur_dir == Direction::StartToStop {
cur_track.length
} else {
0.
};
track_ranges.push(DirectionalTrackRange::new(
cur_track_id.clone(),
cur_offset.min(end_offset),
cur_offset.max(end_offset),
cur_dir,
));
// Search for the exit_point
if cur_track_id == exit_track {
if (cur_dir == Direction::StartToStop && cur_offset > exit_offset)
|| (cur_dir == Direction::StopToStart && cur_offset < exit_offset)
{
return None;
}
break;
}
// Search for the next track section
let endpoint = TrackEndpoint::from_track_and_direction(cur_track_id, cur_dir);
// No neighbour found
if !graph.has_neighbour(&endpoint) {
return None;
}
let switch = graph.get_switch(&endpoint)?;
let switch_id = switch.get_id();
// Check we found the switch in the route
let group = self.switches_directions.get(&switch_id.clone().into())?;
used_switches.insert(switch_id.clone().into(), group.clone());
let next_endpoint = graph.get_neighbour(&endpoint, group)?;
// Update current track section, offset and direction
cur_track = infra_cache
.track_sections()
.get(&next_endpoint.track.0)?
.unwrap_track_section();
(cur_dir, cur_offset) = match next_endpoint.endpoint {
Endpoint::Begin => (Direction::StartToStop, 0.),
Endpoint::End => (Direction::StopToStart, cur_track.length),
};
}
Some(RoutePath {
track_ranges,
switches_directions: used_switches,
})
}
}
#[cfg(test)]
mod test {
use super::Route;
use crate::infra_cache::tests::create_small_infra_cache;
use crate::infra_cache::Graph;
#[test]
fn test_compute_track_ranges_1() {
let infra_cache = create_small_infra_cache();
let graph = Graph::load(&infra_cache);
let r1 = infra_cache.routes().get("R1").unwrap().unwrap_route();
let path = Route::compute_track_ranges(r1, &infra_cache, &graph).unwrap();
assert_eq!(path.track_ranges.len(), 2);
assert_eq!(path.track_ranges[0].track, "A".into());
assert_eq!(path.track_ranges[0].begin, 20.);
assert_eq!(path.track_ranges[0].end, 500.);
assert_eq!(path.track_ranges[1].track, "B".into());
assert_eq!(path.track_ranges[1].begin, 0.);
assert_eq!(path.track_ranges[1].end, 250.);
assert_eq!(path.switches_directions.len(), 1);
assert!(path.switches_directions.contains_key(&"link".into()));
}
#[test]
fn test_compute_track_ranges_2() {
let infra_cache = create_small_infra_cache();
let graph = Graph::load(&infra_cache);
let r1 = infra_cache.routes().get("R2").unwrap().unwrap_route();
let path = Route::compute_track_ranges(r1, &infra_cache, &graph).unwrap();
assert_eq!(path.track_ranges.len(), 2);
assert_eq!(path.track_ranges[0].track, "B".into());
assert_eq!(path.track_ranges[0].begin, 250.);
assert_eq!(path.track_ranges[0].end, 500.);
assert_eq!(path.track_ranges[1].track, "C".into());
assert_eq!(path.track_ranges[1].begin, 0.);
assert_eq!(path.track_ranges[1].end, 480.);
assert_eq!(path.switches_directions.len(), 1);
assert!(path.switches_directions.contains_key(&"switch".into()));
}
}