Skip to content

Commit

Permalink
Handle the start=end route case differently, in preparation for more
Browse files Browse the repository at this point in the history
refactoring
  • Loading branch information
dabreegster committed Aug 24, 2024
1 parent f0836a8 commit d3578e9
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 23 deletions.
42 changes: 20 additions & 22 deletions backend/src/graph/route.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Vec<PathStep>> {
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();

Expand Down Expand Up @@ -105,27 +115,7 @@ impl Router {
start: Position,
end: Position,
) -> Result<LineString> {
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 {
Expand Down Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion web/src/App.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit d3578e9

Please sign in to comment.