Debian Codegraph @

debian/missing-sources/glam-0.29.3/src/f32/wasm32/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::arch::wasm32::*;
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 #[repr(C)]
48 pub struct Mat3A {
49 pub x_axis: Vec3A,
50 pub y_axis: Vec3A,
51 pub z_axis: Vec3A,
52 }
53
54 impl Mat3A {
55 /// A 3x3 matrix with all elements set to `0.0`.
56 pub const ZERO: Self = Self::from_cols(Vec3A::ZERO, Vec3A::ZERO, Vec3A::ZERO);
57
58 /// A 3x3 identity matrix, where all diagonal elements are `1`, and all off-diagonal elements are `0`.
59 pub const IDENTITY: Self = Self::from_cols(Vec3A::X, Vec3A::Y, Vec3A::Z);
60
61 /// All NAN:s.
62 pub const NAN: Self = Self::from_cols(Vec3A::NAN, Vec3A::NAN, Vec3A::NAN);
63
64 #[allow(clippy::too_many_arguments)]
65 #[inline(always)]
66 #[must_use]
67 const fn new(
68 m00: f32,
69 m01: f32,
70 m02: f32,
71 m10: f32,
72 m11: f32,
73 m12: f32,
74 m20: f32,
75 m21: f32,
76 m22: f32,
77 ) -> Self {
78 Self {
79 x_axis: Vec3A::new(m00, m01, m02),
80 y_axis: Vec3A::new(m10, m11, m12),
81 z_axis: Vec3A::new(m20, m21, m22),
82 }
83 }
84
85 /// Creates a 3x3 matrix from three column vectors.
86 #[inline(always)]
87 #[must_use]
88 pub const fn from_cols(x_axis: Vec3A, y_axis: Vec3A, z_axis: Vec3A) -> Self {
89 Self {
90 x_axis,
91 y_axis,
92 z_axis,
93 }
94 }
95
96 /// Creates a 3x3 matrix from a `[f32; 9]` array stored in column major order.
97 /// If your data is stored in row major you will need to `transpose` the returned
98 /// matrix.
99 #[inline]
100 #[must_use]
101 pub const fn from_cols_array(m: &[f32; 9]) -> Self {
102 Self::new(m[0], m[1], m[2], m[3], m[4], m[5], m[6], m[7], m[8])
103 }
104
105 /// Creates a `[f32; 9]` array storing data in column major order.
106 /// If you require data in row major order `transpose` the matrix first.
107 #[inline]
108 #[must_use]
109 pub const fn to_cols_array(&self) -> [f32; 9] {
110 let [x_axis_x, x_axis_y, x_axis_z] = self.x_axis.to_array();
111 let [y_axis_x, y_axis_y, y_axis_z] = self.y_axis.to_array();
112 let [z_axis_x, z_axis_y, z_axis_z] = self.z_axis.to_array();
113
114 [
115 x_axis_x, x_axis_y, x_axis_z, y_axis_x, y_axis_y, y_axis_z, z_axis_x, z_axis_y,
116 z_axis_z,
117 ]
118 }
119
120 /// Creates a 3x3 matrix from a `[[f32; 3]; 3]` 3D array stored in column major order.
121 /// If your data is in row major order you will need to `transpose` the returned
122 /// matrix.
123 #[inline]
124 #[must_use]
125 pub const fn from_cols_array_2d(m: &[[f32; 3]; 3]) -> Self {
126 Self::from_cols(
127 Vec3A::from_array(m[0]),
128 Vec3A::from_array(m[1]),
129 Vec3A::from_array(m[2]),
130 )
131 }
132
133 /// Creates a `[[f32; 3]; 3]` 3D array storing data in column major order.
134 /// If you require data in row major order `transpose` the matrix first.
135 #[inline]
136 #[must_use]
137 pub const fn to_cols_array_2d(&self) -> [[f32; 3]; 3] {
138 [
139 self.x_axis.to_array(),
140 self.y_axis.to_array(),
141 self.z_axis.to_array(),
142 ]
143 }
144
145 /// Creates a 3x3 matrix with its diagonal set to `diagonal` and all other entries set to 0.
146 #[doc(alias = "scale")]
147 #[inline]
148 #[must_use]
149 pub const fn from_diagonal(diagonal: Vec3) -> Self {
150 Self::new(
151 diagonal.x, 0.0, 0.0, 0.0, diagonal.y, 0.0, 0.0, 0.0, diagonal.z,
152 )
153 }
154
155 /// Creates a 3x3 matrix from a 4x4 matrix, discarding the 4th row and column.
156 #[inline]
157 #[must_use]
158 pub fn from_mat4(m: Mat4) -> Self {
159 Self::from_cols(
160 Vec3A::from_vec4(m.x_axis),
161 Vec3A::from_vec4(m.y_axis),
162 Vec3A::from_vec4(m.z_axis),
163 )
164 }
165
166 /// Creates a 3x3 matrix from the minor of the given 4x4 matrix, discarding the `i`th column
167 /// and `j`th row.
168 ///
169 /// # Panics
170 ///
171 /// Panics if `i` or `j` is greater than 3.
172 #[inline]
173 #[must_use]
174 pub fn from_mat4_minor(m: Mat4, i: usize, j: usize) -> Self {
175 match (i, j) {
176 (0, 0) => Self::from_cols(
177 Vec3A::from_vec4(m.y_axis.yzww()),
178 Vec3A::from_vec4(m.z_axis.yzww()),
179 Vec3A::from_vec4(m.w_axis.yzww()),
180 ),
181 (0, 1) => Self::from_cols(
182 Vec3A::from_vec4(m.y_axis.xzww()),
183 Vec3A::from_vec4(m.z_axis.xzww()),
184 Vec3A::from_vec4(m.w_axis.xzww()),
185 ),
186 (0, 2) => Self::from_cols(
187 Vec3A::from_vec4(m.y_axis.xyww()),
188 Vec3A::from_vec4(m.z_axis.xyww()),
189 Vec3A::from_vec4(m.w_axis.xyww()),
190 ),
191 (0, 3) => Self::from_cols(
192 Vec3A::from_vec4(m.y_axis.xyzw()),
193 Vec3A::from_vec4(m.z_axis.xyzw()),
194 Vec3A::from_vec4(m.w_axis.xyzw()),
195 ),
196 (1, 0) => Self::from_cols(
197 Vec3A::from_vec4(m.x_axis.yzww()),
198 Vec3A::from_vec4(m.z_axis.yzww()),
199 Vec3A::from_vec4(m.w_axis.yzww()),
200 ),
201 (1, 1) => Self::from_cols(
202 Vec3A::from_vec4(m.x_axis.xzww()),
203 Vec3A::from_vec4(m.z_axis.xzww()),
204 Vec3A::from_vec4(m.w_axis.xzww()),
205 ),
206 (1, 2) => Self::from_cols(
207 Vec3A::from_vec4(m.x_axis.xyww()),
208 Vec3A::from_vec4(m.z_axis.xyww()),
209 Vec3A::from_vec4(m.w_axis.xyww()),
210 ),
211 (1, 3) => Self::from_cols(
212 Vec3A::from_vec4(m.x_axis.xyzw()),
213 Vec3A::from_vec4(m.z_axis.xyzw()),
214 Vec3A::from_vec4(m.w_axis.xyzw()),
215 ),
216 (2, 0) => Self::from_cols(
217 Vec3A::from_vec4(m.x_axis.yzww()),
218 Vec3A::from_vec4(m.y_axis.yzww()),
219 Vec3A::from_vec4(m.w_axis.yzww()),
220 ),
221 (2, 1) => Self::from_cols(
222 Vec3A::from_vec4(m.x_axis.xzww()),
223 Vec3A::from_vec4(m.y_axis.xzww()),
224 Vec3A::from_vec4(m.w_axis.xzww()),
225 ),
226 (2, 2) => Self::from_cols(
227 Vec3A::from_vec4(m.x_axis.xyww()),
228 Vec3A::from_vec4(m.y_axis.xyww()),
229 Vec3A::from_vec4(m.w_axis.xyww()),
230 ),
231 (2, 3) => Self::from_cols(
232 Vec3A::from_vec4(m.x_axis.xyzw()),
233 Vec3A::from_vec4(m.y_axis.xyzw()),
234 Vec3A::from_vec4(m.w_axis.xyzw()),
235 ),
236 (3, 0) => Self::from_cols(
237 Vec3A::from_vec4(m.x_axis.yzww()),
238 Vec3A::from_vec4(m.y_axis.yzww()),
239 Vec3A::from_vec4(m.z_axis.yzww()),
240 ),
241 (3, 1) => Self::from_cols(
242 Vec3A::from_vec4(m.x_axis.xzww()),
243 Vec3A::from_vec4(m.y_axis.xzww()),
244 Vec3A::from_vec4(m.z_axis.xzww()),
245 ),
246 (3, 2) => Self::from_cols(
247 Vec3A::from_vec4(m.x_axis.xyww()),
248 Vec3A::from_vec4(m.y_axis.xyww()),
249 Vec3A::from_vec4(m.z_axis.xyww()),
250 ),
251 (3, 3) => Self::from_cols(
252 Vec3A::from_vec4(m.x_axis.xyzw()),
253 Vec3A::from_vec4(m.y_axis.xyzw()),
254 Vec3A::from_vec4(m.z_axis.xyzw()),
255 ),
256 _ => panic!("index out of bounds"),
257 }
258 }
259
260 /// Creates a 3D rotation matrix from the given quaternion.
261 ///
262 /// # Panics
263 ///
264 /// Will panic if `rotation` is not normalized when `glam_assert` is enabled.
265 #[inline]
266 #[must_use]
267 pub fn from_quat(rotation: Quat) -> Self {
268 glam_assert!(rotation.is_normalized());
269
270 let x2 = rotation.x + rotation.x;
271 let y2 = rotation.y + rotation.y;
272 let z2 = rotation.z + rotation.z;
273 let xx = rotation.x * x2;
274 let xy = rotation.x * y2;
275 let xz = rotation.x * z2;
276 let yy = rotation.y * y2;
277 let yz = rotation.y * z2;
278 let zz = rotation.z * z2;
279 let wx = rotation.w * x2;
280 let wy = rotation.w * y2;
281 let wz = rotation.w * z2;
282
283 Self::from_cols(
284 Vec3A::new(1.0 - (yy + zz), xy + wz, xz - wy),
285 Vec3A::new(xy - wz, 1.0 - (xx + zz), yz + wx),
286 Vec3A::new(xz + wy, yz - wx, 1.0 - (xx + yy)),
287 )
288 }
289
290 /// Creates a 3D rotation matrix from a normalized rotation `axis` and `angle` (in
291 /// radians).
292 ///
293 /// # Panics
294 ///
295 /// Will panic if `axis` is not normalized when `glam_assert` is enabled.
296 #[inline]
297 #[must_use]
298 pub fn from_axis_angle(axis: Vec3, angle: f32) -> Self {
299 glam_assert!(axis.is_normalized());
300
301 let (sin, cos) = math::sin_cos(angle);
302 let (xsin, ysin, zsin) = axis.mul(sin).into();
303 let (x, y, z) = axis.into();
304 let (x2, y2, z2) = axis.mul(axis).into();
305 let omc = 1.0 - cos;
306 let xyomc = x * y * omc;
307 let xzomc = x * z * omc;
308 let yzomc = y * z * omc;
309 Self::from_cols(
310 Vec3A::new(x2 * omc + cos, xyomc + zsin, xzomc - ysin),
311 Vec3A::new(xyomc - zsin, y2 * omc + cos, yzomc + xsin),
312 Vec3A::new(xzomc + ysin, yzomc - xsin, z2 * omc + cos),
313 )
314 }
315
316 /// Creates a 3D rotation matrix from the given euler rotation sequence and the angles (in
317 /// radians).
318 #[inline]
319 #[must_use]
320 pub fn from_euler(order: EulerRot, a: f32, b: f32, c: f32) -> Self {
321 Self::from_euler_angles(order, a, b, c)
322 }
323
324 /// Extract Euler angles with the given Euler rotation order.
325 ///
326 /// Note if the input matrix contains scales, shears, or other non-rotation transformations then
327 /// the resulting Euler angles will be ill-defined.
328 ///
329 /// # Panics
330 ///
331 /// Will panic if any input matrix column is not normalized when `glam_assert` is enabled.
332 #[inline]
333 #[must_use]
334 pub fn to_euler(&self, order: EulerRot) -> (f32, f32, f32) {
335 glam_assert!(
336 self.x_axis.is_normalized()
337 && self.y_axis.is_normalized()
338 && self.z_axis.is_normalized()
339 );
340 self.to_euler_angles(order)
341 }
342
343 /// Creates a 3D rotation matrix from `angle` (in radians) around the x axis.
344 #[inline]
345 #[must_use]
346 pub fn from_rotation_x(angle: f32) -> Self {
347 let (sina, cosa) = math::sin_cos(angle);
348 Self::from_cols(
349 Vec3A::X,
350 Vec3A::new(0.0, cosa, sina),
351 Vec3A::new(0.0, -sina, cosa),
352 )
353 }
354
355 /// Creates a 3D rotation matrix from `angle` (in radians) around the y axis.
356 #[inline]
357 #[must_use]
358 pub fn from_rotation_y(angle: f32) -> Self {
359 let (sina, cosa) = math::sin_cos(angle);
360 Self::from_cols(
361 Vec3A::new(cosa, 0.0, -sina),
362 Vec3A::Y,
363 Vec3A::new(sina, 0.0, cosa),
364 )
365 }
366
367 /// Creates a 3D rotation matrix from `angle` (in radians) around the z axis.
368 #[inline]
369 #[must_use]
370 pub fn from_rotation_z(angle: f32) -> Self {
371 let (sina, cosa) = math::sin_cos(angle);
372 Self::from_cols(
373 Vec3A::new(cosa, sina, 0.0),
374 Vec3A::new(-sina, cosa, 0.0),
375 Vec3A::Z,
376 )
377 }
378
379 /// Creates an affine transformation matrix from the given 2D `translation`.
380 ///
381 /// The resulting matrix can be used to transform 2D points and vectors. See
382 /// [`Self::transform_point2()`] and [`Self::transform_vector2()`].
383 #[inline]
384 #[must_use]
385 pub fn from_translation(translation: Vec2) -> Self {
386 Self::from_cols(
387 Vec3A::X,
388 Vec3A::Y,
389 Vec3A::new(translation.x, translation.y, 1.0),
390 )
391 }
392
393 /// Creates an affine transformation matrix from the given 2D rotation `angle` (in
394 /// radians).
395 ///
396 /// The resulting matrix can be used to transform 2D points and vectors. See
397 /// [`Self::transform_point2()`] and [`Self::transform_vector2()`].
398 #[inline]
399 #[must_use]
400 pub fn from_angle(angle: f32) -> Self {
401 let (sin, cos) = math::sin_cos(angle);
402 Self::from_cols(
403 Vec3A::new(cos, sin, 0.0),
404 Vec3A::new(-sin, cos, 0.0),
405 Vec3A::Z,
406 )
407 }
408
409 /// Creates an affine transformation matrix from the given 2D `scale`, rotation `angle` (in
410 /// radians) and `translation`.
411 ///
412 /// The resulting matrix can be used to transform 2D points and vectors. See
413 /// [`Self::transform_point2()`] and [`Self::transform_vector2()`].
414 #[inline]
415 #[must_use]
416 pub fn from_scale_angle_translation(scale: Vec2, angle: f32, translation: Vec2) -> Self {
417 let (sin, cos) = math::sin_cos(angle);
418 Self::from_cols(
419 Vec3A::new(cos * scale.x, sin * scale.x, 0.0),
420 Vec3A::new(-sin * scale.y, cos * scale.y, 0.0),
421 Vec3A::new(translation.x, translation.y, 1.0),
422 )
423 }
424
425 /// Creates an affine transformation matrix from the given non-uniform 2D `scale`.
426 ///
427 /// The resulting matrix can be used to transform 2D points and vectors. See
428 /// [`Self::transform_point2()`] and [`Self::transform_vector2()`].
429 ///
430 /// # Panics
431 ///
432 /// Will panic if all elements of `scale` are zero when `glam_assert` is enabled.
433 #[inline]
434 #[must_use]
435 pub fn from_scale(scale: Vec2) -> Self {
436 // Do not panic as long as any component is non-zero
437 glam_assert!(scale.cmpne(Vec2::ZERO).any());
438
439 Self::from_cols(
440 Vec3A::new(scale.x, 0.0, 0.0),
441 Vec3A::new(0.0, scale.y, 0.0),
442 Vec3A::Z,
443 )
444 }
445
446 /// Creates an affine transformation matrix from the given 2x2 matrix.
447 ///
448 /// The resulting matrix can be used to transform 2D points and vectors. See
449 /// [`Self::transform_point2()`] and [`Self::transform_vector2()`].
450 #[inline]
451 pub fn from_mat2(m: Mat2) -> Self {
452 Self::from_cols((m.x_axis, 0.0).into(), (m.y_axis, 0.0).into(), Vec3A::Z)
453 }
454
455 /// Creates a 3x3 matrix from the first 9 values in `slice`.
456 ///
457 /// # Panics
458 ///
459 /// Panics if `slice` is less than 9 elements long.
460 #[inline]
461 #[must_use]
462 pub const fn from_cols_slice(slice: &[f32]) -> Self {
463 Self::new(
464 slice[0], slice[1], slice[2], slice[3], slice[4], slice[5], slice[6], slice[7],
465 slice[8],
466 )
467 }
468
469 /// Writes the columns of `self` to the first 9 elements in `slice`.
470 ///
471 /// # Panics
472 ///
473 /// Panics if `slice` is less than 9 elements long.
474 #[inline]
475 pub fn write_cols_to_slice(self, slice: &mut [f32]) {
476 slice[0] = self.x_axis.x;
477 slice[1] = self.x_axis.y;
478 slice[2] = self.x_axis.z;
479 slice[3] = self.y_axis.x;
480 slice[4] = self.y_axis.y;
481 slice[5] = self.y_axis.z;
482 slice[6] = self.z_axis.x;
483 slice[7] = self.z_axis.y;
484 slice[8] = self.z_axis.z;
485 }
486
487 /// Returns the matrix column for the given `index`.
488 ///
489 /// # Panics
490 ///
491 /// Panics if `index` is greater than 2.
492 #[inline]
493 #[must_use]
494 pub fn col(&self, index: usize) -> Vec3A {
495 match index {
496 0 => self.x_axis,
497 1 => self.y_axis,
498 2 => self.z_axis,
499 _ => panic!("index out of bounds"),
500 }
501 }
502
503 /// Returns a mutable reference to the matrix column for the given `index`.
504 ///
505 /// # Panics
506 ///
507 /// Panics if `index` is greater than 2.
508 #[inline]
509 pub fn col_mut(&mut self, index: usize) -> &mut Vec3A {
510 match index {
511 0 => &mut self.x_axis,
512 1 => &mut self.y_axis,
513 2 => &mut self.z_axis,
514 _ => panic!("index out of bounds"),
515 }
516 }
517
518 /// Returns the matrix row for the given `index`.
519 ///
520 /// # Panics
521 ///
522 /// Panics if `index` is greater than 2.
523 #[inline]
524 #[must_use]
525 pub fn row(&self, index: usize) -> Vec3A {
526 match index {
527 0 => Vec3A::new(self.x_axis.x, self.y_axis.x, self.z_axis.x),
528 1 => Vec3A::new(self.x_axis.y, self.y_axis.y, self.z_axis.y),
529 2 => Vec3A::new(self.x_axis.z, self.y_axis.z, self.z_axis.z),
530 _ => panic!("index out of bounds"),
531 }
532 }
533
534 /// Returns `true` if, and only if, all elements are finite.
535 /// If any element is either `NaN`, positive or negative infinity, this will return `false`.
536 #[inline]
537 #[must_use]
538 pub fn is_finite(&self) -> bool {
539 self.x_axis.is_finite() && self.y_axis.is_finite() && self.z_axis.is_finite()
540 }
541
542 /// Returns `true` if any elements are `NaN`.
543 #[inline]
544 #[must_use]
545 pub fn is_nan(&self) -> bool {
546 self.x_axis.is_nan() || self.y_axis.is_nan() || self.z_axis.is_nan()
547 }
548
549 /// Returns the transpose of `self`.
550 #[inline]
551 #[must_use]
552 pub fn transpose(&self) -> Self {
553 let tmp0 = i32x4_shuffle::<0, 1, 4, 5>(self.x_axis.0, self.y_axis.0);
554 let tmp1 = i32x4_shuffle::<2, 3, 6, 7>(self.x_axis.0, self.y_axis.0);
555
556 Self {
557 x_axis: Vec3A(i32x4_shuffle::<0, 2, 4, 4>(tmp0, self.z_axis.0)),
558 y_axis: Vec3A(i32x4_shuffle::<1, 3, 5, 5>(tmp0, self.z_axis.0)),
559 z_axis: Vec3A(i32x4_shuffle::<0, 2, 6, 6>(tmp1, self.z_axis.0)),
560 }
561 }
562
563 /// Returns the determinant of `self`.
564 #[inline]
565 #[must_use]
566 pub fn determinant(&self) -> f32 {
567 self.z_axis.dot(self.x_axis.cross(self.y_axis))
568 }
569
570 /// Returns the inverse of `self`.
571 ///
572 /// If the matrix is not invertible the returned matrix will be invalid.
573 ///
574 /// # Panics
575 ///
576 /// Will panic if the determinant of `self` is zero when `glam_assert` is enabled.
577 #[inline]
578 #[must_use]
579 pub fn inverse(&self) -> Self {
580 let tmp0 = self.y_axis.cross(self.z_axis);
581 let tmp1 = self.z_axis.cross(self.x_axis);
582 let tmp2 = self.x_axis.cross(self.y_axis);
583 let det = self.z_axis.dot(tmp2);
584 glam_assert!(det != 0.0);
585 let inv_det = Vec3A::splat(det.recip());
586 Self::from_cols(tmp0.mul(inv_det), tmp1.mul(inv_det), tmp2.mul(inv_det)).transpose()
587 }
588
589 /// Transforms the given 2D vector as a point.
590 ///
591 /// This is the equivalent of multiplying `rhs` as a 3D vector where `z` is `1`.
592 ///
593 /// This method assumes that `self` contains a valid affine transform.
594 ///
595 /// # Panics
596 ///
597 /// Will panic if the 2nd row of `self` is not `(0, 0, 1)` when `glam_assert` is enabled.
598 #[inline]
599 #[must_use]
600 pub fn transform_point2(&self, rhs: Vec2) -> Vec2 {
601 glam_assert!(self.row(2).abs_diff_eq(Vec3A::Z, 1e-6));
602 Mat2::from_cols(self.x_axis.xy(), self.y_axis.xy()) * rhs + self.z_axis.xy()
603 }
604
605 /// Rotates the given 2D vector.
606 ///
607 /// This is the equivalent of multiplying `rhs` as a 3D vector where `z` is `0`.
608 ///
609 /// This method assumes that `self` contains a valid affine transform.
610 ///
611 /// # Panics
612 ///
613 /// Will panic if the 2nd row of `self` is not `(0, 0, 1)` when `glam_assert` is enabled.
614 #[inline]
615 #[must_use]
616 pub fn transform_vector2(&self, rhs: Vec2) -> Vec2 {
617 glam_assert!(self.row(2).abs_diff_eq(Vec3A::Z, 1e-6));
618 Mat2::from_cols(self.x_axis.xy(), self.y_axis.xy()) * rhs
619 }
620
621 /// Transforms a 3D vector.
622 #[inline]
623 #[must_use]
624 pub fn mul_vec3(&self, rhs: Vec3) -> Vec3 {
625 self.mul_vec3a(rhs.into()).into()
626 }
627
628 /// Transforms a [`Vec3A`].
629 #[inline]
630 #[must_use]
631 pub fn mul_vec3a(&self, rhs: Vec3A) -> Vec3A {
632 let mut res = self.x_axis.mul(rhs.xxx());
633 res = res.add(self.y_axis.mul(rhs.yyy()));
634 res = res.add(self.z_axis.mul(rhs.zzz()));
635 res
636 }
637
638 /// Multiplies two 3x3 matrices.
639 #[inline]
640 #[must_use]
641 pub fn mul_mat3(&self, rhs: &Self) -> Self {
642 Self::from_cols(
643 self.mul(rhs.x_axis),
644 self.mul(rhs.y_axis),
645 self.mul(rhs.z_axis),
646 )
647 }
648
649 /// Adds two 3x3 matrices.
650 #[inline]
651 #[must_use]
652 pub fn add_mat3(&self, rhs: &Self) -> Self {
653 Self::from_cols(
654 self.x_axis.add(rhs.x_axis),
655 self.y_axis.add(rhs.y_axis),
656 self.z_axis.add(rhs.z_axis),
657 )
658 }
659
660 /// Subtracts two 3x3 matrices.
661 #[inline]
662 #[must_use]
663 pub fn sub_mat3(&self, rhs: &Self) -> Self {
664 Self::from_cols(
665 self.x_axis.sub(rhs.x_axis),
666 self.y_axis.sub(rhs.y_axis),
667 self.z_axis.sub(rhs.z_axis),
668 )
669 }
670
671 /// Multiplies a 3x3 matrix by a scalar.
672 #[inline]
673 #[must_use]
674 pub fn mul_scalar(&self, rhs: f32) -> Self {
675 Self::from_cols(
676 self.x_axis.mul(rhs),
677 self.y_axis.mul(rhs),
678 self.z_axis.mul(rhs),
679 )
680 }
681
682 /// Divides a 3x3 matrix by a scalar.
683 #[inline]
684 #[must_use]
685 pub fn div_scalar(&self, rhs: f32) -> Self {
686 let rhs = Vec3A::splat(rhs);
687 Self::from_cols(
688 self.x_axis.div(rhs),
689 self.y_axis.div(rhs),
690 self.z_axis.div(rhs),
691 )
692 }
693
694 /// Returns true if the absolute difference of all elements between `self` and `rhs`
695 /// is less than or equal to `max_abs_diff`.
696 ///
697 /// This can be used to compare if two matrices contain similar elements. It works best
698 /// when comparing with a known value. The `max_abs_diff` that should be used used
699 /// depends on the values being compared against.
700 ///
701 /// For more see
702 /// [comparing floating point numbers](https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/).
703 #[inline]
704 #[must_use]
705 pub fn abs_diff_eq(&self, rhs: Self, max_abs_diff: f32) -> bool {
706 self.x_axis.abs_diff_eq(rhs.x_axis, max_abs_diff)
707 && self.y_axis.abs_diff_eq(rhs.y_axis, max_abs_diff)
708 && self.z_axis.abs_diff_eq(rhs.z_axis, max_abs_diff)
709 }
710
711 /// Takes the absolute value of each element in `self`
712 #[inline]
713 #[must_use]
714 pub fn abs(&self) -> Self {
715 Self::from_cols(self.x_axis.abs(), self.y_axis.abs(), self.z_axis.abs())
716 }
717
718 #[inline]
719 pub fn as_dmat3(&self) -> DMat3 {
720 DMat3::from_cols(
721 self.x_axis.as_dvec3(),
722 self.y_axis.as_dvec3(),
723 self.z_axis.as_dvec3(),
724 )
725 }
726 }
727
728 impl Default for Mat3A {
729 #[inline]
730 fn default() -> Self {
731 Self::IDENTITY
732 }
733 }
734
735 impl Add<Mat3A> for Mat3A {
736 type Output = Self;
737 #[inline]
738 fn add(self, rhs: Self) -> Self::Output {
739 self.add_mat3(&rhs)
740 }
741 }
742
743 impl AddAssign<Mat3A> for Mat3A {
744 #[inline]
745 fn add_assign(&mut self, rhs: Self) {
746 *self = self.add_mat3(&rhs);
747 }
748 }
749
750 impl Sub<Mat3A> for Mat3A {
751 type Output = Self;
752 #[inline]
753 fn sub(self, rhs: Self) -> Self::Output {
754 self.sub_mat3(&rhs)
755 }
756 }
757
758 impl SubAssign<Mat3A> for Mat3A {
759 #[inline]
760 fn sub_assign(&mut self, rhs: Self) {
761 *self = self.sub_mat3(&rhs);
762 }
763 }
764
765 impl Neg for Mat3A {
766 type Output = Self;
767 #[inline]
768 fn neg(self) -> Self::Output {
769 Self::from_cols(self.x_axis.neg(), self.y_axis.neg(), self.z_axis.neg())
770 }
771 }
772
773 impl Mul<Mat3A> for Mat3A {
774 type Output = Self;
775 #[inline]
776 fn mul(self, rhs: Self) -> Self::Output {
777 self.mul_mat3(&rhs)
778 }
779 }
780
781 impl MulAssign<Mat3A> for Mat3A {
782 #[inline]
783 fn mul_assign(&mut self, rhs: Self) {
784 *self = self.mul_mat3(&rhs);
785 }
786 }
787
788 impl Mul<Vec3A> for Mat3A {
789 type Output = Vec3A;
790 #[inline]
791 fn mul(self, rhs: Vec3A) -> Self::Output {
792 self.mul_vec3a(rhs)
793 }
794 }
795
796 impl Mul<Mat3A> for f32 {
797 type Output = Mat3A;
798 #[inline]
799 fn mul(self, rhs: Mat3A) -> Self::Output {
800 rhs.mul_scalar(self)
801 }
802 }
803
804 impl Mul<f32> for Mat3A {
805 type Output = Self;
806 #[inline]
807 fn mul(self, rhs: f32) -> Self::Output {
808 self.mul_scalar(rhs)
809 }
810 }
811
812 impl MulAssign<f32> for Mat3A {
813 #[inline]
814 fn mul_assign(&mut self, rhs: f32) {
815 *self = self.mul_scalar(rhs);
816 }
817 }
818
819 impl Div<Mat3A> for f32 {
820 type Output = Mat3A;
821 #[inline]
822 fn div(self, rhs: Mat3A) -> Self::Output {
823 rhs.div_scalar(self)
824 }
825 }
826
827 impl Div<f32> for Mat3A {
828 type Output = Self;
829 #[inline]
830 fn div(self, rhs: f32) -> Self::Output {
831 self.div_scalar(rhs)
832 }
833 }
834
835 impl DivAssign<f32> for Mat3A {
836 #[inline]
837 fn div_assign(&mut self, rhs: f32) {
838 *self = self.div_scalar(rhs);
839 }
840 }
841
842 impl Mul<Vec3> for Mat3A {
843 type Output = Vec3;
844 #[inline]
845 fn mul(self, rhs: Vec3) -> Vec3 {
846 self.mul_vec3a(rhs.into()).into()
847 }
848 }
849
850 impl From<Mat3> for Mat3A {
851 #[inline]
852 fn from(m: Mat3) -> Self {
853 Self {
854 x_axis: m.x_axis.into(),
855 y_axis: m.y_axis.into(),
856 z_axis: m.z_axis.into(),
857 }
858 }
859 }
860
861 impl Sum<Self> for Mat3A {
862 fn sum<I>(iter: I) -> Self
863 where
864 I: Iterator<Item = Self>,
865 {
866 iter.fold(Self::ZERO, Self::add)
867 }
868 }
869
870 impl<'a> Sum<&'a Self> for Mat3A {
871 fn sum<I>(iter: I) -> Self
872 where
873 I: Iterator<Item = &'a Self>,
874 {
875 iter.fold(Self::ZERO, |a, &b| Self::add(a, b))
876 }
877 }
878
879 impl Product for Mat3A {
880 fn product<I>(iter: I) -> Self
881 where
882 I: Iterator<Item = Self>,
883 {
884 iter.fold(Self::IDENTITY, Self::mul)
885 }
886 }
887
888 impl<'a> Product<&'a Self> for Mat3A {
889 fn product<I>(iter: I) -> Self
890 where
891 I: Iterator<Item = &'a Self>,
892 {
893 iter.fold(Self::IDENTITY, |a, &b| Self::mul(a, b))
894 }
895 }
896
897 impl PartialEq for Mat3A {
898 #[inline]
899 fn eq(&self, rhs: &Self) -> bool {
900 self.x_axis.eq(&rhs.x_axis) && self.y_axis.eq(&rhs.y_axis) && self.z_axis.eq(&rhs.z_axis)
901 }
902 }
903
904 impl fmt::Debug for Mat3A {
905 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
906 fmt.debug_struct(stringify!(Mat3A))
907 .field("x_axis", &self.x_axis)
908 .field("y_axis", &self.y_axis)
909 .field("z_axis", &self.z_axis)
910 .finish()
911 }
912 }
913
914 impl fmt::Display for Mat3A {
915 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
916 if let Some(p) = f.precision() {
917 write!(
918 f,
919 "[{:.*}, {:.*}, {:.*}]",
920 p, self.x_axis, p, self.y_axis, p, self.z_axis
921 )
922 } else {
923 write!(f, "[{}, {}, {}]", self.x_axis, self.y_axis, self.z_axis)
924 }
925 }
926 }