Debian Codegraph @

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