use std::ops::{RangeFrom, RangeFull, RangeInclusive, RangeToInclusive};
#[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 {
pub const EVERYTHING: Self = Self {
min: f32::NEG_INFINITY,
max: f32::INFINITY,
};
pub const NOTHING: Self = Self {
min: f32::INFINITY,
max: f32::NEG_INFINITY,
};
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,
}
}
#[inline]
pub fn span(self) -> f32 {
self.max - self.min
}
#[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
}
#[inline]
#[must_use]
pub fn clamp(self, x: f32) -> f32 {
x.clamp(self.min, self.max)
}
#[inline]
pub fn as_positive(self) -> Self {
Self {
min: self.min.min(self.max),
max: self.min.max(self.max),
}
}
#[inline]
#[must_use]
pub fn shrink(self, amnt: f32) -> Self {
Self {
min: self.min + amnt,
max: self.max - amnt,
}
}
#[inline]
#[must_use]
pub fn expand(self, amnt: f32) -> Self {
Self {
min: self.min - amnt,
max: self.max + amnt,
}
}
#[inline]
#[must_use]
pub fn flip(self) -> Self {
Self {
min: self.max,
max: self.min,
}
}
#[inline]
#[must_use]
pub fn intersection(self, other: Self) -> Self {
Self {
min: self.min.max(other.min),
max: self.max.min(other.max),
}
}
#[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
}
}