From d3578e9b6756d111c2d42896da327bd313c9359f Mon Sep 17 00:00:00 2001 From: Dustin Carlino Date: Sat, 24 Aug 2024 20:14:43 +0100 Subject: [PATCH] Handle the start=end route case differently, in preparation for more refactoring --- backend/src/graph/route.rs | 42 ++++++++++++++++++-------------------- web/src/App.svelte | 2 +- 2 files changed, 21 insertions(+), 23 deletions(-) diff --git a/backend/src/graph/route.rs b/backend/src/graph/route.rs index 07f99a3..af2cdeb 100644 --- a/backend/src/graph/route.rs +++ b/backend/src/graph/route.rs @@ -50,13 +50,23 @@ impl Router { } } - // TODO This doesn't handle start=end cases pub fn route_steps( &self, graph: &Graph, start: Position, end: Position, ) -> Result> { + if start == end { + bail!("start = end"); + } + + if start.road == end.road { + return Ok(vec![PathStep::Road { + road: start.road, + forwards: start.fraction_along < end.fraction_along, + }]); + } + let start_node = self.node_map.get(start.intersection).unwrap(); let end_node = self.node_map.get(end.intersection).unwrap(); @@ -105,27 +115,7 @@ impl Router { start: Position, end: Position, ) -> Result { - if start == end { - bail!("start = end"); - } - // TODO Handle this in slice_road_step - if start.road == end.road { - // Just slice the one road - let mut slice = graph.roads[start.road.0] - .linestring - .line_split_twice(start.fraction_along, end.fraction_along) - .unwrap() - .into_second() - .unwrap(); - if start.fraction_along > end.fraction_along { - slice.0.reverse(); - } - return Ok(slice); - } - let steps = self.route_steps(graph, start, end)?; - - // TODO Share code with PT? let mut pts = Vec::new(); for (pos, step) in steps.into_iter().with_position() { match step { @@ -187,7 +177,15 @@ fn slice_road_step( .unwrap() .0 } - _ => linestring.0.clone(), + itertools::Position::Middle => linestring.0.clone(), + itertools::Position::Only => { + linestring + .line_split_twice(start.fraction_along, end.fraction_along) + .unwrap() + .into_second() + .unwrap() + .0 + } }; if !forwards { pts.reverse(); diff --git a/web/src/App.svelte b/web/src/App.svelte index fd62e3e..c0f9d41 100644 --- a/web/src/App.svelte +++ b/web/src/App.svelte @@ -40,7 +40,7 @@ import * as pmtiles from "pmtiles"; import maplibregl from "maplibre-gl"; - let offlineMode = false; + let offlineMode = new URLSearchParams(window.location.search).has("offline"); if (offlineMode) { let protocol = new pmtiles.Protocol(); maplibregl.addProtocol("pmtiles", protocol.tile);