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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
use std::collections::{BTreeMap, BTreeSet};

use maplit::btreeset;

use geom::{Circle, Distance, Line, Polygon};
use map_gui::tools::DrawRoadLabels;
use map_model::{IntersectionID, Map, Perimeter, RoadID, RoutingParams, TurnID};
use widgetry::mapspace::ToggleZoomed;
use widgetry::{Color, Drawable, EventCtx, GeomBatch};

use crate::app::App;

pub use browse::BrowseNeighborhoods;

mod browse;
mod connectivity;
mod draw_cells;
mod pathfinding;
mod per_neighborhood;
mod rat_run_viewer;
mod rat_runs;
mod select_boundary;

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

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

    fade_irrelevant: Drawable,
    draw_filters: ToggleZoomed,
    labels: DrawRoadLabels,
}

#[derive(Default)]
pub struct ModalFilters {
    /// For filters placed along a road, where is the filter located?
    pub roads: BTreeMap<RoadID, Distance>,
    pub intersections: BTreeMap<IntersectionID, DiagonalFilter>,
}

impl ModalFilters {
    /// Modify RoutingParams to respect these modal filters
    pub fn update_routing_params(&self, params: &mut RoutingParams) {
        params.avoid_roads.extend(self.roads.keys().cloned());
        for filter in self.intersections.values() {
            params
                .avoid_movements_between
                .extend(filter.avoid_movements_between_roads());
        }
    }

    pub fn allows_turn(&self, t: TurnID) -> bool {
        if let Some(filter) = self.intersections.get(&t.parent) {
            return filter.allows_turn(t.src.road, t.dst.road);
        }
        true
    }
}

/// A diagonal filter exists in an intersection. It's defined by two roads (the order is
/// arbitrary). When all of the intersection's roads are sorted in clockwise order, this pair of
/// roads splits the ordering into two groups. Turns in each group are still possible, but not
/// across groups.
///
/// TODO Be careful with PartialEq! At a 4-way intersection, the same filter can be expressed as a
/// different pair of two roads. And the (r1, r2) ordering is also arbitrary.
#[derive(Clone, PartialEq)]
pub struct DiagonalFilter {
    r1: RoadID,
    r2: RoadID,
    i: IntersectionID,

    group1: BTreeSet<RoadID>,
    group2: BTreeSet<RoadID>,
}

/// A partitioning of the interior of a neighborhood based on driving connectivity
pub struct Cell {
    /// Most roads are fully in one cell. Roads with modal filters on them are sometimes split
    /// between two cells, and the DistanceInterval indicates the split. The distances are over the
    /// road's center line length.
    pub roads: BTreeMap<RoadID, DistanceInterval>,
    /// Intersections where this cell touches the boundary of the neighborhood.
    pub borders: BTreeSet<IntersectionID>,
}

