dont look up full SimStats unless needed

This commit is contained in:
Dustin Carlino 2019-02-01 13:37:55 -08:00
parent 2d7f64e69b
commit 58c4cb5e1b
5 changed files with 35 additions and 20 deletions

View File

@ -4,6 +4,7 @@
- cache draw stuff
- lazy SimStats
- dont consider it when comparing or serializing
## Quick n easy

View File

@ -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),
}
}
}

View File

@ -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
};

View File

@ -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

View File

@ -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<Pt2D> {
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 {
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<Pt2D> {
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),
}
}
}