| 1 | // Generated from mat.rs.tera template. Edit the template, not the generated file. |
| 2 | |
| 3 | use crate::{swizzles::*, DMat3, DQuat, DVec3, DVec4, EulerRot, Mat4}; |
| 4 | #[cfg(not(target_arch = "spirv"))] |
| 5 | use core::fmt; |
| 6 | use core::iter::{Product, Sum}; |
| 7 | use core::ops::{Add, AddAssign, Mul, MulAssign, Neg, Sub, SubAssign}; |
| 8 | |
| 9 | #[cfg(feature = "libm")] |
| 10 | #[allow(unused_imports)] |
| 11 | use num_traits::Float; |
| 12 | |
| 13 | /// Creates a 4x4 matrix from column vectors. |
| 14 | #[inline(always)] |
| 15 | pub const fn dmat4(x_axis: DVec4, y_axis: DVec4, z_axis: DVec4, w_axis: DVec4) -> DMat4 { |
| 16 | DMat4::from_cols(x_axis, y_axis, z_axis, w_axis) |
| 17 | } |
| 18 | |
| 19 | /// A 4x4 column major matrix. |
| 20 | /// |
| 21 | /// This 4x4 matrix type features convenience methods for creating and using affine transforms and |
| 22 | /// perspective projections. If you are primarily dealing with 3D affine transformations |
| 23 | /// considering using [`DAffine3`](crate::DAffine3) which is faster than a 4x4 matrix |
| 24 | /// for some affine operations. |
| 25 | /// |
| 26 | /// Affine transformations including 3D translation, rotation and scale can be created |
| 27 | /// using methods such as [`Self::from_translation()`], [`Self::from_quat()`], |
| 28 | /// [`Self::from_scale()`] and [`Self::from_scale_rotation_translation()`]. |
| 29 | /// |
| 30 | /// Othographic projections can be created using the methods [`Self::orthographic_lh()`] for |
| 31 | /// left-handed coordinate systems and [`Self::orthographic_rh()`] for right-handed |
| 32 | /// systems. The resulting matrix is also an affine transformation. |
| 33 | /// |
| 34 | /// The [`Self::transform_point3()`] and [`Self::transform_vector3()`] convenience methods |
| 35 | /// are provided for performing affine transformations on 3D vectors and points. These |
| 36 | /// multiply 3D inputs as 4D vectors with an implicit `w` value of `1` for points and `0` |
| 37 | /// for vectors respectively. These methods assume that `Self` contains a valid affine |
| 38 | /// transform. |
| 39 | /// |
| 40 | /// Perspective projections can be created using methods such as |
| 41 | /// [`Self::perspective_lh()`], [`Self::perspective_infinite_lh()`] and |
| 42 | /// [`Self::perspective_infinite_reverse_lh()`] for left-handed co-ordinate systems and |
| 43 | /// [`Self::perspective_rh()`], [`Self::perspective_infinite_rh()`] and |
| 44 | /// [`Self::perspective_infinite_reverse_rh()`] for right-handed co-ordinate systems. |
| 45 | /// |
| 46 | /// The resulting perspective project can be use to transform 3D vectors as points with |
| 47 | /// perspective correction using the [`Self::project_point3()`] convenience method. |
| 48 | #[derive(Clone, Copy)] |
| 49 | #[cfg_attr(feature = "cuda", repr(align(16)))] |
| 50 | #[repr(C)] |
| 51 | pub struct DMat4 { |
| 52 | pub x_axis: DVec4, |
| 53 | pub y_axis: DVec4, |
| 54 | pub z_axis: DVec4, |
| 55 | pub w_axis: DVec4, |
| 56 | } |
| 57 | |
| 58 | impl DMat4 { |
| 59 | /// A 4x4 matrix with all elements set to `0.0`. |
| 60 | pub const ZERO: Self = Self::from_cols(DVec4::ZERO, DVec4::ZERO, DVec4::ZERO, DVec4::ZERO); |
| 61 | |
| 62 | /// A 4x4 identity matrix, where all diagonal elements are `1`, and all off-diagonal elements are `0`. |
| 63 | pub const IDENTITY: Self = Self::from_cols(DVec4::X, DVec4::Y, DVec4::Z, DVec4::W); |
| 64 | |
| 65 | /// All NAN:s. |
| 66 | pub const NAN: Self = Self::from_cols(DVec4::NAN, DVec4::NAN, DVec4::NAN, DVec4::NAN); |
| 67 | |
| 68 | #[allow(clippy::too_many_arguments)] |
| 69 | #[inline(always)] |
| 70 | const fn new( |
| 71 | m00: f64, |
| 72 | m01: f64, |
| 73 | m02: f64, |
| 74 | m03: f64, |
| 75 | m10: f64, |
| 76 | m11: f64, |
| 77 | m12: f64, |
| 78 | m13: f64, |
| 79 | m20: f64, |
| 80 | m21: f64, |
| 81 | m22: f64, |
| 82 | m23: f64, |
| 83 | m30: f64, |
| 84 | m31: f64, |
| 85 | m32: f64, |
| 86 | m33: f64, |
| 87 | ) -> Self { |
| 88 | Self { |
| 89 | x_axis: DVec4::new(m00, m01, m02, m03), |
| 90 | y_axis: DVec4::new(m10, m11, m12, m13), |
| 91 | z_axis: DVec4::new(m20, m21, m22, m23), |
| 92 | w_axis: DVec4::new(m30, m31, m32, m33), |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | /// Creates a 4x4 matrix from two column vectors. |
| 97 | #[inline(always)] |
| 98 | pub const fn from_cols(x_axis: DVec4, y_axis: DVec4, z_axis: DVec4, w_axis: DVec4) -> Self { |
| 99 | Self { |
| 100 | x_axis, |
| 101 | y_axis, |
| 102 | z_axis, |
| 103 | w_axis, |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | /// Creates a 4x4 matrix from a `[f64; 16]` array stored in column major order. |
| 108 | /// If your data is stored in row major you will need to `transpose` the returned |
| 109 | /// matrix. |
| 110 | #[inline] |
| 111 | pub const fn from_cols_array(m: &[f64; 16]) -> Self { |
| 112 | Self::new( |
| 113 | m[0], m[1], m[2], m[3], m[4], m[5], m[6], m[7], m[8], m[9], m[10], m[11], m[12], m[13], |
| 114 | m[14], m[15], |
| 115 | ) |
| 116 | } |
| 117 | |
| 118 | /// Creates a `[f64; 16]` array storing data in column major order. |
| 119 | /// If you require data in row major order `transpose` the matrix first. |
| 120 | #[inline] |
| 121 | pub const fn to_cols_array(&self) -> [f64; 16] { |
| 122 | [ |
| 123 | self.x_axis.x, |
| 124 | self.x_axis.y, |
| 125 | self.x_axis.z, |
| 126 | self.x_axis.w, |
| 127 | self.y_axis.x, |
| 128 | self.y_axis.y, |
| 129 | self.y_axis.z, |
| 130 | self.y_axis.w, |
| 131 | self.z_axis.x, |
| 132 | self.z_axis.y, |
| 133 | self.z_axis.z, |
| 134 | self.z_axis.w, |
| 135 | self.w_axis.x, |
| 136 | self.w_axis.y, |
| 137 | self.w_axis.z, |
| 138 | self.w_axis.w, |
| 139 | ] |
| 140 | } |
| 141 | |
| 142 | /// Creates a 4x4 matrix from a `[[f64; 4]; 4]` 4D array stored in column major order. |
| 143 | /// If your data is in row major order you will need to `transpose` the returned |
| 144 | /// matrix. |
| 145 | #[inline] |
| 146 | pub const fn from_cols_array_2d(m: &[[f64; 4]; 4]) -> Self { |
| 147 | Self::from_cols( |
| 148 | DVec4::from_array(m[0]), |
| 149 | DVec4::from_array(m[1]), |
| 150 | DVec4::from_array(m[2]), |
| 151 | DVec4::from_array(m[3]), |
| 152 | ) |
| 153 | } |
| 154 | |
| 155 | /// Creates a `[[f64; 4]; 4]` 4D array storing data in column major order. |
| 156 | /// If you require data in row major order `transpose` the matrix first. |
| 157 | #[inline] |
| 158 | pub const fn to_cols_array_2d(&self) -> [[f64; 4]; 4] { |
| 159 | [ |
| 160 | self.x_axis.to_array(), |
| 161 | self.y_axis.to_array(), |
| 162 | self.z_axis.to_array(), |
| 163 | self.w_axis.to_array(), |
| 164 | ] |
| 165 | } |
| 166 | |
| 167 | /// Creates a 4x4 matrix with its diagonal set to `diagonal` and all other entries set to 0. |
| 168 | #[doc(alias = "scale")] |
| 169 | #[inline] |
| 170 | pub const fn from_diagonal(diagonal: DVec4) -> Self { |
| 171 | Self::new( |
| 172 | diagonal.x, 0.0, 0.0, 0.0, 0.0, diagonal.y, 0.0, 0.0, 0.0, 0.0, diagonal.z, 0.0, 0.0, |
| 173 | 0.0, 0.0, diagonal.w, |
| 174 | ) |
| 175 | } |
| 176 | |
| 177 | #[inline] |
| 178 | fn quat_to_axes(rotation: DQuat) -> (DVec4, DVec4, DVec4) { |
| 179 | glam_assert!(rotation.is_normalized()); |
| 180 | |
| 181 | let (x, y, z, w) = rotation.into(); |
| 182 | let x2 = x + x; |
| 183 | let y2 = y + y; |
| 184 | let z2 = z + z; |
| 185 | let xx = x * x2; |
| 186 | let xy = x * y2; |
| 187 | let xz = x * z2; |
| 188 | let yy = y * y2; |
| 189 | let yz = y * z2; |
| 190 | let zz = z * z2; |
| 191 | let wx = w * x2; |
| 192 | let wy = w * y2; |
| 193 | let wz = w * z2; |
| 194 | |
| 195 | let x_axis = DVec4::new(1.0 - (yy + zz), xy + wz, xz - wy, 0.0); |
| 196 | let y_axis = DVec4::new(xy - wz, 1.0 - (xx + zz), yz + wx, 0.0); |
| 197 | let z_axis = DVec4::new(xz + wy, yz - wx, 1.0 - (xx + yy), 0.0); |
| 198 | (x_axis, y_axis, z_axis) |
| 199 | } |
| 200 | |
| 201 | /// Creates an affine transformation matrix from the given 3D `scale`, `rotation` and |
| 202 | /// `translation`. |
| 203 | /// |
| 204 | /// The resulting matrix can be used to transform 3D points and vectors. See |
| 205 | /// [`Self::transform_point3()`] and [`Self::transform_vector3()`]. |
| 206 | /// |
| 207 | /// # Panics |
| 208 | /// |
| 209 | /// Will panic if `rotation` is not normalized when `glam_assert` is enabled. |
| 210 | #[inline] |
| 211 | pub fn from_scale_rotation_translation( |
| 212 | scale: DVec3, |
| 213 | rotation: DQuat, |
| 214 | translation: DVec3, |
| 215 | ) -> Self { |
| 216 | let (x_axis, y_axis, z_axis) = Self::quat_to_axes(rotation); |
| 217 | Self::from_cols( |
| 218 | x_axis.mul(scale.x), |
| 219 | y_axis.mul(scale.y), |
| 220 | z_axis.mul(scale.z), |
| 221 | DVec4::from((translation, 1.0)), |
| 222 | ) |
| 223 | } |
| 224 | |
| 225 | /// Creates an affine transformation matrix from the given 3D `translation`. |
| 226 | /// |
| 227 | /// The resulting matrix can be used to transform 3D points and vectors. See |
| 228 | /// [`Self::transform_point3()`] and [`Self::transform_vector3()`]. |
| 229 | /// |
| 230 | /// # Panics |
| 231 | /// |
| 232 | /// Will panic if `rotation` is not normalized when `glam_assert` is enabled. |
| 233 | #[inline] |
| 234 | pub fn from_rotation_translation(rotation: DQuat, translation: DVec3) -> Self { |
| 235 | let (x_axis, y_axis, z_axis) = Self::quat_to_axes(rotation); |
| 236 | Self::from_cols(x_axis, y_axis, z_axis, DVec4::from((translation, 1.0))) |
| 237 | } |
| 238 | |
| 239 | /// Extracts `scale`, `rotation` and `translation` from `self`. The input matrix is |
| 240 | /// expected to be a 3D affine transformation matrix otherwise the output will be invalid. |
| 241 | /// |
| 242 | /// # Panics |
| 243 | /// |
| 244 | /// Will panic if the determinant of `self` is zero or if the resulting scale vector |
| 245 | /// contains any zero elements when `glam_assert` is enabled. |
| 246 | #[inline] |
| 247 | pub fn to_scale_rotation_translation(&self) -> (DVec3, DQuat, DVec3) { |
| 248 | let det = self.determinant(); |
| 249 | glam_assert!(det != 0.0); |
| 250 | |
| 251 | let scale = DVec3::new( |
| 252 | self.x_axis.length() * det.signum(), |
| 253 | self.y_axis.length(), |
| 254 | self.z_axis.length(), |
| 255 | ); |
| 256 | |
| 257 | glam_assert!(scale.cmpne(DVec3::ZERO).all()); |
| 258 | |
| 259 | let inv_scale = scale.recip(); |
| 260 | |
| 261 | let rotation = DQuat::from_rotation_axes( |
| 262 | self.x_axis.mul(inv_scale.x).xyz(), |
| 263 | self.y_axis.mul(inv_scale.y).xyz(), |
| 264 | self.z_axis.mul(inv_scale.z).xyz(), |
| 265 | ); |
| 266 | |
| 267 | let translation = self.w_axis.xyz(); |
| 268 | |
| 269 | (scale, rotation, translation) |
| 270 | } |
| 271 | |
| 272 | /// Creates an affine transformation matrix from the given `rotation` quaternion. |
| 273 | /// |
| 274 | /// The resulting matrix can be used to transform 3D points and vectors. See |
| 275 | /// [`Self::transform_point3()`] and [`Self::transform_vector3()`]. |
| 276 | /// |
| 277 | /// # Panics |
| 278 | /// |
| 279 | /// Will panic if `rotation` is not normalized when `glam_assert` is enabled. |
| 280 | #[inline] |
| 281 | pub fn from_quat(rotation: DQuat) -> Self { |
| 282 | let (x_axis, y_axis, z_axis) = Self::quat_to_axes(rotation); |
| 283 | Self::from_cols(x_axis, y_axis, z_axis, DVec4::W) |
| 284 | } |
| 285 | |
| 286 | /// Creates an affine transformation matrix from the given 3x3 linear transformation |
| 287 | /// matrix. |
| 288 | /// |
| 289 | /// The resulting matrix can be used to transform 3D points and vectors. See |
| 290 | /// [`Self::transform_point3()`] and [`Self::transform_vector3()`]. |
| 291 | #[inline] |
| 292 | pub fn from_mat3(m: DMat3) -> Self { |
| 293 | Self::from_cols( |
| 294 | DVec4::from((m.x_axis, 0.0)), |
| 295 | DVec4::from((m.y_axis, 0.0)), |
| 296 | DVec4::from((m.z_axis, 0.0)), |
| 297 | DVec4::W, |
| 298 | ) |
| 299 | } |
| 300 | |
| 301 | /// Creates an affine transformation matrix from the given 3D `translation`. |
| 302 | /// |
| 303 | /// The resulting matrix can be used to transform 3D points and vectors. See |
| 304 | /// [`Self::transform_point3()`] and [`Self::transform_vector3()`]. |
| 305 | #[inline] |
| 306 | pub fn from_translation(translation: DVec3) -> Self { |
| 307 | Self::from_cols( |
| 308 | DVec4::X, |
| 309 | DVec4::Y, |
| 310 | DVec4::Z, |
| 311 | DVec4::new(translation.x, translation.y, translation.z, 1.0), |
| 312 | ) |
| 313 | } |
| 314 | |
| 315 | /// Creates an affine transformation matrix containing a 3D rotation around a normalized |
| 316 | /// rotation `axis` of `angle` (in radians). |
| 317 | /// |
| 318 | /// The resulting matrix can be used to transform 3D points and vectors. See |
| 319 | /// [`Self::transform_point3()`] and [`Self::transform_vector3()`]. |
| 320 | /// |
| 321 | /// # Panics |
| 322 | /// |
| 323 | /// Will panic if `axis` is not normalized when `glam_assert` is enabled. |
| 324 | #[inline] |
| 325 | pub fn from_axis_angle(axis: DVec3, angle: f64) -> Self { |
| 326 | glam_assert!(axis.is_normalized()); |
| 327 | |
| 328 | let (sin, cos) = angle.sin_cos(); |
| 329 | let axis_sin = axis.mul(sin); |
| 330 | let axis_sq = axis.mul(axis); |
| 331 | let omc = 1.0 - cos; |
| 332 | let xyomc = axis.x * axis.y * omc; |
| 333 | let xzomc = axis.x * axis.z * omc; |
| 334 | let yzomc = axis.y * axis.z * omc; |
| 335 | Self::from_cols( |
| 336 | DVec4::new( |
| 337 | axis_sq.x * omc + cos, |
| 338 | xyomc + axis_sin.z, |
| 339 | xzomc - axis_sin.y, |
| 340 | 0.0, |
| 341 | ), |
| 342 | DVec4::new( |
| 343 | xyomc - axis_sin.z, |
| 344 | axis_sq.y * omc + cos, |
| 345 | yzomc + axis_sin.x, |
| 346 | 0.0, |
| 347 | ), |
| 348 | DVec4::new( |
| 349 | xzomc + axis_sin.y, |
| 350 | yzomc - axis_sin.x, |
| 351 | axis_sq.z * omc + cos, |
| 352 | 0.0, |
| 353 | ), |
| 354 | DVec4::W, |
| 355 | ) |
| 356 | } |
| 357 | |
| 358 | #[inline] |
| 359 | /// Creates a affine transformation matrix containing a rotation from the given euler |
| 360 | /// rotation sequence and angles (in radians). |
| 361 | /// |
| 362 | /// The resulting matrix can be used to transform 3D points and vectors. See |
| 363 | /// [`Self::transform_point3()`] and [`Self::transform_vector3()`]. |
| 364 | pub fn from_euler(order: EulerRot, a: f64, b: f64, c: f64) -> Self { |
| 365 | let quat = DQuat::from_euler(order, a, b, c); |
| 366 | Self::from_quat(quat) |
| 367 | } |
| 368 | |
| 369 | /// Creates an affine transformation matrix containing a 3D rotation around the x axis of |
| 370 | /// `angle` (in radians). |
| 371 | /// |
| 372 | /// The resulting matrix can be used to transform 3D points and vectors. See |
| 373 | /// [`Self::transform_point3()`] and [`Self::transform_vector3()`]. |
| 374 | #[inline] |
| 375 | pub fn from_rotation_x(angle: f64) -> Self { |
| 376 | let (sina, cosa) = angle.sin_cos(); |
| 377 | Self::from_cols( |
| 378 | DVec4::X, |
| 379 | DVec4::new(0.0, cosa, sina, 0.0), |
| 380 | DVec4::new(0.0, -sina, cosa, 0.0), |
| 381 | DVec4::W, |
| 382 | ) |
| 383 | } |
| 384 | |
| 385 | /// Creates an affine transformation matrix containing a 3D rotation around the y axis of |
| 386 | /// `angle` (in radians). |
| 387 | /// |
| 388 | /// The resulting matrix can be used to transform 3D points and vectors. See |
| 389 | /// [`Self::transform_point3()`] and [`Self::transform_vector3()`]. |
| 390 | #[inline] |
| 391 | pub fn from_rotation_y(angle: f64) -> Self { |
| 392 | let (sina, cosa) = angle.sin_cos(); |
| 393 | Self::from_cols( |
| 394 | DVec4::new(cosa, 0.0, -sina, 0.0), |
| 395 | DVec4::Y, |
| 396 | DVec4::new(sina, 0.0, cosa, 0.0), |
| 397 | DVec4::W, |
| 398 | ) |
| 399 | } |
| 400 | |
| 401 | /// Creates an affine transformation matrix containing a 3D rotation around the z axis of |
| 402 | /// `angle` (in radians). |
| 403 | /// |
| 404 | /// The resulting matrix can be used to transform 3D points and vectors. See |
| 405 | /// [`Self::transform_point3()`] and [`Self::transform_vector3()`]. |
| 406 | #[inline] |
| 407 | pub fn from_rotation_z(angle: f64) -> Self { |
| 408 | let (sina, cosa) = angle.sin_cos(); |
| 409 | Self::from_cols( |
| 410 | DVec4::new(cosa, sina, 0.0, 0.0), |
| 411 | DVec4::new(-sina, cosa, 0.0, 0.0), |
| 412 | DVec4::Z, |
| 413 | DVec4::W, |
| 414 | ) |
| 415 | } |
| 416 | |
| 417 | /// Creates an affine transformation matrix containing the given 3D non-uniform `scale`. |
| 418 | /// |
| 419 | /// The resulting matrix can be used to transform 3D points and vectors. See |
| 420 | /// [`Self::transform_point3()`] and [`Self::transform_vector3()`]. |
| 421 | /// |
| 422 | /// # Panics |
| 423 | /// |
| 424 | /// Will panic if all elements of `scale` are zero when `glam_assert` is enabled. |
| 425 | #[inline] |
| 426 | pub fn from_scale(scale: DVec3) -> Self { |
| 427 | // Do not panic as long as any component is non-zero |
| 428 | glam_assert!(scale.cmpne(DVec3::ZERO).any()); |
| 429 | |
| 430 | Self::from_cols( |
| 431 | DVec4::new(scale.x, 0.0, 0.0, 0.0), |
| 432 | DVec4::new(0.0, scale.y, 0.0, 0.0), |
| 433 | DVec4::new(0.0, 0.0, scale.z, 0.0), |
| 434 | DVec4::W, |
| 435 | ) |
| 436 | } |
| 437 | |
| 438 | /// Creates a 4x4 matrix from the first 16 values in `slice`. |
| 439 | /// |
| 440 | /// # Panics |
| 441 | /// |
| 442 | /// Panics if `slice` is less than 16 elements long. |
| 443 | #[inline] |
| 444 | pub const fn from_cols_slice(slice: &[f64]) -> Self { |
| 445 | Self::new( |
| 446 | slice[0], slice[1], slice[2], slice[3], slice[4], slice[5], slice[6], slice[7], |
| 447 | slice[8], slice[9], slice[10], slice[11], slice[12], slice[13], slice[14], slice[15], |
| 448 | ) |
| 449 | } |
| 450 | |
| 451 | /// Writes the columns of `self` to the first 16 elements in `slice`. |
| 452 | /// |
| 453 | /// # Panics |
| 454 | /// |
| 455 | /// Panics if `slice` is less than 16 elements long. |
| 456 | #[inline] |
| 457 | pub fn write_cols_to_slice(self, slice: &mut [f64]) { |
| 458 | slice[0] = self.x_axis.x; |
| 459 | slice[1] = self.x_axis.y; |
| 460 | slice[2] = self.x_axis.z; |
| 461 | slice[3] = self.x_axis.w; |
| 462 | slice[4] = self.y_axis.x; |
| 463 | slice[5] = self.y_axis.y; |
| 464 | slice[6] = self.y_axis.z; |
| 465 | slice[7] = self.y_axis.w; |
| 466 | slice[8] = self.z_axis.x; |
| 467 | slice[9] = self.z_axis.y; |
| 468 | slice[10] = self.z_axis.z; |
| 469 | slice[11] = self.z_axis.w; |
| 470 | slice[12] = self.w_axis.x; |
| 471 | slice[13] = self.w_axis.y; |
| 472 | slice[14] = self.w_axis.z; |
| 473 | slice[15] = self.w_axis.w; |
| 474 | } |
| 475 | |
| 476 | /// Returns the matrix column for the given `index`. |
| 477 | /// |
| 478 | /// # Panics |
| 479 | /// |
| 480 | /// Panics if `index` is greater than 3. |
| 481 | #[inline] |
| 482 | pub fn col(&self, index: usize) -> DVec4 { |
| 483 | match index { |
| 484 | 0 => self.x_axis, |
| 485 | 1 => self.y_axis, |
| 486 | 2 => self.z_axis, |
| 487 | 3 => self.w_axis, |
| 488 | _ => panic!("index out of bounds"), |
| 489 | } |
| 490 | } |
| 491 | |
| 492 | /// Returns a mutable reference to the matrix column for the given `index`. |
| 493 | /// |
| 494 | /// # Panics |
| 495 | /// |
| 496 | /// Panics if `index` is greater than 3. |
| 497 | #[inline] |
| 498 | pub fn col_mut(&mut self, index: usize) -> &mut DVec4 { |
| 499 | match index { |
| 500 | 0 => &mut self.x_axis, |
| 501 | 1 => &mut self.y_axis, |
| 502 | 2 => &mut self.z_axis, |
| 503 | 3 => &mut self.w_axis, |
| 504 | _ => panic!("index out of bounds"), |
| 505 | } |
| 506 | } |
| 507 | |
| 508 | /// Returns the matrix row for the given `index`. |
| 509 | /// |
| 510 | /// # Panics |
| 511 | /// |
| 512 | /// Panics if `index` is greater than 3. |
| 513 | #[inline] |
| 514 | pub fn row(&self, index: usize) -> DVec4 { |
| 515 | match index { |
| 516 | 0 => DVec4::new(self.x_axis.x, self.y_axis.x, self.z_axis.x, self.w_axis.x), |
| 517 | 1 => DVec4::new(self.x_axis.y, self.y_axis.y, self.z_axis.y, self.w_axis.y), |
| 518 | 2 => DVec4::new(self.x_axis.z, self.y_axis.z, self.z_axis.z, self.w_axis.z), |
| 519 | 3 => DVec4::new(self.x_axis.w, self.y_axis.w, self.z_axis.w, self.w_axis.w), |
| 520 | _ => panic!("index out of bounds"), |
| 521 | } |
| 522 | } |
| 523 | |
| 524 | /// Returns `true` if, and only if, all elements are finite. |
| 525 | /// If any element is either `NaN`, positive or negative infinity, this will return `false`. |
| 526 | #[inline] |
| 527 | pub fn is_finite(&self) -> bool { |
| 528 | self.x_axis.is_finite() |
| 529 | && self.y_axis.is_finite() |
| 530 | && self.z_axis.is_finite() |
| 531 | && self.w_axis.is_finite() |
| 532 | } |
| 533 | |
| 534 | /// Returns `true` if any elements are `NaN`. |
| 535 | #[inline] |
| 536 | pub fn is_nan(&self) -> bool { |
| 537 | self.x_axis.is_nan() || self.y_axis.is_nan() || self.z_axis.is_nan() || self.w_axis.is_nan() |
| 538 | } |
| 539 | |
| 540 | /// Returns the transpose of `self`. |
| 541 | #[must_use] |
| 542 | #[inline] |
| 543 | pub fn transpose(&self) -> Self { |
| 544 | Self { |
| 545 | x_axis: DVec4::new(self.x_axis.x, self.y_axis.x, self.z_axis.x, self.w_axis.x), |
| 546 | y_axis: DVec4::new(self.x_axis.y, self.y_axis.y, self.z_axis.y, self.w_axis.y), |
| 547 | z_axis: DVec4::new(self.x_axis.z, self.y_axis.z, self.z_axis.z, self.w_axis.z), |
| 548 | w_axis: DVec4::new(self.x_axis.w, self.y_axis.w, self.z_axis.w, self.w_axis.w), |
| 549 | } |
| 550 | } |
| 551 | |
| 552 | /// Returns the determinant of `self`. |
| 553 | pub fn determinant(&self) -> f64 { |
| 554 | let (m00, m01, m02, m03) = self.x_axis.into(); |
| 555 | let (m10, m11, m12, m13) = self.y_axis.into(); |
| 556 | let (m20, m21, m22, m23) = self.z_axis.into(); |
| 557 | let (m30, m31, m32, m33) = self.w_axis.into(); |
| 558 | |
| 559 | let a2323 = m22 * m33 - m23 * m32; |
| 560 | let a1323 = m21 * m33 - m23 * m31; |
| 561 | let a1223 = m21 * m32 - m22 * m31; |
| 562 | let a0323 = m20 * m33 - m23 * m30; |
| 563 | let a0223 = m20 * m32 - m22 * m30; |
| 564 | let a0123 = m20 * m31 - m21 * m30; |
| 565 | |
| 566 | m00 * (m11 * a2323 - m12 * a1323 + m13 * a1223) |
| 567 | - m01 * (m10 * a2323 - m12 * a0323 + m13 * a0223) |
| 568 | + m02 * (m10 * a1323 - m11 * a0323 + m13 * a0123) |
| 569 | - m03 * (m10 * a1223 - m11 * a0223 + m12 * a0123) |
| 570 | } |
| 571 | |
| 572 | /// Returns the inverse of `self`. |
| 573 | /// |
| 574 | /// If the matrix is not invertible the returned matrix will be invalid. |
| 575 | /// |
| 576 | /// # Panics |
| 577 | /// |
| 578 | /// Will panic if the determinant of `self` is zero when `glam_assert` is enabled. |
| 579 | #[must_use] |
| 580 | pub fn inverse(&self) -> Self { |
| 581 | let (m00, m01, m02, m03) = self.x_axis.into(); |
| 582 | let (m10, m11, m12, m13) = self.y_axis.into(); |
| 583 | let (m20, m21, m22, m23) = self.z_axis.into(); |
| 584 | let (m30, m31, m32, m33) = self.w_axis.into(); |
| 585 | |
| 586 | let coef00 = m22 * m33 - m32 * m23; |
| 587 | let coef02 = m12 * m33 - m32 * m13; |
| 588 | let coef03 = m12 * m23 - m22 * m13; |
| 589 | |
| 590 | let coef04 = m21 * m33 - m31 * m23; |
| 591 | let coef06 = m11 * m33 - m31 * m13; |
| 592 | let coef07 = m11 * m23 - m21 * m13; |
| 593 | |
| 594 | let coef08 = m21 * m32 - m31 * m22; |
| 595 | let coef10 = m11 * m32 - m31 * m12; |
| 596 | let coef11 = m11 * m22 - m21 * m12; |
| 597 | |
| 598 | let coef12 = m20 * m33 - m30 * m23; |
| 599 | let coef14 = m10 * m33 - m30 * m13; |
| 600 | let coef15 = m10 * m23 - m20 * m13; |
| 601 | |
| 602 | let coef16 = m20 * m32 - m30 * m22; |
| 603 | let coef18 = m10 * m32 - m30 * m12; |
| 604 | let coef19 = m10 * m22 - m20 * m12; |
| 605 | |
| 606 | let coef20 = m20 * m31 - m30 * m21; |
| 607 | let coef22 = m10 * m31 - m30 * m11; |
| 608 | let coef23 = m10 * m21 - m20 * m11; |
| 609 | |
| 610 | let fac0 = DVec4::new(coef00, coef00, coef02, coef03); |
| 611 | let fac1 = DVec4::new(coef04, coef04, coef06, coef07); |
| 612 | let fac2 = DVec4::new(coef08, coef08, coef10, coef11); |
| 613 | let fac3 = DVec4::new(coef12, coef12, coef14, coef15); |
| 614 | let fac4 = DVec4::new(coef16, coef16, coef18, coef19); |
| 615 | let fac5 = DVec4::new(coef20, coef20, coef22, coef23); |
| 616 | |
| 617 | let vec0 = DVec4::new(m10, m00, m00, m00); |
| 618 | let vec1 = DVec4::new(m11, m01, m01, m01); |
| 619 | let vec2 = DVec4::new(m12, m02, m02, m02); |
| 620 | let vec3 = DVec4::new(m13, m03, m03, m03); |
| 621 | |
| 622 | let inv0 = vec1.mul(fac0).sub(vec2.mul(fac1)).add(vec3.mul(fac2)); |
| 623 | let inv1 = vec0.mul(fac0).sub(vec2.mul(fac3)).add(vec3.mul(fac4)); |
| 624 | let inv2 = vec0.mul(fac1).sub(vec1.mul(fac3)).add(vec3.mul(fac5)); |
| 625 | let inv3 = vec0.mul(fac2).sub(vec1.mul(fac4)).add(vec2.mul(fac5)); |
| 626 | |
| 627 | let sign_a = DVec4::new(1.0, -1.0, 1.0, -1.0); |
| 628 | let sign_b = DVec4::new(-1.0, 1.0, -1.0, 1.0); |
| 629 | |
| 630 | let inverse = Self::from_cols( |
| 631 | inv0.mul(sign_a), |
| 632 | inv1.mul(sign_b), |
| 633 | inv2.mul(sign_a), |
| 634 | inv3.mul(sign_b), |
| 635 | ); |
| 636 | |
| 637 | let col0 = DVec4::new( |
| 638 | inverse.x_axis.x, |
| 639 | inverse.y_axis.x, |
| 640 | inverse.z_axis.x, |
| 641 | inverse.w_axis.x, |
| 642 | ); |
| 643 | |
| 644 | let dot0 = self.x_axis.mul(col0); |
| 645 | let dot1 = dot0.x + dot0.y + dot0.z + dot0.w; |
| 646 | |
| 647 | glam_assert!(dot1 != 0.0); |
| 648 | |
| 649 | let rcp_det = dot1.recip(); |
| 650 | inverse.mul(rcp_det) |
| 651 | } |
| 652 | |
| 653 | /// Creates a left-handed view matrix using a camera position, an up direction, and a facing |
| 654 | /// direction. |
| 655 | /// |
| 656 | /// For a view coordinate system with `+X=right`, `+Y=up` and `+Z=forward`. |
| 657 | #[inline] |
| 658 | pub fn look_to_lh(eye: DVec3, dir: DVec3, up: DVec3) -> Self { |
| 659 | Self::look_to_rh(eye, -dir, up) |
| 660 | } |
| 661 | |
| 662 | /// Creates a right-handed view matrix using a camera position, an up direction, and a facing |
| 663 | /// direction. |
| 664 | /// |
| 665 | /// For a view coordinate system with `+X=right`, `+Y=up` and `+Z=back`. |
| 666 | #[inline] |
| 667 | pub fn look_to_rh(eye: DVec3, dir: DVec3, up: DVec3) -> Self { |
| 668 | let f = dir.normalize(); |
| 669 | let s = f.cross(up).normalize(); |
| 670 | let u = s.cross(f); |
| 671 | |
| 672 | Self::from_cols( |
| 673 | DVec4::new(s.x, u.x, -f.x, 0.0), |
| 674 | DVec4::new(s.y, u.y, -f.y, 0.0), |
| 675 | DVec4::new(s.z, u.z, -f.z, 0.0), |
| 676 | DVec4::new(-eye.dot(s), -eye.dot(u), eye.dot(f), 1.0), |
| 677 | ) |
| 678 | } |
| 679 | |
| 680 | /// Creates a left-handed view matrix using a camera position, an up direction, and a focal |
| 681 | /// point. |
| 682 | /// For a view coordinate system with `+X=right`, `+Y=up` and `+Z=forward`. |
| 683 | /// |
| 684 | /// # Panics |
| 685 | /// |
| 686 | /// Will panic if `up` is not normalized when `glam_assert` is enabled. |
| 687 | #[inline] |
| 688 | pub fn look_at_lh(eye: DVec3, center: DVec3, up: DVec3) -> Self { |
| 689 | glam_assert!(up.is_normalized()); |
| 690 | Self::look_to_lh(eye, center.sub(eye), up) |
| 691 | } |
| 692 | |
| 693 | /// Creates a right-handed view matrix using a camera position, an up direction, and a focal |
| 694 | /// point. |
| 695 | /// For a view coordinate system with `+X=right`, `+Y=up` and `+Z=back`. |
| 696 | /// |
| 697 | /// # Panics |
| 698 | /// |
| 699 | /// Will panic if `up` is not normalized when `glam_assert` is enabled. |
| 700 | #[inline] |
| 701 | pub fn look_at_rh(eye: DVec3, center: DVec3, up: DVec3) -> Self { |
| 702 | glam_assert!(up.is_normalized()); |
| 703 | Self::look_to_rh(eye, center.sub(eye), up) |
| 704 | } |
| 705 | |
| 706 | /// Creates a right-handed perspective projection matrix with [-1,1] depth range. |
| 707 | /// This is the same as the OpenGL `gluPerspective` function. |
| 708 | /// See <https://www.khronos.org/registry/OpenGL-Refpages/gl2.1/xhtml/gluPerspective.xml> |
| 709 | #[inline] |
| 710 | pub fn perspective_rh_gl( |
| 711 | fov_y_radians: f64, |
| 712 | aspect_ratio: f64, |
| 713 | z_near: f64, |
| 714 | z_far: f64, |
| 715 | ) -> Self { |
| 716 | let inv_length = 1.0 / (z_near - z_far); |
| 717 | let f = 1.0 / (0.5 * fov_y_radians).tan(); |
| 718 | let a = f / aspect_ratio; |
| 719 | let b = (z_near + z_far) * inv_length; |
| 720 | let c = (2.0 * z_near * z_far) * inv_length; |
| 721 | Self::from_cols( |
| 722 | DVec4::new(a, 0.0, 0.0, 0.0), |
| 723 | DVec4::new(0.0, f, 0.0, 0.0), |
| 724 | DVec4::new(0.0, 0.0, b, -1.0), |
| 725 | DVec4::new(0.0, 0.0, c, 0.0), |
| 726 | ) |
| 727 | } |
| 728 | |
| 729 | /// Creates a left-handed perspective projection matrix with `[0,1]` depth range. |
| 730 | /// |
| 731 | /// # Panics |
| 732 | /// |
| 733 | /// Will panic if `z_near` or `z_far` are less than or equal to zero when `glam_assert` is |
| 734 | /// enabled. |
| 735 | #[inline] |
| 736 | pub fn perspective_lh(fov_y_radians: f64, aspect_ratio: f64, z_near: f64, z_far: f64) -> Self { |
| 737 | glam_assert!(z_near > 0.0 && z_far > 0.0); |
| 738 | let (sin_fov, cos_fov) = (0.5 * fov_y_radians).sin_cos(); |
| 739 | let h = cos_fov / sin_fov; |
| 740 | let w = h / aspect_ratio; |
| 741 | let r = z_far / (z_far - z_near); |
| 742 | Self::from_cols( |
| 743 | DVec4::new(w, 0.0, 0.0, 0.0), |
| 744 | DVec4::new(0.0, h, 0.0, 0.0), |
| 745 | DVec4::new(0.0, 0.0, r, 1.0), |
| 746 | DVec4::new(0.0, 0.0, -r * z_near, 0.0), |
| 747 | ) |
| 748 | } |
| 749 | |
| 750 | /// Creates a right-handed perspective projection matrix with `[0,1]` depth range. |
| 751 | /// |
| 752 | /// # Panics |
| 753 | /// |
| 754 | /// Will panic if `z_near` or `z_far` are less than or equal to zero when `glam_assert` is |
| 755 | /// enabled. |
| 756 | #[inline] |
| 757 | pub fn perspective_rh(fov_y_radians: f64, aspect_ratio: f64, z_near: f64, z_far: f64) -> Self { |
| 758 | glam_assert!(z_near > 0.0 && z_far > 0.0); |
| 759 | let (sin_fov, cos_fov) = (0.5 * fov_y_radians).sin_cos(); |
| 760 | let h = cos_fov / sin_fov; |
| 761 | let w = h / aspect_ratio; |
| 762 | let r = z_far / (z_near - z_far); |
| 763 | Self::from_cols( |
| 764 | DVec4::new(w, 0.0, 0.0, 0.0), |
| 765 | DVec4::new(0.0, h, 0.0, 0.0), |
| 766 | DVec4::new(0.0, 0.0, r, -1.0), |
| 767 | DVec4::new(0.0, 0.0, r * z_near, 0.0), |
| 768 | ) |
| 769 | } |
| 770 | |
| 771 | /// Creates an infinite left-handed perspective projection matrix with `[0,1]` depth range. |
| 772 | /// |
| 773 | /// # Panics |
| 774 | /// |
| 775 | /// Will panic if `z_near` is less than or equal to zero when `glam_assert` is enabled. |
| 776 | #[inline] |
| 777 | pub fn perspective_infinite_lh(fov_y_radians: f64, aspect_ratio: f64, z_near: f64) -> Self { |
| 778 | glam_assert!(z_near > 0.0); |
| 779 | let (sin_fov, cos_fov) = (0.5 * fov_y_radians).sin_cos(); |
| 780 | let h = cos_fov / sin_fov; |
| 781 | let w = h / aspect_ratio; |
| 782 | Self::from_cols( |
| 783 | DVec4::new(w, 0.0, 0.0, 0.0), |
| 784 | DVec4::new(0.0, h, 0.0, 0.0), |
| 785 | DVec4::new(0.0, 0.0, 1.0, 1.0), |
| 786 | DVec4::new(0.0, 0.0, -z_near, 0.0), |
| 787 | ) |
| 788 | } |
| 789 | |
| 790 | /// Creates an infinite left-handed perspective projection matrix with `[0,1]` depth range. |
| 791 | /// |
| 792 | /// # Panics |
| 793 | /// |
| 794 | /// Will panic if `z_near` is less than or equal to zero when `glam_assert` is enabled. |
| 795 | #[inline] |
| 796 | pub fn perspective_infinite_reverse_lh( |
| 797 | fov_y_radians: f64, |
| 798 | aspect_ratio: f64, |
| 799 | z_near: f64, |
| 800 | ) -> Self { |
| 801 | glam_assert!(z_near > 0.0); |
| 802 | let (sin_fov, cos_fov) = (0.5 * fov_y_radians).sin_cos(); |
| 803 | let h = cos_fov / sin_fov; |
| 804 | let w = h / aspect_ratio; |
| 805 | Self::from_cols( |
| 806 | DVec4::new(w, 0.0, 0.0, 0.0), |
| 807 | DVec4::new(0.0, h, 0.0, 0.0), |
| 808 | DVec4::new(0.0, 0.0, 0.0, 1.0), |
| 809 | DVec4::new(0.0, 0.0, z_near, 0.0), |
| 810 | ) |
| 811 | } |
| 812 | |
| 813 | /// Creates an infinite right-handed perspective projection matrix with |
| 814 | /// `[0,1]` depth range. |
| 815 | #[inline] |
| 816 | pub fn perspective_infinite_rh(fov_y_radians: f64, aspect_ratio: f64, z_near: f64) -> Self { |
| 817 | glam_assert!(z_near > 0.0); |
| 818 | let f = 1.0 / (0.5 * fov_y_radians).tan(); |
| 819 | Self::from_cols( |
| 820 | DVec4::new(f / aspect_ratio, 0.0, 0.0, 0.0), |
| 821 | DVec4::new(0.0, f, 0.0, 0.0), |
| 822 | DVec4::new(0.0, 0.0, -1.0, -1.0), |
| 823 | DVec4::new(0.0, 0.0, -z_near, 0.0), |
| 824 | ) |
| 825 | } |
| 826 | |
| 827 | /// Creates an infinite reverse right-handed perspective projection matrix |
| 828 | /// with `[0,1]` depth range. |
| 829 | #[inline] |
| 830 | pub fn perspective_infinite_reverse_rh( |
| 831 | fov_y_radians: f64, |
| 832 | aspect_ratio: f64, |
| 833 | z_near: f64, |
| 834 | ) -> Self { |
| 835 | glam_assert!(z_near > 0.0); |
| 836 | let f = 1.0 / (0.5 * fov_y_radians).tan(); |
| 837 | Self::from_cols( |
| 838 | DVec4::new(f / aspect_ratio, 0.0, 0.0, 0.0), |
| 839 | DVec4::new(0.0, f, 0.0, 0.0), |
| 840 | DVec4::new(0.0, 0.0, 0.0, -1.0), |
| 841 | DVec4::new(0.0, 0.0, z_near, 0.0), |
| 842 | ) |
| 843 | } |
| 844 | |
| 845 | /// Creates a right-handed orthographic projection matrix with `[-1,1]` depth |
| 846 | /// range. This is the same as the OpenGL `glOrtho` function in OpenGL. |
| 847 | /// See |
| 848 | /// <https://www.khronos.org/registry/OpenGL-Refpages/gl2.1/xhtml/glOrtho.xml> |
| 849 | #[inline] |
| 850 | pub fn orthographic_rh_gl( |
| 851 | left: f64, |
| 852 | right: f64, |
| 853 | bottom: f64, |
| 854 | top: f64, |
| 855 | near: f64, |
| 856 | far: f64, |
| 857 | ) -> Self { |
| 858 | let a = 2.0 / (right - left); |
| 859 | let b = 2.0 / (top - bottom); |
| 860 | let c = -2.0 / (far - near); |
| 861 | let tx = -(right + left) / (right - left); |
| 862 | let ty = -(top + bottom) / (top - bottom); |
| 863 | let tz = -(far + near) / (far - near); |
| 864 | |
| 865 | Self::from_cols( |
| 866 | DVec4::new(a, 0.0, 0.0, 0.0), |
| 867 | DVec4::new(0.0, b, 0.0, 0.0), |
| 868 | DVec4::new(0.0, 0.0, c, 0.0), |
| 869 | DVec4::new(tx, ty, tz, 1.0), |
| 870 | ) |
| 871 | } |
| 872 | |
| 873 | /// Creates a left-handed orthographic projection matrix with `[0,1]` depth range. |
| 874 | #[inline] |
| 875 | pub fn orthographic_lh( |
| 876 | left: f64, |
| 877 | right: f64, |
| 878 | bottom: f64, |
| 879 | top: f64, |
| 880 | near: f64, |
| 881 | far: f64, |
| 882 | ) -> Self { |
| 883 | let rcp_width = 1.0 / (right - left); |
| 884 | let rcp_height = 1.0 / (top - bottom); |
| 885 | let r = 1.0 / (far - near); |
| 886 | Self::from_cols( |
| 887 | DVec4::new(rcp_width + rcp_width, 0.0, 0.0, 0.0), |
| 888 | DVec4::new(0.0, rcp_height + rcp_height, 0.0, 0.0), |
| 889 | DVec4::new(0.0, 0.0, r, 0.0), |
| 890 | DVec4::new( |
| 891 | -(left + right) * rcp_width, |
| 892 | -(top + bottom) * rcp_height, |
| 893 | -r * near, |
| 894 | 1.0, |
| 895 | ), |
| 896 | ) |
| 897 | } |
| 898 | |
| 899 | /// Creates a right-handed orthographic projection matrix with `[0,1]` depth range. |
| 900 | #[inline] |
| 901 | pub fn orthographic_rh( |
| 902 | left: f64, |
| 903 | right: f64, |
| 904 | bottom: f64, |
| 905 | top: f64, |
| 906 | near: f64, |
| 907 | far: f64, |
| 908 | ) -> Self { |
| 909 | let rcp_width = 1.0 / (right - left); |
| 910 | let rcp_height = 1.0 / (top - bottom); |
| 911 | let r = 1.0 / (near - far); |
| 912 | Self::from_cols( |
| 913 | DVec4::new(rcp_width + rcp_width, 0.0, 0.0, 0.0), |
| 914 | DVec4::new(0.0, rcp_height + rcp_height, 0.0, 0.0), |
| 915 | DVec4::new(0.0, 0.0, r, 0.0), |
| 916 | DVec4::new( |
| 917 | -(left + right) * rcp_width, |
| 918 | -(top + bottom) * rcp_height, |
| 919 | r * near, |
| 920 | 1.0, |
| 921 | ), |
| 922 | ) |
| 923 | } |
| 924 | |
| 925 | /// Transforms the given 3D vector as a point, applying perspective correction. |
| 926 | /// |
| 927 | /// This is the equivalent of multiplying the 3D vector as a 4D vector where `w` is `1.0`. |
| 928 | /// The perspective divide is performed meaning the resulting 3D vector is divided by `w`. |
| 929 | /// |
| 930 | /// This method assumes that `self` contains a projective transform. |
| 931 | #[inline] |
| 932 | pub fn project_point3(&self, rhs: DVec3) -> DVec3 { |
| 933 | let mut res = self.x_axis.mul(rhs.x); |
| 934 | res = self.y_axis.mul(rhs.y).add(res); |
| 935 | res = self.z_axis.mul(rhs.z).add(res); |
| 936 | res = self.w_axis.add(res); |
| 937 | res = res.mul(res.wwww().recip()); |
| 938 | res.xyz() |
| 939 | } |
| 940 | |
| 941 | /// Transforms the given 3D vector as a point. |
| 942 | /// |
| 943 | /// This is the equivalent of multiplying the 3D vector as a 4D vector where `w` is |
| 944 | /// `1.0`. |
| 945 | /// |
| 946 | /// This method assumes that `self` contains a valid affine transform. It does not perform |
| 947 | /// a persective divide, if `self` contains a perspective transform, or if you are unsure, |
| 948 | /// the [`Self::project_point3()`] method should be used instead. |
| 949 | /// |
| 950 | /// # Panics |
| 951 | /// |
| 952 | /// Will panic if the 3rd row of `self` is not `(0, 0, 0, 1)` when `glam_assert` is enabled. |
| 953 | #[inline] |
| 954 | pub fn transform_point3(&self, rhs: DVec3) -> DVec3 { |
| 955 | glam_assert!(self.row(3).abs_diff_eq(DVec4::W, 1e-6)); |
| 956 | let mut res = self.x_axis.mul(rhs.x); |
| 957 | res = self.y_axis.mul(rhs.y).add(res); |
| 958 | res = self.z_axis.mul(rhs.z).add(res); |
| 959 | res = self.w_axis.add(res); |
| 960 | res.xyz() |
| 961 | } |
| 962 | |
| 963 | /// Transforms the give 3D vector as a direction. |
| 964 | /// |
| 965 | /// This is the equivalent of multiplying the 3D vector as a 4D vector where `w` is |
| 966 | /// `0.0`. |
| 967 | /// |
| 968 | /// This method assumes that `self` contains a valid affine transform. |
| 969 | /// |
| 970 | /// # Panics |
| 971 | /// |
| 972 | /// Will panic if the 3rd row of `self` is not `(0, 0, 0, 1)` when `glam_assert` is enabled. |
| 973 | #[inline] |
| 974 | pub fn transform_vector3(&self, rhs: DVec3) -> DVec3 { |
| 975 | glam_assert!(self.row(3).abs_diff_eq(DVec4::W, 1e-6)); |
| 976 | let mut res = self.x_axis.mul(rhs.x); |
| 977 | res = self.y_axis.mul(rhs.y).add(res); |
| 978 | res = self.z_axis.mul(rhs.z).add(res); |
| 979 | res.xyz() |
| 980 | } |
| 981 | |
| 982 | /// Transforms a 4D vector. |
| 983 | #[inline] |
| 984 | pub fn mul_vec4(&self, rhs: DVec4) -> DVec4 { |
| 985 | let mut res = self.x_axis.mul(rhs.x); |
| 986 | res = res.add(self.y_axis.mul(rhs.y)); |
| 987 | res = res.add(self.z_axis.mul(rhs.z)); |
| 988 | res = res.add(self.w_axis.mul(rhs.w)); |
| 989 | res |
| 990 | } |
| 991 | |
| 992 | /// Multiplies two 4x4 matrices. |
| 993 | #[inline] |
| 994 | pub fn mul_mat4(&self, rhs: &Self) -> Self { |
| 995 | Self::from_cols( |
| 996 | self.mul(rhs.x_axis), |
| 997 | self.mul(rhs.y_axis), |
| 998 | self.mul(rhs.z_axis), |
| 999 | self.mul(rhs.w_axis), |
| 1000 | ) |
| 1001 | } |
| 1002 | |
| 1003 | /// Adds two 4x4 matrices. |
| 1004 | #[inline] |
| 1005 | pub fn add_mat4(&self, rhs: &Self) -> Self { |
| 1006 | Self::from_cols( |
| 1007 | self.x_axis.add(rhs.x_axis), |
| 1008 | self.y_axis.add(rhs.y_axis), |
| 1009 | self.z_axis.add(rhs.z_axis), |
| 1010 | self.w_axis.add(rhs.w_axis), |
| 1011 | ) |
| 1012 | } |
| 1013 | |
| 1014 | /// Subtracts two 4x4 matrices. |
| 1015 | #[inline] |
| 1016 | pub fn sub_mat4(&self, rhs: &Self) -> Self { |
| 1017 | Self::from_cols( |
| 1018 | self.x_axis.sub(rhs.x_axis), |
| 1019 | self.y_axis.sub(rhs.y_axis), |
| 1020 | self.z_axis.sub(rhs.z_axis), |
| 1021 | self.w_axis.sub(rhs.w_axis), |
| 1022 | ) |
| 1023 | } |
| 1024 | |
| 1025 | /// Multiplies a 4x4 matrix by a scalar. |
| 1026 | #[inline] |
| 1027 | pub fn mul_scalar(&self, rhs: f64) -> Self { |
| 1028 | Self::from_cols( |
| 1029 | self.x_axis.mul(rhs), |
| 1030 | self.y_axis.mul(rhs), |
| 1031 | self.z_axis.mul(rhs), |
| 1032 | self.w_axis.mul(rhs), |
| 1033 | ) |
| 1034 | } |
| 1035 | |
| 1036 | /// Returns true if the absolute difference of all elements between `self` and `rhs` |
| 1037 | /// is less than or equal to `max_abs_diff`. |
| 1038 | /// |
| 1039 | /// This can be used to compare if two matrices contain similar elements. It works best |
| 1040 | /// when comparing with a known value. The `max_abs_diff` that should be used used |
| 1041 | /// depends on the values being compared against. |
| 1042 | /// |
| 1043 | /// For more see |
| 1044 | /// [comparing floating point numbers](https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/). |
| 1045 | #[inline] |
| 1046 | pub fn abs_diff_eq(&self, rhs: Self, max_abs_diff: f64) -> bool { |
| 1047 | self.x_axis.abs_diff_eq(rhs.x_axis, max_abs_diff) |
| 1048 | && self.y_axis.abs_diff_eq(rhs.y_axis, max_abs_diff) |
| 1049 | && self.z_axis.abs_diff_eq(rhs.z_axis, max_abs_diff) |
| 1050 | && self.w_axis.abs_diff_eq(rhs.w_axis, max_abs_diff) |
| 1051 | } |
| 1052 | |
| 1053 | #[inline] |
| 1054 | pub fn as_mat4(&self) -> Mat4 { |
| 1055 | Mat4::from_cols( |
| 1056 | self.x_axis.as_vec4(), |
| 1057 | self.y_axis.as_vec4(), |
| 1058 | self.z_axis.as_vec4(), |
| 1059 | self.w_axis.as_vec4(), |
| 1060 | ) |
| 1061 | } |
| 1062 | } |
| 1063 | |
| 1064 | impl Default for DMat4 { |
| 1065 | #[inline] |
| 1066 | fn default() -> Self { |
| 1067 | Self::IDENTITY |
| 1068 | } |
| 1069 | } |
| 1070 | |
| 1071 | impl Add<DMat4> for DMat4 { |
| 1072 | type Output = Self; |
| 1073 | #[inline] |
| 1074 | fn add(self, rhs: Self) -> Self::Output { |
| 1075 | self.add_mat4(&rhs) |
| 1076 | } |
| 1077 | } |
| 1078 | |
| 1079 | impl AddAssign<DMat4> for DMat4 { |
| 1080 | #[inline] |
| 1081 | fn add_assign(&mut self, rhs: Self) { |
| 1082 | *self = self.add_mat4(&rhs); |
| 1083 | } |
| 1084 | } |
| 1085 | |
| 1086 | impl Sub<DMat4> for DMat4 { |
| 1087 | type Output = Self; |
| 1088 | #[inline] |
| 1089 | fn sub(self, rhs: Self) -> Self::Output { |
| 1090 | self.sub_mat4(&rhs) |
| 1091 | } |
| 1092 | } |
| 1093 | |
| 1094 | impl SubAssign<DMat4> for DMat4 { |
| 1095 | #[inline] |
| 1096 | fn sub_assign(&mut self, rhs: Self) { |
| 1097 | *self = self.sub_mat4(&rhs); |
| 1098 | } |
| 1099 | } |
| 1100 | |
| 1101 | impl Neg for DMat4 { |
| 1102 | type Output = Self; |
| 1103 | #[inline] |
| 1104 | fn neg(self) -> Self::Output { |
| 1105 | Self::from_cols( |
| 1106 | self.x_axis.neg(), |
| 1107 | self.y_axis.neg(), |
| 1108 | self.z_axis.neg(), |
| 1109 | self.w_axis.neg(), |
| 1110 | ) |
| 1111 | } |
| 1112 | } |
| 1113 | |
| 1114 | impl Mul<DMat4> for DMat4 { |
| 1115 | type Output = Self; |
| 1116 | #[inline] |
| 1117 | fn mul(self, rhs: Self) -> Self::Output { |
| 1118 | self.mul_mat4(&rhs) |
| 1119 | } |
| 1120 | } |
| 1121 | |
| 1122 | impl MulAssign<DMat4> for DMat4 { |
| 1123 | #[inline] |
| 1124 | fn mul_assign(&mut self, rhs: Self) { |
| 1125 | *self = self.mul_mat4(&rhs); |
| 1126 | } |
| 1127 | } |
| 1128 | |
| 1129 | impl Mul<DVec4> for DMat4 { |
| 1130 | type Output = DVec4; |
| 1131 | #[inline] |
| 1132 | fn mul(self, rhs: DVec4) -> Self::Output { |
| 1133 | self.mul_vec4(rhs) |
| 1134 | } |
| 1135 | } |
| 1136 | |
| 1137 | impl Mul<DMat4> for f64 { |
| 1138 | type Output = DMat4; |
| 1139 | #[inline] |
| 1140 | fn mul(self, rhs: DMat4) -> Self::Output { |
| 1141 | rhs.mul_scalar(self) |
| 1142 | } |
| 1143 | } |
| 1144 | |
| 1145 | impl Mul<f64> for DMat4 { |
| 1146 | type Output = Self; |
| 1147 | #[inline] |
| 1148 | fn mul(self, rhs: f64) -> Self::Output { |
| 1149 | self.mul_scalar(rhs) |
| 1150 | } |
| 1151 | } |
| 1152 | |
| 1153 | impl MulAssign<f64> for DMat4 { |
| 1154 | #[inline] |
| 1155 | fn mul_assign(&mut self, rhs: f64) { |
| 1156 | *self = self.mul_scalar(rhs); |
| 1157 | } |
| 1158 | } |
| 1159 | |
| 1160 | impl Sum<Self> for DMat4 { |
| 1161 | fn sum<I>(iter: I) -> Self |
| 1162 | where |
| 1163 | I: Iterator<Item = Self>, |
| 1164 | { |
| 1165 | iter.fold(Self::ZERO, Self::add) |
| 1166 | } |
| 1167 | } |
| 1168 | |
| 1169 | impl<'a> Sum<&'a Self> for DMat4 { |
| 1170 | fn sum<I>(iter: I) -> Self |
| 1171 | where |
| 1172 | I: Iterator<Item = &'a Self>, |
| 1173 | { |
| 1174 | iter.fold(Self::ZERO, |a, &b| Self::add(a, b)) |
| 1175 | } |
| 1176 | } |
| 1177 | |
| 1178 | impl Product for DMat4 { |
| 1179 | fn product<I>(iter: I) -> Self |
| 1180 | where |
| 1181 | I: Iterator<Item = Self>, |
| 1182 | { |
| 1183 | iter.fold(Self::IDENTITY, Self::mul) |
| 1184 | } |
| 1185 | } |
| 1186 | |
| 1187 | impl<'a> Product<&'a Self> for DMat4 { |
| 1188 | fn product<I>(iter: I) -> Self |
| 1189 | where |
| 1190 | I: Iterator<Item = &'a Self>, |
| 1191 | { |
| 1192 | iter.fold(Self::IDENTITY, |a, &b| Self::mul(a, b)) |
| 1193 | } |
| 1194 | } |
| 1195 | |
| 1196 | impl PartialEq for DMat4 { |
| 1197 | #[inline] |
| 1198 | fn eq(&self, rhs: &Self) -> bool { |
| 1199 | self.x_axis.eq(&rhs.x_axis) |
| 1200 | && self.y_axis.eq(&rhs.y_axis) |
| 1201 | && self.z_axis.eq(&rhs.z_axis) |
| 1202 | && self.w_axis.eq(&rhs.w_axis) |
| 1203 | } |
| 1204 | } |
| 1205 | |
| 1206 | #[cfg(not(target_arch = "spirv"))] |
| 1207 | impl AsRef<[f64; 16]> for DMat4 { |
| 1208 | #[inline] |
| 1209 | fn as_ref(&self) -> &[f64; 16] { |
| 1210 | unsafe { &*(self as *const Self as *const [f64; 16]) } |
| 1211 | } |
| 1212 | } |
| 1213 | |
| 1214 | #[cfg(not(target_arch = "spirv"))] |
| 1215 | impl AsMut<[f64; 16]> for DMat4 { |
| 1216 | #[inline] |
| 1217 | fn as_mut(&mut self) -> &mut [f64; 16] { |
| 1218 | unsafe { &mut *(self as *mut Self as *mut [f64; 16]) } |
| 1219 | } |
| 1220 | } |
| 1221 | |
| 1222 | #[cfg(not(target_arch = "spirv"))] |
| 1223 | impl fmt::Debug for DMat4 { |
| 1224 | fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 1225 | fmt.debug_struct(stringify!(DMat4)) |
| 1226 | .field("x_axis", &self.x_axis) |
| 1227 | .field("y_axis", &self.y_axis) |
| 1228 | .field("z_axis", &self.z_axis) |
| 1229 | .field("w_axis", &self.w_axis) |
| 1230 | .finish() |
| 1231 | } |
| 1232 | } |
| 1233 | |
| 1234 | #[cfg(not(target_arch = "spirv"))] |
| 1235 | impl fmt::Display for DMat4 { |
| 1236 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 1237 | write!( |
| 1238 | f, |
| 1239 | "[{}, {}, {}, {}]", |
| 1240 | self.x_axis, self.y_axis, self.z_axis, self.w_axis |
| 1241 | ) |
| 1242 | } |
| 1243 | } |