changing turns to have IDs thatll be stable across edits

This commit is contained in:
Dustin Carlino 2018-07-25 18:44:31 -07:00
parent 3ed1bba5c9
commit de54b917bc
6 changed files with 55 additions and 51 deletions

View File

@ -424,5 +424,5 @@ I think this is working so far. The vital question: is it too complicated? Is th
so, I think the steps:
= see what's useful from this branch, bring it to master (encapsulating the driving state stuff)
- ditch TurnIDs; have a BTreeMap of src/dst (LaneID, LaneID)
= ditch TurnIDs; have a BTreeMap of src/dst (LaneID, LaneID)
- add a mutate_lanes() and replace_turns() to all the appropriate layers

View File

@ -15,7 +15,7 @@ use std::collections::HashMap;
pub struct DrawMap {
pub lanes: Vec<DrawLane>,
pub intersections: Vec<DrawIntersection>,
pub turns: Vec<DrawTurn>,
pub turns: HashMap<TurnID, DrawTurn>,
pub buildings: Vec<DrawBuilding>,
pub parcels: Vec<DrawParcel>,
@ -56,10 +56,10 @@ impl DrawMap {
}
assert_eq!(turn_to_lane_offset.len(), map.all_turns().len());
let turns: Vec<DrawTurn> = map.all_turns()
.iter()
.map(|t| DrawTurn::new(map, t, turn_to_lane_offset[&t.id]))
.collect();
let mut turns: HashMap<TurnID, DrawTurn> = HashMap::new();
for t in map.all_turns().values() {
turns.insert(t.id, DrawTurn::new(map, t, turn_to_lane_offset[&t.id]));
}
let intersections: Vec<DrawIntersection> = map.all_intersections()
.iter()
.map(|i| DrawIntersection::new(i, map, &lanes))
@ -93,7 +93,7 @@ impl DrawMap {
intersections_quadtree.insert_with_box(i.id, i.get_bbox());
}
let mut turn_icons_quadtree = QuadTree::default(map_bbox);
for t in &turns {
for t in turns.values() {
turn_icons_quadtree.insert_with_box(t.id, t.get_bbox());
}
let mut buildings_quadtree = QuadTree::default(map_bbox);
@ -133,7 +133,7 @@ impl DrawMap {
}
pub fn get_t(&self, id: TurnID) -> &DrawTurn {
&self.turns[id.0]
&self.turns[&id]
}
pub fn get_b(&self, id: BuildingID) -> &DrawBuilding {

View File

@ -2,17 +2,15 @@ use abstutil::MultiMap;
use geom::Line;
use {Intersection, IntersectionID, LaneID, LaneType, Map, RoadID, Turn, TurnID};
pub(crate) fn make_all_turns(i: &Intersection, m: &Map, turn_id_start: usize) -> Vec<Turn> {
pub(crate) fn make_all_turns(i: &Intersection, m: &Map) -> Vec<Turn> {
let mut turns: Vec<Turn> = Vec::new();
turns.extend(make_driving_turns(i, m, turn_id_start));
let len = turns.len();
turns.extend(make_biking_turns(i, m, turn_id_start + len));
let len = turns.len();
turns.extend(make_crosswalks(i, m, turn_id_start + len));
turns.extend(make_driving_turns(i, m));
turns.extend(make_biking_turns(i, m));
turns.extend(make_crosswalks(i, m));
turns
}
fn make_driving_turns(i: &Intersection, m: &Map, turn_id_start: usize) -> Vec<Turn> {
fn make_driving_turns(i: &Intersection, m: &Map) -> Vec<Turn> {
let incoming: Vec<LaneID> = i.incoming_lanes
.iter()
// TODO why's this double borrow happen?
@ -25,10 +23,10 @@ fn make_driving_turns(i: &Intersection, m: &Map, turn_id_start: usize) -> Vec<Tu
.map(|id| *id)
.collect();
make_turns(m, turn_id_start, i.id, &incoming, &outgoing)
make_turns(m, i.id, &incoming, &outgoing)
}
fn make_biking_turns(i: &Intersection, m: &Map, turn_id_start: usize) -> Vec<Turn> {
fn make_biking_turns(i: &Intersection, m: &Map) -> Vec<Turn> {
// TODO Road should make this easier, but how?
let mut incoming_bike_lanes_per_road: MultiMap<RoadID, LaneID> = MultiMap::new();
let mut incoming_driving_lanes_per_road: MultiMap<RoadID, LaneID> = MultiMap::new();
@ -77,12 +75,11 @@ fn make_biking_turns(i: &Intersection, m: &Map, turn_id_start: usize) -> Vec<Tur
incoming.sort();
outgoing.sort();
make_turns(m, turn_id_start, i.id, &incoming, &outgoing)
make_turns(m, i.id, &incoming, &outgoing)
}
fn make_turns(
map: &Map,
turn_id_start: usize,
parent: IntersectionID,
incoming: &Vec<LaneID>,
outgoing: &Vec<LaneID>,
@ -111,9 +108,8 @@ fn make_turns(
continue;
}
let id = TurnID(turn_id_start + result.len());
result.push(Turn {
id,
id: TurnID::new(*src, *dst),
parent,
src: *src,
dst: *dst,
@ -125,7 +121,7 @@ fn make_turns(
result
}
fn make_crosswalks(i: &Intersection, m: &Map, mut turn_id_start: usize) -> Vec<Turn> {
fn make_crosswalks(i: &Intersection, m: &Map) -> Vec<Turn> {
let mut result = Vec::new();
// TODO dedupe some of this logic render/intersection
@ -143,10 +139,8 @@ fn make_crosswalks(i: &Intersection, m: &Map, mut turn_id_start: usize) -> Vec<T
.unwrap(),
);
let id = TurnID(turn_id_start);
turn_id_start += 1;
result.push(Turn {
id,
id: TurnID::new(src.id, dst.id),
parent: i.id,
src: src.id,
dst: dst.id,
@ -177,10 +171,8 @@ fn make_crosswalks(i: &Intersection, m: &Map, mut turn_id_start: usize) -> Vec<T
continue;
}
let id = TurnID(turn_id_start);
turn_id_start += 1;
result.push(Turn {
id,
id: TurnID::new(src.id, dst.id),
parent: i.id,
src: src.id,
dst: dst.id,

View File

@ -12,7 +12,7 @@ use make;
use parcel::{Parcel, ParcelID};
use raw_data;
use road::{Road, RoadID};
use std::collections::HashMap;
use std::collections::{BTreeMap, HashMap};
use std::io::Error;
use turn::{Turn, TurnID};
@ -20,7 +20,7 @@ pub struct Map {
roads: Vec<Road>,
lanes: Vec<Lane>,
intersections: Vec<Intersection>,
turns: Vec<Turn>,
turns: BTreeMap<TurnID, Turn>,
buildings: Vec<Building>,
parcels: Vec<Parcel>,
@ -38,7 +38,7 @@ impl Map {
roads: Vec::new(),
lanes: Vec::new(),
intersections: Vec::new(),
turns: Vec::new(),
turns: BTreeMap::new(),
buildings: Vec::new(),
parcels: Vec::new(),
};
@ -139,11 +139,11 @@ impl Map {
}
for i in &m.intersections {
let len = m.turns.len();
let turns = make::make_all_turns(i, &m, len);
m.turns.extend(turns);
for t in make::make_all_turns(i, &m) {
m.turns.insert(t.id, t);
}
}
for t in &m.turns {
for t in m.turns.values() {
m.intersections[t.parent.0].turns.push(t.id);
}
@ -182,7 +182,7 @@ impl Map {
&self.intersections
}
pub fn all_turns(&self) -> &Vec<Turn> {
pub fn all_turns(&self) -> &BTreeMap<TurnID, Turn> {
&self.turns
}
@ -207,7 +207,7 @@ impl Map {
}
pub fn get_t(&self, id: TurnID) -> &Turn {
&self.turns[id.0]
&self.turns[&id]
}
pub fn get_b(&self, id: BuildingID) -> &Building {

View File

@ -7,13 +7,19 @@ use std::fmt;
use IntersectionID;
use LaneID;
// TODO reconsider pub usize. maybe outside world shouldnt know.
// Turns are uniquely identified by their (src, dst) lanes.
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq, PartialOrd, Ord, Serialize, Deserialize)]
pub struct TurnID(pub usize);
pub struct TurnID(LaneID, LaneID);
impl TurnID {
pub(crate) fn new(src: LaneID, dst: LaneID) -> TurnID {
TurnID(src, dst)
}
}
impl fmt::Display for TurnID {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "TurnID({0})", self.0)
write!(f, "TurnID({0}, {1})", self.0, self.1)
}
}
@ -25,7 +31,7 @@ pub struct Turn {
pub parent: IntersectionID,
pub src: LaneID,
pub dst: LaneID,
pub(crate) between_sidewalks: bool,
pub between_sidewalks: bool,
/// GeomTurn stuff
pub line: Line,

View File

@ -217,7 +217,7 @@ pub struct DrivingSimState {
// this later after step() doesnt need a RNG.
pub(crate) cars: BTreeMap<CarID, Car>,
lanes: Vec<SimQueue>,
turns: Vec<SimQueue>,
turns: BTreeMap<TurnID, SimQueue>,
intersections: Vec<IntersectionPolicy>,
}
@ -234,7 +234,7 @@ impl DrivingSimState {
}
}
DrivingSimState {
let mut s = DrivingSimState {
intersections,
cars: BTreeMap::new(),
@ -243,11 +243,14 @@ impl DrivingSimState {
.iter()
.map(|l| SimQueue::new(On::Lane(l.id), map))
.collect(),
turns: map.all_turns()
.iter()
.map(|t| SimQueue::new(On::Turn(t.id), map))
.collect(),
turns: BTreeMap::new(),
};
for t in map.all_turns().values() {
if !t.between_sidewalks {
s.turns.insert(t.id, SimQueue::new(On::Turn(t.id), map));
}
}
s
}
pub fn step(&mut self, time: Tick, map: &Map, control_map: &ControlMap) {
@ -269,11 +272,11 @@ impl DrivingSimState {
// new_car_entered_this_step. The last car won't go backwards.
let has_room_now = match on {
On::Lane(id) => self.lanes[id.0].room_at_end(time, &self.cars),
On::Turn(id) => self.turns[id.0].room_at_end(time, &self.cars),
On::Turn(id) => self.turns[&id].room_at_end(time, &self.cars),
};
let is_lead_vehicle = match c.on {
On::Lane(id) => self.lanes[id.0].cars_queue[0] == c.id,
On::Turn(id) => self.turns[id.0].cars_queue[0] == c.id,
On::Turn(id) => self.turns[&id].cars_queue[0] == c.id,
};
if has_room_now && is_lead_vehicle {
Action::Goto(on)
@ -359,7 +362,7 @@ impl DrivingSimState {
}
//l.reset(cars_per_lane.get_vec(&l.id).unwrap_or_else(|| &Vec::new()), &self.cars);
}
for t in &mut self.turns {
for t in self.turns.values_mut() {
if let Some(v) = cars_per_turn.get_vec(&t.id.as_turn()) {
t.reset(v, &self.cars);
} else {
@ -420,6 +423,9 @@ impl DrivingSimState {
}
pub fn get_draw_cars_on_turn(&self, turn: TurnID, time: Tick, map: &Map) -> Vec<DrawCar> {
self.turns[turn.0].get_draw_cars(time, self, map)
if let Some(queue) = self.turns.get(&turn) {
return queue.get_draw_cars(time, self, map);
}
return Vec::new();
}
}