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
use std::fmt::Debug;
use serde::{Deserialize, Serialize};
use crate::collection::OvertureMapsCollectionError;
/// This function takes a [`Vec<f64>`] and returns `a` and `b` if and only
/// if the vector has exactly two elements and the second one is higher than the
/// first one. Otherwise it returns an error.
pub(super) fn validate_between_vector(
b_vector: &Vec<f64>,
) -> Result<(&f64, &f64), OvertureMapsCollectionError> {
let [low, high] = b_vector.as_slice() else {
return Err(OvertureMapsCollectionError::InvalidBetweenVector(
"Between vector has length != 2".to_string(),
));
};
if high < low {
return Err(OvertureMapsCollectionError::InvalidBetweenVector(format!(
"`high` is lower than `low`: [{low}, {high}]"
)));
}
Ok((low, high))
}
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct SegmentValueBetween<T> {
#[serde(skip_serializing_if = "Option::is_none", default = "default_none")]
pub value: Option<T>,
#[serde(skip_serializing_if = "Option::is_none", default)]
pub between: Option<Vec<f64>>,
}
fn default_none<T>() -> Option<T> {
None
}
impl<T: Debug> SegmentValueBetween<T> {
/// Used to filter limits based on a linear reference segment.
/// Returns `true` if the open interval `(between[0], between[1])`
/// overlaps with the open interval `(start, end)`.
///
/// # Examples
///
/// ```
/// # use bambam_omf::collection::SegmentSpeedLimit;
///
/// let limit = SegmentSpeedLimit {
/// min_speed: None,
/// max_speed: None,
/// is_max_speed_variable: None,
/// when: None,
/// between: Some(vec![10.0, 20.0]),
/// };
///
/// // (15, 25) overlaps with (10, 20)
/// assert!(limit.check_open_intersection(15.0, 25.0).unwrap());
/// // (20, 30) does not overlap with open interval (10, 20)
/// assert!(!limit.check_open_intersection(20.0, 30.0).unwrap());
/// ```
pub fn check_open_intersection(
&self,
start: f64,
end: f64,
) -> Result<bool, OvertureMapsCollectionError> {
let b_vector =
self.between
.as_ref()
.ok_or(OvertureMapsCollectionError::InvalidBetweenVector(format!(
"`between` vector is empty: {self:?}"
)))?;
let (low, high) = validate_between_vector(b_vector)?;
Ok(start < *high && end > *low)
}
}