Debian Codegraph @

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

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