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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
use std::fmt;

use serde::{Deserialize, Serialize};

use geom::{Angle, Distance, PolyLine, Pt2D, Speed};

use crate::{DirectedRoadID, Direction, LaneID, Map, MovementID, PathConstraints, TurnID};

/// Represents a specific point some distance along a lane.
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
pub struct Position {
    // Don't let callers construct a Position directly, so it's easy to find callers of new().
    lane: LaneID,
    dist_along: Distance,
}

impl fmt::Display for Position {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "Position({}, {})", self.lane, self.dist_along)
    }
}

impl Position {
    pub fn new(lane: LaneID, dist_along: Distance) -> Position {
        Position { lane, dist_along }
    }

    pub fn start(lane: LaneID) -> Position {
        Position {
            lane,
            dist_along: Distance::ZERO,
        }
    }

    pub fn end(lane: LaneID, map: &Map) -> Position {
        Position {
            lane,
            dist_along: map.get_l(lane).length(),
        }
    }

    pub fn lane(&self) -> LaneID {
        self.lane
    }

    pub fn dist_along(&self) -> Distance {
        self.dist_along
    }

    pub fn pt(&self, map: &Map) -> Pt2D {
        match map
            .get_l(self.lane)
            .lane_center_pts
            .dist_along(self.dist_along)
        {
            Ok((pt, _)) => pt,
            Err(err) => panic!("{} invalid: {}", self, err),
        }
    }

    pub fn pt_and_angle(&self, map: &Map) -> (Pt2D, Angle) {
        match map
            .get_l(self.lane)
            .lane_center_pts
            .dist_along(self.dist_along)
        {
            Ok(pair) => pair,
            Err(err) => panic!("{} invalid: {}", self, err),
        }
    }

    /// Given a position along a lane, find the equivalent position along another lane on the same
    /// road.
    pub fn equiv_pos(&self, lane: LaneID, map: &Map) -> Position {
        self.equiv_pos_for_long_object(lane, Distance::ZERO, map)
    }
    pub fn equiv_pos_for_long_object(
        &self,
        other_lane: LaneID,
        object_length: Distance,
        map: &Map,
    ) -> Position {
        let our_lane = map.get_l(self.lane);
        let other_lane = map.get_l(other_lane);
        assert_eq!(our_lane.id.road, other_lane.id.road);

        let pl = &other_lane.lane_center_pts;
        let pt = self.pt(map);
        if let Some((mut dist, _)) = pl.dist_along_of_point(pl.project_pt(pt)) {
            if other_lane.dir != our_lane.dir {
                // Account for the object_length if needed
                dist += object_length;
                dist = dist.max(Distance::ZERO).min(other_lane.length());
            }
            return Position::new(other_lane.id, dist);
        }
        // TODO So far I haven't observed this happening, but fallback if so.
        warn!("equiv_pos of {} for {} messes up", self, other_lane.id);

        let len = other_lane.length();
        if other_lane.dir == our_lane.dir {
            Position::new(other_lane.id, self.dist_along.min(len))
        } else {
            Position::new(
                other_lane.id,
                (len - self.dist_along + object_length)
                    .max(Distance::ZERO)
                    .min(len),
            )
        }
    }
    pub fn min_dist(mut self, dist_along: Distance, map: &Map) -> Option<Position> {
        if self.dist_along >= dist_along {
            return Some(self);
        }
        if map.get_l(self.lane).length() < dist_along {
            return None;
        }
        self.dist_along = dist_along;
        Some(self)
    }
    pub fn buffer_dist(mut self, buffer: Distance, map: &Map) -> Option<Position> {
        let len = map.get_l(self.lane).length();
        if len <= buffer * 2.0 {
            return None;
        }
        self.dist_along = self.dist_along.max(buffer).min(len - buffer);
        Some(self)
    }
}

/// Either a lane or a turn, where most movement happens.
// TODO Consider adding building and parking lot driveways here.
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq, PartialOrd, Ord, Serialize, Deserialize)]
pub enum Traversable {
    Lane(LaneID),
    Turn(TurnID),
}

