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
use std::collections::HashSet;

use abstutil::{Counter, Timer};
use map_model::{
    DirectedRoadID, IntersectionID, LaneID, Map, Path, PathConstraints, PathRequest, PathStep,
    Position, RoadID,
};

use super::{Cell, ModalFilters, Neighborhood};

pub struct RatRuns {
    pub paths: Vec<Path>,
    pub count_per_road: Counter<RoadID>,
    pub count_per_intersection: Counter<IntersectionID>,
}

pub fn find_rat_runs(
    map: &Map,
    neighborhood: &Neighborhood,
    modal_filters: &ModalFilters,
    timer: &mut Timer,
) -> RatRuns {
    // The overall approach: look for all possible paths from an entrance to an exit, only if they
    // connect to different major roads.
    //
    // But an entrance and exit to _what_? If we try to route from the entrance to one cell to the
    // exit of another, then the route will make strange U-turns and probably use the perimeter. By
    // definition, two cells aren't reachable without using the perimeter. So restrict our search
    // to pairs of entrances/exits in the _same_ cell.
    let mut requests = Vec::new();

    for cell in &neighborhood.cells {
        let entrances = find_entrances(map, neighborhood, cell);
        let exits = find_exits(map, neighborhood, cell);

        for entrance in &entrances {
            for exit in &exits {
                if entrance.major_road_name != exit.major_road_name {
                    requests.push(PathRequest::vehicle(
                        Position::start(entrance.lane),
                        Position::end(exit.lane, map),
                        PathConstraints::Car,
                    ));
                }
            }
        }
    }

    let mut params = map.routing_params().clone();
    modal_filters.update_routing_params(&mut params);
    let cache_custom = true;
    let mut paths: Vec<Path> = timer
        .parallelize(
            "calculate paths between entrances and exits",
            requests,
            |req| map.pathfind_with_params(req, &params, cache_custom),
        )
        .into_iter()
        .flatten()
        .collect();

    // update_routing_params heavily penalizes crossing modal filters, but it doesn't prevent it
    // completely! So strip out paths that were forced to cross a filter.
    paths.retain(|path| {
        !path.get_steps().iter().any(|step| match step {
            PathStep::Lane(l) => modal_filters.roads.contains_key(&l.road),
            PathStep::Turn(t) => !modal_filters.allows_turn(*t),
            // Car paths don't make contraflow movements
            _ => unreachable!(),
        })
    });

    // TODO Rank the likeliness of each rat run by
    // 1) Calculating a path between similar start/endpoints -- travelling along the perimeter,
    //    starting and ending on a specific road that makes sense. (We have to pick the 'direction'
    //    along the perimeter roads that's sensible.)
    // 2) Comparing that time to the time for cutting through

    // How many rat-runs pass through each street?
    let mut count_per_road = Counter::new();
    let mut count_per_intersection = Counter::new();
    for path in &paths {
        for step in path.get_steps() {
            match step {
                PathStep::Lane(l) => {
                    if neighborhood.orig_perimeter.interior.contains(&l.road) {
                        count_per_road.inc(l.road);
                    }
                }
                PathStep::Turn(t) => {
                    if neighborhood.interior_intersections.contains(&t.parent) {
                        count_per_intersection.inc(t.parent);
                    }
                }
                // Car paths don't make contraflow movements
                _ => unreachable!(),
            }
        }
    }

    RatRuns {
        paths,
        count_per_road,
        count_per_intersection,
    }
}

struct EntryExit {
    // TODO Really this is a DirectedRoadID, but since pathfinding later needs to know lanes, just
    // use this
    lane: LaneID,
    major_road_name: String,
}

fn find_entrances(map: &Map, neighborhood: &Neighborhood, cell: &Cell) -> Vec<EntryExit> {
    let mut entrances = Vec::new();
    for i in &cell.borders {
        if let Some(major_road_name) = find_major_road_name(map, neighborhood, *i) {
            let mut seen: HashSet<DirectedRoadID> = HashSet::new();
            for l in map.get_i(*i).get_outgoing_lanes(map, PathConstraints::Car) {
                let dr = map.get_l(l).get_directed_parent();
                if !seen.contains(&dr) && cell.roads.contains_key(&dr.road) {
                    entrances.push(EntryExit {
                        lane: l,
                        major_road_name: major_road_name.clone(),
                    });
                    seen.insert(dr);
                }
            }
        }
    }
    entrances
}

fn find_exits(map: &Map, neighborhood: &Neighborhood, cell: &Cell) -> Vec<EntryExit> {
    let mut exits = Vec::new();
    for i in &cell.borders {
        if let Some(major_road_name) = find_major_road_name(map, neighborhood, *i) {
            let mut seen: HashSet<DirectedRoadID> = HashSet::new();
            for l in map.get_i(*i).get_incoming_lanes(map, PathConstraints::Car) {
                let dr = map.get_l(l).get_directed_parent();
                if !seen.contains(&dr) && cell.roads.contains_key(&dr.road) {
                    exits.push(EntryExit {
                        lane: l,
                        major_road_name: major_road_name.clone(),
                    });
                    seen.insert(dr);
                }
            }
        }
    }
    exits
}

fn find_major_road_name(
    map: &Map,
    neighborhood: &Neighborhood,
    i: IntersectionID,
) -> Option<String> {
    let mut names = Vec::new();
    for r in &map.get_i(i).roads {
        if neighborhood.perimeter.contains(r) {
            names.push(map.get_r(*r).get_name(None));
        }
    }
    names.sort();
    names.dedup();
    // TODO If the major road changes names or we found a corner, bail out
    if names.len() == 1 {
        names.pop()
    } else {
        None
    }
}