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