diff options
Diffstat (limited to 'src/f32/mat3.rs')
-rw-r--r-- | src/f32/mat3.rs | 65 |
1 files changed, 48 insertions, 17 deletions
diff --git a/src/f32/mat3.rs b/src/f32/mat3.rs index 6c2ed4d..20b3c53 100644 --- a/src/f32/mat3.rs +++ b/src/f32/mat3.rs @@ -1,17 +1,14 @@ // Generated from mat.rs.tera template. Edit the template, not the generated file. -use crate::{swizzles::*, DMat3, EulerRot, Mat2, Mat3A, Mat4, Quat, Vec2, Vec3, Vec3A}; +use crate::{f32::math, swizzles::*, DMat3, EulerRot, Mat2, Mat3A, Mat4, Quat, Vec2, Vec3, Vec3A}; #[cfg(not(target_arch = "spirv"))] use core::fmt; use core::iter::{Product, Sum}; use core::ops::{Add, AddAssign, Mul, MulAssign, Neg, Sub, SubAssign}; -#[cfg(feature = "libm")] -#[allow(unused_imports)] -use num_traits::Float; - -/// Creates a 3x3 matrix from column vectors. +/// Creates a 3x3 matrix from three column vectors. #[inline(always)] +#[must_use] pub const fn mat3(x_axis: Vec3, y_axis: Vec3, z_axis: Vec3) -> Mat3 { Mat3::from_cols(x_axis, y_axis, z_axis) } @@ -60,6 +57,7 @@ impl Mat3 { #[allow(clippy::too_many_arguments)] #[inline(always)] + #[must_use] const fn new( m00: f32, m01: f32, @@ -78,8 +76,9 @@ impl Mat3 { } } - /// Creates a 3x3 matrix from two column vectors. + /// Creates a 3x3 matrix from three column vectors. #[inline(always)] + #[must_use] pub const fn from_cols(x_axis: Vec3, y_axis: Vec3, z_axis: Vec3) -> Self { Self { x_axis, @@ -92,6 +91,7 @@ impl Mat3 { /// If your data is stored in row major you will need to `transpose` the returned /// matrix. #[inline] + #[must_use] pub const fn from_cols_array(m: &[f32; 9]) -> Self { Self::new(m[0], m[1], m[2], m[3], m[4], m[5], m[6], m[7], m[8]) } @@ -99,6 +99,7 @@ impl Mat3 { /// Creates a `[f32; 9]` array storing data in column major order. /// If you require data in row major order `transpose` the matrix first. #[inline] + #[must_use] pub const fn to_cols_array(&self) -> [f32; 9] { [ self.x_axis.x, @@ -117,6 +118,7 @@ impl Mat3 { /// If your data is in row major order you will need to `transpose` the returned /// matrix. #[inline] + #[must_use] pub const fn from_cols_array_2d(m: &[[f32; 3]; 3]) -> Self { Self::from_cols( Vec3::from_array(m[0]), @@ -128,6 +130,7 @@ impl Mat3 { /// Creates a `[[f32; 3]; 3]` 3D array storing data in column major order. /// If you require data in row major order `transpose` the matrix first. #[inline] + #[must_use] pub const fn to_cols_array_2d(&self) -> [[f32; 3]; 3] { [ self.x_axis.to_array(), @@ -139,6 +142,7 @@ impl Mat3 { /// Creates a 3x3 matrix with its diagonal set to `diagonal` and all other entries set to 0. #[doc(alias = "scale")] #[inline] + #[must_use] pub const fn from_diagonal(diagonal: Vec3) -> Self { Self::new( diagonal.x, 0.0, 0.0, 0.0, diagonal.y, 0.0, 0.0, 0.0, diagonal.z, @@ -146,6 +150,8 @@ impl Mat3 { } /// Creates a 3x3 matrix from a 4x4 matrix, discarding the 4th row and column. + #[inline] + #[must_use] pub fn from_mat4(m: Mat4) -> Self { Self::from_cols(m.x_axis.xyz(), m.y_axis.xyz(), m.z_axis.xyz()) } @@ -156,6 +162,7 @@ impl Mat3 { /// /// Will panic if `rotation` is not normalized when `glam_assert` is enabled. #[inline] + #[must_use] pub fn from_quat(rotation: Quat) -> Self { glam_assert!(rotation.is_normalized()); @@ -186,10 +193,11 @@ impl Mat3 { /// /// Will panic if `axis` is not normalized when `glam_assert` is enabled. #[inline] + #[must_use] pub fn from_axis_angle(axis: Vec3, angle: f32) -> Self { glam_assert!(axis.is_normalized()); - let (sin, cos) = angle.sin_cos(); + let (sin, cos) = math::sin_cos(angle); let (xsin, ysin, zsin) = axis.mul(sin).into(); let (x, y, z) = axis.into(); let (x2, y2, z2) = axis.mul(axis).into(); @@ -204,9 +212,10 @@ impl Mat3 { ) } - #[inline] /// Creates a 3D rotation matrix from the given euler rotation sequence and the angles (in /// radians). + #[inline] + #[must_use] pub fn from_euler(order: EulerRot, a: f32, b: f32, c: f32) -> Self { let quat = Quat::from_euler(order, a, b, c); Self::from_quat(quat) @@ -214,8 +223,9 @@ impl Mat3 { /// Creates a 3D rotation matrix from `angle` (in radians) around the x axis. #[inline] + #[must_use] pub fn from_rotation_x(angle: f32) -> Self { - let (sina, cosa) = angle.sin_cos(); + let (sina, cosa) = math::sin_cos(angle); Self::from_cols( Vec3::X, Vec3::new(0.0, cosa, sina), @@ -225,8 +235,9 @@ impl Mat3 { /// Creates a 3D rotation matrix from `angle` (in radians) around the y axis. #[inline] + #[must_use] pub fn from_rotation_y(angle: f32) -> Self { - let (sina, cosa) = angle.sin_cos(); + let (sina, cosa) = math::sin_cos(angle); Self::from_cols( Vec3::new(cosa, 0.0, -sina), Vec3::Y, @@ -236,8 +247,9 @@ impl Mat3 { /// Creates a 3D rotation matrix from `angle` (in radians) around the z axis. #[inline] + #[must_use] pub fn from_rotation_z(angle: f32) -> Self { - let (sina, cosa) = angle.sin_cos(); + let (sina, cosa) = math::sin_cos(angle); Self::from_cols( Vec3::new(cosa, sina, 0.0), Vec3::new(-sina, cosa, 0.0), @@ -250,6 +262,7 @@ impl Mat3 { /// The resulting matrix can be used to transform 2D points and vectors. See /// [`Self::transform_point2()`] and [`Self::transform_vector2()`]. #[inline] + #[must_use] pub fn from_translation(translation: Vec2) -> Self { Self::from_cols( Vec3::X, @@ -264,8 +277,9 @@ impl Mat3 { /// The resulting matrix can be used to transform 2D points and vectors. See /// [`Self::transform_point2()`] and [`Self::transform_vector2()`]. #[inline] + #[must_use] pub fn from_angle(angle: f32) -> Self { - let (sin, cos) = angle.sin_cos(); + let (sin, cos) = math::sin_cos(angle); Self::from_cols(Vec3::new(cos, sin, 0.0), Vec3::new(-sin, cos, 0.0), Vec3::Z) } @@ -275,8 +289,9 @@ impl Mat3 { /// The resulting matrix can be used to transform 2D points and vectors. See /// [`Self::transform_point2()`] and [`Self::transform_vector2()`]. #[inline] + #[must_use] pub fn from_scale_angle_translation(scale: Vec2, angle: f32, translation: Vec2) -> Self { - let (sin, cos) = angle.sin_cos(); + let (sin, cos) = math::sin_cos(angle); Self::from_cols( Vec3::new(cos * scale.x, sin * scale.x, 0.0), Vec3::new(-sin * scale.y, cos * scale.y, 0.0), @@ -293,6 +308,7 @@ impl Mat3 { /// /// Will panic if all elements of `scale` are zero when `glam_assert` is enabled. #[inline] + #[must_use] pub fn from_scale(scale: Vec2) -> Self { // Do not panic as long as any component is non-zero glam_assert!(scale.cmpne(Vec2::ZERO).any()); @@ -319,6 +335,7 @@ impl Mat3 { /// /// Panics if `slice` is less than 9 elements long. #[inline] + #[must_use] pub const fn from_cols_slice(slice: &[f32]) -> Self { Self::new( slice[0], slice[1], slice[2], slice[3], slice[4], slice[5], slice[6], slice[7], @@ -350,6 +367,7 @@ impl Mat3 { /// /// Panics if `index` is greater than 2. #[inline] + #[must_use] pub fn col(&self, index: usize) -> Vec3 { match index { 0 => self.x_axis, @@ -380,6 +398,7 @@ impl Mat3 { /// /// Panics if `index` is greater than 2. #[inline] + #[must_use] pub fn row(&self, index: usize) -> Vec3 { match index { 0 => Vec3::new(self.x_axis.x, self.y_axis.x, self.z_axis.x), @@ -392,19 +411,21 @@ impl Mat3 { /// Returns `true` if, and only if, all elements are finite. /// If any element is either `NaN`, positive or negative infinity, this will return `false`. #[inline] + #[must_use] pub fn is_finite(&self) -> bool { self.x_axis.is_finite() && self.y_axis.is_finite() && self.z_axis.is_finite() } /// Returns `true` if any elements are `NaN`. #[inline] + #[must_use] pub fn is_nan(&self) -> bool { self.x_axis.is_nan() || self.y_axis.is_nan() || self.z_axis.is_nan() } /// Returns the transpose of `self`. - #[must_use] #[inline] + #[must_use] pub fn transpose(&self) -> Self { Self { x_axis: Vec3::new(self.x_axis.x, self.y_axis.x, self.z_axis.x), @@ -415,6 +436,7 @@ impl Mat3 { /// Returns the determinant of `self`. #[inline] + #[must_use] pub fn determinant(&self) -> f32 { self.z_axis.dot(self.x_axis.cross(self.y_axis)) } @@ -426,8 +448,8 @@ impl Mat3 { /// # Panics /// /// Will panic if the determinant of `self` is zero when `glam_assert` is enabled. - #[must_use] #[inline] + #[must_use] pub fn inverse(&self) -> Self { let tmp0 = self.y_axis.cross(self.z_axis); let tmp1 = self.z_axis.cross(self.x_axis); @@ -448,6 +470,7 @@ impl Mat3 { /// /// Will panic if the 2nd row of `self` is not `(0, 0, 1)` when `glam_assert` is enabled. #[inline] + #[must_use] pub fn transform_point2(&self, rhs: Vec2) -> Vec2 { glam_assert!(self.row(2).abs_diff_eq(Vec3::Z, 1e-6)); Mat2::from_cols(self.x_axis.xy(), self.y_axis.xy()) * rhs + self.z_axis.xy() @@ -463,6 +486,7 @@ impl Mat3 { /// /// Will panic if the 2nd row of `self` is not `(0, 0, 1)` when `glam_assert` is enabled. #[inline] + #[must_use] pub fn transform_vector2(&self, rhs: Vec2) -> Vec2 { glam_assert!(self.row(2).abs_diff_eq(Vec3::Z, 1e-6)); Mat2::from_cols(self.x_axis.xy(), self.y_axis.xy()) * rhs @@ -470,6 +494,7 @@ impl Mat3 { /// Transforms a 3D vector. #[inline] + #[must_use] pub fn mul_vec3(&self, rhs: Vec3) -> Vec3 { let mut res = self.x_axis.mul(rhs.x); res = res.add(self.y_axis.mul(rhs.y)); @@ -477,14 +502,16 @@ impl Mat3 { res } - /// Transforms a `Vec3A`. + /// Transforms a [`Vec3A`]. #[inline] + #[must_use] pub fn mul_vec3a(&self, rhs: Vec3A) -> Vec3A { self.mul_vec3(rhs.into()).into() } /// Multiplies two 3x3 matrices. #[inline] + #[must_use] pub fn mul_mat3(&self, rhs: &Self) -> Self { Self::from_cols( self.mul(rhs.x_axis), @@ -495,6 +522,7 @@ impl Mat3 { /// Adds two 3x3 matrices. #[inline] + #[must_use] pub fn add_mat3(&self, rhs: &Self) -> Self { Self::from_cols( self.x_axis.add(rhs.x_axis), @@ -505,6 +533,7 @@ impl Mat3 { /// Subtracts two 3x3 matrices. #[inline] + #[must_use] pub fn sub_mat3(&self, rhs: &Self) -> Self { Self::from_cols( self.x_axis.sub(rhs.x_axis), @@ -515,6 +544,7 @@ impl Mat3 { /// Multiplies a 3x3 matrix by a scalar. #[inline] + #[must_use] pub fn mul_scalar(&self, rhs: f32) -> Self { Self::from_cols( self.x_axis.mul(rhs), @@ -533,6 +563,7 @@ impl Mat3 { /// For more see /// [comparing floating point numbers](https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/). #[inline] + #[must_use] pub fn abs_diff_eq(&self, rhs: Self, max_abs_diff: f32) -> bool { self.x_axis.abs_diff_eq(rhs.x_axis, max_abs_diff) && self.y_axis.abs_diff_eq(rhs.y_axis, max_abs_diff) |