Debian Codegraph @

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

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