Debian Codegraph @

debian/missing-sources/glam-0.29.3/src/f32/scalar/mat4.rs

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