Skip to content

Commit

Permalink
Improve Road::{untrimmed_road_geometry,get_untrimmed_sides} using r…
Browse files Browse the repository at this point in the history
…oad position

`untrimmed_road_geometry` returns a line along `RoadPosition::Center`, which is what the `true_center` calculation was doing (when it found the right conditions).

`get_untrimmed_sides` uses the actual `reference_line` offset instead of
assuming it is `RoadPosition::FullWidthCenter`.

Ideally, more of the usages could be switched out for something using
`center_line` instead.
  • Loading branch information
BudgieInWA authored and dabreegster committed Dec 4, 2022
1 parent 1adb096 commit ce540b1
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 46 deletions.
6 changes: 3 additions & 3 deletions osm2streets/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ impl StreetNetwork {
for road in self.roads_per_intersection(endpts[0]) {
// trimmed_center_line hasn't been initialized yet, so override this
let mut input = road.to_input_road();
input.center_pts = road.untrimmed_road_geometry().0;
input.center_pts = road.untrimmed_road_geometry(self.config.driving_side).0;
input_roads.push(input);
}
let mut results = intersection_polygon(
Expand All @@ -176,7 +176,7 @@ impl StreetNetwork {
if road.id == road_id {
input.center_pts = trimmed_center_pts.clone();
} else {
input.center_pts = road.untrimmed_road_geometry().0;
input.center_pts = road.untrimmed_road_geometry(self.config.driving_side).0;
}
input_roads.push(input);
}
Expand Down Expand Up @@ -228,7 +228,7 @@ impl StreetNetwork {
pub(crate) fn debug_road<I: Into<String>>(&self, r: RoadID, label: I) {
if let Some(step) = self.debug_steps.borrow_mut().last_mut() {
step.polylines
.push((self.roads[&r].untrimmed_road_geometry().0, label.into()));
.push((self.roads[&r].center_line.clone(), label.into()));
}
}
}
Expand Down
68 changes: 32 additions & 36 deletions osm2streets/src/road.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,38 +221,23 @@ impl Road {
self.reference_line.length()
}

/// Returns the corrected (but untrimmed) center and total width for a road
pub fn untrimmed_road_geometry(&self) -> (PolyLine, Distance) {
let mut total_width = Distance::ZERO;
let mut sidewalk_right = None;
let mut sidewalk_left = None;
for l in &self.lane_specs_ltr {
total_width += l.width;
if l.lt.is_walkable() {
if l.dir == Direction::Back {
sidewalk_left = Some(l.width);
} else {
sidewalk_right = Some(l.width);
}
}
}

// If there's a sidewalk on only one side, adjust the true center of the road.
// TODO I don't remember the rationale for doing this in the first place. What if there's a
// shoulder and a sidewalk of different widths? We don't do anything then
//TODO use self.left_edge_offset_of(RoadPosition::Center) or something?
let mut true_center = self.reference_line.clone();
match (sidewalk_right, sidewalk_left) {
(Some(w), None) => {
true_center = true_center.must_shift_right(w / 2.0);
}
(None, Some(w)) => {
true_center = true_center.must_shift_right(w / 2.0);
}
_ => {}
}

(true_center, total_width)
/// Returns a line along `RoadPosition::Center` (but untrimmed) and total width for ALL lanes.
pub fn untrimmed_road_geometry(&self, driving_side: DrivingSide) -> (PolyLine, Distance) {
let total_width = self.total_width();
let ref_position = match self.reference_line_placement {
Placement::Consistent(p) => p,
Placement::Varying(p, _) => p,
Placement::Transition => RoadPosition::Center, // Best we can do for now.
};
let ref_offset = self.left_edge_offset_of(ref_position, driving_side);
let center_offset = self.left_edge_offset_of(RoadPosition::Center, driving_side);

(
self.reference_line
.shift_either_direction(center_offset - ref_offset)
.unwrap(),
total_width,
)
}

pub fn total_width(&self) -> Distance {
Expand Down Expand Up @@ -416,10 +401,21 @@ impl Road {

/// Returns the untrimmed left and right side of the road, oriented in the same direction of
/// the road
pub fn get_untrimmed_sides(&self) -> Result<(PolyLine, PolyLine)> {
let (center, total_width) = self.untrimmed_road_geometry();
let left = center.shift_from_center(total_width, -total_width / 2.0)?;
let right = center.shift_from_center(total_width, total_width / 2.0)?;
pub fn get_untrimmed_sides(&self, driving_side: DrivingSide) -> Result<(PolyLine, PolyLine)> {
let total_width = self.total_width();
let ref_position = match self.reference_line_placement {
Placement::Consistent(p) => p,
Placement::Varying(p, _) => p,
Placement::Transition => RoadPosition::Center, // Best we can do for now.
};
let ref_offset = self.left_edge_offset_of(ref_position, driving_side);

let left = self
.reference_line
.shift_from_center(total_width, ref_offset - total_width)?;
let right = self
.reference_line
.shift_from_center(total_width, total_width - ref_offset)?;
Ok((left, right))
}

Expand Down
16 changes: 13 additions & 3 deletions osm2streets/src/transform/dual_carriageways.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,12 +263,22 @@ impl DualCarriagewayPt2 {
side1_length: orig
.side1
.iter()
.map(|r| streets.roads[&r.road].untrimmed_road_geometry().0.length())
.map(|r| {
streets.roads[&r.road]
.untrimmed_road_geometry(streets.config.driving_side)
.0
.length()
})
.sum(),
side2_length: orig
.side2
.iter()
.map(|r| streets.roads[&r.road].untrimmed_road_geometry().0.length())
.map(|r| {
streets.roads[&r.road]
.untrimmed_road_geometry(streets.config.driving_side)
.0
.length()
})
.sum(),
})
}
Expand All @@ -294,7 +304,7 @@ impl DualCarriagewayPt2 {

for pair in side.windows(2) {
dist += streets.roads[&pair[0].road]
.untrimmed_road_geometry()
.untrimmed_road_geometry(streets.config.driving_side)
.0
.length();
let i = pair[0].dst_i;
Expand Down
7 changes: 6 additions & 1 deletion osm2streets/src/transform/find_short_roads.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,12 @@ impl StreetNetwork {
{
continue;
}
if road.untrimmed_road_geometry().0.length() <= threshold {
if road
.untrimmed_road_geometry(self.config.driving_side)
.0
.length()
<= threshold
{
results.push(road.id);
}
}
Expand Down
6 changes: 4 additions & 2 deletions osm2streets/src/transform/separate_cycletracks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ fn find_cycleways(streets: &StreetNetwork) -> Vec<Cycleway> {
{
cycleways.push(Cycleway {
cycleway: cycleway_road.id,
cycleway_center: cycleway_road.untrimmed_road_geometry().0,
cycleway_center: cycleway_road.center_line.clone(),
debug_idx: cycleways.len(),
main_road_src_i,
main_road_dst_i,
Expand Down Expand Up @@ -135,7 +135,9 @@ fn snap(streets: &mut StreetNetwork, input: Cycleway) {
for r in input.main_roads {
let main_road = &streets.roads[&r];
// Which side is closer to the cycleway?
let (left, right) = main_road.get_untrimmed_sides().unwrap();
let (left, right) = main_road
.get_untrimmed_sides(streets.config.driving_side)
.unwrap();
// TODO georust has a way to check distance of linestrings. But for now, just check the
// middles
let snap_to_left = input.cycleway_center.middle().dist_to(left.middle())
Expand Down
3 changes: 2 additions & 1 deletion osm2streets/src/transform/shrink_roads.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ pub fn shrink(streets: &mut StreetNetwork, timer: &mut Timer) {
continue;
}

let (center, total_width) = road.untrimmed_road_geometry();
let center = road.center_line.clone();
let total_width = road.total_width();
let polygon = center.make_polygons(total_width);

// Any conflicts with existing?
Expand Down

0 comments on commit ce540b1

Please sign in to comment.