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

use crate::{TimeInt, TimeReal};

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

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

impl TimeRange {
    /// 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,
    };

    #[inline]
    pub fn new(min: TimeInt, max: TimeInt) -> Self {
        Self { min, max }
    }

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

    /// 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 {
        self.min + TimeInt::from((self.abs_length() / 2) as i64)
    }

    #[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),
        }
    }
}

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

impl From<TimeRange> for RangeInclusive<TimeInt> {
    fn from(range: TimeRange) -> RangeInclusive<TimeInt> {
        range.min..=range.max
    }
}

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

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

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

impl TimeRangeF {
    #[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,
        }
    }

    // pub fn add(&mut self, value: TimeReal) {
    //     self.min = self.min.min(value);
    //     self.max = self.max.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<TimeRangeF> for RangeInclusive<TimeReal> {
    fn from(range: TimeRangeF) -> RangeInclusive<TimeReal> {
        range.min..=range.max
    }
}

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

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