From 96607944674e43e280a3a1962e846bd774e32aad Mon Sep 17 00:00:00 2001 From: Dustin Carlino Date: Mon, 22 Oct 2018 14:34:00 -0700 Subject: [PATCH] show route by trip, not agent --- editor/src/plugins/show_route.rs | 71 ++++++++++++++++---------------- sim/src/sim.rs | 8 +++- sim/src/trips.rs | 2 +- 3 files changed, 42 insertions(+), 39 deletions(-) diff --git a/editor/src/plugins/show_route.rs b/editor/src/plugins/show_route.rs index 4b917eff59..73509d35e1 100644 --- a/editor/src/plugins/show_route.rs +++ b/editor/src/plugins/show_route.rs @@ -2,61 +2,60 @@ use colors::Colors; use dimensioned::si; use ezgui::GfxCtx; use map_model::{Trace, LANE_THICKNESS}; -use objects::{Ctx, ID}; +use objects::Ctx; use piston::input::Key; use plugins::{Plugin, PluginCtx}; -use sim::AgentID; +use sim::TripID; use std::f64; pub enum ShowRouteState { Empty, - Active(AgentID, Trace), + Active(TripID, Trace), } impl Plugin for ShowRouteState { fn event(&mut self, ctx: PluginCtx) -> bool { - let (input, sim, map, selected) = ( - ctx.input, - &ctx.primary.sim, - &ctx.primary.map, - ctx.primary.current_selection, - ); - - let maybe_agent = match self { - ShowRouteState::Empty => match selected { - Some(ID::Car(id)) => { - if input.key_pressed(Key::R, "show this car's route") { - Some(AgentID::Car(id)) + let maybe_trip = match self { + ShowRouteState::Empty => ctx + .primary + .current_selection + .and_then(|id| id.agent_id()) + .and_then(|agent| ctx.primary.sim.agent_to_trip(agent)) + .and_then(|trip| { + if ctx + .input + .key_pressed(Key::R, &format!("show {}'s route", trip)) + { + Some(trip) } else { None } - } - Some(ID::Pedestrian(id)) => { - if input.key_pressed(Key::R, "show this pedestrian's route") { - Some(AgentID::Pedestrian(id)) - } else { - None - } - } - _ => None, - }, - ShowRouteState::Active(agent, _) => { - if input.key_pressed(Key::Return, "stop showing route") { + }), + ShowRouteState::Active(trip, _) => { + if ctx.input.key_pressed(Key::Return, "stop showing route") { None } else { - Some(*agent) + Some(*trip) } } }; - if let Some(agent) = maybe_agent { - // Trace along the entire route by passing in max distance - match sim.trace_route(agent, map, f64::MAX * si::M) { - Some(trace) => { - *self = ShowRouteState::Active(agent, trace); - } - None => { - *self = ShowRouteState::Empty; + if let Some(trip) = maybe_trip { + if let Some(agent) = ctx.primary.sim.trip_to_agent(trip) { + // Trace along the entire route by passing in max distance + if let Some(trace) = + ctx.primary + .sim + .trace_route(agent, &ctx.primary.map, f64::MAX * si::M) + { + *self = ShowRouteState::Active(trip, trace); + } else { + warn!("{} has no trace right now", agent); } + } else { + warn!( + "{} has no agent associated right now; is the trip done?", + trip + ); } } else { *self = ShowRouteState::Empty; diff --git a/sim/src/sim.rs b/sim/src/sim.rs index a9f722a864..26fdc1c820 100644 --- a/sim/src/sim.rs +++ b/sim/src/sim.rs @@ -384,7 +384,7 @@ impl Sim { // TODO dont toggle state in debug_car pub fn debug_trip(&mut self, id: TripID) { - match self.trips_state.current_mode(id) { + match self.trips_state.trip_to_agent(id) { Some(AgentID::Car(id)) => self.debug_car(id), Some(AgentID::Pedestrian(id)) => self.debug_ped(id), None => println!("{} doesn't exist", id), @@ -395,9 +395,13 @@ impl Sim { self.trips_state.agent_to_trip(id) } + pub fn trip_to_agent(&self, id: TripID) -> Option { + self.trips_state.trip_to_agent(id) + } + pub fn get_canonical_point_for_trip(&self, id: TripID, map: &Map) -> Option { // Don't unwrap(); the trip might be registered before the agent has started. - match self.trips_state.current_mode(id) { + match self.trips_state.trip_to_agent(id) { Some(AgentID::Car(id)) => self .driving_state .get_draw_car(id, self.time, map) diff --git a/sim/src/trips.rs b/sim/src/trips.rs index 9dc0405b80..5a74467d55 100644 --- a/sim/src/trips.rs +++ b/sim/src/trips.rs @@ -226,7 +226,7 @@ impl TripManager { self.active_trip_mode.keys().cloned().collect() } - pub fn current_mode(&self, id: TripID) -> Option { + pub fn trip_to_agent(&self, id: TripID) -> Option { let trip = self.trips.get(id.0)?; match trip.legs[0] { TripLeg::Walk(_) => Some(AgentID::Pedestrian(trip.ped)),