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
use std::collections::{BTreeMap, BTreeSet};
use std::convert::TryFrom;
use std::fmt;

use serde::{Deserialize, Serialize};

use abstutil::{deserialize_usize, serialize_usize};
use geom::{Distance, Polygon};
use raw_map::IntersectionType;

use crate::{
    osm, CompressedMovementID, DirectedRoadID, LaneID, Map, Movement, MovementID, PathConstraints,
    Road, RoadID, RoadSideID, SideOfRoad, Turn, TurnID,
};

#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq, PartialOrd, Ord, Serialize, Deserialize)]
pub struct IntersectionID(
    #[serde(
        serialize_with = "serialize_usize",
        deserialize_with = "deserialize_usize"
    )]
    pub usize,
);

impl fmt::Display for IntersectionID {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "Intersection #{}", self.0)
    }
}

/// An intersection connects roads. Most have >2 roads and are controlled by stop signs or traffic
/// signals. Roads that lead to the boundary of the map end at border intersections, with only that
/// one road attached.
#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct Intersection {
    pub id: IntersectionID,
    /// This needs to be in clockwise orientation, or later rendering of sidewalk corners breaks.
    pub polygon: Polygon,
    pub turns: Vec<Turn>,
    pub elevation: Distance,

    pub intersection_type: IntersectionType,
    pub orig_id: osm::NodeID,

    /// Note that a lane may belong to both incoming_lanes and outgoing_lanes.
    // TODO narrow down when and why. is it just sidewalks in weird cases?
    // TODO Change to BTreeSet, or otherwise emphasize to callers that the order of these isn't
    // meaningful
    pub incoming_lanes: Vec<LaneID>,
    pub outgoing_lanes: Vec<LaneID>,

    // TODO Maybe DirectedRoadIDs
    pub roads: BTreeSet<RoadID>,

    /// Was a short road adjacent to this intersection merged?
    pub merged: bool,
    // These increase the map file size, so instead, just use `recalculate_all_movements` after
    // deserializing.
    #[serde(skip_serializing, skip_deserializing)]
    pub movements: BTreeMap<MovementID, Movement>,
}

impl Intersection {
    pub fn is_border(&self) -> bool {
        self.intersection_type == IntersectionType::Border
    }
    pub fn is_incoming_border(&self) -> bool {
        self.intersection_type == IntersectionType::Border && !self.outgoing_lanes.is_empty()
    }
    pub fn is_outgoing_border(&self) -> bool {
        self.intersection_type == IntersectionType::Border && !self.incoming_lanes.is_empty()
    }

    pub fn is_closed(&self) -> bool {
        self.intersection_type == IntersectionType::Construction
    }

    pub fn is_stop_sign(&self) -> bool {
        self.intersection_type == IntersectionType::StopSign
    }

    pub fn is_traffic_signal(&self) -> bool {
        self.intersection_type == IntersectionType::TrafficSignal
    }

    pub fn is_light_rail(&self, map: &Map) -> bool {
        self.roads.iter().all(|r| map.get_r(*r).is_light_rail())
    }

    pub fn is_private(&self, map: &Map) -> bool {
        self.roads.iter().all(|r| map.get_r(*r).is_private())
    }

    pub fn is_footway(&self, map: &Map) -> bool {
        self.roads.iter().all(|r| map.get_r(*r).is_footway())
    }

    pub fn is_cycleway(&self, map: &Map) -> bool {
        self.roads.iter().all(|r| map.get_r(*r).is_cycleway())
    }

    /// Does this intersection only connect two road segments? Then usually, the intersection only
    /// exists to mark the road name or lanes changing.
    pub fn is_degenerate(&self) -> bool {
        self.roads.len() == 2
    }

    /// Does this intersection connect to only a single road segment?
    pub fn is_deadend(&self) -> bool {
        self.roads.len() == 1
    }

    pub fn get_incoming_lanes(&self, map: &Map, constraints: PathConstraints) -> Vec<LaneID> {
        self.incoming_lanes
            .iter()
            .filter(move |l| constraints.can_use(map.get_l(**l), map))
            .cloned()
            .collect()
    }

    /// Strict for bikes. If there are bike lanes, not allowed to use other lanes.
    pub fn get_outgoing_lanes(&self, map: &Map, constraints: PathConstraints) -> Vec<LaneID> {
        constraints.filter_lanes(self.outgoing_lanes.clone(), map)
    }

