Debian Codegraph @

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

raw
1 // Generated from mat.rs.tera template. Edit the template, not the generated file.
2
3 use crate::{coresimd::*, swizzles::*, DMat4, EulerRot, Mat3, Mat3A, Quat, Vec3, Vec3A, Vec4};
4 #[cfg(not(target_arch = "spirv"))]
5 use core::fmt;
6 use core::iter::{Product, Sum};
7 use core::ops::{Add, AddAssign, Mul, MulAssign, Neg, Sub, SubAssign};
8
9 use core::simd::{Which::*, *};
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 = simd_swizzle!(
550 self.x_axis.0,
551 self.y_axis.0,
552 [First(0), First(1), Second(0), Second(1)]
553 );
554 let tmp1 = simd_swizzle!(
555 self.x_axis.0,
556 self.y_axis.0,
557 [First(2), First(3), Second(2), Second(3)]
558 );
559 let tmp2 = simd_swizzle!(
560 self.z_axis.0,
561 self.w_axis.0,
562 [First(0), First(1), Second(0), Second(1)]
563 );
564 let tmp3 = simd_swizzle!(
565 self.z_axis.0,
566 self.w_axis.0,
567 [First(2), First(3), Second(2), Second(3)]
568 );
569
570 Self {
571 x_axis: Vec4(simd_swizzle!(
572 tmp0,
573 tmp2,
574 [First(0), First(2), Second(0), Second(2)]
575 )),
576 y_axis: Vec4(simd_swizzle!(
577 tmp0,
578 tmp2,
579 [First(1), First(3), Second(1), Second(3)]
580 )),
581 z_axis: Vec4(simd_swizzle!(
582 tmp1,
583 tmp3,
584 [First(0), First(2), Second(0), Second(2)]
585 )),
586 w_axis: Vec4(simd_swizzle!(
587 tmp1,
588 tmp3,
589 [First(1), First(3), Second(1), Second(3)]
590 )),
591 }
592 }
593
594 /// Returns the determinant of `self`.
595 pub fn determinant(&self) -> f32 {
596 // Based on https://github.com/g-truc/glm `glm_mat4_determinant`
597 let swp2a = simd_swizzle!(self.z_axis.0, [2, 1, 1, 0]);
598 let swp3a = simd_swizzle!(self.w_axis.0, [3, 3, 2, 3]);
599 let swp2b = simd_swizzle!(self.z_axis.0, [3, 3, 2, 3]);
600 let swp3b = simd_swizzle!(self.w_axis.0, [2, 1, 1, 0]);
601 let swp2c = simd_swizzle!(self.z_axis.0, [2, 1, 0, 0]);
602 let swp3c = simd_swizzle!(self.w_axis.0, [0, 0, 2, 1]);
603
604 let mula = swp2a * swp3a;
605 let mulb = swp2b * swp3b;
606 let mulc = swp2c * swp3c;
607 let sube = mula - mulb;
608 let subf = simd_swizzle!(mulc, [2, 3, 2, 3]) - mulc;
609
610 let subfaca = simd_swizzle!(sube, [0, 0, 1, 2]);
611 let swpfaca = simd_swizzle!(self.y_axis.0, [1, 0, 0, 0]);
612 let mulfaca = swpfaca * subfaca;
613
614 let subtmpb = simd_swizzle!(sube, subf, [First(1), First(3), Second(0), Second(0)]);
615 let subfacb = simd_swizzle!(subtmpb, [0, 1, 1, 3]);
616 let swpfacb = simd_swizzle!(self.y_axis.0, [2, 2, 1, 1]);
617 let mulfacb = swpfacb * subfacb;
618
619 let subres = mulfaca - mulfacb;
620 let subtmpc = simd_swizzle!(sube, subf, [First(2), First(2), Second(0), Second(1)]);
621 let subfacc = simd_swizzle!(subtmpc, [0, 2, 3, 3]);
622 let swpfacc = simd_swizzle!(self.y_axis.0, [3, 3, 3, 2]);
623 let mulfacc = swpfacc * subfacc;
624
625 let addres = subres + mulfacc;
626 let detcof = addres * f32x4::from_array([1.0, -1.0, 1.0, -1.0]);
627
628 dot4(self.x_axis.0, detcof)
629 }
630
631 /// Returns the inverse of `self`.
632 ///
633 /// If the matrix is not invertible the returned matrix will be invalid.
634 ///
635 /// # Panics
636 ///
637 /// Will panic if the determinant of `self` is zero when `glam_assert` is enabled.
638 #[must_use]
639 pub fn inverse(&self) -> Self {
640 // Based on https://github.com/g-truc/glm `glm_mat4_inverse`
641 let fac0 = {
642 let swp0a = simd_swizzle!(
643 self.w_axis.0,
644 self.z_axis.0,
645 [First(3), First(3), Second(3), Second(3)]
646 );
647 let swp0b = simd_swizzle!(
648 self.w_axis.0,
649 self.z_axis.0,
650 [First(2), First(2), Second(2), Second(2)]
651 );
652
653 let swp00 = simd_swizzle!(
654 self.z_axis.0,
655 self.y_axis.0,
656 [First(2), First(2), Second(2), Second(2)]
657 );
658 let swp01 = simd_swizzle!(swp0a, [0, 0, 0, 2]);
659 let swp02 = simd_swizzle!(swp0b, [0, 0, 0, 2]);
660 let swp03 = simd_swizzle!(
661 self.z_axis.0,
662 self.y_axis.0,
663 [First(3), First(3), Second(3), Second(3)]
664 );
665
666 let mul00 = swp00 * swp01;
667 let mul01 = swp02 * swp03;
668 mul00 - mul01
669 };
670 let fac1 = {
671 let swp0a = simd_swizzle!(
672 self.w_axis.0,
673 self.z_axis.0,
674 [First(3), First(3), Second(3), Second(3)]
675 );
676 let swp0b = simd_swizzle!(
677 self.w_axis.0,
678 self.z_axis.0,
679 [First(1), First(1), Second(1), Second(1)]
680 );
681
682 let swp00 = simd_swizzle!(
683 self.z_axis.0,
684 self.y_axis.0,
685 [First(1), First(1), Second(1), Second(1)]
686 );
687 let swp01 = simd_swizzle!(swp0a, [0, 0, 0, 2]);
688 let swp02 = simd_swizzle!(swp0b, [0, 0, 0, 2]);
689 let swp03 = simd_swizzle!(
690 self.z_axis.0,
691 self.y_axis.0,
692 [First(3), First(3), Second(3), Second(3)]
693 );
694
695 let mul00 = swp00 * swp01;
696 let mul01 = swp02 * swp03;
697 mul00 - mul01
698 };
699 let fac2 = {
700 let swp0a = simd_swizzle!(
701 self.w_axis.0,
702 self.z_axis.0,
703 [First(2), First(2), Second(2), Second(2)]
704 );
705 let swp0b = simd_swizzle!(
706 self.w_axis.0,
707 self.z_axis.0,
708 [First(1), First(1), Second(1), Second(1)]
709 );
710
711 let swp00 = simd_swizzle!(
712 self.z_axis.0,
713 self.y_axis.0,
714 [First(1), First(1), Second(1), Second(1)]
715 );
716 let swp01 = simd_swizzle!(swp0a, [0, 0, 0, 2]);
717 let swp02 = simd_swizzle!(swp0b, [0, 0, 0, 2]);
718 let swp03 = simd_swizzle!(
719 self.z_axis.0,
720 self.y_axis.0,
721 [First(2), First(2), Second(2), Second(2)]
722 );
723
724 let mul00 = swp00 * swp01;
725 let mul01 = swp02 * swp03;
726 mul00 - mul01
727 };
728 let fac3 = {
729 let swp0a = simd_swizzle!(
730 self.w_axis.0,
731 self.z_axis.0,
732 [First(3), First(3), Second(3), Second(3)]
733 );
734 let swp0b = simd_swizzle!(
735 self.w_axis.0,
736 self.z_axis.0,
737 [First(0), First(0), Second(0), Second(0)]
738 );
739
740 let swp00 = simd_swizzle!(
741 self.z_axis.0,
742 self.y_axis.0,
743 [First(0), First(0), Second(0), Second(0)]
744 );
745 let swp01 = simd_swizzle!(swp0a, [0, 0, 0, 2]);
746 let swp02 = simd_swizzle!(swp0b, [0, 0, 0, 2]);
747 let swp03 = simd_swizzle!(
748 self.z_axis.0,
749 self.y_axis.0,
750 [First(3), First(3), Second(3), Second(3)]
751 );
752
753 let mul00 = swp00 * swp01;
754 let mul01 = swp02 * swp03;
755 mul00 - mul01
756 };
757 let fac4 = {
758 let swp0a = simd_swizzle!(
759 self.w_axis.0,
760 self.z_axis.0,
761 [First(2), First(2), Second(2), Second(2)]
762 );
763 let swp0b = simd_swizzle!(
764 self.w_axis.0,
765 self.z_axis.0,
766 [First(0), First(0), Second(0), Second(0)]
767 );
768
769 let swp00 = simd_swizzle!(
770 self.z_axis.0,
771 self.y_axis.0,
772 [First(0), First(0), Second(0), Second(0)]
773 );
774 let swp01 = simd_swizzle!(swp0a, [0, 0, 0, 2]);
775 let swp02 = simd_swizzle!(swp0b, [0, 0, 0, 2]);
776 let swp03 = simd_swizzle!(
777 self.z_axis.0,
778 self.y_axis.0,
779 [First(2), First(2), Second(2), Second(2)]
780 );
781
782 let mul00 = swp00 * swp01;
783 let mul01 = swp02 * swp03;
784 mul00 - mul01
785 };
786 let fac5 = {
787 let swp0a = simd_swizzle!(
788 self.w_axis.0,
789 self.z_axis.0,
790 [First(1), First(1), Second(1), Second(1)]
791 );
792 let swp0b = simd_swizzle!(
793 self.w_axis.0,
794 self.z_axis.0,
795 [First(0), First(0), Second(0), Second(0)]
796 );
797
798 let swp00 = simd_swizzle!(
799 self.z_axis.0,
800 self.y_axis.0,
801 [First(0), First(0), Second(0), Second(0)]
802 );
803 let swp01 = simd_swizzle!(swp0a, [0, 0, 0, 2]);
804 let swp02 = simd_swizzle!(swp0b, [0, 0, 0, 2]);
805 let swp03 = simd_swizzle!(
806 self.z_axis.0,
807 self.y_axis.0,
808 [First(1), First(1), Second(1), Second(1)]
809 );
810
811 let mul00 = swp00 * swp01;
812 let mul01 = swp02 * swp03;
813 mul00 - mul01
814 };
815 let sign_a = f32x4::from_array([-1.0, 1.0, -1.0, 1.0]);
816 let sign_b = f32x4::from_array([1.0, -1.0, 1.0, -1.0]);
817
818 let temp0 = simd_swizzle!(
819 self.y_axis.0,
820 self.x_axis.0,
821 [First(0), First(0), Second(0), Second(0)]
822 );
823 let vec0 = simd_swizzle!(temp0, [0, 2, 2, 2]);
824
825 let temp1 = simd_swizzle!(
826 self.y_axis.0,
827 self.x_axis.0,
828 [First(1), First(1), Second(1), Second(1)]
829 );
830 let vec1 = simd_swizzle!(temp1, [0, 2, 2, 2]);
831
832 let temp2 = simd_swizzle!(
833 self.y_axis.0,
834 self.x_axis.0,
835 [First(2), First(2), Second(2), Second(2)]
836 );
837 let vec2 = simd_swizzle!(temp2, [0, 2, 2, 2]);
838
839 let temp3 = simd_swizzle!(
840 self.y_axis.0,
841 self.x_axis.0,
842 [First(3), First(3), Second(3), Second(3)]
843 );
844 let vec3 = simd_swizzle!(temp3, [0, 2, 2, 2]);
845
846 let mul00 = vec1 * fac0;
847 let mul01 = vec2 * fac1;
848 let mul02 = vec3 * fac2;
849 let sub00 = mul00 - mul01;
850 let add00 = sub00 + mul02;
851 let inv0 = sign_b * add00;
852
853 let mul03 = vec0 * fac0;
854 let mul04 = vec2 * fac3;
855 let mul05 = vec3 * fac4;
856 let sub01 = mul03 - mul04;
857 let add01 = sub01 + mul05;
858 let inv1 = sign_a * add01;
859
860 let mul06 = vec0 * fac1;
861 let mul07 = vec1 * fac3;
862 let mul08 = vec3 * fac5;
863 let sub02 = mul06 - mul07;
864 let add02 = sub02 + mul08;
865 let inv2 = sign_b * add02;
866
867 let mul09 = vec0 * fac2;
868 let mul10 = vec1 * fac4;
869 let mul11 = vec2 * fac5;
870 let sub03 = mul09 - mul10;
871 let add03 = sub03 + mul11;
872 let inv3 = sign_a * add03;
873
874 let row0 = simd_swizzle!(inv0, inv1, [First(0), First(0), Second(0), Second(0)]);
875 let row1 = simd_swizzle!(inv2, inv3, [First(0), First(0), Second(0), Second(0)]);
876 let row2 = simd_swizzle!(row0, row1, [First(0), First(2), Second(0), Second(2)]);
877
878 let dot0 = dot4(self.x_axis.0, row2);
879 glam_assert!(dot0 != 0.0);
880
881 let rcp0 = f32x4::splat(dot0.recip());
882
883 Self {
884 x_axis: Vec4(inv0 * rcp0),
885 y_axis: Vec4(inv1 * rcp0),
886 z_axis: Vec4(inv2 * rcp0),
887 w_axis: Vec4(inv3 * rcp0),
888 }
889 }
890
891 /// Creates a left-handed view matrix using a camera position, an up direction, and a facing
892 /// direction.
893 ///
894 /// For a view coordinate system with `+X=right`, `+Y=up` and `+Z=forward`.
895 #[inline]
896 pub fn look_to_lh(eye: Vec3, dir: Vec3, up: Vec3) -> Self {
897 Self::look_to_rh(eye, -dir, up)
898 }
899
900 /// Creates a right-handed view matrix using a camera position, an up direction, and a facing
901 /// direction.
902 ///
903 /// For a view coordinate system with `+X=right`, `+Y=up` and `+Z=back`.
904 #[inline]
905 pub fn look_to_rh(eye: Vec3, dir: Vec3, up: Vec3) -> Self {
906 let f = dir.normalize();
907 let s = f.cross(up).normalize();
908 let u = s.cross(f);
909
910 Self::from_cols(
911 Vec4::new(s.x, u.x, -f.x, 0.0),
912 Vec4::new(s.y, u.y, -f.y, 0.0),
913 Vec4::new(s.z, u.z, -f.z, 0.0),
914 Vec4::new(-eye.dot(s), -eye.dot(u), eye.dot(f), 1.0),
915 )
916 }
917
918 /// Creates a left-handed view matrix using a camera position, an up direction, and a focal
919 /// point.
920 /// For a view coordinate system with `+X=right`, `+Y=up` and `+Z=forward`.
921 ///
922 /// # Panics
923 ///
924 /// Will panic if `up` is not normalized when `glam_assert` is enabled.
925 #[inline]
926 pub fn look_at_lh(eye: Vec3, center: Vec3, up: Vec3) -> Self {
927 glam_assert!(up.is_normalized());
928 Self::look_to_lh(eye, center.sub(eye), up)
929 }
930
931 /// Creates a right-handed view matrix using a camera position, an up direction, and a focal
932 /// point.
933 /// For a view coordinate system with `+X=right`, `+Y=up` and `+Z=back`.
934 ///
935 /// # Panics
936 ///
937 /// Will panic if `up` is not normalized when `glam_assert` is enabled.
938 #[inline]
939 pub fn look_at_rh(eye: Vec3, center: Vec3, up: Vec3) -> Self {
940 glam_assert!(up.is_normalized());
941 Self::look_to_rh(eye, center.sub(eye), up)
942 }
943
944 /// Creates a right-handed perspective projection matrix with [-1,1] depth range.
945 /// This is the same as the OpenGL `gluPerspective` function.
946 /// See <https://www.khronos.org/registry/OpenGL-Refpages/gl2.1/xhtml/gluPerspective.xml>
947 #[inline]
948 pub fn perspective_rh_gl(
949 fov_y_radians: f32,
950 aspect_ratio: f32,
951 z_near: f32,
952 z_far: f32,
953 ) -> Self {
954 let inv_length = 1.0 / (z_near - z_far);
955 let f = 1.0 / (0.5 * fov_y_radians).tan();
956 let a = f / aspect_ratio;
957 let b = (z_near + z_far) * inv_length;
958 let c = (2.0 * z_near * z_far) * inv_length;
959 Self::from_cols(
960 Vec4::new(a, 0.0, 0.0, 0.0),
961 Vec4::new(0.0, f, 0.0, 0.0),
962 Vec4::new(0.0, 0.0, b, -1.0),
963 Vec4::new(0.0, 0.0, c, 0.0),
964 )
965 }
966
967 /// Creates a left-handed perspective projection matrix with `[0,1]` depth range.
968 ///
969 /// # Panics
970 ///
971 /// Will panic if `z_near` or `z_far` are less than or equal to zero when `glam_assert` is
972 /// enabled.
973 #[inline]
974 pub fn perspective_lh(fov_y_radians: f32, aspect_ratio: f32, z_near: f32, z_far: f32) -> Self {
975 glam_assert!(z_near > 0.0 && z_far > 0.0);
976 let (sin_fov, cos_fov) = (0.5 * fov_y_radians).sin_cos();
977 let h = cos_fov / sin_fov;
978 let w = h / aspect_ratio;
979 let r = z_far / (z_far - z_near);
980 Self::from_cols(
981 Vec4::new(w, 0.0, 0.0, 0.0),
982 Vec4::new(0.0, h, 0.0, 0.0),
983 Vec4::new(0.0, 0.0, r, 1.0),
984 Vec4::new(0.0, 0.0, -r * z_near, 0.0),
985 )
986 }
987
988 /// Creates a right-handed perspective projection matrix with `[0,1]` depth range.
989 ///
990 /// # Panics
991 ///
992 /// Will panic if `z_near` or `z_far` are less than or equal to zero when `glam_assert` is
993 /// enabled.
994 #[inline]
995 pub fn perspective_rh(fov_y_radians: f32, aspect_ratio: f32, z_near: f32, z_far: f32) -> Self {
996 glam_assert!(z_near > 0.0 && z_far > 0.0);
997 let (sin_fov, cos_fov) = (0.5 * fov_y_radians).sin_cos();
998 let h = cos_fov / sin_fov;
999 let w = h / aspect_ratio;
1000 let r = z_far / (z_near - z_far);
1001 Self::from_cols(
1002 Vec4::new(w, 0.0, 0.0, 0.0),
1003 Vec4::new(0.0, h, 0.0, 0.0),
1004 Vec4::new(0.0, 0.0, r, -1.0),
1005 Vec4::new(0.0, 0.0, r * z_near, 0.0),
1006 )
1007 }
1008
1009 /// Creates an infinite left-handed perspective projection matrix with `[0,1]` depth range.
1010 ///
1011 /// # Panics
1012 ///
1013 /// Will panic if `z_near` is less than or equal to zero when `glam_assert` is enabled.
1014 #[inline]
1015 pub fn perspective_infinite_lh(fov_y_radians: f32, aspect_ratio: f32, z_near: f32) -> Self {
1016 glam_assert!(z_near > 0.0);
1017 let (sin_fov, cos_fov) = (0.5 * fov_y_radians).sin_cos();
1018 let h = cos_fov / sin_fov;
1019 let w = h / aspect_ratio;
1020 Self::from_cols(
1021 Vec4::new(w, 0.0, 0.0, 0.0),
1022 Vec4::new(0.0, h, 0.0, 0.0),
1023 Vec4::new(0.0, 0.0, 1.0, 1.0),
1024 Vec4::new(0.0, 0.0, -z_near, 0.0),
1025 )
1026 }
1027
1028 /// Creates an infinite left-handed perspective projection matrix with `[0,1]` depth range.
1029 ///
1030 /// # Panics
1031 ///
1032 /// Will panic if `z_near` is less than or equal to zero when `glam_assert` is enabled.
1033 #[inline]
1034 pub fn perspective_infinite_reverse_lh(
1035 fov_y_radians: f32,
1036 aspect_ratio: f32,
1037 z_near: f32,
1038 ) -> Self {
1039 glam_assert!(z_near > 0.0);
1040 let (sin_fov, cos_fov) = (0.5 * fov_y_radians).sin_cos();
1041 let h = cos_fov / sin_fov;
1042 let w = h / aspect_ratio;
1043 Self::from_cols(
1044 Vec4::new(w, 0.0, 0.0, 0.0),
1045 Vec4::new(0.0, h, 0.0, 0.0),
1046 Vec4::new(0.0, 0.0, 0.0, 1.0),
1047 Vec4::new(0.0, 0.0, z_near, 0.0),
1048 )
1049 }
1050
1051 /// Creates an infinite right-handed perspective projection matrix with
1052 /// `[0,1]` depth range.
1053 #[inline]
1054 pub fn perspective_infinite_rh(fov_y_radians: f32, aspect_ratio: f32, z_near: f32) -> Self {
1055 glam_assert!(z_near > 0.0);
1056 let f = 1.0 / (0.5 * fov_y_radians).tan();
1057 Self::from_cols(
1058 Vec4::new(f / aspect_ratio, 0.0, 0.0, 0.0),
1059 Vec4::new(0.0, f, 0.0, 0.0),
1060 Vec4::new(0.0, 0.0, -1.0, -1.0),
1061 Vec4::new(0.0, 0.0, -z_near, 0.0),
1062 )
1063 }
1064
1065 /// Creates an infinite reverse right-handed perspective projection matrix
1066 /// with `[0,1]` depth range.
1067 #[inline]
1068 pub fn perspective_infinite_reverse_rh(
1069 fov_y_radians: f32,
1070 aspect_ratio: f32,
1071 z_near: f32,
1072 ) -> Self {
1073 glam_assert!(z_near > 0.0);
1074 let f = 1.0 / (0.5 * fov_y_radians).tan();
1075 Self::from_cols(
1076 Vec4::new(f / aspect_ratio, 0.0, 0.0, 0.0),
1077 Vec4::new(0.0, f, 0.0, 0.0),
1078 Vec4::new(0.0, 0.0, 0.0, -1.0),
1079 Vec4::new(0.0, 0.0, z_near, 0.0),
1080 )
1081 }
1082
1083 /// Creates a right-handed orthographic projection matrix with `[-1,1]` depth
1084 /// range. This is the same as the OpenGL `glOrtho` function in OpenGL.
1085 /// See
1086 /// <https://www.khronos.org/registry/OpenGL-Refpages/gl2.1/xhtml/glOrtho.xml>
1087 #[inline]
1088 pub fn orthographic_rh_gl(
1089 left: f32,
1090 right: f32,
1091 bottom: f32,
1092 top: f32,
1093 near: f32,
1094 far: f32,
1095 ) -> Self {
1096 let a = 2.0 / (right - left);
1097 let b = 2.0 / (top - bottom);
1098 let c = -2.0 / (far - near);
1099 let tx = -(right + left) / (right - left);
1100 let ty = -(top + bottom) / (top - bottom);
1101 let tz = -(far + near) / (far - near);
1102
1103 Self::from_cols(
1104 Vec4::new(a, 0.0, 0.0, 0.0),
1105 Vec4::new(0.0, b, 0.0, 0.0),
1106 Vec4::new(0.0, 0.0, c, 0.0),
1107 Vec4::new(tx, ty, tz, 1.0),
1108 )
1109 }
1110
1111 /// Creates a left-handed orthographic projection matrix with `[0,1]` depth range.
1112 #[inline]
1113 pub fn orthographic_lh(
1114 left: f32,
1115 right: f32,
1116 bottom: f32,
1117 top: f32,
1118 near: f32,
1119 far: f32,
1120 ) -> Self {
1121 let rcp_width = 1.0 / (right - left);
1122 let rcp_height = 1.0 / (top - bottom);
1123 let r = 1.0 / (far - near);
1124 Self::from_cols(
1125 Vec4::new(rcp_width + rcp_width, 0.0, 0.0, 0.0),
1126 Vec4::new(0.0, rcp_height + rcp_height, 0.0, 0.0),
1127 Vec4::new(0.0, 0.0, r, 0.0),
1128 Vec4::new(
1129 -(left + right) * rcp_width,
1130 -(top + bottom) * rcp_height,
1131 -r * near,
1132 1.0,
1133 ),
1134 )
1135 }
1136
1137 /// Creates a right-handed orthographic projection matrix with `[0,1]` depth range.
1138 #[inline]
1139 pub fn orthographic_rh(
1140 left: f32,
1141 right: f32,
1142 bottom: f32,
1143 top: f32,
1144 near: f32,
1145 far: f32,
1146 ) -> Self {
1147 let rcp_width = 1.0 / (right - left);
1148 let rcp_height = 1.0 / (top - bottom);
1149 let r = 1.0 / (near - far);
1150 Self::from_cols(
1151 Vec4::new(rcp_width + rcp_width, 0.0, 0.0, 0.0),
1152 Vec4::new(0.0, rcp_height + rcp_height, 0.0, 0.0),
1153 Vec4::new(0.0, 0.0, r, 0.0),
1154 Vec4::new(
1155 -(left + right) * rcp_width,
1156 -(top + bottom) * rcp_height,
1157 r * near,
1158 1.0,
1159 ),
1160 )
1161 }
1162
1163 /// Transforms the given 3D vector as a point, applying perspective correction.
1164 ///
1165 /// This is the equivalent of multiplying the 3D vector as a 4D vector where `w` is `1.0`.
1166 /// The perspective divide is performed meaning the resulting 3D vector is divided by `w`.
1167 ///
1168 /// This method assumes that `self` contains a projective transform.
1169 #[inline]
1170 pub fn project_point3(&self, rhs: Vec3) -> Vec3 {
1171 let mut res = self.x_axis.mul(rhs.x);
1172 res = self.y_axis.mul(rhs.y).add(res);
1173 res = self.z_axis.mul(rhs.z).add(res);
1174 res = self.w_axis.add(res);
1175 res = res.mul(res.wwww().recip());
1176 res.xyz()
1177 }
1178
1179 /// Transforms the given 3D vector as a point.
1180 ///
1181 /// This is the equivalent of multiplying the 3D vector as a 4D vector where `w` is
1182 /// `1.0`.
1183 ///
1184 /// This method assumes that `self` contains a valid affine transform. It does not perform
1185 /// a persective divide, if `self` contains a perspective transform, or if you are unsure,
1186 /// the [`Self::project_point3()`] method should be used instead.
1187 ///
1188 /// # Panics
1189 ///
1190 /// Will panic if the 3rd row of `self` is not `(0, 0, 0, 1)` when `glam_assert` is enabled.
1191 #[inline]
1192 pub fn transform_point3(&self, rhs: Vec3) -> Vec3 {
1193 glam_assert!(self.row(3).abs_diff_eq(Vec4::W, 1e-6));
1194 let mut res = self.x_axis.mul(rhs.x);
1195 res = self.y_axis.mul(rhs.y).add(res);
1196 res = self.z_axis.mul(rhs.z).add(res);
1197 res = self.w_axis.add(res);
1198 res.xyz()
1199 }
1200
1201 /// Transforms the give 3D vector as a direction.
1202 ///
1203 /// This is the equivalent of multiplying the 3D vector as a 4D vector where `w` is
1204 /// `0.0`.
1205 ///
1206 /// This method assumes that `self` contains a valid affine transform.
1207 ///
1208 /// # Panics
1209 ///
1210 /// Will panic if the 3rd row of `self` is not `(0, 0, 0, 1)` when `glam_assert` is enabled.
1211 #[inline]
1212 pub fn transform_vector3(&self, rhs: Vec3) -> Vec3 {
1213 glam_assert!(self.row(3).abs_diff_eq(Vec4::W, 1e-6));
1214 let mut res = self.x_axis.mul(rhs.x);
1215 res = self.y_axis.mul(rhs.y).add(res);
1216 res = self.z_axis.mul(rhs.z).add(res);
1217 res.xyz()
1218 }
1219
1220 /// Transforms the given `Vec3A` as 3D point.
1221 ///
1222 /// This is the equivalent of multiplying the `Vec3A` as a 4D vector where `w` is `1.0`.
1223 #[inline]
1224 pub fn transform_point3a(&self, rhs: Vec3A) -> Vec3A {
1225 glam_assert!(self.row(3).abs_diff_eq(Vec4::W, 1e-6));
1226 let mut res = self.x_axis.mul(rhs.xxxx());
1227 res = self.y_axis.mul(rhs.yyyy()).add(res);
1228 res = self.z_axis.mul(rhs.zzzz()).add(res);
1229 res = self.w_axis.add(res);
1230 res.into()
1231 }
1232
1233 /// Transforms the give `Vec3A` as 3D vector.
1234 ///
1235 /// This is the equivalent of multiplying the `Vec3A` as a 4D vector where `w` is `0.0`.
1236 #[inline]
1237 pub fn transform_vector3a(&self, rhs: Vec3A) -> Vec3A {
1238 glam_assert!(self.row(3).abs_diff_eq(Vec4::W, 1e-6));
1239 let mut res = self.x_axis.mul(rhs.xxxx());
1240 res = self.y_axis.mul(rhs.yyyy()).add(res);
1241 res = self.z_axis.mul(rhs.zzzz()).add(res);
1242 res.into()
1243 }
1244
1245 /// Transforms a 4D vector.
1246 #[inline]
1247 pub fn mul_vec4(&self, rhs: Vec4) -> Vec4 {
1248 let mut res = self.x_axis.mul(rhs.xxxx());
1249 res = res.add(self.y_axis.mul(rhs.yyyy()));
1250 res = res.add(self.z_axis.mul(rhs.zzzz()));
1251 res = res.add(self.w_axis.mul(rhs.wwww()));
1252 res
1253 }
1254
1255 /// Multiplies two 4x4 matrices.
1256 #[inline]
1257 pub fn mul_mat4(&self, rhs: &Self) -> Self {
1258 Self::from_cols(
1259 self.mul(rhs.x_axis),
1260 self.mul(rhs.y_axis),
1261 self.mul(rhs.z_axis),
1262 self.mul(rhs.w_axis),
1263 )
1264 }
1265
1266 /// Adds two 4x4 matrices.
1267 #[inline]
1268 pub fn add_mat4(&self, rhs: &Self) -> Self {
1269 Self::from_cols(
1270 self.x_axis.add(rhs.x_axis),
1271 self.y_axis.add(rhs.y_axis),
1272 self.z_axis.add(rhs.z_axis),
1273 self.w_axis.add(rhs.w_axis),
1274 )
1275 }
1276
1277 /// Subtracts two 4x4 matrices.
1278 #[inline]
1279 pub fn sub_mat4(&self, rhs: &Self) -> Self {
1280 Self::from_cols(
1281 self.x_axis.sub(rhs.x_axis),
1282 self.y_axis.sub(rhs.y_axis),
1283 self.z_axis.sub(rhs.z_axis),
1284 self.w_axis.sub(rhs.w_axis),
1285 )
1286 }
1287
1288 /// Multiplies a 4x4 matrix by a scalar.
1289 #[inline]
1290 pub fn mul_scalar(&self, rhs: f32) -> Self {
1291 Self::from_cols(
1292 self.x_axis.mul(rhs),
1293 self.y_axis.mul(rhs),
1294 self.z_axis.mul(rhs),
1295 self.w_axis.mul(rhs),
1296 )
1297 }
1298
1299 /// Returns true if the absolute difference of all elements between `self` and `rhs`
1300 /// is less than or equal to `max_abs_diff`.
1301 ///
1302 /// This can be used to compare if two matrices contain similar elements. It works best
1303 /// when comparing with a known value. The `max_abs_diff` that should be used used
1304 /// depends on the values being compared against.
1305 ///
1306 /// For more see
1307 /// [comparing floating point numbers](https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/).
1308 #[inline]
1309 pub fn abs_diff_eq(&self, rhs: Self, max_abs_diff: f32) -> bool {
1310 self.x_axis.abs_diff_eq(rhs.x_axis, max_abs_diff)
1311 && self.y_axis.abs_diff_eq(rhs.y_axis, max_abs_diff)
1312 && self.z_axis.abs_diff_eq(rhs.z_axis, max_abs_diff)
1313 && self.w_axis.abs_diff_eq(rhs.w_axis, max_abs_diff)
1314 }
1315
1316 #[inline]
1317 pub fn as_dmat4(&self) -> DMat4 {
1318 DMat4::from_cols(
1319 self.x_axis.as_dvec4(),
1320 self.y_axis.as_dvec4(),
1321 self.z_axis.as_dvec4(),
1322 self.w_axis.as_dvec4(),
1323 )
1324 }
1325 }
1326
1327 impl Default for Mat4 {
1328 #[inline]
1329 fn default() -> Self {
1330 Self::IDENTITY
1331 }
1332 }
1333
1334 impl Add<Mat4> for Mat4 {
1335 type Output = Self;
1336 #[inline]
1337 fn add(self, rhs: Self) -> Self::Output {
1338 self.add_mat4(&rhs)
1339 }
1340 }
1341
1342 impl AddAssign<Mat4> for Mat4 {
1343 #[inline]
1344 fn add_assign(&mut self, rhs: Self) {
1345 *self = self.add_mat4(&rhs);
1346 }
1347 }
1348
1349 impl Sub<Mat4> for Mat4 {
1350 type Output = Self;
1351 #[inline]
1352 fn sub(self, rhs: Self) -> Self::Output {
1353 self.sub_mat4(&rhs)
1354 }
1355 }
1356
1357 impl SubAssign<Mat4> for Mat4 {
1358 #[inline]
1359 fn sub_assign(&mut self, rhs: Self) {
1360 *self = self.sub_mat4(&rhs);
1361 }
1362 }
1363
1364 impl Neg for Mat4 {
1365 type Output = Self;
1366 #[inline]
1367 fn neg(self) -> Self::Output {
1368 Self::from_cols(
1369 self.x_axis.neg(),
1370 self.y_axis.neg(),
1371 self.z_axis.neg(),
1372 self.w_axis.neg(),
1373 )
1374 }
1375 }
1376
1377 impl Mul<Mat4> for Mat4 {
1378 type Output = Self;
1379 #[inline]
1380 fn mul(self, rhs: Self) -> Self::Output {
1381 self.mul_mat4(&rhs)
1382 }
1383 }
1384
1385 impl MulAssign<Mat4> for Mat4 {
1386 #[inline]
1387 fn mul_assign(&mut self, rhs: Self) {
1388 *self = self.mul_mat4(&rhs);
1389 }
1390 }
1391
1392 impl Mul<Vec4> for Mat4 {
1393 type Output = Vec4;
1394 #[inline]
1395 fn mul(self, rhs: Vec4) -> Self::Output {
1396 self.mul_vec4(rhs)
1397 }
1398 }
1399
1400 impl Mul<Mat4> for f32 {
1401 type Output = Mat4;
1402 #[inline]
1403 fn mul(self, rhs: Mat4) -> Self::Output {
1404 rhs.mul_scalar(self)
1405 }
1406 }
1407
1408 impl Mul<f32> for Mat4 {
1409 type Output = Self;
1410 #[inline]
1411 fn mul(self, rhs: f32) -> Self::Output {
1412 self.mul_scalar(rhs)
1413 }
1414 }
1415
1416 impl MulAssign<f32> for Mat4 {
1417 #[inline]
1418 fn mul_assign(&mut self, rhs: f32) {
1419 *self = self.mul_scalar(rhs);
1420 }
1421 }
1422
1423 impl Sum<Self> for Mat4 {
1424 fn sum<I>(iter: I) -> Self
1425 where
1426 I: Iterator<Item = Self>,
1427 {
1428 iter.fold(Self::ZERO, Self::add)
1429 }
1430 }
1431
1432 impl<'a> Sum<&'a Self> for Mat4 {
1433 fn sum<I>(iter: I) -> Self
1434 where
1435 I: Iterator<Item = &'a Self>,
1436 {
1437 iter.fold(Self::ZERO, |a, &b| Self::add(a, b))
1438 }
1439 }
1440
1441 impl Product for Mat4 {
1442 fn product<I>(iter: I) -> Self
1443 where
1444 I: Iterator<Item = Self>,
1445 {
1446 iter.fold(Self::IDENTITY, Self::mul)
1447 }
1448 }
1449
1450 impl<'a> Product<&'a Self> for Mat4 {
1451 fn product<I>(iter: I) -> Self
1452 where
1453 I: Iterator<Item = &'a Self>,
1454 {
1455 iter.fold(Self::IDENTITY, |a, &b| Self::mul(a, b))
1456 }
1457 }
1458
1459 impl PartialEq for Mat4 {
1460 #[inline]
1461 fn eq(&self, rhs: &Self) -> bool {
1462 self.x_axis.eq(&rhs.x_axis)
1463 && self.y_axis.eq(&rhs.y_axis)
1464 && self.z_axis.eq(&rhs.z_axis)
1465 && self.w_axis.eq(&rhs.w_axis)
1466 }
1467 }
1468
1469 #[cfg(not(target_arch = "spirv"))]
1470 impl AsRef<[f32; 16]> for Mat4 {
1471 #[inline]
1472 fn as_ref(&self) -> &[f32; 16] {
1473 unsafe { &*(self as *const Self as *const [f32; 16]) }
1474 }
1475 }
1476
1477 #[cfg(not(target_arch = "spirv"))]
1478 impl AsMut<[f32; 16]> for Mat4 {
1479 #[inline]
1480 fn as_mut(&mut self) -> &mut [f32; 16] {
1481 unsafe { &mut *(self as *mut Self as *mut [f32; 16]) }
1482 }
1483 }
1484
1485 #[cfg(not(target_arch = "spirv"))]
1486 impl fmt::Debug for Mat4 {
1487 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
1488 fmt.debug_struct(stringify!(Mat4))
1489 .field("x_axis", &self.x_axis)
1490 .field("y_axis", &self.y_axis)
1491 .field("z_axis", &self.z_axis)
1492 .field("w_axis", &self.w_axis)
1493 .finish()
1494 }
1495 }
1496
1497 #[cfg(not(target_arch = "spirv"))]
1498 impl fmt::Display for Mat4 {
1499 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1500 write!(
1501 f,
1502 "[{}, {}, {}, {}]",
1503 self.x_axis, self.y_axis, self.z_axis, self.w_axis
1504 )
1505 }
1506 }