Skip to content

Commit

Permalink
Associate border arrows with a cell. #68
Browse files Browse the repository at this point in the history
  • Loading branch information
dabreegster committed Jan 4, 2025
1 parent 19b6e2c commit 9034da3
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 8 deletions.
11 changes: 10 additions & 1 deletion backend/src/neighbourhood.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down
28 changes: 24 additions & 4 deletions backend/src/render_cells.rs
Original file line number Diff line number Diff line change
@@ -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),
Expand All @@ -18,6 +19,8 @@ pub struct RenderCells {
pub polygons_per_cell: Vec<MultiPolygon>,
/// Colors per cell, such that adjacent cells are colored differently
pub colors: Vec<Color>,
/// Each border belongs to a cell; put its color here
pub colors_per_border: HashMap<IntersectionID, Color>,
}

impl RenderCells {
Expand Down Expand Up @@ -102,25 +105,28 @@ 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<Option<usize>>,
cells: &Vec<Cell>,
cell_colors: Vec<Color>,
bounds: Rect,
boundary: &MultiPolygon,
) -> 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() {
Expand Down Expand Up @@ -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<Option<usize>>, cell_colors: Vec<Color>, bounds: Rect) -> RenderCells {
fn debug_grid(
grid: Grid<Option<usize>>,
cells: &Vec<Cell>,
cell_colors: Vec<Color>,
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() {
Expand All @@ -205,6 +221,10 @@ fn debug_grid(grid: Grid<Option<usize>>, cell_colors: Vec<Color>, 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
Expand Down
3 changes: 1 addition & 2 deletions web/src/RenderNeighbourhood.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,7 @@
{...layerId("border-arrows")}
filter={["==", ["get", "kind"], "border_arrow"]}
paint={{
"fill-color": "cyan",
"fill-opacity": 0.5,
"fill-color": ["get", "color"],
}}
/>
<LineLayer
Expand Down
11 changes: 10 additions & 1 deletion web/src/wasm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,15 @@ export interface RenderNeighbourhoodOutput {
}
>
| Feature<Point, { kind: "border_intersection" }>
| Feature<
Polygon,
{
kind: "border_arrow";
cell_color: "disconnected" | number;
// Populated by setCellColors, not in the WASM API
color?: string;
}
>
| Feature<
MultiPolygon,
{
Expand Down Expand Up @@ -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") {
Expand Down

0 comments on commit 9034da3

Please sign in to comment.