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
use std::collections::{BTreeMap, BTreeSet};

use abstutil::prettyprint_usize;
use geom::{ArrowCap, Distance, Duration, PolyLine, Polygon, Time};
use map_model::{IntersectionID, IntersectionType, PhaseType};
use sim::AgentType;
use widgetry::{
    Btn, Checkbox, Color, DrawWithTooltips, EventCtx, FanChart, GeomBatch, Line, PlotOptions,
    ScatterPlot, Series, Text, Widget,
};

use crate::app::App;
use crate::helpers::color_for_agent_type;
use crate::info::{header_btns, make_tabs, throughput, DataOptions, Details, Tab};
use crate::options::TrafficSignalStyle;
use crate::render::traffic_signal::draw_signal_stage;

pub fn info(ctx: &EventCtx, app: &App, details: &mut Details, id: IntersectionID) -> Vec<Widget> {
    let mut rows = header(ctx, app, details, id, Tab::IntersectionInfo(id));
    let i = app.primary.map.get_i(id);

    let mut txt = Text::from(Line("Connecting"));
    let mut road_names = BTreeSet::new();
    for r in &i.roads {
        road_names.insert(
            app.primary
                .map
                .get_r(*r)
                .get_name(app.opts.language.as_ref()),
        );
    }
    for r in road_names {
        txt.add(Line(format!("  {}", r)));
    }
    rows.push(txt.draw(ctx));

    if app.opts.dev {
        rows.push(Btn::text_bg1("Open OSM node").build(ctx, format!("open {}", i.orig_id), None));
    }

    rows
}

pub fn traffic(
    ctx: &mut EventCtx,
    app: &App,
    details: &mut Details,
    id: IntersectionID,
    opts: &DataOptions,
) -> Vec<Widget> {
    let mut rows = header(
        ctx,
        app,
        details,
        id,
        Tab::IntersectionTraffic(id, opts.clone()),
    );

    let mut txt = Text::new();

    txt.add(Line(format!(
        "Since midnight: {} commuters and vehicles crossed",
        prettyprint_usize(
            app.primary
                .sim
                .get_analytics()
                .intersection_thruput
                .total_for(id)
        )
    )));
    rows.push(txt.draw(ctx));

    rows.push(opts.to_controls(ctx, app));

    let time = if opts.show_end_of_day {
        app.primary.sim.get_end_of_day()
    } else {
        app.primary.sim.time()
    };
    rows.push(throughput(
        ctx,
        app,
        "Number of commuters and vehicles per hour",
        move |a| {
            if a.intersection_thruput.raw.is_empty() {
                a.intersection_thruput.count_per_hour(id, time)
            } else {
                a.intersection_thruput.raw_throughput(time, id)
            }
        },
        &opts,
    ));

    rows
}

pub fn delay(
    ctx: &mut EventCtx,
    app: &App,
    details: &mut Details,
    id: IntersectionID,
    opts: &DataOptions,
    fan_chart: bool,
) -> Vec<Widget> {
    let mut rows = header(
        ctx,
        app,
        details,
        id,
        Tab::IntersectionDelay(id, opts.clone(), fan_chart),
    );
    let i = app.primary.map.get_i(id);

    assert!(i.is_traffic_signal());
    rows.push(opts.to_controls(ctx, app));
    rows.push(Checkbox::toggle(
        ctx,
        "fan chart / scatter plot",
        "fan chart",
        "scatter plot",
        None,
        fan_chart,
    ));

    rows.push(delay_plot(ctx, app, id, opts, fan_chart));

    rows
}

