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

use geom::{Circle, Distance, Line, Polygon};
use map_model::{IntersectionID, Map, Perimeter, RoadID};
use widgetry::{Color, Drawable, EventCtx, GeomBatch};

use crate::app::App;

pub use browse::BrowseNeighborhoods;

mod browse;
mod viewer;

struct Neighborhood {
    // These're fixed
    orig_perimeter: Perimeter,
    perimeter: BTreeSet<RoadID>,
    borders: BTreeSet<IntersectionID>,

    // The cells change as a result of modal filters, which're stored for all neighborhoods in
    // app.session.
    cells: Vec<BTreeSet<RoadID>>,

    fade_irrelevant: Drawable,
    draw_filters: Drawable,
}

impl Neighborhood {
    fn new(ctx: &EventCtx, app: &App, orig_perimeter: Perimeter) -> Neighborhood {
        let map = &app.primary.map;

        let cells = find_cells(map, &orig_perimeter, &app.session.modal_filters);
        let mut n = Neighborhood {
            orig_perimeter,
            perimeter: BTreeSet::new(),
            borders: BTreeSet::new(),

            cells,

            fade_irrelevant: Drawable::empty(ctx),
            draw_filters: Drawable::empty(ctx),
        };

        let mut holes = Vec::new();
        for id in &n.orig_perimeter.roads {
            n.perimeter.insert(id.road);
            let road = map.get_r(id.road);
            n.borders.insert(road.src_i);
            n.borders.insert(road.dst_i);
            holes.push(road.get_thick_polygon());
        }
        for i in &n.borders {
            holes.push(map.get_i(*i).polygon.clone());
        }
        // TODO The original block's polygon is nice, but we want to include the perimeter. Adding
        // more holes seems to break. But the convex hull of a bunch of holes looks really messy.
        let fade_area = Polygon::with_holes(
            map.get_boundary_polygon().clone().into_ring(),
            if true {
                vec![n
                    .orig_perimeter
                    .clone()
                    .to_block(map)
                    .unwrap()
                    .polygon
                    .into_ring()]
            } else {
                vec![Polygon::convex_hull(holes).into_ring()]
            },
        );
        n.fade_irrelevant = GeomBatch::from(vec![(app.cs.fade_map_dark, fade_area)]).upload(ctx);

        let mut batch = GeomBatch::new();
        for r in &app.session.modal_filters {
            if !n.orig_perimeter.interior.contains(r) {
                continue;
            }

            let road = map.get_r(*r);
            // If this road touches a border, place it closer to that intersection. If it's an
            // inner neighborhood split, then stick to the middle of that road.
            let pct_along = if n.borders.contains(&road.src_i) {
                0.1
            } else if n.borders.contains(&road.dst_i) {
                0.9
            } else {
                0.5
            };
            if let Ok((pt, angle)) = road.center_pts.dist_along(pct_along * road.length()) {
                let filter_len = road.get_width();
                batch.push(Color::RED, Circle::new(pt, filter_len).to_polygon());
                let barrier = Line::must_new(
                    pt.project_away(0.8 * filter_len, angle.rotate_degs(90.0)),
                    pt.project_away(0.8 * filter_len, angle.rotate_degs(-90.0)),
                )
                .make_polygons(Distance::meters(7.0));
                batch.push(Color::WHITE, barrier.clone());
            }
        }
        n.draw_filters = batch.upload(ctx);

        n
    }
}

// Find all of the disconnected "cells" of reachable areas, bounded by a perimeter. This is with
// respect to driving.
fn find_cells(
    map: &Map,
    perimeter: &Perimeter,
    modal_filters: &BTreeSet<RoadID>,
) -> Vec<BTreeSet<RoadID>> {
    let mut cells = Vec::new();
    let mut visited = BTreeSet::new();

    for start in &perimeter.interior {
        if visited.contains(start) {
            continue;
        }
        let cell = floodfill(map, *start, perimeter, modal_filters);
        cells.push(cell.clone());
        visited.extend(cell);
    }

    cells
}

fn floodfill(
    map: &Map,
    start: RoadID,
    perimeter: &Perimeter,
    modal_filters: &BTreeSet<RoadID>,
) -> BTreeSet<RoadID> {
    // We don't need a priority queue
    let mut visited = BTreeSet::new();
    let mut queue = vec![start];

    // TODO For now, each road with a filter is its own tiny cell. That's not really what we
    // want...
    if modal_filters.contains(&start) {
        visited.insert(start);
        return visited;
    }

    while !queue.is_empty() {
        let current = map.get_r(queue.pop().unwrap());
        if visited.contains(&current.id) {
            continue;
        }
        visited.insert(current.id);
        for i in [current.src_i, current.dst_i] {
            for next in &map.get_i(i).roads {
                if perimeter.interior.contains(next) && !modal_filters.contains(next) {
                    queue.push(*next);
                }
            }
        }
    }

    visited
}