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
use std::collections::HashSet;
use abstutil::{Counter, Timer};
use map_model::{
DirectedRoadID, IntersectionID, LaneID, Map, Path, PathConstraints, PathRequest, PathStep,
Pathfinder, Position, RoadID,
};
use crate::{App, Cell, Neighborhood};
pub struct Shortcuts {
pub paths: Vec<Path>,
pub count_per_road: Counter<RoadID>,
pub count_per_intersection: Counter<IntersectionID>,
}
impl Shortcuts {
pub fn quiet_and_total_streets(&self, neighborhood: &Neighborhood) -> (usize, usize) {
let quiet_streets = neighborhood
.orig_perimeter
.interior
.iter()
.filter(|r| self.count_per_road.get(**r) == 0)
.count();
let total_streets = neighborhood.orig_perimeter.interior.len();
(quiet_streets, total_streets)
}
}
pub fn find_shortcuts(app: &App, neighborhood: &Neighborhood, timer: &mut Timer) -> Shortcuts {
let map = &app.map;
let modal_filters = &app.session.modal_filters;
let mut requests = Vec::new();
for cell in &neighborhood.cells {
let entrances = find_entrances(map, neighborhood, cell);
let exits = find_exits(map, neighborhood, cell);
for entrance in &entrances {
for exit in &exits {
if entrance.major_road_name != exit.major_road_name {
requests.push(PathRequest::vehicle(
Position::start(entrance.lane),
Position::end(exit.lane, map),
PathConstraints::Car,
));
}
}
}
}
let mut params = map.routing_params().clone();
modal_filters.update_routing_params(&mut params);
params.avoid_roads.extend(neighborhood.perimeter.clone());
let pathfinder = Pathfinder::new_dijkstra(map, params, vec![PathConstraints::Car], timer);
let paths: Vec<Path> = timer
.parallelize(
"calculate paths between entrances and exits",
requests,
|req| {
pathfinder
.pathfind_v2(req, map)
.and_then(|path| path.into_v1(map).ok())
},
)
.into_iter()
.flatten()
.collect();
let mut count_per_road = Counter::new();
let mut count_per_intersection = Counter::new();
for path in &paths {
for step in path.get_steps() {
match step {
PathStep::Lane(l) => {
if neighborhood.orig_perimeter.interior.contains(&l.road) {
count_per_road.inc(l.road);
}
}
PathStep::Turn(t) => {
if neighborhood.interior_intersections.contains(&t.parent) {
count_per_intersection.inc(t.parent);
}
}
_ => unreachable!(),
}
}
}
Shortcuts {
paths,
count_per_road,
count_per_intersection,
}
}
struct EntryExit {
lane: LaneID,
major_road_name: String,
}
fn find_entrances(map: &Map, neighborhood: &Neighborhood, cell: &Cell) -> Vec<EntryExit> {
let mut entrances = Vec::new();
for i in &cell.borders {
if let Some(major_road_name) = find_major_road_name(map, neighborhood, *i) {
let mut seen: HashSet<DirectedRoadID> = HashSet::new();
for l in map.get_i(*i).get_outgoing_lanes(map, PathConstraints::Car) {
let dr = map.get_l(l).get_directed_parent();
if !seen.contains(&dr) && cell.roads.contains_key(&dr.road) {
entrances.push(EntryExit {
lane: l,
major_road_name: major_road_name.clone(),
});
seen.insert(dr);
}
}
}
}
entrances
}
fn find_exits(map: &Map, neighborhood: &Neighborhood, cell: &Cell) -> Vec<EntryExit> {
let mut exits = Vec::new();
for i in &cell.borders {
if let Some(major_road_name) = find_major_road_name(map, neighborhood, *i) {
let mut seen: HashSet<DirectedRoadID> = HashSet::new();
for l in map.get_i(*i).get_incoming_lanes(map, PathConstraints::Car) {
let dr = map.get_l(l).get_directed_parent();
if !seen.contains(&dr) && cell.roads.contains_key(&dr.road) {
exits.push(EntryExit {
lane: l,
major_road_name: major_road_name.clone(),
});
seen.insert(dr);
}
}
}
}
exits
}
fn find_major_road_name(
map: &Map,
neighborhood: &Neighborhood,
i: IntersectionID,
) -> Option<String> {
let mut names = Vec::new();
for r in &map.get_i(i).roads {
if neighborhood.perimeter.contains(r) {
names.push(map.get_r(*r).get_name(None));
}
}
names.sort();
names.dedup();
if names.len() == 1 {
names.pop()
} else {
None
}
}