Give callers a way to create and use a Pathfinder directly, not storing

it as part of the Map. #852

This has an immediate use in the LTN rat run calculation -- share the
pathfinder across threads, avoid massive logspam.

There's a much larger refactor in another branch, but just starting with
this.
This commit is contained in:
Dustin Carlino 2022-03-21 12:58:20 +00:00
parent 1661957511
commit 3b664ea54e
3 changed files with 39 additions and 10 deletions

View File

@ -3,7 +3,7 @@ use std::collections::HashSet;
use abstutil::{Counter, Timer};
use map_model::{
DirectedRoadID, IntersectionID, LaneID, Map, Path, PathConstraints, PathRequest, PathStep,
PathfinderCaching, Position, RoadID,
Pathfinder, Position, RoadID,
};
use crate::{App, Cell, Neighborhood};
@ -65,11 +65,16 @@ pub fn find_rat_runs(app: &App, neighborhood: &Neighborhood, timer: &mut Timer)
// regards for the larger path somebody actually wants to take.
params.avoid_roads.extend(neighborhood.perimeter.clone());
let pathfinder = Pathfinder::new_dijkstra(map, params, vec![PathConstraints::Car], timer);
let paths: Vec<Path> = timer
.parallelize(
"calculate paths between entrances and exits",
requests,
|req| map.pathfind_with_params(req, &params, PathfinderCaching::CacheDijkstra),
|req| {
pathfinder
.pathfind_v2(req, map)
.and_then(|path| path.into_v1(map).ok())
},
)
.into_iter()
.flatten()

View File

@ -60,10 +60,9 @@ pub use crate::objects::transit::{TransitRoute, TransitRouteID, TransitStop, Tra
pub use crate::objects::turn::{Turn, TurnID, TurnPriority, TurnType};
pub use crate::objects::zone::{AccessRestrictions, Zone};
pub use crate::pathfind::uber_turns::{IntersectionCluster, UberTurn};
use crate::pathfind::Pathfinder;
pub use crate::pathfind::{
Path, PathConstraints, PathRequest, PathStep, PathStepV2, PathV2, PathfinderCaching,
RoutingParams,
Path, PathConstraints, PathRequest, PathStep, PathStepV2, PathV2, Pathfinder,
PathfinderCaching, RoutingParams,
};
pub use crate::traversable::{Position, Traversable, MAX_BIKE_SPEED, MAX_WALKING_SPEED};

View File

@ -66,7 +66,7 @@ impl Clone for Pathfinder {
impl Pathfinder {
/// Quickly create an invalid pathfinder, just to make borrow checking / initialization order
/// work.
pub fn empty() -> Pathfinder {
pub(crate) fn empty() -> Pathfinder {
Pathfinder {
car_graph: VehiclePathfinder::empty(),
bike_graph: VehiclePathfinder::empty(),
@ -79,7 +79,7 @@ impl Pathfinder {
}
}
pub fn new(
pub(crate) fn new(
map: &Map,
params: RoutingParams,
engine: &CreateEngine,
@ -135,8 +135,19 @@ impl Pathfinder {
}
}
/// Create a new Pathfinder with custom routing params that can only serve some modes. Fast to
/// create, slow to use.
pub fn new_dijkstra(
map: &Map,
params: RoutingParams,
modes: Vec<PathConstraints>,
timer: &mut Timer,
) -> Self {
Self::new_limited(map, params, CreateEngine::Dijkstra, modes, timer)
}
/// Create a new Pathfinder with custom routing params that can only serve some modes.
pub fn new_limited(
pub(crate) fn new_limited(
map: &Map,
params: RoutingParams,
engine: CreateEngine,
@ -169,7 +180,7 @@ impl Pathfinder {
p
}
pub fn finalize_transit(&mut self, map: &Map, engine: &CreateEngine) {
pub(crate) fn finalize_transit(&mut self, map: &Map, engine: &CreateEngine) {
self.walking_with_transit_graph =
SidewalkPathfinder::new(map, Some((&self.bus_graph, &self.train_graph)), engine);
}
@ -179,9 +190,22 @@ impl Pathfinder {
self.pathfind_with_params(req, map.routing_params(), PathfinderCaching::NoCache, map)
}
/// Finds a path from a start to an end for a certain type of agent. Uses the RoutingParams
/// built into this Pathfinder.
pub fn pathfind_v2(&self, req: PathRequest, map: &Map) -> Option<PathV2> {
match req.constraints {
PathConstraints::Pedestrian => self.walking_graph.pathfind(req, map),
PathConstraints::Car => self.car_graph.pathfind(req, map),
PathConstraints::Bike => self.bike_graph.pathfind(req, map),
PathConstraints::Bus => self.bus_graph.pathfind(req, map),
PathConstraints::Train => self.train_graph.pathfind(req, map),
}
}
/// Finds a path from a start to an end for a certain type of agent. May use custom routing
/// parameters. If caching is requested and custom routing parameters are used, then the
/// intermediate graph is saved to speed up future calls with the same routing parameters.
// TODO Deprecated
pub fn pathfind_with_params(
&self,
req: PathRequest,
@ -237,6 +261,7 @@ impl Pathfinder {
result
}
// TODO Deprecated
pub fn clear_custom_pathfinder_cache(&self) {
self.cached_alternatives
.get_or(|| RefCell::new(VecMap::new()))
@ -270,7 +295,7 @@ impl Pathfinder {
.should_use_transit(map, start, end)
}
pub fn apply_edits(&mut self, map: &Map, timer: &mut Timer) {
pub(crate) fn apply_edits(&mut self, map: &Map, timer: &mut Timer) {
timer.start("apply edits to car pathfinding");
self.car_graph.apply_edits(map);
timer.stop("apply edits to car pathfinding");