diff --git a/docs/TODO_ux.md b/docs/TODO_ux.md index 4cf68533fb..dbdb7cc041 100644 --- a/docs/TODO_ux.md +++ b/docs/TODO_ux.md @@ -4,6 +4,7 @@ - cache draw stuff - lazy SimStats + - dont consider it when comparing or serializing ## Quick n easy diff --git a/editor/src/objects.rs b/editor/src/objects.rs index 12c0415987..ef3f71455a 100644 --- a/editor/src/objects.rs +++ b/editor/src/objects.rs @@ -93,7 +93,7 @@ impl ID { ID::Parcel(id) => map.maybe_get_p(id).map(|p| Pt2D::center(&p.points)), ID::BusStop(id) => map.maybe_get_bs(id).map(|bs| bs.sidewalk_pos.pt(map)), ID::Area(id) => map.maybe_get_a(id).map(|a| Pt2D::center(&a.points)), - ID::Trip(id) => sim.get_stats().canonical_pt_per_trip.get(&id).cloned(), + ID::Trip(id) => sim.get_canonical_pt_per_trip(id, map), } } } diff --git a/editor/src/plugins/sim/diff_trip.rs b/editor/src/plugins/sim/diff_trip.rs index 12e4aed5f7..5b8f69aa2c 100644 --- a/editor/src/plugins/sim/diff_trip.rs +++ b/editor/src/plugins/sim/diff_trip.rs @@ -79,10 +79,10 @@ fn diff_trip(trip: TripID, ctx: &mut PluginCtx) -> DiffTripState { .map(|(s, _)| (&s.sim, &s.map)) .unwrap(); - let pt1 = primary_sim.get_stats().canonical_pt_per_trip.get(&trip); - let pt2 = secondary_sim.get_stats().canonical_pt_per_trip.get(&trip); + let pt1 = primary_sim.get_canonical_pt_per_trip(trip, primary_map); + let pt2 = secondary_sim.get_canonical_pt_per_trip(trip, secondary_map); let line = if pt1.is_some() && pt2.is_some() { - Line::maybe_new(*pt1.unwrap(), *pt2.unwrap()) + Line::maybe_new(pt1.unwrap(), pt2.unwrap()) } else { None }; diff --git a/editor/src/plugins/view/follow.rs b/editor/src/plugins/view/follow.rs index 7647f4a012..79d78bfe8a 100644 --- a/editor/src/plugins/view/follow.rs +++ b/editor/src/plugins/view/follow.rs @@ -28,8 +28,12 @@ impl Plugin for FollowState { } if let Some(trip) = self.trip { - if let Some(pt) = ctx.primary.sim.get_stats().canonical_pt_per_trip.get(&trip) { - ctx.canvas.center_on_map_pt(*pt); + if let Some(pt) = ctx + .primary + .sim + .get_canonical_pt_per_trip(trip, &ctx.primary.map) + { + ctx.canvas.center_on_map_pt(pt); } else { // TODO ideally they wouldnt vanish for so long according to // get_canonical_point_for_trip diff --git a/sim/src/sim.rs b/sim/src/sim.rs index 6770dedd22..6bf5b96c7c 100644 --- a/sim/src/sim.rs +++ b/sim/src/sim.rs @@ -12,7 +12,7 @@ use crate::{AgentID, CarID, Event, ParkedCar, PedestrianID, SimStats, Tick, Trip use abstutil; use abstutil::Error; use derivative::Derivative; -use geom::Distance; +use geom::{Distance, Pt2D}; use map_model::{BuildingID, IntersectionID, LaneID, LaneType, Map, Path, Trace, Turn}; use rand::{FromEntropy, SeedableRng}; use rand_xorshift::XorShiftRng; @@ -397,23 +397,33 @@ impl Sim { &self.stats } + pub fn get_canonical_pt_per_trip(&self, trip: TripID, map: &Map) -> Option { + self.trips_state + .trip_to_agent(trip) + .and_then(|id| self.canonical_pt_for_agent(id, map)) + } + fn collect_stats(&mut self, map: &Map) { self.stats = SimStats::new(self.time); for trip in self.trips_state.get_active_trips().into_iter() { - let pt = match self.trips_state.trip_to_agent(trip) { - Some(AgentID::Car(id)) => self - .driving_state - .get_draw_car(id, self.time, map) - .map(|c| c.body.last_pt()), - Some(AgentID::Pedestrian(id)) => self - .walking_state - .get_draw_ped(id, map, self.time) - .map(|p| p.pos), - None => None, - }; - if let Some(pt) = pt { - self.stats.canonical_pt_per_trip.insert(trip, pt); + if let Some(agent) = self.trips_state.trip_to_agent(trip) { + if let Some(pt) = self.canonical_pt_for_agent(agent, map) { + self.stats.canonical_pt_per_trip.insert(trip, pt); + } } } } + + fn canonical_pt_for_agent(&self, id: AgentID, map: &Map) -> Option { + match id { + AgentID::Car(id) => self + .driving_state + .get_draw_car(id, self.time, map) + .map(|c| c.body.last_pt()), + AgentID::Pedestrian(id) => self + .walking_state + .get_draw_ped(id, map, self.time) + .map(|p| p.pos), + } + } }