Debian Codegraph @

debian/missing-sources/glam-0.17.3/src/mat4.rs

raw symbols SCIP
1 use crate::core::{
2 storage::{Columns4, XYZW},
3 traits::{
4 matrix::{FloatMatrix4x4, Matrix4x4, MatrixConst},
5 projection::ProjectionMatrix,
6 },
7 };
8 use crate::{DMat3, DQuat, DVec3, DVec4, EulerRot, Mat3, Quat, Vec3, Vec3A, Vec4};
9
10 #[cfg(all(
11 target_feature = "sse2",
12 not(feature = "scalar-math"),
13 target_arch = "x86"
14 ))]
15 use core::arch::x86::*;
16 #[cfg(all(
17 target_feature = "sse2",
18 not(feature = "scalar-math"),
19 target_arch = "x86_64"
20 ))]
21 use core::arch::x86_64::*;
22
23 #[cfg(not(target_arch = "spirv"))]
24 use core::fmt;
25 use core::ops::{Add, AddAssign, Deref, DerefMut, Mul, MulAssign, Sub, SubAssign};
26
27 #[cfg(feature = "std")]
28 use std::iter::{Product, Sum};
29
30 //macro_rules! define_mat4_struct {
31 // ($mat4:ident, $inner:ident) => {
32 // /// A 4x4 column major matrix.
33 // ///
34 // /// This 4x4 matrix type features convenience methods for creating and using affine
35 // /// transforms and perspective projections.
36 // ///
37 // /// Affine transformations including 3D translation, rotation and scale can be created
38 // /// using methods such as [`Self::from_translation()`], [`Self::from_quat()`],
39 // /// [`Self::from_scale()`] and [`Self::from_scale_rotation_translation()`].
40 // ///
41 // /// Othographic projections can be created using the methods [`Self::orthographic_lh()`] for
42 // /// left-handed coordinate systems and [`Self::orthographic_rh()`] for right-handed
43 // /// systems. The resulting matrix is also an affine transformation.
44 // ///
45 // /// The [`Self::transform_point3()`] and [`Self::transform_vector3()`] convenience methods
46 // /// are provided for performing affine transformations on 3D vectors and points. These
47 // /// multiply 3D inputs as 4D vectors with an implicit `w` value of `1` for points and `0`
48 // /// for vectors respectively. These methods assume that `Self` contains a valid affine
49 // /// transform.
50 // ///
51 // /// Perspective projections can be created using methods such as
52 // /// [`Self::perspective_lh()`], [`Self::perspective_infinite_lh()`] and
53 // /// [`Self::perspective_infinite_reverse_lh()`] for left-handed co-ordinate systems and
54 // /// [`Self::perspective_rh()`], [`Self::perspective_infinite_rh()`] and
55 // /// [`Self::perspective_infinite_reverse_rh()`] for right-handed co-ordinate systems.
56 // ///
57 // /// The resulting perspective project can be use to transform 3D vectors as points with
58 // /// perspective correction using the [`Self::project_point3()`] convenience method.
59 // #[derive(Clone, Copy)]
60 // #[repr(transparent)]
61 // pub struct $mat4(pub(crate) $inner);
62 // };
63 //}
64
65 macro_rules! impl_mat4_methods {
66 ($t:ident, $vec4:ident, $vec3:ident, $mat3:ident, $quat:ident, $inner:ident) => {
67 /// A 4x4 matrix with all elements set to `0.0`.
68 pub const ZERO: Self = Self($inner::ZERO);
69
70 /// A 4x4 identity matrix, where all diagonal elements are `1`, and all off-diagonal elements are `0`.
71 pub const IDENTITY: Self = Self($inner::IDENTITY);
72
73 /// Creates a 4x4 matrix from four column vectors.
74 #[inline(always)]
75 pub fn from_cols(x_axis: $vec4, y_axis: $vec4, z_axis: $vec4, w_axis: $vec4) -> Self {
76 Self($inner::from_cols(x_axis.0, y_axis.0, z_axis.0, w_axis.0))
77 }
78
79 /// Creates a 4x4 matrix from a `[S; 16]` array stored in column major order.
80 /// If your data is stored in row major you will need to `transpose` the returned
81 /// matrix.
82 #[inline(always)]
83 pub fn from_cols_array(m: &[$t; 16]) -> Self {
84 Self($inner::from_cols_array(m))
85 }
86
87 /// Creates a `[S; 16]` array storing data in column major order.
88 /// If you require data in row major order `transpose` the matrix first.
89 #[inline(always)]
90 pub fn to_cols_array(&self) -> [$t; 16] {
91 self.0.to_cols_array()
92 }
93
94 /// Creates a 4x4 matrix from a `[[S; 4]; 4]` 2D array stored in column major order.
95 /// If your data is in row major order you will need to `transpose` the returned
96 /// matrix.
97 #[inline(always)]
98 pub fn from_cols_array_2d(m: &[[$t; 4]; 4]) -> Self {
99 Self($inner::from_cols_array_2d(m))
100 }
101
102 /// Creates a `[[S; 4]; 4]` 2D array storing data in column major order.
103 /// If you require data in row major order `transpose` the matrix first.
104 #[inline(always)]
105 pub fn to_cols_array_2d(&self) -> [[$t; 4]; 4] {
106 self.0.to_cols_array_2d()
107 }
108
109 /// Creates a 4x4 matrix with its diagonal set to `diagonal` and all other entries set to 0.
110 #[cfg_attr(docsrs, doc(alias = "scale"))]
111 #[inline(always)]
112 pub fn from_diagonal(diagonal: $vec4) -> Self {
113 Self($inner::from_diagonal(diagonal.0.into()))
114 }
115
116 /// Creates an affine transformation matrix from the given 3D `scale`, `rotation` and
117 /// `translation`.
118 ///
119 /// The resulting matrix can be used to transform 3D points and vectors. See
120 /// [`Self::transform_point3()`] and [`Self::transform_vector3()`].
121 ///
122 /// # Panics
123 ///
124 /// Will panic if `rotation` is not normalized when `glam_assert` is enabled.
125 #[inline(always)]
126 pub fn from_scale_rotation_translation(
127 scale: $vec3,
128 rotation: $quat,
129 translation: $vec3,
130 ) -> Self {
131 Self($inner::from_scale_quaternion_translation(
132 scale.0,
133 rotation.0,
134 translation.0,
135 ))
136 }
137
138 /// Creates an affine transformation matrix from the given 3D `translation`.
139 ///
140 /// The resulting matrix can be used to transform 3D points and vectors. See
141 /// [`Self::transform_point3()`] and [`Self::transform_vector3()`].
142 ///
143 /// # Panics
144 ///
145 /// Will panic if `rotation` is not normalized when `glam_assert` is enabled.
146 #[inline(always)]
147 pub fn from_rotation_translation(rotation: $quat, translation: $vec3) -> Self {
148 Self($inner::from_quaternion_translation(
149 rotation.0,
150 translation.0,
151 ))
152 }
153
154 /// Extracts `scale`, `rotation` and `translation` from `self`. The input matrix is
155 /// expected to be a 3D affine transformation matrix otherwise the output will be invalid.
156 ///
157 /// # Panics
158 ///
159 /// Will panic if the determinant of `self` is zero or if the resulting scale vector
160 /// contains any zero elements when `glam_assert` is enabled.
161 #[inline(always)]
162 pub fn to_scale_rotation_translation(&self) -> ($vec3, $quat, $vec3) {
163 let (scale, rotation, translation) = self.0.to_scale_quaternion_translation();
164 ($vec3(scale), $quat(rotation), $vec3(translation))
165 }
166
167 /// Creates an affine transformation matrix from the given `rotation` quaternion.
168 ///
169 /// The resulting matrix can be used to transform 3D points and vectors. See
170 /// [`Self::transform_point3()`] and [`Self::transform_vector3()`].
171 ///
172 /// # Panics
173 ///
174 /// Will panic if `rotation` is not normalized when `glam_assert` is enabled.
175 #[inline(always)]
176 pub fn from_quat(rotation: $quat) -> Self {
177 Self($inner::from_quaternion(rotation.0))
178 }
179
180 /// Creates an affine transformation matrix from the given 3x3 linear transformation
181 /// matrix.
182 ///
183 /// The resulting matrix can be used to transform 3D points and vectors. See
184 /// [`Self::transform_point3()`] and [`Self::transform_vector3()`].
185 #[inline(always)]
186 pub fn from_mat3(m: $mat3) -> Self {
187 Self::from_cols(
188 (m.x_axis, 0.0).into(),
189 (m.y_axis, 0.0).into(),
190 (m.z_axis, 0.0).into(),
191 $vec4::W,
192 )
193 }
194
195 /// Creates an affine transformation matrix from the given 3D `translation`.
196 ///
197 /// The resulting matrix can be used to transform 3D points and vectors. See
198 /// [`Self::transform_point3()`] and [`Self::transform_vector3()`].
199 #[inline(always)]
200 pub fn from_translation(translation: $vec3) -> Self {
201 Self($inner::from_translation(translation.0))
202 }
203
204 /// Creates an affine transformation matrix containing a 3D rotation around a normalized
205 /// rotation `axis` of `angle` (in radians).
206 ///
207 /// The resulting matrix can be used to transform 3D points and vectors. See
208 /// [`Self::transform_point3()`] and [`Self::transform_vector3()`].
209 ///
210 /// # Panics
211 ///
212 /// Will panic if `axis` is not normalized when `glam_assert` is enabled.
213 #[inline(always)]
214 pub fn from_axis_angle(axis: $vec3, angle: $t) -> Self {
215 Self($inner::from_axis_angle(axis.0, angle))
216 }
217
218 #[inline(always)]
219 /// Creates a affine transformation matrix containing a rotation from the given euler
220 /// rotation sequence and angles (in radians).
221 ///
222 /// The resulting matrix can be used to transform 3D points and vectors. See
223 /// [`Self::transform_point3()`] and [`Self::transform_vector3()`].
224 pub fn from_euler(order: EulerRot, a: $t, b: $t, c: $t) -> Self {
225 let quat = $quat::from_euler(order, a, b, c);
226 Self::from_quat(quat)
227 }
228
229 /// Creates an affine transformation matrix containing a 3D rotation around the x axis of
230 /// `angle` (in radians).
231 ///
232 /// The resulting matrix can be used to transform 3D points and vectors. See
233 /// [`Self::transform_point3()`] and [`Self::transform_vector3()`].
234 #[inline(always)]
235 pub fn from_rotation_x(angle: $t) -> Self {
236 Self($inner::from_rotation_x(angle))
237 }
238
239 /// Creates an affine transformation matrix containing a 3D rotation around the y axis of
240 /// `angle` (in radians).
241 ///
242 /// The resulting matrix can be used to transform 3D points and vectors. See
243 /// [`Self::transform_point3()`] and [`Self::transform_vector3()`].
244 #[inline(always)]
245 pub fn from_rotation_y(angle: $t) -> Self {
246 Self($inner::from_rotation_y(angle))
247 }
248
249 /// Creates an affine transformation matrix containing a 3D rotation around the z axis of
250 /// `angle` (in radians).
251 ///
252 /// The resulting matrix can be used to transform 3D points and vectors. See
253 /// [`Self::transform_point3()`] and [`Self::transform_vector3()`].
254 #[inline(always)]
255 pub fn from_rotation_z(angle: $t) -> Self {
256 Self($inner::from_rotation_z(angle))
257 }
258
259 /// Creates an affine transformation matrix containing the given 3D non-uniform `scale`.
260 ///
261 /// The resulting matrix can be used to transform 3D points and vectors. See
262 /// [`Self::transform_point3()`] and [`Self::transform_vector3()`].
263 ///
264 /// # Panics
265 ///
266 /// Will panic if all elements of `scale` are zero when `glam_assert` is enabled.
267 #[inline(always)]
268 pub fn from_scale(scale: $vec3) -> Self {
269 Self($inner::from_scale(scale.0))
270 }
271
272 /// Creates a 4x4 matrix from the first 16 values in `slice`.
273 ///
274 /// # Panics
275 ///
276 /// Panics if `slice` is less than 16 elements long.
277 #[inline(always)]
278 pub fn from_cols_slice(slice: &[$t]) -> Self {
279 Self(Matrix4x4::from_cols_slice(slice))
280 }
281
282 /// Writes the columns of `self` to the first 16 elements in `slice`.
283 ///
284 /// # Panics
285 ///
286 /// Panics if `slice` is less than 16 elements long.
287 #[inline(always)]
288 pub fn write_cols_to_slice(self, slice: &mut [$t]) {
289 Matrix4x4::write_cols_to_slice(&self.0, slice)
290 }
291
292 /// Returns the matrix column for the given `index`.
293 ///
294 /// # Panics
295 ///
296 /// Panics if `index` is greater than 3.
297 #[inline]
298 pub fn col(&self, index: usize) -> $vec4 {
299 match index {
300 0 => self.x_axis,
301 1 => self.y_axis,
302 2 => self.z_axis,
303 3 => self.w_axis,
304 _ => panic!("index out of bounds"),
305 }
306 }
307
308 /// Returns a mutable reference to the matrix column for the given `index`.
309 ///
310 /// # Panics
311 ///
312 /// Panics if `index` is greater than 3.
313 #[inline]
314 pub fn col_mut(&mut self, index: usize) -> &mut $vec4 {
315 match index {
316 0 => &mut self.x_axis,
317 1 => &mut self.y_axis,
318 2 => &mut self.z_axis,
319 3 => &mut self.w_axis,
320 _ => panic!("index out of bounds"),
321 }
322 }
323
324 /// Returns the matrix row for the given `index`.
325 ///
326 /// # Panics
327 ///
328 /// Panics if `index` is greater than 3.
329 #[inline]
330 pub fn row(&self, index: usize) -> $vec4 {
331 match index {
332 0 => $vec4::new(self.x_axis.x, self.y_axis.x, self.z_axis.x, self.w_axis.x),
333 1 => $vec4::new(self.x_axis.y, self.y_axis.y, self.z_axis.y, self.w_axis.y),
334 2 => $vec4::new(self.x_axis.z, self.y_axis.z, self.z_axis.z, self.w_axis.z),
335 3 => $vec4::new(self.x_axis.w, self.y_axis.w, self.z_axis.w, self.w_axis.w),
336 _ => panic!("index out of bounds"),
337 }
338 }
339
340 /// Returns `true` if, and only if, all elements are finite.
341 /// If any element is either `NaN`, positive or negative infinity, this will return `false`.
342 #[inline]
343 pub fn is_finite(&self) -> bool {
344 self.x_axis.is_finite()
345 && self.y_axis.is_finite()
346 && self.z_axis.is_finite()
347 && self.w_axis.is_finite()
348 }
349
350 /// Returns `true` if any elements are `NaN`.
351 #[inline]
352 pub fn is_nan(&self) -> bool {
353 self.x_axis.is_nan()
354 || self.y_axis.is_nan()
355 || self.z_axis.is_nan()
356 || self.w_axis.is_nan()
357 }
358
359 /// Returns the transpose of `self`.
360 #[must_use]
361 #[inline(always)]
362 pub fn transpose(&self) -> Self {
363 Self(self.0.transpose())
364 }
365
366 /// Returns the determinant of `self`.
367 #[inline(always)]
368 pub fn determinant(&self) -> $t {
369 self.0.determinant()
370 }
371
372 /// Returns the inverse of `self`.
373 ///
374 /// If the matrix is not invertible the returned matrix will be invalid.
375 ///
376 /// # Panics
377 ///
378 /// Will panic if the determinant of `self` is zero when `glam_assert` is enabled.
379 #[must_use]
380 #[inline(always)]
381 pub fn inverse(&self) -> Self {
382 Self(self.0.inverse())
383 }
384
385 /// Creates a left-handed view matrix using a camera position, an up direction, and a focal
386 /// point.
387 /// For a view coordinate system with `+X=right`, `+Y=up` and `+Z=forward`.
388 ///
389 /// # Panics
390 ///
391 /// Will panic if `up` is not normalized when `glam_assert` is enabled.
392 #[inline(always)]
393 pub fn look_at_lh(eye: $vec3, center: $vec3, up: $vec3) -> Self {
394 Self($inner::look_at_lh(eye.0, center.0, up.0))
395 }
396
397 /// Creates a right-handed view matrix using a camera position, an up direction, and a focal
398 /// point.
399 /// For a view coordinate system with `+X=right`, `+Y=up` and `+Z=back`.
400 ///
401 /// # Panics
402 ///
403 /// Will panic if `up` is not normalized when `glam_assert` is enabled.
404 #[inline(always)]
405 pub fn look_at_rh(eye: $vec3, center: $vec3, up: $vec3) -> Self {
406 Self($inner::look_at_rh(eye.0, center.0, up.0))
407 }
408
409 /// Creates a right-handed perspective projection matrix with [-1,1] depth range.
410 /// This is the same as the OpenGL `gluPerspective` function.
411 /// See <https://www.khronos.org/registry/OpenGL-Refpages/gl2.1/xhtml/gluPerspective.xml>
412 #[inline(always)]
413 pub fn perspective_rh_gl(
414 fov_y_radians: $t,
415 aspect_ratio: $t,
416 z_near: $t,
417 z_far: $t,
418 ) -> Self {
419 Self($inner::perspective_rh_gl(
420 fov_y_radians,
421 aspect_ratio,
422 z_near,
423 z_far,
424 ))
425 }
426
427 /// Creates a left-handed perspective projection matrix with `[0,1]` depth range.
428 ///
429 /// # Panics
430 ///
431 /// Will panic if `z_near` or `z_far` are less than or equal to zero when `glam_assert` is
432 /// enabled.
433 #[inline(always)]
434 pub fn perspective_lh(fov_y_radians: $t, aspect_ratio: $t, z_near: $t, z_far: $t) -> Self {
435 Self($inner::perspective_lh(
436 fov_y_radians,
437 aspect_ratio,
438 z_near,
439 z_far,
440 ))
441 }
442
443 /// Creates a right-handed perspective projection matrix with `[0,1]` depth range.
444 ///
445 /// # Panics
446 ///
447 /// Will panic if `z_near` or `z_far` are less than or equal to zero when `glam_assert` is
448 /// enabled.
449 #[inline(always)]
450 pub fn perspective_rh(fov_y_radians: $t, aspect_ratio: $t, z_near: $t, z_far: $t) -> Self {
451 Self($inner::perspective_rh(
452 fov_y_radians,
453 aspect_ratio,
454 z_near,
455 z_far,
456 ))
457 }
458
459 /// Creates an infinite left-handed perspective projection matrix with `[0,1]` depth range.
460 ///
461 /// # Panics
462 ///
463 /// Will panic if `z_near` is less than or equal to zero when `glam_assert` is enabled.
464 #[inline(always)]
465 pub fn perspective_infinite_lh(fov_y_radians: $t, aspect_ratio: $t, z_near: $t) -> Self {
466 Self($inner::perspective_infinite_lh(
467 fov_y_radians,
468 aspect_ratio,
469 z_near,
470 ))
471 }
472
473 /// Creates an infinite left-handed perspective projection matrix with `[0,1]` depth range.
474 ///
475 /// # Panics
476 ///
477 /// Will panic if `z_near` is less than or equal to zero when `glam_assert` is enabled.
478 #[inline(always)]
479 pub fn perspective_infinite_reverse_lh(
480 fov_y_radians: $t,
481 aspect_ratio: $t,
482 z_near: $t,
483 ) -> Self {
484 Self($inner::perspective_infinite_reverse_lh(
485 fov_y_radians,
486 aspect_ratio,
487 z_near,
488 ))
489 }
490
491 /// Creates an infinite right-handed perspective projection matrix with
492 /// `[0,1]` depth range.
493 #[inline(always)]
494 pub fn perspective_infinite_rh(fov_y_radians: $t, aspect_ratio: $t, z_near: $t) -> Self {
495 Self($inner::perspective_infinite_rh(
496 fov_y_radians,
497 aspect_ratio,
498 z_near,
499 ))
500 }
501
502 /// Creates an infinite reverse right-handed perspective projection matrix
503 /// with `[0,1]` depth range.
504 #[inline(always)]
505 pub fn perspective_infinite_reverse_rh(
506 fov_y_radians: $t,
507 aspect_ratio: $t,
508 z_near: $t,
509 ) -> Self {
510 Self($inner::perspective_infinite_reverse_rh(
511 fov_y_radians,
512 aspect_ratio,
513 z_near,
514 ))
515 }
516
517 /// Creates a right-handed orthographic projection matrix with `[-1,1]` depth
518 /// range. This is the same as the OpenGL `glOrtho` function in OpenGL.
519 /// See
520 /// <https://www.khronos.org/registry/OpenGL-Refpages/gl2.1/xhtml/glOrtho.xml>
521 #[inline(always)]
522 pub fn orthographic_rh_gl(
523 left: $t,
524 right: $t,
525 bottom: $t,
526 top: $t,
527 near: $t,
528 far: $t,
529 ) -> Self {
530 Self($inner::orthographic_rh_gl(
531 left, right, bottom, top, near, far,
532 ))
533 }
534
535 /// Creates a left-handed orthographic projection matrix with `[0,1]` depth range.
536 #[inline(always)]
537 pub fn orthographic_lh(
538 left: $t,
539 right: $t,
540 bottom: $t,
541 top: $t,
542 near: $t,
543 far: $t,
544 ) -> Self {
545 Self($inner::orthographic_lh(left, right, bottom, top, near, far))
546 }
547
548 /// Creates a right-handed orthographic projection matrix with `[0,1]` depth range.
549 #[inline(always)]
550 pub fn orthographic_rh(
551 left: $t,
552 right: $t,
553 bottom: $t,
554 top: $t,
555 near: $t,
556 far: $t,
557 ) -> Self {
558 Self($inner::orthographic_rh(left, right, bottom, top, near, far))
559 }
560
561 /// Transforms a 4D vector.
562 #[inline(always)]
563 pub fn mul_vec4(&self, other: $vec4) -> $vec4 {
564 $vec4(self.0.mul_vector(other.0))
565 }
566
567 /// Multiplies two 4x4 matrices.
568 #[inline(always)]
569 pub fn mul_mat4(&self, other: &Self) -> Self {
570 Self(self.0.mul_matrix(&other.0))
571 }
572
573 /// Adds two 4x4 matrices.
574 #[inline(always)]
575 pub fn add_mat4(&self, other: &Self) -> Self {
576 Self(self.0.add_matrix(&other.0))
577 }
578
579 /// Subtracts two 4x4 matrices.
580 #[inline(always)]
581 pub fn sub_mat4(&self, other: &Self) -> Self {
582 Self(self.0.sub_matrix(&other.0))
583 }
584
585 /// Multiplies this matrix by a scalar value.
586 #[inline(always)]
587 pub fn mul_scalar(&self, other: $t) -> Self {
588 Self(self.0.mul_scalar(other))
589 }
590
591 /// Transforms the given 3D vector as a point, applying perspective correction.
592 ///
593 /// This is the equivalent of multiplying the 3D vector as a 4D vector where `w` is `1.0`.
594 /// The perspective divide is performed meaning the resulting 3D vector is divided by `w`.
595 ///
596 /// This method assumes that `self` contains a projective transform.
597 #[inline]
598 pub fn project_point3(&self, other: $vec3) -> $vec3 {
599 $vec3(self.0.project_point3(other.0))
600 }
601
602 /// Transforms the given 3D vector as a point.
603 ///
604 /// This is the equivalent of multiplying the 3D vector as a 4D vector where `w` is
605 /// `1.0`.
606 ///
607 /// This method assumes that `self` contains a valid affine transform. It does not perform
608 /// a persective divide, if `self` contains a perspective transform, or if you are unsure,
609 /// the [`Self::project_point3()`] method should be used instead.
610 ///
611 /// # Panics
612 ///
613 /// Will panic if the 3rd row of `self` is not `(0, 0, 0, 1)` when `glam_assert` is enabled.
614 #[inline]
615 pub fn transform_point3(&self, other: $vec3) -> $vec3 {
616 glam_assert!(self.row(3) == $vec4::W);
617 $vec3(self.0.transform_point3(other.0))
618 }
619
620 /// Transforms the give 3D vector as a direction.
621 ///
622 /// This is the equivalent of multiplying the 3D vector as a 4D vector where `w` is
623 /// `0.0`.
624 ///
625 /// This method assumes that `self` contains a valid affine transform.
626 ///
627 /// # Panics
628 ///
629 /// Will panic if the 3rd row of `self` is not `(0, 0, 0, 1)` when `glam_assert` is enabled.
630 #[inline]
631 pub fn transform_vector3(&self, other: $vec3) -> $vec3 {
632 glam_assert!(self.row(3) == $vec4::W);
633 $vec3(self.0.transform_vector3(other.0))
634 }
635
636 /// Returns true if the absolute difference of all elements between `self` and `other`
637 /// is less than or equal to `max_abs_diff`.
638 ///
639 /// This can be used to compare if two 4x4 matrices contain similar elements. It works
640 /// best when comparing with a known value. The `max_abs_diff` that should be used used
641 /// depends on the values being compared against.
642 ///
643 /// For more see
644 /// [comparing floating point numbers](https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/).
645 #[inline(always)]
646 pub fn abs_diff_eq(&self, other: Self, max_abs_diff: $t) -> bool {
647 self.0.abs_diff_eq(&other.0, max_abs_diff)
648 }
649 };
650 }
651
652 macro_rules! impl_mat4_traits {
653 ($t:ty, $new:ident, $mat4:ident, $vec4:ident) => {
654 /// Creates a 4x4 matrix from four column vectors.
655 #[inline(always)]
656 pub fn $new(x_axis: $vec4, y_axis: $vec4, z_axis: $vec4, w_axis: $vec4) -> $mat4 {
657 $mat4::from_cols(x_axis, y_axis, z_axis, w_axis)
658 }
659
660 impl_matn_common_traits!($t, $mat4, $vec4);
661
662 impl Deref for $mat4 {
663 type Target = Columns4<$vec4>;
664 #[inline(always)]
665 fn deref(&self) -> &Self::Target {
666 unsafe { &*(self as *const Self as *const Self::Target) }
667 }
668 }
669
670 impl DerefMut for $mat4 {
671 #[inline(always)]
672 fn deref_mut(&mut self) -> &mut Self::Target {
673 unsafe { &mut *(self as *mut Self as *mut Self::Target) }
674 }
675 }
676
677 impl PartialEq for $mat4 {
678 #[inline]
679 fn eq(&self, other: &Self) -> bool {
680 self.x_axis.eq(&other.x_axis)
681 && self.y_axis.eq(&other.y_axis)
682 && self.z_axis.eq(&other.z_axis)
683 && self.w_axis.eq(&other.w_axis)
684 }
685 }
686
687 #[cfg(not(target_arch = "spriv"))]
688 impl AsRef<[$t; 16]> for $mat4 {
689 #[inline]
690 fn as_ref(&self) -> &[$t; 16] {
691 unsafe { &*(self as *const Self as *const [$t; 16]) }
692 }
693 }
694
695 #[cfg(not(target_arch = "spriv"))]
696 impl AsMut<[$t; 16]> for $mat4 {
697 #[inline]
698 fn as_mut(&mut self) -> &mut [$t; 16] {
699 unsafe { &mut *(self as *mut Self as *mut [$t; 16]) }
700 }
701 }
702
703 #[cfg(not(target_arch = "spirv"))]
704 impl fmt::Debug for $mat4 {
705 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
706 fmt.debug_struct(stringify!($mat4))
707 .field("x_axis", &self.x_axis)
708 .field("y_axis", &self.y_axis)
709 .field("z_axis", &self.z_axis)
710 .field("w_axis", &self.w_axis)
711 .finish()
712 }
713 }
714
715 #[cfg(not(target_arch = "spirv"))]
716 impl fmt::Display for $mat4 {
717 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
718 write!(
719 f,
720 "[{}, {}, {}, {}]",
721 self.x_axis, self.y_axis, self.z_axis, self.w_axis
722 )
723 }
724 }
725 };
726 }
727
728 #[cfg(all(target_feature = "sse2", not(feature = "scalar-math")))]
729 type InnerF32 = Columns4<__m128>;
730
731 #[cfg(any(not(target_feature = "sse2"), feature = "scalar-math"))]
732 type InnerF32 = Columns4<XYZW<f32>>;
733
734 /// A 4x4 column major matrix.
735 ///
736 /// This 4x4 matrix type features convenience methods for creating and using affine transforms and
737 /// perspective projections. If you are primarily dealing with 3D affine transformations
738 /// condidering using [`Affine3A`][crate::Affine3A] which is faster tha a 4x4 matrix for some
739 /// affine operations.
740 ///
741 /// Affine transformations including 3D translation, rotation and scale can be created
742 /// using methods such as [`Self::from_translation()`], [`Self::from_quat()`],
743 /// [`Self::from_scale()`] and [`Self::from_scale_rotation_translation()`].
744 ///
745 /// Othographic projections can be created using the methods [`Self::orthographic_lh()`] for
746 /// left-handed coordinate systems and [`Self::orthographic_rh()`] for right-handed
747 /// systems. The resulting matrix is also an affine transformation.
748 ///
749 /// The [`Self::transform_point3()`] and [`Self::transform_vector3()`] convenience methods
750 /// are provided for performing affine transformations on 3D vectors and points. These
751 /// multiply 3D inputs as 4D vectors with an implicit `w` value of `1` for points and `0`
752 /// for vectors respectively. These methods assume that `Self` contains a valid affine
753 /// transform.
754 ///
755 /// Perspective projections can be created using methods such as
756 /// [`Self::perspective_lh()`], [`Self::perspective_infinite_lh()`] and
757 /// [`Self::perspective_infinite_reverse_lh()`] for left-handed co-ordinate systems and
758 /// [`Self::perspective_rh()`], [`Self::perspective_infinite_rh()`] and
759 /// [`Self::perspective_infinite_reverse_rh()`] for right-handed co-ordinate systems.
760 ///
761 /// The resulting perspective project can be use to transform 3D vectors as points with
762 /// perspective correction using the [`Self::project_point3()`] convenience method.
763 #[derive(Clone, Copy)]
764 #[cfg_attr(
765 not(any(feature = "scalar-math", target_arch = "spriv")),
766 repr(align(16))
767 )]
768 #[cfg_attr(any(feature = "scalar-math", target_arch = "spriv"), repr(transparent))]
769 pub struct Mat4(pub(crate) InnerF32);
770 // define_mat4_struct!(Mat4, InnerF32);
771
772 impl Mat4 {
773 impl_mat4_methods!(f32, Vec4, Vec3, Mat3, Quat, InnerF32);
774
775 /// Transforms the given `Vec3A` as 3D point.
776 ///
777 /// This is the equivalent of multiplying the `Vec3A` as a 4D vector where `w` is `1.0`.
778 #[inline(always)]
779 pub fn transform_point3a(&self, other: Vec3A) -> Vec3A {
780 #[allow(clippy::useless_conversion)]
781 Vec3A(self.0.transform_float4_as_point3(other.0.into()).into())
782 }
783
784 /// Transforms the give `Vec3A` as 3D vector.
785 ///
786 /// This is the equivalent of multiplying the `Vec3A` as a 4D vector where `w` is `0.0`.
787 #[inline(always)]
788 pub fn transform_vector3a(&self, other: Vec3A) -> Vec3A {
789 #[allow(clippy::useless_conversion)]
790 Vec3A(self.0.transform_float4_as_vector3(other.0.into()).into())
791 }
792
793 #[inline(always)]
794 pub fn as_f64(&self) -> DMat4 {
795 DMat4::from_cols(
796 self.x_axis.as_f64(),
797 self.y_axis.as_f64(),
798 self.z_axis.as_f64(),
799 self.w_axis.as_f64(),
800 )
801 }
802 }
803 impl_mat4_traits!(f32, mat4, Mat4, Vec4);
804
805 type InnerF64 = Columns4<XYZW<f64>>;
806
807 /// A 4x4 column major matrix.
808 ///
809 /// This 4x4 matrix type features convenience methods for creating and using affine transforms and
810 /// perspective projections. If you are primarily dealing with 3D affine transformations
811 /// condidering using [`DAffine3`][crate::DAffine3] which is faster tha a 4x4 matrix for some
812 /// affine operations.
813 ///
814 /// Affine transformations including 3D translation, rotation and scale can be created
815 /// using methods such as [`Self::from_translation()`], [`Self::from_quat()`],
816 /// [`Self::from_scale()`] and [`Self::from_scale_rotation_translation()`].
817 ///
818 /// Othographic projections can be created using the methods [`Self::orthographic_lh()`] for
819 /// left-handed coordinate systems and [`Self::orthographic_rh()`] for right-handed
820 /// systems. The resulting matrix is also an affine transformation.
821 ///
822 /// The [`Self::transform_point3()`] and [`Self::transform_vector3()`] convenience methods
823 /// are provided for performing affine transformations on 3D vectors and points. These
824 /// multiply 3D inputs as 4D vectors with an implicit `w` value of `1` for points and `0`
825 /// for vectors respectively. These methods assume that `Self` contains a valid affine
826 /// transform.
827 ///
828 /// Perspective projections can be created using methods such as
829 /// [`Self::perspective_lh()`], [`Self::perspective_infinite_lh()`] and
830 /// [`Self::perspective_infinite_reverse_lh()`] for left-handed co-ordinate systems and
831 /// [`Self::perspective_rh()`], [`Self::perspective_infinite_rh()`] and
832 /// [`Self::perspective_infinite_reverse_rh()`] for right-handed co-ordinate systems.
833 ///
834 /// The resulting perspective project can be use to transform 3D vectors as points with
835 /// perspective correction using the [`Self::project_point3()`] convenience method.
836 #[derive(Clone, Copy)]
837 #[repr(transparent)]
838 pub struct DMat4(pub(crate) InnerF64);
839 // define_mat4_struct!(DMat4, InnerF64);
840
841 impl DMat4 {
842 impl_mat4_methods!(f64, DVec4, DVec3, DMat3, DQuat, InnerF64);
843
844 #[inline(always)]
845 pub fn as_f32(&self) -> Mat4 {
846 Mat4::from_cols(
847 self.x_axis.as_f32(),
848 self.y_axis.as_f32(),
849 self.z_axis.as_f32(),
850 self.w_axis.as_f32(),
851 )
852 }
853 }
854 impl_mat4_traits!(f64, dmat4, DMat4, DVec4);
855
856 #[cfg(any(feature = "scalar-math", target_arch = "spriv"))]
857 mod const_test_mat4 {
858 const_assert_eq!(
859 core::mem::align_of::<f32>(),
860 core::mem::align_of::<super::Mat4>()
861 );
862 const_assert_eq!(64, core::mem::size_of::<super::Mat4>());
863 }
864
865 #[cfg(not(any(feature = "scalar-math", target_arch = "spriv")))]
866 mod const_test_mat4 {
867 const_assert_eq!(16, core::mem::align_of::<super::Mat4>());
868 const_assert_eq!(64, core::mem::size_of::<super::Mat4>());
869 }
870
871 mod const_test_dmat4 {
872 const_assert_eq!(
873 core::mem::align_of::<f64>(),
874 core::mem::align_of::<super::DMat4>()
875 );
876 const_assert_eq!(128, core::mem::size_of::<super::DMat4>());
877 }