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
use std::collections::VecDeque;
use geom::{Distance, Polygon, Pt2D};
use map_gui::tools::Grid;
use map_model::Map;
use widgetry::{Color, GeomBatch};
use super::Neighborhood;
pub const COLORS: [Color; 6] = [
Color::BLUE,
Color::YELLOW,
Color::GREEN,
Color::PURPLE,
Color::PINK,
Color::ORANGE,
];
pub fn draw_cells(map: &Map, neighborhood: &Neighborhood) -> GeomBatch {
let boundary_polygon = neighborhood
.orig_perimeter
.clone()
.to_block(map)
.unwrap()
.polygon;
let bounds = boundary_polygon.get_bounds();
let resolution_m = 10.0;
let mut grid: Grid<Option<usize>> = Grid::new(
(bounds.width() / resolution_m).ceil() as usize,
(bounds.height() / resolution_m).ceil() as usize,
None,
);
for (cell_idx, cell) in neighborhood.cells.iter().enumerate() {
for (r, interval) in &cell.roads {
let road = map.get_r(*r);
for (pt, _) in road
.center_pts
.exact_slice(interval.start, interval.end)
.step_along(Distance::meters(resolution_m / 2.0), Distance::ZERO)
{
let grid_idx = grid.idx(
((pt.x() - bounds.min_x) / resolution_m) as usize,
((pt.y() - bounds.min_y) / resolution_m) as usize,
);
grid.data[grid_idx] = Some(cell_idx);
}
}
}
let boundary_marker = neighborhood.cells.len();
for (pt, _) in geom::PolyLine::unchecked_new(boundary_polygon.into_ring().into_points())
.step_along(Distance::meters(resolution_m / 2.0), Distance::ZERO)
{
let grid_idx = grid.idx(
((pt.x() - bounds.min_x) / resolution_m) as usize,
((pt.y() - bounds.min_y) / resolution_m) as usize,
);
grid.data[grid_idx] = Some(boundary_marker);
}
diffusion(&mut grid, boundary_marker);
let mut batch = GeomBatch::new();
for (idx, value) in grid.data.iter().enumerate() {
if let Some(cell_idx) = value {
if *cell_idx == boundary_marker {
continue;
}
let (x, y) = grid.xy(idx);
let tile_center = Pt2D::new(
bounds.min_x + resolution_m * (x as f64 + 0.5),
bounds.min_y + resolution_m * (y as f64 + 0.5),
);
let color = COLORS[cell_idx % COLORS.len()].alpha(0.5);
batch.push(
color,
Polygon::rectangle_centered(
tile_center,
Distance::meters(resolution_m),
Distance::meters(resolution_m),
),
);
}
}
batch
}
fn diffusion(grid: &mut Grid<Option<usize>>, boundary_marker: usize) {
let mut queue: VecDeque<usize> = VecDeque::new();
for (idx, value) in grid.data.iter().enumerate() {
if let Some(x) = value {
if *x != boundary_marker {
queue.push_back(idx);
}
}
}
while !queue.is_empty() {
let current_idx = queue.pop_front().unwrap();
let current_color = grid.data[current_idx].unwrap();
let (current_x, current_y) = grid.xy(current_idx);
for (next_x, next_y) in grid.orthogonal_neighbors(current_x, current_y) {
let next_idx = grid.idx(next_x, next_y);
if grid.data[next_idx].is_none() {
grid.data[next_idx] = Some(current_color);
queue.push_back(next_idx);
}
}
}
}