Debian Codegraph @

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