diff --git a/backend/src/neighbourhood.rs b/backend/src/neighbourhood.rs index b13adc6..c4ad24c 100644 --- a/backend/src/neighbourhood.rs +++ b/backend/src/neighbourhood.rs @@ -260,8 +260,17 @@ impl Neighbourhood { let double_ended = map.directions[r] == Direction::BothWays; if let Some(polygon) = make_arrow(line, thickness, double_ended) { let mut f = map.mercator.to_wgs84_gj(&polygon); - // TODO Which cell? f.set_property("kind", "border_arrow"); + match self + .derived + .as_ref() + .unwrap() + .render_cells + .colors_per_border[&i] + { + Color::Disconnected => f.set_property("cell_color", "disconnected"), + Color::Cell(idx) => f.set_property("cell_color", idx), + } features.push(f); } } diff --git a/backend/src/render_cells.rs b/backend/src/render_cells.rs index 8757fe4..001950e 100644 --- a/backend/src/render_cells.rs +++ b/backend/src/render_cells.rs @@ -1,13 +1,14 @@ -use std::collections::{HashSet, VecDeque}; +use std::collections::{HashMap, HashSet, VecDeque}; use geo::{BooleanOps, BoundingRect, Coord, Densify, Euclidean, LineString, MultiPolygon, Rect}; use utils::{Grid, LineSplit}; -use crate::{Cell, MapModel, Neighbourhood}; +use crate::{Cell, IntersectionID, MapModel, Neighbourhood}; const NUM_COLORS: usize = 10; const RESOLUTION_M: f64 = 10.0; +#[derive(Clone, Copy)] pub enum Color { Disconnected, Cell(usize), @@ -18,6 +19,8 @@ pub struct RenderCells { pub polygons_per_cell: Vec, /// Colors per cell, such that adjacent cells are colored differently pub colors: Vec, + /// Each border belongs to a cell; put its color here + pub colors_per_border: HashMap, } impl RenderCells { @@ -102,18 +105,20 @@ impl RenderCells { if true { finalize( grid, + cells, cell_colors, bounds, &MultiPolygon::new(vec![boundary_polygon]), ) } else { - debug_grid(grid, cell_colors, bounds) + debug_grid(grid, cells, cell_colors, bounds) } } } fn finalize( main_grid: Grid>, + cells: &Vec, cell_colors: Vec, bounds: Rect, boundary: &MultiPolygon, @@ -121,6 +126,7 @@ fn finalize( let mut result = RenderCells { polygons_per_cell: Vec::new(), colors: Vec::new(), + colors_per_border: HashMap::new(), }; for (idx, color) in cell_colors.into_iter().enumerate() { @@ -170,15 +176,25 @@ fn finalize( .polygons_per_cell .push(cell_polygon.intersection(boundary)); result.colors.push(color); + + for i in &cells[idx].borders { + result.colors_per_border.insert(*i, color); + } } result } -fn debug_grid(grid: Grid>, cell_colors: Vec, bounds: Rect) -> RenderCells { +fn debug_grid( + grid: Grid>, + cells: &Vec, + cell_colors: Vec, + bounds: Rect, +) -> RenderCells { let mut result = RenderCells { polygons_per_cell: Vec::new(), colors: Vec::new(), + colors_per_border: HashMap::new(), }; for (idx, color) in cell_colors.into_iter().enumerate() { @@ -205,6 +221,10 @@ fn debug_grid(grid: Grid>, cell_colors: Vec, bounds: Rect) result.polygons_per_cell.push(MultiPolygon::new(squares)); result.colors.push(color); + + for i in &cells[idx].borders { + result.colors_per_border.insert(*i, color); + } } result diff --git a/web/src/RenderNeighbourhood.svelte b/web/src/RenderNeighbourhood.svelte index 5851e34..221ded4 100644 --- a/web/src/RenderNeighbourhood.svelte +++ b/web/src/RenderNeighbourhood.svelte @@ -98,8 +98,7 @@ {...layerId("border-arrows")} filter={["==", ["get", "kind"], "border_arrow"]} paint={{ - "fill-color": "cyan", - "fill-opacity": 0.5, + "fill-color": ["get", "color"], }} /> | Feature + | Feature< + Polygon, + { + kind: "border_arrow"; + cell_color: "disconnected" | number; + // Populated by setCellColors, not in the WASM API + color?: string; + } + > | Feature< MultiPolygon, { @@ -204,7 +213,7 @@ function setCellColors( ]; for (let f of gj.features) { - if (f.properties.kind != "cell") { + if (f.properties.kind != "cell" && f.properties.kind != "border_arrow") { continue; } if (f.properties.cell_color == "disconnected") {