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
use std::collections::{BTreeSet, HashMap};
use geom::Distance;
use map_model::osm::RoadRank;
use map_model::{IntersectionID, Map, PathConstraints, RoadID};
use crate::ltn::{Neighborhood, RatRun};
impl Neighborhood {
pub fn from_road(map: &Map, start: RoadID) -> Neighborhood {
assert!(Neighborhood::is_interior_road(start, map));
let mut interior = BTreeSet::new();
let mut perimeter = BTreeSet::new();
let mut borders = BTreeSet::new();
let mut visited = BTreeSet::new();
let mut queue = vec![start];
interior.insert(start);
while !queue.is_empty() {
let current = map.get_r(queue.pop().unwrap());
if visited.contains(¤t.id) {
continue;
}
visited.insert(current.id);
for i in [current.src_i, current.dst_i] {
let (minor, major): (Vec<&RoadID>, Vec<&RoadID>) = map
.get_i(i)
.roads
.iter()
.partition(|r| Neighborhood::is_interior_road(**r, map));
if major.is_empty() {
for r in minor {
interior.insert(*r);
queue.push(*r);
}
} else {
borders.insert(i);
perimeter.extend(major);
}
}
}
Neighborhood {
interior,
perimeter,
borders,
modal_filters: BTreeSet::new(),
rat_runs: Vec::new(),
}
}
pub fn calculate_rat_runs(&mut self, map: &Map) {
self.rat_runs = self
.borders
.iter()
.flat_map(|i| self.rat_run_from(map, *i))
.collect();
self.rat_runs
.sort_by(|a, b| a.length_ratio.partial_cmp(&b.length_ratio).unwrap());
}
pub fn toggle_modal_filter(&mut self, map: &Map, r: RoadID) {
if self.modal_filters.contains(&r) {
self.modal_filters.remove(&r);
} else {
self.modal_filters.insert(r);
}
self.calculate_rat_runs(map);
}
pub fn is_interior_road(r: RoadID, map: &Map) -> bool {
let road = map.get_r(r);
road.get_rank() == RoadRank::Local
&& road
.lanes
.iter()
.any(|l| PathConstraints::Car.can_use(l, map))
}
fn rat_run_from(&self, map: &Map, start: IntersectionID) -> Option<RatRun> {
let mut back_refs = HashMap::new();
let mut queue = vec![start];
while !queue.is_empty() {
let current = queue.pop().unwrap();
if current != start && self.borders.contains(¤t) {
let mut at = current;
let mut path = vec![at];
while let Some(prev) = back_refs.remove(&at) {
path.push(prev);
at = prev;
}
path.push(start);
path.reverse();
return Some(RatRun::new(path, map));
}
for r in &map.get_i(current).roads {
let next = map.get_r(*r).other_endpt(current);
if !self.interior.contains(r)
|| self.modal_filters.contains(r)
|| back_refs.contains_key(&next)
{
continue;
}
back_refs.insert(next, current);
queue.push(next);
}
}
None
}
}
impl RatRun {
fn new(path: Vec<IntersectionID>, map: &Map) -> RatRun {
let mut run = RatRun {
path,
length_ratio: 1.0,
};
if let Some((roads, _)) = map.simple_path_btwn(run.path[0], *run.path.last().unwrap()) {
let shortest: Distance = roads.into_iter().map(|r| map.get_r(r).length()).sum();
let this_path: Distance = run.roads(map).map(|r| r.length()).sum();
run.length_ratio = this_path / shortest;
}
run
}
}