refactor sim->editor metadata for unzoomed/zoomed agents

This commit is contained in:
Dustin Carlino 2019-09-07 12:01:45 -07:00
parent 147a5781ca
commit c5fd22ae81
7 changed files with 52 additions and 41 deletions

View File

@ -18,7 +18,9 @@ use map_model::{
AreaID, BuildingID, BusStopID, DirectedRoadID, IntersectionID, IntersectionType, Lane, LaneID, AreaID, BuildingID, BusStopID, DirectedRoadID, IntersectionID, IntersectionType, Lane, LaneID,
Map, RoadID, Traversable, Turn, TurnID, TurnType, LANE_THICKNESS, Map, RoadID, Traversable, Turn, TurnID, TurnType, LANE_THICKNESS,
}; };
use sim::{CarStatus, DrawCarInput, DrawPedestrianInput, UnzoomedAgent, VehicleType}; use sim::{
AgentMetadata, CarStatus, DrawCarInput, DrawPedestrianInput, UnzoomedAgent, VehicleType,
};
use std::borrow::Borrow; use std::borrow::Borrow;
use std::cell::RefCell; use std::cell::RefCell;
use std::collections::HashMap; use std::collections::HashMap;
@ -434,9 +436,7 @@ impl AgentColorScheme {
Some(VehicleType::Bus) => cs.get_def("unzoomed bus", Color::BLUE.alpha(0.5)), Some(VehicleType::Bus) => cs.get_def("unzoomed bus", Color::BLUE.alpha(0.5)),
None => cs.get_def("unzoomed pedestrian", Color::ORANGE.alpha(0.5)), None => cs.get_def("unzoomed pedestrian", Color::ORANGE.alpha(0.5)),
}, },
AgentColorScheme::Delay => delay_color(agent.time_spent_blocked), _ => self.by_metadata(&agent.metadata),
AgentColorScheme::DistanceCrossedSoFar => percent_color(agent.percent_dist_crossed),
AgentColorScheme::TripTimeSoFar => delay_color(agent.trip_time_so_far),
} }
} }
@ -454,9 +454,7 @@ impl AgentColorScheme {
} }
} }
} }
AgentColorScheme::Delay => delay_color(input.time_spent_blocked), _ => self.by_metadata(&input.metadata),
AgentColorScheme::DistanceCrossedSoFar => percent_color(input.percent_dist_crossed),
AgentColorScheme::TripTimeSoFar => delay_color(input.trip_time_so_far),
} }
} }
@ -469,9 +467,7 @@ impl AgentColorScheme {
CarStatus::Stuck => cs.get_def("stuck bike", Color::RED), CarStatus::Stuck => cs.get_def("stuck bike", Color::RED),
CarStatus::Parked => panic!("Can't have a parked bike {}", input.id), CarStatus::Parked => panic!("Can't have a parked bike {}", input.id),
}, },
AgentColorScheme::Delay => delay_color(input.time_spent_blocked), _ => self.by_metadata(&input.metadata),
AgentColorScheme::DistanceCrossedSoFar => percent_color(input.percent_dist_crossed),
AgentColorScheme::TripTimeSoFar => delay_color(input.trip_time_so_far),
} }
} }
@ -484,9 +480,16 @@ impl AgentColorScheme {
cs.get_def("pedestrian", Color::rgb_f(0.2, 0.7, 0.7)) cs.get_def("pedestrian", Color::rgb_f(0.2, 0.7, 0.7))
} }
} }
AgentColorScheme::Delay => delay_color(input.time_spent_blocked), _ => self.by_metadata(&input.metadata),
AgentColorScheme::DistanceCrossedSoFar => percent_color(input.percent_dist_crossed), }
AgentColorScheme::TripTimeSoFar => delay_color(input.trip_time_so_far), }
fn by_metadata(self, md: &AgentMetadata) -> Color {
match self {
AgentColorScheme::VehicleTypes => unreachable!(),
AgentColorScheme::Delay => delay_color(md.time_spent_blocked),
AgentColorScheme::DistanceCrossedSoFar => percent_color(md.percent_dist_crossed),
AgentColorScheme::TripTimeSoFar => delay_color(md.trip_time_so_far),
} }
} }

View File

