| 1 |
#[macro_use] |
| 2 |
mod macros; |
| 3 |
|
| 4 |
use glam::{ |
| 5 |
DMat2, DMat3, DMat4, DQuat, DVec2, DVec3, DVec4, Mat2, Mat3, Mat3A, Mat4, Quat, Vec2, Vec3, |
| 6 |
Vec3A, Vec4, |
| 7 |
}; |
| 8 |
|
| 9 |
pub trait Deg { |
| 10 |
fn to_radians(self) -> Self; |
| 11 |
} |
| 12 |
|
| 13 |
impl Deg for f32 { |
| 14 |
fn to_radians(self) -> f32 { |
| 15 |
f32::to_radians(self) |
| 16 |
} |
| 17 |
} |
| 18 |
|
| 19 |
impl Deg for f64 { |
| 20 |
fn to_radians(self) -> f64 { |
| 21 |
f64::to_radians(self) |
| 22 |
} |
| 23 |
} |
| 24 |
|
| 25 |
/// Helper function for migrating away from `glam::angle::deg`. |
| 26 |
#[allow(dead_code)] |
| 27 |
#[inline] |
| 28 |
pub fn deg<T: Deg>(angle: T) -> T { |
| 29 |
angle.to_radians() |
| 30 |
} |
| 31 |
|
| 32 |
/// Helper function for migrating away from `glam::angle::rad`. |
| 33 |
#[allow(dead_code)] |
| 34 |
#[inline] |
| 35 |
pub fn rad<T>(angle: T) -> T { |
| 36 |
angle |
| 37 |
} |
| 38 |
|
| 39 |
/// Trait used by the `assert_approx_eq` macro for floating point comparisons. |
| 40 |
pub trait FloatCompare<Rhs: ?Sized = Self> { |
| 41 |
/// Return true if the absolute difference between `self` and `other` is |
| 42 |
/// less then or equal to `max_abs_diff`. |
| 43 |
fn approx_eq(&self, other: &Rhs, max_abs_diff: f32) -> bool; |
| 44 |
/// Returns the absolute difference of `self` and `other` which is printed |
| 45 |
/// if `assert_approx_eq` fails. |
| 46 |
fn abs_diff(&self, other: &Rhs) -> Rhs; |
| 47 |
} |
| 48 |
|
| 49 |
impl FloatCompare for f32 { |
| 50 |
#[inline] |
| 51 |
fn approx_eq(&self, other: &f32, max_abs_diff: f32) -> bool { |
| 52 |
(self - other).abs() <= max_abs_diff |
| 53 |
} |
| 54 |
#[inline] |
| 55 |
fn abs_diff(&self, other: &f32) -> f32 { |
| 56 |
(self - other).abs() |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
impl FloatCompare for f64 { |
| 61 |
#[inline] |
| 62 |
fn approx_eq(&self, other: &f64, max_abs_diff: f32) -> bool { |
| 63 |
(self - other).abs() <= max_abs_diff as f64 |
| 64 |
} |
| 65 |
#[inline] |
| 66 |
fn abs_diff(&self, other: &f64) -> f64 { |
| 67 |
(self - other).abs() |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
impl FloatCompare for Mat2 { |
| 72 |
#[inline] |
| 73 |
fn approx_eq(&self, other: &Self, max_abs_diff: f32) -> bool { |
| 74 |
self.abs_diff_eq(other, max_abs_diff) |
| 75 |
} |
| 76 |
#[inline] |
| 77 |
fn abs_diff(&self, other: &Self) -> Self { |
| 78 |
Self::from_cols( |
| 79 |
(self.x_axis - other.x_axis).abs(), |
| 80 |
(self.y_axis - other.y_axis).abs(), |
| 81 |
) |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
impl FloatCompare for DMat2 { |
| 86 |
#[inline] |
| 87 |
fn approx_eq(&self, other: &Self, max_abs_diff: f32) -> bool { |
| 88 |
self.abs_diff_eq(other, max_abs_diff as f64) |
| 89 |
} |
| 90 |
#[inline] |
| 91 |
fn abs_diff(&self, other: &Self) -> Self { |
| 92 |
Self::from_cols( |
| 93 |
(self.x_axis - other.x_axis).abs(), |
| 94 |
(self.y_axis - other.y_axis).abs(), |
| 95 |
) |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
impl FloatCompare for Mat3 { |
| 100 |
#[inline] |
| 101 |
fn approx_eq(&self, other: &Self, max_abs_diff: f32) -> bool { |
| 102 |
self.abs_diff_eq(*other, max_abs_diff) |
| 103 |
} |
| 104 |
#[inline] |
| 105 |
fn abs_diff(&self, other: &Self) -> Self { |
| 106 |
Self::from_cols( |
| 107 |
(self.x_axis - other.x_axis).abs(), |
| 108 |
(self.y_axis - other.y_axis).abs(), |
| 109 |
(self.z_axis - other.z_axis).abs(), |
| 110 |
) |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
impl FloatCompare for Mat3A { |
| 115 |
#[inline] |
| 116 |
fn approx_eq(&self, other: &Self, max_abs_diff: f32) -> bool { |
| 117 |
self.abs_diff_eq(*other, max_abs_diff) |
| 118 |
} |
| 119 |
#[inline] |
| 120 |
fn abs_diff(&self, other: &Self) -> Self { |
| 121 |
Self::from_cols( |
| 122 |
(self.x_axis - other.x_axis).abs(), |
| 123 |
(self.y_axis - other.y_axis).abs(), |
| 124 |
(self.z_axis - other.z_axis).abs(), |
| 125 |
) |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
impl FloatCompare for DMat3 { |
| 130 |
#[inline] |
| 131 |
fn approx_eq(&self, other: &Self, max_abs_diff: f32) -> bool { |
| 132 |
self.abs_diff_eq(*other, max_abs_diff as f64) |
| 133 |
} |
| 134 |
#[inline] |
| 135 |
fn abs_diff(&self, other: &Self) -> Self { |
| 136 |
Self::from_cols( |
| 137 |
(self.x_axis - other.x_axis).abs(), |
| 138 |
(self.y_axis - other.y_axis).abs(), |
| 139 |
(self.z_axis - other.z_axis).abs(), |
| 140 |
) |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
impl FloatCompare for DMat4 { |
| 145 |
#[inline] |
| 146 |
fn approx_eq(&self, other: &Self, max_abs_diff: f32) -> bool { |
| 147 |
self.abs_diff_eq(*other, max_abs_diff as f64) |
| 148 |
} |
| 149 |
#[inline] |
| 150 |
fn abs_diff(&self, other: &Self) -> Self { |
| 151 |
Self::from_cols( |
| 152 |
(self.x_axis - other.x_axis).abs(), |
| 153 |
(self.y_axis - other.y_axis).abs(), |
| 154 |
(self.z_axis - other.z_axis).abs(), |
| 155 |
(self.w_axis - other.w_axis).abs(), |
| 156 |
) |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
impl FloatCompare for Mat4 { |
| 161 |
#[inline] |
| 162 |
fn approx_eq(&self, other: &Self, max_abs_diff: f32) -> bool { |
| 163 |
self.abs_diff_eq(*other, max_abs_diff) |
| 164 |
} |
| 165 |
#[inline] |
| 166 |
fn abs_diff(&self, other: &Self) -> Self { |
| 167 |
Self::from_cols( |
| 168 |
(self.x_axis - other.x_axis).abs(), |
| 169 |
(self.y_axis - other.y_axis).abs(), |
| 170 |
(self.z_axis - other.z_axis).abs(), |
| 171 |
(self.w_axis - other.w_axis).abs(), |
| 172 |
) |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
impl FloatCompare for Quat { |
| 177 |
#[inline] |
| 178 |
fn approx_eq(&self, other: &Self, max_abs_diff: f32) -> bool { |
| 179 |
self.abs_diff_eq(*other, max_abs_diff) |
| 180 |
} |
| 181 |
#[inline] |
| 182 |
fn abs_diff(&self, other: &Self) -> Self { |
| 183 |
let a: Vec4 = (*self).into(); |
| 184 |
let b: Vec4 = (*other).into(); |
| 185 |
Quat::from_vec4((a - b).abs()) |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
impl FloatCompare for Vec2 { |
| 190 |
#[inline] |
| 191 |
fn approx_eq(&self, other: &Self, max_abs_diff: f32) -> bool { |
| 192 |
self.abs_diff_eq(*other, max_abs_diff) |
| 193 |
} |
| 194 |
#[inline] |
| 195 |
fn abs_diff(&self, other: &Self) -> Self { |
| 196 |
(*self - *other).abs() |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
impl FloatCompare for Vec3 { |
| 201 |
#[inline] |
| 202 |
fn approx_eq(&self, other: &Self, max_abs_diff: f32) -> bool { |
| 203 |
self.abs_diff_eq(*other, max_abs_diff) |
| 204 |
} |
| 205 |
#[inline] |
| 206 |
fn abs_diff(&self, other: &Self) -> Self { |
| 207 |
(*self - *other).abs() |
| 208 |
} |
| 209 |
} |
| 210 |
|
| 211 |
impl FloatCompare for Vec3A { |
| 212 |
#[inline] |
| 213 |
fn approx_eq(&self, other: &Self, max_abs_diff: f32) -> bool { |
| 214 |
self.abs_diff_eq(*other, max_abs_diff) |
| 215 |
} |
| 216 |
#[inline] |
| 217 |
fn abs_diff(&self, other: &Self) -> Self { |
| 218 |
(*self - *other).abs() |
| 219 |
} |
| 220 |
} |
| 221 |
|
| 222 |
impl FloatCompare for Vec4 { |
| 223 |
#[inline] |
| 224 |
fn approx_eq(&self, other: &Self, max_abs_diff: f32) -> bool { |
| 225 |
self.abs_diff_eq(*other, max_abs_diff) |
| 226 |
} |
| 227 |
#[inline] |
| 228 |
fn abs_diff(&self, other: &Self) -> Self { |
| 229 |
(*self - *other).abs() |
| 230 |
} |
| 231 |
} |
| 232 |
|
| 233 |
impl FloatCompare for DQuat { |
| 234 |
#[inline] |
| 235 |
fn approx_eq(&self, other: &Self, max_abs_diff: f32) -> bool { |
| 236 |
self.abs_diff_eq(*other, max_abs_diff as f64) |
| 237 |
} |
| 238 |
#[inline] |
| 239 |
fn abs_diff(&self, other: &Self) -> Self { |
| 240 |
let a: DVec4 = (*self).into(); |
| 241 |
let b: DVec4 = (*other).into(); |
| 242 |
DQuat::from_vec4((a - b).abs()) |
| 243 |
} |
| 244 |
} |
| 245 |
|
| 246 |
impl FloatCompare for DVec2 { |
| 247 |
#[inline] |
| 248 |
fn approx_eq(&self, other: &Self, max_abs_diff: f32) -> bool { |
| 249 |
self.abs_diff_eq(*other, max_abs_diff as f64) |
| 250 |
} |
| 251 |
#[inline] |
| 252 |
fn abs_diff(&self, other: &Self) -> Self { |
| 253 |
(*self - *other).abs() |
| 254 |
} |
| 255 |
} |
| 256 |
|
| 257 |
impl FloatCompare for DVec3 { |
| 258 |
#[inline] |
| 259 |
fn approx_eq(&self, other: &Self, max_abs_diff: f32) -> bool { |
| 260 |
self.abs_diff_eq(*other, max_abs_diff as f64) |
| 261 |
} |
| 262 |
#[inline] |
| 263 |
fn abs_diff(&self, other: &Self) -> Self { |
| 264 |
(*self - *other).abs() |
| 265 |
} |
| 266 |
} |
| 267 |
|
| 268 |
impl FloatCompare for DVec4 { |
| 269 |
#[inline] |
| 270 |
fn approx_eq(&self, other: &Self, max_abs_diff: f32) -> bool { |
| 271 |
self.abs_diff_eq(*other, max_abs_diff as f64) |
| 272 |
} |
| 273 |
#[inline] |
| 274 |
fn abs_diff(&self, other: &Self) -> Self { |
| 275 |
(*self - *other).abs() |
| 276 |
} |
| 277 |
} |