Debian Codegraph @

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

raw
1 // Generated from mat.rs.tera template. Edit the template, not the generated file.
2
3 use crate::{coresimd::*, swizzles::*, 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::simd::{Which::*, *};
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 = simd_swizzle!(
543 self.x_axis.0,
544 self.y_axis.0,
545 [First(0), First(1), Second(0), Second(1)]
546 );
547 let tmp1 = simd_swizzle!(
548 self.x_axis.0,
549 self.y_axis.0,
550 [First(2), First(3), Second(2), Second(3)]
551 );
552 let tmp2 = simd_swizzle!(
553 self.z_axis.0,
554 self.w_axis.0,
555 [First(0), First(1), Second(0), Second(1)]
556 );
557 let tmp3 = simd_swizzle!(
558 self.z_axis.0,
559 self.w_axis.0,
560 [First(2), First(3), Second(2), Second(3)]
561 );
562
563 Self {
564 x_axis: Vec4(simd_swizzle!(
565 tmp0,
566 tmp2,
567 [First(0), First(2), Second(0), Second(2)]
568 )),
569 y_axis: Vec4(simd_swizzle!(
570 tmp0,
571 tmp2,
572 [First(1), First(3), Second(1), Second(3)]
573 )),
574 z_axis: Vec4(simd_swizzle!(
575 tmp1,
576 tmp3,
577 [First(0), First(2), Second(0), Second(2)]
578 )),
579 w_axis: Vec4(simd_swizzle!(
580 tmp1,
581 tmp3,
582 [First(1), First(3), Second(1), Second(3)]
583 )),
584 }
585 }
586
587 /// Returns the determinant of `self`.
588 pub fn determinant(&self) -> f32 {
589 // Based on https://github.com/g-truc/glm `glm_mat4_determinant`
590 let swp2a = simd_swizzle!(self.z_axis.0, [2, 1, 1, 0]);
591 let swp3a = simd_swizzle!(self.w_axis.0, [3, 3, 2, 3]);
592 let swp2b = simd_swizzle!(self.z_axis.0, [3, 3, 2, 3]);
593 let swp3b = simd_swizzle!(self.w_axis.0, [2, 1, 1, 0]);
594 let swp2c = simd_swizzle!(self.z_axis.0, [2, 1, 0, 0]);
595 let swp3c = simd_swizzle!(self.w_axis.0, [0, 0, 2, 1]);
596
597 let mula = swp2a * swp3a;
598 let mulb = swp2b * swp3b;
599 let mulc = swp2c * swp3c;
600 let sube = mula - mulb;
601 let subf = simd_swizzle!(mulc, [2, 3, 2, 3]) - mulc;
602
603 let subfaca = simd_swizzle!(sube, [0, 0, 1, 2]);
604 let swpfaca = simd_swizzle!(self.y_axis.0, [1, 0, 0, 0]);
605 let mulfaca = swpfaca * subfaca;
606
607 let subtmpb = simd_swizzle!(sube, subf, [First(1), First(3), Second(0), Second(0)]);
608 let subfacb = simd_swizzle!(subtmpb, [0, 1, 1, 3]);
609 let swpfacb = simd_swizzle!(self.y_axis.0, [2, 2, 1, 1]);
610 let mulfacb = swpfacb * subfacb;
611
612 let subres = mulfaca - mulfacb;
613 let subtmpc = simd_swizzle!(sube, subf, [First(2), First(2), Second(0), Second(1)]);
614 let subfacc = simd_swizzle!(subtmpc, [0, 2, 3, 3]);
615 let swpfacc = simd_swizzle!(self.y_axis.0, [3, 3, 3, 2]);
616 let mulfacc = swpfacc * subfacc;
617
618 let addres = subres + mulfacc;
619 let detcof = addres * f32x4::from_array([1.0, -1.0, 1.0, -1.0]);
620
621 dot4(self.x_axis.0, detcof)
622 }
623
624 /// Returns the inverse of `self`.
625 ///
626 /// If the matrix is not invertible the returned matrix will be invalid.
627 ///
628 /// # Panics
629 ///
630 /// Will panic if the determinant of `self` is zero when `glam_assert` is enabled.
631 #[must_use]
632 pub fn inverse(&self) -> Self {
633 // Based on https://github.com/g-truc/glm `glm_mat4_inverse`
634 let fac0 = {
635 let swp0a = simd_swizzle!(
636 self.w_axis.0,
637 self.z_axis.0,
638 [First(3), First(3), Second(3), Second(3)]
639 );
640 let swp0b = simd_swizzle!(
641 self.w_axis.0,
642 self.z_axis.0,
643 [First(2), First(2), Second(2), Second(2)]
644 );
645
646 let swp00 = simd_swizzle!(
647 self.z_axis.0,
648 self.y_axis.0,
649 [First(2), First(2), Second(2), Second(2)]
650 );
651 let swp01 = simd_swizzle!(swp0a, [0, 0, 0, 2]);
652 let swp02 = simd_swizzle!(swp0b, [0, 0, 0, 2]);
653 let swp03 = simd_swizzle!(
654 self.z_axis.0,
655 self.y_axis.0,
656 [First(3), First(3), Second(3), Second(3)]
657 );
658
659 let mul00 = swp00 * swp01;
660 let mul01 = swp02 * swp03;
661 mul00 - mul01
662 };
663 let fac1 = {
664 let swp0a = simd_swizzle!(
665 self.w_axis.0,
666 self.z_axis.0,
667 [First(3), First(3), Second(3), Second(3)]
668 );
669 let swp0b = simd_swizzle!(
670 self.w_axis.0,
671 self.z_axis.0,
672 [First(1), First(1), Second(1), Second(1)]
673 );
674
675 let swp00 = simd_swizzle!(
676 self.z_axis.0,
677 self.y_axis.0,
678 [First(1), First(1), Second(1), Second(1)]
679 );
680 let swp01 = simd_swizzle!(swp0a, [0, 0, 0, 2]);
681 let swp02 = simd_swizzle!(swp0b, [0, 0, 0, 2]);
682 let swp03 = simd_swizzle!(
683 self.z_axis.0,
684 self.y_axis.0,
685 [First(3), First(3), Second(3), Second(3)]
686 );
687
688 let mul00 = swp00 * swp01;
689 let mul01 = swp02 * swp03;
690 mul00 - mul01
691 };
692 let fac2 = {
693 let swp0a = simd_swizzle!(
694 self.w_axis.0,
695 self.z_axis.0,
696 [First(2), First(2), Second(2), Second(2)]
697 );
698 let swp0b = simd_swizzle!(
699 self.w_axis.0,
700 self.z_axis.0,
701 [First(1), First(1), Second(1), Second(1)]
702 );
703
704 let swp00 = simd_swizzle!(
705 self.z_axis.0,
706 self.y_axis.0,
707 [First(1), First(1), Second(1), Second(1)]
708 );
709 let swp01 = simd_swizzle!(swp0a, [0, 0, 0, 2]);
710 let swp02 = simd_swizzle!(swp0b, [0, 0, 0, 2]);
711 let swp03 = simd_swizzle!(
712 self.z_axis.0,
713 self.y_axis.0,
714 [First(2), First(2), Second(2), Second(2)]
715 );
716
717 let mul00 = swp00 * swp01;
718 let mul01 = swp02 * swp03;
719 mul00 - mul01
720 };
721 let fac3 = {
722 let swp0a = simd_swizzle!(
723 self.w_axis.0,
724 self.z_axis.0,
725 [First(3), First(3), Second(3), Second(3)]
726 );
727 let swp0b = simd_swizzle!(
728 self.w_axis.0,
729 self.z_axis.0,
730 [First(0), First(0), Second(0), Second(0)]
731 );
732
733 let swp00 = simd_swizzle!(
734 self.z_axis.0,
735 self.y_axis.0,
736 [First(0), First(0), Second(0), Second(0)]
737 );
738 let swp01 = simd_swizzle!(swp0a, [0, 0, 0, 2]);
739 let swp02 = simd_swizzle!(swp0b, [0, 0, 0, 2]);
740 let swp03 = simd_swizzle!(
741 self.z_axis.0,
742 self.y_axis.0,
743 [First(3), First(3), Second(3), Second(3)]
744 );
745
746 let mul00 = swp00 * swp01;
747 let mul01 = swp02 * swp03;
748 mul00 - mul01
749 };
750 let fac4 = {
751 let swp0a = simd_swizzle!(
752 self.w_axis.0,
753 self.z_axis.0,
754 [First(2), First(2), Second(2), Second(2)]
755 );
756 let swp0b = simd_swizzle!(
757 self.w_axis.0,
758 self.z_axis.0,
759 [First(0), First(0), Second(0), Second(0)]
760 );
761
762 let swp00 = simd_swizzle!(
763 self.z_axis.0,
764 self.y_axis.0,
765 [First(0), First(0), Second(0), Second(0)]
766 );
767 let swp01 = simd_swizzle!(swp0a, [0, 0, 0, 2]);
768 let swp02 = simd_swizzle!(swp0b, [0, 0, 0, 2]);
769 let swp03 = simd_swizzle!(
770 self.z_axis.0,
771 self.y_axis.0,
772 [First(2), First(2), Second(2), Second(2)]
773 );
774
775 let mul00 = swp00 * swp01;
776 let mul01 = swp02 * swp03;
777 mul00 - mul01
778 };
779 let fac5 = {
780 let swp0a = simd_swizzle!(
781 self.w_axis.0,
782 self.z_axis.0,
783 [First(1), First(1), Second(1), Second(1)]
784 );
785 let swp0b = simd_swizzle!(
786 self.w_axis.0,
787 self.z_axis.0,
788 [First(0), First(0), Second(0), Second(0)]
789 );
790
791 let swp00 = simd_swizzle!(
792 self.z_axis.0,
793 self.y_axis.0,
794 [First(0), First(0), Second(0), Second(0)]
795 );
796 let swp01 = simd_swizzle!(swp0a, [0, 0, 0, 2]);
797 let swp02 = simd_swizzle!(swp0b, [0, 0, 0, 2]);
798 let swp03 = simd_swizzle!(
799 self.z_axis.0,
800 self.y_axis.0,
801 [First(1), First(1), Second(1), Second(1)]
802 );
803
804 let mul00 = swp00 * swp01;
805 let mul01 = swp02 * swp03;
806 mul00 - mul01
807 };
808 let sign_a = f32x4::from_array([-1.0, 1.0, -1.0, 1.0]);
809 let sign_b = f32x4::from_array([1.0, -1.0, 1.0, -1.0]);
810
811 let temp0 = simd_swizzle!(
812 self.y_axis.0,
813 self.x_axis.0,
814 [First(0), First(0), Second(0), Second(0)]
815 );
816 let vec0 = simd_swizzle!(temp0, [0, 2, 2, 2]);
817
818 let temp1 = simd_swizzle!(
819 self.y_axis.0,
820 self.x_axis.0,
821 [First(1), First(1), Second(1), Second(1)]
822 );
823 let vec1 = simd_swizzle!(temp1, [0, 2, 2, 2]);
824
825 let temp2 = simd_swizzle!(
826 self.y_axis.0,
827 self.x_axis.0,
828 [First(2), First(2), Second(2), Second(2)]
829 );
830 let vec2 = simd_swizzle!(temp2, [0, 2, 2, 2]);
831
832 let temp3 = simd_swizzle!(
833 self.y_axis.0,
834 self.x_axis.0,
835 [First(3), First(3), Second(3), Second(3)]
836 );
837 let vec3 = simd_swizzle!(temp3, [0, 2, 2, 2]);
838
839 let mul00 = vec1 * fac0;
840 let mul01 = vec2 * fac1;
841 let mul02 = vec3 * fac2;
842 let sub00 = mul00 - mul01;
843 let add00 = sub00 + mul02;
844 let inv0 = sign_b * add00;
845
846 let mul03 = vec0 * fac0;
847 let mul04 = vec2 * fac3;
848 let mul05 = vec3 * fac4;
849 let sub01 = mul03 - mul04;
850 let add01 = sub01 + mul05;
851 let inv1 = sign_a * add01;
852
853 let mul06 = vec0 * fac1;
854 let mul07 = vec1 * fac3;
855 let mul08 = vec3 * fac5;
856 let sub02 = mul06 - mul07;
857 let add02 = sub02 + mul08;
858 let inv2 = sign_b * add02;
859
860 let mul09 = vec0 * fac2;
861 let mul10 = vec1 * fac4;
862 let mul11 = vec2 * fac5;
863 let sub03 = mul09 - mul10;
864 let add03 = sub03 + mul11;
865 let inv3 = sign_a * add03;
866
867 let row0 = simd_swizzle!(inv0, inv1, [First(0), First(0), Second(0), Second(0)]);
868 let row1 = simd_swizzle!(inv2, inv3, [First(0), First(0), Second(0), Second(0)]);
869 let row2 = simd_swizzle!(row0, row1, [First(0), First(2), Second(0), Second(2)]);
870
871 let dot0 = dot4(self.x_axis.0, row2);
872 glam_assert!(dot0 != 0.0);
873
874 let rcp0 = f32x4::splat(dot0.recip());
875
876 Self {
877 x_axis: Vec4(inv0 * rcp0),
878 y_axis: Vec4(inv1 * rcp0),
879 z_axis: Vec4(inv2 * rcp0),
880 w_axis: Vec4(inv3 * rcp0),
881 }
882 }
883
884 #[inline]
885 fn look_to_lh(eye: Vec3, dir: Vec3, up: Vec3) -> Self {
886 let f = dir.normalize();
887 let s = up.cross(f).normalize();
888 let u = f.cross(s);
889 Self::from_cols(
890 Vec4::new(s.x, u.x, f.x, 0.0),
891 Vec4::new(s.y, u.y, f.y, 0.0),
892 Vec4::new(s.z, u.z, f.z, 0.0),
893 Vec4::new(-s.dot(eye), -u.dot(eye), -f.dot(eye), 1.0),
894 )
895 }
896
897 /// Creates a left-handed view matrix using a camera position, an up direction, and a focal
898 /// point.
899 /// For a view coordinate system with `+X=right`, `+Y=up` and `+Z=forward`.
900 ///
901 /// # Panics
902 ///
903 /// Will panic if `up` is not normalized when `glam_assert` is enabled.
904 #[inline]
905 pub fn look_at_lh(eye: Vec3, center: Vec3, up: Vec3) -> Self {
906 glam_assert!(up.is_normalized());
907 Self::look_to_lh(eye, center.sub(eye), up)
908 }
909
910 /// Creates a right-handed view matrix using a camera position, an up direction, and a focal
911 /// point.
912 /// For a view coordinate system with `+X=right`, `+Y=up` and `+Z=back`.
913 ///
914 /// # Panics
915 ///
916 /// Will panic if `up` is not normalized when `glam_assert` is enabled.
917 #[inline]
918 pub fn look_at_rh(eye: Vec3, center: Vec3, up: Vec3) -> Self {
919 glam_assert!(up.is_normalized());
920 Self::look_to_lh(eye, eye.sub(center), up)
921 }
922
923 /// Creates a right-handed perspective projection matrix with [-1,1] depth range.
924 /// This is the same as the OpenGL `gluPerspective` function.
925 /// See <https://www.khronos.org/registry/OpenGL-Refpages/gl2.1/xhtml/gluPerspective.xml>
926 #[inline]
927 pub fn perspective_rh_gl(
928 fov_y_radians: f32,
929 aspect_ratio: f32,
930 z_near: f32,
931 z_far: f32,
932 ) -> Self {
933 let inv_length = 1.0 / (z_near - z_far);
934 let f = 1.0 / (0.5 * fov_y_radians).tan();
935 let a = f / aspect_ratio;
936 let b = (z_near + z_far) * inv_length;
937 let c = (2.0 * z_near * z_far) * inv_length;
938 Self::from_cols(
939 Vec4::new(a, 0.0, 0.0, 0.0),
940 Vec4::new(0.0, f, 0.0, 0.0),
941 Vec4::new(0.0, 0.0, b, -1.0),
942 Vec4::new(0.0, 0.0, c, 0.0),
943 )
944 }
945
946 /// Creates a left-handed perspective projection matrix with `[0,1]` depth range.
947 ///
948 /// # Panics
949 ///
950 /// Will panic if `z_near` or `z_far` are less than or equal to zero when `glam_assert` is
951 /// enabled.
952 #[inline]
953 pub fn perspective_lh(fov_y_radians: f32, aspect_ratio: f32, z_near: f32, z_far: f32) -> Self {
954 glam_assert!(z_near > 0.0 && z_far > 0.0);
955 let (sin_fov, cos_fov) = (0.5 * fov_y_radians).sin_cos();
956 let h = cos_fov / sin_fov;
957 let w = h / aspect_ratio;
958 let r = z_far / (z_far - z_near);
959 Self::from_cols(
960 Vec4::new(w, 0.0, 0.0, 0.0),
961 Vec4::new(0.0, h, 0.0, 0.0),
962 Vec4::new(0.0, 0.0, r, 1.0),
963 Vec4::new(0.0, 0.0, -r * z_near, 0.0),
964 )
965 }
966
967 /// Creates a right-handed perspective projection matrix with `[0,1]` depth range.
968 ///
969 /// # Panics
970 ///
971 /// Will panic if `z_near` or `z_far` are less than or equal to zero when `glam_assert` is
972 /// enabled.
973 #[inline]
974 pub fn perspective_rh(fov_y_radians: f32, aspect_ratio: f32, z_near: f32, z_far: f32) -> Self {
975 glam_assert!(z_near > 0.0 && z_far > 0.0);
976 let (sin_fov, cos_fov) = (0.5 * fov_y_radians).sin_cos();
977 let h = cos_fov / sin_fov;
978 let w = h / aspect_ratio;
979 let r = z_far / (z_near - z_far);
980 Self::from_cols(
981 Vec4::new(w, 0.0, 0.0, 0.0),
982 Vec4::new(0.0, h, 0.0, 0.0),
983 Vec4::new(0.0, 0.0, r, -1.0),
984 Vec4::new(0.0, 0.0, r * z_near, 0.0),
985 )
986 }
987
988 /// Creates an infinite left-handed perspective projection matrix with `[0,1]` depth range.
989 ///
990 /// # Panics
991 ///
992 /// Will panic if `z_near` is less than or equal to zero when `glam_assert` is enabled.
993 #[inline]
994 pub fn perspective_infinite_lh(fov_y_radians: f32, aspect_ratio: f32, z_near: f32) -> Self {
995 glam_assert!(z_near > 0.0);
996 let (sin_fov, cos_fov) = (0.5 * fov_y_radians).sin_cos();
997 let h = cos_fov / sin_fov;
998 let w = h / aspect_ratio;
999 Self::from_cols(
1000 Vec4::new(w, 0.0, 0.0, 0.0),
1001 Vec4::new(0.0, h, 0.0, 0.0),
1002 Vec4::new(0.0, 0.0, 1.0, 1.0),
1003 Vec4::new(0.0, 0.0, -z_near, 0.0),
1004 )
1005 }
1006
1007 /// Creates an infinite left-handed perspective projection matrix with `[0,1]` depth range.
1008 ///
1009 /// # Panics
1010 ///
1011 /// Will panic if `z_near` is less than or equal to zero when `glam_assert` is enabled.
1012 #[inline]
1013 pub fn perspective_infinite_reverse_lh(
1014 fov_y_radians: f32,
1015 aspect_ratio: f32,
1016 z_near: f32,
1017 ) -> Self {
1018 glam_assert!(z_near > 0.0);
1019 let (sin_fov, cos_fov) = (0.5 * fov_y_radians).sin_cos();
1020 let h = cos_fov / sin_fov;
1021 let w = h / aspect_ratio;
1022 Self::from_cols(
1023 Vec4::new(w, 0.0, 0.0, 0.0),
1024 Vec4::new(0.0, h, 0.0, 0.0),
1025 Vec4::new(0.0, 0.0, 0.0, 1.0),
1026 Vec4::new(0.0, 0.0, z_near, 0.0),
1027 )
1028 }
1029
1030 /// Creates an infinite right-handed perspective projection matrix with
1031 /// `[0,1]` depth range.
1032 #[inline]
1033 pub fn perspective_infinite_rh(fov_y_radians: f32, aspect_ratio: f32, z_near: f32) -> Self {
1034 glam_assert!(z_near > 0.0);
1035 let f = 1.0 / (0.5 * fov_y_radians).tan();
1036 Self::from_cols(
1037 Vec4::new(f / aspect_ratio, 0.0, 0.0, 0.0),
1038 Vec4::new(0.0, f, 0.0, 0.0),
1039 Vec4::new(0.0, 0.0, -1.0, -1.0),
1040 Vec4::new(0.0, 0.0, -z_near, 0.0),
1041 )
1042 }
1043
1044 /// Creates an infinite reverse right-handed perspective projection matrix
1045 /// with `[0,1]` depth range.
1046 #[inline]
1047 pub fn perspective_infinite_reverse_rh(
1048 fov_y_radians: f32,
1049 aspect_ratio: f32,
1050 z_near: f32,
1051 ) -> Self {
1052 glam_assert!(z_near > 0.0);
1053 let f = 1.0 / (0.5 * fov_y_radians).tan();
1054 Self::from_cols(
1055 Vec4::new(f / aspect_ratio, 0.0, 0.0, 0.0),
1056 Vec4::new(0.0, f, 0.0, 0.0),
1057 Vec4::new(0.0, 0.0, 0.0, -1.0),
1058 Vec4::new(0.0, 0.0, z_near, 0.0),
1059 )
1060 }
1061
1062 /// Creates a right-handed orthographic projection matrix with `[-1,1]` depth
1063 /// range. This is the same as the OpenGL `glOrtho` function in OpenGL.
1064 /// See
1065 /// <https://www.khronos.org/registry/OpenGL-Refpages/gl2.1/xhtml/glOrtho.xml>
1066 #[inline]
1067 pub fn orthographic_rh_gl(
1068 left: f32,
1069 right: f32,
1070 bottom: f32,
1071 top: f32,
1072 near: f32,
1073 far: f32,
1074 ) -> Self {
1075 let a = 2.0 / (right - left);
1076 let b = 2.0 / (top - bottom);
1077 let c = -2.0 / (far - near);
1078 let tx = -(right + left) / (right - left);
1079 let ty = -(top + bottom) / (top - bottom);
1080 let tz = -(far + near) / (far - near);
1081
1082 Self::from_cols(
1083 Vec4::new(a, 0.0, 0.0, 0.0),
1084 Vec4::new(0.0, b, 0.0, 0.0),
1085 Vec4::new(0.0, 0.0, c, 0.0),
1086 Vec4::new(tx, ty, tz, 1.0),
1087 )
1088 }
1089
1090 /// Creates a left-handed orthographic projection matrix with `[0,1]` depth range.
1091 #[inline]
1092 pub fn orthographic_lh(
1093 left: f32,
1094 right: f32,
1095 bottom: f32,
1096 top: f32,
1097 near: f32,
1098 far: f32,
1099 ) -> Self {
1100 let rcp_width = 1.0 / (right - left);
1101 let rcp_height = 1.0 / (top - bottom);
1102 let r = 1.0 / (far - near);
1103 Self::from_cols(
1104 Vec4::new(rcp_width + rcp_width, 0.0, 0.0, 0.0),
1105 Vec4::new(0.0, rcp_height + rcp_height, 0.0, 0.0),
1106 Vec4::new(0.0, 0.0, r, 0.0),
1107 Vec4::new(
1108 -(left + right) * rcp_width,
1109 -(top + bottom) * rcp_height,
1110 -r * near,
1111 1.0,
1112 ),
1113 )
1114 }
1115
1116 /// Creates a right-handed orthographic projection matrix with `[0,1]` depth range.
1117 #[inline]
1118 pub fn orthographic_rh(
1119 left: f32,
1120 right: f32,
1121 bottom: f32,
1122 top: f32,
1123 near: f32,
1124 far: f32,
1125 ) -> Self {
1126 let rcp_width = 1.0 / (right - left);
1127 let rcp_height = 1.0 / (top - bottom);
1128 let r = 1.0 / (near - far);
1129 Self::from_cols(
1130 Vec4::new(rcp_width + rcp_width, 0.0, 0.0, 0.0),
1131 Vec4::new(0.0, rcp_height + rcp_height, 0.0, 0.0),
1132 Vec4::new(0.0, 0.0, r, 0.0),
1133 Vec4::new(
1134 -(left + right) * rcp_width,
1135 -(top + bottom) * rcp_height,
1136 r * near,
1137 1.0,
1138 ),
1139 )
1140 }
1141
1142 /// Transforms the given 3D vector as a point, applying perspective correction.
1143 ///
1144 /// This is the equivalent of multiplying the 3D vector as a 4D vector where `w` is `1.0`.
1145 /// The perspective divide is performed meaning the resulting 3D vector is divided by `w`.
1146 ///
1147 /// This method assumes that `self` contains a projective transform.
1148 #[inline]
1149 pub fn project_point3(&self, rhs: Vec3) -> Vec3 {
1150 let mut res = self.x_axis.mul(rhs.x);
1151 res = self.y_axis.mul(rhs.y).add(res);
1152 res = self.z_axis.mul(rhs.z).add(res);
1153 res = self.w_axis.add(res);
1154 res = res.mul(res.wwww().recip());
1155 res.xyz()
1156 }
1157
1158 /// Transforms the given 3D vector as a point.
1159 ///
1160 /// This is the equivalent of multiplying the 3D vector as a 4D vector where `w` is
1161 /// `1.0`.
1162 ///
1163 /// This method assumes that `self` contains a valid affine transform. It does not perform
1164 /// a persective divide, if `self` contains a perspective transform, or if you are unsure,
1165 /// the [`Self::project_point3()`] method should be used instead.
1166 ///
1167 /// # Panics
1168 ///
1169 /// Will panic if the 3rd row of `self` is not `(0, 0, 0, 1)` when `glam_assert` is enabled.
1170 #[inline]
1171 pub fn transform_point3(&self, rhs: Vec3) -> Vec3 {
1172 glam_assert!(self.row(3).abs_diff_eq(Vec4::W, 1e-6));
1173 let mut res = self.x_axis.mul(rhs.x);
1174 res = self.y_axis.mul(rhs.y).add(res);
1175 res = self.z_axis.mul(rhs.z).add(res);
1176 res = self.w_axis.add(res);
1177 res.xyz()
1178 }
1179
1180 /// Transforms the give 3D vector as a direction.
1181 ///
1182 /// This is the equivalent of multiplying the 3D vector as a 4D vector where `w` is
1183 /// `0.0`.
1184 ///
1185 /// This method assumes that `self` contains a valid affine transform.
1186 ///
1187 /// # Panics
1188 ///
1189 /// Will panic if the 3rd row of `self` is not `(0, 0, 0, 1)` when `glam_assert` is enabled.
1190 #[inline]
1191 pub fn transform_vector3(&self, rhs: Vec3) -> Vec3 {
1192 glam_assert!(self.row(3).abs_diff_eq(Vec4::W, 1e-6));
1193 let mut res = self.x_axis.mul(rhs.x);
1194 res = self.y_axis.mul(rhs.y).add(res);
1195 res = self.z_axis.mul(rhs.z).add(res);
1196 res.xyz()
1197 }
1198
1199 /// Transforms the given `Vec3A` as 3D point.
1200 ///
1201 /// This is the equivalent of multiplying the `Vec3A` as a 4D vector where `w` is `1.0`.
1202 #[inline]
1203 pub fn transform_point3a(&self, rhs: Vec3A) -> Vec3A {
1204 glam_assert!(self.row(3).abs_diff_eq(Vec4::W, 1e-6));
1205 let mut res = self.x_axis.mul(rhs.xxxx());
1206 res = self.y_axis.mul(rhs.yyyy()).add(res);
1207 res = self.z_axis.mul(rhs.zzzz()).add(res);
1208 res = self.w_axis.add(res);
1209 res.into()
1210 }
1211
1212 /// Transforms the give `Vec3A` as 3D vector.
1213 ///
1214 /// This is the equivalent of multiplying the `Vec3A` as a 4D vector where `w` is `0.0`.
1215 #[inline]
1216 pub fn transform_vector3a(&self, rhs: Vec3A) -> Vec3A {
1217 glam_assert!(self.row(3).abs_diff_eq(Vec4::W, 1e-6));
1218 let mut res = self.x_axis.mul(rhs.xxxx());
1219 res = self.y_axis.mul(rhs.yyyy()).add(res);
1220 res = self.z_axis.mul(rhs.zzzz()).add(res);
1221 res.into()
1222 }
1223
1224 /// Transforms a 4D vector.
1225 #[inline]
1226 pub fn mul_vec4(&self, rhs: Vec4) -> Vec4 {
1227 let mut res = self.x_axis.mul(rhs.xxxx());
1228 res = res.add(self.y_axis.mul(rhs.yyyy()));
1229 res = res.add(self.z_axis.mul(rhs.zzzz()));
1230 res = res.add(self.w_axis.mul(rhs.wwww()));
1231 res
1232 }
1233
1234 /// Multiplies two 4x4 matrices.
1235 #[inline]
1236 pub fn mul_mat4(&self, rhs: &Self) -> Self {
1237 Self::from_cols(
1238 self.mul(rhs.x_axis),
1239 self.mul(rhs.y_axis),
1240 self.mul(rhs.z_axis),
1241 self.mul(rhs.w_axis),
1242 )
1243 }
1244
1245 /// Adds two 4x4 matrices.
1246 #[inline]
1247 pub fn add_mat4(&self, rhs: &Self) -> Self {
1248 Self::from_cols(
1249 self.x_axis.add(rhs.x_axis),
1250 self.y_axis.add(rhs.y_axis),
1251 self.z_axis.add(rhs.z_axis),
1252 self.w_axis.add(rhs.w_axis),
1253 )
1254 }
1255
1256 /// Subtracts two 4x4 matrices.
1257 #[inline]
1258 pub fn sub_mat4(&self, rhs: &Self) -> Self {
1259 Self::from_cols(
1260 self.x_axis.sub(rhs.x_axis),
1261 self.y_axis.sub(rhs.y_axis),
1262 self.z_axis.sub(rhs.z_axis),
1263 self.w_axis.sub(rhs.w_axis),
1264 )
1265 }
1266
1267 /// Multiplies a 4x4 matrix by a scalar.
1268 #[inline]
1269 pub fn mul_scalar(&self, rhs: f32) -> Self {
1270 Self::from_cols(
1271 self.x_axis.mul(rhs),
1272 self.y_axis.mul(rhs),
1273 self.z_axis.mul(rhs),
1274 self.w_axis.mul(rhs),
1275 )
1276 }
1277
1278 /// Returns true if the absolute difference of all elements between `self` and `rhs`
1279 /// is less than or equal to `max_abs_diff`.
1280 ///
1281 /// This can be used to compare if two matrices contain similar elements. It works best
1282 /// when comparing with a known value. The `max_abs_diff` that should be used used
1283 /// depends on the values being compared against.
1284 ///
1285 /// For more see
1286 /// [comparing floating point numbers](https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/).
1287 #[inline]
1288 pub fn abs_diff_eq(&self, rhs: Self, max_abs_diff: f32) -> bool {
1289 self.x_axis.abs_diff_eq(rhs.x_axis, max_abs_diff)
1290 && self.y_axis.abs_diff_eq(rhs.y_axis, max_abs_diff)
1291 && self.z_axis.abs_diff_eq(rhs.z_axis, max_abs_diff)
1292 && self.w_axis.abs_diff_eq(rhs.w_axis, max_abs_diff)
1293 }
1294
1295 #[inline]
1296 pub fn as_dmat4(&self) -> DMat4 {
1297 DMat4::from_cols(
1298 self.x_axis.as_dvec4(),
1299 self.y_axis.as_dvec4(),
1300 self.z_axis.as_dvec4(),
1301 self.w_axis.as_dvec4(),
1302 )
1303 }
1304 }
1305
1306 impl Default for Mat4 {
1307 #[inline]
1308 fn default() -> Self {
1309 Self::IDENTITY
1310 }
1311 }
1312
1313 impl Add<Mat4> for Mat4 {
1314 type Output = Self;
1315 #[inline]
1316 fn add(self, rhs: Self) -> Self::Output {
1317 self.add_mat4(&rhs)
1318 }
1319 }
1320
1321 impl AddAssign<Mat4> for Mat4 {
1322 #[inline]
1323 fn add_assign(&mut self, rhs: Self) {
1324 *self = self.add_mat4(&rhs);
1325 }
1326 }
1327
1328 impl Sub<Mat4> for Mat4 {
1329 type Output = Self;
1330 #[inline]
1331 fn sub(self, rhs: Self) -> Self::Output {
1332 self.sub_mat4(&rhs)
1333 }
1334 }
1335
1336 impl SubAssign<Mat4> for Mat4 {
1337 #[inline]
1338 fn sub_assign(&mut self, rhs: Self) {
1339 *self = self.sub_mat4(&rhs);
1340 }
1341 }
1342
1343 impl Neg for Mat4 {
1344 type Output = Self;
1345 #[inline]
1346 fn neg(self) -> Self::Output {
1347 Self::from_cols(
1348 self.x_axis.neg(),
1349 self.y_axis.neg(),
1350 self.z_axis.neg(),
1351 self.w_axis.neg(),
1352 )
1353 }
1354 }
1355
1356 impl Mul<Mat4> for Mat4 {
1357 type Output = Self;
1358 #[inline]
1359 fn mul(self, rhs: Self) -> Self::Output {
1360 self.mul_mat4(&rhs)
1361 }
1362 }
1363
1364 impl MulAssign<Mat4> for Mat4 {
1365 #[inline]
1366 fn mul_assign(&mut self, rhs: Self) {
1367 *self = self.mul_mat4(&rhs);
1368 }
1369 }
1370
1371 impl Mul<Vec4> for Mat4 {
1372 type Output = Vec4;
1373 #[inline]
1374 fn mul(self, rhs: Vec4) -> Self::Output {
1375 self.mul_vec4(rhs)
1376 }
1377 }
1378
1379 impl Mul<Mat4> for f32 {
1380 type Output = Mat4;
1381 #[inline]
1382 fn mul(self, rhs: Mat4) -> Self::Output {
1383 rhs.mul_scalar(self)
1384 }
1385 }
1386
1387 impl Mul<f32> for Mat4 {
1388 type Output = Self;
1389 #[inline]
1390 fn mul(self, rhs: f32) -> Self::Output {
1391 self.mul_scalar(rhs)
1392 }
1393 }
1394
1395 impl MulAssign<f32> for Mat4 {
1396 #[inline]
1397 fn mul_assign(&mut self, rhs: f32) {
1398 *self = self.mul_scalar(rhs);
1399 }
1400 }
1401
1402 impl<'a> Sum<&'a Self> for Mat4 {
1403 fn sum<I>(iter: I) -> Self
1404 where
1405 I: Iterator<Item = &'a Self>,
1406 {
1407 iter.fold(Self::ZERO, |a, &b| Self::add(a, b))
1408 }
1409 }
1410
1411 impl<'a> Product<&'a Self> for Mat4 {
1412 fn product<I>(iter: I) -> Self
1413 where
1414 I: Iterator<Item = &'a Self>,
1415 {
1416 iter.fold(Self::IDENTITY, |a, &b| Self::mul(a, b))
1417 }
1418 }
1419
1420 impl PartialEq for Mat4 {
1421 #[inline]
1422 fn eq(&self, rhs: &Self) -> bool {
1423 self.x_axis.eq(&rhs.x_axis)
1424 && self.y_axis.eq(&rhs.y_axis)
1425 && self.z_axis.eq(&rhs.z_axis)
1426 && self.w_axis.eq(&rhs.w_axis)
1427 }
1428 }
1429
1430 #[cfg(not(target_arch = "spirv"))]
1431 impl AsRef<[f32; 16]> for Mat4 {
1432 #[inline]
1433 fn as_ref(&self) -> &[f32; 16] {
1434 unsafe { &*(self as *const Self as *const [f32; 16]) }
1435 }
1436 }
1437
1438 #[cfg(not(target_arch = "spirv"))]
1439 impl AsMut<[f32; 16]> for Mat4 {
1440 #[inline]
1441 fn as_mut(&mut self) -> &mut [f32; 16] {
1442 unsafe { &mut *(self as *mut Self as *mut [f32; 16]) }
1443 }
1444 }
1445
1446 #[cfg(not(target_arch = "spirv"))]
1447 impl fmt::Debug for Mat4 {
1448 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
1449 fmt.debug_struct(stringify!(Mat4))
1450 .field("x_axis", &self.x_axis)
1451 .field("y_axis", &self.y_axis)
1452 .field("z_axis", &self.z_axis)
1453 .field("w_axis", &self.w_axis)
1454 .finish()
1455 }
1456 }
1457
1458 #[cfg(not(target_arch = "spirv"))]
1459 impl fmt::Display for Mat4 {
1460 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1461 write!(
1462 f,
1463 "[{}, {}, {}, {}]",
1464 self.x_axis, self.y_axis, self.z_axis, self.w_axis
1465 )
1466 }
1467 }