@ -23,7 +23,8 @@ pub(crate) use self::transit::TransitSimState;
pub use self::trips::{FinishedTrips, TripEnd, TripMode, TripStart, TripStatus}; pub use self::trips::{FinishedTrips, TripEnd, TripMode, TripStart, TripStatus};
pub(crate) use self::trips::{TripLeg, TripManager}; pub(crate) use self::trips::{TripLeg, TripManager};
pub use crate::render::{ pub use crate::render::{
CarStatus, DrawCarInput, DrawPedCrowdInput, DrawPedestrianInput, GetDrawAgents, UnzoomedAgent, AgentMetadata, CarStatus, DrawCarInput, DrawPedCrowdInput, DrawPedestrianInput, GetDrawAgents,
UnzoomedAgent,
}; };
use abstutil::Cloneable; use abstutil::Cloneable;
use geom::{Distance, Duration, Pt2D, Speed}; use geom::{Distance, Duration, Pt2D, Speed};

View File

@ -1,6 +1,6 @@
use crate::{ use crate::{
CarStatus, DistanceInterval, DrawCarInput, ParkingSpot, Router, TimeInterval, TransitSimState, AgentMetadata, CarStatus, DistanceInterval, DrawCarInput, ParkingSpot, Router, TimeInterval,
TripID, Vehicle, VehicleType, TransitSimState, TripID, Vehicle, VehicleType,
}; };
use geom::{Distance, Duration, PolyLine}; use geom::{Distance, Duration, PolyLine};
use map_model::{Map, Traversable, LANE_THICKNESS}; use map_model::{Map, Traversable, LANE_THICKNESS};
@ -123,7 +123,6 @@ impl Car {
_ => raw_body, _ => raw_body,
}; };
let path = self.router.get_path();
DrawCarInput { DrawCarInput {
id: self.vehicle.id, id: self.vehicle.id,
waiting_for_turn: match self.state { waiting_for_turn: match self.state {
@ -156,11 +155,17 @@ impl Car {
None None
}, },
body, body,
metadata: self.metadata(now),
}
}
pub fn metadata(&self, now: Duration) -> AgentMetadata {
AgentMetadata {
time_spent_blocked: self time_spent_blocked: self
.blocked_since .blocked_since
.map(|t| now - t) .map(|t| now - t)
.unwrap_or(Duration::ZERO), .unwrap_or(Duration::ZERO),
percent_dist_crossed: path.percent_dist_crossed(), percent_dist_crossed: self.router.get_path().percent_dist_crossed(),
trip_time_so_far: now - self.started_at, trip_time_so_far: now - self.started_at,
} }
} }

View File

@ -650,16 +650,10 @@ impl DrivingSimState {
for (c, dist) in queue.get_car_positions(now, &self.cars, &self.queues) { for (c, dist) in queue.get_car_positions(now, &self.cars, &self.queues) {
let car = &self.cars[&c]; let car = &self.cars[&c];
let path = car.router.get_path();
result.push(UnzoomedAgent { result.push(UnzoomedAgent {
vehicle_type: Some(car.vehicle.vehicle_type), vehicle_type: Some(car.vehicle.vehicle_type),
pos: queue.id.dist_along(dist, map).0, pos: queue.id.dist_along(dist, map).0,
time_spent_blocked: car metadata: car.metadata(now),
.blocked_since
.map(|t| now - t)
.unwrap_or(Duration::ZERO),
percent_dist_crossed: path.percent_dist_crossed(),
trip_time_so_far: now - car.started_at,
}); });
} }
} }

View File

