use core::{
cmp::{max_by, min_by},
ops::RangeInclusive,
};
use derive_more::derive::{Display, Error};
use itertools::Either;
#[cfg(feature = "bevy_reflect")]
use bevy_reflect::Reflect;
#[cfg(all(feature = "serialize", feature = "bevy_reflect"))]
use bevy_reflect::{ReflectDeserialize, ReflectSerialize};
#[derive(Debug, Clone, Copy, PartialEq, PartialOrd)]
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "bevy_reflect", derive(Reflect), reflect(Debug, PartialEq))]
#[cfg_attr(
all(feature = "serialize", feature = "bevy_reflect"),
reflect(Serialize, Deserialize)
)]
pub struct Interval {
start: f32,
end: f32,
}
#[derive(Debug, Error, Display)]
#[display("The resulting interval would be invalid (empty or with a NaN endpoint)")]
pub struct InvalidIntervalError;
#[derive(Debug, Error, Display)]
#[display("Cannot extract spaced points from an unbounded interval")]
pub struct SpacedPointsError;
#[derive(Debug, Error, Display)]
#[display("Could not construct linear function to map between intervals")]
pub(super) enum LinearMapError {
#[display("The source interval is unbounded")]
SourceUnbounded,
#[display("The target interval is unbounded")]
TargetUnbounded,
}
impl Interval {
#[inline]
pub fn new(start: f32, end: f32) -> Result<Self, InvalidIntervalError> {
if start >= end || start.is_nan() || end.is_nan() {
Err(InvalidIntervalError)
} else {
Ok(Self { start, end })
}
}
pub const UNIT: Self = Self {
start: 0.0,
end: 1.0,
};
pub const EVERYWHERE: Self = Self {
start: f32::NEG_INFINITY,
end: f32::INFINITY,
};
#[inline]
pub const fn start(self) -> f32 {
self.start
}
#[inline]
pub const fn end(self) -> f32 {
self.end
}
pub fn intersect(self, other: Interval) -> Result<Interval, InvalidIntervalError> {
let lower = max_by(self.start, other.start, f32::total_cmp);
let upper = min_by(self.end, other.end, f32::total_cmp);
Self::new(lower, upper)
}
#[inline]
pub fn length(self) -> f32 {
self.end - self.start
}
#[inline]
pub fn is_bounded(self) -> bool {
self.length().is_finite()
}
#[inline]
pub fn has_finite_start(self) -> bool {
self.start.is_finite()
}
#[inline]
pub fn has_finite_end(self) -> bool {
self.end.is_finite()
}
#[inline]
pub fn contains(self, item: f32) -> bool {
(self.start..=self.end).contains(&item)
}
#[inline]
pub fn contains_interval(self, other: Self) -> bool {
self.start <= other.start && self.end >= other.end
}
#[inline]
pub fn clamp(self, value: f32) -> f32 {
value.clamp(self.start, self.end)
}
#[inline]
pub fn spaced_points(
self,
points: usize,
) -> Result<impl Iterator<Item = f32>, SpacedPointsError> {
if !self.is_bounded() {
return Err(SpacedPointsError);
}
if points < 2 {
let iter = (points == 1).then_some(self.start).into_iter();
return Ok(Either::Left(iter));
}
let step = self.length() / (points - 1) as f32;
let iter = (0..points).map(move |x| self.start + x as f32 * step);
Ok(Either::Right(iter))
}
#[inline]
pub(super) fn linear_map_to(self, other: Self) -> Result<impl Fn(f32) -> f32, LinearMapError> {
if !self.is_bounded() {
return Err(LinearMapError::SourceUnbounded);
}
if !other.is_bounded() {
return Err(LinearMapError::TargetUnbounded);
}
let scale = other.length() / self.length();
Ok(move |x| (x - self.start) * scale + other.start)
}
}
impl TryFrom<RangeInclusive<f32>> for Interval {
type Error = InvalidIntervalError;
fn try_from(range: RangeInclusive<f32>) -> Result<Self, Self::Error> {
Interval::new(*range.start(), *range.end())
}
}
#[inline]
pub fn interval(start: f32, end: f32) -> Result<Interval, InvalidIntervalError> {
Interval::new(start, end)
}
#[cfg(test)]
mod tests {
use super::*;
use approx::{assert_abs_diff_eq, AbsDiffEq};
#[test]
fn make_intervals() {
let ivl = Interval::new(2.0, -1.0);
assert!(ivl.is_err());
let ivl = Interval::new(-0.0, 0.0);
assert!(ivl.is_err());
let ivl = Interval::new(f32::NEG_INFINITY, 15.5);
assert!(ivl.is_ok());
let ivl = Interval::new(-2.0, f32::INFINITY);
assert!(ivl.is_ok());
let ivl = Interval::new(f32::NEG_INFINITY, f32::INFINITY);
assert!(ivl.is_ok());
let ivl = Interval::new(f32::INFINITY, f32::NEG_INFINITY);
assert!(ivl.is_err());
let ivl = Interval::new(-1.0, f32::NAN);
assert!(ivl.is_err());
let ivl = Interval::new(f32::NAN, -42.0);
assert!(ivl.is_err());
let ivl = Interval::new(f32::NAN, f32::NAN);
assert!(ivl.is_err());
let ivl = Interval::new(0.0, 1.0);
assert!(ivl.is_ok());
}
#[test]
fn lengths() {
let ivl = interval(-5.0, 10.0).unwrap();
assert!((ivl.length() - 15.0).abs() <= f32::EPSILON);
let ivl = interval(5.0, 100.0).unwrap();
assert!((ivl.length() - 95.0).abs() <= f32::EPSILON);
let ivl = interval(0.0, f32::INFINITY).unwrap();
assert_eq!(ivl.length(), f32::INFINITY);
let ivl = interval(f32::NEG_INFINITY, 0.0).unwrap();
assert_eq!(ivl.length(), f32::INFINITY);
let ivl = Interval::EVERYWHERE;
assert_eq!(ivl.length(), f32::INFINITY);
}
#[test]
fn intersections() {
let ivl1 = interval(-1.0, 1.0).unwrap();
let ivl2 = interval(0.0, 2.0).unwrap();
let ivl3 = interval(-3.0, 0.0).unwrap();
let ivl4 = interval(0.0, f32::INFINITY).unwrap();
let ivl5 = interval(f32::NEG_INFINITY, 0.0).unwrap();
let ivl6 = Interval::EVERYWHERE;
assert!(ivl1.intersect(ivl2).is_ok_and(|ivl| ivl == Interval::UNIT));
assert!(ivl1
.intersect(ivl3)
.is_ok_and(|ivl| ivl == interval(-1.0, 0.0).unwrap()));
assert!(ivl2.intersect(ivl3).is_err());
assert!(ivl1.intersect(ivl4).is_ok_and(|ivl| ivl == Interval::UNIT));
assert!(ivl1
.intersect(ivl5)
.is_ok_and(|ivl| ivl == interval(-1.0, 0.0).unwrap()));
assert!(ivl4.intersect(ivl5).is_err());
assert_eq!(ivl1.intersect(ivl6).unwrap(), ivl1);
assert_eq!(ivl4.intersect(ivl6).unwrap(), ivl4);
assert_eq!(ivl5.intersect(ivl6).unwrap(), ivl5);
}
#[test]
fn containment() {
let ivl = Interval::UNIT;
assert!(ivl.contains(0.0));
assert!(ivl.contains(1.0));
assert!(ivl.contains(0.5));
assert!(!ivl.contains(-0.1));
assert!(!ivl.contains(1.1));
assert!(!ivl.contains(f32::NAN));
let ivl = interval(3.0, f32::INFINITY).unwrap();
assert!(ivl.contains(3.0));
assert!(ivl.contains(2.0e5));
assert!(ivl.contains(3.5e6));
assert!(!ivl.contains(2.5));
assert!(!ivl.contains(-1e5));
assert!(!ivl.contains(f32::NAN));
}
#[test]
fn interval_containment() {
let ivl = Interval::UNIT;
assert!(ivl.contains_interval(interval(-0.0, 0.5).unwrap()));
assert!(ivl.contains_interval(interval(0.5, 1.0).unwrap()));
assert!(ivl.contains_interval(interval(0.25, 0.75).unwrap()));
assert!(!ivl.contains_interval(interval(-0.25, 0.5).unwrap()));
assert!(!ivl.contains_interval(interval(0.5, 1.25).unwrap()));
assert!(!ivl.contains_interval(interval(0.25, f32::INFINITY).unwrap()));
assert!(!ivl.contains_interval(interval(f32::NEG_INFINITY, 0.75).unwrap()));
let big_ivl = interval(0.0, f32::INFINITY).unwrap();
assert!(big_ivl.contains_interval(interval(0.0, 5.0).unwrap()));
assert!(big_ivl.contains_interval(interval(0.0, f32::INFINITY).unwrap()));
assert!(big_ivl.contains_interval(interval(1.0, 5.0).unwrap()));
assert!(!big_ivl.contains_interval(interval(-1.0, f32::INFINITY).unwrap()));
assert!(!big_ivl.contains_interval(interval(-2.0, 5.0).unwrap()));
}
#[test]
fn boundedness() {
assert!(!Interval::EVERYWHERE.is_bounded());
assert!(interval(0.0, 3.5e5).unwrap().is_bounded());
assert!(!interval(-2.0, f32::INFINITY).unwrap().is_bounded());
assert!(!interval(f32::NEG_INFINITY, 5.0).unwrap().is_bounded());
}
#[test]
fn linear_maps() {
let ivl1 = interval(-3.0, 5.0).unwrap();
let ivl2 = Interval::UNIT;
let map = ivl1.linear_map_to(ivl2);
assert!(map.is_ok_and(|f| f(-3.0).abs_diff_eq(&0.0, f32::EPSILON)
&& f(5.0).abs_diff_eq(&1.0, f32::EPSILON)
&& f(1.0).abs_diff_eq(&0.5, f32::EPSILON)));
let ivl1 = Interval::UNIT;
let ivl2 = Interval::EVERYWHERE;
assert!(ivl1.linear_map_to(ivl2).is_err());
let ivl1 = interval(f32::NEG_INFINITY, -4.0).unwrap();
let ivl2 = Interval::UNIT;
assert!(ivl1.linear_map_to(ivl2).is_err());
}
#[test]
fn spaced_points() {
let ivl = interval(0.0, 50.0).unwrap();
let points_iter: Vec<f32> = ivl.spaced_points(1).unwrap().collect();
assert_abs_diff_eq!(points_iter[0], 0.0);
assert_eq!(points_iter.len(), 1);
let points_iter: Vec<f32> = ivl.spaced_points(2).unwrap().collect();
assert_abs_diff_eq!(points_iter[0], 0.0);
assert_abs_diff_eq!(points_iter[1], 50.0);
let points_iter = ivl.spaced_points(21).unwrap();
let step = ivl.length() / 20.0;
for (index, point) in points_iter.enumerate() {
let expected = ivl.start() + step * index as f32;
assert_abs_diff_eq!(point, expected);
}
let ivl = interval(-21.0, 79.0).unwrap();
let points_iter = ivl.spaced_points(10000).unwrap();
let step = ivl.length() / 9999.0;
for (index, point) in points_iter.enumerate() {
let expected = ivl.start() + step * index as f32;
assert_abs_diff_eq!(point, expected);
}
let ivl = interval(-1.0, f32::INFINITY).unwrap();
let points_iter = ivl.spaced_points(25);
assert!(points_iter.is_err());
let ivl = interval(f32::NEG_INFINITY, -25.0).unwrap();
let points_iter = ivl.spaced_points(9);
assert!(points_iter.is_err());
}
}