pub fn current_demand(
    ctx: &mut EventCtx,
    app: &App,
    details: &mut Details,
    id: IntersectionID,
) -> Vec<Widget> {
    let mut rows = header(ctx, app, details, id, Tab::IntersectionDemand(id));

    let mut total_demand = 0;
    let mut demand_per_movement: Vec<(&PolyLine, usize)> = Vec::new();
    for m in app.primary.map.get_traffic_signal(id).movements.values() {
        let demand = app
            .primary
            .sim
            .get_analytics()
            .demand
            .get(&m.id)
            .cloned()
            .unwrap_or(0);
        if demand > 0 {
            total_demand += demand;
            demand_per_movement.push((&m.geom, demand));
        }
    }

    let mut batch = GeomBatch::new();
    let polygon = app.primary.map.get_i(id).polygon.clone();
    let bounds = polygon.get_bounds();
    // Pick a zoom so that we fit a fixed width in pixels
    let zoom = (0.25 * ctx.canvas.window_width) / bounds.width();
    batch.push(
        app.cs.normal_intersection,
        polygon.translate(-bounds.min_x, -bounds.min_y).scale(zoom),
    );

    let mut tooltips: Vec<(Polygon, Text)> = Vec::new();
    let mut outlines = Vec::new();
    for (pl, demand) in demand_per_movement {
        let percent = (demand as f64) / (total_demand as f64);
        let arrow = pl
            .make_arrow(percent * Distance::meters(3.0), ArrowCap::Triangle)
            .translate(-bounds.min_x, -bounds.min_y)
            .scale(zoom);
        if let Ok(p) = arrow.to_outline(Distance::meters(1.0)) {
            outlines.push(p);
        }
        batch.push(Color::hex("#A3A3A3"), arrow.clone());
        tooltips.push((arrow, Text::from(Line(prettyprint_usize(demand)))));
    }
    batch.extend(Color::WHITE, outlines);

    let mut txt = Text::from(Line(format!(
        "Active agent demand at {}",
        app.primary.sim.time().ampm_tostring()
    )));
    txt.add(
        Line(format!(
            "Includes all {} active agents anywhere on the map",
            prettyprint_usize(total_demand)
        ))
        .secondary(),
    );

    rows.push(
        Widget::col(vec![
            txt.draw(ctx),
            DrawWithTooltips::new(
                ctx,
                batch,
                tooltips,
                Box::new(|arrow| {
                    let mut list = vec![(Color::hex("#EE702E"), arrow.clone())];
                    if let Ok(p) = arrow.to_outline(Distance::meters(1.0)) {
                        list.push((Color::WHITE, p));
                    }
                    GeomBatch::from(list)
                }),
            ),
        ])
        .padding(10)
        .bg(app.cs.inner_panel)
        .outline(2.0, Color::WHITE),
    );
    rows.push(Btn::text_fg("Explore demand across all traffic signals").build_def(ctx, None));
    if app.opts.dev {
        rows.push(Btn::text_fg("Where are these agents headed?").build(
            ctx,
            format!("routes across {}", id),
            None,
        ));
    }

    rows
}

pub fn arrivals(
    ctx: &mut EventCtx,
    app: &App,
    details: &mut Details,
    id: IntersectionID,
    opts: &DataOptions,
) -> Vec<Widget> {
    let mut rows = header(
        ctx,
        app,
        details,
        id,
        Tab::IntersectionArrivals(id, opts.clone()),
    );

    rows.push(throughput(
        ctx,
        app,
        "Number of in-bound trips from this border",
        move |_| app.primary.sim.all_arrivals_at_border(id),
        opts,
    ));

    rows
}

pub fn traffic_signal(
    ctx: &mut EventCtx,
    app: &App,
    details: &mut Details,
    id: IntersectionID,
) -> Vec<Widget> {
    let mut rows = header(ctx, app, details, id, Tab::IntersectionTrafficSignal(id));

    // Slightly inaccurate -- the turn rendering may slightly exceed the intersection polygon --
    // but this is close enough.
    let bounds = app.primary.map.get_i(id).polygon.get_bounds();
    // Pick a zoom so that we fit a fixed width in pixels
    let zoom = 150.0 / bounds.width();
    let bbox = Polygon::rectangle(zoom * bounds.width(), zoom * bounds.height());

    let signal = app.primary.map.get_traffic_signal(id);
    {
        let mut txt = Text::new();
        txt.add(Line(format!("{} stages", signal.stages.len())).small_heading());
        txt.add(Line(format!("Signal offset: {}", signal.offset)));
        {
            let mut total = Duration::ZERO;
            for s in &signal.stages {
                total += s.phase_type.simple_duration();
            }
            // TODO Say "normally" or something?
            txt.add(Line(format!("One cycle lasts {}", total)));
        }
        rows.push(txt.draw(ctx));
    }

    for (idx, stage) in signal.stages.iter().enumerate() {
        rows.push(
            match stage.phase_type {
                PhaseType::Fixed(d) => Line(format!("Stage {}: {}", idx + 1, d)),
                PhaseType::Adaptive(d) => Line(format!("Stage {}: {} (adaptive)", idx + 1, d)),
            }
            .draw(ctx),
        );

        {
            let mut orig_batch = GeomBatch::new();
            draw_signal_stage(
                ctx.prerender,
                stage,
                idx,
                id,
                None,
                &mut orig_batch,
                app,
                TrafficSignalStyle::Yuwen,
            );

            let mut normal = GeomBatch::new();
            normal.push(Color::BLACK, bbox.clone());
            normal.append(
                orig_batch
                    .translate(-bounds.min_x, -bounds.min_y)
                    .scale(zoom),
            );

            rows.push(Widget::draw_batch(ctx, normal));
        }
    }

    rows
}