/// An interval along a road's length, with start < end.
pub struct DistanceInterval {
    pub start: Distance,
    pub end: Distance,
}

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

        let mut n = Neighborhood {
            orig_perimeter,
            perimeter: BTreeSet::new(),
            borders: BTreeSet::new(),
            interior_intersections: BTreeSet::new(),

            cells: Vec::new(),

            fade_irrelevant: Drawable::empty(ctx),
            draw_filters: ToggleZoomed::empty(ctx),
            // Temporary value
            labels: DrawRoadLabels::only_major_roads(),
        };

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

        for r in &n.orig_perimeter.interior {
            let road = map.get_r(*r);
            for i in [road.src_i, road.dst_i] {
                if !n.borders.contains(&i) {
                    n.interior_intersections.insert(i);
                }
            }
        }

        n.cells = find_cells(
            map,
            &n.orig_perimeter,
            &n.borders,
            &app.session.modal_filters,
        );

        let mut batch = ToggleZoomed::builder();
        for (r, dist) in &app.session.modal_filters.roads {
            if !n.orig_perimeter.interior.contains(r) {
                continue;
            }

            let road = map.get_r(*r);
            if let Ok((pt, angle)) = road.center_pts.dist_along(*dist) {
                let road_width = road.get_width();

                batch
                    .unzoomed
                    .push(Color::RED, Circle::new(pt, road_width).to_polygon());
                batch.unzoomed.push(
                    Color::WHITE,
                    Line::must_new(
                        pt.project_away(0.8 * road_width, angle.rotate_degs(90.0)),
                        pt.project_away(0.8 * road_width, angle.rotate_degs(-90.0)),
                    )
                    .make_polygons(Distance::meters(7.0)),
                );

                // TODO Only cover the driving/parking lanes (and center appropriately)
                draw_zoomed_planters(
                    ctx,
                    &mut batch.zoomed,
                    Line::must_new(
                        pt.project_away(0.3 * road_width, angle.rotate_degs(90.0)),
                        pt.project_away(0.3 * road_width, angle.rotate_degs(-90.0)),
                    ),
                );
            }
        }
        for filter in app.session.modal_filters.intersections.values() {
            let line = filter.geometry(app);
            batch
                .unzoomed
                .push(Color::RED, line.make_polygons(Distance::meters(3.0)));

            draw_zoomed_planters(
                ctx,
                &mut batch.zoomed,
                line.percent_slice(0.3, 0.7).unwrap_or(line),
            );
        }
        n.draw_filters = batch.build(ctx);

        let mut label_roads = n.perimeter.clone();
        label_roads.extend(n.orig_perimeter.interior.clone());
        n.labels = DrawRoadLabels::new(Box::new(move |r| label_roads.contains(&r.id)));

        n
    }
}

// Draw two planters on each end of a line. They'll be offset so that they don't exceed the
// endpoints.
fn draw_zoomed_planters(ctx: &EventCtx, batch: &mut GeomBatch, line: Line) {
    let planter = GeomBatch::load_svg(ctx, "system/assets/map/planter.svg");
    let planter_width = planter.get_dims().width;
    let scaled_planter = planter.scale(0.3 * line.length().inner_meters() / planter_width);

    batch.append(
        scaled_planter
            .clone()
            .centered_on(line.must_dist_along(0.15 * line.length()))
            .rotate(line.angle()),
    );
    batch.append(
        scaled_planter
            .centered_on(line.must_dist_along(0.85 * line.length()))
            .rotate(line.angle()),
    );
}

// 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,
    borders: &BTreeSet<IntersectionID>,
    modal_filters: &ModalFilters,
) -> Vec<Cell> {
    let mut cells = Vec::new();
    let mut visited = BTreeSet::new();

    for start in &perimeter.interior {
        if visited.contains(start) || modal_filters.roads.contains_key(start) {
            continue;
        }
        let cell = floodfill(map, *start, perimeter, borders, &modal_filters);
        visited.extend(cell.roads.keys().cloned());
        cells.push(cell);
    }

    // Filtered roads right along the perimeter have a tiny cell
    for (r, filter_dist) in &modal_filters.roads {
        let road = map.get_r(*r);
        if borders.contains(&road.src_i) {
            let mut cell = Cell {
                roads: BTreeMap::new(),
                borders: btreeset! { road.src_i },
            };
            cell.roads.insert(
                road.id,
                DistanceInterval {
                    start: Distance::ZERO,
                    end: *filter_dist,
                },
            );
            cells.push(cell);
        }
        if borders.contains(&road.dst_i) {
            let mut cell = Cell {
                roads: BTreeMap::new(),
                borders: btreeset! { road.dst_i },
            };
            cell.roads.insert(
                road.id,
                DistanceInterval {
                    start: *filter_dist,
                    end: road.length(),
                },
            );
            cells.push(cell);
        }
    }

    cells
}

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

    // The caller should handle this case
    assert!(!modal_filters.roads.contains_key(&start));

    while !queue.is_empty() {
        let current = map.get_r(queue.pop().unwrap());
        if visited_roads.contains_key(&current.id) {
            continue;
        }
        visited_roads.insert(
            current.id,
            DistanceInterval {
                start: Distance::ZERO,
                end: map.get_r(current.id).length(),
            },
        );
        for i in [current.src_i, current.dst_i] {
            for next in &map.get_i(i).roads {
                if !perimeter.interior.contains(next) {
                    if neighborhood_borders.contains(&i) {
                        cell_borders.insert(i);
                    }
                    continue;
                }
                if let Some(filter) = modal_filters.intersections.get(&i) {
                    if !filter.allows_turn(current.id, *next) {
                        continue;
                    }
                }
                if let Some(filter_dist) = modal_filters.roads.get(next) {
                    let next_road = map.get_r(*next);
                    // Which ends of the filtered road have we reached?
                    let mut visited_start = next_road.src_i == i;
                    let mut visited_end = next_road.dst_i == i;
                    // We may have visited previously from the other side.
                    if let Some(interval) = visited_roads.get(next) {
                        if interval.start == Distance::ZERO {
                            visited_start = true;
                        }
                        if interval.end == next_road.length() {
                            visited_end = true;
                        }
                    }
                    visited_roads.insert(
                        *next,
                        DistanceInterval {
                            start: if visited_start {
                                Distance::ZERO
                            } else {
                                *filter_dist
                            },
                            end: if visited_end {
                                next_road.length()
                            } else {
                                *filter_dist
                            },
                        },
                    );
                } else {
                    queue.push(*next);
                }
            }
        }
    }

    Cell {
        roads: visited_roads,
        borders: cell_borders,
    }
}

