Debian Codegraph @

debian/missing-sources/glam-0.28.0/src/f32/neon/mat4.rs

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