    /// Higher numbers get drawn on top
    pub fn get_zorder(&self, map: &Map) -> isize {
        // TODO Not sure min makes sense -- what about a 1 and a 0? Prefer the nonzeros. If there's
        // a -1 and a 1... need to see it to know what to do.
        self.roads
            .iter()
            .map(|r| map.get_r(*r).zorder)
            .min()
            .unwrap()
    }

    pub fn get_rank(&self, map: &Map) -> osm::RoadRank {
        self.roads
            .iter()
            .map(|r| map.get_r(*r).get_rank())
            .max()
            .unwrap()
    }

    pub fn get_roads_sorted_by_incoming_angle(&self, map: &Map) -> Vec<RoadID> {
        let center = self.polygon.center();
        let mut roads: Vec<RoadID> = self.roads.iter().cloned().collect();
        roads.sort_by_key(|id| {
            let r = map.get_r(*id);
            let endpt = if r.src_i == self.id {
                r.center_pts.first_pt()
            } else if r.dst_i == self.id {
                r.center_pts.last_pt()
            } else {
                unreachable!();
            };
            endpt.angle_to(center).normalized_degrees() as i64
        });
        roads
    }

    // TODO walking_turns_v2 and the intersection geometry algorithm also do something like this.
    // Refactor?
    pub fn get_road_sides_sorted_by_incoming_angle(&self, map: &Map) -> Vec<RoadSideID> {
        let mut sides = Vec::new();
        for r in self.get_roads_sorted_by_incoming_angle(map) {
            let r = map.get_r(r);
            if r.dst_i == self.id {
                sides.push(RoadSideID {
                    road: r.id,
                    side: SideOfRoad::Right,
                });
                sides.push(RoadSideID {
                    road: r.id,
                    side: SideOfRoad::Left,
                });
            } else {
                sides.push(RoadSideID {
                    road: r.id,
                    side: SideOfRoad::Left,
                });
                sides.push(RoadSideID {
                    road: r.id,
                    side: SideOfRoad::Right,
                });
            }
        }
        sides
    }

    /// Return all incoming roads to an intersection, sorted by angle. This skips one-way roads
    /// outbound from the intersection, since no turns originate from those anyway. This allows
    /// heuristics for a 3-way intersection to not care if one of the roads happens to be a dual
    /// carriageway (split into two one-ways).
    pub fn get_sorted_incoming_roads(&self, map: &Map) -> Vec<RoadID> {
        let mut roads = Vec::new();
        for r in self.get_roads_sorted_by_incoming_angle(map) {
            if !map.get_r(r).incoming_lanes(self.id).is_empty() {
                roads.push(r);
            }
        }
        roads
    }

    pub fn some_outgoing_road(&self, map: &Map) -> Option<DirectedRoadID> {
        self.outgoing_lanes
            .get(0)
            .map(|l| map.get_l(*l).get_directed_parent())
    }

    pub fn some_incoming_road(&self, map: &Map) -> Option<DirectedRoadID> {
        self.incoming_lanes
            .get(0)
            .map(|l| map.get_l(*l).get_directed_parent())
    }

    pub fn name(&self, lang: Option<&String>, map: &Map) -> String {
        let road_names = self
            .roads
            .iter()
            .map(|r| map.get_r(*r).get_name(lang))
            .collect::<BTreeSet<_>>();
        abstutil::plain_list_names(road_names)
    }

    /// Don't call for SharedSidewalkCorners
    pub fn turn_to_movement(&self, turn: TurnID) -> (MovementID, CompressedMovementID) {
        for (idx, m) in self.movements.values().enumerate() {
            if m.members.contains(&turn) {
                return (
                    m.id,
                    CompressedMovementID {
                        i: self.id,
                        idx: u8::try_from(idx).unwrap(),
                    },
                );
            }
        }

        panic!(
            "{} doesn't belong to any movements in {} or is a SharedSidewalkCorner maybe",
            turn, self.id
        )
    }

    pub fn find_road_between<'a>(&self, other: IntersectionID, map: &'a Map) -> Option<&'a Road> {
        for r in &self.roads {
            let road = map.get_r(*r);
            if road.other_endpt(self.id) == other {
                return Some(road);
            }
        }
        None
    }
}