impl DiagonalFilter {
    /// Find all possible diagonal filters at an intersection
    fn filters_for(app: &App, i: IntersectionID) -> Vec<DiagonalFilter> {
        let map = &app.primary.map;
        let roads = map.get_i(i).get_roads_sorted_by_incoming_angle(map);
        // TODO Handle >4-ways
        if roads.len() != 4 {
            return Vec::new();
        }

        vec![
            DiagonalFilter::new(map, i, roads[0], roads[1]),
            DiagonalFilter::new(map, i, roads[1], roads[2]),
        ]
    }

    fn new(map: &Map, i: IntersectionID, r1: RoadID, r2: RoadID) -> DiagonalFilter {
        let mut roads = map.get_i(i).get_roads_sorted_by_incoming_angle(map);
        // Make self.r1 be the first entry
        while roads[0] != r1 {
            roads.rotate_right(1);
        }

        let mut group1 = BTreeSet::new();
        group1.insert(roads.remove(0));
        loop {
            let next = roads.remove(0);
            group1.insert(next);
            if next == r2 {
                break;
            }
        }
        // This is only true for 4-ways...
        assert_eq!(group1.len(), 2);
        assert_eq!(roads.len(), 2);

        DiagonalFilter {
            r1,
            r2,
            i,
            group1,
            group2: roads.into_iter().collect(),
        }
    }

    /// Physically where is the filter placed?
    fn geometry(&self, app: &App) -> Line {
        let map = &app.primary.map;
        let r1 = map.get_r(self.r1);
        let r2 = map.get_r(self.r2);

        // Orient the road to face the intersection
        let mut pl1 = r1.center_pts.clone();
        if r1.src_i == self.i {
            pl1 = pl1.reversed();
        }
        let mut pl2 = r2.center_pts.clone();
        if r2.src_i == self.i {
            pl2 = pl2.reversed();
        }

        // The other combinations of left/right here would produce points or a line across just one
        // road
        let pt1 = pl1.must_shift_right(r1.get_half_width()).last_pt();
        let pt2 = pl2.must_shift_left(r2.get_half_width()).last_pt();
        Line::must_new(pt1, pt2)
    }

    fn allows_turn(&self, from: RoadID, to: RoadID) -> bool {
        self.group1.contains(&from) == self.group1.contains(&to)
    }

    fn avoid_movements_between_roads(&self) -> Vec<(RoadID, RoadID)> {
        let mut pairs = Vec::new();
        for from in &self.group1 {
            for to in &self.group2 {
                pairs.push((*from, *to));
                pairs.push((*to, *from));
            }
        }
        pairs
    }
}