From e292d06cfbd391869ec96bc7ec0457e7b61fa183 Mon Sep 17 00:00:00 2001 From: Dustin Carlino Date: Fri, 13 Apr 2018 21:32:35 -0700 Subject: [PATCH] change intersection policy from trait to enum, so serde works --- TODO.md | 1 + sim/src/straw_intersections.rs | 36 +++++++++++++++++++++++++--------- sim/src/straw_model.rs | 9 ++++----- 3 files changed, 32 insertions(+), 14 deletions(-) diff --git a/TODO.md b/TODO.md index 7b7616672c..d349a7726a 100644 --- a/TODO.md +++ b/TODO.md @@ -4,6 +4,7 @@ - cars seemingly whiz past the queue at stop signs (possibly also signals) - at signals, cars doing the same turn wont start it until the last car finishes it +- draw cars in intersections, even when slightly zoomed out - draw cars in slightly different colors, to distinguish them better - traffic signals diff --git a/sim/src/straw_intersections.rs b/sim/src/straw_intersections.rs index 1a2dcabee1..3b3e62d1ca 100644 --- a/sim/src/straw_intersections.rs +++ b/sim/src/straw_intersections.rs @@ -28,19 +28,41 @@ const WAIT_AT_STOP_SIGN: si::Second = si::Second { _marker: std::marker::PhantomData, }; -pub trait IntersectionPolicy { +// Use an enum instead of traits so that serialization works. I couldn't figure out erased_serde. +#[derive(Serialize, Deserialize)] +pub enum IntersectionPolicy { + StopSignPolicy(StopSign), + TrafficSignalPolicy(TrafficSignal), +} + +impl IntersectionPolicy { // This must only be called when the car is ready to enter the intersection. - fn can_do_turn( + pub fn can_do_turn( &mut self, car: CarID, turn: TurnID, time: si::Second, geom_map: &GeomMap, control_map: &ControlMap, - ) -> bool; + ) -> bool { + match *self { + IntersectionPolicy::StopSignPolicy(ref mut p) => p.can_do_turn(car, turn, time, geom_map, control_map), + IntersectionPolicy::TrafficSignalPolicy(ref mut p) => p.can_do_turn(car, turn, time, geom_map, control_map), + } + } - fn on_enter(&self, car: CarID); - fn on_exit(&mut self, car: CarID); + pub fn on_enter(&self, car: CarID) { + match *self { + IntersectionPolicy::StopSignPolicy(ref p) => p.on_enter(car), + IntersectionPolicy::TrafficSignalPolicy(ref p) => p.on_enter(car), + } + } + pub fn on_exit(&mut self, car: CarID) { + match *self { + IntersectionPolicy::StopSignPolicy(ref mut p) => p.on_exit(car), + IntersectionPolicy::TrafficSignalPolicy(ref mut p) => p.on_exit(car), + } + } } #[derive(Serialize, Deserialize)] @@ -86,9 +108,7 @@ impl StopSign { }) .is_some() } -} -impl IntersectionPolicy for StopSign { fn can_do_turn( &mut self, car: CarID, @@ -153,9 +173,7 @@ impl TrafficSignal { accepted: HashMap::new(), } } -} -impl IntersectionPolicy for TrafficSignal { // TODO determine if cars are staying in the intersection past the cycle time. fn can_do_turn( diff --git a/sim/src/straw_model.rs b/sim/src/straw_model.rs index 2168e42a58..214047c9af 100644 --- a/sim/src/straw_model.rs +++ b/sim/src/straw_model.rs @@ -290,8 +290,7 @@ pub struct Sim { cars: HashMap, roads: Vec, turns: Vec, - // TODO figure this out - #[serde(skip_serializing, skip_deserializing)] intersections: Vec>, + intersections: Vec, #[serde(serialize_with = "serialize_s", deserialize_with = "deserialize_s")] pub time: si::Second, id_counter: usize, @@ -300,12 +299,12 @@ pub struct Sim { impl Sim { pub fn new(map: &Map, geom_map: &GeomMap) -> Sim { - let mut intersections: Vec> = Vec::new(); + let mut intersections: Vec = Vec::new(); for i in map.all_intersections() { if i.has_traffic_signal { - intersections.push(Box::new(TrafficSignal::new(i.id))); + intersections.push(IntersectionPolicy::TrafficSignalPolicy(TrafficSignal::new(i.id))); } else { - intersections.push(Box::new(StopSign::new(i.id))); + intersections.push(IntersectionPolicy::StopSignPolicy(StopSign::new(i.id))); } }