| 1 | use crate::core::{ |
| 2 | storage::{XY, XYZ, XYZW}, |
| 3 | traits::{ |
| 4 | quaternion::Quaternion, |
| 5 | scalar::{FloatEx, NumEx}, |
| 6 | vector::*, |
| 7 | }, |
| 8 | }; |
| 9 | |
| 10 | pub trait MatrixConst { |
| 11 | const ZERO: Self; |
| 12 | const IDENTITY: Self; |
| 13 | } |
| 14 | |
| 15 | /// Base matrix trait that sets up trait bounds |
| 16 | pub trait Matrix<T: NumEx>: Sized + Copy + Clone {} |
| 17 | |
| 18 | /// 2x2 Matrix trait for all types of T |
| 19 | pub trait Matrix2x2<T: NumEx, V2: Vector2<T>>: Matrix<T> { |
| 20 | #[inline(always)] |
| 21 | fn new(m00: T, m01: T, m10: T, m11: T) -> Self { |
| 22 | Self::from_cols(V2::new(m00, m01), V2::new(m10, m11)) |
| 23 | } |
| 24 | |
| 25 | fn from_cols(x_axis: V2, y_axis: V2) -> Self; |
| 26 | |
| 27 | fn x_axis(&self) -> &V2; |
| 28 | fn y_axis(&self) -> &V2; |
| 29 | |
| 30 | #[inline(always)] |
| 31 | fn from_cols_array(m: &[T; 4]) -> Self { |
| 32 | Self::new(m[0], m[1], m[2], m[3]) |
| 33 | } |
| 34 | |
| 35 | #[rustfmt::skip] |
| 36 | #[inline(always)] |
| 37 | fn to_cols_array(&self) -> [T; 4] { |
| 38 | let x_axis = self.x_axis(); |
| 39 | let y_axis = self.y_axis(); |
| 40 | [x_axis.x(), x_axis.y(), |
| 41 | y_axis.x(), y_axis.y()] |
| 42 | } |
| 43 | |
| 44 | #[inline(always)] |
| 45 | fn from_cols_array_2d(m: &[[T; 2]; 2]) -> Self { |
| 46 | Self::from_cols(V2::from_array(m[0]), V2::from_array(m[1])) |
| 47 | } |
| 48 | |
| 49 | #[inline(always)] |
| 50 | fn to_cols_array_2d(&self) -> [[T; 2]; 2] { |
| 51 | [self.x_axis().into_array(), self.y_axis().into_array()] |
| 52 | } |
| 53 | |
| 54 | #[inline(always)] |
| 55 | fn from_cols_slice(m: &[T]) -> Self { |
| 56 | Self::new(m[0], m[1], m[2], m[3]) |
| 57 | } |
| 58 | |
| 59 | #[inline(always)] |
| 60 | fn write_cols_to_slice(&self, slice: &mut [T]) { |
| 61 | let x_axis = self.x_axis(); |
| 62 | let y_axis = self.y_axis(); |
| 63 | slice[0] = x_axis.x(); |
| 64 | slice[1] = x_axis.y(); |
| 65 | slice[2] = y_axis.x(); |
| 66 | slice[3] = y_axis.y(); |
| 67 | } |
| 68 | |
| 69 | #[rustfmt::skip] |
| 70 | #[inline(always)] |
| 71 | fn from_diagonal(diagonal: XY<T>) -> Self { |
| 72 | Self::new( |
| 73 | diagonal.x, T::ZERO, |
| 74 | T::ZERO, diagonal.y) |
| 75 | } |
| 76 | |
| 77 | #[inline] |
| 78 | fn determinant(&self) -> T { |
| 79 | let x_axis = self.x_axis(); |
| 80 | let y_axis = self.y_axis(); |
| 81 | x_axis.x() * y_axis.y() - x_axis.y() * y_axis.x() |
| 82 | } |
| 83 | |
| 84 | #[inline(always)] |
| 85 | fn transpose(&self) -> Self { |
| 86 | let x_axis = self.x_axis(); |
| 87 | let y_axis = self.y_axis(); |
| 88 | Self::new(x_axis.x(), y_axis.x(), x_axis.y(), y_axis.y()) |
| 89 | } |
| 90 | |
| 91 | #[inline] |
| 92 | fn mul_vector(&self, other: V2) -> V2 { |
| 93 | let x_axis = self.x_axis(); |
| 94 | let y_axis = self.y_axis(); |
| 95 | #[allow(clippy::suspicious_operation_groupings)] |
| 96 | V2::new( |
| 97 | (x_axis.x() * other.x()) + (y_axis.x() * other.y()), |
| 98 | (x_axis.y() * other.x()) + (y_axis.y() * other.y()), |
| 99 | ) |
| 100 | } |
| 101 | |
| 102 | #[inline] |
| 103 | fn mul_matrix(&self, other: &Self) -> Self { |
| 104 | Self::from_cols( |
| 105 | self.mul_vector(*other.x_axis()), |
| 106 | self.mul_vector(*other.y_axis()), |
| 107 | ) |
| 108 | } |
| 109 | |
| 110 | #[inline] |
| 111 | fn mul_scalar(&self, other: T) -> Self { |
| 112 | Self::from_cols( |
| 113 | self.x_axis().mul_scalar(other), |
| 114 | self.y_axis().mul_scalar(other), |
| 115 | ) |
| 116 | } |
| 117 | |
| 118 | #[inline] |
| 119 | fn add_matrix(&self, other: &Self) -> Self { |
| 120 | Self::from_cols( |
| 121 | self.x_axis().add(*other.x_axis()), |
| 122 | self.y_axis().add(*other.y_axis()), |
| 123 | ) |
| 124 | } |
| 125 | |
| 126 | #[inline] |
| 127 | fn sub_matrix(&self, other: &Self) -> Self { |
| 128 | Self::from_cols( |
| 129 | self.x_axis().sub(*other.x_axis()), |
| 130 | self.y_axis().sub(*other.y_axis()), |
| 131 | ) |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | /// 2x2 matrix trait for float types of T |
| 136 | pub trait FloatMatrix2x2<T: FloatEx, V2: FloatVector2<T>>: Matrix2x2<T, V2> { |
| 137 | #[inline] |
| 138 | fn abs_diff_eq(&self, other: &Self, max_abs_diff: T) -> bool |
| 139 | where |
| 140 | <V2 as Vector<T>>::Mask: MaskVector2, |
| 141 | { |
| 142 | self.x_axis().abs_diff_eq(*other.x_axis(), max_abs_diff) |
| 143 | && self.y_axis().abs_diff_eq(*other.y_axis(), max_abs_diff) |
| 144 | } |
| 145 | |
| 146 | #[inline] |
| 147 | fn from_scale_angle(scale: V2, angle: T) -> Self { |
| 148 | let (sin, cos) = angle.sin_cos(); |
| 149 | let (scale_x, scale_y) = scale.into_tuple(); |
| 150 | Self::new(cos * scale_x, sin * scale_x, -sin * scale_y, cos * scale_y) |
| 151 | } |
| 152 | |
| 153 | #[inline] |
| 154 | fn from_angle(angle: T) -> Self { |
| 155 | let (sin, cos) = angle.sin_cos(); |
| 156 | Self::new(cos, sin, -sin, cos) |
| 157 | } |
| 158 | |
| 159 | #[inline] |
| 160 | fn inverse(&self) -> Self { |
| 161 | let inv_det = { |
| 162 | let det = self.determinant(); |
| 163 | glam_assert!(det != T::ZERO); |
| 164 | det.recip() |
| 165 | }; |
| 166 | let x_axis = self.x_axis(); |
| 167 | let y_axis = self.y_axis(); |
| 168 | Self::new( |
| 169 | y_axis.y() * inv_det, |
| 170 | x_axis.y() * -inv_det, |
| 171 | y_axis.x() * -inv_det, |
| 172 | x_axis.x() * inv_det, |
| 173 | ) |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | pub trait Matrix3x3<T: NumEx, V3: Vector3<T>>: Matrix<T> { |
| 178 | fn from_cols(x_axis: V3, y_axis: V3, z_axis: V3) -> Self; |
| 179 | |
| 180 | fn x_axis(&self) -> &V3; |
| 181 | fn y_axis(&self) -> &V3; |
| 182 | fn z_axis(&self) -> &V3; |
| 183 | |
| 184 | #[rustfmt::skip] |
| 185 | #[inline(always)] |
| 186 | fn from_cols_array(m: &[T; 9]) -> Self { |
| 187 | Self::from_cols( |
| 188 | V3::new(m[0], m[1], m[2]), |
| 189 | V3::new(m[3], m[4], m[5]), |
| 190 | V3::new(m[6], m[7], m[8])) |
| 191 | } |
| 192 | |
| 193 | #[rustfmt::skip] |
| 194 | #[inline(always)] |
| 195 | fn to_cols_array(&self) -> [T; 9] { |
| 196 | let x_axis = self.x_axis(); |
| 197 | let y_axis = self.y_axis(); |
| 198 | let z_axis = self.z_axis(); |
| 199 | [ |
| 200 | x_axis.x(), x_axis.y(), x_axis.z(), |
| 201 | y_axis.x(), y_axis.y(), y_axis.z(), |
| 202 | z_axis.x(), z_axis.y(), z_axis.z(), |
| 203 | ] |
| 204 | } |
| 205 | |
| 206 | #[inline(always)] |
| 207 | fn from_cols_array_2d(m: &[[T; 3]; 3]) -> Self { |
| 208 | Self::from_cols( |
| 209 | V3::from_array(m[0]), |
| 210 | V3::from_array(m[1]), |
| 211 | V3::from_array(m[2]), |
| 212 | ) |
| 213 | } |
| 214 | |
| 215 | #[inline(always)] |
| 216 | fn to_cols_array_2d(&self) -> [[T; 3]; 3] { |
| 217 | [ |
| 218 | self.x_axis().into_array(), |
| 219 | self.y_axis().into_array(), |
| 220 | self.z_axis().into_array(), |
| 221 | ] |
| 222 | } |
| 223 | |
| 224 | #[inline(always)] |
| 225 | fn from_cols_slice(m: &[T]) -> Self { |
| 226 | Self::from_cols( |
| 227 | V3::new(m[0], m[1], m[2]), |
| 228 | V3::new(m[3], m[4], m[5]), |
| 229 | V3::new(m[6], m[7], m[8]), |
| 230 | ) |
| 231 | } |
| 232 | |
| 233 | #[inline(always)] |
| 234 | fn write_cols_to_slice(&self, slice: &mut [T]) { |
| 235 | let x_axis = self.x_axis(); |
| 236 | let y_axis = self.y_axis(); |
| 237 | let z_axis = self.z_axis(); |
| 238 | slice[0] = x_axis.x(); |
| 239 | slice[1] = x_axis.y(); |
| 240 | slice[2] = x_axis.z(); |
| 241 | slice[3] = y_axis.x(); |
| 242 | slice[4] = y_axis.y(); |
| 243 | slice[5] = y_axis.z(); |
| 244 | slice[6] = z_axis.x(); |
| 245 | slice[7] = z_axis.y(); |
| 246 | slice[8] = z_axis.z(); |
| 247 | } |
| 248 | |
| 249 | #[rustfmt::skip] |
| 250 | #[inline(always)] |
| 251 | fn from_diagonal(diagonal: XYZ<T>) -> Self { |
| 252 | Self::from_cols( |
| 253 | V3::new(diagonal.x, T::ZERO, T::ZERO), |
| 254 | V3::new(T::ZERO, diagonal.y, T::ZERO), |
| 255 | V3::new(T::ZERO, T::ZERO, diagonal.z), |
| 256 | ) |
| 257 | } |
| 258 | |
| 259 | #[inline(always)] |
| 260 | fn from_scale(scale: XY<T>) -> Self { |
| 261 | // Do not panic as long as any component is non-zero |
| 262 | glam_assert!(scale.cmpne(XY::<T>::ZERO).any()); |
| 263 | Self::from_cols( |
| 264 | V3::new(scale.x, T::ZERO, T::ZERO), |
| 265 | V3::new(T::ZERO, scale.y, T::ZERO), |
| 266 | V3::Z, |
| 267 | ) |
| 268 | } |
| 269 | |
| 270 | #[inline(always)] |
| 271 | fn from_translation(translation: XY<T>) -> Self { |
| 272 | Self::from_cols(V3::X, V3::Y, V3::new(translation.x, translation.y, T::ONE)) |
| 273 | } |
| 274 | |
| 275 | #[inline] |
| 276 | fn determinant(&self) -> T { |
| 277 | self.z_axis().dot(self.x_axis().cross(*self.y_axis())) |
| 278 | } |
| 279 | |
| 280 | #[inline] |
| 281 | fn transpose(&self) -> Self { |
| 282 | let x_axis = self.x_axis(); |
| 283 | let y_axis = self.y_axis(); |
| 284 | let z_axis = self.z_axis(); |
| 285 | Self::from_cols( |
| 286 | V3::new(x_axis.x(), y_axis.x(), z_axis.x()), |
| 287 | V3::new(x_axis.y(), y_axis.y(), z_axis.y()), |
| 288 | V3::new(x_axis.z(), y_axis.z(), z_axis.z()), |
| 289 | ) |
| 290 | } |
| 291 | |
| 292 | #[inline] |
| 293 | fn mul_vector(&self, other: V3) -> V3 { |
| 294 | let mut res = self.x_axis().mul(other.splat_x()); |
| 295 | res = self.y_axis().mul_add(other.splat_y(), res); |
| 296 | res = self.z_axis().mul_add(other.splat_z(), res); |
| 297 | res |
| 298 | } |
| 299 | |
| 300 | #[inline] |
| 301 | fn mul_matrix(&self, other: &Self) -> Self { |
| 302 | Self::from_cols( |
| 303 | self.mul_vector(*other.x_axis()), |
| 304 | self.mul_vector(*other.y_axis()), |
| 305 | self.mul_vector(*other.z_axis()), |
| 306 | ) |
| 307 | } |
| 308 | |
| 309 | #[inline] |
| 310 | fn mul_scalar(&self, other: T) -> Self { |
| 311 | Self::from_cols( |
| 312 | self.x_axis().mul_scalar(other), |
| 313 | self.y_axis().mul_scalar(other), |
| 314 | self.z_axis().mul_scalar(other), |
| 315 | ) |
| 316 | } |
| 317 | |
| 318 | #[inline] |
| 319 | fn add_matrix(&self, other: &Self) -> Self { |
| 320 | Self::from_cols( |
| 321 | self.x_axis().add(*other.x_axis()), |
| 322 | self.y_axis().add(*other.y_axis()), |
| 323 | self.z_axis().add(*other.z_axis()), |
| 324 | ) |
| 325 | } |
| 326 | |
| 327 | #[inline] |
| 328 | fn sub_matrix(&self, other: &Self) -> Self { |
| 329 | Self::from_cols( |
| 330 | self.x_axis().sub(*other.x_axis()), |
| 331 | self.y_axis().sub(*other.y_axis()), |
| 332 | self.z_axis().sub(*other.z_axis()), |
| 333 | ) |
| 334 | } |
| 335 | } |
| 336 | |
| 337 | pub trait FloatMatrix3x3<T: FloatEx, V3: FloatVector3<T>>: Matrix3x3<T, V3> { |
| 338 | #[inline] |
| 339 | fn abs_diff_eq(&self, other: &Self, max_abs_diff: T) -> bool |
| 340 | where |
| 341 | <V3 as Vector<T>>::Mask: MaskVector3, |
| 342 | { |
| 343 | self.x_axis().abs_diff_eq(*other.x_axis(), max_abs_diff) |
| 344 | && self.y_axis().abs_diff_eq(*other.y_axis(), max_abs_diff) |
| 345 | && self.z_axis().abs_diff_eq(*other.z_axis(), max_abs_diff) |
| 346 | } |
| 347 | |
| 348 | #[inline] |
| 349 | fn from_angle(angle: T) -> Self { |
| 350 | let (sin, cos) = angle.sin_cos(); |
| 351 | Self::from_cols( |
| 352 | V3::new(cos, sin, T::ZERO), |
| 353 | V3::new(-sin, cos, T::ZERO), |
| 354 | V3::Z, |
| 355 | ) |
| 356 | } |
| 357 | #[inline] |
| 358 | fn from_scale_angle_translation(scale: XY<T>, angle: T, translation: XY<T>) -> Self { |
| 359 | let (sin, cos) = angle.sin_cos(); |
| 360 | Self::from_cols( |
| 361 | V3::new(cos * scale.x, sin * scale.x, T::ZERO), |
| 362 | V3::new(-sin * scale.y, cos * scale.y, T::ZERO), |
| 363 | V3::new(translation.x, translation.y, T::ONE), |
| 364 | ) |
| 365 | } |
| 366 | |
| 367 | #[inline] |
| 368 | fn from_axis_angle(axis: XYZ<T>, angle: T) -> Self { |
| 369 | glam_assert!(axis.is_normalized()); |
| 370 | let (sin, cos) = angle.sin_cos(); |
| 371 | let (xsin, ysin, zsin) = axis.mul_scalar(sin).into_tuple(); |
| 372 | let (x, y, z) = axis.into_tuple(); |
| 373 | let (x2, y2, z2) = axis.mul(axis).into_tuple(); |
| 374 | let omc = T::ONE - cos; |
| 375 | let xyomc = x * y * omc; |
| 376 | let xzomc = x * z * omc; |
| 377 | let yzomc = y * z * omc; |
| 378 | Self::from_cols( |
| 379 | V3::new(x2 * omc + cos, xyomc + zsin, xzomc - ysin), |
| 380 | V3::new(xyomc - zsin, y2 * omc + cos, yzomc + xsin), |
| 381 | V3::new(xzomc + ysin, yzomc - xsin, z2 * omc + cos), |
| 382 | ) |
| 383 | } |
| 384 | |
| 385 | #[inline] |
| 386 | fn from_quaternion(rotation: XYZW<T>) -> Self { |
| 387 | glam_assert!(rotation.is_normalized()); |
| 388 | let x2 = rotation.x + rotation.x; |
| 389 | let y2 = rotation.y + rotation.y; |
| 390 | let z2 = rotation.z + rotation.z; |
| 391 | let xx = rotation.x * x2; |
| 392 | let xy = rotation.x * y2; |
| 393 | let xz = rotation.x * z2; |
| 394 | let yy = rotation.y * y2; |
| 395 | let yz = rotation.y * z2; |
| 396 | let zz = rotation.z * z2; |
| 397 | let wx = rotation.w * x2; |
| 398 | let wy = rotation.w * y2; |
| 399 | let wz = rotation.w * z2; |
| 400 | |
| 401 | Self::from_cols( |
| 402 | V3::new(T::ONE - (yy + zz), xy + wz, xz - wy), |
| 403 | V3::new(xy - wz, T::ONE - (xx + zz), yz + wx), |
| 404 | V3::new(xz + wy, yz - wx, T::ONE - (xx + yy)), |
| 405 | ) |
| 406 | } |
| 407 | |
| 408 | #[inline] |
| 409 | fn from_rotation_x(angle: T) -> Self { |
| 410 | let (sina, cosa) = angle.sin_cos(); |
| 411 | Self::from_cols( |
| 412 | V3::X, |
| 413 | V3::new(T::ZERO, cosa, sina), |
| 414 | V3::new(T::ZERO, -sina, cosa), |
| 415 | ) |
| 416 | } |
| 417 | |
| 418 | #[inline] |
| 419 | fn from_rotation_y(angle: T) -> Self { |
| 420 | let (sina, cosa) = angle.sin_cos(); |
| 421 | Self::from_cols( |
| 422 | V3::new(cosa, T::ZERO, -sina), |
| 423 | V3::Y, |
| 424 | V3::new(sina, T::ZERO, cosa), |
| 425 | ) |
| 426 | } |
| 427 | |
| 428 | #[inline] |
| 429 | fn from_rotation_z(angle: T) -> Self { |
| 430 | let (sina, cosa) = angle.sin_cos(); |
| 431 | Self::from_cols( |
| 432 | V3::new(cosa, sina, T::ZERO), |
| 433 | V3::new(-sina, cosa, T::ZERO), |
| 434 | V3::Z, |
| 435 | ) |
| 436 | } |
| 437 | |
| 438 | fn transform_point2(&self, other: XY<T>) -> XY<T>; |
| 439 | fn transform_vector2(&self, other: XY<T>) -> XY<T>; |
| 440 | |
| 441 | #[inline] |
| 442 | fn inverse(&self) -> Self |
| 443 | where |
| 444 | <V3 as Vector<T>>::Mask: MaskVector3, |
| 445 | { |
| 446 | let x_axis = self.x_axis(); |
| 447 | let y_axis = self.y_axis(); |
| 448 | let z_axis = self.z_axis(); |
| 449 | let tmp0 = y_axis.cross(*z_axis); |
| 450 | let tmp1 = z_axis.cross(*x_axis); |
| 451 | let tmp2 = x_axis.cross(*y_axis); |
| 452 | let det = z_axis.dot_into_vec(tmp2); |
| 453 | glam_assert!(det.cmpne(V3::ZERO).all()); |
| 454 | let inv_det = det.recip(); |
| 455 | // TODO: Work out if it's possible to get rid of the transpose |
| 456 | Self::from_cols(tmp0.mul(inv_det), tmp1.mul(inv_det), tmp2.mul(inv_det)).transpose() |
| 457 | } |
| 458 | |
| 459 | #[inline] |
| 460 | fn is_finite(&self) -> bool { |
| 461 | self.x_axis().is_finite() && self.y_axis().is_finite() && self.z_axis().is_finite() |
| 462 | } |
| 463 | |
| 464 | #[inline] |
| 465 | fn is_nan(&self) -> bool { |
| 466 | self.x_axis().is_nan() || self.y_axis().is_nan() || self.z_axis().is_nan() |
| 467 | } |
| 468 | } |
| 469 | |
| 470 | pub trait Matrix4x4<T: NumEx, V4: Vector4<T>>: Matrix<T> { |
| 471 | fn from_cols(x_axis: V4, y_axis: V4, z_axis: V4, w_axis: V4) -> Self; |
| 472 | |
| 473 | fn x_axis(&self) -> &V4; |
| 474 | fn y_axis(&self) -> &V4; |
| 475 | fn z_axis(&self) -> &V4; |
| 476 | fn w_axis(&self) -> &V4; |
| 477 | |
| 478 | #[rustfmt::skip] |
| 479 | #[inline(always)] |
| 480 | fn from_cols_array(m: &[T; 16]) -> Self { |
| 481 | Self::from_cols( |
| 482 | V4::new( m[0], m[1], m[2], m[3]), |
| 483 | V4::new( m[4], m[5], m[6], m[7]), |
| 484 | V4::new( m[8], m[9], m[10], m[11]), |
| 485 | V4::new(m[12], m[13], m[14], m[15])) |
| 486 | } |
| 487 | |
| 488 | #[rustfmt::skip] |
| 489 | #[inline(always)] |
| 490 | fn to_cols_array(&self) -> [T; 16] { |
| 491 | let x_axis = self.x_axis(); |
| 492 | let y_axis = self.y_axis(); |
| 493 | let z_axis = self.z_axis(); |
| 494 | let w_axis = self.w_axis(); |
| 495 | [ |
| 496 | x_axis.x(), x_axis.y(), x_axis.z(), x_axis.w(), |
| 497 | y_axis.x(), y_axis.y(), y_axis.z(), y_axis.w(), |
| 498 | z_axis.x(), z_axis.y(), z_axis.z(), z_axis.w(), |
| 499 | w_axis.x(), w_axis.y(), w_axis.z(), w_axis.w(), |
| 500 | ] |
| 501 | } |
| 502 | |
| 503 | #[inline(always)] |
| 504 | fn from_cols_array_2d(m: &[[T; 4]; 4]) -> Self { |
| 505 | Self::from_cols( |
| 506 | Vector4::from_array(m[0]), |
| 507 | Vector4::from_array(m[1]), |
| 508 | Vector4::from_array(m[2]), |
| 509 | Vector4::from_array(m[3]), |
| 510 | ) |
| 511 | } |
| 512 | |
| 513 | #[inline(always)] |
| 514 | fn to_cols_array_2d(&self) -> [[T; 4]; 4] { |
| 515 | [ |
| 516 | self.x_axis().into_array(), |
| 517 | self.y_axis().into_array(), |
| 518 | self.z_axis().into_array(), |
| 519 | self.w_axis().into_array(), |
| 520 | ] |
| 521 | } |
| 522 | |
| 523 | #[rustfmt::skip] |
| 524 | #[inline(always)] |
| 525 | fn from_cols_slice(m: &[T]) -> Self { |
| 526 | Self::from_cols( |
| 527 | V4::new( m[0], m[1], m[2], m[3]), |
| 528 | V4::new( m[4], m[5], m[6], m[7]), |
| 529 | V4::new( m[8], m[9], m[10], m[11]), |
| 530 | V4::new(m[12], m[13], m[14], m[15])) |
| 531 | } |
| 532 | |
| 533 | #[inline(always)] |
| 534 | fn write_cols_to_slice(&self, slice: &mut [T]) { |
| 535 | let x_axis = self.x_axis(); |
| 536 | let y_axis = self.y_axis(); |
| 537 | let z_axis = self.z_axis(); |
| 538 | let w_axis = self.w_axis(); |
| 539 | slice[0] = x_axis.x(); |
| 540 | slice[1] = x_axis.y(); |
| 541 | slice[2] = x_axis.z(); |
| 542 | slice[3] = x_axis.w(); |
| 543 | |
| 544 | slice[4] = y_axis.x(); |
| 545 | slice[5] = y_axis.y(); |
| 546 | slice[6] = y_axis.z(); |
| 547 | slice[7] = y_axis.w(); |
| 548 | |
| 549 | slice[8] = z_axis.x(); |
| 550 | slice[9] = z_axis.y(); |
| 551 | slice[10] = z_axis.z(); |
| 552 | slice[11] = z_axis.w(); |
| 553 | |
| 554 | slice[12] = w_axis.x(); |
| 555 | slice[13] = w_axis.y(); |
| 556 | slice[14] = w_axis.z(); |
| 557 | slice[15] = w_axis.w(); |
| 558 | } |
| 559 | |
| 560 | #[inline(always)] |
| 561 | fn from_diagonal(diagonal: XYZW<T>) -> Self { |
| 562 | Self::from_cols( |
| 563 | V4::new(diagonal.x, T::ZERO, T::ZERO, T::ZERO), |
| 564 | V4::new(T::ZERO, diagonal.y, T::ZERO, T::ZERO), |
| 565 | V4::new(T::ZERO, T::ZERO, diagonal.z, T::ZERO), |
| 566 | V4::new(T::ZERO, T::ZERO, T::ZERO, diagonal.w), |
| 567 | ) |
| 568 | } |
| 569 | |
| 570 | #[inline(always)] |
| 571 | fn from_scale(scale: XYZ<T>) -> Self { |
| 572 | // Do not panic as long as any component is non-zero |
| 573 | glam_assert!(scale.cmpne(XYZ::<T>::ZERO).any()); |
| 574 | Self::from_cols( |
| 575 | V4::new(scale.x, T::ZERO, T::ZERO, T::ZERO), |
| 576 | V4::new(T::ZERO, scale.y, T::ZERO, T::ZERO), |
| 577 | V4::new(T::ZERO, T::ZERO, scale.z, T::ZERO), |
| 578 | V4::W, |
| 579 | ) |
| 580 | } |
| 581 | |
| 582 | #[inline(always)] |
| 583 | fn from_translation(translation: XYZ<T>) -> Self { |
| 584 | Self::from_cols( |
| 585 | V4::X, |
| 586 | V4::Y, |
| 587 | V4::Z, |
| 588 | V4::new(translation.x, translation.y, translation.z, T::ONE), |
| 589 | ) |
| 590 | } |
| 591 | |
| 592 | #[inline] |
| 593 | fn determinant(&self) -> T { |
| 594 | let (m00, m01, m02, m03) = self.x_axis().into_tuple(); |
| 595 | let (m10, m11, m12, m13) = self.y_axis().into_tuple(); |
| 596 | let (m20, m21, m22, m23) = self.z_axis().into_tuple(); |
| 597 | let (m30, m31, m32, m33) = self.w_axis().into_tuple(); |
| 598 | |
| 599 | let a2323 = m22 * m33 - m23 * m32; |
| 600 | let a1323 = m21 * m33 - m23 * m31; |
| 601 | let a1223 = m21 * m32 - m22 * m31; |
| 602 | let a0323 = m20 * m33 - m23 * m30; |
| 603 | let a0223 = m20 * m32 - m22 * m30; |
| 604 | let a0123 = m20 * m31 - m21 * m30; |
| 605 | |
| 606 | m00 * (m11 * a2323 - m12 * a1323 + m13 * a1223) |
| 607 | - m01 * (m10 * a2323 - m12 * a0323 + m13 * a0223) |
| 608 | + m02 * (m10 * a1323 - m11 * a0323 + m13 * a0123) |
| 609 | - m03 * (m10 * a1223 - m11 * a0223 + m12 * a0123) |
| 610 | } |
| 611 | |
| 612 | #[inline] |
| 613 | fn transpose(&self) -> Self { |
| 614 | let (m00, m01, m02, m03) = self.x_axis().into_tuple(); |
| 615 | let (m10, m11, m12, m13) = self.y_axis().into_tuple(); |
| 616 | let (m20, m21, m22, m23) = self.z_axis().into_tuple(); |
| 617 | let (m30, m31, m32, m33) = self.w_axis().into_tuple(); |
| 618 | |
| 619 | Self::from_cols( |
| 620 | V4::new(m00, m10, m20, m30), |
| 621 | V4::new(m01, m11, m21, m31), |
| 622 | V4::new(m02, m12, m22, m32), |
| 623 | V4::new(m03, m13, m23, m33), |
| 624 | ) |
| 625 | } |
| 626 | |
| 627 | #[inline] |
| 628 | fn mul_vector(&self, other: V4) -> V4 { |
| 629 | let mut res = self.x_axis().mul(other.splat_x()); |
| 630 | res = self.y_axis().mul_add(other.splat_y(), res); |
| 631 | res = self.z_axis().mul_add(other.splat_z(), res); |
| 632 | res = self.w_axis().mul_add(other.splat_w(), res); |
| 633 | res |
| 634 | } |
| 635 | |
| 636 | #[inline] |
| 637 | fn mul_matrix(&self, other: &Self) -> Self { |
| 638 | Self::from_cols( |
| 639 | self.mul_vector(*other.x_axis()), |
| 640 | self.mul_vector(*other.y_axis()), |
| 641 | self.mul_vector(*other.z_axis()), |
| 642 | self.mul_vector(*other.w_axis()), |
| 643 | ) |
| 644 | } |
| 645 | |
| 646 | #[inline] |
| 647 | fn mul_scalar(&self, other: T) -> Self { |
| 648 | Self::from_cols( |
| 649 | self.x_axis().mul_scalar(other), |
| 650 | self.y_axis().mul_scalar(other), |
| 651 | self.z_axis().mul_scalar(other), |
| 652 | self.w_axis().mul_scalar(other), |
| 653 | ) |
| 654 | } |
| 655 | |
| 656 | #[inline] |
| 657 | fn add_matrix(&self, other: &Self) -> Self { |
| 658 | // TODO: Make Vector4::add take a ref? |
| 659 | Self::from_cols( |
| 660 | self.x_axis().add(*other.x_axis()), |
| 661 | self.y_axis().add(*other.y_axis()), |
| 662 | self.z_axis().add(*other.z_axis()), |
| 663 | self.w_axis().add(*other.w_axis()), |
| 664 | ) |
| 665 | } |
| 666 | |
| 667 | #[inline] |
| 668 | fn sub_matrix(&self, other: &Self) -> Self { |
| 669 | // TODO: Make Vector4::sub take a ref? |
| 670 | Self::from_cols( |
| 671 | self.x_axis().sub(*other.x_axis()), |
| 672 | self.y_axis().sub(*other.y_axis()), |
| 673 | self.z_axis().sub(*other.z_axis()), |
| 674 | self.w_axis().sub(*other.w_axis()), |
| 675 | ) |
| 676 | } |
| 677 | } |
| 678 | |
| 679 | pub trait FloatMatrix4x4<T: FloatEx, V4: FloatVector4<T> + Quaternion<T>>: |
| 680 | Matrix4x4<T, V4> |
| 681 | { |
| 682 | // Vector3 represented by a SIMD type if available |
| 683 | type SIMDVector3; |
| 684 | |
| 685 | #[inline] |
| 686 | fn abs_diff_eq(&self, other: &Self, max_abs_diff: T) -> bool |
| 687 | where |
| 688 | <V4 as Vector<T>>::Mask: MaskVector4, |
| 689 | { |
| 690 | self.x_axis().abs_diff_eq(*other.x_axis(), max_abs_diff) |
| 691 | && self.y_axis().abs_diff_eq(*other.y_axis(), max_abs_diff) |
| 692 | && self.z_axis().abs_diff_eq(*other.z_axis(), max_abs_diff) |
| 693 | && self.w_axis().abs_diff_eq(*other.w_axis(), max_abs_diff) |
| 694 | } |
| 695 | |
| 696 | #[inline] |
| 697 | fn quaternion_to_axes(rotation: V4) -> (V4, V4, V4) { |
| 698 | glam_assert!(rotation.is_normalized()); |
| 699 | let (x, y, z, w) = rotation.into_tuple(); |
| 700 | let x2 = x + x; |
| 701 | let y2 = y + y; |
| 702 | let z2 = z + z; |
| 703 | let xx = x * x2; |
| 704 | let xy = x * y2; |
| 705 | let xz = x * z2; |
| 706 | let yy = y * y2; |
| 707 | let yz = y * z2; |
| 708 | let zz = z * z2; |
| 709 | let wx = w * x2; |
| 710 | let wy = w * y2; |
| 711 | let wz = w * z2; |
| 712 | |
| 713 | let x_axis = V4::new(T::ONE - (yy + zz), xy + wz, xz - wy, T::ZERO); |
| 714 | let y_axis = V4::new(xy - wz, T::ONE - (xx + zz), yz + wx, T::ZERO); |
| 715 | let z_axis = V4::new(xz + wy, yz - wx, T::ONE - (xx + yy), T::ZERO); |
| 716 | (x_axis, y_axis, z_axis) |
| 717 | } |
| 718 | |
| 719 | #[inline] |
| 720 | fn from_quaternion(rotation: V4) -> Self { |
| 721 | let (x_axis, y_axis, z_axis) = Self::quaternion_to_axes(rotation); |
| 722 | Self::from_cols(x_axis, y_axis, z_axis, V4::W) |
| 723 | } |
| 724 | |
| 725 | fn to_scale_quaternion_translation(&self) -> (XYZ<T>, V4, XYZ<T>) { |
| 726 | let det = self.determinant(); |
| 727 | glam_assert!(det != T::ZERO); |
| 728 | |
| 729 | let scale: XYZ<T> = Vector3::new( |
| 730 | self.x_axis().length() * det.signum(), |
| 731 | self.y_axis().length(), |
| 732 | self.z_axis().length(), |
| 733 | ); |
| 734 | |
| 735 | glam_assert!(scale.cmpne(XYZ::<T>::ZERO).all()); |
| 736 | |
| 737 | let inv_scale = scale.recip(); |
| 738 | |
| 739 | let rotation = Quaternion::from_rotation_axes( |
| 740 | self.x_axis().mul_scalar(inv_scale.x).into_xyz(), |
| 741 | self.y_axis().mul_scalar(inv_scale.y).into_xyz(), |
| 742 | self.z_axis().mul_scalar(inv_scale.z).into_xyz(), |
| 743 | ); |
| 744 | |
| 745 | let translation = self.w_axis().into_xyz(); |
| 746 | |
| 747 | (scale, rotation, translation) |
| 748 | } |
| 749 | |
| 750 | #[inline] |
| 751 | fn from_scale_quaternion_translation(scale: XYZ<T>, rotation: V4, translation: XYZ<T>) -> Self { |
| 752 | let (x_axis, y_axis, z_axis) = Self::quaternion_to_axes(rotation); |
| 753 | Self::from_cols( |
| 754 | x_axis.mul_scalar(scale.x), |
| 755 | y_axis.mul_scalar(scale.y), |
| 756 | z_axis.mul_scalar(scale.z), |
| 757 | V4::from_xyz(translation, T::ONE), |
| 758 | ) |
| 759 | } |
| 760 | |
| 761 | #[inline] |
| 762 | fn from_quaternion_translation(rotation: V4, translation: XYZ<T>) -> Self { |
| 763 | let (x_axis, y_axis, z_axis) = Self::quaternion_to_axes(rotation); |
| 764 | Self::from_cols(x_axis, y_axis, z_axis, V4::from_xyz(translation, T::ONE)) |
| 765 | } |
| 766 | |
| 767 | #[inline] |
| 768 | fn from_axis_angle(axis: XYZ<T>, angle: T) -> Self { |
| 769 | glam_assert!(axis.is_normalized()); |
| 770 | let (sin, cos) = angle.sin_cos(); |
| 771 | let axis_sin = axis.mul_scalar(sin); |
| 772 | let axis_sq = axis.mul(axis); |
| 773 | let omc = T::ONE - cos; |
| 774 | let xyomc = axis.x * axis.y * omc; |
| 775 | let xzomc = axis.x * axis.z * omc; |
| 776 | let yzomc = axis.y * axis.z * omc; |
| 777 | Self::from_cols( |
| 778 | V4::new( |
| 779 | axis_sq.x * omc + cos, |
| 780 | xyomc + axis_sin.z, |
| 781 | xzomc - axis_sin.y, |
| 782 | T::ZERO, |
| 783 | ), |
| 784 | V4::new( |
| 785 | xyomc - axis_sin.z, |
| 786 | axis_sq.y * omc + cos, |
| 787 | yzomc + axis_sin.x, |
| 788 | T::ZERO, |
| 789 | ), |
| 790 | V4::new( |
| 791 | xzomc + axis_sin.y, |
| 792 | yzomc - axis_sin.x, |
| 793 | axis_sq.z * omc + cos, |
| 794 | T::ZERO, |
| 795 | ), |
| 796 | V4::W, |
| 797 | ) |
| 798 | } |
| 799 | |
| 800 | #[inline] |
| 801 | fn from_rotation_x(angle: T) -> Self { |
| 802 | let (sina, cosa) = angle.sin_cos(); |
| 803 | Self::from_cols( |
| 804 | V4::X, |
| 805 | V4::new(T::ZERO, cosa, sina, T::ZERO), |
| 806 | V4::new(T::ZERO, -sina, cosa, T::ZERO), |
| 807 | V4::W, |
| 808 | ) |
| 809 | } |
| 810 | |
| 811 | #[inline] |
| 812 | fn from_rotation_y(angle: T) -> Self { |
| 813 | let (sina, cosa) = angle.sin_cos(); |
| 814 | Self::from_cols( |
| 815 | V4::new(cosa, T::ZERO, -sina, T::ZERO), |
| 816 | V4::Y, |
| 817 | V4::new(sina, T::ZERO, cosa, T::ZERO), |
| 818 | V4::W, |
| 819 | ) |
| 820 | } |
| 821 | |
| 822 | #[inline] |
| 823 | fn from_rotation_z(angle: T) -> Self { |
| 824 | let (sina, cosa) = angle.sin_cos(); |
| 825 | Self::from_cols( |
| 826 | V4::new(cosa, sina, T::ZERO, T::ZERO), |
| 827 | V4::new(-sina, cosa, T::ZERO, T::ZERO), |
| 828 | V4::Z, |
| 829 | V4::W, |
| 830 | ) |
| 831 | } |
| 832 | |
| 833 | #[inline] |
| 834 | fn look_to_lh(eye: XYZ<T>, dir: XYZ<T>, up: XYZ<T>) -> Self { |
| 835 | let f = dir.normalize(); |
| 836 | let s = up.cross(f).normalize(); |
| 837 | let u = f.cross(s); |
| 838 | Self::from_cols( |
| 839 | V4::new(s.x, u.x, f.x, T::ZERO), |
| 840 | V4::new(s.y, u.y, f.y, T::ZERO), |
| 841 | V4::new(s.z, u.z, f.z, T::ZERO), |
| 842 | V4::new(-s.dot(eye), -u.dot(eye), -f.dot(eye), T::ONE), |
| 843 | ) |
| 844 | } |
| 845 | |
| 846 | #[inline] |
| 847 | fn look_at_lh(eye: XYZ<T>, center: XYZ<T>, up: XYZ<T>) -> Self { |
| 848 | glam_assert!(up.is_normalized()); |
| 849 | Self::look_to_lh(eye, center.sub(eye), up) |
| 850 | } |
| 851 | |
| 852 | #[inline] |
| 853 | fn look_at_rh(eye: XYZ<T>, center: XYZ<T>, up: XYZ<T>) -> Self { |
| 854 | glam_assert!(up.is_normalized()); |
| 855 | Self::look_to_lh(eye, eye.sub(center), up) |
| 856 | } |
| 857 | |
| 858 | #[inline] |
| 859 | fn transform_point3(&self, other: XYZ<T>) -> XYZ<T> { |
| 860 | let mut res = self.x_axis().mul_scalar(other.x); |
| 861 | res = self.y_axis().mul_scalar(other.y).add(res); |
| 862 | res = self.z_axis().mul_scalar(other.z).add(res); |
| 863 | res = self.w_axis().add(res); |
| 864 | res.into_xyz() |
| 865 | } |
| 866 | |
| 867 | #[inline] |
| 868 | fn transform_vector3(&self, other: XYZ<T>) -> XYZ<T> { |
| 869 | let mut res = self.x_axis().mul_scalar(other.x); |
| 870 | res = self.y_axis().mul_scalar(other.y).add(res); |
| 871 | res = self.z_axis().mul_scalar(other.z).add(res); |
| 872 | res.into_xyz() |
| 873 | } |
| 874 | |
| 875 | #[inline] |
| 876 | fn project_point3(&self, other: XYZ<T>) -> XYZ<T> { |
| 877 | let mut res = self.x_axis().mul_scalar(other.x); |
| 878 | res = self.y_axis().mul_scalar(other.y).add(res); |
| 879 | res = self.z_axis().mul_scalar(other.z).add(res); |
| 880 | res = self.w_axis().add(res); |
| 881 | res = res.mul(res.splat_w().recip()); |
| 882 | res.into_xyz() |
| 883 | } |
| 884 | |
| 885 | fn transform_float4_as_point3(&self, other: Self::SIMDVector3) -> Self::SIMDVector3; |
| 886 | fn transform_float4_as_vector3(&self, other: Self::SIMDVector3) -> Self::SIMDVector3; |
| 887 | fn project_float4_as_point3(&self, other: Self::SIMDVector3) -> Self::SIMDVector3; |
| 888 | |
| 889 | fn inverse(&self) -> Self { |
| 890 | let (m00, m01, m02, m03) = self.x_axis().into_tuple(); |
| 891 | let (m10, m11, m12, m13) = self.y_axis().into_tuple(); |
| 892 | let (m20, m21, m22, m23) = self.z_axis().into_tuple(); |
| 893 | let (m30, m31, m32, m33) = self.w_axis().into_tuple(); |
| 894 | |
| 895 | let coef00 = m22 * m33 - m32 * m23; |
| 896 | let coef02 = m12 * m33 - m32 * m13; |
| 897 | let coef03 = m12 * m23 - m22 * m13; |
| 898 | |
| 899 | let coef04 = m21 * m33 - m31 * m23; |
| 900 | let coef06 = m11 * m33 - m31 * m13; |
| 901 | let coef07 = m11 * m23 - m21 * m13; |
| 902 | |
| 903 | let coef08 = m21 * m32 - m31 * m22; |
| 904 | let coef10 = m11 * m32 - m31 * m12; |
| 905 | let coef11 = m11 * m22 - m21 * m12; |
| 906 | |
| 907 | let coef12 = m20 * m33 - m30 * m23; |
| 908 | let coef14 = m10 * m33 - m30 * m13; |
| 909 | let coef15 = m10 * m23 - m20 * m13; |
| 910 | |
| 911 | let coef16 = m20 * m32 - m30 * m22; |
| 912 | let coef18 = m10 * m32 - m30 * m12; |
| 913 | let coef19 = m10 * m22 - m20 * m12; |
| 914 | |
| 915 | let coef20 = m20 * m31 - m30 * m21; |
| 916 | let coef22 = m10 * m31 - m30 * m11; |
| 917 | let coef23 = m10 * m21 - m20 * m11; |
| 918 | |
| 919 | let fac0 = V4::new(coef00, coef00, coef02, coef03); |
| 920 | let fac1 = V4::new(coef04, coef04, coef06, coef07); |
| 921 | let fac2 = V4::new(coef08, coef08, coef10, coef11); |
| 922 | let fac3 = V4::new(coef12, coef12, coef14, coef15); |
| 923 | let fac4 = V4::new(coef16, coef16, coef18, coef19); |
| 924 | let fac5 = V4::new(coef20, coef20, coef22, coef23); |
| 925 | |
| 926 | let vec0 = V4::new(m10, m00, m00, m00); |
| 927 | let vec1 = V4::new(m11, m01, m01, m01); |
| 928 | let vec2 = V4::new(m12, m02, m02, m02); |
| 929 | let vec3 = V4::new(m13, m03, m03, m03); |
| 930 | |
| 931 | let inv0 = vec1.mul(fac0).sub(vec2.mul(fac1)).add(vec3.mul(fac2)); |
| 932 | let inv1 = vec0.mul(fac0).sub(vec2.mul(fac3)).add(vec3.mul(fac4)); |
| 933 | let inv2 = vec0.mul(fac1).sub(vec1.mul(fac3)).add(vec3.mul(fac5)); |
| 934 | let inv3 = vec0.mul(fac2).sub(vec1.mul(fac4)).add(vec2.mul(fac5)); |
| 935 | |
| 936 | let sign_a = Vector4::new(T::ONE, -T::ONE, T::ONE, -T::ONE); |
| 937 | let sign_b = Vector4::new(-T::ONE, T::ONE, -T::ONE, T::ONE); |
| 938 | |
| 939 | let inverse = Self::from_cols( |
| 940 | inv0.mul(sign_a), |
| 941 | inv1.mul(sign_b), |
| 942 | inv2.mul(sign_a), |
| 943 | inv3.mul(sign_b), |
| 944 | ); |
| 945 | |
| 946 | let col0 = V4::new( |
| 947 | inverse.x_axis().x(), |
| 948 | inverse.y_axis().x(), |
| 949 | inverse.z_axis().x(), |
| 950 | inverse.w_axis().x(), |
| 951 | ); |
| 952 | |
| 953 | let dot0 = self.x_axis().mul(col0); |
| 954 | let dot1 = dot0.x() + dot0.y() + dot0.z() + dot0.w(); |
| 955 | |
| 956 | glam_assert!(dot1 != T::ZERO); |
| 957 | |
| 958 | let rcp_det = dot1.recip(); |
| 959 | inverse.mul_scalar(rcp_det) |
| 960 | } |
| 961 | } |