| 1 |
|
| 2 |
use std::ops::{Add, Mul, Sub}; |
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
pub(crate) fn normalize01_64(start: f64, end: f64, value: f64) -> f64 { |
| 12 |
if start == end { |
| 13 |
if value > start { 1.0 } else { 0.0 } |
| 14 |
} else { |
| 15 |
((value - start) / (end - start)).clamp(0.0, 1.0) |
| 16 |
} |
| 17 |
} |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
pub(crate) fn interp<T>(start: T, end: T, interp_amount: f64) -> T |
| 23 |
where |
| 24 |
T: Sub<Output = T>, |
| 25 |
T: Add<Output = T>, |
| 26 |
T: Copy, |
| 27 |
T: Mul<f64, Output = T>, |
| 28 |
{ |
| 29 |
start + (end - start) * interp_amount.clamp(0.0, 1.0) |
| 30 |
} |
| 31 |
|
| 32 |
|
| 33 |
pub(crate) fn interp2(start: (f64, f64), end: (f64, f64), interp_amount: f64) -> (f64, f64) { |
| 34 |
( |
| 35 |
start.0 + interp_amount.clamp(0.0, 1.0) * (end.0 - start.0), |
| 36 |
start.1 + interp_amount.clamp(0.0, 1.0) * (end.1 - start.1), |
| 37 |
) |
| 38 |
} |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
pub(crate) fn nearest_point_on_segment( |
| 44 |
start: (f64, f64), |
| 45 |
end: (f64, f64), |
| 46 |
point: (f64, f64), |
| 47 |
) -> f64 { |
| 48 |
if start == end { |
| 49 |
0.0 |
| 50 |
} else { |
| 51 |
let seg_vector = (end.0 - start.0, end.1 - start.1); |
| 52 |
let proj_vector = (point.0 - start.0, point.1 - start.1); |
| 53 |
|
| 54 |
(dot(proj_vector, seg_vector) / dot(seg_vector, seg_vector)).clamp(0.0, 1.0) |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
pub(crate) fn dot(x: (f64, f64), y: (f64, f64)) -> f64 { |
| 60 |
x.0 * y.0 + x.1 * y.1 |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
pub fn dist(start: (f64, f64), end: (f64, f64)) -> f64 { |
| 65 |
((start.0 - end.0).powi(2) + (start.1 - end.1).powi(2)).sqrt() |
| 66 |
} |
| 67 |
|
| 68 |
#[cfg(test)] |
| 69 |
mod test_utils { |
| 70 |
use crate::utils::{interp, interp2, nearest_point_on_segment, normalize01_64}; |
| 71 |
|
| 72 |
#[test] |
| 73 |
fn test_normalize_float() { |
| 74 |
approx::assert_relative_eq!(normalize01_64(1., 2., 1.5), 0.5); |
| 75 |
approx::assert_relative_eq!(normalize01_64(7., 3., 4.), 0.75); |
| 76 |
approx::assert_relative_eq!(normalize01_64(-1., 1., 2.), 1.); |
| 77 |
approx::assert_relative_eq!(normalize01_64(1., 1., 1.), 0.); |
| 78 |
approx::assert_relative_eq!(normalize01_64(1., 1., 0.), 0.); |
| 79 |
approx::assert_relative_eq!(normalize01_64(1., 1., 2.), 1.); |
| 80 |
} |
| 81 |
|
| 82 |
#[test] |
| 83 |
fn test_inter_float() { |
| 84 |
approx::assert_relative_eq!(interp(5.0, 10.0, 0.2), 6.0); |
| 85 |
approx::assert_relative_eq!(interp(10.0, -2.0, 0.75), 1.0); |
| 86 |
approx::assert_relative_eq!(interp(-1.0, 2.0, -3.0), -1.0); |
| 87 |
approx::assert_relative_eq!(interp(5.0, 7.0, 20.0), 7.0); |
| 88 |
} |
| 89 |
|
| 90 |
#[test] |
| 91 |
fn test_interp_vec2() { |
| 92 |
assert_eq!(interp2((1.0, 2.0), (3.0, 5.0), 0.5), (2.0, 3.5)); |
| 93 |
assert_eq!(interp2((-5.0, 5.0), (-15.0, 0.0), 0.4), (-9.0, 3.0)); |
| 94 |
assert_eq!(interp2((7.0, 9.0), (25.0, 30.0), -0.1), (7.0, 9.0)); |
| 95 |
assert_eq!(interp2((12.0, 5.0), (13.0, 14.0), 3.2), (13.0, 14.0)); |
| 96 |
} |
| 97 |
|
| 98 |
#[test] |
| 99 |
fn test_nearest_point() { |
| 100 |
assert_eq!( |
| 101 |
nearest_point_on_segment((0.0, 0.0), (1.0, 0.0), (0.25, 0.5),), |
| 102 |
0.25 |
| 103 |
); |
| 104 |
assert_eq!( |
| 105 |
nearest_point_on_segment((3.0, 4.0), (5.0, 6.0), (-1.0, -1.0),), |
| 106 |
0.0 |
| 107 |
); |
| 108 |
assert_eq!( |
| 109 |
nearest_point_on_segment((20.0, 10.0), (10.0, 5.0), (2.0, 2.0),), |
| 110 |
1.0 |
| 111 |
); |
| 112 |
assert_eq!( |
| 113 |
nearest_point_on_segment((0.0, 5.0), (5.0, 0.0), (3.0, 3.0),), |
| 114 |
0.5 |
| 115 |
); |
| 116 |
|
| 117 |
|
| 118 |
assert_eq!( |
| 119 |
nearest_point_on_segment((0.0, 0.0), (0.0, 0.0), (5.0, 10.0),), |
| 120 |
0.0 |
| 121 |
); |
| 122 |
assert_eq!( |
| 123 |
nearest_point_on_segment((3.0, 7.0), (3.0, 7.0), (0.0, -20.0),), |
| 124 |
0.0 |
| 125 |
); |
| 126 |
} |
| 127 |
} |