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
use std::collections::{BTreeMap, HashMap};
use serde::{Deserialize, Serialize};
use abstutil::{deserialize_btreemap, serialize_btreemap};
use crate::{
osm, Direction, DrivingSide, IntersectionID, LaneID, Map, RoadID, TurnID, TurnPriority,
TurnType,
};
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
pub struct ControlStopSign {
pub id: IntersectionID,
#[serde(
serialize_with = "serialize_btreemap",
deserialize_with = "deserialize_btreemap"
)]
pub roads: BTreeMap<RoadID, RoadWithStopSign>,
}
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
pub struct RoadWithStopSign {
pub lane_closest_to_edge: LaneID,
pub must_stop: bool,
}
impl ControlStopSign {
pub fn new(map: &Map, id: IntersectionID) -> ControlStopSign {
let mut ss = ControlStopSign {
id,
roads: BTreeMap::new(),
};
for r in &map.get_i(id).roads {
let r = map.get_r(*r);
let want_dir = if r.dst_i == id {
Direction::Fwd
} else {
Direction::Back
};
let travel_lanes: Vec<LaneID> = r
.lanes_ltr()
.into_iter()
.filter_map(|(id, dir, lt)| {
if dir == want_dir && lt.is_for_moving_vehicles() {
Some(id)
} else {
None
}
})
.collect();
if !travel_lanes.is_empty() {
let lane_closest_to_edge = if (map.get_config().driving_side == DrivingSide::Right)
== (want_dir == Direction::Fwd)
{
*travel_lanes.last().unwrap()
} else {
travel_lanes[0]
};
ss.roads.insert(
r.id,
RoadWithStopSign {
lane_closest_to_edge,
must_stop: false,
},
);
}
}
if ss.roads.len() <= 2 {
return ss;
}
if map.get_i(id).is_cycleway(map) {
return ss;
}
let mut rank: HashMap<RoadID, (osm::RoadRank, usize)> = HashMap::new();
for r in ss.roads.keys() {
let r = map.get_r(*r);
let priority = if r.is_cycleway() { 0 } else { 1 };
rank.insert(r.id, (r.get_rank(), priority));
}
let mut ranks = rank.values().cloned().collect::<Vec<_>>();
ranks.sort();
ranks.dedup();
ranks.reverse();
for (r, cfg) in ss.roads.iter_mut() {
if ranks.len() == 1 || rank[r] != ranks[0] {
if !map.get_r(*r).is_extremely_short() {
cfg.must_stop = true;
}
}
}
ss
}
pub fn get_priority(&self, turn: TurnID, map: &Map) -> TurnPriority {
match map.get_t(turn).turn_type {
TurnType::SharedSidewalkCorner => TurnPriority::Protected,
TurnType::Crosswalk => TurnPriority::Protected,
_ => {
if self.roads[&map.get_l(turn.src).parent].must_stop {
TurnPriority::Yield
} else {
TurnPriority::Protected
}
}
}
}
pub fn flip_sign(&mut self, r: RoadID) {
let ss = self.roads.get_mut(&r).unwrap();
ss.must_stop = !ss.must_stop;
}
}