@ -1,4 +1,4 @@
use crate::{CarID, CarStatus, DrawCarInput, ParkedCar, ParkingSpot, Vehicle}; use crate::{AgentMetadata, CarID, CarStatus, DrawCarInput, ParkedCar, ParkingSpot, Vehicle};
use abstutil::{ use abstutil::{
deserialize_btreemap, deserialize_multimap, serialize_btreemap, serialize_multimap, MultiMap, deserialize_btreemap, deserialize_multimap, serialize_btreemap, serialize_multimap, MultiMap,
}; };
@ -161,9 +161,11 @@ impl ParkingSimState {
status: CarStatus::Parked, status: CarStatus::Parked,
on: Traversable::Lane(lane), on: Traversable::Lane(lane),
label: None, label: None,
time_spent_blocked: Duration::ZERO, metadata: AgentMetadata {
percent_dist_crossed: 0.0, time_spent_blocked: Duration::ZERO,
trip_time_so_far: Duration::ZERO, percent_dist_crossed: 0.0,
trip_time_so_far: Duration::ZERO,
},
body: map body: map
.get_l(lane) .get_l(lane)

View File

@ -1,7 +1,8 @@
use crate::{ use crate::{
AgentID, Command, CreatePedestrian, DistanceInterval, DrawPedCrowdInput, DrawPedestrianInput, AgentID, AgentMetadata, Command, CreatePedestrian, DistanceInterval, DrawPedCrowdInput,
IntersectionSimState, ParkingSimState, ParkingSpot, PedestrianID, Scheduler, SidewalkPOI, DrawPedestrianInput, IntersectionSimState, ParkingSimState, ParkingSpot, PedestrianID,
SidewalkSpot, TimeInterval, TransitSimState, TripID, TripManager, TripPositions, UnzoomedAgent, Scheduler, SidewalkPOI, SidewalkSpot, TimeInterval, TransitSimState, TripID, TripManager,
TripPositions, UnzoomedAgent,
}; };
use abstutil::{deserialize_multimap, serialize_multimap, MultiMap}; use abstutil::{deserialize_multimap, serialize_multimap, MultiMap};
use geom::{Distance, Duration, Line, PolyLine, Speed}; use geom::{Distance, Duration, Line, PolyLine, Speed};
@ -274,9 +275,7 @@ impl WalkingSimState {
peds.push(UnzoomedAgent { peds.push(UnzoomedAgent {
vehicle_type: None, vehicle_type: None,
pos: ped.get_draw_ped(now, map).pos, pos: ped.get_draw_ped(now, map).pos,
time_spent_blocked: ped.blocked_since.map(|t| now - t).unwrap_or(Duration::ZERO), metadata: ped.metadata(now),
percent_dist_crossed: ped.path.percent_dist_crossed(),
trip_time_so_far: now - ped.started_at,
}); });
} }
@ -508,6 +507,12 @@ impl Pedestrian {
_ => false, _ => false,
}, },
on, on,
metadata: self.metadata(now),
}
}
fn metadata(&self, now: Duration) -> AgentMetadata {
AgentMetadata {
time_spent_blocked: self time_spent_blocked: self
.blocked_since .blocked_since
.map(|t| now - t) .map(|t| now - t)

View File

@ -11,6 +11,11 @@ pub struct DrawPedestrianInput {
pub waiting_for_turn: Option<TurnID>, pub waiting_for_turn: Option<TurnID>,
pub preparing_bike: bool, pub preparing_bike: bool,
pub on: Traversable, pub on: Traversable,
pub metadata: AgentMetadata,
}
#[derive(Clone)]
pub struct AgentMetadata {
pub time_spent_blocked: Duration, pub time_spent_blocked: Duration,
pub percent_dist_crossed: f64, pub percent_dist_crossed: f64,
pub trip_time_so_far: Duration, pub trip_time_so_far: Duration,
@ -31,9 +36,7 @@ pub struct DrawCarInput {
pub status: CarStatus, pub status: CarStatus,
pub on: Traversable, pub on: Traversable,
pub label: Option<String>, pub label: Option<String>,
pub time_spent_blocked: Duration, pub metadata: AgentMetadata,
pub percent_dist_crossed: f64,
pub trip_time_so_far: Duration,
// Starts at the BACK of the car. // Starts at the BACK of the car.
pub body: PolyLine, pub body: PolyLine,
@ -51,9 +54,7 @@ pub struct UnzoomedAgent {
// None means a pedestrian. // None means a pedestrian.
pub vehicle_type: Option<VehicleType>, pub vehicle_type: Option<VehicleType>,
pub pos: Pt2D, pub pos: Pt2D,
pub time_spent_blocked: Duration, pub metadata: AgentMetadata,
pub percent_dist_crossed: f64,
pub trip_time_so_far: Duration,
} }
// TODO Can we return borrows instead? Nice for time travel, not for main sim? // TODO Can we return borrows instead? Nice for time travel, not for main sim?