From d3611c6084090e5dab4a8e8f88b5fcd21c758029 Mon Sep 17 00:00:00 2001 From: Dustin Carlino Date: Tue, 14 Aug 2018 21:19:53 -0700 Subject: [PATCH] easier queries for lane type --- editor/src/render/lane.rs | 6 ++---- editor/src/ui.rs | 10 +++++----- map_model/src/edits.rs | 4 ++-- map_model/src/lane.rs | 16 ++++++++++++++++ map_model/src/make/buildings.rs | 6 ++---- map_model/src/make/turns.rs | 9 +++------ map_model/src/map.rs | 2 +- sim/src/sim.rs | 12 +++--------- 8 files changed, 34 insertions(+), 31 deletions(-) diff --git a/editor/src/render/lane.rs b/editor/src/render/lane.rs index 6bc11d882e..b76555a8da 100644 --- a/editor/src/render/lane.rs +++ b/editor/src/render/lane.rs @@ -62,9 +62,7 @@ impl DrawLane { markings.push(m); } // TODO not all sides of the lane have to stop - if lane.lane_type == map_model::LaneType::Driving - && !map.get_i(lane.dst_i).has_traffic_signal - { + if lane.is_driving() && !map.get_i(lane.dst_i).has_traffic_signal { if let Some(m) = calculate_stop_sign_line(lane) { markings.push(m); } @@ -280,7 +278,7 @@ fn calculate_stop_sign_line(lane: &map_model::Lane) -> Option { } fn calculate_id_positions(lane: &map_model::Lane) -> Option> { - if lane.lane_type != map_model::LaneType::Driving { + if !lane.is_driving() { return None; } diff --git a/editor/src/ui.rs b/editor/src/ui.rs index 099da44ff6..68fc219226 100644 --- a/editor/src/ui.rs +++ b/editor/src/ui.rs @@ -523,11 +523,11 @@ impl gui::GUI for UI { return gui::EventLoopMode::InputOnly; } - if self.map.get_l(id).lane_type == map_model::LaneType::Sidewalk { - if input.key_pressed(Key::A, "spawn a pedestrian here") { - self.sim_ctrl.sim.spawn_pedestrian(&self.map, id); - return gui::EventLoopMode::InputOnly; - } + if self.map.get_l(id).is_sidewalk() + && input.key_pressed(Key::A, "spawn a pedestrian here") + { + self.sim_ctrl.sim.spawn_pedestrian(&self.map, id); + return gui::EventLoopMode::InputOnly; } } SelectionState::SelectedIntersection(id) => { diff --git a/map_model/src/edits.rs b/map_model/src/edits.rs index 8c185e82ab..331df9c562 100644 --- a/map_model/src/edits.rs +++ b/map_model/src/edits.rs @@ -61,7 +61,7 @@ impl RoadEdit { lane: &Lane, new_type: LaneType, ) -> Option { - if lane.lane_type == LaneType::Sidewalk { + if lane.is_sidewalk() { println!("Sidewalks are fixed; can't change their type"); return None; } @@ -98,7 +98,7 @@ impl RoadEdit { fn delete_lane(r: &Road, lane: &Lane) -> Option { // Sidewalks are fixed - if lane.lane_type == LaneType::Sidewalk { + if lane.is_sidewalk() { println!("Can't delete sidewalks"); return None; } diff --git a/map_model/src/lane.rs b/map_model/src/lane.rs index da9baaff1c..30608ed80d 100644 --- a/map_model/src/lane.rs +++ b/map_model/src/lane.rs @@ -125,4 +125,20 @@ impl Lane { 0 } } + + pub fn is_driving(&self) -> bool { + self.lane_type == LaneType::Driving + } + + pub fn is_biking(&self) -> bool { + self.lane_type == LaneType::Biking + } + + pub fn is_sidewalk(&self) -> bool { + self.lane_type == LaneType::Sidewalk + } + + pub fn is_parking(&self) -> bool { + self.lane_type == LaneType::Parking + } } diff --git a/map_model/src/make/buildings.rs b/map_model/src/make/buildings.rs index 9c1ae2eed8..0870b83d7e 100644 --- a/map_model/src/make/buildings.rs +++ b/map_model/src/make/buildings.rs @@ -4,7 +4,7 @@ use geometry; use ordered_float::NotNaN; use raw_data; use std::collections::BTreeMap; -use {Building, BuildingID, Lane, LaneID, LaneType, Road}; +use {Building, BuildingID, Lane, LaneID, Road}; pub(crate) fn make_building( b: &raw_data::Building, @@ -47,9 +47,7 @@ fn find_front_path( let candidates: Vec<(LaneID, geo::Point)> = lanes .iter() .filter_map(|l| { - if l.lane_type == LaneType::Sidewalk - && roads[l.parent.0].osm_tags.get("name") == Some(street_name) - { + if l.is_sidewalk() && roads[l.parent.0].osm_tags.get("name") == Some(street_name) { if let geo::Closest::SinglePoint(pt) = lane_to_line_string(&lanes[l.id.0]).closest_point(¢er_pt) { diff --git a/map_model/src/make/turns.rs b/map_model/src/make/turns.rs index f8c0cd0739..d93a6f3bec 100644 --- a/map_model/src/make/turns.rs +++ b/map_model/src/make/turns.rs @@ -26,12 +26,12 @@ fn make_driving_turns(i: &Intersection, m: &Map) -> Vec { let incoming: Vec = i.incoming_lanes .iter() // TODO why's this double borrow happen? - .filter(|id| m.get_l(**id).lane_type == LaneType::Driving) + .filter(|id| m.get_l(**id).is_driving()) .map(|id| *id) .collect(); let outgoing: Vec = i.outgoing_lanes .iter() - .filter(|id| m.get_l(**id).lane_type == LaneType::Driving) + .filter(|id| m.get_l(**id).is_driving()) .map(|id| *id) .collect(); @@ -95,10 +95,7 @@ fn make_biking_turns(i: &Intersection, m: &Map) -> Vec { // will create those, and duplicates are bad. Filter them out here. make_turns(m, i.id, &incoming, &outgoing) .into_iter() - .filter(|t| { - m.get_l(t.src).lane_type == LaneType::Biking - || m.get_l(t.dst).lane_type == LaneType::Biking - }) + .filter(|t| m.get_l(t.src).is_biking() || m.get_l(t.dst).is_biking()) .collect() } diff --git a/map_model/src/map.rs b/map_model/src/map.rs index 7784637b58..87da3a9f0b 100644 --- a/map_model/src/map.rs +++ b/map_model/src/map.rs @@ -289,7 +289,7 @@ impl Map { .filter(|t| t.src == l) .collect(); // Sidewalks are bidirectional - if lane.lane_type == LaneType::Sidewalk { + if lane.is_sidewalk() { for t in &self.get_i(lane.src_i).turns { let turn = self.get_t(*t); if turn.src == l { diff --git a/sim/src/sim.rs b/sim/src/sim.rs index 560c3e7374..8992135178 100644 --- a/sim/src/sim.rs +++ b/sim/src/sim.rs @@ -195,13 +195,7 @@ impl Sim { let driving_lanes: Vec = map.all_lanes() .iter() - .filter_map(|l| { - if l.lane_type == LaneType::Driving { - Some(l.id) - } else { - None - } - }) + .filter_map(|l| if l.is_driving() { Some(l.id) } else { None }) .collect(); let mut requested_paths: Vec<(CarID, LaneID, LaneID)> = Vec::new(); for i in 0..num_cars.min(cars_and_starts.len()) { @@ -273,7 +267,7 @@ impl Sim { } pub fn spawn_pedestrian(&mut self, map: &Map, sidewalk: LaneID) -> bool { - assert!(map.get_l(sidewalk).lane_type == LaneType::Sidewalk); + assert!(map.get_l(sidewalk).is_sidewalk()); if let Some(path) = pick_goal_and_find_path(&mut self.rng, map, sidewalk) { self.walking_state @@ -290,7 +284,7 @@ impl Sim { let mut sidewalks: Vec = Vec::new(); for l in map.all_lanes() { - if l.lane_type == LaneType::Sidewalk { + if l.is_sidewalk() { sidewalks.push(l.id); } }