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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
use anyhow::Result;
use serde::{Deserialize, Serialize};
use geom::Duration;
use crate::pathfind::uber_turns::UberTurnV2;
use crate::{
DirectedRoadID, Map, MovementID, Path, PathConstraints, PathRequest, PathStep, TurnID, UberTurn,
};
#[derive(Clone, Debug, Serialize, Deserialize)]
pub enum PathStepV2 {
Along(DirectedRoadID),
Contraflow(DirectedRoadID),
Movement(MovementID),
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct PathV2 {
steps: Vec<PathStepV2>,
req: PathRequest,
cost: Duration,
uber_turns: Vec<UberTurnV2>,
}
impl PathV2 {
pub(crate) fn new(
steps: Vec<PathStepV2>,
req: PathRequest,
cost: Duration,
uber_turns: Vec<UberTurnV2>,
) -> PathV2 {
PathV2 {
steps,
req,
cost,
uber_turns,
}
}
pub(crate) fn from_roads(
mut roads: Vec<DirectedRoadID>,
req: PathRequest,
cost: Duration,
uber_turns: Vec<UberTurnV2>,
map: &Map,
) -> PathV2 {
let mut steps = Vec::new();
for pair in roads.windows(2) {
steps.push(PathStepV2::Along(pair[0]));
steps.push(PathStepV2::Movement(MovementID {
from: pair[0],
to: pair[1],
parent: pair[0].dst_i(map),
crosswalk: false,
}));
}
steps.push(PathStepV2::Along(roads.pop().unwrap()));
PathV2::new(steps, req, cost, uber_turns)
}
pub fn get_req(&self) -> &PathRequest {
&self.req
}
pub fn get_steps(&self) -> &Vec<PathStepV2> {
&self.steps
}
pub fn get_cost(&self) -> Duration {
self.cost
}
pub fn into_v1(self, map: &Map) -> Result<Path> {
if self.req.constraints == PathConstraints::Pedestrian {
return self.into_v1_walking(map);
}
let mut graph = petgraph::graphmap::DiGraphMap::new();
for step in &self.steps {
if let PathStepV2::Movement(mvmnt) = step {
for src in mvmnt.from.lanes(self.req.constraints, map) {
for dst in mvmnt.to.lanes(self.req.constraints, map) {
let turn = TurnID {
parent: map.get_l(src).dst_i,
src,
dst,
};
if map.maybe_get_t(turn).is_some() {
graph.add_edge(src, dst, turn);
}
}
}
}
}
match petgraph::algo::astar(
&graph,
self.req.start.lane(),
|l| l == self.req.end.lane(),
|(_, _, t)| {
let (lt, lc, slow_lane) = map.get_t(*t).penalty(map);
let mut extra_penalty = lt + lc;
if self.req.constraints == PathConstraints::Bike {
extra_penalty = slow_lane;
}
let base = 1;
base + extra_penalty
},
|_| 0,
) {
Some((_, path)) => {
let mut steps = Vec::new();
for pair in path.windows(2) {
steps.push(PathStep::Lane(pair[0]));
steps.push(PathStep::Turn(TurnID {
parent: map.get_l(pair[0]).dst_i,
src: pair[0],
dst: pair[1],
}));
}
steps.push(PathStep::Lane(self.req.end.lane()));
assert_eq!(steps[0], PathStep::Lane(self.req.start.lane()));
let uber_turns = find_uber_turns(&steps, map, self.uber_turns);
Ok(Path::new(map, steps, self.req, uber_turns))
}
None => bail!(
"Can't transform a road-based path to a lane-based path for {}",
self.req
),
}
}
fn into_v1_walking(self, map: &Map) -> Result<Path> {
let mut steps = Vec::new();
for step in self.steps {
steps.push(match step {
PathStepV2::Along(r) => PathStep::Lane(r.must_get_sidewalk(map)),
PathStepV2::Contraflow(r) => PathStep::ContraflowLane(r.must_get_sidewalk(map)),
PathStepV2::Movement(mvmnt) => PathStep::Turn(TurnID {
src: mvmnt.from.must_get_sidewalk(map),
dst: mvmnt.to.must_get_sidewalk(map),
parent: mvmnt.parent,
}),
});
}
Ok(Path::new(map, steps, self.req, Vec::new()))
}
}
fn find_uber_turns(
steps: &[PathStep],
map: &Map,
mut uber_turns_v2: Vec<UberTurnV2>,
) -> Vec<UberTurn> {
let num_uts = uber_turns_v2.len();
let mut result = Vec::new();
let mut current_ut = Vec::new();
for step in steps {
if uber_turns_v2.is_empty() {
break;
}
if let PathStep::Turn(t) = step {
if current_ut.is_empty()
&& uber_turns_v2[0].path[0].from == map.get_l(t.src).get_directed_parent()
{
current_ut.push(*t);
}
if !current_ut.is_empty() {
if current_ut.last() != Some(t) {
current_ut.push(*t);
}
if uber_turns_v2[0].path[0].to == map.get_l(t.dst).get_directed_parent() {
result.push(UberTurn {
path: current_ut.drain(..).collect(),
});
uber_turns_v2.remove(0);
}
}
}
}
assert!(current_ut.is_empty());
assert_eq!(num_uts, result.len());
result
}