Skip to content

Commit

Permalink
Move RoadEdge logic out of sidewalk rendering. It'll be useful for in…
Browse files Browse the repository at this point in the history
…tersection geometry refactors. #136
  • Loading branch information
dabreegster committed Dec 5, 2022
1 parent b1c5fbe commit cd01d89
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 39 deletions.
47 changes: 8 additions & 39 deletions osm2streets/src/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ use std::path::Path;
use anyhow::Result;
use geom::{ArrowCap, Distance, Line, PolyLine, Polygon, Ring};

use crate::{
DebugStreets, Direction, DrivingSide, Intersection, LaneSpec, LaneType, RoadID, StreetNetwork,
};
use crate::road::RoadEdge;
use crate::{DebugStreets, Direction, DrivingSide, Intersection, LaneType, StreetNetwork};

impl StreetNetwork {
/// Saves the plain GeoJSON rendering to a file.
Expand Down Expand Up @@ -398,43 +397,13 @@ fn make_props(list: &[(&str, serde_json::Value)]) -> serde_json::Map<String, ser
// TODO Where should this live?
/// For an intersection, show all corners where sidewalks meet.
fn make_sidewalk_corners(streets: &StreetNetwork, intersection: &Intersection) -> Vec<Polygon> {
#[derive(Clone)]
struct Edge {
road: RoadID,
// Pointed into the intersection
pl: PolyLine,
lane: LaneSpec,
}

// Get the left and right edge of each road, pointed into the intersection. All sorted
// clockwise
// TODO Use the road view idea instead. Or just refactor this.
let mut edges = Vec::new();
for road in streets.roads_per_intersection(intersection.id) {
let mut left = Edge {
road: road.id,
pl: road.center_line.must_shift_left(road.total_width() / 2.0),
lane: road.lane_specs_ltr[0].clone(),
};
let mut right = Edge {
road: road.id,
pl: road.center_line.must_shift_right(road.total_width() / 2.0),
lane: road.lane_specs_ltr.last().unwrap().clone(),
};
if road.dst_i == intersection.id {
edges.push(right);
edges.push(left);
} else {
left.pl = left.pl.reversed();
right.pl = right.pl.reversed();
edges.push(left);
edges.push(right);
}
}

// Look at every adjacent pair
let mut results = Vec::new();
// Look at every adjacent pair of edges
let mut edges = RoadEdge::calculate(
streets.roads_per_intersection(intersection.id),
intersection.id,
);
edges.push(edges[0].clone());
let mut results = Vec::new();
for pair in edges.windows(2) {
let one = &pair[0];
let two = &pair[1];
Expand Down
41 changes: 41 additions & 0 deletions osm2streets/src/road.rs
Original file line number Diff line number Diff line change
Expand Up @@ -449,3 +449,44 @@ impl StreetNetwork {
id
}
}

/// The edge of a road, pointed into some intersection
#[derive(Clone)]
pub(crate) struct RoadEdge {
pub road: RoadID,
/// Pointed into the intersection
pub pl: PolyLine,
pub lane: LaneSpec,
}

impl RoadEdge {
/// Get the left and right edge of each road, pointed into the intersection. All sorted
/// clockwise. No repetitions -- to iterate over all adjacent pairs, the caller must repeat the
/// first edge
// TODO Maybe returning an iterator over pairs of these is more useful
pub fn calculate(sorted_roads: Vec<&Road>, i: IntersectionID) -> Vec<Self> {
let mut edges = Vec::new();
for road in sorted_roads {
let mut left = RoadEdge {
road: road.id,
pl: road.center_line.must_shift_left(road.total_width() / 2.0),
lane: road.lane_specs_ltr[0].clone(),
};
let mut right = RoadEdge {
road: road.id,
pl: road.center_line.must_shift_right(road.total_width() / 2.0),
lane: road.lane_specs_ltr.last().unwrap().clone(),
};
if road.dst_i == i {
edges.push(right);
edges.push(left);
} else {
left.pl = left.pl.reversed();
right.pl = right.pl.reversed();
edges.push(left);
edges.push(right);
}
}
edges
}
}

0 comments on commit cd01d89

Please sign in to comment.