impl fmt::Display for Traversable {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            Traversable::Lane(id) => write!(f, "Traversable::Lane({})", id.encode_u32()),
            Traversable::Turn(id) => write!(
                f,
                "Traversable::Turn({}, {}, {})",
                id.src, id.dst, id.parent
            ),
        }
    }
}

impl Traversable {
    pub fn as_lane(&self) -> LaneID {
        match *self {
            Traversable::Lane(id) => id,
            Traversable::Turn(_) => panic!("not a lane"),
        }
    }

    pub fn as_turn(&self) -> TurnID {
        match *self {
            Traversable::Turn(id) => id,
            Traversable::Lane(_) => panic!("not a turn"),
        }
    }

    pub fn maybe_turn(&self) -> Option<TurnID> {
        match *self {
            Traversable::Turn(id) => Some(id),
            Traversable::Lane(_) => None,
        }
    }

    pub fn maybe_lane(&self) -> Option<LaneID> {
        match *self {
            Traversable::Turn(_) => None,
            Traversable::Lane(id) => Some(id),
        }
    }

    /// Return the center-line geometry of this lane or turn.
    pub fn get_polyline(self, map: &Map) -> &PolyLine {
        match self {
            Traversable::Lane(id) => &map.get_l(id).lane_center_pts,
            Traversable::Turn(id) => &map.get_t(id).geom,
        }
    }

    pub fn get_zorder(&self, map: &Map) -> isize {
        match *self {
            Traversable::Lane(id) => map.get_parent(id).zorder,
            Traversable::Turn(id) => map.get_i(id.parent).get_zorder(map),
        }
    }

    /// The single definitive place to determine how fast somebody could go along a single road.
    /// This should be used for pathfinding and simulation. Returns (speed, percent incline).
    pub(crate) fn max_speed_along_road(
        dr: DirectedRoadID,
        max_speed_on_flat_ground: Option<Speed>,
        constraints: PathConstraints,
        map: &Map,
    ) -> (Speed, f64) {
        let road = map.get_r(dr.road);
        let percent_incline = if dr.dir == Direction::Fwd {
            road.percent_incline
        } else {
            -road.percent_incline
        };

        let base = if constraints == PathConstraints::Bike {
            // We assume every bike has a max_speed defined.
            bike_speed_on_incline(max_speed_on_flat_ground.unwrap(), percent_incline)
        } else if constraints == PathConstraints::Pedestrian {
            // We assume every pedestrian has a max_speed defined.
            walking_speed_on_incline(max_speed_on_flat_ground.unwrap(), percent_incline)
        } else {
            debug_assert!(max_speed_on_flat_ground.is_none());
            // Incline doesn't affect cars, buses, or trains
            road.speed_limit
        };

        let speed = if let Some(s) = max_speed_on_flat_ground {
            base.min(s)
        } else {
            base
        };
        (speed, percent_incline)
    }

    /// The single definitive place to determine how fast somebody could go along a single
    /// movement. This should be used for pathfinding and simulation. Ignores elevation.
    pub(crate) fn max_speed_along_movement(
        mvmnt: MovementID,
        max_speed_on_flat_ground: Option<Speed>,
        _: PathConstraints,
        map: &Map,
    ) -> Speed {
        // TODO Ignore elevation on turns?
        let base = map
            .get_r(mvmnt.from.road)
            .speed_limit
            .min(map.get_r(mvmnt.to.road).speed_limit);
        if let Some(s) = max_speed_on_flat_ground {
            base.min(s)
        } else {
            base
        }
    }
}

// 10 mph
pub const MAX_BIKE_SPEED: Speed = Speed::const_meters_per_second(4.4704);
// 3 mph
pub const MAX_WALKING_SPEED: Speed = Speed::const_meters_per_second(1.34112);

