Debian Codegraph @

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