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
use geom::Pt2D;
use map_model::{BuildingID, IntersectionID, Map, PathConstraints, PathRequest, Position};
use serde::{Deserialize, Serialize};
use crate::TripMode;
#[derive(Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord, Debug, Clone, Copy)]
pub enum TripEndpoint {
Building(BuildingID),
Border(IntersectionID),
SuddenlyAppear(Position),
}
impl TripEndpoint {
pub fn pt(self, map: &Map) -> Pt2D {
match self {
TripEndpoint::Building(b) => map.get_b(b).polygon.center(),
TripEndpoint::Border(i) => map.get_i(i).polygon.center(),
TripEndpoint::SuddenlyAppear(pos) => pos.pt(map),
}
}
pub fn path_req(
from: TripEndpoint,
to: TripEndpoint,
mode: TripMode,
map: &Map,
) -> Option<PathRequest> {
let start = from.pos(mode, true, map)?;
let end = to.pos(mode, false, map)?;
Some(match mode {
TripMode::Walk | TripMode::Transit => PathRequest::walking(start, end),
TripMode::Bike => PathRequest::vehicle(start, end, PathConstraints::Bike),
TripMode::Drive => {
if matches!(from, TripEndpoint::Building(_)) {
PathRequest::leave_from_driveway(start, end, PathConstraints::Car, map)
} else {
PathRequest::vehicle(start, end, PathConstraints::Car)
}
}
})
}
fn pos(self, mode: TripMode, from: bool, map: &Map) -> Option<Position> {
match mode {
TripMode::Walk | TripMode::Transit => self.sidewalk_pos(map, from),
TripMode::Drive | TripMode::Bike => {
let constraints = mode.to_constraints();
if from {
match self {
TripEndpoint::Building(_) => {}
TripEndpoint::Border(i) => {
return map.get_i(i).some_outgoing_road(map).and_then(|dr| {
dr.lanes(constraints, map)
.get(0)
.map(|l| Position::start(*l))
});
}
TripEndpoint::SuddenlyAppear(pos) => {
return Some(pos);
}
}
}
match self {
TripEndpoint::Building(b) => match constraints {
PathConstraints::Car => {
let driving_lane = map.find_driving_lane_near_building(b);
let sidewalk_pos = map.get_b(b).sidewalk_pos;
if driving_lane.road == sidewalk_pos.lane().road {
Some(sidewalk_pos.equiv_pos(driving_lane, map))
} else {
Some(Position::start(driving_lane))
}
}
PathConstraints::Bike => Some(map.get_b(b).biking_connection(map)?.0),
PathConstraints::Bus
| PathConstraints::Train
| PathConstraints::Pedestrian => {
unreachable!()
}
},
TripEndpoint::Border(i) => {
map.get_i(i).some_incoming_road(map).and_then(|dr| {
let lanes = dr.lanes(constraints, map);
if lanes.is_empty() {
None
} else {
Some(Position::end(lanes[0], map))
}
})
}
TripEndpoint::SuddenlyAppear(_) => unreachable!(),
}
}
}
}
fn sidewalk_pos(self, map: &Map, from: bool) -> Option<Position> {
match self {
TripEndpoint::Building(b) => Some(map.get_b(b).sidewalk_pos),
TripEndpoint::Border(i) => {
if from {
TripEndpoint::start_walking_at_border(i, map)
} else {
TripEndpoint::end_walking_at_border(i, map)
}
}
TripEndpoint::SuddenlyAppear(pos) => Some(pos),
}
}
pub fn start_walking_at_border(i: IntersectionID, map: &Map) -> Option<Position> {
let lanes = map
.get_i(i)
.get_outgoing_lanes(map, PathConstraints::Pedestrian);
if !lanes.is_empty() {
return Some(Position::start(lanes[0]));
}
map.get_i(i)
.get_incoming_lanes(map, PathConstraints::Pedestrian)
.get(0)
.map(|l| Position::end(*l, map))
}
pub fn end_walking_at_border(i: IntersectionID, map: &Map) -> Option<Position> {
if let Some(l) = map
.get_i(i)
.get_incoming_lanes(map, PathConstraints::Pedestrian)
.get(0)
{
return Some(Position::end(*l, map));
}
let lanes = map
.get_i(i)
.get_outgoing_lanes(map, PathConstraints::Pedestrian);
if lanes.is_empty() {
return None;
}
Some(Position::start(lanes[0]))
}
}