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
use std::ops::{RangeFrom, RangeFull, RangeInclusive, RangeToInclusive};

/// Inclusive range of floats, i.e. `min..=max`, but more ergonomic than [`RangeInclusive`].
#[repr(C)]
#[derive(Clone, Copy, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
#[cfg_attr(feature = "bytemuck", derive(bytemuck::Pod, bytemuck::Zeroable))]
pub struct Rangef {
    pub min: f32,
    pub max: f32,
}

impl Rangef {
    /// Infinite range that contains everything, from -∞ to +∞, inclusive.
    pub const EVERYTHING: Self = Self {
        min: f32::NEG_INFINITY,
        max: f32::INFINITY,
    };

    /// The inverse of [`Self::EVERYTHING`]: stretches from positive infinity to negative infinity.
    /// Contains nothing.
    pub const NOTHING: Self = Self {
        min: f32::INFINITY,
        max: f32::NEG_INFINITY,
    };

    /// An invalid [`Rangef`] filled with [`f32::NAN`].
    pub const NAN: Self = Self {
        min: f32::NAN,
        max: f32::NAN,
    };

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

    #[inline]
    pub fn point(min_and_max: f32) -> Self {
        Self {
            min: min_and_max,
            max: min_and_max,
        }
    }

    /// The length of the range, i.e. `max - min`.
    #[inline]
    pub fn span(self) -> f32 {
        self.max - self.min
    }

    /// The center of the range
    #[inline]
    pub fn center(self) -> f32 {
        0.5 * (self.min + self.max)
    }

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

    /// Equivalent to `x.clamp(min, max)`
    #[inline]
    #[must_use]
    pub fn clamp(self, x: f32) -> f32 {
        x.clamp(self.min, self.max)
    }

    /// Flip `min` and `max` if needed, so that `min <= max` after.
    #[inline]
    pub fn as_positive(self) -> Self {
        Self {
            min: self.min.min(self.max),
            max: self.min.max(self.max),
        }
    }

    /// Shrink by this much on each side, keeping the center
    #[inline]
    #[must_use]
    pub fn shrink(self, amnt: f32) -> Self {
        Self {
            min: self.min + amnt,
            max: self.max - amnt,
        }
    }

    /// Expand by this much on each side, keeping the center
    #[inline]
    #[must_use]
    pub fn expand(self, amnt: f32) -> Self {
        Self {
            min: self.min - amnt,
            max: self.max + amnt,
        }
    }

    /// Flip the min and the max
    #[inline]
    #[must_use]
    pub fn flip(self) -> Self {
        Self {
            min: self.max,
            max: self.min,
        }
    }

    /// The overlap of two ranges, i.e. the range that is contained by both.
    ///
    /// If the ranges do not overlap, returns a range with `span() < 0.0`.
    ///
    /// ```
    /// # use emath::Rangef;
    /// assert_eq!(Rangef::new(0.0, 10.0).intersection(Rangef::new(5.0, 15.0)), Rangef::new(5.0, 10.0));
    /// assert_eq!(Rangef::new(0.0, 10.0).intersection(Rangef::new(10.0, 20.0)), Rangef::new(10.0, 10.0));
    /// assert!(Rangef::new(0.0, 10.0).intersection(Rangef::new(20.0, 30.0)).span() < 0.0);
    /// ```
    #[inline]
    #[must_use]
    pub fn intersection(self, other: Self) -> Self {
        Self {
            min: self.min.max(other.min),
            max: self.max.min(other.max),
        }
    }

    /// Do the two ranges intersect?
    ///
    /// ```
    /// # use emath::Rangef;
    /// assert!(Rangef::new(0.0, 10.0).intersects(Rangef::new(5.0, 15.0)));
    /// assert!(Rangef::new(0.0, 10.0).intersects(Rangef::new(5.0, 6.0)));
    /// assert!(Rangef::new(0.0, 10.0).intersects(Rangef::new(10.0, 20.0)));
    /// assert!(!Rangef::new(0.0, 10.0).intersects(Rangef::new(20.0, 30.0)));
    /// ```
    #[inline]
    #[must_use]
    pub fn intersects(self, other: Self) -> bool {
        other.min <= self.max && self.min <= other.max
    }
}

impl From<Rangef> for RangeInclusive<f32> {
    #[inline]
    fn from(Rangef { min, max }: Rangef) -> Self {
        min..=max
    }
}

impl From<&Rangef> for RangeInclusive<f32> {
    #[inline]
    fn from(&Rangef { min, max }: &Rangef) -> Self {
        min..=max
    }
}

impl From<RangeInclusive<f32>> for Rangef {
    #[inline]
    fn from(range: RangeInclusive<f32>) -> Self {
        Self::new(*range.start(), *range.end())
    }
}

impl From<&RangeInclusive<f32>> for Rangef {
    #[inline]
    fn from(range: &RangeInclusive<f32>) -> Self {
        Self::new(*range.start(), *range.end())
    }
}

impl From<RangeFrom<f32>> for Rangef {
    #[inline]
    fn from(range: RangeFrom<f32>) -> Self {
        Self::new(range.start, f32::INFINITY)
    }
}

impl From<&RangeFrom<f32>> for Rangef {
    #[inline]
    fn from(range: &RangeFrom<f32>) -> Self {
        Self::new(range.start, f32::INFINITY)
    }
}

impl From<RangeFull> for Rangef {
    #[inline]
    fn from(_: RangeFull) -> Self {
        Self::new(f32::NEG_INFINITY, f32::INFINITY)
    }
}

impl From<&RangeFull> for Rangef {
    #[inline]
    fn from(_: &RangeFull) -> Self {
        Self::new(f32::NEG_INFINITY, f32::INFINITY)
    }
}

impl From<RangeToInclusive<f32>> for Rangef {
    #[inline]
    fn from(range: RangeToInclusive<f32>) -> Self {
        Self::new(f32::NEG_INFINITY, range.end)
    }
}

impl PartialEq<RangeInclusive<f32>> for Rangef {
    #[inline]
    fn eq(&self, other: &RangeInclusive<f32>) -> bool {
        self.min == *other.start() && self.max == *other.end()
    }
}

impl PartialEq<Rangef> for RangeInclusive<f32> {
    #[inline]
    fn eq(&self, other: &Rangef) -> bool {
        *self.start() == other.min && *self.end() == other.max
    }
}