Skip to content

Commit

Permalink
Move the calculation of trim_start and trim_end into the intersection…
Browse files Browse the repository at this point in the history
… geometry layer. #136
  • Loading branch information
dabreegster committed Dec 15, 2022
1 parent 60ed07d commit 9229ce1
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 17 deletions.
27 changes: 24 additions & 3 deletions osm2streets/src/geometry/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,11 @@ pub struct Results {
pub intersection_id: IntersectionID,
pub intersection_polygon: Polygon,
/// The only transformation to `center_line` passed in must be to trim it (reducing the length)
/// or to lengthen the first/last line.
pub trimmed_center_pts: BTreeMap<RoadID, PolyLine>,
/// or to lengthen the first/last line. `trim_starts` and `trim_ends` are calculated from this,
/// and the caller deliberately can't see `trimmed_center_pts`.
trimmed_center_pts: BTreeMap<RoadID, PolyLine>,
pub trim_starts: BTreeMap<RoadID, Distance>,
pub trim_ends: BTreeMap<RoadID, Distance>,
/// Extra points with labels to debug the algorithm
pub debug: Vec<(Pt2D, String)>,
}
Expand Down Expand Up @@ -115,9 +118,12 @@ pub fn intersection_polygon(
intersection_polygon: Polygon::dummy(),
debug: Vec::new(),
trimmed_center_pts: BTreeMap::new(),
trim_starts: BTreeMap::new(),
trim_ends: BTreeMap::new(),
};
let mut untrimmed_roads = roads.clone();

if roads.len() == 1 {
let mut results = if roads.len() == 1 {
terminus::terminus(results, roads.into_values().next().unwrap())
} else if roads.len() == 2 {
let mut iter = roads.into_values();
Expand All @@ -130,7 +136,22 @@ pub fn intersection_polygon(
Ok(result)
} else {
general_case::trim_to_corners(results, roads, sorted_roads)
}?;

// We've filled out trimmed_center_pts, now calculate trim_starts and trim_ends
for (r, pl) in &results.trimmed_center_pts {
// Normally this'll be positive, indicating trim. If it's negative, the algorithm extended
// the first or last line
let road = untrimmed_roads.remove(r).unwrap();
let trim = road.center_line.length() - pl.length();
if road.src_i == intersection_id {
results.trim_starts.insert(*r, trim);
} else {
results.trim_ends.insert(*r, trim);
}
}

Ok(results)
}

/// After trimming roads back, form the final polygon using the endpoints of each road edge and
Expand Down
6 changes: 5 additions & 1 deletion osm2streets/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,11 @@ impl StreetNetwork {
&BTreeMap::new(),
)
.ok()?;
trims.push(untrimmed.length() - results.trimmed_center_pts[&road_id].length());
trims.push(if i == orig_road.src_i {
results.trim_starts[&road_id]
} else {
results.trim_ends[&road_id]
});
}

Road::trim_polyline_both_ends(untrimmed, trims[0], trims[1])
Expand Down
18 changes: 5 additions & 13 deletions osm2streets/src/transform/intersection_geometry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,11 @@ pub fn generate(streets: &mut StreetNetwork, timer: &mut Timer) {
match crate::intersection_polygon(i.id, input_roads, &i.trim_roads_for_merging) {
Ok(results) => {
set_polygons.push((i.id, results.intersection_polygon));
for (r, pl) in results.trimmed_center_pts {
let road = streets.roads.get_mut(&r).unwrap();
// Normally this'll be positive, indicating trim. If it's negative, the
// algorithm extended the first or last line
let trim = road
.get_untrimmed_center_line(streets.config.driving_side)
.length()
- pl.length();
if road.src_i == i.id {
road.trim_start = trim;
} else {
road.trim_end = trim;
}
for (r, dist) in results.trim_starts {
streets.roads.get_mut(&r).unwrap().trim_start = dist;
}
for (r, dist) in results.trim_ends {
streets.roads.get_mut(&r).unwrap().trim_end = dist;
}
for (pt, label) in results.debug {
streets.debug_point(pt, label);
Expand Down

0 comments on commit 9229ce1

Please sign in to comment.