Skip to content

Commit ec08a9c

Browse files
committed
Get cell contouring to work! #4
The problem was translating and scaling MultiPolygons. The contour crate itself already has a way to handle it, so just use that.
1 parent 488b652 commit ec08a9c

File tree

1 file changed

+17
-26
lines changed

1 file changed

+17
-26
lines changed

backend/src/render_cells.rs

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

3-
use geo::{
4-
BoundingRect, Coord, Densify, LineString, MultiPolygon, Polygon, Rect, Scale, Translate,
5-
};
3+
use geo::{BoundingRect, Coord, Densify, LineString, MultiPolygon, Rect};
64

75
use crate::{Cell, MapModel, Neighbourhood};
86

@@ -103,17 +101,15 @@ impl RenderCells {
103101
}
104102
}
105103

106-
//finalize(grid, cell_colors, bounds, boundary_polygon)
107-
debug_grid(grid, cell_colors, bounds, boundary_polygon)
104+
if true {
105+
finalize(grid, cell_colors, bounds)
106+
} else {
107+
debug_grid(grid, cell_colors, bounds)
108+
}
108109
}
109110
}
110111

111-
fn finalize(
112-
grid: Grid<Option<usize>>,
113-
cell_colors: Vec<Color>,
114-
bounds: Rect,
115-
boundary_polygon: Polygon,
116-
) -> RenderCells {
112+
fn finalize(main_grid: Grid<Option<usize>>, cell_colors: Vec<Color>, bounds: Rect) -> RenderCells {
117113
let mut result = RenderCells {
118114
polygons_per_cell: Vec::new(),
119115
colors: Vec::new(),
@@ -124,9 +120,9 @@ fn finalize(
124120
// number per cell, so we can't directly use it -- the area >= some cell index is
125121
// meaningless. Per cell, make a new grid that just has that cell.
126122
let grid: Grid<f64> = Grid {
127-
width: grid.width,
128-
height: grid.height,
129-
data: grid
123+
width: main_grid.width,
124+
height: main_grid.height,
125+
data: main_grid
130126
.data
131127
.iter()
132128
.map(
@@ -143,17 +139,17 @@ fn finalize(
143139

144140
let smooth = false;
145141
let contour_builder =
146-
contour::ContourBuilder::new(grid.width as u32, grid.height as u32, smooth);
142+
contour::ContourBuilder::new(grid.width as u32, grid.height as u32, smooth)
143+
.x_origin(bounds.min().x)
144+
.y_origin(bounds.min().y)
145+
.x_step(RESOLUTION_M)
146+
.y_step(RESOLUTION_M);
147147
let thresholds = vec![1.0];
148148

149149
let mut cell_polygons = Vec::new();
150150
for contour in contour_builder.contours(&grid.data, &thresholds).unwrap() {
151151
let (multipolygon, _) = contour.into_inner();
152-
cell_polygons.push(
153-
multipolygon
154-
.scale(RESOLUTION_M)
155-
.translate(bounds.min().x, bounds.min().y),
156-
);
152+
cell_polygons.push(multipolygon);
157153
}
158154
assert_eq!(cell_polygons.len(), 1);
159155

@@ -167,12 +163,7 @@ fn finalize(
167163
result
168164
}
169165

170-
fn debug_grid(
171-
grid: Grid<Option<usize>>,
172-
cell_colors: Vec<Color>,
173-
bounds: Rect,
174-
boundary_polygon: Polygon,
175-
) -> RenderCells {
166+
fn debug_grid(grid: Grid<Option<usize>>, cell_colors: Vec<Color>, bounds: Rect) -> RenderCells {
176167
let mut result = RenderCells {
177168
polygons_per_cell: Vec::new(),
178169
colors: Vec::new(),

0 commit comments

Comments
 (0)