From 98e073d8bec58a9d0c38e5c052ca04c5627a7a00 Mon Sep 17 00:00:00 2001 From: Dustin Carlino Date: Tue, 11 Jun 2019 17:25:09 -0700 Subject: [PATCH] save hints from debug_initialmap, use them when constructing the real map --- .gitignore | 1 + abstutil/src/io.rs | 9 +++++++++ abstutil/src/lib.rs | 7 ++++--- convert_osm/src/lib.rs | 8 +------- debug_initialmap/src/main.rs | 25 ++++++++++++++++++++++--- map_model/src/make/initial/mod.rs | 21 +++++++++++++++++++++ map_model/src/make/mod.rs | 2 +- map_model/src/map.rs | 21 +++++++++------------ map_model/src/raw_data.rs | 2 +- 9 files changed, 69 insertions(+), 27 deletions(-) diff --git a/.gitignore b/.gitignore index f7b51f2882..56125d4ed3 100644 --- a/.gitignore +++ b/.gitignore @@ -6,6 +6,7 @@ editor_state.json data/ab_tests/* data/edits/* +data/hints/* data/input/* data/maps/* data/neighborhoods/* diff --git a/abstutil/src/io.rs b/abstutil/src/io.rs index 448c5145eb..1ea1cd3c67 100644 --- a/abstutil/src/io.rs +++ b/abstutil/src/io.rs @@ -317,3 +317,12 @@ fn list_dir(dir: &std::path::Path) -> Vec { files.sort(); files } + +pub fn basename(path: &str) -> String { + Path::new(path) + .file_stem() + .unwrap() + .to_os_string() + .into_string() + .unwrap() +} diff --git a/abstutil/src/lib.rs b/abstutil/src/lib.rs index 35a40bddb4..8a61b93c1c 100644 --- a/abstutil/src/lib.rs +++ b/abstutil/src/lib.rs @@ -11,9 +11,10 @@ pub use crate::clone::Cloneable; pub use crate::collections::{contains_duplicates, retain_btreemap, wraparound_get, MultiMap}; pub use crate::error::Error; pub use crate::io::{ - deserialize_btreemap, deserialize_multimap, find_next_file, find_prev_file, list_all_objects, - load_all_objects, read_binary, read_json, save_binary_object, save_json_object, - serialize_btreemap, serialize_multimap, to_json, write_binary, write_json, FileWithProgress, + basename, deserialize_btreemap, deserialize_multimap, find_next_file, find_prev_file, + list_all_objects, load_all_objects, read_binary, read_json, save_binary_object, + save_json_object, serialize_btreemap, serialize_multimap, to_json, write_binary, write_json, + FileWithProgress, }; pub use crate::logs::Warn; pub use crate::notes::note; diff --git a/convert_osm/src/lib.rs b/convert_osm/src/lib.rs index 385e177e29..51973714b6 100644 --- a/convert_osm/src/lib.rs +++ b/convert_osm/src/lib.rs @@ -12,7 +12,6 @@ use kml::ExtraShapes; use map_model::{raw_data, IntersectionType, LANE_THICKNESS}; use std::fs::File; use std::io::{BufRead, BufReader}; -use std::path::Path; use structopt::StructOpt; const MAX_DIST_BTWN_INTERSECTION_AND_SIGNAL: Distance = Distance::const_meters(50.0); @@ -94,12 +93,7 @@ pub fn convert(flags: &Flags, timer: &mut abstutil::Timer) -> raw_data::Map { if !flags.neighborhoods.is_empty() { timer.start("convert neighborhood polygons"); - let map_name = Path::new(&flags.output) - .file_stem() - .unwrap() - .to_os_string() - .into_string() - .unwrap(); + let map_name = abstutil::basename(&flags.output); neighborhoods::convert(&flags.neighborhoods, map_name, &gps_bounds); timer.stop("convert neighborhood polygons"); } diff --git a/debug_initialmap/src/main.rs b/debug_initialmap/src/main.rs index bbbb00fe55..7a2478d611 100644 --- a/debug_initialmap/src/main.rs +++ b/debug_initialmap/src/main.rs @@ -1,7 +1,7 @@ use abstutil::Timer; use ezgui::{Color, EventCtx, EventLoopMode, GfxCtx, Key, Text, GUI}; use geom::{Distance, Polygon}; -use map_model::raw_data::{InitialMap, StableIntersectionID, StableRoadID}; +use map_model::raw_data::{Hint, Hints, InitialMap, StableIntersectionID, StableRoadID}; use std::collections::HashSet; use std::{env, process}; use viewer::World; @@ -12,6 +12,7 @@ const MIN_ROAD_LENGTH: Distance = Distance::const_meters(13.0); struct UI { world: World, data: InitialMap, + hints: Hints, // TODO Or, if these are common things, the World could also hold this state. selected: Option, hide: HashSet, @@ -22,18 +23,25 @@ impl UI { fn new(filename: &str, ctx: &mut EventCtx) -> UI { let mut timer = Timer::new(&format!("load {}", filename)); let raw: map_model::raw_data::Map = abstutil::read_binary(filename, &mut timer).unwrap(); + let map_name = abstutil::basename(filename); let gps_bounds = raw.get_gps_bounds(); - let data = InitialMap::new( - filename.to_string(), + let mut data = InitialMap::new( + map_name, &raw, &gps_bounds, &gps_bounds.to_bounds(), &mut timer, ); + let hints: Hints = abstutil::read_json(&format!("../data/hints/{}.json", data.name)) + .unwrap_or(Hints { hints: Vec::new() }); + data.apply_hints(&hints); + let world = initial_map_to_world(&data, ctx); + UI { world, data, + hints, selected: None, hide: HashSet::new(), osd: Text::new(), @@ -52,6 +60,16 @@ impl GUI for UI { if ctx.input.unimportant_key_pressed(Key::Escape, "quit") { process::exit(0); } + if ctx + .input + .key_pressed(Key::S, &format!("save {} hints", self.hints.hints.len())) + { + abstutil::write_json( + &format!("../data/hints/{}.json", self.data.name), + &self.hints, + ) + .unwrap(); + } if let Some(id) = self.selected { if ctx.input.key_pressed(Key::H, "hide this") { @@ -61,6 +79,7 @@ impl GUI for UI { } if let Some(ID::HalfRoad(r, _)) = self.selected { if ctx.input.key_pressed(Key::M, "merge") { + self.hints.hints.push(Hint::MergeRoad(r)); self.data.merge_road(r); self.world = initial_map_to_world(&self.data, ctx); self.selected = None; diff --git a/map_model/src/make/initial/mod.rs b/map_model/src/make/initial/mod.rs index 7101618f20..9a638ea5ef 100644 --- a/map_model/src/make/initial/mod.rs +++ b/map_model/src/make/initial/mod.rs @@ -7,6 +7,7 @@ use crate::raw_data::{StableIntersectionID, StableRoadID}; use crate::{raw_data, LANE_THICKNESS}; use abstutil::Timer; use geom::{Bounds, Distance, GPSBounds, PolyLine, Pt2D}; +use serde_derive::{Deserialize, Serialize}; use std::collections::{BTreeMap, BTreeSet}; pub struct InitialMap { @@ -148,4 +149,24 @@ impl InitialMap { pub fn merge_road(&mut self, r: StableRoadID) { merge::merge(self, r, &mut Timer::throwaway()); } + + pub fn apply_hints(&mut self, hints: &Hints) { + for h in &hints.hints { + match h { + Hint::MergeRoad(r) => { + self.merge_road(*r); + } + } + } + } +} + +#[derive(Serialize, Deserialize, Debug)] +pub struct Hints { + pub hints: Vec, +} + +#[derive(Serialize, Deserialize, Debug)] +pub enum Hint { + MergeRoad(StableRoadID), } diff --git a/map_model/src/make/mod.rs b/map_model/src/make/mod.rs index f962f52811..514a02ac43 100644 --- a/map_model/src/make/mod.rs +++ b/map_model/src/make/mod.rs @@ -9,5 +9,5 @@ pub use self::buildings::make_all_buildings; pub use self::bus_stops::{make_bus_stops, verify_bus_routes}; pub use self::half_map::make_half_map; pub use self::initial::lane_specs::{get_lane_types, RoadSpec}; -pub use self::initial::InitialMap; +pub use self::initial::{Hint, Hints, InitialMap}; pub use self::turns::make_all_turns; diff --git a/map_model/src/map.rs b/map_model/src/map.rs index 523e5d045b..8998fc4d56 100644 --- a/map_model/src/map.rs +++ b/map_model/src/map.rs @@ -12,7 +12,6 @@ use geom::{Bounds, GPSBounds, Polygon}; use serde_derive::{Deserialize, Serialize}; use std::collections::{BTreeMap, BTreeSet, HashSet, VecDeque}; use std::io; -use std::path; #[derive(Serialize, Deserialize, Debug)] pub struct Map { @@ -52,23 +51,21 @@ pub struct Map { impl Map { pub fn new(path: &str, timer: &mut Timer) -> Result { let data: raw_data::Map = abstutil::read_binary(path, timer)?; - Ok(Map::create_from_raw( - path::Path::new(path) - .file_stem() - .unwrap() - .to_os_string() - .into_string() - .unwrap(), - data, - timer, - )) + Ok(Map::create_from_raw(abstutil::basename(path), data, timer)) } pub fn create_from_raw(name: String, data: raw_data::Map, timer: &mut Timer) -> Map { timer.start("raw_map to InitialMap"); let gps_bounds = data.get_gps_bounds(); let bounds = gps_bounds.to_bounds(); - let initial_map = make::InitialMap::new(name.clone(), &data, &gps_bounds, &bounds, timer); + let mut initial_map = + make::InitialMap::new(name.clone(), &data, &gps_bounds, &bounds, timer); + if let Ok(hints) = + abstutil::read_json::(&format!("../data/hints/{}.json", name)) + { + timer.note(format!("Applying {} hints", hints.hints.len())); + initial_map.apply_hints(&hints); + } timer.stop("raw_map to InitialMap"); timer.start("InitialMap to HalfMap"); diff --git a/map_model/src/raw_data.rs b/map_model/src/raw_data.rs index c43c916f9f..9a69f9025d 100644 --- a/map_model/src/raw_data.rs +++ b/map_model/src/raw_data.rs @@ -1,5 +1,5 @@ use crate::make::get_lane_types; -pub use crate::make::InitialMap; +pub use crate::make::{Hint, Hints, InitialMap}; use crate::{AreaType, IntersectionType, RoadSpec}; use geom::{Distance, GPSBounds, LonLat}; use gtfs::Route;