Debian Codegraph @

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

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