Debian Codegraph @

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

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