Debian Codegraph @

debian/missing-sources/glam-0.21.3/src/f32/wasm32/mat4.rs

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