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
use std::ops::RangeInclusive;

use crate::{NonMinI64, TimeInt, TimeReal};

// ----------------------------------------------------------------------------

#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub struct ResolvedTimeRange {
    min: TimeInt,
    max: TimeInt,
}

impl ResolvedTimeRange {
    /// Contains no time at all.
    pub const EMPTY: Self = Self {
        min: TimeInt::MAX,
        max: TimeInt::MIN,
    };

    /// Contains all time.
    pub const EVERYTHING: Self = Self {
        min: TimeInt::MIN,
        max: TimeInt::MAX,
    };

    /// Creates a new temporal [`ResolvedTimeRange`].
    ///
    /// The returned range is guaranteed to never include [`TimeInt::STATIC`].
    #[inline]
    pub fn new(min: impl TryInto<TimeInt>, max: impl TryInto<TimeInt>) -> Self {
        let min = min.try_into().unwrap_or(TimeInt::MIN).max(TimeInt::MIN);
        let max = max.try_into().unwrap_or(TimeInt::MIN).max(TimeInt::MIN);
        Self { min, max }
    }

    /// The returned range is guaranteed to never include [`TimeInt::STATIC`].
    #[inline]
    pub fn point(time: impl TryInto<TimeInt>) -> Self {
        let time = time.try_into().unwrap_or(TimeInt::MIN).max(TimeInt::MIN);
        Self {
            min: time,
            max: time,
        }
    }

    #[inline]
    pub fn min(&self) -> TimeInt {
        self.min
    }

    #[inline]
    pub fn max(&self) -> TimeInt {
        self.max
    }

    /// Overwrites the start bound of the range.
    ///
    /// The resulting range is guaranteed to never include [`TimeInt::STATIC`].
    #[inline]
    pub fn set_min(&mut self, time: impl TryInto<TimeInt>) {
        let time = time.try_into().unwrap_or(TimeInt::MIN).max(TimeInt::MIN);
        self.min = time;
    }

    /// Overwrites the end bound of the range.
    ///
    /// The resulting range is guaranteed to never include [`TimeInt::STATIC`].
    #[inline]
    pub fn set_max(&mut self, time: impl TryInto<TimeInt>) {
        let time = time.try_into().unwrap_or(TimeInt::MIN).max(TimeInt::MIN);
        self.max = time;
    }

    /// The amount of time or sequences covered by this range.
    #[inline]
    pub fn abs_length(&self) -> u64 {
        self.min.as_i64().abs_diff(self.max.as_i64())
    }

    #[inline]
    pub fn center(&self) -> TimeInt {
        let center = NonMinI64::new((self.abs_length() / 2) as i64).unwrap_or(NonMinI64::MIN);
        self.min + TimeInt::from(center)
    }

    #[inline]
    pub fn contains(&self, time: TimeInt) -> bool {
        self.min <= time && time <= self.max
    }

    #[inline]
    pub fn intersects(&self, other: Self) -> bool {
        self.min <= other.max && self.max >= other.min
    }

    #[inline]
    pub fn union(&self, other: Self) -> Self {
        Self {
            min: self.min.min(other.min),
            max: self.max.max(other.max),
        }
    }

    pub fn from_relative_time_range(
        range: &re_types_core::datatypes::TimeRange,
        cursor: impl Into<re_types_core::datatypes::TimeInt>,
    ) -> Self {
        let cursor = cursor.into();

        let mut min = range.start.start_boundary_time(cursor);
        let mut max = range.end.end_boundary_time(cursor);

        if min > max {
            std::mem::swap(&mut min, &mut max);
        }

        Self::new(min, max)
    }
}

impl re_types_core::SizeBytes for ResolvedTimeRange {
    #[inline]
    fn heap_size_bytes(&self) -> u64 {
        0
    }
}

// ----------------------------------------------------------------------------

/// Like [`ResolvedTimeRange`], but using [`TimeReal`] for improved precision.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub struct ResolvedTimeRangeF {
    pub min: TimeReal,
    pub max: TimeReal,
}

impl ResolvedTimeRangeF {
    #[inline]
    pub fn new(min: impl Into<TimeReal>, max: impl Into<TimeReal>) -> Self {
        Self {
            min: min.into(),
            max: max.into(),
        }
    }

    #[inline]
    pub fn point(value: impl Into<TimeReal>) -> Self {
        let value = value.into();
        Self {
            min: value,
            max: value,
        }
    }

    /// Inclusive
    pub fn contains(&self, value: TimeReal) -> bool {
        self.min <= value && value <= self.max
    }

    /// Where in the range is this value? Returns 0-1 if within the range.
    ///
    /// Returns <0 if before and >1 if after.
    pub fn inverse_lerp(&self, value: TimeReal) -> f64 {
        if self.min == self.max {
            0.5
        } else {
            (value - self.min).as_f64() / (self.max - self.min).as_f64()
        }
    }

    pub fn lerp(&self, t: f64) -> TimeReal {
        self.min + (self.max - self.min) * t
    }

    #[inline]
    pub fn is_empty(&self) -> bool {
        self.min == self.max
    }

    /// The amount of time or sequences covered by this range.
    #[inline]
    pub fn length(&self) -> TimeReal {
        self.max - self.min
    }
}

impl From<ResolvedTimeRangeF> for RangeInclusive<TimeReal> {
    fn from(range: ResolvedTimeRangeF) -> Self {
        range.min..=range.max
    }
}

impl From<&ResolvedTimeRangeF> for RangeInclusive<TimeReal> {
    fn from(range: &ResolvedTimeRangeF) -> Self {
        range.min..=range.max
    }
}

impl From<ResolvedTimeRange> for ResolvedTimeRangeF {
    fn from(range: ResolvedTimeRange) -> Self {
        Self::new(range.min, range.max)
    }
}