1
0
mirror of https://github.com/emilk/egui.git synced 2026-08-30 05:10:03 -04:00

Replace uses of RangeInclusive<f32> with emath::Rangef (#3221)

* Replace uses of `RangeInclusive<f32>` with `emath::Rangef`

* Fix doc-test
This commit is contained in:
Emil Ernerfeldt
2023-08-10 13:07:00 +02:00
committed by GitHub
parent 4c3b380889
commit 8cdffc4e2d
14 changed files with 213 additions and 161 deletions

View File

@@ -110,29 +110,25 @@ impl Align {
/// assert_eq!(Max .align_size_within_range(INFINITY, NEG_INFINITY..=20.0), NEG_INFINITY..=20.0);
/// ```
#[inline]
pub fn align_size_within_range(
self,
size: f32,
range: RangeInclusive<f32>,
) -> RangeInclusive<f32> {
let min = *range.start();
let max = *range.end();
pub fn align_size_within_range(self, size: f32, range: impl Into<Rangef>) -> Rangef {
let range = range.into();
let Rangef { min, max } = range;
if max - min == f32::INFINITY && size == f32::INFINITY {
return range;
}
match self {
Self::Min => min..=min + size,
Self::Min => Rangef::new(min, min + size),
Self::Center => {
if size == f32::INFINITY {
f32::NEG_INFINITY..=f32::INFINITY
Rangef::new(f32::NEG_INFINITY, f32::INFINITY)
} else {
let left = (min + max) / 2.0 - size / 2.0;
left..=left + size
Rangef::new(left, left + size)
}
}
Self::Max => max - size..=max,
Self::Max => Rangef::new(max - size, max),
}
}
}

View File

@@ -99,11 +99,12 @@ impl Real for f64 {}
/// assert_eq!(lerp(1.0..=5.0, 2.0), 9.0);
/// ```
#[inline(always)]
pub fn lerp<R, T>(range: RangeInclusive<R>, t: T) -> R
pub fn lerp<R, T>(range: impl Into<RangeInclusive<R>>, t: T) -> R
where
T: Real + Mul<R, Output = R>,
R: Copy + Add<R, Output = R>,
{
let range = range.into();
(T::one() - t) * *range.start() + t * *range.end()
}
@@ -138,20 +139,28 @@ where
/// Linearly remap a value from one range to another,
/// so that when `x == from.start()` returns `to.start()`
/// and when `x == from.end()` returns `to.end()`.
pub fn remap<T>(x: T, from: RangeInclusive<T>, to: RangeInclusive<T>) -> T
pub fn remap<T>(x: T, from: impl Into<RangeInclusive<T>>, to: impl Into<RangeInclusive<T>>) -> T
where
T: Real,
{
let from = from.into();
let to = to.into();
crate::emath_assert!(from.start() != from.end());
let t = (x - *from.start()) / (*from.end() - *from.start());
lerp(to, t)
}
/// Like [`remap`], but also clamps the value so that the returned value is always in the `to` range.
pub fn remap_clamp<T>(x: T, from: RangeInclusive<T>, to: RangeInclusive<T>) -> T
pub fn remap_clamp<T>(
x: T,
from: impl Into<RangeInclusive<T>>,
to: impl Into<RangeInclusive<T>>,
) -> T
where
T: Real,
{
let from = from.into();
let to = to.into();
if from.end() < from.start() {
return remap_clamp(x, *from.end()..=*from.start(), *to.end()..=*to.start());
}

View File

@@ -36,14 +36,60 @@ impl Rangef {
}
#[inline]
pub fn span(&self) -> f32 {
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
}
#[inline]
pub fn contains(&self, x: f32) -> bool {
#[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 {
Rangef {
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,
}
}
}
impl From<Rangef> for RangeInclusive<f32> {
@@ -108,3 +154,17 @@ impl From<RangeToInclusive<f32>> for Rangef {
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
}
}

View File

@@ -1,5 +1,4 @@
use std::f32::INFINITY;
use std::ops::RangeInclusive;
use crate::*;
@@ -82,15 +81,12 @@ impl Rect {
}
#[inline(always)]
pub fn from_x_y_ranges(
x_range: impl Into<RangeInclusive<f32>>,
y_range: impl Into<RangeInclusive<f32>>,
) -> Self {
pub fn from_x_y_ranges(x_range: impl Into<Rangef>, y_range: impl Into<Rangef>) -> Self {
let x_range = x_range.into();
let y_range = y_range.into();
Rect {
min: pos2(*x_range.start(), *y_range.start()),
max: pos2(*x_range.end(), *y_range.end()),
min: pos2(x_range.min, y_range.min),
max: pos2(x_range.max, y_range.max),
}
}
@@ -390,18 +386,18 @@ impl Rect {
}
#[inline(always)]
pub fn x_range(&self) -> RangeInclusive<f32> {
self.min.x..=self.max.x
pub fn x_range(&self) -> Rangef {
Rangef::new(self.min.x, self.max.x)
}
#[inline(always)]
pub fn y_range(&self) -> RangeInclusive<f32> {
self.min.y..=self.max.y
pub fn y_range(&self) -> Rangef {
Rangef::new(self.min.y, self.max.y)
}
#[inline(always)]
pub fn bottom_up_range(&self) -> RangeInclusive<f32> {
self.max.y..=self.min.y
pub fn bottom_up_range(&self) -> Rangef {
Rangef::new(self.max.y, self.min.y)
}
/// `width < 0 || height < 0`