1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
use std::cmp::Ordering;
use std::collections::{BinaryHeap, HashMap};
use geom::{Duration, Speed};
use crate::pathfind::{zone_cost, WalkingNode};
use crate::{BuildingID, LaneType, Map, PathConstraints, Traversable};
#[derive(Clone)]
pub struct WalkingOptions {
pub allow_shoulders: bool,
pub walking_speed: Speed,
}
impl WalkingOptions {
pub fn default() -> WalkingOptions {
WalkingOptions {
allow_shoulders: true,
walking_speed: WalkingOptions::default_speed(),
}
}
pub fn common_speeds() -> Vec<(&'static str, Speed)> {
vec![
("3 mph (average for an adult)", Speed::miles_per_hour(3.0)),
("1 mph (manual wheelchair)", Speed::miles_per_hour(1.0)),
("5 mph (moderate jog)", Speed::miles_per_hour(5.0)),
]
}
pub fn default_speed() -> Speed {
WalkingOptions::common_speeds()[0].1
}
}
#[derive(PartialEq, Eq)]
struct Item {
cost: Duration,
node: WalkingNode,
}
impl PartialOrd for Item {
fn partial_cmp(&self, other: &Item) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl Ord for Item {
fn cmp(&self, other: &Item) -> Ordering {
let ord = other.cost.cmp(&self.cost);
if ord != Ordering::Equal {
return ord;
}
self.node.cmp(&other.node)
}
}
pub fn all_walking_costs_from(
map: &Map,
starts: Vec<BuildingID>,
time_limit: Duration,
opts: WalkingOptions,
) -> HashMap<BuildingID, Duration> {
if !opts.allow_shoulders
&& starts
.iter()
.all(|b| map.get_l(map.get_b(*b).sidewalk()).lane_type == LaneType::Shoulder)
{
return HashMap::new();
}
let mut queue: BinaryHeap<Item> = BinaryHeap::new();
for b in starts {
queue.push(Item {
cost: Duration::ZERO,
node: WalkingNode::closest(map.get_b(b).sidewalk_pos, map),
});
}
let mut cost_per_node: HashMap<WalkingNode, Duration> = HashMap::new();
while let Some(current) = queue.pop() {
if cost_per_node.contains_key(¤t.node) {
continue;
}
if current.cost > time_limit {
continue;
}
cost_per_node.insert(current.node, current.cost);
let (r, is_dst_i) = match current.node {
WalkingNode::SidewalkEndpoint(r, is_dst_i) => (r, is_dst_i),
_ => unreachable!(),
};
let lane = map.get_l(r.must_get_sidewalk(map));
if opts.allow_shoulders || lane.lane_type != LaneType::Shoulder {
queue.push(Item {
cost: current.cost
+ lane.length()
/ Traversable::Lane(lane.id).max_speed_along(
Some(opts.walking_speed),
PathConstraints::Pedestrian,
map,
),
node: WalkingNode::SidewalkEndpoint(r, !is_dst_i),
});
}
for turn in map.get_turns_for(lane.id, PathConstraints::Pedestrian) {
if (turn.id.parent == lane.dst_i) != is_dst_i {
continue;
}
queue.push(Item {
cost: current.cost
+ turn.geom.length()
/ Traversable::Turn(turn.id).max_speed_along(
Some(opts.walking_speed),
PathConstraints::Pedestrian,
map,
)
+ zone_cost(turn.id.to_movement(map), PathConstraints::Pedestrian, map),
node: WalkingNode::SidewalkEndpoint(
map.get_l(turn.id.dst).get_directed_parent(),
map.get_l(turn.id.dst).dst_i == turn.id.parent,
),
});
}
}
let mut results = HashMap::new();
for b in map.all_buildings() {
if let Some(cost) = cost_per_node.get(&WalkingNode::closest(b.sidewalk_pos, map)) {
let sidewalk_len = map.get_l(b.sidewalk()).length();
let bldg_dist = b.sidewalk_pos.dist_along();
let distance_from_closest_node = if sidewalk_len - bldg_dist <= bldg_dist {
bldg_dist
} else {
sidewalk_len - bldg_dist
};
let total_cost = *cost + distance_from_closest_node / opts.walking_speed;
results.insert(b.id, total_cost);
}
}
results
}