Skip to content

Commit

Permalink
Fix up the API and callers of some Polygon methods. #951
Browse files Browse the repository at this point in the history
- get_outer_ring
- into_points
  • Loading branch information
dabreegster committed Sep 2, 2022
1 parent b686e98 commit 0a10fea
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 81 deletions.
2 changes: 1 addition & 1 deletion apps/game/src/debug/blockfinder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ impl SimpleState<App> for OneBlock {
"pt",
self.block
.polygon
.clone()
.get_outer_ring()
.into_points()
.into_iter()
.map(polygons::Item::Point)
Expand Down
6 changes: 2 additions & 4 deletions apps/game/src/debug/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1091,10 +1091,8 @@ fn reimport_map(
fn draw_bad_intersections(ctx: &mut EventCtx, app: &App) -> Drawable {
let mut batch = GeomBatch::new();
for i in app.primary.map.all_intersections() {
if let Some(ring) = i.polygon.get_outer_ring() {
if ring.doubles_back() {
batch.push(Color::RED.alpha(0.8), i.polygon.clone());
}
if i.polygon.get_outer_ring().doubles_back() {
batch.push(Color::RED.alpha(0.8), i.polygon.clone());
}
}
ctx.upload(batch)
Expand Down
2 changes: 1 addition & 1 deletion apps/game/src/devtools/story.rs
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ impl State<App> for DrawFreehand {
Transition::ModifyState(Box::new(move |state, ctx, app| {
let editor = state.downcast_mut::<StoryMapEditor>().unwrap();
editor.story.markers.push(Marker {
pts: polygon.into_points(),
pts: polygon.get_outer_ring().into_points(),
label: String::new(),
});

Expand Down
1 change: 1 addition & 0 deletions apps/ltn/src/customize_boundary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ impl CustomizeBoundary {
.session
.partitioning
.neighbourhood_boundary_polygon(app, id)
.get_outer_ring()
.into_points();
Box::new(Self {
id,
Expand Down
17 changes: 7 additions & 10 deletions apps/ltn/src/draw_cells.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::collections::{HashSet, VecDeque};

use geom::{Bounds, Distance, PolyLine, Polygon};
use geom::{Bounds, Distance, Polygon};
use map_gui::tools::Grid;
use map_model::Map;
use widgetry::{Color, GeomBatch};
Expand Down Expand Up @@ -55,7 +55,7 @@ impl RenderCells {
let neighbourhood_boundary = self
.boundary_polygon
.get_outer_ring()
.map(|r| r.to_outline(Distance::meters(25.0)));
.to_outline(Distance::meters(25.0));

let mut batch = GeomBatch::new();
for (cell_color, polygons) in self.colors.iter().zip(self.polygons_per_cell.iter()) {
Expand All @@ -67,18 +67,15 @@ impl RenderCells {
continue;
}

let boundary = PolyLine::unchecked_new(poly.clone().into_points())
.make_polygons(Distance::meters(5.0));
let boundary = poly.get_outer_ring().to_outline(Distance::meters(5.0));

let color = cell_color.alpha(1.0).shade(0.2);
// If possible, try to erase where the cell boundary touches the perimeter road.
if let Some(ref neighbourhood_boundary) = neighbourhood_boundary {
if let Ok(list) = boundary.difference(neighbourhood_boundary) {
batch.extend(color, list);
}
continue;
if let Ok(list) = boundary.difference(&neighbourhood_boundary) {
batch.extend(color, list);
} else {
batch.push(color, boundary);
}
batch.push(color, boundary);
}
}
batch
Expand Down
22 changes: 6 additions & 16 deletions geom/src/polygon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,24 +161,14 @@ impl Polygon {
&self.points
}
}
pub fn into_points(mut self) -> Vec<Pt2D> {
if let Some(mut rings) = self.rings.take() {
rings.remove(0).into_points()
} else {
self.points
}
}
// TODO Switch to get_outer_ring. osm2streets depends on this one, have to juggle git repos
pub fn into_ring(self) -> Ring {
Ring::must_new(self.into_points())
self.get_outer_ring()
}

/// Get the outer ring of this polygon. This should usually succeed.
pub fn get_outer_ring(&self) -> Option<Ring> {
if let Some(ref rings) = self.rings {
Some(rings[0].clone())
} else {
Ring::new(self.points.clone()).ok()
}
// TODO Should be &Ring
pub fn get_outer_ring(&self) -> Ring {
self.get_rings().remove(0)
}

pub fn center(&self) -> Pt2D {
Expand Down Expand Up @@ -354,7 +344,7 @@ impl Polygon {
if let Some(ref rings) = self.rings {
rings.clone()
} else {
vec![Ring::must_new(self.clone().into_points())]
vec![Ring::must_new(self.points.clone())]
}
}

Expand Down
68 changes: 33 additions & 35 deletions map_gui/src/render/intersection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,41 +166,39 @@ impl DrawIntersection {
/// Find sections along the intersection polygon that aren't connected to a road. These should
/// contribute an outline.
pub fn get_unzoomed_outline(i: &Intersection, map: &Map) -> Vec<PolyLine> {
if let Some(ring) = i.polygon.get_outer_ring() {
// Turn each road into the left and right point that should be on the ring, so we can
// "subtract" them out.
let road_pairs = i
.roads
.iter()
.map(|r| {
let road = map.get_r(*r);
let half_width = road.get_half_width();
let left = road.center_pts.must_shift_left(half_width);
let right = road.center_pts.must_shift_right(half_width);
if road.src_i == i.id {
(left.first_pt(), right.first_pt())
} else {
(left.last_pt(), right.last_pt())
}
})
.collect::<Vec<_>>();

// Walk along each line segment on the ring. If it's not one of our road pairs, add it
// as a potential segment.
ring.into_points()
.windows(2)
.filter(|window| {
!road_pairs
.iter()
.any(|road_pair| approx_eq(window, &road_pair))
})
.map(|pair| PolyLine::must_new(vec![pair[0], pair[1]]))
.collect::<Vec<_>>()

// TODO We could merge adjacent segments, to get nicer corners
} else {
vec![]
}
// Turn each road into the left and right point that should be on the ring, so we can
// "subtract" them out.
let road_pairs = i
.roads
.iter()
.map(|r| {
let road = map.get_r(*r);
let half_width = road.get_half_width();
let left = road.center_pts.must_shift_left(half_width);
let right = road.center_pts.must_shift_right(half_width);
if road.src_i == i.id {
(left.first_pt(), right.first_pt())
} else {
(left.last_pt(), right.last_pt())
}
})
.collect::<Vec<_>>();

// Walk along each line segment on the ring. If it's not one of our road pairs, add it as a
// potential segment.
i.polygon
.get_outer_ring()
.into_points()
.windows(2)
.filter(|window| {
!road_pairs
.iter()
.any(|road_pair| approx_eq(window, &road_pair))
})
.map(|pair| PolyLine::must_new(vec![pair[0], pair[1]]))
.collect::<Vec<_>>()

// TODO We could merge adjacent segments, to get nicer corners
}

fn redraw_default(&self, g: &mut GfxCtx, app: &dyn AppLike) {
Expand Down
25 changes: 11 additions & 14 deletions map_model/src/objects/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -611,14 +611,12 @@ impl Perimeter {
}
if let Some(last_pt) = pts.last() {
let prev_i = map.get_i(prev_i);
if let Some(ring) = prev_i.polygon.get_outer_ring() {
if !ring.doubles_back() {
// At dead-ends, trace around the intersection on the longer side
let longer = prev_i.is_deadend_for_driving(map);
if let Some(slice) = ring.get_slice_between(*last_pt, pl.first_pt(), longer)
{
pts.extend(slice.into_points());
}
let ring = prev_i.polygon.get_outer_ring();
if !ring.doubles_back() {
// At dead-ends, trace around the intersection on the longer side
let longer = prev_i.is_deadend_for_driving(map);
if let Some(slice) = ring.get_slice_between(*last_pt, pl.first_pt(), longer) {
pts.extend(slice.into_points());
}
}
}
Expand All @@ -628,12 +626,11 @@ impl Perimeter {
// Do the intersection boundary tracing for the last piece. We didn't know enough to do it
// the first time.
let first_intersection = map.get_i(first_intersection.unwrap());
if let Some(ring) = first_intersection.polygon.get_outer_ring() {
if !ring.doubles_back() {
let longer = first_intersection.is_deadend_for_driving(map);
if let Some(slice) = ring.get_slice_between(*pts.last().unwrap(), pts[0], longer) {
pts.extend(slice.into_points());
}
let ring = first_intersection.polygon.get_outer_ring();
if !ring.doubles_back() {
let longer = first_intersection.is_deadend_for_driving(map);
if let Some(slice) = ring.get_slice_between(*pts.last().unwrap(), pts[0], longer) {
pts.extend(slice.into_points());
}
}
pts.push(pts[0]);
Expand Down

0 comments on commit 0a10fea

Please sign in to comment.