Skip to content

Commit 07b653a

Browse files
committed
Clip cell polygons to the boundary
1 parent 6264d35 commit 07b653a

File tree

1 file changed

+20
-10
lines changed

1 file changed

+20
-10
lines changed

backend/src/render_cells.rs

+20-10
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::collections::{HashSet, VecDeque};
22

3-
use geo::{BoundingRect, Coord, Densify, LineString, MultiPolygon, Rect};
3+
use geo::{BooleanOps, BoundingRect, Coord, Densify, LineString, MultiPolygon, Rect};
44

55
use crate::{Cell, MapModel, Neighbourhood};
66

@@ -44,9 +44,7 @@ impl RenderCells {
4444
if let Some(slice) =
4545
slice_linestring(&road.linestring, interval.start, interval.end)
4646
{
47-
// Walk along the center line. We could look at the road's thickness and fill
48-
// out points based on that, but the diffusion should take care of it.
49-
// TODO Clean up old comments like this
47+
// Walk along the center line
5048
for pt in slice.densify(RESOLUTION_M / 2.0).0 {
5149
let grid_idx = grid.idx(
5250
((pt.x - bounds.min().x) / RESOLUTION_M) as usize,
@@ -102,14 +100,24 @@ impl RenderCells {
102100
}
103101

104102
if true {
105-
finalize(grid, cell_colors, bounds)
103+
finalize(
104+
grid,
105+
cell_colors,
106+
bounds,
107+
&MultiPolygon::new(vec![boundary_polygon]),
108+
)
106109
} else {
107110
debug_grid(grid, cell_colors, bounds)
108111
}
109112
}
110113
}
111114

112-
fn finalize(main_grid: Grid<Option<usize>>, cell_colors: Vec<Color>, bounds: Rect) -> RenderCells {
115+
fn finalize(
116+
main_grid: Grid<Option<usize>>,
117+
cell_colors: Vec<Color>,
118+
bounds: Rect,
119+
boundary: &MultiPolygon,
120+
) -> RenderCells {
113121
let mut result = RenderCells {
114122
polygons_per_cell: Vec::new(),
115123
colors: Vec::new(),
@@ -155,10 +163,12 @@ fn finalize(main_grid: Grid<Option<usize>>, cell_colors: Vec<Color>, bounds: Rec
155163
}
156164
assert_eq!(cell_polygons.len(), 1);
157165

158-
// TODO Sometimes one cell "leaks" out of the neighbourhood boundary. Not sure why. But we
159-
// can just clip the result.
160-
161-
result.polygons_per_cell.push(cell_polygons.remove(0));
166+
// Sometimes one cell "leaks" out of the neighbourhood boundary. Not sure why. But we can
167+
// just clip the result.
168+
let cell_polygon = cell_polygons.remove(0);
169+
result
170+
.polygons_per_cell
171+
.push(cell_polygon.intersection(boundary));
162172
result.colors.push(color);
163173
}
164174

0 commit comments

Comments
 (0)