From 8244a900cfad5f8ec3d90877af4aa59322a83e41 Mon Sep 17 00:00:00 2001 From: Dustin Carlino Date: Thu, 21 Apr 2022 14:32:48 +0100 Subject: [PATCH] Refactor constructors for RawRoad and RawIntersection. #893 --- apps/map_editor/src/model.rs | 26 +++++++------------------- convert_osm/src/extract.rs | 17 ++--------------- convert_osm/src/split_ways.rs | 25 +++++++------------------ raw_map/src/lib.rs | 25 +++++++++++++++++++++++++ 4 files changed, 41 insertions(+), 52 deletions(-) diff --git a/apps/map_editor/src/model.rs b/apps/map_editor/src/model.rs index 6ae283e8d4..6f3342cdad 100644 --- a/apps/map_editor/src/model.rs +++ b/apps/map_editor/src/model.rs @@ -1,4 +1,4 @@ -use std::collections::{BTreeMap, HashMap}; +use std::collections::HashMap; use std::io::Write; use abstio::{CityName, MapName}; @@ -185,15 +185,9 @@ impl Model { pub fn create_i(&mut self, ctx: &EventCtx, point: Pt2D) { let id = self.map.new_osm_node_id(time_to_id()); - self.map.intersections.insert( - id, - RawIntersection { - point, - intersection_type: IntersectionType::StopSign, - elevation: Distance::ZERO, - trim_roads_for_merging: BTreeMap::new(), - }, - ); + self.map + .intersections + .insert(id, RawIntersection::new(point, IntersectionType::StopSign)); self.intersection_added(ctx, id); } @@ -338,19 +332,13 @@ impl Model { self.map.roads.insert( id, - RawRoad { - center_points: vec![ + RawRoad::new( + vec![ self.map.intersections[&i1].point, self.map.intersections[&i2].point, ], - scale_width: 1.0, osm_tags, - turn_restrictions: Vec::new(), - complicated_turn_restrictions: Vec::new(), - percent_incline: 0.0, - crosswalk_forward: true, - crosswalk_backward: true, - }, + ), ); self.road_added(ctx, id); diff --git a/convert_osm/src/extract.rs b/convert_osm/src/extract.rs index 3c3120b3d6..f98c55f9b0 100644 --- a/convert_osm/src/extract.rs +++ b/convert_osm/src/extract.rs @@ -109,21 +109,8 @@ pub fn extract_osm( way.tags.insert(osm::SIDEWALK, "right"); } - out.roads.push(( - id, - RawRoad { - center_points: way.pts.clone(), - scale_width: 1.0, - osm_tags: way.tags.clone(), - turn_restrictions: Vec::new(), - complicated_turn_restrictions: Vec::new(), - percent_incline: 0.0, - // Start assuming there's a crosswalk everywhere, and maybe filter it down - // later - crosswalk_forward: true, - crosswalk_backward: true, - }, - )); + out.roads + .push((id, RawRoad::new(way.pts.clone(), way.tags.clone()))); continue; } else if way.tags.is(osm::HIGHWAY, "service") { // If we got here, is_road didn't interpret it as a normal road diff --git a/convert_osm/src/split_ways.rs b/convert_osm/src/split_ways.rs index 6531a3695c..b913e8c6cb 100644 --- a/convert_osm/src/split_ways.rs +++ b/convert_osm/src/split_ways.rs @@ -1,4 +1,4 @@ -use std::collections::{hash_map::Entry, BTreeMap, HashMap, HashSet}; +use std::collections::{hash_map::Entry, HashMap, HashSet}; use abstutil::{Counter, Timer}; use geom::{Distance, HashablePt2D, Pt2D}; @@ -61,32 +61,21 @@ pub fn split_up_roads(map: &mut RawMap, mut input: OsmExtract, timer: &mut Timer for (pt, id) in &pt_to_intersection { map.intersections.insert( *id, - RawIntersection { - point: pt.to_pt2d(), - intersection_type: if input.traffic_signals.remove(pt).is_some() { + RawIntersection::new( + pt.to_pt2d(), + if input.traffic_signals.remove(pt).is_some() { IntersectionType::TrafficSignal } else { IntersectionType::StopSign }, - // Filled out later - elevation: Distance::ZERO, - trim_roads_for_merging: BTreeMap::new(), - }, + ), ); } // Set roundabouts to their center for (id, point) in roundabout_centers { - map.intersections.insert( - id, - RawIntersection { - point, - intersection_type: IntersectionType::StopSign, - // Filled out later - elevation: Distance::ZERO, - trim_roads_for_merging: BTreeMap::new(), - }, - ); + map.intersections + .insert(id, RawIntersection::new(point, IntersectionType::StopSign)); } let mut pt_to_road: HashMap = HashMap::new(); diff --git a/raw_map/src/lib.rs b/raw_map/src/lib.rs index 6d2db231c0..0c5224a3f0 100644 --- a/raw_map/src/lib.rs +++ b/raw_map/src/lib.rs @@ -381,6 +381,21 @@ pub struct RawRoad { } impl RawRoad { + pub fn new(osm_center_points: Vec, osm_tags: Tags) -> Self { + Self { + center_points: osm_center_points, + scale_width: 1.0, + osm_tags, + turn_restrictions: Vec::new(), + complicated_turn_restrictions: Vec::new(), + percent_incline: 0.0, + // Start assuming there's a crosswalk everywhere, and maybe filter it down + // later + crosswalk_forward: true, + crosswalk_backward: true, + } + } + // TODO For the moment, treating all rail things as light rail pub fn is_light_rail(&self) -> bool { self.osm_tags.is_any("railway", vec!["light_rail", "rail"]) @@ -469,6 +484,16 @@ pub struct RawIntersection { } impl RawIntersection { + pub fn new(point: Pt2D, intersection_type: IntersectionType) -> Self { + Self { + point, + intersection_type, + // Filled out later + elevation: Distance::ZERO, + trim_roads_for_merging: BTreeMap::new(), + } + } + fn is_border(&self) -> bool { self.intersection_type == IntersectionType::Border }