fn bike_speed_on_incline(max_speed: Speed, percent_incline: f64) -> Speed {
    // There doesn't seem to be a straightforward way of calculating how an "average" cyclist's
    // speed is affected by hills. http://www.kreuzotter.de/english/espeed.htm has lots of detail,
    // but we'd need to guess values like body size, type of bike, etc.
    // https://github.com/ibi-group/OpenTripPlanner/blob/65dcf0a4142e31028cf9d1b2c15ad32dd1084252/src/main/java/org/opentripplanner/routing/edgetype/StreetEdge.java#L934-L1082
    // is built from this, but seems to be more appropriate for motorized micromobility devices
    // like e-scooters.

    // So, we'll adapt the table from Valhalla --
    // https://valhalla.readthedocs.io/en/latest/sif/elevation_costing/ describes how this works.
    // Their "weighted grade" should be roughly equivalent to how the elevation_lookups package we
    // use calculates things.  This table comes from
    // https://github.com/valhalla/valhalla/blob/f899a940ccbd0bc986769197dec5bb9383014afb/src/sif/bicyclecost.cc#L139.
    // Valhalla is MIT licensed: https://github.com/valhalla/valhalla/blob/master/COPYING.

    // TODO Could binary search or do something a bit faster here, but doesn't matter much
    let pct = percent_incline * 100.0;
    for (grade, factor) in vec![
        (-10.0, 2.2),
        (-8.0, 2.0),
        (-6.5, 1.9),
        (-5.0, 1.7),
        (-3.0, 1.4),
        (-1.5, 1.2),
        (0.0, 1.0),
        (1.5, 0.95),
        (3.0, 0.85),
        (5.0, 0.75),
        (6.5, 0.65),
        (8.0, 0.55),
        (10.0, 0.5),
        (11.5, 0.45),
        (13.0, 0.4),
    ] {
        if pct <= grade {
            return factor * max_speed;
        }
    }
    // The last pair is a factor of 0.3 for grades of 15%, but we'll use it for anything steeper
    // than 15%
    0.3 * max_speed
}

fn walking_speed_on_incline(max_speed: Speed, percent_incline: f64) -> Speed {
    // https://en.wikipedia.org/wiki/Tobler%27s_hiking_function
    let exp = -3.5 * (percent_incline + 0.05).abs();
    let tobler = Speed::km_per_hour(6.0 * std::f64::consts::E.powf(exp));
    // Tobler's hiking function assumes a flat speed of 5km/hr, but that's different than ours.
    // Just scale our max_speed proportionally.
    let result = (tobler / Speed::km_per_hour(5.0)) * max_speed;
    // Data quality issues, y'know...
    if result == Speed::ZERO {
        error!(
            "walking_speed_on_incline saw an incline of {}. Not going to reduce the speed to 0!",
            percent_incline
        );
        return 0.1 * max_speed;
    }
    result
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_bike_speed_on_incline() {
        let base_speed = MAX_BIKE_SPEED;
        assert_approx_eq(
            Speed::miles_per_hour(10.0),
            bike_speed_on_incline(base_speed, 0.0),
        );
        assert_approx_eq(
            Speed::miles_per_hour(22.0),
            bike_speed_on_incline(base_speed, -0.15),
        );
        assert_approx_eq(
            Speed::miles_per_hour(3.0),
            bike_speed_on_incline(base_speed, 0.15),
        );
    }

    #[test]
    fn test_walking_speed_on_incline() {
        let base_speed = MAX_WALKING_SPEED;
        assert_approx_eq(
            Speed::miles_per_hour(3.0),
            walking_speed_on_incline(base_speed, 0.0),
        );
        assert_approx_eq(
            Speed::miles_per_hour(2.54),
            walking_speed_on_incline(base_speed, -0.15),
        );
        assert_approx_eq(
            Speed::miles_per_hour(1.79),
            walking_speed_on_incline(base_speed, 0.15),
        );
    }

    fn assert_approx_eq(s1: Speed, s2: Speed) {
        if (s1.inner_meters_per_second() - s2.inner_meters_per_second()).abs() > 0.1 {
            // Print in mph without rounding
            panic!(
                "{} != {}",
                2.23694 * s1.inner_meters_per_second(),
                2.23694 * s2.inner_meters_per_second()
            );
        }
    }
}