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
use std::collections::BTreeSet;
use geom::{Distance, Line};
use map_gui::tools::{CityPicker, ColorDiscrete};
use map_gui::ID;
use map_model::{IntersectionID, Map, Road, RoadID};
use widgetry::{
Color, Drawable, EventCtx, GeomBatch, GfxCtx, HorizontalAlignment, Key, Line, Outcome, Panel,
State, Text, TextExt, VerticalAlignment, Widget,
};
use crate::app::{App, Transition};
use crate::common::intersections_from_roads;
pub use browse::BrowseNeighborhoods;
mod algorithms;
mod browse;
pub struct Viewer {
panel: Panel,
neighborhood: Neighborhood,
draw_neighborhood: Drawable,
draw_dynamic_stuff: Drawable,
current_rat_run_idx: usize,
}
struct Neighborhood {
interior: BTreeSet<RoadID>,
perimeter: BTreeSet<RoadID>,
borders: BTreeSet<IntersectionID>,
modal_filters: BTreeSet<RoadID>,
rat_runs: Vec<RatRun>,
}
struct RatRun {
path: Vec<IntersectionID>,
length_ratio: f64,
}
impl Viewer {
fn start_from_road(ctx: &mut EventCtx, app: &App, start: RoadID) -> Box<dyn State<App>> {
let mut neighborhood = Neighborhood::from_road(&app.primary.map, start);
neighborhood.calculate_rat_runs(&app.primary.map);
let (draw_neighborhood, legend) = neighborhood.render(ctx, app);
let panel = Panel::new_builder(Widget::col(vec![
Widget::row(vec![
Line("LTN tool").small_heading().into_widget(ctx),
map_gui::tools::change_map_btn(ctx, app)
.centered_vert()
.align_right(),
]),
ctx.style()
.btn_outline
.text("Browse neighborhoods")
.hotkey(Key::B)
.build_def(ctx),
legend,
Text::new().into_widget(ctx).named("rat runs"),
]))
.aligned(HorizontalAlignment::Left, VerticalAlignment::Top)
.build(ctx);
let mut viewer = Viewer {
panel,
neighborhood,
draw_neighborhood,
current_rat_run_idx: 0,
draw_dynamic_stuff: Drawable::empty(ctx),
};
viewer.recalculate(ctx, app);
Box::new(viewer)
}
fn recalculate(&mut self, ctx: &mut EventCtx, app: &App) {
let mut col = Vec::new();
let mut batch = GeomBatch::new();
let map = &app.primary.map;
if self.neighborhood.rat_runs.is_empty() {
col.push("No rat runs!".text_widget(ctx));
} else {
let run = &self.neighborhood.rat_runs[self.current_rat_run_idx];
col.extend(vec![
Widget::row(vec![
"Rat runs:".text_widget(ctx).centered_vert(),
ctx.style()
.btn_prev()
.disabled(self.current_rat_run_idx == 0)
.hotkey(Key::LeftArrow)
.build_widget(ctx, "previous rat run"),
Text::from(
Line(format!(
"{}/{}",
self.current_rat_run_idx + 1,
self.neighborhood.rat_runs.len()
))
.secondary(),
)
.into_widget(ctx)
.centered_vert(),
ctx.style()
.btn_next()
.disabled(self.current_rat_run_idx == self.neighborhood.rat_runs.len() - 1)
.hotkey(Key::RightArrow)
.build_widget(ctx, "next rat run"),
]),
format!(
"This run has a length ratio of {:.2} vs the shortest path",
run.length_ratio
)
.text_widget(ctx),
]);
for i in &run.path {
batch.push(Color::RED.alpha(0.8), map.get_i(*i).polygon.clone());
}
for road in run.roads(map) {
batch.push(Color::RED.alpha(0.8), road.get_thick_polygon());
}
}
col.push(
format!(
"{} modal filters currently added",
self.neighborhood.modal_filters.len()
)
.text_widget(ctx),
);
for r in &self.neighborhood.modal_filters {
let road = map.get_r(*r);
if let Ok((pt, angle)) = road.center_pts.dist_along(road.length() / 2.0) {
let filter_len = road.get_width();
let barrier = Line::must_new(
pt.project_away(filter_len, angle.rotate_degs(90.0)),
pt.project_away(filter_len, angle.rotate_degs(-90.0)),
)
.make_polygons(Distance::meters(10.0));
batch.push(Color::GREEN, barrier.clone());
if let Ok(outline) = barrier.to_outline(Distance::meters(2.0)) {
batch.push(Color::BLACK, outline);
}
}
}
self.panel.replace(ctx, "rat runs", Widget::col(col));
self.draw_dynamic_stuff = batch.upload(ctx);
}
}
impl State<App> for Viewer {
fn event(&mut self, ctx: &mut EventCtx, app: &mut App) -> Transition {
ctx.canvas_movement();
if ctx.redo_mouseover() {
app.primary.current_selection =
match app.mouseover_unzoomed_roads_and_intersections(ctx) {
x @ Some(ID::Road(_)) => x,
Some(ID::Lane(l)) => Some(ID::Road(l.road)),
_ => None,
};
}
if let Some(ID::Road(r)) = app.primary.current_selection {
if ctx.normal_left_click() {
if self.neighborhood.interior.contains(&r) {
self.neighborhood.toggle_modal_filter(&app.primary.map, r);
self.current_rat_run_idx = 0;
self.recalculate(ctx, app);
} else if Neighborhood::is_interior_road(r, &app.primary.map) {
return Transition::Replace(Viewer::start_from_road(ctx, app, r));
}
}
}
if let Outcome::Clicked(x) = self.panel.event(ctx) {
match x.as_ref() {
"change map" => {
return Transition::Push(CityPicker::new_state(
ctx,
app,
Box::new(|ctx, app| {
Transition::Replace(BrowseNeighborhoods::new_state(ctx, app))
}),
));
}
"previous rat run" => {
self.current_rat_run_idx -= 1;
self.recalculate(ctx, app);
}
"next rat run" => {
self.current_rat_run_idx += 1;
self.recalculate(ctx, app);
}
"Browse neighborhoods" => {
return Transition::Replace(BrowseNeighborhoods::new_state(ctx, app));
}
_ => unreachable!(),
}
}
Transition::Keep
}
fn draw(&self, g: &mut GfxCtx, app: &App) {
self.panel.draw(g);
g.redraw(&self.draw_neighborhood);
g.redraw(&self.draw_dynamic_stuff);
if let Some(ID::Road(r)) = app.primary.current_selection {
if self.neighborhood.interior.contains(&r) {
if self.neighborhood.modal_filters.contains(&r) {
g.draw_mouse_tooltip(Text::from(Line("Click to remove this modal filter")));
} else {
g.draw_mouse_tooltip(Text::from(Line("Click to add a modal filter here")));
}
} else if Neighborhood::is_interior_road(r, &app.primary.map) {
g.draw_mouse_tooltip(Text::from(Line("Click to analyze this neighborhood")));
}
}
}
}
impl Neighborhood {
fn render(&self, ctx: &mut EventCtx, app: &App) -> (Drawable, Widget) {
let mut colorer = ColorDiscrete::no_fading(
app,
vec![
("interior", Color::BLUE.alpha(0.8)),
("perimeter", Color::hex("#40B5AD").alpha(0.8)),
("border", Color::CYAN.alpha(0.8)),
("rat-run", Color::RED.alpha(0.8)),
("modal filter", Color::GREEN),
],
);
for r in &self.interior {
colorer.add_r(*r, "interior");
}
for i in intersections_from_roads(&self.interior, &app.primary.map) {
colorer.add_i(i, "interior");
}
for r in &self.perimeter {
colorer.add_r(*r, "perimeter");
}
for i in &self.borders {
colorer.add_i(*i, "border");
}
let (unzoomed, _, legend) = colorer.build(ctx);
(unzoomed, legend)
}
}
impl RatRun {
fn roads<'a>(&'a self, map: &'a Map) -> impl Iterator<Item = &'a Road> {
self.path
.windows(2)
.flat_map(move |pair| map.get_i(pair[0]).find_road_between(pair[1], map))
}
}