Debian Codegraph @

debian/missing-sources/glam/src/f32/coresimd/mat3a.rs

raw
1 // Generated from mat.rs.tera template. Edit the template, not the generated file.
2
3 use crate::{
4 euler::{FromEuler, ToEuler},
5 f32::math,
6 swizzles::*,
7 DMat3, EulerRot, Mat2, Mat3, Mat4, Quat, Vec2, Vec3, Vec3A,
8 };
9 use core::fmt;
10 use core::iter::{Product, Sum};
11 use core::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssign};
12
13 use core::simd::*;
14
15 /// Creates a 3x3 matrix from three column vectors.
16 #[inline(always)]
17 #[must_use]
18 pub const fn mat3a(x_axis: Vec3A, y_axis: Vec3A, z_axis: Vec3A) -> Mat3A {
19 Mat3A::from_cols(x_axis, y_axis, z_axis)
20 }
21
22 /// A 3x3 column major matrix.
23 ///
24 /// This 3x3 matrix type features convenience methods for creating and using linear and
25 /// affine transformations. If you are primarily dealing with 2D affine transformations the
26 /// [`Affine2`](crate::Affine2) type is much faster and more space efficient than
27 /// using a 3x3 matrix.
28 ///
29 /// Linear transformations including 3D rotation and scale can be created using methods
30 /// such as [`Self::from_diagonal()`], [`Self::from_quat()`], [`Self::from_axis_angle()`],
31 /// [`Self::from_rotation_x()`], [`Self::from_rotation_y()`], or
32 /// [`Self::from_rotation_z()`].
33 ///
34 /// The resulting matrices can be use to transform 3D vectors using regular vector
35 /// multiplication.
36 ///
37 /// Affine transformations including 2D translation, rotation and scale can be created
38 /// using methods such as [`Self::from_translation()`], [`Self::from_angle()`],
39 /// [`Self::from_scale()`] and [`Self::from_scale_angle_translation()`].
40 ///
41 /// The [`Self::transform_point2()`] and [`Self::transform_vector2()`] convenience methods
42 /// are provided for performing affine transforms on 2D vectors and points. These multiply
43 /// 2D inputs as 3D vectors with an implicit `z` value of `1` for points and `0` for
44 /// vectors respectively. These methods assume that `Self` contains a valid affine
45 /// transform.
46 #[derive(Clone, Copy)]
47 #[cfg_attr(feature = "bytemuck", derive(bytemuck::Pod, bytemuck::Zeroable))]
48 #[repr(C)]
49 pub struct Mat3A {
50 pub x_axis: Vec3A,
51 pub y_axis: Vec3A,
52 pub z_axis: Vec3A,
53 }
54
55 impl Mat3A {
56 /// A 3x3 matrix with all elements set to `0.0`.
57 pub const ZERO: Self = Self::from_cols(Vec3A::ZERO, Vec3A::ZERO, Vec3A::ZERO);
58
59 /// A 3x3 identity matrix, where all diagonal elements are `1`, and all off-diagonal elements are `0`.
60 pub const IDENTITY: Self = Self::from_cols(Vec3A::X, Vec3A::Y, Vec3A::Z);
61
62 /// All NAN:s.
63 pub const NAN: Self = Self::from_cols(Vec3A::NAN, Vec3A::NAN, Vec3A::NAN);
64
65 #[allow(clippy::too_many_arguments)]
66 #[inline(always)]
67 #[must_use]
68 const fn new(
69 m00: f32,
70 m01: f32,
71 m02: f32,
72 m10: f32,
73 m11: f32,
74 m12: f32,
75 m20: f32,
76 m21: f32,
77 m22: f32,
78 ) -> Self {
79 Self {
80 x_axis: Vec3A::new(m00, m01, m02),
81 y_axis: Vec3A::new(m10, m11, m12),
82 z_axis: Vec3A::new(m20, m21, m22),
83 }
84 }
85
86 /// Creates a 3x3 matrix from three column vectors.
87 #[inline(always)]
88 #[must_use]
89 pub const fn from_cols(x_axis: Vec3A, y_axis: Vec3A, z_axis: Vec3A) -> Self {
90 Self {
91 x_axis,
92 y_axis,
93 z_axis,
94 }
95 }
96
97 /// Creates a 3x3 matrix from a `[f32; 9]` array stored in column major order.
98 /// If your data is stored in row major you will need to `transpose` the returned
99 /// matrix.
100 #[inline]
101 #[must_use]
102 pub const fn from_cols_array(m: &[f32; 9]) -> Self {
103 Self::new(m[0], m[1], m[2], m[3], m[4], m[5], m[6], m[7], m[8])
104 }
105
106 /// Creates a `[f32; 9]` array storing data in column major order.
107 /// If you require data in row major order `transpose` the matrix first.
108 #[inline]
109 #[must_use]
110 pub const fn to_cols_array(&self) -> [f32; 9] {
111 let [x_axis_x, x_axis_y, x_axis_z] = self.x_axis.to_array();
112 let [y_axis_x, y_axis_y, y_axis_z] = self.y_axis.to_array();
113 let [z_axis_x, z_axis_y, z_axis_z] = self.z_axis.to_array();
114
115 [
116 x_axis_x, x_axis_y, x_axis_z, y_axis_x, y_axis_y, y_axis_z, z_axis_x, z_axis_y,
117 z_axis_z,
118 ]
119 }
120
121 /// Creates a 3x3 matrix from a `[[f32; 3]; 3]` 3D array stored in column major order.
122 /// If your data is in row major order you will need to `transpose` the returned
123 /// matrix.
124 #[inline]
125 #[must_use]
126 pub const fn from_cols_array_2d(m: &[[f32; 3]; 3]) -> Self {
127 Self::from_cols(
128 Vec3A::from_array(m[0]),
129 Vec3A::from_array(m[1]),
130 Vec3A::from_array(m[2]),
131 )
132 }
133
134 /// Creates a `[[f32; 3]; 3]` 3D array storing data in column major order.
135 /// If you require data in row major order `transpose` the matrix first.
136 #[inline]
137 #[must_use]
138 pub const fn to_cols_array_2d(&self) -> [[f32; 3]; 3] {
139 [
140 self.x_axis.to_array(),
141 self.y_axis.to_array(),
142 self.z_axis.to_array(),
143 ]
144 }
145
146 /// Creates a 3x3 matrix with its diagonal set to `diagonal` and all other entries set to 0.
147 #[doc(alias = "scale")]
148 #[inline]
149 #[must_use]
150 pub const fn from_diagonal(diagonal: Vec3) -> Self {
151 Self::new(
152 diagonal.x, 0.0, 0.0, 0.0, diagonal.y, 0.0, 0.0, 0.0, diagonal.z,
153 )
154 }
155
156 /// Creates a 3x3 matrix from a 4x4 matrix, discarding the 4th row and column.
157 #[inline]
158 #[must_use]
159 pub fn from_mat4(m: Mat4) -> Self {
160 Self::from_cols(
161 Vec3A::from_vec4(m.x_axis),
162 Vec3A::from_vec4(m.y_axis),
163 Vec3A::from_vec4(m.z_axis),
164 )
165 }
166
167 /// Creates a 3x3 matrix from the minor of the given 4x4 matrix, discarding the `i`th column
168 /// and `j`th row.
169 ///
170 /// # Panics
171 ///
172 /// Panics if `i` or `j` is greater than 3.
173 #[inline]
174 #[must_use]
175 pub fn from_mat4_minor(m: Mat4, i: usize, j: usize) -> Self {
176 match (i, j) {
177 (0, 0) => Self::from_cols(
178 Vec3A::from_vec4(m.y_axis.yzww()),
179 Vec3A::from_vec4(m.z_axis.yzww()),
180 Vec3A::from_vec4(m.w_axis.yzww()),
181 ),
182 (0, 1) => Self::from_cols(
183 Vec3A::from_vec4(m.y_axis.xzww()),
184 Vec3A::from_vec4(m.z_axis.xzww()),
185 Vec3A::from_vec4(m.w_axis.xzww()),
186 ),
187 (0, 2) => Self::from_cols(
188 Vec3A::from_vec4(m.y_axis.xyww()),
189 Vec3A::from_vec4(m.z_axis.xyww()),
190 Vec3A::from_vec4(m.w_axis.xyww()),
191 ),
192 (0, 3) => Self::from_cols(
193 Vec3A::from_vec4(m.y_axis.xyzw()),
194 Vec3A::from_vec4(m.z_axis.xyzw()),
195 Vec3A::from_vec4(m.w_axis.xyzw()),
196 ),
197 (1, 0) => Self::from_cols(
198 Vec3A::from_vec4(m.x_axis.yzww()),
199 Vec3A::from_vec4(m.z_axis.yzww()),
200 Vec3A::from_vec4(m.w_axis.yzww()),
201 ),
202 (1, 1) => Self::from_cols(
203 Vec3A::from_vec4(m.x_axis.xzww()),
204 Vec3A::from_vec4(m.z_axis.xzww()),
205 Vec3A::from_vec4(m.w_axis.xzww()),
206 ),
207 (1, 2) => Self::from_cols(
208 Vec3A::from_vec4(m.x_axis.xyww()),
209 Vec3A::from_vec4(m.z_axis.xyww()),
210 Vec3A::from_vec4(m.w_axis.xyww()),
211 ),
212 (1, 3) => Self::from_cols(
213 Vec3A::from_vec4(m.x_axis.xyzw()),
214 Vec3A::from_vec4(m.z_axis.xyzw()),
215 Vec3A::from_vec4(m.w_axis.xyzw()),
216 ),
217 (2, 0) => Self::from_cols(
218 Vec3A::from_vec4(m.x_axis.yzww()),
219 Vec3A::from_vec4(m.y_axis.yzww()),
220 Vec3A::from_vec4(m.w_axis.yzww()),
221 ),
222 (2, 1) => Self::from_cols(
223 Vec3A::from_vec4(m.x_axis.xzww()),
224 Vec3A::from_vec4(m.y_axis.xzww()),
225 Vec3A::from_vec4(m.w_axis.xzww()),
226 ),
227 (2, 2) => Self::from_cols(
228 Vec3A::from_vec4(m.x_axis.xyww()),
229 Vec3A::from_vec4(m.y_axis.xyww()),
230 Vec3A::from_vec4(m.w_axis.xyww()),
231 ),
232 (2, 3) => Self::from_cols(
233 Vec3A::from_vec4(m.x_axis.xyzw()),
234 Vec3A::from_vec4(m.y_axis.xyzw()),
235 Vec3A::from_vec4(m.w_axis.xyzw()),
236 ),
237 (3, 0) => Self::from_cols(
238 Vec3A::from_vec4(m.x_axis.yzww()),
239 Vec3A::from_vec4(m.y_axis.yzww()),
240 Vec3A::from_vec4(m.z_axis.yzww()),
241 ),
242 (3, 1) => Self::from_cols(
243 Vec3A::from_vec4(m.x_axis.xzww()),
244 Vec3A::from_vec4(m.y_axis.xzww()),
245 Vec3A::from_vec4(m.z_axis.xzww()),
246 ),
247 (3, 2) => Self::from_cols(
248 Vec3A::from_vec4(m.x_axis.xyww()),
249 Vec3A::from_vec4(m.y_axis.xyww()),
250 Vec3A::from_vec4(m.z_axis.xyww()),
251 ),
252 (3, 3) => Self::from_cols(
253 Vec3A::from_vec4(m.x_axis.xyzw()),
254 Vec3A::from_vec4(m.y_axis.xyzw()),
255 Vec3A::from_vec4(m.z_axis.xyzw()),
256 ),
257 _ => panic!("index out of bounds"),
258 }
259 }
260
261 /// Creates a 3D rotation matrix from the given quaternion.
262 ///
263 /// # Panics
264 ///
265 /// Will panic if `rotation` is not normalized when `glam_assert` is enabled.
266 #[inline]
267 #[must_use]
268 pub fn from_quat(rotation: Quat) -> Self {
269 glam_assert!(rotation.is_normalized());
270
271 let x2 = rotation.x + rotation.x;
272 let y2 = rotation.y + rotation.y;
273 let z2 = rotation.z + rotation.z;
274 let xx = rotation.x * x2;
275 let xy = rotation.x * y2;
276 let xz = rotation.x * z2;
277 let yy = rotation.y * y2;
278 let yz = rotation.y * z2;
279 let zz = rotation.z * z2;
280 let wx = rotation.w * x2;
281 let wy = rotation.w * y2;
282 let wz = rotation.w * z2;
283
284 Self::from_cols(
285 Vec3A::new(1.0 - (yy + zz), xy + wz, xz - wy),
286 Vec3A::new(xy - wz, 1.0 - (xx + zz), yz + wx),
287 Vec3A::new(xz + wy, yz - wx, 1.0 - (xx + yy)),
288 )
289 }
290
291 /// Creates a 3D rotation matrix from a normalized rotation `axis` and `angle` (in
292 /// radians).
293 ///
294 /// # Panics
295 ///
296 /// Will panic if `axis` is not normalized when `glam_assert` is enabled.
297 #[inline]
298 #[must_use]
299 pub fn from_axis_angle(axis: Vec3, angle: f32) -> Self {
300 glam_assert!(axis.is_normalized());
301
302 let (sin, cos) = math::sin_cos(angle);
303 let (xsin, ysin, zsin) = axis.mul(sin).into();
304 let (x, y, z) = axis.into();
305 let (x2, y2, z2) = axis.mul(axis).into();
306 let omc = 1.0 - cos;
307 let xyomc = x * y * omc;
308 let xzomc = x * z * omc;
309 let yzomc = y * z * omc;
310 Self::from_cols(
311 Vec3A::new(x2 * omc + cos, xyomc + zsin, xzomc - ysin),
312 Vec3A::new(xyomc - zsin, y2 * omc + cos, yzomc + xsin),
313 Vec3A::new(xzomc + ysin, yzomc - xsin, z2 * omc + cos),
314 )
315 }
316
317 /// Creates a 3D rotation matrix from the given euler rotation sequence and the angles (in
318 /// radians).
319 #[inline]
320 #[must_use]
321 pub fn from_euler(order: EulerRot, a: f32, b: f32, c: f32) -> Self {
322 Self::from_euler_angles(order, a, b, c)
323 }
324
325 /// Extract Euler angles with the given Euler rotation order.
326 ///
327 /// Note if the input matrix contains scales, shears, or other non-rotation transformations then
328 /// the resulting Euler angles will be ill-defined.
329 ///
330 /// # Panics
331 ///
332 /// Will panic if any input matrix column is not normalized when `glam_assert` is enabled.
333 #[inline]
334 #[must_use]
335 pub fn to_euler(&self, order: EulerRot) -> (f32, f32, f32) {
336 glam_assert!(
337 self.x_axis.is_normalized()
338 && self.y_axis.is_normalized()
339 && self.z_axis.is_normalized()
340 );
341 self.to_euler_angles(order)
342 }
343
344 /// Creates a 3D rotation matrix from `angle` (in radians) around the x axis.
345 #[inline]
346 #[must_use]
347 pub fn from_rotation_x(angle: f32) -> Self {
348 let (sina, cosa) = math::sin_cos(angle);
349 Self::from_cols(
350 Vec3A::X,
351 Vec3A::new(0.0, cosa, sina),
352 Vec3A::new(0.0, -sina, cosa),
353 )
354 }
355
356 /// Creates a 3D rotation matrix from `angle` (in radians) around the y axis.
357 #[inline]
358 #[must_use]
359 pub fn from_rotation_y(angle: f32) -> Self {
360 let (sina, cosa) = math::sin_cos(angle);
361 Self::from_cols(
362 Vec3A::new(cosa, 0.0, -sina),
363 Vec3A::Y,
364 Vec3A::new(sina, 0.0, cosa),
365 )
366 }
367
368 /// Creates a 3D rotation matrix from `angle` (in radians) around the z axis.
369 #[inline]
370 #[must_use]
371 pub fn from_rotation_z(angle: f32) -> Self {
372 let (sina, cosa) = math::sin_cos(angle);
373 Self::from_cols(
374 Vec3A::new(cosa, sina, 0.0),
375 Vec3A::new(-sina, cosa, 0.0),
376 Vec3A::Z,
377 )
378 }
379
380 /// Creates an affine transformation matrix from the given 2D `translation`.
381 ///
382 /// The resulting matrix can be used to transform 2D points and vectors. See
383 /// [`Self::transform_point2()`] and [`Self::transform_vector2()`].
384 #[inline]
385 #[must_use]
386 pub fn from_translation(translation: Vec2) -> Self {
387 Self::from_cols(
388 Vec3A::X,
389 Vec3A::Y,
390 Vec3A::new(translation.x, translation.y, 1.0),
391 )
392 }
393
394 /// Creates an affine transformation matrix from the given 2D rotation `angle` (in
395 /// radians).
396 ///
397 /// The resulting matrix can be used to transform 2D points and vectors. See
398 /// [`Self::transform_point2()`] and [`Self::transform_vector2()`].
399 #[inline]
400 #[must_use]
401 pub fn from_angle(angle: f32) -> Self {
402 let (sin, cos) = math::sin_cos(angle);
403 Self::from_cols(
404 Vec3A::new(cos, sin, 0.0),
405 Vec3A::new(-sin, cos, 0.0),
406 Vec3A::Z,
407 )
408 }
409
410 /// Creates an affine transformation matrix from the given 2D `scale`, rotation `angle` (in
411 /// radians) and `translation`.
412 ///
413 /// The resulting matrix can be used to transform 2D points and vectors. See
414 /// [`Self::transform_point2()`] and [`Self::transform_vector2()`].
415 #[inline]
416 #[must_use]
417 pub fn from_scale_angle_translation(scale: Vec2, angle: f32, translation: Vec2) -> Self {
418 let (sin, cos) = math::sin_cos(angle);
419 Self::from_cols(
420 Vec3A::new(cos * scale.x, sin * scale.x, 0.0),
421 Vec3A::new(-sin * scale.y, cos * scale.y, 0.0),
422 Vec3A::new(translation.x, translation.y, 1.0),
423 )
424 }
425
426 /// Creates an affine transformation matrix from the given non-uniform 2D `scale`.
427 ///
428 /// The resulting matrix can be used to transform 2D points and vectors. See
429 /// [`Self::transform_point2()`] and [`Self::transform_vector2()`].
430 ///
431 /// # Panics
432 ///
433 /// Will panic if all elements of `scale` are zero when `glam_assert` is enabled.
434 #[inline]
435 #[must_use]
436 pub fn from_scale(scale: Vec2) -> Self {
437 // Do not panic as long as any component is non-zero
438 glam_assert!(scale.cmpne(Vec2::ZERO).any());
439
440 Self::from_cols(
441 Vec3A::new(scale.x, 0.0, 0.0),
442 Vec3A::new(0.0, scale.y, 0.0),
443 Vec3A::Z,
444 )
445 }
446
447 /// Creates an affine transformation matrix from the given 2x2 matrix.
448 ///
449 /// The resulting matrix can be used to transform 2D points and vectors. See
450 /// [`Self::transform_point2()`] and [`Self::transform_vector2()`].
451 #[inline]
452 pub fn from_mat2(m: Mat2) -> Self {
453 Self::from_cols((m.x_axis, 0.0).into(), (m.y_axis, 0.0).into(), Vec3A::Z)
454 }
455
456 /// Creates a 3x3 matrix from the first 9 values in `slice`.
457 ///
458 /// # Panics
459 ///
460 /// Panics if `slice` is less than 9 elements long.
461 #[inline]
462 #[must_use]
463 pub const fn from_cols_slice(slice: &[f32]) -> Self {
464 Self::new(
465 slice[0], slice[1], slice[2], slice[3], slice[4], slice[5], slice[6], slice[7],
466 slice[8],
467 )
468 }
469
470 /// Writes the columns of `self` to the first 9 elements in `slice`.
471 ///
472 /// # Panics
473 ///
474 /// Panics if `slice` is less than 9 elements long.
475 #[inline]
476 pub fn write_cols_to_slice(self, slice: &mut [f32]) {
477 slice[0] = self.x_axis.x;
478 slice[1] = self.x_axis.y;
479 slice[2] = self.x_axis.z;
480 slice[3] = self.y_axis.x;
481 slice[4] = self.y_axis.y;
482 slice[5] = self.y_axis.z;
483 slice[6] = self.z_axis.x;
484 slice[7] = self.z_axis.y;
485 slice[8] = self.z_axis.z;
486 }
487
488 /// Returns the matrix column for the given `index`.
489 ///
490 /// # Panics
491 ///
492 /// Panics if `index` is greater than 2.
493 #[inline]
494 #[must_use]
495 pub fn col(&self, index: usize) -> Vec3A {
496 match index {
497 0 => self.x_axis,
498 1 => self.y_axis,
499 2 => self.z_axis,
500 _ => panic!("index out of bounds"),
501 }
502 }
503
504 /// Returns a mutable reference to the matrix column for the given `index`.
505 ///
506 /// # Panics
507 ///
508 /// Panics if `index` is greater than 2.
509 #[inline]
510 pub fn col_mut(&mut self, index: usize) -> &mut Vec3A {
511 match index {
512 0 => &mut self.x_axis,
513 1 => &mut self.y_axis,
514 2 => &mut self.z_axis,
515 _ => panic!("index out of bounds"),
516 }
517 }
518
519 /// Returns the matrix row for the given `index`.
520 ///
521 /// # Panics
522 ///
523 /// Panics if `index` is greater than 2.
524 #[inline]
525 #[must_use]
526 pub fn row(&self, index: usize) -> Vec3A {
527 match index {
528 0 => Vec3A::new(self.x_axis.x, self.y_axis.x, self.z_axis.x),
529 1 => Vec3A::new(self.x_axis.y, self.y_axis.y, self.z_axis.y),
530 2 => Vec3A::new(self.x_axis.z, self.y_axis.z, self.z_axis.z),
531 _ => panic!("index out of bounds"),
532 }
533 }
534
535 /// Returns `true` if, and only if, all elements are finite.
536 /// If any element is either `NaN`, positive or negative infinity, this will return `false`.
537 #[inline]
538 #[must_use]
539 pub fn is_finite(&self) -> bool {
540 self.x_axis.is_finite() && self.y_axis.is_finite() && self.z_axis.is_finite()
541 }
542
543 /// Returns `true` if any elements are `NaN`.
544 #[inline]
545 #[must_use]
546 pub fn is_nan(&self) -> bool {
547 self.x_axis.is_nan() || self.y_axis.is_nan() || self.z_axis.is_nan()
548 }
549
550 /// Returns the transpose of `self`.
551 #[inline]
552 #[must_use]
553 pub fn transpose(&self) -> Self {
554 let tmp0 = simd_swizzle!(self.x_axis.0, self.y_axis.0, [0, 1, 4, 5]);
555 let tmp1 = simd_swizzle!(self.x_axis.0, self.y_axis.0, [2, 3, 6, 7]);
556
557 Self {
558 x_axis: Vec3A(simd_swizzle!(tmp0, self.z_axis.0, [0, 2, 4, 4])),
559 y_axis: Vec3A(simd_swizzle!(tmp0, self.z_axis.0, [1, 3, 5, 5])),
560 z_axis: Vec3A(simd_swizzle!(tmp1, self.z_axis.0, [0, 2, 6, 6])),
561 }
562 }
563
564 /// Returns the determinant of `self`.
565 #[inline]
566 #[must_use]
567 pub fn determinant(&self) -> f32 {
568 self.z_axis.dot(self.x_axis.cross(self.y_axis))
569 }
570
571 /// Returns the inverse of `self`.
572 ///
573 /// If the matrix is not invertible the returned matrix will be invalid.
574 ///
575 /// # Panics
576 ///
577 /// Will panic if the determinant of `self` is zero when `glam_assert` is enabled.
578 #[inline]
579 #[must_use]
580 pub fn inverse(&self) -> Self {
581 let tmp0 = self.y_axis.cross(self.z_axis);
582 let tmp1 = self.z_axis.cross(self.x_axis);
583 let tmp2 = self.x_axis.cross(self.y_axis);
584 let det = self.z_axis.dot(tmp2);
585 glam_assert!(det != 0.0);
586 let inv_det = Vec3A::splat(det.recip());
587 Self::from_cols(tmp0.mul(inv_det), tmp1.mul(inv_det), tmp2.mul(inv_det)).transpose()
588 }
589
590 /// Transforms the given 2D vector as a point.
591 ///
592 /// This is the equivalent of multiplying `rhs` as a 3D vector where `z` is `1`.
593 ///
594 /// This method assumes that `self` contains a valid affine transform.
595 ///
596 /// # Panics
597 ///
598 /// Will panic if the 2nd row of `self` is not `(0, 0, 1)` when `glam_assert` is enabled.
599 #[inline]
600 #[must_use]
601 pub fn transform_point2(&self, rhs: Vec2) -> Vec2 {
602 glam_assert!(self.row(2).abs_diff_eq(Vec3A::Z, 1e-6));
603 Mat2::from_cols(self.x_axis.xy(), self.y_axis.xy()) * rhs + self.z_axis.xy()
604 }
605
606 /// Rotates the given 2D vector.
607 ///
608 /// This is the equivalent of multiplying `rhs` as a 3D vector where `z` is `0`.
609 ///
610 /// This method assumes that `self` contains a valid affine transform.
611 ///
612 /// # Panics
613 ///
614 /// Will panic if the 2nd row of `self` is not `(0, 0, 1)` when `glam_assert` is enabled.
615 #[inline]
616 #[must_use]
617 pub fn transform_vector2(&self, rhs: Vec2) -> Vec2 {
618 glam_assert!(self.row(2).abs_diff_eq(Vec3A::Z, 1e-6));
619 Mat2::from_cols(self.x_axis.xy(), self.y_axis.xy()) * rhs
620 }
621
622 /// Creates a left-handed view matrix using a facing direction and an up direction.
623 ///
624 /// For a view coordinate system with `+X=right`, `+Y=up` and `+Z=forward`.
625 ///
626 /// # Panics
627 ///
628 /// Will panic if `dir` or `up` are not normalized when `glam_assert` is enabled.
629 #[inline]
630 #[must_use]
631 pub fn look_to_lh(dir: Vec3, up: Vec3) -> Self {
632 Self::look_to_rh(-dir, up)
633 }
634
635 /// Creates a right-handed view matrix using a facing direction and an up direction.
636 ///
637 /// For a view coordinate system with `+X=right`, `+Y=up` and `+Z=back`.
638 ///
639 /// # Panics
640 ///
641 /// Will panic if `dir` or `up` are not normalized when `glam_assert` is enabled.
642 #[inline]
643 #[must_use]
644 pub fn look_to_rh(dir: Vec3, up: Vec3) -> Self {
645 glam_assert!(dir.is_normalized());
646 glam_assert!(up.is_normalized());
647 let f = dir;
648 let s = f.cross(up).normalize();
649 let u = s.cross(f);
650
651 Self::from_cols(
652 Vec3A::new(s.x, u.x, -f.x),
653 Vec3A::new(s.y, u.y, -f.y),
654 Vec3A::new(s.z, u.z, -f.z),
655 )
656 }
657
658 /// Creates a left-handed view matrix using a camera position, a focal point and an up
659 /// direction.
660 ///
661 /// For a view coordinate system with `+X=right`, `+Y=up` and `+Z=forward`.
662 ///
663 /// # Panics
664 ///
665 /// Will panic if `up` is not normalized when `glam_assert` is enabled.
666 #[inline]
667 #[must_use]
668 pub fn look_at_lh(eye: Vec3, center: Vec3, up: Vec3) -> Self {
669 Self::look_to_lh(center.sub(eye).normalize(), up)
670 }
671
672 /// Creates a right-handed view matrix using a camera position, a focal point and an up
673 /// direction.
674 ///
675 /// For a view coordinate system with `+X=right`, `+Y=up` and `+Z=back`.
676 ///
677 /// # Panics
678 ///
679 /// Will panic if `up` is not normalized when `glam_assert` is enabled.
680 #[inline]
681 pub fn look_at_rh(eye: Vec3, center: Vec3, up: Vec3) -> Self {
682 Self::look_to_rh(center.sub(eye).normalize(), up)
683 }
684
685 /// Transforms a 3D vector.
686 #[inline]
687 #[must_use]
688 pub fn mul_vec3(&self, rhs: Vec3) -> Vec3 {
689 self.mul_vec3a(rhs.into()).into()
690 }
691
692 /// Transforms a [`Vec3A`].
693 #[inline]
694 #[must_use]
695 pub fn mul_vec3a(&self, rhs: Vec3A) -> Vec3A {
696 let mut res = self.x_axis.mul(rhs.xxx());
697 res = res.add(self.y_axis.mul(rhs.yyy()));
698 res = res.add(self.z_axis.mul(rhs.zzz()));
699 res
700 }
701
702 /// Multiplies two 3x3 matrices.
703 #[inline]
704 #[must_use]
705 pub fn mul_mat3(&self, rhs: &Self) -> Self {
706 self.mul(rhs)
707 }
708
709 /// Adds two 3x3 matrices.
710 #[inline]
711 #[must_use]
712 pub fn add_mat3(&self, rhs: &Self) -> Self {
713 self.add(rhs)
714 }
715
716 /// Subtracts two 3x3 matrices.
717 #[inline]
718 #[must_use]
719 pub fn sub_mat3(&self, rhs: &Self) -> Self {
720 self.sub(rhs)
721 }
722
723 /// Multiplies a 3x3 matrix by a scalar.
724 #[inline]
725 #[must_use]
726 pub fn mul_scalar(&self, rhs: f32) -> Self {
727 Self::from_cols(
728 self.x_axis.mul(rhs),
729 self.y_axis.mul(rhs),
730 self.z_axis.mul(rhs),
731 )
732 }
733
734 /// Divides a 3x3 matrix by a scalar.
735 #[inline]
736 #[must_use]
737 pub fn div_scalar(&self, rhs: f32) -> Self {
738 let rhs = Vec3A::splat(rhs);
739 Self::from_cols(
740 self.x_axis.div(rhs),
741 self.y_axis.div(rhs),
742 self.z_axis.div(rhs),
743 )
744 }
745
746 /// Returns true if the absolute difference of all elements between `self` and `rhs`
747 /// is less than or equal to `max_abs_diff`.
748 ///
749 /// This can be used to compare if two matrices contain similar elements. It works best
750 /// when comparing with a known value. The `max_abs_diff` that should be used used
751 /// depends on the values being compared against.
752 ///
753 /// For more see
754 /// [comparing floating point numbers](https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/).
755 #[inline]
756 #[must_use]
757 pub fn abs_diff_eq(&self, rhs: Self, max_abs_diff: f32) -> bool {
758 self.x_axis.abs_diff_eq(rhs.x_axis, max_abs_diff)
759 && self.y_axis.abs_diff_eq(rhs.y_axis, max_abs_diff)
760 && self.z_axis.abs_diff_eq(rhs.z_axis, max_abs_diff)
761 }
762
763 /// Takes the absolute value of each element in `self`
764 #[inline]
765 #[must_use]
766 pub fn abs(&self) -> Self {
767 Self::from_cols(self.x_axis.abs(), self.y_axis.abs(), self.z_axis.abs())
768 }
769
770 #[inline]
771 pub fn as_dmat3(&self) -> DMat3 {
772 DMat3::from_cols(
773 self.x_axis.as_dvec3(),
774 self.y_axis.as_dvec3(),
775 self.z_axis.as_dvec3(),
776 )
777 }
778 }
779
780 impl Default for Mat3A {
781 #[inline]
782 fn default() -> Self {
783 Self::IDENTITY
784 }
785 }
786
787 impl Add for Mat3A {
788 type Output = Self;
789 #[inline]
790 fn add(self, rhs: Self) -> Self {
791 Self::from_cols(
792 self.x_axis.add(rhs.x_axis),
793 self.y_axis.add(rhs.y_axis),
794 self.z_axis.add(rhs.z_axis),
795 )
796 }
797 }
798
799 impl Add<&Self> for Mat3A {
800 type Output = Self;
801 #[inline]
802 fn add(self, rhs: &Self) -> Self {
803 self.add(*rhs)
804 }
805 }
806
807 impl Add<&Mat3A> for &Mat3A {
808 type Output = Mat3A;
809 #[inline]
810 fn add(self, rhs: &Mat3A) -> Mat3A {
811 (*self).add(*rhs)
812 }
813 }
814
815 impl Add<Mat3A> for &Mat3A {
816 type Output = Mat3A;
817 #[inline]
818 fn add(self, rhs: Mat3A) -> Mat3A {
819 (*self).add(rhs)
820 }
821 }
822
823 impl AddAssign for Mat3A {
824 #[inline]
825 fn add_assign(&mut self, rhs: Self) {
826 *self = self.add(rhs);
827 }
828 }
829
830 impl AddAssign<&Self> for Mat3A {
831 #[inline]
832 fn add_assign(&mut self, rhs: &Self) {
833 self.add_assign(*rhs);
834 }
835 }
836
837 impl Sub for Mat3A {
838 type Output = Self;
839 #[inline]
840 fn sub(self, rhs: Self) -> Self {
841 Self::from_cols(
842 self.x_axis.sub(rhs.x_axis),
843 self.y_axis.sub(rhs.y_axis),
844 self.z_axis.sub(rhs.z_axis),
845 )
846 }
847 }
848
849 impl Sub<&Self> for Mat3A {
850 type Output = Self;
851 #[inline]
852 fn sub(self, rhs: &Self) -> Self {
853 self.sub(*rhs)
854 }
855 }
856
857 impl Sub<&Mat3A> for &Mat3A {
858 type Output = Mat3A;
859 #[inline]
860 fn sub(self, rhs: &Mat3A) -> Mat3A {
861 (*self).sub(*rhs)
862 }
863 }
864
865 impl Sub<Mat3A> for &Mat3A {
866 type Output = Mat3A;
867 #[inline]
868 fn sub(self, rhs: Mat3A) -> Mat3A {
869 (*self).sub(rhs)
870 }
871 }
872
873 impl SubAssign for Mat3A {
874 #[inline]
875 fn sub_assign(&mut self, rhs: Self) {
876 *self = self.sub(rhs);
877 }
878 }
879
880 impl SubAssign<&Self> for Mat3A {
881 #[inline]
882 fn sub_assign(&mut self, rhs: &Self) {
883 self.sub_assign(*rhs);
884 }
885 }
886
887 impl Neg for Mat3A {
888 type Output = Self;
889 #[inline]
890 fn neg(self) -> Self::Output {
891 Self::from_cols(self.x_axis.neg(), self.y_axis.neg(), self.z_axis.neg())
892 }
893 }
894
895 impl Neg for &Mat3A {
896 type Output = Mat3A;
897 #[inline]
898 fn neg(self) -> Mat3A {
899 (*self).neg()
900 }
901 }
902
903 impl Mul for Mat3A {
904 type Output = Self;
905 #[inline]
906 fn mul(self, rhs: Self) -> Self {
907 Self::from_cols(
908 self.mul(rhs.x_axis),
909 self.mul(rhs.y_axis),
910 self.mul(rhs.z_axis),
911 )
912 }
913 }
914
915 impl Mul<&Self> for Mat3A {
916 type Output = Self;
917 #[inline]
918 fn mul(self, rhs: &Self) -> Self {
919 self.mul(*rhs)
920 }
921 }
922
923 impl Mul<&Mat3A> for &Mat3A {
924 type Output = Mat3A;
925 #[inline]
926 fn mul(self, rhs: &Mat3A) -> Mat3A {
927 (*self).mul(*rhs)
928 }
929 }
930
931 impl Mul<Mat3A> for &Mat3A {
932 type Output = Mat3A;
933 #[inline]
934 fn mul(self, rhs: Mat3A) -> Mat3A {
935 (*self).mul(rhs)
936 }
937 }
938
939 impl MulAssign for Mat3A {
940 #[inline]
941 fn mul_assign(&mut self, rhs: Self) {
942 *self = self.mul(rhs);
943 }
944 }
945
946 impl MulAssign<&Self> for Mat3A {
947 #[inline]
948 fn mul_assign(&mut self, rhs: &Self) {
949 self.mul_assign(*rhs);
950 }
951 }
952
953 impl Mul<Vec3A> for Mat3A {
954 type Output = Vec3A;
955 #[inline]
956 fn mul(self, rhs: Vec3A) -> Self::Output {
957 self.mul_vec3a(rhs)
958 }
959 }
960
961 impl Mul<&Vec3A> for Mat3A {
962 type Output = Vec3A;
963 #[inline]
964 fn mul(self, rhs: &Vec3A) -> Vec3A {
965 self.mul(*rhs)
966 }
967 }
968
969 impl Mul<&Vec3A> for &Mat3A {
970 type Output = Vec3A;
971 #[inline]
972 fn mul(self, rhs: &Vec3A) -> Vec3A {
973 (*self).mul(*rhs)
974 }
975 }
976
977 impl Mul<Vec3A> for &Mat3A {
978 type Output = Vec3A;
979 #[inline]
980 fn mul(self, rhs: Vec3A) -> Vec3A {
981 (*self).mul(rhs)
982 }
983 }
984
985 impl Mul<Mat3A> for f32 {
986 type Output = Mat3A;
987 #[inline]
988 fn mul(self, rhs: Mat3A) -> Self::Output {
989 rhs.mul_scalar(self)
990 }
991 }
992
993 impl Mul<&Mat3A> for f32 {
994 type Output = Mat3A;
995 #[inline]
996 fn mul(self, rhs: &Mat3A) -> Mat3A {
997 self.mul(*rhs)
998 }
999 }
1000
1001 impl Mul<&Mat3A> for &f32 {
1002 type Output = Mat3A;
1003 #[inline]
1004 fn mul(self, rhs: &Mat3A) -> Mat3A {
1005 (*self).mul(*rhs)
1006 }
1007 }
1008
1009 impl Mul<Mat3A> for &f32 {
1010 type Output = Mat3A;
1011 #[inline]
1012 fn mul(self, rhs: Mat3A) -> Mat3A {
1013 (*self).mul(rhs)
1014 }
1015 }
1016
1017 impl Mul<f32> for Mat3A {
1018 type Output = Self;
1019 #[inline]
1020 fn mul(self, rhs: f32) -> Self {
1021 self.mul_scalar(rhs)
1022 }
1023 }
1024
1025 impl Mul<&f32> for Mat3A {
1026 type Output = Self;
1027 #[inline]
1028 fn mul(self, rhs: &f32) -> Self {
1029 self.mul(*rhs)
1030 }
1031 }
1032
1033 impl Mul<&f32> for &Mat3A {
1034 type Output = Mat3A;
1035 #[inline]
1036 fn mul(self, rhs: &f32) -> Mat3A {
1037 (*self).mul(*rhs)
1038 }
1039 }
1040
1041 impl Mul<f32> for &Mat3A {
1042 type Output = Mat3A;
1043 #[inline]
1044 fn mul(self, rhs: f32) -> Mat3A {
1045 (*self).mul(rhs)
1046 }
1047 }
1048
1049 impl MulAssign<f32> for Mat3A {
1050 #[inline]
1051 fn mul_assign(&mut self, rhs: f32) {
1052 *self = self.mul(rhs);
1053 }
1054 }
1055
1056 impl MulAssign<&f32> for Mat3A {
1057 #[inline]
1058 fn mul_assign(&mut self, rhs: &f32) {
1059 self.mul_assign(*rhs);
1060 }
1061 }
1062
1063 impl Div<Mat3A> for f32 {
1064 type Output = Mat3A;
1065 #[inline]
1066 fn div(self, rhs: Mat3A) -> Self::Output {
1067 rhs.div_scalar(self)
1068 }
1069 }
1070
1071 impl Div<&Mat3A> for f32 {
1072 type Output = Mat3A;
1073 #[inline]
1074 fn div(self, rhs: &Mat3A) -> Mat3A {
1075 self.div(*rhs)
1076 }
1077 }
1078
1079 impl Div<&Mat3A> for &f32 {
1080 type Output = Mat3A;
1081 #[inline]
1082 fn div(self, rhs: &Mat3A) -> Mat3A {
1083 (*self).div(*rhs)
1084 }
1085 }
1086
1087 impl Div<Mat3A> for &f32 {
1088 type Output = Mat3A;
1089 #[inline]
1090 fn div(self, rhs: Mat3A) -> Mat3A {
1091 (*self).div(rhs)
1092 }
1093 }
1094
1095 impl Div<f32> for Mat3A {
1096 type Output = Self;
1097 #[inline]
1098 fn div(self, rhs: f32) -> Self {
1099 self.div_scalar(rhs)
1100 }
1101 }
1102
1103 impl Div<&f32> for Mat3A {
1104 type Output = Self;
1105 #[inline]
1106 fn div(self, rhs: &f32) -> Self {
1107 self.div(*rhs)
1108 }
1109 }
1110
1111 impl Div<&f32> for &Mat3A {
1112 type Output = Mat3A;
1113 #[inline]
1114 fn div(self, rhs: &f32) -> Mat3A {
1115 (*self).div(*rhs)
1116 }
1117 }
1118
1119 impl Div<f32> for &Mat3A {
1120 type Output = Mat3A;
1121 #[inline]
1122 fn div(self, rhs: f32) -> Mat3A {
1123 (*self).div(rhs)
1124 }
1125 }
1126
1127 impl DivAssign<f32> for Mat3A {
1128 #[inline]
1129 fn div_assign(&mut self, rhs: f32) {
1130 *self = self.div(rhs);
1131 }
1132 }
1133
1134 impl DivAssign<&f32> for Mat3A {
1135 #[inline]
1136 fn div_assign(&mut self, rhs: &f32) {
1137 self.div_assign(*rhs);
1138 }
1139 }
1140
1141 impl Mul<Vec3> for Mat3A {
1142 type Output = Vec3;
1143 #[inline]
1144 fn mul(self, rhs: Vec3) -> Vec3 {
1145 self.mul_vec3a(rhs.into()).into()
1146 }
1147 }
1148
1149 impl Mul<&Vec3> for Mat3A {
1150 type Output = Vec3;
1151 #[inline]
1152 fn mul(self, rhs: &Vec3) -> Vec3 {
1153 self.mul(*rhs)
1154 }
1155 }
1156
1157 impl Mul<&Vec3> for &Mat3A {
1158 type Output = Vec3;
1159 #[inline]
1160 fn mul(self, rhs: &Vec3) -> Vec3 {
1161 (*self).mul(*rhs)
1162 }
1163 }
1164
1165 impl Mul<Vec3> for &Mat3A {
1166 type Output = Vec3;
1167 #[inline]
1168 fn mul(self, rhs: Vec3) -> Vec3 {
1169 (*self).mul(rhs)
1170 }
1171 }
1172
1173 impl From<Mat3> for Mat3A {
1174 #[inline]
1175 fn from(m: Mat3) -> Self {
1176 Self {
1177 x_axis: m.x_axis.into(),
1178 y_axis: m.y_axis.into(),
1179 z_axis: m.z_axis.into(),
1180 }
1181 }
1182 }
1183
1184 impl Sum<Self> for Mat3A {
1185 fn sum<I>(iter: I) -> Self
1186 where
1187 I: Iterator<Item = Self>,
1188 {
1189 iter.fold(Self::ZERO, Self::add)
1190 }
1191 }
1192
1193 impl<'a> Sum<&'a Self> for Mat3A {
1194 fn sum<I>(iter: I) -> Self
1195 where
1196 I: Iterator<Item = &'a Self>,
1197 {
1198 iter.fold(Self::ZERO, |a, &b| Self::add(a, b))
1199 }
1200 }
1201
1202 impl Product for Mat3A {
1203 fn product<I>(iter: I) -> Self
1204 where
1205 I: Iterator<Item = Self>,
1206 {
1207 iter.fold(Self::IDENTITY, Self::mul)
1208 }
1209 }
1210
1211 impl<'a> Product<&'a Self> for Mat3A {
1212 fn product<I>(iter: I) -> Self
1213 where
1214 I: Iterator<Item = &'a Self>,
1215 {
1216 iter.fold(Self::IDENTITY, |a, &b| Self::mul(a, b))
1217 }
1218 }
1219
1220 impl PartialEq for Mat3A {
1221 #[inline]
1222 fn eq(&self, rhs: &Self) -> bool {
1223 self.x_axis.eq(&rhs.x_axis) && self.y_axis.eq(&rhs.y_axis) && self.z_axis.eq(&rhs.z_axis)
1224 }
1225 }
1226
1227 impl fmt::Debug for Mat3A {
1228 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
1229 fmt.debug_struct(stringify!(Mat3A))
1230 .field("x_axis", &self.x_axis)
1231 .field("y_axis", &self.y_axis)
1232 .field("z_axis", &self.z_axis)
1233 .finish()
1234 }
1235 }
1236
1237 impl fmt::Display for Mat3A {
1238 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1239 if let Some(p) = f.precision() {
1240 write!(
1241 f,
1242 "[{:.*}, {:.*}, {:.*}]",
1243 p, self.x_axis, p, self.y_axis, p, self.z_axis
1244 )
1245 } else {
1246 write!(f, "[{}, {}, {}]", self.x_axis, self.y_axis, self.z_axis)
1247 }
1248 }
1249 }