Debian Codegraph @

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

raw
1 // Generated from mat.rs.tera template. Edit the template, not the generated file.
2
3 use crate::{
4 coresimd::*,
5 euler::{FromEuler, ToEuler},
6 f32::math,
7 swizzles::*,
8 DMat4, EulerRot, Mat3, Mat3A, Quat, Vec3, Vec3A, Vec4,
9 };
10 use core::fmt;
11 use core::iter::{Product, Sum};
12 use core::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssign};
13
14 use core::simd::*;
15
16 /// Creates a 4x4 matrix from four column vectors.
17 #[inline(always)]
18 #[must_use]
19 pub const fn mat4(x_axis: Vec4, y_axis: Vec4, z_axis: Vec4, w_axis: Vec4) -> Mat4 {
20 Mat4::from_cols(x_axis, y_axis, z_axis, w_axis)
21 }
22
23 /// A 4x4 column major matrix.
24 ///
25 /// This 4x4 matrix type features convenience methods for creating and using affine transforms and
26 /// perspective projections. If you are primarily dealing with 3D affine transformations
27 /// considering using [`Affine3A`](crate::Affine3A) which is faster than a 4x4 matrix
28 /// for some affine operations.
29 ///
30 /// Affine transformations including 3D translation, rotation and scale can be created
31 /// using methods such as [`Self::from_translation()`], [`Self::from_quat()`],
32 /// [`Self::from_scale()`] and [`Self::from_scale_rotation_translation()`].
33 ///
34 /// Orthographic projections can be created using the methods [`Self::orthographic_lh()`] for
35 /// left-handed coordinate systems and [`Self::orthographic_rh()`] for right-handed
36 /// systems. The resulting matrix is also an affine transformation.
37 ///
38 /// The [`Self::transform_point3()`] and [`Self::transform_vector3()`] convenience methods
39 /// are provided for performing affine transformations on 3D vectors and points. These
40 /// multiply 3D inputs as 4D vectors with an implicit `w` value of `1` for points and `0`
41 /// for vectors respectively. These methods assume that `Self` contains a valid affine
42 /// transform.
43 ///
44 /// Perspective projections can be created using methods such as
45 /// [`Self::perspective_lh()`], [`Self::perspective_infinite_lh()`] and
46 /// [`Self::perspective_infinite_reverse_lh()`] for left-handed co-ordinate systems and
47 /// [`Self::perspective_rh()`], [`Self::perspective_infinite_rh()`] and
48 /// [`Self::perspective_infinite_reverse_rh()`] for right-handed co-ordinate systems.
49 ///
50 /// The resulting perspective project can be use to transform 3D vectors as points with
51 /// perspective correction using the [`Self::project_point3()`] convenience method.
52 #[derive(Clone, Copy)]
53 #[cfg_attr(feature = "bytemuck", derive(bytemuck::Pod, bytemuck::Zeroable))]
54 #[repr(C)]
55 pub struct Mat4 {
56 pub x_axis: Vec4,
57 pub y_axis: Vec4,
58 pub z_axis: Vec4,
59 pub w_axis: Vec4,
60 }
61
62 impl Mat4 {
63 /// A 4x4 matrix with all elements set to `0.0`.
64 pub const ZERO: Self = Self::from_cols(Vec4::ZERO, Vec4::ZERO, Vec4::ZERO, Vec4::ZERO);
65
66 /// A 4x4 identity matrix, where all diagonal elements are `1`, and all off-diagonal elements are `0`.
67 pub const IDENTITY: Self = Self::from_cols(Vec4::X, Vec4::Y, Vec4::Z, Vec4::W);
68
69 /// All NAN:s.
70 pub const NAN: Self = Self::from_cols(Vec4::NAN, Vec4::NAN, Vec4::NAN, Vec4::NAN);
71
72 #[allow(clippy::too_many_arguments)]
73 #[inline(always)]
74 #[must_use]
75 const fn new(
76 m00: f32,
77 m01: f32,
78 m02: f32,
79 m03: f32,
80 m10: f32,
81 m11: f32,
82 m12: f32,
83 m13: f32,
84 m20: f32,
85 m21: f32,
86 m22: f32,
87 m23: f32,
88 m30: f32,
89 m31: f32,
90 m32: f32,
91 m33: f32,
92 ) -> Self {
93 Self {
94 x_axis: Vec4::new(m00, m01, m02, m03),
95 y_axis: Vec4::new(m10, m11, m12, m13),
96 z_axis: Vec4::new(m20, m21, m22, m23),
97 w_axis: Vec4::new(m30, m31, m32, m33),
98 }
99 }
100
101 /// Creates a 4x4 matrix from four column vectors.
102 #[inline(always)]
103 #[must_use]
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 #[must_use]
118 pub const fn from_cols_array(m: &[f32; 16]) -> Self {
119 Self::new(
120 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],
121 m[14], m[15],
122 )
123 }
124
125 /// Creates a `[f32; 16]` array storing data in column major order.
126 /// If you require data in row major order `transpose` the matrix first.
127 #[inline]
128 #[must_use]
129 pub const fn to_cols_array(&self) -> [f32; 16] {
130 let [x_axis_x, x_axis_y, x_axis_z, x_axis_w] = self.x_axis.to_array();
131 let [y_axis_x, y_axis_y, y_axis_z, y_axis_w] = self.y_axis.to_array();
132 let [z_axis_x, z_axis_y, z_axis_z, z_axis_w] = self.z_axis.to_array();
133 let [w_axis_x, w_axis_y, w_axis_z, w_axis_w] = self.w_axis.to_array();
134
135 [
136 x_axis_x, x_axis_y, x_axis_z, x_axis_w, y_axis_x, y_axis_y, y_axis_z, y_axis_w,
137 z_axis_x, z_axis_y, z_axis_z, z_axis_w, w_axis_x, w_axis_y, w_axis_z, w_axis_w,
138 ]
139 }
140
141 /// Creates a 4x4 matrix from a `[[f32; 4]; 4]` 4D array stored in column major order.
142 /// If your data is in row major order you will need to `transpose` the returned
143 /// matrix.
144 #[inline]
145 #[must_use]
146 pub const fn from_cols_array_2d(m: &[[f32; 4]; 4]) -> Self {
147 Self::from_cols(
148 Vec4::from_array(m[0]),
149 Vec4::from_array(m[1]),
150 Vec4::from_array(m[2]),
151 Vec4::from_array(m[3]),
152 )
153 }
154
155 /// Creates a `[[f32; 4]; 4]` 4D array storing data in column major order.
156 /// If you require data in row major order `transpose` the matrix first.
157 #[inline]
158 #[must_use]
159 pub const fn to_cols_array_2d(&self) -> [[f32; 4]; 4] {
160 [
161 self.x_axis.to_array(),
162 self.y_axis.to_array(),
163 self.z_axis.to_array(),
164 self.w_axis.to_array(),
165 ]
166 }
167
168 /// Creates a 4x4 matrix with its diagonal set to `diagonal` and all other entries set to 0.
169 #[doc(alias = "scale")]
170 #[inline]
171 #[must_use]
172 pub const fn from_diagonal(diagonal: Vec4) -> Self {
173 // diagonal.x, diagonal.y etc can't be done in a const-context
174 let [x, y, z, w] = diagonal.to_array();
175 Self::new(
176 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,
177 )
178 }
179
180 #[inline]
181 #[must_use]
182 fn quat_to_axes(rotation: Quat) -> (Vec4, Vec4, Vec4) {
183 glam_assert!(rotation.is_normalized());
184
185 let (x, y, z, w) = rotation.into();
186 let x2 = x + x;
187 let y2 = y + y;
188 let z2 = z + z;
189 let xx = x * x2;
190 let xy = x * y2;
191 let xz = x * z2;
192 let yy = y * y2;
193 let yz = y * z2;
194 let zz = z * z2;
195 let wx = w * x2;
196 let wy = w * y2;
197 let wz = w * z2;
198
199 let x_axis = Vec4::new(1.0 - (yy + zz), xy + wz, xz - wy, 0.0);
200 let y_axis = Vec4::new(xy - wz, 1.0 - (xx + zz), yz + wx, 0.0);
201 let z_axis = Vec4::new(xz + wy, yz - wx, 1.0 - (xx + yy), 0.0);
202 (x_axis, y_axis, z_axis)
203 }
204
205 /// Creates an affine transformation matrix from the given 3D `scale`, `rotation` and
206 /// `translation`.
207 ///
208 /// The resulting matrix can be used to transform 3D points and vectors. See
209 /// [`Self::transform_point3()`] and [`Self::transform_vector3()`].
210 ///
211 /// # Panics
212 ///
213 /// Will panic if `rotation` is not normalized when `glam_assert` is enabled.
214 #[inline]
215 #[must_use]
216 pub fn from_scale_rotation_translation(scale: Vec3, rotation: Quat, translation: Vec3) -> Self {
217 let (x_axis, y_axis, z_axis) = Self::quat_to_axes(rotation);
218 Self::from_cols(
219 x_axis.mul(scale.x),
220 y_axis.mul(scale.y),
221 z_axis.mul(scale.z),
222 Vec4::from((translation, 1.0)),
223 )
224 }
225
226 /// Creates an affine transformation matrix from the given 3D `translation`.
227 ///
228 /// The resulting matrix can be used to transform 3D points and vectors. See
229 /// [`Self::transform_point3()`] and [`Self::transform_vector3()`].
230 ///
231 /// # Panics
232 ///
233 /// Will panic if `rotation` is not normalized when `glam_assert` is enabled.
234 #[inline]
235 #[must_use]
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 #[must_use]
250 pub fn to_scale_rotation_translation(&self) -> (Vec3, Quat, Vec3) {
251 let det = self.determinant();
252 glam_assert!(det != 0.0);
253
254 let scale = Vec3::new(
255 self.x_axis.length() * math::signum(det),
256 self.y_axis.length(),
257 self.z_axis.length(),
258 );
259
260 glam_assert!(scale.cmpne(Vec3::ZERO).all());
261
262 let inv_scale = scale.recip();
263
264 let rotation = Quat::from_rotation_axes(
265 self.x_axis.mul(inv_scale.x).xyz(),
266 self.y_axis.mul(inv_scale.y).xyz(),
267 self.z_axis.mul(inv_scale.z).xyz(),
268 );
269
270 let translation = self.w_axis.xyz();
271
272 (scale, rotation, translation)
273 }
274
275 /// Creates an affine transformation matrix from the given `rotation` quaternion.
276 ///
277 /// The resulting matrix can be used to transform 3D points and vectors. See
278 /// [`Self::transform_point3()`] and [`Self::transform_vector3()`].
279 ///
280 /// # Panics
281 ///
282 /// Will panic if `rotation` is not normalized when `glam_assert` is enabled.
283 #[inline]
284 #[must_use]
285 pub fn from_quat(rotation: Quat) -> Self {
286 let (x_axis, y_axis, z_axis) = Self::quat_to_axes(rotation);
287 Self::from_cols(x_axis, y_axis, z_axis, Vec4::W)
288 }
289
290 /// Creates an affine transformation matrix from the given 3x3 linear transformation
291 /// matrix.
292 ///
293 /// The resulting matrix can be used to transform 3D points and vectors. See
294 /// [`Self::transform_point3()`] and [`Self::transform_vector3()`].
295 #[inline]
296 #[must_use]
297 pub fn from_mat3(m: Mat3) -> Self {
298 Self::from_cols(
299 Vec4::from((m.x_axis, 0.0)),
300 Vec4::from((m.y_axis, 0.0)),
301 Vec4::from((m.z_axis, 0.0)),
302 Vec4::W,
303 )
304 }
305
306 /// Creates an affine transformation matrics from a 3x3 matrix (expressing scale, shear and
307 /// rotation) and a translation vector.
308 ///
309 /// Equivalent to `Mat4::from_translation(translation) * Mat4::from_mat3(mat3)`
310 #[inline]
311 #[must_use]
312 pub fn from_mat3_translation(mat3: Mat3, translation: Vec3) -> Self {
313 Self::from_cols(
314 Vec4::from((mat3.x_axis, 0.0)),
315 Vec4::from((mat3.y_axis, 0.0)),
316 Vec4::from((mat3.z_axis, 0.0)),
317 Vec4::from((translation, 1.0)),
318 )
319 }
320
321 /// Creates an affine transformation matrix from the given 3x3 linear transformation
322 /// matrix.
323 ///
324 /// The resulting matrix can be used to transform 3D points and vectors. See
325 /// [`Self::transform_point3()`] and [`Self::transform_vector3()`].
326 #[inline]
327 #[must_use]
328 pub fn from_mat3a(m: Mat3A) -> Self {
329 Self::from_cols(
330 Vec4::from((m.x_axis, 0.0)),
331 Vec4::from((m.y_axis, 0.0)),
332 Vec4::from((m.z_axis, 0.0)),
333 Vec4::W,
334 )
335 }
336
337 /// Creates an affine transformation matrix from the given 3D `translation`.
338 ///
339 /// The resulting matrix can be used to transform 3D points and vectors. See
340 /// [`Self::transform_point3()`] and [`Self::transform_vector3()`].
341 #[inline]
342 #[must_use]
343 pub fn from_translation(translation: Vec3) -> Self {
344 Self::from_cols(
345 Vec4::X,
346 Vec4::Y,
347 Vec4::Z,
348 Vec4::new(translation.x, translation.y, translation.z, 1.0),
349 )
350 }
351
352 /// Creates an affine transformation matrix containing a 3D rotation around a normalized
353 /// rotation `axis` of `angle` (in radians).
354 ///
355 /// The resulting matrix can be used to transform 3D points and vectors. See
356 /// [`Self::transform_point3()`] and [`Self::transform_vector3()`].
357 ///
358 /// # Panics
359 ///
360 /// Will panic if `axis` is not normalized when `glam_assert` is enabled.
361 #[inline]
362 #[must_use]
363 pub fn from_axis_angle(axis: Vec3, angle: f32) -> Self {
364 glam_assert!(axis.is_normalized());
365
366 let (sin, cos) = math::sin_cos(angle);
367 let axis_sin = axis.mul(sin);
368 let axis_sq = axis.mul(axis);
369 let omc = 1.0 - cos;
370 let xyomc = axis.x * axis.y * omc;
371 let xzomc = axis.x * axis.z * omc;
372 let yzomc = axis.y * axis.z * omc;
373 Self::from_cols(
374 Vec4::new(
375 axis_sq.x * omc + cos,
376 xyomc + axis_sin.z,
377 xzomc - axis_sin.y,
378 0.0,
379 ),
380 Vec4::new(
381 xyomc - axis_sin.z,
382 axis_sq.y * omc + cos,
383 yzomc + axis_sin.x,
384 0.0,
385 ),
386 Vec4::new(
387 xzomc + axis_sin.y,
388 yzomc - axis_sin.x,
389 axis_sq.z * omc + cos,
390 0.0,
391 ),
392 Vec4::W,
393 )
394 }
395
396 /// Creates a affine transformation matrix containing a rotation from the given euler
397 /// rotation sequence and angles (in radians).
398 ///
399 /// The resulting matrix can be used to transform 3D points and vectors. See
400 /// [`Self::transform_point3()`] and [`Self::transform_vector3()`].
401 #[inline]
402 #[must_use]
403 pub fn from_euler(order: EulerRot, a: f32, b: f32, c: f32) -> Self {
404 Self::from_euler_angles(order, a, b, c)
405 }
406
407 /// Extract Euler angles with the given Euler rotation order.
408 ///
409 /// Note if the upper 3x3 matrix contain scales, shears, or other non-rotation transformations
410 /// then the resulting Euler angles will be ill-defined.
411 ///
412 /// # Panics
413 ///
414 /// Will panic if any column of the upper 3x3 rotation matrix is not normalized when
415 /// `glam_assert` is enabled.
416 #[inline]
417 #[must_use]
418 pub fn to_euler(&self, order: EulerRot) -> (f32, f32, f32) {
419 glam_assert!(
420 self.x_axis.xyz().is_normalized()
421 && self.y_axis.xyz().is_normalized()
422 && self.z_axis.xyz().is_normalized()
423 );
424 self.to_euler_angles(order)
425 }
426
427 /// Creates an affine transformation matrix containing a 3D rotation around the x axis of
428 /// `angle` (in radians).
429 ///
430 /// The resulting matrix can be used to transform 3D points and vectors. See
431 /// [`Self::transform_point3()`] and [`Self::transform_vector3()`].
432 #[inline]
433 #[must_use]
434 pub fn from_rotation_x(angle: f32) -> Self {
435 let (sina, cosa) = math::sin_cos(angle);
436 Self::from_cols(
437 Vec4::X,
438 Vec4::new(0.0, cosa, sina, 0.0),
439 Vec4::new(0.0, -sina, cosa, 0.0),
440 Vec4::W,
441 )
442 }
443
444 /// Creates an affine transformation matrix containing a 3D rotation around the y axis of
445 /// `angle` (in radians).
446 ///
447 /// The resulting matrix can be used to transform 3D points and vectors. See
448 /// [`Self::transform_point3()`] and [`Self::transform_vector3()`].
449 #[inline]
450 #[must_use]
451 pub fn from_rotation_y(angle: f32) -> Self {
452 let (sina, cosa) = math::sin_cos(angle);
453 Self::from_cols(
454 Vec4::new(cosa, 0.0, -sina, 0.0),
455 Vec4::Y,
456 Vec4::new(sina, 0.0, cosa, 0.0),
457 Vec4::W,
458 )
459 }
460
461 /// Creates an affine transformation matrix containing a 3D rotation around the z axis of
462 /// `angle` (in radians).
463 ///
464 /// The resulting matrix can be used to transform 3D points and vectors. See
465 /// [`Self::transform_point3()`] and [`Self::transform_vector3()`].
466 #[inline]
467 #[must_use]
468 pub fn from_rotation_z(angle: f32) -> Self {
469 let (sina, cosa) = math::sin_cos(angle);
470 Self::from_cols(
471 Vec4::new(cosa, sina, 0.0, 0.0),
472 Vec4::new(-sina, cosa, 0.0, 0.0),
473 Vec4::Z,
474 Vec4::W,
475 )
476 }
477
478 /// Creates an affine transformation matrix containing the given 3D non-uniform `scale`.
479 ///
480 /// The resulting matrix can be used to transform 3D points and vectors. See
481 /// [`Self::transform_point3()`] and [`Self::transform_vector3()`].
482 ///
483 /// # Panics
484 ///
485 /// Will panic if all elements of `scale` are zero when `glam_assert` is enabled.
486 #[inline]
487 #[must_use]
488 pub fn from_scale(scale: Vec3) -> Self {
489 // Do not panic as long as any component is non-zero
490 glam_assert!(scale.cmpne(Vec3::ZERO).any());
491
492 Self::from_cols(
493 Vec4::new(scale.x, 0.0, 0.0, 0.0),
494 Vec4::new(0.0, scale.y, 0.0, 0.0),
495 Vec4::new(0.0, 0.0, scale.z, 0.0),
496 Vec4::W,
497 )
498 }
499
500 /// Creates a 4x4 matrix from the first 16 values in `slice`.
501 ///
502 /// # Panics
503 ///
504 /// Panics if `slice` is less than 16 elements long.
505 #[inline]
506 #[must_use]
507 pub const fn from_cols_slice(slice: &[f32]) -> Self {
508 Self::new(
509 slice[0], slice[1], slice[2], slice[3], slice[4], slice[5], slice[6], slice[7],
510 slice[8], slice[9], slice[10], slice[11], slice[12], slice[13], slice[14], slice[15],
511 )
512 }
513
514 /// Writes the columns of `self` to the first 16 elements in `slice`.
515 ///
516 /// # Panics
517 ///
518 /// Panics if `slice` is less than 16 elements long.
519 #[inline]
520 pub fn write_cols_to_slice(self, slice: &mut [f32]) {
521 slice[0] = self.x_axis.x;
522 slice[1] = self.x_axis.y;
523 slice[2] = self.x_axis.z;
524 slice[3] = self.x_axis.w;
525 slice[4] = self.y_axis.x;
526 slice[5] = self.y_axis.y;
527 slice[6] = self.y_axis.z;
528 slice[7] = self.y_axis.w;
529 slice[8] = self.z_axis.x;
530 slice[9] = self.z_axis.y;
531 slice[10] = self.z_axis.z;
532 slice[11] = self.z_axis.w;
533 slice[12] = self.w_axis.x;
534 slice[13] = self.w_axis.y;
535 slice[14] = self.w_axis.z;
536 slice[15] = self.w_axis.w;
537 }
538
539 /// Returns the matrix column for the given `index`.
540 ///
541 /// # Panics
542 ///
543 /// Panics if `index` is greater than 3.
544 #[inline]
545 #[must_use]
546 pub fn col(&self, index: usize) -> Vec4 {
547 match index {
548 0 => self.x_axis,
549 1 => self.y_axis,
550 2 => self.z_axis,
551 3 => self.w_axis,
552 _ => panic!("index out of bounds"),
553 }
554 }
555
556 /// Returns a mutable reference to the matrix column for the given `index`.
557 ///
558 /// # Panics
559 ///
560 /// Panics if `index` is greater than 3.
561 #[inline]
562 pub fn col_mut(&mut self, index: usize) -> &mut Vec4 {
563 match index {
564 0 => &mut self.x_axis,
565 1 => &mut self.y_axis,
566 2 => &mut self.z_axis,
567 3 => &mut self.w_axis,
568 _ => panic!("index out of bounds"),
569 }
570 }
571
572 /// Returns the matrix row for the given `index`.
573 ///
574 /// # Panics
575 ///
576 /// Panics if `index` is greater than 3.
577 #[inline]
578 #[must_use]
579 pub fn row(&self, index: usize) -> Vec4 {
580 match index {
581 0 => Vec4::new(self.x_axis.x, self.y_axis.x, self.z_axis.x, self.w_axis.x),
582 1 => Vec4::new(self.x_axis.y, self.y_axis.y, self.z_axis.y, self.w_axis.y),
583 2 => Vec4::new(self.x_axis.z, self.y_axis.z, self.z_axis.z, self.w_axis.z),
584 3 => Vec4::new(self.x_axis.w, self.y_axis.w, self.z_axis.w, self.w_axis.w),
585 _ => panic!("index out of bounds"),
586 }
587 }
588
589 /// Returns `true` if, and only if, all elements are finite.
590 /// If any element is either `NaN`, positive or negative infinity, this will return `false`.
591 #[inline]
592 #[must_use]
593 pub fn is_finite(&self) -> bool {
594 self.x_axis.is_finite()
595 && self.y_axis.is_finite()
596 && self.z_axis.is_finite()
597 && self.w_axis.is_finite()
598 }
599
600 /// Returns `true` if any elements are `NaN`.
601 #[inline]
602 #[must_use]
603 pub fn is_nan(&self) -> bool {
604 self.x_axis.is_nan() || self.y_axis.is_nan() || self.z_axis.is_nan() || self.w_axis.is_nan()
605 }
606
607 /// Returns the transpose of `self`.
608 #[inline]
609 #[must_use]
610 pub fn transpose(&self) -> Self {
611 // Based on https://github.com/microsoft/DirectXMath `XMMatrixTranspose`
612 let tmp0 = simd_swizzle!(self.x_axis.0, self.y_axis.0, [0, 1, 4, 5]);
613 let tmp1 = simd_swizzle!(self.x_axis.0, self.y_axis.0, [2, 3, 6, 7]);
614 let tmp2 = simd_swizzle!(self.z_axis.0, self.w_axis.0, [0, 1, 4, 5]);
615 let tmp3 = simd_swizzle!(self.z_axis.0, self.w_axis.0, [2, 3, 6, 7]);
616
617 Self {
618 x_axis: Vec4(simd_swizzle!(tmp0, tmp2, [0, 2, 4, 6])),
619 y_axis: Vec4(simd_swizzle!(tmp0, tmp2, [1, 3, 5, 7])),
620 z_axis: Vec4(simd_swizzle!(tmp1, tmp3, [0, 2, 4, 6])),
621 w_axis: Vec4(simd_swizzle!(tmp1, tmp3, [1, 3, 5, 7])),
622 }
623 }
624
625 /// Returns the determinant of `self`.
626 #[must_use]
627 pub fn determinant(&self) -> f32 {
628 // Based on https://github.com/g-truc/glm `glm_mat4_determinant`
629 let swp2a = simd_swizzle!(self.z_axis.0, [2, 1, 1, 0]);
630 let swp3a = simd_swizzle!(self.w_axis.0, [3, 3, 2, 3]);
631 let swp2b = simd_swizzle!(self.z_axis.0, [3, 3, 2, 3]);
632 let swp3b = simd_swizzle!(self.w_axis.0, [2, 1, 1, 0]);
633 let swp2c = simd_swizzle!(self.z_axis.0, [2, 1, 0, 0]);
634 let swp3c = simd_swizzle!(self.w_axis.0, [0, 0, 2, 1]);
635
636 let mula = swp2a * swp3a;
637 let mulb = swp2b * swp3b;
638 let mulc = swp2c * swp3c;
639 let sube = mula - mulb;
640 let subf = simd_swizzle!(mulc, [2, 3, 2, 3]) - mulc;
641
642 let subfaca = simd_swizzle!(sube, [0, 0, 1, 2]);
643 let swpfaca = simd_swizzle!(self.y_axis.0, [1, 0, 0, 0]);
644 let mulfaca = swpfaca * subfaca;
645
646 let subtmpb = simd_swizzle!(sube, subf, [1, 3, 4, 4]);
647 let subfacb = simd_swizzle!(subtmpb, [0, 1, 1, 3]);
648 let swpfacb = simd_swizzle!(self.y_axis.0, [2, 2, 1, 1]);
649 let mulfacb = swpfacb * subfacb;
650
651 let subres = mulfaca - mulfacb;
652 let subtmpc = simd_swizzle!(sube, subf, [2, 2, 4, 5]);
653 let subfacc = simd_swizzle!(subtmpc, [0, 2, 3, 3]);
654 let swpfacc = simd_swizzle!(self.y_axis.0, [3, 3, 3, 2]);
655 let mulfacc = swpfacc * subfacc;
656
657 let addres = subres + mulfacc;
658 let detcof = addres * f32x4::from_array([1.0, -1.0, 1.0, -1.0]);
659
660 dot4(self.x_axis.0, detcof)
661 }
662
663 /// Returns the inverse of `self`.
664 ///
665 /// If the matrix is not invertible the returned matrix will be invalid.
666 ///
667 /// # Panics
668 ///
669 /// Will panic if the determinant of `self` is zero when `glam_assert` is enabled.
670 #[must_use]
671 pub fn inverse(&self) -> Self {
672 // Based on https://github.com/g-truc/glm `glm_mat4_inverse`
673 let fac0 = {
674 let swp0a = simd_swizzle!(self.w_axis.0, self.z_axis.0, [3, 3, 7, 7]);
675 let swp0b = simd_swizzle!(self.w_axis.0, self.z_axis.0, [2, 2, 6, 6]);
676
677 let swp00 = simd_swizzle!(self.z_axis.0, self.y_axis.0, [2, 2, 6, 6]);
678 let swp01 = simd_swizzle!(swp0a, [0, 0, 0, 2]);
679 let swp02 = simd_swizzle!(swp0b, [0, 0, 0, 2]);
680 let swp03 = simd_swizzle!(self.z_axis.0, self.y_axis.0, [3, 3, 7, 7]);
681
682 let mul00 = swp00 * swp01;
683 let mul01 = swp02 * swp03;
684 mul00 - mul01
685 };
686 let fac1 = {
687 let swp0a = simd_swizzle!(self.w_axis.0, self.z_axis.0, [3, 3, 7, 7]);
688 let swp0b = simd_swizzle!(self.w_axis.0, self.z_axis.0, [1, 1, 5, 5]);
689
690 let swp00 = simd_swizzle!(self.z_axis.0, self.y_axis.0, [1, 1, 5, 5]);
691 let swp01 = simd_swizzle!(swp0a, [0, 0, 0, 2]);
692 let swp02 = simd_swizzle!(swp0b, [0, 0, 0, 2]);
693 let swp03 = simd_swizzle!(self.z_axis.0, self.y_axis.0, [3, 3, 7, 7]);
694
695 let mul00 = swp00 * swp01;
696 let mul01 = swp02 * swp03;
697 mul00 - mul01
698 };
699 let fac2 = {
700 let swp0a = simd_swizzle!(self.w_axis.0, self.z_axis.0, [2, 2, 6, 6]);
701 let swp0b = simd_swizzle!(self.w_axis.0, self.z_axis.0, [1, 1, 5, 5]);
702
703 let swp00 = simd_swizzle!(self.z_axis.0, self.y_axis.0, [1, 1, 5, 5]);
704 let swp01 = simd_swizzle!(swp0a, [0, 0, 0, 2]);
705 let swp02 = simd_swizzle!(swp0b, [0, 0, 0, 2]);
706 let swp03 = simd_swizzle!(self.z_axis.0, self.y_axis.0, [2, 2, 6, 6]);
707
708 let mul00 = swp00 * swp01;
709 let mul01 = swp02 * swp03;
710 mul00 - mul01
711 };
712 let fac3 = {
713 let swp0a = simd_swizzle!(self.w_axis.0, self.z_axis.0, [3, 3, 7, 7]);
714 let swp0b = simd_swizzle!(self.w_axis.0, self.z_axis.0, [0, 0, 4, 4]);
715
716 let swp00 = simd_swizzle!(self.z_axis.0, self.y_axis.0, [0, 0, 4, 4]);
717 let swp01 = simd_swizzle!(swp0a, [0, 0, 0, 2]);
718 let swp02 = simd_swizzle!(swp0b, [0, 0, 0, 2]);
719 let swp03 = simd_swizzle!(self.z_axis.0, self.y_axis.0, [3, 3, 7, 7]);
720
721 let mul00 = swp00 * swp01;
722 let mul01 = swp02 * swp03;
723 mul00 - mul01
724 };
725 let fac4 = {
726 let swp0a = simd_swizzle!(self.w_axis.0, self.z_axis.0, [2, 2, 6, 6]);
727 let swp0b = simd_swizzle!(self.w_axis.0, self.z_axis.0, [0, 0, 4, 4]);
728
729 let swp00 = simd_swizzle!(self.z_axis.0, self.y_axis.0, [0, 0, 4, 4]);
730 let swp01 = simd_swizzle!(swp0a, [0, 0, 0, 2]);
731 let swp02 = simd_swizzle!(swp0b, [0, 0, 0, 2]);
732 let swp03 = simd_swizzle!(self.z_axis.0, self.y_axis.0, [2, 2, 6, 6]);
733
734 let mul00 = swp00 * swp01;
735 let mul01 = swp02 * swp03;
736 mul00 - mul01
737 };
738 let fac5 = {
739 let swp0a = simd_swizzle!(self.w_axis.0, self.z_axis.0, [1, 1, 5, 5]);
740 let swp0b = simd_swizzle!(self.w_axis.0, self.z_axis.0, [0, 0, 4, 4]);
741
742 let swp00 = simd_swizzle!(self.z_axis.0, self.y_axis.0, [0, 0, 4, 4]);
743 let swp01 = simd_swizzle!(swp0a, [0, 0, 0, 2]);
744 let swp02 = simd_swizzle!(swp0b, [0, 0, 0, 2]);
745 let swp03 = simd_swizzle!(self.z_axis.0, self.y_axis.0, [1, 1, 5, 5]);
746
747 let mul00 = swp00 * swp01;
748 let mul01 = swp02 * swp03;
749 mul00 - mul01
750 };
751 let sign_a = f32x4::from_array([-1.0, 1.0, -1.0, 1.0]);
752 let sign_b = f32x4::from_array([1.0, -1.0, 1.0, -1.0]);
753
754 let temp0 = simd_swizzle!(self.y_axis.0, self.x_axis.0, [0, 0, 4, 4]);
755 let vec0 = simd_swizzle!(temp0, [0, 2, 2, 2]);
756
757 let temp1 = simd_swizzle!(self.y_axis.0, self.x_axis.0, [1, 1, 5, 5]);
758 let vec1 = simd_swizzle!(temp1, [0, 2, 2, 2]);
759
760 let temp2 = simd_swizzle!(self.y_axis.0, self.x_axis.0, [2, 2, 6, 6]);
761 let vec2 = simd_swizzle!(temp2, [0, 2, 2, 2]);
762
763 let temp3 = simd_swizzle!(self.y_axis.0, self.x_axis.0, [3, 3, 7, 7]);
764 let vec3 = simd_swizzle!(temp3, [0, 2, 2, 2]);
765
766 let mul00 = vec1 * fac0;
767 let mul01 = vec2 * fac1;
768 let mul02 = vec3 * fac2;
769 let sub00 = mul00 - mul01;
770 let add00 = sub00 + mul02;
771 let inv0 = sign_b * add00;
772
773 let mul03 = vec0 * fac0;
774 let mul04 = vec2 * fac3;
775 let mul05 = vec3 * fac4;
776 let sub01 = mul03 - mul04;
777 let add01 = sub01 + mul05;
778 let inv1 = sign_a * add01;
779
780 let mul06 = vec0 * fac1;
781 let mul07 = vec1 * fac3;
782 let mul08 = vec3 * fac5;
783 let sub02 = mul06 - mul07;
784 let add02 = sub02 + mul08;
785 let inv2 = sign_b * add02;
786
787 let mul09 = vec0 * fac2;
788 let mul10 = vec1 * fac4;
789 let mul11 = vec2 * fac5;
790 let sub03 = mul09 - mul10;
791 let add03 = sub03 + mul11;
792 let inv3 = sign_a * add03;
793
794 let row0 = simd_swizzle!(inv0, inv1, [0, 0, 4, 4]);
795 let row1 = simd_swizzle!(inv2, inv3, [0, 0, 4, 4]);
796 let row2 = simd_swizzle!(row0, row1, [0, 2, 4, 6]);
797
798 let dot0 = dot4(self.x_axis.0, row2);
799 glam_assert!(dot0 != 0.0);
800
801 let rcp0 = f32x4::splat(dot0.recip());
802
803 Self {
804 x_axis: Vec4(inv0 * rcp0),
805 y_axis: Vec4(inv1 * rcp0),
806 z_axis: Vec4(inv2 * rcp0),
807 w_axis: Vec4(inv3 * rcp0),
808 }
809 }
810
811 /// Creates a left-handed view matrix using a camera position, a facing direction and an up
812 /// direction
813 ///
814 /// For a view coordinate system with `+X=right`, `+Y=up` and `+Z=forward`.
815 ///
816 /// # Panics
817 ///
818 /// Will panic if `dir` or `up` are not normalized when `glam_assert` is enabled.
819 #[inline]
820 #[must_use]
821 pub fn look_to_lh(eye: Vec3, dir: Vec3, up: Vec3) -> Self {
822 Self::look_to_rh(eye, -dir, up)
823 }
824
825 /// Creates a right-handed view matrix using a camera position, a facing direction, and an up
826 /// direction.
827 ///
828 /// For a view coordinate system with `+X=right`, `+Y=up` and `+Z=back`.
829 ///
830 /// # Panics
831 ///
832 /// Will panic if `dir` or `up` are not normalized when `glam_assert` is enabled.
833 #[inline]
834 #[must_use]
835 pub fn look_to_rh(eye: Vec3, dir: Vec3, up: Vec3) -> Self {
836 glam_assert!(dir.is_normalized());
837 glam_assert!(up.is_normalized());
838 let f = dir;
839 let s = f.cross(up).normalize();
840 let u = s.cross(f);
841
842 Self::from_cols(
843 Vec4::new(s.x, u.x, -f.x, 0.0),
844 Vec4::new(s.y, u.y, -f.y, 0.0),
845 Vec4::new(s.z, u.z, -f.z, 0.0),
846 Vec4::new(-eye.dot(s), -eye.dot(u), eye.dot(f), 1.0),
847 )
848 }
849
850 /// Creates a left-handed view matrix using a camera position, a focal points and an up
851 /// direction.
852 ///
853 /// For a view coordinate system with `+X=right`, `+Y=up` and `+Z=forward`.
854 ///
855 /// # Panics
856 ///
857 /// Will panic if `up` is not normalized when `glam_assert` is enabled.
858 #[inline]
859 #[must_use]
860 pub fn look_at_lh(eye: Vec3, center: Vec3, up: Vec3) -> Self {
861 Self::look_to_lh(eye, center.sub(eye).normalize(), up)
862 }
863
864 /// Creates a right-handed view matrix using a camera position, a focal point, and an up
865 /// direction.
866 ///
867 /// For a view coordinate system with `+X=right`, `+Y=up` and `+Z=back`.
868 ///
869 /// # Panics
870 ///
871 /// Will panic if `up` is not normalized when `glam_assert` is enabled.
872 #[inline]
873 pub fn look_at_rh(eye: Vec3, center: Vec3, up: Vec3) -> Self {
874 Self::look_to_rh(eye, center.sub(eye).normalize(), up)
875 }
876
877 /// Creates a right-handed perspective projection matrix with [-1,1] depth range.
878 ///
879 /// This is the same as the OpenGL `glFrustum` function.
880 ///
881 /// See <https://registry.khronos.org/OpenGL-Refpages/gl2.1/xhtml/glFrustum.xml>
882 #[inline]
883 #[must_use]
884 pub fn frustum_rh_gl(
885 left: f32,
886 right: f32,
887 bottom: f32,
888 top: f32,
889 z_near: f32,
890 z_far: f32,
891 ) -> Self {
892 let inv_width = 1.0 / (right - left);
893 let inv_height = 1.0 / (top - bottom);
894 let inv_depth = 1.0 / (z_far - z_near);
895 let a = (right + left) * inv_width;
896 let b = (top + bottom) * inv_height;
897 let c = -(z_far + z_near) * inv_depth;
898 let d = -(2.0 * z_far * z_near) * inv_depth;
899 let two_z_near = 2.0 * z_near;
900 Self::from_cols(
901 Vec4::new(two_z_near * inv_width, 0.0, 0.0, 0.0),
902 Vec4::new(0.0, two_z_near * inv_height, 0.0, 0.0),
903 Vec4::new(a, b, c, -1.0),
904 Vec4::new(0.0, 0.0, d, 0.0),
905 )
906 }
907
908 /// Creates a left-handed perspective projection matrix with `[0,1]` depth range.
909 ///
910 /// # Panics
911 ///
912 /// Will panic if `z_near` or `z_far` are less than or equal to zero when `glam_assert` is
913 /// enabled.
914 #[inline]
915 #[must_use]
916 pub fn frustum_lh(
917 left: f32,
918 right: f32,
919 bottom: f32,
920 top: f32,
921 z_near: f32,
922 z_far: f32,
923 ) -> Self {
924 glam_assert!(z_near > 0.0 && z_far > 0.0);
925 let inv_width = 1.0 / (right - left);
926 let inv_height = 1.0 / (top - bottom);
927 let inv_depth = 1.0 / (z_far - z_near);
928 let a = (right + left) * inv_width;
929 let b = (top + bottom) * inv_height;
930 let c = z_far * inv_depth;
931 let d = -(z_far * z_near) * inv_depth;
932 let two_z_near = 2.0 * z_near;
933 Self::from_cols(
934 Vec4::new(two_z_near * inv_width, 0.0, 0.0, 0.0),
935 Vec4::new(0.0, two_z_near * inv_height, 0.0, 0.0),
936 Vec4::new(a, b, c, 1.0),
937 Vec4::new(0.0, 0.0, d, 0.0),
938 )
939 }
940
941 /// Creates a right-handed perspective projection matrix with `[0,1]` depth range.
942 ///
943 /// # Panics
944 ///
945 /// Will panic if `z_near` or `z_far` are less than or equal to zero when `glam_assert` is
946 /// enabled.
947 #[inline]
948 #[must_use]
949 pub fn frustum_rh(
950 left: f32,
951 right: f32,
952 bottom: f32,
953 top: f32,
954 z_near: f32,
955 z_far: f32,
956 ) -> Self {
957 glam_assert!(z_near > 0.0 && z_far > 0.0);
958 let inv_width = 1.0 / (right - left);
959 let inv_height = 1.0 / (top - bottom);
960 let inv_depth = 1.0 / (z_far - z_near);
961 let a = (right + left) * inv_width;
962 let b = (top + bottom) * inv_height;
963 let c = -z_far * inv_depth;
964 let d = -(z_far * z_near) * inv_depth;
965 let two_z_near = 2.0 * z_near;
966 Self::from_cols(
967 Vec4::new(two_z_near * inv_width, 0.0, 0.0, 0.0),
968 Vec4::new(0.0, two_z_near * inv_height, 0.0, 0.0),
969 Vec4::new(a, b, c, -1.0),
970 Vec4::new(0.0, 0.0, d, 0.0),
971 )
972 }
973
974 /// Creates a right-handed perspective projection matrix with `[-1,1]` depth range.
975 ///
976 /// Useful to map the standard right-handed coordinate system into what OpenGL expects.
977 ///
978 /// This is the same as the OpenGL `gluPerspective` function.
979 /// See <https://www.khronos.org/registry/OpenGL-Refpages/gl2.1/xhtml/gluPerspective.xml>
980 #[inline]
981 #[must_use]
982 pub fn perspective_rh_gl(
983 fov_y_radians: f32,
984 aspect_ratio: f32,
985 z_near: f32,
986 z_far: f32,
987 ) -> Self {
988 let inv_length = 1.0 / (z_near - z_far);
989 let f = 1.0 / math::tan(0.5 * fov_y_radians);
990 let a = f / aspect_ratio;
991 let b = (z_near + z_far) * inv_length;
992 let c = (2.0 * z_near * z_far) * inv_length;
993 Self::from_cols(
994 Vec4::new(a, 0.0, 0.0, 0.0),
995 Vec4::new(0.0, f, 0.0, 0.0),
996 Vec4::new(0.0, 0.0, b, -1.0),
997 Vec4::new(0.0, 0.0, c, 0.0),
998 )
999 }
1000
1001 /// Creates a left-handed perspective projection matrix with `[0,1]` depth range.
1002 ///
1003 /// Useful to map the standard left-handed coordinate system into what WebGPU/Metal/Direct3D expect.
1004 ///
1005 /// # Panics
1006 ///
1007 /// Will panic if `z_near` or `z_far` are less than or equal to zero when `glam_assert` is
1008 /// enabled.
1009 #[inline]
1010 #[must_use]
1011 pub fn perspective_lh(fov_y_radians: f32, aspect_ratio: f32, z_near: f32, z_far: f32) -> Self {
1012 glam_assert!(z_near > 0.0 && z_far > 0.0);
1013 let (sin_fov, cos_fov) = math::sin_cos(0.5 * fov_y_radians);
1014 let h = cos_fov / sin_fov;
1015 let w = h / aspect_ratio;
1016 let r = z_far / (z_far - z_near);
1017 Self::from_cols(
1018 Vec4::new(w, 0.0, 0.0, 0.0),
1019 Vec4::new(0.0, h, 0.0, 0.0),
1020 Vec4::new(0.0, 0.0, r, 1.0),
1021 Vec4::new(0.0, 0.0, -r * z_near, 0.0),
1022 )
1023 }
1024
1025 /// Creates a right-handed perspective projection matrix with `[0,1]` depth range.
1026 ///
1027 /// Useful to map the standard right-handed coordinate system into what WebGPU/Metal/Direct3D expect.
1028 ///
1029 /// # Panics
1030 ///
1031 /// Will panic if `z_near` or `z_far` are less than or equal to zero when `glam_assert` is
1032 /// enabled.
1033 #[inline]
1034 #[must_use]
1035 pub fn perspective_rh(fov_y_radians: f32, aspect_ratio: f32, z_near: f32, z_far: f32) -> Self {
1036 glam_assert!(z_near > 0.0 && z_far > 0.0);
1037 let (sin_fov, cos_fov) = math::sin_cos(0.5 * fov_y_radians);
1038 let h = cos_fov / sin_fov;
1039 let w = h / aspect_ratio;
1040 let r = z_far / (z_near - z_far);
1041 Self::from_cols(
1042 Vec4::new(w, 0.0, 0.0, 0.0),
1043 Vec4::new(0.0, h, 0.0, 0.0),
1044 Vec4::new(0.0, 0.0, r, -1.0),
1045 Vec4::new(0.0, 0.0, r * z_near, 0.0),
1046 )
1047 }
1048
1049 /// Creates an infinite left-handed perspective projection matrix with `[0,1]` depth range.
1050 ///
1051 /// Like `perspective_lh`, but with an infinite value for `z_far`.
1052 /// The result is that points near `z_near` are mapped to depth `0`, and as they move towards infinity the depth approaches `1`.
1053 ///
1054 /// # Panics
1055 ///
1056 /// Will panic if `z_near` or `z_far` are less than or equal to zero when `glam_assert` is
1057 /// enabled.
1058 #[inline]
1059 #[must_use]
1060 pub fn perspective_infinite_lh(fov_y_radians: f32, aspect_ratio: f32, z_near: f32) -> Self {
1061 glam_assert!(z_near > 0.0);
1062 let (sin_fov, cos_fov) = math::sin_cos(0.5 * fov_y_radians);
1063 let h = cos_fov / sin_fov;
1064 let w = h / aspect_ratio;
1065 Self::from_cols(
1066 Vec4::new(w, 0.0, 0.0, 0.0),
1067 Vec4::new(0.0, h, 0.0, 0.0),
1068 Vec4::new(0.0, 0.0, 1.0, 1.0),
1069 Vec4::new(0.0, 0.0, -z_near, 0.0),
1070 )
1071 }
1072
1073 /// Creates an infinite reverse left-handed perspective projection matrix with `[0,1]` depth range.
1074 ///
1075 /// Similar to `perspective_infinite_lh`, but maps `Z = z_near` to a depth of `1` and `Z = infinity` to a depth of `0`.
1076 ///
1077 /// # Panics
1078 ///
1079 /// Will panic if `z_near` is less than or equal to zero when `glam_assert` is enabled.
1080 #[inline]
1081 #[must_use]
1082 pub fn perspective_infinite_reverse_lh(
1083 fov_y_radians: f32,
1084 aspect_ratio: f32,
1085 z_near: f32,
1086 ) -> Self {
1087 glam_assert!(z_near > 0.0);
1088 let (sin_fov, cos_fov) = math::sin_cos(0.5 * fov_y_radians);
1089 let h = cos_fov / sin_fov;
1090 let w = h / aspect_ratio;
1091 Self::from_cols(
1092 Vec4::new(w, 0.0, 0.0, 0.0),
1093 Vec4::new(0.0, h, 0.0, 0.0),
1094 Vec4::new(0.0, 0.0, 0.0, 1.0),
1095 Vec4::new(0.0, 0.0, z_near, 0.0),
1096 )
1097 }
1098
1099 /// Creates an infinite right-handed perspective projection matrix with `[0,1]` depth range.
1100 ///
1101 /// Like `perspective_rh`, but with an infinite value for `z_far`.
1102 /// The result is that points near `z_near` are mapped to depth `0`, and as they move towards infinity the depth approaches `1`.
1103 ///
1104 /// # Panics
1105 ///
1106 /// Will panic if `z_near` or `z_far` are less than or equal to zero when `glam_assert` is
1107 /// enabled.
1108 #[inline]
1109 #[must_use]
1110 pub fn perspective_infinite_rh(fov_y_radians: f32, aspect_ratio: f32, z_near: f32) -> Self {
1111 glam_assert!(z_near > 0.0);
1112 let f = 1.0 / math::tan(0.5 * fov_y_radians);
1113 Self::from_cols(
1114 Vec4::new(f / aspect_ratio, 0.0, 0.0, 0.0),
1115 Vec4::new(0.0, f, 0.0, 0.0),
1116 Vec4::new(0.0, 0.0, -1.0, -1.0),
1117 Vec4::new(0.0, 0.0, -z_near, 0.0),
1118 )
1119 }
1120
1121 /// Creates an infinite reverse right-handed perspective projection matrix with `[0,1]` depth range.
1122 ///
1123 /// Similar to `perspective_infinite_rh`, but maps `Z = z_near` to a depth of `1` and `Z = infinity` to a depth of `0`.
1124 ///
1125 /// # Panics
1126 ///
1127 /// Will panic if `z_near` is less than or equal to zero when `glam_assert` is enabled.
1128 #[inline]
1129 #[must_use]
1130 pub fn perspective_infinite_reverse_rh(
1131 fov_y_radians: f32,
1132 aspect_ratio: f32,
1133 z_near: f32,
1134 ) -> Self {
1135 glam_assert!(z_near > 0.0);
1136 let f = 1.0 / math::tan(0.5 * fov_y_radians);
1137 Self::from_cols(
1138 Vec4::new(f / aspect_ratio, 0.0, 0.0, 0.0),
1139 Vec4::new(0.0, f, 0.0, 0.0),
1140 Vec4::new(0.0, 0.0, 0.0, -1.0),
1141 Vec4::new(0.0, 0.0, z_near, 0.0),
1142 )
1143 }
1144
1145 /// Creates a right-handed orthographic projection matrix with `[-1,1]` depth
1146 /// range. This is the same as the OpenGL `glOrtho` function in OpenGL.
1147 /// See
1148 /// <https://www.khronos.org/registry/OpenGL-Refpages/gl2.1/xhtml/glOrtho.xml>
1149 ///
1150 /// Useful to map a right-handed coordinate system to the normalized device coordinates that OpenGL expects.
1151 #[inline]
1152 #[must_use]
1153 pub fn orthographic_rh_gl(
1154 left: f32,
1155 right: f32,
1156 bottom: f32,
1157 top: f32,
1158 near: f32,
1159 far: f32,
1160 ) -> Self {
1161 let a = 2.0 / (right - left);
1162 let b = 2.0 / (top - bottom);
1163 let c = -2.0 / (far - near);
1164 let tx = -(right + left) / (right - left);
1165 let ty = -(top + bottom) / (top - bottom);
1166 let tz = -(far + near) / (far - near);
1167
1168 Self::from_cols(
1169 Vec4::new(a, 0.0, 0.0, 0.0),
1170 Vec4::new(0.0, b, 0.0, 0.0),
1171 Vec4::new(0.0, 0.0, c, 0.0),
1172 Vec4::new(tx, ty, tz, 1.0),
1173 )
1174 }
1175
1176 /// Creates a left-handed orthographic projection matrix with `[0,1]` depth range.
1177 ///
1178 /// Useful to map a left-handed coordinate system to the normalized device coordinates that WebGPU/Direct3D/Metal expect.
1179 #[inline]
1180 #[must_use]
1181 pub fn orthographic_lh(
1182 left: f32,
1183 right: f32,
1184 bottom: f32,
1185 top: f32,
1186 near: f32,
1187 far: f32,
1188 ) -> Self {
1189 let rcp_width = 1.0 / (right - left);
1190 let rcp_height = 1.0 / (top - bottom);
1191 let r = 1.0 / (far - near);
1192 Self::from_cols(
1193 Vec4::new(rcp_width + rcp_width, 0.0, 0.0, 0.0),
1194 Vec4::new(0.0, rcp_height + rcp_height, 0.0, 0.0),
1195 Vec4::new(0.0, 0.0, r, 0.0),
1196 Vec4::new(
1197 -(left + right) * rcp_width,
1198 -(top + bottom) * rcp_height,
1199 -r * near,
1200 1.0,
1201 ),
1202 )
1203 }
1204
1205 /// Creates a right-handed orthographic projection matrix with `[0,1]` depth range.
1206 ///
1207 /// Useful to map a right-handed coordinate system to the normalized device coordinates that WebGPU/Direct3D/Metal expect.
1208 #[inline]
1209 #[must_use]
1210 pub fn orthographic_rh(
1211 left: f32,
1212 right: f32,
1213 bottom: f32,
1214 top: f32,
1215 near: f32,
1216 far: f32,
1217 ) -> Self {
1218 let rcp_width = 1.0 / (right - left);
1219 let rcp_height = 1.0 / (top - bottom);
1220 let r = 1.0 / (near - far);
1221 Self::from_cols(
1222 Vec4::new(rcp_width + rcp_width, 0.0, 0.0, 0.0),
1223 Vec4::new(0.0, rcp_height + rcp_height, 0.0, 0.0),
1224 Vec4::new(0.0, 0.0, r, 0.0),
1225 Vec4::new(
1226 -(left + right) * rcp_width,
1227 -(top + bottom) * rcp_height,
1228 r * near,
1229 1.0,
1230 ),
1231 )
1232 }
1233
1234 /// Transforms the given 3D vector as a point, applying perspective correction.
1235 ///
1236 /// This is the equivalent of multiplying the 3D vector as a 4D vector where `w` is `1.0`.
1237 /// The perspective divide is performed meaning the resulting 3D vector is divided by `w`.
1238 ///
1239 /// This method assumes that `self` contains a projective transform.
1240 #[inline]
1241 #[must_use]
1242 pub fn project_point3(&self, rhs: Vec3) -> Vec3 {
1243 let mut res = self.x_axis.mul(rhs.x);
1244 res = self.y_axis.mul(rhs.y).add(res);
1245 res = self.z_axis.mul(rhs.z).add(res);
1246 res = self.w_axis.add(res);
1247 res = res.div(res.w);
1248 res.xyz()
1249 }
1250
1251 /// Transforms the given 3D vector as a point.
1252 ///
1253 /// This is the equivalent of multiplying the 3D vector as a 4D vector where `w` is
1254 /// `1.0`.
1255 ///
1256 /// This method assumes that `self` contains a valid affine transform. It does not perform
1257 /// a perspective divide, if `self` contains a perspective transform, or if you are unsure,
1258 /// the [`Self::project_point3()`] method should be used instead.
1259 ///
1260 /// # Panics
1261 ///
1262 /// Will panic if the 3rd row of `self` is not `(0, 0, 0, 1)` when `glam_assert` is enabled.
1263 #[inline]
1264 #[must_use]
1265 pub fn transform_point3(&self, rhs: Vec3) -> Vec3 {
1266 glam_assert!(self.row(3).abs_diff_eq(Vec4::W, 1e-6));
1267 let mut res = self.x_axis.mul(rhs.x);
1268 res = self.y_axis.mul(rhs.y).add(res);
1269 res = self.z_axis.mul(rhs.z).add(res);
1270 res = self.w_axis.add(res);
1271 res.xyz()
1272 }
1273
1274 /// Transforms the give 3D vector as a direction.
1275 ///
1276 /// This is the equivalent of multiplying the 3D vector as a 4D vector where `w` is
1277 /// `0.0`.
1278 ///
1279 /// This method assumes that `self` contains a valid affine transform.
1280 ///
1281 /// # Panics
1282 ///
1283 /// Will panic if the 3rd row of `self` is not `(0, 0, 0, 1)` when `glam_assert` is enabled.
1284 #[inline]
1285 #[must_use]
1286 pub fn transform_vector3(&self, rhs: Vec3) -> Vec3 {
1287 glam_assert!(self.row(3).abs_diff_eq(Vec4::W, 1e-6));
1288 let mut res = self.x_axis.mul(rhs.x);
1289 res = self.y_axis.mul(rhs.y).add(res);
1290 res = self.z_axis.mul(rhs.z).add(res);
1291 res.xyz()
1292 }
1293
1294 /// Transforms the given [`Vec3A`] as a 3D point, applying perspective correction.
1295 ///
1296 /// This is the equivalent of multiplying the [`Vec3A`] as a 4D vector where `w` is `1.0`.
1297 /// The perspective divide is performed meaning the resulting 3D vector is divided by `w`.
1298 ///
1299 /// This method assumes that `self` contains a projective transform.
1300 #[inline]
1301 #[must_use]
1302 pub fn project_point3a(&self, rhs: Vec3A) -> Vec3A {
1303 let mut res = self.x_axis.mul(rhs.xxxx());
1304 res = self.y_axis.mul(rhs.yyyy()).add(res);
1305 res = self.z_axis.mul(rhs.zzzz()).add(res);
1306 res = self.w_axis.add(res);
1307 res = res.div(res.wwww());
1308 Vec3A::from_vec4(res)
1309 }
1310
1311 /// Transforms the given [`Vec3A`] as 3D point.
1312 ///
1313 /// This is the equivalent of multiplying the [`Vec3A`] as a 4D vector where `w` is `1.0`.
1314 #[inline]
1315 #[must_use]
1316 pub fn transform_point3a(&self, rhs: Vec3A) -> Vec3A {
1317 glam_assert!(self.row(3).abs_diff_eq(Vec4::W, 1e-6));
1318 let mut res = self.x_axis.mul(rhs.xxxx());
1319 res = self.y_axis.mul(rhs.yyyy()).add(res);
1320 res = self.z_axis.mul(rhs.zzzz()).add(res);
1321 res = self.w_axis.add(res);
1322 Vec3A::from_vec4(res)
1323 }
1324
1325 /// Transforms the give [`Vec3A`] as 3D vector.
1326 ///
1327 /// This is the equivalent of multiplying the [`Vec3A`] as a 4D vector where `w` is `0.0`.
1328 #[inline]
1329 #[must_use]
1330 pub fn transform_vector3a(&self, rhs: Vec3A) -> Vec3A {
1331 glam_assert!(self.row(3).abs_diff_eq(Vec4::W, 1e-6));
1332 let mut res = self.x_axis.mul(rhs.xxxx());
1333 res = self.y_axis.mul(rhs.yyyy()).add(res);
1334 res = self.z_axis.mul(rhs.zzzz()).add(res);
1335 Vec3A::from_vec4(res)
1336 }
1337
1338 /// Transforms a 4D vector.
1339 #[inline]
1340 #[must_use]
1341 pub fn mul_vec4(&self, rhs: Vec4) -> Vec4 {
1342 let mut res = self.x_axis.mul(rhs.xxxx());
1343 res = res.add(self.y_axis.mul(rhs.yyyy()));
1344 res = res.add(self.z_axis.mul(rhs.zzzz()));
1345 res = res.add(self.w_axis.mul(rhs.wwww()));
1346 res
1347 }
1348
1349 /// Multiplies two 4x4 matrices.
1350 #[inline]
1351 #[must_use]
1352 pub fn mul_mat4(&self, rhs: &Self) -> Self {
1353 self.mul(rhs)
1354 }
1355
1356 /// Adds two 4x4 matrices.
1357 #[inline]
1358 #[must_use]
1359 pub fn add_mat4(&self, rhs: &Self) -> Self {
1360 self.add(rhs)
1361 }
1362
1363 /// Subtracts two 4x4 matrices.
1364 #[inline]
1365 #[must_use]
1366 pub fn sub_mat4(&self, rhs: &Self) -> Self {
1367 self.sub(rhs)
1368 }
1369
1370 /// Multiplies a 4x4 matrix by a scalar.
1371 #[inline]
1372 #[must_use]
1373 pub fn mul_scalar(&self, rhs: f32) -> Self {
1374 Self::from_cols(
1375 self.x_axis.mul(rhs),
1376 self.y_axis.mul(rhs),
1377 self.z_axis.mul(rhs),
1378 self.w_axis.mul(rhs),
1379 )
1380 }
1381
1382 /// Divides a 4x4 matrix by a scalar.
1383 #[inline]
1384 #[must_use]
1385 pub fn div_scalar(&self, rhs: f32) -> Self {
1386 let rhs = Vec4::splat(rhs);
1387 Self::from_cols(
1388 self.x_axis.div(rhs),
1389 self.y_axis.div(rhs),
1390 self.z_axis.div(rhs),
1391 self.w_axis.div(rhs),
1392 )
1393 }
1394
1395 /// Returns true if the absolute difference of all elements between `self` and `rhs`
1396 /// is less than or equal to `max_abs_diff`.
1397 ///
1398 /// This can be used to compare if two matrices contain similar elements. It works best
1399 /// when comparing with a known value. The `max_abs_diff` that should be used used
1400 /// depends on the values being compared against.
1401 ///
1402 /// For more see
1403 /// [comparing floating point numbers](https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/).
1404 #[inline]
1405 #[must_use]
1406 pub fn abs_diff_eq(&self, rhs: Self, max_abs_diff: f32) -> bool {
1407 self.x_axis.abs_diff_eq(rhs.x_axis, max_abs_diff)
1408 && self.y_axis.abs_diff_eq(rhs.y_axis, max_abs_diff)
1409 && self.z_axis.abs_diff_eq(rhs.z_axis, max_abs_diff)
1410 && self.w_axis.abs_diff_eq(rhs.w_axis, max_abs_diff)
1411 }
1412
1413 /// Takes the absolute value of each element in `self`
1414 #[inline]
1415 #[must_use]
1416 pub fn abs(&self) -> Self {
1417 Self::from_cols(
1418 self.x_axis.abs(),
1419 self.y_axis.abs(),
1420 self.z_axis.abs(),
1421 self.w_axis.abs(),
1422 )
1423 }
1424
1425 #[inline]
1426 pub fn as_dmat4(&self) -> DMat4 {
1427 DMat4::from_cols(
1428 self.x_axis.as_dvec4(),
1429 self.y_axis.as_dvec4(),
1430 self.z_axis.as_dvec4(),
1431 self.w_axis.as_dvec4(),
1432 )
1433 }
1434 }
1435
1436 impl Default for Mat4 {
1437 #[inline]
1438 fn default() -> Self {
1439 Self::IDENTITY
1440 }
1441 }
1442
1443 impl Add for Mat4 {
1444 type Output = Self;
1445 #[inline]
1446 fn add(self, rhs: Self) -> Self {
1447 Self::from_cols(
1448 self.x_axis.add(rhs.x_axis),
1449 self.y_axis.add(rhs.y_axis),
1450 self.z_axis.add(rhs.z_axis),
1451 self.w_axis.add(rhs.w_axis),
1452 )
1453 }
1454 }
1455
1456 impl Add<&Self> for Mat4 {
1457 type Output = Self;
1458 #[inline]
1459 fn add(self, rhs: &Self) -> Self {
1460 self.add(*rhs)
1461 }
1462 }
1463
1464 impl Add<&Mat4> for &Mat4 {
1465 type Output = Mat4;
1466 #[inline]
1467 fn add(self, rhs: &Mat4) -> Mat4 {
1468 (*self).add(*rhs)
1469 }
1470 }
1471
1472 impl Add<Mat4> for &Mat4 {
1473 type Output = Mat4;
1474 #[inline]
1475 fn add(self, rhs: Mat4) -> Mat4 {
1476 (*self).add(rhs)
1477 }
1478 }
1479
1480 impl AddAssign for Mat4 {
1481 #[inline]
1482 fn add_assign(&mut self, rhs: Self) {
1483 *self = self.add(rhs);
1484 }
1485 }
1486
1487 impl AddAssign<&Self> for Mat4 {
1488 #[inline]
1489 fn add_assign(&mut self, rhs: &Self) {
1490 self.add_assign(*rhs);
1491 }
1492 }
1493
1494 impl Sub for Mat4 {
1495 type Output = Self;
1496 #[inline]
1497 fn sub(self, rhs: Self) -> Self {
1498 Self::from_cols(
1499 self.x_axis.sub(rhs.x_axis),
1500 self.y_axis.sub(rhs.y_axis),
1501 self.z_axis.sub(rhs.z_axis),
1502 self.w_axis.sub(rhs.w_axis),
1503 )
1504 }
1505 }
1506
1507 impl Sub<&Self> for Mat4 {
1508 type Output = Self;
1509 #[inline]
1510 fn sub(self, rhs: &Self) -> Self {
1511 self.sub(*rhs)
1512 }
1513 }
1514
1515 impl Sub<&Mat4> for &Mat4 {
1516 type Output = Mat4;
1517 #[inline]
1518 fn sub(self, rhs: &Mat4) -> Mat4 {
1519 (*self).sub(*rhs)
1520 }
1521 }
1522
1523 impl Sub<Mat4> for &Mat4 {
1524 type Output = Mat4;
1525 #[inline]
1526 fn sub(self, rhs: Mat4) -> Mat4 {
1527 (*self).sub(rhs)
1528 }
1529 }
1530
1531 impl SubAssign for Mat4 {
1532 #[inline]
1533 fn sub_assign(&mut self, rhs: Self) {
1534 *self = self.sub(rhs);
1535 }
1536 }
1537
1538 impl SubAssign<&Self> for Mat4 {
1539 #[inline]
1540 fn sub_assign(&mut self, rhs: &Self) {
1541 self.sub_assign(*rhs);
1542 }
1543 }
1544
1545 impl Neg for Mat4 {
1546 type Output = Self;
1547 #[inline]
1548 fn neg(self) -> Self::Output {
1549 Self::from_cols(
1550 self.x_axis.neg(),
1551 self.y_axis.neg(),
1552 self.z_axis.neg(),
1553 self.w_axis.neg(),
1554 )
1555 }
1556 }
1557
1558 impl Neg for &Mat4 {
1559 type Output = Mat4;
1560 #[inline]
1561 fn neg(self) -> Mat4 {
1562 (*self).neg()
1563 }
1564 }
1565
1566 impl Mul for Mat4 {
1567 type Output = Self;
1568 #[inline]
1569 fn mul(self, rhs: Self) -> Self {
1570 Self::from_cols(
1571 self.mul(rhs.x_axis),
1572 self.mul(rhs.y_axis),
1573 self.mul(rhs.z_axis),
1574 self.mul(rhs.w_axis),
1575 )
1576 }
1577 }
1578
1579 impl Mul<&Self> for Mat4 {
1580 type Output = Self;
1581 #[inline]
1582 fn mul(self, rhs: &Self) -> Self {
1583 self.mul(*rhs)
1584 }
1585 }
1586
1587 impl Mul<&Mat4> for &Mat4 {
1588 type Output = Mat4;
1589 #[inline]
1590 fn mul(self, rhs: &Mat4) -> Mat4 {
1591 (*self).mul(*rhs)
1592 }
1593 }
1594
1595 impl Mul<Mat4> for &Mat4 {
1596 type Output = Mat4;
1597 #[inline]
1598 fn mul(self, rhs: Mat4) -> Mat4 {
1599 (*self).mul(rhs)
1600 }
1601 }
1602
1603 impl MulAssign for Mat4 {
1604 #[inline]
1605 fn mul_assign(&mut self, rhs: Self) {
1606 *self = self.mul(rhs);
1607 }
1608 }
1609
1610 impl MulAssign<&Self> for Mat4 {
1611 #[inline]
1612 fn mul_assign(&mut self, rhs: &Self) {
1613 self.mul_assign(*rhs);
1614 }
1615 }
1616
1617 impl Mul<Vec4> for Mat4 {
1618 type Output = Vec4;
1619 #[inline]
1620 fn mul(self, rhs: Vec4) -> Self::Output {
1621 self.mul_vec4(rhs)
1622 }
1623 }
1624
1625 impl Mul<&Vec4> for Mat4 {
1626 type Output = Vec4;
1627 #[inline]
1628 fn mul(self, rhs: &Vec4) -> Vec4 {
1629 self.mul(*rhs)
1630 }
1631 }
1632
1633 impl Mul<&Vec4> for &Mat4 {
1634 type Output = Vec4;
1635 #[inline]
1636 fn mul(self, rhs: &Vec4) -> Vec4 {
1637 (*self).mul(*rhs)
1638 }
1639 }
1640
1641 impl Mul<Vec4> for &Mat4 {
1642 type Output = Vec4;
1643 #[inline]
1644 fn mul(self, rhs: Vec4) -> Vec4 {
1645 (*self).mul(rhs)
1646 }
1647 }
1648
1649 impl Mul<Mat4> for f32 {
1650 type Output = Mat4;
1651 #[inline]
1652 fn mul(self, rhs: Mat4) -> Self::Output {
1653 rhs.mul_scalar(self)
1654 }
1655 }
1656
1657 impl Mul<&Mat4> for f32 {
1658 type Output = Mat4;
1659 #[inline]
1660 fn mul(self, rhs: &Mat4) -> Mat4 {
1661 self.mul(*rhs)
1662 }
1663 }
1664
1665 impl Mul<&Mat4> for &f32 {
1666 type Output = Mat4;
1667 #[inline]
1668 fn mul(self, rhs: &Mat4) -> Mat4 {
1669 (*self).mul(*rhs)
1670 }
1671 }
1672
1673 impl Mul<Mat4> for &f32 {
1674 type Output = Mat4;
1675 #[inline]
1676 fn mul(self, rhs: Mat4) -> Mat4 {
1677 (*self).mul(rhs)
1678 }
1679 }
1680
1681 impl Mul<f32> for Mat4 {
1682 type Output = Self;
1683 #[inline]
1684 fn mul(self, rhs: f32) -> Self {
1685 self.mul_scalar(rhs)
1686 }
1687 }
1688
1689 impl Mul<&f32> for Mat4 {
1690 type Output = Self;
1691 #[inline]
1692 fn mul(self, rhs: &f32) -> Self {
1693 self.mul(*rhs)
1694 }
1695 }
1696
1697 impl Mul<&f32> for &Mat4 {
1698 type Output = Mat4;
1699 #[inline]
1700 fn mul(self, rhs: &f32) -> Mat4 {
1701 (*self).mul(*rhs)
1702 }
1703 }
1704
1705 impl Mul<f32> for &Mat4 {
1706 type Output = Mat4;
1707 #[inline]
1708 fn mul(self, rhs: f32) -> Mat4 {
1709 (*self).mul(rhs)
1710 }
1711 }
1712
1713 impl MulAssign<f32> for Mat4 {
1714 #[inline]
1715 fn mul_assign(&mut self, rhs: f32) {
1716 *self = self.mul(rhs);
1717 }
1718 }
1719
1720 impl MulAssign<&f32> for Mat4 {
1721 #[inline]
1722 fn mul_assign(&mut self, rhs: &f32) {
1723 self.mul_assign(*rhs);
1724 }
1725 }
1726
1727 impl Div<Mat4> for f32 {
1728 type Output = Mat4;
1729 #[inline]
1730 fn div(self, rhs: Mat4) -> Self::Output {
1731 rhs.div_scalar(self)
1732 }
1733 }
1734
1735 impl Div<&Mat4> for f32 {
1736 type Output = Mat4;
1737 #[inline]
1738 fn div(self, rhs: &Mat4) -> Mat4 {
1739 self.div(*rhs)
1740 }
1741 }
1742
1743 impl Div<&Mat4> for &f32 {
1744 type Output = Mat4;
1745 #[inline]
1746 fn div(self, rhs: &Mat4) -> Mat4 {
1747 (*self).div(*rhs)
1748 }
1749 }
1750
1751 impl Div<Mat4> for &f32 {
1752 type Output = Mat4;
1753 #[inline]
1754 fn div(self, rhs: Mat4) -> Mat4 {
1755 (*self).div(rhs)
1756 }
1757 }
1758
1759 impl Div<f32> for Mat4 {
1760 type Output = Self;
1761 #[inline]
1762 fn div(self, rhs: f32) -> Self {
1763 self.div_scalar(rhs)
1764 }
1765 }
1766
1767 impl Div<&f32> for Mat4 {
1768 type Output = Self;
1769 #[inline]
1770 fn div(self, rhs: &f32) -> Self {
1771 self.div(*rhs)
1772 }
1773 }
1774
1775 impl Div<&f32> for &Mat4 {
1776 type Output = Mat4;
1777 #[inline]
1778 fn div(self, rhs: &f32) -> Mat4 {
1779 (*self).div(*rhs)
1780 }
1781 }
1782
1783 impl Div<f32> for &Mat4 {
1784 type Output = Mat4;
1785 #[inline]
1786 fn div(self, rhs: f32) -> Mat4 {
1787 (*self).div(rhs)
1788 }
1789 }
1790
1791 impl DivAssign<f32> for Mat4 {
1792 #[inline]
1793 fn div_assign(&mut self, rhs: f32) {
1794 *self = self.div(rhs);
1795 }
1796 }
1797
1798 impl DivAssign<&f32> for Mat4 {
1799 #[inline]
1800 fn div_assign(&mut self, rhs: &f32) {
1801 self.div_assign(*rhs);
1802 }
1803 }
1804
1805 impl Sum<Self> for Mat4 {
1806 fn sum<I>(iter: I) -> Self
1807 where
1808 I: Iterator<Item = Self>,
1809 {
1810 iter.fold(Self::ZERO, Self::add)
1811 }
1812 }
1813
1814 impl<'a> Sum<&'a Self> for Mat4 {
1815 fn sum<I>(iter: I) -> Self
1816 where
1817 I: Iterator<Item = &'a Self>,
1818 {
1819 iter.fold(Self::ZERO, |a, &b| Self::add(a, b))
1820 }
1821 }
1822
1823 impl Product for Mat4 {
1824 fn product<I>(iter: I) -> Self
1825 where
1826 I: Iterator<Item = Self>,
1827 {
1828 iter.fold(Self::IDENTITY, Self::mul)
1829 }
1830 }
1831
1832 impl<'a> Product<&'a Self> for Mat4 {
1833 fn product<I>(iter: I) -> Self
1834 where
1835 I: Iterator<Item = &'a Self>,
1836 {
1837 iter.fold(Self::IDENTITY, |a, &b| Self::mul(a, b))
1838 }
1839 }
1840
1841 impl PartialEq for Mat4 {
1842 #[inline]
1843 fn eq(&self, rhs: &Self) -> bool {
1844 self.x_axis.eq(&rhs.x_axis)
1845 && self.y_axis.eq(&rhs.y_axis)
1846 && self.z_axis.eq(&rhs.z_axis)
1847 && self.w_axis.eq(&rhs.w_axis)
1848 }
1849 }
1850
1851 impl AsRef<[f32; 16]> for Mat4 {
1852 #[inline]
1853 fn as_ref(&self) -> &[f32; 16] {
1854 unsafe { &*(self as *const Self as *const [f32; 16]) }
1855 }
1856 }
1857
1858 impl AsMut<[f32; 16]> for Mat4 {
1859 #[inline]
1860 fn as_mut(&mut self) -> &mut [f32; 16] {
1861 unsafe { &mut *(self as *mut Self as *mut [f32; 16]) }
1862 }
1863 }
1864
1865 impl fmt::Debug for Mat4 {
1866 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
1867 fmt.debug_struct(stringify!(Mat4))
1868 .field("x_axis", &self.x_axis)
1869 .field("y_axis", &self.y_axis)
1870 .field("z_axis", &self.z_axis)
1871 .field("w_axis", &self.w_axis)
1872 .finish()
1873 }
1874 }
1875
1876 impl fmt::Display for Mat4 {
1877 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1878 if let Some(p) = f.precision() {
1879 write!(
1880 f,
1881 "[{:.*}, {:.*}, {:.*}, {:.*}]",
1882 p, self.x_axis, p, self.y_axis, p, self.z_axis, p, self.w_axis
1883 )
1884 } else {
1885 write!(
1886 f,
1887 "[{}, {}, {}, {}]",
1888 self.x_axis, self.y_axis, self.z_axis, self.w_axis
1889 )
1890 }
1891 }
1892 }