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 crate::{osm, DirectedRoadID, LaneID, Map, PathConstraints, Road, RoadID, TurnID};
use abstutil::{deserialize_usize, serialize_usize};
use geom::{Distance, Polygon};
use serde::{Deserialize, Serialize};
use std::collections::BTreeSet;
use std::fmt;

#[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)
    }
}

#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq, Serialize, Deserialize)]
pub enum IntersectionType {
    StopSign,
    TrafficSignal,
    Border,
    Construction,
}

#[derive(Serialize, Deserialize, 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: BTreeSet<TurnID>,
    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?
    pub incoming_lanes: Vec<LaneID>,
    pub outgoing_lanes: Vec<LaneID>,

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

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 get_incoming_lanes<'a>(
        &'a self,
        map: &'a Map,
        constraints: PathConstraints,
    ) -> impl Iterator<Item = LaneID> + 'a {
        self.incoming_lanes
            .iter()
            .filter(move |l| constraints.can_use(map.get_l(**l), map))
            .cloned()
    }

    // 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.iter().copied(), map)
    }

    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(crate) fn get_roads_sorted_by_incoming_angle(&self, all_roads: &Vec<Road>) -> Vec<RoadID> {
        let center = self.polygon.center();
        let mut roads: Vec<RoadID> = self.roads.iter().cloned().collect();
        roads.sort_by_key(|id| {
            let r = &all_roads[id.0];
            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
    }

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

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

    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)
    }
}