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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
use std::fmt;
use serde::{Deserialize, Serialize};
use geom::{Angle, Line, PolyLine};
use crate::{
DirectedRoadID, Direction, Intersection, IntersectionID, LaneID, Map, MovementID,
PathConstraints, RestrictionType,
};
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq, PartialOrd, Ord, Serialize, Deserialize)]
pub struct TurnID {
pub parent: IntersectionID,
pub src: LaneID,
pub dst: LaneID,
}
impl fmt::Display for TurnID {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "TurnID({}, {}, {})", self.src, self.dst, self.parent)
}
}
#[derive(Clone, Copy, Debug, Eq, PartialOrd, Ord, PartialEq, Serialize, Deserialize)]
pub enum TurnType {
Crosswalk,
SharedSidewalkCorner,
Straight,
Right,
Left,
UTurn,
UnmarkedCrossing,
}
impl TurnType {
pub fn pedestrian_crossing(self) -> bool {
self == TurnType::Crosswalk || self == TurnType::UnmarkedCrossing
}
}
#[derive(Serialize, Deserialize, Debug, PartialEq, Clone, Copy, PartialOrd)]
pub enum TurnPriority {
Banned,
Yield,
Protected,
}
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
pub struct Turn {
pub id: TurnID,
pub turn_type: TurnType,
pub geom: PolyLine,
}
impl Turn {
pub fn conflicts_with(&self, other: &Turn) -> bool {
if self.turn_type == TurnType::SharedSidewalkCorner
|| other.turn_type == TurnType::SharedSidewalkCorner
{
return false;
}
if self.id == other.id {
return false;
}
if self.between_sidewalks() && other.between_sidewalks() {
return false;
}
if self.geom.first_pt() == other.geom.first_pt() {
return false;
}
if self.geom.last_pt() == other.geom.last_pt() {
return true;
}
self.geom.intersection(&other.geom).is_some()
}
pub fn angle(&self) -> Angle {
if self.geom.points().len() < 3 {
return Angle::ZERO;
}
self.geom
.last_line()
.angle()
.shortest_rotation_towards(self.geom.first_line().angle())
}
pub fn between_sidewalks(&self) -> bool {
self.turn_type == TurnType::SharedSidewalkCorner
|| self.turn_type == TurnType::Crosswalk
|| self.turn_type == TurnType::UnmarkedCrossing
}
pub fn penalty(&self, constraints: PathConstraints, map: &Map) -> (usize, usize, usize) {
let from = map.get_l(self.id.src);
let to = map.get_l(self.id.dst);
let from_idx = {
let mut cnt = 0;
let r = map.get_r(from.id.road);
for (l, lt) in r.children(from.dir).iter().rev() {
if from.lane_type != *lt {
continue;
}
if map
.get_turns_from_lane(*l)
.into_iter()
.any(|t| t.id.dst.road == to.id.road)
{
cnt += 1;
if from.id == *l {
break;
}
}
}
cnt
};
let to_idx = {
let mut cnt = 0;
let r = map.get_r(to.id.road);
for (l, lt) in r.children(to.dir).iter().rev() {
if to.lane_type != *lt {
continue;
}
cnt += 1;
if to.id == *l {
break;
}
}
cnt
};
let lc_cost = ((from_idx as isize) - (to_idx as isize)).abs() as usize;
let lt_cost = if constraints == PathConstraints::Bike {
if to.is_biking() {
0
} else if to.is_bus() {
1
} else {
2
}
} else if constraints == PathConstraints::Bus {
if to.is_bus() {
0
} else {
1
}
} else if to.is_bus() {
3
} else {
0
};
let slow_lane = if to_idx > 1 { 1 } else { 0 };
(lt_cost, lc_cost, slow_lane)
}
pub fn is_crossing_arterial_intersection(&self, map: &Map) -> bool {
use crate::osm::RoadRank;
if !self.turn_type.pedestrian_crossing() {
return false;
}
let intersection = map.get_i(self.id.parent);
intersection.roads.iter().any(|r| {
let rank = map.get_r(*r).get_rank();
rank == RoadRank::Arterial || rank == RoadRank::Highway
})
}
pub(crate) fn permitted_by_lane(&self, map: &Map) -> bool {
if let Some(types) = map
.get_l(self.id.src)
.get_lane_level_turn_restrictions(map.get_parent(self.id.src), false)
{
types.contains(&self.turn_type)
} else {
true
}
}
pub(crate) fn permitted_by_road(&self, i: &Intersection, map: &Map) -> bool {
if self.between_sidewalks() {
return true;
}
let src = map.get_parent(self.id.src);
let dst = self.id.dst.road;
for (restriction, to) in &src.turn_restrictions {
if !i.roads.contains(to) {
continue;
}
match restriction {
RestrictionType::BanTurns => {
if dst == *to {
return false;
}
}
RestrictionType::OnlyAllowTurns => {
if dst != *to {
return false;
}
}
}
}
true
}
pub fn crosswalk_over_road(&self, map: &Map) -> Option<DirectedRoadID> {
if !self.turn_type.pedestrian_crossing() {
return None;
}
if self.id.src.road != self.id.dst.road {
return None;
}
Some(DirectedRoadID {
road: self.id.src.road,
dir: if map.get_r(self.id.src.road).dst_i == self.id.parent {
Direction::Fwd
} else {
Direction::Back
},
})
}
pub fn crosswalk_line(&self) -> Option<Line> {
let pts = self.geom.points();
if pts.len() < 3 {
warn!("Crosswalk {} was squished earlier", self.id);
return None;
}
Line::new(pts[1], pts[2]).ok()
}
}
impl TurnID {
pub fn to_movement(self, map: &Map) -> MovementID {
MovementID {
from: map.get_l(self.src).get_directed_parent(),
to: map.get_l(self.dst).get_directed_parent(),
parent: self.parent,
crosswalk: map.get_l(self.src).is_walkable(),
}
}
}