Debian Codegraph @

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