Debian Codegraph @

debian/missing-sources/glam-0.21.3/src/f32/scalar/mat4.rs

raw
1 // Generated from mat.rs.tera template. Edit the template, not the generated file.
2
3 use crate::{swizzles::*, DMat4, EulerRot, Mat3, 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 #[cfg(feature = "libm")]
10 #[allow(unused_imports)]
11 use num_traits::Float;
12
13 /// Creates a 4x4 matrix from 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 /// Othographic 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 #[cfg_attr(
50 any(
51 not(any(feature = "scalar-math", target_arch = "spirv")),
52 feature = "cuda"
53 ),
54 repr(align(16))
55 )]
56 #[repr(C)]
57 pub struct Mat4 {
58 pub x_axis: Vec4,
59 pub y_axis: Vec4,
60 pub z_axis: Vec4,
61 pub w_axis: Vec4,
62 }
63
64 impl Mat4 {
65 /// A 4x4 matrix with all elements set to `0.0`.
66 pub const ZERO: Self = Self::from_cols(Vec4::ZERO, Vec4::ZERO, Vec4::ZERO, Vec4::ZERO);
67
68 /// A 4x4 identity matrix, where all diagonal elements are `1`, and all off-diagonal elements are `0`.
69 pub const IDENTITY: Self = Self::from_cols(Vec4::X, Vec4::Y, Vec4::Z, Vec4::W);
70
71 /// All NAN:s.
72 pub const NAN: Self = Self::from_cols(Vec4::NAN, Vec4::NAN, Vec4::NAN, Vec4::NAN);
73
74 #[allow(clippy::too_many_arguments)]
75 #[inline(always)]
76 const fn new(
77 m00: f32,
78 m01: f32,
79 m02: f32,
80 m03: f32,
81 m10: f32,
82 m11: f32,
83 m12: f32,
84 m13: f32,
85 m20: f32,
86 m21: f32,
87 m22: f32,
88 m23: f32,
89 m30: f32,
90 m31: f32,
91 m32: f32,
92 m33: f32,
93 ) -> Self {
94 Self {
95 x_axis: Vec4::new(m00, m01, m02, m03),
96 y_axis: Vec4::new(m10, m11, m12, m13),
97 z_axis: Vec4::new(m20, m21, m22, m23),
98 w_axis: Vec4::new(m30, m31, m32, m33),
99 }
100 }
101
102 /// Creates a 4x4 matrix from two column vectors.
103 #[inline(always)]
104 pub const fn from_cols(x_axis: Vec4, y_axis: Vec4, z_axis: Vec4, w_axis: Vec4) -> Self {
105 Self {
106 x_axis,
107 y_axis,
108 z_axis,
109 w_axis,
110 }
111 }
112
113 /// Creates a 4x4 matrix from a `[f32; 16]` array stored in column major order.
114 /// If your data is stored in row major you will need to `transpose` the returned
115 /// matrix.
116 #[inline]
117 pub const fn from_cols_array(m: &[f32; 16]) -> Self {
118 Self::new(
119 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],
120 m[14], m[15],
121 )
122 }
123
124 /// Creates a `[f32; 16]` array storing data in column major order.
125 /// If you require data in row major order `transpose` the matrix first.
126 #[inline]
127 pub fn to_cols_array(&self) -> [f32; 16] {
128 [
129 self.x_axis.x,
130 self.x_axis.y,
131 self.x_axis.z,
132 self.x_axis.w,
133 self.y_axis.x,
134 self.y_axis.y,
135 self.y_axis.z,
136 self.y_axis.w,
137 self.z_axis.x,
138 self.z_axis.y,
139 self.z_axis.z,
140 self.z_axis.w,
141 self.w_axis.x,
142 self.w_axis.y,
143 self.w_axis.z,
144 self.w_axis.w,
145 ]
146 }
147
148 /// Creates a 4x4 matrix from a `[[f32; 4]; 4]` 4D array stored in column major order.
149 /// If your data is in row major order you will need to `transpose` the returned
150 /// matrix.
151 #[inline]
152 pub const fn from_cols_array_2d(m: &[[f32; 4]; 4]) -> Self {
153 Self::from_cols(
154 Vec4::from_array(m[0]),
155 Vec4::from_array(m[1]),
156 Vec4::from_array(m[2]),
157 Vec4::from_array(m[3]),
158 )
159 }
160
161 /// Creates a `[[f32; 4]; 4]` 4D array storing data in column major order.
162 /// If you require data in row major order `transpose` the matrix first.
163 #[inline]
164 pub fn to_cols_array_2d(&self) -> [[f32; 4]; 4] {
165 [
166 self.x_axis.to_array(),
167 self.y_axis.to_array(),
168 self.z_axis.to_array(),
169 self.w_axis.to_array(),
170 ]
171 }
172
173 /// Creates a 4x4 matrix with its diagonal set to `diagonal` and all other entries set to 0.
174 #[doc(alias = "scale")]
175 #[inline]
176 pub fn from_diagonal(diagonal: Vec4) -> Self {
177 Self::new(
178 diagonal.x, 0.0, 0.0, 0.0, 0.0, diagonal.y, 0.0, 0.0, 0.0, 0.0, diagonal.z, 0.0, 0.0,
179 0.0, 0.0, diagonal.w,
180 )
181 }
182
183 #[inline]
184 fn quat_to_axes(rotation: Quat) -> (Vec4, Vec4, Vec4) {
185 glam_assert!(rotation.is_normalized());
186
187 let (x, y, z, w) = rotation.into();
188 let x2 = x + x;
189 let y2 = y + y;
190 let z2 = z + z;
191 let xx = x * x2;
192 let xy = x * y2;
193 let xz = x * z2;
194 let yy = y * y2;
195 let yz = y * z2;
196 let zz = z * z2;
197 let wx = w * x2;
198 let wy = w * y2;
199 let wz = w * z2;
200
201 let x_axis = Vec4::new(1.0 - (yy + zz), xy + wz, xz - wy, 0.0);
202 let y_axis = Vec4::new(xy - wz, 1.0 - (xx + zz), yz + wx, 0.0);
203 let z_axis = Vec4::new(xz + wy, yz - wx, 1.0 - (xx + yy), 0.0);
204 (x_axis, y_axis, z_axis)
205 }
206
207 /// Creates an affine transformation matrix from the given 3D `scale`, `rotation` and
208 /// `translation`.
209 ///
210 /// The resulting matrix can be used to transform 3D points and vectors. See
211 /// [`Self::transform_point3()`] and [`Self::transform_vector3()`].
212 ///
213 /// # Panics
214 ///
215 /// Will panic if `rotation` is not normalized when `glam_assert` is enabled.
216 #[inline]
217 pub fn from_scale_rotation_translation(scale: Vec3, rotation: Quat, translation: Vec3) -> Self {
218 let (x_axis, y_axis, z_axis) = Self::quat_to_axes(rotation);
219 Self::from_cols(
220 x_axis.mul(scale.x),
221 y_axis.mul(scale.y),
222 z_axis.mul(scale.z),
223 Vec4::from((translation, 1.0)),
224 )
225 }
226
227 /// Creates an affine transformation matrix from the given 3D `translation`.
228 ///
229 /// The resulting matrix can be used to transform 3D points and vectors. See
230 /// [`Self::transform_point3()`] and [`Self::transform_vector3()`].
231 ///
232 /// # Panics
233 ///
234 /// Will panic if `rotation` is not normalized when `glam_assert` is enabled.
235 #[inline]
236 pub fn from_rotation_translation(rotation: Quat, translation: Vec3) -> Self {
237 let (x_axis, y_axis, z_axis) = Self::quat_to_axes(rotation);
238 Self::from_cols(x_axis, y_axis, z_axis, Vec4::from((translation, 1.0)))
239 }
240
241 /// Extracts `scale`, `rotation` and `translation` from `self`. The input matrix is
242 /// expected to be a 3D affine transformation matrix otherwise the output will be invalid.
243 ///
244 /// # Panics
245 ///
246 /// Will panic if the determinant of `self` is zero or if the resulting scale vector
247 /// contains any zero elements when `glam_assert` is enabled.
248 #[inline]
249 pub fn to_scale_rotation_translation(&self) -> (Vec3, Quat, Vec3) {
250 let det = self.determinant();
251 glam_assert!(det != 0.0);
252
253 let scale = Vec3::new(
254 self.x_axis.length() * det.signum(),
255 self.y_axis.length(),
256 self.z_axis.length(),
257 );
258
259 glam_assert!(scale.cmpne(Vec3::ZERO).all());
260
261 let inv_scale = scale.recip();
262
263 let rotation = Quat::from_rotation_axes(
264 self.x_axis.mul(inv_scale.x).xyz(),
265 self.y_axis.mul(inv_scale.y).xyz(),
266 self.z_axis.mul(inv_scale.z).xyz(),
267 );
268
269 let translation = self.w_axis.xyz();
270
271 (scale, rotation, translation)
272 }
273
274 /// Creates an affine transformation matrix from the given `rotation` quaternion.
275 ///
276 /// The resulting matrix can be used to transform 3D points and vectors. See
277 /// [`Self::transform_point3()`] and [`Self::transform_vector3()`].
278 ///
279 /// # Panics
280 ///
281 /// Will panic if `rotation` is not normalized when `glam_assert` is enabled.
282 #[inline]
283 pub fn from_quat(rotation: Quat) -> Self {
284 let (x_axis, y_axis, z_axis) = Self::quat_to_axes(rotation);
285 Self::from_cols(x_axis, y_axis, z_axis, Vec4::W)
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_mat3(m: Mat3) -> 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) = angle.sin_cos();
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) = angle.sin_cos();
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) = angle.sin_cos();
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) = angle.sin_cos();
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 Self {
547 x_axis: Vec4::new(self.x_axis.x, self.y_axis.x, self.z_axis.x, self.w_axis.x),
548 y_axis: Vec4::new(self.x_axis.y, self.y_axis.y, self.z_axis.y, self.w_axis.y),
549 z_axis: Vec4::new(self.x_axis.z, self.y_axis.z, self.z_axis.z, self.w_axis.z),
550 w_axis: Vec4::new(self.x_axis.w, self.y_axis.w, self.z_axis.w, self.w_axis.w),
551 }
552 }
553
554 /// Returns the determinant of `self`.
555 pub fn determinant(&self) -> f32 {
556 let (m00, m01, m02, m03) = self.x_axis.into();
557 let (m10, m11, m12, m13) = self.y_axis.into();
558 let (m20, m21, m22, m23) = self.z_axis.into();
559 let (m30, m31, m32, m33) = self.w_axis.into();
560
561 let a2323 = m22 * m33 - m23 * m32;
562 let a1323 = m21 * m33 - m23 * m31;
563 let a1223 = m21 * m32 - m22 * m31;
564 let a0323 = m20 * m33 - m23 * m30;
565 let a0223 = m20 * m32 - m22 * m30;
566 let a0123 = m20 * m31 - m21 * m30;
567
568 m00 * (m11 * a2323 - m12 * a1323 + m13 * a1223)
569 - m01 * (m10 * a2323 - m12 * a0323 + m13 * a0223)
570 + m02 * (m10 * a1323 - m11 * a0323 + m13 * a0123)
571 - m03 * (m10 * a1223 - m11 * a0223 + m12 * a0123)
572 }
573
574 /// Returns the inverse of `self`.
575 ///
576 /// If the matrix is not invertible the returned matrix will be invalid.
577 ///
578 /// # Panics
579 ///
580 /// Will panic if the determinant of `self` is zero when `glam_assert` is enabled.
581 #[must_use]
582 pub fn inverse(&self) -> Self {
583 let (m00, m01, m02, m03) = self.x_axis.into();
584 let (m10, m11, m12, m13) = self.y_axis.into();
585 let (m20, m21, m22, m23) = self.z_axis.into();
586 let (m30, m31, m32, m33) = self.w_axis.into();
587
588 let coef00 = m22 * m33 - m32 * m23;
589 let coef02 = m12 * m33 - m32 * m13;
590 let coef03 = m12 * m23 - m22 * m13;
591
592 let coef04 = m21 * m33 - m31 * m23;
593 let coef06 = m11 * m33 - m31 * m13;
594 let coef07 = m11 * m23 - m21 * m13;
595
596 let coef08 = m21 * m32 - m31 * m22;
597 let coef10 = m11 * m32 - m31 * m12;
598 let coef11 = m11 * m22 - m21 * m12;
599
600 let coef12 = m20 * m33 - m30 * m23;
601 let coef14 = m10 * m33 - m30 * m13;
602 let coef15 = m10 * m23 - m20 * m13;
603
604 let coef16 = m20 * m32 - m30 * m22;
605 let coef18 = m10 * m32 - m30 * m12;
606 let coef19 = m10 * m22 - m20 * m12;
607
608 let coef20 = m20 * m31 - m30 * m21;
609 let coef22 = m10 * m31 - m30 * m11;
610 let coef23 = m10 * m21 - m20 * m11;
611
612 let fac0 = Vec4::new(coef00, coef00, coef02, coef03);
613 let fac1 = Vec4::new(coef04, coef04, coef06, coef07);
614 let fac2 = Vec4::new(coef08, coef08, coef10, coef11);
615 let fac3 = Vec4::new(coef12, coef12, coef14, coef15);
616 let fac4 = Vec4::new(coef16, coef16, coef18, coef19);
617 let fac5 = Vec4::new(coef20, coef20, coef22, coef23);
618
619 let vec0 = Vec4::new(m10, m00, m00, m00);
620 let vec1 = Vec4::new(m11, m01, m01, m01);
621 let vec2 = Vec4::new(m12, m02, m02, m02);
622 let vec3 = Vec4::new(m13, m03, m03, m03);
623
624 let inv0 = vec1.mul(fac0).sub(vec2.mul(fac1)).add(vec3.mul(fac2));
625 let inv1 = vec0.mul(fac0).sub(vec2.mul(fac3)).add(vec3.mul(fac4));
626 let inv2 = vec0.mul(fac1).sub(vec1.mul(fac3)).add(vec3.mul(fac5));
627 let inv3 = vec0.mul(fac2).sub(vec1.mul(fac4)).add(vec2.mul(fac5));
628
629 let sign_a = Vec4::new(1.0, -1.0, 1.0, -1.0);
630 let sign_b = Vec4::new(-1.0, 1.0, -1.0, 1.0);
631
632 let inverse = Self::from_cols(
633 inv0.mul(sign_a),
634 inv1.mul(sign_b),
635 inv2.mul(sign_a),
636 inv3.mul(sign_b),
637 );
638
639 let col0 = Vec4::new(
640 inverse.x_axis.x,
641 inverse.y_axis.x,
642 inverse.z_axis.x,
643 inverse.w_axis.x,
644 );
645
646 let dot0 = self.x_axis.mul(col0);
647 let dot1 = dot0.x + dot0.y + dot0.z + dot0.w;
648
649 glam_assert!(dot1 != 0.0);
650
651 let rcp_det = dot1.recip();
652 inverse.mul(rcp_det)
653 }
654
655 #[inline]
656 fn look_to_lh(eye: Vec3, dir: Vec3, up: Vec3) -> Self {
657 let f = dir.normalize();
658 let s = up.cross(f).normalize();
659 let u = f.cross(s);
660 Self::from_cols(
661 Vec4::new(s.x, u.x, f.x, 0.0),
662 Vec4::new(s.y, u.y, f.y, 0.0),
663 Vec4::new(s.z, u.z, f.z, 0.0),
664 Vec4::new(-s.dot(eye), -u.dot(eye), -f.dot(eye), 1.0),
665 )
666 }
667
668 /// Creates a left-handed view matrix using a camera position, an up direction, and a focal
669 /// point.
670 /// For a view coordinate system with `+X=right`, `+Y=up` and `+Z=forward`.
671 ///
672 /// # Panics
673 ///
674 /// Will panic if `up` is not normalized when `glam_assert` is enabled.
675 #[inline]
676 pub fn look_at_lh(eye: Vec3, center: Vec3, up: Vec3) -> Self {
677 glam_assert!(up.is_normalized());
678 Self::look_to_lh(eye, center.sub(eye), up)
679 }
680
681 /// Creates a right-handed view matrix using a camera position, an up direction, and a focal
682 /// point.
683 /// For a view coordinate system with `+X=right`, `+Y=up` and `+Z=back`.
684 ///
685 /// # Panics
686 ///
687 /// Will panic if `up` is not normalized when `glam_assert` is enabled.
688 #[inline]
689 pub fn look_at_rh(eye: Vec3, center: Vec3, up: Vec3) -> Self {
690 glam_assert!(up.is_normalized());
691 Self::look_to_lh(eye, eye.sub(center), up)
692 }
693
694 /// Creates a right-handed perspective projection matrix with [-1,1] depth range.
695 /// This is the same as the OpenGL `gluPerspective` function.
696 /// See <https://www.khronos.org/registry/OpenGL-Refpages/gl2.1/xhtml/gluPerspective.xml>
697 #[inline]
698 pub fn perspective_rh_gl(
699 fov_y_radians: f32,
700 aspect_ratio: f32,
701 z_near: f32,
702 z_far: f32,
703 ) -> Self {
704 let inv_length = 1.0 / (z_near - z_far);
705 let f = 1.0 / (0.5 * fov_y_radians).tan();
706 let a = f / aspect_ratio;
707 let b = (z_near + z_far) * inv_length;
708 let c = (2.0 * z_near * z_far) * inv_length;
709 Self::from_cols(
710 Vec4::new(a, 0.0, 0.0, 0.0),
711 Vec4::new(0.0, f, 0.0, 0.0),
712 Vec4::new(0.0, 0.0, b, -1.0),
713 Vec4::new(0.0, 0.0, c, 0.0),
714 )
715 }
716
717 /// Creates a left-handed perspective projection matrix with `[0,1]` depth range.
718 ///
719 /// # Panics
720 ///
721 /// Will panic if `z_near` or `z_far` are less than or equal to zero when `glam_assert` is
722 /// enabled.
723 #[inline]
724 pub fn perspective_lh(fov_y_radians: f32, aspect_ratio: f32, z_near: f32, z_far: f32) -> Self {
725 glam_assert!(z_near > 0.0 && z_far > 0.0);
726 let (sin_fov, cos_fov) = (0.5 * fov_y_radians).sin_cos();
727 let h = cos_fov / sin_fov;
728 let w = h / aspect_ratio;
729 let r = z_far / (z_far - z_near);
730 Self::from_cols(
731 Vec4::new(w, 0.0, 0.0, 0.0),
732 Vec4::new(0.0, h, 0.0, 0.0),
733 Vec4::new(0.0, 0.0, r, 1.0),
734 Vec4::new(0.0, 0.0, -r * z_near, 0.0),
735 )
736 }
737
738 /// Creates a right-handed perspective projection matrix with `[0,1]` depth range.
739 ///
740 /// # Panics
741 ///
742 /// Will panic if `z_near` or `z_far` are less than or equal to zero when `glam_assert` is
743 /// enabled.
744 #[inline]
745 pub fn perspective_rh(fov_y_radians: f32, aspect_ratio: f32, z_near: f32, z_far: f32) -> Self {
746 glam_assert!(z_near > 0.0 && z_far > 0.0);
747 let (sin_fov, cos_fov) = (0.5 * fov_y_radians).sin_cos();
748 let h = cos_fov / sin_fov;
749 let w = h / aspect_ratio;
750 let r = z_far / (z_near - z_far);
751 Self::from_cols(
752 Vec4::new(w, 0.0, 0.0, 0.0),
753 Vec4::new(0.0, h, 0.0, 0.0),
754 Vec4::new(0.0, 0.0, r, -1.0),
755 Vec4::new(0.0, 0.0, r * z_near, 0.0),
756 )
757 }
758
759 /// Creates an infinite left-handed perspective projection matrix with `[0,1]` depth range.
760 ///
761 /// # Panics
762 ///
763 /// Will panic if `z_near` is less than or equal to zero when `glam_assert` is enabled.
764 #[inline]
765 pub fn perspective_infinite_lh(fov_y_radians: f32, aspect_ratio: f32, z_near: f32) -> Self {
766 glam_assert!(z_near > 0.0);
767 let (sin_fov, cos_fov) = (0.5 * fov_y_radians).sin_cos();
768 let h = cos_fov / sin_fov;
769 let w = h / aspect_ratio;
770 Self::from_cols(
771 Vec4::new(w, 0.0, 0.0, 0.0),
772 Vec4::new(0.0, h, 0.0, 0.0),
773 Vec4::new(0.0, 0.0, 1.0, 1.0),
774 Vec4::new(0.0, 0.0, -z_near, 0.0),
775 )
776 }
777
778 /// Creates an infinite left-handed perspective projection matrix with `[0,1]` depth range.
779 ///
780 /// # Panics
781 ///
782 /// Will panic if `z_near` is less than or equal to zero when `glam_assert` is enabled.
783 #[inline]
784 pub fn perspective_infinite_reverse_lh(
785 fov_y_radians: f32,
786 aspect_ratio: f32,
787 z_near: f32,
788 ) -> Self {
789 glam_assert!(z_near > 0.0);
790 let (sin_fov, cos_fov) = (0.5 * fov_y_radians).sin_cos();
791 let h = cos_fov / sin_fov;
792 let w = h / aspect_ratio;
793 Self::from_cols(
794 Vec4::new(w, 0.0, 0.0, 0.0),
795 Vec4::new(0.0, h, 0.0, 0.0),
796 Vec4::new(0.0, 0.0, 0.0, 1.0),
797 Vec4::new(0.0, 0.0, z_near, 0.0),
798 )
799 }
800
801 /// Creates an infinite right-handed perspective projection matrix with
802 /// `[0,1]` depth range.
803 #[inline]
804 pub fn perspective_infinite_rh(fov_y_radians: f32, aspect_ratio: f32, z_near: f32) -> Self {
805 glam_assert!(z_near > 0.0);
806 let f = 1.0 / (0.5 * fov_y_radians).tan();
807 Self::from_cols(
808 Vec4::new(f / aspect_ratio, 0.0, 0.0, 0.0),
809 Vec4::new(0.0, f, 0.0, 0.0),
810 Vec4::new(0.0, 0.0, -1.0, -1.0),
811 Vec4::new(0.0, 0.0, -z_near, 0.0),
812 )
813 }
814
815 /// Creates an infinite reverse right-handed perspective projection matrix
816 /// with `[0,1]` depth range.
817 #[inline]
818 pub fn perspective_infinite_reverse_rh(
819 fov_y_radians: f32,
820 aspect_ratio: f32,
821 z_near: f32,
822 ) -> Self {
823 glam_assert!(z_near > 0.0);
824 let f = 1.0 / (0.5 * fov_y_radians).tan();
825 Self::from_cols(
826 Vec4::new(f / aspect_ratio, 0.0, 0.0, 0.0),
827 Vec4::new(0.0, f, 0.0, 0.0),
828 Vec4::new(0.0, 0.0, 0.0, -1.0),
829 Vec4::new(0.0, 0.0, z_near, 0.0),
830 )
831 }
832
833 /// Creates a right-handed orthographic projection matrix with `[-1,1]` depth
834 /// range. This is the same as the OpenGL `glOrtho` function in OpenGL.
835 /// See
836 /// <https://www.khronos.org/registry/OpenGL-Refpages/gl2.1/xhtml/glOrtho.xml>
837 #[inline]
838 pub fn orthographic_rh_gl(
839 left: f32,
840 right: f32,
841 bottom: f32,
842 top: f32,
843 near: f32,
844 far: f32,
845 ) -> Self {
846 let a = 2.0 / (right - left);
847 let b = 2.0 / (top - bottom);
848 let c = -2.0 / (far - near);
849 let tx = -(right + left) / (right - left);
850 let ty = -(top + bottom) / (top - bottom);
851 let tz = -(far + near) / (far - near);
852
853 Self::from_cols(
854 Vec4::new(a, 0.0, 0.0, 0.0),
855 Vec4::new(0.0, b, 0.0, 0.0),
856 Vec4::new(0.0, 0.0, c, 0.0),
857 Vec4::new(tx, ty, tz, 1.0),
858 )
859 }
860
861 /// Creates a left-handed orthographic projection matrix with `[0,1]` depth range.
862 #[inline]
863 pub fn orthographic_lh(
864 left: f32,
865 right: f32,
866 bottom: f32,
867 top: f32,
868 near: f32,
869 far: f32,
870 ) -> Self {
871 let rcp_width = 1.0 / (right - left);
872 let rcp_height = 1.0 / (top - bottom);
873 let r = 1.0 / (far - near);
874 Self::from_cols(
875 Vec4::new(rcp_width + rcp_width, 0.0, 0.0, 0.0),
876 Vec4::new(0.0, rcp_height + rcp_height, 0.0, 0.0),
877 Vec4::new(0.0, 0.0, r, 0.0),
878 Vec4::new(
879 -(left + right) * rcp_width,
880 -(top + bottom) * rcp_height,
881 -r * near,
882 1.0,
883 ),
884 )
885 }
886
887 /// Creates a right-handed orthographic projection matrix with `[0,1]` depth range.
888 #[inline]
889 pub fn orthographic_rh(
890 left: f32,
891 right: f32,
892 bottom: f32,
893 top: f32,
894 near: f32,
895 far: f32,
896 ) -> Self {
897 let rcp_width = 1.0 / (right - left);
898 let rcp_height = 1.0 / (top - bottom);
899 let r = 1.0 / (near - far);
900 Self::from_cols(
901 Vec4::new(rcp_width + rcp_width, 0.0, 0.0, 0.0),
902 Vec4::new(0.0, rcp_height + rcp_height, 0.0, 0.0),
903 Vec4::new(0.0, 0.0, r, 0.0),
904 Vec4::new(
905 -(left + right) * rcp_width,
906 -(top + bottom) * rcp_height,
907 r * near,
908 1.0,
909 ),
910 )
911 }
912
913 /// Transforms the given 3D vector as a point, applying perspective correction.
914 ///
915 /// This is the equivalent of multiplying the 3D vector as a 4D vector where `w` is `1.0`.
916 /// The perspective divide is performed meaning the resulting 3D vector is divided by `w`.
917 ///
918 /// This method assumes that `self` contains a projective transform.
919 #[inline]
920 pub fn project_point3(&self, rhs: Vec3) -> Vec3 {
921 let mut res = self.x_axis.mul(rhs.x);
922 res = self.y_axis.mul(rhs.y).add(res);
923 res = self.z_axis.mul(rhs.z).add(res);
924 res = self.w_axis.add(res);
925 res = res.mul(res.wwww().recip());
926 res.xyz()
927 }
928
929 /// Transforms the given 3D vector as a point.
930 ///
931 /// This is the equivalent of multiplying the 3D vector as a 4D vector where `w` is
932 /// `1.0`.
933 ///
934 /// This method assumes that `self` contains a valid affine transform. It does not perform
935 /// a persective divide, if `self` contains a perspective transform, or if you are unsure,
936 /// the [`Self::project_point3()`] method should be used instead.
937 ///
938 /// # Panics
939 ///
940 /// Will panic if the 3rd row of `self` is not `(0, 0, 0, 1)` when `glam_assert` is enabled.
941 #[inline]
942 pub fn transform_point3(&self, rhs: Vec3) -> Vec3 {
943 glam_assert!(self.row(3).abs_diff_eq(Vec4::W, 1e-6));
944 let mut res = self.x_axis.mul(rhs.x);
945 res = self.y_axis.mul(rhs.y).add(res);
946 res = self.z_axis.mul(rhs.z).add(res);
947 res = self.w_axis.add(res);
948 res.xyz()
949 }
950
951 /// Transforms the give 3D vector as a direction.
952 ///
953 /// This is the equivalent of multiplying the 3D vector as a 4D vector where `w` is
954 /// `0.0`.
955 ///
956 /// This method assumes that `self` contains a valid affine transform.
957 ///
958 /// # Panics
959 ///
960 /// Will panic if the 3rd row of `self` is not `(0, 0, 0, 1)` when `glam_assert` is enabled.
961 #[inline]
962 pub fn transform_vector3(&self, rhs: Vec3) -> Vec3 {
963 glam_assert!(self.row(3).abs_diff_eq(Vec4::W, 1e-6));
964 let mut res = self.x_axis.mul(rhs.x);
965 res = self.y_axis.mul(rhs.y).add(res);
966 res = self.z_axis.mul(rhs.z).add(res);
967 res.xyz()
968 }
969
970 /// Transforms the given `Vec3A` as 3D point.
971 ///
972 /// This is the equivalent of multiplying the `Vec3A` as a 4D vector where `w` is `1.0`.
973 #[inline]
974 pub fn transform_point3a(&self, rhs: Vec3A) -> Vec3A {
975 self.transform_point3(rhs.into()).into()
976 }
977
978 /// Transforms the give `Vec3A` as 3D vector.
979 ///
980 /// This is the equivalent of multiplying the `Vec3A` as a 4D vector where `w` is `0.0`.
981 #[inline]
982 pub fn transform_vector3a(&self, rhs: Vec3A) -> Vec3A {
983 self.transform_vector3(rhs.into()).into()
984 }
985
986 /// Transforms a 4D vector.
987 #[inline]
988 pub fn mul_vec4(&self, rhs: Vec4) -> Vec4 {
989 let mut res = self.x_axis.mul(rhs.x);
990 res = res.add(self.y_axis.mul(rhs.y));
991 res = res.add(self.z_axis.mul(rhs.z));
992 res = res.add(self.w_axis.mul(rhs.w));
993 res
994 }
995
996 /// Multiplies two 4x4 matrices.
997 #[inline]
998 pub fn mul_mat4(&self, rhs: &Self) -> Self {
999 Self::from_cols(
1000 self.mul(rhs.x_axis),
1001 self.mul(rhs.y_axis),
1002 self.mul(rhs.z_axis),
1003 self.mul(rhs.w_axis),
1004 )
1005 }
1006
1007 /// Adds two 4x4 matrices.
1008 #[inline]
1009 pub fn add_mat4(&self, rhs: &Self) -> Self {
1010 Self::from_cols(
1011 self.x_axis.add(rhs.x_axis),
1012 self.y_axis.add(rhs.y_axis),
1013 self.z_axis.add(rhs.z_axis),
1014 self.w_axis.add(rhs.w_axis),
1015 )
1016 }
1017
1018 /// Subtracts two 4x4 matrices.
1019 #[inline]
1020 pub fn sub_mat4(&self, rhs: &Self) -> Self {
1021 Self::from_cols(
1022 self.x_axis.sub(rhs.x_axis),
1023 self.y_axis.sub(rhs.y_axis),
1024 self.z_axis.sub(rhs.z_axis),
1025 self.w_axis.sub(rhs.w_axis),
1026 )
1027 }
1028
1029 /// Multiplies a 4x4 matrix by a scalar.
1030 #[inline]
1031 pub fn mul_scalar(&self, rhs: f32) -> Self {
1032 Self::from_cols(
1033 self.x_axis.mul(rhs),
1034 self.y_axis.mul(rhs),
1035 self.z_axis.mul(rhs),
1036 self.w_axis.mul(rhs),
1037 )
1038 }
1039
1040 /// Returns true if the absolute difference of all elements between `self` and `rhs`
1041 /// is less than or equal to `max_abs_diff`.
1042 ///
1043 /// This can be used to compare if two matrices contain similar elements. It works best
1044 /// when comparing with a known value. The `max_abs_diff` that should be used used
1045 /// depends on the values being compared against.
1046 ///
1047 /// For more see
1048 /// [comparing floating point numbers](https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/).
1049 #[inline]
1050 pub fn abs_diff_eq(&self, rhs: Self, max_abs_diff: f32) -> bool {
1051 self.x_axis.abs_diff_eq(rhs.x_axis, max_abs_diff)
1052 && self.y_axis.abs_diff_eq(rhs.y_axis, max_abs_diff)
1053 && self.z_axis.abs_diff_eq(rhs.z_axis, max_abs_diff)
1054 && self.w_axis.abs_diff_eq(rhs.w_axis, max_abs_diff)
1055 }
1056
1057 #[inline]
1058 pub fn as_dmat4(&self) -> DMat4 {
1059 DMat4::from_cols(
1060 self.x_axis.as_dvec4(),
1061 self.y_axis.as_dvec4(),
1062 self.z_axis.as_dvec4(),
1063 self.w_axis.as_dvec4(),
1064 )
1065 }
1066 }
1067
1068 impl Default for Mat4 {
1069 #[inline]
1070 fn default() -> Self {
1071 Self::IDENTITY
1072 }
1073 }
1074
1075 impl Add<Mat4> for Mat4 {
1076 type Output = Self;
1077 #[inline]
1078 fn add(self, rhs: Self) -> Self::Output {
1079 self.add_mat4(&rhs)
1080 }
1081 }
1082
1083 impl AddAssign<Mat4> for Mat4 {
1084 #[inline]
1085 fn add_assign(&mut self, rhs: Self) {
1086 *self = self.add_mat4(&rhs);
1087 }
1088 }
1089
1090 impl Sub<Mat4> for Mat4 {
1091 type Output = Self;
1092 #[inline]
1093 fn sub(self, rhs: Self) -> Self::Output {
1094 self.sub_mat4(&rhs)
1095 }
1096 }
1097
1098 impl SubAssign<Mat4> for Mat4 {
1099 #[inline]
1100 fn sub_assign(&mut self, rhs: Self) {
1101 *self = self.sub_mat4(&rhs);
1102 }
1103 }
1104
1105 impl Neg for Mat4 {
1106 type Output = Self;
1107 #[inline]
1108 fn neg(self) -> Self::Output {
1109 Self::from_cols(
1110 self.x_axis.neg(),
1111 self.y_axis.neg(),
1112 self.z_axis.neg(),
1113 self.w_axis.neg(),
1114 )
1115 }
1116 }
1117
1118 impl Mul<Mat4> for Mat4 {
1119 type Output = Self;
1120 #[inline]
1121 fn mul(self, rhs: Self) -> Self::Output {
1122 self.mul_mat4(&rhs)
1123 }
1124 }
1125
1126 impl MulAssign<Mat4> for Mat4 {
1127 #[inline]
1128 fn mul_assign(&mut self, rhs: Self) {
1129 *self = self.mul_mat4(&rhs);
1130 }
1131 }
1132
1133 impl Mul<Vec4> for Mat4 {
1134 type Output = Vec4;
1135 #[inline]
1136 fn mul(self, rhs: Vec4) -> Self::Output {
1137 self.mul_vec4(rhs)
1138 }
1139 }
1140
1141 impl Mul<Mat4> for f32 {
1142 type Output = Mat4;
1143 #[inline]
1144 fn mul(self, rhs: Mat4) -> Self::Output {
1145 rhs.mul_scalar(self)
1146 }
1147 }
1148
1149 impl Mul<f32> for Mat4 {
1150 type Output = Self;
1151 #[inline]
1152 fn mul(self, rhs: f32) -> Self::Output {
1153 self.mul_scalar(rhs)
1154 }
1155 }
1156
1157 impl MulAssign<f32> for Mat4 {
1158 #[inline]
1159 fn mul_assign(&mut self, rhs: f32) {
1160 *self = self.mul_scalar(rhs);
1161 }
1162 }
1163
1164 impl<'a> Sum<&'a Self> for Mat4 {
1165 fn sum<I>(iter: I) -> Self
1166 where
1167 I: Iterator<Item = &'a Self>,
1168 {
1169 iter.fold(Self::ZERO, |a, &b| Self::add(a, b))
1170 }
1171 }
1172
1173 impl<'a> Product<&'a Self> for Mat4 {
1174 fn product<I>(iter: I) -> Self
1175 where
1176 I: Iterator<Item = &'a Self>,
1177 {
1178 iter.fold(Self::IDENTITY, |a, &b| Self::mul(a, b))
1179 }
1180 }
1181
1182 impl PartialEq for Mat4 {
1183 #[inline]
1184 fn eq(&self, rhs: &Self) -> bool {
1185 self.x_axis.eq(&rhs.x_axis)
1186 && self.y_axis.eq(&rhs.y_axis)
1187 && self.z_axis.eq(&rhs.z_axis)
1188 && self.w_axis.eq(&rhs.w_axis)
1189 }
1190 }
1191
1192 #[cfg(not(target_arch = "spirv"))]
1193 impl AsRef<[f32; 16]> for Mat4 {
1194 #[inline]
1195 fn as_ref(&self) -> &[f32; 16] {
1196 unsafe { &*(self as *const Self as *const [f32; 16]) }
1197 }
1198 }
1199
1200 #[cfg(not(target_arch = "spirv"))]
1201 impl AsMut<[f32; 16]> for Mat4 {
1202 #[inline]
1203 fn as_mut(&mut self) -> &mut [f32; 16] {
1204 unsafe { &mut *(self as *mut Self as *mut [f32; 16]) }
1205 }
1206 }
1207
1208 #[cfg(not(target_arch = "spirv"))]
1209 impl fmt::Debug for Mat4 {
1210 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
1211 fmt.debug_struct(stringify!(Mat4))
1212 .field("x_axis", &self.x_axis)
1213 .field("y_axis", &self.y_axis)
1214 .field("z_axis", &self.z_axis)
1215 .field("w_axis", &self.w_axis)
1216 .finish()
1217 }
1218 }
1219
1220 #[cfg(not(target_arch = "spirv"))]
1221 impl fmt::Display for Mat4 {
1222 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1223 write!(
1224 f,
1225 "[{}, {}, {}, {}]",
1226 self.x_axis, self.y_axis, self.z_axis, self.w_axis
1227 )
1228 }
1229 }