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
use serde::{Deserialize, Serialize};
use std::cmp::Ordering;
use std::collections::{BinaryHeap, HashMap, HashSet};
use petgraph::graphmap::DiGraphMap;
use geom::Duration;
pub use self::walking::{all_walking_costs_from, WalkingOptions};
use crate::pathfind::zone_cost;
pub use crate::pathfind::{vehicle_cost, WalkingNode};
use crate::{BuildingID, DirectedRoadID, IntersectionID, LaneID, Map, PathConstraints};
mod walking;
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq, PartialOrd, Ord, Serialize, Deserialize)]
pub enum Spot {
Building(BuildingID),
Border(IntersectionID),
DirectedRoad(DirectedRoadID),
}
pub fn find_scc(map: &Map, constraints: PathConstraints) -> (HashSet<LaneID>, HashSet<LaneID>) {
let mut graph = DiGraphMap::new();
for turn in map.all_turns() {
if constraints.can_use(map.get_l(turn.id.src), map)
&& constraints.can_use(map.get_l(turn.id.dst), map)
{
graph.add_edge(turn.id.src, turn.id.dst, 1);
}
}
let components = petgraph::algo::kosaraju_scc(&graph);
if components.is_empty() {
return (HashSet::new(), HashSet::new());
}
let largest_group: HashSet<LaneID> = components
.into_iter()
.max_by_key(|c| c.len())
.unwrap()
.into_iter()
.collect();
let disconnected = map
.all_lanes()
.filter_map(|l| {
if constraints.can_use(l, map) && !largest_group.contains(&l.id) {
Some(l.id)
} else {
None
}
})
.collect();
(largest_group, disconnected)
}
pub fn all_vehicle_costs_from(
map: &Map,
starts: Vec<Spot>,
time_limit: Duration,
constraints: PathConstraints,
) -> HashMap<BuildingID, Duration> {
assert!(constraints != PathConstraints::Pedestrian);
let mut bldg_to_road = HashMap::new();
for b in map.all_buildings() {
if constraints == PathConstraints::Car {
if let Some((pos, _)) = b.driving_connection(map) {
bldg_to_road.insert(b.id, map.get_l(pos.lane()).get_directed_parent());
}
} else if constraints == PathConstraints::Bike {
if let Some((pos, _)) = b.biking_connection(map) {
bldg_to_road.insert(b.id, map.get_l(pos.lane()).get_directed_parent());
}
}
}
let mut queue: BinaryHeap<Item> = BinaryHeap::new();
for spot in starts {
match spot {
Spot::Building(b_id) => {
if let Some(start_road) = bldg_to_road.get(&b_id).cloned() {
queue.push(Item {
cost: Duration::ZERO,
node: start_road,
});
}
}
Spot::Border(i_id) => {
let intersection = map.get_i(i_id);
let incoming_lanes = intersection.get_incoming_lanes(map, constraints);
let mut outgoing_lanes = intersection.get_outgoing_lanes(map, constraints);
let mut all_lanes = incoming_lanes;
all_lanes.append(&mut outgoing_lanes);
for l_id in all_lanes {
queue.push(Item {
cost: Duration::ZERO,
node: map.get_l(l_id).get_directed_parent(),
});
}
}
Spot::DirectedRoad(dr) => {
queue.push(Item {
cost: Duration::ZERO,
node: dr,
});
}
}
}
let mut cost_per_node: HashMap<DirectedRoadID, Duration> = HashMap::new();
while let Some(current) = queue.pop() {
if cost_per_node.contains_key(¤t.node) {
continue;
}
if current.cost > time_limit {
continue;
}
cost_per_node.insert(current.node, current.cost);
for mvmnt in map.get_movements_for(current.node, constraints) {
queue.push(Item {
cost: current.cost
+ vehicle_cost(mvmnt.from, mvmnt, constraints, map.routing_params(), map)
+ zone_cost(mvmnt, constraints, map),
node: mvmnt.to,
});
}
}
let mut results = HashMap::new();
for (b, road) in bldg_to_road {
if let Some(duration) = cost_per_node.get(&road).cloned() {
results.insert(b, duration);
}
}
results
}
#[derive(PartialEq, Eq)]
struct Item {
cost: Duration,
node: DirectedRoadID,
}
impl PartialOrd for Item {
fn partial_cmp(&self, other: &Item) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl Ord for Item {
fn cmp(&self, other: &Item) -> Ordering {
let ord = other.cost.cmp(&self.cost);
if ord != Ordering::Equal {
return ord;
}
self.node.cmp(&other.node)
}
}