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
use core::ops::{Div, Mul, RangeFrom, RangeFull, RangeInclusive, RangeToInclusive};
use crate::fast_midpoint;
/// 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 {
fast_midpoint(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 flipped(self) -> Self {
Self {
min: self.max,
max: self.min,
}
}
/// Flip the min and the max
#[inline]
#[must_use]
#[deprecated = "Renamed to `flipped`"]
pub fn flip(self) -> Self {
self.flipped()
}
/// 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),
}
}
/// The smallest range that contains both ranges.
///
/// If the ranges do not overlap, the gap between them is included too.
///
/// ```
/// # use emath::Rangef;
/// assert_eq!(Rangef::new(0.0, 10.0).union(Rangef::new(5.0, 15.0)), Rangef::new(0.0, 15.0));
/// assert_eq!(Rangef::new(0.0, 10.0).union(Rangef::new(20.0, 30.0)), Rangef::new(0.0, 30.0));
/// assert_eq!(Rangef::NOTHING.union(Rangef::point(3.0)), Rangef::point(3.0));
/// ```
#[inline]
#[must_use]
pub fn union(self, other: Self) -> Self {
Self {
min: self.min.min(other.min),
max: self.max.max(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())
}
}
/// Makes specifying size ranges slightly more convenient (no need for the extra `.0` suffixes)
impl From<RangeInclusive<i32>> for Rangef {
#[inline]
fn from(range: RangeInclusive<i32>) -> Self {
Self::new(*range.start() as _, *range.end() as _)
}
}
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
}
}
impl Mul<f32> for Rangef {
type Output = Self;
#[inline]
fn mul(self, factor: f32) -> Self {
Self {
min: self.min * factor,
max: self.max * factor,
}
}
}
impl Mul<Rangef> for f32 {
type Output = Rangef;
#[inline]
fn mul(self, range: Rangef) -> Rangef {
Rangef {
min: self * range.min,
max: self * range.max,
}
}
}
impl Div<f32> for Rangef {
type Output = Self;
#[inline]
fn div(self, factor: f32) -> Self {
Self {
min: self.min / factor,
max: self.max / factor,
}
}
}