fn delay_plot(
    ctx: &EventCtx,
    app: &App,
    i: IntersectionID,
    opts: &DataOptions,
    fan_chart: bool,
) -> Widget {
    let data = if opts.show_before {
        app.prebaked()
    } else {
        app.primary.sim.get_analytics()
    };
    let mut by_type: BTreeMap<AgentType, Vec<(Time, Duration)>> = AgentType::all()
        .into_iter()
        .map(|t| (t, Vec::new()))
        .collect();
    let limit = if opts.show_end_of_day {
        app.primary.sim.get_end_of_day()
    } else {
        app.primary.sim.time()
    };
    if let Some(list) = data.intersection_delays.get(&i) {
        for (_, t, dt, agent_type) in list {
            if *t > limit {
                break;
            }
            by_type.get_mut(agent_type).unwrap().push((*t, *dt));
        }
    }
    let series: Vec<Series<Duration>> = by_type
        .into_iter()
        .map(|(agent_type, pts)| Series {
            label: agent_type.noun().to_string(),
            color: color_for_agent_type(app, agent_type),
            pts,
        })
        .collect();
    let plot_opts = PlotOptions {
        filterable: true,
        max_x: Some(limit),
        max_y: None,
        disabled: opts.disabled_series(),
    };
    Widget::col(vec![
        Line("Delay through intersection").small_heading().draw(ctx),
        if fan_chart {
            FanChart::new(ctx, series, plot_opts)
        } else {
            ScatterPlot::new(ctx, series, plot_opts)
        },
    ])
    .padding(10)
    .bg(app.cs.inner_panel)
    .outline(2.0, Color::WHITE)
}

fn header(
    ctx: &EventCtx,
    app: &App,
    details: &mut Details,
    id: IntersectionID,
    tab: Tab,
) -> Vec<Widget> {
    let mut rows = vec![];

    let i = app.primary.map.get_i(id);

    let label = match i.intersection_type {
        IntersectionType::StopSign => format!("{} (Stop signs)", id),
        IntersectionType::TrafficSignal => format!("{} (Traffic signals)", id),
        IntersectionType::Border => format!("Border #{}", id.0),
        IntersectionType::Construction => format!("{} (under construction)", id),
    };
    rows.push(Widget::row(vec![
        Line(label).small_heading().draw(ctx),
        header_btns(ctx),
    ]));

    rows.push(make_tabs(ctx, &mut details.hyperlinks, tab, {
        let mut tabs = vec![
            ("Info", Tab::IntersectionInfo(id)),
            ("Traffic", Tab::IntersectionTraffic(id, DataOptions::new())),
        ];
        if i.is_traffic_signal() {
            tabs.push((
                "Delay",
                Tab::IntersectionDelay(id, DataOptions::new(), false),
            ));
            tabs.push(("Current demand", Tab::IntersectionDemand(id)));
            tabs.push(("Signal", Tab::IntersectionTrafficSignal(id)));
        }
        if i.is_incoming_border() {
            tabs.push((
                "Arrivals",
                Tab::IntersectionArrivals(id, DataOptions::new()),
            ));
        }
        tabs
    }));

    rows
}