1#![cfg_attr(feature = "document-features", doc = document_features::document_features!())]
20#![allow(clippy::float_cmp)]
23
24use std::ops::{Add, Div, Mul, RangeInclusive, Sub};
25
26pub mod align;
29pub mod easing;
30mod gui_rounding;
31mod history;
32mod numeric;
33mod ordered_float;
34mod pos2;
35mod range;
36mod rect;
37mod rect_align;
38mod rect_transform;
39mod rot2;
40pub mod smart_aim;
41mod ts_transform;
42mod vec2;
43mod vec2b;
44
45pub use self::{
46 align::{Align, Align2},
47 gui_rounding::{GUI_ROUNDING, GuiRounding},
48 history::History,
49 numeric::*,
50 ordered_float::*,
51 pos2::*,
52 range::Rangef,
53 rect::*,
54 rect_align::RectAlign,
55 rect_transform::*,
56 rot2::*,
57 ts_transform::*,
58 vec2::*,
59 vec2b::*,
60};
61
62pub trait One {
66 const ONE: Self;
67}
68
69impl One for f32 {
70 const ONE: Self = 1.0;
71}
72
73impl One for f64 {
74 const ONE: Self = 1.0;
75}
76
77pub trait Real:
79 Copy
80 + PartialEq
81 + PartialOrd
82 + One
83 + Add<Self, Output = Self>
84 + Sub<Self, Output = Self>
85 + Mul<Self, Output = Self>
86 + Div<Self, Output = Self>
87{
88}
89
90impl Real for f32 {}
91
92impl Real for f64 {}
93
94#[inline(always)]
106pub fn lerp<R, T>(range: impl Into<RangeInclusive<R>>, t: T) -> R
107where
108 T: Real + Mul<R, Output = R>,
109 R: Copy + Add<R, Output = R>,
110{
111 let range = range.into();
112 (T::ONE - t) * *range.start() + t * *range.end()
113}
114
115#[inline(always)]
122pub fn fast_midpoint<R>(a: R, b: R) -> R
123where
124 R: Copy + Add<R, Output = R> + Div<R, Output = R> + One,
125{
126 let two = R::ONE + R::ONE;
127 (a + b) / two
128}
129
130#[inline]
145pub fn inverse_lerp<R>(range: RangeInclusive<R>, value: R) -> Option<R>
146where
147 R: Copy + PartialEq + Sub<R, Output = R> + Div<R, Output = R>,
148{
149 let min = *range.start();
150 let max = *range.end();
151 if min == max {
152 None
153 } else {
154 Some((value - min) / (max - min))
155 }
156}
157
158pub fn remap<T>(x: T, from: impl Into<RangeInclusive<T>>, to: impl Into<RangeInclusive<T>>) -> T
162where
163 T: Real,
164{
165 let from = from.into();
166 let to = to.into();
167 debug_assert!(
168 from.start() != from.end(),
169 "from.start() and from.end() should not be equal"
170 );
171 let t = (x - *from.start()) / (*from.end() - *from.start());
172 lerp(to, t)
173}
174
175pub fn remap_clamp<T>(
177 x: T,
178 from: impl Into<RangeInclusive<T>>,
179 to: impl Into<RangeInclusive<T>>,
180) -> T
181where
182 T: Real,
183{
184 let from = from.into();
185 let to = to.into();
186 if from.end() < from.start() {
187 return remap_clamp(x, *from.end()..=*from.start(), *to.end()..=*to.start());
188 }
189 if x <= *from.start() {
190 *to.start()
191 } else if *from.end() <= x {
192 *to.end()
193 } else {
194 debug_assert!(
195 from.start() != from.end(),
196 "from.start() and from.end() should not be equal"
197 );
198 let t = (x - *from.start()) / (*from.end() - *from.start());
199 if T::ONE <= t { *to.end() } else { lerp(to, t) }
201 }
202}
203
204pub fn round_to_decimals(value: f64, decimal_places: usize) -> f64 {
206 format!("{value:.decimal_places$}").parse().unwrap_or(value)
208}
209
210pub fn format_with_minimum_decimals(value: f64, decimals: usize) -> String {
211 format_with_decimals_in_range(value, decimals..=6)
212}
213
214pub fn format_with_decimals_in_range(value: f64, decimal_range: RangeInclusive<usize>) -> String {
218 let min_decimals = *decimal_range.start();
219 let max_decimals = *decimal_range.end();
220 debug_assert!(
221 min_decimals <= max_decimals,
222 "min_decimals should be <= max_decimals, but got min_decimals: {min_decimals}, max_decimals: {max_decimals}"
223 );
224 debug_assert!(
225 max_decimals < 100,
226 "max_decimals should be < 100, but got {max_decimals}"
227 );
228 let max_decimals = max_decimals.min(16);
229 let min_decimals = min_decimals.min(max_decimals);
230
231 if min_decimals < max_decimals {
232 for decimals in min_decimals..max_decimals {
234 let text = format!("{value:.decimals$}");
235 let epsilon = 16.0 * f32::EPSILON; if almost_equal(text.parse::<f32>().unwrap(), value as f32, epsilon) {
237 return text;
239 }
240 }
241 }
245 format!("{value:.max_decimals$}")
246}
247
248pub fn almost_equal(a: f32, b: f32, epsilon: f32) -> bool {
254 if a == b {
255 true } else {
257 let abs_max = a.abs().max(b.abs());
258 abs_max <= epsilon || ((a - b).abs() / abs_max) <= epsilon
259 }
260}
261
262#[expect(clippy::approx_constant)]
263#[test]
264fn test_format() {
265 assert_eq!(format_with_minimum_decimals(1_234_567.0, 0), "1234567");
266 assert_eq!(format_with_minimum_decimals(1_234_567.0, 1), "1234567.0");
267 assert_eq!(format_with_minimum_decimals(3.14, 2), "3.14");
268 assert_eq!(format_with_minimum_decimals(3.14, 3), "3.140");
269 assert_eq!(
270 format_with_minimum_decimals(std::f64::consts::PI, 2),
271 "3.14159"
272 );
273}
274
275#[test]
276fn test_almost_equal() {
277 for &x in &[
278 0.0_f32,
279 f32::MIN_POSITIVE,
280 1e-20,
281 1e-10,
282 f32::EPSILON,
283 0.1,
284 0.99,
285 1.0,
286 1.001,
287 1e10,
288 f32::MAX / 100.0,
289 f32::INFINITY,
291 ] {
292 for &x in &[-x, x] {
293 for roundtrip in &[
294 |x: f32| x.to_degrees().to_radians(),
295 |x: f32| x.to_radians().to_degrees(),
296 ] {
297 let epsilon = f32::EPSILON;
298 assert!(
299 almost_equal(x, roundtrip(x), epsilon),
300 "{} vs {}",
301 x,
302 roundtrip(x)
303 );
304 }
305 }
306 }
307}
308
309#[test]
310fn test_remap() {
311 assert_eq!(remap_clamp(1.0, 0.0..=1.0, 0.0..=16.0), 16.0);
312 assert_eq!(remap_clamp(1.0, 1.0..=0.0, 16.0..=0.0), 16.0);
313 assert_eq!(remap_clamp(0.5, 1.0..=0.0, 16.0..=0.0), 8.0);
314}
315
316pub trait NumExt {
320 #[must_use]
322 fn at_least(self, lower_limit: Self) -> Self;
323
324 #[must_use]
326 fn at_most(self, upper_limit: Self) -> Self;
327}
328
329macro_rules! impl_num_ext {
330 ($t: ty) => {
331 impl NumExt for $t {
332 #[inline(always)]
333 fn at_least(self, lower_limit: Self) -> Self {
334 self.max(lower_limit)
335 }
336
337 #[inline(always)]
338 fn at_most(self, upper_limit: Self) -> Self {
339 self.min(upper_limit)
340 }
341 }
342 };
343}
344
345impl_num_ext!(u8);
346impl_num_ext!(u16);
347impl_num_ext!(u32);
348impl_num_ext!(u64);
349impl_num_ext!(u128);
350impl_num_ext!(usize);
351impl_num_ext!(i8);
352impl_num_ext!(i16);
353impl_num_ext!(i32);
354impl_num_ext!(i64);
355impl_num_ext!(i128);
356impl_num_ext!(isize);
357impl_num_ext!(f32);
358impl_num_ext!(f64);
359impl_num_ext!(Vec2);
360impl_num_ext!(Pos2);
361
362pub fn normalized_angle(mut angle: f32) -> f32 {
366 use std::f32::consts::{PI, TAU};
367 angle %= TAU;
368 if angle > PI {
369 angle -= TAU;
370 } else if angle < -PI {
371 angle += TAU;
372 }
373 angle
374}
375
376#[test]
377fn test_normalized_angle() {
378 macro_rules! almost_eq {
379 ($left: expr, $right: expr) => {
380 let left = $left;
381 let right = $right;
382 assert!((left - right).abs() < 1e-6, "{} != {}", left, right);
383 };
384 }
385
386 use std::f32::consts::TAU;
387 almost_eq!(normalized_angle(-3.0 * TAU), 0.0);
388 almost_eq!(normalized_angle(-2.3 * TAU), -0.3 * TAU);
389 almost_eq!(normalized_angle(-TAU), 0.0);
390 almost_eq!(normalized_angle(0.0), 0.0);
391 almost_eq!(normalized_angle(TAU), 0.0);
392 almost_eq!(normalized_angle(2.7 * TAU), -0.3 * TAU);
393}
394
395pub fn exponential_smooth_factor(
410 reach_this_fraction: f32,
411 in_this_many_seconds: f32,
412 dt: f32,
413) -> f32 {
414 1.0 - (1.0 - reach_this_fraction).powf(dt / in_this_many_seconds)
415}
416
417pub fn interpolation_factor(
438 (start_time, end_time): (f64, f64),
439 current_time: f64,
440 dt: f32,
441 easing: impl Fn(f32) -> f32,
442) -> f32 {
443 let animation_duration = (end_time - start_time) as f32;
444 let prev_time = current_time - dt as f64;
445 let prev_t = easing((prev_time - start_time) as f32 / animation_duration);
446 let end_t = easing((current_time - start_time) as f32 / animation_duration);
447 if end_t < 1.0 {
448 (end_t - prev_t) / (1.0 - prev_t)
449 } else {
450 1.0
451 }
452}
453
454#[inline]
458pub fn ease_in_ease_out(t: f32) -> f32 {
459 let t = t.clamp(0.0, 1.0);
460 (3.0 * t * t - 2.0 * t * t * t).clamp(0.0, 1.0)
461}