Debian Codegraph @

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