Skip to content

Commit

Permalink
Debug the grid. It's fine...
Browse files Browse the repository at this point in the history
  • Loading branch information
dabreegster committed Dec 20, 2023
1 parent 1c1317a commit 3e7c64e
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 5 deletions.
53 changes: 49 additions & 4 deletions backend/src/render_cells.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use std::collections::{HashSet, VecDeque};

use geo::{BoundingRect, Densify, LineString, MultiPolygon, Polygon, Rect, Scale, Translate};
use geo::{
BoundingRect, Coord, Densify, LineString, MultiPolygon, Polygon, Rect, Scale, Translate,
};

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

Expand Down Expand Up @@ -101,7 +103,8 @@ impl RenderCells {
}
}

finalize(grid, cell_colors, bounds, boundary_polygon)
//finalize(grid, cell_colors, bounds, boundary_polygon)
debug_grid(grid, cell_colors, bounds, boundary_polygon)
}
}

Expand Down Expand Up @@ -147,8 +150,10 @@ fn finalize(
for contour in contour_builder.contours(&grid.data, &thresholds).unwrap() {
let (multipolygon, _) = contour.into_inner();
cell_polygons.push(
multipolygon.scale(RESOLUTION_M)
.translate(bounds.min().x, bounds.min().y));
multipolygon
.scale(RESOLUTION_M)
.translate(bounds.min().x, bounds.min().y),
);
}
assert_eq!(cell_polygons.len(), 1);

Expand All @@ -162,6 +167,46 @@ fn finalize(
result
}

fn debug_grid(
grid: Grid<Option<usize>>,
cell_colors: Vec<Color>,
bounds: Rect,
boundary_polygon: Polygon,
) -> RenderCells {
let mut result = RenderCells {
polygons_per_cell: Vec::new(),
colors: Vec::new(),
};

for (idx, color) in cell_colors.into_iter().enumerate() {
let mut squares = Vec::new();
for x in 0..grid.width {
for y in 0..grid.height {
if grid.data[grid.idx(x, y)] == Some(idx) {
squares.push(
Rect::new(
Coord {
x: (x as f64) * RESOLUTION_M + bounds.min().x,
y: (y as f64) * RESOLUTION_M + bounds.min().y,
},
Coord {
x: ((x + 1) as f64) * RESOLUTION_M + bounds.min().x,
y: ((y + 1) as f64) * RESOLUTION_M + bounds.min().y,
},
)
.to_polygon(),
);
}
}
}

result.polygons_per_cell.push(MultiPolygon::new(squares));
result.colors.push(color);
}

result
}

/// Returns a set of adjacent indices. The pairs are symmetric -- (x, y) and (y, x) will both be
/// populated. Adjacency with boundary_marker doesn't count.
fn diffusion(grid: &mut Grid<Option<usize>>, boundary_marker: usize) -> HashSet<(usize, usize)> {
Expand Down
2 changes: 1 addition & 1 deletion web/src/NeighbourhoodLayer.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
for (let f of details.features) {
if (f.properties.color == "disconnected") {
f.properties.color = "red";
} else if (f.properties.color) {
} else if (Object.hasOwn(f.properties, "color")) {
f.properties.color = cell_colors[f.properties.color % cell_colors.length];
}
}
Expand Down

0 comments on commit 3e7c64e

Please sign in to comment.