| 1 | use crate::core::{ |
| 2 | storage::{Columns3, XYZ}, |
| 3 | traits::matrix::{FloatMatrix3x3, Matrix3x3, MatrixConst}, |
| 4 | }; |
| 5 | use crate::{DMat2, DMat4, DQuat, DVec2, DVec3, EulerRot, Mat2, Mat4, Quat, Vec2, Vec3, Vec3A}; |
| 6 | #[cfg(not(target_arch = "spirv"))] |
| 7 | use core::fmt; |
| 8 | use core::ops::{Add, Deref, DerefMut, Mul, Sub}; |
| 9 | |
| 10 | #[cfg(all( |
| 11 | target_arch = "x86", |
| 12 | target_feature = "sse2", |
| 13 | not(feature = "scalar-math") |
| 14 | ))] |
| 15 | use core::arch::x86::*; |
| 16 | #[cfg(all( |
| 17 | target_arch = "x86_64", |
| 18 | target_feature = "sse2", |
| 19 | not(feature = "scalar-math") |
| 20 | ))] |
| 21 | use core::arch::x86_64::*; |
| 22 | |
| 23 | #[cfg(feature = "std")] |
| 24 | use std::iter::{Product, Sum}; |
| 25 | |
| 26 | macro_rules! define_mat3_struct { |
| 27 | ($mat3:ident, $inner:ident) => { |
| 28 | /// A 3x3 column major matrix. |
| 29 | /// |
| 30 | /// This 3x3 matrix type features convenience methods for creating and using linear and |
| 31 | /// affine transformations. If you are primarily dealing with 2D affine transformations the |
| 32 | /// [`Affine2`][crate::Affine2] type is much faster and more space efficient than using a |
| 33 | /// 3x3 matrix. |
| 34 | /// |
| 35 | /// Linear transformations including 3D rotation and scale can be created using methods |
| 36 | /// such as [`Self::from_diagonal()`], [`Self::from_quat()`], [`Self::from_axis_angle()`], |
| 37 | /// [`Self::from_rotation_x()`], [`Self::from_rotation_y()`], or |
| 38 | /// [`Self::from_rotation_z()`]. |
| 39 | /// |
| 40 | /// The resulting matrices can be use to transform 3D vectors using regular vector |
| 41 | /// multiplication. |
| 42 | /// |
| 43 | /// Affine transformations including 2D translation, rotation and scale can be created |
| 44 | /// using methods such as [`Self::from_translation()`], [`Self::from_angle()`], |
| 45 | /// [`Self::from_scale()`] and [`Self::from_scale_angle_translation()`]. |
| 46 | /// |
| 47 | /// The [`Self::transform_point2()`] and [`Self::transform_vector2()`] convenience methods |
| 48 | /// are provided for performing affine transforms on 2D vectors and points. These multiply |
| 49 | /// 2D inputs as 3D vectors with an implicit `z` value of `1` for points and `0` for |
| 50 | /// vectors respectively. These methods assume that `Self` contains a valid affine |
| 51 | /// transform. |
| 52 | #[derive(Clone, Copy)] |
| 53 | #[cfg_attr(not(target_arch = "spirv"), repr(C))] |
| 54 | pub struct $mat3(pub(crate) $inner); |
| 55 | }; |
| 56 | } |
| 57 | |
| 58 | macro_rules! impl_mat3_methods { |
| 59 | ($t:ty, $vec3:ident, $vec3a:ident, $vec2:ident, $quat:ident, $mat2:ident, $inner:ident) => { |
| 60 | /// A 3x3 matrix with all elements set to `0.0`. |
| 61 | pub const ZERO: Self = Self($inner::ZERO); |
| 62 | |
| 63 | /// A 3x3 identity matrix, where all diagonal elements are `1`, and all off-diagonal |
| 64 | /// elements are `0`. |
| 65 | pub const IDENTITY: Self = Self($inner::IDENTITY); |
| 66 | |
| 67 | /// Creates a 3x3 matrix from three column vectors. |
| 68 | #[inline(always)] |
| 69 | pub fn from_cols(x_axis: $vec3a, y_axis: $vec3a, z_axis: $vec3a) -> Self { |
| 70 | Self(Matrix3x3::from_cols(x_axis.0, y_axis.0, z_axis.0)) |
| 71 | } |
| 72 | |
| 73 | /// Creates a 3x3 matrix from a `[S; 9]` array stored in column major order. |
| 74 | /// If your data is stored in row major you will need to `transpose` the returned |
| 75 | /// matrix. |
| 76 | #[inline(always)] |
| 77 | pub fn from_cols_array(m: &[$t; 9]) -> Self { |
| 78 | Self(Matrix3x3::from_cols_array(m)) |
| 79 | } |
| 80 | |
| 81 | /// Creates a `[S; 9]` array storing data in column major order. |
| 82 | /// If you require data in row major order `transpose` the matrix first. |
| 83 | #[inline(always)] |
| 84 | pub fn to_cols_array(&self) -> [$t; 9] { |
| 85 | self.0.to_cols_array() |
| 86 | } |
| 87 | |
| 88 | /// Creates a 3x3 matrix from a `[[S; 3]; 3]` 2D array stored in column major order. |
| 89 | /// If your data is in row major order you will need to `transpose` the returned |
| 90 | /// matrix. |
| 91 | #[inline(always)] |
| 92 | pub fn from_cols_array_2d(m: &[[$t; 3]; 3]) -> Self { |
| 93 | Self(Matrix3x3::from_cols_array_2d(m)) |
| 94 | } |
| 95 | |
| 96 | /// Creates a `[[S; 3]; 3]` 2D array storing data in column major order. |
| 97 | /// If you require data in row major order `transpose` the matrix first. |
| 98 | #[inline(always)] |
| 99 | pub fn to_cols_array_2d(&self) -> [[$t; 3]; 3] { |
| 100 | self.0.to_cols_array_2d() |
| 101 | } |
| 102 | |
| 103 | /// Creates a 3x3 matrix with its diagonal set to `diagonal` and all other entries set to 0. |
| 104 | /// The resulting matrix is a 3D scale transfom. |
| 105 | #[cfg_attr(docsrs, doc(alias = "scale"))] |
| 106 | #[inline(always)] |
| 107 | pub fn from_diagonal(diagonal: $vec3) -> Self { |
| 108 | Self($inner::from_diagonal(diagonal.0)) |
| 109 | } |
| 110 | |
| 111 | #[inline(always)] |
| 112 | /// Creates a 3D rotation matrix from the given quaternion. |
| 113 | pub fn from_quat(rotation: $quat) -> Self { |
| 114 | // TODO: SIMD? |
| 115 | Self($inner::from_quaternion(rotation.0.into())) |
| 116 | } |
| 117 | |
| 118 | /// Creates a 3D rotation matrix from a normalized rotation `axis` and `angle` (in |
| 119 | /// radians). |
| 120 | #[inline(always)] |
| 121 | pub fn from_axis_angle(axis: $vec3, angle: $t) -> Self { |
| 122 | Self(FloatMatrix3x3::from_axis_angle(axis.0, angle)) |
| 123 | } |
| 124 | |
| 125 | #[deprecated( |
| 126 | since = "0.15.0", |
| 127 | note = "Please use `from_euler(EulerRot::YXZ, yaw, pitch, roll)` instead" |
| 128 | )] |
| 129 | #[inline(always)] |
| 130 | pub fn from_rotation_ypr(yaw: $t, pitch: $t, roll: $t) -> Self { |
| 131 | Self::from_euler(EulerRot::YXZ, yaw, pitch, roll) |
| 132 | } |
| 133 | |
| 134 | #[inline(always)] |
| 135 | /// Creates a 3D rotation matrix from the given euler rotation sequence and the angles (in |
| 136 | /// radians). |
| 137 | pub fn from_euler(order: EulerRot, a: $t, b: $t, c: $t) -> Self { |
| 138 | let quat = $quat::from_euler(order, a, b, c); |
| 139 | Self::from_quat(quat) |
| 140 | } |
| 141 | |
| 142 | /// Creates a 3D rotation matrix from `angle` (in radians) around the x axis. |
| 143 | #[inline(always)] |
| 144 | pub fn from_rotation_x(angle: $t) -> Self { |
| 145 | Self($inner::from_rotation_x(angle)) |
| 146 | } |
| 147 | |
| 148 | /// Creates a 3D rotation matrix from `angle` (in radians) around the y axis. |
| 149 | #[inline(always)] |
| 150 | pub fn from_rotation_y(angle: $t) -> Self { |
| 151 | Self($inner::from_rotation_y(angle)) |
| 152 | } |
| 153 | |
| 154 | /// Creates a 3D rotation matrix from `angle` (in radians) around the z axis. |
| 155 | #[inline(always)] |
| 156 | pub fn from_rotation_z(angle: $t) -> Self { |
| 157 | Self($inner::from_rotation_z(angle)) |
| 158 | } |
| 159 | |
| 160 | /// Creates an affine transformation matrix from the given 2D `translation`. |
| 161 | /// |
| 162 | /// The resulting matrix can be used to transform 2D points and vectors. See |
| 163 | /// [`Self::transform_point2()`] and [`Self::transform_vector2()`]. |
| 164 | #[inline(always)] |
| 165 | pub fn from_translation(translation: $vec2) -> Self { |
| 166 | Self(Matrix3x3::from_translation(translation.0)) |
| 167 | } |
| 168 | |
| 169 | /// Creates an affine transformation matrix from the given 2D rotation `angle` (in |
| 170 | /// radians). |
| 171 | /// |
| 172 | /// The resulting matrix can be used to transform 2D points and vectors. See |
| 173 | /// [`Self::transform_point2()`] and [`Self::transform_vector2()`]. |
| 174 | #[inline(always)] |
| 175 | pub fn from_angle(angle: $t) -> Self { |
| 176 | Self(FloatMatrix3x3::from_angle(angle)) |
| 177 | } |
| 178 | |
| 179 | /// Creates an affine transformation matrix from the given 2D `scale`, rotation `angle` (in |
| 180 | /// radians) and `translation`. |
| 181 | /// |
| 182 | /// The resulting matrix can be used to transform 2D points and vectors. See |
| 183 | /// [`Self::transform_point2()`] and [`Self::transform_vector2()`]. |
| 184 | #[inline(always)] |
| 185 | pub fn from_scale_angle_translation(scale: $vec2, angle: $t, translation: $vec2) -> Self { |
| 186 | Self(FloatMatrix3x3::from_scale_angle_translation( |
| 187 | scale.0, |
| 188 | angle, |
| 189 | translation.0, |
| 190 | )) |
| 191 | } |
| 192 | |
| 193 | /// Creates an affine transformation matrix from the given non-uniform 2D `scale`. |
| 194 | /// |
| 195 | /// The resulting matrix can be used to transform 2D points and vectors. See |
| 196 | /// [`Self::transform_point2()`] and [`Self::transform_vector2()`]. |
| 197 | #[inline(always)] |
| 198 | pub fn from_scale(scale: $vec2) -> Self { |
| 199 | Self(Matrix3x3::from_scale(scale.0)) |
| 200 | } |
| 201 | |
| 202 | /// Creates a 3x3 matrix from the first 9 values in `slice`. |
| 203 | /// |
| 204 | /// # Panics |
| 205 | /// |
| 206 | /// Panics if `slice` is less than 9 elements long. |
| 207 | #[inline(always)] |
| 208 | pub fn from_cols_slice(slice: &[$t]) -> Self { |
| 209 | Self(Matrix3x3::from_cols_slice(slice)) |
| 210 | } |
| 211 | |
| 212 | /// Writes the columns of `self` to the first 9 elements in `slice`. |
| 213 | /// |
| 214 | /// # Panics |
| 215 | /// |
| 216 | /// Panics if `slice` is less than 9 elements long. |
| 217 | #[inline(always)] |
| 218 | pub fn write_cols_to_slice(self, slice: &mut [$t]) { |
| 219 | Matrix3x3::write_cols_to_slice(&self.0, slice) |
| 220 | } |
| 221 | |
| 222 | /// Returns the matrix column for the given `index`. |
| 223 | /// |
| 224 | /// # Panics |
| 225 | /// |
| 226 | /// Panics if `index` is greater than 2. |
| 227 | #[inline] |
| 228 | pub fn col(&self, index: usize) -> $vec3a { |
| 229 | match index { |
| 230 | 0 => self.x_axis, |
| 231 | 1 => self.y_axis, |
| 232 | 2 => self.z_axis, |
| 233 | _ => panic!( |
| 234 | "index out of bounds: the len is 3 but the index is {}", |
| 235 | index |
| 236 | ), |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | /// Returns the matrix row for the given `index`. |
| 241 | /// |
| 242 | /// # Panics |
| 243 | /// |
| 244 | /// Panics if `index` is greater than 2. |
| 245 | #[inline] |
| 246 | pub fn row(&self, index: usize) -> $vec3a { |
| 247 | match index { |
| 248 | 0 => $vec3a::new(self.x_axis.x, self.y_axis.x, self.z_axis.x), |
| 249 | 1 => $vec3a::new(self.x_axis.y, self.y_axis.y, self.z_axis.y), |
| 250 | 2 => $vec3a::new(self.x_axis.z, self.y_axis.z, self.z_axis.z), |
| 251 | _ => panic!( |
| 252 | "index out of bounds: the len is 3 but the index is {}", |
| 253 | index |
| 254 | ), |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | // #[inline] |
| 259 | // pub(crate) fn col_mut(&mut self, index: usize) -> &mut $vec3 { |
| 260 | // match index { |
| 261 | // 0 => &mut self.x_axis, |
| 262 | // 1 => &mut self.y_axis, |
| 263 | // 2 => &mut self.z_axis, |
| 264 | // _ => panic!( |
| 265 | // "index out of bounds: the len is 3 but the index is {}", |
| 266 | // index |
| 267 | // ), |
| 268 | // } |
| 269 | // } |
| 270 | |
| 271 | /// Returns `true` if, and only if, all elements are finite. |
| 272 | /// If any element is either `NaN`, positive or negative infinity, this will return `false`. |
| 273 | #[inline] |
| 274 | pub fn is_finite(&self) -> bool { |
| 275 | self.x_axis.is_finite() && self.y_axis.is_finite() && self.z_axis.is_finite() |
| 276 | } |
| 277 | |
| 278 | /// Returns `true` if any elements are `NaN`. |
| 279 | #[inline] |
| 280 | pub fn is_nan(&self) -> bool { |
| 281 | self.x_axis.is_nan() || self.y_axis.is_nan() || self.z_axis.is_nan() |
| 282 | } |
| 283 | |
| 284 | /// Returns the transpose of `self`. |
| 285 | #[must_use] |
| 286 | #[inline(always)] |
| 287 | pub fn transpose(&self) -> Self { |
| 288 | Self(self.0.transpose()) |
| 289 | } |
| 290 | |
| 291 | /// Returns the determinant of `self`. |
| 292 | #[inline(always)] |
| 293 | pub fn determinant(&self) -> $t { |
| 294 | self.0.determinant() |
| 295 | } |
| 296 | |
| 297 | /// Returns the inverse of `self`. |
| 298 | /// |
| 299 | /// If the matrix is not invertible the returned matrix will be invalid. |
| 300 | #[must_use] |
| 301 | #[inline(always)] |
| 302 | pub fn inverse(&self) -> Self { |
| 303 | Self(self.0.inverse()) |
| 304 | } |
| 305 | |
| 306 | /// Transforms a 3D vector. |
| 307 | #[inline(always)] |
| 308 | pub fn mul_vec3(&self, other: $vec3) -> $vec3 { |
| 309 | $vec3(self.0.mul_vector(other.0.into()).into()) |
| 310 | } |
| 311 | |
| 312 | /// Multiplies two 3x3 matrices. |
| 313 | #[inline] |
| 314 | pub fn mul_mat3(&self, other: &Self) -> Self { |
| 315 | Self(self.0.mul_matrix(&other.0)) |
| 316 | } |
| 317 | |
| 318 | /// Adds two 3x3 matrices. |
| 319 | #[inline(always)] |
| 320 | pub fn add_mat3(&self, other: &Self) -> Self { |
| 321 | Self(self.0.add_matrix(&other.0)) |
| 322 | } |
| 323 | |
| 324 | /// Subtracts two 3x3 matrices. |
| 325 | #[inline(always)] |
| 326 | pub fn sub_mat3(&self, other: &Self) -> Self { |
| 327 | Self(self.0.sub_matrix(&other.0)) |
| 328 | } |
| 329 | |
| 330 | /// Multiplies a 3x3 matrix by a scalar. |
| 331 | #[inline(always)] |
| 332 | pub fn mul_scalar(&self, other: $t) -> Self { |
| 333 | Self(self.0.mul_scalar(other)) |
| 334 | } |
| 335 | |
| 336 | /// Transforms the given 2D vector as a point. |
| 337 | /// |
| 338 | /// This is the equivalent of multiplying `other` as a 3D vector where `z` is `1`. |
| 339 | /// |
| 340 | /// This method assumes that `self` contains a valid affine transform. |
| 341 | #[inline(always)] |
| 342 | pub fn transform_point2(&self, other: $vec2) -> $vec2 { |
| 343 | $mat2::from_cols(self.x_axis.into(), self.y_axis.into()) * other + self.z_axis.into() |
| 344 | } |
| 345 | |
| 346 | /// Rotates the given 2D vector. |
| 347 | /// |
| 348 | /// This is the equivalent of multiplying `other` as a 3D vector where `z` is `0`. |
| 349 | /// |
| 350 | /// This method assumes that `self` contains a valid affine transform. |
| 351 | #[inline(always)] |
| 352 | pub fn transform_vector2(&self, other: $vec2) -> $vec2 { |
| 353 | $mat2::from_cols(self.x_axis.into(), self.y_axis.into()) * other |
| 354 | } |
| 355 | |
| 356 | /// Returns true if the absolute difference of all elements between `self` and `other` |
| 357 | /// is less than or equal to `max_abs_diff`. |
| 358 | /// |
| 359 | /// This can be used to compare if two matrices contain similar elements. It works best |
| 360 | /// when comparing with a known value. The `max_abs_diff` that should be used used |
| 361 | /// depends on the values being compared against. |
| 362 | /// |
| 363 | /// For more see |
| 364 | /// [comparing floating point numbers](https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/). |
| 365 | #[inline(always)] |
| 366 | pub fn abs_diff_eq(&self, other: Self, max_abs_diff: $t) -> bool { |
| 367 | self.0.abs_diff_eq(&other.0, max_abs_diff) |
| 368 | } |
| 369 | }; |
| 370 | } |
| 371 | |
| 372 | macro_rules! impl_mat3_traits { |
| 373 | ($t:ty, $new:ident, $mat3:ident, $mat4:ident, $vec3:ident, $vec3a:ident) => { |
| 374 | /// Creates a 3x3 matrix from three column vectors. |
| 375 | #[inline(always)] |
| 376 | pub fn $new(x_axis: $vec3a, y_axis: $vec3a, z_axis: $vec3a) -> $mat3 { |
| 377 | $mat3::from_cols(x_axis, y_axis, z_axis) |
| 378 | } |
| 379 | |
| 380 | impl Default for $mat3 { |
| 381 | #[inline(always)] |
| 382 | fn default() -> Self { |
| 383 | Self::IDENTITY |
| 384 | } |
| 385 | } |
| 386 | |
| 387 | impl PartialEq for $mat3 { |
| 388 | #[inline] |
| 389 | fn eq(&self, other: &Self) -> bool { |
| 390 | self.x_axis.eq(&other.x_axis) |
| 391 | && self.y_axis.eq(&other.y_axis) |
| 392 | && self.z_axis.eq(&other.z_axis) |
| 393 | } |
| 394 | } |
| 395 | |
| 396 | impl Deref for $mat3 { |
| 397 | type Target = Columns3<$vec3a>; |
| 398 | #[inline(always)] |
| 399 | fn deref(&self) -> &Self::Target { |
| 400 | unsafe { &*(self as *const Self as *const Self::Target) } |
| 401 | } |
| 402 | } |
| 403 | |
| 404 | impl DerefMut for $mat3 { |
| 405 | #[inline(always)] |
| 406 | fn deref_mut(&mut self) -> &mut Self::Target { |
| 407 | unsafe { &mut *(self as *mut Self as *mut Self::Target) } |
| 408 | } |
| 409 | } |
| 410 | |
| 411 | #[cfg(not(target_arch = "spirv"))] |
| 412 | impl fmt::Display for $mat3 { |
| 413 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 414 | write!(f, "[{}, {}, {}]", self.x_axis, self.y_axis, self.z_axis) |
| 415 | } |
| 416 | } |
| 417 | |
| 418 | #[cfg(not(target_arch = "spirv"))] |
| 419 | impl fmt::Debug for $mat3 { |
| 420 | fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { |
| 421 | fmt.debug_struct("$mat3") |
| 422 | .field("x_axis", &self.x_axis) |
| 423 | .field("y_axis", &self.y_axis) |
| 424 | .field("z_axis", &self.z_axis) |
| 425 | .finish() |
| 426 | } |
| 427 | } |
| 428 | |
| 429 | impl Add<$mat3> for $mat3 { |
| 430 | type Output = Self; |
| 431 | #[inline(always)] |
| 432 | fn add(self, other: Self) -> Self { |
| 433 | self.add_mat3(&other) |
| 434 | } |
| 435 | } |
| 436 | |
| 437 | impl Sub<$mat3> for $mat3 { |
| 438 | type Output = Self; |
| 439 | #[inline(always)] |
| 440 | fn sub(self, other: Self) -> Self { |
| 441 | self.sub_mat3(&other) |
| 442 | } |
| 443 | } |
| 444 | |
| 445 | impl Mul<$mat3> for $mat3 { |
| 446 | type Output = Self; |
| 447 | #[inline(always)] |
| 448 | fn mul(self, other: Self) -> Self { |
| 449 | self.mul_mat3(&other) |
| 450 | } |
| 451 | } |
| 452 | |
| 453 | impl Mul<$vec3> for $mat3 { |
| 454 | type Output = $vec3; |
| 455 | #[inline(always)] |
| 456 | fn mul(self, other: $vec3) -> $vec3 { |
| 457 | self.mul_vec3(other) |
| 458 | } |
| 459 | } |
| 460 | |
| 461 | impl Mul<$mat3> for $t { |
| 462 | type Output = $mat3; |
| 463 | #[inline(always)] |
| 464 | fn mul(self, other: $mat3) -> $mat3 { |
| 465 | other.mul_scalar(self) |
| 466 | } |
| 467 | } |
| 468 | |
| 469 | impl Mul<$t> for $mat3 { |
| 470 | type Output = Self; |
| 471 | #[inline(always)] |
| 472 | fn mul(self, other: $t) -> Self { |
| 473 | self.mul_scalar(other) |
| 474 | } |
| 475 | } |
| 476 | |
| 477 | impl From<$mat4> for $mat3 { |
| 478 | /// Creates a 3x3 matrix from the top left submatrix of the given 4x4 matrix. |
| 479 | fn from(m: $mat4) -> $mat3 { |
| 480 | $mat3::from_cols(m.x_axis.into(), m.y_axis.into(), m.z_axis.into()) |
| 481 | } |
| 482 | } |
| 483 | |
| 484 | #[cfg(feature = "std")] |
| 485 | impl<'a> Sum<&'a Self> for $mat3 { |
| 486 | fn sum<I>(iter: I) -> Self |
| 487 | where |
| 488 | I: Iterator<Item = &'a Self>, |
| 489 | { |
| 490 | iter.fold($mat3::ZERO, |a, &b| Self::add(a, b)) |
| 491 | } |
| 492 | } |
| 493 | |
| 494 | #[cfg(feature = "std")] |
| 495 | impl<'a> Product<&'a Self> for $mat3 { |
| 496 | fn product<I>(iter: I) -> Self |
| 497 | where |
| 498 | I: Iterator<Item = &'a Self>, |
| 499 | { |
| 500 | iter.fold($mat3::IDENTITY, |a, &b| Self::mul(a, b)) |
| 501 | } |
| 502 | } |
| 503 | }; |
| 504 | } |
| 505 | |
| 506 | macro_rules! impl_mat3_traits_unsafe { |
| 507 | ($t:ty, $mat3:ident) => { |
| 508 | impl AsRef<[$t; 9]> for $mat3 { |
| 509 | #[inline(always)] |
| 510 | fn as_ref(&self) -> &[$t; 9] { |
| 511 | unsafe { &*(self as *const Self as *const [$t; 9]) } |
| 512 | } |
| 513 | } |
| 514 | |
| 515 | impl AsMut<[$t; 9]> for $mat3 { |
| 516 | #[inline(always)] |
| 517 | fn as_mut(&mut self) -> &mut [$t; 9] { |
| 518 | unsafe { &mut *(self as *mut Self as *mut [$t; 9]) } |
| 519 | } |
| 520 | } |
| 521 | }; |
| 522 | } |
| 523 | |
| 524 | type InnerF32 = Columns3<XYZ<f32>>; |
| 525 | define_mat3_struct!(Mat3, InnerF32); |
| 526 | |
| 527 | impl Mat3 { |
| 528 | impl_mat3_methods!(f32, Vec3, Vec3, Vec2, Quat, Mat2, InnerF32); |
| 529 | |
| 530 | /// Transforms a `Vec3A`. |
| 531 | #[inline] |
| 532 | pub fn mul_vec3a(&self, other: Vec3A) -> Vec3A { |
| 533 | self.mul_vec3(other.into()).into() |
| 534 | } |
| 535 | |
| 536 | #[inline(always)] |
| 537 | pub fn as_f64(&self) -> DMat3 { |
| 538 | DMat3::from_cols( |
| 539 | self.x_axis.as_f64(), |
| 540 | self.y_axis.as_f64(), |
| 541 | self.z_axis.as_f64(), |
| 542 | ) |
| 543 | } |
| 544 | } |
| 545 | impl_mat3_traits!(f32, mat3, Mat3, Mat4, Vec3, Vec3); |
| 546 | impl_mat3_traits_unsafe!(f32, Mat3); |
| 547 | |
| 548 | impl Mul<Vec3A> for Mat3 { |
| 549 | type Output = Vec3A; |
| 550 | #[inline(always)] |
| 551 | fn mul(self, other: Vec3A) -> Vec3A { |
| 552 | self.mul_vec3a(other) |
| 553 | } |
| 554 | } |
| 555 | |
| 556 | #[cfg(all(target_feature = "sse2", not(feature = "scalar-math")))] |
| 557 | type InnerF32A = Columns3<__m128>; |
| 558 | #[cfg(any(not(target_feature = "sse2"), feature = "scalar-math"))] |
| 559 | type InnerF32A = Columns3<crate::core::storage::XYZF32A16>; |
| 560 | define_mat3_struct!(Mat3A, InnerF32A); |
| 561 | |
| 562 | impl Mat3A { |
| 563 | impl_mat3_methods!(f32, Vec3, Vec3A, Vec2, Quat, Mat2, InnerF32A); |
| 564 | |
| 565 | /// Transforms a `Vec3A`. |
| 566 | #[inline] |
| 567 | pub fn mul_vec3a(&self, other: Vec3A) -> Vec3A { |
| 568 | Vec3A(self.0.mul_vector(other.0)) |
| 569 | } |
| 570 | |
| 571 | #[inline(always)] |
| 572 | pub fn as_f64(&self) -> DMat3 { |
| 573 | DMat3::from_cols( |
| 574 | self.x_axis.as_f64(), |
| 575 | self.y_axis.as_f64(), |
| 576 | self.z_axis.as_f64(), |
| 577 | ) |
| 578 | } |
| 579 | } |
| 580 | impl_mat3_traits!(f32, mat3a, Mat3A, Mat4, Vec3, Vec3A); |
| 581 | |
| 582 | impl Mul<Vec3A> for Mat3A { |
| 583 | type Output = Vec3A; |
| 584 | #[inline(always)] |
| 585 | fn mul(self, other: Vec3A) -> Vec3A { |
| 586 | self.mul_vec3a(other) |
| 587 | } |
| 588 | } |
| 589 | |
| 590 | impl From<Mat3> for Mat3A { |
| 591 | #[inline(always)] |
| 592 | fn from(m: Mat3) -> Self { |
| 593 | Self(m.0.into()) |
| 594 | } |
| 595 | } |
| 596 | |
| 597 | impl From<Mat3A> for Mat3 { |
| 598 | #[inline(always)] |
| 599 | fn from(m: Mat3A) -> Self { |
| 600 | Self(m.0.into()) |
| 601 | } |
| 602 | } |
| 603 | |
| 604 | type InnerF64 = Columns3<XYZ<f64>>; |
| 605 | define_mat3_struct!(DMat3, InnerF64); |
| 606 | |
| 607 | impl DMat3 { |
| 608 | impl_mat3_methods!(f64, DVec3, DVec3, DVec2, DQuat, DMat2, InnerF64); |
| 609 | |
| 610 | #[inline(always)] |
| 611 | pub fn as_f32(&self) -> Mat3 { |
| 612 | Mat3::from_cols( |
| 613 | self.x_axis.as_f32(), |
| 614 | self.y_axis.as_f32(), |
| 615 | self.z_axis.as_f32(), |
| 616 | ) |
| 617 | } |
| 618 | } |
| 619 | impl_mat3_traits!(f64, dmat3, DMat3, DMat4, DVec3, DVec3); |
| 620 | impl_mat3_traits_unsafe!(f64, DMat3); |
| 621 | |
| 622 | mod const_test_mat3 { |
| 623 | const_assert_eq!(4, core::mem::align_of::<super::Mat3>()); |
| 624 | const_assert_eq!(36, core::mem::size_of::<super::Mat3>()); |
| 625 | } |
| 626 | |
| 627 | mod const_test_mat3a { |
| 628 | const_assert_eq!(16, core::mem::align_of::<super::Mat3A>()); |
| 629 | const_assert_eq!(48, core::mem::size_of::<super::Mat3A>()); |
| 630 | } |
| 631 | |
| 632 | mod const_test_dmat3 { |
| 633 | const_assert_eq!(8, core::mem::align_of::<super::DMat3>()); |
| 634 | const_assert_eq!(72, core::mem::size_of::<super::DMat3>()); |
| 635 | } |