glam/float.rs
1/// A trait for extending [`prim@f32`] and [`prim@f64`] with extra methods.
2pub trait FloatExt {
3 /// Performs a linear interpolation between `self` and `rhs` based on the value `s`, using the
4 /// form `self * (1.0 - s) + rhs * s`.
5 ///
6 /// When `s` is `0.0`, the result will be equal to `self`. When `s` is `1.0`, the result will
7 /// be equal to `rhs`. When `s` is outside of the range `[0, 1]`, the result is linearly
8 /// extrapolated.
9 ///
10 /// The result is guaranteed to be `self` at `s == 0.0` and `rhs` at `s == 1.0`, even when the
11 /// values differ greatly in magnitude, but it is not monotonic in `s` for nearly equal inputs
12 /// and may not preserve equal inputs exactly. The float vector types provide a monotonic
13 /// `lerp_monotonic` method for interpolating between values that may be equal or nearly equal.
14 #[must_use]
15 fn lerp(self, rhs: Self, s: Self) -> Self;
16
17 /// Returns `v` normalized to the range `[a, b]`.
18 ///
19 /// When `v` is equal to `a` the result will be `0`. When `v` is equal to `b` will be `1`.
20 ///
21 /// When `v` is outside of the range `[a, b]`, the result is linearly extrapolated.
22 ///
23 /// `a` and `b` must not be equal, otherwise the result will be either infinite or `NAN`.
24 fn inverse_lerp(a: Self, b: Self, v: Self) -> Self;
25
26 /// Performs Hermite interpolation between `0.0` and `1.0` using `x` normalized to `[edge0, edge1]`.
27 ///
28 /// This is equivalent to `t * t * (3.0 - 2.0 * t)`, where `t` is clamped to `[0.0, 1.0]`.
29 /// Results are undefined if `edge0` is greater than or equal to `edge1`.
30 #[must_use]
31 fn smoothstep(self, edge0: Self, edge1: Self) -> Self;
32
33 /// Remap `self` from the input range to the output range.
34 ///
35 /// When `self` is equal to `in_start` this returns `out_start`.
36 /// When `self` is equal to `in_end` this returns `out_end`.
37 ///
38 /// When `self` is outside of the range `[in_start, in_end]`, the result is linearly extrapolated.
39 ///
40 /// `in_start` and `in_end` must not be equal, otherwise the result will be either infinite or `NAN`.
41 #[must_use]
42 fn remap(self, in_start: Self, in_end: Self, out_start: Self, out_end: Self) -> Self;
43
44 /// Returns the fractional part of the input as `self - self.floor()`.
45 ///
46 /// Note that this differs from the Rust implementation of `fract` which returns
47 /// `self - self.trunc()`.
48 ///
49 /// Note that this is fast but not precise for large numbers.
50 fn fract_gl(self) -> Self;
51
52 /// Returns `0.0` if `value < self` and 1.0 otherwise.
53 ///
54 /// Similar to glsl's step(edge, x), which translates into edge.step(x)
55 #[must_use]
56 fn step(self, value: Self) -> Self;
57
58 /// Returns `self` clamped within the range `[0.0, 1.0]`
59 #[must_use]
60 fn saturate(self) -> Self;
61
62 /// Moves `self` towards `rhs` by at most the distance `d`.
63 ///
64 /// When `d` is `0.0`, the result will be equal to `self`. When `d` is greater than or
65 /// equal to the distance between `self` and `rhs`, the result will be equal to `rhs`.
66 /// Will not go past `rhs`.
67 #[must_use]
68 fn move_towards(self, rhs: Self, d: Self) -> Self;
69}